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; | 81 class MediaInternals; |
78 class MediaStreamManager; | 82 class MediaStreamManager; |
79 class MediaStreamUIProxy; | 83 class MediaStreamUIProxy; |
80 class ResourceContext; | 84 class ResourceContext; |
81 | 85 |
82 class CONTENT_EXPORT AudioRendererHost : public BrowserMessageFilter { | 86 class CONTENT_EXPORT AudioRendererHost |
| 87 : public BrowserMessageFilter, |
| 88 private AudioOutputDelegate::EventHandler { |
83 public: | 89 public: |
84 // Called from UI thread from the owner of this object. | 90 // Called from UI thread from the owner of this object. |
85 AudioRendererHost(int render_process_id, | 91 AudioRendererHost(int render_process_id, |
86 media::AudioManager* audio_manager, | 92 media::AudioManager* audio_manager, |
87 AudioMirroringManager* mirroring_manager, | 93 AudioMirroringManager* mirroring_manager, |
88 MediaInternals* media_internals, | 94 MediaInternals* media_internals, |
89 MediaStreamManager* media_stream_manager, | 95 MediaStreamManager* media_stream_manager, |
90 const std::string& salt); | 96 const std::string& salt); |
91 | 97 |
92 // Calls |callback| with the list of AudioOutputControllers for this object. | 98 // Calls |callback| with the list of AudioOutputControllers for this object. |
(...skipping 12 matching lines...) Expand all Loading... |
105 | 111 |
106 private: | 112 private: |
107 friend class AudioRendererHostTest; | 113 friend class AudioRendererHostTest; |
108 friend class BrowserThread; | 114 friend class BrowserThread; |
109 friend class base::DeleteHelper<AudioRendererHost>; | 115 friend class base::DeleteHelper<AudioRendererHost>; |
110 friend class MockAudioRendererHost; | 116 friend class MockAudioRendererHost; |
111 friend class TestAudioRendererHost; | 117 friend class TestAudioRendererHost; |
112 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, CreateMockStream); | 118 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, CreateMockStream); |
113 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, MockStreamDataConversation); | 119 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, MockStreamDataConversation); |
114 | 120 |
115 class AudioEntry; | |
116 typedef std::map<int, AudioEntry*> AudioEntryMap; | |
117 | |
118 // Internal callback type for access requests to output devices. | 121 // Internal callback type for access requests to output devices. |
119 // |have_access| is true only if there is permission to access the device. | 122 // |have_access| is true only if there is permission to access the device. |
120 typedef base::Callback<void(bool have_access)> OutputDeviceAccessCB; | 123 typedef base::Callback<void(bool have_access)> OutputDeviceAccessCB; |
121 | 124 |
122 // 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 |
123 // routing IDs reference a valid RenderFrameHost. The function then runs | 126 // routing IDs reference a valid RenderFrameHost. The function then runs |
124 // |callback| on the IO thread with true/false if valid/invalid. | 127 // |callback| on the IO thread with true/false if valid/invalid. |
125 using ValidateRenderFrameIdFunction = | 128 using ValidateRenderFrameIdFunction = |
126 void (*)(int render_process_id, | 129 void (*)(int render_process_id, |
127 int render_frame_id, | 130 int render_frame_id, |
128 const base::Callback<void(bool)>& callback); | 131 const base::Callback<void(bool)>& callback); |
129 | 132 |
130 ~AudioRendererHost() override; | 133 ~AudioRendererHost() override; |
131 | 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 |
132 // Methods called on IO thread ---------------------------------------------- | 142 // Methods called on IO thread ---------------------------------------------- |
133 | 143 |
134 // Audio related IPC message handlers. | 144 // Audio related IPC message handlers. |
135 | 145 |
136 // 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 |
137 // in the RenderFrame referenced by |render_frame_id|. | 147 // in the RenderFrame referenced by |render_frame_id|. |
138 // |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 |
139 // 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, |
140 // |session_id| will be ignored and the given |device_id| and | 150 // |session_id| will be ignored and the given |device_id| and |
141 // |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... |
161 void OnPauseStream(int stream_id); | 171 void OnPauseStream(int stream_id); |
162 | 172 |
163 // Close the audio stream referenced by |stream_id|. | 173 // Close the audio stream referenced by |stream_id|. |
164 void OnCloseStream(int stream_id); | 174 void OnCloseStream(int stream_id); |
165 | 175 |
166 // Set the volume of the audio stream referenced by |stream_id|. | 176 // Set the volume of the audio stream referenced by |stream_id|. |
167 void OnSetVolume(int stream_id, double volume); | 177 void OnSetVolume(int stream_id, double volume); |
168 | 178 |
169 // Helper methods. | 179 // Helper methods. |
170 | 180 |
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 | 181 // Called after the |render_frame_id| provided to OnCreateStream() was |
177 // validated. When |is_valid| is false, this calls ReportErrorAndClose(). | 182 // validated. When |is_valid| is false, this calls ReportErrorAndClose(). |
178 void DidValidateRenderFrame(int stream_id, bool is_valid); | 183 void DidValidateRenderFrame(int stream_id, bool is_valid); |
179 | 184 |
180 // Updates status of stream for AudioStreamMonitor and updates | 185 // Updates status of stream for AudioStreamMonitor and updates |
181 // the number of playing streams. | 186 // the number of playing streams. |
182 void StreamStateChanged(int stream_id, bool is_playing); | 187 void StreamStateChanged(int stream_id, bool is_playing); |
183 | 188 |
184 RenderProcessHost::AudioOutputControllerList DoGetOutputControllers() const; | 189 RenderProcessHost::AudioOutputControllerList DoGetOutputControllers() const; |
185 | 190 |
186 // Send an error message to the renderer. | 191 // Send an error message to the renderer. |
187 void SendErrorMessage(int stream_id); | 192 void SendErrorMessage(int stream_id); |
188 | 193 |
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. | 194 // Send an error message to the renderer, then close the stream. |
194 void ReportErrorAndClose(int stream_id); | 195 void ReportErrorAndClose(int stream_id); |
195 | 196 |
196 // A helper method to look up a AudioEntry identified by |stream_id|. | 197 // A helper method to look up a AudioOutputDelegate identified by |stream_id|. |
197 // Returns NULL if not found. | 198 // Returns nullptr if not found. |
198 AudioEntry* LookupById(int stream_id); | 199 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 | 200 |
204 // Check if the renderer process has access to the requested output device. | 201 // Check if the renderer process has access to the requested output device. |
205 void CheckOutputDeviceAccess(int render_frame_id, | 202 void CheckOutputDeviceAccess(int render_frame_id, |
206 const std::string& device_id, | 203 const std::string& device_id, |
207 const url::Origin& security_origin, | 204 const url::Origin& security_origin, |
208 int stream_id, | 205 int stream_id, |
209 base::TimeTicks auth_start_time); | 206 base::TimeTicks auth_start_time); |
210 | 207 |
211 // Proceed with device authorization after checking permissions. | 208 // Proceed with device authorization after checking permissions. |
212 void AccessChecked(std::unique_ptr<MediaStreamUIProxy> ui_proxy, | 209 void AccessChecked(std::unique_ptr<MediaStreamUIProxy> ui_proxy, |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 // ID of the RenderProcessHost that owns this instance. | 246 // ID of the RenderProcessHost that owns this instance. |
250 const int render_process_id_; | 247 const int render_process_id_; |
251 | 248 |
252 media::AudioManager* const audio_manager_; | 249 media::AudioManager* const audio_manager_; |
253 AudioMirroringManager* const mirroring_manager_; | 250 AudioMirroringManager* const mirroring_manager_; |
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 |