summaryrefslogtreecommitdiffstats
path: root/media/formats/mp2t/es_adapter_video.h
blob: f2311694aa8e664d05b9b60f7b9ed86e1c2eb869 (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
96
97
98
99
100
101
102
103
// Copyright 2014 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_FORMATS_MP2T_ES_ADAPTER_VIDEO_H_
#define MEDIA_FORMATS_MP2T_ES_ADAPTER_VIDEO_H_

#include <deque>
#include <list>
#include <utility>

#include "base/callback.h"
#include "base/memory/ref_counted.h"
#include "base/time/time.h"
#include "media/base/media_export.h"
#include "media/base/stream_parser_buffer.h"

namespace media {

class VideoDecoderConfig;

namespace mp2t {

// Some constraints of the MSE spec are not necessarily met by video streams
// inside an Mpeg2 TS stream.
// The goal of the ES adapter is to modify the incoming buffers to meet these
// constraints, e.g.
// - get the frame duration,
// - replace the leading non-key frames by the first key frame to avoid
//   creating a hole in the video timeline.
class MEDIA_EXPORT EsAdapterVideo {
 public:
  typedef base::Callback<void(const VideoDecoderConfig&)> NewVideoConfigCB;
  typedef base::Callback<void(scoped_refptr<StreamParserBuffer>)> EmitBufferCB;

  EsAdapterVideo(
      const NewVideoConfigCB& new_video_config_cb,
      const EmitBufferCB& emit_buffer_cb);
  ~EsAdapterVideo();

  // Force the emission of the pending video buffers.
  void Flush();

  // Reset the ES adapter to its initial state.
  void Reset();

  // Provide the configuration that applies to the upcoming video buffers.
  void OnConfigChanged(const VideoDecoderConfig& video_decoder_config);

  // Provide a new video buffer.
  // Returns true when successful.
  bool OnNewBuffer(
      const scoped_refptr<StreamParserBuffer>& stream_parser_buffer);

 private:
  typedef std::deque<scoped_refptr<StreamParserBuffer> > BufferQueue;
  typedef std::pair<int64, VideoDecoderConfig> ConfigEntry;

  void ProcessPendingBuffers(bool flush);

  // Return the PTS of the frame that comes just after |current_pts| in
  // presentation order. Return kNoTimestamp() if not found.
  base::TimeDelta GetNextFramePts(base::TimeDelta current_pts);

  // Replace the leading non key frames by |stream_parser_buffer|
  // (this one must be a key frame).
  void ReplaceDiscardedFrames(
      const scoped_refptr<StreamParserBuffer>& stream_parser_buffer);

  NewVideoConfigCB new_video_config_cb_;
  EmitBufferCB emit_buffer_cb_;

  bool has_valid_config_;
  bool has_valid_frame_;

  // Duration of the last video frame.
  base::TimeDelta last_frame_duration_;

  // Association between a video config and a buffer index.
  std::list<ConfigEntry> config_list_;

  // Global index of the first buffer in |buffer_list_|.
  int64 buffer_index_;

  // List of buffer to be emitted and PTS of frames already emitted.
  BufferQueue buffer_list_;
  std::list<base::TimeDelta> emitted_pts_;

  // Minimum PTS/DTS since the last Reset.
  bool has_valid_initial_timestamp_;
  base::TimeDelta min_pts_;
  DecodeTimestamp min_dts_;

  // Number of frames to replace with the first valid key frame.
  int discarded_frame_count_;

  DISALLOW_COPY_AND_ASSIGN(EsAdapterVideo);
};

}  // namespace mp2t
}  // namespace media

#endif  // MEDIA_FORMATS_MP2T_ES_ADAPTER_VIDEO_H_