| 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" | |
| 11 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" | 9 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" |
| 10 #include "third_party/webrtc/api/mediastreaminterface.h" |
| 12 | 11 |
| 13 namespace content { | 12 namespace content { |
| 14 | 13 |
| 15 MediaStreamAudioTrack::MediaStreamAudioTrack(bool is_local_track) | 14 MediaStreamAudioTrack::MediaStreamAudioTrack(bool is_local_track) |
| 16 : MediaStreamTrack(is_local_track), is_enabled_(1), weak_factory_(this) { | 15 : MediaStreamTrack(is_local_track) { |
| 17 DVLOG(1) << "MediaStreamAudioTrack@" << this << "::MediaStreamAudioTrack(" | 16 DVLOG(1) << "MediaStreamAudioTrack::MediaStreamAudioTrack(is a " |
| 18 << (is_local_track ? "local" : "remote") << " track)"; | 17 << (is_local_track ? "local" : "remote") << " track)"; |
| 19 } | 18 } |
| 20 | 19 |
| 21 MediaStreamAudioTrack::~MediaStreamAudioTrack() { | 20 MediaStreamAudioTrack::~MediaStreamAudioTrack() { |
| 22 DCHECK(thread_checker_.CalledOnValidThread()); | 21 DCHECK(main_render_thread_checker_.CalledOnValidThread()); |
| 23 DVLOG(1) << "MediaStreamAudioTrack@" << this << " is being destroyed."; | 22 DVLOG(1) << "MediaStreamAudioTrack::~MediaStreamAudioTrack()"; |
| 24 Stop(); | 23 DCHECK(stop_callback_.is_null()) |
| 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 | |
| 85 void MediaStreamAudioTrack::Start(const base::Closure& stop_callback) { | 37 void MediaStreamAudioTrack::Start(const base::Closure& stop_callback) { |
| 86 DCHECK(thread_checker_.CalledOnValidThread()); | 38 DCHECK(main_render_thread_checker_.CalledOnValidThread()); |
| 87 DCHECK(!stop_callback.is_null()); | 39 DCHECK(!stop_callback.is_null()); |
| 88 DCHECK(stop_callback_.is_null()); | 40 DCHECK(stop_callback_.is_null()); |
| 89 DVLOG(1) << "Starting MediaStreamAudioTrack@" << this << '.'; | 41 DVLOG(1) << "MediaStreamAudioTrack::Start()"; |
| 90 stop_callback_ = stop_callback; | 42 stop_callback_ = stop_callback; |
| 91 } | 43 } |
| 92 | 44 |
| 93 void MediaStreamAudioTrack::Stop() { | 45 void MediaStreamAudioTrack::Stop() { |
| 94 DCHECK(thread_checker_.CalledOnValidThread()); | 46 DCHECK(main_render_thread_checker_.CalledOnValidThread()); |
| 95 DVLOG(1) << "Stopping MediaStreamAudioTrack@" << this << '.'; | 47 DVLOG(1) << "MediaStreamAudioTrack::Stop()"; |
| 96 | |
| 97 if (!stop_callback_.is_null()) | 48 if (!stop_callback_.is_null()) |
| 98 base::ResetAndReturn(&stop_callback_).Run(); | 49 base::ResetAndReturn(&stop_callback_).Run(); |
| 99 | 50 OnStop(); |
| 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(); | |
| 108 } | 51 } |
| 109 | 52 |
| 110 void MediaStreamAudioTrack::OnSetFormat(const media::AudioParameters& params) { | 53 void MediaStreamAudioTrack::OnStop() {} |
| 111 deliverer_.OnSetFormat(params); | |
| 112 } | |
| 113 | 54 |
| 114 void MediaStreamAudioTrack::OnData(const media::AudioBus& audio_bus, | 55 webrtc::AudioTrackInterface* MediaStreamAudioTrack::GetAudioAdapter() { |
| 115 base::TimeTicks reference_time) { | 56 NOTREACHED(); |
| 116 // Note: Using NoBarrier_Load because the timing of when the audio thread sees | 57 return nullptr; |
| 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 } | |
| 132 } | 58 } |
| 133 | 59 |
| 134 } // namespace content | 60 } // namespace content |
| OLD | NEW |