Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(17)

Side by Side Diff: content/browser/renderer_host/media/audio_renderer_host.h

Issue 2443573003: Factor out AudioOutputDelegate from AudioRendererHost. (Closed)
Patch Set: Add AudioOutputDelegate::Deleter. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 = std::vector<AudioOutputDelegate::UniquePtr>;
125
122 // 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
123 // routing IDs reference a valid RenderFrameHost. The function then runs 127 // routing IDs reference a valid RenderFrameHost. The function then runs
124 // |callback| on the IO thread with true/false if valid/invalid. 128 // |callback| on the IO thread with true/false if valid/invalid.
125 using ValidateRenderFrameIdFunction = 129 using ValidateRenderFrameIdFunction =
126 void (*)(int render_process_id, 130 void (*)(int render_process_id,
127 int render_frame_id, 131 int render_frame_id,
128 const base::Callback<void(bool)>& callback); 132 const base::Callback<void(bool)>& callback);
129 133
130 ~AudioRendererHost() override; 134 ~AudioRendererHost() override;
131 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
132 // Methods called on IO thread ---------------------------------------------- 143 // Methods called on IO thread ----------------------------------------------
133 144
134 // Audio related IPC message handlers. 145 // Audio related IPC message handlers.
135 146
136 // 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
137 // in the RenderFrame referenced by |render_frame_id|. 148 // in the RenderFrame referenced by |render_frame_id|.
138 // |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
139 // 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,
140 // |session_id| will be ignored and the given |device_id| and 151 // |session_id| will be ignored and the given |device_id| and
141 // |security_origin| will be used to select the output device. 152 // |security_origin| will be used to select the output device.
(...skipping 19 matching lines...) Expand all
161 void OnPauseStream(int stream_id); 172 void OnPauseStream(int stream_id);
162 173
163 // Close the audio stream referenced by |stream_id|. 174 // Close the audio stream referenced by |stream_id|.
164 void OnCloseStream(int stream_id); 175 void OnCloseStream(int stream_id);
165 176
166 // Set the volume of the audio stream referenced by |stream_id|. 177 // Set the volume of the audio stream referenced by |stream_id|.
167 void OnSetVolume(int stream_id, double volume); 178 void OnSetVolume(int stream_id, double volume);
168 179
169 // Helper methods. 180 // Helper methods.
170 181
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 182 // Called after the |render_frame_id| provided to OnCreateStream() was
177 // validated. When |is_valid| is false, this calls ReportErrorAndClose(). 183 // validated. When |is_valid| is false, this calls OnStreamError().
178 void DidValidateRenderFrame(int stream_id, bool is_valid); 184 void DidValidateRenderFrame(int stream_id, bool is_valid);
179 185
180 // Updates status of stream for AudioStreamMonitor and updates 186 // Updates status of stream for AudioStreamMonitor and updates
181 // the number of playing streams. 187 // the number of playing streams.
182 void StreamStateChanged(int stream_id, bool is_playing); 188 void StreamStateChanged(int stream_id, bool is_playing);
183 189
184 RenderProcessHost::AudioOutputControllerList DoGetOutputControllers() const; 190 RenderProcessHost::AudioOutputControllerList DoGetOutputControllers() const;
185 191
186 // Send an error message to the renderer. 192 // Send an error message to the renderer.
187 void SendErrorMessage(int stream_id); 193 void SendErrorMessage(int stream_id);
188 194
189 // Delete an audio entry, notifying observers first. This is called by 195 // Helper methods to look up a AudioOutputDelegate identified by |stream_id|.
190 // AudioOutputController after it has closed. 196 // Returns delegates_.end() if not found.
191 void DeleteEntry(std::unique_ptr<AudioEntry> entry); 197 AudioOutputDelegateList::iterator LookupIteratorById(int stream_id);
192 198 // Returns nullptr if not found.
193 // Send an error message to the renderer, then close the stream. 199 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 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
244 void set_render_frame_id_validate_function_for_testing( 241 void set_render_frame_id_validate_function_for_testing(
245 ValidateRenderFrameIdFunction function) { 242 ValidateRenderFrameIdFunction function) {
246 validate_render_frame_id_function_ = function; 243 validate_render_frame_id_function_ = function;
247 } 244 }
248 245
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_;
251 media::AudioLogFactory* log_factory_;
254 std::unique_ptr<media::AudioLog> audio_log_; 252 std::unique_ptr<media::AudioLog> audio_log_;
255 253
256 // Used to access to AudioInputDeviceManager. 254 // Used to access to AudioInputDeviceManager.
257 MediaStreamManager* media_stream_manager_; 255 MediaStreamManager* media_stream_manager_;
258 256
259 // A map of stream IDs to audio sources. 257 // A list of the current open streams.
260 AudioEntryMap audio_entries_; 258 AudioOutputDelegateList delegates_;
261 259
262 // The number of streams in the playing state. Atomic read safe from any 260 // The number of streams in the playing state. Atomic read safe from any
263 // thread, but should only be updated from the IO thread. 261 // thread, but should only be updated from the IO thread.
264 base::AtomicRefCount num_playing_streams_; 262 base::AtomicRefCount num_playing_streams_;
265 263
266 // Salt required to translate renderer device IDs to raw device unique IDs 264 // Salt required to translate renderer device IDs to raw device unique IDs
267 std::string salt_; 265 std::string salt_;
268 266
269 // Map of device authorizations for streams that are not yet created 267 // 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 268 // 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. 269 // is a bool that is true if the authorization process completes successfully.
272 // The second element contains the unique ID of the authorized device. 270 // The second element contains the unique ID of the authorized device.
273 std::map<int, std::pair<bool, std::string>> authorizations_; 271 std::map<int, std::pair<bool, std::string>> authorizations_;
274 272
275 // At stream creation time, AudioRendererHost will call this function on the 273 // 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 274 // UI thread to validate render frame IDs. A default is set by the
277 // constructor, but this can be overridden by unit tests. 275 // constructor, but this can be overridden by unit tests.
278 ValidateRenderFrameIdFunction validate_render_frame_id_function_; 276 ValidateRenderFrameIdFunction validate_render_frame_id_function_;
279 277
280 // The maximum number of simultaneous streams during the lifetime of this 278 // The maximum number of simultaneous streams during the lifetime of this
281 // host. Reported as UMA stat at shutdown. 279 // host. Reported as UMA stat at shutdown.
282 size_t max_simultaneous_streams_; 280 size_t max_simultaneous_streams_;
283 281
284 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); 282 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost);
285 }; 283 };
286 284
287 } // namespace content 285 } // namespace content
288 286
289 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ 287 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698