| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_DEVICE_IMPL_H_ | 5 #ifndef CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_DEVICE_IMPL_H_ |
| 6 #define CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_DEVICE_IMPL_H_ | 6 #define CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_DEVICE_IMPL_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 | 178 |
| 179 namespace content { | 179 namespace content { |
| 180 | 180 |
| 181 class WebRtcAudioCapturer; | 181 class WebRtcAudioCapturer; |
| 182 class WebRtcAudioRenderer; | 182 class WebRtcAudioRenderer; |
| 183 | 183 |
| 184 // TODO(xians): Move the following two interfaces to webrtc so that | 184 // TODO(xians): Move the following two interfaces to webrtc so that |
| 185 // libjingle can own references to the renderer and capturer. | 185 // libjingle can own references to the renderer and capturer. |
| 186 class WebRtcAudioRendererSource { | 186 class WebRtcAudioRendererSource { |
| 187 public: | 187 public: |
| 188 // Callback to get the rendered interleaved data. | 188 // Callback to get the rendered data. |
| 189 // TODO(xians): Change uint8* to int16*. | 189 virtual void RenderData(media::AudioBus* audio_bus, |
| 190 virtual void RenderData(uint8* audio_data, | 190 int sample_rate, |
| 191 int number_of_channels, | |
| 192 int number_of_frames, | |
| 193 int audio_delay_milliseconds) = 0; | 191 int audio_delay_milliseconds) = 0; |
| 194 | 192 |
| 195 // Set the format for the capture audio parameters. | |
| 196 virtual void SetRenderFormat(const media::AudioParameters& params) = 0; | |
| 197 | |
| 198 // Callback to notify the client that the renderer is going away. | 193 // Callback to notify the client that the renderer is going away. |
| 199 virtual void RemoveAudioRenderer(WebRtcAudioRenderer* renderer) = 0; | 194 virtual void RemoveAudioRenderer(WebRtcAudioRenderer* renderer) = 0; |
| 200 | 195 |
| 201 protected: | 196 protected: |
| 202 virtual ~WebRtcAudioRendererSource() {} | 197 virtual ~WebRtcAudioRendererSource() {} |
| 203 }; | 198 }; |
| 204 | 199 |
| 205 class PeerConnectionAudioSink { | 200 class PeerConnectionAudioSink { |
| 206 public: | 201 public: |
| 207 // Callback to deliver the captured interleaved data. | 202 // Callback to deliver the captured interleaved data. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 228 | 223 |
| 229 // Set the format for the capture audio parameters. | 224 // Set the format for the capture audio parameters. |
| 230 // This is called when the capture format has changed, and it must be called | 225 // This is called when the capture format has changed, and it must be called |
| 231 // on the same thread as calling CaptureData(). | 226 // on the same thread as calling CaptureData(). |
| 232 virtual void OnSetFormat(const media::AudioParameters& params) = 0; | 227 virtual void OnSetFormat(const media::AudioParameters& params) = 0; |
| 233 | 228 |
| 234 protected: | 229 protected: |
| 235 virtual ~PeerConnectionAudioSink() {} | 230 virtual ~PeerConnectionAudioSink() {} |
| 236 }; | 231 }; |
| 237 | 232 |
| 233 // TODO(xians): Merge this interface with WebRtcAudioRendererSource. |
| 234 // The reason why we could not do it today is that WebRtcAudioRendererSource |
| 235 // gets the data by pulling, while the data is pushed into |
| 236 // WebRtcPlayoutDataSource::Sink. |
| 237 class WebRtcPlayoutDataSource { |
| 238 public: |
| 239 class Sink { |
| 240 public: |
| 241 // Callback to get the playout data. |
| 242 virtual void OnPlayoutData(media::AudioBus* audio_bus, |
| 243 int sample_rate, |
| 244 int audio_delay_milliseconds) = 0; |
| 245 protected: |
| 246 virtual ~Sink() {} |
| 247 }; |
| 248 |
| 249 // Adds/Removes the sink of WebRtcAudioRendererSource to the ADM. |
| 250 // These methods are used by the MediaStreamAudioProcesssor to get the |
| 251 // rendered data for AEC. |
| 252 virtual void AddPlayoutSink(Sink* sink) = 0; |
| 253 virtual void RemovePlayoutSink(Sink* sink) = 0; |
| 254 |
| 255 protected: |
| 256 virtual ~WebRtcPlayoutDataSource() {} |
| 257 }; |
| 258 |
| 238 // Note that this class inherits from webrtc::AudioDeviceModule but due to | 259 // Note that this class inherits from webrtc::AudioDeviceModule but due to |
| 239 // the high number of non-implemented methods, we move the cruft over to the | 260 // the high number of non-implemented methods, we move the cruft over to the |
| 240 // WebRtcAudioDeviceNotImpl. | 261 // WebRtcAudioDeviceNotImpl. |
| 241 class CONTENT_EXPORT WebRtcAudioDeviceImpl | 262 class CONTENT_EXPORT WebRtcAudioDeviceImpl |
| 242 : NON_EXPORTED_BASE(public PeerConnectionAudioSink), | 263 : NON_EXPORTED_BASE(public PeerConnectionAudioSink), |
| 243 NON_EXPORTED_BASE(public WebRtcAudioDeviceNotImpl), | 264 NON_EXPORTED_BASE(public WebRtcAudioDeviceNotImpl), |
| 244 NON_EXPORTED_BASE(public WebRtcAudioRendererSource) { | 265 NON_EXPORTED_BASE(public WebRtcAudioRendererSource), |
| 266 NON_EXPORTED_BASE(public WebRtcPlayoutDataSource) { |
| 245 public: | 267 public: |
| 246 // The maximum volume value WebRtc uses. | 268 // The maximum volume value WebRtc uses. |
| 247 static const int kMaxVolumeLevel = 255; | 269 static const int kMaxVolumeLevel = 255; |
| 248 | 270 |
| 249 // Instances of this object are created on the main render thread. | 271 // Instances of this object are created on the main render thread. |
| 250 WebRtcAudioDeviceImpl(); | 272 WebRtcAudioDeviceImpl(); |
| 251 | 273 |
| 252 // webrtc::RefCountedModule implementation. | 274 // webrtc::RefCountedModule implementation. |
| 253 // The creator must call AddRef() after construction and use Release() | 275 // The creator must call AddRef() after construction and use Release() |
| 254 // to release the reference and delete this object. | 276 // to release the reference and delete this object. |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 // will be rendered to a matching output device. | 336 // will be rendered to a matching output device. |
| 315 // Returns true if the capture device has a paired output device, otherwise | 337 // Returns true if the capture device has a paired output device, otherwise |
| 316 // false. Note that if there are more than one open capture device the | 338 // false. Note that if there are more than one open capture device the |
| 317 // function will not be able to pick an appropriate device and return false. | 339 // function will not be able to pick an appropriate device and return false. |
| 318 bool GetAuthorizedDeviceInfoForAudioRenderer( | 340 bool GetAuthorizedDeviceInfoForAudioRenderer( |
| 319 int* session_id, int* output_sample_rate, int* output_buffer_size); | 341 int* session_id, int* output_sample_rate, int* output_buffer_size); |
| 320 | 342 |
| 321 const scoped_refptr<WebRtcAudioRenderer>& renderer() const { | 343 const scoped_refptr<WebRtcAudioRenderer>& renderer() const { |
| 322 return renderer_; | 344 return renderer_; |
| 323 } | 345 } |
| 324 int output_buffer_size() const { | |
| 325 return output_audio_parameters_.frames_per_buffer(); | |
| 326 } | |
| 327 int output_channels() const { | |
| 328 return output_audio_parameters_.channels(); | |
| 329 } | |
| 330 int output_sample_rate() const { | |
| 331 return output_audio_parameters_.sample_rate(); | |
| 332 } | |
| 333 | 346 |
| 334 private: | 347 private: |
| 335 typedef std::list<scoped_refptr<WebRtcAudioCapturer> > CapturerList; | 348 typedef std::list<scoped_refptr<WebRtcAudioCapturer> > CapturerList; |
| 349 typedef std::list<WebRtcPlayoutDataSource::Sink*> PlayoutDataSinkList; |
| 350 class RenderBuffer; |
| 336 | 351 |
| 337 // Make destructor private to ensure that we can only be deleted by Release(). | 352 // Make destructor private to ensure that we can only be deleted by Release(). |
| 338 virtual ~WebRtcAudioDeviceImpl(); | 353 virtual ~WebRtcAudioDeviceImpl(); |
| 339 | 354 |
| 340 // PeerConnectionAudioSink implementation. | 355 // PeerConnectionAudioSink implementation. |
| 341 | 356 |
| 342 // Called on the AudioInputDevice worker thread. | 357 // Called on the AudioInputDevice worker thread. |
| 343 virtual int OnData(const int16* audio_data, | 358 virtual int OnData(const int16* audio_data, |
| 344 int sample_rate, | 359 int sample_rate, |
| 345 int number_of_channels, | 360 int number_of_channels, |
| 346 int number_of_frames, | 361 int number_of_frames, |
| 347 const std::vector<int>& channels, | 362 const std::vector<int>& channels, |
| 348 int audio_delay_milliseconds, | 363 int audio_delay_milliseconds, |
| 349 int current_volume, | 364 int current_volume, |
| 350 bool need_audio_processing, | 365 bool need_audio_processing, |
| 351 bool key_pressed) OVERRIDE; | 366 bool key_pressed) OVERRIDE; |
| 352 | 367 |
| 353 // Called on the AudioInputDevice worker thread. | 368 // Called on the AudioInputDevice worker thread. |
| 354 virtual void OnSetFormat(const media::AudioParameters& params) OVERRIDE; | 369 virtual void OnSetFormat(const media::AudioParameters& params) OVERRIDE; |
| 355 | 370 |
| 356 // WebRtcAudioRendererSource implementation. | 371 // WebRtcAudioRendererSource implementation. |
| 357 | 372 |
| 358 // Called on the AudioInputDevice worker thread. | 373 // Called on the AudioInputDevice worker thread. |
| 359 virtual void RenderData(uint8* audio_data, | 374 virtual void RenderData(media::AudioBus* audio_bus, |
| 360 int number_of_channels, | 375 int sample_rate, |
| 361 int number_of_frames, | |
| 362 int audio_delay_milliseconds) OVERRIDE; | 376 int audio_delay_milliseconds) OVERRIDE; |
| 363 | 377 |
| 364 // Called on the main render thread. | 378 // Called on the main render thread. |
| 365 virtual void SetRenderFormat(const media::AudioParameters& params) OVERRIDE; | |
| 366 virtual void RemoveAudioRenderer(WebRtcAudioRenderer* renderer) OVERRIDE; | 379 virtual void RemoveAudioRenderer(WebRtcAudioRenderer* renderer) OVERRIDE; |
| 367 | 380 |
| 381 // WebRtcPlayoutDataSource implementation. |
| 382 virtual void AddPlayoutSink(WebRtcPlayoutDataSource::Sink* sink) OVERRIDE; |
| 383 virtual void RemovePlayoutSink(WebRtcPlayoutDataSource::Sink* sink) OVERRIDE; |
| 384 |
| 368 // Used to DCHECK that we are called on the correct thread. | 385 // Used to DCHECK that we are called on the correct thread. |
| 369 base::ThreadChecker thread_checker_; | 386 base::ThreadChecker thread_checker_; |
| 370 | 387 |
| 371 int ref_count_; | 388 int ref_count_; |
| 372 | 389 |
| 373 // List of captures which provides access to the native audio input layer | 390 // List of captures which provides access to the native audio input layer |
| 374 // in the browser process. | 391 // in the browser process. |
| 375 CapturerList capturers_; | 392 CapturerList capturers_; |
| 376 | 393 |
| 377 // Provides access to the audio renderer in the browser process. | 394 // Provides access to the audio renderer in the browser process. |
| 378 scoped_refptr<WebRtcAudioRenderer> renderer_; | 395 scoped_refptr<WebRtcAudioRenderer> renderer_; |
| 379 | 396 |
| 397 // A list of raw pointer of WebRtcPlayoutDataSource::Sink objects which want |
| 398 // to get the playout data, the sink need to call RemovePlayoutSink() |
| 399 // before it goes away. |
| 400 PlayoutDataSinkList playout_sinks_; |
| 401 |
| 380 // Weak reference to the audio callback. | 402 // Weak reference to the audio callback. |
| 381 // The webrtc client defines |audio_transport_callback_| by calling | 403 // The webrtc client defines |audio_transport_callback_| by calling |
| 382 // RegisterAudioCallback(). | 404 // RegisterAudioCallback(). |
| 383 webrtc::AudioTransport* audio_transport_callback_; | 405 webrtc::AudioTransport* audio_transport_callback_; |
| 384 | 406 |
| 385 // Cached values of used output audio parameters. Platform dependent. | |
| 386 media::AudioParameters output_audio_parameters_; | |
| 387 | |
| 388 // Cached value of the current audio delay on the input/capture side. | 407 // Cached value of the current audio delay on the input/capture side. |
| 389 int input_delay_ms_; | 408 int input_delay_ms_; |
| 390 | 409 |
| 391 // Cached value of the current audio delay on the output/renderer side. | 410 // Cached value of the current audio delay on the output/renderer side. |
| 392 int output_delay_ms_; | 411 int output_delay_ms_; |
| 393 | 412 |
| 394 // Protects |recording_|, |output_delay_ms_|, |input_delay_ms_|, |renderer_| | 413 // Protects |recording_|, |output_delay_ms_|, |input_delay_ms_|, |renderer_| |
| 395 // |recording_| and |microphone_volume_|. | 414 // |recording_| and |microphone_volume_|. |
| 396 mutable base::Lock lock_; | 415 mutable base::Lock lock_; |
| 397 | 416 |
| 398 // Used to protect the racing of calling OnData() since there can be more | 417 // Used to protect the racing of calling OnData() since there can be more |
| 399 // than one input stream calling OnData(). | 418 // than one input stream calling OnData(). |
| 400 mutable base::Lock capture_callback_lock_; | 419 mutable base::Lock capture_callback_lock_; |
| 401 | 420 |
| 402 bool initialized_; | 421 bool initialized_; |
| 403 bool playing_; | 422 bool playing_; |
| 404 bool recording_; | 423 bool recording_; |
| 405 | 424 |
| 406 // Used for histograms of total recording and playout times. | 425 // Used for histograms of total recording and playout times. |
| 407 base::Time start_capture_time_; | 426 base::Time start_capture_time_; |
| 408 base::Time start_render_time_; | 427 base::Time start_render_time_; |
| 409 | 428 |
| 410 // Stores latest microphone volume received in a CaptureData() callback. | 429 // Stores latest microphone volume received in a CaptureData() callback. |
| 411 // Range is [0, 255]. | 430 // Range is [0, 255]. |
| 412 uint32_t microphone_volume_; | 431 uint32_t microphone_volume_; |
| 413 | 432 |
| 433 // Buffer used for temporary storage during render callback. |
| 434 // It is only accessed by the audio render thread. |
| 435 std::vector<int16> render_buffer_; |
| 436 |
| 414 DISALLOW_COPY_AND_ASSIGN(WebRtcAudioDeviceImpl); | 437 DISALLOW_COPY_AND_ASSIGN(WebRtcAudioDeviceImpl); |
| 415 }; | 438 }; |
| 416 | 439 |
| 417 } // namespace content | 440 } // namespace content |
| 418 | 441 |
| 419 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_DEVICE_IMPL_H_ | 442 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_DEVICE_IMPL_H_ |
| OLD | NEW |