| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/webrtc/media_stream_remote_audio_track.h" | 5 #include "content/renderer/media/webrtc/peer_connection_remote_audio_source.h" |
| 6 | |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include <list> | |
| 10 | 6 |
| 11 #include "base/logging.h" | 7 #include "base/logging.h" |
| 12 #include "content/public/renderer/media_stream_audio_sink.h" | 8 #include "base/time/time.h" |
| 13 #include "third_party/webrtc/api/mediastreaminterface.h" | 9 #include "media/base/audio_bus.h" |
| 14 | 10 |
| 15 namespace content { | 11 namespace content { |
| 16 | 12 |
| 17 class MediaStreamRemoteAudioSource::AudioSink | 13 namespace { |
| 18 : public webrtc::AudioTrackSinkInterface { | 14 // Used as an identifier for the down-casters. |
| 19 public: | 15 void* const kClassIdentifier = const_cast<void**>(&kClassIdentifier); |
| 20 AudioSink() { | 16 } // namespace |
| 21 } | |
| 22 ~AudioSink() override { | |
| 23 DCHECK(sinks_.empty()); | |
| 24 } | |
| 25 | 17 |
| 26 void Add(MediaStreamAudioSink* sink, MediaStreamAudioTrack* track, | 18 PeerConnectionRemoteAudioTrack::PeerConnectionRemoteAudioTrack( |
| 27 bool enabled) { | 19 scoped_refptr<webrtc::AudioTrackInterface> track_interface) |
| 28 DCHECK(thread_checker_.CalledOnValidThread()); | 20 : MediaStreamAudioTrack(false /* is_local_track */), |
| 29 SinkInfo info(sink, track, enabled); | 21 track_interface_(std::move(track_interface)) { |
| 30 base::AutoLock lock(lock_); | 22 DVLOG(1) |
| 31 sinks_.push_back(info); | 23 << "PeerConnectionRemoteAudioTrack::PeerConnectionRemoteAudioTrack()"; |
| 32 } | |
| 33 | |
| 34 void Remove(MediaStreamAudioSink* sink, MediaStreamAudioTrack* track) { | |
| 35 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 36 base::AutoLock lock(lock_); | |
| 37 sinks_.remove_if([&sink, &track](const SinkInfo& info) { | |
| 38 return info.sink == sink && info.track == track; | |
| 39 }); | |
| 40 } | |
| 41 | |
| 42 void SetEnabled(MediaStreamAudioTrack* track, bool enabled) { | |
| 43 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 44 base::AutoLock lock(lock_); | |
| 45 for (SinkInfo& info : sinks_) { | |
| 46 if (info.track == track) | |
| 47 info.enabled = enabled; | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 void RemoveAll(MediaStreamAudioTrack* track) { | |
| 52 base::AutoLock lock(lock_); | |
| 53 sinks_.remove_if([&track](const SinkInfo& info) { | |
| 54 return info.track == track; | |
| 55 }); | |
| 56 } | |
| 57 | |
| 58 bool IsNeeded() const { | |
| 59 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 60 return !sinks_.empty(); | |
| 61 } | |
| 62 | |
| 63 private: | |
| 64 void OnData(const void* audio_data, int bits_per_sample, int sample_rate, | |
| 65 size_t number_of_channels, size_t number_of_frames) override { | |
| 66 if (!audio_bus_ || | |
| 67 static_cast<size_t>(audio_bus_->channels()) != number_of_channels || | |
| 68 static_cast<size_t>(audio_bus_->frames()) != number_of_frames) { | |
| 69 audio_bus_ = media::AudioBus::Create(number_of_channels, | |
| 70 number_of_frames); | |
| 71 } | |
| 72 | |
| 73 audio_bus_->FromInterleaved(audio_data, number_of_frames, | |
| 74 bits_per_sample / 8); | |
| 75 | |
| 76 bool format_changed = false; | |
| 77 if (params_.format() != media::AudioParameters::AUDIO_PCM_LOW_LATENCY || | |
| 78 static_cast<size_t>(params_.channels()) != number_of_channels || | |
| 79 params_.sample_rate() != sample_rate || | |
| 80 static_cast<size_t>(params_.frames_per_buffer()) != number_of_frames) { | |
| 81 params_ = media::AudioParameters( | |
| 82 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | |
| 83 media::GuessChannelLayout(number_of_channels), | |
| 84 sample_rate, 16, number_of_frames); | |
| 85 format_changed = true; | |
| 86 } | |
| 87 | |
| 88 // TODO(tommi): We should get the timestamp from WebRTC. | |
| 89 base::TimeTicks estimated_capture_time(base::TimeTicks::Now()); | |
| 90 | |
| 91 base::AutoLock lock(lock_); | |
| 92 for (const SinkInfo& info : sinks_) { | |
| 93 if (info.enabled) { | |
| 94 if (format_changed) | |
| 95 info.sink->OnSetFormat(params_); | |
| 96 info.sink->OnData(*audio_bus_.get(), estimated_capture_time); | |
| 97 } | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 mutable base::Lock lock_; | |
| 102 struct SinkInfo { | |
| 103 SinkInfo(MediaStreamAudioSink* sink, MediaStreamAudioTrack* track, | |
| 104 bool enabled) : sink(sink), track(track), enabled(enabled) {} | |
| 105 MediaStreamAudioSink* sink; | |
| 106 MediaStreamAudioTrack* track; | |
| 107 bool enabled; | |
| 108 }; | |
| 109 std::list<SinkInfo> sinks_; | |
| 110 base::ThreadChecker thread_checker_; | |
| 111 media::AudioParameters params_; // Only used on the callback thread. | |
| 112 scoped_ptr<media::AudioBus> audio_bus_; // Only used on the callback thread. | |
| 113 }; | |
| 114 | |
| 115 MediaStreamRemoteAudioTrack::MediaStreamRemoteAudioTrack( | |
| 116 const blink::WebMediaStreamSource& source, bool enabled) | |
| 117 : MediaStreamAudioTrack(false), source_(source), enabled_(enabled) { | |
| 118 DCHECK(source.getExtraData()); // Make sure the source has a native source. | |
| 119 } | 24 } |
| 120 | 25 |
| 121 MediaStreamRemoteAudioTrack::~MediaStreamRemoteAudioTrack() { | 26 PeerConnectionRemoteAudioTrack::~PeerConnectionRemoteAudioTrack() { |
| 122 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 27 DVLOG(1) |
| 28 << "PeerConnectionRemoteAudioTrack::~PeerConnectionRemoteAudioTrack()"; |
| 123 // Ensure the track is stopped. | 29 // Ensure the track is stopped. |
| 124 MediaStreamAudioTrack::Stop(); | 30 MediaStreamAudioTrack::Stop(); |
| 125 } | 31 } |
| 126 | 32 |
| 127 void MediaStreamRemoteAudioTrack::SetEnabled(bool enabled) { | 33 // static |
| 34 PeerConnectionRemoteAudioTrack* PeerConnectionRemoteAudioTrack::From( |
| 35 MediaStreamAudioTrack* track) { |
| 36 if (track && track->GetClassIdentifier() == kClassIdentifier) |
| 37 return static_cast<PeerConnectionRemoteAudioTrack*>(track); |
| 38 return nullptr; |
| 39 } |
| 40 |
| 41 void PeerConnectionRemoteAudioTrack::SetEnabled(bool enabled) { |
| 128 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 42 DCHECK(main_render_thread_checker_.CalledOnValidThread()); |
| 129 | 43 |
| 130 // This affects the shared state of the source for whether or not it's a part | 44 // This affects the shared state of the source for whether or not it's a part |
| 131 // of the mixed audio that's rendered for remote tracks from WebRTC. | 45 // of the mixed audio that's rendered for remote tracks from WebRTC. |
| 132 // All tracks from the same source will share this state and thus can step | 46 // All tracks from the same source will share this state and thus can step |
| 133 // on each other's toes. | 47 // on each other's toes. |
| 134 // This is also why we can't check the |enabled_| state for equality with | 48 // This is also why we can't check the enabled state for equality with |
| 135 // |enabled| before setting the mixing enabled state. |enabled_| and the | 49 // |enabled| before setting the mixing enabled state. This track's enabled |
| 136 // shared state might not be the same. | 50 // state and the shared state might not be the same. |
| 137 source()->SetEnabledForMixing(enabled); | 51 track_interface_->set_enabled(enabled); |
| 138 | 52 |
| 139 enabled_ = enabled; | 53 MediaStreamAudioTrack::SetEnabled(enabled); |
| 140 source()->SetSinksEnabled(this, enabled); | |
| 141 } | 54 } |
| 142 | 55 |
| 143 void MediaStreamRemoteAudioTrack::OnStop() { | 56 void* PeerConnectionRemoteAudioTrack::GetClassIdentifier() const { |
| 57 return kClassIdentifier; |
| 58 } |
| 59 |
| 60 void PeerConnectionRemoteAudioTrack::OnStop() { |
| 144 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 61 DCHECK(main_render_thread_checker_.CalledOnValidThread()); |
| 145 DVLOG(1) << "MediaStreamRemoteAudioTrack::OnStop()"; | 62 DVLOG(1) << "PeerConnectionRemoteAudioTrack::OnStop()"; |
| 146 | |
| 147 source()->RemoveAll(this); | |
| 148 | 63 |
| 149 // Stop means that a track should be stopped permanently. But | 64 // Stop means that a track should be stopped permanently. But |
| 150 // since there is no proper way of doing that on a remote track, we can | 65 // since there is no proper way of doing that on a remote track, we can |
| 151 // at least disable the track. Blink will not call down to the content layer | 66 // at least disable the track. Blink will not call down to the content layer |
| 152 // after a track has been stopped. | 67 // after a track has been stopped. |
| 153 SetEnabled(false); | 68 SetEnabled(false); |
| 154 } | 69 } |
| 155 | 70 |
| 156 void MediaStreamRemoteAudioTrack::AddSink(MediaStreamAudioSink* sink) { | 71 PeerConnectionRemoteAudioSource::PeerConnectionRemoteAudioSource( |
| 157 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 72 scoped_refptr<webrtc::AudioTrackInterface> track_interface) |
| 158 return source()->AddSink(sink, this, enabled_); | 73 : MediaStreamAudioSource(false /* is_local_source */), |
| 74 track_interface_(std::move(track_interface)), |
| 75 is_sink_of_peer_connection_(false) { |
| 76 DCHECK(track_interface_); |
| 77 DVLOG(1) |
| 78 << "PeerConnectionRemoteAudioSource::PeerConnectionRemoteAudioSource()"; |
| 159 } | 79 } |
| 160 | 80 |
| 161 void MediaStreamRemoteAudioTrack::RemoveSink(MediaStreamAudioSink* sink) { | 81 PeerConnectionRemoteAudioSource::~PeerConnectionRemoteAudioSource() { |
| 162 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 82 DVLOG(1) |
| 163 return source()->RemoveSink(sink, this); | 83 << "PeerConnectionRemoteAudioSource::~PeerConnectionRemoteAudioSource()"; |
| 84 // Ensure the source is stopped. |
| 85 MediaStreamAudioSource::StopSource(); |
| 164 } | 86 } |
| 165 | 87 |
| 166 media::AudioParameters MediaStreamRemoteAudioTrack::GetOutputFormat() const { | 88 scoped_ptr<MediaStreamAudioTrack> |
| 167 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 89 PeerConnectionRemoteAudioSource::CreateMediaStreamAudioTrack( |
| 168 // This method is not implemented on purpose and should be removed. | 90 const std::string& id) { |
| 169 // TODO(tommi): See comment for GetOutputFormat in MediaStreamAudioTrack. | 91 DCHECK(thread_checker_.CalledOnValidThread()); |
| 170 NOTIMPLEMENTED(); | 92 return scoped_ptr<MediaStreamAudioTrack>( |
| 171 return media::AudioParameters(); | 93 new PeerConnectionRemoteAudioTrack(track_interface_)); |
| 172 } | 94 } |
| 173 | 95 |
| 174 webrtc::AudioTrackInterface* MediaStreamRemoteAudioTrack::GetAudioAdapter() { | 96 bool PeerConnectionRemoteAudioSource::EnsureSourceIsStarted() { |
| 175 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 97 DCHECK(thread_checker_.CalledOnValidThread()); |
| 176 return source()->GetAudioAdapter(); | 98 |
| 99 if (is_stopped_) |
| 100 return false; |
| 101 if (is_sink_of_peer_connection_) |
| 102 return true; |
| 103 |
| 104 VLOG(1) << "Starting PeerConnection remote audio source with id=" |
| 105 << track_interface_->id(); |
| 106 track_interface_->AddSink(this); |
| 107 is_sink_of_peer_connection_ = true; |
| 108 return true; |
| 177 } | 109 } |
| 178 | 110 |
| 179 MediaStreamRemoteAudioSource* MediaStreamRemoteAudioTrack::source() const { | 111 void PeerConnectionRemoteAudioSource::DoStopSource() { |
| 180 return static_cast<MediaStreamRemoteAudioSource*>(source_.getExtraData()); | 112 DCHECK(thread_checker_.CalledOnValidThread()); |
| 113 if (is_stopped_) |
| 114 return; |
| 115 if (is_sink_of_peer_connection_) { |
| 116 track_interface_->RemoveSink(this); |
| 117 is_sink_of_peer_connection_ = false; |
| 118 VLOG(1) << "Stopped PeerConnection remote audio source with id=" |
| 119 << track_interface_->id(); |
| 120 } |
| 121 is_stopped_ = true; |
| 181 } | 122 } |
| 182 | 123 |
| 183 MediaStreamRemoteAudioSource::MediaStreamRemoteAudioSource( | 124 void PeerConnectionRemoteAudioSource::OnData(const void* audio_data, |
| 184 const scoped_refptr<webrtc::AudioTrackInterface>& track) : track_(track) {} | 125 int bits_per_sample, |
| 126 int sample_rate, |
| 127 size_t number_of_channels, |
| 128 size_t number_of_frames) { |
| 129 // TODO(tommi): We should get the timestamp from WebRTC. |
| 130 base::TimeTicks playout_time(base::TimeTicks::Now()); |
| 185 | 131 |
| 186 MediaStreamRemoteAudioSource::~MediaStreamRemoteAudioSource() { | 132 if (!audio_bus_ || |
| 187 DCHECK(thread_checker_.CalledOnValidThread()); | 133 static_cast<size_t>(audio_bus_->channels()) != number_of_channels || |
| 188 } | 134 static_cast<size_t>(audio_bus_->frames()) != number_of_frames) { |
| 189 | 135 audio_bus_ = media::AudioBus::Create(number_of_channels, number_of_frames); |
| 190 void MediaStreamRemoteAudioSource::SetEnabledForMixing(bool enabled) { | |
| 191 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 192 track_->set_enabled(enabled); | |
| 193 } | |
| 194 | |
| 195 void MediaStreamRemoteAudioSource::AddSink(MediaStreamAudioSink* sink, | |
| 196 MediaStreamAudioTrack* track, | |
| 197 bool enabled) { | |
| 198 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 199 if (!sink_) { | |
| 200 sink_.reset(new AudioSink()); | |
| 201 track_->AddSink(sink_.get()); | |
| 202 } | 136 } |
| 203 | 137 |
| 204 sink_->Add(sink, track, enabled); | 138 audio_bus_->FromInterleaved(audio_data, number_of_frames, |
| 205 } | 139 bits_per_sample / 8); |
| 206 | 140 |
| 207 void MediaStreamRemoteAudioSource::RemoveSink(MediaStreamAudioSink* sink, | 141 media::AudioParameters params = MediaStreamAudioSource::GetAudioParameters(); |
| 208 MediaStreamAudioTrack* track) { | 142 if (!params.IsValid() || |
| 209 DCHECK(thread_checker_.CalledOnValidThread()); | 143 params.format() != media::AudioParameters::AUDIO_PCM_LOW_LATENCY || |
| 210 DCHECK(sink_); | 144 static_cast<size_t>(params.channels()) != number_of_channels || |
| 145 params.sample_rate() != sample_rate || |
| 146 static_cast<size_t>(params.frames_per_buffer()) != number_of_frames) { |
| 147 MediaStreamAudioSource::SetFormat( |
| 148 media::AudioParameters(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, |
| 149 media::GuessChannelLayout(number_of_channels), |
| 150 sample_rate, bits_per_sample, number_of_frames)); |
| 151 } |
| 211 | 152 |
| 212 sink_->Remove(sink, track); | 153 MediaStreamAudioSource::DeliverDataToTracks(*audio_bus_, playout_time); |
| 213 | |
| 214 if (!sink_->IsNeeded()) { | |
| 215 track_->RemoveSink(sink_.get()); | |
| 216 sink_.reset(); | |
| 217 } | |
| 218 } | |
| 219 | |
| 220 void MediaStreamRemoteAudioSource::SetSinksEnabled(MediaStreamAudioTrack* track, | |
| 221 bool enabled) { | |
| 222 if (sink_) | |
| 223 sink_->SetEnabled(track, enabled); | |
| 224 } | |
| 225 | |
| 226 void MediaStreamRemoteAudioSource::RemoveAll(MediaStreamAudioTrack* track) { | |
| 227 if (sink_) | |
| 228 sink_->RemoveAll(track); | |
| 229 } | |
| 230 | |
| 231 webrtc::AudioTrackInterface* MediaStreamRemoteAudioSource::GetAudioAdapter() { | |
| 232 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 233 return track_.get(); | |
| 234 } | 154 } |
| 235 | 155 |
| 236 } // namespace content | 156 } // namespace content |
| OLD | NEW |