OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | |
Avi (use Gerrit)
2014/09/21 00:20:00
No "(c)" on new files.
miu
2014/09/21 02:33:30
Done.
| |
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. | |
Avi (use Gerrit)
2014/09/21 00:20:00
"Once that's fixed"... is there a bug to do that a
miu
2014/09/21 02:33:30
Done.
| |
29 class AudioDiscarder : public media::AudioOutputStream { | |
30 public: | |
31 explicit AudioDiscarder(const media::AudioParameters& params) | |
32 : consumer_(media::AudioManager::Get()->GetWorkerTaskRunner(), params) {} | |
33 | |
34 // AudioOutputStream implementation. | |
35 virtual bool Open() OVERRIDE { return true; } | |
36 virtual void Start(AudioSourceCallback* callback) OVERRIDE { | |
37 consumer_.Start(base::Bind(&AudioDiscarder::FetchAudioData, callback)); | |
38 } | |
39 virtual void Stop() OVERRIDE { consumer_.Stop(); } | |
40 virtual void SetVolume(double volume) OVERRIDE {} | |
41 virtual void GetVolume(double* volume) OVERRIDE { *volume = 0; } | |
42 virtual void Close() OVERRIDE { delete this; } | |
43 | |
44 private: | |
45 virtual ~AudioDiscarder() {} | |
46 | |
47 static void FetchAudioData(AudioSourceCallback* callback, | |
48 media::AudioBus* audio_bus) { | |
49 callback->OnMoreData(audio_bus, media::AudioBuffersState()); | |
50 } | |
51 | |
52 // Calls FetchAudioData() at regular intervals and discards the data. | |
53 media::FakeAudioConsumer consumer_; | |
54 | |
55 DISALLOW_COPY_AND_ASSIGN(AudioDiscarder); | |
56 }; | |
57 | |
58 } // namespace | |
59 | |
60 // A simple AudioMirroringManager::MirroringDestination implementation that | |
61 // identifies the audio streams rendered by a WebContents and provides | |
62 // AudioDiscarders to AudioMirroringManager. | |
63 class WebContentsAudioMuter::MuteDestination | |
64 : public base::RefCountedThreadSafe<MuteDestination>, | |
65 public AudioMirroringManager::MirroringDestination { | |
66 public: | |
67 explicit MuteDestination(WebContents* web_contents) | |
68 : web_contents_(web_contents) {} | |
69 | |
70 private: | |
71 friend class base::RefCountedThreadSafe<MuteDestination>; | |
72 | |
73 typedef AudioMirroringManager::SourceFrameRef SourceFrameRef; | |
74 | |
75 virtual ~MuteDestination() {} | |
76 | |
77 virtual void QueryForMatches( | |
78 const std::set<SourceFrameRef>& candidates, | |
79 const MatchesCallback& results_callback) OVERRIDE { | |
80 BrowserThread::PostTask( | |
81 BrowserThread::UI, | |
82 FROM_HERE, | |
83 base::Bind(&MuteDestination::QueryForMatchesOnUIThread, | |
84 this, | |
85 candidates, | |
86 media::BindToCurrentLoop(results_callback))); | |
87 } | |
88 | |
89 void QueryForMatchesOnUIThread(const std::set<SourceFrameRef>& candidates, | |
90 const MatchesCallback& results_callback) { | |
91 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
92 std::set<SourceFrameRef> matches; | |
93 // Add each ID to |matches| if it maps to a RenderFrameHost that maps to the | |
94 // WebContents being muted. | |
95 for (std::set<SourceFrameRef>::const_iterator i = candidates.begin(); | |
96 i != candidates.end(); ++i) { | |
97 WebContents* const contents_containing_frame = | |
98 WebContents::FromRenderFrameHost( | |
99 RenderFrameHost::FromID(i->first, i->second)); | |
100 if (contents_containing_frame == web_contents_) | |
101 matches.insert(*i); | |
102 } | |
103 results_callback.Run(matches); | |
104 } | |
105 | |
106 virtual media::AudioOutputStream* AddInput( | |
107 const media::AudioParameters& params) OVERRIDE { | |
108 return new AudioDiscarder(params); | |
109 } | |
110 | |
111 WebContents* const web_contents_; | |
112 | |
113 DISALLOW_COPY_AND_ASSIGN(MuteDestination); | |
114 }; | |
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 | |
OLD | NEW |