ZLMediaKit/src/RtpCodec/RtpMakerAAC.cpp

104 lines
3.3 KiB
C++
Raw Normal View History

2017-10-09 22:11:01 +08:00
/*
2017-09-27 16:20:30 +08:00
* MIT License
2017-04-01 16:35:56 +08:00
*
2017-09-27 16:20:30 +08:00
* 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.
2017-04-01 16:35:56 +08:00
*/
2017-05-02 17:15:12 +08:00
#include "Common/config.h"
2017-04-25 11:35:41 +08:00
#include "RtpMakerAAC.h"
#include "Util/mini.h"
2017-08-10 14:31:04 +08:00
#include "Network/sockutil.h"
2017-04-01 16:35:56 +08:00
using namespace ZL::Util;
2017-08-10 14:31:04 +08:00
using namespace ZL::Network;
2017-04-01 16:35:56 +08:00
namespace ZL {
namespace Rtsp {
void RtpMaker_AAC::makeRtp(const char *pcData, int iLen, uint32_t uiStamp) {
2018-02-09 11:42:55 +08:00
GET_CONFIG_AND_REGISTER(uint32_t,cycleMS,Config::Rtp::kCycleMS);
uiStamp %= cycleMS;
2017-04-01 16:35:56 +08:00
char *ptr = (char *) pcData;
int iSize = iLen;
while (iSize > 0 ) {
2018-10-24 15:43:52 +08:00
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);
2017-04-01 16:35:56 +08:00
break;
}
2018-10-24 15:43:52 +08:00
_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);
2017-04-01 16:35:56 +08:00
}
}
inline void RtpMaker_AAC::makeAACRtp(const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp) {
uint16_t u16RtpLen = uiLen + 12;
2018-10-24 15:43:52 +08:00
_ui32TimeStamp = (_ui32SampleRate / 1000) * uiStamp;
uint32_t ts = htonl(_ui32TimeStamp);
uint16_t sq = htons(_ui16Sequence);
uint32_t sc = htonl(_ui32Ssrc);
2017-04-01 16:35:56 +08:00
auto pRtppkt = obtainPkt();
auto &rtppkt = *(pRtppkt.get());
unsigned char *pucRtp = rtppkt.payload;
pucRtp[0] = '$';
2018-10-24 15:43:52 +08:00
pucRtp[1] = _ui8Interleaved;
2017-04-01 16:35:56 +08:00
pucRtp[2] = u16RtpLen >> 8;
pucRtp[3] = u16RtpLen & 0x00FF;
pucRtp[4] = 0x80;
2018-10-24 15:43:52 +08:00
pucRtp[5] = (bMark << 7) | _ui8PlayloadType;
2017-04-01 16:35:56 +08:00
memcpy(&pucRtp[6], &sq, 2);
memcpy(&pucRtp[8], &ts, 4);
//ssrc
memcpy(&pucRtp[12], &sc, 4);
2018-08-20 10:18:04 +08:00
//playload
2017-04-01 16:35:56 +08:00
memcpy(&pucRtp[16], pData, uiLen);
2018-08-20 10:18:04 +08:00
2018-10-24 15:43:52 +08:00
rtppkt.PT = _ui8PlayloadType;
rtppkt.interleaved = _ui8Interleaved;
2017-04-01 16:35:56 +08:00
rtppkt.mark = bMark;
rtppkt.length = uiLen + 16;
2018-10-24 15:43:52 +08:00
rtppkt.sequence = _ui16Sequence;
rtppkt.timeStamp = _ui32TimeStamp;
rtppkt.ssrc = _ui32Ssrc;
2017-04-01 16:35:56 +08:00
rtppkt.type = TrackAudio;
rtppkt.offset = 16;
2018-08-20 10:18:04 +08:00
2017-04-01 16:35:56 +08:00
onMakeRtp(pRtppkt, false);
2018-10-24 15:43:52 +08:00
_ui16Sequence++;
2017-04-01 16:35:56 +08:00
}
} /* namespace RTP */
} /* namespace ZL */