ZLMediaKit/src/Device/Device.cpp

308 lines
9.4 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-04-25 11:35:41 +08:00
#include <stdio.h>
#include <stdio.h>
2017-04-01 16:35:56 +08:00
#include "Device.h"
#include "Util/logger.h"
#include "Util/util.h"
2018-09-20 15:43:49 +08:00
#include "Util/base64.h"
2017-04-01 16:35:56 +08:00
#include "Util/TimeTicker.h"
2017-04-25 11:35:41 +08:00
2018-10-24 17:17:55 +08:00
using namespace toolkit;
2017-04-01 16:35:56 +08:00
2018-10-24 17:17:55 +08:00
namespace mediakit {
2017-04-01 16:35:56 +08:00
DevChannel::DevChannel(const char *strVhost,
const char *strApp,
const char *strId,
float fDuration,
bool bEanbleHls,
bool bEnableMp4 ) :
RtspToRtmpMediaSource(strVhost,strApp,strId,bEanbleHls,bEnableMp4) {
2017-04-01 16:35:56 +08:00
2018-10-24 15:43:52 +08:00
_strSdp = "v=0\r\n";
_strSdp += "o=- 1383190487994921 1 IN IP4 0.0.0.0\r\n";
_strSdp += "s=RTSP Session, streamed by the ZL\r\n";
_strSdp += "i=ZL Live Stream\r\n";
_strSdp += "c=IN IP4 0.0.0.0\r\n";
_strSdp += "t=0 0\r\n";
2017-04-01 16:35:56 +08:00
//直播,时间长度永远
if(fDuration <= 0){
2018-10-24 15:43:52 +08:00
_strSdp += "a=range:npt=0-\r\n";
2017-04-01 16:35:56 +08:00
}else{
2018-10-24 15:43:52 +08:00
_strSdp += StrPrinter <<"a=range:npt=0-" << fDuration << "\r\n" << endl;
2017-04-01 16:35:56 +08:00
}
2018-10-24 15:43:52 +08:00
_strSdp += "a=control:*\r\n";
2017-04-01 16:35:56 +08:00
}
DevChannel::~DevChannel() {
}
#ifdef ENABLE_X264
2017-04-01 16:35:56 +08:00
void DevChannel::inputYUV(char* apcYuv[3], int aiYuvLen[3], uint32_t uiStamp) {
//TimeTicker1(50);
2018-10-24 15:43:52 +08:00
if (!_pH264Enc) {
_pH264Enc.reset(new H264Encoder());
if (!_pH264Enc->init(_video->iWidth, _video->iHeight, _video->iFrameRate)) {
_pH264Enc.reset();
2017-04-01 16:35:56 +08:00
WarnL << "H264Encoder init failed!";
}
}
2018-10-24 15:43:52 +08:00
if (_pH264Enc) {
2017-04-01 16:35:56 +08:00
H264Encoder::H264Frame *pOut;
2018-10-24 15:43:52 +08:00
int iFrames = _pH264Enc->inputData(apcYuv, aiYuvLen, uiStamp, &pOut);
2017-04-01 16:35:56 +08:00
for (int i = 0; i < iFrames; i++) {
inputH264((char *) pOut[i].pucData, pOut[i].iLength, uiStamp);
}
}
}
#endif //ENABLE_X264
2017-04-01 16:35:56 +08:00
#ifdef ENABLE_FAAC
void DevChannel::inputPCM(char* pcData, int iDataLen, uint32_t uiStamp) {
2018-10-24 15:43:52 +08:00
if (!_pAacEnc) {
_pAacEnc.reset(new AACEncoder());
if (!_pAacEnc->init(_audio->iSampleRate, _audio->iChannel, _audio->iSampleBit)) {
_pAacEnc.reset();
2017-04-01 16:35:56 +08:00
WarnL << "AACEncoder init failed!";
}
}
2018-10-24 15:43:52 +08:00
if (_pAacEnc) {
2017-04-01 16:35:56 +08:00
unsigned char *pucOut;
2018-10-24 15:43:52 +08:00
int iRet = _pAacEnc->inputData(pcData, iDataLen, &pucOut);
2017-04-01 16:35:56 +08:00
if (iRet > 0) {
inputAAC((char *) pucOut, iRet, uiStamp);
}
}
}
#endif //ENABLE_FAAC
2017-04-01 16:35:56 +08:00
2018-08-29 11:19:00 +08:00
void DevChannel::inputH264(const char* pcData, int iDataLen, uint32_t uiStamp) {
2018-10-24 15:43:52 +08:00
if (!_pRtpMaker_h264) {
2017-04-01 16:35:56 +08:00
uint32_t ui32Ssrc;
memcpy(&ui32Ssrc, makeRandStr(4, false).data(), 4);
auto lam = [this](const RtpPacket::Ptr &pkt, bool bKeyPos) {
2018-10-25 16:55:48 +08:00
onWrite(pkt,bKeyPos);
2017-04-01 16:35:56 +08:00
};
2018-10-24 17:17:55 +08:00
GET_CONFIG_AND_REGISTER(uint32_t,videoMtu,Rtp::kVideoMtuSize);
2018-10-24 15:43:52 +08:00
_pRtpMaker_h264.reset(new RtpMaker_H264(lam, ui32Ssrc,videoMtu));
2017-04-01 16:35:56 +08:00
}
2018-10-24 15:43:52 +08:00
if (!_bSdp_gotH264 && _video) {
2017-04-01 16:35:56 +08:00
makeSDP_264((unsigned char*) pcData, iDataLen);
}
int iOffset = 4;
if (memcmp("\x00\x00\x01", pcData, 3) == 0) {
iOffset = 3;
}
2018-03-02 15:00:04 +08:00
if(uiStamp == 0){
2018-10-24 15:43:52 +08:00
uiStamp = (uint32_t)_aTicker[0].elapsedTime();
2018-03-02 15:00:04 +08:00
}
2018-10-24 15:43:52 +08:00
_pRtpMaker_h264->makeRtp(pcData + iOffset, iDataLen - iOffset, uiStamp);
2017-04-01 16:35:56 +08:00
}
2018-08-29 11:19:00 +08:00
void DevChannel::inputAAC(const char* pcData, int iDataLen, uint32_t uiStamp,bool withAdtsHeader) {
if(withAdtsHeader){
inputAAC(pcData+7,iDataLen-7,uiStamp,pcData);
2018-10-24 15:43:52 +08:00
} else if(_pAdtsHeader){
_pAdtsHeader->aac_frame_length = iDataLen;
writeAdtsHeader(*_pAdtsHeader,(uint8_t *)_pAdtsHeader->buffer);
inputAAC(pcData,iDataLen,uiStamp,(const char *)_pAdtsHeader->buffer);
2018-08-29 11:19:00 +08:00
}
2017-08-24 09:54:25 +08:00
}
2018-08-29 11:19:00 +08:00
void DevChannel::inputAAC(const char *pcDataWithoutAdts,int iDataLen, uint32_t uiStamp,const char *pcAdtsHeader){
2018-10-24 15:43:52 +08:00
if (!_pRtpMaker_aac) {
2017-04-01 16:35:56 +08:00
uint32_t ssrc;
memcpy(&ssrc, makeRandStr(8, false).data() + 4, 4);
auto lam = [this](const RtpPacket::Ptr &pkt, bool keyPos) {
2018-10-25 16:55:48 +08:00
onWrite(pkt,keyPos);
2017-04-01 16:35:56 +08:00
};
2018-10-24 17:17:55 +08:00
GET_CONFIG_AND_REGISTER(uint32_t,audioMtu,Rtp::kAudioMtuSize);
2018-10-24 15:43:52 +08:00
_pRtpMaker_aac.reset(new RtpMaker_AAC(lam, ssrc, audioMtu,_audio->iSampleRate));
2017-04-01 16:35:56 +08:00
}
2018-10-24 15:43:52 +08:00
if (!_bSdp_gotAAC && _audio && pcAdtsHeader) {
2017-12-19 10:34:52 +08:00
makeSDP_AAC((unsigned char*) pcAdtsHeader);
2017-04-01 16:35:56 +08:00
}
2018-03-02 15:00:04 +08:00
if(uiStamp == 0){
2018-10-24 15:43:52 +08:00
uiStamp = (uint32_t)_aTicker[1].elapsedTime();
2018-03-02 15:00:04 +08:00
}
2017-12-19 10:34:52 +08:00
if(pcDataWithoutAdts && iDataLen){
2018-10-24 15:43:52 +08:00
_pRtpMaker_aac->makeRtp(pcDataWithoutAdts, iDataLen, uiStamp);
2017-12-19 10:34:52 +08:00
}
2017-04-01 16:35:56 +08:00
}
inline void DevChannel::makeSDP_264(unsigned char *pcData, int iDataLen) {
int offset = 4;
if (memcmp("\x00\x00\x01", pcData, 3) == 0) {
offset = 3;
}
switch (pcData[offset] & 0x1F) {
case 7:/*SPS frame*/
{
2018-10-24 15:43:52 +08:00
if (_uiSPSLen != 0) {
2017-04-01 16:35:56 +08:00
break;
}
2018-10-24 15:43:52 +08:00
memcpy(_aucSPS, pcData + offset, iDataLen - offset);
_uiSPSLen = iDataLen - offset;
2017-04-01 16:35:56 +08:00
}
break;
case 8:/*PPS frame*/
{
2018-10-24 15:43:52 +08:00
if (_uiPPSLen != 0) {
2017-04-01 16:35:56 +08:00
break;
}
2018-10-24 15:43:52 +08:00
memcpy(_aucPPS, pcData + offset, iDataLen - offset);
_uiPPSLen = iDataLen - offset;
2017-04-01 16:35:56 +08:00
}
break;
default:
break;
}
2018-10-24 15:43:52 +08:00
if (!_uiSPSLen || !_uiPPSLen) {
2017-04-01 16:35:56 +08:00
return;
}
char acTmp[256];
int profile_level_id = 0;
2018-10-24 15:43:52 +08:00
if (_uiSPSLen >= 4) { // sanity check
profile_level_id = (_aucSPS[1] << 16) | (_aucSPS[2] << 8) | _aucSPS[3]; // profile_idc|constraint_setN_flag|level_idc
2017-04-01 16:35:56 +08:00
}
//视频通道
2018-10-24 15:43:52 +08:00
_strSdp += StrPrinter << "m=video 0 RTP/AVP "
<< _pRtpMaker_h264->getPlayloadType() << "\r\n" << endl;
_strSdp += "b=AS:5100\r\n";
_strSdp += StrPrinter << "a=rtpmap:" << _pRtpMaker_h264->getPlayloadType()
<< " H264/" << _pRtpMaker_h264->getSampleRate() << "\r\n" << endl;
_strSdp += StrPrinter << "a=fmtp:" << _pRtpMaker_h264->getPlayloadType()
2017-04-01 16:35:56 +08:00
<< " packetization-mode=1;profile-level-id=" << endl;
memset(acTmp, 0, sizeof(acTmp));
sprintf(acTmp, "%06X", profile_level_id);
2018-10-24 15:43:52 +08:00
_strSdp += acTmp;
_strSdp += ";sprop-parameter-sets=";
2017-04-01 16:35:56 +08:00
memset(acTmp, 0, sizeof(acTmp));
2018-10-24 15:43:52 +08:00
av_base64_encode(acTmp, sizeof(acTmp), (uint8_t *) _aucSPS, _uiSPSLen);
2017-04-01 16:35:56 +08:00
//WarnL<<"SPS base64:"<<strTemp;
//WarnL<<"SPS hexdump:"<<hexdump(SPS_BUF, SPS_LEN);
2018-10-24 15:43:52 +08:00
_strSdp += acTmp;
_strSdp += ",";
2017-04-01 16:35:56 +08:00
memset(acTmp, 0, sizeof(acTmp));
2018-10-24 15:43:52 +08:00
av_base64_encode(acTmp, sizeof(acTmp), (uint8_t *) _aucPPS, _uiPPSLen);
_strSdp += acTmp;
_strSdp += "\r\n";
if (_video->iFrameRate > 0 && _video->iHeight > 0 && _video->iWidth > 0) {
_strSdp += "a=framerate:";
_strSdp += StrPrinter << _video->iFrameRate << endl;
_strSdp += StrPrinter << "\r\na=framesize:"
<< _pRtpMaker_h264->getPlayloadType() << " " << endl;
_strSdp += StrPrinter << _video->iWidth << endl;
_strSdp += "-";
_strSdp += StrPrinter << _video->iHeight << endl;
_strSdp += "\r\n";
2017-04-01 16:35:56 +08:00
}
2018-10-24 15:43:52 +08:00
_strSdp += StrPrinter << "a=control:trackID="
<< _pRtpMaker_h264->getInterleaved() / 2 << "\r\n" << endl;
_bSdp_gotH264 = true;
if (_audio) {
if (_bSdp_gotAAC) {
makeSDP(_strSdp);
2017-04-01 16:35:56 +08:00
}
} else {
2018-10-24 15:43:52 +08:00
makeSDP(_strSdp);
2017-04-01 16:35:56 +08:00
}
}
2017-12-19 10:34:52 +08:00
inline void DevChannel::makeSDP_AAC(unsigned char *fixedHeader) {
2017-04-01 16:35:56 +08:00
auto audioSpecificConfig = makeAdtsConfig(fixedHeader);
if (audioSpecificConfig.size() != 2) {
return;
}
char fConfigStr[5] = { 0 };
sprintf(fConfigStr, "%02X%02x", (uint8_t)audioSpecificConfig[0],(uint8_t)audioSpecificConfig[1]);
2018-10-24 15:43:52 +08:00
_strSdp += StrPrinter << "m=audio 0 RTP/AVP "
<< _pRtpMaker_aac->getPlayloadType() << "\r\n" << endl;
_strSdp += "b=AS:96\r\n";
_strSdp += StrPrinter << "a=rtpmap:" << _pRtpMaker_aac->getPlayloadType()
<< " MPEG4-GENERIC/" << _pRtpMaker_aac->getSampleRate() << "\r\n"
2017-04-01 16:35:56 +08:00
<< endl;
2018-10-24 15:43:52 +08:00
_strSdp += StrPrinter << "a=fmtp:" << _pRtpMaker_aac->getPlayloadType()
2017-04-01 16:35:56 +08:00
<< " streamtype=5;profile-level-id=1;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3;config="
<< endl;
2018-10-24 15:43:52 +08:00
_strSdp += fConfigStr;
_strSdp += "\r\n";
_strSdp += StrPrinter << "a=control:trackID="
<< _pRtpMaker_aac->getInterleaved() / 2 << "\r\n" << endl;
2017-04-01 16:35:56 +08:00
2018-10-24 15:43:52 +08:00
_bSdp_gotAAC = true;
if (_video) {
if (_bSdp_gotH264) {
makeSDP(_strSdp);
2017-04-01 16:35:56 +08:00
}
} else {
2018-10-24 15:43:52 +08:00
makeSDP(_strSdp);
2017-04-01 16:35:56 +08:00
}
}
void DevChannel::makeSDP(const string& strSdp) {
onGetSDP(strSdp);
2017-04-01 16:35:56 +08:00
}
void DevChannel::initVideo(const VideoInfo& info) {
2018-10-24 15:43:52 +08:00
_video.reset(new VideoInfo(info));
2017-04-01 16:35:56 +08:00
}
void DevChannel::initAudio(const AudioInfo& info) {
2018-10-24 15:43:52 +08:00
_audio.reset(new AudioInfo(info));
_pAdtsHeader = std::make_shared<AACFrame>();
2018-08-29 11:19:00 +08:00
2018-10-24 15:43:52 +08:00
_pAdtsHeader->syncword = 0x0FFF;
_pAdtsHeader->id = 0;
_pAdtsHeader->layer = 0;
_pAdtsHeader->protection_absent = 1;
_pAdtsHeader->profile = info.iProfile;//audioObjectType - 1;
2018-08-29 11:19:00 +08:00
int i = 0;
for(auto rate : samplingFrequencyTable){
if(rate == info.iSampleRate){
2018-10-24 15:43:52 +08:00
_pAdtsHeader->sf_index = i;
2018-08-29 11:19:00 +08:00
};
++i;
}
2018-10-24 15:43:52 +08:00
_pAdtsHeader->private_bit = 0;
_pAdtsHeader->channel_configuration = info.iChannel;
_pAdtsHeader->original = 0;
_pAdtsHeader->home = 0;
_pAdtsHeader->copyright_identification_bit = 0;
_pAdtsHeader->copyright_identification_start = 0;
_pAdtsHeader->aac_frame_length = 7;
_pAdtsHeader->adts_buffer_fullness = 2047;
_pAdtsHeader->no_raw_data_blocks_in_frame = 0;
2018-08-29 11:19:00 +08:00
2017-04-01 16:35:56 +08:00
}
2018-10-24 17:17:55 +08:00
} /* namespace mediakit */
2017-04-01 16:35:56 +08:00