fake_audio_consumer.h revision 7d4cd473f85ac64c3747c96c277f9e506a0d2246
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. 4 5#ifndef MEDIA_AUDIO_FAKE_AUDIO_CONSUMER_H_ 6#define MEDIA_AUDIO_FAKE_AUDIO_CONSUMER_H_ 7 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" 12 13namespace base { 14class MessageLoopProxy; 15} 16 17namespace media { 18class AudioBus; 19 20// A fake audio consumer. Using a provided message loop, FakeAudioConsumer will 21// simulate a real time consumer of audio data. 22class 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(); 29 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); 35 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(); 40 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(); 46 47 scoped_refptr<base::MessageLoopProxy> message_loop_; 48 ReadCB read_cb_; 49 scoped_ptr<AudioBus> audio_bus_; 50 base::TimeDelta buffer_duration_; 51 base::TimeTicks next_read_time_; 52 53 // Used to post delayed tasks to the AudioThread that we can cancel. 54 base::CancelableClosure read_task_cb_; 55 56 DISALLOW_COPY_AND_ASSIGN(FakeAudioConsumer); 57}; 58 59} // namespace media 60 61#endif // MEDIA_AUDIO_FAKE_AUDIO_CONSUMER_H_ 62