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_
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"
26 class WebRtcLocalAudioRenderer;
27 class WebRtcLocalAudioTrack;
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) {
42 // Use to construct the audio capturer.
43 // Called on the main render thread.
44 static scoped_refptr<WebRtcAudioCapturer> CreateCapturer();
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,
56 const std::string& device_id,
57 int paired_output_sample_rate,
58 int paired_output_frames_per_buffer);
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);
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);
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,
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();
85 // Volume APIs used by WebRtcAudioDeviceImpl.
86 // Called on the AudioInputDevice audio thread.
87 void SetVolume(int volume);
89 int MaxVolume() const;
91 bool is_recording() const { return running_; }
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
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;
101 // Gets information about the paired output device. Returns true if such a
103 bool GetPairedOutputParameters(int* session_id,
104 int* output_sample_rate,
105 int* output_frames_per_buffer) const;
107 const std::string& device_id() const { return device_id_; }
108 int session_id() const { return session_id_; }
110 WebKit::WebAudioSourceProvider* audio_source_provider() const {
111 return source_provider_.get();
114 // Stops recording audio.
118 friend class base::RefCountedThreadSafe<WebRtcAudioCapturer>;
119 WebRtcAudioCapturer();
120 virtual ~WebRtcAudioCapturer();
124 typedef std::list<scoped_refptr<TrackOwner> > TrackList;
126 // AudioCapturerSource::CaptureCallback implementation.
127 // Called on the AudioInputDevice audio thread.
128 virtual void Capture(media::AudioBus* audio_source,
129 int audio_delay_milliseconds,
131 bool key_pressed) OVERRIDE;
132 virtual void OnCaptureError() OVERRIDE;
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);
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_|.
145 // Helper function to get the buffer size based on |peer_connection_mode_|
147 int GetBufferSize(int sample_rate) const;
149 // Used to DCHECK that we are called on the correct thread.
150 base::ThreadChecker thread_checker_;
152 // Protects |source_|, |audio_tracks_|, |running_|, |loopback_fifo_|,
153 // |params_| and |buffering_|.
154 mutable base::Lock lock_;
156 // A list of audio tracks that the audio data is fed to.
159 // The audio data source from the browser process.
160 scoped_refptr<media::AudioCapturerSource> source_;
162 // Cached audio parameters for output.
163 media::AudioParameters params_;
169 // Cached value for the hardware native buffer size, used when
170 // |peer_connection_mode_| is set to false.
171 int hardware_buffer_size_;
173 // The media session ID used to identify which input device to be started by
177 // The device this capturer is given permission to use.
178 std::string device_id_;
180 // Stores latest microphone volume received in a CaptureData() callback.
181 // Range is [0, 255].
184 // The source provider to feed the capture data to other clients like
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_;
190 // Flag which affects the buffer size used by the capturer.
191 bool peer_connection_mode_;
193 int output_sample_rate_;
194 int output_frames_per_buffer_;
196 DISALLOW_COPY_AND_ASSIGN(WebRtcAudioCapturer);
199 } // namespace content
201 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_