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 
     42         DISCONTINUITY_SEEK              = DISCONTINUITY_TIME,
     43 
     44         // For legacy reasons this also implies a time discontinuity.
     45         DISCONTINUITY_FORMATCHANGE      =
     46             DISCONTINUITY_AUDIO_FORMAT
     47                 | DISCONTINUITY_VIDEO_FORMAT
     48                 | DISCONTINUITY_TIME,
     49     };
     50 
     51     enum Flags {
     52         // The 90kHz clock (PTS/DTS) is absolute, i.e. PTS=0 corresponds to
     53         // a media time of 0.
     54         // If this flag is _not_ specified, the first PTS encountered in a
     55         // program of this stream will be assumed to correspond to media time 0
     56         // instead.
     57         TS_TIMESTAMPS_ARE_ABSOLUTE = 1
     58     };
     59 
     60     ATSParser(uint32_t flags = 0);
     61 
     62     status_t feedTSPacket(const void *data, size_t size);
     63 
     64     void signalDiscontinuity(
     65             DiscontinuityType type, const sp<AMessage> &extra);
     66 
     67     void signalEOS(status_t finalResult);
     68 
     69     enum SourceType {
     70         VIDEO,
     71         AUDIO
     72     };
     73     sp<MediaSource> getSource(SourceType type);
     74 
     75     bool PTSTimeDeltaEstablished();
     76 
     77     enum {
     78         // From ISO/IEC 13818-1: 2000 (E), Table 2-29
     79         STREAMTYPE_RESERVED             = 0x00,
     80         STREAMTYPE_MPEG1_VIDEO          = 0x01,
     81         STREAMTYPE_MPEG2_VIDEO          = 0x02,
     82         STREAMTYPE_MPEG1_AUDIO          = 0x03,
     83         STREAMTYPE_MPEG2_AUDIO          = 0x04,
     84         STREAMTYPE_MPEG2_AUDIO_ADTS     = 0x0f,
     85         STREAMTYPE_MPEG4_VIDEO          = 0x10,
     86         STREAMTYPE_H264                 = 0x1b,
     87     };
     88 
     89 protected:
     90     virtual ~ATSParser();
     91 
     92 private:
     93     struct Program;
     94     struct Stream;
     95     struct PSISection;
     96 
     97     uint32_t mFlags;
     98     Vector<sp<Program> > mPrograms;
     99 
    100     // Keyed by PID
    101     KeyedVector<unsigned, sp<PSISection> > mPSISections;
    102 
    103     void parseProgramAssociationTable(ABitReader *br);
    104     void parseProgramMap(ABitReader *br);
    105     void parsePES(ABitReader *br);
    106 
    107     status_t parsePID(
    108         ABitReader *br, unsigned PID,
    109         unsigned payload_unit_start_indicator);
    110 
    111     void parseAdaptationField(ABitReader *br);
    112     status_t parseTS(ABitReader *br);
    113 
    114     DISALLOW_EVIL_CONSTRUCTORS(ATSParser);
    115 };
    116 
    117 }  // namespace android
    118 
    119 #endif  // A_TS_PARSER_H_
    120