Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / media / formats / mp2t / es_adapter_video.h
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef MEDIA_FORMATS_MP2T_ES_ADAPTER_VIDEO_H_
6 #define MEDIA_FORMATS_MP2T_ES_ADAPTER_VIDEO_H_
7
8 #include <deque>
9 #include <list>
10 #include <utility>
11
12 #include "base/callback.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/time/time.h"
15 #include "media/base/media_export.h"
16 #include "media/base/stream_parser_buffer.h"
17
18 namespace media {
19
20 class VideoDecoderConfig;
21
22 namespace mp2t {
23
24 // Some constraints of the MSE spec are not necessarily met by video streams
25 // inside an Mpeg2 TS stream.
26 // The goal of the ES adapter is to modify the incoming buffers to meet these
27 // constraints, e.g.
28 // - get the frame duration,
29 // - replace the leading non-key frames by the first key frame to avoid
30 //   creating a hole in the video timeline.
31 class MEDIA_EXPORT EsAdapterVideo {
32  public:
33   typedef base::Callback<void(const VideoDecoderConfig&)> NewVideoConfigCB;
34   typedef base::Callback<void(scoped_refptr<StreamParserBuffer>)> EmitBufferCB;
35
36   EsAdapterVideo(
37       const NewVideoConfigCB& new_video_config_cb,
38       const EmitBufferCB& emit_buffer_cb);
39   ~EsAdapterVideo();
40
41   // Force the emission of the pending video buffers.
42   void Flush();
43
44   // Reset the ES adapter to its initial state.
45   void Reset();
46
47   // Provide the configuration that applies to the upcoming video buffers.
48   void OnConfigChanged(const VideoDecoderConfig& video_decoder_config);
49
50   // Provide a new video buffer.
51   // Returns true when successful.
52   bool OnNewBuffer(
53       const scoped_refptr<StreamParserBuffer>& stream_parser_buffer);
54
55  private:
56   typedef std::deque<scoped_refptr<StreamParserBuffer> > BufferQueue;
57   typedef std::pair<int64, VideoDecoderConfig> ConfigEntry;
58
59   void ProcessPendingBuffers(bool flush);
60
61   // Return the PTS of the frame that comes just after |current_pts| in
62   // presentation order. Return kNoTimestamp() if not found.
63   base::TimeDelta GetNextFramePts(base::TimeDelta current_pts);
64
65   // Replace the leading non key frames by |stream_parser_buffer|
66   // (this one must be a key frame).
67   void ReplaceDiscardedFrames(
68       const scoped_refptr<StreamParserBuffer>& stream_parser_buffer);
69
70   NewVideoConfigCB new_video_config_cb_;
71   EmitBufferCB emit_buffer_cb_;
72
73   bool has_valid_config_;
74   bool has_valid_frame_;
75
76   // Duration of the last video frame.
77   base::TimeDelta last_frame_duration_;
78
79   // Association between a video config and a buffer index.
80   std::list<ConfigEntry> config_list_;
81
82   // Global index of the first buffer in |buffer_list_|.
83   int64 buffer_index_;
84
85   // List of buffer to be emitted and PTS of frames already emitted.
86   BufferQueue buffer_list_;
87   std::list<base::TimeDelta> emitted_pts_;
88
89   // Minimum PTS/DTS since the last Reset.
90   bool has_valid_initial_timestamp_;
91   base::TimeDelta min_pts_;
92   DecodeTimestamp min_dts_;
93
94   // Number of frames to replace with the first valid key frame.
95   int discarded_frame_count_;
96
97   DISALLOW_COPY_AND_ASSIGN(EsAdapterVideo);
98 };
99
100 }  // namespace mp2t
101 }  // namespace media
102
103 #endif  // MEDIA_FORMATS_MP2T_ES_ADAPTER_VIDEO_H_