首页 > 其他分享 >SRT库介绍

SRT库介绍

时间:2024-09-06 18:25:41浏览次数:13  
标签:const SRT int void 介绍 srt SRTSOCKET

文章目录

简介

SRT(Secure Reliable Transport,安全可靠的传输)是一种由Haivision开发的开源、低延迟、高带宽的网络流媒体传输协议。它旨在通过互联网提供安全、高质量的视频流,特别适用于直播、视频会议、监控等领域。SRT协议建立在UDT(基于UDP的数据传输协议)之上,继承了UDP速度快、开销低的优点,并增加了可靠性、安全性和低延迟等特性。

SRT协议介绍

参照 SRT协议介绍

https://blog.csdn.net/zhangdonghuirjdd/article/details/141900896

FFmpeg+SRS推拉流测试

详情参照 SRT推拉流环境搭建以及测试

https://blog.csdn.net/zhangdonghuirjdd/article/details/141951777

SRT库介绍

源码下载地址

https://github.com/Haivision/srt/ 

解压后
在这里插入图片描述

apps示例程序

其中 apps中的工具(示例程序)源码包括
在这里插入图片描述

srt-file-transmit

通过srt传输文件的工具

srt-live-transmit

是一个示例应用程序 ,一种通用的数据传输工具,用于在srt和其他程序之间传输数据。

srt-test-multiplex

示例程序 演示一个通道中发送多路流(多路流复用)

           +--                                                   --+
           |   id=1                                         id=1   |
           |  ------                                      -------  |
           |         \               ----->              /         |
           |   id=2   \  ----------------------------   /   id=2   |
port=5555 -|  --------- (   multiplexed UDP stream   ) ----------  |- port=2000
           |          /  ----------------------------   \          |
           |   id=3  /                                   \  id=3   |
           |  ------                                       ------  |
           +--                                                   --+

srt-test-relay

示例程序 流中继/转发

srt-tunnel

srt-tunnel 命令测试 作用是将srt协议用作tcp协议的隧道,文档见于doc/srt-tunnel.md

 <TCP client> --> <Tunnel: TCP->SRT> --> ...
            ....
          (远距离)
           ....
  --> <Tunnel: SRT->TCP> --> <TCP server>

docs/build

本库在各个系统下的编译说明
在这里插入图片描述

srtcore

协议栈核心代码
在这里插入图片描述

examples

一些常用方法调用例子 ,包括数据、文件的发送接受,客户端、服务器调用例子

在这里插入图片描述

编译 安装

./configure
make 

注意 如果要选择编译器 configure 需要加 “–with-compiler-prefix”则是编译器的路径

错误处理

如果出现

error: ‘for’ loop initial declarations are only allowed in C99 mode
for (size_t i = 0; i < sizeof(aad) / 4; ++i)

修改代码 变量定义放在for 循环之外
编译完成
在这里插入图片描述

API说明

初始化、回收

int srt_startup(void);
int srt_cleanup(void);

创建配置套接字

SRTSOCKET srt_socket(int af, int type, int protocol);
SRTSOCKET srt_create_socket();
int srt_bind(SRTSOCKET u, const struct sockaddr* name, int namelen);
int srt_bind_acquire(SRTSOCKET u, UDPSOCKET udpsock);
SRT_SOCKSTATUS srt_getsockstate(SRTSOCKET u);
int srt_getsndbuffer(SRTSOCKET sock, size_t* blocks, size_t* bytes);
int srt_close(SRTSOCKET u);

连接

int srt_listen(SRTSOCKET u, int backlog);
SRTSOCKET srt_accept(SRTSOCKET lsn, struct sockaddr* addr, int* addrlen);
SRTSOCKET srt_accept_bond(const SRTSOCKET listeners[], int nlisteners, int msTimeOut);
int srt_listen_callback(SRTSOCKET lsn, srt_listen_callback_fn* hook_fn, void* hook_opaque);
int srt_connect(SRTSOCKET u, const struct sockaddr* name, int namelen);
int srt_connect_bind(SRTSOCKET u, const struct sockaddr* source,
                     const struct sockaddr* target, int len);
int srt_connect_debug(SRTSOCKET u, const struct sockaddr* name, int namelen, int forced_isn);  
int srt_rendezvous(SRTSOCKET u, const struct sockaddr* local_name, int local_namelen,
        const struct sockaddr* remote_name, int remote_namelen);      
 int srt_connect_callback(SRTSOCKET u, srt_connect_callback_fn* hook_fn, void* hook_opaque);            

管理Socket Group

typedef struct SRT_SocketGroupData_
{
    SRTSOCKET id;
    struct sockaddr_storage peeraddr;
    SRT_SOCKSTATUS sockstate;
    uint16_t weight;
    SRT_MEMBERSTATUS memberstate;
    int result;
    int token;
} SRT_SOCKGROUPDATA;

