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_LOCAL_AUDIO_RENDERER_H_ | 5 #ifndef CONTENT_RENDERER_MEDIA_TRACK_AUDIO_RENDERER_H_ |
6 #define CONTENT_RENDERER_MEDIA_WEBRTC_LOCAL_AUDIO_RENDERER_H_ | 6 #define CONTENT_RENDERER_MEDIA_TRACK_AUDIO_RENDERER_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/callback.h" | 13 #include "base/callback.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
16 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
17 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
18 #include "base/threading/thread_checker.h" | 18 #include "base/threading/thread_checker.h" |
19 #include "content/common/content_export.h" | 19 #include "content/common/content_export.h" |
20 #include "content/public/renderer/media_stream_audio_renderer.h" | 20 #include "content/public/renderer/media_stream_audio_renderer.h" |
21 #include "content/public/renderer/media_stream_audio_sink.h" | 21 #include "content/public/renderer/media_stream_audio_sink.h" |
22 #include "content/renderer/media/webrtc_audio_device_impl.h" | 22 #include "media/base/audio_renderer_sink.h" |
23 #include "content/renderer/media/webrtc_local_audio_track.h" | |
24 #include "media/base/output_device.h" | 23 #include "media/base/output_device.h" |
25 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" | 24 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" |
26 | 25 |
27 namespace media { | 26 namespace media { |
28 class AudioBus; | 27 class AudioBus; |
29 class AudioShifter; | 28 class AudioShifter; |
30 class AudioOutputDevice; | 29 class AudioOutputDevice; |
31 class AudioParameters; | 30 class AudioParameters; |
32 } | 31 } |
33 | 32 |
34 namespace content { | 33 namespace content { |
35 | 34 |
36 class WebRtcAudioCapturer; | 35 // TrackAudioRenderer is a MediaStreamAudioRenderer for plumbing audio data |
37 | 36 // generated from either local or remote (but not PeerConnection/WebRTC-sourced) |
38 // WebRtcLocalAudioRenderer is a MediaStreamAudioRenderer designed for rendering | 37 // MediaStreamAudioTracks to an audio output device, reconciling differences in |
39 // local audio media stream tracks, | 38 // the rates of production and consumption of the audio data. Note that remote |
40 // http://dev.w3.org/2011/webrtc/editor/getusermedia.html#mediastreamtrack | 39 // PeerConnection-sourced tracks are NOT rendered by this implementation (see |
41 // It also implements media::AudioRendererSink::RenderCallback to render audio | 40 // MediaStreamRendererFactoryImpl). |
42 // data provided from a WebRtcLocalAudioTrack source. | 41 // |
43 // When the audio layer in the browser process asks for data to render, this | 42 // This class uses AudioDeviceFactory to create media::AudioOutputDevices and |
44 // class provides the data by implementing the MediaStreamAudioSink | 43 // owns/manages their lifecycles. Output devices are automatically re-created |
45 // interface, i.e., we are a sink seen from the WebRtcAudioCapturer perspective. | 44 // in response to audio format changes, or use of the SwitchOutputDevice() API |
46 // TODO(henrika): improve by using similar principles as in | 45 // by client code. |
47 // MediaStreamVideoRendererSink which register itself to the video track when | 46 // |
48 // the provider is started and deregisters itself when it is stopped. Tracking | 47 // Audio data is feed-in from the source via calls to OnData(). The |
49 // this at http://crbug.com/164813. | 48 // internally-owned media::AudioOutputDevice calls Render() to pull-out that |
50 class CONTENT_EXPORT WebRtcLocalAudioRenderer | 49 // audio data. However, because of clock differences and other environmental |
| 50 // factors, the audio will inevitably feed-in at a rate different from the rate |
| 51 // it is being rendered-out. media::AudioShifter is used to buffer, stretch |
| 52 // and skip audio to maintain time synchronization between the producer and |
| 53 // consumer. |
| 54 class CONTENT_EXPORT TrackAudioRenderer |
51 : NON_EXPORTED_BASE(public MediaStreamAudioRenderer), | 55 : NON_EXPORTED_BASE(public MediaStreamAudioRenderer), |
52 NON_EXPORTED_BASE(public MediaStreamAudioSink), | 56 NON_EXPORTED_BASE(public MediaStreamAudioSink), |
53 NON_EXPORTED_BASE(public media::AudioRendererSink::RenderCallback), | 57 NON_EXPORTED_BASE(public media::AudioRendererSink::RenderCallback), |
54 NON_EXPORTED_BASE(public media::OutputDevice) { | 58 NON_EXPORTED_BASE(public media::OutputDevice) { |
55 public: | 59 public: |
56 // Creates a local renderer and registers a capturing |source| object. | 60 // Creates a renderer for the given |audio_track|. |playout_render_frame_id| |
57 // The |source| is owned by the WebRtcAudioDeviceImpl. | 61 // refers to the RenderFrame that owns this instance (e.g., it contains the |
| 62 // DOM widget representing the player). |session_id| and |device_id| are |
| 63 // optional, and are used to direct audio output to a pre-selected device; |
| 64 // otherwise, audio is output to the default device for the system. |
| 65 // |
58 // Called on the main thread. | 66 // Called on the main thread. |
59 WebRtcLocalAudioRenderer(const blink::WebMediaStreamTrack& audio_track, | 67 TrackAudioRenderer(const blink::WebMediaStreamTrack& audio_track, |
60 int source_render_frame_id, | 68 int playout_render_frame_id, |
61 int session_id, | 69 int session_id, |
62 const std::string& device_id, | 70 const std::string& device_id, |
63 const url::Origin& security_origin); | 71 const url::Origin& security_origin); |
64 | 72 |
65 // MediaStreamAudioRenderer implementation. | 73 // MediaStreamAudioRenderer implementation. |
66 // Called on the main thread. | 74 // Called on the main thread. |
67 void Start() override; | 75 void Start() override; |
68 void Stop() override; | 76 void Stop() override; |
69 void Play() override; | 77 void Play() override; |
70 void Pause() override; | 78 void Pause() override; |
71 void SetVolume(float volume) override; | 79 void SetVolume(float volume) override; |
72 media::OutputDevice* GetOutputDevice() override; | 80 media::OutputDevice* GetOutputDevice() override; |
73 base::TimeDelta GetCurrentRenderTime() const override; | 81 base::TimeDelta GetCurrentRenderTime() const override; |
74 bool IsLocalRenderer() const override; | 82 bool IsLocalRenderer() const override; |
75 | 83 |
76 // media::OutputDevice implementation | 84 // media::OutputDevice implementation |
77 void SwitchOutputDevice(const std::string& device_id, | 85 void SwitchOutputDevice(const std::string& device_id, |
78 const url::Origin& security_origin, | 86 const url::Origin& security_origin, |
79 const media::SwitchOutputDeviceCB& callback) override; | 87 const media::SwitchOutputDeviceCB& callback) override; |
80 media::AudioParameters GetOutputParameters() override; | 88 media::AudioParameters GetOutputParameters() override; |
81 media::OutputDeviceStatus GetDeviceStatus() override; | 89 media::OutputDeviceStatus GetDeviceStatus() override; |
82 | 90 |
83 const base::TimeDelta& total_render_time() const { | |
84 return total_render_time_; | |
85 } | |
86 | |
87 protected: | 91 protected: |
88 ~WebRtcLocalAudioRenderer() override; | 92 ~TrackAudioRenderer() override; |
89 | 93 |
90 private: | 94 private: |
91 // MediaStreamAudioSink implementation. | 95 // MediaStreamAudioSink implementation. |
92 | 96 |
93 // Called on the AudioInputDevice worker thread. | 97 // Called on the AudioInputDevice worker thread. |
94 void OnData(const media::AudioBus& audio_bus, | 98 void OnData(const media::AudioBus& audio_bus, |
95 base::TimeTicks estimated_capture_time) override; | 99 base::TimeTicks reference_time) override; |
96 | 100 |
97 // Called on the AudioInputDevice worker thread. | 101 // Called on the AudioInputDevice worker thread. |
98 void OnSetFormat(const media::AudioParameters& params) override; | 102 void OnSetFormat(const media::AudioParameters& params) override; |
99 | 103 |
100 // media::AudioRendererSink::RenderCallback implementation. | 104 // media::AudioRendererSink::RenderCallback implementation. |
101 // Render() is called on the AudioOutputDevice thread and OnRenderError() | 105 // Render() is called on the AudioOutputDevice thread and OnRenderError() |
102 // on the IO thread. | 106 // on the IO thread. |
103 int Render(media::AudioBus* audio_bus, | 107 int Render(media::AudioBus* audio_bus, |
104 uint32_t audio_delay_milliseconds, | 108 uint32_t audio_delay_milliseconds, |
105 uint32_t frames_skipped) override; | 109 uint32_t frames_skipped) override; |
106 void OnRenderError() override; | 110 void OnRenderError() override; |
107 | 111 |
108 // Initializes and starts the |sink_| if | 112 // Initializes and starts the |sink_| if |
109 // we have received valid |source_params_| && | 113 // we have received valid |source_params_| && |
110 // |playing_| has been set to true && | 114 // |playing_| has been set to true. |
111 // |volume_| is not zero. | |
112 void MaybeStartSink(); | 115 void MaybeStartSink(); |
113 | 116 |
114 // Sets new |source_params_| and then re-initializes and restarts |sink_|. | 117 // Sets new |source_params_| and then re-initializes and restarts |sink_|. |
115 void ReconfigureSink(const media::AudioParameters& params); | 118 void ReconfigureSink(const media::AudioParameters& params); |
116 | 119 |
117 // The audio track which provides data to render. Given that this class | 120 // Creates a new AudioShifter, destroying the old one (if any). This is |
118 // implements local loopback, the audio track is getting data from a capture | 121 // called any time playback is started/stopped, or the sink changes. |
119 // instance like a selected microphone and forwards the recorded data to its | 122 void CreateAudioShifter(); |
120 // sinks. The recorded data is stored in a FIFO and consumed | 123 |
121 // by this class when the sink asks for new data. | 124 // Called when either the source or sink has changed somehow, or audio has |
| 125 // been paused. Drops the AudioShifter and updates |
| 126 // |prior_elapsed_render_time_|. May be called from either the main thread or |
| 127 // the audio thread. Assumption: |thread_lock_| is already acquired. |
| 128 void HaltAudioFlowWhileLockHeld(); |
| 129 |
| 130 // The audio track which provides access to the source data to render. |
| 131 // |
122 // This class is calling MediaStreamAudioSink::AddToAudioTrack() and | 132 // This class is calling MediaStreamAudioSink::AddToAudioTrack() and |
123 // MediaStreamAudioSink::RemoveFromAudioTrack() to connect and disconnect | 133 // MediaStreamAudioSink::RemoveFromAudioTrack() to connect and disconnect |
124 // with the audio track. | 134 // with the audio track. |
125 blink::WebMediaStreamTrack audio_track_; | 135 blink::WebMediaStreamTrack audio_track_; |
126 | 136 |
127 // The render view and frame in which the audio is rendered into |sink_|. | 137 // The render view and frame in which the audio is rendered into |sink_|. |
128 const int source_render_frame_id_; | 138 const int playout_render_frame_id_; |
129 const int session_id_; | 139 const int session_id_; |
130 | 140 |
131 // MessageLoop associated with the single thread that performs all control | 141 // MessageLoop associated with the single thread that performs all control |
132 // tasks. Set to the MessageLoop that invoked the ctor. | 142 // tasks. Set to the MessageLoop that invoked the ctor. |
133 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 143 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
134 | 144 |
135 // The sink (destination) for rendered audio. | 145 // The sink (destination) for rendered audio. |
136 scoped_refptr<media::AudioOutputDevice> sink_; | 146 scoped_refptr<media::AudioOutputDevice> sink_; |
137 | 147 |
138 // This does all the synchronization/resampling/smoothing. | 148 // This does all the synchronization/resampling/smoothing. |
139 scoped_ptr<media::AudioShifter> audio_shifter_; | 149 scoped_ptr<media::AudioShifter> audio_shifter_; |
140 | 150 |
141 // Stores last time a render callback was received. The time difference | 151 // These track the time duration of all the audio rendered so far by this |
142 // between a new time stamp and this value can be used to derive the | 152 // instance. |prior_elapsed_render_time_| tracks the time duration of all |
143 // total render time. | 153 // audio rendered before the last format change. |num_samples_rendered_| |
144 base::TimeTicks last_render_time_; | 154 // tracks the number of audio samples rendered since the last format change. |
| 155 base::TimeDelta prior_elapsed_render_time_; |
| 156 int64_t num_samples_rendered_; |
145 | 157 |
146 // Keeps track of total time audio has been rendered. | 158 // The audio parameters of the track's source. |
147 base::TimeDelta total_render_time_; | |
148 | |
149 // The audio parameters of the capture source. | |
150 // Must only be touched on the main thread. | 159 // Must only be touched on the main thread. |
151 media::AudioParameters source_params_; | 160 media::AudioParameters source_params_; |
152 | 161 |
153 // The audio parameters used by the sink. | |
154 // Must only be touched on the main thread. | |
155 media::AudioParameters sink_params_; | |
156 | |
157 // Set when playing, cleared when paused. | 162 // Set when playing, cleared when paused. |
158 bool playing_; | 163 bool playing_; |
159 | 164 |
160 // Protects |audio_shifter_|, |playing_|, |last_render_time_|, | 165 // Protects |audio_shifter_|, |prior_elapsed_render_time_|, and |
161 // |total_render_time_| and |volume_|. | 166 // |num_samples_rendered_|. |
162 mutable base::Lock thread_lock_; | 167 mutable base::Lock thread_lock_; |
163 | 168 |
164 // The preferred device id of the output device or empty for the default | 169 // The preferred device id of the output device or empty for the default |
165 // output device. | 170 // output device. |
166 std::string output_device_id_; | 171 std::string output_device_id_; |
167 url::Origin security_origin_; | 172 url::Origin security_origin_; |
168 | 173 |
169 // Cache value for the volume. | 174 // Cache value for the volume. Whenever |sink_| is re-created, its volume |
| 175 // should be set to this. |
170 float volume_; | 176 float volume_; |
171 | 177 |
172 // Flag to indicate whether |sink_| has been started yet. | 178 // Flag to indicate whether |sink_| has been started yet. |
173 bool sink_started_; | 179 bool sink_started_; |
174 | 180 |
175 // Used to DCHECK that some methods are called on the capture audio thread. | 181 // Used to DCHECK that some methods are called on the audio thread. |
176 base::ThreadChecker capture_thread_checker_; | 182 base::ThreadChecker audio_thread_checker_; |
177 | 183 |
178 DISALLOW_COPY_AND_ASSIGN(WebRtcLocalAudioRenderer); | 184 DISALLOW_COPY_AND_ASSIGN(TrackAudioRenderer); |
179 }; | 185 }; |
180 | 186 |
181 } // namespace content | 187 } // namespace content |
182 | 188 |
183 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_LOCAL_AUDIO_RENDERER_H_ | 189 #endif // CONTENT_RENDERER_MEDIA_TRACK_AUDIO_RENDERER_H_ |
OLD | NEW |