Factory.cpp 10.4 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 "L16.h"
24
#include "Common/Parser.h"
25

xiongziliang committed
26 27
namespace mediakit{

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

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

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

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

60 61 62 63
    if (strcasecmp(track->_codec.data(), "L16") == 0) {
        return std::make_shared<L16Track>(track->_samplerate, track->_channel);
    }

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

77 78 79
        return std::make_shared<H264Track>(sps,pps,0,0);
    }

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

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

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

139 140
RtpCodec::Ptr Factory::getRtpDecoderByTrack(const Track::Ptr &track) {
    switch (track->getCodecId()){
xiongziliang committed
141 142 143
        case CodecH264 : return std::make_shared<H264RtpDecoder>();
        case CodecH265 : return std::make_shared<H265RtpDecoder>();
        case CodecAAC : return std::make_shared<AACRtpDecoder>(track->clone());
144
        case CodecL16 :
145
        case CodecOpus :
xiongziliang committed
146
        case CodecG711A :
147
        case CodecG711U : return std::make_shared<CommonRtpDecoder>(track->getCodecId());
xiongziliang committed
148
        default : WarnL << "暂不支持该CodecId:" << track->getCodecName(); return nullptr;
149 150 151
    }
}

152 153
/////////////////////////////rtmp相关///////////////////////////////////////////

xiongziliang committed
154
static CodecId getVideoCodecIdByAmf(const AMFValue &val){
155
    if (val.type() == AMF_STRING) {
156
        auto str = val.as_string();
157
        if (str == "avc1") {
158 159
            return CodecH264;
        }
160
        if (str == "hev1" || str == "hvc1") {
xiongziliang committed
161 162
            return CodecH265;
        }
163
        WarnL << "暂不支持该视频Amf:" << str;
164 165 166
        return CodecInvalid;
    }

167
    if (val.type() != AMF_NULL) {
168
        auto type_id = val.as_integer();
169 170 171 172
        switch (type_id) {
            case FLV_CODEC_H264 : return CodecH264;
            case FLV_CODEC_H265 : return CodecH265;
            default : WarnL << "暂不支持该视频Amf:" << type_id; return CodecInvalid;
173 174 175 176 177
        }
    }
    return CodecInvalid;
}

xiongziliang committed
178 179 180 181 182
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>();
183
        case CodecOpus: return std::make_shared<OpusTrack>();
xiongziliang committed
184 185 186 187 188 189
        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
190 191 192 193 194 195 196
Track::Ptr Factory::getVideoTrackByAmf(const AMFValue &amf) {
    CodecId codecId = getVideoCodecIdByAmf(amf);
    if(codecId == CodecInvalid){
        return nullptr;
    }
    return getTrackByCodecId(codecId);
}
197

xiongziliang committed
198
static CodecId getAudioCodecIdByAmf(const AMFValue &val) {
199 200 201 202 203
    if (val.type() == AMF_STRING) {
        auto str = val.as_string();
        if (str == "mp4a") {
            return CodecAAC;
        }
204
        WarnL << "暂不支持该音频Amf:" << str;
205 206 207 208 209 210
        return CodecInvalid;
    }

    if (val.type() != AMF_NULL) {
        auto type_id = val.as_integer();
        switch (type_id) {
xiongziliang committed
211 212 213
            case FLV_CODEC_AAC : return CodecAAC;
            case FLV_CODEC_G711A : return CodecG711A;
            case FLV_CODEC_G711U : return CodecG711U;
214 215
            case FLV_CODEC_OPUS : return CodecOpus;
            default : WarnL << "暂不支持该音频Amf:" << type_id; return CodecInvalid;
216 217 218 219 220 221
        }
    }

    return CodecInvalid;
}

xiongziliang committed
222
Track::Ptr Factory::getAudioTrackByAmf(const AMFValue& amf, int sample_rate, int channels, int sample_bit){
xiongziliang committed
223 224 225 226
    CodecId codecId = getAudioCodecIdByAmf(amf);
    if (codecId == CodecInvalid) {
        return nullptr;
    }
xiongziliang committed
227
    return getTrackByCodecId(codecId, sample_rate, channels, sample_bit);
xiongziliang committed
228 229
}

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

256 257
AMFValue Factory::getAmfByCodecId(CodecId codecId) {
    switch (codecId){
258 259
        case CodecAAC: return AMFValue(FLV_CODEC_AAC);
        case CodecH264: return AMFValue(FLV_CODEC_H264);
xiongziliang committed
260
        case CodecH265: return AMFValue(FLV_CODEC_H265);
261 262
        case CodecG711A: return AMFValue(FLV_CODEC_G711A);
        case CodecG711U: return AMFValue(FLV_CODEC_G711U);
263
        case CodecOpus: return AMFValue(FLV_CODEC_OPUS);
xiongziliang committed
264
        default: return AMFValue(AMF_NULL);
265 266
    }
}
267

xiongziliang committed
268 269
}//namespace mediakit