OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/renderer_host/media/audio_mirroring_manager.h" |
| 6 |
| 7 #include "content/public/browser/browser_thread.h" |
| 8 |
| 9 namespace content { |
| 10 |
| 11 AudioMirroringManager::AudioMirroringManager() {} |
| 12 |
| 13 AudioMirroringManager::~AudioMirroringManager() { |
| 14 DCHECK(diverters_.empty()); |
| 15 DCHECK(sessions_.empty()); |
| 16 } |
| 17 |
| 18 void AudioMirroringManager::AddDiverter( |
| 19 int render_process_id, int render_view_id, Diverter* diverter) { |
| 20 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 21 DCHECK(diverter); |
| 22 |
| 23 // DCHECK(diverter not already in diverters_ under any key) |
| 24 #ifndef NDEBUG |
| 25 for (DiverterMap::const_iterator it = diverters_.begin(); |
| 26 it != diverters_.end(); ++it) { |
| 27 DCHECK_NE(diverter, it->second); |
| 28 } |
| 29 #endif |
| 30 |
| 31 // Add the diverter to the set of active diverters. |
| 32 const Target target(render_process_id, render_view_id); |
| 33 diverters_.insert(std::make_pair(target, diverter)); |
| 34 |
| 35 // If a mirroring session is active, start diverting the audio stream |
| 36 // immediately. |
| 37 SessionMap::iterator session_it = sessions_.find(target); |
| 38 if (session_it != sessions_.end()) { |
| 39 diverter->StartDiverting( |
| 40 session_it->second->AddInput(diverter->GetAudioParameters())); |
| 41 } |
| 42 } |
| 43 |
| 44 void AudioMirroringManager::RemoveDiverter( |
| 45 int render_process_id, int render_view_id, Diverter* diverter) { |
| 46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 47 |
| 48 // Stop diverting the audio stream if a mirroring session is active. |
| 49 const Target target(render_process_id, render_view_id); |
| 50 SessionMap::iterator session_it = sessions_.find(target); |
| 51 if (session_it != sessions_.end()) |
| 52 diverter->StopDiverting(); |
| 53 |
| 54 // Remove the diverter from the set of active diverters. |
| 55 for (DiverterMap::iterator it = diverters_.lower_bound(target); |
| 56 it != diverters_.end() && it->first == target; ++it) { |
| 57 if (it->second == diverter) { |
| 58 diverters_.erase(it); |
| 59 break; |
| 60 } |
| 61 } |
| 62 } |
| 63 |
| 64 void AudioMirroringManager::StartMirroring( |
| 65 int render_process_id, int render_view_id, |
| 66 MirroringDestination* destination) { |
| 67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 68 DCHECK(destination); |
| 69 |
| 70 // Insert an entry into the set of active mirroring sessions. If a mirroring |
| 71 // session is already active for |render_process_id| + |render_view_id|, |
| 72 // replace the entry. |
| 73 const Target target(render_process_id, render_view_id); |
| 74 SessionMap::iterator session_it = sessions_.find(target); |
| 75 MirroringDestination* old_destination; |
| 76 if (session_it == sessions_.end()) { |
| 77 old_destination = NULL; |
| 78 sessions_.insert(std::make_pair(target, destination)); |
| 79 |
| 80 DVLOG(1) << "Start mirroring render_process_id:render_view_id=" |
| 81 << render_process_id << ':' << render_view_id |
| 82 << " --> MirroringDestination@" << destination; |
| 83 } else { |
| 84 old_destination = session_it->second; |
| 85 session_it->second = destination; |
| 86 |
| 87 DVLOG(1) << "Switch mirroring of render_process_id:render_view_id=" |
| 88 << render_process_id << ':' << render_view_id |
| 89 << " MirroringDestination@" << old_destination |
| 90 << " --> MirroringDestination@" << destination; |
| 91 } |
| 92 |
| 93 // Divert audio streams coming from |target| to |destination|. If streams |
| 94 // were already diverted to the |old_destination|, remove them. |
| 95 for (DiverterMap::iterator it = diverters_.lower_bound(target); |
| 96 it != diverters_.end() && it->first == target; ++it) { |
| 97 Diverter* const diverter = it->second; |
| 98 if (old_destination) |
| 99 diverter->StopDiverting(); |
| 100 diverter->StartDiverting( |
| 101 destination->AddInput(diverter->GetAudioParameters())); |
| 102 } |
| 103 } |
| 104 |
| 105 void AudioMirroringManager::StopMirroring( |
| 106 int render_process_id, int render_view_id, |
| 107 MirroringDestination* destination) { |
| 108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 109 |
| 110 // Stop mirroring if there is an active session *and* the destination |
| 111 // matches. |
| 112 const Target target(render_process_id, render_view_id); |
| 113 SessionMap::iterator session_it = sessions_.find(target); |
| 114 if (session_it == sessions_.end() || destination != session_it->second) |
| 115 return; |
| 116 |
| 117 DVLOG(1) << "Stop mirroring render_process_id:render_view_id=" |
| 118 << render_process_id << ':' << render_view_id |
| 119 << " --> MirroringDestination@" << destination; |
| 120 |
| 121 // Stop diverting each audio stream in the mirroring session being stopped. |
| 122 for (DiverterMap::iterator it = diverters_.lower_bound(target); |
| 123 it != diverters_.end() && it->first == target; ++it) { |
| 124 it->second->StopDiverting(); |
| 125 } |
| 126 |
| 127 // Remove the entry from the set of active mirroring sessions. |
| 128 sessions_.erase(session_it); |
| 129 } |
| 130 |
| 131 AudioMirroringManager::MirroringDestination::~MirroringDestination() {} |
| 132 |
| 133 } // namespace content |
OLD | NEW |