ATSParser.cpp revision b4a7a2df4c28c3f32b5d877b54831d2cc5d78f81
1cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber/*
2cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber * Copyright (C) 2010 The Android Open Source Project
3cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber *
4cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber * Licensed under the Apache License, Version 2.0 (the "License");
5cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber * you may not use this file except in compliance with the License.
6cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber * You may obtain a copy of the License at
7cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber *
8cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber *      http://www.apache.org/licenses/LICENSE-2.0
9cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber *
10cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber * Unless required by applicable law or agreed to in writing, software
11cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber * distributed under the License is distributed on an "AS IS" BASIS,
12cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber * See the License for the specific language governing permissions and
14cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber * limitations under the License.
15cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber */
16cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
176e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber//#define LOG_NDEBUG 0
186e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber#define LOG_TAG "ATSParser"
196e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber#include <utils/Log.h>
206e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber
21cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber#include "ATSParser.h"
22cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
23cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber#include "AnotherPacketSource.h"
246a63a939601645404fd98f58c19cc38ca818d99eAndreas Huber#include "ESQueue.h"
2585f12e9b9062402d6110df3f7099707912040edbAndreas Huber#include "include/avc_utils.h"
26cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
2785f12e9b9062402d6110df3f7099707912040edbAndreas Huber#include <media/stagefright/foundation/ABitReader.h>
28cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber#include <media/stagefright/foundation/ABuffer.h>
29cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber#include <media/stagefright/foundation/ADebug.h>
30cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber#include <media/stagefright/foundation/AMessage.h>
31cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber#include <media/stagefright/foundation/hexdump.h>
32cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber#include <media/stagefright/MediaDefs.h>
33cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber#include <media/stagefright/MediaErrors.h>
34cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber#include <media/stagefright/MetaData.h>
358dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber#include <media/stagefright/Utils.h>
3632f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber#include <media/IStreamSource.h>
37cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber#include <utils/KeyedVector.h>
38cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
39b4a7a2df4c28c3f32b5d877b54831d2cc5d78f81Colin Cross#include <inttypes.h>
40b4a7a2df4c28c3f32b5d877b54831d2cc5d78f81Colin Cross
41cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Hubernamespace android {
42cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
436e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber// I want the expression "y" evaluated even if verbose logging is off.
446e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber#define MY_LOGV(x, y) \
453856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    do { unsigned tmp = y; ALOGV(x, tmp); } while (0)
466e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber
47cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huberstatic const size_t kTSPacketSize = 188;
48cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
49cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huberstruct ATSParser::Program : public RefBase {
50386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber    Program(ATSParser *parser, unsigned programNumber, unsigned programMapPID);
51cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
528dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    bool parsePSISection(
538dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber            unsigned pid, ABitReader *br, status_t *err);
548dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
55cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    bool parsePID(
5687f2a558dd12043631e12c361abef301bf603140Andreas Huber            unsigned pid, unsigned continuity_counter,
5787f2a558dd12043631e12c361abef301bf603140Andreas Huber            unsigned payload_unit_start_indicator,
5806528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber            ABitReader *br, status_t *err);
59cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
6032f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber    void signalDiscontinuity(
6132f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber            DiscontinuityType type, const sp<AMessage> &extra);
6232f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber
63f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    void signalEOS(status_t finalResult);
642a4d22d79e927f2245537921e10fc5fda1c47a29Andreas Huber
65cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    sp<MediaSource> getSource(SourceType type);
66cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
67bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber    int64_t convertPTSToTimestamp(uint64_t PTS);
68bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber
6943c3e6ce02215ca99d506458f596cb1211639f29Andreas Huber    bool PTSTimeDeltaEstablished() const {
7043c3e6ce02215ca99d506458f596cb1211639f29Andreas Huber        return mFirstPTSValid;
7143c3e6ce02215ca99d506458f596cb1211639f29Andreas Huber    }
7243c3e6ce02215ca99d506458f596cb1211639f29Andreas Huber
73386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber    unsigned number() const { return mProgramNumber; }
74386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber
75386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber    void updateProgramMapPID(unsigned programMapPID) {
76386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber        mProgramMapPID = programMapPID;
77386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber    }
78386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber
798dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    unsigned programMapPID() const {
808dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber        return mProgramMapPID;
818dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    }
828dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
8387f2a558dd12043631e12c361abef301bf603140Andreas Huber    uint32_t parserFlags() const {
8487f2a558dd12043631e12c361abef301bf603140Andreas Huber        return mParser->mFlags;
8587f2a558dd12043631e12c361abef301bf603140Andreas Huber    }
8687f2a558dd12043631e12c361abef301bf603140Andreas Huber
87cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huberprivate:
88f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    ATSParser *mParser;
89386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber    unsigned mProgramNumber;
90cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    unsigned mProgramMapPID;
91cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    KeyedVector<unsigned, sp<Stream> > mStreams;
92bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber    bool mFirstPTSValid;
93bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber    uint64_t mFirstPTS;
94cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
9506528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber    status_t parseProgramMap(ABitReader *br);
96cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
97cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    DISALLOW_EVIL_CONSTRUCTORS(Program);
98cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber};
99cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
100cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huberstruct ATSParser::Stream : public RefBase {
10187f2a558dd12043631e12c361abef301bf603140Andreas Huber    Stream(Program *program,
10287f2a558dd12043631e12c361abef301bf603140Andreas Huber           unsigned elementaryPID,
10387f2a558dd12043631e12c361abef301bf603140Andreas Huber           unsigned streamType,
10487f2a558dd12043631e12c361abef301bf603140Andreas Huber           unsigned PCR_PID);
105cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
106bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber    unsigned type() const { return mStreamType; }
107bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber    unsigned pid() const { return mElementaryPID; }
108bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber    void setPID(unsigned pid) { mElementaryPID = pid; }
109bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber
1105403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber    status_t parse(
11187f2a558dd12043631e12c361abef301bf603140Andreas Huber            unsigned continuity_counter,
112cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            unsigned payload_unit_start_indicator,
113cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            ABitReader *br);
114cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
11532f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber    void signalDiscontinuity(
11632f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber            DiscontinuityType type, const sp<AMessage> &extra);
11732f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber
118f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    void signalEOS(status_t finalResult);
1192a4d22d79e927f2245537921e10fc5fda1c47a29Andreas Huber
120cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    sp<MediaSource> getSource(SourceType type);
121cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
122cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huberprotected:
123cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    virtual ~Stream();
124cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
125cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huberprivate:
126bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber    Program *mProgram;
127cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    unsigned mElementaryPID;
128cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    unsigned mStreamType;
12987f2a558dd12043631e12c361abef301bf603140Andreas Huber    unsigned mPCR_PID;
13087f2a558dd12043631e12c361abef301bf603140Andreas Huber    int32_t mExpectedContinuityCounter;
131cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
132cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    sp<ABuffer> mBuffer;
133cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    sp<AnotherPacketSource> mSource;
134cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    bool mPayloadStarted;
135cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
13690a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber    uint64_t mPrevPTS;
13790a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber
138386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber    ElementaryStreamQueue *mQueue;
1396a63a939601645404fd98f58c19cc38ca818d99eAndreas Huber
1405403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber    status_t flush();
1415403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber    status_t parsePES(ABitReader *br);
142cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
143cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    void onPayloadData(
144cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            unsigned PTS_DTS_flags, uint64_t PTS, uint64_t DTS,
145cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            const uint8_t *data, size_t size);
146cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
14782f7321b03eec1e40af9d681370f754ee0279582Andreas Huber    void extractAACFrames(const sp<ABuffer> &buffer);
14882f7321b03eec1e40af9d681370f754ee0279582Andreas Huber
1496e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber    bool isAudio() const;
1506e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber    bool isVideo() const;
1516e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber
152cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    DISALLOW_EVIL_CONSTRUCTORS(Stream);
153cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber};
154cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
1558dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huberstruct ATSParser::PSISection : public RefBase {
1568dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    PSISection();
1578dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
1588dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    status_t append(const void *data, size_t size);
1598dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    void clear();
1608dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
1618dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    bool isComplete() const;
1628dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    bool isEmpty() const;
1638dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
1648dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    const uint8_t *data() const;
1658dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    size_t size() const;
1668dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
1678dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huberprotected:
1688dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    virtual ~PSISection();
1698dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
1708dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huberprivate:
1718dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    sp<ABuffer> mBuffer;
1728dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
1738dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    DISALLOW_EVIL_CONSTRUCTORS(PSISection);
1748dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber};
1758dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
176cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber////////////////////////////////////////////////////////////////////////////////
177cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
178386d609dc513e838c7e7c4c46c604493ccd560beAndreas HuberATSParser::Program::Program(
179386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber        ATSParser *parser, unsigned programNumber, unsigned programMapPID)
180f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    : mParser(parser),
181386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber      mProgramNumber(programNumber),
182f933441648ef6a71dee783d733aac17b9508b452Andreas Huber      mProgramMapPID(programMapPID),
183bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber      mFirstPTSValid(false),
184bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber      mFirstPTS(0) {
1853856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("new program number %u", programNumber);
186cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber}
187cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
1888dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huberbool ATSParser::Program::parsePSISection(
1898dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber        unsigned pid, ABitReader *br, status_t *err) {
19006528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber    *err = OK;
19106528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
1928dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    if (pid != mProgramMapPID) {
1938dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber        return false;
1948dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    }
195cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
1968dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    *err = parseProgramMap(br);
19706528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
1988dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    return true;
1998dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber}
2008dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
2018dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huberbool ATSParser::Program::parsePID(
20287f2a558dd12043631e12c361abef301bf603140Andreas Huber        unsigned pid, unsigned continuity_counter,
20387f2a558dd12043631e12c361abef301bf603140Andreas Huber        unsigned payload_unit_start_indicator,
2048dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber        ABitReader *br, status_t *err) {
2058dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    *err = OK;
206cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
207cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    ssize_t index = mStreams.indexOfKey(pid);
208cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    if (index < 0) {
209cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        return false;
210cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    }
211cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
2125403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber    *err = mStreams.editValueAt(index)->parse(
21387f2a558dd12043631e12c361abef301bf603140Andreas Huber            continuity_counter, payload_unit_start_indicator, br);
214cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
215cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    return true;
216cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber}
217cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
21832f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Hubervoid ATSParser::Program::signalDiscontinuity(
21932f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber        DiscontinuityType type, const sp<AMessage> &extra) {
220b7c8e91880463ff4981e3e53e98e45d68e2fe374Andreas Huber    int64_t mediaTimeUs;
221b7c8e91880463ff4981e3e53e98e45d68e2fe374Andreas Huber    if ((type & DISCONTINUITY_TIME)
222b7c8e91880463ff4981e3e53e98e45d68e2fe374Andreas Huber            && extra != NULL
223b7c8e91880463ff4981e3e53e98e45d68e2fe374Andreas Huber            && extra->findInt64(
224b7c8e91880463ff4981e3e53e98e45d68e2fe374Andreas Huber                IStreamListener::kKeyMediaTimeUs, &mediaTimeUs)) {
225b7c8e91880463ff4981e3e53e98e45d68e2fe374Andreas Huber        mFirstPTSValid = false;
226b7c8e91880463ff4981e3e53e98e45d68e2fe374Andreas Huber    }
227b7c8e91880463ff4981e3e53e98e45d68e2fe374Andreas Huber
2282a4d22d79e927f2245537921e10fc5fda1c47a29Andreas Huber    for (size_t i = 0; i < mStreams.size(); ++i) {
22932f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber        mStreams.editValueAt(i)->signalDiscontinuity(type, extra);
230f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
231f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
232f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
233f933441648ef6a71dee783d733aac17b9508b452Andreas Hubervoid ATSParser::Program::signalEOS(status_t finalResult) {
234f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    for (size_t i = 0; i < mStreams.size(); ++i) {
235f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        mStreams.editValueAt(i)->signalEOS(finalResult);
2362a4d22d79e927f2245537921e10fc5fda1c47a29Andreas Huber    }
2372a4d22d79e927f2245537921e10fc5fda1c47a29Andreas Huber}
2382a4d22d79e927f2245537921e10fc5fda1c47a29Andreas Huber
239bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huberstruct StreamInfo {
240bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber    unsigned mType;
241bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber    unsigned mPID;
242bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber};
243bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber
24406528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huberstatus_t ATSParser::Program::parseProgramMap(ABitReader *br) {
245cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    unsigned table_id = br->getBits(8);
2463856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("  table_id = %u", table_id);
247cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    CHECK_EQ(table_id, 0x02u);
248cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
2496e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber    unsigned section_syntax_indicator = br->getBits(1);
2503856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("  section_syntax_indicator = %u", section_syntax_indicator);
2516e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber    CHECK_EQ(section_syntax_indicator, 1u);
252cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
253cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    CHECK_EQ(br->getBits(1), 0u);
2546e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber    MY_LOGV("  reserved = %u", br->getBits(2));
255cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
256cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    unsigned section_length = br->getBits(12);
2573856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("  section_length = %u", section_length);
258f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    CHECK_EQ(section_length & 0xc00, 0u);
259cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    CHECK_LE(section_length, 1021u);
260cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
2616e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber    MY_LOGV("  program_number = %u", br->getBits(16));
2626e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber    MY_LOGV("  reserved = %u", br->getBits(2));
2636e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber    MY_LOGV("  version_number = %u", br->getBits(5));
2646e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber    MY_LOGV("  current_next_indicator = %u", br->getBits(1));
2656e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber    MY_LOGV("  section_number = %u", br->getBits(8));
2666e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber    MY_LOGV("  last_section_number = %u", br->getBits(8));
2676e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber    MY_LOGV("  reserved = %u", br->getBits(3));
26887f2a558dd12043631e12c361abef301bf603140Andreas Huber
26987f2a558dd12043631e12c361abef301bf603140Andreas Huber    unsigned PCR_PID = br->getBits(13);
27087f2a558dd12043631e12c361abef301bf603140Andreas Huber    ALOGV("  PCR_PID = 0x%04x", PCR_PID);
27187f2a558dd12043631e12c361abef301bf603140Andreas Huber
2726e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber    MY_LOGV("  reserved = %u", br->getBits(4));
273cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
274cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    unsigned program_info_length = br->getBits(12);
2753856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("  program_info_length = %u", program_info_length);
276f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    CHECK_EQ(program_info_length & 0xc00, 0u);
277cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
278cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    br->skipBits(program_info_length * 8);  // skip descriptors
279cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
280bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber    Vector<StreamInfo> infos;
281bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber
282cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    // infoBytesRemaining is the number of bytes that make up the
283cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    // variable length section of ES_infos. It does not include the
284cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    // final CRC.
285cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    size_t infoBytesRemaining = section_length - 9 - program_info_length - 4;
286cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
287cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    while (infoBytesRemaining > 0) {
288cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        CHECK_GE(infoBytesRemaining, 5u);
289cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
290cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        unsigned streamType = br->getBits(8);
2913856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("    stream_type = 0x%02x", streamType);
292cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
2936e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber        MY_LOGV("    reserved = %u", br->getBits(3));
294cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
295cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        unsigned elementaryPID = br->getBits(13);
2963856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("    elementary_PID = 0x%04x", elementaryPID);
297cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
2986e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber        MY_LOGV("    reserved = %u", br->getBits(4));
299cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
300cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        unsigned ES_info_length = br->getBits(12);
3013856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("    ES_info_length = %u", ES_info_length);
302f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        CHECK_EQ(ES_info_length & 0xc00, 0u);
303cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
304cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        CHECK_GE(infoBytesRemaining - 5, ES_info_length);
305cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
306cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber#if 0
307cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        br->skipBits(ES_info_length * 8);  // skip descriptors
308cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber#else
309cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        unsigned info_bytes_remaining = ES_info_length;
310cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        while (info_bytes_remaining >= 2) {
3116e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber            MY_LOGV("      tag = 0x%02x", br->getBits(8));
312cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
313cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            unsigned descLength = br->getBits(8);
3143856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("      len = %u", descLength);
315cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
316cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            CHECK_GE(info_bytes_remaining, 2 + descLength);
317cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
318cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            br->skipBits(descLength * 8);
319cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
320cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            info_bytes_remaining -= descLength + 2;
321cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        }
322cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        CHECK_EQ(info_bytes_remaining, 0u);
323cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber#endif
324cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
325bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber        StreamInfo info;
326bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber        info.mType = streamType;
327bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber        info.mPID = elementaryPID;
328bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber        infos.push(info);
329cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
330cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        infoBytesRemaining -= 5 + ES_info_length;
331cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    }
332cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
333cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    CHECK_EQ(infoBytesRemaining, 0u);
3346e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber    MY_LOGV("  CRC = 0x%08x", br->getBits(32));
335bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber
336bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber    bool PIDsChanged = false;
337bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber    for (size_t i = 0; i < infos.size(); ++i) {
338bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber        StreamInfo &info = infos.editItemAt(i);
339bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber
340bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber        ssize_t index = mStreams.indexOfKey(info.mPID);
341bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber
342bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber        if (index >= 0 && mStreams.editValueAt(index)->type() != info.mType) {
343df64d15042bbd5e0e4933ac49bf3c177dd94752cSteve Block            ALOGI("uh oh. stream PIDs have changed.");
344bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber            PIDsChanged = true;
345bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber            break;
346bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber        }
347bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber    }
348bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber
349bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber    if (PIDsChanged) {
35006528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber#if 0
351df64d15042bbd5e0e4933ac49bf3c177dd94752cSteve Block        ALOGI("before:");
35206528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber        for (size_t i = 0; i < mStreams.size(); ++i) {
35306528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber            sp<Stream> stream = mStreams.editValueAt(i);
35406528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
355df64d15042bbd5e0e4933ac49bf3c177dd94752cSteve Block            ALOGI("PID 0x%08x => type 0x%02x", stream->pid(), stream->type());
35606528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber        }
35706528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
358df64d15042bbd5e0e4933ac49bf3c177dd94752cSteve Block        ALOGI("after:");
35906528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber        for (size_t i = 0; i < infos.size(); ++i) {
36006528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber            StreamInfo &info = infos.editItemAt(i);
36106528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
362df64d15042bbd5e0e4933ac49bf3c177dd94752cSteve Block            ALOGI("PID 0x%08x => type 0x%02x", info.mPID, info.mType);
36306528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber        }
36406528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber#endif
36506528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
36606528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber        // The only case we can recover from is if we have two streams
36706528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber        // and they switched PIDs.
36806528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
36906528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber        bool success = false;
37006528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
37106528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber        if (mStreams.size() == 2 && infos.size() == 2) {
37206528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber            const StreamInfo &info1 = infos.itemAt(0);
37306528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber            const StreamInfo &info2 = infos.itemAt(1);
37406528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
37506528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber            sp<Stream> s1 = mStreams.editValueAt(0);
37606528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber            sp<Stream> s2 = mStreams.editValueAt(1);
37706528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
37806528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber            bool caseA =
37906528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber                info1.mPID == s1->pid() && info1.mType == s2->type()
38006528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber                    && info2.mPID == s2->pid() && info2.mType == s1->type();
38106528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
38206528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber            bool caseB =
38306528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber                info1.mPID == s2->pid() && info1.mType == s1->type()
38406528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber                    && info2.mPID == s1->pid() && info2.mType == s2->type();
38506528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
38606528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber            if (caseA || caseB) {
38706528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber                unsigned pid1 = s1->pid();
38806528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber                unsigned pid2 = s2->pid();
38906528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber                s1->setPID(pid2);
39006528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber                s2->setPID(pid1);
39106528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
39206528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber                mStreams.clear();
39306528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber                mStreams.add(s1->pid(), s1);
39406528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber                mStreams.add(s2->pid(), s2);
39506528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
39606528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber                success = true;
39706528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber            }
39806528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber        }
39906528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
40006528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber        if (!success) {
401df64d15042bbd5e0e4933ac49bf3c177dd94752cSteve Block            ALOGI("Stream PIDs changed and we cannot recover.");
40206528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber            return ERROR_MALFORMED;
40306528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber        }
404bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber    }
405bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber
406bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber    for (size_t i = 0; i < infos.size(); ++i) {
407bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber        StreamInfo &info = infos.editItemAt(i);
408bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber
409bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber        ssize_t index = mStreams.indexOfKey(info.mPID);
410bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber
411bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber        if (index < 0) {
41287f2a558dd12043631e12c361abef301bf603140Andreas Huber            sp<Stream> stream = new Stream(
41387f2a558dd12043631e12c361abef301bf603140Andreas Huber                    this, info.mPID, info.mType, PCR_PID);
41487f2a558dd12043631e12c361abef301bf603140Andreas Huber
415bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber            mStreams.add(info.mPID, stream);
416bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber        }
417bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber    }
41806528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
41906528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber    return OK;
420cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber}
421cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
422cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Hubersp<MediaSource> ATSParser::Program::getSource(SourceType type) {
423386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber    size_t index = (type == AUDIO) ? 0 : 0;
424decd96988e495133e4a1728f612d4c9fdb4d218eAndreas Huber
425cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    for (size_t i = 0; i < mStreams.size(); ++i) {
426cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        sp<MediaSource> source = mStreams.editValueAt(i)->getSource(type);
427cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        if (source != NULL) {
428decd96988e495133e4a1728f612d4c9fdb4d218eAndreas Huber            if (index == 0) {
429decd96988e495133e4a1728f612d4c9fdb4d218eAndreas Huber                return source;
430decd96988e495133e4a1728f612d4c9fdb4d218eAndreas Huber            }
431decd96988e495133e4a1728f612d4c9fdb4d218eAndreas Huber            --index;
432cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        }
433cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    }
434cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
435cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    return NULL;
436cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber}
437cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
438bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huberint64_t ATSParser::Program::convertPTSToTimestamp(uint64_t PTS) {
439c4c17d47b674b425fb6c399822c0ab3258543c0aAndreas Huber    if (!(mParser->mFlags & TS_TIMESTAMPS_ARE_ABSOLUTE)) {
440c4c17d47b674b425fb6c399822c0ab3258543c0aAndreas Huber        if (!mFirstPTSValid) {
441c4c17d47b674b425fb6c399822c0ab3258543c0aAndreas Huber            mFirstPTSValid = true;
442c4c17d47b674b425fb6c399822c0ab3258543c0aAndreas Huber            mFirstPTS = PTS;
443c4c17d47b674b425fb6c399822c0ab3258543c0aAndreas Huber            PTS = 0;
444c4c17d47b674b425fb6c399822c0ab3258543c0aAndreas Huber        } else if (PTS < mFirstPTS) {
445c4c17d47b674b425fb6c399822c0ab3258543c0aAndreas Huber            PTS = 0;
446c4c17d47b674b425fb6c399822c0ab3258543c0aAndreas Huber        } else {
447c4c17d47b674b425fb6c399822c0ab3258543c0aAndreas Huber            PTS -= mFirstPTS;
448c4c17d47b674b425fb6c399822c0ab3258543c0aAndreas Huber        }
449bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber    }
450bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber
45187f2a558dd12043631e12c361abef301bf603140Andreas Huber    int64_t timeUs = (PTS * 100) / 9;
45287f2a558dd12043631e12c361abef301bf603140Andreas Huber
45387f2a558dd12043631e12c361abef301bf603140Andreas Huber    if (mParser->mAbsoluteTimeAnchorUs >= 0ll) {
45487f2a558dd12043631e12c361abef301bf603140Andreas Huber        timeUs += mParser->mAbsoluteTimeAnchorUs;
45587f2a558dd12043631e12c361abef301bf603140Andreas Huber    }
45687f2a558dd12043631e12c361abef301bf603140Andreas Huber
457d5e56231a598b180a1d898bb7dc61b75580e59a4Andreas Huber    if (mParser->mTimeOffsetValid) {
458d5e56231a598b180a1d898bb7dc61b75580e59a4Andreas Huber        timeUs += mParser->mTimeOffsetUs;
459d5e56231a598b180a1d898bb7dc61b75580e59a4Andreas Huber    }
460d5e56231a598b180a1d898bb7dc61b75580e59a4Andreas Huber
46187f2a558dd12043631e12c361abef301bf603140Andreas Huber    return timeUs;
462bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber}
463bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber
464cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber////////////////////////////////////////////////////////////////////////////////
465cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
466bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas HuberATSParser::Stream::Stream(
46787f2a558dd12043631e12c361abef301bf603140Andreas Huber        Program *program,
46887f2a558dd12043631e12c361abef301bf603140Andreas Huber        unsigned elementaryPID,
46987f2a558dd12043631e12c361abef301bf603140Andreas Huber        unsigned streamType,
47087f2a558dd12043631e12c361abef301bf603140Andreas Huber        unsigned PCR_PID)
471bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber    : mProgram(program),
472bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber      mElementaryPID(elementaryPID),
473cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber      mStreamType(streamType),
47487f2a558dd12043631e12c361abef301bf603140Andreas Huber      mPCR_PID(PCR_PID),
47587f2a558dd12043631e12c361abef301bf603140Andreas Huber      mExpectedContinuityCounter(-1),
4766a63a939601645404fd98f58c19cc38ca818d99eAndreas Huber      mPayloadStarted(false),
47790a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber      mPrevPTS(0),
478386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber      mQueue(NULL) {
479386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber    switch (mStreamType) {
480386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber        case STREAMTYPE_H264:
48187f2a558dd12043631e12c361abef301bf603140Andreas Huber            mQueue = new ElementaryStreamQueue(
48287f2a558dd12043631e12c361abef301bf603140Andreas Huber                    ElementaryStreamQueue::H264,
48387f2a558dd12043631e12c361abef301bf603140Andreas Huber                    (mProgram->parserFlags() & ALIGNED_VIDEO_DATA)
48487f2a558dd12043631e12c361abef301bf603140Andreas Huber                        ? ElementaryStreamQueue::kFlag_AlignedData : 0);
485386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber            break;
4866e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber        case STREAMTYPE_MPEG2_AUDIO_ADTS:
487386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber            mQueue = new ElementaryStreamQueue(ElementaryStreamQueue::AAC);
488386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber            break;
489386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber        case STREAMTYPE_MPEG1_AUDIO:
490386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber        case STREAMTYPE_MPEG2_AUDIO:
491386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber            mQueue = new ElementaryStreamQueue(
492386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber                    ElementaryStreamQueue::MPEG_AUDIO);
493386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber            break;
494386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber
495386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber        case STREAMTYPE_MPEG1_VIDEO:
496386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber        case STREAMTYPE_MPEG2_VIDEO:
497386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber            mQueue = new ElementaryStreamQueue(
498386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber                    ElementaryStreamQueue::MPEG_VIDEO);
499386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber            break;
500386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber
501386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber        case STREAMTYPE_MPEG4_VIDEO:
502386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber            mQueue = new ElementaryStreamQueue(
503386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber                    ElementaryStreamQueue::MPEG4_VIDEO);
504386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber            break;
505386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber
50690a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber        case STREAMTYPE_PCM_AUDIO:
50790a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber            mQueue = new ElementaryStreamQueue(
50890a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber                    ElementaryStreamQueue::PCM_AUDIO);
50990a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber            break;
51090a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber
511d3c079ae9859011d118f94616d0069c2987013edChangwan Ryu        case STREAMTYPE_AC3:
512d3c079ae9859011d118f94616d0069c2987013edChangwan Ryu            mQueue = new ElementaryStreamQueue(
513d3c079ae9859011d118f94616d0069c2987013edChangwan Ryu                    ElementaryStreamQueue::AC3);
514d3c079ae9859011d118f94616d0069c2987013edChangwan Ryu            break;
515d3c079ae9859011d118f94616d0069c2987013edChangwan Ryu
516386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber        default:
517386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber            break;
518386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber    }
519386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber
5203856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("new stream PID 0x%02x, type 0x%02x", elementaryPID, streamType);
52118ac5407da14dad9731f40ffc9a56bee73830019Andreas Huber
52218ac5407da14dad9731f40ffc9a56bee73830019Andreas Huber    if (mQueue != NULL) {
52318ac5407da14dad9731f40ffc9a56bee73830019Andreas Huber        mBuffer = new ABuffer(192 * 1024);
52418ac5407da14dad9731f40ffc9a56bee73830019Andreas Huber        mBuffer->setRange(0, 0);
52518ac5407da14dad9731f40ffc9a56bee73830019Andreas Huber    }
526cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber}
527cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
528cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas HuberATSParser::Stream::~Stream() {
529386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber    delete mQueue;
530386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber    mQueue = NULL;
531cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber}
532cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
5335403129e2a2f44620f2ac8109889e5a61be08732Andreas Huberstatus_t ATSParser::Stream::parse(
53487f2a558dd12043631e12c361abef301bf603140Andreas Huber        unsigned continuity_counter,
535cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        unsigned payload_unit_start_indicator, ABitReader *br) {
53618ac5407da14dad9731f40ffc9a56bee73830019Andreas Huber    if (mQueue == NULL) {
5375403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber        return OK;
53818ac5407da14dad9731f40ffc9a56bee73830019Andreas Huber    }
53918ac5407da14dad9731f40ffc9a56bee73830019Andreas Huber
54087f2a558dd12043631e12c361abef301bf603140Andreas Huber    if (mExpectedContinuityCounter >= 0
54187f2a558dd12043631e12c361abef301bf603140Andreas Huber            && (unsigned)mExpectedContinuityCounter != continuity_counter) {
54287f2a558dd12043631e12c361abef301bf603140Andreas Huber        ALOGI("discontinuity on stream pid 0x%04x", mElementaryPID);
54387f2a558dd12043631e12c361abef301bf603140Andreas Huber
54487f2a558dd12043631e12c361abef301bf603140Andreas Huber        mPayloadStarted = false;
54587f2a558dd12043631e12c361abef301bf603140Andreas Huber        mBuffer->setRange(0, 0);
54687f2a558dd12043631e12c361abef301bf603140Andreas Huber        mExpectedContinuityCounter = -1;
54787f2a558dd12043631e12c361abef301bf603140Andreas Huber
54894a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber#if 0
54994a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber        // Uncomment this if you'd rather see no corruption whatsoever on
55094a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber        // screen and suspend updates until we come across another IDR frame.
55194a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber
55294a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber        if (mStreamType == STREAMTYPE_H264) {
55394a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber            ALOGI("clearing video queue");
55494a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber            mQueue->clear(true /* clearFormat */);
55594a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber        }
55694a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber#endif
55794a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber
55887f2a558dd12043631e12c361abef301bf603140Andreas Huber        return OK;
55987f2a558dd12043631e12c361abef301bf603140Andreas Huber    }
56087f2a558dd12043631e12c361abef301bf603140Andreas Huber
56187f2a558dd12043631e12c361abef301bf603140Andreas Huber    mExpectedContinuityCounter = (continuity_counter + 1) & 0x0f;
56287f2a558dd12043631e12c361abef301bf603140Andreas Huber
563cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    if (payload_unit_start_indicator) {
564cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        if (mPayloadStarted) {
565cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            // Otherwise we run the danger of receiving the trailing bytes
566cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            // of a PES packet that we never saw the start of and assuming
567cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            // we have a a complete PES packet.
568cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
5695403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber            status_t err = flush();
5705403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber
5715403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber            if (err != OK) {
5725403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber                return err;
5735403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber            }
574cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        }
575cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
576cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        mPayloadStarted = true;
577cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    }
578cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
579cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    if (!mPayloadStarted) {
5805403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber        return OK;
581cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    }
582cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
583cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    size_t payloadSizeBits = br->numBitsLeft();
584f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    CHECK_EQ(payloadSizeBits % 8, 0u);
585cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
5863e57322b332214e3cb1874e67a5704c9b2b5f6ecAndreas Huber    size_t neededSize = mBuffer->size() + payloadSizeBits / 8;
5873e57322b332214e3cb1874e67a5704c9b2b5f6ecAndreas Huber    if (mBuffer->capacity() < neededSize) {
5883e57322b332214e3cb1874e67a5704c9b2b5f6ecAndreas Huber        // Increment in multiples of 64K.
5893e57322b332214e3cb1874e67a5704c9b2b5f6ecAndreas Huber        neededSize = (neededSize + 65535) & ~65535;
5903e57322b332214e3cb1874e67a5704c9b2b5f6ecAndreas Huber
591b4a7a2df4c28c3f32b5d877b54831d2cc5d78f81Colin Cross        ALOGI("resizing buffer to %zu bytes", neededSize);
5923e57322b332214e3cb1874e67a5704c9b2b5f6ecAndreas Huber
5933e57322b332214e3cb1874e67a5704c9b2b5f6ecAndreas Huber        sp<ABuffer> newBuffer = new ABuffer(neededSize);
5943e57322b332214e3cb1874e67a5704c9b2b5f6ecAndreas Huber        memcpy(newBuffer->data(), mBuffer->data(), mBuffer->size());
5953e57322b332214e3cb1874e67a5704c9b2b5f6ecAndreas Huber        newBuffer->setRange(0, mBuffer->size());
5963e57322b332214e3cb1874e67a5704c9b2b5f6ecAndreas Huber        mBuffer = newBuffer;
5973e57322b332214e3cb1874e67a5704c9b2b5f6ecAndreas Huber    }
598cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
599cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    memcpy(mBuffer->data() + mBuffer->size(), br->data(), payloadSizeBits / 8);
600cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    mBuffer->setRange(0, mBuffer->size() + payloadSizeBits / 8);
6015403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber
6025403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber    return OK;
603cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber}
604cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
6056e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huberbool ATSParser::Stream::isVideo() const {
6066e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber    switch (mStreamType) {
6076e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber        case STREAMTYPE_H264:
6086e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber        case STREAMTYPE_MPEG1_VIDEO:
6096e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber        case STREAMTYPE_MPEG2_VIDEO:
6106e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber        case STREAMTYPE_MPEG4_VIDEO:
6116e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber            return true;
6126e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber
6136e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber        default:
6146e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber            return false;
6156e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber    }
6166e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber}
6176e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber
6186e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huberbool ATSParser::Stream::isAudio() const {
6196e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber    switch (mStreamType) {
6206e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber        case STREAMTYPE_MPEG1_AUDIO:
6216e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber        case STREAMTYPE_MPEG2_AUDIO:
6226e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber        case STREAMTYPE_MPEG2_AUDIO_ADTS:
62390a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber        case STREAMTYPE_PCM_AUDIO:
624d3c079ae9859011d118f94616d0069c2987013edChangwan Ryu        case STREAMTYPE_AC3:
6256e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber            return true;
6266e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber
6276e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber        default:
6286e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber            return false;
6296e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber    }
6306e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber}
6316e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber
63232f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Hubervoid ATSParser::Stream::signalDiscontinuity(
63332f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber        DiscontinuityType type, const sp<AMessage> &extra) {
6340389cc09f7b90f155a8942a0d2e1925cad1dbe2dMarco Nelissen    mExpectedContinuityCounter = -1;
6350389cc09f7b90f155a8942a0d2e1925cad1dbe2dMarco Nelissen
63618ac5407da14dad9731f40ffc9a56bee73830019Andreas Huber    if (mQueue == NULL) {
63718ac5407da14dad9731f40ffc9a56bee73830019Andreas Huber        return;
63818ac5407da14dad9731f40ffc9a56bee73830019Andreas Huber    }
63918ac5407da14dad9731f40ffc9a56bee73830019Andreas Huber
6402a4d22d79e927f2245537921e10fc5fda1c47a29Andreas Huber    mPayloadStarted = false;
6412a4d22d79e927f2245537921e10fc5fda1c47a29Andreas Huber    mBuffer->setRange(0, 0);
6422a4d22d79e927f2245537921e10fc5fda1c47a29Andreas Huber
6436e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber    bool clearFormat = false;
6446e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber    if (isAudio()) {
6456e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber        if (type & DISCONTINUITY_AUDIO_FORMAT) {
6466e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber            clearFormat = true;
6476e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber        }
6486e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber    } else {
6496e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber        if (type & DISCONTINUITY_VIDEO_FORMAT) {
6506e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber            clearFormat = true;
6516e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber        }
6526e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber    }
653f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
6546e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber    mQueue->clear(clearFormat);
65532f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber
6566e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber    if (type & DISCONTINUITY_TIME) {
6576e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber        uint64_t resumeAtPTS;
6586e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber        if (extra != NULL
6596e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber                && extra->findInt64(
6606e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber                    IStreamListener::kKeyResumeAtPTS,
6616e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber                    (int64_t *)&resumeAtPTS)) {
6626e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber            int64_t resumeAtMediaTimeUs =
6636e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber                mProgram->convertPTSToTimestamp(resumeAtPTS);
66432f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber
6656e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber            extra->setInt64("resume-at-mediatimeUs", resumeAtMediaTimeUs);
666e332a9181cf6a3155ed1a0fd2afc212ccb1f2753Andreas Huber        }
6676e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber    }
668f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
6696e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber    if (mSource != NULL) {
6706e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber        mSource->queueDiscontinuity(type, extra);
671bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber    }
672f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
673bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber
674f933441648ef6a71dee783d733aac17b9508b452Andreas Hubervoid ATSParser::Stream::signalEOS(status_t finalResult) {
675f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    if (mSource != NULL) {
676f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        mSource->signalEOS(finalResult);
6772a4d22d79e927f2245537921e10fc5fda1c47a29Andreas Huber    }
6782a4d22d79e927f2245537921e10fc5fda1c47a29Andreas Huber}
6792a4d22d79e927f2245537921e10fc5fda1c47a29Andreas Huber
6805403129e2a2f44620f2ac8109889e5a61be08732Andreas Huberstatus_t ATSParser::Stream::parsePES(ABitReader *br) {
681cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    unsigned packet_startcode_prefix = br->getBits(24);
682cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
6833856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("packet_startcode_prefix = 0x%08x", packet_startcode_prefix);
684cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
685386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber    if (packet_startcode_prefix != 1) {
6863856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("Supposedly payload_unit_start=1 unit does not start "
687386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber             "with startcode.");
6885403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber
6895403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber        return ERROR_MALFORMED;
690386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber    }
691386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber
692cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    CHECK_EQ(packet_startcode_prefix, 0x000001u);
693cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
694cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    unsigned stream_id = br->getBits(8);
6953856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("stream_id = 0x%02x", stream_id);
696cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
697cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    unsigned PES_packet_length = br->getBits(16);
6983856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("PES_packet_length = %u", PES_packet_length);
699cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
700cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    if (stream_id != 0xbc  // program_stream_map
701cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            && stream_id != 0xbe  // padding_stream
702cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            && stream_id != 0xbf  // private_stream_2
703cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            && stream_id != 0xf0  // ECM
704cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            && stream_id != 0xf1  // EMM
705cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            && stream_id != 0xff  // program_stream_directory
706cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            && stream_id != 0xf2  // DSMCC
707cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            && stream_id != 0xf8) {  // H.222.1 type E
708cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        CHECK_EQ(br->getBits(2), 2u);
709cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
7106e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber        MY_LOGV("PES_scrambling_control = %u", br->getBits(2));
7116e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber        MY_LOGV("PES_priority = %u", br->getBits(1));
7126e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber        MY_LOGV("data_alignment_indicator = %u", br->getBits(1));
7136e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber        MY_LOGV("copyright = %u", br->getBits(1));
7146e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber        MY_LOGV("original_or_copy = %u", br->getBits(1));
715cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
716cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        unsigned PTS_DTS_flags = br->getBits(2);
7173856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("PTS_DTS_flags = %u", PTS_DTS_flags);
718cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
719cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        unsigned ESCR_flag = br->getBits(1);
7203856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("ESCR_flag = %u", ESCR_flag);
721cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
722cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        unsigned ES_rate_flag = br->getBits(1);
7233856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("ES_rate_flag = %u", ES_rate_flag);
724cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
725cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        unsigned DSM_trick_mode_flag = br->getBits(1);
7263856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("DSM_trick_mode_flag = %u", DSM_trick_mode_flag);
727cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
728cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        unsigned additional_copy_info_flag = br->getBits(1);
7293856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("additional_copy_info_flag = %u", additional_copy_info_flag);
730cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
7316e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber        MY_LOGV("PES_CRC_flag = %u", br->getBits(1));
7326e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber        MY_LOGV("PES_extension_flag = %u", br->getBits(1));
733cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
734cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        unsigned PES_header_data_length = br->getBits(8);
7353856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("PES_header_data_length = %u", PES_header_data_length);
736cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
737cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        unsigned optional_bytes_remaining = PES_header_data_length;
738cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
739cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        uint64_t PTS = 0, DTS = 0;
740cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
741cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        if (PTS_DTS_flags == 2 || PTS_DTS_flags == 3) {
742cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            CHECK_GE(optional_bytes_remaining, 5u);
743cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
744cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            CHECK_EQ(br->getBits(4), PTS_DTS_flags);
745cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
746cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            PTS = ((uint64_t)br->getBits(3)) << 30;
747cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            CHECK_EQ(br->getBits(1), 1u);
748cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            PTS |= ((uint64_t)br->getBits(15)) << 15;
749cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            CHECK_EQ(br->getBits(1), 1u);
750cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            PTS |= br->getBits(15);
751cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            CHECK_EQ(br->getBits(1), 1u);
752cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
753b4a7a2df4c28c3f32b5d877b54831d2cc5d78f81Colin Cross            ALOGV("PTS = 0x%016" PRIx64 " (%.2f)", PTS, PTS / 90000.0);
754cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
755cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            optional_bytes_remaining -= 5;
756cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
757cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            if (PTS_DTS_flags == 3) {
758cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber                CHECK_GE(optional_bytes_remaining, 5u);
759cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
760cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber                CHECK_EQ(br->getBits(4), 1u);
761cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
762cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber                DTS = ((uint64_t)br->getBits(3)) << 30;
763cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber                CHECK_EQ(br->getBits(1), 1u);
764cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber                DTS |= ((uint64_t)br->getBits(15)) << 15;
765cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber                CHECK_EQ(br->getBits(1), 1u);
766cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber                DTS |= br->getBits(15);
767cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber                CHECK_EQ(br->getBits(1), 1u);
768cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
769b4a7a2df4c28c3f32b5d877b54831d2cc5d78f81Colin Cross                ALOGV("DTS = %" PRIu64, DTS);
770cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
771cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber                optional_bytes_remaining -= 5;
772cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            }
773cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        }
774cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
775cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        if (ESCR_flag) {
776cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            CHECK_GE(optional_bytes_remaining, 6u);
777cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
778cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            br->getBits(2);
779cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
780cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            uint64_t ESCR = ((uint64_t)br->getBits(3)) << 30;
781cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            CHECK_EQ(br->getBits(1), 1u);
782cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            ESCR |= ((uint64_t)br->getBits(15)) << 15;
783cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            CHECK_EQ(br->getBits(1), 1u);
784cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            ESCR |= br->getBits(15);
785cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            CHECK_EQ(br->getBits(1), 1u);
786cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
787b4a7a2df4c28c3f32b5d877b54831d2cc5d78f81Colin Cross            ALOGV("ESCR = %" PRIu64, ESCR);
7886e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber            MY_LOGV("ESCR_extension = %u", br->getBits(9));
789cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
790cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            CHECK_EQ(br->getBits(1), 1u);
791cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
792cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            optional_bytes_remaining -= 6;
793cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        }
794cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
795cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        if (ES_rate_flag) {
796cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            CHECK_GE(optional_bytes_remaining, 3u);
797cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
798cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            CHECK_EQ(br->getBits(1), 1u);
7996e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber            MY_LOGV("ES_rate = %u", br->getBits(22));
800cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            CHECK_EQ(br->getBits(1), 1u);
801cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
802cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            optional_bytes_remaining -= 3;
803cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        }
804cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
805cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        br->skipBits(optional_bytes_remaining * 8);
806cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
807cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        // ES data follows.
808cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
809cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        if (PES_packet_length != 0) {
810cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            CHECK_GE(PES_packet_length, PES_header_data_length + 3);
811cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
812cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            unsigned dataLength =
813cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber                PES_packet_length - 3 - PES_header_data_length;
814cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
8155403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber            if (br->numBitsLeft() < dataLength * 8) {
81629357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block                ALOGE("PES packet does not carry enough data to contain "
817b4a7a2df4c28c3f32b5d877b54831d2cc5d78f81Colin Cross                     "payload. (numBitsLeft = %zu, required = %u)",
8185403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber                     br->numBitsLeft(), dataLength * 8);
8195403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber
8205403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber                return ERROR_MALFORMED;
8215403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber            }
8225403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber
8230da4dab0a45a2bc1d95cbc6ef6a4850ed2569584Andreas Huber            CHECK_GE(br->numBitsLeft(), dataLength * 8);
8240da4dab0a45a2bc1d95cbc6ef6a4850ed2569584Andreas Huber
8250da4dab0a45a2bc1d95cbc6ef6a4850ed2569584Andreas Huber            onPayloadData(
8260da4dab0a45a2bc1d95cbc6ef6a4850ed2569584Andreas Huber                    PTS_DTS_flags, PTS, DTS, br->data(), dataLength);
827cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
828cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            br->skipBits(dataLength * 8);
829cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        } else {
8300da4dab0a45a2bc1d95cbc6ef6a4850ed2569584Andreas Huber            onPayloadData(
8310da4dab0a45a2bc1d95cbc6ef6a4850ed2569584Andreas Huber                    PTS_DTS_flags, PTS, DTS,
8320da4dab0a45a2bc1d95cbc6ef6a4850ed2569584Andreas Huber                    br->data(), br->numBitsLeft() / 8);
8330da4dab0a45a2bc1d95cbc6ef6a4850ed2569584Andreas Huber
834cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            size_t payloadSizeBits = br->numBitsLeft();
835f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            CHECK_EQ(payloadSizeBits % 8, 0u);
836cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
837b4a7a2df4c28c3f32b5d877b54831d2cc5d78f81Colin Cross            ALOGV("There's %zu bytes of payload.", payloadSizeBits / 8);
838cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        }
839cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    } else if (stream_id == 0xbe) {  // padding_stream
840cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        CHECK_NE(PES_packet_length, 0u);
841cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        br->skipBits(PES_packet_length * 8);
842cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    } else {
843cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        CHECK_NE(PES_packet_length, 0u);
844cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        br->skipBits(PES_packet_length * 8);
845cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    }
8465403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber
8475403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber    return OK;
848cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber}
849cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
8505403129e2a2f44620f2ac8109889e5a61be08732Andreas Huberstatus_t ATSParser::Stream::flush() {
851cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    if (mBuffer->size() == 0) {
8525403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber        return OK;
853cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    }
854cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
855b4a7a2df4c28c3f32b5d877b54831d2cc5d78f81Colin Cross    ALOGV("flushing stream 0x%04x size = %zu", mElementaryPID, mBuffer->size());
856cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
857cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    ABitReader br(mBuffer->data(), mBuffer->size());
8585403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber
8595403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber    status_t err = parsePES(&br);
860cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
861cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    mBuffer->setRange(0, 0);
8625403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber
8635403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber    return err;
864cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber}
865cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
866cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Hubervoid ATSParser::Stream::onPayloadData(
86784333e0475bc911adc16417f4ca327c975cf6c36Andreas Huber        unsigned PTS_DTS_flags, uint64_t PTS, uint64_t /* DTS */,
868cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        const uint8_t *data, size_t size) {
86990a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber#if 0
87090a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber    ALOGI("payload streamType 0x%02x, PTS = 0x%016llx, dPTS = %lld",
87190a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber          mStreamType,
87290a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber          PTS,
87390a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber          (int64_t)PTS - mPrevPTS);
87490a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber    mPrevPTS = PTS;
87590a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber#endif
87690a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber
8773856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("onPayloadData mStreamType=0x%02x", mStreamType);
878cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
87998a46cf165d8de3779874eba01803bbc485f45b9Andreas Huber    int64_t timeUs = 0ll;  // no presentation timestamp available.
88098a46cf165d8de3779874eba01803bbc485f45b9Andreas Huber    if (PTS_DTS_flags == 2 || PTS_DTS_flags == 3) {
88198a46cf165d8de3779874eba01803bbc485f45b9Andreas Huber        timeUs = mProgram->convertPTSToTimestamp(PTS);
88298a46cf165d8de3779874eba01803bbc485f45b9Andreas Huber    }
88382f7321b03eec1e40af9d681370f754ee0279582Andreas Huber
884386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber    status_t err = mQueue->appendData(data, size, timeUs);
885decd96988e495133e4a1728f612d4c9fdb4d218eAndreas Huber
886decd96988e495133e4a1728f612d4c9fdb4d218eAndreas Huber    if (err != OK) {
887decd96988e495133e4a1728f612d4c9fdb4d218eAndreas Huber        return;
888decd96988e495133e4a1728f612d4c9fdb4d218eAndreas Huber    }
88982f7321b03eec1e40af9d681370f754ee0279582Andreas Huber
8906a63a939601645404fd98f58c19cc38ca818d99eAndreas Huber    sp<ABuffer> accessUnit;
891386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber    while ((accessUnit = mQueue->dequeueAccessUnit()) != NULL) {
8926a63a939601645404fd98f58c19cc38ca818d99eAndreas Huber        if (mSource == NULL) {
893386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber            sp<MetaData> meta = mQueue->getFormat();
89482f7321b03eec1e40af9d681370f754ee0279582Andreas Huber
8956a63a939601645404fd98f58c19cc38ca818d99eAndreas Huber            if (meta != NULL) {
8963856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                ALOGV("Stream PID 0x%08x of type 0x%02x now has data.",
897386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber                     mElementaryPID, mStreamType);
898386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber
8996a63a939601645404fd98f58c19cc38ca818d99eAndreas Huber                mSource = new AnotherPacketSource(meta);
9006a63a939601645404fd98f58c19cc38ca818d99eAndreas Huber                mSource->queueAccessUnit(accessUnit);
90182f7321b03eec1e40af9d681370f754ee0279582Andreas Huber            }
902386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber        } else if (mQueue->getFormat() != NULL) {
9032a4d22d79e927f2245537921e10fc5fda1c47a29Andreas Huber            // After a discontinuity we invalidate the queue's format
9042a4d22d79e927f2245537921e10fc5fda1c47a29Andreas Huber            // and won't enqueue any access units to the source until
9052a4d22d79e927f2245537921e10fc5fda1c47a29Andreas Huber            // the queue has reestablished the new format.
9065bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
9075bc087c573c70c84c6a39946457590b42d392a33Andreas Huber            if (mSource->getFormat() == NULL) {
908386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber                mSource->setFormat(mQueue->getFormat());
9095bc087c573c70c84c6a39946457590b42d392a33Andreas Huber            }
9106a63a939601645404fd98f58c19cc38ca818d99eAndreas Huber            mSource->queueAccessUnit(accessUnit);
91182f7321b03eec1e40af9d681370f754ee0279582Andreas Huber        }
91282f7321b03eec1e40af9d681370f754ee0279582Andreas Huber    }
91382f7321b03eec1e40af9d681370f754ee0279582Andreas Huber}
91482f7321b03eec1e40af9d681370f754ee0279582Andreas Huber
915cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Hubersp<MediaSource> ATSParser::Stream::getSource(SourceType type) {
916386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber    switch (type) {
917386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber        case VIDEO:
918386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber        {
9196e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber            if (isVideo()) {
920386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber                return mSource;
921386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber            }
922386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber            break;
923386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber        }
924386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber
925386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber        case AUDIO:
926386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber        {
9276e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber            if (isAudio()) {
928386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber                return mSource;
929386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber            }
930386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber            break;
931386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber        }
932386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber
933386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber        default:
934386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber            break;
935cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    }
936cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
937cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    return NULL;
938cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber}
939cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
940cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber////////////////////////////////////////////////////////////////////////////////
941cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
942c4c17d47b674b425fb6c399822c0ab3258543c0aAndreas HuberATSParser::ATSParser(uint32_t flags)
94387f2a558dd12043631e12c361abef301bf603140Andreas Huber    : mFlags(flags),
94487f2a558dd12043631e12c361abef301bf603140Andreas Huber      mAbsoluteTimeAnchorUs(-1ll),
945d5e56231a598b180a1d898bb7dc61b75580e59a4Andreas Huber      mTimeOffsetValid(false),
946d5e56231a598b180a1d898bb7dc61b75580e59a4Andreas Huber      mTimeOffsetUs(0ll),
94787f2a558dd12043631e12c361abef301bf603140Andreas Huber      mNumTSPacketsParsed(0),
94887f2a558dd12043631e12c361abef301bf603140Andreas Huber      mNumPCRs(0) {
9498dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    mPSISections.add(0 /* PID */, new PSISection);
950cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber}
951cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
952cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas HuberATSParser::~ATSParser() {
953cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber}
954cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
95506528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huberstatus_t ATSParser::feedTSPacket(const void *data, size_t size) {
956cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    CHECK_EQ(size, kTSPacketSize);
957cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
958cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    ABitReader br((const uint8_t *)data, kTSPacketSize);
95906528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber    return parseTS(&br);
960cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber}
961cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
96232f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Hubervoid ATSParser::signalDiscontinuity(
96332f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber        DiscontinuityType type, const sp<AMessage> &extra) {
964b7c8e91880463ff4981e3e53e98e45d68e2fe374Andreas Huber    int64_t mediaTimeUs;
965b7c8e91880463ff4981e3e53e98e45d68e2fe374Andreas Huber    if ((type & DISCONTINUITY_TIME)
966b7c8e91880463ff4981e3e53e98e45d68e2fe374Andreas Huber            && extra != NULL
967b7c8e91880463ff4981e3e53e98e45d68e2fe374Andreas Huber            && extra->findInt64(
968b7c8e91880463ff4981e3e53e98e45d68e2fe374Andreas Huber                IStreamListener::kKeyMediaTimeUs, &mediaTimeUs)) {
969b7c8e91880463ff4981e3e53e98e45d68e2fe374Andreas Huber        mAbsoluteTimeAnchorUs = mediaTimeUs;
970b7c8e91880463ff4981e3e53e98e45d68e2fe374Andreas Huber    } else if (type == DISCONTINUITY_ABSOLUTE_TIME) {
97187f2a558dd12043631e12c361abef301bf603140Andreas Huber        int64_t timeUs;
97287f2a558dd12043631e12c361abef301bf603140Andreas Huber        CHECK(extra->findInt64("timeUs", &timeUs));
97387f2a558dd12043631e12c361abef301bf603140Andreas Huber
97487f2a558dd12043631e12c361abef301bf603140Andreas Huber        CHECK(mPrograms.empty());
97587f2a558dd12043631e12c361abef301bf603140Andreas Huber        mAbsoluteTimeAnchorUs = timeUs;
97687f2a558dd12043631e12c361abef301bf603140Andreas Huber        return;
977d5e56231a598b180a1d898bb7dc61b75580e59a4Andreas Huber    } else if (type == DISCONTINUITY_TIME_OFFSET) {
978d5e56231a598b180a1d898bb7dc61b75580e59a4Andreas Huber        int64_t offset;
979d5e56231a598b180a1d898bb7dc61b75580e59a4Andreas Huber        CHECK(extra->findInt64("offset", &offset));
980d5e56231a598b180a1d898bb7dc61b75580e59a4Andreas Huber
981d5e56231a598b180a1d898bb7dc61b75580e59a4Andreas Huber        mTimeOffsetValid = true;
982d5e56231a598b180a1d898bb7dc61b75580e59a4Andreas Huber        mTimeOffsetUs = offset;
983d5e56231a598b180a1d898bb7dc61b75580e59a4Andreas Huber        return;
98487f2a558dd12043631e12c361abef301bf603140Andreas Huber    }
98587f2a558dd12043631e12c361abef301bf603140Andreas Huber
986f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    for (size_t i = 0; i < mPrograms.size(); ++i) {
98732f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber        mPrograms.editItemAt(i)->signalDiscontinuity(type, extra);
988f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
989f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
990f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
991f933441648ef6a71dee783d733aac17b9508b452Andreas Hubervoid ATSParser::signalEOS(status_t finalResult) {
992f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    CHECK_NE(finalResult, (status_t)OK);
993f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
9942a4d22d79e927f2245537921e10fc5fda1c47a29Andreas Huber    for (size_t i = 0; i < mPrograms.size(); ++i) {
995f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        mPrograms.editItemAt(i)->signalEOS(finalResult);
9962a4d22d79e927f2245537921e10fc5fda1c47a29Andreas Huber    }
9972a4d22d79e927f2245537921e10fc5fda1c47a29Andreas Huber}
9982a4d22d79e927f2245537921e10fc5fda1c47a29Andreas Huber
999cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Hubervoid ATSParser::parseProgramAssociationTable(ABitReader *br) {
1000cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    unsigned table_id = br->getBits(8);
10013856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("  table_id = %u", table_id);
1002cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    CHECK_EQ(table_id, 0x00u);
1003cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
1004cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    unsigned section_syntax_indictor = br->getBits(1);
10053856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("  section_syntax_indictor = %u", section_syntax_indictor);
1006cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    CHECK_EQ(section_syntax_indictor, 1u);
1007cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
1008cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    CHECK_EQ(br->getBits(1), 0u);
10096e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber    MY_LOGV("  reserved = %u", br->getBits(2));
1010cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
1011cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    unsigned section_length = br->getBits(12);
10123856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("  section_length = %u", section_length);
1013f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    CHECK_EQ(section_length & 0xc00, 0u);
1014cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
10156e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber    MY_LOGV("  transport_stream_id = %u", br->getBits(16));
10166e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber    MY_LOGV("  reserved = %u", br->getBits(2));
10176e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber    MY_LOGV("  version_number = %u", br->getBits(5));
10186e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber    MY_LOGV("  current_next_indicator = %u", br->getBits(1));
10196e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber    MY_LOGV("  section_number = %u", br->getBits(8));
10206e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber    MY_LOGV("  last_section_number = %u", br->getBits(8));
1021cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
1022cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    size_t numProgramBytes = (section_length - 5 /* header */ - 4 /* crc */);
1023cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    CHECK_EQ((numProgramBytes % 4), 0u);
1024cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
1025cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    for (size_t i = 0; i < numProgramBytes / 4; ++i) {
1026cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        unsigned program_number = br->getBits(16);
10273856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("    program_number = %u", program_number);
1028cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
10296e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber        MY_LOGV("    reserved = %u", br->getBits(3));
1030cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
1031cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        if (program_number == 0) {
10326e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber            MY_LOGV("    network_PID = 0x%04x", br->getBits(13));
1033cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        } else {
1034cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            unsigned programMapPID = br->getBits(13);
1035cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
10363856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("    program_map_PID = 0x%04x", programMapPID);
1037cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
1038386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber            bool found = false;
1039386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber            for (size_t index = 0; index < mPrograms.size(); ++index) {
1040386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber                const sp<Program> &program = mPrograms.itemAt(index);
1041386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber
1042386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber                if (program->number() == program_number) {
1043386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber                    program->updateProgramMapPID(programMapPID);
1044386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber                    found = true;
1045386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber                    break;
1046386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber                }
1047386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber            }
1048386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber
1049386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber            if (!found) {
1050386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber                mPrograms.push(
1051386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber                        new Program(this, program_number, programMapPID));
1052386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber            }
10538dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
10548dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber            if (mPSISections.indexOfKey(programMapPID) < 0) {
10558dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber                mPSISections.add(programMapPID, new PSISection);
10568dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber            }
1057cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        }
1058cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    }
1059cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
10606e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber    MY_LOGV("  CRC = 0x%08x", br->getBits(32));
1061cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber}
1062cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
106306528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huberstatus_t ATSParser::parsePID(
1064cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        ABitReader *br, unsigned PID,
106587f2a558dd12043631e12c361abef301bf603140Andreas Huber        unsigned continuity_counter,
1066cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        unsigned payload_unit_start_indicator) {
10678dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    ssize_t sectionIndex = mPSISections.indexOfKey(PID);
10688dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
10698dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    if (sectionIndex >= 0) {
10704b4bb11b8747adeb2efe56c7df4ab6803dd7db41Andreas Huber        sp<PSISection> section = mPSISections.valueAt(sectionIndex);
10718dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
1072cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        if (payload_unit_start_indicator) {
10738dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber            CHECK(section->isEmpty());
10748dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
1075cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            unsigned skip = br->getBits(8);
1076cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            br->skipBits(skip * 8);
1077cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        }
10788dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
10798dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber        CHECK((br->numBitsLeft() % 8) == 0);
10808dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber        status_t err = section->append(br->data(), br->numBitsLeft() / 8);
10818dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
10828dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber        if (err != OK) {
10838dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber            return err;
10848dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber        }
10858dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
10868dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber        if (!section->isComplete()) {
10878dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber            return OK;
10888dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber        }
10898dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
10908dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber        ABitReader sectionBits(section->data(), section->size());
10918dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
10928dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber        if (PID == 0) {
10938dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber            parseProgramAssociationTable(&sectionBits);
10948dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber        } else {
10958dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber            bool handled = false;
10968dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber            for (size_t i = 0; i < mPrograms.size(); ++i) {
10978dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber                status_t err;
10988dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber                if (!mPrograms.editItemAt(i)->parsePSISection(
10998dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber                            PID, &sectionBits, &err)) {
11008dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber                    continue;
11018dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber                }
11028dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
11038dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber                if (err != OK) {
11048dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber                    return err;
11058dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber                }
11068dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
11078dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber                handled = true;
11088dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber                break;
11098dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber            }
11108dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
11118dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber            if (!handled) {
11128dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber                mPSISections.removeItem(PID);
11134b4bb11b8747adeb2efe56c7df4ab6803dd7db41Andreas Huber                section.clear();
11148dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber            }
11158dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber        }
11168dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
11174b4bb11b8747adeb2efe56c7df4ab6803dd7db41Andreas Huber        if (section != NULL) {
11184b4bb11b8747adeb2efe56c7df4ab6803dd7db41Andreas Huber            section->clear();
11194b4bb11b8747adeb2efe56c7df4ab6803dd7db41Andreas Huber        }
11208dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
112106528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber        return OK;
1122cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    }
1123cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
1124cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    bool handled = false;
1125cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    for (size_t i = 0; i < mPrograms.size(); ++i) {
112606528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber        status_t err;
1127cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        if (mPrograms.editItemAt(i)->parsePID(
112887f2a558dd12043631e12c361abef301bf603140Andreas Huber                    PID, continuity_counter, payload_unit_start_indicator,
112987f2a558dd12043631e12c361abef301bf603140Andreas Huber                    br, &err)) {
113006528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber            if (err != OK) {
113106528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber                return err;
113206528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber            }
113306528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
1134cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            handled = true;
1135cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            break;
1136cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        }
1137cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    }
1138cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
1139cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    if (!handled) {
11403856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("PID 0x%04x not handled.", PID);
1141cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    }
114206528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
114306528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber    return OK;
1144cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber}
1145cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
114687f2a558dd12043631e12c361abef301bf603140Andreas Hubervoid ATSParser::parseAdaptationField(ABitReader *br, unsigned PID) {
1147cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    unsigned adaptation_field_length = br->getBits(8);
114887f2a558dd12043631e12c361abef301bf603140Andreas Huber
1149cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    if (adaptation_field_length > 0) {
115087f2a558dd12043631e12c361abef301bf603140Andreas Huber        unsigned discontinuity_indicator = br->getBits(1);
115187f2a558dd12043631e12c361abef301bf603140Andreas Huber
115287f2a558dd12043631e12c361abef301bf603140Andreas Huber        if (discontinuity_indicator) {
115387f2a558dd12043631e12c361abef301bf603140Andreas Huber            ALOGV("PID 0x%04x: discontinuity_indicator = 1 (!!!)", PID);
115487f2a558dd12043631e12c361abef301bf603140Andreas Huber        }
115587f2a558dd12043631e12c361abef301bf603140Andreas Huber
115687f2a558dd12043631e12c361abef301bf603140Andreas Huber        br->skipBits(2);
115787f2a558dd12043631e12c361abef301bf603140Andreas Huber        unsigned PCR_flag = br->getBits(1);
115887f2a558dd12043631e12c361abef301bf603140Andreas Huber
115987f2a558dd12043631e12c361abef301bf603140Andreas Huber        size_t numBitsRead = 4;
116087f2a558dd12043631e12c361abef301bf603140Andreas Huber
116187f2a558dd12043631e12c361abef301bf603140Andreas Huber        if (PCR_flag) {
116287f2a558dd12043631e12c361abef301bf603140Andreas Huber            br->skipBits(4);
116387f2a558dd12043631e12c361abef301bf603140Andreas Huber            uint64_t PCR_base = br->getBits(32);
116487f2a558dd12043631e12c361abef301bf603140Andreas Huber            PCR_base = (PCR_base << 1) | br->getBits(1);
116587f2a558dd12043631e12c361abef301bf603140Andreas Huber
116687f2a558dd12043631e12c361abef301bf603140Andreas Huber            br->skipBits(6);
116787f2a558dd12043631e12c361abef301bf603140Andreas Huber            unsigned PCR_ext = br->getBits(9);
116887f2a558dd12043631e12c361abef301bf603140Andreas Huber
116987f2a558dd12043631e12c361abef301bf603140Andreas Huber            // The number of bytes from the start of the current
117087f2a558dd12043631e12c361abef301bf603140Andreas Huber            // MPEG2 transport stream packet up and including
117187f2a558dd12043631e12c361abef301bf603140Andreas Huber            // the final byte of this PCR_ext field.
117287f2a558dd12043631e12c361abef301bf603140Andreas Huber            size_t byteOffsetFromStartOfTSPacket =
117387f2a558dd12043631e12c361abef301bf603140Andreas Huber                (188 - br->numBitsLeft() / 8);
117487f2a558dd12043631e12c361abef301bf603140Andreas Huber
117587f2a558dd12043631e12c361abef301bf603140Andreas Huber            uint64_t PCR = PCR_base * 300 + PCR_ext;
117687f2a558dd12043631e12c361abef301bf603140Andreas Huber
1177b4a7a2df4c28c3f32b5d877b54831d2cc5d78f81Colin Cross            ALOGV("PID 0x%04x: PCR = 0x%016" PRIx64 " (%.2f)",
117887f2a558dd12043631e12c361abef301bf603140Andreas Huber                  PID, PCR, PCR / 27E6);
117987f2a558dd12043631e12c361abef301bf603140Andreas Huber
118087f2a558dd12043631e12c361abef301bf603140Andreas Huber            // The number of bytes received by this parser up to and
118187f2a558dd12043631e12c361abef301bf603140Andreas Huber            // including the final byte of this PCR_ext field.
118287f2a558dd12043631e12c361abef301bf603140Andreas Huber            size_t byteOffsetFromStart =
118387f2a558dd12043631e12c361abef301bf603140Andreas Huber                mNumTSPacketsParsed * 188 + byteOffsetFromStartOfTSPacket;
118487f2a558dd12043631e12c361abef301bf603140Andreas Huber
118587f2a558dd12043631e12c361abef301bf603140Andreas Huber            for (size_t i = 0; i < mPrograms.size(); ++i) {
118687f2a558dd12043631e12c361abef301bf603140Andreas Huber                updatePCR(PID, PCR, byteOffsetFromStart);
118787f2a558dd12043631e12c361abef301bf603140Andreas Huber            }
118887f2a558dd12043631e12c361abef301bf603140Andreas Huber
118987f2a558dd12043631e12c361abef301bf603140Andreas Huber            numBitsRead += 52;
119087f2a558dd12043631e12c361abef301bf603140Andreas Huber        }
119187f2a558dd12043631e12c361abef301bf603140Andreas Huber
119287f2a558dd12043631e12c361abef301bf603140Andreas Huber        CHECK_GE(adaptation_field_length * 8, numBitsRead);
119387f2a558dd12043631e12c361abef301bf603140Andreas Huber
119487f2a558dd12043631e12c361abef301bf603140Andreas Huber        br->skipBits(adaptation_field_length * 8 - numBitsRead);
1195cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    }
1196cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber}
1197cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
119806528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huberstatus_t ATSParser::parseTS(ABitReader *br) {
11993856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("---");
1200cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
1201cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    unsigned sync_byte = br->getBits(8);
1202cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    CHECK_EQ(sync_byte, 0x47u);
1203cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
120452668ca838e1869676ff95e7388f93ec1858b1e9Andreas Huber    if (br->getBits(1)) {  // transport_error_indicator
120552668ca838e1869676ff95e7388f93ec1858b1e9Andreas Huber        // silently ignore.
120652668ca838e1869676ff95e7388f93ec1858b1e9Andreas Huber        return OK;
120752668ca838e1869676ff95e7388f93ec1858b1e9Andreas Huber    }
1208cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
1209cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    unsigned payload_unit_start_indicator = br->getBits(1);
12103856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("payload_unit_start_indicator = %u", payload_unit_start_indicator);
1211cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
12126e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber    MY_LOGV("transport_priority = %u", br->getBits(1));
1213cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
1214cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    unsigned PID = br->getBits(13);
12153856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("PID = 0x%04x", PID);
1216cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
12176e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber    MY_LOGV("transport_scrambling_control = %u", br->getBits(2));
1218cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
1219cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    unsigned adaptation_field_control = br->getBits(2);
12203856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("adaptation_field_control = %u", adaptation_field_control);
1221cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
12220da4dab0a45a2bc1d95cbc6ef6a4850ed2569584Andreas Huber    unsigned continuity_counter = br->getBits(4);
122387f2a558dd12043631e12c361abef301bf603140Andreas Huber    ALOGV("PID = 0x%04x, continuity_counter = %u", PID, continuity_counter);
12240da4dab0a45a2bc1d95cbc6ef6a4850ed2569584Andreas Huber
1225df64d15042bbd5e0e4933ac49bf3c177dd94752cSteve Block    // ALOGI("PID = 0x%04x, continuity_counter = %u", PID, continuity_counter);
1226cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
1227cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    if (adaptation_field_control == 2 || adaptation_field_control == 3) {
122887f2a558dd12043631e12c361abef301bf603140Andreas Huber        parseAdaptationField(br, PID);
1229cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    }
1230cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
123187f2a558dd12043631e12c361abef301bf603140Andreas Huber    status_t err = OK;
123287f2a558dd12043631e12c361abef301bf603140Andreas Huber
1233cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    if (adaptation_field_control == 1 || adaptation_field_control == 3) {
123487f2a558dd12043631e12c361abef301bf603140Andreas Huber        err = parsePID(
123587f2a558dd12043631e12c361abef301bf603140Andreas Huber                br, PID, continuity_counter, payload_unit_start_indicator);
1236cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    }
123706528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
123887f2a558dd12043631e12c361abef301bf603140Andreas Huber    ++mNumTSPacketsParsed;
123987f2a558dd12043631e12c361abef301bf603140Andreas Huber
124087f2a558dd12043631e12c361abef301bf603140Andreas Huber    return err;
1241cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber}
1242cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
1243cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Hubersp<MediaSource> ATSParser::getSource(SourceType type) {
1244386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber    int which = -1;  // any
1245386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber
1246cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    for (size_t i = 0; i < mPrograms.size(); ++i) {
1247386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber        const sp<Program> &program = mPrograms.editItemAt(i);
1248386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber
1249386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber        if (which >= 0 && (int)program->number() != which) {
1250386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber            continue;
1251386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber        }
1252386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber
1253386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber        sp<MediaSource> source = program->getSource(type);
1254cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
1255cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        if (source != NULL) {
1256cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            return source;
1257cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        }
1258cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    }
1259cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
1260cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    return NULL;
1261cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber}
1262cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
126343c3e6ce02215ca99d506458f596cb1211639f29Andreas Huberbool ATSParser::PTSTimeDeltaEstablished() {
126443c3e6ce02215ca99d506458f596cb1211639f29Andreas Huber    if (mPrograms.isEmpty()) {
126543c3e6ce02215ca99d506458f596cb1211639f29Andreas Huber        return false;
126643c3e6ce02215ca99d506458f596cb1211639f29Andreas Huber    }
126743c3e6ce02215ca99d506458f596cb1211639f29Andreas Huber
126843c3e6ce02215ca99d506458f596cb1211639f29Andreas Huber    return mPrograms.editItemAt(0)->PTSTimeDeltaEstablished();
126943c3e6ce02215ca99d506458f596cb1211639f29Andreas Huber}
127043c3e6ce02215ca99d506458f596cb1211639f29Andreas Huber
127187f2a558dd12043631e12c361abef301bf603140Andreas Hubervoid ATSParser::updatePCR(
127284333e0475bc911adc16417f4ca327c975cf6c36Andreas Huber        unsigned /* PID */, uint64_t PCR, size_t byteOffsetFromStart) {
1273b4a7a2df4c28c3f32b5d877b54831d2cc5d78f81Colin Cross    ALOGV("PCR 0x%016" PRIx64 " @ %zu", PCR, byteOffsetFromStart);
127487f2a558dd12043631e12c361abef301bf603140Andreas Huber
127587f2a558dd12043631e12c361abef301bf603140Andreas Huber    if (mNumPCRs == 2) {
127687f2a558dd12043631e12c361abef301bf603140Andreas Huber        mPCR[0] = mPCR[1];
127787f2a558dd12043631e12c361abef301bf603140Andreas Huber        mPCRBytes[0] = mPCRBytes[1];
127887f2a558dd12043631e12c361abef301bf603140Andreas Huber        mSystemTimeUs[0] = mSystemTimeUs[1];
127987f2a558dd12043631e12c361abef301bf603140Andreas Huber        mNumPCRs = 1;
128087f2a558dd12043631e12c361abef301bf603140Andreas Huber    }
128187f2a558dd12043631e12c361abef301bf603140Andreas Huber
128287f2a558dd12043631e12c361abef301bf603140Andreas Huber    mPCR[mNumPCRs] = PCR;
128387f2a558dd12043631e12c361abef301bf603140Andreas Huber    mPCRBytes[mNumPCRs] = byteOffsetFromStart;
128487f2a558dd12043631e12c361abef301bf603140Andreas Huber    mSystemTimeUs[mNumPCRs] = ALooper::GetNowUs();
128587f2a558dd12043631e12c361abef301bf603140Andreas Huber
128687f2a558dd12043631e12c361abef301bf603140Andreas Huber    ++mNumPCRs;
128787f2a558dd12043631e12c361abef301bf603140Andreas Huber
128887f2a558dd12043631e12c361abef301bf603140Andreas Huber    if (mNumPCRs == 2) {
128987f2a558dd12043631e12c361abef301bf603140Andreas Huber        double transportRate =
129087f2a558dd12043631e12c361abef301bf603140Andreas Huber            (mPCRBytes[1] - mPCRBytes[0]) * 27E6 / (mPCR[1] - mPCR[0]);
129187f2a558dd12043631e12c361abef301bf603140Andreas Huber
129287f2a558dd12043631e12c361abef301bf603140Andreas Huber        ALOGV("transportRate = %.2f bytes/sec", transportRate);
129387f2a558dd12043631e12c361abef301bf603140Andreas Huber    }
129487f2a558dd12043631e12c361abef301bf603140Andreas Huber}
129587f2a558dd12043631e12c361abef301bf603140Andreas Huber
12968dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber////////////////////////////////////////////////////////////////////////////////
12978dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
12988dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas HuberATSParser::PSISection::PSISection() {
12998dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber}
13008dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
13018dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas HuberATSParser::PSISection::~PSISection() {
13028dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber}
13038dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
13048dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huberstatus_t ATSParser::PSISection::append(const void *data, size_t size) {
13058dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    if (mBuffer == NULL || mBuffer->size() + size > mBuffer->capacity()) {
13068dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber        size_t newCapacity =
13078dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber            (mBuffer == NULL) ? size : mBuffer->capacity() + size;
13088dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
13098dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber        newCapacity = (newCapacity + 1023) & ~1023;
13108dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
13118dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber        sp<ABuffer> newBuffer = new ABuffer(newCapacity);
13128dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
13138dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber        if (mBuffer != NULL) {
13148dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber            memcpy(newBuffer->data(), mBuffer->data(), mBuffer->size());
13158dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber            newBuffer->setRange(0, mBuffer->size());
13168dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber        } else {
13178dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber            newBuffer->setRange(0, 0);
13188dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber        }
13198dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
13208dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber        mBuffer = newBuffer;
13218dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    }
13228dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
13238dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    memcpy(mBuffer->data() + mBuffer->size(), data, size);
13248dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    mBuffer->setRange(0, mBuffer->size() + size);
13258dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
13268dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    return OK;
13278dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber}
13288dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
13298dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Hubervoid ATSParser::PSISection::clear() {
13308dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    if (mBuffer != NULL) {
13318dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber        mBuffer->setRange(0, 0);
13328dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    }
13338dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber}
13348dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
13358dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huberbool ATSParser::PSISection::isComplete() const {
13368dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    if (mBuffer == NULL || mBuffer->size() < 3) {
13378dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber        return false;
13388dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    }
13398dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
13408dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    unsigned sectionLength = U16_AT(mBuffer->data() + 1) & 0xfff;
13418dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    return mBuffer->size() >= sectionLength + 3;
13428dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber}
13438dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
13448dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huberbool ATSParser::PSISection::isEmpty() const {
13458dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    return mBuffer == NULL || mBuffer->size() == 0;
13468dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber}
13478dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
13488dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huberconst uint8_t *ATSParser::PSISection::data() const {
13498dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    return mBuffer == NULL ? NULL : mBuffer->data();
13508dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber}
13518dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
13528dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Hubersize_t ATSParser::PSISection::size() const {
13538dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    return mBuffer == NULL ? 0 : mBuffer->size();
13548dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber}
13558dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
1356cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber}  // namespace android
1357