srt-master下sendfile

本文档通过sendfile和recvfile演示了srt如何进行文件的发送和接收。首先启动发送服务,然后建立接收服务,接收完成后将在接收端保存文件。sendfile和recvfile分别调用了srt的核心APIs,如srt_sendfile和srt_recvfile,实现了文件的传输。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

根据srt-master提供文件收发Demo(sendfile,recvfile)

1.构建发送服务

./sendfile

username@Ubuntu:~/srt/srt-master$ ./sendfile
server is ready at port: 9000

2.构建接收服务

sample-usage: recvfile server_ip server_port remote_filename local_filename

./recvfile 4.25.10.131 9000 output.ts output-tes.ts

3.接收完成流程

username@username-VirtualBox:~/workspace/srt-master$ ./recvfile 4.25.10.131 9000 output.ts output-tes.ts
09:00:41.797566/recvfile.N: SRT.c: Connection established to: 4.25.10.131:9000
speed = 0.0100161Mbits/sec
loss = 0pkt (0%)

 

接收完成后会在接收方存储一份output-tes.ts文件,此Demo主要实现srt的接收和发送流程,简单的调用srt-api实现。

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

   // aquiring 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 check the size)
   fstream ifs(file, ios::in | ios::binary);

   ifs.seekg(0, ios::end);
   int64_t size = ifs.tellg();
   //ifs.seekg(0, ios::beg);
   ifs.close();

   // send file size information
   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
   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;
   int losspercent = 100*trace.pktSndLossTotal/trace.pktSent;
   cout << "loss = " << trace.pktSndLossTotal << "pkt (" << losspercent << "%)\n";

   srt_close(fhandle);

   //ifs.close();

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

发送文件主要调用srtcore,srt_c_api.c中的srt_sendfile接口

接收文件主要调用srtcore,srt_c_api.c中的srt_recvfile接口

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值