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_
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"
19 class WebAudioSourceProviderClient
;
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
) {
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
;
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
;
58 virtual ~WebAudioSourceProviderImpl();
61 // Calls setFormat() on |client_| from the Blink renderer thread.
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.
72 // Tracks the current playback state.
73 enum PlaybackState
{ kStopped
, kStarted
, kPlaying
};
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
);
95 #endif // MEDIA_BLINK_WEBAUDIOSOURCEPROVIDER_IMPL_H_