Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / media / webrtc_audio_capturer.h
blobca10ff2ab399b782d9f1749e796c21540134056e
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/base/audio_capturer_source.h"
21 #include "third_party/WebKit/public/platform/WebMediaConstraints.h"
23 namespace media {
24 class AudioBus;
27 namespace content {
29 class MediaStreamAudioProcessor;
30 class MediaStreamAudioSource;
31 class WebRtcAudioDeviceImpl;
32 class WebRtcLocalAudioRenderer;
33 class WebRtcLocalAudioTrack;
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_frame_id| specifies the
47 // RenderFrame consuming audio for capture; -1 is used for tests.
48 // |device_info| contains all the device information that the capturer is
49 // created for. |constraints| contains the settings for audio processing.
50 // TODO(xians): Implement the interface for the audio source and move the
51 // |constraints| to ApplyConstraints(). Called on the main render thread.
52 static scoped_refptr<WebRtcAudioCapturer> CreateCapturer(
53 int render_frame_id,
54 const StreamDeviceInfo& device_info,
55 const blink::WebMediaConstraints& constraints,
56 WebRtcAudioDeviceImpl* audio_device,
57 MediaStreamAudioSource* audio_source);
59 // Add a audio track to the sinks of the capturer.
60 // WebRtcAudioDeviceImpl calls this method on the main render thread but
61 // other clients may call it from other threads. The current implementation
62 // does not support multi-thread calling.
63 // The first AddTrack will implicitly trigger the Start() of this object.
64 void AddTrack(WebRtcLocalAudioTrack* track);
66 // Remove a audio track from the sinks of the capturer.
67 // If the track has been added to the capturer, it must call RemoveTrack()
68 // before it goes away.
69 // Called on the main render thread or libjingle working thread.
70 void RemoveTrack(WebRtcLocalAudioTrack* track);
72 // Called when a stream is connecting to a peer connection. This will set
73 // up the native buffer size for the stream in order to optimize the
74 // performance for peer connection.
75 void EnablePeerConnectionMode();
77 // Volume APIs used by WebRtcAudioDeviceImpl.
78 // Called on the AudioInputDevice audio thread.
79 void SetVolume(int volume);
80 int Volume() const;
81 int MaxVolume() const;
83 // Audio parameters utilized by the source of the audio capturer.
84 // TODO(phoglund): Think over the implications of this accessor and if we can
85 // remove it.
86 media::AudioParameters source_audio_parameters() const;
88 // Gets information about the paired output device. Returns true if such a
89 // device exists.
90 bool GetPairedOutputParameters(int* session_id,
91 int* output_sample_rate,
92 int* output_frames_per_buffer) const;
94 const std::string& device_id() const { return device_info_.device.id; }
95 int session_id() const { return device_info_.session_id; }
97 // Stops recording audio. This method will empty its track lists since
98 // stopping the capturer will implicitly invalidate all its tracks.
99 // This method is exposed to the public because the MediaStreamAudioSource can
100 // call Stop()
101 void Stop();
103 // Returns the output format.
104 // Called on the main render thread.
105 media::AudioParameters GetOutputFormat() const;
107 // Used by clients to inject their own source to the capturer.
108 void SetCapturerSource(
109 const scoped_refptr<media::AudioCapturerSource>& source,
110 media::AudioParameters params);
112 protected:
113 friend class base::RefCountedThreadSafe<WebRtcAudioCapturer>;
114 ~WebRtcAudioCapturer() override;
116 private:
117 class TrackOwner;
118 typedef TaggedList<TrackOwner> TrackList;
120 WebRtcAudioCapturer(int render_frame_id,
121 const StreamDeviceInfo& device_info,
122 const blink::WebMediaConstraints& constraints,
123 WebRtcAudioDeviceImpl* audio_device,
124 MediaStreamAudioSource* audio_source);
126 // AudioCapturerSource::CaptureCallback implementation.
127 // Called on the AudioInputDevice audio thread.
128 void Capture(const media::AudioBus* audio_source,
129 int audio_delay_milliseconds,
130 double volume,
131 bool key_pressed) override;
132 void OnCaptureError() override;
134 // Initializes the default audio capturing source using the provided render
135 // frame id and device information. Return true if success, otherwise false.
136 bool Initialize();
138 // SetCapturerSourceInternal() is called if the client on the source side
139 // desires to provide their own captured audio data. Client is responsible
140 // for calling Start() on its own source to get the ball rolling.
141 // Called on the main render thread.
142 void SetCapturerSourceInternal(
143 const scoped_refptr<media::AudioCapturerSource>& source,
144 media::ChannelLayout channel_layout,
145 float sample_rate);
147 // Starts recording audio.
148 // Triggered by AddSink() on the main render thread or a Libjingle working
149 // thread. It should NOT be called under |lock_|.
150 void Start();
152 // Helper function to get the buffer size based on |peer_connection_mode_|
153 // and sample rate;
154 int GetBufferSize(int sample_rate) const;
156 // Used to DCHECK that we are called on the correct thread.
157 base::ThreadChecker thread_checker_;
159 // Protects |source_|, |audio_tracks_|, |running_|, |loopback_fifo_|,
160 // |params_| and |buffering_|.
161 mutable base::Lock lock_;
163 // A tagged list of audio tracks that the audio data is fed
164 // to. Tagged items need to be notified that the audio format has
165 // changed.
166 TrackList tracks_;
168 // The audio data source from the browser process.
169 scoped_refptr<media::AudioCapturerSource> source_;
171 // Cached audio constraints for the capturer.
172 blink::WebMediaConstraints constraints_;
174 // Audio processor doing processing like FIFO, AGC, AEC and NS. Its output
175 // data is in a unit of 10 ms data chunk.
176 scoped_refptr<MediaStreamAudioProcessor> audio_processor_;
178 bool running_;
180 int render_frame_id_;
182 // Cached information of the device used by the capturer.
183 const StreamDeviceInfo device_info_;
185 // Stores latest microphone volume received in a CaptureData() callback.
186 // Range is [0, 255].
187 int volume_;
189 // Flag which affects the buffer size used by the capturer.
190 bool peer_connection_mode_;
192 // Raw pointer to the WebRtcAudioDeviceImpl, which is valid for the lifetime
193 // of RenderThread.
194 WebRtcAudioDeviceImpl* audio_device_;
196 // Raw pointer to the MediaStreamAudioSource object that holds a reference
197 // to this WebRtcAudioCapturer.
198 // Since |audio_source_| is owned by a blink::WebMediaStreamSource object and
199 // blink guarantees that the blink::WebMediaStreamSource outlives any
200 // blink::WebMediaStreamTrack connected to the source, |audio_source_| is
201 // guaranteed to exist as long as a WebRtcLocalAudioTrack is connected to this
202 // WebRtcAudioCapturer.
203 MediaStreamAudioSource* const audio_source_;
205 DISALLOW_COPY_AND_ASSIGN(WebRtcAudioCapturer);
208 } // namespace content
210 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_