Device.cpp 9.61 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

xzl committed
34 35 36 37 38
using namespace ZL::Util;

namespace ZL {
namespace DEV {

39 40 41 42 43 44 45
DevChannel::DevChannel(const char *strVhost,
                       const char *strApp,
                       const char *strId,
                       float fDuration,
                       bool bEanbleHls,
                       bool bEnableMp4 ) :
        RtspToRtmpMediaSource(strVhost,strApp,strId,bEanbleHls,bEnableMp4) {
xzl committed
46

47 48 49 50 51 52
	m_strSdp = "v=0\r\n";
	m_strSdp += "o=- 1383190487994921 1 IN IP4 0.0.0.0\r\n";
	m_strSdp += "s=RTSP Session, streamed by the ZL\r\n";
	m_strSdp += "i=ZL Live Stream\r\n";
	m_strSdp += "c=IN IP4 0.0.0.0\r\n";
	m_strSdp += "t=0 0\r\n";
xzl committed
53
	//直播,时间长度永远
54
	if(fDuration <= 0){
55
		m_strSdp += "a=range:npt=0-\r\n";
xzl committed
56
	}else{
57
		m_strSdp += StrPrinter <<"a=range:npt=0-" << fDuration  << "\r\n" << endl;
xzl committed
58
	}
59
	m_strSdp += "a=control:*\r\n";
xzl committed
60 61 62 63
}
DevChannel::~DevChannel() {
}

64
#ifdef ENABLE_X264
xzl committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
void DevChannel::inputYUV(char* apcYuv[3], int aiYuvLen[3], uint32_t uiStamp) {
	//TimeTicker1(50);
	if (!m_pH264Enc) {
		m_pH264Enc.reset(new H264Encoder());
		if (!m_pH264Enc->init(m_video->iWidth, m_video->iHeight, m_video->iFrameRate)) {
			m_pH264Enc.reset();
			WarnL << "H264Encoder init failed!";
		}
	}
	if (m_pH264Enc) {
		H264Encoder::H264Frame *pOut;
		int iFrames = m_pH264Enc->inputData(apcYuv, aiYuvLen, uiStamp, &pOut);
		for (int i = 0; i < iFrames; i++) {
			inputH264((char *) pOut[i].pucData, pOut[i].iLength, uiStamp);
		}
	}
}
82
#endif //ENABLE_X264
xzl committed
83 84

#ifdef ENABLE_FAAC
85
void DevChannel::inputPCM(char* pcData, int iDataLen, uint32_t uiStamp) {
xzl committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
	if (!m_pAacEnc) {
		m_pAacEnc.reset(new AACEncoder());
		if (!m_pAacEnc->init(m_audio->iSampleRate, m_audio->iChannel, m_audio->iSampleBit)) {
			m_pAacEnc.reset();
			WarnL << "AACEncoder init failed!";
		}
	}
	if (m_pAacEnc) {
		unsigned char *pucOut;
		int iRet = m_pAacEnc->inputData(pcData, iDataLen, &pucOut);
		if (iRet > 0) {
			inputAAC((char *) pucOut, iRet, uiStamp);
		}
	}
}
101
#endif //ENABLE_FAAC
xzl committed
102

103
void DevChannel::inputH264(const char* pcData, int iDataLen, uint32_t uiStamp) {
xzl committed
104 105 106 107
	if (!m_pRtpMaker_h264) {
		uint32_t ui32Ssrc;
		memcpy(&ui32Ssrc, makeRandStr(4, false).data(), 4);
		auto lam = [this](const RtpPacket::Ptr &pkt, bool bKeyPos) {
108
			onGetRTP(pkt,bKeyPos);
xzl committed
109
		};
110
        GET_CONFIG_AND_REGISTER(uint32_t,videoMtu,Config::Rtp::kVideoMtuSize);
xzl committed
111 112 113 114 115 116 117 118 119
		m_pRtpMaker_h264.reset(new RtpMaker_H264(lam, ui32Ssrc,videoMtu));
	}
	if (!m_bSdp_gotH264 && m_video) {
		makeSDP_264((unsigned char*) pcData, iDataLen);
	}
	int iOffset = 4;
	if (memcmp("\x00\x00\x01", pcData, 3) == 0) {
		iOffset = 3;
	}
xiongziliang committed
120 121 122
    if(uiStamp == 0){
        uiStamp = (uint32_t)m_aTicker[0].elapsedTime();
    }
xzl committed
123 124 125
	m_pRtpMaker_h264->makeRtp(pcData + iOffset, iDataLen - iOffset, uiStamp);
}

126 127 128 129 130
void DevChannel::inputAAC(const char* pcData, int iDataLen, uint32_t uiStamp,bool withAdtsHeader) {
	if(withAdtsHeader){
		inputAAC(pcData+7,iDataLen-7,uiStamp,pcData);
	} else if(m_pAdtsHeader){
		m_pAdtsHeader->aac_frame_length = iDataLen;
131 132
		writeAdtsHeader(*m_pAdtsHeader,(uint8_t *)m_pAdtsHeader->buffer);
		inputAAC(pcData,iDataLen,uiStamp,(const char *)m_pAdtsHeader->buffer);
133
	}
134
}
135
void DevChannel::inputAAC(const char *pcDataWithoutAdts,int iDataLen, uint32_t uiStamp,const char *pcAdtsHeader){
xzl committed
136 137 138 139
	if (!m_pRtpMaker_aac) {
		uint32_t ssrc;
		memcpy(&ssrc, makeRandStr(8, false).data() + 4, 4);
		auto lam = [this](const RtpPacket::Ptr &pkt, bool keyPos) {
140
			onGetRTP(pkt,keyPos);
xzl committed
141
		};
142 143
        GET_CONFIG_AND_REGISTER(uint32_t,audioMtu,Config::Rtp::kAudioMtuSize);
        m_pRtpMaker_aac.reset(new RtpMaker_AAC(lam, ssrc, audioMtu,m_audio->iSampleRate));
xzl committed
144
	}
145 146
	if (!m_bSdp_gotAAC && m_audio && pcAdtsHeader) {
		makeSDP_AAC((unsigned char*) pcAdtsHeader);
xzl committed
147
	}
xiongziliang committed
148 149 150
    if(uiStamp == 0){
        uiStamp = (uint32_t)m_aTicker[1].elapsedTime();
    }
151 152 153
    if(pcDataWithoutAdts && iDataLen){
        m_pRtpMaker_aac->makeRtp(pcDataWithoutAdts, iDataLen, uiStamp);
    }
xzl committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
}

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*/
	{
		if (m_uiSPSLen != 0) {
			break;
		}
		memcpy(m_aucSPS, pcData + offset, iDataLen - offset);
		m_uiSPSLen = iDataLen - offset;
	}
		break;
	case 8:/*PPS frame*/
	{
		if (m_uiPPSLen != 0) {
			break;
		}
		memcpy(m_aucPPS, pcData + offset, iDataLen - offset);
		m_uiPPSLen = iDataLen - offset;
	}
		break;
	default:
		break;
	}
	if (!m_uiSPSLen || !m_uiPPSLen) {
		return;
	}

