Associate audio streams with their source/destination RenderView.
[chromium-blink-merge.git] / content / browser / renderer_host / media / audio_input_renderer_host.h
blobd0d4438d23834e159427fab76680e79c7619177f
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 // For the OnStartDevice() request, AudioInputRendererHost starts the device
18 // referenced by the session id, and an OnDeviceStarted() callback with the
19 // id of the opened device will be received later. Then it will send a
20 // IPC message to notify the renderer that the device is ready, so that
21 // renderer can continue with the OnCreateStream() request.
23 // OnDeviceStopped() is called when the user closes the device through
24 // AudioInputDeviceManager without calling Stop() before. What
25 // AudioInputRenderHost::OnDeviceStopped() does is to send a IPC mesaage to
26 // notify the renderer in order to stop the stream.
28 // Start device sequence:
30 // OnStartDevice -> AudioInputDeviceManager::Start ->
31 // AudioInputDeviceManagerEventHandler::OnDeviceStarted ->
32 // AudioInputMsg_NotifyDeviceStarted
34 // Shutdown device sequence:
36 // OnDeviceStopped -> CloseAndDeleteStream
37 // AudioInputMsg_NotifyStreamStateChanged
39 // This class is owned by BrowserRenderProcessHost and instantiated on UI
40 // thread. All other operations and method calls happen on IO thread, so we
41 // need to be extra careful about the lifetime of this object.
43 // To ensure low latency audio, a SyncSocket pair is used to signal buffer
44 // readiness without having to route messages using the IO thread.
46 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_INPUT_RENDERER_HOST_H_
47 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_INPUT_RENDERER_HOST_H_
49 #include <map>
50 #include <string>
52 #include "base/compiler_specific.h"
53 #include "base/gtest_prod_util.h"
54 #include "base/memory/ref_counted.h"
55 #include "base/memory/scoped_ptr.h"
56 #include "base/process.h"
57 #include "base/sequenced_task_runner_helpers.h"
58 #include "base/shared_memory.h"
59 #include "content/browser/renderer_host/media/audio_input_device_manager_event_handler.h"
60 #include "content/public/browser/browser_message_filter.h"
61 #include "content/public/browser/browser_thread.h"
62 #include "media/audio/audio_input_controller.h"
63 #include "media/audio/audio_io.h"
64 #include "media/audio/simple_sources.h"
66 namespace media {
67 class AudioManager;
68 class AudioParameters;
71 namespace content {
72 class MediaStreamManager;
74 class CONTENT_EXPORT AudioInputRendererHost
75 : public BrowserMessageFilter,
76 public media::AudioInputController::EventHandler,
77 public AudioInputDeviceManagerEventHandler {
78 public:
79 // Called from UI thread from the owner of this object.
80 AudioInputRendererHost(
81 media::AudioManager* audio_manager,
82 MediaStreamManager* media_stream_manager);
84 // BrowserMessageFilter implementation.
85 virtual void OnChannelClosing() OVERRIDE;
86 virtual void OnDestruct() const OVERRIDE;
87 virtual bool OnMessageReceived(const IPC::Message& message,
88 bool* message_was_ok) OVERRIDE;
90 // AudioInputController::EventHandler implementation.
91 virtual void OnCreated(media::AudioInputController* controller) OVERRIDE;
92 virtual void OnRecording(media::AudioInputController* controller) OVERRIDE;
93 virtual void OnError(media::AudioInputController* controller,
94 int error_code) OVERRIDE;
95 virtual void OnData(media::AudioInputController* controller,
96 const uint8* data,
97 uint32 size) OVERRIDE;
99 // AudioInputDeviceManagerEventHandler implementation.
100 virtual void OnDeviceStarted(int session_id,
101 const std::string& device_id) OVERRIDE;
102 virtual void OnDeviceStopped(int session_id) OVERRIDE;
104 private:
105 // TODO(henrika): extend test suite (compare AudioRenderHost)
106 friend class BrowserThread;
107 friend class base::DeleteHelper<AudioInputRendererHost>;
109 struct AudioEntry;
110 typedef std::map<int, AudioEntry*> AudioEntryMap;
111 typedef std::map<int, int> SessionEntryMap;
113 virtual ~AudioInputRendererHost();
115 // Methods called on IO thread ----------------------------------------------
117 // Start the audio input device with the session id. If the device
118 // starts successfully, it will trigger OnDeviceStarted() callback.
119 void OnStartDevice(int stream_id, int session_id);
121 // Audio related IPC message handlers.
122 // Creates an audio input stream with the specified format. If this call is
123 // successful this object would keep an internal entry of the stream for the
124 // required properties.
125 void OnCreateStream(int stream_id,
126 const media::AudioParameters& params,
127 const std::string& device_id,
128 bool automatic_gain_control);
130 // Track that the data for the audio stream referenced by |stream_id| is
131 // consumed by an entity in the render view referenced by |render_view_id|.
132 void OnAssociateStreamWithConsumer(int stream_id, int render_view_id);
134 // Record the audio input stream referenced by |stream_id|.
135 void OnRecordStream(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 // Complete the process of creating an audio input stream. This will set up
144 // the shared memory or shared socket in low latency mode.
145 void DoCompleteCreation(media::AudioInputController* controller);
147 // Send a state change message to the renderer.
148 void DoSendRecordingMessage(media::AudioInputController* controller);
150 // Handle error coming from audio stream.
151 void DoHandleError(media::AudioInputController* controller, int error_code);
153 // Send an error message to the renderer.
154 void SendErrorMessage(int stream_id);
156 // Delete all audio entry and all audio streams
157 void DeleteEntries();
159 // Closes the stream. The stream is then deleted in DeleteEntry() after it
160 // is closed.
161 void CloseAndDeleteStream(AudioEntry* entry);
163 // Delete an audio entry and close the related audio stream.
164 void DeleteEntry(AudioEntry* entry);
166 // Delete audio entry and close the related audio input stream.
167 void DeleteEntryOnError(AudioEntry* entry);
169 // Stop the device and delete its audio session entry.
170 void StopAndDeleteDevice(int stream_id);
172 // A helper method to look up a AudioEntry identified by |stream_id|.
173 // Returns NULL if not found.
174 AudioEntry* LookupById(int stream_id);
176 // Search for a AudioEntry having the reference to |controller|.
177 // This method is used to look up an AudioEntry after a controller
178 // event is received.
179 AudioEntry* LookupByController(media::AudioInputController* controller);
181 // A helper method to look up a session identified by |stream_id|.
182 // Returns 0 if not found.
183 int LookupSessionById(int stream_id);
185 // Used to create an AudioInputController.
186 media::AudioManager* audio_manager_;
188 // Used to access to AudioInputDeviceManager.
189 MediaStreamManager* media_stream_manager_;
191 // A map of stream IDs to audio sources.
192 AudioEntryMap audio_entries_;
194 // A map of session IDs to audio session sources.
195 SessionEntryMap session_entries_;
197 DISALLOW_COPY_AND_ASSIGN(AudioInputRendererHost);
200 } // namespace content
202 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_INPUT_RENDERER_HOST_H_