ZLMediaKit/src/RtmpMuxer/RtmpDemuxer.cpp

235 lines
6.7 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
*
2017-09-27 16:20:30 +08:00
* 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.
2017-04-01 16:35:56 +08:00
*/
2018-10-24 18:09:54 +08:00
#include "RtmpDemuxer.h"
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
2018-10-24 18:09:54 +08:00
RtmpDemuxer::RtmpDemuxer(const AMFValue &val) {
2017-04-01 16:35:56 +08:00
auto videoCodec = val["videocodecid"];
auto audioCodec = val["audiocodecid"];
if (videoCodec.type() == AMF_STRING) {
if (videoCodec.as_string() == "avc1") {
//h264
2018-10-24 15:43:52 +08:00
_iVideoCodecID = H264_CODEC_ID;
2017-04-01 16:35:56 +08:00
} else {
2017-08-09 18:39:30 +08:00
InfoL << "不支持RTMP视频格式:" << videoCodec.as_string();
2017-04-01 16:35:56 +08:00
}
}else if (videoCodec.type() != AMF_NULL){
2018-10-24 15:43:52 +08:00
_iVideoCodecID = videoCodec.as_integer();
if (_iVideoCodecID != H264_CODEC_ID) {
2017-08-09 18:39:30 +08:00
InfoL << "不支持RTMP视频格式:" << videoCodec.as_integer();
2017-04-01 16:35:56 +08:00
}
}
2017-04-01 16:35:56 +08:00
if (audioCodec.type() == AMF_STRING) {
if (audioCodec.as_string() == "mp4a") {
//aac
2018-10-24 15:43:52 +08:00
_iAudioCodecID = AAC_CODEC_ID;
2017-04-01 16:35:56 +08:00
} else {
2017-08-09 18:39:30 +08:00
InfoL << "不支持RTMP音频格式:" << audioCodec.as_string();
2017-04-01 16:35:56 +08:00
}
}else if (audioCodec.type() != AMF_NULL) {
2018-10-24 15:43:52 +08:00
_iAudioCodecID = audioCodec.as_integer();
if (_iAudioCodecID != AAC_CODEC_ID) {
2017-08-09 18:39:30 +08:00
InfoL << "不支持RTMP音频格式:" << audioCodec.as_integer();
2017-04-01 16:35:56 +08:00
}
}
onCheckMedia(val);
}
2018-10-24 18:09:54 +08:00
RtmpDemuxer::~RtmpDemuxer() {
2017-04-01 16:35:56 +08:00
}
2018-10-24 18:09:54 +08:00
bool RtmpDemuxer::inputRtmp(const RtmpPacket::Ptr &pkt) {
switch (pkt->typeId) {
case MSG_VIDEO:{
2018-10-24 15:43:52 +08:00
if(_iVideoCodecID == 0){
//未初始化视频
2018-10-24 15:43:52 +08:00
_iVideoCodecID = pkt->getMediaType();
if(_iVideoCodecID != H264_CODEC_ID){
InfoL << "不支持RTMP视频格式:" << _iVideoCodecID;
}
}
2018-10-24 15:43:52 +08:00
if(_iVideoCodecID == H264_CODEC_ID){
return inputVideo(pkt);
}
return false;
}
case MSG_AUDIO: {
2018-10-24 15:43:52 +08:00
if(_iAudioCodecID == 0){
//未初始化音频
2018-10-24 15:43:52 +08:00
_iAudioCodecID = pkt->getMediaType();
if(_iAudioCodecID != AAC_CODEC_ID){
InfoL << "不支持RTMP音频格式:" << _iAudioCodecID;
}
}
2018-10-24 15:43:52 +08:00
if (_iAudioCodecID == AAC_CODEC_ID) {
return inputAudio(pkt);
}
return false;
}
default:
return false;
}
2017-04-01 16:35:56 +08:00
}
2018-10-24 18:09:54 +08:00
inline bool RtmpDemuxer::inputVideo(const RtmpPacket::Ptr &pkt) {
2017-12-04 23:55:09 +08:00
if (pkt->isCfgFrame()) {
2017-04-01 16:35:56 +08:00
//WarnL << " got h264 cfg";
2018-10-24 15:43:52 +08:00
if (_strSPS.size()) {
2017-04-01 16:35:56 +08:00
return false;
}
2018-10-24 15:43:52 +08:00
_strSPS.assign("\x00\x00\x00\x01", 4);
_strSPS.append(pkt->getH264SPS());
2017-04-01 16:35:56 +08:00
2018-10-24 15:43:52 +08:00
_strPPS.assign("\x00\x00\x00\x01", 4);
_strPPS.append(pkt->getH264PPS());
2017-04-01 16:35:56 +08:00
2018-10-24 15:43:52 +08:00
getAVCInfo(pkt->getH264SPS(), _iVideoWidth, _iVideoHeight, _fVideoFps);
2017-04-01 16:35:56 +08:00
return false;
}
2018-10-24 15:43:52 +08:00
if (_strSPS.size()) {
2017-12-04 23:55:09 +08:00
uint32_t iTotalLen = pkt->strBuf.size();
2017-04-01 16:35:56 +08:00
uint32_t iOffset = 5;
while(iOffset + 4 < iTotalLen){
uint32_t iFrameLen;
2017-12-04 23:55:09 +08:00
memcpy(&iFrameLen, pkt->strBuf.data() + iOffset, 4);
2017-04-01 16:35:56 +08:00
iFrameLen = ntohl(iFrameLen);
iOffset += 4;
if(iFrameLen + iOffset > iTotalLen){
break;
}
2017-12-04 23:55:09 +08:00
_onGetH264(pkt->strBuf.data() + iOffset, iFrameLen, pkt->timeStamp);
2017-04-01 16:35:56 +08:00
iOffset += iFrameLen;
}
}
2017-12-04 23:55:09 +08:00
return pkt->isVideoKeyFrame();
2017-04-01 16:35:56 +08:00
}
2018-10-24 18:09:54 +08:00
inline void RtmpDemuxer::_onGetH264(const char* pcData, int iLen, uint32_t ui32TimeStamp) {
2017-04-01 16:35:56 +08:00
switch (pcData[0] & 0x1F) {
case 5: {
2018-10-24 15:43:52 +08:00
onGetH264(_strSPS.data() + 4, _strSPS.length() - 4, ui32TimeStamp);
onGetH264(_strPPS.data() + 4, _strPPS.length() - 4, ui32TimeStamp);
2017-04-01 16:35:56 +08:00
}
case 1: {
onGetH264(pcData, iLen, ui32TimeStamp);
}
break;
default:
//WarnL <<(int)(pcData[0] & 0x1F);
break;
}
}
2018-10-24 18:09:54 +08:00
inline void RtmpDemuxer::onGetH264(const char* pcData, int iLen, uint32_t ui32TimeStamp) {
2018-10-24 15:43:52 +08:00
_h264frame.type = pcData[0] & 0x1F;
_h264frame.timeStamp = ui32TimeStamp;
_h264frame.buffer.assign("\x0\x0\x0\x1", 4); //添加264头
_h264frame.buffer.append(pcData, iLen);
2017-04-01 16:35:56 +08:00
{
2018-10-24 15:43:52 +08:00
lock_guard<recursive_mutex> lck(_mtxCB);
2017-04-01 16:35:56 +08:00
if (onVideo) {
2018-10-24 15:43:52 +08:00
onVideo(_h264frame);
2017-04-01 16:35:56 +08:00
}
}
2018-10-24 15:43:52 +08:00
_h264frame.buffer.clear();
2017-04-01 16:35:56 +08:00
}
2018-10-24 18:09:54 +08:00
inline bool RtmpDemuxer::inputAudio(const RtmpPacket::Ptr &pkt) {
2017-12-04 23:55:09 +08:00
if (pkt->isCfgFrame()) {
2018-10-24 15:43:52 +08:00
if (_strAudioCfg.size()) {
2017-04-01 16:35:56 +08:00
return false;
}
2018-10-24 15:43:52 +08:00
_strAudioCfg = pkt->getAacCfg();
_iSampleBit = pkt->getAudioSampleBit();
makeAdtsHeader(_strAudioCfg,_adts);
getAACInfo(_adts, _iSampleRate, _iChannel);
2017-04-01 16:35:56 +08:00
return false;
}
2018-10-24 15:43:52 +08:00
if (_strAudioCfg.size()) {
2017-12-04 23:55:09 +08:00
onGetAAC(pkt->strBuf.data() + 2, pkt->strBuf.size() - 2, pkt->timeStamp);
2017-04-01 16:35:56 +08:00
}
return false;
}
2018-10-24 18:09:54 +08:00
inline void RtmpDemuxer::onGetAAC(const char* pcData, int iLen, uint32_t ui32TimeStamp) {
2018-10-24 15:43:52 +08:00
if(iLen + 7 > sizeof(_adts.buffer)){
2018-08-29 18:25:17 +08:00
WarnL << "Illegal adts data, exceeding the length limit.";
return;
}
2017-04-01 16:35:56 +08:00
//添加adts头
2018-10-24 15:43:52 +08:00
memcpy(_adts.buffer + 7, pcData, iLen);
_adts.aac_frame_length = 7 + iLen;
_adts.timeStamp = ui32TimeStamp;
writeAdtsHeader(_adts, _adts.buffer);
2017-04-01 16:35:56 +08:00
{
2018-10-24 15:43:52 +08:00
lock_guard<recursive_mutex> lck(_mtxCB);
2017-04-01 16:35:56 +08:00
if (onAudio) {
2018-10-24 15:43:52 +08:00
onAudio(_adts);
2017-04-01 16:35:56 +08:00
}
}
2018-10-24 15:43:52 +08:00
_adts.aac_frame_length = 7;
2017-04-01 16:35:56 +08:00
}
2018-10-24 18:09:54 +08:00
inline void RtmpDemuxer::onCheckMedia(const AMFValue& obj) {
2017-04-01 16:35:56 +08:00
obj.object_for_each([&](const string &key ,const AMFValue& val) {
if(key == "duration") {
2018-10-24 15:43:52 +08:00
_fDuration = val.as_number();
2017-04-01 16:35:56 +08:00
return;
}
if(key == "width") {
2018-10-24 15:43:52 +08:00
_iVideoWidth = val.as_number();
2017-04-01 16:35:56 +08:00
return;
}
if(key == "height") {
2018-10-24 15:43:52 +08:00
_iVideoHeight = val.as_number();
2017-04-01 16:35:56 +08:00
return;
}
if(key == "framerate") {
2018-10-24 15:43:52 +08:00
_fVideoFps = val.as_number();
2017-04-01 16:35:56 +08:00
return;
}
if(key == "audiosamplerate") {
2018-10-24 15:43:52 +08:00
_iSampleRate = val.as_number();
2017-04-01 16:35:56 +08:00
return;
}
if(key == "audiosamplesize") {
2018-10-24 15:43:52 +08:00
_iSampleBit = val.as_number();
2017-04-01 16:35:56 +08:00
return;
}
if(key == "stereo") {
2018-10-24 15:43:52 +08:00
_iChannel = val.as_boolean() ? 2 :1;
2017-04-01 16:35:56 +08:00
return;
}
});
}
2018-10-24 17:17:55 +08:00
} /* namespace mediakit */