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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
57 #include "content/browser/renderer_host/media/audio_output_device_enumerator.h" | 57 #include "content/browser/renderer_host/media/audio_output_device_enumerator.h" |
58 #include "content/common/content_export.h" | 58 #include "content/common/content_export.h" |
59 #include "content/public/browser/browser_message_filter.h" | 59 #include "content/public/browser/browser_message_filter.h" |
60 #include "content/public/browser/browser_thread.h" | 60 #include "content/public/browser/browser_thread.h" |
61 #include "content/public/browser/render_process_host.h" | 61 #include "content/public/browser/render_process_host.h" |
62 #include "content/public/browser/resource_context.h" | 62 #include "content/public/browser/resource_context.h" |
63 #include "media/audio/audio_io.h" | 63 #include "media/audio/audio_io.h" |
64 #include "media/audio/audio_logging.h" | 64 #include "media/audio/audio_logging.h" |
65 #include "media/audio/audio_output_controller.h" | 65 #include "media/audio/audio_output_controller.h" |
66 #include "media/audio/simple_sources.h" | 66 #include "media/audio/simple_sources.h" |
67 #include "media/base/output_device_info.h" | |
68 #include "media/mojo/interfaces/audio_output.mojom.h" | |
69 #include "mojo/public/cpp/bindings/binding.h" | |
70 #include "mojo/public/cpp/bindings/interface_request.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 base::RefCountedThreadSafe<AudioRendererHost, | |
88 BrowserThread::DeleteOnIOThread>, | |
89 NON_EXPORTED_BASE(private media::mojom::AudioOutput) { | |
83 public: | 90 public: |
84 // Called from UI thread from the owner of this object. | 91 // Called from UI thread from the owner of this object. |
85 AudioRendererHost(int render_process_id, | 92 AudioRendererHost(int render_process_id, |
86 media::AudioManager* audio_manager, | 93 media::AudioManager* audio_manager, |
87 AudioMirroringManager* mirroring_manager, | 94 AudioMirroringManager* mirroring_manager, |
88 MediaInternals* media_internals, | 95 MediaInternals* media_internals, |
89 MediaStreamManager* media_stream_manager, | 96 MediaStreamManager* media_stream_manager, |
90 const std::string& salt); | 97 const std::string& salt); |
91 | 98 |
92 // Calls |callback| with the list of AudioOutputControllers for this object. | 99 // Calls |callback| with the list of AudioOutputControllers for this object. |
93 void GetOutputControllers( | 100 void GetOutputControllers( |
94 const RenderProcessHost::GetAudioOutputControllersCallback& | 101 const RenderProcessHost::GetAudioOutputControllersCallback& |
95 callback) const; | 102 callback) const; |
96 | 103 |
97 // BrowserMessageFilter implementation. | 104 // Returns true if any streams managed by this host are actively playing. Can |
98 void OnChannelClosing() override; | |
99 void OnDestruct() const override; | |
100 bool OnMessageReceived(const IPC::Message& message) override; | |
101 | |
102 // Returns true if any streams managed by this host are actively playing. Can | |
103 // be called from any thread. | 105 // be called from any thread. |
104 bool HasActiveAudio(); | 106 bool HasActiveAudio(); |
105 | 107 |
108 void BindRequest(media::mojom::AudioOutputRequest request); | |
109 | |
106 private: | 110 private: |
107 friend class AudioRendererHostTest; | 111 friend class AudioRendererHostTest; |
108 friend class BrowserThread; | 112 friend class BrowserThread; |
109 friend class base::DeleteHelper<AudioRendererHost>; | 113 friend class base::DeleteHelper<AudioRendererHost>; |
110 friend class MockAudioRendererHost; | 114 friend class MockAudioRendererHost; |
111 friend class TestAudioRendererHost; | 115 friend class TestAudioRendererHost; |
116 friend class base::RefCounted<AudioRendererHost>; | |
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; | 120 class AudioEntry; |
116 typedef std::map<int, AudioEntry*> AudioEntryMap; | 121 typedef std::map<int, AudioEntry*> AudioEntryMap; |
117 | 122 |
118 // Internal callback type for access requests to output devices. | 123 // Internal callback type for access requests to output devices. |
119 // |have_access| is true only if there is permission to access the device. | 124 // |have_access| is true only if there is permission to access the device. |
120 typedef base::Callback<void(bool have_access)> OutputDeviceAccessCB; | 125 typedef base::Callback<void(bool have_access)> OutputDeviceAccessCB; |
121 | 126 |
122 // Internal callback type for information requests about an output device. | 127 // Internal callback type for information requests about an output device. |
123 // |success| indicates the operation was successful. If true, |device_info| | 128 // |success| indicates the operation was successful. If true, |device_info| |
124 // contains data about the device. | 129 // contains data about the device. |
125 typedef base::Callback<void(bool success, | 130 typedef base::Callback<void(bool success, |
126 const AudioOutputDeviceInfo& device_info)> | 131 const AudioOutputDeviceInfo& device_info)> |
127 OutputDeviceInfoCB; | 132 OutputDeviceInfoCB; |
128 | 133 |
129 // The type of a function that is run on the UI thread to check whether the | 134 // The type of a function that is run on the UI thread to check whether the |
130 // routing IDs reference a valid RenderFrameHost. The function then runs | 135 // routing IDs reference a valid RenderFrameHost. The function then runs |
131 // |callback| on the IO thread with true/false if valid/invalid. | 136 // |callback| on the IO thread with true/false if valid/invalid. |
132 using ValidateRenderFrameIdFunction = | 137 using ValidateRenderFrameIdFunction = |
133 void (*)(int render_process_id, | 138 void (*)(int render_process_id, |
134 int render_frame_id, | 139 int render_frame_id, |
135 const base::Callback<void(bool)>& callback); | 140 const base::Callback<void(bool)>& callback); |
136 | 141 |
137 ~AudioRendererHost() override; | 142 ~AudioRendererHost() override; |
138 | 143 |
139 // Methods called on IO thread ---------------------------------------------- | |
140 | |
141 // Audio related IPC message handlers. | |
142 | |
143 // Request permission to use an output device for use by a stream produced | 144 // Request permission to use an output device for use by a stream produced |
144 // in the RenderFrame referenced by |render_frame_id|. | 145 // in the RenderFrame referenced by |render_frame_id|. |
145 // |session_id| is used for unified IO to find out which input device to be | 146 // |session_id| is used for unified IO to find out which input device to be |
146 // opened for the stream. For clients that do not use unified IO, | 147 // opened for the stream. For clients that do not use unified IO, |
147 // |session_id| will be ignored and the given |device_id| and | 148 // |session_id| will be ignored and the given |device_id| and |
148 // |security_origin| will be used to select the output device. | 149 // |security_origin| will be used to select the output device. |
149 // Upon completion of the process, the peer is notified with the device output | 150 // Upon completion of the process, the peer is notified with the device output |
150 // parameters via the NotifyDeviceAuthorized message. | 151 // parameters via the NotifyDeviceAuthorized message. |
151 void OnRequestDeviceAuthorization(int stream_id, | 152 void RequestDeviceAuthorization( |
152 int render_frame_id, | 153 int stream_id, |
153 int session_id, | 154 int render_frame_id, |
154 const std::string& device_id, | 155 int session_id, |
155 const url::Origin& security_origin); | 156 const mojo::String& device_id, |
157 const url::Origin& origin, | |
158 const RequestDeviceAuthorizationCallback& callback) override; | |
159 void CreateStream(int stream_id, | |
160 int render_frame_id, | |
161 media::mojom::AudioOutputStreamClientPtr client, | |
162 const media::AudioParameters& params, | |
163 const CreateStreamCallback& callback) override; | |
164 void CloseStream(int stream_id) override; | |
156 | 165 |
166 void SendAuthorizationMessage(RequestDeviceAuthorizationCallback callback, | |
o1ka
2016/09/02 07:31:46
pass it as "const &" everywhere?
| |
167 int stream_id, | |
168 media::OutputDeviceStatus status, | |
169 const media::AudioParameters& params, | |
170 const std::string& matched_device_id); | |
171 | |
172 // Methods called on IO thread ---------------------------------------------- | |
173 | |
174 // Audio related IPC message handlers. | |
157 // Creates an audio output stream with the specified format. | 175 // Creates an audio output stream with the specified format. |
158 // Upon success/failure, the peer is notified via the NotifyStreamCreated | 176 // Upon success/failure, the peer is notified via the NotifyStreamCreated |
159 // message. | 177 // message. |
160 void OnCreateStream(int stream_id, | 178 void OnCreateStream(int stream_id, |
161 int render_frame_id, | 179 int render_frame_id, |
162 const media::AudioParameters& params); | 180 const media::AudioParameters& params); |
163 | 181 |
164 // Play the audio stream referenced by |stream_id|. | 182 // Play the audio stream referenced by |stream_id|. |
165 void OnPlayStream(int stream_id); | 183 void OnPlayStream(int stream_id); |
166 | 184 |
167 // Pause the audio stream referenced by |stream_id|. | 185 // Pause the audio stream referenced by |stream_id|. |
168 void OnPauseStream(int stream_id); | 186 void OnPauseStream(int stream_id); |
169 | 187 |
170 // Close the audio stream referenced by |stream_id|. | 188 // Close the audio stream referenced by |stream_id|. |
171 void OnCloseStream(int stream_id); | 189 void OnCloseStream(int stream_id); |
172 | 190 |
173 // Set the volume of the audio stream referenced by |stream_id|. | 191 // Set the volume of the audio stream referenced by |stream_id|. |
174 void OnSetVolume(int stream_id, double volume); | 192 void OnSetVolume(int stream_id, double volume); |
175 | 193 |
176 // Helper methods. | |
177 | |
178 // Proceed with device authorization after checking permissions. | 194 // Proceed with device authorization after checking permissions. |
179 void OnDeviceAuthorized(int stream_id, | 195 void OnDeviceAuthorized(int stream_id, |
196 RequestDeviceAuthorizationCallback callback, | |
o1ka
2016/09/02 07:31:46
const &?
| |
180 const std::string& device_id, | 197 const std::string& device_id, |
181 const url::Origin& security_origin, | 198 const url::Origin& security_origin, |
182 base::TimeTicks auth_start_time, | 199 base::TimeTicks auth_start_time, |
183 bool have_access); | 200 bool have_access); |
184 | 201 |
185 // Proceed with device authorization after translating device ID. | 202 // Proceed with device authorization after translating device ID. |
186 void OnDeviceIDTranslated(int stream_id, | 203 void OnDeviceIDTranslated(int stream_id, |
187 base::TimeTicks auth_start_time, | 204 base::TimeTicks auth_start_time, |
205 RequestDeviceAuthorizationCallback callback, | |
188 bool device_found, | 206 bool device_found, |
189 const AudioOutputDeviceInfo& device_info); | 207 const AudioOutputDeviceInfo& device_info); |
190 | 208 |
191 // Start the actual creation of an audio stream, after the device | 209 // Start the actual creation of an audio stream, after the device |
192 // authorization process is complete. | 210 // authorization process is complete. |
193 void DoCreateStream(int stream_id, | 211 void DoCreateStream(int stream_id, |
194 int render_frame_id, | 212 int render_frame_id, |
195 const media::AudioParameters& params, | 213 const media::AudioParameters& params, |
196 const std::string& device_unique_id, | 214 const std::string& device_unique_id, |
215 media::mojom::AudioOutputStreamClientPtr client, | |
216 CreateStreamCallback callback, | |
197 bool render_frame_id_is_valid); | 217 bool render_frame_id_is_valid); |
198 | 218 |
199 // Complete the process of creating an audio stream. This will set up the | 219 // Complete the process of creating an audio stream. This will set up the |
200 // shared memory or shared socket in low latency mode and send the | 220 // shared memory or shared socket in low latency mode and send the |
201 // NotifyStreamCreated message to the peer. | 221 // NotifyStreamCreated message to the peer. |
202 void DoCompleteCreation(int stream_id); | 222 void DoCompleteCreation(int stream_id, CreateStreamCallback callback); |
203 | 223 |
204 // Send playing/paused status to the renderer. | 224 // Send playing/paused status to the renderer. |
205 void DoNotifyStreamStateChanged(int stream_id, bool is_playing); | 225 void DoNotifyStreamStateChanged(int stream_id, bool is_playing); |
206 | 226 |
207 RenderProcessHost::AudioOutputControllerList DoGetOutputControllers() const; | 227 RenderProcessHost::AudioOutputControllerList DoGetOutputControllers() const; |
208 | 228 |
209 // Send an error message to the renderer. | 229 // Send an error message to the renderer. |
210 void SendErrorMessage(int stream_id); | 230 void SendErrorMessage(int stream_id); |
211 | 231 |
212 // Delete an audio entry, notifying observers first. This is called by | 232 // Delete an audio entry, notifying observers first. This is called by |
213 // AudioOutputController after it has closed. | 233 // AudioOutputController after it has closed. |
214 void DeleteEntry(std::unique_ptr<AudioEntry> entry); | 234 void DeleteEntry(std::unique_ptr<AudioEntry> entry); |
215 | 235 |
216 // Send an error message to the renderer, then close the stream. | 236 // Send an error message to the renderer, then close the stream. |
217 void ReportErrorAndClose(int stream_id); | 237 void ReportErrorAndClose(int stream_id); |
218 | 238 |
219 // A helper method to look up a AudioEntry identified by |stream_id|. | 239 // A helper method to look up a AudioEntry identified by |stream_id|. |
220 // Returns NULL if not found. | 240 // Returns NULL if not found. |
221 AudioEntry* LookupById(int stream_id); | 241 AudioEntry* LookupById(int stream_id); |
222 | 242 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
284 // When DCHECKs are turned on, AudioRendererHost will call this function on | 304 // When DCHECKs are turned on, AudioRendererHost will call this function on |
285 // the UI thread to validate render frame IDs. A default is set by the | 305 // the UI thread to validate render frame IDs. A default is set by the |
286 // constructor, but this can be overridden by unit tests. | 306 // constructor, but this can be overridden by unit tests. |
287 ValidateRenderFrameIdFunction validate_render_frame_id_function_; | 307 ValidateRenderFrameIdFunction validate_render_frame_id_function_; |
288 #endif // DCHECK_IS_ON() | 308 #endif // DCHECK_IS_ON() |
289 | 309 |
290 // The maximum number of simultaneous streams during the lifetime of this | 310 // The maximum number of simultaneous streams during the lifetime of this |
291 // host. Reported as UMA stat at shutdown. | 311 // host. Reported as UMA stat at shutdown. |
292 size_t max_simultaneous_streams_; | 312 size_t max_simultaneous_streams_; |
293 | 313 |
314 // Binds AudioRendererHost to a media::mojom::AudioOutputRequest. | |
315 std::unique_ptr<mojo::Binding<media::mojom::AudioOutput>> binding_; | |
316 | |
294 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); | 317 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); |
295 }; | 318 }; |
296 | 319 |
297 } // namespace content | 320 } // namespace content |
298 | 321 |
299 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ | 322 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ |
OLD | NEW |