RtspSdp.h 6.73 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.
*/
xiongziliang committed
26 27
#ifndef ZLMEDIAKIT_RTSPSDP_H
#define ZLMEDIAKIT_RTSPSDP_H
28

29 30
#include "RtspMuxer/H264RtpCodec.h"
#include "RtspMuxer/AACRtpCodec.h"
31
#include "Util/base64.h"
32
#include "Player/Track.h"
33

xiongziliang committed
34
namespace mediakit {
35

36
/**
37
* rtsp sdp基类
38
*/
xiongziliang committed
39
class Sdp : public CodecInfo{
40 41
public:
    typedef std::shared_ptr<Sdp> Ptr;
42 43 44 45 46 47

    /**
     * 构造sdp
     * @param sample_rate 采样率
     * @param playload_type pt类型
     */
48 49 50 51
    Sdp(uint32_t sample_rate, uint8_t playload_type){
        _sample_rate = sample_rate;
        _playload_type = playload_type;
    }
52

53
    virtual ~Sdp(){}
54

55 56 57 58
    /**
     * 获取sdp字符串
     * @return
     */
59
    virtual string getSdp() const  = 0;
60

61

62 63 64 65
    /**
     * 创建Rtp打包器
     * @param ssrc 打包器ssrc,可以为0
     * @param mtu mtu大小,一般小于1500字节,推荐1400
xiongziliang committed
66
     * @return Rtp打包器
67
     */
xiongziliang committed
68
    virtual RtpCodec::Ptr  createRtpEncoder(uint32_t ssrc, int mtu);
69 70 71
private:
    uint8_t _playload_type;
    uint32_t _sample_rate;
72 73
};

74
/**
75 76
* sdp中除音视频外的其他描述部分
*/
xiongziliang committed
77
class TitleSdp : public Sdp{
78
public:
79 80 81 82 83 84 85

    /**
     * 构造title类型sdp
     * @param dur_sec rtsp点播时长,0代表直播,单位秒
     * @param header 自定义sdp描述
     * @param version sdp版本
     */
xiongziliang committed
86
    TitleSdp(float dur_sec = 0,
87
             const map<string,string> &header = map<string,string>(),
88
             int version = 0) : Sdp(0,0){
89 90 91 92 93 94 95 96
        _printer << "v=" << version << "\r\n";

        if(!header.empty()){
            for (auto &pr : header){
                _printer << pr.first << "=" << pr.second << "\r\n";
            }
        } else {
            _printer << "o=- 1383190487994921 1 IN IP4 0.0.0.0\r\n";
97 98
            _printer << "s=RTSP Session, streamed by the ZLMediaKit\r\n";
            _printer << "i=ZLMediaKit Live Stream\r\n";
99 100 101 102 103 104 105 106 107 108 109
            _printer << "c=IN IP4 0.0.0.0\r\n";
            _printer << "t=0 0\r\n";
        }

        if(dur_sec <= 0){
            _printer << "a=range:npt=0-\r\n";
        }else{
            _printer << "a=range:npt=0-" << dur_sec  << "\r\n";
        }
        _printer << "a=control:*\r\n";
    }
110
    string getSdp() const override {
111 112
        return _printer;
    }
113 114 115 116 117
    /**
     * 返回音频或视频类型
     * @return
     */
    TrackType getTrackType() const override {
118
        return TrackTitle;
119 120 121 122 123 124 125 126 127
    }

    /**
     * 返回编码器id
     * @return
     */
    CodecId getCodecId() const override{
        return CodecInvalid;
    }
128 129 130 131
private:
    _StrPrinter _printer;
};

132
/**
133 134
* h264类型sdp
*/
xiongziliang committed
135
class H264Sdp : public Sdp {
136
public:
137 138 139

    /**
     *
xiongziliang committed
140 141
     * @param sps 264 sps,不带0x00000001头
     * @param pps 264 pps,不带0x00000001头
142
     * @param playload_type  rtp playload type 默认96
143 144
     * @param bitrate 比特率
     */
xiongziliang committed
145 146
    H264Sdp(const string &strSPS,
            const string &strPPS,
147
            int playload_type = 96,
148
            int bitrate = 4000) : Sdp(90000,playload_type) {
149 150 151
        //视频通道
        _printer << "m=video 0 RTP/AVP " << playload_type << "\r\n";
        _printer << "b=AS:" << bitrate << "\r\n";
152
        _printer << "a=rtpmap:" << playload_type << " H264/" << 90000 << "\r\n";
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
        _printer << "a=fmtp:" << playload_type << " packetization-mode=1;profile-level-id=";

        char strTemp[100];
        int profile_level_id = 0;
        if (strSPS.length() >= 4) { // sanity check
            profile_level_id = (strSPS[1] << 16) | (strSPS[2] << 8) | strSPS[3]; // profile_idc|constraint_setN_flag|level_idc
        }
        memset(strTemp, 0, 100);
        sprintf(strTemp, "%06X", profile_level_id);
        _printer << strTemp;
        _printer << ";sprop-parameter-sets=";
        memset(strTemp, 0, 100);
        av_base64_encode(strTemp, 100, (uint8_t *) strSPS.data(), strSPS.size());
        _printer << strTemp << ",";
        memset(strTemp, 0, 100);
        av_base64_encode(strTemp, 100, (uint8_t *) strPPS.data(), strPPS.size());
        _printer << strTemp << "\r\n";
170
        _printer << "a=control:trackID=" << getTrackType() << "\r\n";
171 172
    }

173
    string getSdp() const override {
174 175 176
        return _printer;
    }

177
    TrackType getTrackType() const override {
178
        return TrackVideo;
179
    }
180

181 182 183
    CodecId getCodecId() const override {
        return CodecH264;
    }
184 185 186 187 188
private:
    _StrPrinter _printer;
};


189
/**
190 191
* aac类型SDP
*/
xiongziliang committed
192
class AACSdp : public Sdp {
193
public:
194 195

    /**
196
     *
197 198
     * @param aac_cfg aac两个字节的配置描述
     * @param sample_rate 音频采样率
199
     * @param playload_type rtp playload type 默认98
200 201
     * @param bitrate 比特率
     */
xiongziliang committed
202
    AACSdp(const string &aac_cfg,
203 204
           int sample_rate,
           int playload_type = 98,
205
           int bitrate = 128) : Sdp(sample_rate,playload_type){
206 207 208 209 210 211 212 213 214
        _printer << "m=audio 0 RTP/AVP " << playload_type << "\r\n";
        _printer << "b=AS:" << bitrate << "\r\n";
        _printer << "a=rtpmap:" << playload_type << " MPEG4-GENERIC/" << sample_rate << "\r\n";

        char configStr[32] = {0};
        snprintf(configStr, sizeof(configStr), "%02X%02x", aac_cfg[0], aac_cfg[1]);
        _printer << "a=fmtp:" << playload_type << " streamtype=5;profile-level-id=1;mode=AAC-hbr;"
                 << "sizelength=13;indexlength=3;indexdeltalength=3;config="
                 << configStr << "\r\n";
215
        _printer << "a=control:trackID=" << getTrackType() << "\r\n";
216 217
    }

218
    string getSdp() const override {
219 220 221
        return _printer;
    }

222
    TrackType getTrackType() const override {
223
        return TrackAudio;
xiongziliang committed
224
    }
225 226 227
    CodecId getCodecId() const override {
        return CodecAAC;
    }
228 229 230 231 232
private:
    _StrPrinter _printer;
};


xiongziliang committed
233
} /* namespace mediakit */
234 235 236



xiongziliang committed
237
#endif //ZLMEDIAKIT_RTSPSDP_H