2020-04-04 20:30:09 +08:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
|
|
|
|
#ifndef ZLMEDIAKIT_RTSPPUSHER_H
|
|
|
|
|
#define ZLMEDIAKIT_RTSPPUSHER_H
|
|
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include "RtspMediaSource.h"
|
|
|
|
|
#include "Util/util.h"
|
|
|
|
|
#include "Util/logger.h"
|
|
|
|
|
#include "Poller/Timer.h"
|
|
|
|
|
#include "Network/Socket.h"
|
|
|
|
|
#include "Network/TcpClient.h"
|
|
|
|
|
#include "RtspSplitter.h"
|
|
|
|
|
#include "Pusher/PusherBase.h"
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace toolkit;
|
|
|
|
|
|
|
|
|
|
namespace mediakit {
|
|
|
|
|
|
|
|
|
|
class RtspPusher : public TcpClient, public RtspSplitter, public PusherBase {
|
|
|
|
|
public:
|
|
|
|
|
typedef std::shared_ptr<RtspPusher> Ptr;
|
2019-04-01 10:16:15 +08:00
|
|
|
|
RtspPusher(const EventPoller::Ptr &poller,const RtspMediaSource::Ptr &src);
|
2019-03-27 18:41:52 +08:00
|
|
|
|
virtual ~RtspPusher();
|
|
|
|
|
|
|
|
|
|
void publish(const string &strUrl) override;
|
|
|
|
|
|
|
|
|
|
void teardown() override;
|
|
|
|
|
|
|
|
|
|
void setOnPublished(const Event &cb) override {
|
|
|
|
|
_onPublished = cb;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setOnShutdown(const Event & cb) override{
|
|
|
|
|
_onShutdown = cb;
|
|
|
|
|
}
|
|
|
|
|
protected:
|
|
|
|
|
//for Tcpclient override
|
|
|
|
|
void onRecv(const Buffer::Ptr &pBuf) override;
|
|
|
|
|
void onConnect(const SockException &err) override;
|
|
|
|
|
void onErr(const SockException &ex) override;
|
|
|
|
|
|
|
|
|
|
//RtspSplitter override
|
|
|
|
|
void onWholeRtspPacket(Parser &parser) override ;
|
|
|
|
|
void onRtpPacket(const char *data,uint64_t len) override {};
|
|
|
|
|
private:
|
2020-01-04 12:03:53 +08:00
|
|
|
|
void onPublishResult(const SockException &ex, bool handshakeCompleted);
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
2019-03-28 09:34:22 +08:00
|
|
|
|
void sendAnnounce();
|
|
|
|
|
void sendSetup(unsigned int uiTrackIndex);
|
|
|
|
|
void sendRecord();
|
|
|
|
|
void sendOptions();
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
|
|
|
|
void handleResAnnounce(const Parser &parser);
|
|
|
|
|
void handleResSetup(const Parser &parser, unsigned int uiTrackIndex);
|
|
|
|
|
bool handleAuthenticationFailure(const string ¶msStr);
|
|
|
|
|
|
|
|
|
|
inline int getTrackIndexByTrackType(TrackType type);
|
|
|
|
|
|
2020-04-07 13:03:53 +08:00
|
|
|
|
void sendRtpPacket(const RtspMediaSource::RingDataType & pkt) ;
|
2019-03-28 09:34:22 +08:00
|
|
|
|
void sendRtspRequest(const string &cmd, const string &url ,const StrCaseMap &header = StrCaseMap(),const string &sdp = "" );
|
|
|
|
|
void sendRtspRequest(const string &cmd, const string &url ,const std::initializer_list<string> &header,const string &sdp = "");
|
2019-07-11 12:12:33 +08:00
|
|
|
|
|
|
|
|
|
void createUdpSockIfNecessary(int track_idx);
|
2019-09-04 18:57:54 +08:00
|
|
|
|
void setSocketFlags();
|
2019-03-27 18:41:52 +08:00
|
|
|
|
private:
|
|
|
|
|
//rtsp鉴权相关
|
|
|
|
|
string _rtspMd5Nonce;
|
|
|
|
|
string _rtspRealm;
|
|
|
|
|
|
|
|
|
|
//超时功能实现
|
|
|
|
|
std::shared_ptr<Timer> _pPublishTimer;
|
|
|
|
|
//源
|
|
|
|
|
std::weak_ptr<RtspMediaSource> _pMediaSrc;
|
|
|
|
|
RtspMediaSource::RingType::RingReader::Ptr _pRtspReader;
|
|
|
|
|
//事件监听
|
|
|
|
|
Event _onShutdown;
|
|
|
|
|
Event _onPublished;
|
|
|
|
|
|
|
|
|
|
string _strUrl;
|
2019-06-28 16:48:02 +08:00
|
|
|
|
SdpParser _sdpParser;
|
2019-03-27 18:41:52 +08:00
|
|
|
|
vector<SdpTrack::Ptr> _aTrackInfo;
|
|
|
|
|
string _strSession;
|
|
|
|
|
unsigned int _uiCseq = 1;
|
|
|
|
|
string _strContentBase;
|
|
|
|
|
Rtsp::eRtpType _eType = Rtsp::RTP_TCP;
|
|
|
|
|
Socket::Ptr _apUdpSock[2];
|
|
|
|
|
function<void(const Parser&)> _onHandshake;
|
|
|
|
|
//心跳定时器
|
|
|
|
|
std::shared_ptr<Timer> _pBeatTimer;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} /* namespace mediakit */
|
|
|
|
|
#endif //ZLMEDIAKIT_RTSPPUSHER_H
|