Factory.cpp 9.16 KB
Newer Older
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
3 4 5
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
xiongziliang committed
6 7 8
 * 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.
9
 */
10 11

#include "Factory.h"
xiongziliang committed
12
#include "Rtmp/Rtmp.h"
xiongziliang committed
13
#include "H264Rtmp.h"
xiongziliang committed
14
#include "H265Rtmp.h"
xiongziliang committed
15
#include "AACRtmp.h"
16
#include "G711Rtmp.h"
xiongziliang committed
17 18
#include "H264Rtp.h"
#include "AACRtp.h"
19
#include "G711Rtp.h"
xiongziliang committed
20
#include "H265Rtp.h"
21
#include "Common/Parser.h"
22

xiongziliang committed
23 24
namespace mediakit{

xiongziliang committed
25
Track::Ptr Factory::getTrackBySdp(const SdpTrack::Ptr &track) {
xiongziliang committed
26
    if (strcasecmp(track->_codec.data(), "mpeg4-generic") == 0) {
xiongziliang committed
27
        string aac_cfg_str = FindField(track->_fmtp.data(), "config=", nullptr);
28
        if (aac_cfg_str.empty()) {
xiongziliang committed
29
            aac_cfg_str = FindField(track->_fmtp.data(), "config=", ";");
30
        }
31
        if (aac_cfg_str.empty()) {
32 33
            //如果sdp中获取不到aac config信息,那么在rtp也无法获取,那么忽略该Track
            return nullptr;
34 35 36 37
        }
        string aac_cfg;

        unsigned int cfg1;
xiongziliang committed
38
        sscanf(aac_cfg_str.substr(0, 2).data(), "%02X", &cfg1);
39 40 41 42
        cfg1 &= 0x00FF;
        aac_cfg.push_back(cfg1);

        unsigned int cfg2;
xiongziliang committed
43
        sscanf(aac_cfg_str.substr(2, 2).data(), "%02X", &cfg2);
44 45 46 47 48 49
        cfg2 &= 0x00FF;
        aac_cfg.push_back(cfg2);

        return std::make_shared<AACTrack>(aac_cfg);
    }

50
    if (strcasecmp(track->_codec.data(), "PCMA") == 0) {
xiongziliang committed
51
        return std::make_shared<G711Track>(CodecG711A, track->_samplerate, track->_channel, 16);
52 53 54
    }

    if (strcasecmp(track->_codec.data(), "PCMU") == 0) {
xiongziliang committed
55
        return std::make_shared<G711Track>(CodecG711U,  track->_samplerate, track->_channel, 16);
56 57
    }

xiongziliang committed
58
    if (strcasecmp(track->_codec.data(), "h264") == 0) {
59
        //a=fmtp:96 packetization-mode=1;profile-level-id=42C01F;sprop-parameter-sets=Z0LAH9oBQBboQAAAAwBAAAAPI8YMqA==,aM48gA==
60
        auto map = Parser::parseArgs(FindField(track->_fmtp.data()," ", nullptr),";","=");
61
        auto sps_pps = map["sprop-parameter-sets"];
xiongziliang committed
62 63
        string base64_SPS = FindField(sps_pps.data(), NULL, ",");
        string base64_PPS = FindField(sps_pps.data(), ",", NULL);
64 65
        auto sps = decodeBase64(base64_SPS);
        auto pps = decodeBase64(base64_PPS);
66
        if(sps.empty() || pps.empty()){
67
            //如果sdp里面没有sps/pps,那么可能在后续的rtp里面恢复出sps/pps
68 69 70
            return std::make_shared<H264Track>();
        }

71 72 73
        return std::make_shared<H264Track>(sps,pps,0,0);
    }

xiongziliang committed
74
    if (strcasecmp(track->_codec.data(), "h265") == 0) {
75
        //a=fmtp:96 sprop-sps=QgEBAWAAAAMAsAAAAwAAAwBdoAKAgC0WNrkky/AIAAADAAgAAAMBlQg=; sprop-pps=RAHA8vA8kAA=
76
        auto map = Parser::parseArgs(FindField(track->_fmtp.data()," ", nullptr),";","=");
77 78 79
        auto vps = decodeBase64(map["sprop-vps"]);
        auto sps = decodeBase64(map["sprop-sps"]);
        auto pps = decodeBase64(map["sprop-pps"]);
80 81 82 83
        if(sps.empty() || pps.empty()){
            //如果sdp里面没有sps/pps,那么可能在后续的rtp里面恢复出sps/pps
            return std::make_shared<H265Track>();
        }
84
        return std::make_shared<H265Track>(vps,sps,pps,0,0,0);
85 86
    }

87 88 89 90 91 92 93 94
    //可以根据传统的payload type 获取编码类型以及采样率等信息
    CodecId codec_id = RtpPayload::getCodecId(track->_pt);
    switch (codec_id){
        case CodecG711A :
        case CodecG711U : return std::make_shared<G711Track>(codec_id,  track->_samplerate, track->_channel, 16);
        default : break;
    }

xiongziliang committed
95
    WarnL << "暂不支持该sdp:" << track->getName();
96
    return nullptr;
97 98
}

xiongziliang committed
99 100 101
RtpCodec::Ptr Factory::getRtpEncoderBySdp(const Sdp::Ptr &sdp) {
    GET_CONFIG(uint32_t,audio_mtu,Rtp::kAudioMtuSize);
    GET_CONFIG(uint32_t,video_mtu,Rtp::kVideoMtuSize);
xiongziliang committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115
    // 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;
    }
xiongziliang committed
116 117
    auto mtu = (sdp->getTrackType() == TrackVideo ? video_mtu : audio_mtu);
    auto sample_rate = sdp->getSampleRate();
xiongziliang committed
118
    auto pt = sdp->getPayloadType();
xiongziliang committed
119 120 121
    auto interleaved = sdp->getTrackType() * 2;
    auto codec_id = sdp->getCodecId();
    switch (codec_id){
xiongziliang committed
122 123 124 125 126 127
        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 CodecG711A :
        case CodecG711U : return std::make_shared<G711RtpEncoder>(ssrc, mtu, sample_rate, pt, interleaved);
        default : WarnL << "暂不支持该CodecId:" << codec_id; return nullptr;
128 129 130
    }
}

