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 |
(...skipping 15 matching lines...) Expand all Loading... |
26 class WebRtcLocalAudioRenderer; | 26 class WebRtcLocalAudioRenderer; |
27 | 27 |
28 // This class manages the capture data flow by getting data from its | 28 // This class manages the capture data flow by getting data from its |
29 // |source_|, and passing it to its |sink_|. | 29 // |source_|, and passing it to its |sink_|. |
30 // It allows clients to inject their own capture data source by calling | 30 // It allows clients to inject their own capture data source by calling |
31 // SetCapturerSource(). | 31 // SetCapturerSource(). |
32 // The threading model for this class is rather complex since it will be | 32 // The threading model for this class is rather complex since it will be |
33 // created on the main render thread, captured data is provided on a dedicated | 33 // created on the main render thread, captured data is provided on a dedicated |
34 // AudioInputDevice thread, and methods can be called either on the Libjingle | 34 // AudioInputDevice thread, and methods can be called either on the Libjingle |
35 // thread or on the main render thread but also other client threads | 35 // thread or on the main render thread but also other client threads |
36 // if an alternative AudioCapturerSource has been set. In addition, the | 36 // if an alternative AudioCapturerSource has been set. |
37 // AudioCapturerSource::CaptureEventHandler methods are called on the IO thread | |
38 // and requests for data to render is done on the AudioOutputDevice thread. | |
39 class CONTENT_EXPORT WebRtcAudioCapturer | 37 class CONTENT_EXPORT WebRtcAudioCapturer |
40 : public base::RefCountedThreadSafe<WebRtcAudioCapturer>, | 38 : public base::RefCountedThreadSafe<WebRtcAudioCapturer>, |
41 NON_EXPORTED_BASE(public media::AudioCapturerSource::CaptureCallback), | 39 NON_EXPORTED_BASE(public media::AudioCapturerSource::CaptureCallback) { |
42 NON_EXPORTED_BASE( | |
43 public media::AudioCapturerSource::CaptureEventHandler) { | |
44 public: | 40 public: |
45 // Use to construct the audio capturer. | 41 // Use to construct the audio capturer. |
46 // Called on the main render thread. | 42 // Called on the main render thread. |
47 static scoped_refptr<WebRtcAudioCapturer> CreateCapturer(); | 43 static scoped_refptr<WebRtcAudioCapturer> CreateCapturer(); |
48 | 44 |
49 // Creates and configures the default audio capturing source using the | 45 // Creates and configures the default audio capturing source using the |
50 // provided audio parameters. | 46 // provided audio parameters, |session_id| is used to be passed to the |
| 47 // browser to decide which device to use. |
51 // Called on the main render thread. | 48 // Called on the main render thread. |
52 bool Initialize(media::ChannelLayout channel_layout, int sample_rate); | 49 bool Initialize(media::ChannelLayout channel_layout, |
| 50 int sample_rate, |
| 51 int session_id); |
53 | 52 |
54 // Called by the client on the sink side to add a sink. | 53 // Called by the client on the sink side to add a sink. |
55 // WebRtcAudioDeviceImpl calls this method on the main render thread but | 54 // WebRtcAudioDeviceImpl calls this method on the main render thread but |
56 // other clients may call it from other threads. The current implementation | 55 // other clients may call it from other threads. The current implementation |
57 // does not support multi-thread calling. | 56 // does not support multi-thread calling. |
58 // Called on the main render thread. | 57 // Called on the main render thread. |
59 void AddCapturerSink(WebRtcAudioCapturerSink* sink); | 58 void AddCapturerSink(WebRtcAudioCapturerSink* sink); |
60 | 59 |
61 // Called by the client on the sink side to remove a sink. | 60 // Called by the client on the sink side to remove a sink. |
62 // Called on the main render thread. | 61 // Called on the main render thread. |
(...skipping 14 matching lines...) Expand all Loading... |
77 void Start(); | 76 void Start(); |
78 | 77 |
79 // Stops recording audio. | 78 // Stops recording audio. |
80 // Called on the main render thread or a Libjingle working thread. | 79 // Called on the main render thread or a Libjingle working thread. |
81 void Stop(); | 80 void Stop(); |
82 | 81 |
83 // Sets the microphone volume. | 82 // Sets the microphone volume. |
84 // Called on the AudioInputDevice audio thread. | 83 // Called on the AudioInputDevice audio thread. |
85 void SetVolume(double volume); | 84 void SetVolume(double volume); |
86 | 85 |
87 // Specifies the |session_id| to query which device to use. | |
88 // Called on the main render thread. | |
89 void SetDevice(int session_id); | |
90 | |
91 // Enables or disables the WebRtc AGC control. | 86 // Enables or disables the WebRtc AGC control. |
92 // Called from a Libjingle working thread. | 87 // Called from a Libjingle working thread. |
93 void SetAutomaticGainControl(bool enable); | 88 void SetAutomaticGainControl(bool enable); |
94 | 89 |
95 bool is_recording() const { return running_; } | 90 bool is_recording() const { return running_; } |
96 | 91 |
97 // Audio parameters utilized by the audio capturer. Can be utilized by | 92 // Audio parameters utilized by the audio capturer. Can be utilized by |
98 // a local renderer to set up a renderer using identical parameters as the | 93 // a local renderer to set up a renderer using identical parameters as the |
99 // capturer. | 94 // capturer. |
100 // TODO(phoglund): This accessor is inherently unsafe since the returned | 95 // TODO(phoglund): This accessor is inherently unsafe since the returned |
101 // parameters can become outdated at any time. Think over the implications | 96 // parameters can become outdated at any time. Think over the implications |
102 // of this accessor and if we can remove it. | 97 // of this accessor and if we can remove it. |
103 media::AudioParameters audio_parameters() const; | 98 media::AudioParameters audio_parameters() const; |
104 | 99 |
105 // AudioCapturerSource::CaptureCallback implementation. | 100 // AudioCapturerSource::CaptureCallback implementation. |
106 // Called on the AudioInputDevice audio thread. | 101 // Called on the AudioInputDevice audio thread. |
107 virtual void Capture(media::AudioBus* audio_source, | 102 virtual void Capture(media::AudioBus* audio_source, |
108 int audio_delay_milliseconds, | 103 int audio_delay_milliseconds, |
109 double volume) OVERRIDE; | 104 double volume) OVERRIDE; |
110 virtual void OnCaptureError() OVERRIDE; | 105 virtual void OnCaptureError() OVERRIDE; |
111 | 106 |
112 // AudioCapturerSource::CaptureEventHandler implementation. | |
113 // Called on the IO thread. | |
114 virtual void OnDeviceStarted(const std::string& device_id) OVERRIDE; | |
115 virtual void OnDeviceStopped() OVERRIDE; | |
116 | |
117 protected: | 107 protected: |
118 friend class base::RefCountedThreadSafe<WebRtcAudioCapturer>; | 108 friend class base::RefCountedThreadSafe<WebRtcAudioCapturer>; |
119 virtual ~WebRtcAudioCapturer(); | 109 virtual ~WebRtcAudioCapturer(); |
120 | 110 |
121 private: | 111 private: |
122 class SinkOwner; | 112 class SinkOwner; |
123 typedef std::list<scoped_refptr<SinkOwner> > SinkList; | 113 typedef std::list<scoped_refptr<SinkOwner> > SinkList; |
124 | 114 |
125 WebRtcAudioCapturer(); | 115 WebRtcAudioCapturer(); |
126 | 116 |
127 // Reconfigures the capturer with a new buffer size and capture parameters. | 117 // Reconfigures the capturer with a new buffer size and capture parameters. |
128 // Must be called without holding the lock. Returns true on success. | 118 // Must be called without holding the lock. Returns true on success. |
129 bool Reconfigure(int sample_rate, media::ChannelLayout channel_layout); | 119 bool Reconfigure(int sample_rate, media::ChannelLayout channel_layout); |
130 | 120 |
131 // Distributes information about a stopped capture device to all registered | |
132 // capture sinks. | |
133 // Runs on the main render thread. | |
134 void DoOnDeviceStopped(); | |
135 | |
136 // Used to DCHECK that we are called on the correct thread. | 121 // Used to DCHECK that we are called on the correct thread. |
137 base::ThreadChecker thread_checker_; | 122 base::ThreadChecker thread_checker_; |
138 | 123 |
139 // Message loop for the main render thread. Utilized in OnDeviceStopped() to | 124 // Protects |source_|, |sinks_|, |running_|, |loopback_fifo_|, |params_|, |
140 // ensure that OnSourceCaptureDeviceStopped() is called on the main thread | 125 // |buffering_| and |agc_is_enabled_|. |
141 // instead of the originating IO thread. | |
142 scoped_refptr<base::MessageLoopProxy> main_loop_; | |
143 | |
144 // Protects |source_|, |sinks_|, |running_|, |on_device_stopped_cb_|, | |
145 // |loopback_fifo_|, |params_|, |buffering_| and |agc_is_enabled_|. | |
146 mutable base::Lock lock_; | 126 mutable base::Lock lock_; |
147 | 127 |
148 // A list of sinks that the audio data is fed to. | 128 // A list of sinks that the audio data is fed to. |
149 SinkList sinks_; | 129 SinkList sinks_; |
150 | 130 |
151 // The audio data source from the browser process. | 131 // The audio data source from the browser process. |
152 scoped_refptr<media::AudioCapturerSource> source_; | 132 scoped_refptr<media::AudioCapturerSource> source_; |
153 | 133 |
154 // Buffers used for temporary storage during capture callbacks. | 134 // Buffers used for temporary storage during capture callbacks. |
155 // Allocated during initialization. | 135 // Allocated during initialization. |
156 class ConfiguredBuffer; | 136 class ConfiguredBuffer; |
157 scoped_refptr<ConfiguredBuffer> buffer_; | 137 scoped_refptr<ConfiguredBuffer> buffer_; |
158 std::string device_id_; | |
159 bool running_; | 138 bool running_; |
160 | 139 |
161 // True when automatic gain control is enabled, false otherwise. | 140 // True when automatic gain control is enabled, false otherwise. |
162 bool agc_is_enabled_; | 141 bool agc_is_enabled_; |
163 | 142 |
| 143 // The media session ID used to identify which input device to be started. |
| 144 int session_id_; |
| 145 |
164 DISALLOW_COPY_AND_ASSIGN(WebRtcAudioCapturer); | 146 DISALLOW_COPY_AND_ASSIGN(WebRtcAudioCapturer); |
165 }; | 147 }; |
166 | 148 |
167 } // namespace content | 149 } // namespace content |
168 | 150 |
169 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_ | 151 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_ |
OLD | NEW |