文章目录
简介
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