| 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 28 matching lines...) Expand all Loading... |
| 39 | 39 |
| 40 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ | 40 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ |
| 41 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ | 41 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ |
| 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 #include <vector> |
| 49 | 50 |
| 50 #include "base/atomic_ref_count.h" | 51 #include "base/atomic_ref_count.h" |
| 51 #include "base/gtest_prod_util.h" | 52 #include "base/gtest_prod_util.h" |
| 52 #include "base/logging.h" | 53 #include "base/logging.h" |
| 53 #include "base/macros.h" | 54 #include "base/macros.h" |
| 54 #include "base/memory/ref_counted.h" | 55 #include "base/memory/ref_counted.h" |
| 56 #include "base/memory/shared_memory.h" |
| 55 #include "base/process/process.h" | 57 #include "base/process/process.h" |
| 56 #include "base/sequenced_task_runner_helpers.h" | 58 #include "base/sequenced_task_runner_helpers.h" |
| 59 #include "base/sync_socket.h" |
| 60 #include "content/browser/renderer_host/media/audio_output_delegate.h" |
| 57 #include "content/browser/renderer_host/media/media_devices_manager.h" | 61 #include "content/browser/renderer_host/media/media_devices_manager.h" |
| 58 #include "content/common/content_export.h" | 62 #include "content/common/content_export.h" |
| 59 #include "content/public/browser/browser_message_filter.h" | 63 #include "content/public/browser/browser_message_filter.h" |
| 60 #include "content/public/browser/browser_thread.h" | 64 #include "content/public/browser/browser_thread.h" |
| 61 #include "content/public/browser/render_process_host.h" | 65 #include "content/public/browser/render_process_host.h" |
| 62 #include "content/public/browser/resource_context.h" | 66 #include "content/public/browser/resource_context.h" |
| 63 #include "media/audio/audio_io.h" | 67 #include "media/audio/audio_io.h" |
| 64 #include "media/audio/audio_logging.h" | 68 #include "media/audio/audio_logging.h" |
| 65 #include "media/audio/audio_output_controller.h" | 69 #include "media/audio/audio_output_controller.h" |
| 66 #include "media/audio/simple_sources.h" | 70 #include "media/audio/simple_sources.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; | |
| 78 class MediaStreamManager; | 81 class MediaStreamManager; |
| 79 class MediaStreamUIProxy; | 82 class MediaStreamUIProxy; |
| 80 class ResourceContext; | 83 class ResourceContext; |
| 81 | 84 |
| 82 class CONTENT_EXPORT AudioRendererHost : public BrowserMessageFilter { | 85 class CONTENT_EXPORT AudioRendererHost |
| 86 : public BrowserMessageFilter, |
| 87 private AudioOutputDelegate::EventHandler { |
| 83 public: | 88 public: |
| 84 // Called from UI thread from the owner of this object. | 89 // Called from UI thread from the owner of this object. |
| 85 AudioRendererHost(int render_process_id, | 90 AudioRendererHost(int render_process_id, |
| 86 media::AudioManager* audio_manager, | 91 media::AudioManager* audio_manager, |
| 87 AudioMirroringManager* mirroring_manager, | 92 AudioMirroringManager* mirroring_manager, |
| 88 MediaInternals* media_internals, | 93 media::AudioLogFactory* log_factory, |
| 89 MediaStreamManager* media_stream_manager, | 94 MediaStreamManager* media_stream_manager, |
| 90 const std::string& salt); | 95 const std::string& salt); |
| 91 | 96 |
| 92 // Calls |callback| with the list of AudioOutputControllers for this object. | 97 // Calls |callback| with the list of AudioOutputControllers for this object. |
| 93 void GetOutputControllers( | 98 void GetOutputControllers( |
| 94 const RenderProcessHost::GetAudioOutputControllersCallback& | 99 const RenderProcessHost::GetAudioOutputControllersCallback& |
| 95 callback) const; | 100 callback) const; |
| 96 | 101 |
| 97 // BrowserMessageFilter implementation. | 102 // BrowserMessageFilter implementation. |
| 98 void OnChannelClosing() override; | 103 void OnChannelClosing() override; |
| 99 void OnDestruct() const override; | 104 void OnDestruct() const override; |
| 100 bool OnMessageReceived(const IPC::Message& message) override; | 105 bool OnMessageReceived(const IPC::Message& message) override; |
| 101 | 106 |
| 102 // Returns true if any streams managed by this host are actively playing. Can | 107 // Returns true if any streams managed by this host are actively playing. Can |
| 103 // be called from any thread. | 108 // be called from any thread. |
| 104 bool HasActiveAudio(); | 109 bool HasActiveAudio(); |
| 105 | 110 |
| 106 private: | 111 private: |
| 107 friend class AudioRendererHostTest; | 112 friend class AudioRendererHostTest; |
| 108 friend class BrowserThread; | 113 friend class BrowserThread; |
| 109 friend class base::DeleteHelper<AudioRendererHost>; | 114 friend class base::DeleteHelper<AudioRendererHost>; |
| 110 friend class MockAudioRendererHost; | 115 friend class MockAudioRendererHost; |
| 111 friend class TestAudioRendererHost; | 116 friend class TestAudioRendererHost; |
| 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); |
| 114 | 119 |
| 115 class AudioEntry; | |
| 116 typedef std::map<int, AudioEntry*> AudioEntryMap; | |
| 117 | |
| 118 // Internal callback type for access requests to output devices. | 120 // Internal callback type for access requests to output devices. |
| 119 // |have_access| is true only if there is permission to access the device. | 121 // |have_access| is true only if there is permission to access the device. |
| 120 typedef base::Callback<void(bool have_access)> OutputDeviceAccessCB; | 122 typedef base::Callback<void(bool have_access)> OutputDeviceAccessCB; |
| 121 | 123 |
| 122 // The type of a function that is run on the UI thread to check whether the | 124 // The type of a function that is run on the UI thread to check whether the |
| 123 // routing IDs reference a valid RenderFrameHost. The function then runs | 125 // routing IDs reference a valid RenderFrameHost. The function then runs |
| 124 // |callback| on the IO thread with true/false if valid/invalid. | 126 // |callback| on the IO thread with true/false if valid/invalid. |
| 125 using ValidateRenderFrameIdFunction = | 127 using ValidateRenderFrameIdFunction = |
| 126 void (*)(int render_process_id, | 128 void (*)(int render_process_id, |
| 127 int render_frame_id, | 129 int render_frame_id, |
| 128 const base::Callback<void(bool)>& callback); | 130 const base::Callback<void(bool)>& callback); |
| 129 | 131 |
| 130 ~AudioRendererHost() override; | 132 ~AudioRendererHost() override; |
| 131 | 133 |
| 134 // AudioOutputDelegate::EventHandler implementation |
| 135 void OnStreamCreated(int stream_id, |
| 136 base::SharedMemory* shared_memory, |
| 137 base::CancelableSyncSocket* foreign_socket) override; |
| 138 void OnStreamError(int stream_id) override; |
| 139 void OnStreamStateChanged(bool is_playing) override; |
| 140 |
| 132 // Methods called on IO thread ---------------------------------------------- | 141 // Methods called on IO thread ---------------------------------------------- |
| 133 | 142 |
| 134 // Audio related IPC message handlers. | 143 // Audio related IPC message handlers. |
| 135 | 144 |
| 136 // 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 |
| 137 // in the RenderFrame referenced by |render_frame_id|. | 146 // in the RenderFrame referenced by |render_frame_id|. |
| 138 // |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 |
| 139 // 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, |
| 140 // |session_id| will be ignored and the given |device_id| and | 149 // |session_id| will be ignored and the given |device_id| and |
| 141 // |security_origin| will be used to select the output device. | 150 // |security_origin| will be used to select the output device. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 161 void OnPauseStream(int stream_id); | 170 void OnPauseStream(int stream_id); |
| 162 | 171 |
| 163 // Close the audio stream referenced by |stream_id|. | 172 // Close the audio stream referenced by |stream_id|. |
| 164 void OnCloseStream(int stream_id); | 173 void OnCloseStream(int stream_id); |
| 165 | 174 |
| 166 // Set the volume of the audio stream referenced by |stream_id|. | 175 // Set the volume of the audio stream referenced by |stream_id|. |
| 167 void OnSetVolume(int stream_id, double volume); | 176 void OnSetVolume(int stream_id, double volume); |
| 168 | 177 |
| 169 // Helper methods. | 178 // Helper methods. |
| 170 | 179 |
| 171 // Complete the process of creating an audio stream. This will set up the | |
| 172 // shared memory or shared socket in low latency mode and send the | |
| 173 // NotifyStreamCreated message to the peer. | |
| 174 void DoCompleteCreation(int stream_id); | |
| 175 | |
| 176 // Called after the |render_frame_id| provided to OnCreateStream() was | 180 // Called after the |render_frame_id| provided to OnCreateStream() was |
| 177 // validated. When |is_valid| is false, this calls ReportErrorAndClose(). | 181 // validated. When |is_valid| is false, this calls ReportErrorAndClose(). |
| 178 void DidValidateRenderFrame(int stream_id, bool is_valid); | 182 void DidValidateRenderFrame(int stream_id, bool is_valid); |
| 179 | 183 |
| 180 // Updates status of stream for AudioStreamMonitor and updates | 184 // Updates status of stream for AudioStreamMonitor and updates |
| 181 // the number of playing streams. | 185 // the number of playing streams. |
| 182 void StreamStateChanged(int stream_id, bool is_playing); | 186 void StreamStateChanged(int stream_id, bool is_playing); |
| 183 | 187 |
| 184 RenderProcessHost::AudioOutputControllerList DoGetOutputControllers() const; | 188 RenderProcessHost::AudioOutputControllerList DoGetOutputControllers() const; |
| 185 | 189 |
| 186 // Send an error message to the renderer. | 190 // Send an error message to the renderer. |
| 187 void SendErrorMessage(int stream_id); | 191 void SendErrorMessage(int stream_id); |
| 188 | 192 |
| 189 // Delete an audio entry, notifying observers first. This is called by | |
| 190 // AudioOutputController after it has closed. | |
| 191 void DeleteEntry(std::unique_ptr<AudioEntry> entry); | |
| 192 | |
| 193 // Send an error message to the renderer, then close the stream. | 193 // Send an error message to the renderer, then close the stream. |
| 194 void ReportErrorAndClose(int stream_id); | 194 void ReportErrorAndClose(int stream_id); |
| 195 | 195 |
| 196 // A helper method to look up a AudioEntry identified by |stream_id|. | 196 // A helper method to look up a AudioOutputDelegate identified by |stream_id|. |
| 197 // Returns NULL if not found. | 197 // Returns nullptr if not found. |
| 198 AudioEntry* LookupById(int stream_id); | 198 AudioOutputDelegate* LookupById(int stream_id); |
| 199 | |
| 200 // A helper method to update the number of playing streams and alert the | |
| 201 // ResourceScheduler when the renderer starts or stops playing an audiostream. | |
| 202 void UpdateNumPlayingStreams(AudioEntry* entry, bool is_playing); | |
| 203 | 199 |
| 204 // Check if the renderer process has access to the requested output device. | 200 // Check if the renderer process has access to the requested output device. |
| 205 void CheckOutputDeviceAccess(int render_frame_id, | 201 void CheckOutputDeviceAccess(int render_frame_id, |
| 206 const std::string& device_id, | 202 const std::string& device_id, |
| 207 const url::Origin& security_origin, | 203 const url::Origin& security_origin, |
| 208 int stream_id, | 204 int stream_id, |
| 209 base::TimeTicks auth_start_time); | 205 base::TimeTicks auth_start_time); |
| 210 | 206 |
| 211 // Proceed with device authorization after checking permissions. | 207 // Proceed with device authorization after checking permissions. |
| 212 void AccessChecked(std::unique_ptr<MediaStreamUIProxy> ui_proxy, | 208 void AccessChecked(std::unique_ptr<MediaStreamUIProxy> ui_proxy, |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 void set_render_frame_id_validate_function_for_testing( | 240 void set_render_frame_id_validate_function_for_testing( |
| 245 ValidateRenderFrameIdFunction function) { | 241 ValidateRenderFrameIdFunction function) { |
| 246 validate_render_frame_id_function_ = function; | 242 validate_render_frame_id_function_ = function; |
| 247 } | 243 } |
| 248 | 244 |
| 249 // ID of the RenderProcessHost that owns this instance. | 245 // ID of the RenderProcessHost that owns this instance. |
| 250 const int render_process_id_; | 246 const int render_process_id_; |
| 251 | 247 |
| 252 media::AudioManager* const audio_manager_; | 248 media::AudioManager* const audio_manager_; |
| 253 AudioMirroringManager* const mirroring_manager_; | 249 AudioMirroringManager* const mirroring_manager_; |
| 250 media::AudioLogFactory* log_factory_; |
| 254 std::unique_ptr<media::AudioLog> audio_log_; | 251 std::unique_ptr<media::AudioLog> audio_log_; |
| 255 | 252 |
| 256 // Used to access to AudioInputDeviceManager. | 253 // Used to access to AudioInputDeviceManager. |
| 257 MediaStreamManager* media_stream_manager_; | 254 MediaStreamManager* media_stream_manager_; |
| 258 | 255 |
| 259 // A map of stream IDs to audio sources. | 256 using AudioOutputDelegateList = |
| 260 AudioEntryMap audio_entries_; | 257 std::vector<std::unique_ptr<AudioOutputDelegate>>; |
| 258 |
| 259 // A list of the current open streams. |
| 260 AudioOutputDelegateList delegates_; |
| 261 | 261 |
| 262 // The number of streams in the playing state. Atomic read safe from any | 262 // The number of streams in the playing state. Atomic read safe from any |
| 263 // thread, but should only be updated from the IO thread. | 263 // thread, but should only be updated from the IO thread. |
| 264 base::AtomicRefCount num_playing_streams_; | 264 base::AtomicRefCount num_playing_streams_; |
| 265 | 265 |
| 266 // Salt required to translate renderer device IDs to raw device unique IDs | 266 // Salt required to translate renderer device IDs to raw device unique IDs |
| 267 std::string salt_; | 267 std::string salt_; |
| 268 | 268 |
| 269 // Map of device authorizations for streams that are not yet created | 269 // Map of device authorizations for streams that are not yet created |
| 270 // The key is the stream ID, and the value is a pair. The pair's first element | 270 // The key is the stream ID, and the value is a pair. The pair's first element |
| 271 // is a bool that is true if the authorization process completes successfully. | 271 // is a bool that is true if the authorization process completes successfully. |
| 272 // The second element contains the unique ID of the authorized device. | 272 // The second element contains the unique ID of the authorized device. |
| 273 std::map<int, std::pair<bool, std::string>> authorizations_; | 273 std::map<int, std::pair<bool, std::string>> authorizations_; |
| 274 | 274 |
| 275 // At stream creation time, AudioRendererHost will call this function on the | 275 // At stream creation time, AudioRendererHost will call this function on the |
| 276 // UI thread to validate render frame IDs. A default is set by the | 276 // UI thread to validate render frame IDs. A default is set by the |
| 277 // constructor, but this can be overridden by unit tests. | 277 // constructor, but this can be overridden by unit tests. |
| 278 ValidateRenderFrameIdFunction validate_render_frame_id_function_; | 278 ValidateRenderFrameIdFunction validate_render_frame_id_function_; |
| 279 | 279 |
| 280 // The maximum number of simultaneous streams during the lifetime of this | 280 // The maximum number of simultaneous streams during the lifetime of this |
| 281 // host. Reported as UMA stat at shutdown. | 281 // host. Reported as UMA stat at shutdown. |
| 282 size_t max_simultaneous_streams_; | 282 size_t max_simultaneous_streams_; |
| 283 | 283 |
| 284 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); | 284 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); |
| 285 }; | 285 }; |
| 286 | 286 |
| 287 } // namespace content | 287 } // namespace content |
| 288 | 288 |
| 289 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ | 289 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ |
| OLD | NEW |