ZLMediaKit/src/Extension/Factory.cpp

271 lines
9.1 KiB
C++
Raw Normal View History

2018-10-25 10:00:17 +08:00
/*
* MIT License
*
* 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.
*/
2018-10-24 09:23:57 +08:00
#include "Factory.h"
2018-10-24 22:03:17 +08:00
#include "RtmpMuxer/H264RtmpCodec.h"
#include "RtmpMuxer/AACRtmpCodec.h"
#include "RtspMuxer/H264RtpCodec.h"
#include "RtspMuxer/AACRtpCodec.h"
2018-10-30 17:11:36 +08:00
#include "RtspMuxer/H265RtpCodec.h"
2018-10-24 09:23:57 +08:00
2018-10-24 17:17:55 +08:00
namespace mediakit{
2018-10-24 09:23:57 +08:00
Sdp::Ptr Factory::getSdpByTrack(const Track::Ptr &track) {
switch (track->getCodecId()){
case CodecH264:{
H264Track::Ptr h264Track = dynamic_pointer_cast<H264Track>(track);
if(!h264Track){
2018-10-25 09:26:11 +08:00
WarnL << "该Track不是H264Track类型";
return nullptr;
}
if(!h264Track->ready()){
WarnL << "该Track未准备好";
2018-10-24 09:23:57 +08:00
return nullptr;
}
return std::make_shared<H264Sdp>(h264Track->getSps(),h264Track->getPps());
}
2018-10-30 21:05:48 +08:00
case CodecH265:{
H265Track::Ptr h265Track = dynamic_pointer_cast<H265Track>(track);
if(!h265Track){
WarnL << "该Track不是H265Track类型";
return nullptr;
}
if(!h265Track->ready()){
WarnL << "该Track未准备好";
return nullptr;
}
2018-11-02 15:06:27 +08:00
return std::make_shared<H265Sdp>(h265Track->getVps(),h265Track->getSps(),h265Track->getPps());
2018-10-30 21:05:48 +08:00
}
2018-10-24 09:23:57 +08:00
case CodecAAC:{
AACTrack::Ptr aacTrack = dynamic_pointer_cast<AACTrack>(track);
if(!aacTrack){
2018-10-25 09:26:11 +08:00
WarnL << "该Track不是AACTrack类型";
return nullptr;
}
if(!aacTrack->ready()){
WarnL << "该Track未准备好";
2018-10-24 09:23:57 +08:00
return nullptr;
}
return std::make_shared<AACSdp>(aacTrack->getAacCfg(),aacTrack->getAudioSampleRate());
}
default:
2018-10-25 09:26:11 +08:00
WarnL << "暂不支持的CodecId:" << track->getCodecId();
2018-10-24 09:23:57 +08:00
return nullptr;
}
}
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);
2018-10-24 09:23:57 +08:00
if (aac_cfg_str.size() != 4) {
2019-03-27 18:41:52 +08:00
aac_cfg_str = FindField(track->_fmtp.data(), "config=", ";");
2018-10-24 09:23:57 +08:00
}
if (aac_cfg_str.size() != 4) {
2018-10-25 09:26:11 +08:00
//延后获取adts头
return std::make_shared<AACTrack>();
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-03-27 18:41:52 +08:00
string sps_pps = FindField(track->_fmtp.data(), "sprop-parameter-sets=", nullptr);
2018-10-24 09:23:57 +08:00
if(sps_pps.empty()){
return std::make_shared<H264Track>();
}
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
if(base64_PPS.back() == ';'){
base64_PPS.pop_back();
}
auto sps = decodeBase64(base64_SPS);
auto pps = decodeBase64(base64_PPS);
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=
int pt;
2018-11-02 14:55:25 +08:00
char sprop_vps[128] = {0},sprop_sps[128] = {0},sprop_pps[128] = {0};
2019-03-27 18:41:52 +08:00
if (4 == sscanf(track->_fmtp.data(), "%d sprop-vps=%127[^;]; sprop-sps=%127[^;]; sprop-pps=%127[^;]", &pt, sprop_vps,sprop_sps, sprop_pps)) {
2018-11-02 14:55:25 +08:00
auto vps = decodeBase64(sprop_vps);
auto sps = decodeBase64(sprop_sps);
auto pps = decodeBase64(sprop_pps);
return std::make_shared<H265Track>(vps,sps,pps,0,0,0);
}
2019-03-27 18:41:52 +08:00
if (3 == sscanf(track->_fmtp.data(), "%d sprop-sps=%127[^;]; sprop-pps=%127[^;]", &pt,sprop_sps, sprop_pps)) {
2018-11-02 14:55:25 +08:00
auto sps = decodeBase64(sprop_sps);
auto pps = decodeBase64(sprop_pps);
return std::make_shared<H265Track>("",sps,pps,0,0,0);
2018-10-24 18:41:37 +08:00
}
2018-11-02 14:55:25 +08:00
return std::make_shared<H265Track>();
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;
}
}
2018-10-24 09:23:57 +08:00
RtpCodec::Ptr Factory::getRtpEncoderById(CodecId codecId,
2018-10-30 17:11:36 +08:00
uint32_t ui32Ssrc,
uint32_t ui32MtuSize,
uint32_t ui32SampleRate,
uint8_t ui8PlayloadType,
uint8_t ui8Interleaved) {
2018-10-24 09:23:57 +08:00
switch (codecId){
case CodecH264:
return std::make_shared<H264RtpEncoder>(ui32Ssrc,ui32MtuSize,ui32SampleRate,ui8PlayloadType,ui8Interleaved);
2018-10-30 17:11:36 +08:00
case CodecH265:
return std::make_shared<H265RtpEncoder>(ui32Ssrc,ui32MtuSize,ui32SampleRate,ui8PlayloadType,ui8Interleaved);
2018-10-24 09:23:57 +08:00
case CodecAAC:
return std::make_shared<AACRtpEncoder>(ui32Ssrc,ui32MtuSize,ui32SampleRate,ui8PlayloadType,ui8Interleaved);
default:
2018-10-25 09:26:11 +08:00
WarnL << "暂不支持该CodecId:" << codecId;
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:
2019-01-24 12:21:29 +08:00
WarnL << "暂不支持该CodecId:" << track->getCodecId();
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;
}
WarnL << "暂不支持该Amf:" << str;
return CodecInvalid;
}
if (val.type() != AMF_NULL){
auto type_id = val.as_integer();
switch (type_id){
case 7:{
return CodecH264;
}
case 10:{
return CodecAAC;
}
default:
WarnL << "暂不支持该Amf:" << type_id;
return CodecInvalid;
}
}else{
WarnL << "Metedata不存在相应的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:
WarnL << "暂不支持该CodecId:" << track->getCodecId();
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){
case CodecAAC:{
return AMFValue("mp4a");
}
case CodecH264:{
return AMFValue("avc1");
}
default:
return AMFValue(AMF_NULL);
}
}
2018-10-24 18:41:37 +08:00
2018-10-24 17:17:55 +08:00
}//namespace mediakit