blob: 29303931b13cc1f29baf0be774c25a7c0d6fe0dc [file] [log] [blame]
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001// 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 Murdochbb1529c2013-08-08 10:24:53 +01008#include <vector>
9
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000010#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 Murdoch7dbb3d52013-07-17 14:55:54 +010015#include "content/renderer/media/media_stream_audio_renderer.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000016#include "content/renderer/media/webrtc_audio_device_impl.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010017#include "content/renderer/media/webrtc_local_audio_track.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000018
19namespace media {
20class AudioBus;
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010021class AudioFifo;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010022class AudioOutputDevice;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000023class AudioParameters;
24}
25
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000026namespace content {
27
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000028class WebRtcAudioCapturer;
29
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010030// WebRtcLocalAudioRenderer is a MediaStreamAudioRenderer designed for rendering
31// local audio media stream tracks,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000032// http://dev.w3.org/2011/webrtc/editor/getusermedia.html#mediastreamtrack
33// It also implements media::AudioRendererSink::RenderCallback to render audio
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010034// data provided from a WebRtcLocalAudioTrack source.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000035// 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.
42class CONTENT_EXPORT WebRtcLocalAudioRenderer
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010043 : NON_EXPORTED_BASE(public MediaStreamAudioRenderer),
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000044 NON_EXPORTED_BASE(public media::AudioRendererSink::RenderCallback),
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010045 NON_EXPORTED_BASE(public WebRtcAudioCapturerSink) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000046 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)c2e0dbd2013-05-09 18:35:53 +010050 WebRtcLocalAudioRenderer(WebRtcLocalAudioTrack* audio_track,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000051 int source_render_view_id);
52
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010053 // MediaStreamAudioRenderer implementation.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000054 // 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)c2e0dbd2013-05-09 18:35:53 +010063 const base::TimeDelta& total_render_time() const {
64 return total_render_time_;
65 }
66
67 protected:
68 virtual ~WebRtcLocalAudioRenderer();
69
70 private:
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010071 // WebRtcAudioCapturerSink implementation.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000072
73 // Called on the AudioInputDevice worker thread.
Ben Murdochbb1529c2013-08-08 10:24:53 +010074 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)2a99a7e2013-03-28 15:31:22 +000082
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)c2e0dbd2013-05-09 18:35:53 +010093 // 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)2a99a7e2013-03-28 15:31:22 +000097 // by this class when the sink asks for new data.
98 // The WebRtcAudioCapturer is today created by WebRtcAudioDeviceImpl.
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010099 scoped_refptr<WebRtcLocalAudioTrack> audio_track_;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000100
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)c2e0dbd2013-05-09 18:35:53 +0100105 scoped_refptr<media::AudioOutputDevice> sink_;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000106
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)c2e0dbd2013-05-09 18:35:53 +0100121 // The audio parameters used by the renderer.
122 media::AudioParameters audio_params_;
123
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000124 // Set when playing, cleared when paused.
125 bool playing_;
126
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000127 // 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_