Home | History | Annotate | Download | only in mpeg2ts
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef A_TS_PARSER_H_
     18 
     19 #define A_TS_PARSER_H_
     20 
     21 #include <sys/types.h>
     22 
     23 #include <media/stagefright/foundation/ABase.h>
     24 #include <media/stagefright/foundation/AMessage.h>
     25 #include <utils/KeyedVector.h>
     26 #include <utils/Vector.h>
     27 #include <utils/RefBase.h>
     28 
     29 namespace android {
     30 
     31 struct ABitReader;
     32 struct ABuffer;
     33 struct MediaSource;
     34 
     35 struct ATSParser : public RefBase {
     36     enum DiscontinuityType {
     37         DISCONTINUITY_NONE              = 0,
     38         DISCONTINUITY_TIME              = 1,
     39         DISCONTINUITY_AUDIO_FORMAT      = 2,
     40         DISCONTINUITY_VIDEO_FORMAT      = 4,
     41         DISCONTINUITY_ABSOLUTE_TIME     = 8,
     42         DISCONTINUITY_TIME_OFFSET       = 16,
     43 
     44         DISCONTINUITY_SEEK              = DISCONTINUITY_TIME,
     45 
     46         // For legacy reasons this also implies a time discontinuity.
     47         DISCONTINUITY_FORMATCHANGE      =
     48             DISCONTINUITY_AUDIO_FORMAT
     49                 | DISCONTINUITY_VIDEO_FORMAT
     50                 | DISCONTINUITY_TIME,
     51     };
     52 
     53     enum Flags {
     54         // The 90kHz clock (PTS/DTS) is absolute, i.e. PTS=0 corresponds to
     55         // a media time of 0.
     56         // If this flag is _not_ specified, the first PTS encountered in a
     57         // program of this stream will be assumed to correspond to media time 0
     58         // instead.
     59         TS_TIMESTAMPS_ARE_ABSOLUTE = 1,
     60         // Video PES packets contain exactly one (aligned) access unit.
     61         ALIGNED_VIDEO_DATA         = 2,
     62     };
     63 
     64     ATSParser(uint32_t flags = 0);
     65 
     66     status_t feedTSPacket(const void *data, size_t size);
     67 
     68     void signalDiscontinuity(
     69             DiscontinuityType type, const sp<AMessage> &extra);
     70 
     71     void signalEOS(status_t finalResult);
     72 
     73     enum SourceType {
     74         VIDEO = 0,
     75         AUDIO = 1,
     76         NUM_SOURCE_TYPES = 2
     77     };
     78     sp<MediaSource> getSource(SourceType type);
     79 
     80     bool PTSTimeDeltaEstablished();
     81 
     82     enum {
     83         // From ISO/IEC 13818-1: 2000 (E), Table 2-29
     84         STREAMTYPE_RESERVED             = 0x00,
     85         STREAMTYPE_MPEG1_VIDEO          = 0x01,
     86         STREAMTYPE_MPEG2_VIDEO          = 0x02,
     87         STREAMTYPE_MPEG1_AUDIO          = 0x03,
     88         STREAMTYPE_MPEG2_AUDIO          = 0x04,
     89         STREAMTYPE_MPEG2_AUDIO_ADTS     = 0x0f,
     90         STREAMTYPE_MPEG4_VIDEO          = 0x10,
     91         STREAMTYPE_H264                 = 0x1b,
     92         STREAMTYPE_PCM_AUDIO            = 0x83,
     93     };
     94 
     95 protected:
     96     virtual ~ATSParser();
     97 
     98 private:
     99     struct Program;
    100     struct Stream;
    101     struct PSISection;
    102 
    103     uint32_t mFlags;
    104     Vector<sp<Program> > mPrograms;
    105 
    106     // Keyed by PID
    107     KeyedVector<unsigned, sp<PSISection> > mPSISections;
    108 
    109     int64_t mAbsoluteTimeAnchorUs;
    110 
    111     bool mTimeOffsetValid;
    112     int64_t mTimeOffsetUs;
    113 
    114     size_t mNumTSPacketsParsed;
    115 
    116     void parseProgramAssociationTable(ABitReader *br);
    117     void parseProgramMap(ABitReader *br);
    118     void parsePES(ABitReader *br);
    119 
    120     status_t parsePID(
    121         ABitReader *br, unsigned PID,
    122         unsigned continuity_counter,
    123         unsigned payload_unit_start_indicator);
    124 
    125     void parseAdaptationField(ABitReader *br, unsigned PID);
    126     status_t parseTS(ABitReader *br);
    127 
    128     void updatePCR(unsigned PID, uint64_t PCR, size_t byteOffsetFromStart);
    129 
    130     uint64_t mPCR[2];
    131     size_t mPCRBytes[2];
    132     int64_t mSystemTimeUs[2];
    133     size_t mNumPCRs;
    134 
    135     DISALLOW_EVIL_CONSTRUCTORS(ATSParser);
    136 };
    137 
    138 }  // namespace android
    139 
    140 #endif  // A_TS_PARSER_H_
    141