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);
|
2020-08-30 11:40:03 +08:00
|
|
|
|
~RtspPusher() override;
|
|
|
|
|
void publish(const string &url) override;
|
2019-03-27 18:41:52 +08:00
|
|
|
|
void teardown() override;
|
|
|
|
|
|
|
|
|
|
void setOnPublished(const Event &cb) override {
|
2020-08-30 11:40:03 +08:00
|
|
|
|
_on_published = cb;
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setOnShutdown(const Event & cb) override{
|
2020-08-30 11:40:03 +08:00
|
|
|
|
_on_shutdown = cb;
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
2020-08-30 11:40:03 +08:00
|
|
|
|
|
2019-03-27 18:41:52 +08:00
|
|
|
|
protected:
|
|
|
|
|
//for Tcpclient override
|
2020-08-30 11:40:03 +08:00
|
|
|
|
void onRecv(const Buffer::Ptr &buf) override;
|
2019-03-27 18:41:52 +08:00
|
|
|
|
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 {};
|
2020-08-30 11:40:03 +08:00
|
|
|
|
|
2019-03-27 18:41:52 +08:00
|
|
|
|
private:
|
2020-08-30 11:40:03 +08:00
|
|
|
|
void onPublishResult(const SockException &ex, bool handshake_done);
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
2019-03-28 09:34:22 +08:00
|
|
|
|
void sendAnnounce();
|
2020-08-30 11:40:03 +08:00
|
|
|
|
void sendSetup(unsigned int track_idx);
|
2019-03-28 09:34:22 +08:00
|
|
|
|
void sendRecord();
|
|
|
|
|
void sendOptions();
|
2021-01-17 10:28:06 +08:00
|
|
|
|
void sendTeardown();
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
|
|
|
|
void handleResAnnounce(const Parser &parser);
|
2020-08-30 11:40:03 +08:00
|
|
|
|
void handleResSetup(const Parser &parser, unsigned int track_idx);
|
|
|
|
|
bool handleAuthenticationFailure(const string ¶ms_str);
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
|
|
|
|
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();
|
2020-08-30 11:40:03 +08:00
|
|
|
|
|
2019-03-27 18:41:52 +08:00
|
|
|
|
private:
|
2020-08-30 11:40:03 +08:00
|
|
|
|
unsigned int _cseq = 1;
|
|
|
|
|
Rtsp::eRtpType _rtp_type = Rtsp::RTP_TCP;
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
2020-08-30 11:40:03 +08:00
|
|
|
|
//rtsp鉴权相关
|
|
|
|
|
string _nonce;
|
|
|
|
|
string _realm;
|
|
|
|
|
string _url;
|
|
|
|
|
string _session_id;
|
|
|
|
|
string _content_base;
|
|
|
|
|
SdpParser _sdp_parser;
|
|
|
|
|
vector<SdpTrack::Ptr> _track_vec;
|
|
|
|
|
Socket::Ptr _udp_socks[2];
|
2019-03-27 18:41:52 +08:00
|
|
|
|
//超时功能实现
|
2020-08-30 11:40:03 +08:00
|
|
|
|
std::shared_ptr<Timer> _publish_timer;
|
2019-03-27 18:41:52 +08:00
|
|
|
|
//心跳定时器
|
2020-08-30 11:40:03 +08:00
|
|
|
|
std::shared_ptr<Timer> _beat_timer;
|
|
|
|
|
std::weak_ptr<RtspMediaSource> _push_src;
|
|
|
|
|
RtspMediaSource::RingType::RingReader::Ptr _rtsp_reader;
|
|
|
|
|
//事件监听
|
|
|
|
|
Event _on_shutdown;
|
|
|
|
|
Event _on_published;
|
|
|
|
|
function<void(const Parser&)> _on_res_func;
|
2019-03-27 18:41:52 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} /* namespace mediakit */
|
|
|
|
|
#endif //ZLMEDIAKIT_RTSPPUSHER_H
|