summaryrefslogtreecommitdiffstats
path: root/media/libaah_rtp/aah_tx_packet.cpp
blob: 4cd6e4715b57b699a6e1dec05a5e8a8c91fdba36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "LibAAH_RTP"
#include <utils/Log.h>

#include <arpa/inet.h>
#include <string.h>

#include <media/stagefright/foundation/ADebug.h>

#include "aah_tx_packet.h"

namespace android {

const int TRTPPacket::kRTPHeaderLen;
const uint32_t TRTPPacket::kTRTPEpochMask;

TRTPPacket::~TRTPPacket() {
    delete mPacket;
}

/*** TRTP packet properties ***/

void TRTPPacket::setSeqNumber(uint16_t val) {
    mSeqNumber = val;

    if (mIsPacked) {
        const int kTRTPSeqNumberOffset = 2;
        uint16_t* buf = reinterpret_cast<uint16_t*>(
            mPacket + kTRTPSeqNumberOffset);
        *buf = htons(mSeqNumber);
    }
}

uint16_t TRTPPacket::getSeqNumber() const {
    return mSeqNumber;
}

void TRTPPacket::setPTS(int64_t val) {
    CHECK(!mIsPacked);
    mPTS = val;
    mPTSValid = true;
}

int64_t TRTPPacket::getPTS() const {
    return mPTS;
}

void TRTPPacket::setEpoch(uint32_t val) {
    mEpoch = val;

    if (mIsPacked) {
        const int kTRTPEpochOffset = 8;
        uint32_t* buf = reinterpret_cast<uint32_t*>(
            mPacket + kTRTPEpochOffset);
        uint32_t val = ntohl(*buf);
        val &= ~(kTRTPEpochMask << kTRTPEpochShift);
        val |= (mEpoch & kTRTPEpochMask) << kTRTPEpochShift;
        *buf = htonl(val);
    }
}

void TRTPPacket::setProgramID(uint16_t val) {
    CHECK(!mIsPacked);
    mProgramID = val;
}

void TRTPPacket::setSubstreamID(uint16_t val) {
    CHECK(!mIsPacked);
    mSubstreamID = val;
}


void TRTPPacket::setClockTransform(const LinearTransform& trans) {
    CHECK(!mIsPacked);
    mClockTranform = trans;
    mClockTranformValid = true;
}

uint8_t* TRTPPacket::getPacket() const {
    CHECK(mIsPacked);
    return mPacket;
}

int TRTPPacket::getPacketLen() const {
    CHECK(mIsPacked);
    return mPacketLen;
}

void TRTPPacket::setExpireTime(nsecs_t val) {
    CHECK(!mIsPacked);
    mExpireTime = val;
}

nsecs_t TRTPPacket::getExpireTime() const {
    return mExpireTime;
}

/*** TRTP audio packet properties ***/

void TRTPAudioPacket::setCodecType(TRTPAudioCodecType val) {
    CHECK(!mIsPacked);
    mCodecType = val;
}

void TRTPAudioPacket::setRandomAccessPoint(bool val) {
    CHECK(!mIsPacked);
    mRandomAccessPoint = val;
}

void TRTPAudioPacket::setDropable(bool val) {
    CHECK(!mIsPacked);
    mDropable = val;
}

void TRTPAudioPacket::setDiscontinuity(bool val) {
    CHECK(!mIsPacked);
    mDiscontinuity = val;
}

void TRTPAudioPacket::setEndOfStream(bool val) {
    CHECK(!mIsPacked);
    mEndOfStream = val;
}

void TRTPAudioPacket::setVolume(uint8_t val) {
    CHECK(!mIsPacked);
    mVolume = val;
}

void TRTPAudioPacket::setAccessUnitData(const void* data, size_t len) {
    CHECK(!mIsPacked);
    mAccessUnitData = data;
    mAccessUnitLen = len;
}

void TRTPAudioPacket::setAuxData(const void* data, size_t len) {
    CHECK(!mIsPacked);
    mAuxData = data;
    mAuxDataLen = len;
}

/*** TRTP control packet properties ***/

void TRTPControlPacket::setCommandID(TRTPCommandID val) {
    CHECK(!mIsPacked);
    mCommandID = val;
}

/*** TRTP packet serializers ***/

void TRTPPacket::writeU8(uint8_t*& buf, uint8_t val) {
    *buf = val;
    buf++;
}

void TRTPPacket::writeU16(uint8_t*& buf, uint16_t val) {
    *reinterpret_cast<uint16_t*>(buf) = htons(val);
    buf += 2;
}

void TRTPPacket::writeU32(uint8_t*& buf, uint32_t val) {
    *reinterpret_cast<uint32_t*>(buf) = htonl(val);
    buf += 4;
}

void TRTPPacket::writeU64(uint8_t*& buf, uint64_t val) {
    buf[0] = static_cast<uint8_t>(val >> 56);
    buf[1] = static_cast<uint8_t>(val >> 48);
    buf[2] = static_cast<uint8_t>(val >> 40);
    buf[3] = static_cast<uint8_t>(val >> 32);
    buf[4] = static_cast<uint8_t>(val >> 24);
    buf[5] = static_cast<uint8_t>(val >> 16);
    buf[6] = static_cast<uint8_t>(val >>  8);
    buf[7] = static_cast<uint8_t>(val);
    buf += 8;
}

void TRTPPacket::writeTRTPHeader(uint8_t*& buf,
                                 bool isFirstFragment,
                                 int totalPacketLen) {
    // RTP header
    writeU8(buf,
            ((mVersion & 0x03) << 6) |
            (static_cast<int>(mPadding) << 5) |
            (static_cast<int>(mExtension) << 4) |
            (mCsrcCount & 0x0F));
    writeU8(buf,
            (static_cast<int>(isFirstFragment) << 7) |
            (mPayloadType & 0x7F));
    writeU16(buf, mSeqNumber);
    if (isFirstFragment && mPTSValid) {
        writeU32(buf, mPTS & 0xFFFFFFFF);
    } else {
        writeU32(buf, 0);
    }
    writeU32(buf,
            ((mEpoch & kTRTPEpochMask) << kTRTPEpochShift) |
            ((mProgramID & 0x1F) << 5) |
            (mSubstreamID & 0x1F));

    // TRTP header
    writeU8(buf, mTRTPVersion);
    writeU8(buf,
            ((mTRTPHeaderType & 0x0F) << 4) |
            (mClockTranformValid ? 0x02 : 0x00) |
            (mPTSValid ? 0x01 : 0x00));
    writeU32(buf, totalPacketLen - kRTPHeaderLen);
    if (mPTSValid) {
        writeU32(buf, mPTS >> 32);
    }

    if (mClockTranformValid) {
        writeU64(buf, mClockTranform.a_zero);
        writeU32(buf, mClockTranform.a_to_b_numer);
        writeU32(buf, mClockTranform.a_to_b_denom);
        writeU64(buf, mClockTranform.b_zero);
    }
}

bool TRTPAudioPacket::pack() {
    if (mIsPacked) {
        return false;
    }

    int packetLen = kRTPHeaderLen +
                    mAuxDataLen +
                    mAccessUnitLen +
                    TRTPHeaderLen();

    // TODO : support multiple fragments
    const int kMaxUDPPayloadLen = 65507;
    if (packetLen > kMaxUDPPayloadLen) {
        return false;
    }

    mPacket = new uint8_t[packetLen];
    if (!mPacket) {
        return false;
    }

    mPacketLen = packetLen;

    uint8_t* cur = mPacket;
    bool hasAux = mAuxData && mAuxDataLen;
    uint8_t flags = (static_cast<int>(hasAux) << 4) |
                    (static_cast<int>(mRandomAccessPoint) << 3) |
                    (static_cast<int>(mDropable) << 2) |
                    (static_cast<int>(mDiscontinuity) << 1) |
                    (static_cast<int>(mEndOfStream));

    writeTRTPHeader(cur, true, packetLen);
    writeU8(cur, mCodecType);
    writeU8(cur, flags);
    writeU8(cur, mVolume);

    if (hasAux) {
        writeU32(cur, mAuxDataLen);
        memcpy(cur, mAuxData, mAuxDataLen);
        cur += mAuxDataLen;
    }

    memcpy(cur, mAccessUnitData, mAccessUnitLen);

    mIsPacked = true;
    return true;
}

int TRTPPacket::TRTPHeaderLen() const {
    // 6 bytes for version, payload type, flags and length.  An additional 4 if
    // there are upper timestamp bits present and another 24 if there is a clock
    // transformation present.
    return 6 +
           (mClockTranformValid ? 24 : 0) +
           (mPTSValid ? 4 : 0);
}

int TRTPAudioPacket::TRTPHeaderLen() const {
    // TRTPPacket::TRTPHeaderLen() for the base TRTPHeader.  3 bytes for audio's
    // codec type, flags and volume field.  Another 5 bytes if the codec type is
    // PCM and we are sending sample rate/channel count. as well as however long
    // the aux data (if present) is.

    int pcmParamLength;
    switch(mCodecType) {
        case kCodecPCMBigEndian:
        case kCodecPCMLittleEndian:
            pcmParamLength = 5;
            break;

        default:
            pcmParamLength = 0;
            break;
    }


    int auxDataLenField = (NULL != mAuxData) ? sizeof(uint32_t) : 0;
    return TRTPPacket::TRTPHeaderLen() +
           3 +
           auxDataLenField +
           pcmParamLength;
}

bool TRTPControlPacket::pack() {
    if (mIsPacked) {
        return false;
    }

    // command packets contain a 2-byte command ID
    int packetLen = kRTPHeaderLen +
                    TRTPHeaderLen() +
                    2;

    mPacket = new uint8_t[packetLen];
    if (!mPacket) {
        return false;
    }

    mPacketLen = packetLen;

    uint8_t* cur = mPacket;

    writeTRTPHeader(cur, true, packetLen);
    writeU16(cur, mCommandID);

    mIsPacked = true;
    return true;
}

}  // namespace android