Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(596)

Side by Side Diff: content/renderer/media/webrtc_audio_capturer.h

Issue 133903004: Cleaned up the WebRtcAudioCapturer a bit. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: addressed Per's comments. Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "base/time/time.h" 15 #include "base/time/time.h"
16 #include "content/common/media/media_stream_options.h"
16 #include "content/renderer/media/tagged_list.h" 17 #include "content/renderer/media/tagged_list.h"
17 #include "content/renderer/media/webrtc_audio_device_impl.h"
18 #include "media/audio/audio_input_device.h" 18 #include "media/audio/audio_input_device.h"
19 #include "media/base/audio_capturer_source.h" 19 #include "media/base/audio_capturer_source.h"
20 20
21 namespace media { 21 namespace media {
22 class AudioBus; 22 class AudioBus;
23 } 23 }
24 24
25 namespace content { 25 namespace content {
26 26
27 class WebRtcAudioDeviceImpl;
27 class WebRtcLocalAudioRenderer; 28 class WebRtcLocalAudioRenderer;
28 class WebRtcLocalAudioTrack; 29 class WebRtcLocalAudioTrack;
29 30
30 // This class manages the capture data flow by getting data from its 31 // This class manages the capture data flow by getting data from its
31 // |source_|, and passing it to its |tracks_|. 32 // |source_|, and passing it to its |tracks_|.
32 // It allows clients to inject their own capture data source by calling
33 // SetCapturerSource().
34 // The threading model for this class is rather complex since it will be 33 // The threading model for this class is rather complex since it will be
35 // created on the main render thread, captured data is provided on a dedicated 34 // created on the main render thread, captured data is provided on a dedicated
36 // AudioInputDevice thread, and methods can be called either on the Libjingle 35 // AudioInputDevice thread, and methods can be called either on the Libjingle
37 // thread or on the main render thread but also other client threads 36 // thread or on the main render thread but also other client threads
38 // if an alternative AudioCapturerSource has been set. 37 // if an alternative AudioCapturerSource has been set.
39 class CONTENT_EXPORT WebRtcAudioCapturer 38 class CONTENT_EXPORT WebRtcAudioCapturer
40 : public base::RefCountedThreadSafe<WebRtcAudioCapturer>, 39 : public base::RefCountedThreadSafe<WebRtcAudioCapturer>,
41 NON_EXPORTED_BASE(public media::AudioCapturerSource::CaptureCallback) { 40 NON_EXPORTED_BASE(public media::AudioCapturerSource::CaptureCallback) {
42 public: 41 public:
43 // Use to construct the audio capturer. 42 // Used to construct the audio capturer. |render_view_id| specifies the
43 // render view consuming audio for capture. |device_info| contains all the
44 // device information that the capturer is created for.
44 // Called on the main render thread. 45 // Called on the main render thread.
45 static scoped_refptr<WebRtcAudioCapturer> CreateCapturer(); 46 static scoped_refptr<WebRtcAudioCapturer> CreateCapturer(
46 47 int render_view_id,
47 // Creates and configures the default audio capturing source using the 48 const StreamDeviceInfo& device_info,
48 // provided audio parameters. |render_view_id| specifies the render view 49 WebRtcAudioDeviceImpl* audio_device);
perkj_chrome 2014/01/14 08:42:40 Here you already have the audio_device. So why can
no longer working on chromium 2014/01/14 11:10:21 Yes, we are doing that.
49 // consuming audio for capture. |session_id| is passed to the browser to
50 // decide which device to use. |device_id| is used to identify which device
51 // the capturer is created for. Called on the main render thread.
52 bool Initialize(int render_view_id,
53 media::ChannelLayout channel_layout,
54 int sample_rate,
55 int buffer_size,
56 int session_id,
57 const std::string& device_id,
58 int paired_output_sample_rate,
59 int paired_output_frames_per_buffer,
60 int effects);
61 50
62 // Add a audio track to the sinks of the capturer. 51 // Add a audio track to the sinks of the capturer.
63 // WebRtcAudioDeviceImpl calls this method on the main render thread but 52 // WebRtcAudioDeviceImpl calls this method on the main render thread but
64 // other clients may call it from other threads. The current implementation 53 // other clients may call it from other threads. The current implementation
65 // does not support multi-thread calling. 54 // does not support multi-thread calling.
66 // The first AddTrack will implicitly trigger the Start() of this object. 55 // The first AddTrack will implicitly trigger the Start() of this object.
67 // Called on the main render thread or libjingle working thread. 56 // Called on the main render thread or libjingle working thread.
68 void AddTrack(WebRtcLocalAudioTrack* track); 57 void AddTrack(WebRtcLocalAudioTrack* track);
69 58
70 // Remove a audio track from the sinks of the capturer. 59 // Remove a audio track from the sinks of the capturer.
71 // If the track has been added to the capturer, it must call RemoveTrack() 60 // If the track has been added to the capturer, it must call RemoveTrack()
72 // before it goes away. 61 // before it goes away.
73 // Called on the main render thread or libjingle working thread. 62 // Called on the main render thread or libjingle working thread.
74 void RemoveTrack(WebRtcLocalAudioTrack* track); 63 void RemoveTrack(WebRtcLocalAudioTrack* track);
75 64
76 // SetCapturerSource() is called if the client on the source side desires to
77 // provide their own captured audio data. Client is responsible for calling
78 // Start() on its own source to have the ball rolling.
79 // Called on the main render thread.
80 void SetCapturerSource(
81 const scoped_refptr<media::AudioCapturerSource>& source,
82 media::ChannelLayout channel_layout,
83 float sample_rate,
84 int effects);
85
86 // Called when a stream is connecting to a peer connection. This will set 65 // Called when a stream is connecting to a peer connection. This will set
87 // up the native buffer size for the stream in order to optimize the 66 // up the native buffer size for the stream in order to optimize the
88 // performance for peer connection. 67 // performance for peer connection.
89 void EnablePeerConnectionMode(); 68 void EnablePeerConnectionMode();
90 69
91 // Volume APIs used by WebRtcAudioDeviceImpl. 70 // Volume APIs used by WebRtcAudioDeviceImpl.
92 // Called on the AudioInputDevice audio thread. 71 // Called on the AudioInputDevice audio thread.
93 void SetVolume(int volume); 72 void SetVolume(int volume);
94 int Volume() const; 73 int Volume() const;
95 int MaxVolume() const; 74 int MaxVolume() const;
96 75
97 bool is_recording() const { return running_; }
98
99 // Audio parameters utilized by the audio capturer. Can be utilized by 76 // Audio parameters utilized by the audio capturer. Can be utilized by
100 // a local renderer to set up a renderer using identical parameters as the 77 // a local renderer to set up a renderer using identical parameters as the
101 // capturer. 78 // capturer.
102 // TODO(phoglund): This accessor is inherently unsafe since the returned 79 // TODO(phoglund): This accessor is inherently unsafe since the returned
103 // parameters can become outdated at any time. Think over the implications 80 // parameters can become outdated at any time. Think over the implications
104 // of this accessor and if we can remove it. 81 // of this accessor and if we can remove it.
105 media::AudioParameters audio_parameters() const; 82 media::AudioParameters audio_parameters() const;
106 83
107 // Gets information about the paired output device. Returns true if such a 84 // Gets information about the paired output device. Returns true if such a
108 // device exists. 85 // device exists.
109 bool GetPairedOutputParameters(int* session_id, 86 bool GetPairedOutputParameters(int* session_id,
110 int* output_sample_rate, 87 int* output_sample_rate,
111 int* output_frames_per_buffer) const; 88 int* output_frames_per_buffer) const;
112 89
113 const std::string& device_id() const { return device_id_; } 90 const std::string& device_id() const { return device_info_.device.id; }
114 int session_id() const { return session_id_; } 91 int session_id() const { return device_info_.session_id; }
115 92
116 // Stops recording audio. This method will empty its track lists since 93 // Stops recording audio. This method will empty its track lists since
117 // stopping the capturer will implicitly invalidate all its tracks. 94 // stopping the capturer will implicitly invalidate all its tracks.
118 // This method is exposed to the public because the media stream track can 95 // This method is exposed to the public because the media stream track can
119 // call Stop() on its source. 96 // call Stop() on its source.
120 void Stop(); 97 void Stop();
121 98
122 // Called by the WebAudioCapturerSource to get the audio processing params. 99 // Called by the WebAudioCapturerSource to get the audio processing params.
123 // This function is triggered by provideInput() on the WebAudio audio thread, 100 // This function is triggered by provideInput() on the WebAudio audio thread,
124 // TODO(xians): Remove after moving APM from WebRtc to Chrome. 101 // TODO(xians): Remove after moving APM from WebRtc to Chrome.
125 void GetAudioProcessingParams(base::TimeDelta* delay, int* volume, 102 void GetAudioProcessingParams(base::TimeDelta* delay, int* volume,
126 bool* key_pressed); 103 bool* key_pressed);
127 104
105 // Use by the unittests to inject their own source to the capturer.
106 void SetCapturerSourceForTesting(
107 const scoped_refptr<media::AudioCapturerSource>& source,
108 media::AudioParameters params);
109
128 protected: 110 protected:
129 friend class base::RefCountedThreadSafe<WebRtcAudioCapturer>; 111 friend class base::RefCountedThreadSafe<WebRtcAudioCapturer>;
130 WebRtcAudioCapturer();
131 virtual ~WebRtcAudioCapturer(); 112 virtual ~WebRtcAudioCapturer();
132 113
133 private: 114 private:
134 class TrackOwner; 115 class TrackOwner;
135 typedef TaggedList<TrackOwner> TrackList; 116 typedef TaggedList<TrackOwner> TrackList;
136 117
118 WebRtcAudioCapturer(int render_view_id,
119 const StreamDeviceInfo& device_info,
120 WebRtcAudioDeviceImpl* audio_device);
121
137 // AudioCapturerSource::CaptureCallback implementation. 122 // AudioCapturerSource::CaptureCallback implementation.
138 // Called on the AudioInputDevice audio thread. 123 // Called on the AudioInputDevice audio thread.
139 virtual void Capture(media::AudioBus* audio_source, 124 virtual void Capture(media::AudioBus* audio_source,
140 int audio_delay_milliseconds, 125 int audio_delay_milliseconds,
141 double volume, 126 double volume,
142 bool key_pressed) OVERRIDE; 127 bool key_pressed) OVERRIDE;
143 virtual void OnCaptureError() OVERRIDE; 128 virtual void OnCaptureError() OVERRIDE;
144 129
130 // Initializes the default audio capturing source using the provided render
131 // view id and device information. Return true if success, otherwise false.
132 bool Initialize();
133
134 // SetCapturerSource() is called if the client on the source side desires to
135 // provide their own captured audio data. Client is responsible for calling
136 // Start() on its own source to have the ball rolling.
137 // Called on the main render thread.
138 void SetCapturerSource(
139 const scoped_refptr<media::AudioCapturerSource>& source,
140 media::ChannelLayout channel_layout,
141 float sample_rate,
142 int effects);
143
145 // Reconfigures the capturer with a new capture parameters. 144 // Reconfigures the capturer with a new capture parameters.
146 // Must be called without holding the lock. 145 // Must be called without holding the lock.
147 void Reconfigure(int sample_rate, media::ChannelLayout channel_layout, 146 void Reconfigure(int sample_rate, media::ChannelLayout channel_layout,
148 int effects); 147 int effects);
149 148
150 // Starts recording audio. 149 // Starts recording audio.
151 // Triggered by AddSink() on the main render thread or a Libjingle working 150 // Triggered by AddSink() on the main render thread or a Libjingle working
152 // thread. It should NOT be called under |lock_|. 151 // thread. It should NOT be called under |lock_|.
153 void Start(); 152 void Start();
154 153
(...skipping 16 matching lines...) Expand all
171 // The audio data source from the browser process. 170 // The audio data source from the browser process.
172 scoped_refptr<media::AudioCapturerSource> source_; 171 scoped_refptr<media::AudioCapturerSource> source_;
173 172
174 // Cached audio parameters for output. 173 // Cached audio parameters for output.
175 media::AudioParameters params_; 174 media::AudioParameters params_;
176 175
177 bool running_; 176 bool running_;
178 177
179 int render_view_id_; 178 int render_view_id_;
180 179
181 // Cached value for the hardware native buffer size, used when 180 // Cached information of the device used by the capturer.
182 // |peer_connection_mode_| is set to false. 181 const StreamDeviceInfo device_info_;
183 int hardware_buffer_size_;
184
185 // The media session ID used to identify which input device to be started by
186 // the browser.
187 int session_id_;
188
189 // The device this capturer is given permission to use.
190 std::string device_id_;
191 182
192 // Stores latest microphone volume received in a CaptureData() callback. 183 // Stores latest microphone volume received in a CaptureData() callback.
193 // Range is [0, 255]. 184 // Range is [0, 255].
194 int volume_; 185 int volume_;
195 186
196 // Flag which affects the buffer size used by the capturer. 187 // Flag which affects the buffer size used by the capturer.
197 bool peer_connection_mode_; 188 bool peer_connection_mode_;
198 189
199 int output_sample_rate_;
200 int output_frames_per_buffer_;
201
202 // Cache value for the audio processing params. 190 // Cache value for the audio processing params.
203 base::TimeDelta audio_delay_; 191 base::TimeDelta audio_delay_;
204 bool key_pressed_; 192 bool key_pressed_;
205 193
194 // Raw pointer to the WebRtcAudioDeviceImpl.
perkj_chrome 2014/01/14 08:42:40 Explain how this can be safe. ie - what is the lif
no longer working on chromium 2014/01/14 11:10:21 Done.
195 WebRtcAudioDeviceImpl* audio_device_;
196
206 DISALLOW_COPY_AND_ASSIGN(WebRtcAudioCapturer); 197 DISALLOW_COPY_AND_ASSIGN(WebRtcAudioCapturer);
207 }; 198 };
208 199
209 } // namespace content 200 } // namespace content
210 201
211 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_ 202 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698