IndexedDBFactory now ForceCloses databases.
[chromium-blink-merge.git] / content / renderer / media / webrtc_local_audio_source_provider.h
blobeb437fabe595a4ebb903872822d1e940bb69d5d1
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.
5 #ifndef CONTENT_RENDERER_MEDIA_WEBRTC_LOCAL_AUDIO_SOURCE_PROVIDER_H_
6 #define CONTENT_RENDERER_MEDIA_WEBRTC_LOCAL_AUDIO_SOURCE_PROVIDER_H_
8 #include <vector>
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/WebVector.h"
20 namespace media {
21 class AudioBus;
22 class AudioConverter;
23 class AudioFifo;
24 class AudioParameters;
27 namespace blink {
28 class WebAudioSourceProviderClient;
31 namespace content {
33 // WebRtcLocalAudioSourceProvider provides a bridge between classes:
34 // WebRtcAudioCapturer ---> blink::WebAudioSourceProvider
36 // WebRtcLocalAudioSourceProvider works as a sink to the WebRtcAudiocapturer
37 // and store the capture data to a FIFO. When the media stream is connected to
38 // WebAudio as a source provider, WebAudio will periodically call
39 // provideInput() to get the data from the FIFO.
41 // All calls are protected by a lock.
42 class CONTENT_EXPORT WebRtcLocalAudioSourceProvider
43 : NON_EXPORTED_BASE(public blink::WebAudioSourceProvider),
44 NON_EXPORTED_BASE(public media::AudioConverter::InputCallback),
45 NON_EXPORTED_BASE(public MediaStreamAudioSink) {
46 public:
47 static const size_t kWebAudioRenderBufferSize;
49 WebRtcLocalAudioSourceProvider();
50 virtual ~WebRtcLocalAudioSourceProvider();
52 // MediaStreamAudioSink implementation.
53 virtual void OnData(const int16* audio_data,
54 int sample_rate,
55 int number_of_channels,
56 int number_of_frames) OVERRIDE;
57 virtual void OnSetFormat(const media::AudioParameters& params) OVERRIDE;
59 // blink::WebAudioSourceProvider implementation.
60 virtual void setClient(blink::WebAudioSourceProviderClient* client) OVERRIDE;
61 virtual void provideInput(const blink::WebVector<float*>& audio_data,
62 size_t number_of_frames) OVERRIDE;
64 // media::AudioConverter::Inputcallback implementation.
65 // This function is triggered by provideInput()on the WebAudio audio thread,
66 // so it has been under the protection of |lock_|.
67 virtual double ProvideInput(media::AudioBus* audio_bus,
68 base::TimeDelta buffer_delay) OVERRIDE;
70 // Method to allow the unittests to inject its own sink parameters to avoid
71 // query the hardware.
72 // TODO(xians,tommi): Remove and instead offer a way to inject the sink
73 // parameters so that the implementation doesn't rely on the global default
74 // hardware config but instead gets the parameters directly from the sink
75 // (WebAudio in this case). Ideally the unit test should be able to use that
76 // same mechanism to inject the sink parameters for testing.
77 void SetSinkParamsForTesting(const media::AudioParameters& sink_params);
79 private:
80 // Used to DCHECK that some methods are called on the capture audio thread.
81 base::ThreadChecker capture_thread_checker_;
83 scoped_ptr<media::AudioConverter> audio_converter_;
84 scoped_ptr<media::AudioFifo> fifo_;
85 scoped_ptr<media::AudioBus> input_bus_;
86 scoped_ptr<media::AudioBus> output_wrapper_;
87 bool is_enabled_;
88 media::AudioParameters source_params_;
89 media::AudioParameters sink_params_;
91 // Protects all the member variables above.
92 base::Lock lock_;
94 // Used to report the correct delay to |webaudio_source_|.
95 base::TimeTicks last_fill_;
97 DISALLOW_COPY_AND_ASSIGN(WebRtcLocalAudioSourceProvider);
100 } // namespace content
102 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_LOCAL_AUDIO_SOURCE_PROVIDER_H_