ZLMediaKit/src/Common/Device.cpp

167 lines
5.2 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"
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,
bool bEanbleHls,
2018-10-29 11:48:24 +08:00
bool bEnableMp4) :
MultiMediaSourceMuxer(strVhost, strApp, strId, fDuration, 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) {
//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-10-29 12:27:13 +08:00
void DevChannel::inputH264(const char* pcData, int iDataLen, uint32_t uiStamp) {
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-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;
}
2018-10-29 11:48:24 +08:00
inputFrame(std::make_shared<H264FrameNoCopyAble>((char *)pcData,iDataLen,uiStamp,prefixeSize));
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-29 11:50:36 +08:00
} else if(_audio) {
2018-10-29 11:48:24 +08:00
inputAAC(pcData,iDataLen,uiStamp,(char *)_adtsHeader);
2018-08-29 11:19:00 +08:00
}
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){
inputFrame(std::make_shared<AACFrameNoCopyAble>((char *)pcDataWithoutAdts - 7,iDataLen + 7,uiStamp,7));
2017-04-01 16:35:56 +08:00
} else {
2018-10-29 11:48:24 +08:00
char *dataWithAdts = new char[iDataLen + 7];
memcpy(dataWithAdts,pcAdtsHeader,7);
memcpy(dataWithAdts + 7 , pcDataWithoutAdts , iDataLen);
inputFrame(std::make_shared<AACFrameNoCopyAble>(dataWithAdts,iDataLen + 7,uiStamp,7));
delete [] dataWithAdts;
2017-04-01 16:35:56 +08:00
}
}
void DevChannel::initVideo(const VideoInfo& info) {
2018-10-29 11:48:24 +08:00
_video = std::make_shared<VideoInfo>(info);
addTrack(std::make_shared<H264Track>());
2017-04-01 16:35:56 +08:00
}
void DevChannel::initAudio(const AudioInfo& info) {
2018-10-29 11:48: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;
2018-08-29 11:19:00 +08:00
int i = 0;
for(auto rate : samplingFrequencyTable){
if(rate == info.iSampleRate){
2018-10-29 11:48:24 +08:00
adtsHeader.sf_index = i;
2018-08-29 11:19:00 +08:00
};
++i;
}
2018-10-29 11:48:24 +08:00
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