| 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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 #include "content/browser/renderer_host/media/audio_output_device_enumerator.h" | 57 #include "content/browser/renderer_host/media/audio_output_device_enumerator.h" |
| 58 #include "content/common/content_export.h" | 58 #include "content/common/content_export.h" |
| 59 #include "content/public/browser/browser_message_filter.h" | 59 #include "content/public/browser/browser_message_filter.h" |
| 60 #include "content/public/browser/browser_thread.h" | 60 #include "content/public/browser/browser_thread.h" |
| 61 #include "content/public/browser/render_process_host.h" | 61 #include "content/public/browser/render_process_host.h" |
| 62 #include "content/public/browser/resource_context.h" | 62 #include "content/public/browser/resource_context.h" |
| 63 #include "media/audio/audio_io.h" | 63 #include "media/audio/audio_io.h" |
| 64 #include "media/audio/audio_logging.h" | 64 #include "media/audio/audio_logging.h" |
| 65 #include "media/audio/audio_output_controller.h" | 65 #include "media/audio/audio_output_controller.h" |
| 66 #include "media/audio/simple_sources.h" | 66 #include "media/audio/simple_sources.h" |
| 67 #include "media/base/output_device_info.h" |
| 68 #include "media/mojo/interfaces/audio_output.mojom.h" |
| 69 #include "mojo/public/cpp/bindings/binding.h" |
| 70 #include "mojo/public/cpp/bindings/interface_request.h" |
| 67 #include "url/origin.h" | 71 #include "url/origin.h" |
| 68 | 72 |
| 69 namespace media { | 73 namespace media { |
| 70 class AudioManager; | 74 class AudioManager; |
| 71 class AudioParameters; | 75 class AudioParameters; |
| 72 } | 76 } |
| 73 | 77 |
| 74 namespace content { | 78 namespace content { |
| 75 | 79 |
| 76 class AudioMirroringManager; | 80 class AudioMirroringManager; |
| 77 class MediaInternals; | 81 class MediaInternals; |
| 78 class MediaStreamManager; | 82 class MediaStreamManager; |
| 79 class MediaStreamUIProxy; | 83 class MediaStreamUIProxy; |
| 80 class ResourceContext; | 84 class ResourceContext; |
| 81 | 85 |
| 82 class CONTENT_EXPORT AudioRendererHost : public BrowserMessageFilter { | 86 class CONTENT_EXPORT AudioRendererHost |
| 87 : public base::RefCountedThreadSafe<AudioRendererHost, |
| 88 BrowserThread::DeleteOnIOThread>, |
| 89 NON_EXPORTED_BASE(private media::mojom::AudioOutput) { |
| 83 public: | 90 public: |
| 84 // Called from UI thread from the owner of this object. | 91 // Called from UI thread from the owner of this object. |
| 85 AudioRendererHost(int render_process_id, | 92 AudioRendererHost(int render_process_id, |
| 86 media::AudioManager* audio_manager, | 93 media::AudioManager* audio_manager, |
| 87 AudioMirroringManager* mirroring_manager, | 94 AudioMirroringManager* mirroring_manager, |
| 88 MediaInternals* media_internals, | 95 MediaInternals* media_internals, |
| 89 MediaStreamManager* media_stream_manager, | 96 MediaStreamManager* media_stream_manager, |
| 90 const std::string& salt); | 97 const std::string& salt); |
| 91 | 98 |
| 92 // Calls |callback| with the list of AudioOutputControllers for this object. | 99 // Calls |callback| with the list of AudioOutputControllers for this object. |
| 93 void GetOutputControllers( | 100 void GetOutputControllers( |
| 94 const RenderProcessHost::GetAudioOutputControllersCallback& | 101 const RenderProcessHost::GetAudioOutputControllersCallback& |
| 95 callback) const; | 102 callback) const; |
| 96 | 103 |
| 97 // BrowserMessageFilter implementation. | 104 // Returns true if any streams managed by this host are actively playing. Can |
| 98 void OnChannelClosing() override; | |
| 99 void OnDestruct() const override; | |
| 100 bool OnMessageReceived(const IPC::Message& message) override; | |
| 101 | |
| 102 // Returns true if any streams managed by this host are actively playing. Can | |
| 103 // be called from any thread. | 105 // be called from any thread. |
| 104 bool HasActiveAudio(); | 106 bool HasActiveAudio(); |
| 105 | 107 |
| 108 void BindRequest(media::mojom::AudioOutputRequest request); |
| 109 |
| 106 private: | 110 private: |
| 107 friend class AudioRendererHostTest; | 111 friend class AudioRendererHostTest; |
| 108 friend class BrowserThread; | 112 friend class BrowserThread; |
| 109 friend class base::DeleteHelper<AudioRendererHost>; | 113 friend class base::DeleteHelper<AudioRendererHost>; |
| 110 friend class MockAudioRendererHost; | 114 friend class MockAudioRendererHost; |
| 111 friend class TestAudioRendererHost; | 115 friend class TestAudioRendererHost; |
| 116 friend class base::RefCounted<AudioRendererHost>; |
| 112 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, CreateMockStream); | 117 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, CreateMockStream); |
| 113 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, MockStreamDataConversation); | 118 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, MockStreamDataConversation); |
| 119 int32_t next_stream_id_; |
| 114 | 120 |
| 115 class AudioEntry; | 121 class AudioEntry; |
| 116 typedef std::map<int, AudioEntry*> AudioEntryMap; | 122 typedef std::map<int, AudioEntry*> AudioEntryMap; |
| 117 | 123 |
| 118 // Internal callback type for access requests to output devices. | 124 // Internal callback type for access requests to output devices. |
| 119 // |have_access| is true only if there is permission to access the device. | 125 // |have_access| is true only if there is permission to access the device. |
| 120 typedef base::Callback<void(bool have_access)> OutputDeviceAccessCB; | 126 typedef base::Callback<void(bool have_access)> OutputDeviceAccessCB; |
| 121 | 127 |
| 122 // Internal callback type for information requests about an output device. | 128 // Internal callback type for information requests about an output device. |
| 123 // |success| indicates the operation was successful. If true, |device_info| | 129 // |success| indicates the operation was successful. If true, |device_info| |
| 124 // contains data about the device. | 130 // contains data about the device. |
| 125 typedef base::Callback<void(bool success, | 131 typedef base::Callback<void(bool success, |
| 126 const AudioOutputDeviceInfo& device_info)> | 132 const AudioOutputDeviceInfo& device_info)> |
| 127 OutputDeviceInfoCB; | 133 OutputDeviceInfoCB; |
| 128 | 134 |
| 129 // The type of a function that is run on the UI thread to check whether the | 135 // The type of a function that is run on the UI thread to check whether the |
| 130 // routing IDs reference a valid RenderFrameHost. The function then runs | 136 // routing IDs reference a valid RenderFrameHost. The function then runs |
| 131 // |callback| on the IO thread with true/false if valid/invalid. | 137 // |callback| on the IO thread with true/false if valid/invalid. |
| 132 using ValidateRenderFrameIdFunction = | 138 using ValidateRenderFrameIdFunction = |
| 133 void (*)(int render_process_id, | 139 void (*)(int render_process_id, |
| 134 int render_frame_id, | 140 int render_frame_id, |
| 135 const base::Callback<void(bool)>& callback); | 141 const base::Callback<void(bool)>& callback); |
| 136 | 142 |
| 137 ~AudioRendererHost() override; | 143 ~AudioRendererHost() override; |
| 138 | 144 |
| 139 // Methods called on IO thread ---------------------------------------------- | |
| 140 | |
| 141 // Audio related IPC message handlers. | |
| 142 | |
| 143 // Request permission to use an output device for use by a stream produced | 145 // Request permission to use an output device for use by a stream produced |
| 144 // in the RenderFrame referenced by |render_frame_id|. | 146 // in the RenderFrame referenced by |render_frame_id|. |
| 145 // |session_id| is used for unified IO to find out which input device to be | 147 // |session_id| is used for unified IO to find out which input device to be |
| 146 // opened for the stream. For clients that do not use unified IO, | 148 // opened for the stream. For clients that do not use unified IO, |
| 147 // |session_id| will be ignored and the given |device_id| and | 149 // |session_id| will be ignored and the given |device_id| and |
| 148 // |security_origin| will be used to select the output device. | 150 // |security_origin| will be used to select the output device. |
| 149 // Upon completion of the process, the peer is notified with the device output | 151 void RequestDeviceAuthorization( |
| 150 // parameters via the NotifyDeviceAuthorized message. | 152 int64_t render_frame_id, |
| 151 void OnRequestDeviceAuthorization(int stream_id, | 153 int64_t session_id, |
| 152 int render_frame_id, | 154 const mojo::String& device_id, |
| 153 int session_id, | 155 const url::Origin& origin, |
| 154 const std::string& device_id, | 156 const RequestDeviceAuthorizationCallback& callback) override; |
| 155 const url::Origin& security_origin); | |
| 156 | 157 |
| 158 void SendAuthorizationMessage(RequestDeviceAuthorizationCallback callback, |
| 159 int32_t stream_id, |
| 160 media::OutputDeviceStatus status, |
| 161 const media::AudioParameters& params, |
| 162 const std::string& matched_device_id); |
| 163 |
| 164 // Methods called on IO thread ---------------------------------------------- |
| 165 |
| 166 // Audio related IPC message handlers. |
| 157 // Creates an audio output stream with the specified format. | 167 // Creates an audio output stream with the specified format. |
| 158 // Upon success/failure, the peer is notified via the NotifyStreamCreated | 168 // Upon success/failure, the peer is notified via the NotifyStreamCreated |
| 159 // message. | 169 // message. |
| 160 void OnCreateStream(int stream_id, | 170 void OnCreateStream(int32_t stream_id, |
| 161 int render_frame_id, | 171 int render_frame_id, |
| 162 const media::AudioParameters& params); | 172 const media::AudioParameters& params); |
| 163 | 173 |
| 164 // Play the audio stream referenced by |stream_id|. | 174 // Play the audio stream referenced by |stream_id|. |
| 165 void OnPlayStream(int stream_id); | 175 void OnPlayStream(int32_t stream_id); |
| 166 | 176 |
| 167 // Pause the audio stream referenced by |stream_id|. | 177 // Pause the audio stream referenced by |stream_id|. |
| 168 void OnPauseStream(int stream_id); | 178 void OnPauseStream(int32_t stream_id); |
| 169 | 179 |
| 170 // Close the audio stream referenced by |stream_id|. | 180 // Close the audio stream referenced by |stream_id|. |
| 171 void OnCloseStream(int stream_id); | 181 void OnCloseStream(int32_t stream_id); |
| 172 | 182 |
| 173 // Set the volume of the audio stream referenced by |stream_id|. | 183 // Set the volume of the audio stream referenced by |stream_id|. |
| 174 void OnSetVolume(int stream_id, double volume); | 184 void OnSetVolume(int32_t stream_id, double volume); |
| 175 | |
| 176 // Helper methods. | |
| 177 | 185 |
| 178 // Proceed with device authorization after checking permissions. | 186 // Proceed with device authorization after checking permissions. |
| 179 void OnDeviceAuthorized(int stream_id, | 187 void OnDeviceAuthorized(int32_t stream_id, |
| 188 RequestDeviceAuthorizationCallback callback, |
| 180 const std::string& device_id, | 189 const std::string& device_id, |
| 181 const url::Origin& security_origin, | 190 const url::Origin& security_origin, |
| 182 base::TimeTicks auth_start_time, | 191 base::TimeTicks auth_start_time, |
| 183 bool have_access); | 192 bool have_access); |
| 184 | 193 |
| 185 // Proceed with device authorization after translating device ID. | 194 // Proceed with device authorization after translating device ID. |
| 186 void OnDeviceIDTranslated(int stream_id, | 195 void OnDeviceIDTranslated(int32_t stream_id, |
| 187 base::TimeTicks auth_start_time, | 196 base::TimeTicks auth_start_time, |
| 197 RequestDeviceAuthorizationCallback callback, |
| 188 bool device_found, | 198 bool device_found, |
| 189 const AudioOutputDeviceInfo& device_info); | 199 const AudioOutputDeviceInfo& device_info); |
| 190 | 200 |
| 191 // Start the actual creation of an audio stream, after the device | 201 // Start the actual creation of an audio stream, after the device |
| 192 // authorization process is complete. | 202 // authorization process is complete. |
| 193 void DoCreateStream(int stream_id, | 203 void DoCreateStream(int32_t stream_id, |
| 194 int render_frame_id, | 204 int render_frame_id, |
| 195 const media::AudioParameters& params, | 205 const media::AudioParameters& params, |
| 196 const std::string& device_unique_id, | 206 const std::string& device_unique_id, |
| 197 bool render_frame_id_is_valid); | 207 bool render_frame_id_is_valid); |
| 198 | 208 |
| 199 // Complete the process of creating an audio stream. This will set up the | 209 // Complete the process of creating an audio stream. This will set up the |
| 200 // shared memory or shared socket in low latency mode and send the | 210 // shared memory or shared socket in low latency mode and send the |
| 201 // NotifyStreamCreated message to the peer. | 211 // NotifyStreamCreated message to the peer. |
| 202 void DoCompleteCreation(int stream_id); | 212 void DoCompleteCreation(AudioEntry* entry, int32_t stream_id); |
| 203 | 213 |
| 204 // Send playing/paused status to the renderer. | 214 // Send playing/paused status to the renderer. |
| 205 void DoNotifyStreamStateChanged(int stream_id, bool is_playing); | 215 // void DoNotifyStreamStateChanged(int32_t stream_id, bool is_playing); |
| 206 | 216 |
| 207 RenderProcessHost::AudioOutputControllerList DoGetOutputControllers() const; | 217 RenderProcessHost::AudioOutputControllerList DoGetOutputControllers() const; |
| 208 | 218 |
| 209 // Send an error message to the renderer. | 219 // Send an error message to the renderer. |
| 210 void SendErrorMessage(int stream_id); | 220 void SendErrorMessage(int32_t stream_id); |
| 211 | 221 |
| 212 // Delete an audio entry, notifying observers first. This is called by | 222 // Delete an audio entry, notifying observers first. This is called by |
| 213 // AudioOutputController after it has closed. | 223 // AudioOutputController after it has closed. |
| 214 void DeleteEntry(std::unique_ptr<AudioEntry> entry); | 224 void DeleteEntry(std::unique_ptr<AudioEntry> entry); |
| 215 | 225 |
| 216 // Send an error message to the renderer, then close the stream. | 226 // Send an error message to the renderer, then close the stream. |
| 217 void ReportErrorAndClose(int stream_id); | 227 void ReportErrorAndClose(int32_t stream_id); |
| 218 | 228 |
| 219 // 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|. |
| 220 // Returns NULL if not found. | 230 // Returns NULL if not found. |
| 221 AudioEntry* LookupById(int stream_id); | 231 AudioEntry* LookupById(int32_t stream_id); |
| 222 | 232 |
| 223 // 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 |
| 224 // ResourceScheduler when the renderer starts or stops playing an audiostream. | 234 // ResourceScheduler when the renderer starts or stops playing an audiostream. |
| 225 void UpdateNumPlayingStreams(AudioEntry* entry, bool is_playing); | 235 void UpdateNumPlayingStreams(AudioEntry* entry, bool is_playing); |
| 226 | 236 |
| 227 // Check if the renderer process has access to the requested output device. | 237 // Check if the renderer process has access to the requested output device. |
| 228 void CheckOutputDeviceAccess(int render_frame_id, | 238 void CheckOutputDeviceAccess(int render_frame_id, |
| 229 const std::string& device_id, | 239 const std::string& device_id, |
| 230 const url::Origin& security_origin, | 240 const url::Origin& security_origin, |
| 231 const OutputDeviceAccessCB& callback); | 241 const OutputDeviceAccessCB& callback); |
| 232 | 242 |
| 233 // Invoke |callback| after permission to use a device has been checked. | 243 // Invoke |callback| after permission to use a device has been checked. |
| 234 void AccessChecked(std::unique_ptr<MediaStreamUIProxy> ui_proxy, | 244 void AccessChecked(std::unique_ptr<MediaStreamUIProxy> ui_proxy, |
| 235 const OutputDeviceAccessCB& callback, | 245 const OutputDeviceAccessCB& callback, |
| 236 bool have_access); | 246 ool have_access); |
| 237 | 247 |
| 238 // Translate the hashed |device_id| to a unique device ID. | 248 // Translate the hashed |device_id| to a unique device ID. |
| 239 void TranslateDeviceID(const std::string& device_id, | 249 void TranslateDeviceID(const std::string& device_id, |
| 240 const url::Origin& security_origin, | 250 const url::Origin& security_origin, |
| 241 const OutputDeviceInfoCB& callback, | 251 const OutputDeviceInfoCB& callback, |
| 242 const AudioOutputDeviceEnumeration& enumeration); | 252 const AudioOutputDeviceEnumeration& enumeration); |
| 243 | 253 |
| 244 // Helper method to check if the authorization procedure for stream | 254 // Helper method to check if the authorization procedure for stream |
| 245 // |stream_id| has started. | 255 // |stream_id| has started. |
| 246 bool IsAuthorizationStarted(int stream_id); | 256 bool IsAuthorizationStarted(int32_t stream_id); |
| 247 | 257 |
| 248 #if DCHECK_IS_ON() | 258 #if DCHECK_IS_ON() |
| 249 // Called from AudioRendererHostTest to override the function that checks for | 259 // Called from AudioRendererHostTest to override the function that checks for |
| 250 // the existence of the RenderFrameHost at stream creation time. | 260 // the existence of the RenderFrameHost at stream creation time. |
| 251 void set_render_frame_id_validate_function_for_testing( | 261 void set_render_frame_id_validate_function_for_testing( |
| 252 ValidateRenderFrameIdFunction function) { | 262 ValidateRenderFrameIdFunction function) { |
| 253 validate_render_frame_id_function_ = function; | 263 validate_render_frame_id_function_ = function; |
| 254 } | 264 } |
| 255 #endif // DCHECK_IS_ON() | 265 #endif // DCHECK_IS_ON() |
| 256 | 266 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 284 // When DCHECKs are turned on, AudioRendererHost will call this function on | 294 // When DCHECKs are turned on, AudioRendererHost will call this function on |
| 285 // the UI thread to validate render frame IDs. A default is set by the | 295 // the UI thread to validate render frame IDs. A default is set by the |
| 286 // constructor, but this can be overridden by unit tests. | 296 // constructor, but this can be overridden by unit tests. |
| 287 ValidateRenderFrameIdFunction validate_render_frame_id_function_; | 297 ValidateRenderFrameIdFunction validate_render_frame_id_function_; |
| 288 #endif // DCHECK_IS_ON() | 298 #endif // DCHECK_IS_ON() |
| 289 | 299 |
| 290 // The maximum number of simultaneous streams during the lifetime of this | 300 // The maximum number of simultaneous streams during the lifetime of this |
| 291 // host. Reported as UMA stat at shutdown. | 301 // host. Reported as UMA stat at shutdown. |
| 292 size_t max_simultaneous_streams_; | 302 size_t max_simultaneous_streams_; |
| 293 | 303 |
| 304 // Binds AudioRendererHost to a media::mojom::AudioOutputRequest. |
| 305 std::unique_ptr<mojo::Binding<media::mojom::AudioOutput>> binding_; |
| 306 |
| 294 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); | 307 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); |
| 295 }; | 308 }; |
| 296 | 309 |
| 297 } // namespace content | 310 } // namespace content |
| 298 | 311 |
| 299 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ | 312 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ |
| OLD | NEW |