ZLMediaKit/src/Rtmp/RtmpSession.h

106 lines
3.1 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;
2020-08-30 10:48:34 +08:00
RtmpSession(const Socket::Ptr &sock);
~RtmpSession() override;
void onRecv(const Buffer::Ptr &buf) override;
2020-03-20 11:51:24 +08:00
void onError(const SockException &err) override;
void onManager() override;
2020-08-30 10:48:34 +08:00
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{
2020-08-30 10:48:34 +08:00
_total_bytes += buffer->size();
2020-03-20 11:51:24 +08:00
send(buffer);
}
2020-08-30 10:48:34 +08:00
void onRtmpChunk(RtmpPacket &chunk_data) 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;
2020-08-30 10:48:34 +08:00
invoke << str << _recv_req_id << reply << status;
2020-03-20 11:51:24 +08:00
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);
2020-08-30 10:48:34 +08:00
private:
2020-08-30 10:48:34 +08:00
bool _paused = false;
2020-03-20 11:51:24 +08:00
bool _set_meta_data = false;
2020-08-30 10:48:34 +08:00
double _recv_req_id = 0;
//消耗的总流量
uint64_t _total_bytes = 0;
std::string _tc_url;
2020-03-20 11:51:24 +08:00
//时间戳修整器
Stamp _stamp[2];
2020-08-30 10:48:34 +08:00
//数据接收超时计时器
Ticker _ticker;
MediaInfo _media_info;
2018-03-14 22:35:54 +08:00
2020-08-30 10:48:34 +08:00
std::weak_ptr<RtmpMediaSource> _player_src;
std::shared_ptr<RtmpMediaSourceImp> _publisher_src;
RtmpMediaSource::RingType::RingReader::Ptr _ring_reader;
2017-04-01 16:35:56 +08:00
};
2020-04-29 11:25:15 +08:00
/**
* ssl加密的rtmp服务器
*/
typedef TcpSessionWithSSL<RtmpSession> RtmpSessionWithSSL;
2018-10-24 17:17:55 +08:00
} /* namespace mediakit */
2017-04-01 16:35:56 +08:00
#endif /* SRC_RTMP_RTMPSESSION_H_ */