SRTSOCKET srt_create_group(SRT_GROUP_TYPE type);
int srt_group_data(SRTSOCKET socketgroup, SRT_SOCKGROUPDATA output[], size_t* inoutlen);
int srt_connect_group(SRTSOCKET group,
                      SRT_SOCKGROUPCONFIG name [], int arraysize);
SRT_SOCKGROUPCONFIG srt_prepare_endpoint(const struct sockaddr* src /*nullable*/,
                                       const struct sockaddr* dst, int namelen);
SRT_SOCKOPT_CONFIG* srt_create_config();    
void srt_delete_config(SRT_SOCKOPT_CONFIG* c);
int srt_config_add(SRT_SOCKOPT_CONFIG* c, SRT_SOCKOPT opt, void* val, int len);
                                  

属性设置

int srt_getpeername(SRTSOCKET u, struct sockaddr* name, int* namelen);
int srt_getsockname(SRTSOCKET u, struct sockaddr* name, int* namelen);
int srt_getsockopt(SRTSOCKET u, int level /*ignored*/, SRT_SOCKOPT opt, void* optval, int* optlen);
int srt_getsockflag(SRTSOCKET u, SRT_SOCKOPT opt, void* optval, int* optlen);
uint32_t srt_getversion();

传输

int srt_send(SRTSOCKET u, const char* buf, int len);
int srt_sendmsg(SRTSOCKET u, const char* buf, int len, int ttl/* = -1*/, int inorder/* = false*/);
int srt_sendmsg2(SRTSOCKET u, const char* buf, int len, SRT_MSGCTRL *mctrl);

int srt_recv(SRTSOCKET u, char* buf, int len);
int srt_recvmsg(SRTSOCKET u, char* buf, int len);
int srt_recvmsg2(SRTSOCKET u, char *buf, int len, SRT_MSGCTRL *mctrl);
int64_t srt_sendfile(SRTSOCKET u, const char* path, int64_t* offset, int64_t size, int block);
int64_t srt_recvfile(SRTSOCKET u, const char* path, int64_t* offset, int64_t size, int block);

数据统计/性能跟踪

// Performance monitor with Byte counters for better bitrate estimation.
int srt_bstats(SRTSOCKET u, SRT_TRACEBSTATS * perf, int clear);

// Performance monitor with Byte counters and instantaneous stats instead of moving averages for Snd/Rcvbuffer sizes.
int srt_bistats(SRTSOCKET u, SRT_TRACEBSTATS * perf, int clear, int instantaneous);

异步操作

int srt_epoll_create(void);
int srt_epoll_add_usock(int eid, SRTSOCKET u, const int* events);
int srt_epoll_add_ssock(int eid, SYSSOCKET s, const int* events);
int srt_epoll_update_usock(int eid, SRTSOCKET u, const int* events);
int srt_epoll_update_ssock(int eid, SYSSOCKET s, const int* events);

int srt_epoll_remove_usock(int eid, SRTSOCKET u);
int srt_epoll_remove_ssock(int eid, SYSSOCKET s);

int srt_epoll_wait(int eid, SRTSOCKET* readfds, int* rnum, SRTSOCKET* writefds, int* wnum, int64_t msTimeOut,
                        SYSSOCKET* lrfds, int* lrnum, SYSSOCKET* lwfds, int* lwnum);
int srt_epoll_uwait(int eid, SRT_EPOLL_EVENT* fdsSet, int fdsSize, int64_t msTimeOut);
int srt_epoll_clear_usocks(int eid);
int32_t srt_epoll_set(int eid, int32_t flags);
int srt_epoll_release(int eid);

日志操作

void srt_setloglevel(int ll);
void srt_addlogfa(int fa);
void srt_dellogfa(int fa);
void srt_resetlogfa(const int* fara, size_t fara_size);
void srt_setloghandler(void* opaque, SRT_LOG_HANDLER_FN* handler);
typedef void SRT_LOG_HANDLER_FN(void* opaque, int level, const char* file, int line, const char* area, const char* message);
void srt_setlogflags(int flags);

时间

int64_t srt_time_now();
int64_t srt_connection_time(SRTSOCKET sock);
int srt_clock_type(void);

异常和错误码

int srt_getlasterror(int* errno_loc);
const char* srt_strerror(int code, int errnoval);
const char* srt_getlasterror_str(void);
void srt_clearlasterror(void);
const char* srt_rejectreason_str(enum SRT_REJECT_REASON id);
int srt_setrejectreason(SRTSOCKET sock, int value);
int srt_getrejectreason(SRTSOCKET sock);

常见失败原因
在这里插入图片描述

