SRT a simple client and server

1.Client

        It supports pushing SRT streams after receiving UDP data streams, and also supports pulling SRT streams from the server. It also supports testing of SRT session mode. The project relies on msprotocol: a lightweight network protocol that is easy to extend and easy to use. It can be applied to X86 and ARM64 embedded devices, and currently supports file, hls, http, rtmp, msav, raw, rtp, rtsp, tcp, udp, udplite, srt and other protocols;

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <err.h>
#include <libmscommon/mscommon.h>
#include <libmscommon/msnetwork.h>
#include <libmscommon/msepoll.h>
#include <libmscommon/mstime.h>
#include <libmscommon/msmd.h>
#include <libmscommon/msenv.h>
#include <libmscommon/msmem.h>
#include <libmslog/mslog.h>
#include <libmsprotocol/msprotocol.h>

#define FLAG "PTC_CLENT"

#define mshostname_ipaddr	"192.168.200.244"
#define mshostname_port	50000
#define mshostname_url_http	"http://127.0.0.1:8010/ch31.ts"
#define mshostname_url_rtsp	"rtsp://127.0.0.1:8010/ch31.ts"

int main(int argc, char **argv)
{
	mslog_api_init( 53, ms_null,ms_null, ms_null);
	if(argc<2){
		ms_error("Userage:%s url" ,argv[0]);
		return 0;
	}
	
	msthread_api_setName("ptc_client");
	msmem_api_init(ms_false);
	msptc_api_init();
	msptc_api_register();
	msmd_api_setUlimit();
	
	ms_debug("Get param");
	mslog_api_init( 53, "/home/sugao","client.log", ms_null);

	URLContext cilent_ctt;
	URLContext *pcilent_ctt=&cilent_ctt;
	ms_memset0(pcilent_ctt, sizeof(URLContext));
	ms_strcpy(pcilent_ctt->url,(argv[1]));
//Get msptl_in	
	msptc_api_urlSplit_urlctt(pcilent_ctt);
	URLProtocol *msptl_in=msptc_api_match(pcilent_ctt->url_info.protocol_name);
	ms_debug("url_open");
	if(ms_strncmp_saeq(msptl_in->name, PTLNAME_WEBSOCKECT)){
		pcilent_ctt->urlctt_opt.ws_opt.dataType=WDT_TXTDATA;
	}
	if(msptl_in->url_open(pcilent_ctt)<0){
		ms_errGoto(ms_end, "open %s failed",pcilent_ctt->url);
	}

	ms_sleep(1);
	ms_debug("url_write");
	ms_byte *sendstr="Hello,I am client";
	ms_s32 len_need_send=ms_buflen(sendstr);
	ms_s32 len_send=msptl_in->url_write(pcilent_ctt,sendstr,len_need_send);
	if(len_send>0){
		ms_verbose("test:Send success!(len_send,len_need_send:%d,%d)-Context:%s%s ",len_send,len_need_send,LINE_END,sendstr);
		if(ms_strncmp_saeq(msptl_in->name, PTLNAME_WEBSOCKECT)){
			ms_verbose("dataType:%d ",pcilent_ctt->urlctt_opt.ws_opt.dataType);
		}
	}else{
		ms_error("test:Send failed!(len_send,len_need_send:%d,%d)-Context:%s%s ",len_send,len_need_send,LINE_END,sendstr);
	}

	ms_sleep(1);
	ms_byte recvbuf[1024]={0};
	ms_debug("url_read");
	ms_s32 len=msptl_in->url_read(pcilent_ctt,recvbuf,ms_bufsize(recvbuf));
	if(len>0){
		ms_verbose("test:Read success!Context:%s%s", LINE_END,recvbuf);
		if(ms_strncmp_saeq(msptl_in->name, PTLNAME_WEBSOCKECT)){
			ms_verbose("dataType:%d ",pcilent_ctt->urlctt_opt.ws_opt.dataType);
		}
	}else{
		ms_error("test:Read failed!");
	}
	msptl_in->url_close(pcilent_ctt);
ms_end:	
	msptc_api_deinit();
	msmem_api_deinit();
	mslog_api_deinit();
}
#undef MAIN_C

