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.
5 #include "content/browser/media/capture/web_contents_audio_muter.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"
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.
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
{
32 explicit AudioDiscarder(const media::AudioParameters
& params
)
33 : consumer_(media::AudioManager::Get()->GetWorkerTaskRunner(), params
) {}
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
));
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; }
46 virtual ~AudioDiscarder() {}
48 static void FetchAudioData(AudioSourceCallback
* callback
,
49 media::AudioBus
* audio_bus
) {
50 callback
->OnMoreData(audio_bus
, media::AudioBuffersState());
53 // Calls FetchAudioData() at regular intervals and discards the data.
54 media::FakeAudioConsumer consumer_
;
56 DISALLOW_COPY_AND_ASSIGN(AudioDiscarder
);
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
>,
66 public AudioMirroringManager::MirroringDestination
{
68 explicit MuteDestination(WebContents
* web_contents
)
69 : web_contents_(web_contents
) {}
72 friend class base::RefCountedThreadSafe
<MuteDestination
>;
74 typedef AudioMirroringManager::SourceFrameRef SourceFrameRef
;
76 virtual ~MuteDestination() {}
78 virtual void QueryForMatches(
79 const std::set
<SourceFrameRef
>& candidates
,
80 const MatchesCallback
& results_callback
) OVERRIDE
{
81 BrowserThread::PostTask(
84 base::Bind(&MuteDestination::QueryForMatchesOnUIThread
,
87 media::BindToCurrentLoop(results_callback
)));
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_
)
104 results_callback
.Run(matches
);
107 virtual media::AudioOutputStream
* AddInput(
108 const media::AudioParameters
& params
) OVERRIDE
{
109 return new AudioDiscarder(params
);
112 WebContents
* const web_contents_
;
114 DISALLOW_COPY_AND_ASSIGN(MuteDestination
);
117 WebContentsAudioMuter::WebContentsAudioMuter(WebContents
* web_contents
)
118 : destination_(new MuteDestination(web_contents
)), is_muting_(false) {
119 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
122 WebContentsAudioMuter::~WebContentsAudioMuter() {
123 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
127 void WebContentsAudioMuter::StartMuting() {
128 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
132 BrowserThread::PostTask(
135 base::Bind(&AudioMirroringManager::StartMirroring
,
136 base::Unretained(AudioMirroringManager::GetInstance()),
140 void WebContentsAudioMuter::StopMuting() {
141 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
145 BrowserThread::PostTask(
148 base::Bind(&AudioMirroringManager::StopMirroring
,
149 base::Unretained(AudioMirroringManager::GetInstance()),
153 } // namespace content