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/callback_forward.h" 9 #include "base/memory/ref_counted.h" 10 #include "media/base/media_export.h" 11 12 namespace base { 13 class MessageLoopProxy; 14 } 15 16 namespace media { 17 class AudioBus; 18 class AudioParameters; 19 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 // |worker_loop| is the loop on which the ReadCB provided to Start() will be 25 // executed on. This may or may not be the be for the same thread that 26 // invokes the Start/Stop methods. 27 // |params| is used to determine the frequency of callbacks. 28 FakeAudioConsumer(const scoped_refptr<base::MessageLoopProxy>& worker_loop, 29 const AudioParameters& params); 30 ~FakeAudioConsumer(); 31 32 // Start executing |read_cb| at a regular intervals. Stop() must be called by 33 // the same thread before destroying FakeAudioConsumer. 34 typedef base::Callback<void(AudioBus* audio_bus)> ReadCB; 35 void Start(const ReadCB& read_cb); 36 37 // Stop executing the ReadCB provided to Start(). Blocks until the worker 38 // loop is not inside a ReadCB invocation. Safe to call multiple times. Must 39 // be called on the same thread that called Start(). 40 void Stop(); 41 42 private: 43 // All state and implementation is kept within this ref-counted class because 44 // cancellation of posted tasks must happen on the worker thread some time 45 // after the call to Stop() (on the main thread) returns. 46 class Worker; 47 const scoped_refptr<Worker> worker_; 48 49 DISALLOW_COPY_AND_ASSIGN(FakeAudioConsumer); 50 }; 51 52 } // namespace media 53 54 #endif // MEDIA_AUDIO_FAKE_AUDIO_CONSUMER_H_ 55