Chromium Code Reviews| Index: content/browser/renderer_host/media/audio_renderer_host.cc |
| diff --git a/content/browser/renderer_host/media/audio_renderer_host.cc b/content/browser/renderer_host/media/audio_renderer_host.cc |
| index 83b5f2510baaa405905af5e07c561e76283b9ade..8d4e1de98757d1611a030d12bd0575d0fefdaecf 100644 |
| --- a/content/browser/renderer_host/media/audio_renderer_host.cc |
| +++ b/content/browser/renderer_host/media/audio_renderer_host.cc |
| @@ -30,6 +30,9 @@ struct AudioRendererHost::AudioEntry { |
| // The audio stream ID. |
| int stream_id; |
| + // The routing ID of the source render view. |
| + int render_view_id; |
| + |
| // Shared memory for transmission of the audio data. |
| base::SharedMemory shared_memory; |
| @@ -43,21 +46,36 @@ struct AudioRendererHost::AudioEntry { |
| AudioRendererHost::AudioEntry::AudioEntry() |
| : stream_id(0), |
| + render_view_id(MSG_ROUTING_NONE), |
| pending_close(false) { |
| } |
| AudioRendererHost::AudioEntry::~AudioEntry() {} |
| +AudioRendererHost::MirroringDestination::~MirroringDestination() {} |
| + |
| /////////////////////////////////////////////////////////////////////////////// |
| // AudioRendererHost implementations. |
| AudioRendererHost::AudioRendererHost( |
| + int render_process_id, |
| media::AudioManager* audio_manager, MediaObserver* media_observer) |
| - : audio_manager_(audio_manager), |
| + : render_process_id_(render_process_id), |
| + audio_manager_(audio_manager), |
| media_observer_(media_observer) { |
| } |
| AudioRendererHost::~AudioRendererHost() { |
| DCHECK(audio_entries_.empty()); |
| + DCHECK(mirror_sessions_.empty()); |
| +} |
| + |
| +base::LazyInstance<AudioRendererHost::ActiveHostMap>::Leaky |
| + AudioRendererHost::g_host_map_ = LAZY_INSTANCE_INITIALIZER; |
|
tommi (sloooow) - chröme
2012/12/18 13:41:26
thinking out loud here... this map is only accesse
|
| + |
| +void AudioRendererHost::OnChannelConnected(int32 peer_pid) { |
| + g_host_map_.Get().insert(std::make_pair(render_process_id_, this)); |
| + |
| + BrowserMessageFilter::OnChannelConnected(peer_pid); |
| } |
| void AudioRendererHost::OnChannelClosing() { |
| @@ -65,6 +83,13 @@ void AudioRendererHost::OnChannelClosing() { |
| // Since the IPC channel is gone, close all requested audio streams. |
| DeleteEntries(); |
| + |
| + while (!mirror_sessions_.empty()) { |
| + MirrorSessionMap::iterator it = mirror_sessions_.begin(); |
| + DoStopMirroring(render_process_id_, it->first, it->second); |
| + } |
| + |
| + g_host_map_.Get().erase(render_process_id_); |
| } |
| void AudioRendererHost::OnDestruct() const { |
| @@ -216,6 +241,133 @@ bool AudioRendererHost::OnMessageReceived(const IPC::Message& message, |
| return handled; |
| } |
| +// static |
| +AudioRendererHost* AudioRendererHost::FromRenderProcessID( |
| + int render_process_id) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + |
| + // Note: It's possible for look-ups to fail since, for example, an |
| + // AudioRendererHost could be shutdown before StartMirroring() is called. |
| + // It's common for mirroring sessions to target a different render |
| + // process/view than the one that will receive the mirrored audio data. |
| + // Example sequence of events: |
| + // |
| + // 1. RenderProcess_1 wants to mirror a tab within RenderProcess_2. |
| + // 2. RenderProcess_1 starts sending IPCs to initiate a mirroring session. |
| + // 3. In the meantime, RenderProcess_2 is shut down (e.g., tab closed). The |
| + // AudioRendererHost for RenderProcess_2 is destroyed as a result. |
| + // 4. A browser-side entity for RenderProcess_1 attempts to call |
| + // StartMirroring(), but since the AudioRendererHost for RenderProcess_2 |
| + // is gone, the look-up here fails. |
| + |
| + ActiveHostMap& host_map = g_host_map_.Get(); |
| + ActiveHostMap::const_iterator it = host_map.find(render_process_id); |
| + return it == host_map.end() ? NULL : it->second; |
| +} |
| + |
| +// static |
| +void AudioRendererHost::StartMirroring( |
| + int render_process_id, int render_view_id, |
| + const scoped_refptr<MirroringDestination>& destination) { |
| + BrowserThread::PostTask( |
| + BrowserThread::IO, FROM_HERE, |
| + base::Bind(&AudioRendererHost::DoStartMirroring, |
| + render_process_id, render_view_id, destination)); |
| +} |
| + |
| +// static |
| +void AudioRendererHost::DoStartMirroring( |
| + int render_process_id, int render_view_id, |
| + const scoped_refptr<MirroringDestination>& destination) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + DCHECK(destination); |
| + |
| + AudioRendererHost* const host = FromRenderProcessID(render_process_id); |
| + if (!host) { |
|
tommi (sloooow) - chröme
2012/12/18 13:41:26
no {}
|
| + return; // See comment in FromRenderProcessID(). |
| + } |
| + |
| + // Insert an entry into the set of active mirroring sessions. If a mirroring |
| + // session is already active for |render_process_id| + |render_view_id|, |
| + // replace the entry. |
| + MirrorSessionMap::iterator session_it = |
| + host->mirror_sessions_.find(render_view_id); |
| + scoped_refptr<MirroringDestination> old_destination; |
| + if (session_it == host->mirror_sessions_.end()) { |
| + host->mirror_sessions_.insert(std::make_pair(render_view_id, destination)); |
| + |
| + DVLOG(1) << "Start mirroring render_process_id:render_view_id=" |
| + << render_process_id << ':' << render_view_id |
| + << " --> MirroringDestination@" << destination; |
| + } else { |
| + old_destination = session_it->second; |
| + session_it->second = destination; |
| + |
| + DVLOG(1) << "Switch mirroring of render_process_id:render_view_id=" |
| + << render_process_id << ':' << render_view_id |
| + << " MirroringDestination@" << old_destination |
| + << " --> MirroringDestination@" << destination; |
| + } |
| + |
| + // Divert any existing audio streams to |destination|. If streams were |
| + // already diverted to the |old_destination|, remove them. |
| + for (AudioEntryMap::const_iterator it = host->audio_entries_.begin(); |
| + it != host->audio_entries_.end(); ++it) { |
| + AudioEntry* const entry = it->second; |
| + if (entry->render_view_id == render_view_id && !entry->pending_close) { |
| + if (old_destination) |
| + entry->controller->RevertDiversion(); |
| + entry->controller->DivertToStream( |
| + destination->AddInput(entry->controller->params())); |
| + } |
| + } |
| +} |
| + |
| +// static |
| +void AudioRendererHost::StopMirroring( |
| + int render_process_id, int render_view_id, |
| + const scoped_refptr<MirroringDestination>& destination) { |
| + BrowserThread::PostTask( |
| + BrowserThread::IO, FROM_HERE, |
| + base::Bind(&AudioRendererHost::DoStopMirroring, |
| + render_process_id, render_view_id, destination)); |
| +} |
| + |
| +// static |
| +void AudioRendererHost::DoStopMirroring( |
| + int render_process_id, int render_view_id, |
| + const scoped_refptr<MirroringDestination>& destination) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + |
| + AudioRendererHost* const host = FromRenderProcessID(render_process_id); |
| + if (!host) { |
|
tommi (sloooow) - chröme
2012/12/18 13:41:26
no {}
|
| + return; // See comment in FromRenderProcessID(). |
| + } |
| + |
| + MirrorSessionMap::iterator session_it = |
| + host->mirror_sessions_.find(render_view_id); |
| + if (session_it == host->mirror_sessions_.end() || |
| + destination != session_it->second) { |
| + return; |
| + } |
| + |
| + DVLOG(1) << "Stop mirroring render_process_id:render_view_id=" |
| + << render_process_id << ':' << render_view_id |
| + << " --> MirroringDestination@" << destination; |
| + |
| + // Revert the "divert" for each audio stream currently active in the mirroring |
| + // session being stopped. |
| + for (AudioEntryMap::const_iterator it = host->audio_entries_.begin(); |
| + it != host->audio_entries_.end(); ++it) { |
| + AudioEntry* const entry = it->second; |
| + if (entry->render_view_id == render_view_id && !entry->pending_close) { |
|
tommi (sloooow) - chröme
2012/12/18 13:41:26
same here (no {}) and throughout for single line i
|
| + entry->controller->RevertDiversion(); |
| + } |
| + } |
| + |
| + host->mirror_sessions_.erase(session_it); |
| +} |
| + |
| void AudioRendererHost::OnCreateStream( |
| int stream_id, const media::AudioParameters& params, int input_channels) { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| @@ -285,10 +437,31 @@ void AudioRendererHost::OnCreateStream( |
| void AudioRendererHost::OnAssociateStreamWithProducer(int stream_id, |
| int render_view_id) { |
| - // TODO(miu): Will use render_view_id in upcoming change. |
| - DVLOG(1) << "AudioRendererHost@" << this |
| - << "::OnAssociateStreamWithProducer(stream_id=" << stream_id |
| - << ", render_view_id=" << render_view_id << ")"; |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + |
| + AudioEntry* const entry = LookupById(stream_id); |
| + if (!entry) { |
| + SendErrorMessage(stream_id); |
| + return; |
| + } |
| + |
| + if (entry->render_view_id == render_view_id) |
| + return; // No change. |
| + |
| + MirrorSessionMap::const_iterator prev_session_it = |
| + mirror_sessions_.find(entry->render_view_id); |
| + if (prev_session_it != mirror_sessions_.end()) { |
| + entry->controller->RevertDiversion(); |
| + } |
| + |
| + entry->render_view_id = render_view_id; |
| + |
| + MirrorSessionMap::const_iterator next_session_it = |
| + mirror_sessions_.find(render_view_id); |
| + if (next_session_it != mirror_sessions_.end()) { |
| + entry->controller->DivertToStream( |
| + next_session_it->second->AddInput(entry->controller->params())); |
| + } |
| } |
| void AudioRendererHost::OnPlayStream(int stream_id) { |