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,
33 int source_render_frame_id,
36 int frames_per_buffer);
38 // Initialize function called by clients like WebRtcAudioDeviceImpl.
39 // Stop() has to be called before |source| is deleted.
40 bool Initialize(WebRtcAudioRendererSource* source);
42 // When sharing a single instance of WebRtcAudioRenderer between multiple
43 // users (e.g. WebMediaPlayerMS), call this method to create a proxy object
44 // that maintains the Play and Stop states per caller.
45 // The wrapper ensures that Play() won't be called when the caller's state
46 // is "playing", Pause() won't be called when the state already is "paused"
47 // etc and similarly maintains the same state for Stop().
48 // When Stop() is called or when the proxy goes out of scope, the proxy
49 // will ensure that Pause() is called followed by a call to Stop(), which
50 // is the usage pattern that WebRtcAudioRenderer requires.
51 scoped_refptr<MediaStreamAudioRenderer> CreateSharedAudioRendererProxy();
53 // Used to DCHECK on the expected state.
54 bool IsStarted() const;
57 // MediaStreamAudioRenderer implementation. This is private since we want
58 // callers to use proxy objects.
59 // TODO(tommi): Make the MediaStreamAudioRenderer implementation a pimpl?
60 virtual void Start() OVERRIDE;
61 virtual void Play() OVERRIDE;
62 virtual void Pause() OVERRIDE;
63 virtual void Stop() OVERRIDE;
64 virtual void SetVolume(float volume) OVERRIDE;
65 virtual base::TimeDelta GetCurrentRenderTime() const OVERRIDE;
66 virtual bool IsLocalRenderer() const OVERRIDE;
69 virtual ~WebRtcAudioRenderer();
78 // Used to DCHECK that we are called on the correct thread.
79 base::ThreadChecker thread_checker_;
81 // Flag to keep track the state of the renderer.
84 // media::AudioRendererSink::RenderCallback implementation.
85 // These two methods are called on the AudioOutputDevice worker thread.
86 virtual int Render(media::AudioBus* audio_bus,
87 int audio_delay_milliseconds) OVERRIDE;
88 virtual void OnRenderError() OVERRIDE;
90 // Called by AudioPullFifo when more data is necessary.
91 // This method is called on the AudioOutputDevice worker thread.
92 void SourceCallback(int fifo_frame_delay, media::AudioBus* audio_bus);
94 // The render view and frame in which the audio is rendered into |sink_|.
95 const int source_render_view_id_;
96 const int source_render_frame_id_;
97 const int session_id_;
99 // The sink (destination) for rendered audio.
100 scoped_refptr<media::AudioOutputDevice> sink_;
102 // Audio data source from the browser process.
103 WebRtcAudioRendererSource* source_;
105 // Buffers used for temporary storage during render callbacks.
106 // Allocated during initialization.
107 scoped_ptr<int16[]> buffer_;
109 // Protects access to |state_|, |source_| and |sink_|.
112 // Ref count for the MediaPlayers which are playing audio.
115 // Ref count for the MediaPlayers which have called Start() but not Stop().
116 int start_ref_count_;
118 // Used to buffer data between the client and the output device in cases where
119 // the client buffer size is not the same as the output device buffer size.
120 scoped_ptr<media::AudioPullFifo> audio_fifo_;
122 // Contains the accumulated delay estimate which is provided to the WebRTC
124 int audio_delay_milliseconds_;
126 // Delay due to the FIFO in milliseconds.
127 int fifo_delay_milliseconds_;
129 // The preferred sample rate and buffer sizes provided via the ctor.
130 const int sample_rate_;
131 const int frames_per_buffer_;
133 DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioRenderer);
136 } // namespace content
138 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_RENDERER_H_