ZLMediaKit/src/Extension/Factory.cpp

265 lines
10 KiB
C++
Raw Normal View History

2018-10-25 10:00:17 +08:00
/*
2020-04-04 20:30:09 +08:00
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
2018-10-25 10:00:17 +08:00
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
2018-10-25 10:00:17 +08:00
*
2020-04-04 20:30:09 +08:00
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
2018-10-25 10:00:17 +08:00
*/
2018-10-24 09:23:57 +08:00
#include "Factory.h"
2020-04-04 22:54:49 +08:00
#include "Rtmp/Rtmp.h"
2019-06-28 17:37:11 +08:00
#include "H264Rtmp.h"
2020-04-04 22:54:49 +08:00
#include "H265Rtmp.h"
2019-06-28 17:37:11 +08:00
#include "AACRtmp.h"
2020-08-01 10:22:12 +08:00
#include "CommonRtmp.h"
2019-06-28 17:37:11 +08:00
#include "H264Rtp.h"
#include "AACRtp.h"
#include "H265Rtp.h"
2020-08-01 10:22:12 +08:00
#include "CommonRtp.h"
#include "G711Rtp.h"
2020-08-01 10:22:12 +08:00
#include "Opus.h"
#include "G711.h"
#include "L16.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) {
2021-08-23 21:15:19 +08:00
auto codec = getCodecId(track->_codec);
if (codec == CodecInvalid) {
//根据传统的payload type 获取编码类型以及采样率等信息
codec = RtpPayload::getCodecId(track->_pt);
}
2021-08-23 21:15:19 +08:00
switch (codec) {
case CodecG711A:
case CodecG711U: return std::make_shared<G711Track>(codec, track->_samplerate, track->_channel, 16);
case CodecL16: return std::make_shared<L16Track>(track->_samplerate, track->_channel);
case CodecOpus : return std::make_shared<OpusTrack>();
2021-08-23 21:15:19 +08:00
case CodecAAC : {
string aac_cfg_str = FindField(track->_fmtp.data(), "config=", ";");
if (aac_cfg_str.empty()) {
aac_cfg_str = FindField(track->_fmtp.data(), "config=", nullptr);
}
if (aac_cfg_str.empty()) {
//如果sdp中获取不到aac config信息那么在rtp也无法获取那么忽略该Track
return nullptr;
}
string aac_cfg;
for (size_t i = 0; i < aac_cfg_str.size() / 2; ++i) {
unsigned int cfg;
sscanf(aac_cfg_str.substr(i * 2, 2).data(), "%02X", &cfg);
cfg &= 0x00FF;
aac_cfg.push_back((char) cfg);
}
return std::make_shared<AACTrack>(aac_cfg);
2019-11-21 15:11:43 +08:00
}
2021-08-23 21:15:19 +08:00
case CodecH264 : {
//a=fmtp:96 packetization-mode=1;profile-level-id=42C01F;sprop-parameter-sets=Z0LAH9oBQBboQAAAAwBAAAAPI8YMqA==,aM48gA==
auto map = Parser::parseArgs(track->_fmtp, ";", "=");
auto sps_pps = map["sprop-parameter-sets"];
string base64_SPS = FindField(sps_pps.data(), NULL, ",");
string base64_PPS = FindField(sps_pps.data(), ",", NULL);
auto sps = decodeBase64(base64_SPS);
auto pps = decodeBase64(base64_PPS);
if (sps.empty() || pps.empty()) {
//如果sdp里面没有sps/pps,那么可能在后续的rtp里面恢复出sps/pps
return std::make_shared<H264Track>();
}
return std::make_shared<H264Track>(sps, pps, 0, 0);
}
2018-10-24 09:23:57 +08:00
2021-08-23 21:15:19 +08:00
case CodecH265: {
//a=fmtp:96 sprop-sps=QgEBAWAAAAMAsAAAAwAAAwBdoAKAgC0WNrkky/AIAAADAAgAAAMBlQg=; sprop-pps=RAHA8vA8kAA=
auto map = Parser::parseArgs(track->_fmtp, ";", "=");
auto vps = decodeBase64(map["sprop-vps"]);
auto sps = decodeBase64(map["sprop-sps"]);
auto pps = decodeBase64(map["sprop-pps"]);
if (sps.empty() || pps.empty()) {
//如果sdp里面没有sps/pps,那么可能在后续的rtp里面恢复出sps/pps
return std::make_shared<H265Track>();
}
return std::make_shared<H265Track>(vps, sps, pps, 0, 0, 0);
2019-11-21 16:31:50 +08:00
}
2018-10-24 18:41:37 +08:00
2021-08-23 21:15:19 +08:00
default: {
//其他codec不支持
WarnL << "暂不支持该rtsp编码类型:" << track->getName();
return nullptr;
}
2020-04-18 23:00:48 +08:00
}
2018-10-24 18:41:37 +08:00
}
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();
2020-05-25 13:51:00 +08:00
auto pt = sdp->getPayloadType();
2019-06-28 16:12:39 +08:00
auto interleaved = sdp->getTrackType() * 2;
auto codec_id = sdp->getCodecId();
switch (codec_id){
2020-08-01 10:22:12 +08:00
case CodecH264 : return std::make_shared<H264RtpEncoder>(ssrc, mtu, sample_rate, pt, interleaved);
case CodecH265 : return std::make_shared<H265RtpEncoder>(ssrc, mtu, sample_rate, pt, interleaved);
case CodecAAC : return std::make_shared<AACRtpEncoder>(ssrc, mtu, sample_rate, pt, interleaved);
case CodecL16 :
case CodecOpus : return std::make_shared<CommonRtpEncoder>(codec_id, ssrc, mtu, sample_rate, pt, interleaved);
2020-04-18 22:13:11 +08:00
case CodecG711A :
case CodecG711U :
if(pt == Rtsp::PT_PCMA || pt == Rtsp::PT_PCMU){
return std::make_shared<G711RtpEncoder>(codec_id, ssrc, mtu, sample_rate, pt, interleaved,1);
}
return std::make_shared<CommonRtpEncoder>(codec_id, ssrc, mtu, sample_rate, pt, interleaved);
2020-04-18 22:13:11 +08:00
default : WarnL << "暂不支持该CodecId:" << codec_id; return nullptr;
2018-10-24 09:23:57 +08:00
}
}
2019-01-24 12:21:29 +08:00
RtpCodec::Ptr Factory::getRtpDecoderByTrack(const Track::Ptr &track) {
switch (track->getCodecId()){
2020-04-18 22:13:11 +08:00
case CodecH264 : return std::make_shared<H264RtpDecoder>();
case CodecH265 : return std::make_shared<H265RtpDecoder>();
case CodecAAC : return std::make_shared<AACRtpDecoder>(track->clone());
case CodecL16 :
2020-08-01 10:22:12 +08:00
case CodecOpus :
2020-04-18 22:13:11 +08:00
case CodecG711A :
2020-08-01 10:22:12 +08:00
case CodecG711U : return std::make_shared<CommonRtpDecoder>(track->getCodecId());
2020-04-18 22:13:11 +08:00
default : WarnL << "暂不支持该CodecId:" << track->getCodecName(); return nullptr;
2018-10-24 09:23:57 +08:00
}
}
2018-10-30 17:11:36 +08:00
/////////////////////////////rtmp相关///////////////////////////////////////////
2020-04-18 18:46:20 +08:00
static CodecId getVideoCodecIdByAmf(const AMFValue &val){
2020-08-01 10:22:12 +08:00
if (val.type() == AMF_STRING) {
2018-10-30 17:11:36 +08:00
auto str = val.as_string();
2020-08-01 10:22:12 +08:00
if (str == "avc1") {
2018-10-30 17:11:36 +08:00
return CodecH264;
}
2020-08-01 10:22:12 +08:00
if (str == "hev1" || str == "hvc1") {
2019-12-12 22:25:55 +08:00
return CodecH265;
}
2020-08-01 10:22:12 +08:00
WarnL << "暂不支持该视频Amf:" << str;
2018-10-30 17:11:36 +08:00
return CodecInvalid;
}
2020-08-01 10:22:12 +08:00
if (val.type() != AMF_NULL) {
2018-10-30 17:11:36 +08:00
auto type_id = val.as_integer();
2020-08-01 10:22:12 +08:00
switch (type_id) {
case FLV_CODEC_H264 : return CodecH264;
case FLV_CODEC_H265 : return CodecH265;
default : WarnL << "暂不支持该视频Amf:" << type_id; return CodecInvalid;
2018-10-30 17:11:36 +08:00
}
}
return CodecInvalid;
}
2020-04-18 22:13:11 +08:00
Track::Ptr getTrackByCodecId(CodecId codecId, int sample_rate = 0, int channels = 0, int sample_bit = 0) {
switch (codecId){
case CodecH264 : return std::make_shared<H264Track>();
case CodecH265 : return std::make_shared<H265Track>();
case CodecAAC : return std::make_shared<AACTrack>();
2020-08-01 10:22:12 +08:00
case CodecOpus: return std::make_shared<OpusTrack>();
2020-04-18 22:13:11 +08:00
case CodecG711A :
case CodecG711U : return (sample_rate && channels && sample_bit) ? std::make_shared<G711Track>(codecId, sample_rate, channels, sample_bit) : nullptr;
default : WarnL << "暂不支持该CodecId:" << codecId; return nullptr;
}
}
2020-04-18 18:46:20 +08:00
Track::Ptr Factory::getVideoTrackByAmf(const AMFValue &amf) {
CodecId codecId = getVideoCodecIdByAmf(amf);
if(codecId == CodecInvalid){
return nullptr;
}
return getTrackByCodecId(codecId);
}
2018-10-30 17:11:36 +08:00
2020-04-18 18:46:20 +08:00
static CodecId getAudioCodecIdByAmf(const AMFValue &val) {
if (val.type() == AMF_STRING) {
auto str = val.as_string();
if (str == "mp4a") {
return CodecAAC;
}
2020-08-01 10:22:12 +08:00
WarnL << "暂不支持该音频Amf:" << str;
return CodecInvalid;
}
if (val.type() != AMF_NULL) {
auto type_id = val.as_integer();
switch (type_id) {
2020-04-18 18:46:20 +08:00
case FLV_CODEC_AAC : return CodecAAC;
case FLV_CODEC_G711A : return CodecG711A;
case FLV_CODEC_G711U : return CodecG711U;
2020-08-01 10:22:12 +08:00
case FLV_CODEC_OPUS : return CodecOpus;
default : WarnL << "暂不支持该音频Amf:" << type_id; return CodecInvalid;
}
}
return CodecInvalid;
}
2020-04-18 22:13:11 +08:00
Track::Ptr Factory::getAudioTrackByAmf(const AMFValue& amf, int sample_rate, int channels, int sample_bit){
2020-04-18 18:46:20 +08:00
CodecId codecId = getAudioCodecIdByAmf(amf);
if (codecId == CodecInvalid) {
return nullptr;
}
2020-04-18 22:13:11 +08:00
return getTrackByCodecId(codecId, sample_rate, channels, sample_bit);
2020-04-18 18:46:20 +08:00
}
RtmpCodec::Ptr Factory::getRtmpCodecByTrack(const Track::Ptr &track, bool is_encode) {
switch (track->getCodecId()){
2020-04-18 22:13:11 +08:00
case CodecH264 : return std::make_shared<H264RtmpEncoder>(track);
case CodecAAC : return std::make_shared<AACRtmpEncoder>(track);
case CodecH265 : return std::make_shared<H265RtmpEncoder>(track);
2020-08-01 10:22:12 +08:00
case CodecOpus : return std::make_shared<CommonRtmpEncoder>(track);
case CodecG711A :
case CodecG711U : {
auto audio_track = dynamic_pointer_cast<AudioTrack>(track);
if (is_encode && (audio_track->getAudioSampleRate() != 8000 ||
audio_track->getAudioChannel() != 1 ||
audio_track->getAudioSampleBit() != 16)) {
//rtmp对g711只支持8000/1/16规格但是ZLMediaKit可以解析其他规格的G711
WarnL << "RTMP只支持8000/1/16规格的G711,目前规格是:"
<< audio_track->getAudioSampleRate() << "/"
<< audio_track->getAudioChannel() << "/"
<< audio_track->getAudioSampleBit()
<< ",该音频已被忽略";
return nullptr;
}
2020-08-01 10:22:12 +08:00
return std::make_shared<CommonRtmpEncoder>(track);
}
2020-04-18 22:13:11 +08:00
default : WarnL << "暂不支持该CodecId:" << track->getCodecName(); return nullptr;
2018-10-24 22:03:17 +08:00
}
}
2018-10-25 14:49:47 +08:00
AMFValue Factory::getAmfByCodecId(CodecId codecId) {
switch (codecId){
case CodecAAC: return AMFValue(FLV_CODEC_AAC);
case CodecH264: return AMFValue(FLV_CODEC_H264);
2020-04-04 22:54:49 +08:00
case CodecH265: return AMFValue(FLV_CODEC_H265);
case CodecG711A: return AMFValue(FLV_CODEC_G711A);
case CodecG711U: return AMFValue(FLV_CODEC_G711U);
2020-08-01 10:22:12 +08:00
case CodecOpus: return AMFValue(FLV_CODEC_OPUS);
2019-12-12 22:25:55 +08:00
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