webrtc_local_audio_source_provider.h revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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/WebVector.h"
19
20namespace media {
21class AudioBus;
22class AudioConverter;
23class AudioFifo;
24class AudioParameters;
25}
26
27namespace blink {
28class WebAudioSourceProviderClient;
29}
30
31namespace content {
32
33// WebRtcLocalAudioSourceProvider provides a bridge between classes:
34//     WebRtcAudioCapturer ---> blink::WebAudioSourceProvider
35//
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.
40//
41// All calls are protected by a lock.
42class 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;
48
49  WebRtcLocalAudioSourceProvider();
50  virtual ~WebRtcLocalAudioSourceProvider();
51
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;
58
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;
63
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;
69
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);
78
79 private:
80  // Used to DCHECK that some methods are called on the capture audio thread.
81  base::ThreadChecker capture_thread_checker_;
82
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_;
90
91  // Protects all the member variables above.
92  base::Lock lock_;
93
94  // Used to report the correct delay to |webaudio_source_|.
95  base::TimeTicks last_fill_;
96
97  DISALLOW_COPY_AND_ASSIGN(WebRtcLocalAudioSourceProvider);
98};
99
100}  // namespace content
101
102#endif  // CONTENT_RENDERER_MEDIA_WEBRTC_LOCAL_AUDIO_SOURCE_PROVIDER_H_
103