- add sources.
[platform/framework/web/crosswalk.git] / src / content / renderer / media / webrtc_local_audio_source_provider.h
1 // Copyright 2013 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_SOURCE_PROVIDER_H_
6 #define CONTENT_RENDERER_MEDIA_WEBRTC_LOCAL_AUDIO_SOURCE_PROVIDER_H_
7
8 #include "base/memory/scoped_ptr.h"
9 #include "base/synchronization/lock.h"
10 #include "base/threading/thread_checker.h"
11 #include "base/time/time.h"
12 #include "content/common/content_export.h"
13 #include "media/base/audio_converter.h"
14 #include "third_party/WebKit/public/platform/WebAudioSourceProvider.h"
15 #include "third_party/WebKit/public/platform/WebVector.h"
16
17 namespace media {
18 class AudioBus;
19 class AudioConverter;
20 class AudioFifo;
21 class AudioParameters;
22 }
23
24 namespace WebKit {
25 class WebAudioSourceProviderClient;
26 }
27
28 namespace content {
29
30 // WebRtcLocalAudioSourceProvider provides a bridge between classes:
31 //     WebRtcAudioCapturer ---> WebKit::WebAudioSourceProvider
32 //
33 // WebRtcLocalAudioSourceProvider works as a sink to the WebRtcAudiocapturer
34 // and store the capture data to a FIFO. When the media stream is connected to
35 // WebAudio as a source provider, WebAudio will periodically call
36 // provideInput() to get the data from the FIFO.
37 //
38 // All calls are protected by a lock.
39 class CONTENT_EXPORT WebRtcLocalAudioSourceProvider
40     : NON_EXPORTED_BASE(public media::AudioConverter::InputCallback),
41       NON_EXPORTED_BASE(public WebKit::WebAudioSourceProvider) {
42  public:
43   static const size_t kWebAudioRenderBufferSize;
44
45   WebRtcLocalAudioSourceProvider();
46   virtual ~WebRtcLocalAudioSourceProvider();
47
48   // Initialize function for the souce provider. This can be called multiple
49   // times if the source format has changed.
50   void Initialize(const media::AudioParameters& source_params);
51
52   // Called by the WebRtcAudioCapturer to deliever captured data into fifo on
53   // the capture audio thread.
54   void DeliverData(media::AudioBus* audio_source,
55                    int audio_delay_milliseconds,
56                    int volume,
57                    bool key_pressed);
58
59   // Called by the WebAudioCapturerSource to get the audio processing params.
60   // This function is triggered by provideInput() on the WebAudio audio thread,
61   // so it has been under the protection of |lock_|.
62   void GetAudioProcessingParams(int* delay_ms, int* volume, bool* key_pressed);
63
64   // WebKit::WebAudioSourceProvider implementation.
65   virtual void setClient(WebKit::WebAudioSourceProviderClient* client) OVERRIDE;
66   virtual void provideInput(const WebKit::WebVector<float*>& audio_data,
67                             size_t number_of_frames) OVERRIDE;
68
69   // media::AudioConverter::Inputcallback implementation.
70   // This function is triggered by provideInput()on the WebAudio audio thread,
71   // so it has been under the protection of |lock_|.
72   virtual double ProvideInput(media::AudioBus* audio_bus,
73                               base::TimeDelta buffer_delay) OVERRIDE;
74
75   // Method to allow the unittests to inject its own sink parameters to avoid
76   // query the hardware.
77   // TODO(xians,tommi): Remove and instead offer a way to inject the sink
78   // parameters so that the implementation doesn't rely on the global default
79   // hardware config but instead gets the parameters directly from the sink
80   // (WebAudio in this case). Ideally the unit test should be able to use that
81   // same mechanism to inject the sink parameters for testing.
82   void SetSinkParamsForTesting(const media::AudioParameters& sink_params);
83
84  private:
85   // Used to DCHECK that we are called on the correct thread.
86   base::ThreadChecker thread_checker_;
87
88   scoped_ptr<media::AudioConverter> audio_converter_;
89   scoped_ptr<media::AudioFifo> fifo_;
90   scoped_ptr<media::AudioBus> bus_wrapper_;
91   int audio_delay_ms_;
92   int volume_;
93   bool key_pressed_;
94   bool is_enabled_;
95   media::AudioParameters source_params_;
96   media::AudioParameters sink_params_;
97
98   // Protects all the member variables above.
99   base::Lock lock_;
100
101   // Used to report the correct delay to |webaudio_source_|.
102   base::TimeTicks last_fill_;
103
104   DISALLOW_COPY_AND_ASSIGN(WebRtcLocalAudioSourceProvider);
105 };
106
107 }  // namespace content
108
109 #endif  // CONTENT_RENDERER_MEDIA_WEBRTC_LOCAL_AUDIO_SOURCE_PROVIDER_H_