错误码
在这里插入图片描述

测试

#ifndef _WIN32
   #include <cstdlib>
   #include <netdb.h>
#else
   #include <winsock2.h>
   #include <ws2tcpip.h>
#endif
#include <fstream>
#include <iostream>
#include <string>
#include <cstring>
#include <srt.h>

using namespace std;

#ifndef _WIN32
void* sendfile(void*);
#else
DWORD WINAPI sendfile(LPVOID);
#endif

int main(int argc, char* argv[])
{
   if ((2 < argc) || ((2 == argc) && (0 == atoi(argv[1]))))
   {
      cout << "usage: sendfile [server_port]" << endl;
      return 0;
   }

   // Initialize the SRT library.
   srt_startup();

   addrinfo hints;
   addrinfo* res;

   memset(&hints, 0, sizeof(struct addrinfo));
   hints.ai_flags = AI_PASSIVE;
   hints.ai_family = AF_INET;
   hints.ai_socktype = SOCK_DGRAM;

   string service("9000");
   if (2 == argc)
      service = argv[1];

   if (0 != getaddrinfo(NULL, service.c_str(), &hints, &res))
   {
      cout << "illegal port number or port is busy.\n" << endl;
      return 0;
   }

   SRTSOCKET serv = srt_create_socket();

   // SRT requires that third argument is always SOCK_DGRAM. The Stream API is set by an option,
   // although there's also lots of other options to be set, for which there's a convenience option,
   // SRTO_TRANSTYPE.
   SRT_TRANSTYPE tt = SRTT_FILE;
   srt_setsockopt(serv, 0, SRTO_TRANSTYPE, &tt, sizeof tt);

   // Windows UDP issue
   // For better performance, modify HKLM\System\CurrentControlSet\Services\Afd\Parameters\FastSendDatagramThreshold
#ifdef _WIN32
   int mss = 1052;
   srt_setsockopt(serv, 0, SRTO_MSS, &mss, sizeof(int));
#endif

   if (SRT_ERROR == srt_bind(serv, res->ai_addr, res->ai_addrlen))
   {
      cout << "bind: " << srt_getlasterror_str() << endl;
      return 0;
   }

   freeaddrinfo(res);

   cout << "server is ready at port: " << service << endl;
   srt_listen(serv, 10);

   sockaddr_storage clientaddr;
   int addrlen = sizeof(clientaddr);

   SRTSOCKET fhandle;

   // Accept multiple client connections.
   while (true)
   {
      if (SRT_INVALID_SOCK == (fhandle = srt_accept(serv, (sockaddr*)&clientaddr, &addrlen)))
      {
         cout << "accept: " << srt_getlasterror_str() << endl;
         return 0;
      }

      char clienthost[NI_MAXHOST];
      char clientservice[NI_MAXSERV];
      getnameinfo((sockaddr *)&clientaddr, addrlen, clienthost, sizeof(clienthost), clientservice, sizeof(clientservice), NI_NUMERICHOST|NI_NUMERICSERV);
      cout << "new connection: " << clienthost << ":" << clientservice << endl;

#ifndef _WIN32
      pthread_t filethread;
      pthread_create(&filethread, NULL, sendfile, new SRTSOCKET(fhandle));
      pthread_detach(filethread);
#else
      CreateThread(NULL, 0, sendfile, new SRTSOCKET(fhandle), 0, NULL);
#endif
   }

   srt_close(serv);

   // Signal to the SRT library to clean up all allocated sockets and resources.
   srt_cleanup();

   return 0;
}

#ifndef _WIN32
void* sendfile(void* usocket)
#else
DWORD WINAPI sendfile(LPVOID usocket)
#endif
{
   SRTSOCKET fhandle = *(SRTSOCKET*)usocket;
   delete (SRTSOCKET*)usocket;

   // Acquiring file name information from client.
   char file[1024];
   int len;

   if (SRT_ERROR == srt_recv(fhandle, (char*)&len, sizeof(int)))
   {
      cout << "recv: " << srt_getlasterror_str() << endl;
      return 0;
   }

   if (SRT_ERROR == srt_recv(fhandle, file, len))
   {
      cout << "recv: " << srt_getlasterror_str() << endl;
      return 0;
   }
   file[len] = '\0';

   // Open the file only to know its size.
   fstream ifs(file, ios::in | ios::binary);
   ifs.seekg(0, ios::end);
   const int64_t size = ifs.tellg();
   ifs.close();

   // Send file size.
   if (SRT_ERROR == srt_send(fhandle, (char*)&size, sizeof(int64_t)))
   {
      cout << "send: " << srt_getlasterror_str() << endl;
      return 0;
   }

   SRT_TRACEBSTATS trace;
   srt_bstats(fhandle, &trace, true);

   // Send the file itself.
   int64_t offset = 0;
   if (SRT_ERROR == srt_sendfile(fhandle, file, &offset, size, SRT_DEFAULT_SENDFILE_BLOCK))
   {
      cout << "sendfile: " << srt_getlasterror_str() << endl;
      return 0;
   }

   srt_bstats(fhandle, &trace, true);
   cout << "speed = " << trace.mbpsSendRate << "Mbits/sec" << endl;
   const int64_t losspercent = 100 * trace.pktSndLossTotal / trace.pktSent;
   cout << "network loss = " << trace.pktSndLossTotal << "pkts (" << losspercent << "%)\n";

   srt_close(fhandle);

   #ifndef _WIN32
      return NULL;
   #else
      return 0;
   #endif
}

