12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifndef MEDIA_AUDIO_FAKE_AUDIO_CONSUMER_H_
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define MEDIA_AUDIO_FAKE_AUDIO_CONSUMER_H_
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/callback_forward.h"
9eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/memory/ref_counted.h"
10eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "media/base/media_export.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace base {
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class SingleThreadTaskRunner;
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace media {
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class AudioBus;
18eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochclass AudioParameters;
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// A fake audio consumer.  Using a provided message loop, FakeAudioConsumer will
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// simulate a real time consumer of audio data.
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class MEDIA_EXPORT FakeAudioConsumer {
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // |worker_task_runner| is the task runner on which the ReadCB provided to
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Start() will be executed on.  This may or may not be the be for the same
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // thread that invokes the Start/Stop methods.
27eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // |params| is used to determine the frequency of callbacks.
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FakeAudioConsumer(
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const scoped_refptr<base::SingleThreadTaskRunner>& worker_task_runner,
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const AudioParameters& params);
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ~FakeAudioConsumer();
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
33eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Start executing |read_cb| at a regular intervals.  Stop() must be called by
34eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // the same thread before destroying FakeAudioConsumer.
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  typedef base::Callback<void(AudioBus* audio_bus)> ReadCB;
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void Start(const ReadCB& read_cb);
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
38eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Stop executing the ReadCB provided to Start().  Blocks until the worker
39eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // loop is not inside a ReadCB invocation.  Safe to call multiple times.  Must
40eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // be called on the same thread that called Start().
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void Stop();
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
44eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // All state and implementation is kept within this ref-counted class because
45eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // cancellation of posted tasks must happen on the worker thread some time
46eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // after the call to Stop() (on the main thread) returns.
47eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  class Worker;
48eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  const scoped_refptr<Worker> worker_;
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(FakeAudioConsumer);
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace media
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif  // MEDIA_AUDIO_FAKE_AUDIO_CONSUMER_H_
56