RtspPlayer.h 4.84 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
xiongziliang committed
3 4 5
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
xiongziliang committed
6 7 8
 * 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.
xzl committed
9 10 11 12
 */

#ifndef SRC_RTSPPLAYER_RTSPPLAYER_H_TXT_
#define SRC_RTSPPLAYER_RTSPPLAYER_H_TXT_
xiongzilaing committed
13

xzl committed
14
#include <string>
xiongzilaing committed
15
#include <memory>
xzl committed
16
#include "RtspSession.h"
xiongzilaing committed
17 18
#include "RtspMediaSource.h"
#include "Player/PlayerBase.h"
xzl committed
19
#include "Util/util.h"
xiongzilaing committed
20
#include "Util/logger.h"
xzl committed
21
#include "Util/TimeTicker.h"
xiongzilaing committed
22 23 24
#include "Poller/Timer.h"
#include "Network/Socket.h"
#include "Network/TcpClient.h"
25
#include "RtspSplitter.h"
xiongziliang committed
26
#include "RtpReceiver.h"
xiongziliang committed
27
#include "Common/Stamp.h"
xzl committed
28 29

using namespace std;
xiongziliang committed
30
using namespace toolkit;
xiongzilaing committed
31

xiongziliang committed
32
namespace mediakit {
xzl committed
33

3503207480@qq.com committed
34
//实现了rtsp播放器协议部分的功能,及数据接收功能
xiongziliang committed
35
class RtspPlayer: public PlayerBase,public TcpClient, public RtspSplitter, public RtpReceiver {
xzl committed
36
public:
37 38
    typedef std::shared_ptr<RtspPlayer> Ptr;

39 40 41
    RtspPlayer(const EventPoller::Ptr &poller);
    ~RtspPlayer() override;

42
    void play(const string &strUrl) override;
43
    void pause(bool pause_flag) override;
44 45
    void teardown() override;
    float getPacketLossRate(TrackType type) const override;
46

xzl committed
47
protected:
48
    //派生类回调函数
49 50
    virtual bool onCheckSDP(const string &sdp) = 0;
    virtual void onRecvRTP(const RtpPacket::Ptr &rtp, const SdpTrack::Ptr &track) = 0;
51 52
    uint32_t getProgressMilliSecond() const;
    void seekToMilliSecond(uint32_t ms);
53 54 55 56 57 58 59 60 61 62 63 64 65

    /**
     * 收到完整的rtsp包回调,包括sdp等content数据
     * @param parser rtsp包
     */
    void onWholeRtspPacket(Parser &parser) override ;

    /**
     * 收到rtp包回调
     * @param data
     * @param len
     */
    void onRtpPacket(const char *data,uint64_t len) override ;
xiongziliang committed
66 67 68

    /**
     * rtp数据包排序后输出
69 70
     * @param rtp rtp数据包
     * @param track_idx track索引
xiongziliang committed
71
     */
72
    void onRtpSorted(const RtpPacket::Ptr &rtp, int track_idx) override;
73 74 75

    /**
     * 收到RTCP包回调
76 77 78 79
     * @param track_idx track索引
     * @param track sdp相关信息
     * @param data rtcp内容
     * @param len rtcp内容长度
80
     */
81
    virtual void onRtcpPacket(int track_idx, SdpTrack::Ptr &track, unsigned char *data, unsigned int len);
82

83 84
    /////////////TcpClient override/////////////
    void onConnect(const SockException &err) override;
85
    void onRecv(const Buffer::Ptr &buf) override;
86
    void onErr(const SockException &ex) override;
87

xzl committed
88
private:
89
    void onRecvRTP_l(const RtpPacket::Ptr &rtp, const SdpTrack::Ptr &track);
xiongziliang committed
90
    void onPlayResult_l(const SockException &ex , bool handshake_done);
91

xiongziliang committed
92
    int getTrackIndexByInterleaved(int interleaved) const;
93
    int getTrackIndexByTrackType(TrackType track_type) const;
xzl committed
94

95
    void handleResSETUP(const Parser &parser, unsigned int track_idx);
96 97 98
    void handleResDESCRIBE(const Parser &parser);
    bool handleAuthenticationFailure(const string &wwwAuthenticateParamsStr);
    void handleResPAUSE(const Parser &parser, int type);
99
    bool handleResponse(const string &cmd, const Parser &parser);
100

101
    void sendOptions();
102
    void sendSetup(unsigned int track_idx);
103 104
    void sendPause(int type , uint32_t ms);
    void sendDescribe();
105
    void sendKeepAlive();
106
    void sendRtspRequest(const string &cmd, const string &url ,const StrCaseMap &header = StrCaseMap());
107
    void sendRtspRequest(const string &cmd, const string &url ,const std::initializer_list<string> &header);
108
    void sendReceiverReport(bool over_tcp, int track_idx);
109
    void createUdpSockIfNecessary(int track_idx);
110

111
private:
112 113 114 115 116 117 118
    string _play_url;
    vector<SdpTrack::Ptr> _sdp_track;
    function<void(const Parser&)> _on_response;
    //RTP端口,trackid idx 为数组下标
    Socket::Ptr _rtp_sock[2];
    //RTCP端口,trackid idx 为数组下标
    Socket::Ptr _rtcp_sock[2];
119 120

    //rtsp鉴权相关
121 122
    string _md5_nonce;
    string _realm;
123
    //rtsp info
124 125 126 127
    string _session_id;
    uint32_t _cseq_send = 1;
    string _content_base;
    Rtsp::eRtpType _rtp_type = Rtsp::RTP_TCP;
128 129

    /* 丢包率统计需要用到的参数 */
130 131 132 133 134
    uint16_t _rtp_seq_start[2] = {0, 0};
    uint16_t _rtp_seq_now[2] = {0, 0};
    uint64_t _rtp_recv_count[2] = {0, 0};
    //当前rtp时间戳
    uint32_t _stamp[2] = {0, 0};
135 136

    //超时功能实现
137 138 139
    Ticker _rtp_recv_ticker;
    std::shared_ptr<Timer> _play_check_timer;
    std::shared_ptr<Timer> _rtp_check_timer;
140

141 142 143 144
    //rtcp统计,trackid idx 为数组下标
    RtcpCounter _rtcp_counter[2];
    //rtcp发送时间,trackid idx 为数组下标
    Ticker _rtcp_send_ticker[2];
145

146 147
    //是否为性能测试模式
    bool _benchmark_mode = false;
148 149 150

    //服务器支持的命令
    set<string> _supported_cmd;
xzl committed
151 152
};

xiongziliang committed
153
} /* namespace mediakit */
xzl committed
154
#endif /* SRC_RTSPPLAYER_RTSPPLAYER_H_TXT_ */