RtpMakerAAC.cpp 3.18 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * MIT License
xzl committed
3
 *
xiongziliang committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * Copyright (c) 2016 xiongziliang <771730766@qq.com>
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
xzl committed
25 26
 */

xiongziliang committed
27
#include "Common/config.h"
xiongzilaing committed
28 29
#include "RtpMakerAAC.h"
#include "Util/mini.h"
30
#include "Network/sockutil.h"
xiongziliang committed
31
using namespace toolkit;
xzl committed
32

xiongziliang committed
33
namespace mediakit{
xzl committed
34 35

void RtpMaker_AAC::makeRtp(const char *pcData, int iLen, uint32_t uiStamp) {
xiongziliang committed
36
    GET_CONFIG_AND_REGISTER(uint32_t,cycleMS,Rtp::kCycleMS);
37 38

    uiStamp %= cycleMS;
xzl committed
39 40 41
	char *ptr = (char *) pcData;
	int iSize = iLen;
	while (iSize > 0 ) {
42 43 44 45 46 47 48
		if (iSize <= _iMtuSize - 20) {
			_aucSectionBuf[0] = 0;
			_aucSectionBuf[1] = 16;
			_aucSectionBuf[2] = iLen >> 5;
			_aucSectionBuf[3] = (iLen & 0x1F) << 3;
			memcpy(_aucSectionBuf + 4, ptr, iSize);
			makeAACRtp(_aucSectionBuf, iSize + 4, true, uiStamp);
xzl committed
49 50
			break;
		}
51 52 53 54 55 56 57 58
		_aucSectionBuf[0] = 0;
		_aucSectionBuf[1] = 16;
		_aucSectionBuf[2] = (iLen) >> 5;
		_aucSectionBuf[3] = (iLen & 0x1F) << 3;
		memcpy(_aucSectionBuf + 4, ptr, _iMtuSize - 20);
		makeAACRtp(_aucSectionBuf, _iMtuSize - 16, false, uiStamp);
		ptr += (_iMtuSize - 20);
		iSize -= (_iMtuSize - 20);
xzl committed
59 60 61 62 63 64

	}
}

inline void RtpMaker_AAC::makeAACRtp(const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp) {
	uint16_t u16RtpLen = uiLen + 12;
65
	uint32_t ts = htonl((_ui32SampleRate / 1000) * uiStamp);
66 67
	uint16_t sq = htons(_ui16Sequence);
	uint32_t sc = htonl(_ui32Ssrc);
xzl committed
68 69 70 71
	auto pRtppkt = obtainPkt();
	auto &rtppkt = *(pRtppkt.get());
	unsigned char *pucRtp = rtppkt.payload;
	pucRtp[0] = '$';
72
	pucRtp[1] = _ui8Interleaved;
xzl committed
73 74 75
	pucRtp[2] = u16RtpLen >> 8;
	pucRtp[3] = u16RtpLen & 0x00FF;
	pucRtp[4] = 0x80;
76
	pucRtp[5] = (bMark << 7) | _ui8PlayloadType;
xzl committed
77 78 79 80
	memcpy(&pucRtp[6], &sq, 2);
	memcpy(&pucRtp[8], &ts, 4);
	//ssrc
	memcpy(&pucRtp[12], &sc, 4);
81
	//playload
xzl committed
82
	memcpy(&pucRtp[16], pData, uiLen);
83

84 85
	rtppkt.PT = _ui8PlayloadType;
	rtppkt.interleaved = _ui8Interleaved;
xzl committed
86 87
	rtppkt.mark = bMark;
	rtppkt.length = uiLen + 16;
88
	rtppkt.sequence = _ui16Sequence;
89
	rtppkt.timeStamp = uiStamp;
90
	rtppkt.ssrc = _ui32Ssrc;
xzl committed
91
	rtppkt.type = TrackAudio;
92
	rtppkt.offset = 16;
93

xzl committed
94
	onMakeRtp(pRtppkt, false);
95
	_ui16Sequence++;
96
	_ui32TimeStamp = uiStamp;
xzl committed
97 98
}

xiongziliang committed
99
}//namespace mediakit