RtmpSession.h 3.61 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 32 33
 */

#ifndef SRC_RTMP_RTMPSESSION_H_
#define SRC_RTMP_RTMPSESSION_H_

#include <unordered_map>
#include "amf.h"
#include "Rtmp.h"
#include "utils.h"
xiongziliang committed
34
#include "Common/config.h"
xzl committed
35 36
#include "RtmpProtocol.h"
#include "RtmpToRtspMediaSource.h"
xiongzilaing committed
37 38
#include "Util/util.h"
#include "Util/TimeTicker.h"
39
#include "Network/TcpSession.h"
40 41
#include "MediaFile/Stamp.h"

xiongziliang committed
42
using namespace toolkit;
xzl committed
43

xiongziliang committed
44
namespace mediakit {
xzl committed
45

46
class RtmpSession: public TcpSession ,public  RtmpProtocol , public MediaSourceEvent{
xzl committed
47 48
public:
	typedef std::shared_ptr<RtmpSession> Ptr;
xiongziliang committed
49
	RtmpSession(const Socket::Ptr &_sock);
xzl committed
50
	virtual ~RtmpSession();
51
	void onRecv(const Buffer::Ptr &pBuf) override;
xzl committed
52 53 54 55 56 57 58 59 60 61 62
	void onError(const SockException &err) override;
	void onManager() override;
private:
	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);
63
	void onCmd_play2(AMFDecoder &dec);
64
	void doPlay(AMFDecoder &dec);
65 66 67
	void doPlayResponse(const string &err,const std::function<void(bool)> &cb);
	void sendPlayResponse(const string &err,const RtmpMediaSource::Ptr &src);

xzl committed
68 69 70 71
	void onCmd_seek(AMFDecoder &dec);
	void onCmd_pause(AMFDecoder &dec);
	void setMetaData(AMFDecoder &dec);

xiongziliang committed
72
	void onSendMedia(const RtmpPacket::Ptr &pkt);
73
	void onSendRawData(const Buffer::Ptr &buffer) override{
74
        _ui64TotalBytes += buffer->size();
75
		send(buffer);
xiongziliang committed
76
	}
xzl committed
77 78 79 80 81
	void onRtmpChunk(RtmpPacket &chunkData) override;

	template<typename first, typename second>
	inline void sendReply(const char *str, const first &reply, const second &status) {
		AMFEncoder invoke;
82
		invoke << str << _dNowReqID << reply << status;
xzl committed
83 84
		sendResponse(MSG_CMD, invoke.data());
	}
85

86 87
    bool close(MediaSource &sender,bool force) override ;
    void onNoneReader(MediaSource &sender) override;
88
	void setSocketFlags();
89
	string getStreamId(const string &str);
90
	void dumpMetadata(const AMFValue &metadata);
xiongziliang committed
91
private:
92
	bool _metadata_got = false;
93 94 95 96 97
	std::string _strTcUrl;
	MediaInfo _mediaInfo;
	double _dNowReqID = 0;
	Ticker _ticker;//数据接收时间
	RingBuffer<RtmpPacket::Ptr>::RingReader::Ptr _pRingReader;
98
	std::shared_ptr<RtmpToRtspMediaSource> _pPublisherSrc;
99
	std::weak_ptr<RtmpMediaSource> _pPlayerSrc;
100 101
	//时间戳修整器
	Stamp _stamp[2];
xiongziliang committed
102
	//消耗的总流量
103
	uint64_t _ui64TotalBytes = 0;
104

xzl committed
105 106
};

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

#endif /* SRC_RTMP_RTMPSESSION_H_ */