- add sources.
[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/memory/ref_counted.h"
13 #include "base/synchronization/lock.h"
14 #include "base/threading/thread_checker.h"
15 #include "content/renderer/media/webrtc_audio_device_impl.h"
16 #include "content/renderer/media/webrtc_local_audio_source_provider.h"
17 #include "media/audio/audio_input_device.h"
18 #include "media/base/audio_capturer_source.h"
19
20 namespace media {
21 class AudioBus;
22 }
23
24 namespace content {
25
26 class WebRtcLocalAudioRenderer;
27 class WebRtcLocalAudioTrack;
28
29 // This class manages the capture data flow by getting data from its
30 // |source_|, and passing it to its |tracks_|.
31 // It allows clients to inject their own capture data source by calling
32 // SetCapturerSource().
33 // The threading model for this class is rather complex since it will be
34 // created on the main render thread, captured data is provided on a dedicated
35 // AudioInputDevice thread, and methods can be called either on the Libjingle
36 // thread or on the main render thread but also other client threads
37 // if an alternative AudioCapturerSource has been set.
38 class CONTENT_EXPORT WebRtcAudioCapturer
39     : public base::RefCountedThreadSafe<WebRtcAudioCapturer>,
40       NON_EXPORTED_BASE(public media::AudioCapturerSource::CaptureCallback) {
41  public:
42   // Use to construct the audio capturer.
43   // Called on the main render thread.
44   static scoped_refptr<WebRtcAudioCapturer> CreateCapturer();
45
46   // Creates and configures the default audio capturing source using the
47   // provided audio parameters.  |render_view_id| specifies the render view
48   // consuming audio for capture.  |session_id| is passed to the browser to
49   // decide which device to use.  |device_id| is used to identify which device
50   // the capturer is created for.  Called on the main render thread.
51   bool Initialize(int render_view_id,
52                   media::ChannelLayout channel_layout,
53                   int sample_rate,
54                   int buffer_size,
55                   int session_id,
56                   const std::string& device_id,
57                   int paired_output_sample_rate,
58                   int paired_output_frames_per_buffer);
59
60   // Add a audio track to the sinks of the capturer.
61   // WebRtcAudioDeviceImpl calls this method on the main render thread but
62   // other clients may call it from other threads. The current implementation
63   // does not support multi-thread calling.
64   // Called on the main render thread or libjingle working thread.
65   void AddTrack(WebRtcLocalAudioTrack* track);
66
67   // Remove a audio track from the sinks of the capturer.
68   // Called on the main render thread or libjingle working thread.
69   void RemoveTrack(WebRtcLocalAudioTrack* track);
70
71   // SetCapturerSource() is called if the client on the source side desires to
72   // provide their own captured audio data. Client is responsible for calling
73   // Start() on its own source to have the ball rolling.
74   // Called on the main render thread.
75   void SetCapturerSource(
76       const scoped_refptr<media::AudioCapturerSource>& source,
77       media::ChannelLayout channel_layout,
78       float sample_rate);
79
80   // Called when a stream is connecting to a peer connection. This will set
81   // up the native buffer size for the stream in order to optimize the
82   // performance for peer connection.
83   void EnablePeerConnectionMode();
84
85   // Volume APIs used by WebRtcAudioDeviceImpl.
86   // Called on the AudioInputDevice audio thread.
87   void SetVolume(int volume);
88   int Volume() const;
89   int MaxVolume() const;
90
91   bool is_recording() const { return running_; }
92
93   // Audio parameters utilized by the audio capturer. Can be utilized by
94   // a local renderer to set up a renderer using identical parameters as the
95   // capturer.
96   // TODO(phoglund): This accessor is inherently unsafe since the returned
97   // parameters can become outdated at any time. Think over the implications
98   // of this accessor and if we can remove it.
99   media::AudioParameters audio_parameters() const;
100
101   // Gets information about the paired output device. Returns true if such a
102   // device exists.
103   bool GetPairedOutputParameters(int* session_id,
104                                  int* output_sample_rate,
105                                  int* output_frames_per_buffer) const;
106
107   const std::string& device_id() const { return device_id_; }
108   int session_id() const { return session_id_; }
109
110   WebKit::WebAudioSourceProvider* audio_source_provider() const {
111     return source_provider_.get();
112   }
113
114   // Stops recording audio.
115   void Stop();
116
117  protected:
118   friend class base::RefCountedThreadSafe<WebRtcAudioCapturer>;
119   WebRtcAudioCapturer();
120   virtual ~WebRtcAudioCapturer();
121
122  private:
123   class TrackOwner;
124   typedef std::list<scoped_refptr<TrackOwner> > TrackList;
125
126   // AudioCapturerSource::CaptureCallback implementation.
127   // Called on the AudioInputDevice audio thread.
128   virtual void Capture(media::AudioBus* audio_source,
129                        int audio_delay_milliseconds,
130                        double volume,
131                        bool key_pressed) OVERRIDE;
132   virtual void OnCaptureError() OVERRIDE;
133
134   // Reconfigures the capturer with a new capture parameters.
135   // Must be called without holding the lock.
136   void Reconfigure(int sample_rate, media::ChannelLayout channel_layout);
137
138   // Starts recording audio.
139   // Triggered by AddSink() on the main render thread or a Libjingle working
140   // thread. It should NOT be called under |lock_|.
141   void Start();
142
143
144
145   // Helper function to get the buffer size based on |peer_connection_mode_|
146   // and sample rate;
147   int GetBufferSize(int sample_rate) const;
148
149   // Used to DCHECK that we are called on the correct thread.
150   base::ThreadChecker thread_checker_;
151
152   // Protects |source_|, |audio_tracks_|, |running_|, |loopback_fifo_|,
153   // |params_| and |buffering_|.
154   mutable base::Lock lock_;
155
156   // A list of audio tracks that the audio data is fed to.
157   TrackList tracks_;
158
159   // The audio data source from the browser process.
160   scoped_refptr<media::AudioCapturerSource> source_;
161
162   // Cached audio parameters for output.
163   media::AudioParameters params_;
164
165   bool running_;
166
167   int render_view_id_;
168
169   // Cached value for the hardware native buffer size, used when
170   // |peer_connection_mode_| is set to false.
171   int hardware_buffer_size_;
172
173   // The media session ID used to identify which input device to be started by
174   // the browser.
175   int session_id_;
176
177   // The device this capturer is given permission to use.
178   std::string device_id_;
179
180   // Stores latest microphone volume received in a CaptureData() callback.
181   // Range is [0, 255].
182   int volume_;
183
184   // The source provider to feed the capture data to other clients like
185   // WebAudio.
186   // TODO(xians): Move the source provider to track once we don't need to feed
187   // delay, volume, key_pressed information to WebAudioCapturerSource.
188   const scoped_ptr<WebRtcLocalAudioSourceProvider> source_provider_;
189
190   // Flag which affects the buffer size used by the capturer.
191   bool peer_connection_mode_;
192
193   int output_sample_rate_;
194   int output_frames_per_buffer_;
195
196   DISALLOW_COPY_AND_ASSIGN(WebRtcAudioCapturer);
197 };
198
199 }  // namespace content
200
201 #endif  // CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_