Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / content / renderer / media / webrtc_audio_renderer.h
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.
4
5 #ifndef CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_RENDERER_H_
6 #define CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_RENDERER_H_
7
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"
17
18 namespace media {
19 class AudioOutputDevice;
20 }
21
22 namespace content {
23
24 class WebRtcAudioRendererSource;
25
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) {
31  public:
32   WebRtcAudioRenderer(int source_render_view_id,
33                       int source_render_frame_id,
34                       int session_id,
35                       int sample_rate,
36                       int frames_per_buffer);
37
38   // Initialize function called by clients like WebRtcAudioDeviceImpl.
39   // Stop() has to be called before |source| is deleted.
40   bool Initialize(WebRtcAudioRendererSource* source);
41
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();
52
53   // Used to DCHECK on the expected state.
54   bool IsStarted() const;
55
56  private:
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;
67
68  protected:
69   virtual ~WebRtcAudioRenderer();
70
71  private:
72   enum State {
73     UNINITIALIZED,
74     PLAYING,
75     PAUSED,
76   };
77
78   // Used to DCHECK that we are called on the correct thread.
79   base::ThreadChecker thread_checker_;
80
81   // Flag to keep track the state of the renderer.
82   State state_;
83
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;
89
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);
93
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_;
98
99   // The sink (destination) for rendered audio.
100   scoped_refptr<media::AudioOutputDevice> sink_;
101
102   // Audio data source from the browser process.
103   WebRtcAudioRendererSource* source_;
104
105   // Buffers used for temporary storage during render callbacks.
106   // Allocated during initialization.
107   scoped_ptr<int16[]> buffer_;
108
109   // Protects access to |state_|, |source_| and |sink_|.
110   base::Lock lock_;
111
112   // Ref count for the MediaPlayers which are playing audio.
113   int play_ref_count_;
114
115   // Ref count for the MediaPlayers which have called Start() but not Stop().
116   int start_ref_count_;
117
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_;
121
122   // Contains the accumulated delay estimate which is provided to the WebRTC
123   // AEC.
124   int audio_delay_milliseconds_;
125
126   // Delay due to the FIFO in milliseconds.
127   int fifo_delay_milliseconds_;
128
129   // The preferred sample rate and buffer sizes provided via the ctor.
130   const int sample_rate_;
131   const int frames_per_buffer_;
132
133   DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioRenderer);
134 };
135
136 }  // namespace content
137
138 #endif  // CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_RENDERER_H_