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 22 matching lines...) Expand all Loading... |
33 // v v | 33 // v v |
34 // If there is an error at any point, a NotifyStreamError will | 34 // If there is an error at any point, a NotifyStreamError will |
35 // be sent. Otherwise, the renderer can assume that the actual state | 35 // be sent. Otherwise, the renderer can assume that the actual state |
36 // of the output stream is consistent with the control signals it sends. | 36 // of the output stream is consistent with the control signals it sends. |
37 | 37 |
38 // A SyncSocket pair is used to signal buffer readiness between processes. | 38 // A SyncSocket pair is used to signal buffer readiness between processes. |
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> | |
44 | |
45 #include <map> | 43 #include <map> |
46 #include <memory> | 44 #include <memory> |
47 #include <string> | 45 #include <string> |
48 #include <utility> | 46 #include <utility> |
| 47 #include <vector> |
49 | 48 |
50 #include "base/atomic_ref_count.h" | |
51 #include "base/gtest_prod_util.h" | |
52 #include "base/logging.h" | |
53 #include "base/macros.h" | |
54 #include "base/memory/ref_counted.h" | |
55 #include "base/process/process.h" | |
56 #include "base/sequenced_task_runner_helpers.h" | |
57 #include "content/browser/renderer_host/media/audio_output_authorization_handler
.h" | 49 #include "content/browser/renderer_host/media/audio_output_authorization_handler
.h" |
58 #include "content/browser/renderer_host/media/media_devices_manager.h" | 50 #include "content/browser/renderer_host/media/audio_output_delegate.h" |
59 #include "content/common/content_export.h" | 51 #include "content/common/content_export.h" |
60 #include "content/public/browser/browser_message_filter.h" | 52 #include "content/public/browser/browser_message_filter.h" |
61 #include "content/public/browser/browser_thread.h" | |
62 #include "content/public/browser/render_process_host.h" | 53 #include "content/public/browser/render_process_host.h" |
63 #include "content/public/browser/resource_context.h" | 54 |
64 #include "media/audio/audio_io.h" | 55 namespace base { |
65 #include "media/audio/audio_logging.h" | 56 class SharedMemory; |
66 #include "media/audio/audio_output_controller.h" | 57 class CancelableSyncSocket; |
67 #include "media/audio/simple_sources.h" | 58 } |
68 #include "media/base/output_device_info.h" | |
69 #include "url/origin.h" | |
70 | 59 |
71 namespace media { | 60 namespace media { |
72 class AudioManager; | 61 class AudioManager; |
73 class AudioParameters; | 62 class AudioParameters; |
74 } | 63 } |
75 | 64 |
76 namespace content { | 65 namespace content { |
77 | 66 |
78 class AudioMirroringManager; | 67 class AudioMirroringManager; |
79 class MediaInternals; | |
80 class MediaStreamManager; | 68 class MediaStreamManager; |
81 | 69 |
82 class CONTENT_EXPORT AudioRendererHost : public BrowserMessageFilter { | 70 class CONTENT_EXPORT AudioRendererHost |
| 71 : public BrowserMessageFilter, |
| 72 public AudioOutputDelegate::EventHandler { |
83 public: | 73 public: |
84 // Called from UI thread from the owner of this object. | 74 // Called from UI thread from the owner of this object. |
85 AudioRendererHost(int render_process_id, | 75 AudioRendererHost(int render_process_id, |
86 media::AudioManager* audio_manager, | 76 media::AudioManager* audio_manager, |
87 AudioMirroringManager* mirroring_manager, | 77 AudioMirroringManager* mirroring_manager, |
88 MediaInternals* media_internals, | |
89 MediaStreamManager* media_stream_manager, | 78 MediaStreamManager* media_stream_manager, |
90 const std::string& salt); | 79 const std::string& salt); |
91 | 80 |
92 // Calls |callback| with the list of AudioOutputControllers for this object. | 81 // Calls |callback| with the list of AudioOutputControllers for this object. |
93 void GetOutputControllers( | 82 void GetOutputControllers( |
94 const RenderProcessHost::GetAudioOutputControllersCallback& | 83 const RenderProcessHost::GetAudioOutputControllersCallback& |
95 callback) const; | 84 callback) const; |
96 | 85 |
97 // BrowserMessageFilter implementation. | 86 // BrowserMessageFilter implementation. |
98 void OnChannelClosing() override; | 87 void OnChannelClosing() override; |
99 void OnDestruct() const override; | 88 void OnDestruct() const override; |
100 bool OnMessageReceived(const IPC::Message& message) override; | 89 bool OnMessageReceived(const IPC::Message& message) override; |
101 | 90 |
| 91 // AudioOutputDelegate::EventHandler implementation |
| 92 void OnStreamCreated(int stream_id, |
| 93 base::SharedMemory* shared_memory, |
| 94 base::CancelableSyncSocket* foreign_socket) override; |
| 95 void OnStreamError(int stream_id) override; |
| 96 void OnStreamStateChanged(bool is_playing) override; |
| 97 |
102 // Returns true if any streams managed by this host are actively playing. Can | 98 // Returns true if any streams managed by this host are actively playing. Can |
103 // be called from any thread. | 99 // be called from any thread. |
104 bool HasActiveAudio(); | 100 bool HasActiveAudio(); |
105 | 101 |
106 void OverrideDevicePermissionsForTesting(bool has_access); | 102 void OverrideDevicePermissionsForTesting(bool has_access); |
107 | 103 |
108 private: | 104 private: |
109 friend class AudioRendererHostTest; | 105 friend class AudioRendererHostTest; |
110 friend class BrowserThread; | 106 friend class BrowserThread; |
111 friend class base::DeleteHelper<AudioRendererHost>; | 107 friend class base::DeleteHelper<AudioRendererHost>; |
112 friend class MockAudioRendererHost; | 108 friend class MockAudioRendererHost; |
113 friend class TestAudioRendererHost; | 109 friend class TestAudioRendererHost; |
114 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, CreateMockStream); | 110 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, CreateMockStream); |
115 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, MockStreamDataConversation); | 111 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, MockStreamDataConversation); |
116 | 112 |
117 class AudioEntry; | |
118 typedef std::map<int, AudioEntry*> AudioEntryMap; | |
119 | |
120 // Internal callback type for access requests to output devices. | 113 // Internal callback type for access requests to output devices. |
121 // |have_access| is true only if there is permission to access the device. | 114 // |have_access| is true only if there is permission to access the device. |
122 typedef base::Callback<void(bool have_access)> OutputDeviceAccessCB; | 115 typedef base::Callback<void(bool have_access)> OutputDeviceAccessCB; |
123 | 116 |
| 117 using AudioOutputDelegateList = std::vector<AudioOutputDelegate::ScopedPtr>; |
| 118 |
124 // The type of a function that is run on the UI thread to check whether the | 119 // 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 | 120 // routing IDs reference a valid RenderFrameHost. The function then runs |
126 // |callback| on the IO thread with true/false if valid/invalid. | 121 // |callback| on the IO thread with true/false if valid/invalid. |
127 using ValidateRenderFrameIdFunction = | 122 using ValidateRenderFrameIdFunction = |
128 void (*)(int render_process_id, | 123 void (*)(int render_process_id, |
129 int render_frame_id, | 124 int render_frame_id, |
130 const base::Callback<void(bool)>& callback); | 125 const base::Callback<void(bool)>& callback); |
131 | 126 |
132 ~AudioRendererHost() override; | 127 ~AudioRendererHost() override; |
133 | 128 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 void OnPauseStream(int stream_id); | 166 void OnPauseStream(int stream_id); |
172 | 167 |
173 // Close the audio stream referenced by |stream_id|. | 168 // Close the audio stream referenced by |stream_id|. |
174 void OnCloseStream(int stream_id); | 169 void OnCloseStream(int stream_id); |
175 | 170 |
176 // Set the volume of the audio stream referenced by |stream_id|. | 171 // Set the volume of the audio stream referenced by |stream_id|. |
177 void OnSetVolume(int stream_id, double volume); | 172 void OnSetVolume(int stream_id, double volume); |
178 | 173 |
179 // Helper methods. | 174 // Helper methods. |
180 | 175 |
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 | 176 // Called after the |render_frame_id| provided to OnCreateStream() was |
187 // validated. When |is_valid| is false, this calls ReportErrorAndClose(). | 177 // validated. When |is_valid| is false, this calls OnStreamError(). |
188 void DidValidateRenderFrame(int stream_id, bool is_valid); | 178 void DidValidateRenderFrame(int stream_id, bool is_valid); |
189 | 179 |
190 // Updates status of stream for AudioStreamMonitor and updates | 180 // Updates status of stream for AudioStreamMonitor and updates |
191 // the number of playing streams. | 181 // the number of playing streams. |
192 void StreamStateChanged(int stream_id, bool is_playing); | 182 void StreamStateChanged(int stream_id, bool is_playing); |
193 | 183 |
194 RenderProcessHost::AudioOutputControllerList DoGetOutputControllers() const; | 184 RenderProcessHost::AudioOutputControllerList DoGetOutputControllers() const; |
195 | 185 |
196 // Send an error message to the renderer. | 186 // Send an error message to the renderer. |
197 void SendErrorMessage(int stream_id); | 187 void SendErrorMessage(int stream_id); |
198 | 188 |
199 // Delete an audio entry, notifying observers first. This is called by | 189 // Helper methods to look up a AudioOutputDelegate identified by |stream_id|. |
200 // AudioOutputController after it has closed. | 190 // Returns delegates_.end() if not found. |
201 void DeleteEntry(std::unique_ptr<AudioEntry> entry); | 191 AudioOutputDelegateList::iterator LookupIteratorById(int stream_id); |
202 | 192 // Returns nullptr if not found. |
203 // Send an error message to the renderer, then close the stream. | 193 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 | 194 |
214 // Helper method to check if the authorization procedure for stream | 195 // Helper method to check if the authorization procedure for stream |
215 // |stream_id| has started. | 196 // |stream_id| has started. |
216 bool IsAuthorizationStarted(int stream_id); | 197 bool IsAuthorizationStarted(int stream_id); |
217 | 198 |
218 // Called from AudioRendererHostTest to override the function that checks for | 199 // Called from AudioRendererHostTest to override the function that checks for |
219 // the existence of the RenderFrameHost at stream creation time. | 200 // the existence of the RenderFrameHost at stream creation time. |
220 void set_render_frame_id_validate_function_for_testing( | 201 void set_render_frame_id_validate_function_for_testing( |
221 ValidateRenderFrameIdFunction function) { | 202 ValidateRenderFrameIdFunction function) { |
222 validate_render_frame_id_function_ = function; | 203 validate_render_frame_id_function_ = function; |
223 } | 204 } |
224 | 205 |
225 // ID of the RenderProcessHost that owns this instance. | 206 // ID of the RenderProcessHost that owns this instance. |
226 const int render_process_id_; | 207 const int render_process_id_; |
227 | 208 |
228 media::AudioManager* const audio_manager_; | 209 media::AudioManager* const audio_manager_; |
229 AudioMirroringManager* const mirroring_manager_; | 210 AudioMirroringManager* const mirroring_manager_; |
230 std::unique_ptr<media::AudioLog> audio_log_; | |
231 | 211 |
232 // A map of stream IDs to audio sources. | 212 // Used to access to AudioInputDeviceManager. |
233 AudioEntryMap audio_entries_; | 213 MediaStreamManager* media_stream_manager_; |
| 214 |
| 215 // A list of the current open streams. |
| 216 AudioOutputDelegateList delegates_; |
234 | 217 |
235 // The number of streams in the playing state. Atomic read safe from any | 218 // The number of streams in the playing state. Atomic read safe from any |
236 // thread, but should only be updated from the IO thread. | 219 // thread, but should only be updated from the IO thread. |
237 base::AtomicRefCount num_playing_streams_; | 220 base::AtomicRefCount num_playing_streams_; |
238 | 221 |
239 // Salt required to translate renderer device IDs to raw device unique IDs | 222 // Salt required to translate renderer device IDs to raw device unique IDs |
240 std::string salt_; | 223 std::string salt_; |
241 | 224 |
242 // Map of device authorizations for streams that are not yet created | 225 // 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 | 226 // 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_; | 238 size_t max_simultaneous_streams_; |
256 | 239 |
257 AudioOutputAuthorizationHandler authorization_handler_; | 240 AudioOutputAuthorizationHandler authorization_handler_; |
258 | 241 |
259 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); | 242 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); |
260 }; | 243 }; |
261 | 244 |
262 } // namespace content | 245 } // namespace content |
263 | 246 |
264 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ | 247 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ |
OLD | NEW |