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