[ServiceWorker] Implement WebServiceWorkerContextClient::openWindow().
[chromium-blink-merge.git] / content / renderer / media / webrtc_audio_capturer.h
blobed72fad14ac9eeb687505da24c89deb9ae925e6c
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_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);
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);
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);
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();
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;
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;
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;
99 const std::string& device_id() const { return device_info_.device.id; }
100 int session_id() const { return device_info_.session_id; }
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();
108 // Returns the output format.
109 // Called on the main render thread.
110 media::AudioParameters GetOutputFormat() const;
112 // Used by the unittests to inject their own source to the capturer.
113 void SetCapturerSourceForTesting(
114 const scoped_refptr<media::AudioCapturerSource>& source,
115 media::AudioParameters params);
117 protected:
118 friend class base::RefCountedThreadSafe<WebRtcAudioCapturer>;
119 ~WebRtcAudioCapturer() override;
121 private:
122 class TrackOwner;
123 typedef TaggedList<TrackOwner> TrackList;
125 WebRtcAudioCapturer(int render_view_id,
126 const StreamDeviceInfo& device_info,
127 const blink::WebMediaConstraints& constraints,
128 WebRtcAudioDeviceImpl* audio_device,
129 MediaStreamAudioSource* audio_source);
131 // AudioCapturerSource::CaptureCallback implementation.
132 // Called on the AudioInputDevice audio thread.
133 void Capture(const media::AudioBus* audio_source,
134 int audio_delay_milliseconds,
135 double volume,
136 bool key_pressed) override;
137 void OnCaptureError() override;
139 // Initializes the default audio capturing source using the provided render
140 // view id and device information. Return true if success, otherwise false.
141 bool Initialize();
143 // SetCapturerSource() is called if the client on the source side desires to
144 // provide their own captured audio data. Client is responsible for calling
145 // Start() on its own source to have the ball rolling.
146 // Called on the main render thread.
147 void SetCapturerSource(
148 const scoped_refptr<media::AudioCapturerSource>& source,
149 media::ChannelLayout channel_layout,
150 float sample_rate);
152 // Starts recording audio.
153 // Triggered by AddSink() on the main render thread or a Libjingle working
154 // thread. It should NOT be called under |lock_|.
155 void Start();
157 // Helper function to get the buffer size based on |peer_connection_mode_|
158 // and sample rate;
159 int GetBufferSize(int sample_rate) const;
161 // Used to DCHECK that we are called on the correct thread.
162 base::ThreadChecker thread_checker_;
164 // Protects |source_|, |audio_tracks_|, |running_|, |loopback_fifo_|,
165 // |params_| and |buffering_|.
166 mutable base::Lock lock_;
168 // A tagged list of audio tracks that the audio data is fed
169 // to. Tagged items need to be notified that the audio format has
170 // changed.
171 TrackList tracks_;
173 // The audio data source from the browser process.
174 scoped_refptr<media::AudioCapturerSource> source_;
176 // Cached audio constraints for the capturer.
177 blink::WebMediaConstraints constraints_;
179 // Audio processor doing processing like FIFO, AGC, AEC and NS. Its output
180 // data is in a unit of 10 ms data chunk.
181 scoped_refptr<MediaStreamAudioProcessor> audio_processor_;
183 bool running_;
185 int render_view_id_;
187 // Cached information of the device used by the capturer.
188 const StreamDeviceInfo device_info_;
190 // Stores latest microphone volume received in a CaptureData() callback.
191 // Range is [0, 255].
192 int volume_;
194 // Flag which affects the buffer size used by the capturer.
195 bool peer_connection_mode_;
197 // Raw pointer to the WebRtcAudioDeviceImpl, which is valid for the lifetime
198 // of RenderThread.
199 WebRtcAudioDeviceImpl* audio_device_;
201 // Raw pointer to the MediaStreamAudioSource object that holds a reference
202 // to this WebRtcAudioCapturer.
203 // Since |audio_source_| is owned by a blink::WebMediaStreamSource object and
204 // blink guarantees that the blink::WebMediaStreamSource outlives any
205 // blink::WebMediaStreamTrack connected to the source, |audio_source_| is
206 // guaranteed to exist as long as a WebRtcLocalAudioTrack is connected to this
207 // WebRtcAudioCapturer.
208 MediaStreamAudioSource* const audio_source_;
210 DISALLOW_COPY_AND_ASSIGN(WebRtcAudioCapturer);
213 } // namespace content
215 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_