| 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 | 42 |
| 43 #include <stddef.h> | 43 #include <stddef.h> |
| 44 | 44 |
| 45 #include <map> | 45 #include <map> |
| 46 #include <memory> | 46 #include <memory> |
| 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/macros.h" | 53 #include "base/macros.h" |
| 53 #include "base/memory/ref_counted.h" | 54 #include "base/memory/ref_counted.h" |
| 54 #include "base/process/process.h" | 55 #include "base/process/process.h" |
| 55 #include "base/sequenced_task_runner_helpers.h" | 56 #include "base/sequenced_task_runner_helpers.h" |
| 56 #include "content/browser/renderer_host/media/audio_output_device_enumerator.h" | 57 #include "content/browser/renderer_host/media/audio_output_device_enumerator.h" |
| 57 #include "content/common/content_export.h" | 58 #include "content/common/content_export.h" |
| 58 #include "content/public/browser/browser_message_filter.h" | 59 #include "content/public/browser/browser_message_filter.h" |
| 59 #include "content/public/browser/browser_thread.h" | 60 #include "content/public/browser/browser_thread.h" |
| 60 #include "content/public/browser/render_process_host.h" | 61 #include "content/public/browser/render_process_host.h" |
| 61 #include "content/public/browser/resource_context.h" | 62 #include "content/public/browser/resource_context.h" |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 // |have_access| is true only if there is permission to access the device. | 123 // |have_access| is true only if there is permission to access the device. |
| 123 typedef base::Callback<void(bool have_access)> OutputDeviceAccessCB; | 124 typedef base::Callback<void(bool have_access)> OutputDeviceAccessCB; |
| 124 | 125 |
| 125 // Internal callback type for information requests about an output device. | 126 // Internal callback type for information requests about an output device. |
| 126 // |success| indicates the operation was successful. If true, |device_info| | 127 // |success| indicates the operation was successful. If true, |device_info| |
| 127 // contains data about the device. | 128 // contains data about the device. |
| 128 typedef base::Callback<void(bool success, | 129 typedef base::Callback<void(bool success, |
| 129 const AudioOutputDeviceInfo& device_info)> | 130 const AudioOutputDeviceInfo& device_info)> |
| 130 OutputDeviceInfoCB; | 131 OutputDeviceInfoCB; |
| 131 | 132 |
| 133 // The type of a function that is run on the UI thread to check whether the |
| 134 // routing IDs reference a valid RenderFrameHost. The function then runs |
| 135 // |callback| on the IO thread with true/false if valid/invalid. |
| 136 using ValidateRenderFrameIdFunction = |
| 137 void (*)(int render_process_id, |
| 138 int render_frame_id, |
| 139 const base::Callback<void(bool)>& callback); |
| 140 |
| 132 ~AudioRendererHost() override; | 141 ~AudioRendererHost() override; |
| 133 | 142 |
| 134 // Methods called on IO thread ---------------------------------------------- | 143 // Methods called on IO thread ---------------------------------------------- |
| 135 | 144 |
| 136 // Audio related IPC message handlers. | 145 // Audio related IPC message handlers. |
| 137 | 146 |
| 138 // Request permission to use an output device for use by a stream produced | 147 // Request permission to use an output device for use by a stream produced |
| 139 // in the RenderFrame referenced by |render_frame_id|. | 148 // in the RenderFrame referenced by |render_frame_id|. |
| 140 // |session_id| is used for unified IO to find out which input device to be | 149 // |session_id| is used for unified IO to find out which input device to be |
| 141 // opened for the stream. For clients that do not use unified IO, | 150 // opened for the stream. For clients that do not use unified IO, |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 void OnDeviceIDTranslated(int stream_id, | 190 void OnDeviceIDTranslated(int stream_id, |
| 182 base::TimeTicks auth_start_time, | 191 base::TimeTicks auth_start_time, |
| 183 bool device_found, | 192 bool device_found, |
| 184 const AudioOutputDeviceInfo& device_info); | 193 const AudioOutputDeviceInfo& device_info); |
| 185 | 194 |
| 186 // Start the actual creation of an audio stream, after the device | 195 // Start the actual creation of an audio stream, after the device |
| 187 // authorization process is complete. | 196 // authorization process is complete. |
| 188 void DoCreateStream(int stream_id, | 197 void DoCreateStream(int stream_id, |
| 189 int render_frame_id, | 198 int render_frame_id, |
| 190 const media::AudioParameters& params, | 199 const media::AudioParameters& params, |
| 191 const std::string& device_unique_id); | 200 const std::string& device_unique_id, |
| 201 bool render_frame_id_is_valid); |
| 192 | 202 |
| 193 // Complete the process of creating an audio stream. This will set up the | 203 // Complete the process of creating an audio stream. This will set up the |
| 194 // shared memory or shared socket in low latency mode and send the | 204 // shared memory or shared socket in low latency mode and send the |
| 195 // NotifyStreamCreated message to the peer. | 205 // NotifyStreamCreated message to the peer. |
| 196 void DoCompleteCreation(int stream_id); | 206 void DoCompleteCreation(int stream_id); |
| 197 | 207 |
| 198 // Send playing/paused status to the renderer. | 208 // Send playing/paused status to the renderer. |
| 199 void DoNotifyStreamStateChanged(int stream_id, bool is_playing); | 209 void DoNotifyStreamStateChanged(int stream_id, bool is_playing); |
| 200 | 210 |
| 201 RenderProcessHost::AudioOutputControllerList DoGetOutputControllers() const; | 211 RenderProcessHost::AudioOutputControllerList DoGetOutputControllers() const; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 232 // Translate the hashed |device_id| to a unique device ID. | 242 // Translate the hashed |device_id| to a unique device ID. |
| 233 void TranslateDeviceID(const std::string& device_id, | 243 void TranslateDeviceID(const std::string& device_id, |
| 234 const url::Origin& security_origin, | 244 const url::Origin& security_origin, |
| 235 const OutputDeviceInfoCB& callback, | 245 const OutputDeviceInfoCB& callback, |
| 236 const AudioOutputDeviceEnumeration& enumeration); | 246 const AudioOutputDeviceEnumeration& enumeration); |
| 237 | 247 |
| 238 // Helper method to check if the authorization procedure for stream | 248 // Helper method to check if the authorization procedure for stream |
| 239 // |stream_id| has started. | 249 // |stream_id| has started. |
| 240 bool IsAuthorizationStarted(int stream_id); | 250 bool IsAuthorizationStarted(int stream_id); |
| 241 | 251 |
| 252 #if DCHECK_IS_ON() |
| 253 // Called from AudioRendererHostTest to override the function that checks for |
| 254 // the existence of the RenderFrameHost at stream creation time. |
| 255 void set_render_frame_id_validate_function_for_testing( |
| 256 ValidateRenderFrameIdFunction function) { |
| 257 validate_render_frame_id_function_ = function; |
| 258 } |
| 259 #endif // DCHECK_IS_ON() |
| 260 |
| 242 // ID of the RenderProcessHost that owns this instance. | 261 // ID of the RenderProcessHost that owns this instance. |
| 243 const int render_process_id_; | 262 const int render_process_id_; |
| 244 | 263 |
| 245 media::AudioManager* const audio_manager_; | 264 media::AudioManager* const audio_manager_; |
| 246 AudioMirroringManager* const mirroring_manager_; | 265 AudioMirroringManager* const mirroring_manager_; |
| 247 std::unique_ptr<media::AudioLog> audio_log_; | 266 std::unique_ptr<media::AudioLog> audio_log_; |
| 248 | 267 |
| 249 // Used to access to AudioInputDeviceManager. | 268 // Used to access to AudioInputDeviceManager. |
| 250 MediaStreamManager* media_stream_manager_; | 269 MediaStreamManager* media_stream_manager_; |
| 251 | 270 |
| 252 // A map of stream IDs to audio sources. | 271 // A map of stream IDs to audio sources. |
| 253 AudioEntryMap audio_entries_; | 272 AudioEntryMap audio_entries_; |
| 254 | 273 |
| 255 // The number of streams in the playing state. Atomic read safe from any | 274 // The number of streams in the playing state. Atomic read safe from any |
| 256 // thread, but should only be updated from the IO thread. | 275 // thread, but should only be updated from the IO thread. |
| 257 base::AtomicRefCount num_playing_streams_; | 276 base::AtomicRefCount num_playing_streams_; |
| 258 | 277 |
| 259 // Salt required to translate renderer device IDs to raw device unique IDs | 278 // Salt required to translate renderer device IDs to raw device unique IDs |
| 260 std::string salt_; | 279 std::string salt_; |
| 261 | 280 |
| 262 // Map of device authorizations for streams that are not yet created | 281 // Map of device authorizations for streams that are not yet created |
| 263 // The key is the stream ID, and the value is a pair. The pair's first element | 282 // The key is the stream ID, and the value is a pair. The pair's first element |
| 264 // is a bool that is true if the authorization process completes successfully. | 283 // is a bool that is true if the authorization process completes successfully. |
| 265 // The second element contains the unique ID of the authorized device. | 284 // The second element contains the unique ID of the authorized device. |
| 266 std::map<int, std::pair<bool, std::string>> authorizations_; | 285 std::map<int, std::pair<bool, std::string>> authorizations_; |
| 267 | 286 |
| 287 #if DCHECK_IS_ON() |
| 288 // When DCHECKs are turned on, AudioRendererHost will call this function on |
| 289 // the UI thread to validate render frame IDs. A default is set by the |
| 290 // constructor, but this can be overridden by unit tests. |
| 291 ValidateRenderFrameIdFunction validate_render_frame_id_function_; |
| 292 #endif // DCHECK_IS_ON() |
| 293 |
| 268 // The maximum number of simultaneous streams during the lifetime of this | 294 // The maximum number of simultaneous streams during the lifetime of this |
| 269 // host. Reported as UMA stat at shutdown. | 295 // host. Reported as UMA stat at shutdown. |
| 270 size_t max_simultaneous_streams_; | 296 size_t max_simultaneous_streams_; |
| 271 | 297 |
| 272 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); | 298 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); |
| 273 }; | 299 }; |
| 274 | 300 |
| 275 } // namespace content | 301 } // namespace content |
| 276 | 302 |
| 277 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ | 303 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ |
| OLD | NEW |