RtmpPlayer.h 3.99 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_RtmpPlayer2_H_
#define SRC_RTMP_RtmpPlayer2_H_

xiongzilaing committed
30
#include <memory>
xzl committed
31 32 33
#include <string>
#include <functional>
#include "amf.h"
xiongzilaing committed
34
#include "Rtmp.h"
xzl committed
35
#include "RtmpProtocol.h"
xiongzilaing committed
36 37 38 39 40 41
#include "Player/PlayerBase.h"
#include "Util/util.h"
#include "Util/logger.h"
#include "Util/TimeTicker.h"
#include "Network/Socket.h"
#include "Network/TcpClient.h"
xiongziliang committed
42

xiongziliang committed
43
using namespace toolkit;
xiongziliang committed
44
using namespace mediakit::Client;
xzl committed
45

xiongziliang committed
46
namespace mediakit {
xzl committed
47 48 49 50

class RtmpPlayer:public PlayerBase, public TcpClient,  public RtmpProtocol{
public:
	typedef std::shared_ptr<RtmpPlayer> Ptr;
51
	RtmpPlayer(const EventPoller::Ptr &poller);
xzl committed
52 53
	virtual ~RtmpPlayer();

xiongziliang committed
54
	void play(const string &strUrl) override;
xzl committed
55 56 57 58
	void pause(bool bPause) override;
	void teardown() override;
protected:
	virtual bool onCheckMeta(AMFValue &val) =0;
xiongziliang committed
59
	virtual void onMediaData(const RtmpPacket::Ptr &chunkData) =0;
60 61
	uint32_t getProgressMilliSecond() const;
	void seekToMilliSecond(uint32_t ms);
xiongziliang committed
62
protected:
63
	void onMediaData_l(const RtmpPacket::Ptr &chunkData) {
64
		_mediaTicker.resetTime();
xzl committed
65 66
		onMediaData(chunkData);
	}
67 68

	void onPlayResult_l(const SockException &ex);
xzl committed
69 70

	//for Tcpclient
71
	void onRecv(const Buffer::Ptr &pBuf) override;
xzl committed
72 73 74 75 76
	void onConnect(const SockException &err) override;
	void onErr(const SockException &ex) override;
	//fro RtmpProtocol
	void onRtmpChunk(RtmpPacket &chunkData) override;
	void onStreamDry(uint32_t ui32StreamId) override;
77 78
    void onSendRawData(const Buffer::Ptr &buffer) override{
        send(buffer);
771730766@qq.com committed
79
    }
xzl committed
80 81 82

	template<typename FUN>
	inline void addOnResultCB(const FUN &fun) {
83 84
		lock_guard<recursive_mutex> lck(_mtxOnResultCB);
		_mapOnResultCB.emplace(_iReqID, fun);
xzl committed
85 86 87
	}
	template<typename FUN>
	inline void addOnStatusCB(const FUN &fun) {
88 89
		lock_guard<recursive_mutex> lck(_mtxOnStatusCB);
		_dqOnStatusCB.emplace_back(fun);
xzl committed
90 91 92 93 94 95 96 97 98 99 100
	}

	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_play();
	inline void send_pause(bool bPause);

101 102 103 104
	string _strApp;
	string _strStream;
	string _strTcUrl;
	bool _bPaused = false;
xzl committed
105

106 107 108 109
	unordered_map<int, function<void(AMFDecoder &dec)> > _mapOnResultCB;
	recursive_mutex _mtxOnResultCB;
	deque<function<void(AMFValue &dec)> > _dqOnStatusCB;
	recursive_mutex _mtxOnStatusCB;
xzl committed
110 111 112 113 114

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

	//超时功能实现
115 116 117
	Ticker _mediaTicker;
	std::shared_ptr<Timer> _pMediaTimer;
	std::shared_ptr<Timer> _pPlayTimer;
xzl committed
118
	//心跳定时器
119
	std::shared_ptr<Timer> _pBeatTimer;
xzl committed
120 121

	//播放进度控制
122 123 124
	uint32_t _iSeekTo = 0;
	uint32_t _aiFistStamp[2] = { 0, 0 };
	uint32_t _aiNowStamp[2] = { 0, 0 };
125
	Ticker _aNowStampTicker[2];
xzl committed
126 127
};

xiongziliang committed
128
} /* namespace mediakit */
xzl committed
129 130

#endif /* SRC_RTMP_RtmpPlayer2_H_ */