Chromium Code Reviews| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/lazy_instance.h" | |
| 8 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 9 #include "base/process.h" | 10 #include "base/process.h" |
| 10 #include "base/shared_memory.h" | 11 #include "base/shared_memory.h" |
| 11 #include "content/browser/browser_main_loop.h" | 12 #include "content/browser/browser_main_loop.h" |
| 12 #include "content/browser/renderer_host/media/audio_sync_reader.h" | 13 #include "content/browser/renderer_host/media/audio_sync_reader.h" |
| 14 #include "content/browser/renderer_host/media/web_contents_audio_input_stream.h" | |
| 13 #include "content/common/media/audio_messages.h" | 15 #include "content/common/media/audio_messages.h" |
| 14 #include "content/public/browser/media_observer.h" | 16 #include "content/public/browser/media_observer.h" |
| 17 #include "media/audio/audio_io.h" | |
| 15 #include "media/audio/shared_memory_util.h" | 18 #include "media/audio/shared_memory_util.h" |
| 16 #include "media/base/audio_bus.h" | 19 #include "media/base/audio_bus.h" |
| 17 #include "media/base/limits.h" | 20 #include "media/base/limits.h" |
| 18 | 21 |
| 19 using media::AudioBus; | 22 using media::AudioBus; |
| 20 | 23 |
| 21 namespace content { | 24 namespace content { |
| 22 | 25 |
| 23 struct AudioRendererHost::AudioEntry { | 26 struct AudioRendererHost::AudioEntry { |
| 24 AudioEntry(); | 27 AudioEntry(); |
| 25 ~AudioEntry(); | 28 ~AudioEntry(); |
| 26 | 29 |
| 27 // The AudioOutputController that manages the audio stream. | 30 // The AudioOutputController that manages the audio stream. |
| 28 scoped_refptr<media::AudioOutputController> controller; | 31 scoped_refptr<media::AudioOutputController> controller; |
| 29 | 32 |
| 30 // The audio stream ID. | 33 // The audio stream ID. |
| 31 int stream_id; | 34 int stream_id; |
| 32 | 35 |
| 36 // The routing ID of the source render view. | |
| 37 int render_view_id; | |
| 38 | |
| 33 // Shared memory for transmission of the audio data. | 39 // Shared memory for transmission of the audio data. |
| 34 base::SharedMemory shared_memory; | 40 base::SharedMemory shared_memory; |
| 35 | 41 |
| 36 // The synchronous reader to be used by the controller. We have the | 42 // The synchronous reader to be used by the controller. We have the |
| 37 // ownership of the reader. | 43 // ownership of the reader. |
| 38 scoped_ptr<media::AudioOutputController::SyncReader> reader; | 44 scoped_ptr<media::AudioOutputController::SyncReader> reader; |
| 39 | 45 |
| 46 // When non-NULL, normal audio output is being diverted for audio mirroring. | |
| 47 scoped_ptr<media::DivertedAudioOutputStream> diverted_stream; | |
| 48 | |
| 40 // Set to true after we called Close() for the controller. | 49 // Set to true after we called Close() for the controller. |
| 41 bool pending_close; | 50 bool pending_close; |
| 42 }; | 51 }; |
| 43 | 52 |
| 44 AudioRendererHost::AudioEntry::AudioEntry() | 53 AudioRendererHost::AudioEntry::AudioEntry() |
| 45 : stream_id(0), | 54 : stream_id(0), |
| 55 render_view_id(MSG_ROUTING_NONE), | |
| 46 pending_close(false) { | 56 pending_close(false) { |
| 47 } | 57 } |
| 48 | 58 |
| 49 AudioRendererHost::AudioEntry::~AudioEntry() {} | 59 AudioRendererHost::AudioEntry::~AudioEntry() {} |
| 50 | 60 |
| 61 namespace { | |
| 62 | |
| 63 struct GlobalLookup { | |
| 64 base::Lock lock; | |
| 65 std::map<int, AudioRendererHost*> by_process_id; | |
| 66 }; | |
| 67 | |
| 68 base::LazyInstance<GlobalLookup>::Leaky g_lookup = LAZY_INSTANCE_INITIALIZER; | |
| 69 | |
| 70 void RegisterAudioRendererHost(int render_process_id, AudioRendererHost* arh) { | |
| 71 GlobalLookup& lookup = g_lookup.Get(); | |
|
Alpha Left Google
2012/11/26 22:59:59
Make sure RegisterARG and DeregisterARH are called
miu
2012/11/28 05:05:01
Done.
| |
| 72 base::AutoLock guard(lookup.lock); | |
| 73 lookup.by_process_id.insert(std::make_pair(render_process_id, arh)); | |
| 74 } | |
| 75 | |
| 76 void DeregisterAudioRendererHost(int render_process_id) { | |
| 77 GlobalLookup& lookup = g_lookup.Get(); | |
| 78 base::AutoLock guard(lookup.lock); | |
| 79 lookup.by_process_id.erase(render_process_id); | |
| 80 } | |
| 81 | |
| 82 } // namespace | |
| 83 | |
| 84 // static | |
| 85 scoped_refptr<AudioRendererHost> AudioRendererHost::FromRenderProcessID( | |
| 86 int render_process_id) { | |
| 87 GlobalLookup& lookup = g_lookup.Get(); | |
| 88 base::AutoLock guard(lookup.lock); | |
| 89 std::map<int, AudioRendererHost*>::const_iterator it = | |
| 90 lookup.by_process_id.find(render_process_id); | |
| 91 return (it != lookup.by_process_id.end()) ? it->second : NULL; | |
| 92 } | |
| 93 | |
| 51 /////////////////////////////////////////////////////////////////////////////// | 94 /////////////////////////////////////////////////////////////////////////////// |
| 52 // AudioRendererHost implementations. | 95 // AudioRendererHost implementations. |
| 53 AudioRendererHost::AudioRendererHost( | 96 AudioRendererHost::AudioRendererHost( |
| 97 int render_process_id, | |
| 54 media::AudioManager* audio_manager, MediaObserver* media_observer) | 98 media::AudioManager* audio_manager, MediaObserver* media_observer) |
| 55 : audio_manager_(audio_manager), | 99 : render_process_id_(render_process_id), |
| 100 audio_manager_(audio_manager), | |
| 56 media_observer_(media_observer) { | 101 media_observer_(media_observer) { |
| 102 RegisterAudioRendererHost(render_process_id_, this); | |
|
Alpha Left Google
2012/11/26 22:59:59
Call register in OnChannelConnected() which is cal
miu
2012/11/28 05:05:01
Done.
| |
| 57 } | 103 } |
| 58 | 104 |
| 59 AudioRendererHost::~AudioRendererHost() { | 105 AudioRendererHost::~AudioRendererHost() { |
| 60 DCHECK(audio_entries_.empty()); | 106 DCHECK(audio_entries_.empty()); |
| 107 DCHECK(mirror_sessions_.empty()); | |
| 108 DeregisterAudioRendererHost(render_process_id_); | |
|
Alpha Left Google
2012/11/26 22:59:59
Do it in OnChannelClosing().
miu
2012/11/28 05:05:01
Done.
| |
| 61 } | 109 } |
| 62 | 110 |
| 63 void AudioRendererHost::OnChannelClosing() { | 111 void AudioRendererHost::OnChannelClosing() { |
| 64 BrowserMessageFilter::OnChannelClosing(); | 112 BrowserMessageFilter::OnChannelClosing(); |
| 65 | 113 |
| 66 // Since the IPC channel is gone, close all requested audio streams. | 114 // Since the IPC channel is gone, close all requested audio streams. |
|
Alpha Left Google
2012/11/26 22:59:59
You can call StopMirroring() here, this method is
miu
2012/11/28 05:05:01
Done.
| |
| 67 DeleteEntries(); | 115 DeleteEntries(); |
| 68 } | 116 } |
| 69 | 117 |
| 70 void AudioRendererHost::OnDestruct() const { | 118 void AudioRendererHost::OnDestruct() const { |
| 71 BrowserThread::DeleteOnIOThread::Destruct(this); | 119 BrowserThread::DeleteOnIOThread::Destruct(this); |
| 72 } | 120 } |
| 73 | 121 |
| 74 /////////////////////////////////////////////////////////////////////////////// | 122 /////////////////////////////////////////////////////////////////////////////// |
| 75 // media::AudioOutputController::EventHandler implementations. | 123 // media::AudioOutputController::EventHandler implementations. |
| 76 void AudioRendererHost::OnCreated(media::AudioOutputController* controller) { | 124 void AudioRendererHost::OnCreated(media::AudioOutputController* controller) { |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 207 IPC_MESSAGE_HANDLER(AudioHostMsg_PauseStream, OnPauseStream) | 255 IPC_MESSAGE_HANDLER(AudioHostMsg_PauseStream, OnPauseStream) |
| 208 IPC_MESSAGE_HANDLER(AudioHostMsg_FlushStream, OnFlushStream) | 256 IPC_MESSAGE_HANDLER(AudioHostMsg_FlushStream, OnFlushStream) |
| 209 IPC_MESSAGE_HANDLER(AudioHostMsg_CloseStream, OnCloseStream) | 257 IPC_MESSAGE_HANDLER(AudioHostMsg_CloseStream, OnCloseStream) |
| 210 IPC_MESSAGE_HANDLER(AudioHostMsg_SetVolume, OnSetVolume) | 258 IPC_MESSAGE_HANDLER(AudioHostMsg_SetVolume, OnSetVolume) |
| 211 IPC_MESSAGE_UNHANDLED(handled = false) | 259 IPC_MESSAGE_UNHANDLED(handled = false) |
| 212 IPC_END_MESSAGE_MAP_EX() | 260 IPC_END_MESSAGE_MAP_EX() |
| 213 | 261 |
| 214 return handled; | 262 return handled; |
| 215 } | 263 } |
| 216 | 264 |
| 265 void AudioRendererHost::StartMirroring( | |
| 266 int render_view_id, | |
| 267 const scoped_refptr<WebContentsAudioInputStream>& destination) { | |
| 268 BrowserThread::PostTask( | |
| 269 BrowserThread::IO, FROM_HERE, | |
| 270 base::Bind(&AudioRendererHost::DoStartMirroring, this, | |
| 271 render_view_id, destination)); | |
| 272 } | |
| 273 | |
| 274 void AudioRendererHost::DoStartMirroring( | |
| 275 int render_view_id, | |
| 276 const scoped_refptr<WebContentsAudioInputStream>& destination) { | |
| 277 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 278 DCHECK(destination); | |
| 279 | |
| 280 MirrorSessionMap::iterator session_it = mirror_sessions_.find(render_view_id); | |
| 281 if (session_it != mirror_sessions_.end()) { | |
| 282 DVLOG(1) << "Forcing StopMirroring(" << render_view_id | |
| 283 << ") on existing mirroring session (@" << session_it->second | |
| 284 << ")."; | |
| 285 DoStopMirroring(render_view_id, session_it->second); | |
| 286 } | |
| 287 | |
| 288 DVLOG(1) << "Start mirroring RenderView " << render_view_id | |
| 289 << " --> WebContentsAudioInputStream@" << destination; | |
| 290 | |
| 291 mirror_sessions_.insert(std::make_pair(render_view_id, destination)); | |
| 292 for (AudioEntryMap::const_iterator it = audio_entries_.begin(); | |
| 293 it != audio_entries_.end(); ++it) { | |
| 294 if (it->second->render_view_id == render_view_id) { | |
| 295 it->second->diverted_stream.reset(it->second->controller->Divert()); | |
| 296 destination->AddAudioOutputStream(it->second->diverted_stream.get()); | |
| 297 } | |
| 298 } | |
| 299 } | |
| 300 | |
| 301 void AudioRendererHost::StopMirroring( | |
| 302 int render_view_id, | |
| 303 const scoped_refptr<WebContentsAudioInputStream>& destination) { | |
| 304 BrowserThread::PostTask( | |
| 305 BrowserThread::IO, FROM_HERE, | |
| 306 base::Bind(&AudioRendererHost::DoStopMirroring, this, | |
| 307 render_view_id, destination)); | |
| 308 } | |
| 309 | |
| 310 void AudioRendererHost::DoStopMirroring( | |
| 311 int render_view_id, | |
| 312 const scoped_refptr<WebContentsAudioInputStream>& destination) { | |
| 313 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 314 | |
| 315 MirrorSessionMap::iterator session_it = mirror_sessions_.find(render_view_id); | |
| 316 if (session_it == mirror_sessions_.end() || | |
| 317 destination != session_it->second) { | |
| 318 return; | |
| 319 } | |
| 320 | |
| 321 DVLOG(1) << "Stop mirroring RenderView " << render_view_id | |
| 322 << " --> WebContentsAudioInputStream@" << destination; | |
| 323 | |
| 324 for (AudioEntryMap::const_iterator it = audio_entries_.begin(); | |
| 325 it != audio_entries_.end(); ++it) { | |
| 326 if (it->second->render_view_id == render_view_id) { | |
| 327 destination->RemoveAudioOutputStream(it->second->diverted_stream.get()); | |
| 328 it->second->diverted_stream.reset(); | |
| 329 } | |
| 330 } | |
| 331 mirror_sessions_.erase(session_it); | |
| 332 } | |
| 333 | |
| 217 void AudioRendererHost::OnCreateStream( | 334 void AudioRendererHost::OnCreateStream( |
| 218 int stream_id, const media::AudioParameters& params, int input_channels) { | 335 int stream_id, const media::AudioParameters& params, int input_channels) { |
| 219 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 336 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 220 DCHECK(LookupById(stream_id) == NULL); | 337 DCHECK(LookupById(stream_id) == NULL); |
| 221 | 338 |
| 222 media::AudioParameters audio_params(params); | 339 media::AudioParameters audio_params(params); |
| 223 uint32 buffer_size = media::AudioBus::CalculateMemorySize(audio_params); | 340 uint32 buffer_size = media::AudioBus::CalculateMemorySize(audio_params); |
| 224 DCHECK_GT(buffer_size, 0U); | 341 DCHECK_GT(buffer_size, 0U); |
| 225 DCHECK_LE(buffer_size, | 342 DCHECK_LE(buffer_size, |
| 226 static_cast<uint32>(media::limits::kMaxPacketSizeInBytes)); | 343 static_cast<uint32>(media::limits::kMaxPacketSizeInBytes)); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 274 } | 391 } |
| 275 | 392 |
| 276 // If we have created the controller successfully, create an entry and add it | 393 // If we have created the controller successfully, create an entry and add it |
| 277 // to the map. | 394 // to the map. |
| 278 entry->stream_id = stream_id; | 395 entry->stream_id = stream_id; |
| 279 audio_entries_.insert(std::make_pair(stream_id, entry.release())); | 396 audio_entries_.insert(std::make_pair(stream_id, entry.release())); |
| 280 if (media_observer_) | 397 if (media_observer_) |
| 281 media_observer_->OnSetAudioStreamStatus(this, stream_id, "created"); | 398 media_observer_->OnSetAudioStreamStatus(this, stream_id, "created"); |
| 282 } | 399 } |
| 283 | 400 |
| 401 void AudioRendererHost::OnAssociateStreamWithProducer(int stream_id, | |
| 402 int render_view_id) { | |
| 403 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 404 | |
| 405 AudioEntry* const entry = LookupById(stream_id); | |
| 406 if (!entry) { | |
| 407 SendErrorMessage(stream_id); | |
| 408 return; | |
| 409 } | |
| 410 | |
| 411 if (entry->render_view_id == render_view_id) | |
| 412 return; // No change. | |
| 413 | |
| 414 // If this stream is currently being mirrored, remove it from that mirroring | |
| 415 // session. | |
| 416 MirrorSessionMap::const_iterator prev_session_it = | |
| 417 mirror_sessions_.find(entry->render_view_id); | |
| 418 scoped_ptr<media::DivertedAudioOutputStream> diverted_stream; | |
| 419 if (prev_session_it != mirror_sessions_.end()) { | |
| 420 prev_session_it->second-> | |
| 421 RemoveAudioOutputStream(entry->diverted_stream.get()); | |
| 422 diverted_stream.swap(entry->diverted_stream); | |
| 423 } | |
| 424 | |
| 425 entry->render_view_id = render_view_id; | |
| 426 | |
| 427 // If a mirroring session is already active for the RenderView, add this | |
| 428 // stream to that mirroring session. | |
| 429 MirrorSessionMap::const_iterator next_session_it = | |
| 430 mirror_sessions_.find(render_view_id); | |
| 431 if (next_session_it != mirror_sessions_.end()) { | |
| 432 if (diverted_stream) { | |
| 433 entry->diverted_stream.swap(diverted_stream); | |
| 434 } else { | |
| 435 entry->diverted_stream.reset(entry->controller->Divert()); | |
| 436 } | |
| 437 next_session_it->second->AddAudioOutputStream(entry->diverted_stream.get()); | |
| 438 } | |
| 439 } | |
| 440 | |
| 284 void AudioRendererHost::OnPlayStream(int stream_id) { | 441 void AudioRendererHost::OnPlayStream(int stream_id) { |
| 285 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 442 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 286 | 443 |
| 287 AudioEntry* entry = LookupById(stream_id); | 444 AudioEntry* entry = LookupById(stream_id); |
| 288 if (!entry) { | 445 if (!entry) { |
| 289 SendErrorMessage(stream_id); | 446 SendErrorMessage(stream_id); |
| 290 return; | 447 return; |
| 291 } | 448 } |
| 292 | 449 |
| 293 entry->controller->Play(); | 450 entry->controller->Play(); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 363 for (AudioEntryMap::iterator i = audio_entries_.begin(); | 520 for (AudioEntryMap::iterator i = audio_entries_.begin(); |
| 364 i != audio_entries_.end(); ++i) { | 521 i != audio_entries_.end(); ++i) { |
| 365 CloseAndDeleteStream(i->second); | 522 CloseAndDeleteStream(i->second); |
| 366 } | 523 } |
| 367 } | 524 } |
| 368 | 525 |
| 369 void AudioRendererHost::CloseAndDeleteStream(AudioEntry* entry) { | 526 void AudioRendererHost::CloseAndDeleteStream(AudioEntry* entry) { |
| 370 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 527 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 371 | 528 |
| 372 if (!entry->pending_close) { | 529 if (!entry->pending_close) { |
| 530 // If this stream is currently being mirrored, remove it from the mirroring | |
| 531 // session. | |
| 532 MirrorSessionMap::const_iterator session_it = | |
| 533 mirror_sessions_.find(entry->render_view_id); | |
| 534 if (session_it != mirror_sessions_.end()) { | |
| 535 session_it->second->RemoveAudioOutputStream(entry->diverted_stream.get()); | |
| 536 entry->diverted_stream.reset(); | |
| 537 } | |
| 538 | |
| 373 entry->controller->Close( | 539 entry->controller->Close( |
| 374 base::Bind(&AudioRendererHost::DeleteEntry, this, entry)); | 540 base::Bind(&AudioRendererHost::DeleteEntry, this, entry)); |
| 375 entry->pending_close = true; | 541 entry->pending_close = true; |
| 376 } | 542 } |
| 377 } | 543 } |
| 378 | 544 |
| 379 void AudioRendererHost::DeleteEntry(AudioEntry* entry) { | 545 void AudioRendererHost::DeleteEntry(AudioEntry* entry) { |
| 380 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 546 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 381 | 547 |
| 382 // Delete the entry when this method goes out of scope. | 548 // Delete the entry when this method goes out of scope. |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 425 return NULL; | 591 return NULL; |
| 426 } | 592 } |
| 427 | 593 |
| 428 media::AudioOutputController* AudioRendererHost::LookupControllerByIdForTesting( | 594 media::AudioOutputController* AudioRendererHost::LookupControllerByIdForTesting( |
| 429 int stream_id) { | 595 int stream_id) { |
| 430 AudioEntry* const entry = LookupById(stream_id); | 596 AudioEntry* const entry = LookupById(stream_id); |
| 431 return entry ? entry->controller : NULL; | 597 return entry ? entry->controller : NULL; |
| 432 } | 598 } |
| 433 | 599 |
| 434 } // namespace content | 600 } // namespace content |
| OLD | NEW |