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.
5 #ifndef CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_RENDERER_H_
6 #define CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_RENDERER_H_
8 #include "base/memory/ref_counted.h"
9 #include "base/synchronization/lock.h"
10 #include "base/threading/thread_checker.h"
11 #include "content/renderer/media/media_stream_audio_renderer.h"
12 #include "content/renderer/media/webrtc_audio_device_impl.h"
13 #include "media/base/audio_decoder.h"
14 #include "media/base/audio_pull_fifo.h"
15 #include "media/base/audio_renderer_sink.h"
16 #include "media/base/channel_layout.h"
19 class AudioOutputDevice;
24 class WebRtcAudioRendererSource;
26 // This renderer handles calls from the pipeline and WebRtc ADM. It is used
27 // for connecting WebRtc MediaStream with the audio pipeline.
28 class CONTENT_EXPORT WebRtcAudioRenderer
29 : NON_EXPORTED_BASE(public media::AudioRendererSink::RenderCallback),
30 NON_EXPORTED_BASE(public MediaStreamAudioRenderer) {
32 WebRtcAudioRenderer(int source_render_view_id,
35 int frames_per_buffer);
37 // Initialize function called by clients like WebRtcAudioDeviceImpl.
38 // Stop() has to be called before |source| is deleted.
39 bool Initialize(WebRtcAudioRendererSource* source);
41 // When sharing a single instance of WebRtcAudioRenderer between multiple
42 // users (e.g. WebMediaPlayerMS), call this method to create a proxy object
43 // that maintains the Play and Stop states per caller.
44 // The wrapper ensures that Play() won't be called when the caller's state
45 // is "playing", Pause() won't be called when the state already is "paused"
46 // etc and similarly maintains the same state for Stop().
47 // When Stop() is called or when the proxy goes out of scope, the proxy
48 // will ensure that Pause() is called followed by a call to Stop(), which
49 // is the usage pattern that WebRtcAudioRenderer requires.
50 scoped_refptr<MediaStreamAudioRenderer> CreateSharedAudioRendererProxy();
52 // Used to DCHECK on the expected state.
53 bool IsStarted() const;
56 // MediaStreamAudioRenderer implementation. This is private since we want
57 // callers to use proxy objects.
58 // TODO(tommi): Make the MediaStreamAudioRenderer implementation a pimpl?
59 virtual void Start() OVERRIDE;
60 virtual void Play() OVERRIDE;
61 virtual void Pause() OVERRIDE;
62 virtual void Stop() OVERRIDE;
63 virtual void SetVolume(float volume) OVERRIDE;
64 virtual base::TimeDelta GetCurrentRenderTime() const OVERRIDE;
65 virtual bool IsLocalRenderer() const OVERRIDE;
68 virtual ~WebRtcAudioRenderer();
77 // Used to DCHECK that we are called on the correct thread.
78 base::ThreadChecker thread_checker_;
80 // Flag to keep track the state of the renderer.
83 // media::AudioRendererSink::RenderCallback implementation.
84 // These two methods are called on the AudioOutputDevice worker thread.
85 virtual int Render(media::AudioBus* audio_bus,
86 int audio_delay_milliseconds) OVERRIDE;
87 virtual void OnRenderError() OVERRIDE;
89 // Called by AudioPullFifo when more data is necessary.
90 // This method is called on the AudioOutputDevice worker thread.
91 void SourceCallback(int fifo_frame_delay, media::AudioBus* audio_bus);
93 // The render view in which the audio is rendered into |sink_|.
94 const int source_render_view_id_;
95 const int session_id_;
97 // The sink (destination) for rendered audio.
98 scoped_refptr<media::AudioOutputDevice> sink_;
100 // Audio data source from the browser process.
101 WebRtcAudioRendererSource* source_;
103 // Buffers used for temporary storage during render callbacks.
104 // Allocated during initialization.
105 scoped_ptr<int16[]> buffer_;
107 // Protects access to |state_|, |source_| and |sink_|.
110 // Ref count for the MediaPlayers which are playing audio.
113 // Ref count for the MediaPlayers which have called Start() but not Stop().
114 int start_ref_count_;
116 // Used to buffer data between the client and the output device in cases where
117 // the client buffer size is not the same as the output device buffer size.
118 scoped_ptr<media::AudioPullFifo> audio_fifo_;
120 // Contains the accumulated delay estimate which is provided to the WebRTC
122 int audio_delay_milliseconds_;
124 // Delay due to the FIFO in milliseconds.
125 int fifo_delay_milliseconds_;
127 // The preferred sample rate and buffer sizes provided via the ctor.
128 const int sample_rate_;
129 const int frames_per_buffer_;
131 DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioRenderer);
134 } // namespace content
136 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_RENDERER_H_