RtspSdp.h 3.66 KB
Newer Older
1 2 3
/*
* MIT License
*
xiongziliang committed
4
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
*
* 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
#include "Util/base64.h"
30
#include "Extension/Track.h"
31
#include "RtspMuxer/RtpCodec.h"
32

xiongziliang committed
33
namespace mediakit {
34

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

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

52
    virtual ~Sdp(){}
53

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

60

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

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

    /**
     * 构造title类型sdp
     * @param dur_sec rtsp点播时长,0代表直播,单位秒
     * @param header 自定义sdp描述
     * @param version sdp版本
     */
xiongziliang committed
85
    TitleSdp(float dur_sec = 0,
86
             const map<string,string> &header = map<string,string>(),
87
             int version = 0) : Sdp(0,0){
88 89 90 91 92 93 94 95
        _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";
96 97
            _printer << "s=RTSP Session, streamed by the ZLMediaKit\r\n";
            _printer << "i=ZLMediaKit Live Stream\r\n";
98 99 100 101 102 103 104 105 106 107 108
            _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";
    }
109
    string getSdp() const override {
110 111
        return _printer;
    }
112 113 114 115 116
    /**
     * 返回音频或视频类型
     * @return
     */
    TrackType getTrackType() const override {
117
        return TrackTitle;
118 119 120 121 122 123 124 125 126
    }

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


xiongziliang committed
132
} /* namespace mediakit */
133 134 135



xiongziliang committed
136
#endif //ZLMEDIAKIT_RTSPSDP_H