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