summaryrefslogtreecommitdiff
path: root/chromium/media/base/fake_audio_worker.h
blob: 097bc0b28acd6eded6a6fd7089acdf2f1810bb15 (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
62
63
64
65
66
67
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_BASE_FAKE_AUDIO_WORKER_H_
#define MEDIA_BASE_FAKE_AUDIO_WORKER_H_

#include "base/callback_forward.h"
#include "base/memory/ref_counted.h"
#include "media/base/media_export.h"

namespace base {
class SingleThreadTaskRunner;
class TimeDelta;
class TimeTicks;
}

namespace media {
class AudioParameters;

// A fake audio worker.  Using a provided message loop, FakeAudioWorker will
// call back the provided callback like a real audio consumer or producer would.
class MEDIA_EXPORT FakeAudioWorker {
 public:
  // The worker callback, which is run at regular intervals. |ideal_time| is
  // when the callback was scheduled to run, while |now| is when the callback is
  // actually being run.
  using Callback = base::RepeatingCallback<void(base::TimeTicks ideal_time,
                                                base::TimeTicks now)>;

  // |worker_task_runner| is the task runner on which the closure provided to
  // Start() will be executed on.  This may or may not be the be for the same
  // thread that invokes the Start/Stop methods.
  // |params| is used to determine the frequency of callbacks.
  FakeAudioWorker(
      const scoped_refptr<base::SingleThreadTaskRunner>& worker_task_runner,
      const AudioParameters& params);

  FakeAudioWorker(const FakeAudioWorker&) = delete;
  FakeAudioWorker& operator=(const FakeAudioWorker&) = delete;

  ~FakeAudioWorker();

  // Start executing |worker_cb| at a regular intervals.  Stop() must be called
  // by the same thread before destroying FakeAudioWorker.
  void Start(Callback worker_cb);

  // Stop executing the closure provided to Start(). Blocks until the worker
  // loop is not inside a closure invocation. Safe to call multiple times.
  // Must be called on the same thread that called Start().
  void Stop();

  // Returns a reasonable fixed output delay value for a "sink" using a
  // FakeAudioWorker.
  static base::TimeDelta ComputeFakeOutputDelay(const AudioParameters& params);

 private:
  // All state and implementation is kept within this ref-counted class because
  // cancellation of posted tasks must happen on the worker thread some time
  // after the call to Stop() (on the main thread) returns.
  class Worker;
  const scoped_refptr<Worker> worker_;
};

}  // namespace media

#endif  // MEDIA_BASE_FAKE_AUDIO_WORKER_H_