ZLMediaKit/src/Rtmp/RtmpSession.h

108 lines
3.5 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
*
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_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"
2019-08-22 14:56:58 +08:00
#include "MediaFile/Stamp.h"
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:
typedef std::shared_ptr<RtmpSession> Ptr;
2018-12-20 10:42:51 +08:00
RtmpSession(const Socket::Ptr &_sock);
2017-04-01 16:35:56 +08:00
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);
2018-10-31 12:11:14 +08:00
void doPlayResponse(const string &err,const std::function<void(bool)> &cb);
void sendPlayResponse(const string &err,const RtmpMediaSource::Ptr &src);
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);
void onSendRawData(const Buffer::Ptr &buffer) override{
2018-10-24 15:43:52 +08:00
_ui64TotalBytes += buffer->size();
send(buffer);
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;
2018-10-24 15:43:52 +08:00
invoke << str << _dNowReqID << reply << status;
2017-04-01 16:35:56 +08:00
sendResponse(MSG_CMD, invoke.data());
}
2018-02-06 10:56:58 +08:00
2019-05-31 15:40:55 +08:00
bool close(MediaSource &sender,bool force) override ;
void onNoneReader(MediaSource &sender) override;
void setSocketFlags();
2019-09-06 18:30:42 +08:00
string getStreamId(const string &str);
private:
2018-10-24 15:43:52 +08:00
std::string _strTcUrl;
MediaInfo _mediaInfo;
double _dNowReqID = 0;
Ticker _ticker;//数据接收时间
RingBuffer<RtmpPacket::Ptr>::RingReader::Ptr _pRingReader;
std::shared_ptr<RtmpToRtspMediaSource> _pPublisherSrc;
2018-10-24 15:43:52 +08:00
std::weak_ptr<RtmpMediaSource> _pPlayerSrc;
2019-08-22 14:56:58 +08:00
//时间戳修整器
Stamp _stamp[2];
//消耗的总流量
2018-10-24 15:43:52 +08:00
uint64_t _ui64TotalBytes = 0;
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_ */