2020-04-04 20:30:09 +08:00
|
|
|
|
/*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
|
2020-04-04 20:30:09 +08:00
|
|
|
|
*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
|
2020-04-04 20:30:09 +08:00
|
|
|
|
*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* Use of this source code is governed by MIT-like license that can be found in the
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* 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 "Poller/Timer.h"
|
|
|
|
|
#include "Network/Socket.h"
|
|
|
|
|
#include "Network/TcpClient.h"
|
|
|
|
|
#include "RtspSplitter.h"
|
|
|
|
|
#include "Pusher/PusherBase.h"
|
2022-12-02 14:43:06 +08:00
|
|
|
|
#include "Rtcp/RtcpContext.h"
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
|
|
|
|
namespace mediakit {
|
2022-12-02 14:43:06 +08:00
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
class RtspPusher : public toolkit::TcpClient, public RtspSplitter, public PusherBase {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
public:
|
2022-12-02 14:43:06 +08:00
|
|
|
|
using Ptr = std::shared_ptr<RtspPusher>;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
RtspPusher(const toolkit::EventPoller::Ptr &poller,const RtspMediaSource::Ptr &src);
|
2020-08-30 11:40:03 +08:00
|
|
|
|
~RtspPusher() override;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void publish(const std::string &url) override;
|
2019-03-27 18:41:52 +08:00
|
|
|
|
void teardown() override;
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
//for Tcpclient override
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void onRecv(const toolkit::Buffer::Ptr &buf) override;
|
|
|
|
|
void onConnect(const toolkit::SockException &err) override;
|
2023-04-28 22:03:16 +08:00
|
|
|
|
void onError(const toolkit::SockException &ex) override;
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
|
|
|
|
//RtspSplitter override
|
|
|
|
|
void onWholeRtspPacket(Parser &parser) override ;
|
2021-01-31 19:18:17 +08:00
|
|
|
|
void onRtpPacket(const char *data,size_t len) override;
|
|
|
|
|
|
|
|
|
|
virtual void onRtcpPacket(int track_idx, SdpTrack::Ptr &track, uint8_t *data, size_t len);
|
2020-08-30 11:40:03 +08:00
|
|
|
|
|
2019-03-27 18:41:52 +08:00
|
|
|
|
private:
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void onPublishResult_l(const toolkit::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);
|
2022-02-02 20:34:50 +08:00
|
|
|
|
bool handleAuthenticationFailure(const std::string ¶ms_str);
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
2021-01-31 19:18:17 +08:00
|
|
|
|
int getTrackIndexByInterleaved(int interleaved) const;
|
|
|
|
|
int getTrackIndexByTrackType(TrackType type) const;
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
2020-04-07 13:03:53 +08:00
|
|
|
|
void sendRtpPacket(const RtspMediaSource::RingDataType & pkt) ;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void sendRtspRequest(const std::string &cmd, const std::string &url ,const StrCaseMap &header = StrCaseMap(),const std::string &sdp = "" );
|
|
|
|
|
void sendRtspRequest(const std::string &cmd, const std::string &url ,const std::initializer_list<std::string> &header,const std::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();
|
2021-01-31 19:18:17 +08:00
|
|
|
|
void updateRtcpContext(const RtpPacket::Ptr &pkt);
|
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鉴权相关
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::string _nonce;
|
|
|
|
|
std::string _realm;
|
|
|
|
|
std::string _url;
|
|
|
|
|
std::string _session_id;
|
|
|
|
|
std::string _content_base;
|
2020-08-30 11:40:03 +08:00
|
|
|
|
SdpParser _sdp_parser;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::vector<SdpTrack::Ptr> _track_vec;
|
2021-01-31 19:18:17 +08:00
|
|
|
|
//RTP端口,trackid idx 为数组下标
|
2022-02-02 20:34:50 +08:00
|
|
|
|
toolkit::Socket::Ptr _rtp_sock[2];
|
2021-01-31 19:18:17 +08:00
|
|
|
|
//RTCP端口,trackid idx 为数组下标
|
2022-02-02 20:34:50 +08:00
|
|
|
|
toolkit::Socket::Ptr _rtcp_sock[2];
|
2019-03-27 18:41:52 +08:00
|
|
|
|
//超时功能实现
|
2022-12-02 14:43:06 +08:00
|
|
|
|
toolkit::Timer::Ptr _publish_timer;
|
2019-03-27 18:41:52 +08:00
|
|
|
|
//心跳定时器
|
2022-12-02 14:43:06 +08:00
|
|
|
|
toolkit::Timer::Ptr _beat_timer;
|
2020-08-30 11:40:03 +08:00
|
|
|
|
std::weak_ptr<RtspMediaSource> _push_src;
|
|
|
|
|
RtspMediaSource::RingType::RingReader::Ptr _rtsp_reader;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::function<void(const Parser&)> _on_res_func;
|
2021-01-31 19:18:17 +08:00
|
|
|
|
////////// rtcp ////////////////
|
|
|
|
|
//rtcp发送时间,trackid idx 为数组下标
|
2022-02-02 20:34:50 +08:00
|
|
|
|
toolkit::Ticker _rtcp_send_ticker[2];
|
2021-01-31 19:18:17 +08:00
|
|
|
|
//统计rtp并发送rtcp
|
2022-12-02 14:43:06 +08:00
|
|
|
|
std::vector<RtcpContext::Ptr> _rtcp_context;
|
2019-03-27 18:41:52 +08:00
|
|
|
|
};
|
|
|
|
|
|
2021-11-09 16:46:38 +08:00
|
|
|
|
using RtspPusherImp = PusherImp<RtspPusher, PusherBase>;
|
|
|
|
|
|
2019-03-27 18:41:52 +08:00
|
|
|
|
} /* namespace mediakit */
|
|
|
|
|
#endif //ZLMEDIAKIT_RTSPPUSHER_H
|