RtspPusher.h 2.85 KB
Newer Older
xiongziliang committed
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 26
//
// Created by xzl on 2019/3/27.
//

#ifndef ZLMEDIAKIT_RTSPPUSHER_H
#define ZLMEDIAKIT_RTSPPUSHER_H

#include <string>
#include <memory>
#include "RtspMediaSource.h"
#include "Util/util.h"
#include "Util/logger.h"
#include "Poller/Timer.h"
#include "Network/Socket.h"
#include "Network/TcpClient.h"
#include "RtspSplitter.h"
#include "Pusher/PusherBase.h"

using namespace std;
using namespace toolkit;

namespace mediakit {

class RtspPusher : public TcpClient, public RtspSplitter, public PusherBase {
public:
    typedef std::shared_ptr<RtspPusher> Ptr;
27
    RtspPusher(const EventPoller::Ptr &poller,const RtspMediaSource::Ptr &src);
xiongziliang committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
    virtual ~RtspPusher();

    void publish(const string &strUrl) override;

    void teardown() override;

    void setOnPublished(const Event &cb) override {
        _onPublished = cb;
    }

    void setOnShutdown(const Event & cb) override{
        _onShutdown = cb;
    }
protected:
    //for Tcpclient override
    void onRecv(const Buffer::Ptr &pBuf) override;
    void onConnect(const SockException &err) override;
    void onErr(const SockException &ex) override;

    //RtspSplitter override
    void onWholeRtspPacket(Parser &parser) override ;
    void onRtpPacket(const char *data,uint64_t len) override {};
private:
    void publish(const string &strUrl, const string &strUser, const string &strPwd,  Rtsp::eRtpType eType );
xiongziliang committed
52
    void onPublishResult(const SockException &ex);
xiongziliang committed
53

xiongziliang committed
54 55 56 57
    void sendAnnounce();
    void sendSetup(unsigned int uiTrackIndex);
    void sendRecord();
    void sendOptions();
xiongziliang committed
58 59 60 61 62 63 64 65

    void handleResAnnounce(const Parser &parser);
    void handleResSetup(const Parser &parser, unsigned int uiTrackIndex);
    bool handleAuthenticationFailure(const string &paramsStr);

    inline int getTrackIndexByTrackType(TrackType type);

    void sendRtpPacket(const RtpPacket::Ptr & pkt) ;
xiongziliang committed
66 67
    void sendRtspRequest(const string &cmd, const string &url ,const StrCaseMap &header = StrCaseMap(),const string &sdp = "" );
    void sendRtspRequest(const string &cmd, const string &url ,const std::initializer_list<string> &header,const string &sdp = "");
68 69

    void createUdpSockIfNecessary(int track_idx);
xiongziliang committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
private:
    //rtsp鉴权相关
    string _rtspMd5Nonce;
    string _rtspRealm;

    //超时功能实现
    std::shared_ptr<Timer> _pPublishTimer;
    //源
    std::weak_ptr<RtspMediaSource> _pMediaSrc;
    RtspMediaSource::RingType::RingReader::Ptr _pRtspReader;
    //事件监听
    Event _onShutdown;
    Event _onPublished;

    string _strUrl;
xiongziliang committed
85
    SdpParser _sdpParser;
xiongziliang committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    vector<SdpTrack::Ptr> _aTrackInfo;
    string _strSession;
    unsigned int _uiCseq = 1;
    string _strContentBase;
    Rtsp::eRtpType _eType = Rtsp::RTP_TCP;
    Socket::Ptr _apUdpSock[2];
    function<void(const Parser&)> _onHandshake;
    //心跳定时器
    std::shared_ptr<Timer> _pBeatTimer;


};

} /* namespace mediakit */
#endif //ZLMEDIAKIT_RTSPPUSHER_H