| OLD | NEW |
| 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/origin.h" |
| 59 | 64 |
| 60 namespace media { | 65 namespace media { |
| 61 class AudioManager; | 66 class AudioManager; |
| 62 class AudioParameters; | 67 class AudioParameters; |
| 63 } | 68 } |
| 64 | 69 |
| 65 namespace content { | 70 namespace content { |
| 66 | 71 |
| 67 class AudioMirroringManager; | 72 class AudioMirroringManager; |
| 68 class MediaInternals; | 73 class MediaInternals; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 url::Origin& 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 url::Origin& 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 OnDeviceAuthorized(int stream_id, |
| 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 OnDeviceIDTranslated(int stream_id, |
| 165 const GURL& security_origin, | 184 bool device_found, |
| 166 int request_id, | 185 const AudioOutputDeviceInfo& device_info); |
| 167 media::AudioDeviceNames*); | |
| 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 OnSwitchDeviceAuthorized(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 OnSwitchDeviceIDTranslated(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 OnDeviceSwitched(int stream_id); |
| 216 |
| 183 RenderProcessHost::AudioOutputControllerList DoGetOutputControllers() const; | 217 RenderProcessHost::AudioOutputControllerList DoGetOutputControllers() const; |
| 184 | 218 |
| 185 // Send an error message to the renderer. | 219 // Send an error message to the renderer. |
| 186 void SendErrorMessage(int stream_id); | 220 void SendErrorMessage(int stream_id); |
| 187 | 221 |
| 188 // Delete an audio entry, notifying observers first. This is called by | 222 // Delete an audio entry, notifying observers first. This is called by |
| 189 // AudioOutputController after it has closed. | 223 // AudioOutputController after it has closed. |
| 190 void DeleteEntry(scoped_ptr<AudioEntry> entry); | 224 void DeleteEntry(scoped_ptr<AudioEntry> entry); |
| 191 | 225 |
| 192 // Send an error message to the renderer, then close the stream. | 226 // Send an error message to the renderer, then close the stream. |
| 193 void ReportErrorAndClose(int stream_id); | 227 void ReportErrorAndClose(int stream_id); |
| 194 | 228 |
| 195 // A helper method to look up a AudioEntry identified by |stream_id|. | 229 // A helper method to look up a AudioEntry identified by |stream_id|. |
| 196 // Returns NULL if not found. | 230 // Returns NULL if not found. |
| 197 AudioEntry* LookupById(int stream_id); | 231 AudioEntry* LookupById(int stream_id); |
| 198 | 232 |
| 199 // A helper method to update the number of playing streams and alert the | 233 // A helper method to update the number of playing streams and alert the |
| 200 // ResourceScheduler when the renderer starts or stops playing an audiostream. | 234 // ResourceScheduler when the renderer starts or stops playing an audiostream. |
| 201 void UpdateNumPlayingStreams(AudioEntry* entry, bool is_playing); | 235 void UpdateNumPlayingStreams(AudioEntry* entry, bool is_playing); |
| 202 | 236 |
| 203 // Checks that the renderer process supplies a URL it is allowed to use | 237 // Check if the renderer process has access to the requested output device. |
| 204 bool IsURLAllowed(const GURL& url); | 238 void CheckOutputDeviceAccess(int render_frame_id, |
| 239 const std::string& device_id, |
| 240 const GURL& gurl_security_origin, |
| 241 const OutputDeviceAccessCB& callback); |
| 242 |
| 243 // Invoke |callback| after permission to use a device has been checked. |
| 244 void AccessChecked(scoped_ptr<MediaStreamUIProxy> ui_proxy, |
| 245 const OutputDeviceAccessCB& callback, |
| 246 bool have_access); |
| 247 |
| 248 // Translate the hashed |device_id| to a unique device ID. |
| 249 void TranslateDeviceID(const std::string& device_id, |
| 250 const GURL& gurl_security_origin, |
| 251 const OutputDeviceInfoCB& callback); |
| 252 |
| 253 // Finish translation of the hashed |device_id| to a unique device ID. |
| 254 void FinishTranslateDeviceID( |
| 255 const std::string& device_id, |
| 256 const GURL& gurl_security_origin, |
| 257 const OutputDeviceInfoCB& callback, |
| 258 const AudioOutputDeviceEnumeration& device_infos); |
| 259 |
| 260 // Helper method to check if the authorization procedure for stream |
| 261 // |stream_id| has started. |
| 262 bool IsAuthorizationStarted(int stream_id); |
| 205 | 263 |
| 206 // ID of the RenderProcessHost that owns this instance. | 264 // ID of the RenderProcessHost that owns this instance. |
| 207 const int render_process_id_; | 265 const int render_process_id_; |
| 208 | 266 |
| 209 media::AudioManager* const audio_manager_; | 267 media::AudioManager* const audio_manager_; |
| 210 AudioMirroringManager* const mirroring_manager_; | 268 AudioMirroringManager* const mirroring_manager_; |
| 211 scoped_ptr<media::AudioLog> audio_log_; | 269 scoped_ptr<media::AudioLog> audio_log_; |
| 212 | 270 |
| 213 // Used to access to AudioInputDeviceManager. | 271 // Used to access to AudioInputDeviceManager. |
| 214 MediaStreamManager* media_stream_manager_; | 272 MediaStreamManager* media_stream_manager_; |
| 215 | 273 |
| 216 // A map of stream IDs to audio sources. | 274 // A map of stream IDs to audio sources. |
| 217 AudioEntryMap audio_entries_; | 275 AudioEntryMap audio_entries_; |
| 218 | 276 |
| 219 // The number of streams in the playing state. | 277 // The number of streams in the playing state. |
| 220 base::AtomicRefCount num_playing_streams_; | 278 base::AtomicRefCount num_playing_streams_; |
| 221 | 279 |
| 222 // Salt required to translate renderer device IDs to raw device IDs | 280 // Salt required to translate renderer device IDs to raw device unique IDs |
| 223 ResourceContext::SaltCallback salt_callback_; | 281 ResourceContext::SaltCallback salt_callback_; |
| 224 | 282 |
| 283 // Map of device authorizations for streams that are not yet created |
| 284 // The key is the stream ID, and the value is a pair. The pair's first element |
| 285 // is a bool that is true if the authorization process completes successfully. |
| 286 // The second element contains the unique ID of the authorized device. |
| 287 std::map<int, std::pair<bool, std::string>> authorizations_; |
| 288 |
| 225 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); | 289 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); |
| 226 }; | 290 }; |
| 227 | 291 |
| 228 } // namespace content | 292 } // namespace content |
| 229 | 293 |
| 230 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ | 294 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ |
| OLD | NEW |