OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_ | 5 #ifndef CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_ |
6 #define CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_ | 6 #define CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/synchronization/lock.h" | 13 #include "base/synchronization/lock.h" |
14 #include "base/threading/thread_checker.h" | 14 #include "base/threading/thread_checker.h" |
15 #include "content/renderer/media/webrtc_audio_device_impl.h" | 15 #include "content/renderer/media/webrtc_audio_device_impl.h" |
| 16 #include "content/renderer/media/webrtc_local_audio_source_provider.h" |
16 #include "media/audio/audio_input_device.h" | 17 #include "media/audio/audio_input_device.h" |
17 #include "media/base/audio_capturer_source.h" | 18 #include "media/base/audio_capturer_source.h" |
18 | 19 |
19 namespace media { | 20 namespace media { |
20 class AudioBus; | 21 class AudioBus; |
21 } | 22 } |
22 | 23 |
23 namespace content { | 24 namespace content { |
24 | 25 |
25 class WebRtcLocalAudioRenderer; | 26 class WebRtcLocalAudioRenderer; |
(...skipping 17 matching lines...) Expand all Loading... |
43 static scoped_refptr<WebRtcAudioCapturer> CreateCapturer(); | 44 static scoped_refptr<WebRtcAudioCapturer> CreateCapturer(); |
44 | 45 |
45 // Creates and configures the default audio capturing source using the | 46 // Creates and configures the default audio capturing source using the |
46 // provided audio parameters. |render_view_id| specifies the render view | 47 // provided audio parameters. |render_view_id| specifies the render view |
47 // consuming audio for capture. |session_id| is passed to the browser to | 48 // consuming audio for capture. |session_id| is passed to the browser to |
48 // decide which device to use. |device_id| is used to identify which device | 49 // decide which device to use. |device_id| is used to identify which device |
49 // the capturer is created for. Called on the main render thread. | 50 // the capturer is created for. Called on the main render thread. |
50 bool Initialize(int render_view_id, | 51 bool Initialize(int render_view_id, |
51 media::ChannelLayout channel_layout, | 52 media::ChannelLayout channel_layout, |
52 int sample_rate, | 53 int sample_rate, |
| 54 int buffer_size, |
53 int session_id, | 55 int session_id, |
54 const std::string& device_id); | 56 const std::string& device_id); |
55 | 57 |
56 // Add a audio track to the sinks of the capturer. | 58 // Add a audio track to the sinks of the capturer. |
57 // WebRtcAudioDeviceImpl calls this method on the main render thread but | 59 // WebRtcAudioDeviceImpl calls this method on the main render thread but |
58 // other clients may call it from other threads. The current implementation | 60 // other clients may call it from other threads. The current implementation |
59 // does not support multi-thread calling. | 61 // does not support multi-thread calling. |
60 // Called on the main render thread or libjingle working thread. | 62 // Called on the main render thread or libjingle working thread. |
61 void AddTrack(WebRtcLocalAudioTrack* track); | 63 void AddTrack(WebRtcLocalAudioTrack* track); |
62 | 64 |
63 // Remove a audio track from the sinks of the capturer. | 65 // Remove a audio track from the sinks of the capturer. |
64 // Called on the main render thread or libjingle working thread. | 66 // Called on the main render thread or libjingle working thread. |
65 void RemoveTrack(WebRtcLocalAudioTrack* track); | 67 void RemoveTrack(WebRtcLocalAudioTrack* track); |
66 | 68 |
67 // SetCapturerSource() is called if the client on the source side desires to | 69 // SetCapturerSource() is called if the client on the source side desires to |
68 // provide their own captured audio data. Client is responsible for calling | 70 // provide their own captured audio data. Client is responsible for calling |
69 // Start() on its own source to have the ball rolling. | 71 // Start() on its own source to have the ball rolling. |
70 // Called on the main render thread. | 72 // Called on the main render thread. |
71 void SetCapturerSource( | 73 void SetCapturerSource( |
72 const scoped_refptr<media::AudioCapturerSource>& source, | 74 const scoped_refptr<media::AudioCapturerSource>& source, |
73 media::ChannelLayout channel_layout, | 75 media::ChannelLayout channel_layout, |
74 float sample_rate); | 76 float sample_rate); |
75 | 77 |
| 78 // Called when a stream is connecting to a peer connection. This will set |
| 79 // up the native buffer size for the stream in order to optimize the |
| 80 // performance for peer connection. |
| 81 void EnablePeerConnectionMode(); |
| 82 |
76 // Volume APIs used by WebRtcAudioDeviceImpl. | 83 // Volume APIs used by WebRtcAudioDeviceImpl. |
77 // Called on the AudioInputDevice audio thread. | 84 // Called on the AudioInputDevice audio thread. |
78 void SetVolume(int volume); | 85 void SetVolume(int volume); |
79 int Volume() const; | 86 int Volume() const; |
80 int MaxVolume() const; | 87 int MaxVolume() const; |
81 | 88 |
82 // Enables or disables the WebRtc AGC control. | 89 // Enables or disables the WebRtc AGC control. |
83 // Called from a Libjingle working thread. | 90 // Called from a Libjingle working thread. |
84 void SetAutomaticGainControl(bool enable); | 91 void SetAutomaticGainControl(bool enable); |
85 | 92 |
86 bool is_recording() const { return running_; } | 93 bool is_recording() const { return running_; } |
87 | 94 |
88 // Audio parameters utilized by the audio capturer. Can be utilized by | 95 // Audio parameters utilized by the audio capturer. Can be utilized by |
89 // a local renderer to set up a renderer using identical parameters as the | 96 // a local renderer to set up a renderer using identical parameters as the |
90 // capturer. | 97 // capturer. |
91 // TODO(phoglund): This accessor is inherently unsafe since the returned | 98 // TODO(phoglund): This accessor is inherently unsafe since the returned |
92 // parameters can become outdated at any time. Think over the implications | 99 // parameters can become outdated at any time. Think over the implications |
93 // of this accessor and if we can remove it. | 100 // of this accessor and if we can remove it. |
94 media::AudioParameters audio_parameters() const; | 101 media::AudioParameters audio_parameters() const; |
95 | 102 |
96 const std::string& device_id() const { return device_id_; } | 103 const std::string& device_id() const { return device_id_; } |
97 | 104 |
| 105 WebKit::WebAudioSourceProvider* audio_source_provider() const { |
| 106 return source_provider_.get(); |
| 107 } |
| 108 |
98 protected: | 109 protected: |
99 friend class base::RefCountedThreadSafe<WebRtcAudioCapturer>; | 110 friend class base::RefCountedThreadSafe<WebRtcAudioCapturer>; |
100 WebRtcAudioCapturer(); | 111 WebRtcAudioCapturer(); |
101 virtual ~WebRtcAudioCapturer(); | 112 virtual ~WebRtcAudioCapturer(); |
102 | 113 |
103 private: | 114 private: |
104 class TrackOwner; | 115 class TrackOwner; |
105 typedef std::list<scoped_refptr<TrackOwner> > TrackList; | 116 typedef std::list<scoped_refptr<TrackOwner> > TrackList; |
106 | 117 |
107 // AudioCapturerSource::CaptureCallback implementation. | 118 // AudioCapturerSource::CaptureCallback implementation. |
108 // Called on the AudioInputDevice audio thread. | 119 // Called on the AudioInputDevice audio thread. |
109 virtual void Capture(media::AudioBus* audio_source, | 120 virtual void Capture(media::AudioBus* audio_source, |
110 int audio_delay_milliseconds, | 121 int audio_delay_milliseconds, |
111 double volume, | 122 double volume, |
112 bool key_pressed) OVERRIDE; | 123 bool key_pressed) OVERRIDE; |
113 virtual void OnCaptureError() OVERRIDE; | 124 virtual void OnCaptureError() OVERRIDE; |
114 | 125 |
115 // Reconfigures the capturer with a new buffer size and capture parameters. | 126 // Reconfigures the capturer with a new capture parameters. |
116 // Must be called without holding the lock. Returns true on success. | 127 // Must be called without holding the lock. |
117 bool Reconfigure(int sample_rate, media::ChannelLayout channel_layout); | 128 void Reconfigure(int sample_rate, media::ChannelLayout channel_layout); |
118 | 129 |
119 // Starts recording audio. | 130 // Starts recording audio. |
120 // Triggered by AddSink() on the main render thread or a Libjingle working | 131 // Triggered by AddSink() on the main render thread or a Libjingle working |
121 // thread. It should NOT be called under |lock_|. | 132 // thread. It should NOT be called under |lock_|. |
122 void Start(); | 133 void Start(); |
123 | 134 |
124 // Stops recording audio. | 135 // Stops recording audio. |
125 // Triggered by RemoveSink() on the main render thread or a Libjingle working | 136 // Triggered by RemoveSink() on the main render thread or a Libjingle working |
126 // thread. It should NOT be called under |lock_|. | 137 // thread. It should NOT be called under |lock_|. |
127 void Stop(); | 138 void Stop(); |
128 | 139 |
| 140 // Helper function to get the buffer size based on |peer_connection_mode_| |
| 141 // and sample rate; |
| 142 int GetBufferSize(int sample_rate) const; |
129 | 143 |
130 // Used to DCHECK that we are called on the correct thread. | 144 // Used to DCHECK that we are called on the correct thread. |
131 base::ThreadChecker thread_checker_; | 145 base::ThreadChecker thread_checker_; |
132 | 146 |
133 // Protects |source_|, |audio_tracks_|, |running_|, |loopback_fifo_|, | 147 // Protects |source_|, |audio_tracks_|, |running_|, |loopback_fifo_|, |
134 // |params_|, |buffering_| and |agc_is_enabled_|. | 148 // |params_|, |buffering_| and |agc_is_enabled_|. |
135 mutable base::Lock lock_; | 149 mutable base::Lock lock_; |
136 | 150 |
137 // A list of audio tracks that the audio data is fed to. | 151 // A list of audio tracks that the audio data is fed to. |
138 TrackList tracks_; | 152 TrackList tracks_; |
139 | 153 |
140 // The audio data source from the browser process. | 154 // The audio data source from the browser process. |
141 scoped_refptr<media::AudioCapturerSource> source_; | 155 scoped_refptr<media::AudioCapturerSource> source_; |
142 | 156 |
143 // Buffers used for temporary storage during capture callbacks. | 157 // Cached audio parameters for output. |
144 // Allocated during initialization. | 158 media::AudioParameters params_; |
145 class ConfiguredBuffer; | 159 |
146 scoped_refptr<ConfiguredBuffer> buffer_; | |
147 bool running_; | 160 bool running_; |
148 | 161 |
149 // True when automatic gain control is enabled, false otherwise. | 162 // True when automatic gain control is enabled, false otherwise. |
150 bool agc_is_enabled_; | 163 bool agc_is_enabled_; |
151 | 164 |
| 165 int render_view_id_; |
| 166 |
| 167 // Cached value for the hardware native buffer size, used when |
| 168 // |peer_connection_mode_| is set to false. |
| 169 int hardware_buffer_size_; |
| 170 |
152 // The media session ID used to identify which input device to be started by | 171 // The media session ID used to identify which input device to be started by |
153 // the browser. | 172 // the browser. |
154 int session_id_; | 173 int session_id_; |
155 | 174 |
156 // The device this capturer is given permission to use. | 175 // The device this capturer is given permission to use. |
157 std::string device_id_; | 176 std::string device_id_; |
158 | 177 |
159 // Stores latest microphone volume received in a CaptureData() callback. | 178 // Stores latest microphone volume received in a CaptureData() callback. |
160 // Range is [0, 255]. | 179 // Range is [0, 255]. |
161 int volume_; | 180 int volume_; |
162 | 181 |
| 182 // The source provider to feed the capture data to other clients like |
| 183 // WebAudio. |
| 184 // TODO(xians): Move the source provider to track once we don't need to feed |
| 185 // delay, volume, key_pressed information to WebAudioCapturerSource. |
| 186 const scoped_ptr<WebRtcLocalAudioSourceProvider> source_provider_; |
| 187 |
| 188 // Flag which affects the buffer size used by the capturer. |
| 189 bool peer_connection_mode_; |
| 190 |
163 DISALLOW_COPY_AND_ASSIGN(WebRtcAudioCapturer); | 191 DISALLOW_COPY_AND_ASSIGN(WebRtcAudioCapturer); |
164 }; | 192 }; |
165 | 193 |
166 } // namespace content | 194 } // namespace content |
167 | 195 |
168 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_ | 196 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_ |
OLD | NEW |