Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CONTENT_RENDERER_MEDIA_WEBRTC_LOCAL_AUDIO_RENDERER_H_ |
| 6 | #define CONTENT_RENDERER_MEDIA_WEBRTC_LOCAL_AUDIO_RENDERER_H_ |
| 7 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 8 | #include <vector> |
| 9 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 10 | #include "base/callback.h" |
| 11 | #include "base/memory/ref_counted.h" |
| 12 | #include "base/synchronization/lock.h" |
| 13 | #include "base/threading/thread_checker.h" |
| 14 | #include "content/common/content_export.h" |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 15 | #include "content/renderer/media/media_stream_audio_renderer.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 16 | #include "content/renderer/media/webrtc_audio_device_impl.h" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 17 | #include "content/renderer/media/webrtc_local_audio_track.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 18 | |
| 19 | namespace media { |
| 20 | class AudioBus; |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 21 | class AudioFifo; |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 22 | class AudioOutputDevice; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 23 | class AudioParameters; |
| 24 | } |
| 25 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 26 | namespace content { |
| 27 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 28 | class WebRtcAudioCapturer; |
| 29 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 30 | // WebRtcLocalAudioRenderer is a MediaStreamAudioRenderer designed for rendering |
| 31 | // local audio media stream tracks, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 32 | // http://dev.w3.org/2011/webrtc/editor/getusermedia.html#mediastreamtrack |
| 33 | // It also implements media::AudioRendererSink::RenderCallback to render audio |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 34 | // data provided from a WebRtcLocalAudioTrack source. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 35 | // When the audio layer in the browser process asks for data to render, this |
| 36 | // class provides the data by implementing the WebRtcAudioCapturerSink |
| 37 | // interface, i.e., we are a sink seen from the WebRtcAudioCapturer perspective. |
| 38 | // TODO(henrika): improve by using similar principles as in RTCVideoRenderer |
| 39 | // which register itself to the video track when the provider is started and |
| 40 | // deregisters itself when it is stopped. |
| 41 | // Tracking this at http://crbug.com/164813. |
| 42 | class CONTENT_EXPORT WebRtcLocalAudioRenderer |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 43 | : NON_EXPORTED_BASE(public MediaStreamAudioRenderer), |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 44 | NON_EXPORTED_BASE(public media::AudioRendererSink::RenderCallback), |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 45 | NON_EXPORTED_BASE(public WebRtcAudioCapturerSink) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 46 | public: |
| 47 | // Creates a local renderer and registers a capturing |source| object. |
| 48 | // The |source| is owned by the WebRtcAudioDeviceImpl. |
| 49 | // Called on the main thread. |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 50 | WebRtcLocalAudioRenderer(WebRtcLocalAudioTrack* audio_track, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 51 | int source_render_view_id); |
| 52 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 53 | // MediaStreamAudioRenderer implementation. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 54 | // Called on the main thread. |
| 55 | virtual void Start() OVERRIDE; |
| 56 | virtual void Stop() OVERRIDE; |
| 57 | virtual void Play() OVERRIDE; |
| 58 | virtual void Pause() OVERRIDE; |
| 59 | virtual void SetVolume(float volume) OVERRIDE; |
| 60 | virtual base::TimeDelta GetCurrentRenderTime() const OVERRIDE; |
| 61 | virtual bool IsLocalRenderer() const OVERRIDE; |
| 62 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 63 | const base::TimeDelta& total_render_time() const { |
| 64 | return total_render_time_; |
| 65 | } |
| 66 | |
| 67 | protected: |
| 68 | virtual ~WebRtcLocalAudioRenderer(); |
| 69 | |
| 70 | private: |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 71 | // WebRtcAudioCapturerSink implementation. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 72 | |
| 73 | // Called on the AudioInputDevice worker thread. |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 74 | virtual int CaptureData(const std::vector<int>& channels, |
| 75 | const int16* audio_data, |
| 76 | int sample_rate, |
| 77 | int number_of_channels, |
| 78 | int number_of_frames, |
| 79 | int audio_delay_milliseconds, |
| 80 | int current_volume, |
| 81 | bool need_audio_processing) OVERRIDE; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 82 | |
| 83 | // Can be called on different user thread. |
| 84 | virtual void SetCaptureFormat(const media::AudioParameters& params) OVERRIDE; |
| 85 | |
| 86 | // media::AudioRendererSink::RenderCallback implementation. |
| 87 | // Render() is called on the AudioOutputDevice thread and OnRenderError() |
| 88 | // on the IO thread. |
| 89 | virtual int Render(media::AudioBus* audio_bus, |
| 90 | int audio_delay_milliseconds) OVERRIDE; |
| 91 | virtual void OnRenderError() OVERRIDE; |
| 92 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 93 | // The audio track which provides data to render. Given that this class |
| 94 | // implements local loopback, the audio track is getting data from a capture |
| 95 | // instance like a selected microphone and forwards the recorded data to its |
| 96 | // sinks. The recorded data is stored in a FIFO and consumed |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 97 | // by this class when the sink asks for new data. |
| 98 | // The WebRtcAudioCapturer is today created by WebRtcAudioDeviceImpl. |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 99 | scoped_refptr<WebRtcLocalAudioTrack> audio_track_; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 100 | |
| 101 | // The render view in which the audio is rendered into |sink_|. |
| 102 | const int source_render_view_id_; |
| 103 | |
| 104 | // The sink (destination) for rendered audio. |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 105 | scoped_refptr<media::AudioOutputDevice> sink_; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 106 | |
| 107 | // Used to DCHECK that we are called on the correct thread. |
| 108 | base::ThreadChecker thread_checker_; |
| 109 | |
| 110 | // Contains copies of captured audio frames. |
| 111 | scoped_ptr<media::AudioFifo> loopback_fifo_; |
| 112 | |
| 113 | // Stores last time a render callback was received. The time difference |
| 114 | // between a new time stamp and this value can be used to derive the |
| 115 | // total render time. |
| 116 | base::Time last_render_time_; |
| 117 | |
| 118 | // Keeps track of total time audio has been rendered. |
| 119 | base::TimeDelta total_render_time_; |
| 120 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 121 | // The audio parameters used by the renderer. |
| 122 | media::AudioParameters audio_params_; |
| 123 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 124 | // Set when playing, cleared when paused. |
| 125 | bool playing_; |
| 126 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 127 | // Protects |loopback_fifo_|, |playing_| and |sink_|. |
| 128 | mutable base::Lock thread_lock_; |
| 129 | |
| 130 | DISALLOW_COPY_AND_ASSIGN(WebRtcLocalAudioRenderer); |
| 131 | }; |
| 132 | |
| 133 | } // namespace content |
| 134 | |
| 135 | #endif // CONTENT_RENDERER_MEDIA_WEBRTC_LOCAL_AUDIO_RENDERER_H_ |