Factory.cpp 10.2 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 "CommonRtmp.h"
xiongziliang committed
17 18 19
#include "H264Rtp.h"
#include "AACRtp.h"
#include "H265Rtp.h"
20 21 22
#include "CommonRtp.h"
#include "Opus.h"
#include "G711.h"
23
#include "Common/Parser.h"
24

xiongziliang committed
25 26
namespace mediakit{

xiongziliang committed
27
Track::Ptr Factory::getTrackBySdp(const SdpTrack::Ptr &track) {
xiongziliang committed
28
    if (strcasecmp(track->_codec.data(), "mpeg4-generic") == 0) {
xiongziliang committed
29
        string aac_cfg_str = FindField(track->_fmtp.data(), "config=", nullptr);
30
        if (aac_cfg_str.empty()) {
xiongziliang committed
31
            aac_cfg_str = FindField(track->_fmtp.data(), "config=", ";");
32
        }
33
        if (aac_cfg_str.empty()) {
34 35
            //如果sdp中获取不到aac config信息,那么在rtp也无法获取,那么忽略该Track
            return nullptr;
36 37
        }
        string aac_cfg;
38 39 40 41 42 43
        for(int 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);
        }
44 45 46
        return std::make_shared<AACTrack>(aac_cfg);
    }

47 48 49 50
    if (strcasecmp(track->_codec.data(), "opus") == 0) {
        return std::make_shared<OpusTrack>();
    }

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

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

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

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

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

88 89 90 91 92 93 94 95
    //可以根据传统的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
96
    WarnL << "暂不支持该sdp:" << track->getName();
97
    return nullptr;
98 99
}

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

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

145 146
/////////////////////////////rtmp相关///////////////////////////////////////////

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

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

xiongziliang committed
171 172 173 174 175
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>();
176
        case CodecOpus: return std::make_shared<OpusTrack>();
xiongziliang committed
177 178 179 180 181 182
        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
183 184 185 186 187 188 189
Track::Ptr Factory::getVideoTrackByAmf(const AMFValue &amf) {
    CodecId codecId = getVideoCodecIdByAmf(amf);
    if(codecId == CodecInvalid){
        return nullptr;
    }
    return getTrackByCodecId(codecId);
}
190

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

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

    return CodecInvalid;
}

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

223
RtmpCodec::Ptr Factory::getRtmpCodecByTrack(const Track::Ptr &track, bool is_encode) {
224
    switch (track->getCodecId()){
xiongziliang committed
225 226 227
        case CodecH264 : return std::make_shared<H264RtmpEncoder>(track);
        case CodecAAC : return std::make_shared<AACRtmpEncoder>(track);
        case CodecH265 : return std::make_shared<H265RtmpEncoder>(track);
228
        case CodecOpus : return std::make_shared<CommonRtmpEncoder>(track);
229
        case CodecG711A :
230 231 232 233 234 235 236 237 238 239 240 241 242
        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;
            }
243
            return std::make_shared<CommonRtmpEncoder>(track);
244
        }
xiongziliang committed
245
        default : WarnL << "暂不支持该CodecId:" << track->getCodecName(); return nullptr;
xiongziliang committed
246 247 248
    }
}

249 250
AMFValue Factory::getAmfByCodecId(CodecId codecId) {
    switch (codecId){
251 252
        case CodecAAC: return AMFValue(FLV_CODEC_AAC);
        case CodecH264: return AMFValue(FLV_CODEC_H264);
xiongziliang committed
253
        case CodecH265: return AMFValue(FLV_CODEC_H265);
254 255
        case CodecG711A: return AMFValue(FLV_CODEC_G711A);
        case CodecG711U: return AMFValue(FLV_CODEC_G711U);
256
        case CodecOpus: return AMFValue(FLV_CODEC_OPUS);
xiongziliang committed
257
        default: return AMFValue(AMF_NULL);
258 259
    }
}
260

xiongziliang committed
261 262
}//namespace mediakit