RtmpPusher.h 3.9 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * MIT License
xzl committed
3
 *
xiongziliang committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * Copyright (c) 2016 xiongziliang <771730766@qq.com>
 *
 * 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"
xzl committed
33 34 35 36 37 38 39

namespace ZL {
namespace Rtmp {

class RtmpPusher: public RtmpProtocol , public TcpClient{
public:
	typedef std::shared_ptr<RtmpPusher> Ptr;
xiongziliang committed
40
	typedef std::function<void(const SockException &ex)> Event;
41
	RtmpPusher(const char *strVhost,const char *strApp,const char *strStream);
42
	RtmpPusher(const RtmpMediaSource::Ptr  &src);
xzl committed
43 44 45 46 47
	virtual ~RtmpPusher();

	void publish(const char* strUrl);
	void teardown();

xiongziliang committed
48 49 50 51 52 53 54 55
	void setOnPublished(Event onPublished) {
		m_onPublished = onPublished;
	}

	void setOnShutdown(Event onShutdown) {
		m_onShutdown = onShutdown;
	}

xzl committed
56 57 58 59 60 61 62 63 64 65 66 67
protected:

	//for Tcpclient
	void onRecv(const Socket::Buffer::Ptr &pBuf) override;
	void onConnect(const SockException &err) override;
	void onErr(const SockException &ex) override;

	//fro RtmpProtocol
	void onRtmpChunk(RtmpPacket &chunkData) override;
	void onSendRawData(const char *pcRawData, int iSize) override {
		send(pcRawData, iSize);
	}
771730766@qq.com committed
68 69
	void onSendRawData(const Socket::Buffer::Ptr &buffer,int flags) override{
		m_pSock->send(buffer,flags);
xiongziliang committed
70
	}
xzl committed
71
private:
72
    void init(const RtmpMediaSource::Ptr  &src);
xiongziliang committed
73 74 75 76 77
	void onShutdown(const SockException &ex) {
		m_pPublishTimer.reset();
		if(m_onShutdown){
			m_onShutdown(ex);
		}
xzl committed
78
	}
xiongziliang committed
79 80 81 82 83
	void onPublishResult(const SockException &ex) {
		m_pPublishTimer.reset();
		if(m_onPublished){
			m_onPublished(ex);
		}
xzl committed
84 85 86 87
	}

	template<typename FUN>
	inline void addOnResultCB(const FUN &fun) {
xzl committed
88
		lock_guard<recursive_mutex> lck(m_mtxOnResultCB);
xzl committed
89 90 91 92
		m_mapOnResultCB.emplace(m_iReqID, fun);
	}
	template<typename FUN>
	inline void addOnStatusCB(const FUN &fun) {
xzl committed
93
		lock_guard<recursive_mutex> lck(m_mtxOnStatusCB);
xzl committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
		m_dqOnStatusCB.emplace_back(fun);
	}

	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();

	string m_strApp;
	string m_strStream;
	string m_strTcUrl;

	unordered_map<int, function<void(AMFDecoder &dec)> > m_mapOnResultCB;
xzl committed
111
	recursive_mutex m_mtxOnResultCB;
xzl committed
112
	deque<function<void(AMFValue &dec)> > m_dqOnStatusCB;
xzl committed
113
	recursive_mutex m_mtxOnStatusCB;
xzl committed
114 115 116 117 118

	typedef void (RtmpPusher::*rtmpCMDHandle)(AMFDecoder &dec);
	static unordered_map<string, rtmpCMDHandle> g_mapCmd;

	//超时功能实现
xiongziliang committed
119
	std::shared_ptr<Timer> m_pPublishTimer;
xzl committed
120 121 122 123
    
    //源
    std::weak_ptr<RtmpMediaSource> m_pMediaSrc;
    RtmpMediaSource::RingType::RingReader::Ptr m_pRtmpReader;
xiongziliang committed
124 125 126
    //事件监听
    Event m_onShutdown;
    Event m_onPublished;
xzl committed
127 128 129 130 131 132
};

} /* namespace Rtmp */
} /* namespace ZL */

#endif /* SRC_RTMP_RTMPPUSHER_H_ */