Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / renderer_host / media / audio_renderer_host.h
blob3a265d42d6a87b5dd1bf17438ea9ff528133cdc6
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
3 // found in the LICENSE file.
4 //
5 // AudioRendererHost serves audio related requests from AudioRenderer which
6 // lives inside the render process and provide access to audio hardware.
7 //
8 // This class is owned by BrowserRenderProcessHost, and instantiated on UI
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
11 // singleton and created in IO thread, audio output streams are also created in
12 // the IO thread, so we need to destroy them also in IO thread. After this class
13 // is created, a task of OnInitialized() is posted on IO thread in which
14 // singleton of AudioManager is created.
16 // Here's an example of a typical IPC dialog for audio:
18 // Renderer AudioRendererHost
19 // | |
20 // | RequestDeviceAuthorization > |
21 // | < NotifyDeviceAuthorized |
22 // | |
23 // | CreateStream > |
24 // | < NotifyStreamCreated |
25 // | |
26 // | PlayStream > |
27 // | < NotifyStreamStateChanged | kAudioStreamPlaying
28 // | |
29 // | PauseStream > |
30 // | < NotifyStreamStateChanged | kAudioStreamPaused
31 // | |
32 // | PlayStream > |
33 // | < NotifyStreamStateChanged | kAudioStreamPlaying
34 // | ... |
35 // | CloseStream > |
36 // v v
38 // A SyncSocket pair is used to signal buffer readiness between processes.
40 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_
41 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_
43 #include <map>
44 #include <string>
45 #include <utility>
47 #include "base/atomic_ref_count.h"
48 #include "base/gtest_prod_util.h"
49 #include "base/memory/ref_counted.h"
50 #include "base/memory/scoped_ptr.h"
51 #include "base/process/process.h"
52 #include "base/sequenced_task_runner_helpers.h"
53 #include "content/browser/renderer_host/media/audio_output_device_enumerator.h"
54 #include "content/common/content_export.h"
55 #include "content/public/browser/browser_message_filter.h"
56 #include "content/public/browser/browser_thread.h"
57 #include "content/public/browser/render_process_host.h"
58 #include "content/public/browser/resource_context.h"
59 #include "media/audio/audio_io.h"
60 #include "media/audio/audio_logging.h"
61 #include "media/audio/audio_output_controller.h"
62 #include "media/audio/simple_sources.h"
63 #include "url/origin.h"
65 namespace media {
66 class AudioManager;
67 class AudioParameters;
70 namespace content {
72 class AudioMirroringManager;
73 class MediaInternals;
74 class MediaStreamManager;
75 class MediaStreamUIProxy;
76 class ResourceContext;
78 class CONTENT_EXPORT AudioRendererHost : public BrowserMessageFilter {
79 public:
80 // Called from UI thread from the owner of this object.
81 AudioRendererHost(int render_process_id,
82 media::AudioManager* audio_manager,
83 AudioMirroringManager* mirroring_manager,
84 MediaInternals* media_internals,
85 MediaStreamManager* media_stream_manager,
86 const ResourceContext::SaltCallback& salt_callback);
88 // Calls |callback| with the list of AudioOutputControllers for this object.
89 void GetOutputControllers(
90 const RenderProcessHost::GetAudioOutputControllersCallback&
91 callback) const;
93 // BrowserMessageFilter implementation.
94 void OnChannelClosing() override;
95 void OnDestruct() const override;
96 bool OnMessageReceived(const IPC::Message& message) override;
98 // Returns true if any streams managed by this host are actively playing. Can
99 // be called from any thread.
100 bool HasActiveAudio();
102 // Returns true if any streams managed by the RenderFrame identified by
103 // |render_frame_id| are actively playing. Can be called from any thread.
104 bool RenderFrameHasActiveAudio(int render_frame_id) const;
106 private:
107 friend class AudioRendererHostTest;
108 friend class BrowserThread;
109 friend class base::DeleteHelper<AudioRendererHost>;
110 friend class MockAudioRendererHost;
111 friend class TestAudioRendererHost;
112 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, CreateMockStream);
113 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, MockStreamDataConversation);
115 class AudioEntry;
116 typedef std::map<int, AudioEntry*> AudioEntryMap;
118 // Internal callback type for access requests to output devices.
119 // |have_access| is true only if there is permission to access the device.
120 typedef base::Callback<void(bool have_access)> OutputDeviceAccessCB;
122 // Internal callback type for information requests about an output device.
123 // |success| indicates the operation was successful. If true, |device_info|
124 // contains data about the device.
125 typedef base::Callback<void(bool success,
126 const AudioOutputDeviceInfo& device_info)>
127 OutputDeviceInfoCB;
129 ~AudioRendererHost() override;
131 // Methods called on IO thread ----------------------------------------------
133 // Audio related IPC message handlers.
135 // Request permission to use an output device for use by a stream produced
136 // in the RenderFrame referenced by |render_frame_id|.
137 // |session_id| is used for unified IO to find out which input device to be
138 // opened for the stream. For clients that do not use unified IO,
139 // |session_id| will be ignored and the given |device_id| and
140 // |security_origin| will be used to select the output device.
141 // Upon completion of the process, the peer is notified with the device output
142 // parameters via the NotifyDeviceAuthorized message.
143 void OnRequestDeviceAuthorization(int stream_id,
144 int render_frame_id,
145 int session_id,
146 const std::string& device_id,
147 const url::Origin& gurl_security_origin);
149 // Creates an audio output stream with the specified format.
150 // Upon success/failure, the peer is notified via the NotifyStreamCreated
151 // message.
152 void OnCreateStream(int stream_id,
153 int render_frame_id,
154 const media::AudioParameters& params);
156 // Play the audio stream referenced by |stream_id|.
157 void OnPlayStream(int stream_id);
159 // Pause the audio stream referenced by |stream_id|.
160 void OnPauseStream(int stream_id);
162 // Close the audio stream referenced by |stream_id|.
163 void OnCloseStream(int stream_id);
165 // Set the volume of the audio stream referenced by |stream_id|.
166 void OnSetVolume(int stream_id, double volume);
168 // Set the output device of the audio stream referenced by |stream_id|.
169 void OnSwitchOutputDevice(int stream_id,
170 int render_frame_id,
171 const std::string& device_id,
172 const url::Origin& gurl_security_origin);
174 // Helper methods.
176 // Proceed with device authorization after checking permissions.
177 void OnDeviceAuthorized(int stream_id,
178 const std::string& device_id,
179 const GURL& security_origin,
180 bool have_access);
182 // Proceed with device authorization after translating device ID.
183 void OnDeviceIDTranslated(int stream_id,
184 bool device_found,
185 const AudioOutputDeviceInfo& device_info);
187 // Start the actual creation of an audio stream, after the device
188 // authorization process is complete.
189 void DoCreateStream(int stream_id,
190 int render_frame_id,
191 const media::AudioParameters& params,
192 const std::string& device_unique_id);
194 // Complete the process of creating an audio stream. This will set up the
195 // shared memory or shared socket in low latency mode and send the
196 // NotifyStreamCreated message to the peer.
197 void DoCompleteCreation(int stream_id);
199 // Send playing/paused status to the renderer.
200 void DoNotifyStreamStateChanged(int stream_id, bool is_playing);
202 // Proceed with output device switching after checking permissions.
203 void OnSwitchDeviceAuthorized(int stream_id,
204 const std::string& device_id,
205 const GURL& security_origin,
206 bool have_access);
208 // Proceed with output device switching after translating device ID.
209 void OnSwitchDeviceIDTranslated(int stream_id,
210 bool device_found,
211 const AudioOutputDeviceInfo& device_info);
213 // Finish handling the output device switch request, after the device has
214 // been switched.
215 void OnDeviceSwitched(int stream_id);
217 RenderProcessHost::AudioOutputControllerList DoGetOutputControllers() const;
219 // Send an error message to the renderer.
220 void SendErrorMessage(int stream_id);
222 // Delete an audio entry, notifying observers first. This is called by
223 // AudioOutputController after it has closed.
224 void DeleteEntry(scoped_ptr<AudioEntry> entry);
226 // Send an error message to the renderer, then close the stream.
227 void ReportErrorAndClose(int stream_id);
229 // A helper method to look up a AudioEntry identified by |stream_id|.
230 // Returns NULL if not found.
231 AudioEntry* LookupById(int stream_id);
233 // A helper method to update the number of playing streams and alert the
234 // ResourceScheduler when the renderer starts or stops playing an audiostream.
235 void UpdateNumPlayingStreams(AudioEntry* entry, bool is_playing);
237 // Check if the renderer process has access to the requested output device.
238 void CheckOutputDeviceAccess(int render_frame_id,
239 const std::string& device_id,
240 const GURL& gurl_security_origin,
241 const OutputDeviceAccessCB& callback);
243 // Invoke |callback| after permission to use a device has been checked.
244 void AccessChecked(scoped_ptr<MediaStreamUIProxy> ui_proxy,
245 const OutputDeviceAccessCB& callback,
246 bool have_access);
248 // Translate the hashed |device_id| to a unique device ID.
249 void TranslateDeviceID(const std::string& device_id,
250 const GURL& gurl_security_origin,
251 const OutputDeviceInfoCB& callback);
253 // Finish translation of the hashed |device_id| to a unique device ID.
254 void FinishTranslateDeviceID(
255 const std::string& device_id,
256 const GURL& gurl_security_origin,
257 const OutputDeviceInfoCB& callback,
258 const AudioOutputDeviceEnumeration& device_infos);
260 // Helper method to check if the authorization procedure for stream
261 // |stream_id| has started.
262 bool IsAuthorizationStarted(int stream_id);
264 // ID of the RenderProcessHost that owns this instance.
265 const int render_process_id_;
267 media::AudioManager* const audio_manager_;
268 AudioMirroringManager* const mirroring_manager_;
269 scoped_ptr<media::AudioLog> audio_log_;
271 // Used to access to AudioInputDeviceManager.
272 MediaStreamManager* media_stream_manager_;
274 // A map of stream IDs to audio sources.
275 AudioEntryMap audio_entries_;
277 // The number of streams in the playing state.
278 base::AtomicRefCount num_playing_streams_;
280 // Salt required to translate renderer device IDs to raw device unique IDs
281 ResourceContext::SaltCallback salt_callback_;
283 // Map of device authorizations for streams that are not yet created
284 // The key is the stream ID, and the value is a pair. The pair's first element
285 // is a bool that is true if the authorization process completes successfully.
286 // The second element contains the unique ID of the authorized device.
287 std::map<int, std::pair<bool, std::string>> authorizations_;
289 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost);
292 } // namespace content
294 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_