ZLMediaKit/src/Rtmp/RtmpSession.h

97 lines
3.0 KiB
C++
Raw Normal View History

2017-10-09 22:11:01 +08:00
/*
2020-04-04 20:30:09 +08:00
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
2017-09-27 16:20:30 +08:00
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
2020-04-04 20:30:09 +08:00
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
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 "RtmpMediaSourceImp.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"
2019-12-04 10:45:38 +08:00
#include "Common/Stamp.h"
2019-08-22 14:56:58 +08:00
2018-10-24 17:17:55 +08:00
using namespace toolkit;
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
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:
2020-03-20 11:51:24 +08:00
typedef std::shared_ptr<RtmpSession> Ptr;
RtmpSession(const Socket::Ptr &_sock);
virtual ~RtmpSession();
void onRecv(const Buffer::Ptr &pBuf) override;
void onError(const SockException &err) override;
void onManager() override;
2017-04-01 16:35:56 +08:00
private:
2020-03-20 11:51:24 +08:00
void onProcessCmd(AMFDecoder &dec);
void onCmd_connect(AMFDecoder &dec);
void onCmd_createStream(AMFDecoder &dec);
2017-04-01 16:35:56 +08:00
2020-03-20 11:51:24 +08:00
void onCmd_publish(AMFDecoder &dec);
void onCmd_deleteStream(AMFDecoder &dec);
2017-04-01 16:35:56 +08:00
2020-03-20 11:51:24 +08:00
void onCmd_play(AMFDecoder &dec);
void onCmd_play2(AMFDecoder &dec);
void doPlay(AMFDecoder &dec);
void doPlayResponse(const string &err,const std::function<void(bool)> &cb);
void sendPlayResponse(const string &err,const RtmpMediaSource::Ptr &src);
2018-10-31 12:11:14 +08:00
2020-03-20 11:51:24 +08:00
void onCmd_seek(AMFDecoder &dec);
void onCmd_pause(AMFDecoder &dec);
void setMetaData(AMFDecoder &dec);
2017-04-01 16:35:56 +08:00
2020-03-20 11:51:24 +08:00
void onSendMedia(const RtmpPacket::Ptr &pkt);
void onSendRawData(const Buffer::Ptr &buffer) override{
2018-10-24 15:43:52 +08:00
_ui64TotalBytes += buffer->size();
2020-03-20 11:51:24 +08:00
send(buffer);
}
void onRtmpChunk(RtmpPacket &chunkData) override;
2017-04-01 16:35:56 +08:00
2020-03-20 11:51:24 +08:00
template<typename first, typename second>
inline void sendReply(const char *str, const first &reply, const second &status) {
AMFEncoder invoke;
invoke << str << _dNowReqID << reply << status;
sendResponse(MSG_CMD, invoke.data());
}
2018-02-06 10:56:58 +08:00
2020-03-20 11:51:24 +08:00
//MediaSourceEvent override
bool close(MediaSource &sender,bool force) override ;
int totalReaderCount(MediaSource &sender) override;
2019-12-28 16:48:11 +08:00
2020-03-20 11:51:24 +08:00
void setSocketFlags();
string getStreamId(const string &str);
void dumpMetadata(const AMFValue &metadata);
private:
2020-03-20 11:51:24 +08:00
std::string _strTcUrl;
MediaInfo _mediaInfo;
double _dNowReqID = 0;
bool _set_meta_data = false;
Ticker _ticker;//数据接收时间
2020-04-09 16:19:03 +08:00
RtmpMediaSource::RingType::RingReader::Ptr _pRingReader;
2020-03-20 11:51:24 +08:00
std::shared_ptr<RtmpMediaSourceImp> _pPublisherSrc;
std::weak_ptr<RtmpMediaSource> _pPlayerSrc;
//时间戳修整器
Stamp _stamp[2];
//消耗的总流量
uint64_t _ui64TotalBytes = 0;
2020-04-09 16:19:03 +08:00
bool _paused = false;
2018-03-14 22:35:54 +08:00
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_RTMPSESSION_H_ */