Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 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/media/capture/web_contents_audio_muter.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/bind_helpers.h" | |
| 9 #include "content/browser/media/capture/audio_mirroring_manager.h" | |
| 10 #include "content/public/browser/browser_thread.h" | |
| 11 #include "content/public/browser/render_frame_host.h" | |
| 12 #include "content/public/browser/web_contents.h" | |
| 13 #include "media/audio/audio_io.h" | |
| 14 #include "media/audio/audio_manager.h" | |
| 15 #include "media/audio/fake_audio_consumer.h" | |
| 16 #include "media/base/bind_to_current_loop.h" | |
| 17 | |
| 18 namespace content { | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 // An AudioOutputStream that pumps audio data, but does nothing with it. | |
| 23 // Pumping the audio data is necessary because video playback is synchronized to | |
| 24 // the audio stream and will freeze otherwise. | |
| 25 // | |
| 26 // TODO(miu): media::FakeAudioOutputStream does pretty much the same thing as | |
| 27 // this class, but requires construction/destruction via media::AudioManagerBase | |
| 28 // on the audio thread. Once that's fixed, this class will no longer be needed. | |
| 29 // http://crbug.com/416278 | |
| 30 class AudioDiscarder : public media::AudioOutputStream { | |
| 31 public: | |
| 32 explicit AudioDiscarder(const media::AudioParameters& params) | |
| 33 : consumer_(media::AudioManager::Get()->GetWorkerTaskRunner(), params) {} | |
| 34 | |
| 35 // AudioOutputStream implementation. | |
| 36 virtual bool Open() OVERRIDE { return true; } | |
| 37 virtual void Start(AudioSourceCallback* callback) OVERRIDE { | |
| 38 consumer_.Start(base::Bind(&AudioDiscarder::FetchAudioData, callback)); | |
| 39 } | |
| 40 virtual void Stop() OVERRIDE { consumer_.Stop(); } | |
| 41 virtual void SetVolume(double volume) OVERRIDE {} | |
| 42 virtual void GetVolume(double* volume) OVERRIDE { *volume = 0; } | |
| 43 virtual void Close() OVERRIDE { delete this; } | |
| 44 | |
| 45 private: | |
| 46 virtual ~AudioDiscarder() {} | |
| 47 | |
| 48 static void FetchAudioData(AudioSourceCallback* callback, | |
| 49 media::AudioBus* audio_bus) { | |
| 50 callback->OnMoreData(audio_bus, media::AudioBuffersState()); | |
| 51 } | |
| 52 | |
| 53 // Calls FetchAudioData() at regular intervals and discards the data. | |
| 54 media::FakeAudioConsumer consumer_; | |
| 55 | |
| 56 DISALLOW_COPY_AND_ASSIGN(AudioDiscarder); | |
| 57 }; | |
| 58 | |
| 59 } // namespace | |
| 60 | |
| 61 // A simple AudioMirroringManager::MirroringDestination implementation that | |
| 62 // identifies the audio streams rendered by a WebContents and provides | |
| 63 // AudioDiscarders to AudioMirroringManager. | |
| 64 class WebContentsAudioMuter::MuteDestination | |
| 65 : public base::RefCountedThreadSafe<MuteDestination>, | |
|
DaleCurtis
2014/09/22 17:45:45
I think it's easier to reason about if you just us
miu
2014/09/22 19:44:58
There are tasks posted to two different threads th
| |
| 66 public AudioMirroringManager::MirroringDestination { | |
| 67 public: | |
| 68 explicit MuteDestination(WebContents* web_contents) | |
| 69 : web_contents_(web_contents) {} | |
| 70 | |
| 71 private: | |
| 72 friend class base::RefCountedThreadSafe<MuteDestination>; | |
| 73 | |
| 74 typedef AudioMirroringManager::SourceFrameRef SourceFrameRef; | |
| 75 | |
| 76 virtual ~MuteDestination() {} | |
| 77 | |
| 78 virtual void QueryForMatches( | |
| 79 const std::set<SourceFrameRef>& candidates, | |
| 80 const MatchesCallback& results_callback) OVERRIDE { | |
| 81 BrowserThread::PostTask( | |
| 82 BrowserThread::UI, | |
| 83 FROM_HERE, | |
| 84 base::Bind(&MuteDestination::QueryForMatchesOnUIThread, | |
| 85 this, | |
| 86 candidates, | |
| 87 media::BindToCurrentLoop(results_callback))); | |
| 88 } | |
| 89 | |
| 90 void QueryForMatchesOnUIThread(const std::set<SourceFrameRef>& candidates, | |
| 91 const MatchesCallback& results_callback) { | |
| 92 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 93 std::set<SourceFrameRef> matches; | |
| 94 // Add each ID to |matches| if it maps to a RenderFrameHost that maps to the | |
| 95 // WebContents being muted. | |
| 96 for (std::set<SourceFrameRef>::const_iterator i = candidates.begin(); | |
| 97 i != candidates.end(); ++i) { | |
| 98 WebContents* const contents_containing_frame = | |
| 99 WebContents::FromRenderFrameHost( | |
| 100 RenderFrameHost::FromID(i->first, i->second)); | |
| 101 if (contents_containing_frame == web_contents_) | |
| 102 matches.insert(*i); | |
| 103 } | |
| 104 results_callback.Run(matches); | |
| 105 } | |
| 106 | |
| 107 virtual media::AudioOutputStream* AddInput( | |
| 108 const media::AudioParameters& params) OVERRIDE { | |
| 109 return new AudioDiscarder(params); | |
| 110 } | |
| 111 | |
| 112 WebContents* const web_contents_; | |
| 113 | |
| 114 DISALLOW_COPY_AND_ASSIGN(MuteDestination); | |
| 115 }; | |
| 116 | |
| 117 | |
|
DaleCurtis
2014/09/22 17:45:45
Extra line.
miu
2014/09/22 19:44:58
Done.
| |
| 118 WebContentsAudioMuter::WebContentsAudioMuter(WebContents* web_contents) | |
| 119 : destination_(new MuteDestination(web_contents)), is_muting_(false) { | |
|
DaleCurtis
2014/09/22 17:45:45
Stack allocate?
miu
2014/09/22 19:44:58
Not sure what you mean. If you're suggesting I ma
| |
| 120 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 121 } | |
| 122 | |
| 123 WebContentsAudioMuter::~WebContentsAudioMuter() { | |
| 124 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 125 StopMuting(); | |
| 126 } | |
| 127 | |
| 128 void WebContentsAudioMuter::StartMuting() { | |
| 129 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 130 if (is_muting_) | |
| 131 return; | |
| 132 is_muting_ = true; | |
| 133 BrowserThread::PostTask( | |
| 134 BrowserThread::IO, | |
| 135 FROM_HERE, | |
| 136 base::Bind(&AudioMirroringManager::StartMirroring, | |
| 137 base::Unretained(AudioMirroringManager::GetInstance()), | |
| 138 destination_)); | |
| 139 } | |
| 140 | |
| 141 void WebContentsAudioMuter::StopMuting() { | |
| 142 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 143 if (!is_muting_) | |
| 144 return; | |
| 145 is_muting_ = false; | |
| 146 BrowserThread::PostTask( | |
| 147 BrowserThread::IO, | |
| 148 FROM_HERE, | |
| 149 base::Bind(&AudioMirroringManager::StopMirroring, | |
| 150 base::Unretained(AudioMirroringManager::GetInstance()), | |
| 151 destination_)); | |
| 152 } | |
| 153 | |
| 154 } // namespace content | |
| OLD | NEW |