Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(638)

Side by Side Diff: content/browser/media/capture/web_contents_audio_muter.cc

Issue 586303004: WebContentsAudioMuter: Mute all audio output from a WebContentsImpl. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed Dale's comments. Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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>,
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 WebContentsAudioMuter::WebContentsAudioMuter(WebContents* web_contents)
118 : destination_(new MuteDestination(web_contents)), is_muting_(false) {
119 DCHECK_CURRENTLY_ON(BrowserThread::UI);
120 }
121
122 WebContentsAudioMuter::~WebContentsAudioMuter() {
123 DCHECK_CURRENTLY_ON(BrowserThread::UI);
124 StopMuting();
125 }
126
127 void WebContentsAudioMuter::StartMuting() {
128 DCHECK_CURRENTLY_ON(BrowserThread::UI);
129 if (is_muting_)
130 return;
131 is_muting_ = true;
132 BrowserThread::PostTask(
133 BrowserThread::IO,
134 FROM_HERE,
135 base::Bind(&AudioMirroringManager::StartMirroring,
136 base::Unretained(AudioMirroringManager::GetInstance()),
137 destination_));
138 }
139
140 void WebContentsAudioMuter::StopMuting() {
141 DCHECK_CURRENTLY_ON(BrowserThread::UI);
142 if (!is_muting_)
143 return;
144 is_muting_ = false;
145 BrowserThread::PostTask(
146 BrowserThread::IO,
147 FROM_HERE,
148 base::Bind(&AudioMirroringManager::StopMirroring,
149 base::Unretained(AudioMirroringManager::GetInstance()),
150 destination_));
151 }
152
153 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/media/capture/web_contents_audio_muter.h ('k') | content/browser/web_contents/web_contents_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698