ZLMediaKit/src/Rtmp/RtmpSession.h

118 lines
3.9 KiB
C
Raw Normal View History

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
*
2017-09-27 16:20:30 +08:00
* 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.
2017-04-01 16:35:56 +08:00
*/
#ifndef SRC_RTMP_RTMPSESSION_H_
#define SRC_RTMP_RTMPSESSION_H_
#include <unordered_map>
#include "amf.h"
#include "Rtmp.h"
#include "utils.h"
2017-05-02 17:15:12 +08:00
#include "Common/config.h"
2017-04-01 16:35:56 +08:00
#include "RtmpProtocol.h"
#include "RtmpToRtspMediaSource.h"
2017-04-25 11:35:41 +08:00
#include "Util/util.h"
#include "Util/TimeTicker.h"
2018-02-23 15:36:51 +08:00
#include "Network/TcpSession.h"
2017-04-01 16:35:56 +08:00
using namespace ZL::Util;
using namespace ZL::Network;
namespace ZL {
namespace Rtmp {
2018-02-23 15:36:51 +08:00
class RtmpSession: public TcpSession ,public RtmpProtocol , public MediaSourceEvent{
2017-04-01 16:35:56 +08:00
public:
typedef std::shared_ptr<RtmpSession> Ptr;
RtmpSession(const std::shared_ptr<ThreadPool> &_th, const Socket::Ptr &_sock);
virtual ~RtmpSession();
2018-02-23 15:36:51 +08:00
void onRecv(const Buffer::Ptr &pBuf) override;
2017-04-01 16:35:56 +08:00
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);
2017-05-18 16:24:36 +08:00
void onCmd_play2(AMFDecoder &dec);
void doPlay(AMFDecoder &dec);
void doPlayResponse(const string &err,bool tryDelay,const std::shared_ptr<onceToken> &token);
2017-04-01 16:35:56 +08:00
void onCmd_seek(AMFDecoder &dec);
void onCmd_pause(AMFDecoder &dec);
void setMetaData(AMFDecoder &dec);
2017-12-04 23:55:09 +08:00
void onSendMedia(const RtmpPacket::Ptr &pkt);
2017-04-01 16:35:56 +08:00
void onSendRawData(const char *pcRawData,int iSize) override{
m_ui64TotalBytes += iSize;
2017-04-01 16:35:56 +08:00
send(pcRawData, iSize);
}
2018-02-23 15:36:51 +08:00
void onSendRawData(const Buffer::Ptr &buffer,int flags) override{
m_ui64TotalBytes += buffer->size();
2018-02-23 15:36:51 +08:00
_sock->send(buffer,flags);
2017-12-04 23:55:09 +08:00
}
2017-04-01 16:35:56 +08:00
void onRtmpChunk(RtmpPacket &chunkData) override;
template<typename first, typename second>
inline void sendReply(const char *str, const first &reply, const second &status) {
AMFEncoder invoke;
invoke << str << m_dNowReqID << reply << status;
sendResponse(MSG_CMD, invoke.data());
}
2018-02-06 10:56:58 +08:00
bool shutDown() override {
InfoL << "kick out:" << m_mediaInfo.m_vhost << " " << m_mediaInfo.m_app << " " << m_mediaInfo.m_streamid;
safeShutdown();
return true;
}
2018-03-14 22:35:54 +08:00
void doDelay(int delaySec,const std::function<void()> &fun);
void cancelDelyaTask();
private:
std::string m_strTcUrl;
MediaInfo m_mediaInfo;
double m_dNowReqID = 0;
Ticker m_ticker;//数据接收时间
SmoothTicker m_stampTicker[2];//时间戳生产器
RingBuffer<RtmpPacket::Ptr>::RingReader::Ptr m_pRingReader;
std::shared_ptr<RtmpMediaSource> m_pPublisherSrc;
std::weak_ptr<RtmpMediaSource> m_pPlayerSrc;
uint32_t m_aui32FirstStamp[2] = {0};
//消耗的总流量
uint64_t m_ui64TotalBytes = 0;
2018-03-14 22:35:54 +08:00
std::function<void()> m_delayTask;
uint32_t m_iTaskTimeLine = 0;
2017-04-01 16:35:56 +08:00
};
} /* namespace Rtmp */
} /* namespace ZL */
#endif /* SRC_RTMP_RTMPSESSION_H_ */