RtmpProtocol.h 4.39 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
 */

#ifndef SRC_RTMP_RTMPPROTOCOL_H_
#define SRC_RTMP_RTMPPROTOCOL_H_

xiongzilaing committed
30
#include <memory>
xzl committed
31 32 33
#include <string>
#include <functional>
#include "amf.h"
xiongzilaing committed
34 35 36 37 38
#include "Rtmp.h"
#include "Util/util.h"
#include "Util/logger.h"
#include "Util/TimeTicker.h"
#include "Network/Socket.h"
771730766@qq.com committed
39
#include "Util/ResourcePool.h"
xzl committed
40 41

using namespace std;
xiongziliang committed
42
using namespace toolkit;
xzl committed
43

xiongziliang committed
44
namespace mediakit {
xzl committed
45 46 47 48 49 50 51 52

class RtmpProtocol {
public:
	RtmpProtocol();
	virtual ~RtmpProtocol();
	//作为客户端发送c0c1,等待s0s1s2并且回调
	void startClientSession(const function<void()> &cb);
	void onParseRtmp(const char *pcRawData,int iSize);
xiongziliang committed
53
	void reset();
xzl committed
54
protected:
55
	virtual void onSendRawData(const Buffer::Ptr &buffer) = 0;
xzl committed
56 57
	virtual void onRtmpChunk(RtmpPacket &chunkData) = 0;
	virtual void onStreamBegin(uint32_t ui32StreamId){
58
		_ui32StreamId = ui32StreamId;
xzl committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
	}
	virtual void onStreamEof(uint32_t ui32StreamId){};
	virtual void onStreamDry(uint32_t ui32StreamId){};
protected:
	void sendAcknowledgement(uint32_t ui32Size);
	void sendAcknowledgementSize(uint32_t ui32Size);
	void sendPeerBandwidth(uint32_t ui32Size);
	void sendChunkSize(uint32_t ui32Size);
	void sendPingRequest(uint32_t ui32TimeStamp = ::time(NULL));
	void sendPingResponse(uint32_t ui32TimeStamp = ::time(NULL));
	void sendSetBufferLength(uint32_t ui32StreamId, uint32_t ui32Length);
	void sendUserControl(uint16_t ui16EventType, uint32_t ui32EventData);
	void sendUserControl(uint16_t ui16EventType, const string &strEventData);

	void sendInvoke(const string &strCmd, const AMFValue &val);
	void sendRequest(int iCmd, const string &str);
	void sendResponse(int iType, const string &str);
76
	void sendRtmp(uint8_t ui8Type, uint32_t ui32StreamId, const std::string &strBuf, uint32_t ui32TimeStamp, int iChunkID);
77
	void sendRtmp(uint8_t ui8Type, uint32_t ui32StreamId, const Buffer::Ptr &buffer, uint32_t ui32TimeStamp, int iChunkID);
xzl committed
78
protected:
79 80 81 82 83
	int _iReqID = 0;
	uint32_t _ui32StreamId = STREAM_CONTROL;
	int _iNowStreamID = 0;
	int _iNowChunkID = 0;
	bool _bDataStarted = false;
84 85
	inline BufferRaw::Ptr obtainBuffer();
	inline BufferRaw::Ptr obtainBuffer(const void *data, int len);
86
	//ResourcePool<BufferRaw,MAX_SEND_PKT> _bufferPool;
xzl committed
87 88 89
private:
	void handle_S0S1S2(const function<void()> &cb);
	void handle_C0C1();
90
	void handle_C1_simple();
91
#ifdef ENABLE_OPENSSL
92 93 94 95 96
	void handle_C1_complex();
	string get_C1_digest(const uint8_t *ptr,char **digestPos);
	string get_C1_key(const uint8_t *ptr);
	void check_C1_Digest(const string &digest,const string &data);
	void send_complex_S0S1S2(int schemeType,const string &digest);
97
#endif //ENABLE_OPENSSL
98

xzl committed
99 100 101 102
	void handle_C2();
	void handle_rtmp();
	void handle_rtmpChunk(RtmpPacket &chunkData);

103
private:
xzl committed
104
	////////////ChunkSize////////////
105 106
	size_t _iChunkLenIn = DEFAULT_CHUNK_LEN;
	size_t _iChunkLenOut = DEFAULT_CHUNK_LEN;
xzl committed
107
	////////////Acknowledgement////////////
108 109 110
	uint32_t _ui32ByteSent = 0;
	uint32_t _ui32LastSent = 0;
	uint32_t _ui32WinSize = 0;
xzl committed
111
	///////////PeerBandwidth///////////
112 113
	uint32_t _ui32Bandwidth = 2500000;
	uint8_t _ui8LimitType = 2;
xzl committed
114
	////////////Chunk////////////
115
	unordered_map<int, RtmpPacket> _mapChunkData;
xzl committed
116
	//////////Rtmp parser//////////
117 118
	string _strRcvBuf;
	function<void()> _nextHandle;
xzl committed
119 120
};

xiongziliang committed
121
} /* namespace mediakit */
xzl committed
122 123

#endif /* SRC_RTMP_RTMPPROTOCOL_H_ */