| 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 |
| 124 using AudioOutputDelegateList = |
| 125 std::vector<std::unique_ptr<AudioOutputDelegate>>; |
| 126 |
| 122 // The type of a function that is run on the UI thread to check whether the | 127 // 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 | 128 // routing IDs reference a valid RenderFrameHost. The function then runs |
| 124 // |callback| on the IO thread with true/false if valid/invalid. | 129 // |callback| on the IO thread with true/false if valid/invalid. |
| 125 using ValidateRenderFrameIdFunction = | 130 using ValidateRenderFrameIdFunction = |
| 126 void (*)(int render_process_id, | 131 void (*)(int render_process_id, |
| 127 int render_frame_id, | 132 int render_frame_id, |
| 128 const base::Callback<void(bool)>& callback); | 133 const base::Callback<void(bool)>& callback); |
| 129 | 134 |
| 130 ~AudioRendererHost() override; | 135 ~AudioRendererHost() override; |
| 131 | 136 |
| 137 // AudioOutputDelegate::EventHandler implementation |
| 138 void OnStreamCreated(int stream_id, |
| 139 base::SharedMemory* shared_memory, |
| 140 base::CancelableSyncSocket* foreign_socket) override; |
| 141 void OnStreamError(int stream_id) override; |
| 142 void OnStreamStateChanged(bool is_playing) override; |
| 143 |
| 132 // Methods called on IO thread ---------------------------------------------- | 144 // Methods called on IO thread ---------------------------------------------- |
| 133 | 145 |
| 134 // Audio related IPC message handlers. | 146 // Audio related IPC message handlers. |
| 135 | 147 |
| 136 // Request permission to use an output device for use by a stream produced | 148 // Request permission to use an output device for use by a stream produced |
| 137 // in the RenderFrame referenced by |render_frame_id|. | 149 // in the RenderFrame referenced by |render_frame_id|. |
| 138 // |session_id| is used for unified IO to find out which input device to be | 150 // |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, | 151 // opened for the stream. For clients that do not use unified IO, |
| 140 // |session_id| will be ignored and the given |device_id| and | 152 // |session_id| will be ignored and the given |device_id| and |
| 141 // |security_origin| will be used to select the output device. | 153 // |security_origin| will be used to select the output device. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 161 void OnPauseStream(int stream_id); | 173 void OnPauseStream(int stream_id); |
| 162 | 174 |
| 163 // Close the audio stream referenced by |stream_id|. | 175 // Close the audio stream referenced by |stream_id|. |
| 164 void OnCloseStream(int stream_id); | 176 void OnCloseStream(int stream_id); |
| 165 | 177 |
| 166 // Set the volume of the audio stream referenced by |stream_id|. | 178 // Set the volume of the audio stream referenced by |stream_id|. |
| 167 void OnSetVolume(int stream_id, double volume); | 179 void OnSetVolume(int stream_id, double volume); |
| 168 | 180 |
| 169 // Helper methods. | 181 // Helper methods. |
| 170 | 182 |
| 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 | 183 // Called after the |render_frame_id| provided to OnCreateStream() was |
| 177 // validated. When |is_valid| is false, this calls ReportErrorAndClose(). | 184 // validated. When |is_valid| is false, this calls OnStreamError(). |
| 178 void DidValidateRenderFrame(int stream_id, bool is_valid); | 185 void DidValidateRenderFrame(int stream_id, bool is_valid); |
| 179 | 186 |
| 180 // Updates status of stream for AudioStreamMonitor and updates | 187 // Updates status of stream for AudioStreamMonitor and updates |
| 181 // the number of playing streams. | 188 // the number of playing streams. |
| 182 void StreamStateChanged(int stream_id, bool is_playing); | 189 void StreamStateChanged(int stream_id, bool is_playing); |
| 183 | 190 |
| 184 RenderProcessHost::AudioOutputControllerList DoGetOutputControllers() const; | 191 RenderProcessHost::AudioOutputControllerList DoGetOutputControllers() const; |
| 185 | 192 |
| 186 // Send an error message to the renderer. | 193 // Send an error message to the renderer. |
| 187 void SendErrorMessage(int stream_id); | 194 void SendErrorMessage(int stream_id); |
| 188 | 195 |
| 189 // Delete an audio entry, notifying observers first. This is called by | 196 // Helper methods to look up a AudioOutputDelegate identified by |stream_id|. |
| 190 // AudioOutputController after it has closed. | 197 // Returns delegates_.end() if not found. |
| 191 void DeleteEntry(std::unique_ptr<AudioEntry> entry); | 198 AudioOutputDelegateList::iterator LookupIteratorById(int stream_id); |
| 192 | 199 // Returns nullptr if not found. |
| 193 // Send an error message to the renderer, then close the stream. | 200 AudioOutputDelegate* LookupById(int stream_id); |
| 194 void ReportErrorAndClose(int stream_id); | |
| 195 | |
| 196 // A helper method to look up a AudioEntry identified by |stream_id|. | |
| 197 // Returns NULL if not found. | |
| 198 AudioEntry* 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 | 201 |
| 204 // Check if the renderer process has access to the requested output device. | 202 // Check if the renderer process has access to the requested output device. |
| 205 void CheckOutputDeviceAccess(int render_frame_id, | 203 void CheckOutputDeviceAccess(int render_frame_id, |
| 206 const std::string& device_id, | 204 const std::string& device_id, |
| 207 const url::Origin& security_origin, | 205 const url::Origin& security_origin, |
| 208 int stream_id, | 206 int stream_id, |
| 209 base::TimeTicks auth_start_time); | 207 base::TimeTicks auth_start_time); |
| 210 | 208 |
| 211 // Proceed with device authorization after checking permissions. | 209 // Proceed with device authorization after checking permissions. |
| 212 void AccessChecked(std::unique_ptr<MediaStreamUIProxy> ui_proxy, | 210 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( | 242 void set_render_frame_id_validate_function_for_testing( |
| 245 ValidateRenderFrameIdFunction function) { | 243 ValidateRenderFrameIdFunction function) { |
| 246 validate_render_frame_id_function_ = function; | 244 validate_render_frame_id_function_ = function; |
| 247 } | 245 } |
| 248 | 246 |
| 249 // ID of the RenderProcessHost that owns this instance. | 247 // ID of the RenderProcessHost that owns this instance. |
| 250 const int render_process_id_; | 248 const int render_process_id_; |
| 251 | 249 |
| 252 media::AudioManager* const audio_manager_; | 250 media::AudioManager* const audio_manager_; |
| 253 AudioMirroringManager* const mirroring_manager_; | 251 AudioMirroringManager* const mirroring_manager_; |
| 252 media::AudioLogFactory* log_factory_; |
| 254 std::unique_ptr<media::AudioLog> audio_log_; | 253 std::unique_ptr<media::AudioLog> audio_log_; |
| 255 | 254 |
| 256 // Used to access to AudioInputDeviceManager. | 255 // Used to access to AudioInputDeviceManager. |
| 257 MediaStreamManager* media_stream_manager_; | 256 MediaStreamManager* media_stream_manager_; |
| 258 | 257 |
| 259 // A map of stream IDs to audio sources. | 258 // A list of the current open streams. |
| 260 AudioEntryMap audio_entries_; | 259 AudioOutputDelegateList delegates_; |
| 261 | 260 |
| 262 // The number of streams in the playing state. Atomic read safe from any | 261 // The number of streams in the playing state. Atomic read safe from any |
| 263 // thread, but should only be updated from the IO thread. | 262 // thread, but should only be updated from the IO thread. |
| 264 base::AtomicRefCount num_playing_streams_; | 263 base::AtomicRefCount num_playing_streams_; |
| 265 | 264 |
| 266 // Salt required to translate renderer device IDs to raw device unique IDs | 265 // Salt required to translate renderer device IDs to raw device unique IDs |
| 267 std::string salt_; | 266 std::string salt_; |
| 268 | 267 |
| 269 // Map of device authorizations for streams that are not yet created | 268 // 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 | 269 // 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. | 270 // is a bool that is true if the authorization process completes successfully. |
| 272 // The second element contains the unique ID of the authorized device. | 271 // The second element contains the unique ID of the authorized device. |
| 273 std::map<int, std::pair<bool, std::string>> authorizations_; | 272 std::map<int, std::pair<bool, std::string>> authorizations_; |
| 274 | 273 |
| 275 // At stream creation time, AudioRendererHost will call this function on the | 274 // 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 | 275 // UI thread to validate render frame IDs. A default is set by the |
| 277 // constructor, but this can be overridden by unit tests. | 276 // constructor, but this can be overridden by unit tests. |
| 278 ValidateRenderFrameIdFunction validate_render_frame_id_function_; | 277 ValidateRenderFrameIdFunction validate_render_frame_id_function_; |
| 279 | 278 |
| 280 // The maximum number of simultaneous streams during the lifetime of this | 279 // The maximum number of simultaneous streams during the lifetime of this |
| 281 // host. Reported as UMA stat at shutdown. | 280 // host. Reported as UMA stat at shutdown. |
| 282 size_t max_simultaneous_streams_; | 281 size_t max_simultaneous_streams_; |
| 283 | 282 |
| 284 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); | 283 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); |
| 285 }; | 284 }; |
| 286 | 285 |
| 287 } // namespace content | 286 } // namespace content |
| 288 | 287 |
| 289 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ | 288 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ |
| OLD | NEW |