1/*
2 * Copyright 2012, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef TS_PACKETIZER_H_
18
19#define TS_PACKETIZER_H_
20
21#include <media/stagefright/foundation/ABase.h>
22#include <utils/Errors.h>
23#include <utils/RefBase.h>
24#include <utils/Vector.h>
25
26namespace android {
27
28struct ABuffer;
29struct AMessage;
30
31// Forms the packets of a transport stream given access units.
32// Emits metadata tables (PAT and PMT) and timestamp stream (PCR) based
33// on flags.
34struct TSPacketizer : public RefBase {
35    enum {
36        EMIT_HDCP20_DESCRIPTOR = 1,
37        EMIT_HDCP21_DESCRIPTOR = 2,
38    };
39    TSPacketizer(uint32_t flags);
40
41    // Returns trackIndex or error.
42    ssize_t addTrack(const sp<AMessage> &format);
43
44    enum {
45        EMIT_PAT_AND_PMT                = 1,
46        EMIT_PCR                        = 2,
47        IS_ENCRYPTED                    = 4,
48        PREPEND_SPS_PPS_TO_IDR_FRAMES   = 8,
49    };
50    status_t packetize(
51            size_t trackIndex, const sp<ABuffer> &accessUnit,
52            sp<ABuffer> *packets,
53            uint32_t flags,
54            const uint8_t *PES_private_data, size_t PES_private_data_len,
55            size_t numStuffingBytes = 0);
56
57    status_t extractCSDIfNecessary(size_t trackIndex);
58
59    // XXX to be removed once encoder config option takes care of this for
60    // encrypted mode.
61    sp<ABuffer> prependCSD(
62            size_t trackIndex, const sp<ABuffer> &accessUnit) const;
63
64protected:
65    virtual ~TSPacketizer();
66
67private:
68    enum {
69        kPID_PMT = 0x100,
70        kPID_PCR = 0x1000,
71    };
72
73    struct Track;
74
75    uint32_t mFlags;
76    Vector<sp<Track> > mTracks;
77
78    Vector<sp<ABuffer> > mProgramInfoDescriptors;
79
80    unsigned mPATContinuityCounter;
81    unsigned mPMTContinuityCounter;
82
83    uint32_t mCrcTable[256];
84
85    void initCrcTable();
86    uint32_t crc32(const uint8_t *start, size_t size) const;
87
88    DISALLOW_EVIL_CONSTRUCTORS(TSPacketizer);
89};
90
91}  // namespace android
92
93#endif  // TS_PACKETIZER_H_
94
95