1/*
2 * Copyright (C) 2011 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 __AAH_TX_PACKET_H__
18#define __AAH_TX_PACKET_H__
19
20#include <media/stagefright/foundation/ABase.h>
21#include <utils/LinearTransform.h>
22#include <utils/RefBase.h>
23#include <utils/Timers.h>
24
25namespace android {
26
27class TRTPPacket : public RefBase {
28  public:
29    enum TRTPHeaderType {
30        kHeaderTypeAudio = 1,
31        kHeaderTypeVideo = 2,
32        kHeaderTypeSubpicture = 3,
33        kHeaderTypeControl = 4,
34    };
35
36    enum TRTPPayloadFlags {
37        kFlag_TSTransformPresent = 0x02,
38        kFlag_TSValid = 0x01,
39    };
40
41  protected:
42    TRTPPacket(TRTPHeaderType headerType)
43        : mIsPacked(false)
44        , mVersion(2)
45        , mPadding(false)
46        , mExtension(false)
47        , mCsrcCount(0)
48        , mPayloadType(100)
49        , mSeqNumber(0)
50        , mPTSValid(false)
51        , mPTS(0)
52        , mEpoch(0)
53        , mProgramID(0)
54        , mSubstreamID(0)
55        , mClockTranformValid(false)
56        , mTRTPVersion(1)
57        , mTRTPLength(0)
58        , mTRTPHeaderType(headerType)
59        , mPacket(NULL)
60        , mPacketLen(0) { }
61
62  public:
63    virtual ~TRTPPacket();
64
65    void setSeqNumber(uint16_t val);
66    uint16_t getSeqNumber() const;
67
68    void setPTS(int64_t val);
69    int64_t getPTS() const;
70
71    void setEpoch(uint32_t val);
72    void setProgramID(uint16_t val);
73    void setSubstreamID(uint16_t val);
74    void setClockTransform(const LinearTransform& trans);
75
76    uint8_t* getPacket() const;
77    int getPacketLen() const;
78
79    void setExpireTime(nsecs_t val);
80    nsecs_t getExpireTime() const;
81
82    virtual bool pack() = 0;
83
84    // mask for the number of bits in a TRTP epoch
85    static const uint32_t kTRTPEpochMask = (1 << 22) - 1;
86    static const int kTRTPEpochShift = 10;
87
88  protected:
89    static const int kRTPHeaderLen = 12;
90    virtual int TRTPHeaderLen() const;
91
92    void writeTRTPHeader(uint8_t*& buf,
93                         bool isFirstFragment,
94                         int totalPacketLen);
95
96    void writeU8(uint8_t*& buf, uint8_t val);
97    void writeU16(uint8_t*& buf, uint16_t val);
98    void writeU32(uint8_t*& buf, uint32_t val);
99    void writeU64(uint8_t*& buf, uint64_t val);
100
101    bool mIsPacked;
102
103    uint8_t mVersion;
104    bool mPadding;
105    bool mExtension;
106    uint8_t mCsrcCount;
107    uint8_t mPayloadType;
108    uint16_t mSeqNumber;
109    bool mPTSValid;
110    int64_t  mPTS;
111    uint32_t mEpoch;
112    uint16_t mProgramID;
113    uint16_t mSubstreamID;
114    LinearTransform mClockTranform;
115    bool mClockTranformValid;
116    uint8_t mTRTPVersion;
117    uint32_t mTRTPLength;
118    TRTPHeaderType mTRTPHeaderType;
119
120    uint8_t* mPacket;
121    int mPacketLen;
122
123    nsecs_t mExpireTime;
124
125    DISALLOW_EVIL_CONSTRUCTORS(TRTPPacket);
126};
127
128class TRTPAudioPacket : public TRTPPacket {
129  public:
130    enum AudioPayloadFlags {
131        kFlag_AuxLengthPresent = 0x10,
132        kFlag_RandomAccessPoint = 0x08,
133        kFlag_Dropable = 0x04,
134        kFlag_Discontinuity = 0x02,
135        kFlag_EndOfStream = 0x01,
136    };
137
138    TRTPAudioPacket()
139        : TRTPPacket(kHeaderTypeAudio)
140        , mCodecType(kCodecInvalid)
141        , mRandomAccessPoint(false)
142        , mDropable(false)
143        , mDiscontinuity(false)
144        , mEndOfStream(false)
145        , mVolume(0)
146        , mAccessUnitData(NULL)
147        , mAccessUnitLen(0)
148        , mAuxData(NULL)
149        , mAuxDataLen(0) { }
150
151    enum TRTPAudioCodecType {
152        kCodecInvalid = 0,
153        kCodecPCMBigEndian = 1,
154        kCodecPCMLittleEndian = 2,
155        kCodecMPEG1Audio = 3,
156        kCodecAACAudio = 4,
157    };
158
159    void setCodecType(TRTPAudioCodecType val);
160    void setRandomAccessPoint(bool val);
161    void setDropable(bool val);
162    void setDiscontinuity(bool val);
163    void setEndOfStream(bool val);
164    void setVolume(uint8_t val);
165    void setAccessUnitData(const void* data, size_t len);
166    void setAuxData(const void* data, size_t len);
167
168    virtual bool pack();
169
170  protected:
171    virtual int TRTPHeaderLen() const;
172
173  private:
174    TRTPAudioCodecType mCodecType;
175    bool mRandomAccessPoint;
176    bool mDropable;
177    bool mDiscontinuity;
178    bool mEndOfStream;
179    uint8_t mVolume;
180
181    const void* mAccessUnitData;
182    size_t mAccessUnitLen;
183    const void* mAuxData;
184    size_t mAuxDataLen;
185
186    DISALLOW_EVIL_CONSTRUCTORS(TRTPAudioPacket);
187};
188
189class TRTPControlPacket : public TRTPPacket {
190  public:
191    TRTPControlPacket()
192        : TRTPPacket(kHeaderTypeControl)
193        , mCommandID(kCommandNop) {}
194
195    enum TRTPCommandID {
196        kCommandNop   = 1,
197        kCommandFlush = 2,
198        kCommandEOS   = 3,
199    };
200
201    void setCommandID(TRTPCommandID val);
202
203    virtual bool pack();
204
205  private:
206    TRTPCommandID mCommandID;
207
208    DISALLOW_EVIL_CONSTRUCTORS(TRTPControlPacket);
209};
210
211}  // namespace android
212
213#endif  // __AAH_TX_PLAYER_H__
214