Device.cpp 7.4 KB
Newer Older
xzl committed
1 2 3 4 5 6 7
/*
 * Device.cpp
 *
 *  Created on: 2016年8月10日
 *      Author: xzl
 */

xiongzilaing committed
8 9 10
#include <stdio.h>
#include <stdio.h>
#include "base64.h"
xzl committed
11 12 13 14
#include "Device.h"
#include "Util/logger.h"
#include "Util/util.h"
#include "Util/TimeTicker.h"
xiongzilaing committed
15

xzl committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
using namespace ZL::Util;

namespace ZL {
namespace DEV {


/////////////////////////////////////////////////////////////////////////////////
#ifdef ENABLE_RTSP2RTMP
DevChannel::DevChannel(const char *strApp, const char *strId,float fDuration,bool bLiveStream ) :
		m_mediaSrc(new RtspToRtmpMediaSource(strApp,strId , bLiveStream)) {
#else
DevChannel::DevChannel(const char *strApp, const char *strId,float fDuration,bool bLiveStream ) :
		m_mediaSrc(new RtspToRtmpMediaSource(strApp,strId )) {
#endif //ENABLE_RTSP2RTMP
	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";
	//直播,时间长度永远
	if(fDuration <= 0 || bLiveStream){
		m_strSDP += "a=range:npt=0-\r\n";
	}else{
		m_strSDP += StrPrinter <<"a=range:npt=0-" << fDuration  << "\r\n" << endl;
	}
	m_strSDP += "a=control:*\r\n";
}
DevChannel::~DevChannel() {
}

void DevChannel::inputYUV(char* apcYuv[3], int aiYuvLen[3], uint32_t uiStamp) {
	//TimeTicker1(50);
#ifdef ENABLE_X264
	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);
		}
	}
#else
	ErrorL << "libx264 was not enabled!";
#endif //ENABLE_X264
}

void DevChannel::inputPCM(char* pcData, int iDataLen, uint32_t uiStamp) {
#ifdef ENABLE_FAAC
	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);
		}
	}
#else
	ErrorL << "libfaac was not enabled!";
#endif //ENABLE_FAAC
}

void DevChannel::inputH264(char* pcData, int iDataLen, uint32_t uiStamp) {
	if (!m_pRtpMaker_h264) {
		uint32_t ui32Ssrc;
		memcpy(&ui32Ssrc, makeRandStr(4, false).data(), 4);
		auto lam = [this](const RtpPacket::Ptr &pkt, bool bKeyPos) {
			m_mediaSrc->onGetRTP(pkt,bKeyPos);
		};
		static uint32_t videoMtu = mINI::Instance()[Config::Rtp::kVideoMtuSize].as<uint32_t>();
		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;
	}
	m_pRtpMaker_h264->makeRtp(pcData + iOffset, iDataLen - iOffset, uiStamp);
}

void DevChannel::inputAAC(char* pcData, int iDataLen, uint32_t uiStamp) {
	if (!m_pRtpMaker_aac) {
		uint32_t ssrc;
		memcpy(&ssrc, makeRandStr(8, false).data() + 4, 4);
		auto lam = [this](const RtpPacket::Ptr &pkt, bool keyPos) {
			m_mediaSrc->onGetRTP(pkt,keyPos);
		};
		static uint32_t audioMtu = mINI::Instance()[Config::Rtp::kAudioMtuSize].as<uint32_t>();
		m_pRtpMaker_aac.reset(new RtpMaker_AAC(lam, ssrc, audioMtu,m_audio->iSampleRate));
	}
	if (!m_bSdp_gotAAC && m_audio) {
		makeSDP_AAC((unsigned char*) pcData, iDataLen);
	}
	m_pRtpMaker_aac->makeRtp(pcData + 7, iDataLen - 7, uiStamp);

}

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
	}

	//视频通道
	m_strSDP += StrPrinter << "m=video 0 RTP/AVP "
			<< m_pRtpMaker_h264->getPlayloadType() << "\r\n" << endl;
	m_strSDP += "b=AS:5100\r\n";
	m_strSDP += StrPrinter << "a=rtpmap:" << m_pRtpMaker_h264->getPlayloadType()
			<< " H264/" << m_pRtpMaker_h264->getSampleRate() << "\r\n" << endl;
	m_strSDP += StrPrinter << "a=fmtp:" << m_pRtpMaker_h264->getPlayloadType()
			<< " packetization-mode=1;profile-level-id=" << endl;

	memset(acTmp, 0, sizeof(acTmp));
	sprintf(acTmp, "%06X", profile_level_id);
	m_strSDP += acTmp;
	m_strSDP += ";sprop-parameter-sets=";
	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);
	m_strSDP += acTmp;
	m_strSDP += ",";
	memset(acTmp, 0, sizeof(acTmp));
	av_base64_encode(acTmp, sizeof(acTmp), (uint8_t *) m_aucPPS, m_uiPPSLen);
	m_strSDP += acTmp;
	m_strSDP += "\r\n";
	if (m_video->iFrameRate > 0 && m_video->iHeight > 0 && m_video->iWidth > 0) {
		m_strSDP += "a=framerate:";
		m_strSDP += StrPrinter << m_video->iFrameRate << endl;
		m_strSDP += StrPrinter << "\r\na=framesize:"
				<< m_pRtpMaker_h264->getPlayloadType() << " " << endl;
		m_strSDP += StrPrinter << m_video->iWidth << endl;
		m_strSDP += "-";
		m_strSDP += StrPrinter << m_video->iHeight << endl;
		m_strSDP += "\r\n";
	}
	m_strSDP += StrPrinter << "a=control:trackID="
			<< m_pRtpMaker_h264->getInterleaved() / 2 << "\r\n" << endl;
	m_bSdp_gotH264 = true;
	if (m_audio) {
		if (m_bSdp_gotAAC) {
			makeSDP(m_strSDP);
		}
	} else {
		makeSDP(m_strSDP);
	}
}

inline void DevChannel::makeSDP_AAC(unsigned char *fixedHeader, int dataLen) {
	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]);

	m_strSDP += StrPrinter << "m=audio 0 RTP/AVP "
			<< m_pRtpMaker_aac->getPlayloadType() << "\r\n" << endl;
	m_strSDP += "b=AS:96\r\n";
	m_strSDP += StrPrinter << "a=rtpmap:" << m_pRtpMaker_aac->getPlayloadType()
			<< " MPEG4-GENERIC/" << m_pRtpMaker_aac->getSampleRate() << "\r\n"
			<< endl;
	m_strSDP += StrPrinter << "a=fmtp:" << m_pRtpMaker_aac->getPlayloadType()
				<< " streamtype=5;profile-level-id=1;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3;config="
				<< endl;
	m_strSDP += fConfigStr;
	m_strSDP += "\r\n";
	m_strSDP += StrPrinter << "a=control:trackID="
			<< m_pRtpMaker_aac->getInterleaved() / 2 << "\r\n" << endl;

	m_bSdp_gotAAC = true;
	if (m_video) {
		if (m_bSdp_gotH264) {
			makeSDP(m_strSDP);
		}
	} else {
		makeSDP(m_strSDP);
	}
}

void DevChannel::makeSDP(const string& strSdp) {
	m_mediaSrc->onGetSDP(strSdp);
	m_mediaSrc->regist();
}

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

void DevChannel::initAudio(const AudioInfo& info) {
	m_audio.reset(new AudioInfo(info));
}
} /* namespace DEV */
} /* namespace ZL */