| 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 RenderProcessHostImpl, and instantiated on UI | 8 // This class is owned by RenderProcessHostImpl, 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 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 #include <string> | 47 #include <string> |
| 48 #include <utility> | 48 #include <utility> |
| 49 | 49 |
| 50 #include "base/atomic_ref_count.h" | 50 #include "base/atomic_ref_count.h" |
| 51 #include "base/gtest_prod_util.h" | 51 #include "base/gtest_prod_util.h" |
| 52 #include "base/logging.h" | 52 #include "base/logging.h" |
| 53 #include "base/macros.h" | 53 #include "base/macros.h" |
| 54 #include "base/memory/ref_counted.h" | 54 #include "base/memory/ref_counted.h" |
| 55 #include "base/process/process.h" | 55 #include "base/process/process.h" |
| 56 #include "base/sequenced_task_runner_helpers.h" | 56 #include "base/sequenced_task_runner_helpers.h" |
| 57 #include "content/browser/renderer_host/media/audio_output_authorization_handler
.h" |
| 57 #include "content/browser/renderer_host/media/media_devices_manager.h" | 58 #include "content/browser/renderer_host/media/media_devices_manager.h" |
| 58 #include "content/common/content_export.h" | 59 #include "content/common/content_export.h" |
| 59 #include "content/public/browser/browser_message_filter.h" | 60 #include "content/public/browser/browser_message_filter.h" |
| 60 #include "content/public/browser/browser_thread.h" | 61 #include "content/public/browser/browser_thread.h" |
| 61 #include "content/public/browser/render_process_host.h" | 62 #include "content/public/browser/render_process_host.h" |
| 62 #include "content/public/browser/resource_context.h" | 63 #include "content/public/browser/resource_context.h" |
| 63 #include "media/audio/audio_io.h" | 64 #include "media/audio/audio_io.h" |
| 64 #include "media/audio/audio_logging.h" | 65 #include "media/audio/audio_logging.h" |
| 65 #include "media/audio/audio_output_controller.h" | 66 #include "media/audio/audio_output_controller.h" |
| 66 #include "media/audio/simple_sources.h" | 67 #include "media/audio/simple_sources.h" |
| 68 #include "media/base/output_device_info.h" |
| 67 #include "url/origin.h" | 69 #include "url/origin.h" |
| 68 | 70 |
| 69 namespace media { | 71 namespace media { |
| 70 class AudioManager; | 72 class AudioManager; |
| 71 class AudioParameters; | 73 class AudioParameters; |
| 72 } | 74 } |
| 73 | 75 |
| 74 namespace content { | 76 namespace content { |
| 75 | 77 |
| 76 class AudioMirroringManager; | 78 class AudioMirroringManager; |
| 77 class MediaInternals; | 79 class MediaInternals; |
| 78 class MediaStreamManager; | 80 class MediaStreamManager; |
| 79 class ResourceContext; | |
| 80 | 81 |
| 81 class CONTENT_EXPORT AudioRendererHost : public BrowserMessageFilter { | 82 class CONTENT_EXPORT AudioRendererHost : public BrowserMessageFilter { |
| 82 public: | 83 public: |
| 83 // Called from UI thread from the owner of this object. | 84 // Called from UI thread from the owner of this object. |
| 84 AudioRendererHost(int render_process_id, | 85 AudioRendererHost(int render_process_id, |
| 85 media::AudioManager* audio_manager, | 86 media::AudioManager* audio_manager, |
| 86 AudioMirroringManager* mirroring_manager, | 87 AudioMirroringManager* mirroring_manager, |
| 87 MediaInternals* media_internals, | 88 MediaInternals* media_internals, |
| 88 MediaStreamManager* media_stream_manager, | 89 MediaStreamManager* media_stream_manager, |
| 89 const std::string& salt); | 90 const std::string& salt); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 // |session_id| will be ignored and the given |device_id| and | 140 // |session_id| will be ignored and the given |device_id| and |
| 140 // |security_origin| will be used to select the output device. | 141 // |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 // Upon completion of the process, the peer is notified with the device output |
| 142 // parameters via the NotifyDeviceAuthorized message. | 143 // parameters via the NotifyDeviceAuthorized message. |
| 143 void OnRequestDeviceAuthorization(int stream_id, | 144 void OnRequestDeviceAuthorization(int stream_id, |
| 144 int render_frame_id, | 145 int render_frame_id, |
| 145 int session_id, | 146 int session_id, |
| 146 const std::string& device_id, | 147 const std::string& device_id, |
| 147 const url::Origin& security_origin); | 148 const url::Origin& security_origin); |
| 148 | 149 |
| 150 void AuthorizationCompleted(int stream_id, |
| 151 base::TimeTicks auth_start_time, |
| 152 media::OutputDeviceStatus status, |
| 153 const media::AudioParameters& params, |
| 154 const std::string& unique_id); |
| 155 |
| 149 // Creates an audio output stream with the specified format. | 156 // Creates an audio output stream with the specified format. |
| 150 // Upon success/failure, the peer is notified via the NotifyStreamCreated | 157 // Upon success/failure, the peer is notified via the NotifyStreamCreated |
| 151 // message. | 158 // message. |
| 152 void OnCreateStream(int stream_id, | 159 void OnCreateStream(int stream_id, |
| 153 int render_frame_id, | 160 int render_frame_id, |
| 154 const media::AudioParameters& params); | 161 const media::AudioParameters& params); |
| 155 | 162 |
| 156 // Play the audio stream referenced by |stream_id|. | 163 // Play the audio stream referenced by |stream_id|. |
| 157 void OnPlayStream(int stream_id); | 164 void OnPlayStream(int stream_id); |
| 158 | 165 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 void ReportErrorAndClose(int stream_id); | 200 void ReportErrorAndClose(int stream_id); |
| 194 | 201 |
| 195 // A helper method to look up a AudioEntry identified by |stream_id|. | 202 // A helper method to look up a AudioEntry identified by |stream_id|. |
| 196 // Returns NULL if not found. | 203 // Returns NULL if not found. |
| 197 AudioEntry* LookupById(int stream_id); | 204 AudioEntry* LookupById(int stream_id); |
| 198 | 205 |
| 199 // A helper method to update the number of playing streams and alert the | 206 // A helper method to update the number of playing streams and alert the |
| 200 // ResourceScheduler when the renderer starts or stops playing an audiostream. | 207 // ResourceScheduler when the renderer starts or stops playing an audiostream. |
| 201 void UpdateNumPlayingStreams(AudioEntry* entry, bool is_playing); | 208 void UpdateNumPlayingStreams(AudioEntry* entry, bool is_playing); |
| 202 | 209 |
| 203 // Check if the renderer process has access to the requested output device. | |
| 204 void CheckOutputDeviceAccess(int render_frame_id, | |
| 205 const std::string& device_id, | |
| 206 const url::Origin& security_origin, | |
| 207 int stream_id, | |
| 208 base::TimeTicks auth_start_time); | |
| 209 | |
| 210 // Proceed with device authorization after checking permissions. | |
| 211 void AccessChecked(const std::string& device_id, | |
| 212 const url::Origin& security_origin, | |
| 213 int stream_id, | |
| 214 base::TimeTicks auth_start_time, | |
| 215 bool have_access); | |
| 216 | |
| 217 // Translate the hashed |device_id| to a unique device ID. | |
| 218 void TranslateDeviceID(const std::string& device_id, | |
| 219 const url::Origin& security_origin, | |
| 220 int stream_id, | |
| 221 base::TimeTicks auth_start_time, | |
| 222 const MediaDeviceEnumeration& enumeration); | |
| 223 | |
| 224 // Get audio hardware parameters on the device thread. | |
| 225 media::AudioParameters GetDeviceParametersOnDeviceThread( | |
| 226 const std::string& device_id); | |
| 227 | |
| 228 // Proceed with device authorization after translating device ID and | |
| 229 // receiving hardware parameters. | |
| 230 void DeviceParametersReceived(int stream_id, | |
| 231 base::TimeTicks auth_start_time, | |
| 232 bool device_found, | |
| 233 const std::string& unique_id, | |
| 234 const media::AudioParameters& output_params); | |
| 235 | |
| 236 // Helper method to check if the authorization procedure for stream | 210 // Helper method to check if the authorization procedure for stream |
| 237 // |stream_id| has started. | 211 // |stream_id| has started. |
| 238 bool IsAuthorizationStarted(int stream_id); | 212 bool IsAuthorizationStarted(int stream_id); |
| 239 | 213 |
| 240 // Called from AudioRendererHostTest to override the function that checks for | 214 // Called from AudioRendererHostTest to override the function that checks for |
| 241 // the existence of the RenderFrameHost at stream creation time. | 215 // the existence of the RenderFrameHost at stream creation time. |
| 242 void set_render_frame_id_validate_function_for_testing( | 216 void set_render_frame_id_validate_function_for_testing( |
| 243 ValidateRenderFrameIdFunction function) { | 217 ValidateRenderFrameIdFunction function) { |
| 244 validate_render_frame_id_function_ = function; | 218 validate_render_frame_id_function_ = function; |
| 245 } | 219 } |
| 246 | 220 |
| 247 // ID of the RenderProcessHost that owns this instance. | 221 // ID of the RenderProcessHost that owns this instance. |
| 248 const int render_process_id_; | 222 const int render_process_id_; |
| 249 | 223 |
| 250 media::AudioManager* const audio_manager_; | 224 media::AudioManager* const audio_manager_; |
| 251 AudioMirroringManager* const mirroring_manager_; | 225 AudioMirroringManager* const mirroring_manager_; |
| 252 std::unique_ptr<media::AudioLog> audio_log_; | 226 std::unique_ptr<media::AudioLog> audio_log_; |
| 253 | 227 |
| 254 // Used to access to AudioInputDeviceManager. | |
| 255 MediaStreamManager* media_stream_manager_; | |
| 256 | |
| 257 // A map of stream IDs to audio sources. | 228 // A map of stream IDs to audio sources. |
| 258 AudioEntryMap audio_entries_; | 229 AudioEntryMap audio_entries_; |
| 259 | 230 |
| 260 // The number of streams in the playing state. Atomic read safe from any | 231 // The number of streams in the playing state. Atomic read safe from any |
| 261 // thread, but should only be updated from the IO thread. | 232 // thread, but should only be updated from the IO thread. |
| 262 base::AtomicRefCount num_playing_streams_; | 233 base::AtomicRefCount num_playing_streams_; |
| 263 | 234 |
| 264 // Salt required to translate renderer device IDs to raw device unique IDs | |
| 265 std::string salt_; | |
| 266 | |
| 267 // Map of device authorizations for streams that are not yet created | 235 // Map of device authorizations for streams that are not yet created |
| 268 // The key is the stream ID, and the value is a pair. The pair's first element | 236 // The key is the stream ID, and the value is a pair. The pair's first element |
| 269 // is a bool that is true if the authorization process completes successfully. | 237 // is a bool that is true if the authorization process completes successfully. |
| 270 // The second element contains the unique ID of the authorized device. | 238 // The second element contains the unique ID of the authorized device. |
| 271 std::map<int, std::pair<bool, std::string>> authorizations_; | 239 std::map<int, std::pair<bool, std::string>> authorizations_; |
| 272 | 240 |
| 273 // At stream creation time, AudioRendererHost will call this function on the | 241 // At stream creation time, AudioRendererHost will call this function on the |
| 274 // UI thread to validate render frame IDs. A default is set by the | 242 // UI thread to validate render frame IDs. A default is set by the |
| 275 // constructor, but this can be overridden by unit tests. | 243 // constructor, but this can be overridden by unit tests. |
| 276 ValidateRenderFrameIdFunction validate_render_frame_id_function_; | 244 ValidateRenderFrameIdFunction validate_render_frame_id_function_; |
| 277 | 245 |
| 278 // The maximum number of simultaneous streams during the lifetime of this | 246 // The maximum number of simultaneous streams during the lifetime of this |
| 279 // host. Reported as UMA stat at shutdown. | 247 // host. Reported as UMA stat at shutdown. |
| 280 size_t max_simultaneous_streams_; | 248 size_t max_simultaneous_streams_; |
| 281 | 249 |
| 250 AudioOutputAuthorizationHandler authorization_handler_; |
| 251 |
| 282 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); | 252 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); |
| 283 }; | 253 }; |
| 284 | 254 |
| 285 } // namespace content | 255 } // namespace content |
| 286 | 256 |
| 287 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ | 257 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ |
| OLD | NEW |