Implement getMediaDevices.
[chromium-blink-merge.git] / content / renderer / media / webrtc_audio_capturer.h
blobf433a1aa77cd7f4892ec5642863758f98aaaae84
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 #ifndef CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_
6 #define CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_
8 #include <list>
9 #include <string>
11 #include "base/callback.h"
12 #include "base/files/file.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/synchronization/lock.h"
15 #include "base/threading/thread_checker.h"
16 #include "base/time/time.h"
17 #include "content/common/media/media_stream_options.h"
18 #include "content/renderer/media/tagged_list.h"
19 #include "media/audio/audio_input_device.h"
20 #include "media/audio/audio_power_monitor.h"
21 #include "media/base/audio_capturer_source.h"
22 #include "third_party/WebKit/public/platform/WebMediaConstraints.h"
24 namespace media {
25 class AudioBus;
28 namespace content {
30 class MediaStreamAudioProcessor;
31 class MediaStreamAudioSource;
32 class WebRtcAudioDeviceImpl;
33 class WebRtcLocalAudioRenderer;
34 class WebRtcLocalAudioTrack;
36 // This class manages the capture data flow by getting data from its
37 // |source_|, and passing it to its |tracks_|.
38 // The threading model for this class is rather complex since it will be
39 // created on the main render thread, captured data is provided on a dedicated
40 // AudioInputDevice thread, and methods can be called either on the Libjingle
41 // thread or on the main render thread but also other client threads
42 // if an alternative AudioCapturerSource has been set.
43 class CONTENT_EXPORT WebRtcAudioCapturer
44 : public base::RefCountedThreadSafe<WebRtcAudioCapturer>,
45 NON_EXPORTED_BASE(public media::AudioCapturerSource::CaptureCallback) {
46 public:
47 // Used to construct the audio capturer. |render_view_id| specifies the
48 // render view consuming audio for capture, |render_view_id| as -1 is used
49 // by the unittests to skip creating a source via
50 // AudioDeviceFactory::NewInputDevice(), and allow injecting their own source
51 // via SetCapturerSourceForTesting() at a later state. |device_info|
52 // contains all the device information that the capturer is created for.
53 // |constraints| contains the settings for audio processing.
54 // TODO(xians): Implement the interface for the audio source and move the
55 // |constraints| to ApplyConstraints().
56 // Called on the main render thread.
57 static scoped_refptr<WebRtcAudioCapturer> CreateCapturer(
58 int render_view_id,
59 const StreamDeviceInfo& device_info,
60 const blink::WebMediaConstraints& constraints,
61 WebRtcAudioDeviceImpl* audio_device,
62 MediaStreamAudioSource* audio_source);
65 // Add a audio track to the sinks of the capturer.
66 // WebRtcAudioDeviceImpl calls this method on the main render thread but
67 // other clients may call it from other threads. The current implementation
68 // does not support multi-thread calling.
69 // The first AddTrack will implicitly trigger the Start() of this object.
70 void AddTrack(WebRtcLocalAudioTrack* track);
72 // Remove a audio track from the sinks of the capturer.
73 // If the track has been added to the capturer, it must call RemoveTrack()
74 // before it goes away.
75 // Called on the main render thread or libjingle working thread.
76 void RemoveTrack(WebRtcLocalAudioTrack* track);
78 // Called when a stream is connecting to a peer connection. This will set
79 // up the native buffer size for the stream in order to optimize the
80 // performance for peer connection.
81 void EnablePeerConnectionMode();
83 // Volume APIs used by WebRtcAudioDeviceImpl.
84 // Called on the AudioInputDevice audio thread.
85 void SetVolume(int volume);
86 int Volume() const;
87 int MaxVolume() const;
89 // Audio parameters utilized by the source of the audio capturer.
90 // TODO(phoglund): Think over the implications of this accessor and if we can
91 // remove it.
92 media::AudioParameters source_audio_parameters() const;
94 // Gets information about the paired output device. Returns true if such a
95 // device exists.
96 bool GetPairedOutputParameters(int* session_id,
97 int* output_sample_rate,
98 int* output_frames_per_buffer) const;
100 const std::string& device_id() const { return device_info_.device.id; }
101 int session_id() const { return device_info_.session_id; }
103 // Stops recording audio. This method will empty its track lists since
104 // stopping the capturer will implicitly invalidate all its tracks.
105 // This method is exposed to the public because the MediaStreamAudioSource can
106 // call Stop()
107 void Stop();
109 // Called by the WebAudioCapturerSource to get the audio processing params.
110 // This function is triggered by provideInput() on the WebAudio audio thread,
111 // TODO(xians): Remove after moving APM from WebRtc to Chrome.
112 void GetAudioProcessingParams(base::TimeDelta* delay, int* volume,
113 bool* key_pressed);
115 // Used by the unittests to inject their own source to the capturer.
116 void SetCapturerSourceForTesting(
117 const scoped_refptr<media::AudioCapturerSource>& source,
118 media::AudioParameters params);
120 void StartAecDump(base::File aec_dump_file);
121 void StopAecDump();
123 protected:
124 friend class base::RefCountedThreadSafe<WebRtcAudioCapturer>;
125 virtual ~WebRtcAudioCapturer();
127 private:
128 class TrackOwner;
129 typedef TaggedList<TrackOwner> TrackList;
131 WebRtcAudioCapturer(int render_view_id,
132 const StreamDeviceInfo& device_info,
133 const blink::WebMediaConstraints& constraints,
134 WebRtcAudioDeviceImpl* audio_device,
135 MediaStreamAudioSource* audio_source);
137 // AudioCapturerSource::CaptureCallback implementation.
138 // Called on the AudioInputDevice audio thread.
139 virtual void Capture(media::AudioBus* audio_source,
140 int audio_delay_milliseconds,
141 double volume,
142 bool key_pressed) OVERRIDE;
143 virtual void OnCaptureError() OVERRIDE;
145 // Initializes the default audio capturing source using the provided render
146 // view id and device information. Return true if success, otherwise false.
147 bool Initialize();
149 // SetCapturerSource() is called if the client on the source side desires to
150 // provide their own captured audio data. Client is responsible for calling
151 // Start() on its own source to have the ball rolling.
152 // Called on the main render thread.
153 void SetCapturerSource(
154 const scoped_refptr<media::AudioCapturerSource>& source,
155 media::ChannelLayout channel_layout,
156 float sample_rate);
158 // Starts recording audio.
159 // Triggered by AddSink() on the main render thread or a Libjingle working
160 // thread. It should NOT be called under |lock_|.
161 void Start();
163 // Helper function to get the buffer size based on |peer_connection_mode_|
164 // and sample rate;
165 int GetBufferSize(int sample_rate) const;
167 // Used to DCHECK that we are called on the correct thread.
168 base::ThreadChecker thread_checker_;
170 // Protects |source_|, |audio_tracks_|, |running_|, |loopback_fifo_|,
171 // |params_| and |buffering_|.
172 mutable base::Lock lock_;
174 // A tagged list of audio tracks that the audio data is fed
175 // to. Tagged items need to be notified that the audio format has
176 // changed.
177 TrackList tracks_;
179 // The audio data source from the browser process.
180 scoped_refptr<media::AudioCapturerSource> source_;
182 // Cached audio constraints for the capturer.
183 blink::WebMediaConstraints constraints_;
185 // Audio processor doing processing like FIFO, AGC, AEC and NS. Its output
186 // data is in a unit of 10 ms data chunk.
187 scoped_refptr<MediaStreamAudioProcessor> audio_processor_;
189 bool running_;
191 int render_view_id_;
193 // Cached information of the device used by the capturer.
194 const StreamDeviceInfo device_info_;
196 // Stores latest microphone volume received in a CaptureData() callback.
197 // Range is [0, 255].
198 int volume_;
200 // Flag which affects the buffer size used by the capturer.
201 bool peer_connection_mode_;
203 // Cache value for the audio processing params.
204 base::TimeDelta audio_delay_;
205 bool key_pressed_;
207 // Flag to help deciding if the data needs audio processing.
208 bool need_audio_processing_;
210 // Raw pointer to the WebRtcAudioDeviceImpl, which is valid for the lifetime
211 // of RenderThread.
212 WebRtcAudioDeviceImpl* audio_device_;
214 // Raw pointer to the MediaStreamAudioSource object that holds a reference
215 // to this WebRtcAudioCapturer.
216 // Since |audio_source_| is owned by a blink::WebMediaStreamSource object and
217 // blink guarantees that the blink::WebMediaStreamSource outlives any
218 // blink::WebMediaStreamTrack connected to the source, |audio_source_| is
219 // guaranteed to exist as long as a WebRtcLocalAudioTrack is connected to this
220 // WebRtcAudioCapturer.
221 MediaStreamAudioSource* const audio_source_;
223 // Audio power monitor for logging audio power level.
224 media::AudioPowerMonitor audio_power_monitor_;
226 // Records when the last time audio power level is logged.
227 base::TimeTicks last_audio_level_log_time_;
229 DISALLOW_COPY_AND_ASSIGN(WebRtcAudioCapturer);
232 } // namespace content
234 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_