	char acTmp[256];
	int profile_level_id = 0;
	if (m_uiSPSLen >= 4) { // sanity check
		profile_level_id = (m_aucSPS[1] << 16) | (m_aucSPS[2] << 8) | m_aucSPS[3]; // profile_idc|constraint_setN_flag|level_idc
	}

	//视频通道
194
	m_strSdp += StrPrinter << "m=video 0 RTP/AVP "
xzl committed
195
			<< m_pRtpMaker_h264->getPlayloadType() << "\r\n" << endl;
196 197
	m_strSdp += "b=AS:5100\r\n";
	m_strSdp += StrPrinter << "a=rtpmap:" << m_pRtpMaker_h264->getPlayloadType()
xzl committed
198
			<< " H264/" << m_pRtpMaker_h264->getSampleRate() << "\r\n" << endl;
199
	m_strSdp += StrPrinter << "a=fmtp:" << m_pRtpMaker_h264->getPlayloadType()
xzl committed
200 201 202 203
			<< " packetization-mode=1;profile-level-id=" << endl;

	memset(acTmp, 0, sizeof(acTmp));
	sprintf(acTmp, "%06X", profile_level_id);
204 205
	m_strSdp += acTmp;
	m_strSdp += ";sprop-parameter-sets=";
xzl committed
206 207 208 209
	memset(acTmp, 0, sizeof(acTmp));
	av_base64_encode(acTmp, sizeof(acTmp), (uint8_t *) m_aucSPS, m_uiSPSLen);
	//WarnL<<"SPS base64:"<<strTemp;
	//WarnL<<"SPS hexdump:"<<hexdump(SPS_BUF, SPS_LEN);
210 211
	m_strSdp += acTmp;
	m_strSdp += ",";
xzl committed
212 213
	memset(acTmp, 0, sizeof(acTmp));
	av_base64_encode(acTmp, sizeof(acTmp), (uint8_t *) m_aucPPS, m_uiPPSLen);
214 215
	m_strSdp += acTmp;
	m_strSdp += "\r\n";
xzl committed
216
	if (m_video->iFrameRate > 0 && m_video->iHeight > 0 && m_video->iWidth > 0) {
217 218 219
		m_strSdp += "a=framerate:";
		m_strSdp += StrPrinter << m_video->iFrameRate << endl;
		m_strSdp += StrPrinter << "\r\na=framesize:"
xzl committed
220
				<< m_pRtpMaker_h264->getPlayloadType() << " " << endl;
221 222 223 224
		m_strSdp += StrPrinter << m_video->iWidth << endl;
		m_strSdp += "-";
		m_strSdp += StrPrinter << m_video->iHeight << endl;
		m_strSdp += "\r\n";
xzl committed
225
	}
226
	m_strSdp += StrPrinter << "a=control:trackID="
xzl committed
227 228 229 230
			<< m_pRtpMaker_h264->getInterleaved() / 2 << "\r\n" << endl;
	m_bSdp_gotH264 = true;
	if (m_audio) {
		if (m_bSdp_gotAAC) {
231
			makeSDP(m_strSdp);
xzl committed
232 233
		}
	} else {
234
		makeSDP(m_strSdp);
xzl committed
235 236 237
	}
}

