2017-10-09 22:11:01 +08:00
|
|
|
|
/*
|
2017-09-27 16:20:30 +08:00
|
|
|
|
* MIT License
|
2017-04-01 16:35:56 +08:00
|
|
|
|
*
|
2019-05-08 15:40:07 +08:00
|
|
|
|
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*
|
|
|
|
|
* 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.
|
2017-04-01 16:35:56 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef SRC_RTMP_RtmpPlayer2_H_
|
|
|
|
|
#define SRC_RTMP_RtmpPlayer2_H_
|
|
|
|
|
|
2017-04-25 11:35:41 +08:00
|
|
|
|
#include <memory>
|
2017-04-01 16:35:56 +08:00
|
|
|
|
#include <string>
|
|
|
|
|
#include <functional>
|
|
|
|
|
#include "amf.h"
|
2017-04-25 11:35:41 +08:00
|
|
|
|
#include "Rtmp.h"
|
2017-04-01 16:35:56 +08:00
|
|
|
|
#include "RtmpProtocol.h"
|
2017-04-25 11:35:41 +08:00
|
|
|
|
#include "Player/PlayerBase.h"
|
|
|
|
|
#include "Util/util.h"
|
|
|
|
|
#include "Util/logger.h"
|
|
|
|
|
#include "Util/TimeTicker.h"
|
|
|
|
|
#include "Network/Socket.h"
|
|
|
|
|
#include "Network/TcpClient.h"
|
2019-03-27 18:56:49 +08:00
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
using namespace toolkit;
|
2019-03-27 18:56:49 +08:00
|
|
|
|
using namespace mediakit::Client;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
namespace mediakit {
|
2019-08-30 11:17:27 +08:00
|
|
|
|
//实现了rtmp播放器协议部分的功能,及数据接收功能
|
2017-04-01 16:35:56 +08:00
|
|
|
|
class RtmpPlayer:public PlayerBase, public TcpClient, public RtmpProtocol{
|
|
|
|
|
public:
|
|
|
|
|
typedef std::shared_ptr<RtmpPlayer> Ptr;
|
2019-04-01 10:16:15 +08:00
|
|
|
|
RtmpPlayer(const EventPoller::Ptr &poller);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
virtual ~RtmpPlayer();
|
|
|
|
|
|
2019-03-27 18:41:52 +08:00
|
|
|
|
void play(const string &strUrl) override;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
void pause(bool bPause) override;
|
|
|
|
|
void teardown() override;
|
|
|
|
|
protected:
|
2019-09-24 11:42:27 +08:00
|
|
|
|
virtual bool onCheckMeta(const AMFValue &val) =0;
|
2017-12-04 23:55:09 +08:00
|
|
|
|
virtual void onMediaData(const RtmpPacket::Ptr &chunkData) =0;
|
2018-10-26 14:12:16 +08:00
|
|
|
|
uint32_t getProgressMilliSecond() const;
|
|
|
|
|
void seekToMilliSecond(uint32_t ms);
|
2019-02-28 18:03:49 +08:00
|
|
|
|
protected:
|
2019-08-01 13:12:24 +08:00
|
|
|
|
void onMediaData_l(const RtmpPacket::Ptr &chunkData);
|
2019-11-19 15:52:02 +08:00
|
|
|
|
void onPlayResult_l(const SockException &ex, bool handshakeCompleted);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2019-08-30 11:17:27 +08:00
|
|
|
|
//form Tcpclient
|
2018-02-23 15:36:51 +08:00
|
|
|
|
void onRecv(const Buffer::Ptr &pBuf) override;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
void onConnect(const SockException &err) override;
|
|
|
|
|
void onErr(const SockException &ex) override;
|
2019-08-30 11:17:27 +08:00
|
|
|
|
//from RtmpProtocol
|
2017-04-01 16:35:56 +08:00
|
|
|
|
void onRtmpChunk(RtmpPacket &chunkData) override;
|
|
|
|
|
void onStreamDry(uint32_t ui32StreamId) override;
|
2018-09-26 23:12:03 +08:00
|
|
|
|
void onSendRawData(const Buffer::Ptr &buffer) override{
|
|
|
|
|
send(buffer);
|
2018-01-30 11:23:57 +08:00
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
template<typename FUN>
|
|
|
|
|
inline void addOnResultCB(const FUN &fun) {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
lock_guard<recursive_mutex> lck(_mtxOnResultCB);
|
|
|
|
|
_mapOnResultCB.emplace(_iReqID, fun);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
template<typename FUN>
|
|
|
|
|
inline void addOnStatusCB(const FUN &fun) {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
lock_guard<recursive_mutex> lck(_mtxOnStatusCB);
|
|
|
|
|
_dqOnStatusCB.emplace_back(fun);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
2019-08-01 13:12:24 +08:00
|
|
|
|
private:
|
2018-10-24 15:43:52 +08:00
|
|
|
|
string _strApp;
|
|
|
|
|
string _strStream;
|
|
|
|
|
string _strTcUrl;
|
|
|
|
|
bool _bPaused = false;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2018-10-24 15:43:52 +08:00
|
|
|
|
unordered_map<int, function<void(AMFDecoder &dec)> > _mapOnResultCB;
|
|
|
|
|
recursive_mutex _mtxOnResultCB;
|
|
|
|
|
deque<function<void(AMFValue &dec)> > _dqOnStatusCB;
|
|
|
|
|
recursive_mutex _mtxOnStatusCB;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
//超时功能实现
|
2018-10-24 15:43:52 +08:00
|
|
|
|
Ticker _mediaTicker;
|
|
|
|
|
std::shared_ptr<Timer> _pMediaTimer;
|
|
|
|
|
std::shared_ptr<Timer> _pPlayTimer;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//心跳定时器
|
2018-10-24 15:43:52 +08:00
|
|
|
|
std::shared_ptr<Timer> _pBeatTimer;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
//播放进度控制
|
2018-10-26 14:12:16 +08:00
|
|
|
|
uint32_t _iSeekTo = 0;
|
|
|
|
|
uint32_t _aiFistStamp[2] = { 0, 0 };
|
|
|
|
|
uint32_t _aiNowStamp[2] = { 0, 0 };
|
2018-10-24 15:43:52 +08:00
|
|
|
|
Ticker _aNowStampTicker[2];
|
2019-09-24 11:42:27 +08:00
|
|
|
|
bool _metadata_got = false;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
};
|
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
} /* namespace mediakit */
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
#endif /* SRC_RTMP_RtmpPlayer2_H_ */
|