Fix crash on app list start page contents not existing.
[chromium-blink-merge.git] / content / renderer / media / webrtc_local_audio_source_provider.h
blob4bb8e59b1bb098495f0ef7f6259850706534e83e
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/WebMediaStreamTrack.h"
19 #include "third_party/WebKit/public/platform/WebVector.h"
21 namespace media {
22 class AudioBus;
23 class AudioConverter;
24 class AudioFifo;
25 class AudioParameters;
28 namespace blink {
29 class WebAudioSourceProviderClient;
32 namespace content {
34 // WebRtcLocalAudioSourceProvider provides a bridge between classes:
35 // WebRtcLocalAudioTrack ---> blink::WebAudioSourceProvider
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.
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;
51 explicit WebRtcLocalAudioSourceProvider(
52 const blink::WebMediaStreamTrack& track);
53 virtual ~WebRtcLocalAudioSourceProvider();
55 // MediaStreamAudioSink implementation.
56 void OnData(const media::AudioBus& audio_bus,
57 base::TimeTicks estimated_capture_time) override;
58 void OnSetFormat(const media::AudioParameters& params) override;
59 void OnReadyStateChanged(
60 blink::WebMediaStreamSource::ReadyState state) override;
62 // blink::WebAudioSourceProvider implementation.
63 virtual void setClient(blink::WebAudioSourceProviderClient* client) override;
64 virtual void provideInput(const blink::WebVector<float*>& audio_data,
65 size_t number_of_frames) override;
67 // media::AudioConverter::Inputcallback implementation.
68 // This function is triggered by provideInput()on the WebAudio audio thread,
69 // so it has been under the protection of |lock_|.
70 double ProvideInput(media::AudioBus* audio_bus,
71 base::TimeDelta buffer_delay) override;
73 // Method to allow the unittests to inject its own sink parameters to avoid
74 // query the hardware.
75 // TODO(xians,tommi): Remove and instead offer a way to inject the sink
76 // parameters so that the implementation doesn't rely on the global default
77 // hardware config but instead gets the parameters directly from the sink
78 // (WebAudio in this case). Ideally the unit test should be able to use that
79 // same mechanism to inject the sink parameters for testing.
80 void SetSinkParamsForTesting(const media::AudioParameters& sink_params);
82 private:
83 // Used to DCHECK that some methods are called on the capture audio thread.
84 base::ThreadChecker capture_thread_checker_;
86 scoped_ptr<media::AudioConverter> audio_converter_;
87 scoped_ptr<media::AudioFifo> fifo_;
88 scoped_ptr<media::AudioBus> output_wrapper_;
89 bool is_enabled_;
90 media::AudioParameters source_params_;
91 media::AudioParameters sink_params_;
93 // Protects all the member variables above.
94 base::Lock lock_;
96 // Used to report the correct delay to |webaudio_source_|.
97 base::TimeTicks last_fill_;
99 // The audio track that this source provider is connected to.
100 blink::WebMediaStreamTrack track_;
102 // Flag to tell if the track has been stopped or not.
103 bool track_stopped_;
105 DISALLOW_COPY_AND_ASSIGN(WebRtcLocalAudioSourceProvider);
108 } // namespace content
110 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_LOCAL_AUDIO_SOURCE_PROVIDER_H_