131 132
RtpCodec::Ptr Factory::getRtpDecoderByTrack(const Track::Ptr &track) {
    switch (track->getCodecId()){
xiongziliang committed
133 134 135 136 137 138
        case CodecH264 : return std::make_shared<H264RtpDecoder>();
        case CodecH265 : return std::make_shared<H265RtpDecoder>();
        case CodecAAC : return std::make_shared<AACRtpDecoder>(track->clone());
        case CodecG711A :
        case CodecG711U : return std::make_shared<G711RtpDecoder>(track->clone());
        default : WarnL << "暂不支持该CodecId:" << track->getCodecName(); return nullptr;
139 140 141
    }
}

142 143
/////////////////////////////rtmp相关///////////////////////////////////////////

xiongziliang committed
144
static CodecId getVideoCodecIdByAmf(const AMFValue &val){
145 146 147 148 149 150 151 152
    if (val.type() == AMF_STRING){
        auto str = val.as_string();
        if(str == "avc1"){
            return CodecH264;
        }
        if(str == "mp4a"){
            return CodecAAC;
        }
xiongziliang committed
153 154 155
        if(str == "hev1" || str == "hvc1"){
            return CodecH265;
        }
156 157 158 159 160 161 162
        WarnL << "暂不支持该Amf:" << str;
        return CodecInvalid;
    }

    if (val.type() != AMF_NULL){
        auto type_id = val.as_integer();
        switch (type_id){
xiongziliang committed
163 164 165
            case FLV_CODEC_H264: return CodecH264;
            case FLV_CODEC_AAC: return CodecAAC;
            case FLV_CODEC_H265: return CodecH265;
xiongziliang committed
166
            default : WarnL << "暂不支持该Amf:" << type_id; return CodecInvalid;
167 168
        }
    }
169

170 171 172
    return CodecInvalid;
}

xiongziliang committed
173 174 175 176 177 178 179 180 181 182 183 184

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>();
        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;
    }
}

xiongziliang committed
185 186 187 188 189 190 191
Track::Ptr Factory::getVideoTrackByAmf(const AMFValue &amf) {
    CodecId codecId = getVideoCodecIdByAmf(amf);
    if(codecId == CodecInvalid){
        return nullptr;
    }
    return getTrackByCodecId(codecId);
}
192

xiongziliang committed
193
static CodecId getAudioCodecIdByAmf(const AMFValue &val) {
194 195 196 197 198 199 200 201 202 203 204 205
    if (val.type() == AMF_STRING) {
        auto str = val.as_string();
        if (str == "mp4a") {
            return CodecAAC;
        }
        WarnL << "暂不支持该Amf:" << str;
        return CodecInvalid;
    }

    if (val.type() != AMF_NULL) {
        auto type_id = val.as_integer();
        switch (type_id) {
xiongziliang committed
206 207 208 209
            case FLV_CODEC_AAC : return CodecAAC;
            case FLV_CODEC_G711A : return CodecG711A;
            case FLV_CODEC_G711U : return CodecG711U;
            default : WarnL << "暂不支持该Amf:" << type_id; return CodecInvalid;
210 211 212 213 214 215
        }
    }

    return CodecInvalid;
}

xiongziliang committed
216
Track::Ptr Factory::getAudioTrackByAmf(const AMFValue& amf, int sample_rate, int channels, int sample_bit){
xiongziliang committed
217 218 219 220
    CodecId codecId = getAudioCodecIdByAmf(amf);
    if (codecId == CodecInvalid) {
        return nullptr;
    }
xiongziliang committed
221
    return getTrackByCodecId(codecId, sample_rate, channels, sample_bit);
xiongziliang committed
222 223
}

224 225
RtmpCodec::Ptr Factory::getRtmpCodecByTrack(const Track::Ptr &track) {
    switch (track->getCodecId()){
xiongziliang committed
226 227 228
        case CodecH264 : return std::make_shared<H264RtmpEncoder>(track);
        case CodecAAC : return std::make_shared<AACRtmpEncoder>(track);
        case CodecH265 : return std::make_shared<H265RtmpEncoder>(track);
229
        case CodecG711A :
xiongziliang committed
230 231
        case CodecG711U : return std::make_shared<G711RtmpEncoder>(track);
        default : WarnL << "暂不支持该CodecId:" << track->getCodecName(); return nullptr;
xiongziliang committed
232 233 234
    }
}

235 236
AMFValue Factory::getAmfByCodecId(CodecId codecId) {
    switch (codecId){
xiongziliang committed
237
        //此处用string标明rtmp编码类型目的是为了兼容某些android系统
xiongziliang committed
238 239
        case CodecAAC: return AMFValue("mp4a");
        case CodecH264: return AMFValue("avc1");
xiongziliang committed
240
        case CodecH265: return AMFValue(FLV_CODEC_H265);
241 242
        case CodecG711A: return AMFValue(FLV_CODEC_G711A);
        case CodecG711U: return AMFValue(FLV_CODEC_G711U);
xiongziliang committed
243
        default: return AMFValue(AMF_NULL);
244 245
    }
}
246

xiongziliang committed
247 248
}//namespace mediakit