OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/browser/renderer_host/media/audio_renderer_host.h" | 5 #include "content/browser/renderer_host/media/audio_renderer_host.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
127 !!RenderFrameHost::FromID(render_process_id, render_frame_id); | 127 !!RenderFrameHost::FromID(render_process_id, render_frame_id); |
128 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 128 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
129 base::Bind(callback, frame_exists)); | 129 base::Bind(callback, frame_exists)); |
130 } | 130 } |
131 | 131 |
132 } // namespace | 132 } // namespace |
133 | 133 |
134 class AudioRendererHost::AudioEntry | 134 class AudioRendererHost::AudioEntry |
135 : public media::AudioOutputController::EventHandler { | 135 : public media::AudioOutputController::EventHandler { |
136 public: | 136 public: |
137 AudioEntry(AudioRendererHost* host, | |
138 int stream_id, | |
139 int render_frame_id, | |
140 const media::AudioParameters& params, | |
141 const std::string& output_device_id, | |
142 std::unique_ptr<base::SharedMemory> shared_memory, | |
143 std::unique_ptr<media::AudioOutputController::SyncReader> reader); | |
144 ~AudioEntry() override; | 137 ~AudioEntry() override; |
145 | 138 |
139 // Returns nullptr on failure. | |
140 static std::unique_ptr<AudioRendererHost::AudioEntry> Create( | |
141 AudioRendererHost* host, | |
142 int stream_id, | |
143 int render_frame_id, | |
144 const media::AudioParameters& params, | |
145 const std::string& output_device_id); | |
146 | |
146 int stream_id() const { | 147 int stream_id() const { |
147 return stream_id_; | 148 return stream_id_; |
148 } | 149 } |
149 | 150 |
150 int render_frame_id() const { return render_frame_id_; } | 151 int render_frame_id() const { return render_frame_id_; } |
151 | 152 |
152 media::AudioOutputController* controller() const { return controller_.get(); } | 153 media::AudioOutputController* controller() const { return controller_.get(); } |
153 | 154 |
154 base::SharedMemory* shared_memory() { | 155 AudioSyncReader* reader() const { return reader_.get(); } |
155 return shared_memory_.get(); | |
156 } | |
157 | 156 |
158 media::AudioOutputController::SyncReader* reader() const { | 157 // Used by ARH to track the number of active streams for UMA stats. |
159 return reader_.get(); | |
160 } | |
161 | |
162 bool playing() const { return playing_; } | 158 bool playing() const { return playing_; } |
163 void set_playing(bool playing) { playing_ = playing; } | 159 void set_playing(bool playing) { playing_ = playing; } |
164 | 160 |
165 private: | 161 private: |
162 AudioEntry(AudioRendererHost* host, | |
163 int stream_id, | |
164 int render_frame_id, | |
165 const media::AudioParameters& params, | |
166 const std::string& output_device_id, | |
167 std::unique_ptr<AudioSyncReader> reader); | |
168 | |
166 // media::AudioOutputController::EventHandler implementation. | 169 // media::AudioOutputController::EventHandler implementation. |
167 void OnCreated() override; | 170 void OnCreated() override; |
168 void OnPlaying() override; | 171 void OnPlaying() override; |
169 void OnPaused() override; | 172 void OnPaused() override; |
170 void OnError() override; | 173 void OnError() override; |
171 | 174 |
172 AudioRendererHost* const host_; | 175 AudioRendererHost* const host_; |
173 const int stream_id_; | 176 const int stream_id_; |
174 | 177 |
175 // The routing ID of the source RenderFrame. | 178 // The routing ID of the source RenderFrame. |
176 const int render_frame_id_; | 179 const int render_frame_id_; |
177 | 180 |
178 // Shared memory for transmission of the audio data. Used by |reader_|. | |
179 const std::unique_ptr<base::SharedMemory> shared_memory_; | |
180 | |
181 // The synchronous reader to be used by |controller_|. | 181 // The synchronous reader to be used by |controller_|. |
182 const std::unique_ptr<media::AudioOutputController::SyncReader> reader_; | 182 const std::unique_ptr<AudioSyncReader> reader_; |
183 | 183 |
184 // The AudioOutputController that manages the audio stream. | 184 // The AudioOutputController that manages the audio stream. |
185 const scoped_refptr<media::AudioOutputController> controller_; | 185 const scoped_refptr<media::AudioOutputController> controller_; |
186 | 186 |
187 bool playing_; | 187 bool playing_; |
188 | |
189 DISALLOW_COPY_AND_ASSIGN(AudioEntry); | |
188 }; | 190 }; |
189 | 191 |
190 AudioRendererHost::AudioEntry::AudioEntry( | 192 AudioRendererHost::AudioEntry::AudioEntry( |
191 AudioRendererHost* host, | 193 AudioRendererHost* host, |
192 int stream_id, | 194 int stream_id, |
193 int render_frame_id, | 195 int render_frame_id, |
194 const media::AudioParameters& params, | 196 const media::AudioParameters& params, |
195 const std::string& output_device_id, | 197 const std::string& output_device_id, |
196 std::unique_ptr<base::SharedMemory> shared_memory, | 198 std::unique_ptr<AudioSyncReader> reader) |
197 std::unique_ptr<media::AudioOutputController::SyncReader> reader) | |
198 : host_(host), | 199 : host_(host), |
199 stream_id_(stream_id), | 200 stream_id_(stream_id), |
200 render_frame_id_(render_frame_id), | 201 render_frame_id_(render_frame_id), |
201 shared_memory_(std::move(shared_memory)), | |
202 reader_(std::move(reader)), | 202 reader_(std::move(reader)), |
203 controller_(media::AudioOutputController::Create(host->audio_manager_, | 203 controller_(media::AudioOutputController::Create(host->audio_manager_, |
204 this, | 204 this, |
205 params, | 205 params, |
206 output_device_id, | 206 output_device_id, |
207 reader_.get())), | 207 reader_.get())), |
208 playing_(false) { | 208 playing_(false) { |
209 DCHECK(controller_.get()); | 209 DCHECK(controller_); |
210 } | 210 } |
211 | 211 |
212 AudioRendererHost::AudioEntry::~AudioEntry() {} | 212 AudioRendererHost::AudioEntry::~AudioEntry() {} |
213 | 213 |
214 // static | |
215 std::unique_ptr<AudioRendererHost::AudioEntry> | |
216 AudioRendererHost::AudioEntry::Create(AudioRendererHost* host, | |
217 int stream_id, | |
218 int render_frame_id, | |
219 const media::AudioParameters& params, | |
220 const std::string& output_device_id) { | |
221 std::unique_ptr<AudioSyncReader> reader(AudioSyncReader::Create(params)); | |
222 if (!reader) { | |
223 return {}; | |
224 } | |
225 return std::unique_ptr<AudioEntry>( | |
226 new AudioEntry(host, stream_id, render_frame_id, params, output_device_id, | |
o1ka
2016/09/16 10:51:59
(you can use base::WrapUnique instead as well
http
Max Morin
2016/09/16 12:46:15
Done.
| |
227 std::move(reader))); | |
228 } | |
229 | |
214 /////////////////////////////////////////////////////////////////////////////// | 230 /////////////////////////////////////////////////////////////////////////////// |
215 // AudioRendererHost implementations. | 231 // AudioRendererHost implementations. |
216 | 232 |
217 AudioRendererHost::AudioRendererHost(int render_process_id, | 233 AudioRendererHost::AudioRendererHost(int render_process_id, |
218 media::AudioManager* audio_manager, | 234 media::AudioManager* audio_manager, |
219 AudioMirroringManager* mirroring_manager, | 235 AudioMirroringManager* mirroring_manager, |
220 MediaInternals* media_internals, | 236 MediaInternals* media_internals, |
221 MediaStreamManager* media_stream_manager, | 237 MediaStreamManager* media_stream_manager, |
222 const std::string& salt) | 238 const std::string& salt) |
223 : BrowserMessageFilter(AudioMsgStart), | 239 : BrowserMessageFilter(AudioMsgStart), |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
319 ReportErrorAndClose(stream_id); | 335 ReportErrorAndClose(stream_id); |
320 return; | 336 return; |
321 } | 337 } |
322 | 338 |
323 AudioEntry* const entry = LookupById(stream_id); | 339 AudioEntry* const entry = LookupById(stream_id); |
324 if (!entry) { | 340 if (!entry) { |
325 ReportErrorAndClose(stream_id); | 341 ReportErrorAndClose(stream_id); |
326 return; | 342 return; |
327 } | 343 } |
328 | 344 |
329 // Once the audio stream is created then complete the creation process by | 345 base::SharedMemory* shared_memory = entry->reader()->shared_memory(); |
330 // mapping shared memory and sharing with the renderer process. | 346 base::CancelableSyncSocket* foreign_socket = |
o1ka
2016/09/16 10:51:59
Could you transit all the comments to the new code
Max Morin
2016/09/16 12:46:15
Done.
| |
331 base::SharedMemoryHandle foreign_memory_handle; | 347 entry->reader()->foreign_socket(); |
332 if (!entry->shared_memory()->ShareToProcess(PeerHandle(), | 348 base::SharedMemoryHandle memory_handle; |
o1ka
2016/09/16 10:51:59
nit: I think foreign_memory_handle was more descri
Max Morin
2016/09/16 12:46:15
Done.
| |
333 &foreign_memory_handle)) { | |
334 // If we failed to map and share the shared memory then close the audio | |
335 // stream and send an error message. | |
336 ReportErrorAndClose(entry->stream_id()); | |
337 return; | |
338 } | |
339 | |
340 AudioSyncReader* reader = static_cast<AudioSyncReader*>(entry->reader()); | |
341 | |
342 base::SyncSocket::TransitDescriptor socket_descriptor; | 349 base::SyncSocket::TransitDescriptor socket_descriptor; |
343 | 350 size_t shared_memory_size = shared_memory->requested_size(); |
344 // If we failed to prepare the sync socket for the renderer then we fail | 351 if (!(shared_memory->ShareToProcess(PeerHandle(), &memory_handle) && |
345 // the construction of audio stream. | 352 foreign_socket->PrepareTransitDescriptor(PeerHandle(), |
346 if (!reader->PrepareForeignSocket(PeerHandle(), &socket_descriptor)) { | 353 &socket_descriptor))) { |
347 ReportErrorAndClose(entry->stream_id()); | 354 ReportErrorAndClose(entry->stream_id()); |
348 return; | 355 return; |
349 } | 356 } |
350 | 357 |
351 Send(new AudioMsg_NotifyStreamCreated( | 358 Send(new AudioMsg_NotifyStreamCreated( |
352 entry->stream_id(), foreign_memory_handle, socket_descriptor, | 359 stream_id, memory_handle, socket_descriptor, |
353 entry->shared_memory()->requested_size())); | 360 static_cast<uint32_t>(shared_memory_size))); |
o1ka
2016/09/16 10:51:59
checked_cast? (https://cs.chromium.org/chromium/sr
Max Morin
2016/09/16 12:46:15
Done.
| |
354 } | 361 } |
355 | 362 |
356 void AudioRendererHost::DidValidateRenderFrame(int stream_id, bool is_valid) { | 363 void AudioRendererHost::DidValidateRenderFrame(int stream_id, bool is_valid) { |
357 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 364 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
358 | 365 |
359 if (!is_valid) { | 366 if (!is_valid) { |
360 DLOG(WARNING) << "Render frame for stream (id=" << stream_id | 367 DLOG(WARNING) << "Render frame for stream (id=" << stream_id |
361 << ") no longer exists."; | 368 << ") no longer exists."; |
362 ReportErrorAndClose(stream_id); | 369 ReportErrorAndClose(stream_id); |
363 } | 370 } |
364 } | 371 } |
365 | 372 |
366 void AudioRendererHost::DoNotifyStreamStateChanged(int stream_id, | 373 void AudioRendererHost::DoNotifyStreamStateChanged(int stream_id, |
367 bool is_playing) { | 374 bool is_playing) { |
368 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 375 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
369 | 376 |
370 AudioEntry* const entry = LookupById(stream_id); | 377 AudioEntry* const entry = LookupById(stream_id); |
371 if (!entry) | 378 if (!entry) |
372 return; | 379 return; |
373 | 380 |
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
601 // a valid render frame. This validation is important for all the reasons | 608 // a valid render frame. This validation is important for all the reasons |
602 // stated in the comments above. This does not block stream creation, but will | 609 // stated in the comments above. This does not block stream creation, but will |
603 // force-close the stream later if validation fails. | 610 // force-close the stream later if validation fails. |
604 BrowserThread::PostTask( | 611 BrowserThread::PostTask( |
605 BrowserThread::UI, FROM_HERE, | 612 BrowserThread::UI, FROM_HERE, |
606 base::Bind(validate_render_frame_id_function_, render_process_id_, | 613 base::Bind(validate_render_frame_id_function_, render_process_id_, |
607 render_frame_id, | 614 render_frame_id, |
608 base::Bind(&AudioRendererHost::DidValidateRenderFrame, this, | 615 base::Bind(&AudioRendererHost::DidValidateRenderFrame, this, |
609 stream_id))); | 616 stream_id))); |
610 | 617 |
611 // Create the shared memory and share with the renderer process. | 618 std::unique_ptr<AudioEntry> entry = AudioEntry::Create( |
612 uint32_t shared_memory_size = sizeof(media::AudioOutputBufferParameters) + | 619 this, stream_id, render_frame_id, params, device_unique_id); |
613 AudioBus::CalculateMemorySize(params); | 620 if (!entry) { |
614 std::unique_ptr<base::SharedMemory> shared_memory(new base::SharedMemory()); | |
615 if (!shared_memory->CreateAndMapAnonymous(shared_memory_size)) { | |
616 SendErrorMessage(stream_id); | 621 SendErrorMessage(stream_id); |
617 return; | 622 return; |
618 } | 623 } |
619 | |
620 std::unique_ptr<AudioSyncReader> reader( | |
621 new AudioSyncReader(shared_memory.get(), params)); | |
622 if (!reader->Init()) { | |
623 SendErrorMessage(stream_id); | |
624 return; | |
625 } | |
626 | 624 |
627 MediaObserver* const media_observer = | 625 MediaObserver* const media_observer = |
628 GetContentClient()->browser()->GetMediaObserver(); | 626 GetContentClient()->browser()->GetMediaObserver(); |
629 if (media_observer) | 627 if (media_observer) |
630 media_observer->OnCreatingAudioStream(render_process_id_, render_frame_id); | 628 media_observer->OnCreatingAudioStream(render_process_id_, render_frame_id); |
631 | 629 |
632 std::unique_ptr<AudioEntry> entry( | |
633 new AudioEntry(this, stream_id, render_frame_id, params, device_unique_id, | |
634 std::move(shared_memory), std::move(reader))); | |
635 if (mirroring_manager_) { | 630 if (mirroring_manager_) { |
636 mirroring_manager_->AddDiverter( | 631 mirroring_manager_->AddDiverter( |
637 render_process_id_, entry->render_frame_id(), entry->controller()); | 632 render_process_id_, entry->render_frame_id(), entry->controller()); |
638 } | 633 } |
639 audio_entries_.insert(std::make_pair(stream_id, entry.release())); | 634 audio_entries_.insert(std::make_pair(stream_id, entry.release())); |
640 g_audio_streams_tracker.Get().IncreaseStreamCount(); | 635 g_audio_streams_tracker.Get().IncreaseStreamCount(); |
641 | 636 |
642 audio_log_->OnCreated(stream_id, params, device_unique_id); | 637 audio_log_->OnCreated(stream_id, params, device_unique_id); |
643 MediaInternals::GetInstance()->SetWebContentsTitleForAudioLogEntry( | 638 MediaInternals::GetInstance()->SetWebContentsTitleForAudioLogEntry( |
644 stream_id, render_process_id_, render_frame_id, audio_log_.get()); | 639 stream_id, render_process_id_, render_frame_id, audio_log_.get()); |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
855 callback.Run(false, device_info); | 850 callback.Run(false, device_info); |
856 } | 851 } |
857 | 852 |
858 bool AudioRendererHost::IsAuthorizationStarted(int stream_id) { | 853 bool AudioRendererHost::IsAuthorizationStarted(int stream_id) { |
859 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 854 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
860 const auto& i = authorizations_.find(stream_id); | 855 const auto& i = authorizations_.find(stream_id); |
861 return i != authorizations_.end(); | 856 return i != authorizations_.end(); |
862 } | 857 } |
863 | 858 |
864 } // namespace content | 859 } // namespace content |
OLD | NEW |