Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / media / audio / fake_audio_consumer.h
blob793e553e76b415760ef69236dd3c75de8058a18e
1 // Copyright (c) 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_AUDIO_FAKE_AUDIO_CONSUMER_H_
6 #define MEDIA_AUDIO_FAKE_AUDIO_CONSUMER_H_
8 #include "base/cancelable_callback.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/time.h"
11 #include "media/audio/audio_parameters.h"
13 namespace base {
14 class MessageLoopProxy;
17 namespace media {
18 class AudioBus;
20 // A fake audio consumer. Using a provided message loop, FakeAudioConsumer will
21 // simulate a real time consumer of audio data.
22 class MEDIA_EXPORT FakeAudioConsumer {
23 public:
24 // |message_loop| is the loop on which the ReadCB provided to Start() will be
25 // executed on. |params| is used to determine the frequency of callbacks.
26 FakeAudioConsumer(const scoped_refptr<base::MessageLoopProxy>& message_loop,
27 const AudioParameters& params);
28 ~FakeAudioConsumer();
30 // Start executing |read_cb| at a regular interval. Must be called on the
31 // message loop provided during construction. Stop() must be called before
32 // destroying FakeAudioConsumer.
33 typedef base::Callback<void(AudioBus* audio_bus)> ReadCB;
34 void Start(const ReadCB& read_cb);
36 // Stop executing the ReadCB provided to Start(). Cancels any outstanding
37 // callbacks. Safe to call multiple times. Must be called on the message
38 // loop provided during construction.
39 void Stop();
41 private:
42 // Task that regularly calls |read_cb_| according to the playback rate as
43 // determined by the audio parameters given during construction. Runs on
44 // |message_loop_|.
45 void DoRead();
47 scoped_refptr<base::MessageLoopProxy> message_loop_;
48 ReadCB read_cb_;
49 scoped_ptr<AudioBus> audio_bus_;
50 base::TimeDelta buffer_duration_;
51 base::Time next_read_time_;
53 // Used to post delayed tasks to the AudioThread that we can cancel.
54 base::CancelableClosure read_task_cb_;
56 DISALLOW_COPY_AND_ASSIGN(FakeAudioConsumer);
59 } // namespace media
61 #endif // MEDIA_AUDIO_FAKE_AUDIO_CONSUMER_H_