ZLMediaKit/src/Rtmp/RtmpToRtspMediaSource.cpp

160 lines
6.0 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
*/
2017-05-02 17:15:12 +08:00
#include "Common/config.h"
2017-04-01 16:35:56 +08:00
#include "RtmpToRtspMediaSource.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 "Network/sockutil.h"
2018-10-24 17:17:55 +08:00
using namespace toolkit;
2017-04-25 11:35:41 +08:00
2018-10-24 17:17:55 +08:00
namespace mediakit {
2017-04-01 16:35:56 +08:00
RtmpToRtspMediaSource::RtmpToRtspMediaSource(const string &vhost,
const string &app,
const string &id,
bool bEnableHls,
bool bEnableMp4) :
2018-10-24 15:43:52 +08:00
RtmpMediaSource(vhost,app,id),_bEnableHls(bEnableHls),_bEnableMp4(bEnableMp4) {
2017-04-01 16:35:56 +08:00
}
RtmpToRtspMediaSource::~RtmpToRtspMediaSource() {}
2017-04-01 16:35:56 +08:00
void RtmpToRtspMediaSource::onGetH264(const H264Frame &frame) {
2018-10-24 15:43:52 +08:00
if(_pRecorder){
_pRecorder->inputH264((char *) frame.data(), frame.size(), frame.timeStamp, frame.type);
}
2017-04-01 16:35:56 +08:00
2018-10-24 15:43:52 +08:00
if(_pRtpMaker_h264){
_pRtpMaker_h264->makeRtp(frame.data() + 4, frame.size() - 4, frame.timeStamp);
2017-04-01 16:35:56 +08:00
}
}
2018-10-24 14:21:59 +08:00
inline void RtmpToRtspMediaSource::onGetAAC(const AACFrame &frame) {
2018-10-24 15:43:52 +08:00
if(_pRecorder){
_pRecorder->inputAAC((char *) frame.buffer, frame.aac_frame_length, frame.timeStamp);
}
2017-04-01 16:35:56 +08:00
2018-10-24 15:43:52 +08:00
if (_pRtpMaker_aac) {
_pRtpMaker_aac->makeRtp((char *) frame.buffer + 7, frame.aac_frame_length - 7, frame.timeStamp);
2017-04-01 16:35:56 +08:00
}
}
void RtmpToRtspMediaSource::makeSDP() {
string strSDP;
strSDP = "v=0\r\n";
strSDP += "o=- 1383190487994921 1 IN IP4 0.0.0.0\r\n";
strSDP += "s=RTSP Session, streamed by the ZL\r\n";
strSDP += "i=ZL Live Stream\r\n";
strSDP += "c=IN IP4 0.0.0.0\r\n";
strSDP += "t=0 0\r\n";
2018-10-24 15:43:52 +08:00
if(_pParser->getDuration() <= 0){
2017-04-01 16:35:56 +08:00
strSDP += "a=range:npt=0-\r\n";
}else{
2018-10-24 15:43:52 +08:00
strSDP += StrPrinter << "0-"<< _pParser->getDuration()<< "\r\n" << endl;
2017-04-01 16:35:56 +08:00
}
strSDP += "a=control:*\r\n";
2018-02-09 11:42:55 +08:00
2018-10-23 16:41:25 +08:00
//todo(xzl) 修复此处
2018-10-24 15:43:52 +08:00
// if (_pParser->containVideo()) {
2018-10-23 16:41:25 +08:00
// uint32_t ssrc0;
// memcpy(&ssrc0, makeRandStr(4, false).data(), 4);
// auto lam = [this](const RtpPacket::Ptr &pkt, bool bKeyPos) {
2018-10-24 15:43:52 +08:00
// _pRtspSrc->onGetRTP(pkt,bKeyPos);
2018-10-23 16:41:25 +08:00
// };
//
2018-10-24 17:17:55 +08:00
// GET_CONFIG_AND_REGISTER(uint32_t,videoMtu,Rtp::kVideoMtuSize);
2018-10-24 15:43:52 +08:00
// _pRtpMaker_h264.reset(new RtpMaker_H264(lam, ssrc0,videoMtu));
2018-10-23 16:41:25 +08:00
//
// char strTemp[100];
// int profile_level_id = 0;
2018-10-24 15:43:52 +08:00
// string strSPS =_pParser->getSps().substr(4);
// string strPPS =_pParser->getPps().substr(4);
2018-10-23 16:41:25 +08:00
// if (strSPS.length() >= 4) { // sanity check
// profile_level_id = (strSPS[1] << 16) | (strSPS[2] << 8) | strSPS[3]; // profile_idc|constraint_setN_flag|level_idc
// }
//
// //视频通道
2018-10-24 15:43:52 +08:00
// strSDP += StrPrinter << "m=video 0 RTP/AVP " << _pRtpMaker_h264->getPlayloadType()
2018-10-23 16:41:25 +08:00
// << "\r\n" << endl;
// strSDP += "b=AS:5100\r\n";
2018-10-24 15:43:52 +08:00
// strSDP += StrPrinter << "a=rtpmap:" << _pRtpMaker_h264->getPlayloadType()
// << " H264/" << _pRtpMaker_h264->getSampleRate() << "\r\n" << endl;
// strSDP += StrPrinter << "a=fmtp:" << _pRtpMaker_h264->getPlayloadType()
2018-10-23 16:41:25 +08:00
// << " packetization-mode=1;profile-level-id=" << endl;
//
// memset(strTemp, 0, 100);
// sprintf(strTemp, "%06X", profile_level_id);
// strSDP += strTemp;
// strSDP += ";sprop-parameter-sets=";
// memset(strTemp, 0, 100);
// av_base64_encode(strTemp, 100, (uint8_t *) strSPS.data(), strSPS.size());
// strSDP += strTemp;
// strSDP += ",";
// memset(strTemp, 0, 100);
// av_base64_encode(strTemp, 100, (uint8_t *) strPPS.data(), strPPS.size());
// strSDP += strTemp;
// strSDP += "\r\n";
2018-10-24 15:43:52 +08:00
// strSDP += StrPrinter << "a=control:trackID=" << _pRtpMaker_h264->getInterleaved() / 2
2018-10-23 16:41:25 +08:00
// << "\r\n" << endl;
// }
//
2018-10-24 15:43:52 +08:00
// if (_pParser->containAudio()) {
2018-10-23 16:41:25 +08:00
// uint32_t ssrc1;
// memcpy(&ssrc1, makeRandStr(8, false).data() + 4, 4);
// auto lam = [this](const RtpPacket::Ptr &pkt, bool bKeyPos) {
2018-10-24 15:43:52 +08:00
// _pRtspSrc->onGetRTP(pkt,bKeyPos);
2018-10-23 16:41:25 +08:00
// };
2018-10-24 17:17:55 +08:00
// GET_CONFIG_AND_REGISTER(uint32_t,audioMtu,Rtp::kAudioMtuSize);
2018-10-24 15:43:52 +08:00
// _pRtpMaker_aac.reset(new RtpMaker_AAC(lam, ssrc1, audioMtu,_pParser->getAudioSampleRate()));
2018-10-23 16:41:25 +08:00
//
// char configStr[32];
2018-10-24 15:43:52 +08:00
// const string & strAacCfg = _pParser->getAudioCfg();
2018-10-23 16:41:25 +08:00
// snprintf(configStr, sizeof(configStr), "%02X%02x", strAacCfg[0], strAacCfg[1]);
2018-10-24 15:43:52 +08:00
// strSDP += StrPrinter << "m=audio 0 RTP/AVP " << _pRtpMaker_aac->getPlayloadType()
2018-10-23 16:41:25 +08:00
// << "\r\n" << endl;
// strSDP += "b=AS:96\r\n";
2018-10-24 15:43:52 +08:00
// strSDP += StrPrinter << "a=rtpmap:" << _pRtpMaker_aac->getPlayloadType()
// << " MPEG4-GENERIC/" << _pRtpMaker_aac->getSampleRate() << "\r\n"
2018-10-23 16:41:25 +08:00
// << endl;
2018-10-24 15:43:52 +08:00
// strSDP += StrPrinter << "a=fmtp:" << _pRtpMaker_aac->getPlayloadType()
2018-10-23 16:41:25 +08:00
// << " streamtype=5;profile-level-id=1;mode=AAC-hbr;"
// << "sizelength=13;indexlength=3;indexdeltalength=3;config="
// << endl;
// strSDP.append(configStr, 4);
// strSDP += "\r\n";
2018-10-24 15:43:52 +08:00
// strSDP += StrPrinter << "a=control:trackID=" << _pRtpMaker_aac->getInterleaved() / 2
2018-10-23 16:41:25 +08:00
// << "\r\n" << endl;
// }
2017-04-01 16:35:56 +08:00
2018-10-24 15:43:52 +08:00
_pRtspSrc.reset(new RtspMediaSource(getVhost(),getApp(),getId()));
_pRtspSrc->setListener(_listener);
_pRtspSrc->onGetSDP(strSDP);
2017-04-01 16:35:56 +08:00
}
2018-10-24 17:17:55 +08:00
} /* namespace mediakit */