标签:const,SRT,int,void,介绍,srt,SRTSOCKET
From: https://blog.csdn.net/zhangdonghuirjdd/article/details/141964067

相关文章

  • ARP详细介绍
    ARP详细介绍地址解析协议,根据IP地址获取MAC地址,还能检测地址是否有重复动态ARP广播ARP报文寻找目标IP的MAC地址主机1通过发送ARPRequest报文获取主机2的MAC地址​ 由于不知道目的MAC地址,因此ARPRequest报文內目的MAC地址为0ARPRequest是广播数据帧,因此交换机收到后,......
  • 6、显卡品牌分类介绍:微星 - 计算机硬件品牌系列文章
    微星科技是一家知名的电脑硬件制造商,‌特别是在显卡和主板领域有着显著的影响力。‌该公司成立于1986年8月,‌总部位于上海市延安西路889号太平洋中心三楼。‌微星科技早期以主板、‌显卡为主要产品,‌与技嘉、‌华硕等齐名,‌为主板三大厂之一。‌近年来,‌微星向电竞个人电脑市......
  • 有关API接口的介绍及简单的API接口代码示例
    在当今数字化时代,应用程序不再孤立存在。它们通过API(应用程序编程接口)相互连接,共享数据和功能。API是现代软件开发中不可或缺的一部分,它允许不同的软件系统相互通信,实现数据的交换和业务逻辑的集成。一、API接口的基本概念API接口是一种预定义的函数或协议,它允许不同的软件应用......
  • Linux内核源码介绍
    建议点击这里查看个人主页上的最新原文作者:陈孝松主页:chenxiaosong.com哔哩哔哩:陈孝松课程:chenxiaosong.com/courses博客:chenxiaosong.com/blog贡献:chenxiaosong.com/contributions邮箱:[email protected]交流群:544216206,点击查看群介绍点......
  • Flink学习-作业调度介绍
    Flink官网地址:作业调度|ApacheFlink作业调度 #这篇文档简要描述了Flink怎样调度作业,怎样在JobManager里描述和追踪作业状态调度 #Flink通过 TaskSlots 来定义执行资源。每个TaskManager有一到多个taskslot,每个taskslot可以运行一条由多个并行task......
  • Java:集合的相关汇总介绍
    主要包含Set(集)、List(列表包含Queue)和Map(映射)。1、Collection:Collection是集合List、Set、Queue的最基本的接口。2、Iterator:迭代器,可以通过迭代器遍历集合中的数据。3、Map:是映射表的基础接口。一、List接口Java的List是非常常用的数据类型。List是有......
  • Mysql高级篇(上)—— Mysql架构介绍(二)
    Mysql高级篇(上)MySQL架构介绍(二)逻辑架构逻辑架构剖析MySQL8.0中SQL执行流程Linux环境下MySQL8.0中SQL执行原理MySQL语法顺序Oracle中SQL执行流程(了解)数据库缓存池bufferpool(了解)存储引擎常见存储引擎简述InnoDB和MyISAM两种存储引擎的特点对比MySQL架构介绍(二)......
  • 【AIGC】Transformer模型:Postion Embedding概述、应用场景和实现方式的详细介绍。
    ......
  • linux curl命令介绍以及使用
    文章目录`curl`简介`curl`的安装基本用法发送GET请求将响应内容保存到文件显示请求的头部信息发送POST请求上传文件携带请求头处理重定向通过代理发送请求下载文件指定请求的超时时间高级用法模拟浏览器行为保持会话(Cookie)验证HTTPS请求总结在Linux中,curl是一个......
  • k8s介绍
    ⼀、编排分类单机容器编排:docker-compose容器集群编排:dockerswarm、mesos+marathon、kubernetes应⽤编排:ansible(模块,剧本,⻆⾊)⼆、系统管理进化史1.传统部署时代早期,各个组织是在物理服务器上运⾏应⽤程序。由于⽆法限制在物理服务器中运⾏的应⽤程序资源使⽤,......