webrtc_audio_capturer.h revision f2477e01787aa58f445919b809d89e252beef54f
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 "base/time/time.h"
16#include "content/renderer/media/webrtc_audio_device_impl.h"
17#include "media/audio/audio_input_device.h"
18#include "media/base/audio_capturer_source.h"
19
20namespace media {
21class AudioBus;
22}
23
24namespace content {
25
26class WebRtcLocalAudioRenderer;
27class 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.
38class 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  // The first AddTrack will implicitly trigger the Start() of this object.
65  // Called on the main render thread or libjingle working thread.
66  void AddTrack(WebRtcLocalAudioTrack* track);
67
68  // Remove a audio track from the sinks of the capturer.
69  // If the track has been added to the capturer, it  must call RemoveTrack()
70  // before it goes away.
71  // Called on the main render thread or libjingle working thread.
72  void RemoveTrack(WebRtcLocalAudioTrack* track);
73
74  // SetCapturerSource() is called if the client on the source side desires to
75  // provide their own captured audio data. Client is responsible for calling
76  // Start() on its own source to have the ball rolling.
77  // Called on the main render thread.
78  void SetCapturerSource(
79      const scoped_refptr<media::AudioCapturerSource>& source,
80      media::ChannelLayout channel_layout,
81      float sample_rate);
82
83  // Called when a stream is connecting to a peer connection. This will set
84  // up the native buffer size for the stream in order to optimize the
85  // performance for peer connection.
86  void EnablePeerConnectionMode();
87
88  // Volume APIs used by WebRtcAudioDeviceImpl.
89  // Called on the AudioInputDevice audio thread.
90  void SetVolume(int volume);
91  int Volume() const;
92  int MaxVolume() const;
93
94  bool is_recording() const { return running_; }
95
96  // Audio parameters utilized by the audio capturer. Can be utilized by
97  // a local renderer to set up a renderer using identical parameters as the
98  // capturer.
99  // TODO(phoglund): This accessor is inherently unsafe since the returned
100  // parameters can become outdated at any time. Think over the implications
101  // of this accessor and if we can remove it.
102  media::AudioParameters audio_parameters() const;
103
104  // Gets information about the paired output device. Returns true if such a
105  // device exists.
106  bool GetPairedOutputParameters(int* session_id,
107                                 int* output_sample_rate,
108                                 int* output_frames_per_buffer) const;
109
110  const std::string& device_id() const { return device_id_; }
111  int session_id() const { return session_id_; }
112
113  // Stops recording audio. This method will empty its track lists since
114  // stopping the capturer will implicitly invalidate all its tracks.
115  // This method is exposed to the public because the media stream track can
116  // call Stop() on its source.
117  void Stop();
118
119  // Called by the WebAudioCapturerSource to get the audio processing params.
120  // This function is triggered by provideInput() on the WebAudio audio thread,
121  // TODO(xians): Remove after moving APM from WebRtc to Chrome.
122  void GetAudioProcessingParams(base::TimeDelta* delay, int* volume,
123                                bool* key_pressed);
124
125 protected:
126  friend class base::RefCountedThreadSafe<WebRtcAudioCapturer>;
127  WebRtcAudioCapturer();
128  virtual ~WebRtcAudioCapturer();
129
130 private:
131  class TrackOwner;
132  typedef std::list<scoped_refptr<TrackOwner> > TrackList;
133
134  // AudioCapturerSource::CaptureCallback implementation.
135  // Called on the AudioInputDevice audio thread.
136  virtual void Capture(media::AudioBus* audio_source,
137                       int audio_delay_milliseconds,
138                       double volume,
139                       bool key_pressed) OVERRIDE;
140  virtual void OnCaptureError() OVERRIDE;
141
142  // Reconfigures the capturer with a new capture parameters.
143  // Must be called without holding the lock.
144  void Reconfigure(int sample_rate, media::ChannelLayout channel_layout);
145
146  // Starts recording audio.
147  // Triggered by AddSink() on the main render thread or a Libjingle working
148  // thread. It should NOT be called under |lock_|.
149  void Start();
150
151  // Helper function to get the buffer size based on |peer_connection_mode_|
152  // and sample rate;
153  int GetBufferSize(int sample_rate) const;
154
155  // Used to DCHECK that we are called on the correct thread.
156  base::ThreadChecker thread_checker_;
157
158  // Protects |source_|, |audio_tracks_|, |running_|, |loopback_fifo_|,
159  // |params_| and |buffering_|.
160  mutable base::Lock lock_;
161
162  // A list of audio tracks that the audio data is fed to.
163  TrackList tracks_;
164
165  // A list of audio tracks that the capturer needs to notify the audio format
166  // has changed.
167  TrackList tracks_to_notify_format_;
168
169  // The audio data source from the browser process.
170  scoped_refptr<media::AudioCapturerSource> source_;
171
172  // Cached audio parameters for output.
173  media::AudioParameters params_;
174
175  bool running_;
176
177  int render_view_id_;
178
179  // Cached value for the hardware native buffer size, used when
180  // |peer_connection_mode_| is set to false.
181  int hardware_buffer_size_;
182
183  // The media session ID used to identify which input device to be started by
184  // the browser.
185  int session_id_;
186
187  // The device this capturer is given permission to use.
188  std::string device_id_;
189
190  // Stores latest microphone volume received in a CaptureData() callback.
191  // Range is [0, 255].
192  int volume_;
193
194  // Flag which affects the buffer size used by the capturer.
195  bool peer_connection_mode_;
196
197  int output_sample_rate_;
198  int output_frames_per_buffer_;
199
200  // Cache value for the audio processing params.
201  base::TimeDelta audio_delay_;
202  bool key_pressed_;
203
204  DISALLOW_COPY_AND_ASSIGN(WebRtcAudioCapturer);
205};
206
207}  // namespace content
208
209#endif  // CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_
210