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
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*
|
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"
|
2019-12-26 11:53:19 +08:00
|
|
|
|
#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
|
|
|
|
namespace mediakit {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
class RtmpSession : public toolkit::TcpSession, public RtmpProtocol, public MediaSourceEvent {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
public:
|
2022-02-02 20:34:50 +08:00
|
|
|
|
using Ptr = std::shared_ptr<RtmpSession>;
|
|
|
|
|
|
|
|
|
|
RtmpSession(const toolkit::Socket::Ptr &sock);
|
2020-08-30 10:48:34 +08:00
|
|
|
|
~RtmpSession() override;
|
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void onRecv(const toolkit::Buffer::Ptr &buf) override;
|
|
|
|
|
void onError(const toolkit::SockException &err) override;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
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);
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void doPlayResponse(const std::string &err,const std::function<void(bool)> &cb);
|
|
|
|
|
void sendPlayResponse(const std::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);
|
2021-08-12 16:07:31 +08:00
|
|
|
|
void onCmd_playCtrl(AMFDecoder &dec);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
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);
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void onSendRawData(toolkit::Buffer::Ptr buffer) override{
|
2020-08-30 10:48:34 +08:00
|
|
|
|
_total_bytes += buffer->size();
|
2020-11-01 03:41:35 +08:00
|
|
|
|
send(std::move(buffer));
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
2021-02-04 17:58:51 +08:00
|
|
|
|
void onRtmpChunk(RtmpPacket::Ptr 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-09-27 11:32:49 +08:00
|
|
|
|
///////MediaSourceEvent override///////
|
|
|
|
|
// 关闭
|
|
|
|
|
bool close(MediaSource &sender, bool force) override;
|
|
|
|
|
// 播放总人数
|
2020-03-20 11:51:24 +08:00
|
|
|
|
int totalReaderCount(MediaSource &sender) override;
|
2020-09-27 11:32:49 +08:00
|
|
|
|
// 获取媒体源类型
|
|
|
|
|
MediaOriginType getOriginType(MediaSource &sender) const override;
|
|
|
|
|
// 获取媒体源url或者文件路径
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::string getOriginUrl(MediaSource &sender) const override;
|
2020-09-27 11:32:49 +08:00
|
|
|
|
// 获取媒体源客户端相关信息
|
|
|
|
|
std::shared_ptr<SockInfo> getOriginSock(MediaSource &sender) const override;
|
2019-12-28 16:48:11 +08:00
|
|
|
|
|
2020-03-20 11:51:24 +08:00
|
|
|
|
void setSocketFlags();
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::string getStreamId(const std::string &str);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
void dumpMetadata(const AMFValue &metadata);
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void sendStatus(const std::initializer_list<std::string> &key_value);
|
2020-08-30 10:48:34 +08:00
|
|
|
|
|
2018-08-10 11:55:18 +08:00
|
|
|
|
private:
|
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;
|
2022-06-04 11:06:35 +08:00
|
|
|
|
//断连续推延时
|
|
|
|
|
uint32_t _continue_push_ms = 0;
|
2020-08-30 10:48:34 +08:00
|
|
|
|
//消耗的总流量
|
|
|
|
|
uint64_t _total_bytes = 0;
|
|
|
|
|
std::string _tc_url;
|
2021-12-28 17:12:52 +08:00
|
|
|
|
//推流时间戳修整器
|
2020-03-20 11:51:24 +08:00
|
|
|
|
Stamp _stamp[2];
|
2020-08-30 10:48:34 +08:00
|
|
|
|
//数据接收超时计时器
|
2022-02-02 20:34:50 +08:00
|
|
|
|
toolkit::Ticker _ticker;
|
2020-08-30 10:48:34 +08:00
|
|
|
|
MediaInfo _media_info;
|
2022-01-10 16:37:50 +08:00
|
|
|
|
std::weak_ptr<RtmpMediaSource> _play_src;
|
|
|
|
|
AMFValue _push_metadata;
|
|
|
|
|
RtmpMediaSourceImp::Ptr _push_src;
|
|
|
|
|
std::shared_ptr<void> _push_src_ownership;
|
2020-08-30 10:48:34 +08:00
|
|
|
|
RtmpMediaSource::RingType::RingReader::Ptr _ring_reader;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
};
|
|
|
|
|
|
2020-04-29 11:25:15 +08:00
|
|
|
|
/**
|
|
|
|
|
* 支持ssl加密的rtmp服务器
|
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
using RtmpSessionWithSSL = toolkit::TcpSessionWithSSL<RtmpSession>;
|
2020-04-29 11:25:15 +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_ */
|