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/webrtc_audio_device_impl.h"
12 #include "media/base/audio_decoder.h"
13 #include "media/base/audio_pull_fifo.h"
14 #include "media/base/audio_renderer_sink.h"
15 #include "webkit/media/media_stream_audio_renderer.h"
19 class RendererAudioOutputDevice
;
20 class WebRtcAudioRendererSource
;
22 // This renderer handles calls from the pipeline and WebRtc ADM. It is used
23 // for connecting WebRtc MediaStream with the audio pipeline.
24 class CONTENT_EXPORT WebRtcAudioRenderer
25 : NON_EXPORTED_BASE(public media::AudioRendererSink::RenderCallback
),
26 NON_EXPORTED_BASE(public webkit_media::MediaStreamAudioRenderer
) {
28 explicit WebRtcAudioRenderer(int source_render_view_id
);
30 // Initialize function called by clients like WebRtcAudioDeviceImpl.
31 // Stop() has to be called before |source| is deleted.
32 bool Initialize(WebRtcAudioRendererSource
* source
);
34 // Methods called by WebMediaPlayerMS and WebRtcAudioDeviceImpl.
35 // MediaStreamAudioRenderer implementation.
36 virtual void Start() OVERRIDE
;
37 virtual void Play() OVERRIDE
;
38 virtual void Pause() OVERRIDE
;
39 virtual void Stop() OVERRIDE
;
40 virtual void SetVolume(float volume
) OVERRIDE
;
41 virtual base::TimeDelta
GetCurrentRenderTime() const OVERRIDE
;
42 virtual bool IsLocalRenderer() const OVERRIDE
;
45 virtual ~WebRtcAudioRenderer();
54 // Used to DCHECK that we are called on the correct thread.
55 base::ThreadChecker thread_checker_
;
57 // Flag to keep track the state of the renderer.
60 // media::AudioRendererSink::RenderCallback implementation.
61 // These two methods are called on the AudioOutputDevice worker thread.
62 virtual int Render(media::AudioBus
* audio_bus
,
63 int audio_delay_milliseconds
) OVERRIDE
;
64 virtual void OnRenderError() OVERRIDE
;
66 // Called by AudioPullFifo when more data is necessary.
67 // This method is called on the AudioOutputDevice worker thread.
68 void SourceCallback(int fifo_frame_delay
, media::AudioBus
* audio_bus
);
70 // The render view in which the audio is rendered into |sink_|.
71 const int source_render_view_id_
;
73 // The sink (destination) for rendered audio.
74 scoped_refptr
<RendererAudioOutputDevice
> sink_
;
76 // Audio data source from the browser process.
77 WebRtcAudioRendererSource
* source_
;
79 // Buffers used for temporary storage during render callbacks.
80 // Allocated during initialization.
81 scoped_ptr
<int16
[]> buffer_
;
83 // Protects access to |state_|, |source_| and |sink_|.
86 // Ref count for the MediaPlayers which are playing audio.
89 // Used to buffer data between the client and the output device in cases where
90 // the client buffer size is not the same as the output device buffer size.
91 scoped_ptr
<media::AudioPullFifo
> audio_fifo_
;
93 // Contains the accumulated delay estimate which is provided to the WebRTC
95 int audio_delay_milliseconds_
;
97 // Lengh of an audio frame in milliseconds.
98 double frame_duration_milliseconds_
;
100 double fifo_io_ratio_
;
102 DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioRenderer
);
105 } // namespace content
107 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_RENDERER_H_