RtspPusher.h 3.68 KB
Newer Older
xiongziliang committed
1 2 3
/*
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
 *
4
 * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
xiongziliang committed
5 6 7 8 9
 *
 * 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.
 */
xiongziliang committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23

#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"
xiongziliang committed
24
#include "Rtcp/RtcpContext.h"
xiongziliang committed
25 26 27 28 29 30 31 32 33

using namespace std;
using namespace toolkit;

namespace mediakit {

class RtspPusher : public TcpClient, public RtspSplitter, public PusherBase {
public:
    typedef std::shared_ptr<RtspPusher> Ptr;
34
    RtspPusher(const EventPoller::Ptr &poller,const RtspMediaSource::Ptr &src);
xiongziliang committed
35 36
    ~RtspPusher() override;
    void publish(const string &url) override;
xiongziliang committed
37 38 39
    void teardown() override;

    void setOnPublished(const Event &cb) override {
xiongziliang committed
40
        _on_published = cb;
xiongziliang committed
41 42 43
    }

    void setOnShutdown(const Event & cb) override{
xiongziliang committed
44
        _on_shutdown = cb;
xiongziliang committed
45
    }
xiongziliang committed
46

xiongziliang committed
47 48
protected:
    //for Tcpclient override
xiongziliang committed
49
    void onRecv(const Buffer::Ptr &buf) override;
xiongziliang committed
50 51 52 53 54
    void onConnect(const SockException &err) override;
    void onErr(const SockException &ex) override;

    //RtspSplitter override
    void onWholeRtspPacket(Parser &parser) override ;
xiongziliang committed
55 56 57
    void onRtpPacket(const char *data,size_t len) override;

    virtual void onRtcpPacket(int track_idx, SdpTrack::Ptr &track, uint8_t *data, size_t len);
xiongziliang committed
58

xiongziliang committed
59
private:
xiongziliang committed
60
    void onPublishResult(const SockException &ex, bool handshake_done);
xiongziliang committed
61

xiongziliang committed
62
    void sendAnnounce();
xiongziliang committed
63
    void sendSetup(unsigned int track_idx);
xiongziliang committed
64 65
    void sendRecord();
    void sendOptions();
xiongziliang committed
66
    void sendTeardown();
xiongziliang committed
67 68

    void handleResAnnounce(const Parser &parser);
xiongziliang committed
69 70
    void handleResSetup(const Parser &parser, unsigned int track_idx);
    bool handleAuthenticationFailure(const string &params_str);
xiongziliang committed
71

xiongziliang committed
72 73
    int getTrackIndexByInterleaved(int interleaved) const;
    int getTrackIndexByTrackType(TrackType type) const;
xiongziliang committed
74

75
    void sendRtpPacket(const RtspMediaSource::RingDataType & pkt) ;
xiongziliang committed
76 77
    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 = "");
78 79

    void createUdpSockIfNecessary(int track_idx);
80
    void setSocketFlags();
xiongziliang committed
81
    void updateRtcpContext(const RtpPacket::Ptr &pkt);
xiongziliang committed
82

xiongziliang committed
83
private:
xiongziliang committed
84 85
    unsigned int _cseq = 1;
    Rtsp::eRtpType _rtp_type = Rtsp::RTP_TCP;
xiongziliang committed
86

xiongziliang committed
87 88 89 90 91 92 93 94
    //rtsp鉴权相关
    string _nonce;
    string _realm;
    string _url;
    string _session_id;
    string _content_base;
    SdpParser _sdp_parser;
    vector<SdpTrack::Ptr> _track_vec;
xiongziliang committed
95 96 97 98
    //RTP端口,trackid idx 为数组下标
    Socket::Ptr _rtp_sock[2];
    //RTCP端口,trackid idx 为数组下标
    Socket::Ptr _rtcp_sock[2];
xiongziliang committed
99
    //超时功能实现
xiongziliang committed
100
    std::shared_ptr<Timer> _publish_timer;
xiongziliang committed
101
    //心跳定时器
xiongziliang committed
102 103 104 105 106 107 108
    std::shared_ptr<Timer> _beat_timer;
    std::weak_ptr<RtspMediaSource> _push_src;
    RtspMediaSource::RingType::RingReader::Ptr _rtsp_reader;
    //事件监听
    Event _on_shutdown;
    Event _on_published;
    function<void(const Parser&)> _on_res_func;
xiongziliang committed
109 110 111 112 113
    ////////// rtcp ////////////////
    //rtcp发送时间,trackid idx 为数组下标
    Ticker _rtcp_send_ticker[2];
    //统计rtp并发送rtcp
    vector<RtcpContext::Ptr> _rtcp_context;
xiongziliang committed
114 115 116 117
};

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