| 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" |
| 57 #include "content/browser/renderer_host/media/audio_output_authorization_handler
.h" | 60 #include "content/browser/renderer_host/media/audio_output_authorization_handler
.h" |
| 61 #include "content/browser/renderer_host/media/audio_output_delegate.h" |
| 58 #include "content/browser/renderer_host/media/media_devices_manager.h" | 62 #include "content/browser/renderer_host/media/media_devices_manager.h" |
| 59 #include "content/common/content_export.h" | 63 #include "content/common/content_export.h" |
| 60 #include "content/public/browser/browser_message_filter.h" | 64 #include "content/public/browser/browser_message_filter.h" |
| 61 #include "content/public/browser/browser_thread.h" | 65 #include "content/public/browser/browser_thread.h" |
| 62 #include "content/public/browser/render_process_host.h" | 66 #include "content/public/browser/render_process_host.h" |
| 63 #include "content/public/browser/resource_context.h" | 67 #include "content/public/browser/resource_context.h" |
| 64 #include "media/audio/audio_io.h" | 68 #include "media/audio/audio_io.h" |
| 65 #include "media/audio/audio_logging.h" | |
| 66 #include "media/audio/audio_output_controller.h" | 69 #include "media/audio/audio_output_controller.h" |
| 67 #include "media/audio/simple_sources.h" | 70 #include "media/audio/simple_sources.h" |
| 68 #include "media/base/output_device_info.h" | 71 #include "media/base/output_device_info.h" |
| 69 #include "url/origin.h" | 72 #include "url/origin.h" |
| 70 | 73 |
| 71 namespace media { | 74 namespace media { |
| 72 class AudioManager; | 75 class AudioManager; |
| 73 class AudioParameters; | 76 class AudioParameters; |
| 74 } | 77 } |
| 75 | 78 |
| 76 namespace content { | 79 namespace content { |
| 77 | 80 |
| 78 class AudioMirroringManager; | 81 class AudioMirroringManager; |
| 79 class MediaInternals; | |
| 80 class MediaStreamManager; | 82 class MediaStreamManager; |
| 81 | 83 |
| 82 class CONTENT_EXPORT AudioRendererHost : public BrowserMessageFilter { | 84 class CONTENT_EXPORT AudioRendererHost |
| 85 : public BrowserMessageFilter, |
| 86 private AudioOutputDelegate::EventHandler { |
| 83 public: | 87 public: |
| 84 // Called from UI thread from the owner of this object. | 88 // Called from UI thread from the owner of this object. |
| 85 AudioRendererHost(int render_process_id, | 89 AudioRendererHost(int render_process_id, |
| 86 media::AudioManager* audio_manager, | 90 media::AudioManager* audio_manager, |
| 87 AudioMirroringManager* mirroring_manager, | 91 AudioMirroringManager* mirroring_manager, |
| 88 MediaInternals* media_internals, | |
| 89 MediaStreamManager* media_stream_manager, | 92 MediaStreamManager* media_stream_manager, |
| 90 const std::string& salt); | 93 const std::string& salt); |
| 91 | 94 |
| 92 // Calls |callback| with the list of AudioOutputControllers for this object. | 95 // Calls |callback| with the list of AudioOutputControllers for this object. |
| 93 void GetOutputControllers( | 96 void GetOutputControllers( |
| 94 const RenderProcessHost::GetAudioOutputControllersCallback& | 97 const RenderProcessHost::GetAudioOutputControllersCallback& |
| 95 callback) const; | 98 callback) const; |
| 96 | 99 |
| 97 // BrowserMessageFilter implementation. | 100 // BrowserMessageFilter implementation. |
| 98 void OnChannelClosing() override; | 101 void OnChannelClosing() override; |
| 99 void OnDestruct() const override; | 102 void OnDestruct() const override; |
| 100 bool OnMessageReceived(const IPC::Message& message) override; | 103 bool OnMessageReceived(const IPC::Message& message) override; |
| 101 | 104 |
| 102 // Returns true if any streams managed by this host are actively playing. Can | 105 // Returns true if any streams managed by this host are actively playing. Can |
| 103 // be called from any thread. | 106 // be called from any thread. |
| 104 bool HasActiveAudio(); | 107 bool HasActiveAudio(); |
| 105 | 108 |
| 106 void OverrideDevicePermissionsForTesting(bool has_access); | 109 void OverrideDevicePermissionsForTesting(bool has_access); |
| 107 | 110 |
| 108 private: | 111 private: |
| 109 friend class AudioRendererHostTest; | 112 friend class AudioRendererHostTest; |
| 110 friend class BrowserThread; | 113 friend class BrowserThread; |
| 111 friend class base::DeleteHelper<AudioRendererHost>; | 114 friend class base::DeleteHelper<AudioRendererHost>; |
| 112 friend class MockAudioRendererHost; | 115 friend class MockAudioRendererHost; |
| 113 friend class TestAudioRendererHost; | 116 friend class TestAudioRendererHost; |
| 114 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, CreateMockStream); | 117 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, CreateMockStream); |
| 115 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, MockStreamDataConversation); | 118 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, MockStreamDataConversation); |
| 116 | 119 |
| 117 class AudioEntry; | |
| 118 typedef std::map<int, AudioEntry*> AudioEntryMap; | |
| 119 | |
| 120 // Internal callback type for access requests to output devices. | 120 // Internal callback type for access requests to output devices. |
| 121 // |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. |
| 122 typedef base::Callback<void(bool have_access)> OutputDeviceAccessCB; | 122 typedef base::Callback<void(bool have_access)> OutputDeviceAccessCB; |
| 123 | 123 |
| 124 using AudioOutputDelegateList = std::vector<AudioOutputDelegate::UniquePtr>; |
| 125 |
| 124 // The type of a function that is run on the UI thread to check whether the | 126 // The type of a function that is run on the UI thread to check whether the |
| 125 // routing IDs reference a valid RenderFrameHost. The function then runs | 127 // routing IDs reference a valid RenderFrameHost. The function then runs |
| 126 // |callback| on the IO thread with true/false if valid/invalid. | 128 // |callback| on the IO thread with true/false if valid/invalid. |
| 127 using ValidateRenderFrameIdFunction = | 129 using ValidateRenderFrameIdFunction = |
| 128 void (*)(int render_process_id, | 130 void (*)(int render_process_id, |
| 129 int render_frame_id, | 131 int render_frame_id, |
| 130 const base::Callback<void(bool)>& callback); | 132 const base::Callback<void(bool)>& callback); |
| 131 | 133 |
| 132 ~AudioRendererHost() override; | 134 ~AudioRendererHost() override; |
| 133 | 135 |
| 136 // AudioOutputDelegate::EventHandler implementation |
| 137 void OnStreamCreated(int stream_id, |
| 138 base::SharedMemory* shared_memory, |
| 139 base::CancelableSyncSocket* foreign_socket) override; |
| 140 void OnStreamError(int stream_id) override; |
| 141 void OnStreamStateChanged(bool is_playing) override; |
| 142 |
| 134 // Methods called on IO thread ---------------------------------------------- | 143 // Methods called on IO thread ---------------------------------------------- |
| 135 | 144 |
| 136 // Audio related IPC message handlers. | 145 // Audio related IPC message handlers. |
| 137 | 146 |
| 138 // Request permission to use an output device for use by a stream produced | 147 // Request permission to use an output device for use by a stream produced |
| 139 // in the RenderFrame referenced by |render_frame_id|. | 148 // in the RenderFrame referenced by |render_frame_id|. |
| 140 // |session_id| is used for unified IO to find out which input device to be | 149 // |session_id| is used for unified IO to find out which input device to be |
| 141 // opened for the stream. For clients that do not use unified IO, | 150 // opened for the stream. For clients that do not use unified IO, |
| 142 // |session_id| will be ignored and the given |device_id| and | 151 // |session_id| will be ignored and the given |device_id| and |
| 143 // |security_origin| will be used to select the output device. | 152 // |security_origin| will be used to select the output device. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 171 void OnPauseStream(int stream_id); | 180 void OnPauseStream(int stream_id); |
| 172 | 181 |
| 173 // Close the audio stream referenced by |stream_id|. | 182 // Close the audio stream referenced by |stream_id|. |
| 174 void OnCloseStream(int stream_id); | 183 void OnCloseStream(int stream_id); |
| 175 | 184 |
| 176 // Set the volume of the audio stream referenced by |stream_id|. | 185 // Set the volume of the audio stream referenced by |stream_id|. |
| 177 void OnSetVolume(int stream_id, double volume); | 186 void OnSetVolume(int stream_id, double volume); |
| 178 | 187 |
| 179 // Helper methods. | 188 // Helper methods. |
| 180 | 189 |
| 181 // Complete the process of creating an audio stream. This will set up the | |
| 182 // shared memory or shared socket in low latency mode and send the | |
| 183 // NotifyStreamCreated message to the peer. | |
| 184 void DoCompleteCreation(int stream_id); | |
| 185 | |
| 186 // Called after the |render_frame_id| provided to OnCreateStream() was | 190 // Called after the |render_frame_id| provided to OnCreateStream() was |
| 187 // validated. When |is_valid| is false, this calls ReportErrorAndClose(). | 191 // validated. When |is_valid| is false, this calls OnStreamError(). |
| 188 void DidValidateRenderFrame(int stream_id, bool is_valid); | 192 void DidValidateRenderFrame(int stream_id, bool is_valid); |
| 189 | 193 |
| 190 // Updates status of stream for AudioStreamMonitor and updates | 194 // Updates status of stream for AudioStreamMonitor and updates |
| 191 // the number of playing streams. | 195 // the number of playing streams. |
| 192 void StreamStateChanged(int stream_id, bool is_playing); | 196 void StreamStateChanged(int stream_id, bool is_playing); |
| 193 | 197 |
| 194 RenderProcessHost::AudioOutputControllerList DoGetOutputControllers() const; | 198 RenderProcessHost::AudioOutputControllerList DoGetOutputControllers() const; |
| 195 | 199 |
| 196 // Send an error message to the renderer. | 200 // Send an error message to the renderer. |
| 197 void SendErrorMessage(int stream_id); | 201 void SendErrorMessage(int stream_id); |
| 198 | 202 |
| 199 // Delete an audio entry, notifying observers first. This is called by | 203 // Helper methods to look up a AudioOutputDelegate identified by |stream_id|. |
| 200 // AudioOutputController after it has closed. | 204 // Returns delegates_.end() if not found. |
| 201 void DeleteEntry(std::unique_ptr<AudioEntry> entry); | 205 AudioOutputDelegateList::iterator LookupIteratorById(int stream_id); |
| 202 | 206 // Returns nullptr if not found. |
| 203 // Send an error message to the renderer, then close the stream. | 207 AudioOutputDelegate* LookupById(int stream_id); |
| 204 void ReportErrorAndClose(int stream_id); | |
| 205 | |
| 206 // A helper method to look up a AudioEntry identified by |stream_id|. | |
| 207 // Returns NULL if not found. | |
| 208 AudioEntry* LookupById(int stream_id); | |
| 209 | |
| 210 // A helper method to update the number of playing streams and alert the | |
| 211 // ResourceScheduler when the renderer starts or stops playing an audiostream. | |
| 212 void UpdateNumPlayingStreams(AudioEntry* entry, bool is_playing); | |
| 213 | 208 |
| 214 // Helper method to check if the authorization procedure for stream | 209 // Helper method to check if the authorization procedure for stream |
| 215 // |stream_id| has started. | 210 // |stream_id| has started. |
| 216 bool IsAuthorizationStarted(int stream_id); | 211 bool IsAuthorizationStarted(int stream_id); |
| 217 | 212 |
| 218 // Called from AudioRendererHostTest to override the function that checks for | 213 // Called from AudioRendererHostTest to override the function that checks for |
| 219 // the existence of the RenderFrameHost at stream creation time. | 214 // the existence of the RenderFrameHost at stream creation time. |
| 220 void set_render_frame_id_validate_function_for_testing( | 215 void set_render_frame_id_validate_function_for_testing( |
| 221 ValidateRenderFrameIdFunction function) { | 216 ValidateRenderFrameIdFunction function) { |
| 222 validate_render_frame_id_function_ = function; | 217 validate_render_frame_id_function_ = function; |
| 223 } | 218 } |
| 224 | 219 |
| 225 // ID of the RenderProcessHost that owns this instance. | 220 // ID of the RenderProcessHost that owns this instance. |
| 226 const int render_process_id_; | 221 const int render_process_id_; |
| 227 | 222 |
| 228 media::AudioManager* const audio_manager_; | 223 media::AudioManager* const audio_manager_; |
| 229 AudioMirroringManager* const mirroring_manager_; | 224 AudioMirroringManager* const mirroring_manager_; |
| 230 std::unique_ptr<media::AudioLog> audio_log_; | |
| 231 | 225 |
| 232 // A map of stream IDs to audio sources. | 226 // Used to access to AudioInputDeviceManager. |
| 233 AudioEntryMap audio_entries_; | 227 MediaStreamManager* media_stream_manager_; |
| 228 |
| 229 // A list of the current open streams. |
| 230 AudioOutputDelegateList delegates_; |
| 234 | 231 |
| 235 // The number of streams in the playing state. Atomic read safe from any | 232 // The number of streams in the playing state. Atomic read safe from any |
| 236 // thread, but should only be updated from the IO thread. | 233 // thread, but should only be updated from the IO thread. |
| 237 base::AtomicRefCount num_playing_streams_; | 234 base::AtomicRefCount num_playing_streams_; |
| 238 | 235 |
| 239 // Salt required to translate renderer device IDs to raw device unique IDs | 236 // Salt required to translate renderer device IDs to raw device unique IDs |
| 240 std::string salt_; | 237 std::string salt_; |
| 241 | 238 |
| 242 // Map of device authorizations for streams that are not yet created | 239 // Map of device authorizations for streams that are not yet created |
| 243 // The key is the stream ID, and the value is a pair. The pair's first element | 240 // The key is the stream ID, and the value is a pair. The pair's first element |
| (...skipping 11 matching lines...) Expand all Loading... |
| 255 size_t max_simultaneous_streams_; | 252 size_t max_simultaneous_streams_; |
| 256 | 253 |
| 257 AudioOutputAuthorizationHandler authorization_handler_; | 254 AudioOutputAuthorizationHandler authorization_handler_; |
| 258 | 255 |
| 259 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); | 256 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); |
| 260 }; | 257 }; |
| 261 | 258 |
| 262 } // namespace content | 259 } // namespace content |
| 263 | 260 |
| 264 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ | 261 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ |
| OLD | NEW |