Device.cpp 5.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
 */
xiongzilaing committed
26 27
#include <stdio.h>
#include <stdio.h>
xzl committed
28 29 30
#include "Device.h"
#include "Util/logger.h"
#include "Util/util.h"
xiongziliang committed
31
#include "Util/base64.h"
xzl committed
32
#include "Util/TimeTicker.h"
xiongzilaing committed
33

xiongziliang committed
34
using namespace toolkit;
xzl committed
35

xiongziliang committed
36
namespace mediakit {
xzl committed
37

xiongziliang committed
38 39 40
DevChannel::DevChannel(const string &strVhost,
                       const string &strApp,
                       const string &strId,
41 42
                       float fDuration,
                       bool bEanbleHls,
43 44
                       bool bEnableMp4) :
        MultiMediaSourceMuxer(strVhost, strApp, strId, fDuration, bEanbleHls, bEnableMp4) {}
xzl committed
45

46
DevChannel::~DevChannel() {}
xzl committed
47

48
#ifdef ENABLE_X264
xzl committed
49 50
void DevChannel::inputYUV(char* apcYuv[3], int aiYuvLen[3], uint32_t uiStamp) {
	//TimeTicker1(50);
51 52 53 54
	if (!_pH264Enc) {
		_pH264Enc.reset(new H264Encoder());
		if (!_pH264Enc->init(_video->iWidth, _video->iHeight, _video->iFrameRate)) {
			_pH264Enc.reset();
xzl committed
55 56 57
			WarnL << "H264Encoder init failed!";
		}
	}
58
	if (_pH264Enc) {
xzl committed
59
		H264Encoder::H264Frame *pOut;
60
		int iFrames = _pH264Enc->inputData(apcYuv, aiYuvLen, uiStamp, &pOut);
xzl committed
61 62 63 64 65
		for (int i = 0; i < iFrames; i++) {
			inputH264((char *) pOut[i].pucData, pOut[i].iLength, uiStamp);
		}
	}
}
66
#endif //ENABLE_X264
xzl committed
67 68

#ifdef ENABLE_FAAC
69
void DevChannel::inputPCM(char* pcData, int iDataLen, uint32_t uiStamp) {
70 71 72 73
	if (!_pAacEnc) {
		_pAacEnc.reset(new AACEncoder());
		if (!_pAacEnc->init(_audio->iSampleRate, _audio->iChannel, _audio->iSampleBit)) {
			_pAacEnc.reset();
xzl committed
74 75 76
			WarnL << "AACEncoder init failed!";
		}
	}
77
	if (_pAacEnc) {
xzl committed
78
		unsigned char *pucOut;
79
		int iRet = _pAacEnc->inputData(pcData, iDataLen, &pucOut);
xzl committed
80 81 82 83 84
		if (iRet > 0) {
			inputAAC((char *) pucOut, iRet, uiStamp);
		}
	}
}
85
#endif //ENABLE_FAAC
xzl committed
86

87
void DevChannel::inputH264(const char* pcData, int iDataLen, uint32_t uiStamp) {
xiongziliang committed
88
    if(uiStamp == 0){
89
        uiStamp = (uint32_t)_aTicker[0].elapsedTime();
xiongziliang committed
90
    }
91 92 93 94 95 96 97 98 99 100 101

    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;
    }

102
    inputFrame(std::make_shared<H264FrameNoCopyAble>((char *)pcData,iDataLen,uiStamp,prefixeSize));
xzl committed
103 104
}

105 106 107
void DevChannel::inputAAC(const char* pcData, int iDataLen, uint32_t uiStamp,bool withAdtsHeader) {
	if(withAdtsHeader){
		inputAAC(pcData+7,iDataLen-7,uiStamp,pcData);
xiongziliang committed
108
	} else if(_audio) {
109
		inputAAC(pcData,iDataLen,uiStamp,(char *)_adtsHeader);
110
	}
111
}
112

113
void DevChannel::inputAAC(const char *pcDataWithoutAdts,int iDataLen, uint32_t uiStamp,const char *pcAdtsHeader){
xiongziliang committed
114
    if(uiStamp == 0){
115
        uiStamp = (uint32_t)_aTicker[1].elapsedTime();
xiongziliang committed
116
    }
117 118
    if(pcAdtsHeader + 7 == pcDataWithoutAdts){
		inputFrame(std::make_shared<AACFrameNoCopyAble>((char *)pcDataWithoutAdts - 7,iDataLen + 7,uiStamp,7));
xzl committed
119
	} else {
120 121 122 123 124
    	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;
xzl committed
125 126 127 128 129
	}
}


void DevChannel::initVideo(const VideoInfo& info) {
130 131
	_video = std::make_shared<VideoInfo>(info);
	addTrack(std::make_shared<H264Track>());
xzl committed
132 133 134
}

void DevChannel::initAudio(const AudioInfo& info) {
135 136 137 138 139 140 141 142 143
	_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;
144 145 146
	int i = 0;
	for(auto rate : samplingFrequencyTable){
		if(rate == info.iSampleRate){
147
			adtsHeader.sf_index = i;
148 149 150
		};
		++i;
	}
151 152 153 154 155 156 157 158 159 160
	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);
xzl committed
161
}
162

xiongziliang committed
163
} /* namespace mediakit */
xzl committed
164