238
inline void DevChannel::makeSDP_AAC(unsigned char *fixedHeader) {
xzl committed
239 240 241 242 243 244 245 246
	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]);

247
	m_strSdp += StrPrinter << "m=audio 0 RTP/AVP "
xzl committed
248
			<< m_pRtpMaker_aac->getPlayloadType() << "\r\n" << endl;
249 250
	m_strSdp += "b=AS:96\r\n";
	m_strSdp += StrPrinter << "a=rtpmap:" << m_pRtpMaker_aac->getPlayloadType()
xzl committed
251 252
			<< " MPEG4-GENERIC/" << m_pRtpMaker_aac->getSampleRate() << "\r\n"
			<< endl;
253
	m_strSdp += StrPrinter << "a=fmtp:" << m_pRtpMaker_aac->getPlayloadType()
xzl committed
254 255
				<< " streamtype=5;profile-level-id=1;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3;config="
				<< endl;
256 257 258
	m_strSdp += fConfigStr;
	m_strSdp += "\r\n";
	m_strSdp += StrPrinter << "a=control:trackID="
xzl committed
259 260 261 262 263
			<< m_pRtpMaker_aac->getInterleaved() / 2 << "\r\n" << endl;

	m_bSdp_gotAAC = true;
	if (m_video) {
		if (m_bSdp_gotH264) {
264
			makeSDP(m_strSdp);
xzl committed
265 266
		}
	} else {
267
		makeSDP(m_strSdp);
xzl committed
268 269 270 271
	}
}

void DevChannel::makeSDP(const string& strSdp) {
272
	onGetSDP(strSdp);
xzl committed
273 274 275 276 277 278 279 280
}

void DevChannel::initVideo(const VideoInfo& info) {
	m_video.reset(new VideoInfo(info));
}

void DevChannel::initAudio(const AudioInfo& info) {
	m_audio.reset(new AudioInfo(info));
281
	m_pAdtsHeader = std::make_shared<AACFrame>();
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305

	m_pAdtsHeader->syncword = 0x0FFF;
	m_pAdtsHeader->id = 0;
	m_pAdtsHeader->layer = 0;
	m_pAdtsHeader->protection_absent = 1;
	m_pAdtsHeader->profile =  info.iProfile;//audioObjectType - 1;
	int i = 0;
	for(auto rate : samplingFrequencyTable){
		if(rate == info.iSampleRate){
			m_pAdtsHeader->sf_index = i;
		};
		++i;
	}

	m_pAdtsHeader->private_bit = 0;
	m_pAdtsHeader->channel_configuration = info.iChannel;
	m_pAdtsHeader->original = 0;
	m_pAdtsHeader->home = 0;
	m_pAdtsHeader->copyright_identification_bit = 0;
	m_pAdtsHeader->copyright_identification_start = 0;
	m_pAdtsHeader->aac_frame_length = 7;
	m_pAdtsHeader->adts_buffer_fullness = 2047;
	m_pAdtsHeader->no_raw_data_blocks_in_frame = 0;

xzl committed
306 307 308 309
}
} /* namespace DEV */
} /* namespace ZL */