OLD | NEW |
| (Empty) |
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/files/file.h" | |
13 #include "base/macros.h" | |
14 #include "base/memory/ref_counted.h" | |
15 #include "base/synchronization/lock.h" | |
16 #include "base/threading/thread_checker.h" | |
17 #include "base/time/time.h" | |
18 #include "content/common/media/media_stream_options.h" | |
19 #include "content/renderer/media/tagged_list.h" | |
20 #include "media/audio/audio_input_device.h" | |
21 #include "media/base/audio_capturer_source.h" | |
22 #include "third_party/WebKit/public/platform/WebMediaConstraints.h" | |
23 | |
24 namespace media { | |
25 class AudioBus; | |
26 } | |
27 | |
28 namespace content { | |
29 | |
30 class MediaStreamAudioProcessor; | |
31 class MediaStreamAudioSource; | |
32 class WebRtcAudioDeviceImpl; | |
33 class WebRtcLocalAudioRenderer; | |
34 class WebRtcLocalAudioTrack; | |
35 | |
36 // This class manages the capture data flow by getting data from its | |
37 // |source_|, and passing it to its |tracks_|. | |
38 // The threading model for this class is rather complex since it will be | |
39 // created on the main render thread, captured data is provided on a dedicated | |
40 // AudioInputDevice thread, and methods can be called either on the Libjingle | |
41 // thread or on the main render thread but also other client threads | |
42 // if an alternative AudioCapturerSource has been set. | |
43 class CONTENT_EXPORT WebRtcAudioCapturer | |
44 : public base::RefCountedThreadSafe<WebRtcAudioCapturer>, | |
45 NON_EXPORTED_BASE(public media::AudioCapturerSource::CaptureCallback) { | |
46 public: | |
47 // Used to construct the audio capturer. |render_frame_id| specifies the | |
48 // RenderFrame consuming audio for capture; -1 is used for tests. | |
49 // |device_info| contains all the device information that the capturer is | |
50 // created for. |constraints| contains the settings for audio processing. | |
51 // TODO(xians): Implement the interface for the audio source and move the | |
52 // |constraints| to ApplyConstraints(). Called on the main render thread. | |
53 static scoped_refptr<WebRtcAudioCapturer> CreateCapturer( | |
54 int render_frame_id, | |
55 const StreamDeviceInfo& device_info, | |
56 const blink::WebMediaConstraints& constraints, | |
57 WebRtcAudioDeviceImpl* audio_device, | |
58 MediaStreamAudioSource* audio_source); | |
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 void AddTrack(WebRtcLocalAudioTrack* track); | |
66 | |
67 // Remove a audio track from the sinks of the capturer. | |
68 // If the track has been added to the capturer, it must call RemoveTrack() | |
69 // before it goes away. | |
70 // Called on the main render thread or libjingle working thread. | |
71 void RemoveTrack(WebRtcLocalAudioTrack* track); | |
72 | |
73 // Called when a stream is connecting to a peer connection. This will set | |
74 // up the native buffer size for the stream in order to optimize the | |
75 // performance for peer connection. | |
76 void EnablePeerConnectionMode(); | |
77 | |
78 // Volume APIs used by WebRtcAudioDeviceImpl. | |
79 // Called on the AudioInputDevice audio thread. | |
80 void SetVolume(int volume); | |
81 int Volume() const; | |
82 int MaxVolume() const; | |
83 | |
84 // Audio parameters utilized by the source of the audio capturer. | |
85 // TODO(phoglund): Think over the implications of this accessor and if we can | |
86 // remove it. | |
87 media::AudioParameters source_audio_parameters() const; | |
88 | |
89 // Gets information about the paired output device. Returns true if such a | |
90 // device exists. | |
91 bool GetPairedOutputParameters(int* session_id, | |
92 int* output_sample_rate, | |
93 int* output_frames_per_buffer) const; | |
94 | |
95 const std::string& device_id() const { return device_info_.device.id; } | |
96 int session_id() const { return device_info_.session_id; } | |
97 | |
98 // Stops recording audio. This method will empty its track lists since | |
99 // stopping the capturer will implicitly invalidate all its tracks. | |
100 // This method is exposed to the public because the MediaStreamAudioSource can | |
101 // call Stop() | |
102 void Stop(); | |
103 | |
104 // Returns the output format. | |
105 // Called on the main render thread. | |
106 media::AudioParameters GetOutputFormat() const; | |
107 | |
108 // Used by clients to inject their own source to the capturer. | |
109 void SetCapturerSource( | |
110 const scoped_refptr<media::AudioCapturerSource>& source, | |
111 media::AudioParameters params); | |
112 | |
113 protected: | |
114 friend class base::RefCountedThreadSafe<WebRtcAudioCapturer>; | |
115 ~WebRtcAudioCapturer() override; | |
116 | |
117 private: | |
118 class TrackOwner; | |
119 typedef TaggedList<TrackOwner> TrackList; | |
120 | |
121 WebRtcAudioCapturer(int render_frame_id, | |
122 const StreamDeviceInfo& device_info, | |
123 const blink::WebMediaConstraints& constraints, | |
124 WebRtcAudioDeviceImpl* audio_device, | |
125 MediaStreamAudioSource* audio_source); | |
126 | |
127 // AudioCapturerSource::CaptureCallback implementation. | |
128 // Called on the AudioInputDevice audio thread. | |
129 void Capture(const media::AudioBus* audio_source, | |
130 int audio_delay_milliseconds, | |
131 double volume, | |
132 bool key_pressed) override; | |
133 void OnCaptureError(const std::string& message) override; | |
134 | |
135 // Initializes the default audio capturing source using the provided render | |
136 // frame id and device information. Return true if success, otherwise false. | |
137 bool Initialize(); | |
138 | |
139 // SetCapturerSourceInternal() is called if the client on the source side | |
140 // desires to provide their own captured audio data. Client is responsible | |
141 // for calling Start() on its own source to get the ball rolling. | |
142 // Called on the main render thread. | |
143 // buffer_size is optional. Set to 0 to let it be chosen automatically. | |
144 void SetCapturerSourceInternal( | |
145 const scoped_refptr<media::AudioCapturerSource>& source, | |
146 media::ChannelLayout channel_layout, | |
147 int sample_rate, | |
148 int buffer_size); | |
149 | |
150 // Starts recording audio. | |
151 // Triggered by AddSink() on the main render thread or a Libjingle working | |
152 // thread. It should NOT be called under |lock_|. | |
153 void Start(); | |
154 | |
155 // Helper function to get the buffer size based on |peer_connection_mode_| | |
156 // and sample rate; | |
157 int GetBufferSize(int sample_rate) const; | |
158 | |
159 // Used to DCHECK that we are called on the correct thread. | |
160 base::ThreadChecker thread_checker_; | |
161 | |
162 // Protects |source_|, |audio_tracks_|, |running_|, |loopback_fifo_|, | |
163 // |params_| and |buffering_|. | |
164 mutable base::Lock lock_; | |
165 | |
166 // A tagged list of audio tracks that the audio data is fed | |
167 // to. Tagged items need to be notified that the audio format has | |
168 // changed. | |
169 TrackList tracks_; | |
170 | |
171 // The audio data source from the browser process. | |
172 scoped_refptr<media::AudioCapturerSource> source_; | |
173 | |
174 // Cached audio constraints for the capturer. | |
175 blink::WebMediaConstraints constraints_; | |
176 | |
177 // Audio processor doing processing like FIFO, AGC, AEC and NS. Its output | |
178 // data is in a unit of 10 ms data chunk. | |
179 scoped_refptr<MediaStreamAudioProcessor> audio_processor_; | |
180 | |
181 bool running_; | |
182 | |
183 int render_frame_id_; | |
184 | |
185 // Cached information of the device used by the capturer. | |
186 const StreamDeviceInfo device_info_; | |
187 | |
188 // Stores latest microphone volume received in a CaptureData() callback. | |
189 // Range is [0, 255]. | |
190 int volume_; | |
191 | |
192 // Flag which affects the buffer size used by the capturer. | |
193 bool peer_connection_mode_; | |
194 | |
195 // Raw pointer to the WebRtcAudioDeviceImpl, which is valid for the lifetime | |
196 // of RenderThread. | |
197 WebRtcAudioDeviceImpl* audio_device_; | |
198 | |
199 // Raw pointer to the MediaStreamAudioSource object that holds a reference | |
200 // to this WebRtcAudioCapturer. | |
201 // Since |audio_source_| is owned by a blink::WebMediaStreamSource object and | |
202 // blink guarantees that the blink::WebMediaStreamSource outlives any | |
203 // blink::WebMediaStreamTrack connected to the source, |audio_source_| is | |
204 // guaranteed to exist as long as a WebRtcLocalAudioTrack is connected to this | |
205 // WebRtcAudioCapturer. | |
206 MediaStreamAudioSource* const audio_source_; | |
207 | |
208 DISALLOW_COPY_AND_ASSIGN(WebRtcAudioCapturer); | |
209 }; | |
210 | |
211 } // namespace content | |
212 | |
213 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_ | |
OLD | NEW |