RtmpSession.h 3.8 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 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"
xiongziliang committed
40
using namespace toolkit;
xzl committed
41

xiongziliang committed
42
namespace mediakit {
xzl committed
43

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

xzl committed
66 67 68 69
	void onCmd_seek(AMFDecoder &dec);
	void onCmd_pause(AMFDecoder &dec);
	void setMetaData(AMFDecoder &dec);

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

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

84
    bool close() override {
85
        InfoL << "kick out:" << _mediaInfo._vhost << " " << _mediaInfo._app << " " << _mediaInfo._streamid;
86 87 88
        safeShutdown();
        return true;
    }
89 90

    void doDelay(int delaySec,const std::function<void()> &fun);
xiongziliang committed
91
	void cancelDelyaTask();
92
	void findStream(const function<void(const RtmpMediaSource::Ptr &src)> &cb ,bool retry = true);
xiongziliang committed
93
private:
94 95 96 97 98 99 100 101 102
	std::string _strTcUrl;
	MediaInfo _mediaInfo;
	double _dNowReqID = 0;
	Ticker _ticker;//数据接收时间
	SmoothTicker _stampTicker[2];//时间戳生产器
	RingBuffer<RtmpPacket::Ptr>::RingReader::Ptr _pRingReader;
	std::shared_ptr<RtmpMediaSource> _pPublisherSrc;
	std::weak_ptr<RtmpMediaSource> _pPlayerSrc;
	uint32_t _aui32FirstStamp[2] = {0};
xiongziliang committed
103
	//消耗的总流量
104 105 106
	uint64_t _ui64TotalBytes = 0;
    std::function<void()> _delayTask;
    uint32_t _iTaskTimeLine = 0;
107

xzl committed
108 109
};

xiongziliang committed
110
} /* namespace mediakit */
xzl committed
111 112

#endif /* SRC_RTMP_RTMPSESSION_H_ */