OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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_output_delegate.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "content/browser/media/audio_stream_monitor.h" | |
9 #include "content/browser/media/media_internals.h" | |
10 #include "content/browser/renderer_host/media/audio_sync_reader.h" | |
11 #include "content/public/browser/browser_thread.h" | |
12 | |
13 namespace { | |
14 void RemoveDiverter(content::AudioMirroringManager* mirroring_manager, | |
15 scoped_refptr<media::AudioOutputController> controller) { | |
16 // De-register the controller from the AudioMirroringManager now that the | |
17 // controller has closed the AudioOutputStream and shut itself down. This | |
18 // ensures that calling RemoveDiverter() here won't trigger the controller to | |
19 // re-start the default AudioOutputStream and cause a brief audio blip to come | |
20 // out the user's speakers. http://crbug.com/474432 | |
21 mirroring_manager->RemoveDiverter(controller.get()); | |
22 } | |
23 } // namespace | |
24 | |
25 namespace content { | |
26 | |
27 AudioOutputDelegate::AudioOutputDelegate( | |
28 EventHandler* handler, | |
29 media::AudioLog* audio_log, | |
30 AudioMirroringManager* mirroring_manager, | |
31 MediaObserver* media_observer, | |
32 int stream_id, | |
33 int render_frame_id, | |
34 int render_process_id, | |
35 media::AudioParameters params, | |
36 const std::string& output_device_id) | |
37 : handler_(handler), | |
38 audio_log_(audio_log), | |
o1ka
2016/10/25 12:32:34
It's probably fine to have an individual audio_log
Max Morin
2016/10/26 09:20:17
Done.
| |
39 mirroring_manager_(mirroring_manager), | |
40 controller_(media::AudioOutputController::Create( | |
41 media::AudioManager::Get(), | |
42 this, | |
43 params, | |
44 output_device_id, | |
45 AudioSyncReader::Create(params))), | |
46 stream_id_(stream_id), | |
47 render_frame_id_(render_frame_id), | |
48 render_process_id_(render_process_id), | |
49 weak_factory_(this) { | |
50 DCHECK(controller_); | |
51 if (media_observer) | |
52 media_observer->OnCreatingAudioStream(render_process_id_, render_frame_id_); | |
53 if (mirroring_manager_) | |
54 mirroring_manager_->AddDiverter(render_process_id_, render_frame_id_, | |
55 controller_.get()); | |
56 audio_log_->OnCreated(stream_id, params, output_device_id); | |
57 MediaInternals::GetInstance()->SetWebContentsTitleForAudioLogEntry( | |
58 stream_id_, render_process_id_, render_frame_id_, audio_log_); | |
59 } | |
60 | |
61 AudioOutputDelegate::~AudioOutputDelegate() {} | |
o1ka
2016/10/25 12:32:34
AudioOutputController is refcounted, so it may now
Max Morin
2016/10/26 09:20:17
Done.
| |
62 | |
63 void AudioOutputDelegate::OnPlayStream() { | |
64 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
65 controller_->Play(); | |
66 audio_log_->OnStarted(stream_id_); | |
67 } | |
68 | |
69 void AudioOutputDelegate::OnPauseStream() { | |
70 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
71 controller_->Pause(); | |
72 audio_log_->OnStopped(stream_id_); | |
73 } | |
74 | |
75 void AudioOutputDelegate::OnSetVolume(double volume) { | |
76 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
77 controller_->SetVolume(volume); | |
78 audio_log_->OnSetVolume(stream_id_, volume); | |
79 } | |
80 | |
81 void AudioOutputDelegate::OnCloseStream() { | |
82 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
83 if (playing_) { | |
84 handler_->OnStreamStateChanged(false); | |
85 playing_ = false; | |
86 } | |
87 AudioStreamMonitor::StopMonitoringStream(render_process_id_, render_frame_id_, | |
88 stream_id_); | |
89 if (mirroring_manager_) | |
90 controller_->Close(base::Bind( | |
91 &RemoveDiverter, base::Unretained(mirroring_manager_), controller_)); | |
o1ka
2016/10/25 12:32:34
Unretained is dangerous: no obvious guarantee that
Max Morin
2016/10/26 09:20:17
Done.
| |
92 else | |
93 controller_->Close(base::Bind([]() {})); | |
o1ka
2016/10/25 12:32:34
base::Bind(&DoNothing) is a common way
https://cs.
Max Morin
2016/10/26 09:20:17
Acknowledged.
| |
94 audio_log_->OnClosed(stream_id_); | |
95 } | |
96 | |
97 void AudioOutputDelegate::OnControllerCreated() { | |
98 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | |
99 BrowserThread::PostTask( | |
100 BrowserThread::IO, FROM_HERE, | |
101 base::Bind(&AudioOutputDelegate::OnControllerCreated, | |
102 weak_factory_.GetWeakPtr())); | |
103 return; | |
104 } | |
105 AudioSyncReader* reader = | |
106 static_cast<AudioSyncReader*>(controller_->reader()); | |
107 handler_->OnStreamCreated(stream_id_, reader->shared_memory(), | |
108 reader->foreign_socket()); | |
109 } | |
110 | |
111 void AudioOutputDelegate::OnControllerPlaying() { | |
112 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | |
113 BrowserThread::PostTask( | |
114 BrowserThread::IO, FROM_HERE, | |
115 base::Bind(&AudioOutputDelegate::OnControllerPlaying, | |
116 weak_factory_.GetWeakPtr())); | |
117 return; | |
118 } | |
119 if (!playing_) { | |
120 handler_->OnStreamStateChanged(true); | |
121 playing_ = true; | |
122 } | |
123 AudioStreamMonitor::StartMonitoringStream( | |
124 render_process_id_, render_frame_id_, stream_id_, | |
125 base::Bind(&media::AudioOutputController::ReadCurrentPowerAndClip, | |
126 controller_)); | |
127 } | |
128 | |
129 void AudioOutputDelegate::OnControllerPaused() { | |
130 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | |
131 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
132 base::Bind(&AudioOutputDelegate::OnControllerPaused, | |
133 weak_factory_.GetWeakPtr())); | |
134 return; | |
135 } | |
136 if (playing_) { | |
137 handler_->OnStreamStateChanged(false); | |
138 playing_ = false; | |
139 } | |
140 AudioStreamMonitor::StopMonitoringStream(render_process_id_, render_frame_id_, | |
141 stream_id_); | |
142 } | |
143 | |
144 void AudioOutputDelegate::OnControllerError() { | |
145 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | |
146 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
147 base::Bind(&AudioOutputDelegate::OnControllerError, | |
148 weak_factory_.GetWeakPtr())); | |
149 return; | |
150 } | |
151 audio_log_->OnError(stream_id_); | |
152 handler_->OnStreamError(stream_id_); | |
153 } | |
154 | |
155 } // namespace content | |
OLD | NEW |