RtmpPlayer.h 3.53 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
xiongziliang committed
3
 *
4
 * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
xiongziliang committed
5
 *
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
 */

xiongziliang committed
11 12
#ifndef SRC_RTMP_RtmpPlayer_H_
#define SRC_RTMP_RtmpPlayer_H_
xzl committed
13

xiongzilaing committed
14
#include <memory>
xzl committed
15 16 17
#include <string>
#include <functional>
#include "amf.h"
xiongzilaing committed
18
#include "Rtmp.h"
xzl committed
19
#include "RtmpProtocol.h"
xiongzilaing committed
20 21 22 23 24 25
#include "Player/PlayerBase.h"
#include "Util/util.h"
#include "Util/logger.h"
#include "Util/TimeTicker.h"
#include "Network/Socket.h"
#include "Network/TcpClient.h"
xiongziliang committed
26

xiongziliang committed
27
using namespace toolkit;
xiongziliang committed
28
using namespace mediakit::Client;
xzl committed
29

xiongziliang committed
30
namespace mediakit {
xiongziliang committed
31

3503207480@qq.com committed
32
//实现了rtmp播放器协议部分的功能,及数据接收功能
xiongziliang committed
33
class RtmpPlayer : public PlayerBase, public TcpClient, public RtmpProtocol {
xzl committed
34
public:
35 36
    typedef std::shared_ptr<RtmpPlayer> Ptr;
    RtmpPlayer(const EventPoller::Ptr &poller);
xiongziliang committed
37
    ~RtmpPlayer() override;
xzl committed
38

39 40 41
    void play(const string &strUrl) override;
    void pause(bool bPause) override;
    void teardown() override;
xiongziliang committed
42

xzl committed
43
protected:
xiongziliang committed
44
    virtual bool onCheckMeta(const AMFValue &val) = 0;
xia-chu committed
45
    virtual void onMediaData(RtmpPacket::Ptr chunk_data) = 0;
46 47
    uint32_t getProgressMilliSecond() const;
    void seekToMilliSecond(uint32_t ms);
xiongziliang committed
48

xiongziliang committed
49
protected:
xia-chu committed
50
    void onMediaData_l(RtmpPacket::Ptr chunk_data);
51
    //在获取config帧后才触发onPlayResult_l(而不是收到play命令回复),所以此时所有track都初始化完毕了
xiongziliang committed
52
    void onPlayResult_l(const SockException &ex, bool handshake_done);
xzl committed
53

54
    //form Tcpclient
xiongziliang committed
55
    void onRecv(const Buffer::Ptr &buf) override;
56 57 58
    void onConnect(const SockException &err) override;
    void onErr(const SockException &ex) override;
    //from RtmpProtocol
xia-chu committed
59
    void onRtmpChunk(RtmpPacket::Ptr chunk_data) override;
xiongziliang committed
60
    void onStreamDry(uint32_t stream_index) override;
61 62
    void onSendRawData(Buffer::Ptr buffer) override {
        send(std::move(buffer));
771730766@qq.com committed
63
    }
xzl committed
64

xiongziliang committed
65
    template<typename FUNC>
xiongziliang committed
66
    void addOnResultCB(const FUNC &func) {
xiongziliang committed
67 68
        lock_guard<recursive_mutex> lck(_mtx_on_result);
        _map_on_result.emplace(_send_req_id, func);
69
    }
xiongziliang committed
70
    template<typename FUNC>
xiongziliang committed
71
    void addOnStatusCB(const FUNC &func) {
xiongziliang committed
72 73
        lock_guard<recursive_mutex> lck(_mtx_on_status);
        _deque_on_status.emplace_back(func);
74
    }
xzl committed
75

76 77 78
    void onCmd_result(AMFDecoder &dec);
    void onCmd_onStatus(AMFDecoder &dec);
    void onCmd_onMetaData(AMFDecoder &dec);
xzl committed
79

xiongziliang committed
80 81 82 83
    void send_connect();
    void send_createStream();
    void send_play();
    void send_pause(bool pause);
xiongziliang committed
84

85
private:
xiongziliang committed
86 87 88
    string _app;
    string _stream_id;
    string _tc_url;
xzl committed
89

xiongziliang committed
90
    bool _paused = false;
91
    bool _metadata_got = false;
92 93
    //是否为性能测试模式
    bool _benchmark_mode = false;
xiongziliang committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

    //播放进度控制
    uint32_t _seek_ms = 0;
    uint32_t _fist_stamp[2] = {0, 0};
    uint32_t _now_stamp[2] = {0, 0};
    Ticker _now_stamp_ticker[2];

    recursive_mutex _mtx_on_result;
    recursive_mutex _mtx_on_status;
    deque<function<void(AMFValue &dec)> > _deque_on_status;
    unordered_map<int, function<void(AMFDecoder &dec)> > _map_on_result;

    //rtmp接收超时计时器
    Ticker _rtmp_recv_ticker;
    //心跳发送定时器
    std::shared_ptr<Timer> _beat_timer;
    //播放超时定时器
    std::shared_ptr<Timer> _play_timer;
    //rtmp接收超时定时器
    std::shared_ptr<Timer> _rtmp_recv_timer;
xzl committed
114 115
};

xiongziliang committed
116
} /* namespace mediakit */
xiongziliang committed
117
#endif /* SRC_RTMP_RtmpPlayer_H_ */