summaryrefslogtreecommitdiffstats
path: root/media/audio/fake_audio_consumer.h
blob: 793e553e76b415760ef69236dd3c75de8058a18e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_AUDIO_FAKE_AUDIO_CONSUMER_H_
#define MEDIA_AUDIO_FAKE_AUDIO_CONSUMER_H_

#include "base/cancelable_callback.h"
#include "base/memory/scoped_ptr.h"
#include "base/time.h"
#include "media/audio/audio_parameters.h"

namespace base {
class MessageLoopProxy;
}

namespace media {
class AudioBus;

// A fake audio consumer.  Using a provided message loop, FakeAudioConsumer will
// simulate a real time consumer of audio data.
class MEDIA_EXPORT FakeAudioConsumer {
 public:
  // |message_loop| is the loop on which the ReadCB provided to Start() will be
  // executed on.  |params| is used to determine the frequency of callbacks.
  FakeAudioConsumer(const scoped_refptr<base::MessageLoopProxy>& message_loop,
                    const AudioParameters& params);
  ~FakeAudioConsumer();

  // Start executing |read_cb| at a regular interval.  Must be called on the
  // message loop provided during construction.  Stop() must be called before
  // destroying FakeAudioConsumer.
  typedef base::Callback<void(AudioBus* audio_bus)> ReadCB;
  void Start(const ReadCB& read_cb);

  // Stop executing the ReadCB provided to Start().  Cancels any outstanding
  // callbacks.  Safe to call multiple times.  Must be called on the message
  // loop provided during construction.
  void Stop();

 private:
  // Task that regularly calls |read_cb_| according to the playback rate as
  // determined by the audio parameters given during construction.  Runs on
  // |message_loop_|.
  void DoRead();

  scoped_refptr<base::MessageLoopProxy> message_loop_;
  ReadCB read_cb_;
  scoped_ptr<AudioBus> audio_bus_;
  base::TimeDelta buffer_duration_;
  base::Time next_read_time_;

  // Used to post delayed tasks to the AudioThread that we can cancel.
  base::CancelableClosure read_task_cb_;

  DISALLOW_COPY_AND_ASSIGN(FakeAudioConsumer);
};

}  // namespace media

#endif  // MEDIA_AUDIO_FAKE_AUDIO_CONSUMER_H_