OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/media/media_stream_audio_track.h" | 5 #include "content/renderer/media/media_stream_audio_track.h" |
6 | 6 |
7 #include "base/callback_helpers.h" | 7 #include "base/callback_helpers.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "content/public/renderer/media_stream_audio_sink.h" |
| 10 #include "media/base/audio_bus.h" |
9 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" | 11 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" |
10 #include "third_party/webrtc/api/mediastreaminterface.h" | |
11 | 12 |
12 namespace content { | 13 namespace content { |
13 | 14 |
14 MediaStreamAudioTrack::MediaStreamAudioTrack(bool is_local_track) | 15 MediaStreamAudioTrack::MediaStreamAudioTrack(bool is_local_track) |
15 : MediaStreamTrack(is_local_track) { | 16 : MediaStreamTrack(is_local_track), is_enabled_(1), weak_factory_(this) { |
16 DVLOG(1) << "MediaStreamAudioTrack::MediaStreamAudioTrack(is a " | 17 DVLOG(1) << "MediaStreamAudioTrack@" << this << "::MediaStreamAudioTrack(" |
17 << (is_local_track ? "local" : "remote") << " track)"; | 18 << (is_local_track ? "local" : "remote") << " track)"; |
18 } | 19 } |
19 | 20 |
20 MediaStreamAudioTrack::~MediaStreamAudioTrack() { | 21 MediaStreamAudioTrack::~MediaStreamAudioTrack() { |
21 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 22 DCHECK(thread_checker_.CalledOnValidThread()); |
22 DVLOG(1) << "MediaStreamAudioTrack::~MediaStreamAudioTrack()"; | 23 DVLOG(1) << "MediaStreamAudioTrack@" << this << " is being destroyed."; |
23 DCHECK(stop_callback_.is_null()) | 24 Stop(); |
24 << "BUG: Subclass must ensure Stop() is called."; | |
25 } | 25 } |
26 | 26 |
27 // static | 27 // static |
28 MediaStreamAudioTrack* MediaStreamAudioTrack::From( | 28 MediaStreamAudioTrack* MediaStreamAudioTrack::From( |
29 const blink::WebMediaStreamTrack& track) { | 29 const blink::WebMediaStreamTrack& track) { |
30 if (track.isNull() || | 30 if (track.isNull() || |
31 track.source().getType() != blink::WebMediaStreamSource::TypeAudio) { | 31 track.source().getType() != blink::WebMediaStreamSource::TypeAudio) { |
32 return nullptr; | 32 return nullptr; |
33 } | 33 } |
34 return static_cast<MediaStreamAudioTrack*>(track.getExtraData()); | 34 return static_cast<MediaStreamAudioTrack*>(track.getExtraData()); |
35 } | 35 } |
36 | 36 |
| 37 void MediaStreamAudioTrack::AddSink(MediaStreamAudioSink* sink) { |
| 38 DCHECK(thread_checker_.CalledOnValidThread()); |
| 39 |
| 40 DVLOG(1) << "Adding MediaStreamAudioSink@" << sink |
| 41 << " to MediaStreamAudioTrack@" << this << '.'; |
| 42 |
| 43 // If the track has already stopped, just notify the sink of this fact without |
| 44 // adding it. |
| 45 if (stop_callback_.is_null()) { |
| 46 sink->OnReadyStateChanged(blink::WebMediaStreamSource::ReadyStateEnded); |
| 47 return; |
| 48 } |
| 49 |
| 50 deliverer_.AddConsumer(sink); |
| 51 sink->OnEnabledChanged(!!base::subtle::NoBarrier_Load(&is_enabled_)); |
| 52 } |
| 53 |
| 54 void MediaStreamAudioTrack::RemoveSink(MediaStreamAudioSink* sink) { |
| 55 DCHECK(thread_checker_.CalledOnValidThread()); |
| 56 deliverer_.RemoveConsumer(sink); |
| 57 DVLOG(1) << "Removed MediaStreamAudioSink@" << sink |
| 58 << " from MediaStreamAudioTrack@" << this << '.'; |
| 59 } |
| 60 |
| 61 media::AudioParameters MediaStreamAudioTrack::GetOutputFormat() const { |
| 62 return deliverer_.GetAudioParameters(); |
| 63 } |
| 64 |
| 65 void MediaStreamAudioTrack::SetEnabled(bool enabled) { |
| 66 DCHECK(thread_checker_.CalledOnValidThread()); |
| 67 DVLOG(1) << "MediaStreamAudioTrack@" << this << "::SetEnabled(" |
| 68 << (enabled ? 'Y' : 'N') << ')'; |
| 69 |
| 70 const bool previously_enabled = |
| 71 !!base::subtle::NoBarrier_AtomicExchange(&is_enabled_, enabled ? 1 : 0); |
| 72 if (enabled == previously_enabled) |
| 73 return; |
| 74 |
| 75 std::vector<MediaStreamAudioSink*> sinks_to_notify; |
| 76 deliverer_.GetConsumerList(&sinks_to_notify); |
| 77 for (MediaStreamAudioSink* sink : sinks_to_notify) |
| 78 sink->OnEnabledChanged(enabled); |
| 79 } |
| 80 |
| 81 void* MediaStreamAudioTrack::GetClassIdentifier() const { |
| 82 return nullptr; |
| 83 } |
| 84 |
37 void MediaStreamAudioTrack::Start(const base::Closure& stop_callback) { | 85 void MediaStreamAudioTrack::Start(const base::Closure& stop_callback) { |
38 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 86 DCHECK(thread_checker_.CalledOnValidThread()); |
39 DCHECK(!stop_callback.is_null()); | 87 DCHECK(!stop_callback.is_null()); |
40 DCHECK(stop_callback_.is_null()); | 88 DCHECK(stop_callback_.is_null()); |
41 DVLOG(1) << "MediaStreamAudioTrack::Start()"; | 89 DVLOG(1) << "Starting MediaStreamAudioTrack@" << this << '.'; |
42 stop_callback_ = stop_callback; | 90 stop_callback_ = stop_callback; |
43 } | 91 } |
44 | 92 |
45 void MediaStreamAudioTrack::Stop() { | 93 void MediaStreamAudioTrack::Stop() { |
46 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 94 DCHECK(thread_checker_.CalledOnValidThread()); |
47 DVLOG(1) << "MediaStreamAudioTrack::Stop()"; | 95 DVLOG(1) << "Stopping MediaStreamAudioTrack@" << this << '.'; |
| 96 |
48 if (!stop_callback_.is_null()) | 97 if (!stop_callback_.is_null()) |
49 base::ResetAndReturn(&stop_callback_).Run(); | 98 base::ResetAndReturn(&stop_callback_).Run(); |
50 OnStop(); | 99 |
| 100 std::vector<MediaStreamAudioSink*> sinks_to_end; |
| 101 deliverer_.GetConsumerList(&sinks_to_end); |
| 102 for (MediaStreamAudioSink* sink : sinks_to_end) { |
| 103 deliverer_.RemoveConsumer(sink); |
| 104 sink->OnReadyStateChanged(blink::WebMediaStreamSource::ReadyStateEnded); |
| 105 } |
| 106 |
| 107 weak_factory_.InvalidateWeakPtrs(); |
51 } | 108 } |
52 | 109 |
53 void MediaStreamAudioTrack::OnStop() {} | 110 void MediaStreamAudioTrack::OnSetFormat(const media::AudioParameters& params) { |
| 111 deliverer_.OnSetFormat(params); |
| 112 } |
54 | 113 |
55 webrtc::AudioTrackInterface* MediaStreamAudioTrack::GetAudioAdapter() { | 114 void MediaStreamAudioTrack::OnData(const media::AudioBus& audio_bus, |
56 NOTREACHED(); | 115 base::TimeTicks reference_time) { |
57 return nullptr; | 116 // Note: Using NoBarrier_Load because the timing of when the audio thread sees |
| 117 // a changed |is_enabled_| value can be relaxed. |
| 118 const bool deliver_data = !!base::subtle::NoBarrier_Load(&is_enabled_); |
| 119 |
| 120 if (deliver_data) { |
| 121 deliverer_.OnData(audio_bus, reference_time); |
| 122 } else { |
| 123 // The W3C spec requires silent audio to flow while a track is disabled. |
| 124 if (!silent_bus_ || silent_bus_->channels() != audio_bus.channels() || |
| 125 silent_bus_->frames() != audio_bus.frames()) { |
| 126 silent_bus_ = media::AudioBus::Create(audio_bus.channels(), |
| 127 audio_bus.frames()); |
| 128 silent_bus_->Zero(); |
| 129 } |
| 130 deliverer_.OnData(*silent_bus_, reference_time); |
| 131 } |
58 } | 132 } |
59 | 133 |
60 } // namespace content | 134 } // namespace content |
OLD | NEW |