ZLMediaKit/srt/SrtTransport.hpp

173 lines
5.6 KiB
C++
Raw Normal View History

2022-06-03 13:25:32 +08:00
#ifndef ZLMEDIAKIT_SRT_TRANSPORT_H
#define ZLMEDIAKIT_SRT_TRANSPORT_H
2022-06-07 09:52:20 +08:00
#include <atomic>
2022-06-03 13:25:32 +08:00
#include <chrono>
#include <memory>
2022-06-07 09:52:20 +08:00
#include <mutex>
2022-06-03 13:25:32 +08:00
#include "Network/Session.h"
#include "Poller/EventPoller.h"
#include "Poller/Timer.h"
2022-06-03 13:25:32 +08:00
#include "Common.hpp"
#include "NackContext.hpp"
2022-06-03 13:25:32 +08:00
#include "Packet.hpp"
#include "PacketQueue.hpp"
#include "PacketSendQueue.hpp"
2022-06-03 13:25:32 +08:00
#include "Statistic.hpp"
namespace SRT {
2022-06-07 09:52:20 +08:00
2022-06-03 13:25:32 +08:00
using namespace toolkit;
extern const std::string kPort;
extern const std::string kTimeOutSec;
2022-06-07 09:52:20 +08:00
extern const std::string kLatencyMul;
2022-06-11 22:51:11 +08:00
extern const std::string kPktBufSize;
2022-06-03 13:25:32 +08:00
class SrtTransport : public std::enable_shared_from_this<SrtTransport> {
public:
friend class SrtSession;
using Ptr = std::shared_ptr<SrtTransport>;
SrtTransport(const EventPoller::Ptr &poller);
virtual ~SrtTransport();
const EventPoller::Ptr &getPoller() const;
void setSession(Session::Ptr session);
const Session::Ptr &getSession() const;
2022-06-07 09:52:20 +08:00
2022-06-03 13:25:32 +08:00
/**
* socket收到udp数据
* @param buf
* @param len
* @param addr
*/
2022-06-03 21:17:01 +08:00
virtual void inputSockData(uint8_t *buf, int len, struct sockaddr_storage *addr);
2022-06-04 11:23:18 +08:00
virtual void onSendTSData(const Buffer::Ptr &buffer, bool flush);
2022-06-03 13:25:32 +08:00
std::string getIdentifier();
void unregisterSelf();
2022-06-07 09:52:20 +08:00
void unregisterSelfHandshake();
2022-06-03 13:25:32 +08:00
protected:
2022-06-07 09:52:20 +08:00
virtual bool isPusher() { return true; };
virtual void onSRTData(DataPacket::Ptr pkt) {};
2022-06-03 13:25:32 +08:00
virtual void onShutdown(const SockException &ex);
2022-06-07 09:52:20 +08:00
virtual void onHandShakeFinished(std::string &streamid, struct sockaddr_storage *addr) {};
virtual void sendPacket(Buffer::Ptr pkt, bool flush = true);
virtual int getLatencyMul() { return 4; };
2022-06-11 22:51:11 +08:00
virtual int getPktBufSize() { return 8192; };
2022-06-03 13:25:32 +08:00
private:
void registerSelf();
2022-06-07 09:52:20 +08:00
void registerSelfHandshake();
2022-06-03 13:25:32 +08:00
2022-06-07 09:52:20 +08:00
void switchToOtherTransport(uint8_t *buf, int len, uint32_t socketid, struct sockaddr_storage *addr);
2022-06-03 13:25:32 +08:00
void handleHandshake(uint8_t *buf, int len, struct sockaddr_storage *addr);
2022-06-07 09:52:20 +08:00
void handleHandshakeInduction(HandshakePacket &pkt, struct sockaddr_storage *addr);
void handleHandshakeConclusion(HandshakePacket &pkt, struct sockaddr_storage *addr);
2022-06-03 13:25:32 +08:00
void handleKeeplive(uint8_t *buf, int len, struct sockaddr_storage *addr);
void handleACK(uint8_t *buf, int len, struct sockaddr_storage *addr);
void handleACKACK(uint8_t *buf, int len, struct sockaddr_storage *addr);
void handleNAK(uint8_t *buf, int len, struct sockaddr_storage *addr);
void handleCongestionWarning(uint8_t *buf, int len, struct sockaddr_storage *addr);
void handleShutDown(uint8_t *buf, int len, struct sockaddr_storage *addr);
void handleDropReq(uint8_t *buf, int len, struct sockaddr_storage *addr);
void handleUserDefinedType(uint8_t *buf, int len, struct sockaddr_storage *addr);
void handlePeerError(uint8_t *buf, int len, struct sockaddr_storage *addr);
void handleDataPacket(uint8_t *buf, int len, struct sockaddr_storage *addr);
2022-06-07 09:52:20 +08:00
void sendNAKPacket(std::list<PacketQueue::LostPair> &lost_list);
2022-06-03 13:25:32 +08:00
void sendACKPacket();
void sendLightACKPacket();
void sendKeepLivePacket();
2022-06-03 21:17:01 +08:00
void sendShutDown();
2022-06-07 09:52:20 +08:00
void sendMsgDropReq(uint32_t first, uint32_t last);
size_t getPayloadSize();
2022-06-07 09:52:20 +08:00
2022-08-27 12:46:35 +08:00
bool isSameCon(HandshakePacket &pkt);
2022-06-03 13:25:32 +08:00
protected:
2022-06-07 09:52:20 +08:00
void sendDataPacket(DataPacket::Ptr pkt, char *buf, int len, bool flush = false);
void sendControlPacket(ControlPacket::Ptr pkt, bool flush = true);
2022-06-03 13:25:32 +08:00
private:
// 当前选中的udp链接
2022-06-03 13:25:32 +08:00
Session::Ptr _selected_session;
// 链接迁移前后使用过的udp链接
2022-06-07 09:52:20 +08:00
std::unordered_map<Session *, std::weak_ptr<Session>> _history_sessions;
2022-06-03 13:25:32 +08:00
EventPoller::Ptr _poller;
uint32_t _peer_socket_id;
uint32_t _socket_id = 0;
TimePoint _now;
2022-06-03 13:25:32 +08:00
TimePoint _start_timestamp;
// for calculate rtt for delay
TimePoint _induction_ts;
2022-06-03 13:25:32 +08:00
uint32_t _mtu = 1500;
uint32_t _max_window_size = 8192;
2022-06-07 09:52:20 +08:00
uint32_t _init_seq_number = 0;
2022-06-03 13:25:32 +08:00
std::string _stream_id;
uint32_t _sync_cookie = 0;
uint32_t _send_packet_seq_number = 0;
uint32_t _send_msg_number = 1;
2022-06-03 13:25:32 +08:00
PacketSendQueue::Ptr _send_buf;
uint32_t _buf_delay = 120;
2022-06-12 21:11:50 +08:00
PacketQueueInterface::Ptr _recv_buf;
2022-06-11 23:04:24 +08:00
// NackContext _recv_nack;
2022-06-07 09:52:20 +08:00
uint32_t _rtt = 100 * 1000;
uint32_t _rtt_variance = 50 * 1000;
2022-06-03 13:25:32 +08:00
uint32_t _light_ack_pkt_count = 0;
uint32_t _ack_number_count = 0;
2022-06-03 17:49:43 +08:00
uint32_t _last_ack_pkt_seq_num = 0;
uint32_t _last_pkt_seq = 0;
UTicker _ack_ticker;
2022-06-07 09:52:20 +08:00
std::map<uint32_t, TimePoint> _ack_send_timestamp;
2022-06-03 13:25:32 +08:00
std::shared_ptr<PacketRecvRateContext> _pkt_recv_rate_context;
std::shared_ptr<EstimatedLinkCapacityContext> _estimated_link_capacity_context;
std::shared_ptr<RecvRateContext> _recv_rate_context;
2022-06-03 13:25:32 +08:00
UTicker _nak_ticker;
2022-06-03 13:25:32 +08:00
// 保持发送的握手消息,防止丢失重发
2022-06-03 13:25:32 +08:00
HandshakePacket::Ptr _handleshake_res;
ResourcePool<BufferRaw> _packet_pool;
};
class SrtTransportManager {
public:
static SrtTransportManager &Instance();
SrtTransport::Ptr getItem(const std::string &key);
void addItem(const std::string &key, const SrtTransport::Ptr &ptr);
void removeItem(const std::string &key);
void addHandshakeItem(const std::string &key, const SrtTransport::Ptr &ptr);
void removeHandshakeItem(const std::string &key);
SrtTransport::Ptr getHandshakeItem(const std::string &key);
2022-06-07 09:52:20 +08:00
2022-06-03 13:25:32 +08:00
private:
SrtTransportManager() = default;
private:
std::mutex _mtx;
std::unordered_map<std::string, std::weak_ptr<SrtTransport>> _map;
std::mutex _handshake_mtx;
std::unordered_map<std::string, std::weak_ptr<SrtTransport>> _handshake_map;
};
} // namespace SRT
#endif // ZLMEDIAKIT_SRT_TRANSPORT_H