RtmpSession.h 2.22 KB
Newer Older
xzl committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * RtmpSession.h
 *
 *  Created on: 2017年2月10日
 *      Author: xzl
 */

#ifndef SRC_RTMP_RTMPSESSION_H_
#define SRC_RTMP_RTMPSESSION_H_

#include <netinet/in.h>
#include <unordered_map>
#include "amf.h"
#include "Rtmp.h"
#include "utils.h"
xiongziliang committed
16
#include "Common/config.h"
xzl committed
17 18
#include "RtmpProtocol.h"
#include "RtmpToRtspMediaSource.h"
xiongzilaing committed
19 20
#include "Util/util.h"
#include "Util/TimeTicker.h"
xzl committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include "Network/TcpLimitedSession.h"

using namespace ZL::Util;
using namespace ZL::Network;

namespace ZL {
namespace Rtmp {

class RtmpSession: public TcpLimitedSession<MAX_TCP_SESSION> ,public  RtmpProtocol{
public:
	typedef std::shared_ptr<RtmpSession> Ptr;
	RtmpSession(const std::shared_ptr<ThreadPool> &_th, const Socket::Ptr &_sock);
	virtual ~RtmpSession();
	void onRecv(const Socket::Buffer::Ptr &pBuf) override;
	void onError(const SockException &err) override;
	void onManager() override;
private:
	std::string m_strApp;
	std::string m_strId;
	double m_dNowReqID = 0;
	Ticker m_ticker;//数据接收时间
	typedef void (RtmpSession::*rtmpCMDHandle)(AMFDecoder &dec);
	static unordered_map<string, rtmpCMDHandle> g_mapCmd;

	RingBuffer<RtmpPacket>::RingReader::Ptr m_pRingReader;
	std::shared_ptr<RtmpMediaSource> m_pPublisherSrc;
xzl committed
47
	bool m_bPublisherSrcRegisted = false;
xzl committed
48
    std::weak_ptr<RtmpMediaSource> m_pPlayerSrc;
49
    uint32_t m_aui32FirstStamp[2] = {0};
xzl committed
50 51 52 53 54 55 56 57 58

	void onProcessCmd(AMFDecoder &dec);
	void onCmd_connect(AMFDecoder &dec);
	void onCmd_createStream(AMFDecoder &dec);

	void onCmd_publish(AMFDecoder &dec);
	void onCmd_deleteStream(AMFDecoder &dec);

	void onCmd_play(AMFDecoder &dec);
59 60
	void onCmd_play2(AMFDecoder &dec);
	void doPlay();
xzl committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
	void onCmd_seek(AMFDecoder &dec);
	void onCmd_pause(AMFDecoder &dec);
	void setMetaData(AMFDecoder &dec);

	void onSendMedia(const RtmpPacket &pkt);
	void onSendRawData(const char *pcRawData,int iSize) override{
		send(pcRawData, iSize);
	}
	void onRtmpChunk(RtmpPacket &chunkData) override;

	template<typename first, typename second>
	inline void sendReply(const char *str, const first &reply, const second &status) {
		AMFEncoder invoke;
		invoke << str << m_dNowReqID << reply << status;
		sendResponse(MSG_CMD, invoke.data());
	}
};

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

#endif /* SRC_RTMP_RTMPSESSION_H_ */