ZLMediaKit/src/Extension/Factory.cpp

232 lines
7.8 KiB
C++
Raw Normal View History

2018-10-25 10:00:17 +08:00
/*
* MIT License
*
2019-05-08 15:40:07 +08:00
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
2018-10-25 10:00:17 +08:00
*
* 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.
*/
2018-10-24 09:23:57 +08:00
#include "Factory.h"
2019-06-28 17:37:11 +08:00
#include "H264Rtmp.h"
#include "AACRtmp.h"
#include "H264Rtp.h"
#include "AACRtp.h"
#include "H265Rtp.h"
2019-09-05 14:56:33 +08:00
#include "Common/Parser.h"
2018-10-24 09:23:57 +08:00
2018-10-24 17:17:55 +08:00
namespace mediakit{
2018-10-26 09:56:29 +08:00
Track::Ptr Factory::getTrackBySdp(const SdpTrack::Ptr &track) {
2018-11-27 11:05:44 +08:00
if (strcasecmp(track->_codec.data(), "mpeg4-generic") == 0) {
2019-03-27 18:41:52 +08:00
string aac_cfg_str = FindField(track->_fmtp.data(), "config=", nullptr);
2019-08-06 16:23:20 +08:00
if (aac_cfg_str.empty()) {
2019-03-27 18:41:52 +08:00
aac_cfg_str = FindField(track->_fmtp.data(), "config=", ";");
2018-10-24 09:23:57 +08:00
}
2019-08-06 16:23:20 +08:00
if (aac_cfg_str.empty()) {
2019-10-14 16:52:15 +08:00
//如果sdp中获取不到aac config信息那么在rtp也无法获取那么忽略该Track
return nullptr;
2018-10-24 09:23:57 +08:00
}
string aac_cfg;
unsigned int cfg1;
2019-03-27 18:41:52 +08:00
sscanf(aac_cfg_str.substr(0, 2).data(), "%02X", &cfg1);
2018-10-24 09:23:57 +08:00
cfg1 &= 0x00FF;
aac_cfg.push_back(cfg1);
unsigned int cfg2;
2019-03-27 18:41:52 +08:00
sscanf(aac_cfg_str.substr(2, 2).data(), "%02X", &cfg2);
2018-10-24 09:23:57 +08:00
cfg2 &= 0x00FF;
aac_cfg.push_back(cfg2);
return std::make_shared<AACTrack>(aac_cfg);
}
2018-11-27 11:05:44 +08:00
if (strcasecmp(track->_codec.data(), "h264") == 0) {
2019-10-08 09:39:54 +08:00
//a=fmtp:96 packetization-mode=1;profile-level-id=42C01F;sprop-parameter-sets=Z0LAH9oBQBboQAAAAwBAAAAPI8YMqA==,aM48gA==
2019-10-08 09:44:08 +08:00
auto map = Parser::parseArgs(FindField(track->_fmtp.data()," ", nullptr),";","=");
auto sps_pps = map["sprop-parameter-sets"];
2019-03-27 18:41:52 +08:00
string base64_SPS = FindField(sps_pps.data(), NULL, ",");
string base64_PPS = FindField(sps_pps.data(), ",", NULL);
2018-10-24 09:23:57 +08:00
auto sps = decodeBase64(base64_SPS);
auto pps = decodeBase64(base64_PPS);
2019-11-21 15:11:43 +08:00
if(sps.empty() || pps.empty()){
2019-11-21 16:31:50 +08:00
//如果sdp里面没有sps/pps,那么可能在后续的rtp里面恢复出sps/pps
2019-11-21 15:11:43 +08:00
return std::make_shared<H264Track>();
}
2018-10-24 09:23:57 +08:00
return std::make_shared<H264Track>(sps,pps,0,0);
}
2018-11-27 11:05:44 +08:00
if (strcasecmp(track->_codec.data(), "h265") == 0) {
2018-10-30 17:11:36 +08:00
//a=fmtp:96 sprop-sps=QgEBAWAAAAMAsAAAAwAAAwBdoAKAgC0WNrkky/AIAAADAAgAAAMBlQg=; sprop-pps=RAHA8vA8kAA=
2019-10-08 09:44:08 +08:00
auto map = Parser::parseArgs(FindField(track->_fmtp.data()," ", nullptr),";","=");
2019-09-05 14:56:33 +08:00
auto vps = decodeBase64(map["sprop-vps"]);
auto sps = decodeBase64(map["sprop-sps"]);
auto pps = decodeBase64(map["sprop-pps"]);
2019-11-21 16:31:50 +08:00
if(sps.empty() || pps.empty()){
//如果sdp里面没有sps/pps,那么可能在后续的rtp里面恢复出sps/pps
return std::make_shared<H265Track>();
}
2019-09-05 14:56:33 +08:00
return std::make_shared<H265Track>(vps,sps,pps,0,0,0);
2018-10-24 18:41:37 +08:00
}
2018-10-30 17:11:36 +08:00
WarnL << "暂不支持该sdp:" << track->_codec << " " << track->_fmtp;
return nullptr;
2018-10-24 18:41:37 +08:00
}
2018-10-30 17:11:36 +08:00
2018-10-24 18:41:37 +08:00
Track::Ptr Factory::getTrackByCodecId(CodecId codecId) {
switch (codecId){
case CodecH264:{
return std::make_shared<H264Track>();
}
2018-10-30 17:11:36 +08:00
case CodecH265:{
return std::make_shared<H265Track>();
}
2018-10-24 18:41:37 +08:00
case CodecAAC:{
return std::make_shared<AACTrack>();
}
default:
2018-10-25 09:26:11 +08:00
WarnL << "暂不支持该CodecId:" << codecId;
2018-10-24 18:41:37 +08:00
return nullptr;
}
}
2019-06-28 16:12:39 +08:00
RtpCodec::Ptr Factory::getRtpEncoderBySdp(const Sdp::Ptr &sdp) {
GET_CONFIG(uint32_t,audio_mtu,Rtp::kAudioMtuSize);
GET_CONFIG(uint32_t,video_mtu,Rtp::kVideoMtuSize);
2019-07-19 09:42:48 +08:00
// ssrc不冲突即可,可以为任意的32位整形
static atomic<uint32_t> s_ssrc(0);
uint32_t ssrc = s_ssrc++;
if(!ssrc){
//ssrc不能为0
ssrc = 1;
}
if(sdp->getTrackType() == TrackVideo){
//视频的ssrc是偶数方便调试
ssrc = 2 * ssrc;
}else{
//音频ssrc是奇数
ssrc = 2 * ssrc + 1;
}
2019-06-28 16:12:39 +08:00
auto mtu = (sdp->getTrackType() == TrackVideo ? video_mtu : audio_mtu);
auto sample_rate = sdp->getSampleRate();
auto pt = sdp->getPlayloadType();
auto interleaved = sdp->getTrackType() * 2;
auto codec_id = sdp->getCodecId();
switch (codec_id){
2018-10-24 09:23:57 +08:00
case CodecH264:
2019-06-28 16:12:39 +08:00
return std::make_shared<H264RtpEncoder>(ssrc,mtu,sample_rate,pt,interleaved);
2018-10-30 17:11:36 +08:00
case CodecH265:
2019-06-28 16:12:39 +08:00
return std::make_shared<H265RtpEncoder>(ssrc,mtu,sample_rate,pt,interleaved);
2018-10-24 09:23:57 +08:00
case CodecAAC:
2019-06-28 16:12:39 +08:00
return std::make_shared<AACRtpEncoder>(ssrc,mtu,sample_rate,pt,interleaved);
2018-10-24 09:23:57 +08:00
default:
2019-06-28 16:12:39 +08:00
WarnL << "暂不支持该CodecId:" << codec_id;
2018-10-24 09:23:57 +08:00
return nullptr;
}
}
2019-01-24 12:21:29 +08:00
RtpCodec::Ptr Factory::getRtpDecoderByTrack(const Track::Ptr &track) {
switch (track->getCodecId()){
2018-10-24 09:23:57 +08:00
case CodecH264:
return std::make_shared<H264RtpDecoder>();
2018-10-30 17:11:36 +08:00
case CodecH265:
return std::make_shared<H265RtpDecoder>();
2018-10-24 09:23:57 +08:00
case CodecAAC:
2019-01-24 12:21:29 +08:00
return std::make_shared<AACRtpDecoder>(track->clone());
2018-10-24 09:23:57 +08:00
default:
2020-03-08 21:19:20 +08:00
WarnL << "暂不支持该CodecId:" << track->getCodecName();
2018-10-24 09:23:57 +08:00
return nullptr;
}
}
2018-10-30 17:11:36 +08:00
/////////////////////////////rtmp相关///////////////////////////////////////////
Track::Ptr Factory::getTrackByAmf(const AMFValue &amf) {
CodecId codecId = getCodecIdByAmf(amf);
if(codecId == CodecInvalid){
return nullptr;
}
return getTrackByCodecId(codecId);
}
CodecId Factory::getCodecIdByAmf(const AMFValue &val){
if (val.type() == AMF_STRING){
auto str = val.as_string();
if(str == "avc1"){
return CodecH264;
}
if(str == "mp4a"){
return CodecAAC;
}
2019-12-12 22:25:55 +08:00
if(str == "hev1" || str == "hvc1"){
return CodecH265;
}
2018-10-30 17:11:36 +08:00
WarnL << "暂不支持该Amf:" << str;
return CodecInvalid;
}
if (val.type() != AMF_NULL){
auto type_id = val.as_integer();
switch (type_id){
2019-12-12 22:25:55 +08:00
case 7: return CodecH264;
case 10: return CodecAAC;
case 12: return CodecH265;
2018-10-30 17:11:36 +08:00
default:
WarnL << "暂不支持该Amf:" << type_id;
return CodecInvalid;
}
}else{
WarnL << "Metadata不存在相应的Track";
2018-10-30 17:11:36 +08:00
}
2018-10-30 17:11:36 +08:00
return CodecInvalid;
}
RtmpCodec::Ptr Factory::getRtmpCodecByTrack(const Track::Ptr &track) {
switch (track->getCodecId()){
2018-10-24 22:03:17 +08:00
case CodecH264:
return std::make_shared<H264RtmpEncoder>(track);
2018-10-24 22:03:17 +08:00
case CodecAAC:
return std::make_shared<AACRtmpEncoder>(track);
2018-10-24 22:03:17 +08:00
default:
2020-03-08 21:19:20 +08:00
WarnL << "暂不支持该CodecId:" << track->getCodecName();
2018-10-24 22:03:17 +08:00
return nullptr;
}
}
2018-10-25 14:49:47 +08:00
AMFValue Factory::getAmfByCodecId(CodecId codecId) {
switch (codecId){
2019-12-12 22:25:55 +08:00
case CodecAAC: return AMFValue("mp4a");
case CodecH264: return AMFValue("avc1");
case CodecH265: return AMFValue(12);
default: return AMFValue(AMF_NULL);
2018-10-25 14:49:47 +08:00
}
}
2018-10-24 18:41:37 +08:00
2018-10-24 17:17:55 +08:00
}//namespace mediakit