RtmpPusher.h 3.56 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * MIT License
xzl committed
3
 *
xiongziliang committed
4
 * Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
xiongziliang committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
xzl committed
25 26 27 28 29 30 31
 */

#ifndef SRC_RTMP_RTMPPUSHER_H_
#define SRC_RTMP_RTMPPUSHER_H_

#include "RtmpProtocol.h"
#include "RtmpMediaSource.h"
xiongzilaing committed
32
#include "Network/TcpClient.h"
xiongziliang committed
33
#include "Pusher/PusherBase.h"
xzl committed
34

xiongziliang committed
35
namespace mediakit {
xzl committed
36

xiongziliang committed
37
class RtmpPusher: public RtmpProtocol , public TcpClient , public PusherBase{
xzl committed
38
public:
39 40 41
    typedef std::shared_ptr<RtmpPusher> Ptr;
    RtmpPusher(const EventPoller::Ptr &poller,const RtmpMediaSource::Ptr &src);
    virtual ~RtmpPusher();
xzl committed
42

43
    void publish(const string &strUrl) override ;
xzl committed
44

45
    void teardown() override;
xiongziliang committed
46

47 48 49
    void setOnPublished(const Event &cb) override {
        _onPublished = cb;
    }
xiongziliang committed
50

51 52 53
    void setOnShutdown(const Event &cb) override{
        _onShutdown = cb;
    }
xzl committed
54
protected:
55 56 57 58
    //for Tcpclient override
    void onRecv(const Buffer::Ptr &pBuf) override;
    void onConnect(const SockException &err) override;
    void onErr(const SockException &ex) override;
xzl committed
59

60 61 62 63 64
    //for RtmpProtocol override
    void onRtmpChunk(RtmpPacket &chunkData) override;
    void onSendRawData(const Buffer::Ptr &buffer) override{
        send(buffer);
    }
xzl committed
65
private:
66
    void onPublishResult(const SockException &ex,bool handshakeCompleted);
xzl committed
67

68 69 70 71 72 73 74 75 76 77
    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
78

79 80 81
    void onCmd_result(AMFDecoder &dec);
    void onCmd_onStatus(AMFDecoder &dec);
    void onCmd_onMetaData(AMFDecoder &dec);
xzl committed
82

83 84 85 86 87
    inline void send_connect();
    inline void send_createStream();
    inline void send_publish();
    inline void send_metaData();
    void setSocketFlags();
88
private:
89 90 91
    string _strApp;
    string _strStream;
    string _strTcUrl;
xzl committed
92

93 94 95 96 97 98
    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
99
    //源
100 101
    std::weak_ptr<RtmpMediaSource> _pMediaSrc;
    RtmpMediaSource::RingType::RingReader::Ptr _pRtmpReader;
xiongziliang committed
102
    //事件监听
103 104
    Event _onShutdown;
    Event _onPublished;
xzl committed
105 106
};

xiongziliang committed
107
} /* namespace mediakit */
xzl committed
108 109

#endif /* SRC_RTMP_RTMPPUSHER_H_ */