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 // AudioInputRendererHost serves audio related requests from audio capturer
6 // which lives inside the render process and provide access to audio hardware.
8 // Create stream sequence (AudioInputController = AIC):
10 // AudioInputHostMsg_CreateStream -> OnCreateStream -> AIC::CreateLowLatency ->
11 // <- AudioInputMsg_NotifyStreamCreated <- DoCompleteCreation <- OnCreated <-
13 // Close stream sequence:
15 // AudioInputHostMsg_CloseStream -> OnCloseStream -> AIC::Close ->
17 // This class is owned by BrowserRenderProcessHost and instantiated on UI
18 // thread. All other operations and method calls happen on IO thread, so we
19 // need to be extra careful about the lifetime of this object.
21 // To ensure low latency audio, a SyncSocket pair is used to signal buffer
22 // readiness without having to route messages using the IO thread.
24 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_INPUT_RENDERER_HOST_H_
25 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_INPUT_RENDERER_HOST_H_
30 #include "base/compiler_specific.h"
31 #include "base/gtest_prod_util.h"
32 #include "base/memory/ref_counted.h"
33 #include "base/memory/scoped_ptr.h"
34 #include "base/memory/weak_ptr.h"
35 #include "base/process/process.h"
36 #include "base/sequenced_task_runner_helpers.h"
37 #include "content/common/media/audio_messages.h"
38 #include "content/public/browser/browser_message_filter.h"
39 #include "content/public/browser/browser_thread.h"
40 #include "media/audio/audio_input_controller.h"
41 #include "media/audio/audio_io.h"
42 #include "media/audio/audio_logging.h"
43 #include "media/audio/simple_sources.h"
47 class AudioParameters
;
48 class UserInputMonitor
;
52 class AudioMirroringManager
;
53 class MediaStreamManager
;
54 class RenderProcessHost
;
56 class CONTENT_EXPORT AudioInputRendererHost
57 : public BrowserMessageFilter
,
58 public media::AudioInputController::EventHandler
{
60 // Error codes to make native loggin more clear. These error codes are added
61 // to generic error strings to provide a higher degree of details.
62 // Changing these values can lead to problems when matching native debug
63 // logs with the actual cause of error.
65 // An unspecified error occured.
68 // Failed to look up audio intry for the provided stream id.
69 INVALID_AUDIO_ENTRY
, // = 1
71 // A stream with the specified stream id already exists.
72 STREAM_ALREADY_EXISTS
, // = 2
74 // The page does not have permission to open the specified capture device.
75 PERMISSION_DENIED
, // = 3
77 // Failed to create shared memory.
78 SHARED_MEMORY_CREATE_FAILED
, // = 4
80 // Failed to initialize the AudioInputSyncWriter instance.
81 SYNC_WRITER_INIT_FAILED
, // = 5
83 // Failed to create native audio input stream.
84 STREAM_CREATE_ERROR
, // = 6
86 // Renderer process handle is invalid.
87 INVALID_PEER_HANDLE
, // = 7
89 // Only low-latency mode is supported.
90 INVALID_LATENCY_MODE
, // = 8
92 // Failed to map and share the shared memory.
93 MEMORY_SHARING_FAILED
, // = 9
95 // Unable to prepare the foreign socket handle.
96 SYNC_SOCKET_ERROR
, // = 10
98 // This error message comes from the AudioInputController instance.
99 AUDIO_INPUT_CONTROLLER_ERROR
, // = 11
102 // Called from UI thread from the owner of this object.
103 // |user_input_monitor| is used for typing detection and can be NULL.
104 AudioInputRendererHost(int render_process_id
,
106 media::AudioManager
* audio_manager
,
107 MediaStreamManager
* media_stream_manager
,
108 AudioMirroringManager
* audio_mirroring_manager
,
109 media::UserInputMonitor
* user_input_monitor
);
111 #if defined(ENABLE_WEBRTC)
112 // Enable and disable debug recording of input on all audio entries.
113 void EnableDebugRecording(const base::FilePath
& file
);
114 void DisableDebugRecording();
117 // BrowserMessageFilter implementation.
118 void OnChannelClosing() override
;
119 void OnDestruct() const override
;
120 bool OnMessageReceived(const IPC::Message
& message
) override
;
122 // AudioInputController::EventHandler implementation.
123 void OnCreated(media::AudioInputController
* controller
) override
;
124 void OnRecording(media::AudioInputController
* controller
) override
;
125 void OnError(media::AudioInputController
* controller
,
126 media::AudioInputController::ErrorCode error_code
) override
;
127 void OnData(media::AudioInputController
* controller
,
128 const media::AudioBus
* data
) override
;
129 void OnLog(media::AudioInputController
* controller
,
130 const std::string
& message
) override
;
132 // Sets the PID renderer. This is used for constructing the debug recording
134 void set_renderer_pid(int32 renderer_pid
);
137 // TODO(henrika): extend test suite (compare AudioRenderHost)
138 friend class BrowserThread
;
139 friend class TestAudioInputRendererHost
;
140 friend class base::DeleteHelper
<AudioInputRendererHost
>;
143 typedef std::map
<int, AudioEntry
*> AudioEntryMap
;
145 ~AudioInputRendererHost() override
;
147 // Methods called on IO thread ----------------------------------------------
149 // Audio related IPC message handlers.
151 // For ChromeOS: Checks if the stream should contain keyboard mic, if so
152 // registers to AudioInputDeviceManager. Then calls DoCreateStream.
153 // For non-ChromeOS: Just calls DoCreateStream.
154 void OnCreateStream(int stream_id
,
157 const AudioInputHostMsg_CreateStream_Config
& config
);
159 // Creates an audio input stream with the specified format whose data is
160 // consumed by an entity in the RenderFrame referenced by |render_frame_id|.
161 // |session_id| is used to find out which device to be used for the stream.
162 // Upon success/failure, the peer is notified via the
163 // NotifyStreamCreated message.
164 void DoCreateStream(int stream_id
,
167 const AudioInputHostMsg_CreateStream_Config
& config
);
169 // Record the audio input stream referenced by |stream_id|.
170 void OnRecordStream(int stream_id
);
172 // Close the audio stream referenced by |stream_id|.
173 void OnCloseStream(int stream_id
);
175 // Set the volume of the audio stream referenced by |stream_id|.
176 void OnSetVolume(int stream_id
, double volume
);
178 // Complete the process of creating an audio input stream. This will set up
179 // the shared memory or shared socket in low latency mode and send the
180 // NotifyStreamCreated message to the peer.
181 void DoCompleteCreation(media::AudioInputController
* controller
);
183 // Send a state change message to the renderer.
184 void DoSendRecordingMessage(media::AudioInputController
* controller
);
186 // Handle error coming from audio stream.
187 void DoHandleError(media::AudioInputController
* controller
,
188 media::AudioInputController::ErrorCode error_code
);
190 // Log audio level of captured audio stream.
191 void DoLog(media::AudioInputController
* controller
,
192 const std::string
& message
);
194 // Send an error message to the renderer.
195 void SendErrorMessage(int stream_id
, ErrorCode error_code
);
197 // Delete all audio entry and all audio streams
198 void DeleteEntries();
200 // Closes the stream. The stream is then deleted in DeleteEntry() after it
202 void CloseAndDeleteStream(AudioEntry
* entry
);
204 // Delete an audio entry and close the related audio stream.
205 void DeleteEntry(AudioEntry
* entry
);
207 // Delete audio entry and close the related audio input stream.
208 void DeleteEntryOnError(AudioEntry
* entry
, ErrorCode error_code
);
210 // A helper method to look up a AudioEntry identified by |stream_id|.
211 // Returns NULL if not found.
212 AudioEntry
* LookupById(int stream_id
);
214 // Search for a AudioEntry having the reference to |controller|.
215 // This method is used to look up an AudioEntry after a controller
216 // event is received.
217 AudioEntry
* LookupByController(media::AudioInputController
* controller
);
219 // If ChromeOS and |config|'s layout has keyboard mic, unregister in
220 // AudioInputDeviceManager.
221 void MaybeUnregisterKeyboardMicStream(
222 const AudioInputHostMsg_CreateStream_Config
& config
);
224 #if defined(ENABLE_WEBRTC)
225 void MaybeEnableDebugRecordingForId(int stream_id
);
227 base::FilePath
GetDebugRecordingFilePathWithExtensions(
228 const base::FilePath
& file
);
230 void EnableDebugRecordingForId(const base::FilePath
& file
, int stream_id
);
232 void DoEnableDebugRecording(int stream_id
, base::File file
);
233 void DoDisableDebugRecording(int stream_id
);
235 // Delete the debug writer used for debug recordings for |stream_id|.
236 void DeleteDebugWriter(int stream_id
);
239 // ID of the RenderProcessHost that owns this instance.
240 const int render_process_id_
;
242 // PID of the render process connected to the RenderProcessHost that owns this
246 // Used to create an AudioInputController.
247 media::AudioManager
* audio_manager_
;
249 // Used to access to AudioInputDeviceManager.
250 MediaStreamManager
* media_stream_manager_
;
252 AudioMirroringManager
* audio_mirroring_manager_
;
254 // A map of stream IDs to audio sources.
255 AudioEntryMap audio_entries_
;
257 // Raw pointer of the UserInputMonitor.
258 media::UserInputMonitor
* user_input_monitor_
;
260 scoped_ptr
<media::AudioLog
> audio_log_
;
262 base::WeakPtrFactory
<AudioInputRendererHost
> weak_factory_
;
264 DISALLOW_COPY_AND_ASSIGN(AudioInputRendererHost
);
267 } // namespace content
269 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_INPUT_RENDERER_HOST_H_