Patch 1/3 to get WebScheduler via WebThread
[chromium-blink-merge.git] / media / blink / webaudiosourceprovider_impl.h
blob77204b2215ca6916b7b3784b53d3364150ba6aa4
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 MEDIA_BLINK_WEBAUDIOSOURCEPROVIDER_IMPL_H_
6 #define MEDIA_BLINK_WEBAUDIOSOURCEPROVIDER_IMPL_H_
8 #include "base/callback.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/synchronization/lock.h"
11 #include "media/base/audio_renderer_sink.h"
12 #include "media/base/media_export.h"
13 #include "third_party/WebKit/public/platform/WebAudioSourceProvider.h"
14 #include "third_party/WebKit/public/platform/WebVector.h"
16 namespace blink {
17 class WebAudioSourceProviderClient;
20 namespace media {
22 // WebAudioSourceProviderImpl provides a bridge between classes:
23 // blink::WebAudioSourceProvider <---> AudioRendererSink
25 // WebAudioSourceProviderImpl wraps an existing audio sink that is used unless
26 // WebKit has set a client via setClient(). While a client is set WebKit will
27 // periodically call provideInput() to render a certain number of audio
28 // sample-frames using the sink's RenderCallback to get the data.
30 // All calls are protected by a lock.
31 class MEDIA_EXPORT WebAudioSourceProviderImpl
32 : NON_EXPORTED_BASE(public blink::WebAudioSourceProvider),
33 NON_EXPORTED_BASE(public AudioRendererSink) {
34 public:
35 explicit WebAudioSourceProviderImpl(
36 const scoped_refptr<AudioRendererSink>& sink);
38 // blink::WebAudioSourceProvider implementation.
39 virtual void setClient(blink::WebAudioSourceProviderClient* client);
40 virtual void provideInput(const blink::WebVector<float*>& audio_data,
41 size_t number_of_frames);
43 // AudioRendererSink implementation.
44 void Start() override;
45 void Stop() override;
46 void Play() override;
47 void Pause() override;
48 bool SetVolume(double volume) override;
49 void Initialize(const AudioParameters& params,
50 RenderCallback* renderer) override;
52 protected:
53 virtual ~WebAudioSourceProviderImpl();
55 private:
56 // Calls setFormat() on |client_| from the Blink renderer thread.
57 void OnSetFormat();
59 // Closure that posts a task to call OnSetFormat() on the renderer thread.
60 base::Closure set_format_cb_;
62 // Set to true when Initialize() is called.
63 int channels_;
64 int sample_rate_;
65 double volume_;
67 // Tracks the current playback state.
68 enum PlaybackState { kStopped, kStarted, kPlaying };
69 PlaybackState state_;
71 // Where audio comes from.
72 AudioRendererSink::RenderCallback* renderer_;
74 // When set via setClient() it overrides |sink_| for consuming audio.
75 blink::WebAudioSourceProviderClient* client_;
77 // Where audio ends up unless overridden by |client_|.
78 base::Lock sink_lock_;
79 scoped_refptr<AudioRendererSink> sink_;
80 scoped_ptr<AudioBus> bus_wrapper_;
82 // NOTE: Weak pointers must be invalidated before all other member variables.
83 base::WeakPtrFactory<WebAudioSourceProviderImpl> weak_factory_;
85 DISALLOW_IMPLICIT_CONSTRUCTORS(WebAudioSourceProviderImpl);
88 } // namespace media
90 #endif // MEDIA_BLINK_WEBAUDIOSOURCEPROVIDER_IMPL_H_