Chromium Code Reviews| 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 "content/renderer/media/webrtc_audio_device_impl.h" | 15 #include "content/renderer/media/webrtc_audio_device_impl.h" |
| 15 #include "content/renderer/media/webrtc_local_audio_renderer.h" | 16 #include "content/renderer/media/webrtc_local_audio_renderer.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 #include "media/base/audio_fifo.h" | 19 #include "media/base/audio_fifo.h" |
| 19 | 20 |
| 20 namespace media { | 21 namespace media { |
| 21 class AudioBus; | 22 class AudioBus; |
| 22 } | 23 } |
| 23 | 24 |
| 24 namespace content { | 25 namespace content { |
| 25 | 26 |
| 26 class WebRtcAudioCapturerSink; | 27 class WebRtcAudioCapturerSink; |
| 27 class WebRtcLocalAudioRenderer; | 28 class WebRtcLocalAudioRenderer; |
| 28 | 29 |
| 29 // This class manages the capture data flow by getting data from its | 30 // This class manages the capture data flow by getting data from its |
| 30 // |source_|, and passing it to its |sink_|. | 31 // |source_|, and passing it to its |sink_|. |
| 31 // It allows clients to inject their own capture data source by calling | 32 // It allows clients to inject their own capture data source by calling |
| 32 // SetCapturerSource(). It is also possible to enable a local sink and | 33 // SetCapturerSource(). It is also possible to enable a local sink and |
| 33 // register a callback which the sink can call when it wants to read captured | 34 // register a callback which the sink can call when it wants to read captured |
| 34 // data cached in a FIFO for local loopback rendering. | 35 // data cached in a FIFO for local loopback rendering. |
| 35 // The threading model for this class is rather messy since it will be | 36 // The threading model for this class is rather complex since it will be |
| 36 // created on a Libjingle thread, captured data is provided on a dedicated | 37 // created on the main render thread, captured data is provided on a dedicated |
| 37 // AudioInputDevice thread, and methods can be called either on the Libjingle | 38 // AudioInputDevice thread, and methods can be called either on the Libjingle |
| 38 // thread or on the main render thread but also other client threads | 39 // thread or on the main render thread but also other client threads |
| 39 // if an alternative AudioCapturerSource has been set. In addition, the | 40 // if an alternative AudioCapturerSource has been set. In addition, the |
| 40 // AudioCapturerSource::CaptureEventHandler methods are called on the IO thread | 41 // AudioCapturerSource::CaptureEventHandler methods are called on the IO thread |
| 41 // and requests for data to render is done on the AudioOutputDevice thread. | 42 // and requests for data to render is done on the AudioOutputDevice thread. |
| 42 class WebRtcAudioCapturer | 43 class CONTENT_EXPORT WebRtcAudioCapturer |
| 43 : public base::RefCountedThreadSafe<WebRtcAudioCapturer>, | 44 : public base::RefCountedThreadSafe<WebRtcAudioCapturer>, |
| 44 public media::AudioCapturerSource::CaptureCallback, | 45 NON_EXPORTED_BASE(public media::AudioCapturerSource::CaptureCallback), |
| 45 public media::AudioCapturerSource::CaptureEventHandler, | 46 NON_EXPORTED_BASE(public media::AudioCapturerSource::CaptureEventHandler), |
| 46 public content::WebRtcLocalAudioRenderer::LocalRenderCallback { | 47 NON_EXPORTED_BASE(public content::WebRtcLocalAudioRenderer::LocalRenderCal lback) { |
|
tommi (sloooow) - chröme
2013/01/15 17:43:49
80+
henrika (OOO until Aug 14)
2013/01/16 16:37:17
Done.
| |
| 47 public: | 48 public: |
| 48 // Use to construct the audio capturer. | 49 // Use to construct the audio capturer. |
| 50 // Called on the main render thread. | |
| 49 static scoped_refptr<WebRtcAudioCapturer> CreateCapturer(); | 51 static scoped_refptr<WebRtcAudioCapturer> CreateCapturer(); |
| 50 | 52 |
| 53 // Creates and configures the default audio capturing source using the | |
| 54 // provided audio parameters. | |
| 55 // Called on the main render thread. | |
| 56 bool Initialize(media::ChannelLayout channel_layout, int sample_rate); | |
| 57 | |
| 51 // Called by the client on the sink side to add a sink. | 58 // Called by the client on the sink side to add a sink. |
| 59 // WebRtcAudioDeviceImpl calls this method on the main render thread but | |
| 60 // other clients may call it from other threads. The current implementation | |
| 61 // does not support multi-thread calling. | |
| 62 // TODO(henrika): add lock if we extend number of supported sinks. | |
| 63 // Called on the main render thread. | |
| 52 void AddCapturerSink(WebRtcAudioCapturerSink* sink); | 64 void AddCapturerSink(WebRtcAudioCapturerSink* sink); |
| 53 | 65 |
| 54 // Called by the client on the sink side to remove a sink. | 66 // Called by the client on the sink side to remove a sink. |
| 67 // Called on the main render thread. | |
| 68 // TODO(henrika): add lock if we extend number of supported sinks. | |
| 69 // Called on the main render thread. | |
| 55 void RemoveCapturerSink(WebRtcAudioCapturerSink* sink); | 70 void RemoveCapturerSink(WebRtcAudioCapturerSink* sink); |
| 56 | 71 |
| 57 // SetCapturerSource() is called if the client on the source side desires to | 72 // SetCapturerSource() is called if the client on the source side desires to |
| 58 // provide their own captured audio data. Client is responsible for calling | 73 // provide their own captured audio data. Client is responsible for calling |
| 59 // Start() on its own source to have the ball rolling. | 74 // Start() on its own source to have the ball rolling. |
| 75 // Called on the main render thread. | |
| 60 void SetCapturerSource( | 76 void SetCapturerSource( |
| 61 const scoped_refptr<media::AudioCapturerSource>& source); | 77 const scoped_refptr<media::AudioCapturerSource>& source); |
| 62 | 78 |
| 63 // The |on_device_stopped_cb| callback will be called in OnDeviceStopped(). | 79 // The |on_device_stopped_cb| callback will be called in OnDeviceStopped(). |
| 80 // Called on the main render thread. | |
| 64 void SetStopCallback(const base::Closure& on_device_stopped_cb); | 81 void SetStopCallback(const base::Closure& on_device_stopped_cb); |
| 65 | 82 |
| 66 // Informs this class that a local sink shall be used in addition to the | 83 // Informs this class that a local sink shall be used in addition to the |
| 67 // registered WebRtcAudioCapturerSink sink(s). The capturer will enter a | 84 // registered WebRtcAudioCapturerSink sink(s). The capturer will enter a |
| 68 // buffering mode and store all incoming audio frames in a local FIFO. | 85 // buffering mode and store all incoming audio frames in a local FIFO. |
| 69 // The renderer will read data from this buffer using the ProvideInput() | 86 // The renderer will read data from this buffer using the ProvideInput() |
| 70 // method. Called on the main render thread. | 87 // method. |
| 88 // Called on the main render thread. | |
| 71 void PrepareLoopback(); | 89 void PrepareLoopback(); |
| 72 | 90 |
| 73 // Cancels loopback mode and stops buffering local copies of captured | 91 // Cancels loopback mode and stops buffering local copies of captured |
| 74 // data in the FIFO. | 92 // data in the FIFO. |
| 75 // Called on the main render thread. | 93 // Called on the main render thread. |
| 76 void CancelLoopback(); | 94 void CancelLoopback(); |
| 77 | 95 |
| 78 // Pauses buffering of captured data. Does only have an effect if a local | 96 // Pauses buffering of captured data. Does only have an effect if a local |
| 79 // sink is used. | 97 // sink is used. |
| 80 // Called on the main render thread. | 98 // Called on the main render thread. |
| 81 void PauseBuffering(); | 99 void PauseBuffering(); |
| 82 | 100 |
| 83 // Resumes buffering of captured data. Does only have an effect if a local | 101 // Resumes buffering of captured data. Does only have an effect if a local |
| 84 // sink is used. | 102 // sink is used. |
| 85 // Called on the main render thread. | 103 // Called on the main render thread. |
| 86 void ResumeBuffering(); | 104 void ResumeBuffering(); |
| 87 | 105 |
| 88 // Starts recording audio. | 106 // Starts recording audio. |
| 107 // Called on the main render thread or a Libjingle working thread. | |
| 89 void Start(); | 108 void Start(); |
| 90 | 109 |
| 91 // Stops recording audio. | 110 // Stops recording audio. |
| 111 // Called on the main render thread or a Libjingle working thread. | |
| 92 void Stop(); | 112 void Stop(); |
| 93 | 113 |
| 94 // Sets the microphone volume. | 114 // Sets the microphone volume. |
| 115 // Called on the AudioInputDevice audio thread. | |
| 95 void SetVolume(double volume); | 116 void SetVolume(double volume); |
| 96 | 117 |
| 97 // Specifies the |session_id| to query which device to use. | 118 // Specifies the |session_id| to query which device to use. |
| 119 // Called on the main render thread. | |
| 98 void SetDevice(int session_id); | 120 void SetDevice(int session_id); |
| 99 | 121 |
| 100 // Enables or disables the WebRtc AGC control. | 122 // Enables or disables the WebRtc AGC control. |
| 123 // Called from a Libjingle working thread. | |
| 101 void SetAutomaticGainControl(bool enable); | 124 void SetAutomaticGainControl(bool enable); |
| 102 | 125 |
| 103 bool is_recording() const { return running_; } | 126 bool is_recording() const { return running_; } |
| 104 | 127 |
| 105 // Returns true if a local renderer has called PrepareLoopback() and it can | 128 // Returns true if a local renderer has called PrepareLoopback() and it can |
| 106 // be utilized to prevent more than one local renderer. | 129 // be utilized to prevent more than one local renderer. |
| 130 // Called on the main render thread. | |
| 107 bool IsInLoopbackMode(); | 131 bool IsInLoopbackMode(); |
| 108 | 132 |
| 109 // Audio parameters utilized by the audio capturer. Can be utilized by | 133 // Audio parameters utilized by the audio capturer. Can be utilized by |
| 110 // a local renderer to set up a renderer using identical parameters as the | 134 // a local renderer to set up a renderer using identical parameters as the |
| 111 // capturer. | 135 // capturer. |
| 112 const media::AudioParameters& audio_parameter() const { return params_; } | 136 const media::AudioParameters& audio_parameter() const { return params_; } |
| 113 | 137 |
| 114 // AudioCapturerSource::CaptureCallback implementation. | 138 // AudioCapturerSource::CaptureCallback implementation. |
| 115 // Called on the AudioInputDevice audio thread. | 139 // Called on the AudioInputDevice audio thread. |
| 116 virtual void Capture(media::AudioBus* audio_source, | 140 virtual void Capture(media::AudioBus* audio_source, |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 131 | 155 |
| 132 protected: | 156 protected: |
| 133 friend class base::RefCountedThreadSafe<WebRtcAudioCapturer>; | 157 friend class base::RefCountedThreadSafe<WebRtcAudioCapturer>; |
| 134 virtual ~WebRtcAudioCapturer(); | 158 virtual ~WebRtcAudioCapturer(); |
| 135 | 159 |
| 136 private: | 160 private: |
| 137 typedef std::list<WebRtcAudioCapturerSink*> SinkList; | 161 typedef std::list<WebRtcAudioCapturerSink*> SinkList; |
| 138 | 162 |
| 139 WebRtcAudioCapturer(); | 163 WebRtcAudioCapturer(); |
| 140 | 164 |
| 141 // Initializes the capturer, called right after the object is created. | 165 // Used to DCHECK that we are called on the correct thread. |
| 142 // Returns false if the initialization fails. | 166 base::ThreadChecker thread_checker_; |
| 143 bool Initialize(); | |
| 144 | 167 |
| 145 // Protects |source_|, |sinks_|, |running_|, |on_device_stopped_cb_|, | 168 // Protects |source_|, |sinks_|, |running_|, |on_device_stopped_cb_|, |
| 146 // |loopback_fifo_| and |buffering_|. | 169 // |loopback_fifo_| and |buffering_|. |
| 147 base::Lock lock_; | 170 base::Lock lock_; |
| 148 | 171 |
| 149 // A list of sinks that the audio data is fed to. | 172 // A list of sinks that the audio data is fed to. |
| 150 SinkList sinks_; | 173 SinkList sinks_; |
| 151 | 174 |
| 152 // The audio data source from the browser process. | 175 // The audio data source from the browser process. |
| 153 scoped_refptr<media::AudioCapturerSource> source_; | 176 scoped_refptr<media::AudioCapturerSource> source_; |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 171 | 194 |
| 172 // True when FIFO is utilized, false otherwise. | 195 // True when FIFO is utilized, false otherwise. |
| 173 bool buffering_; | 196 bool buffering_; |
| 174 | 197 |
| 175 DISALLOW_COPY_AND_ASSIGN(WebRtcAudioCapturer); | 198 DISALLOW_COPY_AND_ASSIGN(WebRtcAudioCapturer); |
| 176 }; | 199 }; |
| 177 | 200 |
| 178 } // namespace content | 201 } // namespace content |
| 179 | 202 |
| 180 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_ | 203 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_ |
| OLD | NEW |