summaryrefslogtreecommitdiffstats
path: root/media/base/synchronizer.h
blob: 122c32f4f82007a5122ddc9dc72c7a4b01483997 (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
// Copyright (c) 2009 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.

// Utility class for video renderers to help synchronize against a reference
// clock while also taking rendering duration into account.
//
// Video renderers should maintain an instance of this object for the entire
// lifetime since Synchronizer maintains internal state to improve playback.
//
// VideoRenderer usage is as follows:
//   Receive a new frame from the decoder
//   Call StartRendering
//   Perform colour space conversion and render the frame
//   Call StopRendering
//   Call CalculateDelay passing in the rendered frame and optional next frame
//   Issue delayed task or sleep on thread for returned amount of time

#ifndef MEDIA_BASE_SYNCHRONIZER_H_
#define MEDIA_BASE_SYNCHRONIZER_H_

#include "base/basictypes.h"
#include "base/time.h"
#include "media/base/buffers.h"

namespace media {

class Synchronizer {
 public:
  Synchronizer();
  ~Synchronizer();

  // Starts the rendering timer.
  void StartRendering();

  // Stops the rendering timer.
  void StopRendering();

  // Calculates the appropriate amount of delay in microseconds given the
  // current time, the current sample and the next sample (may be NULL,
  // but is recommended to pass in for smoother playback).
  void CalculateDelay(base::TimeDelta time, const StreamSample* now,
                      const StreamSample* next, base::TimeDelta* delay_out,
                      bool* should_skip_out);

 private:
  static const int64 kMinFrameDelayUs;
  static const int64 kMaxFrameDelayUs;

  base::TimeTicks rendering_start_;
  base::TimeTicks rendering_stop_;
  base::TimeDelta last_time_;

  DISALLOW_COPY_AND_ASSIGN(Synchronizer);
};

}  // namespace media

#endif  // MEDIA_BASE_SYNCHRONIZER_H_