summaryrefslogtreecommitdiff
path: root/chromium/media/base/null_video_sink.h
blob: 3b972a9a156dfe413f3f88f752c6d60417f1826a (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_BASE_NULL_VIDEO_SINK_H_
#define MEDIA_BASE_NULL_VIDEO_SINK_H_

#include "base/cancelable_callback.h"
#include "base/macros.h"
#include "base/time/default_tick_clock.h"
#include "base/time/tick_clock.h"
#include "media/base/media_export.h"
#include "media/base/video_renderer_sink.h"

namespace base {
class SingleThreadTaskRunner;
}

namespace media {

class MEDIA_EXPORT NullVideoSink : public VideoRendererSink {
 public:
  using NewFrameCB = base::RepeatingCallback<void(scoped_refptr<VideoFrame>)>;

  // Periodically calls |callback| every |interval| on |task_runner| once the
  // sink has been started.  If |clockless| is true, the RenderCallback will
  // be called back to back by repeated post tasks. Optionally, if specified,
  // |new_frame_cb| will be called for each new frame received.
  NullVideoSink(bool clockless,
                base::TimeDelta interval,
                const NewFrameCB& new_frame_cb,
                const scoped_refptr<base::SingleThreadTaskRunner>& task_runner);
  ~NullVideoSink() override;

  // VideoRendererSink implementation.
  void Start(RenderCallback* callback) override;
  void Stop() override;
  void PaintSingleFrame(scoped_refptr<VideoFrame> frame,
                        bool repaint_duplicate_frame) override;

  void set_tick_clock_for_testing(const base::TickClock* tick_clock) {
    tick_clock_ = tick_clock;
  }

  // Sets |stop_cb_|, which will be fired when Stop() is called.
  void set_stop_cb(base::OnceClosure stop_cb) { stop_cb_ = std::move(stop_cb); }

  bool is_started() const { return started_; }

  void set_background_render(bool is_background_rendering) {
    background_render_ = is_background_rendering;
  }

  void set_clockless(bool clockless) { clockless_ = clockless; }

 private:
  // Task that periodically calls Render() to consume video data.
  void CallRender();

  bool clockless_;
  const base::TimeDelta interval_;
  const NewFrameCB new_frame_cb_;
  scoped_refptr<base::SingleThreadTaskRunner> task_runner_;

  bool started_;
  RenderCallback* callback_;

  // Manages cancellation of periodic Render() callback task.
  base::CancelableClosure cancelable_worker_;

  // Used to determine when a new frame is received.
  scoped_refptr<VideoFrame> last_frame_;

  // Used to determine the interval given to RenderCallback::Render() as well as
  // to maintain stable periodicity of callbacks.
  base::TimeTicks current_render_time_;

  // Allow for an injectable tick clock for testing.
  base::TimeTicks last_now_;

  // If specified, used instead of a DefaultTickClock.
  const base::TickClock* tick_clock_;

  // If set, called when Stop() is called.
  base::OnceClosure stop_cb_;

  // Value passed to RenderCallback::Render().
  bool background_render_;

  DISALLOW_COPY_AND_ASSIGN(NullVideoSink);
};

}  // namespace media

#endif  // MEDIA_BASE_NULL_VIDEO_SINK_H_