RtmpPusher.h 2.73 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 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 25
    typedef std::shared_ptr<RtmpPusher> Ptr;
    RtmpPusher(const EventPoller::Ptr &poller,const RtmpMediaSource::Ptr &src);
    virtual ~RtmpPusher();
xzl committed
26

27
    void publish(const string &strUrl) override ;
xzl committed
28

29
    void teardown() override;
xiongziliang committed
30

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

35 36 37
    void setOnShutdown(const Event &cb) override{
        _onShutdown = cb;
    }
xzl committed
38
protected:
39 40 41 42
    //for Tcpclient override
    void onRecv(const Buffer::Ptr &pBuf) override;
    void onConnect(const SockException &err) override;
    void onErr(const SockException &ex) override;
xzl committed
43

44 45 46 47 48
    //for RtmpProtocol override
    void onRtmpChunk(RtmpPacket &chunkData) override;
    void onSendRawData(const Buffer::Ptr &buffer) override{
        send(buffer);
    }
xzl committed
49
private:
50
    void onPublishResult(const SockException &ex,bool handshakeCompleted);
xzl committed
51

52 53 54 55 56 57 58 59 60 61
    template<typename FUN>
    inline void addOnResultCB(const FUN &fun) {
        lock_guard<recursive_mutex> lck(_mtxOnResultCB);
        _mapOnResultCB.emplace(_iReqID, fun);
    }
    template<typename FUN>
    inline void addOnStatusCB(const FUN &fun) {
        lock_guard<recursive_mutex> lck(_mtxOnStatusCB);
        _dqOnStatusCB.emplace_back(fun);
    }
xzl committed
62

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

67 68 69 70 71
    inline void send_connect();
    inline void send_createStream();
    inline void send_publish();
    inline void send_metaData();
    void setSocketFlags();
72
private:
73 74 75
    string _strApp;
    string _strStream;
    string _strTcUrl;
xzl committed
76

77 78 79 80 81 82
    unordered_map<int, function<void(AMFDecoder &dec)> > _mapOnResultCB;
    recursive_mutex _mtxOnResultCB;
    deque<function<void(AMFValue &dec)> > _dqOnStatusCB;
    recursive_mutex _mtxOnStatusCB;
    //超时功能实现
    std::shared_ptr<Timer> _pPublishTimer;
xzl committed
83
    //源
84 85
    std::weak_ptr<RtmpMediaSource> _pMediaSrc;
    RtmpMediaSource::RingType::RingReader::Ptr _pRtmpReader;
xiongziliang committed
86
    //事件监听
87 88
    Event _onShutdown;
    Event _onPublished;
xzl committed
89 90
};

xiongziliang committed
91
} /* namespace mediakit */
xzl committed
92 93

#endif /* SRC_RTMP_RTMPPUSHER_H_ */