| 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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 } | 58 } |
| 59 | 59 |
| 60 namespace media { | 60 namespace media { |
| 61 class AudioManager; | 61 class AudioManager; |
| 62 class AudioParameters; | 62 class AudioParameters; |
| 63 } | 63 } |
| 64 | 64 |
| 65 namespace content { | 65 namespace content { |
| 66 | 66 |
| 67 class AudioMirroringManager; | 67 class AudioMirroringManager; |
| 68 class AudioStreamRegistry; |
| 68 class MediaStreamManager; | 69 class MediaStreamManager; |
| 69 | 70 |
| 70 class CONTENT_EXPORT AudioRendererHost | 71 class CONTENT_EXPORT AudioRendererHost |
| 71 : public BrowserMessageFilter, | 72 : public BrowserMessageFilter, |
| 72 public AudioOutputDelegate::EventHandler { | 73 public AudioOutputDelegate::EventHandler { |
| 73 public: | 74 public: |
| 74 // Called from UI thread from the owner of this object. | 75 // Called from UI thread from the owner of this object. |
| 75 AudioRendererHost(int render_process_id, | 76 AudioRendererHost(int render_process_id, |
| 77 AudioStreamRegistry* stream_registry, |
| 76 media::AudioManager* audio_manager, | 78 media::AudioManager* audio_manager, |
| 77 AudioMirroringManager* mirroring_manager, | 79 AudioMirroringManager* mirroring_manager, |
| 78 MediaStreamManager* media_stream_manager, | 80 MediaStreamManager* media_stream_manager, |
| 79 const std::string& salt); | 81 const std::string& salt); |
| 80 | 82 |
| 81 // Calls |callback| with the list of AudioOutputControllers for this object. | 83 // Calls |callback| with the list of AudioOutputControllers for this object. |
| 82 void GetOutputControllers( | 84 void GetOutputControllers( |
| 83 const RenderProcessHost::GetAudioOutputControllersCallback& | 85 const RenderProcessHost::GetAudioOutputControllersCallback& |
| 84 callback) const; | 86 callback) const; |
| 85 | 87 |
| 86 // BrowserMessageFilter implementation. | 88 // BrowserMessageFilter implementation. |
| 87 void OnChannelClosing() override; | 89 void OnChannelClosing() override; |
| 88 void OnDestruct() const override; | 90 void OnDestruct() const override; |
| 89 bool OnMessageReceived(const IPC::Message& message) override; | 91 bool OnMessageReceived(const IPC::Message& message) override; |
| 90 | 92 |
| 91 // AudioOutputDelegate::EventHandler implementation | 93 // AudioOutputDelegate::EventHandler implementation |
| 92 void OnStreamCreated(int stream_id, | 94 void OnStreamCreated(int stream_id, |
| 93 base::SharedMemory* shared_memory, | 95 base::SharedMemory* shared_memory, |
| 94 base::CancelableSyncSocket* foreign_socket) override; | 96 base::CancelableSyncSocket* foreign_socket) override; |
| 95 void OnStreamError(int stream_id) override; | 97 void OnStreamError(int stream_id) override; |
| 96 void OnStreamStateChanged(bool is_playing) override; | 98 void OnStreamStateChanged(bool is_playing) override; |
| 97 | 99 |
| 98 // Returns true if any streams managed by this host are actively playing. Can | |
| 99 // be called from any thread. | |
| 100 bool HasActiveAudio(); | |
| 101 | |
| 102 void OverrideDevicePermissionsForTesting(bool has_access); | 100 void OverrideDevicePermissionsForTesting(bool has_access); |
| 103 | 101 |
| 104 private: | 102 private: |
| 105 friend class AudioRendererHostTest; | 103 friend class AudioRendererHostTest; |
| 106 friend class BrowserThread; | 104 friend class BrowserThread; |
| 107 friend class base::DeleteHelper<AudioRendererHost>; | 105 friend class base::DeleteHelper<AudioRendererHost>; |
| 108 friend class MockAudioRendererHost; | 106 friend class MockAudioRendererHost; |
| 109 friend class TestAudioRendererHost; | 107 friend class TestAudioRendererHost; |
| 110 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, CreateMockStream); | 108 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, CreateMockStream); |
| 111 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, MockStreamDataConversation); | 109 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, MockStreamDataConversation); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 | 168 |
| 171 // Set the volume of the audio stream referenced by |stream_id|. | 169 // Set the volume of the audio stream referenced by |stream_id|. |
| 172 void OnSetVolume(int stream_id, double volume); | 170 void OnSetVolume(int stream_id, double volume); |
| 173 | 171 |
| 174 // Helper methods. | 172 // Helper methods. |
| 175 | 173 |
| 176 // Called after the |render_frame_id| provided to OnCreateStream() was | 174 // Called after the |render_frame_id| provided to OnCreateStream() was |
| 177 // validated. When |is_valid| is false, this calls OnStreamError(). | 175 // validated. When |is_valid| is false, this calls OnStreamError(). |
| 178 void DidValidateRenderFrame(int stream_id, bool is_valid); | 176 void DidValidateRenderFrame(int stream_id, bool is_valid); |
| 179 | 177 |
| 180 // Updates status of stream for AudioStreamMonitor and updates | |
| 181 // the number of playing streams. | |
| 182 void StreamStateChanged(int stream_id, bool is_playing); | |
| 183 | |
| 184 RenderProcessHost::AudioOutputControllerList DoGetOutputControllers() const; | 178 RenderProcessHost::AudioOutputControllerList DoGetOutputControllers() const; |
| 185 | 179 |
| 186 // Send an error message to the renderer. | 180 // Send an error message to the renderer. |
| 187 void SendErrorMessage(int stream_id); | 181 void SendErrorMessage(int stream_id); |
| 188 | 182 |
| 189 // Helper methods to look up a AudioOutputDelegate identified by |stream_id|. | 183 // Helper methods to look up a AudioOutputDelegate identified by |stream_id|. |
| 190 // Returns delegates_.end() if not found. | 184 // Returns delegates_.end() if not found. |
| 191 AudioOutputDelegateVector::iterator LookupIteratorById(int stream_id); | 185 AudioOutputDelegateVector::iterator LookupIteratorById(int stream_id); |
| 192 // Returns nullptr if not found. | 186 // Returns nullptr if not found. |
| 193 AudioOutputDelegate* LookupById(int stream_id); | 187 AudioOutputDelegate* LookupById(int stream_id); |
| 194 | 188 |
| 195 // Helper method to check if the authorization procedure for stream | 189 // Helper method to check if the authorization procedure for stream |
| 196 // |stream_id| has started. | 190 // |stream_id| has started. |
| 197 bool IsAuthorizationStarted(int stream_id); | 191 bool IsAuthorizationStarted(int stream_id); |
| 198 | 192 |
| 199 // Called from AudioRendererHostTest to override the function that checks for | 193 // Called from AudioRendererHostTest to override the function that checks for |
| 200 // the existence of the RenderFrameHost at stream creation time. | 194 // the existence of the RenderFrameHost at stream creation time. |
| 201 void set_render_frame_id_validate_function_for_testing( | 195 void set_render_frame_id_validate_function_for_testing( |
| 202 ValidateRenderFrameIdFunction function) { | 196 ValidateRenderFrameIdFunction function) { |
| 203 validate_render_frame_id_function_ = function; | 197 validate_render_frame_id_function_ = function; |
| 204 } | 198 } |
| 205 | 199 |
| 206 // ID of the RenderProcessHost that owns this instance. | 200 // ID of the RenderProcessHost that owns this instance. |
| 207 const int render_process_id_; | 201 const int render_process_id_; |
| 208 | 202 |
| 203 AudioStreamRegistry* const stream_registry_; |
| 209 media::AudioManager* const audio_manager_; | 204 media::AudioManager* const audio_manager_; |
| 210 AudioMirroringManager* const mirroring_manager_; | 205 AudioMirroringManager* const mirroring_manager_; |
| 211 | 206 |
| 212 // Used to access to AudioInputDeviceManager. | 207 // Used to access to AudioInputDeviceManager. |
| 213 MediaStreamManager* media_stream_manager_; | 208 MediaStreamManager* media_stream_manager_; |
| 214 | 209 |
| 215 // A list of the current open streams. | 210 // A list of the current open streams. |
| 216 AudioOutputDelegateVector delegates_; | 211 AudioOutputDelegateVector delegates_; |
| 217 | 212 |
| 218 // The number of streams in the playing state. Atomic read safe from any | |
| 219 // thread, but should only be updated from the IO thread. | |
| 220 base::AtomicRefCount num_playing_streams_; | |
| 221 | |
| 222 // Salt required to translate renderer device IDs to raw device unique IDs | 213 // Salt required to translate renderer device IDs to raw device unique IDs |
| 223 std::string salt_; | 214 std::string salt_; |
| 224 | 215 |
| 225 // Map of device authorizations for streams that are not yet created | 216 // Map of device authorizations for streams that are not yet created |
| 226 // The key is the stream ID, and the value is a pair. The pair's first element | 217 // The key is the stream ID, and the value is a pair. The pair's first element |
| 227 // is a bool that is true if the authorization process completes successfully. | 218 // is a bool that is true if the authorization process completes successfully. |
| 228 // The second element contains the unique ID of the authorized device. | 219 // The second element contains the unique ID of the authorized device. |
| 229 std::map<int, std::pair<bool, std::string>> authorizations_; | 220 std::map<int, std::pair<bool, std::string>> authorizations_; |
| 230 | 221 |
| 231 // At stream creation time, AudioRendererHost will call this function on the | 222 // At stream creation time, AudioRendererHost will call this function on the |
| 232 // UI thread to validate render frame IDs. A default is set by the | 223 // UI thread to validate render frame IDs. A default is set by the |
| 233 // constructor, but this can be overridden by unit tests. | 224 // constructor, but this can be overridden by unit tests. |
| 234 ValidateRenderFrameIdFunction validate_render_frame_id_function_; | 225 ValidateRenderFrameIdFunction validate_render_frame_id_function_; |
| 235 | 226 |
| 236 // The maximum number of simultaneous streams during the lifetime of this | |
| 237 // host. Reported as UMA stat at shutdown. | |
| 238 size_t max_simultaneous_streams_; | |
| 239 | |
| 240 AudioOutputAuthorizationHandler authorization_handler_; | 227 AudioOutputAuthorizationHandler authorization_handler_; |
| 241 | 228 |
| 242 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); | 229 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); |
| 243 }; | 230 }; |
| 244 | 231 |
| 245 } // namespace content | 232 } // namespace content |
| 246 | 233 |
| 247 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ | 234 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ |
| OLD | NEW |