ATSParser.h revision d5e56231a598b180a1d898bb7dc61b75580e59a4
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
29namespace android {
30
31struct ABitReader;
32struct ABuffer;
33struct MediaSource;
34
35struct 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,
75        AUDIO
76    };
77    sp<MediaSource> getSource(SourceType type);
78
79    bool PTSTimeDeltaEstablished();
80
81    enum {
82        // From ISO/IEC 13818-1: 2000 (E), Table 2-29
83        STREAMTYPE_RESERVED             = 0x00,
84        STREAMTYPE_MPEG1_VIDEO          = 0x01,
85        STREAMTYPE_MPEG2_VIDEO          = 0x02,
86        STREAMTYPE_MPEG1_AUDIO          = 0x03,
87        STREAMTYPE_MPEG2_AUDIO          = 0x04,
88        STREAMTYPE_MPEG2_AUDIO_ADTS     = 0x0f,
89        STREAMTYPE_MPEG4_VIDEO          = 0x10,
90        STREAMTYPE_H264                 = 0x1b,
91        STREAMTYPE_PCM_AUDIO            = 0x83,
92    };
93
94protected:
95    virtual ~ATSParser();
96
97private:
98    struct Program;
99    struct Stream;
100    struct PSISection;
101
102    uint32_t mFlags;
103    Vector<sp<Program> > mPrograms;
104
105    // Keyed by PID
106    KeyedVector<unsigned, sp<PSISection> > mPSISections;
107
108    int64_t mAbsoluteTimeAnchorUs;
109
110    bool mTimeOffsetValid;
111    int64_t mTimeOffsetUs;
112
113    size_t mNumTSPacketsParsed;
114
115    void parseProgramAssociationTable(ABitReader *br);
116    void parseProgramMap(ABitReader *br);
117    void parsePES(ABitReader *br);
118
119    status_t parsePID(
120        ABitReader *br, unsigned PID,
121        unsigned continuity_counter,
122        unsigned payload_unit_start_indicator);
123
124    void parseAdaptationField(ABitReader *br, unsigned PID);
125    status_t parseTS(ABitReader *br);
126
127    void updatePCR(unsigned PID, uint64_t PCR, size_t byteOffsetFromStart);
128
129    uint64_t mPCR[2];
130    size_t mPCRBytes[2];
131    int64_t mSystemTimeUs[2];
132    size_t mNumPCRs;
133
134    DISALLOW_EVIL_CONSTRUCTORS(ATSParser);
135};
136
137}  // namespace android
138
139#endif  // A_TS_PARSER_H_
140