Battery Status API: add UMA logging for Linux.
[chromium-blink-merge.git] / content / browser / renderer_host / media / audio_input_renderer_host.h
blob7bd6692eabb28a683f378ef942f0e67667d37829
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 // AudioInputRendererHost serves audio related requests from audio capturer
6 // which lives inside the render process and provide access to audio hardware.
7 //
8 // Create stream sequence (AudioInputController = AIC):
9 //
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_
27 #include <map>
28 #include <string>
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/shared_memory.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"
45 namespace media {
46 class AudioManager;
47 class AudioParameters;
48 class UserInputMonitor;
51 namespace content {
52 class AudioMirroringManager;
53 class MediaStreamManager;
55 class CONTENT_EXPORT AudioInputRendererHost
56 : public BrowserMessageFilter,
57 public media::AudioInputController::EventHandler {
58 public:
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.
64 enum ErrorCode {
65 // An unspecified error occured.
66 UNKNOWN_ERROR = 0,
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(media::AudioManager* audio_manager,
105 MediaStreamManager* media_stream_manager,
106 AudioMirroringManager* audio_mirroring_manager,
107 media::UserInputMonitor* user_input_monitor);
109 // BrowserMessageFilter implementation.
110 virtual void OnChannelClosing() OVERRIDE;
111 virtual void OnDestruct() const OVERRIDE;
112 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
114 // AudioInputController::EventHandler implementation.
115 virtual void OnCreated(media::AudioInputController* controller) OVERRIDE;
116 virtual void OnRecording(media::AudioInputController* controller) OVERRIDE;
117 virtual void OnError(media::AudioInputController* controller,
118 media::AudioInputController::ErrorCode error_code) OVERRIDE;
119 virtual void OnData(media::AudioInputController* controller,
120 const media::AudioBus* data) OVERRIDE;
121 virtual void OnLog(media::AudioInputController* controller,
122 const std::string& message) OVERRIDE;
124 private:
125 // TODO(henrika): extend test suite (compare AudioRenderHost)
126 friend class BrowserThread;
127 friend class TestAudioInputRendererHost;
128 friend class base::DeleteHelper<AudioInputRendererHost>;
130 struct AudioEntry;
131 typedef std::map<int, AudioEntry*> AudioEntryMap;
133 virtual ~AudioInputRendererHost();
135 // Methods called on IO thread ----------------------------------------------
137 // Audio related IPC message handlers.
139 // Creates an audio input stream with the specified format whose data is
140 // consumed by an entity in the render view referenced by |render_view_id|.
141 // |session_id| is used to find out which device to be used for the stream.
142 // Upon success/failure, the peer is notified via the
143 // NotifyStreamCreated message.
144 void OnCreateStream(int stream_id,
145 int render_view_id,
146 int session_id,
147 const AudioInputHostMsg_CreateStream_Config& config);
149 // Record the audio input stream referenced by |stream_id|.
150 void OnRecordStream(int stream_id);
152 // Close the audio stream referenced by |stream_id|.
153 void OnCloseStream(int stream_id);
155 // Set the volume of the audio stream referenced by |stream_id|.
156 void OnSetVolume(int stream_id, double volume);
158 // Complete the process of creating an audio input stream. This will set up
159 // the shared memory or shared socket in low latency mode and send the
160 // NotifyStreamCreated message to the peer.
161 void DoCompleteCreation(media::AudioInputController* controller);
163 // Send a state change message to the renderer.
164 void DoSendRecordingMessage(media::AudioInputController* controller);
166 // Handle error coming from audio stream.
167 void DoHandleError(media::AudioInputController* controller,
168 media::AudioInputController::ErrorCode error_code);
170 // Log audio level of captured audio stream.
171 void DoLog(media::AudioInputController* controller,
172 const std::string& message);
174 // Send an error message to the renderer.
175 void SendErrorMessage(int stream_id, ErrorCode error_code);
177 // Delete all audio entry and all audio streams
178 void DeleteEntries();
180 // Closes the stream. The stream is then deleted in DeleteEntry() after it
181 // is closed.
182 void CloseAndDeleteStream(AudioEntry* entry);
184 // Delete an audio entry and close the related audio stream.
185 void DeleteEntry(AudioEntry* entry);
187 // Delete audio entry and close the related audio input stream.
188 void DeleteEntryOnError(AudioEntry* entry, ErrorCode error_code);
190 // A helper method to look up a AudioEntry identified by |stream_id|.
191 // Returns NULL if not found.
192 AudioEntry* LookupById(int stream_id);
194 // Search for a AudioEntry having the reference to |controller|.
195 // This method is used to look up an AudioEntry after a controller
196 // event is received.
197 AudioEntry* LookupByController(media::AudioInputController* controller);
199 // Used to create an AudioInputController.
200 media::AudioManager* audio_manager_;
202 // Used to access to AudioInputDeviceManager.
203 MediaStreamManager* media_stream_manager_;
205 AudioMirroringManager* audio_mirroring_manager_;
207 // A map of stream IDs to audio sources.
208 AudioEntryMap audio_entries_;
210 // Raw pointer of the UserInputMonitor.
211 media::UserInputMonitor* user_input_monitor_;
213 scoped_ptr<media::AudioLog> audio_log_;
215 DISALLOW_COPY_AND_ASSIGN(AudioInputRendererHost);
218 } // namespace content
220 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_INPUT_RENDERER_HOST_H_