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_PARSER_H_
6 #define MEDIA_FORMATS_MP2T_ES_PARSER_H_
7 
8 #include <stdint.h>
9 
10 #include <list>
11 #include <memory>
12 #include <utility>
13 
14 #include "base/callback.h"
15 #include "base/macros.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/time/time.h"
18 #include "media/base/media_export.h"
19 #include "media/base/stream_parser_buffer.h"
20 
21 namespace media {
22 
23 class DecryptConfig;
24 class OffsetByteQueue;
25 class StreamParserBuffer;
26 
27 namespace mp2t {
28 
29 class MEDIA_EXPORT EsParser {
30  public:
31   using EmitBufferCB =
32       base::RepeatingCallback<void(scoped_refptr<StreamParserBuffer>)>;
33   using GetDecryptConfigCB = base::RepeatingCallback<const DecryptConfig*()>;
34 
35   EsParser();
36   virtual ~EsParser();
37 
38   // ES parsing.
39   // Should use kNoTimestamp when a timestamp is not valid.
40   bool Parse(const uint8_t* buf,
41              int size,
42              base::TimeDelta pts,
43              DecodeTimestamp dts);
44 
45   // Flush any pending buffer.
46   virtual void Flush() = 0;
47 
48   // Reset the state of the ES parser.
49   void Reset();
50 
51  protected:
52   struct TimingDesc {
53     TimingDesc();
54     TimingDesc(DecodeTimestamp dts, base::TimeDelta pts);
55 
56     DecodeTimestamp dts;
57     base::TimeDelta pts;
58   };
59 
60   // Parse ES data from |es_queue_|.
61   // Return true when successful.
62   virtual bool ParseFromEsQueue() = 0;
63 
64   // Reset the internal state of the ES parser.
65   virtual void ResetInternal() = 0;
66 
67   // Get the timing descriptor with the largest byte count that is less or
68   // equal to |es_byte_count|.
69   // This timing descriptor and all the ones that come before (in stream order)
70   // are removed from list |timing_desc_list_|.
71   // If no timing descriptor is found, then the default TimingDesc is returned.
72   TimingDesc GetTimingDescriptor(int64_t es_byte_count);
73 
74   // Bytes of the ES stream that have not been emitted yet.
75   std::unique_ptr<media::OffsetByteQueue> es_queue_;
76 
77  private:
78   // Anchor some timing information into the ES queue.
79   // Here are two examples how this timing info is applied according to
80   // the MPEG-2 TS spec - ISO/IEC 13818:
81   // - "In the case of audio, if a PTS is present in PES packet header it shall
82   // refer to the first access unit commencing in the PES packet. An audio
83   // access unit commences in a PES packet if the first byte of the audio
84   // access unit is present in the PES packet."
85   // - "For AVC video streams conforming to one or more profiles defined
86   // in Annex A of Rec. ITU-T H.264 | ISO/IEC 14496-10 video, if a PTS is
87   // present in the PES packet header, it shall refer to the first AVC access
88   // unit that commences in this PES packet.
89   std::list<std::pair<int64_t, TimingDesc>> timing_desc_list_;
90 
91   DISALLOW_COPY_AND_ASSIGN(EsParser);
92 };
93 
94 }  // namespace mp2t
95 }  // namespace media
96 
97 #endif  // MEDIA_FORMATS_MP2T_ES_PARSER_H_
98