| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MEDIA_AUDIO_FAKE_AUDIO_CONSUMER_H_ | 5 #ifndef MEDIA_AUDIO_FAKE_AUDIO_CONSUMER_H_ |
| 6 #define MEDIA_AUDIO_FAKE_AUDIO_CONSUMER_H_ | 6 #define MEDIA_AUDIO_FAKE_AUDIO_CONSUMER_H_ |
| 7 | 7 |
| 8 #include "base/cancelable_callback.h" | 8 #include "base/callback_forward.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/time.h" | 10 #include "media/base/media_export.h" |
| 11 #include "media/audio/audio_parameters.h" | |
| 12 | 11 |
| 13 namespace base { | 12 namespace base { |
| 14 class MessageLoopProxy; | 13 class MessageLoopProxy; |
| 15 } | 14 } |
| 16 | 15 |
| 17 namespace media { | 16 namespace media { |
| 18 class AudioBus; | 17 class AudioBus; |
| 18 class AudioParameters; |
| 19 | 19 |
| 20 // A fake audio consumer. Using a provided message loop, FakeAudioConsumer will | 20 // A fake audio consumer. Using a provided message loop, FakeAudioConsumer will |
| 21 // simulate a real time consumer of audio data. | 21 // simulate a real time consumer of audio data. |
| 22 class MEDIA_EXPORT FakeAudioConsumer { | 22 class MEDIA_EXPORT FakeAudioConsumer { |
| 23 public: | 23 public: |
| 24 // |message_loop| is the loop on which the ReadCB provided to Start() will be | 24 // |worker_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. | 25 // executed on. This may or may not be the be for the same thread that |
| 26 FakeAudioConsumer(const scoped_refptr<base::MessageLoopProxy>& message_loop, | 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, |
| 27 const AudioParameters& params); | 29 const AudioParameters& params); |
| 28 ~FakeAudioConsumer(); | 30 ~FakeAudioConsumer(); |
| 29 | 31 |
| 30 // Start executing |read_cb| at a regular interval. Must be called on the | 32 // Start executing |read_cb| at a regular intervals. Stop() must be called by |
| 31 // message loop provided during construction. Stop() must be called before | 33 // the same thread before destroying FakeAudioConsumer. |
| 32 // destroying FakeAudioConsumer. | |
| 33 typedef base::Callback<void(AudioBus* audio_bus)> ReadCB; | 34 typedef base::Callback<void(AudioBus* audio_bus)> ReadCB; |
| 34 void Start(const ReadCB& read_cb); | 35 void Start(const ReadCB& read_cb); |
| 35 | 36 |
| 36 // Stop executing the ReadCB provided to Start(). Cancels any outstanding | 37 // Stop executing the ReadCB provided to Start(). Blocks until the worker |
| 37 // callbacks. Safe to call multiple times. Must be called on the message | 38 // loop is not inside a ReadCB invocation. Safe to call multiple times. Must |
| 38 // loop provided during construction. | 39 // be called on the same thread that called Start(). |
| 39 void Stop(); | 40 void Stop(); |
| 40 | 41 |
| 41 private: | 42 private: |
| 42 // Task that regularly calls |read_cb_| according to the playback rate as | 43 // All state and implementation is kept within this class because it must hang |
| 43 // determined by the audio parameters given during construction. Runs on | 44 // around until tasks posted on |worker_loop| have all run (or been canceled). |
| 44 // |message_loop_|. | 45 class Worker; |
| 45 void DoRead(); | 46 Worker* const worker_; |
| 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 | 47 |
| 56 DISALLOW_COPY_AND_ASSIGN(FakeAudioConsumer); | 48 DISALLOW_COPY_AND_ASSIGN(FakeAudioConsumer); |
| 57 }; | 49 }; |
| 58 | 50 |
| 59 } // namespace media | 51 } // namespace media |
| 60 | 52 |
| 61 #endif // MEDIA_AUDIO_FAKE_AUDIO_CONSUMER_H_ | 53 #endif // MEDIA_AUDIO_FAKE_AUDIO_CONSUMER_H_ |
| OLD | NEW |