Update V8 to version 4.5.80.
[chromium-blink-merge.git] / media / blink / webaudiosourceprovider_impl.h
blob1fdb9de8b134432cd47c50d8fb52c4307ccc8c15
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 void SwitchOutputDevice(const std::string& device_id,
52 const GURL& security_origin,
53 const SwitchOutputDeviceCB& callback) override;
54 void Initialize(const AudioParameters& params,
55 RenderCallback* renderer) override;
57 protected:
58 virtual ~WebAudioSourceProviderImpl();
60 private:
61 // Calls setFormat() on |client_| from the Blink renderer thread.
62 void OnSetFormat();
64 // Closure that posts a task to call OnSetFormat() on the renderer thread.
65 base::Closure set_format_cb_;
67 // Set to true when Initialize() is called.
68 int channels_;
69 int sample_rate_;
70 double volume_;
72 // Tracks the current playback state.
73 enum PlaybackState { kStopped, kStarted, kPlaying };
74 PlaybackState state_;
76 // Where audio comes from.
77 AudioRendererSink::RenderCallback* renderer_;
79 // When set via setClient() it overrides |sink_| for consuming audio.
80 blink::WebAudioSourceProviderClient* client_;
82 // Where audio ends up unless overridden by |client_|.
83 base::Lock sink_lock_;
84 scoped_refptr<AudioRendererSink> sink_;
85 scoped_ptr<AudioBus> bus_wrapper_;
87 // NOTE: Weak pointers must be invalidated before all other member variables.
88 base::WeakPtrFactory<WebAudioSourceProviderImpl> weak_factory_;
90 DISALLOW_IMPLICIT_CONSTRUCTORS(WebAudioSourceProviderImpl);
93 } // namespace media
95 #endif // MEDIA_BLINK_WEBAUDIOSOURCEPROVIDER_IMPL_H_