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.
5 // AudioRendererHost serves audio related requests from AudioRenderer which
6 // lives inside the render process and provide access to audio hardware.
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
21 // | < NotifyStreamCreated |
24 // | < NotifyStreamStateChanged | kAudioStreamPlaying
27 // | < NotifyStreamStateChanged | kAudioStreamPaused
30 // | < NotifyStreamStateChanged | kAudioStreamPlaying
35 // A SyncSocket pair is used to signal buffer readiness between processes.
37 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_
38 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_
43 #include "base/atomic_ref_count.h"
44 #include "base/gtest_prod_util.h"
45 #include "base/memory/ref_counted.h"
46 #include "base/memory/scoped_ptr.h"
47 #include "base/process/process.h"
48 #include "base/sequenced_task_runner_helpers.h"
49 #include "content/common/content_export.h"
50 #include "content/public/browser/browser_message_filter.h"
51 #include "content/public/browser/browser_thread.h"
52 #include "content/public/browser/render_process_host.h"
53 #include "content/public/browser/resource_context.h"
54 #include "media/audio/audio_io.h"
55 #include "media/audio/audio_logging.h"
56 #include "media/audio/audio_output_controller.h"
57 #include "media/audio/simple_sources.h"
62 class AudioParameters
;
67 class AudioMirroringManager
;
69 class MediaStreamManager
;
70 class MediaStreamUIProxy
;
71 class ResourceContext
;
73 class CONTENT_EXPORT AudioRendererHost
: public BrowserMessageFilter
{
75 // Called from UI thread from the owner of this object.
76 AudioRendererHost(int render_process_id
,
77 media::AudioManager
* audio_manager
,
78 AudioMirroringManager
* mirroring_manager
,
79 MediaInternals
* media_internals
,
80 MediaStreamManager
* media_stream_manager
,
81 const ResourceContext::SaltCallback
& salt_callback
);
83 // Calls |callback| with the list of AudioOutputControllers for this object.
84 void GetOutputControllers(
85 const RenderProcessHost::GetAudioOutputControllersCallback
&
88 // BrowserMessageFilter implementation.
89 void OnChannelClosing() override
;
90 void OnDestruct() const override
;
91 bool OnMessageReceived(const IPC::Message
& message
) override
;
93 // Returns true if any streams managed by this host are actively playing. Can
94 // be called from any thread.
95 bool HasActiveAudio();
97 // Returns true if any streams managed by the RenderFrame identified by
98 // |render_frame_id| are actively playing. Can be called from any thread.
99 bool RenderFrameHasActiveAudio(int render_frame_id
) const;
102 friend class AudioRendererHostTest
;
103 friend class BrowserThread
;
104 friend class base::DeleteHelper
<AudioRendererHost
>;
105 friend class MockAudioRendererHost
;
106 friend class TestAudioRendererHost
;
107 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest
, CreateMockStream
);
108 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest
, MockStreamDataConversation
);
111 typedef std::map
<int, AudioEntry
*> AudioEntryMap
;
113 ~AudioRendererHost() override
;
115 // Methods called on IO thread ----------------------------------------------
117 // Audio related IPC message handlers.
119 // Creates an audio output stream with the specified format whose data is
120 // produced by an entity in the RenderFrame referenced by |render_frame_id|.
121 // |session_id| is used for unified IO to find out which input device to be
122 // opened for the stream. For clients that do not use unified IO,
123 // |session_id| will be ignored.
124 // Upon success/failure, the peer is notified via the NotifyStreamCreated
126 void OnCreateStream(int stream_id
,
129 const media::AudioParameters
& params
);
131 // Play the audio stream referenced by |stream_id|.
132 void OnPlayStream(int stream_id
);
134 // Pause the audio stream referenced by |stream_id|.
135 void OnPauseStream(int stream_id
);
137 // Close the audio stream referenced by |stream_id|.
138 void OnCloseStream(int stream_id
);
140 // Set the volume of the audio stream referenced by |stream_id|.
141 void OnSetVolume(int stream_id
, double volume
);
143 // Set the output device of the audio stream referenced by |stream_id|.
144 void OnSwitchOutputDevice(int stream_id
,
146 const std::string
& device_id
,
147 const GURL
& security_origin
,
150 void OutputDeviceAccessChecked(scoped_ptr
<MediaStreamUIProxy
> ui_proxy
,
152 const std::string
& device_id
,
153 const GURL
& security_origin
,
158 void StartTranslateOutputDeviceName(int stream_id
,
159 const std::string
& device_id
,
160 const GURL
& security_origin
,
163 void FinishTranslateOutputDeviceName(int stream_id
,
164 const std::string
& device_id
,
165 const GURL
& security_origin
,
167 media::AudioDeviceNames
*);
169 void DoSwitchOutputDevice(int stream_id
,
170 const std::string
& raw_device_id
,
173 void DoOutputDeviceSwitched(int stream_id
, int request_id
);
175 // Complete the process of creating an audio stream. This will set up the
176 // shared memory or shared socket in low latency mode and send the
177 // NotifyStreamCreated message to the peer.
178 void DoCompleteCreation(int stream_id
);
180 // Send playing/paused status to the renderer.
181 void DoNotifyStreamStateChanged(int stream_id
, bool is_playing
);
183 RenderProcessHost::AudioOutputControllerList
DoGetOutputControllers() const;
185 // Send an error message to the renderer.
186 void SendErrorMessage(int stream_id
);
188 // Delete an audio entry, notifying observers first. This is called by
189 // AudioOutputController after it has closed.
190 void DeleteEntry(scoped_ptr
<AudioEntry
> entry
);
192 // Send an error message to the renderer, then close the stream.
193 void ReportErrorAndClose(int stream_id
);
195 // A helper method to look up a AudioEntry identified by |stream_id|.
196 // Returns NULL if not found.
197 AudioEntry
* LookupById(int stream_id
);
199 // A helper method to update the number of playing streams and alert the
200 // ResourceScheduler when the renderer starts or stops playing an audiostream.
201 void UpdateNumPlayingStreams(AudioEntry
* entry
, bool is_playing
);
203 // Checks that the renderer process supplies a URL it is allowed to use
204 bool IsURLAllowed(const GURL
& url
);
206 // ID of the RenderProcessHost that owns this instance.
207 const int render_process_id_
;
209 media::AudioManager
* const audio_manager_
;
210 AudioMirroringManager
* const mirroring_manager_
;
211 scoped_ptr
<media::AudioLog
> audio_log_
;
213 // Used to access to AudioInputDeviceManager.
214 MediaStreamManager
* media_stream_manager_
;
216 // A map of stream IDs to audio sources.
217 AudioEntryMap audio_entries_
;
219 // The number of streams in the playing state.
220 base::AtomicRefCount num_playing_streams_
;
222 // Salt required to translate renderer device IDs to raw device IDs
223 ResourceContext::SaltCallback salt_callback_
;
225 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost
);
228 } // namespace content
230 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_