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