Factory.cpp 7.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * 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.
 */
26 27

#include "Factory.h"
xiongziliang committed
28 29 30 31
#include "RtmpMuxer/H264RtmpCodec.h"
#include "RtmpMuxer/AACRtmpCodec.h"
#include "RtspMuxer/H264RtpCodec.h"
#include "RtspMuxer/AACRtpCodec.h"
32

xiongziliang committed
33 34
namespace mediakit{

35 36 37 38 39
Sdp::Ptr Factory::getSdpByTrack(const Track::Ptr &track) {
    switch (track->getCodecId()){
        case CodecH264:{
            H264Track::Ptr h264Track = dynamic_pointer_cast<H264Track>(track);
            if(!h264Track){
40 41 42 43 44
                WarnL << "该Track不是H264Track类型";
                return nullptr;
            }
            if(!h264Track->ready()){
                WarnL << "该Track未准备好";
45 46 47 48 49 50 51 52
                return nullptr;
            }
            return std::make_shared<H264Sdp>(h264Track->getSps(),h264Track->getPps());
        }

        case CodecAAC:{
            AACTrack::Ptr aacTrack = dynamic_pointer_cast<AACTrack>(track);
            if(!aacTrack){
53 54 55 56 57
                WarnL << "该Track不是AACTrack类型";
                return nullptr;
            }
            if(!aacTrack->ready()){
                WarnL << "该Track未准备好";
58 59 60 61 62
                return nullptr;
            }
            return std::make_shared<AACSdp>(aacTrack->getAacCfg(),aacTrack->getAudioSampleRate());
        }
        default:
63
            WarnL << "暂不支持的CodecId:" << track->getCodecId();
64 65 66 67 68
            return nullptr;
    }
}


xiongziliang committed
69 70 71
Track::Ptr Factory::getTrackBySdp(const SdpTrack::Ptr &track) {
    if (strcasestr(track->_codec.data(), "mpeg4-generic") != nullptr) {
        string aac_cfg_str = FindField(track->_fmtp.c_str(), "config=", "\r\n");
72
        if (aac_cfg_str.size() != 4) {
xiongziliang committed
73
            aac_cfg_str = FindField(track->_fmtp.c_str(), "config=", ";");
74 75
        }
        if (aac_cfg_str.size() != 4) {
76 77
            //延后获取adts头
            return std::make_shared<AACTrack>();
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
        }
        string aac_cfg;

        unsigned int cfg1;
        sscanf(aac_cfg_str.substr(0, 2).c_str(), "%02X", &cfg1);
        cfg1 &= 0x00FF;
        aac_cfg.push_back(cfg1);

        unsigned int cfg2;
        sscanf(aac_cfg_str.substr(2, 2).c_str(), "%02X", &cfg2);
        cfg2 &= 0x00FF;
        aac_cfg.push_back(cfg2);

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

xiongziliang committed
94 95
    if (strcasestr(track->_codec.data(), "h264") != nullptr) {
        string sps_pps = FindField(track->_fmtp.c_str(), "sprop-parameter-sets=", "\r\n");
96 97 98 99 100 101 102 103 104 105 106 107 108 109
        if(sps_pps.empty()){
            return std::make_shared<H264Track>();
        }
        string base64_SPS = FindField(sps_pps.c_str(), NULL, ",");
        string base64_PPS = FindField(sps_pps.c_str(), ",", NULL);
        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);
    }

xiongziliang committed
110
    WarnL << "暂不支持该sdp:" << track->_codec << " " << track->_fmtp;
111 112 113 114
    return nullptr;
}


xiongziliang committed
115
CodecId Factory::getCodecIdByAmf(const AMFValue &val){
116 117 118 119 120 121 122 123
    if (val.type() == AMF_STRING){
        auto str = val.as_string();
        if(str == "avc1"){
            return CodecH264;
        }
        if(str == "mp4a"){
            return CodecAAC;
        }
124
        WarnL << "暂不支持该Amf:" << str;
125 126 127 128 129 130 131 132 133 134 135 136 137
        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:
138
                WarnL << "暂不支持该Amf:" << type_id;
139 140 141
                return CodecInvalid;
        }
    }
142
    WarnL << "暂不支持该Amf:" << val.type();
143 144 145 146 147 148 149 150 151 152 153 154
    return CodecInvalid;
}

Track::Ptr Factory::getTrackByCodecId(CodecId codecId) {
    switch (codecId){
        case CodecH264:{
            return std::make_shared<H264Track>();
        }
        case CodecAAC:{
            return std::make_shared<AACTrack>();
        }
        default:
155
            WarnL << "暂不支持该CodecId:" << codecId;
156 157 158 159 160 161 162 163 164 165 166 167 168
            return nullptr;
    }
}


Track::Ptr Factory::getTrackByAmf(const AMFValue &amf) {
    CodecId codecId = getCodecIdByAmf(amf);
    if(codecId == CodecInvalid){
        return nullptr;
    }
    return getTrackByCodecId(codecId);
}

169 170 171 172 173 174 175 176 177 178 179 180 181

RtpCodec::Ptr Factory::getRtpEncoderById(CodecId codecId,
                                          uint32_t ui32Ssrc,
                                          uint32_t ui32MtuSize,
                                          uint32_t ui32SampleRate,
                                          uint8_t ui8PlayloadType,
                                          uint8_t ui8Interleaved) {
    switch (codecId){
        case CodecH264:
            return std::make_shared<H264RtpEncoder>(ui32Ssrc,ui32MtuSize,ui32SampleRate,ui8PlayloadType,ui8Interleaved);
        case CodecAAC:
            return std::make_shared<AACRtpEncoder>(ui32Ssrc,ui32MtuSize,ui32SampleRate,ui8PlayloadType,ui8Interleaved);
        default:
182
            WarnL << "暂不支持该CodecId:" << codecId;
183 184 185 186
            return nullptr;
    }
}

187
RtpCodec::Ptr Factory::getRtpDecoderById(CodecId codecId) {
188 189 190 191
    switch (codecId){
        case CodecH264:
            return std::make_shared<H264RtpDecoder>();
        case CodecAAC:
192
            return std::make_shared<AACRtpDecoder>();
193
        default:
194
            WarnL << "暂不支持该CodecId:" << codecId;
195 196 197 198
            return nullptr;
    }
}

199 200
RtmpCodec::Ptr Factory::getRtmpCodecByTrack(const Track::Ptr &track) {
    switch (track->getCodecId()){
xiongziliang committed
201
        case CodecH264:
202
            return std::make_shared<H264RtmpEncoder>(track);
xiongziliang committed
203
        case CodecAAC:
204
            return std::make_shared<AACRtmpEncoder>(track);
xiongziliang committed
205
        default:
206
            WarnL << "暂不支持该CodecId:" << track->getCodecId();
xiongziliang committed
207 208 209 210
            return nullptr;
    }
}

211 212 213 214 215 216 217 218 219 220 221 222
AMFValue Factory::getAmfByCodecId(CodecId codecId) {
    switch (codecId){
        case CodecAAC:{
            return AMFValue("mp4a");
        }
        case CodecH264:{
            return AMFValue("avc1");
        }
        default:
            return AMFValue(AMF_NULL);
    }
}
223

224

xiongziliang committed
225 226
}//namespace mediakit