RtmpPusher.h 2.74 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 11 12 13 14 15
 */

#ifndef SRC_RTMP_RTMPPUSHER_H_
#define SRC_RTMP_RTMPPUSHER_H_

#include "RtmpProtocol.h"
#include "RtmpMediaSource.h"
xiongzilaing committed
16
#include "Network/TcpClient.h"
xiongziliang committed
17
#include "Pusher/PusherBase.h"
xzl committed
18

xiongziliang committed
19
namespace mediakit {
xzl committed
20

xiongziliang committed
21
class RtmpPusher : public RtmpProtocol, public TcpClient, public PusherBase {
xzl committed
22
public:
23 24
    typedef std::shared_ptr<RtmpPusher> Ptr;
    RtmpPusher(const EventPoller::Ptr &poller,const RtmpMediaSource::Ptr &src);
xiongziliang committed
25
    ~RtmpPusher() override;
xzl committed
26

xiongziliang committed
27
    void publish(const string &url) override ;
28
    void teardown() override;
xiongziliang committed
29

30
    void setOnPublished(const Event &cb) override {
xiongziliang committed
31
        _on_published = cb;
32
    }
xiongziliang committed
33

34
    void setOnShutdown(const Event &cb) override{
xiongziliang committed
35
        _on_shutdown = cb;
36
    }
xiongziliang committed
37

xzl committed
38
protected:
39
    //for Tcpclient override
xiongziliang committed
40
    void onRecv(const Buffer::Ptr &buf) override;
41 42
    void onConnect(const SockException &err) override;
    void onErr(const SockException &ex) override;
xzl committed
43

44
    //for RtmpProtocol override
xia-chu committed
45
    void onRtmpChunk(RtmpPacket::Ptr chunk_data) override;
46 47
    void onSendRawData(Buffer::Ptr buffer) override{
        send(std::move(buffer));
48
    }
xiongziliang committed
49

xzl committed
50
private:
xiongziliang committed
51
    void onPublishResult(const SockException &ex, bool handshake_done);
xzl committed
52

53 54
    template<typename FUN>
    inline void addOnResultCB(const FUN &fun) {
xiongziliang committed
55 56
        lock_guard<recursive_mutex> lck(_mtx_on_result);
        _map_on_result.emplace(_send_req_id, fun);
57 58 59
    }
    template<typename FUN>
    inline void addOnStatusCB(const FUN &fun) {
xiongziliang committed
60 61
        lock_guard<recursive_mutex> lck(_mtx_on_status);
        _deque_on_status.emplace_back(fun);
62
    }
xzl committed
63

64 65 66
    void onCmd_result(AMFDecoder &dec);
    void onCmd_onStatus(AMFDecoder &dec);
    void onCmd_onMetaData(AMFDecoder &dec);
xzl committed
67

68 69 70 71 72
    inline void send_connect();
    inline void send_createStream();
    inline void send_publish();
    inline void send_metaData();
    void setSocketFlags();
xiongziliang committed
73

74
private:
xiongziliang committed
75 76 77 78 79 80 81 82 83
    string _app;
    string _stream_id;
    string _tc_url;

    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;

xiongziliang committed
84
    //事件监听
xiongziliang committed
85 86 87 88 89 90 91
    Event _on_shutdown;
    Event _on_published;

    //推流超时定时器
    std::shared_ptr<Timer> _publish_timer;
    std::weak_ptr<RtmpMediaSource> _publish_src;
    RtmpMediaSource::RingType::RingReader::Ptr _rtmp_reader;
xzl committed
92 93
};

xiongziliang committed
94
} /* namespace mediakit */
xzl committed
95 96

#endif /* SRC_RTMP_RTMPPUSHER_H_ */