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