RtmpPusher.h 3.35 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 39
public:
	typedef std::shared_ptr<RtmpPusher> Ptr;
40
	RtmpPusher(const EventPoller::Ptr &poller,const RtmpMediaSource::Ptr &src);
xzl committed
41 42
	virtual ~RtmpPusher();

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

xiongziliang committed
45
	void teardown() override;
xiongziliang committed
46

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

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

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

	template<typename FUN>
	inline void addOnResultCB(const FUN &fun) {
70 71
		lock_guard<recursive_mutex> lck(_mtxOnResultCB);
		_mapOnResultCB.emplace(_iReqID, fun);
xzl committed
72 73 74
	}
	template<typename FUN>
	inline void addOnStatusCB(const FUN &fun) {
75 76
		lock_guard<recursive_mutex> lck(_mtxOnStatusCB);
		_dqOnStatusCB.emplace_back(fun);
xzl committed
77 78 79 80 81 82 83 84 85 86 87
	}

	void onCmd_result(AMFDecoder &dec);
	void onCmd_onStatus(AMFDecoder &dec);
	void onCmd_onMetaData(AMFDecoder &dec);

	inline void send_connect();
	inline void send_createStream();
	inline void send_publish();
	inline void send_metaData();

88
private:
89 90 91
	string _strApp;
	string _strStream;
	string _strTcUrl;
xzl committed
92

93 94 95 96
	unordered_map<int, function<void(AMFDecoder &dec)> > _mapOnResultCB;
	recursive_mutex _mtxOnResultCB;
	deque<function<void(AMFValue &dec)> > _dqOnStatusCB;
	recursive_mutex _mtxOnStatusCB;
xzl committed
97
	//超时功能实现
98
	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_ */