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

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: Split permissions check and device-ID translation for clarity 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>
42 45
43 #include "base/atomic_ref_count.h" 46 #include "base/atomic_ref_count.h"
44 #include "base/gtest_prod_util.h" 47 #include "base/gtest_prod_util.h"
45 #include "base/memory/ref_counted.h" 48 #include "base/memory/ref_counted.h"
46 #include "base/memory/scoped_ptr.h" 49 #include "base/memory/scoped_ptr.h"
47 #include "base/process/process.h" 50 #include "base/process/process.h"
48 #include "base/sequenced_task_runner_helpers.h" 51 #include "base/sequenced_task_runner_helpers.h"
52 #include "content/browser/renderer_host/media/audio_output_device_enumerator.h"
49 #include "content/common/content_export.h" 53 #include "content/common/content_export.h"
50 #include "content/public/browser/browser_message_filter.h" 54 #include "content/public/browser/browser_message_filter.h"
51 #include "content/public/browser/browser_thread.h" 55 #include "content/public/browser/browser_thread.h"
52 #include "content/public/browser/render_process_host.h" 56 #include "content/public/browser/render_process_host.h"
53 #include "content/public/browser/resource_context.h" 57 #include "content/public/browser/resource_context.h"
54 #include "media/audio/audio_io.h" 58 #include "media/audio/audio_io.h"
55 #include "media/audio/audio_logging.h" 59 #include "media/audio/audio_logging.h"
56 #include "media/audio/audio_output_controller.h" 60 #include "media/audio/audio_output_controller.h"
57 #include "media/audio/simple_sources.h" 61 #include "media/audio/simple_sources.h"
58 #include "url/gurl.h" 62 #include "url/gurl.h"
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 friend class BrowserThread; 107 friend class BrowserThread;
104 friend class base::DeleteHelper<AudioRendererHost>; 108 friend class base::DeleteHelper<AudioRendererHost>;
105 friend class MockAudioRendererHost; 109 friend class MockAudioRendererHost;
106 friend class TestAudioRendererHost; 110 friend class TestAudioRendererHost;
107 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, CreateMockStream); 111 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, CreateMockStream);
108 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, MockStreamDataConversation); 112 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, MockStreamDataConversation);
109 113
110 class AudioEntry; 114 class AudioEntry;
111 typedef std::map<int, AudioEntry*> AudioEntryMap; 115 typedef std::map<int, AudioEntry*> AudioEntryMap;
112 116
117 // Internal callback type for access requests to output devices.
118 // |have_access| indicates if there is permission to access the device.
119 typedef base::Callback<void(bool have_access)> OutputDeviceAccessCB;
120
121 // Internal callback type for information requests about an output device.
122 // |success| indicates the operation was successful. If true, |device_info|
123 // contains data about the device.
124 typedef base::Callback<void(bool success,
125 const AudioOutputDeviceInfo& device_info)>
126 OutputDeviceInfoCB;
127
113 ~AudioRendererHost() override; 128 ~AudioRendererHost() override;
114 129
115 // Methods called on IO thread ---------------------------------------------- 130 // Methods called on IO thread ----------------------------------------------
116 131
117 // Audio related IPC message handlers. 132 // Audio related IPC message handlers.
118 133
119 // Creates an audio output stream with the specified format whose data is 134 // 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|. 135 // in the RenderFrame referenced by |render_frame_id|.
121 // |session_id| is used for unified IO to find out which input device to be 136 // |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, 137 // opened for the stream. For clients that do not use unified IO,
123 // |session_id| will be ignored. 138 // |session_id| will be ignored and the given |device_id| and
139 // |security_origin| will be used to select the output device.
140 // Upon completion of the process, the peer is notified with the device output
141 // parameters via the NotifyDeviceAuthorized message.
142 void OnRequestDeviceAuthorization(int stream_id,
143 int render_frame_id,
144 int session_id,
145 const std::string& device_id,
146 const GURL& security_origin);
147
148 // Creates an audio output stream with the specified format.
124 // Upon success/failure, the peer is notified via the NotifyStreamCreated 149 // Upon success/failure, the peer is notified via the NotifyStreamCreated
125 // message. 150 // message.
126 void OnCreateStream(int stream_id, 151 void OnCreateStream(int stream_id,
127 int render_frame_id, 152 int render_frame_id,
128 int session_id,
129 const media::AudioParameters& params); 153 const media::AudioParameters& params);
130 154
131 // Play the audio stream referenced by |stream_id|. 155 // Play the audio stream referenced by |stream_id|.
132 void OnPlayStream(int stream_id); 156 void OnPlayStream(int stream_id);
133 157
134 // Pause the audio stream referenced by |stream_id|. 158 // Pause the audio stream referenced by |stream_id|.
135 void OnPauseStream(int stream_id); 159 void OnPauseStream(int stream_id);
136 160
137 // Close the audio stream referenced by |stream_id|. 161 // Close the audio stream referenced by |stream_id|.
138 void OnCloseStream(int stream_id); 162 void OnCloseStream(int stream_id);
139 163
140 // Set the volume of the audio stream referenced by |stream_id|. 164 // Set the volume of the audio stream referenced by |stream_id|.
141 void OnSetVolume(int stream_id, double volume); 165 void OnSetVolume(int stream_id, double volume);
142 166
143 // Set the output device of the audio stream referenced by |stream_id|. 167 // Set the output device of the audio stream referenced by |stream_id|.
144 void OnSwitchOutputDevice(int stream_id, 168 void OnSwitchOutputDevice(int stream_id,
145 int render_frame_id, 169 int render_frame_id,
146 const std::string& device_id, 170 const std::string& device_id,
147 const GURL& security_origin, 171 const GURL& security_origin);
148 int request_id);
149 172
150 void OutputDeviceAccessChecked(scoped_ptr<MediaStreamUIProxy> ui_proxy, 173 // 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 174
158 void StartTranslateOutputDeviceName(int stream_id, 175 // Proceed with device authorization after checking permissions.
159 const std::string& device_id, 176 void RequestDeviceAuthorizationAccessChecked(int stream_id,
160 const GURL& security_origin, 177 const std::string& device_id,
161 int request_id); 178 const GURL& security_origin,
179 bool have_access);
162 180
163 void FinishTranslateOutputDeviceName(int stream_id, 181 // Proceed with device authorization after translating device ID.
164 const std::string& device_id, 182 void RequestDeviceAuthorizationTranslated(
165 const GURL& security_origin, 183 int stream_id,
166 int request_id, 184 bool device_found,
167 media::AudioDeviceNames*); 185 const AudioOutputDeviceInfo& device_info);
168 186
169 void DoSwitchOutputDevice(int stream_id, 187 // Start the actual creation of an audio stream, after the device
170 const std::string& raw_device_id, 188 // authorization process is complete.
171 int request_id); 189 void DoCreateStream(int stream_id,
172 190 int render_frame_id,
173 void DoOutputDeviceSwitched(int stream_id, int request_id); 191 const media::AudioParameters& params,
192 const std::string& device_unique_id);
174 193
175 // Complete the process of creating an audio stream. This will set up the 194 // 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 195 // shared memory or shared socket in low latency mode and send the
177 // NotifyStreamCreated message to the peer. 196 // NotifyStreamCreated message to the peer.
178 void DoCompleteCreation(int stream_id); 197 void DoCompleteCreation(int stream_id);
179 198
180 // Send playing/paused status to the renderer. 199 // Send playing/paused status to the renderer.
181 void DoNotifyStreamStateChanged(int stream_id, bool is_playing); 200 void DoNotifyStreamStateChanged(int stream_id, bool is_playing);
182 201
202 // Proceed with output device switching after checking permissions.
203 void SwitchOutputDeviceAccessChecked(int stream_id,
204 const std::string& device_id,
205 const GURL& security_origin,
206 bool have_access);
207
208 // Proceed with output device switching after translating device ID.
209 void SwitchOutputDeviceTranslated(int stream_id,
210 bool device_found,
211 const AudioOutputDeviceInfo& device_info);
212
213 // Finish handling the output device switch request, after the device has
214 // been switched.
215 void OutputDeviceSwitched(int stream_id,
216 const media::AudioParameters& output_params);
217
183 RenderProcessHost::AudioOutputControllerList DoGetOutputControllers() const; 218 RenderProcessHost::AudioOutputControllerList DoGetOutputControllers() const;
184 219
185 // Send an error message to the renderer. 220 // Send an error message to the renderer.
186 void SendErrorMessage(int stream_id); 221 void SendErrorMessage(int stream_id);
187 222
188 // Delete an audio entry, notifying observers first. This is called by 223 // Delete an audio entry, notifying observers first. This is called by
189 // AudioOutputController after it has closed. 224 // AudioOutputController after it has closed.
190 void DeleteEntry(scoped_ptr<AudioEntry> entry); 225 void DeleteEntry(scoped_ptr<AudioEntry> entry);
191 226
192 // Send an error message to the renderer, then close the stream. 227 // Send an error message to the renderer, then close the stream.
193 void ReportErrorAndClose(int stream_id); 228 void ReportErrorAndClose(int stream_id);
194 229
195 // A helper method to look up a AudioEntry identified by |stream_id|. 230 // A helper method to look up a AudioEntry identified by |stream_id|.
196 // Returns NULL if not found. 231 // Returns NULL if not found.
197 AudioEntry* LookupById(int stream_id); 232 AudioEntry* LookupById(int stream_id);
198 233
199 // A helper method to update the number of playing streams and alert the 234 // A helper method to update the number of playing streams and alert the
200 // ResourceScheduler when the renderer starts or stops playing an audiostream. 235 // ResourceScheduler when the renderer starts or stops playing an audiostream.
201 void UpdateNumPlayingStreams(AudioEntry* entry, bool is_playing); 236 void UpdateNumPlayingStreams(AudioEntry* entry, bool is_playing);
202 237
203 // Checks that the renderer process supplies a URL it is allowed to use 238 // Checks that the renderer process supplies a URL it is allowed to use
204 bool IsURLAllowed(const GURL& url); 239 bool IsURLAllowed(const GURL& url);
205 240
241 // Check if the renderer process has access to the requested output device.
242 void CheckOutputDeviceAccess(int render_frame_id,
243 const std::string& device_id,
244 const GURL& security_origin,
245 const OutputDeviceAccessCB& callback);
246
247 // Invoke |callback| after permission to use a device has been checked.
248 void AccessChecked(scoped_ptr<MediaStreamUIProxy> ui_proxy,
249 const OutputDeviceAccessCB& callback,
250 bool have_access);
251
252 // Check if the renderer process has access to the requested output device.
253 void TranslateDeviceID(const std::string& device_id,
254 const GURL& security_origin,
255 const OutputDeviceInfoCB& callback);
256
257 void FinishTranslateDeviceID(
258 const std::string& device_id,
259 const GURL& security_origin,
260 const OutputDeviceInfoCB& callback,
261 const AudioOutputDeviceEnumeration& device_infos);
262
263 // Helper method to check if the authorization procedure for stream
264 // |stream_id| has started.
265 bool IsAuthorizationStarted(int stream_id);
266
206 // ID of the RenderProcessHost that owns this instance. 267 // ID of the RenderProcessHost that owns this instance.
207 const int render_process_id_; 268 const int render_process_id_;
208 269
209 media::AudioManager* const audio_manager_; 270 media::AudioManager* const audio_manager_;
210 AudioMirroringManager* const mirroring_manager_; 271 AudioMirroringManager* const mirroring_manager_;
211 scoped_ptr<media::AudioLog> audio_log_; 272 scoped_ptr<media::AudioLog> audio_log_;
212 273
213 // Used to access to AudioInputDeviceManager. 274 // Used to access to AudioInputDeviceManager.
214 MediaStreamManager* media_stream_manager_; 275 MediaStreamManager* media_stream_manager_;
215 276
216 // A map of stream IDs to audio sources. 277 // A map of stream IDs to audio sources.
217 AudioEntryMap audio_entries_; 278 AudioEntryMap audio_entries_;
218 279
219 // The number of streams in the playing state. 280 // The number of streams in the playing state.
220 base::AtomicRefCount num_playing_streams_; 281 base::AtomicRefCount num_playing_streams_;
221 282
222 // Salt required to translate renderer device IDs to raw device IDs 283 // Salt required to translate renderer device IDs to raw device unique IDs
223 ResourceContext::SaltCallback salt_callback_; 284 ResourceContext::SaltCallback salt_callback_;
224 285
286 // Map of device authorizations for streams that are not yet created
287 class AuthorizationData;
288 std::map<int, AuthorizationData> authorizations_;
289
225 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); 290 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost);
226 }; 291 };
227 292
228 } // namespace content 293 } // namespace content
229 294
230 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ 295 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698