Update To 11.40.268.0
[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 <vector>
9
10 #include "base/memory/scoped_ptr.h"
11 #include "base/synchronization/lock.h"
12 #include "base/threading/thread_checker.h"
13 #include "base/time/time.h"
14 #include "content/common/content_export.h"
15 #include "content/public/renderer/media_stream_audio_sink.h"
16 #include "media/base/audio_converter.h"
17 #include "third_party/WebKit/public/platform/WebAudioSourceProvider.h"
18 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
19 #include "third_party/WebKit/public/platform/WebVector.h"
20
21 namespace media {
22 class AudioBus;
23 class AudioConverter;
24 class AudioFifo;
25 class AudioParameters;
26 }
27
28 namespace blink {
29 class WebAudioSourceProviderClient;
30 }
31
32 namespace content {
33
34 // WebRtcLocalAudioSourceProvider provides a bridge between classes:
35 //     WebRtcLocalAudioTrack ---> blink::WebAudioSourceProvider
36 //
37 // WebRtcLocalAudioSourceProvider works as a sink to the WebRtcLocalAudioTrack
38 // and store the capture data to a FIFO. When the media stream is connected to
39 // WebAudio MediaStreamAudioSourceNode as a source provider,
40 // MediaStreamAudioSourceNode will periodically call provideInput() to get the
41 // data from the FIFO.
42 //
43 // All calls are protected by a lock.
44 class CONTENT_EXPORT WebRtcLocalAudioSourceProvider
45     :  NON_EXPORTED_BASE(public blink::WebAudioSourceProvider),
46        NON_EXPORTED_BASE(public media::AudioConverter::InputCallback),
47        NON_EXPORTED_BASE(public MediaStreamAudioSink) {
48  public:
49   static const size_t kWebAudioRenderBufferSize;
50
51   explicit WebRtcLocalAudioSourceProvider(
52       const blink::WebMediaStreamTrack& track);
53   virtual ~WebRtcLocalAudioSourceProvider();
54
55   // MediaStreamAudioSink implementation.
56   void OnData(const int16* audio_data,
57               int sample_rate,
58               int number_of_channels,
59               int number_of_frames) override;
60   void OnSetFormat(const media::AudioParameters& params) override;
61   void OnReadyStateChanged(
62       blink::WebMediaStreamSource::ReadyState state) override;
63
64   // blink::WebAudioSourceProvider implementation.
65   virtual void setClient(blink::WebAudioSourceProviderClient* client) override;
66   virtual void provideInput(const blink::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   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 some methods are called on the capture audio thread.
86   base::ThreadChecker capture_thread_checker_;
87
88   scoped_ptr<media::AudioConverter> audio_converter_;
89   scoped_ptr<media::AudioFifo> fifo_;
90   scoped_ptr<media::AudioBus> input_bus_;
91   scoped_ptr<media::AudioBus> output_wrapper_;
92   bool is_enabled_;
93   media::AudioParameters source_params_;
94   media::AudioParameters sink_params_;
95
96   // Protects all the member variables above.
97   base::Lock lock_;
98
99   // Used to report the correct delay to |webaudio_source_|.
100   base::TimeTicks last_fill_;
101
102   // The audio track that this source provider is connected to.
103   blink::WebMediaStreamTrack track_;
104
105   // Flag to tell if the track has been stopped or not.
106   bool track_stopped_;
107
108   DISALLOW_COPY_AND_ASSIGN(WebRtcLocalAudioSourceProvider);
109 };
110
111 }  // namespace content
112
113 #endif  // CONTENT_RENDERER_MEDIA_WEBRTC_LOCAL_AUDIO_SOURCE_PROVIDER_H_