ZLMediaKit/src/Common/Device.cpp

207 lines
6.8 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
*
2019-05-08 15:40:07 +08:00
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
2017-09-27 16:20:30 +08:00
*
* 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"
2019-06-28 17:25:53 +08:00
#include "Extension/AAC.h"
#include "Extension/H264.h"
2019-11-01 15:40:21 +08:00
#include "Extension/H265.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
2019-03-27 18:41:52 +08:00
DevChannel::DevChannel(const string &strVhost,
const string &strApp,
const string &strId,
float fDuration,
2020-03-20 11:51:24 +08:00
bool bEanbleRtsp,
bool bEanbleRtmp,
bool bEanbleHls,
2018-10-29 11:48:24 +08:00
bool bEnableMp4) :
MultiMediaSourceMuxer(strVhost, strApp, strId, fDuration, bEanbleRtsp, bEanbleRtmp, bEanbleHls, bEnableMp4) {}
2017-04-01 16:35:56 +08:00
2018-10-29 11:48:24 +08:00
DevChannel::~DevChannel() {}
2017-04-01 16:35:56 +08:00
#ifdef ENABLE_X264
2017-04-01 16:35:56 +08:00
void DevChannel::inputYUV(char* apcYuv[3], int aiYuvLen[3], uint32_t uiStamp) {
2020-03-20 11:51:24 +08:00
//TimeTicker1(50);
if (!_pH264Enc) {
_pH264Enc.reset(new H264Encoder());
if (!_pH264Enc->init(_video->iWidth, _video->iHeight, _video->iFrameRate)) {
_pH264Enc.reset();
WarnL << "H264Encoder init failed!";
}
}
if (_pH264Enc) {
H264Encoder::H264Frame *pOut;
int iFrames = _pH264Enc->inputData(apcYuv, aiYuvLen, uiStamp, &pOut);
for (int i = 0; i < iFrames; i++) {
inputH264((char *) pOut[i].pucData, pOut[i].iLength, uiStamp);
}
}
2017-04-01 16:35:56 +08:00
}
#endif //ENABLE_X264
2017-04-01 16:35:56 +08:00
#ifdef ENABLE_FAAC
void DevChannel::inputPCM(char* pcData, int iDataLen, uint32_t uiStamp) {
2020-03-20 11:51:24 +08:00
if (!_pAacEnc) {
_pAacEnc.reset(new AACEncoder());
if (!_pAacEnc->init(_audio->iSampleRate, _audio->iChannel, _audio->iSampleBit)) {
_pAacEnc.reset();
WarnL << "AACEncoder init failed!";
}
}
if (_pAacEnc) {
unsigned char *pucOut;
int iRet = _pAacEnc->inputData(pcData, iDataLen, &pucOut);
if (iRet > 0) {
inputAAC((char *) pucOut, iRet, uiStamp);
}
}
2017-04-01 16:35:56 +08:00
}
#endif //ENABLE_FAAC
2017-04-01 16:35:56 +08:00
2019-07-03 16:22:12 +08:00
void DevChannel::inputH264(const char* pcData, int iDataLen, uint32_t dts,uint32_t pts) {
if(dts == 0){
2020-03-20 11:51:24 +08:00
dts = (uint32_t)_aTicker[0].elapsedTime();
}
if(pts == 0){
pts = dts;
2018-03-02 15:00:04 +08:00
}
2018-10-29 12:27:13 +08:00
int prefixeSize;
if (memcmp("\x00\x00\x00\x01", pcData, 4) == 0) {
prefixeSize = 4;
} else if (memcmp("\x00\x00\x01", pcData, 3) == 0) {
prefixeSize = 3;
} else {
prefixeSize = 0;
}
2020-01-08 12:14:27 +08:00
2020-03-20 11:51:24 +08:00
H264Frame::Ptr frame = std::make_shared<H264Frame>();
frame->_dts = dts;
frame->_pts = pts;
frame->_buffer.assign("\x00\x00\x00\x01",4);
frame->_buffer.append(pcData + prefixeSize, iDataLen - prefixeSize);
frame->_prefix_size = 4;
2020-01-08 12:14:27 +08:00
inputFrame(frame);
2017-04-01 16:35:56 +08:00
}
2019-11-01 15:40:21 +08:00
void DevChannel::inputH265(const char* pcData, int iDataLen, uint32_t dts,uint32_t pts) {
2020-03-20 11:51:24 +08:00
if(dts == 0){
dts = (uint32_t)_aTicker[0].elapsedTime();
}
if(pts == 0){
pts = dts;
}
int prefixeSize;
if (memcmp("\x00\x00\x00\x01", pcData, 4) == 0) {
prefixeSize = 4;
} else if (memcmp("\x00\x00\x01", pcData, 3) == 0) {
prefixeSize = 3;
} else {
prefixeSize = 0;
}
H265Frame::Ptr frame = std::make_shared<H265Frame>();
frame->_dts = dts;
frame->_pts = pts;
frame->_buffer.assign("\x00\x00\x00\x01",4);
frame->_buffer.append(pcData + prefixeSize, iDataLen - prefixeSize);
frame->_prefix_size = 4;
inputFrame(frame);
2019-11-01 15:40:21 +08:00
}
2018-08-29 11:19:00 +08:00
void DevChannel::inputAAC(const char* pcData, int iDataLen, uint32_t uiStamp,bool withAdtsHeader) {
2020-03-20 11:51:24 +08:00
if(withAdtsHeader){
inputAAC(pcData+7,iDataLen-7,uiStamp,pcData);
} else if(_audio) {
inputAAC(pcData,iDataLen,uiStamp,(char *)_adtsHeader);
}
2017-08-24 09:54:25 +08:00
}
2018-10-29 11:48:24 +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-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
}
2018-10-29 11:48:24 +08:00
if(pcAdtsHeader + 7 == pcDataWithoutAdts){
2020-04-03 20:45:58 +08:00
inputFrame(std::make_shared<AACFrameNoCacheAble>((char *)pcDataWithoutAdts - 7,iDataLen + 7,uiStamp,0,7));
2020-03-20 11:51:24 +08:00
} else {
char *dataWithAdts = new char[iDataLen + 7];
memcpy(dataWithAdts,pcAdtsHeader,7);
memcpy(dataWithAdts + 7 , pcDataWithoutAdts , iDataLen);
2020-04-03 20:45:58 +08:00
inputFrame(std::make_shared<AACFrameNoCacheAble>(dataWithAdts,iDataLen + 7,uiStamp,0,7));
2020-03-20 11:51:24 +08:00
delete [] dataWithAdts;
}
2017-04-01 16:35:56 +08:00
}
void DevChannel::initVideo(const VideoInfo& info) {
2020-03-20 11:51:24 +08:00
_video = std::make_shared<VideoInfo>(info);
addTrack(std::make_shared<H264Track>());
2017-04-01 16:35:56 +08:00
}
2019-11-01 15:40:21 +08:00
void DevChannel::initH265Video(const VideoInfo &info){
2020-03-20 11:51:24 +08:00
_video = std::make_shared<VideoInfo>(info);
addTrack(std::make_shared<H265Track>());
2019-11-01 15:40:21 +08:00
}
2017-04-01 16:35:56 +08:00
void DevChannel::initAudio(const AudioInfo& info) {
2020-03-20 11:51:24 +08:00
_audio = std::make_shared<AudioInfo>(info);
addTrack(std::make_shared<AACTrack>());
AACFrame adtsHeader;
adtsHeader.syncword = 0x0FFF;
adtsHeader.id = 0;
adtsHeader.layer = 0;
adtsHeader.protection_absent = 1;
adtsHeader.profile = info.iProfile;//audioObjectType - 1;
int i = 0;
for(auto rate : samplingFrequencyTable){
if(rate == info.iSampleRate){
adtsHeader.sf_index = i;
};
++i;
}
adtsHeader.private_bit = 0;
adtsHeader.channel_configuration = info.iChannel;
adtsHeader.original = 0;
adtsHeader.home = 0;
adtsHeader.copyright_identification_bit = 0;
adtsHeader.copyright_identification_start = 0;
adtsHeader.aac_frame_length = 7;
adtsHeader.adts_buffer_fullness = 2047;
adtsHeader.no_raw_data_blocks_in_frame = 0;
writeAdtsHeader(adtsHeader,_adtsHeader);
2017-04-01 16:35:56 +08:00
}
2018-10-26 16:09:48 +08:00
2018-10-24 17:17:55 +08:00
} /* namespace mediakit */
2017-04-01 16:35:56 +08:00