2. Server

        Supports receiving SRT push streams and converting them to UDP output. The project relies on msprotocol: a lightweight network protocol that is easy to extend and easy to use. It can be applied to X86 and ARM64 embedded devices, and currently supports file, hls, http, rtmp, msav, raw, rtp, rtsp, tcp, udp, udplite, srt and other protocols;

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <err.h>
#include <libmscommon/mscommon.h>
#include <libmscommon/msepoll.h>
#include <libmslog/mslog.h>
#include <libmsprotocol/msprotocol.h>

#include <srt/srt.h>

#define FLAG "SRTSERVER_SAMPLE"
static char * pinurl=ms_null;
static char * pouturl=ms_null;
void* srtserver_send(void* args)
{
	URLVal *purlval_srt=(URLVal *)args;

	URLVal urlval_udp;
	memset(&urlval_udp,0,sizeof(URLVal));
	urlval_udp.purl_ptl=msptc_api_match(PTLNAME_UDP);
	ms_strcpy(urlval_udp.url_ctt.url, pouturl);
	urlval_udp.url_ctt.urlctt_opt.udp_opt.interfacein="p2p1";
	urlval_udp.url_ctt.urlctt_opt.udp_opt.localhostipin="192.168.200.244";
	urlval_udp.url_ctt.flags|=FLAG_WRITE;
	if(urlval_udp.purl_ptl->url_open(&urlval_udp.url_ctt)<0){
		ms_errExit(-1, "Open in url failed");
	}
	
	char data[1316]={0};
	while (1){
		int recvlen=purlval_srt->purl_ptl->url_read(&purlval_srt->url_ctt,data,sizeof(data));
		if(recvlen>0){
			if (urlval_udp.purl_ptl->url_write(&urlval_udp.url_ctt, data , recvlen)<0){
				printf("url_write failed\r\n");
				break;
			}
		}
	}
	urlval_udp.purl_ptl->url_close(&urlval_udp.url_ctt);
	return NULL;
}
int main(int argc, char **argv)
{
	mslog_api_init( 54, ms_null,ms_null, ms_null);
	if(3!=argc){
		ms_error("usage:%s   inurl(srt)   outurl(udp)",argv[0]);
		mslog_api_deinit();
		return -1;
	}
	pinurl=argv[1];
	pouturl=argv[2];
	msptc_api_init();
	msptc_api_register();
	
	URLVal urlval_srt;
	memset(&urlval_srt,0,sizeof(URLVal));
	urlval_srt.purl_ptl=msptc_api_match(PTLNAME_SRT);
	ms_memset(&urlval_srt.url_ctt,0,sizeof(URLContext));

	ms_strcpy(urlval_srt.url_ctt.url, pinurl);
	
	urlval_srt.url_ctt.urlctt_opt.srt_opt.listen=1;
	if(urlval_srt.purl_ptl->url_open(&urlval_srt.url_ctt)<0){
		ms_errExit(-1, "Open out url failed");
	}
	char buf[2048]={0};
	int len=0;
	int recver=0;

	while(1){
		recver=urlval_srt.purl_ptl->priv_func.srt.accept(&urlval_srt.url_ctt);
		if(recver<0){
			ms_errRet(-1, "url_accept error");
		}

		pthread_t rcvthread;
		pthread_create(&rcvthread, NULL, srtserver_send, &urlval_srt);
		ms_sleep(1);
	}	
	urlval_srt.purl_ptl->url_close(&urlval_srt.url_ctt);
	msptc_api_deinit();
	mslog_api_deinit();
}
#undef MAIN_C

Guess you like

Origin blog.csdn.net/weixin_35804181/article/details/132906837