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

Side by Side Diff: content/browser/renderer_host/media/audio_renderer_host.h

Issue 1323403005: Allow AudioOutputDevice objects to be initialized with a specific hardware output device and store … (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 5 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 // 4 //
5 // AudioRendererHost serves audio related requests from AudioRenderer which 5 // AudioRendererHost serves audio related requests from AudioRenderer which
6 // lives inside the render process and provide access to audio hardware. 6 // lives inside the render process and provide access to audio hardware.
7 // 7 //
8 // This class is owned by BrowserRenderProcessHost, and instantiated on UI 8 // This class is owned by BrowserRenderProcessHost, and instantiated on UI
9 // thread, but all other operations and method calls happen on IO thread, so we 9 // thread, but all other operations and method calls happen on IO thread, so we
10 // need to be extra careful about the lifetime of this object. AudioManager is a 10 // need to be extra careful about the lifetime of this object. AudioManager is a
11 // singleton and created in IO thread, audio output streams are also created in 11 // singleton and created in IO thread, audio output streams are also created in
12 // the IO thread, so we need to destroy them also in IO thread. After this class 12 // the IO thread, so we need to destroy them also in IO thread. After this class
13 // is created, a task of OnInitialized() is posted on IO thread in which 13 // is created, a task of OnInitialized() is posted on IO thread in which
14 // singleton of AudioManager is created. 14 // singleton of AudioManager is created.
15 // 15 //
16 // Here's an example of a typical IPC dialog for audio: 16 // Here's an example of a typical IPC dialog for audio:
17 // 17 //
18 // Renderer AudioRendererHost 18 // Renderer AudioRendererHost
19 // | | 19 // | |
20 // | RequestDeviceAuthorization > |
21 // | < NotifyDeviceAuthorized |
22 // | |
20 // | CreateStream > | 23 // | CreateStream > |
21 // | < NotifyStreamCreated | 24 // | < NotifyStreamCreated |
22 // | | 25 // | |
23 // | PlayStream > | 26 // | PlayStream > |
24 // | < NotifyStreamStateChanged | kAudioStreamPlaying 27 // | < NotifyStreamStateChanged | kAudioStreamPlaying
25 // | | 28 // | |
26 // | PauseStream > | 29 // | PauseStream > |
27 // | < NotifyStreamStateChanged | kAudioStreamPaused 30 // | < NotifyStreamStateChanged | kAudioStreamPaused
28 // | | 31 // | |
29 // | PlayStream > | 32 // | PlayStream > |
30 // | < NotifyStreamStateChanged | kAudioStreamPlaying 33 // | < NotifyStreamStateChanged | kAudioStreamPlaying
31 // | ... | 34 // | ... |
32 // | CloseStream > | 35 // | CloseStream > |
33 // v v 36 // v v
34 37
35 // A SyncSocket pair is used to signal buffer readiness between processes. 38 // A SyncSocket pair is used to signal buffer readiness between processes.
36 39
37 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ 40 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_
38 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ 41 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_
39 42
40 #include <map> 43 #include <map>
41 #include <string> 44 #include <string>
45 #include <utility>
42 46
43 #include "base/atomic_ref_count.h" 47 #include "base/atomic_ref_count.h"
44 #include "base/gtest_prod_util.h" 48 #include "base/gtest_prod_util.h"
45 #include "base/memory/ref_counted.h" 49 #include "base/memory/ref_counted.h"
46 #include "base/memory/scoped_ptr.h" 50 #include "base/memory/scoped_ptr.h"
47 #include "base/process/process.h" 51 #include "base/process/process.h"
48 #include "base/sequenced_task_runner_helpers.h" 52 #include "base/sequenced_task_runner_helpers.h"
53 #include "content/browser/renderer_host/media/audio_output_device_enumerator.h"
49 #include "content/common/content_export.h" 54 #include "content/common/content_export.h"
50 #include "content/public/browser/browser_message_filter.h" 55 #include "content/public/browser/browser_message_filter.h"
51 #include "content/public/browser/browser_thread.h" 56 #include "content/public/browser/browser_thread.h"
52 #include "content/public/browser/render_process_host.h" 57 #include "content/public/browser/render_process_host.h"
53 #include "content/public/browser/resource_context.h" 58 #include "content/public/browser/resource_context.h"
54 #include "media/audio/audio_io.h" 59 #include "media/audio/audio_io.h"
55 #include "media/audio/audio_logging.h" 60 #include "media/audio/audio_logging.h"
56 #include "media/audio/audio_output_controller.h" 61 #include "media/audio/audio_output_controller.h"
57 #include "media/audio/simple_sources.h" 62 #include "media/audio/simple_sources.h"
58 #include "url/gurl.h" 63 #include "url/gurl.h"
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 friend class BrowserThread; 108 friend class BrowserThread;
104 friend class base::DeleteHelper<AudioRendererHost>; 109 friend class base::DeleteHelper<AudioRendererHost>;
105 friend class MockAudioRendererHost; 110 friend class MockAudioRendererHost;
106 friend class TestAudioRendererHost; 111 friend class TestAudioRendererHost;
107 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, CreateMockStream); 112 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, CreateMockStream);
108 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, MockStreamDataConversation); 113 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, MockStreamDataConversation);
109 114
110 class AudioEntry; 115 class AudioEntry;
111 typedef std::map<int, AudioEntry*> AudioEntryMap; 116 typedef std::map<int, AudioEntry*> AudioEntryMap;
112 117
118 // Internal callback type for access requests to output devices.
119 // |have_access| is true only if there is permission to access the device.
120 typedef base::Callback<void(bool have_access)> OutputDeviceAccessCB;
121
122 // Internal callback type for information requests about an output device.
123 // |success| indicates the operation was successful. If true, |device_info|
124 // contains data about the device.
125 typedef base::Callback<void(bool success,
126 const AudioOutputDeviceInfo& device_info)>
127 OutputDeviceInfoCB;
128
113 ~AudioRendererHost() override; 129 ~AudioRendererHost() override;
114 130
115 // Methods called on IO thread ---------------------------------------------- 131 // Methods called on IO thread ----------------------------------------------
116 132
117 // Audio related IPC message handlers. 133 // Audio related IPC message handlers.
118 134
119 // Creates an audio output stream with the specified format whose data is 135 // Request permission to use an output device for use by a stream produced
120 // produced by an entity in the RenderFrame referenced by |render_frame_id|. 136 // in the RenderFrame referenced by |render_frame_id|.
121 // |session_id| is used for unified IO to find out which input device to be 137 // |session_id| is used for unified IO to find out which input device to be
122 // opened for the stream. For clients that do not use unified IO, 138 // opened for the stream. For clients that do not use unified IO,
123 // |session_id| will be ignored. 139 // |session_id| will be ignored and the given |device_id| and
140 // |security_origin| will be used to select the output device.
141 // Upon completion of the process, the peer is notified with the device output
142 // parameters via the NotifyDeviceAuthorized message.
143 void OnRequestDeviceAuthorization(int stream_id,
144 int render_frame_id,
145 int session_id,
146 const std::string& device_id,
147 const GURL& security_origin);
148
149 // Creates an audio output stream with the specified format.
124 // Upon success/failure, the peer is notified via the NotifyStreamCreated 150 // Upon success/failure, the peer is notified via the NotifyStreamCreated
125 // message. 151 // message.
126 void OnCreateStream(int stream_id, 152 void OnCreateStream(int stream_id,
127 int render_frame_id, 153 int render_frame_id,
128 int session_id,
129 const media::AudioParameters& params); 154 const media::AudioParameters& params);
130 155
131 // Play the audio stream referenced by |stream_id|. 156 // Play the audio stream referenced by |stream_id|.
132 void OnPlayStream(int stream_id); 157 void OnPlayStream(int stream_id);
133 158
134 // Pause the audio stream referenced by |stream_id|. 159 // Pause the audio stream referenced by |stream_id|.
135 void OnPauseStream(int stream_id); 160 void OnPauseStream(int stream_id);
136 161
137 // Close the audio stream referenced by |stream_id|. 162 // Close the audio stream referenced by |stream_id|.
138 void OnCloseStream(int stream_id); 163 void OnCloseStream(int stream_id);
139 164
140 // Set the volume of the audio stream referenced by |stream_id|. 165 // Set the volume of the audio stream referenced by |stream_id|.
141 void OnSetVolume(int stream_id, double volume); 166 void OnSetVolume(int stream_id, double volume);
142 167
143 // Set the output device of the audio stream referenced by |stream_id|. 168 // Set the output device of the audio stream referenced by |stream_id|.
144 void OnSwitchOutputDevice(int stream_id, 169 void OnSwitchOutputDevice(int stream_id,
145 int render_frame_id, 170 int render_frame_id,
146 const std::string& device_id, 171 const std::string& device_id,
147 const GURL& security_origin, 172 const GURL& security_origin);
148 int request_id);
149 173
150 void OutputDeviceAccessChecked(scoped_ptr<MediaStreamUIProxy> ui_proxy, 174 // Helper methods.
151 int stream_id,
152 const std::string& device_id,
153 const GURL& security_origin,
154 int render_frame_id,
155 int request_id,
156 bool have_access);
157 175
158 void StartTranslateOutputDeviceName(int stream_id, 176 // Proceed with device authorization after checking permissions.
159 const std::string& device_id, 177 void RequestDeviceAuthorizationAccessChecked(int stream_id,
DaleCurtis 2015/09/12 01:17:19 Perhaps something simpler like RequestDeviceAuthor
Guido Urdaneta 2015/09/14 11:35:48 Done. Now OnDeviceAuthorized()
160 const GURL& security_origin, 178 const std::string& device_id,
161 int request_id); 179 const GURL& security_origin,
180 bool have_access);
162 181
163 void FinishTranslateOutputDeviceName(int stream_id, 182 // Proceed with device authorization after translating device ID.
164 const std::string& device_id, 183 void RequestDeviceAuthorizationTranslated(
165 const GURL& security_origin, 184 int stream_id,
166 int request_id, 185 bool device_found,
167 media::AudioDeviceNames*); 186 const AudioOutputDeviceInfo& device_info);
168 187
169 void DoSwitchOutputDevice(int stream_id, 188 // Start the actual creation of an audio stream, after the device
170 const std::string& raw_device_id, 189 // authorization process is complete.
171 int request_id); 190 void DoCreateStream(int stream_id,
172 191 int render_frame_id,
173 void DoOutputDeviceSwitched(int stream_id, int request_id); 192 const media::AudioParameters& params,
193 const std::string& device_unique_id);
174 194
175 // Complete the process of creating an audio stream. This will set up the 195 // Complete the process of creating an audio stream. This will set up the
176 // shared memory or shared socket in low latency mode and send the 196 // shared memory or shared socket in low latency mode and send the
177 // NotifyStreamCreated message to the peer. 197 // NotifyStreamCreated message to the peer.
178 void DoCompleteCreation(int stream_id); 198 void DoCompleteCreation(int stream_id);
179 199
180 // Send playing/paused status to the renderer. 200 // Send playing/paused status to the renderer.
181 void DoNotifyStreamStateChanged(int stream_id, bool is_playing); 201 void DoNotifyStreamStateChanged(int stream_id, bool is_playing);
182 202
203 // Proceed with output device switching after checking permissions.
204 void SwitchOutputDeviceAccessChecked(int stream_id,
205 const std::string& device_id,
206 const GURL& security_origin,
207 bool have_access);
208
209 // Proceed with output device switching after translating device ID.
210 void SwitchOutputDeviceTranslated(int stream_id,
211 bool device_found,
212 const AudioOutputDeviceInfo& device_info);
213
214 // Finish handling the output device switch request, after the device has
215 // been switched.
216 void OutputDeviceSwitched(int stream_id,
217 const media::AudioParameters& output_params);
218
183 RenderProcessHost::AudioOutputControllerList DoGetOutputControllers() const; 219 RenderProcessHost::AudioOutputControllerList DoGetOutputControllers() const;
184 220
185 // Send an error message to the renderer. 221 // Send an error message to the renderer.
186 void SendErrorMessage(int stream_id); 222 void SendErrorMessage(int stream_id);
187 223
188 // Delete an audio entry, notifying observers first. This is called by 224 // Delete an audio entry, notifying observers first. This is called by
189 // AudioOutputController after it has closed. 225 // AudioOutputController after it has closed.
190 void DeleteEntry(scoped_ptr<AudioEntry> entry); 226 void DeleteEntry(scoped_ptr<AudioEntry> entry);
191 227
192 // Send an error message to the renderer, then close the stream. 228 // Send an error message to the renderer, then close the stream.
193 void ReportErrorAndClose(int stream_id); 229 void ReportErrorAndClose(int stream_id);
194 230
195 // A helper method to look up a AudioEntry identified by |stream_id|. 231 // A helper method to look up a AudioEntry identified by |stream_id|.
196 // Returns NULL if not found. 232 // Returns NULL if not found.
197 AudioEntry* LookupById(int stream_id); 233 AudioEntry* LookupById(int stream_id);
198 234
199 // A helper method to update the number of playing streams and alert the 235 // A helper method to update the number of playing streams and alert the
200 // ResourceScheduler when the renderer starts or stops playing an audiostream. 236 // ResourceScheduler when the renderer starts or stops playing an audiostream.
201 void UpdateNumPlayingStreams(AudioEntry* entry, bool is_playing); 237 void UpdateNumPlayingStreams(AudioEntry* entry, bool is_playing);
202 238
203 // Checks that the renderer process supplies a URL it is allowed to use 239 // Checks that the renderer process supplies a URL it is allowed to use
204 bool IsURLAllowed(const GURL& url); 240 bool IsURLAllowed(const GURL& url);
205 241
242 // Check if the renderer process has access to the requested output device.
243 void CheckOutputDeviceAccess(int render_frame_id,
244 const std::string& device_id,
245 const GURL& security_origin,
246 const OutputDeviceAccessCB& callback);
247
248 // Invoke |callback| after permission to use a device has been checked.
249 void AccessChecked(scoped_ptr<MediaStreamUIProxy> ui_proxy,
250 const OutputDeviceAccessCB& callback,
251 bool have_access);
252
253 // Check if the renderer process has access to the requested output device.
254 void TranslateDeviceID(const std::string& device_id,
255 const GURL& security_origin,
256 const OutputDeviceInfoCB& callback);
257
258 void FinishTranslateDeviceID(
259 const std::string& device_id,
260 const GURL& security_origin,
261 const OutputDeviceInfoCB& callback,
262 const AudioOutputDeviceEnumeration& device_infos);
263
264 // Helper method to check if the authorization procedure for stream
265 // |stream_id| has started.
266 bool IsAuthorizationStarted(int stream_id);
267
206 // ID of the RenderProcessHost that owns this instance. 268 // ID of the RenderProcessHost that owns this instance.
207 const int render_process_id_; 269 const int render_process_id_;
208 270
209 media::AudioManager* const audio_manager_; 271 media::AudioManager* const audio_manager_;
210 AudioMirroringManager* const mirroring_manager_; 272 AudioMirroringManager* const mirroring_manager_;
211 scoped_ptr<media::AudioLog> audio_log_; 273 scoped_ptr<media::AudioLog> audio_log_;
212 274
213 // Used to access to AudioInputDeviceManager. 275 // Used to access to AudioInputDeviceManager.
214 MediaStreamManager* media_stream_manager_; 276 MediaStreamManager* media_stream_manager_;
215 277
216 // A map of stream IDs to audio sources. 278 // A map of stream IDs to audio sources.
217 AudioEntryMap audio_entries_; 279 AudioEntryMap audio_entries_;
218 280
219 // The number of streams in the playing state. 281 // The number of streams in the playing state.
220 base::AtomicRefCount num_playing_streams_; 282 base::AtomicRefCount num_playing_streams_;
221 283
222 // Salt required to translate renderer device IDs to raw device IDs 284 // Salt required to translate renderer device IDs to raw device unique IDs
223 ResourceContext::SaltCallback salt_callback_; 285 ResourceContext::SaltCallback salt_callback_;
224 286
287 // Map of device authorizations for streams that are not yet created
288 // The key is the stream ID, and the value is a pair. The pair's first element
289 // is a bool that is true if the authorization process completes successfully.
290 // The second element contains the unique ID of the authorized device.
291 std::map<int, std::pair<bool, std::string>> authorizations_;
292
225 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); 293 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost);
226 }; 294 };
227 295
228 } // namespace content 296 } // namespace content
229 297
230 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ 298 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698