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 "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"
21 class AudioParameters;
25 class WebAudioSourceProviderClient;
30 // WebRtcLocalAudioSourceProvider provides a bridge between classes:
31 // WebRtcAudioCapturer ---> WebKit::WebAudioSourceProvider
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.
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) {
43 static const size_t kWebAudioRenderBufferSize;
45 WebRtcLocalAudioSourceProvider();
46 virtual ~WebRtcLocalAudioSourceProvider();
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);
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,
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);
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;
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;
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);
85 // Used to DCHECK that we are called on the correct thread.
86 base::ThreadChecker thread_checker_;
88 scoped_ptr<media::AudioConverter> audio_converter_;
89 scoped_ptr<media::AudioFifo> fifo_;
90 scoped_ptr<media::AudioBus> bus_wrapper_;
95 media::AudioParameters source_params_;
96 media::AudioParameters sink_params_;
98 // Protects all the member variables above.
101 // Used to report the correct delay to |webaudio_source_|.
102 base::TimeTicks last_fill_;
104 DISALLOW_COPY_AND_ASSIGN(WebRtcLocalAudioSourceProvider);
107 } // namespace content
109 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_LOCAL_AUDIO_SOURCE_PROVIDER_H_