版权声明:本文为博主原创文章,未经博主允许不得转载。QQ/微信联系:123011785 https://blog.csdn.net/songxiao1988918/article/details/79228961
视频监控安防平台-RTSPClient高并发请求
由于最近项目上要求一个服务要承载100路以上的高清视频并发请求,之前是用live555封装的rtsp库,测试了下live555并发请求高清视频在50-60路左右,再往上就开始报错,本来想去优化live555,但是live555代码量有点多,想想就算了,直接自己重新做一个rtsp库,码流回调是以一帧一帧ES流形式返回,经过测试100路高清视频请求基本没什么压力,开发是在centos7.3上面搞的,rtspclient的demo下载地址:
http://download.csdn.net/download/songxiao1988918/10235477
欢迎大家试用,一起学习一起成长。
下面我把头文件和列子粘贴一下:
#ifndef _librtspc_i__
#define _librtspc_i__
#define RTSP_STREAM_VIDEODATA 1 //视频流类型 一帧一帧的ES流
#define RTSP_STREAM_AUDIODATA 2 //音频流类型
typedef void (*on_callback)(unsigned int type, void* data, unsigned int len, unsigned int pts, long userdata);
class librtspc_i
{
public:
/**
* 功能:开启rtsp请求
* 输入:
* url - rtsp的url
* wnd - 用户数据
* callback - 码流回调函数
* 返回值:
* 返回结果 - 成功返回0
*/
virtual int open_rtsp( const char * url, long wnd, on_callback callback) = 0;
/**
* 功能:关闭rtsp请求
* 输入:
* url - rtsp的url
* wnd - 用户数据
* callback - 码流回调函数
* 返回值:
* 返回结果 - 成功返回0
*/
virtual int close_rtsp( void ) = 0;
virtual int get_videosize( int* w, int* h ) = 0;
virtual unsigned get_audiotype() = 0;
virtual int check(void)=0;
};
// c++
extern "C" librtspc_i* new_librtspc( void );
extern "C" void delete_librtspc( librtspc_i* p );
#endif //_librtspc_i__
rtspclient demo
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <pthread.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/time.h>
#include <errno.h>
#include <math.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <assert.h>
#include <pthread.h>
#include "librtspc_i.h"
void real_callback(unsigned int type, void* data, unsigned int len, unsigned int pts, long userdata)
{
printf("recv type:%d, len:%d, userdata:%d\n", type, len, userdata);
}
static void *rtspc_thread(void *arg)
{
int count=2592000;
while(count--)
{
librtspc_i* rtspc=new_librtspc();
assert(rtspc!=NULL);
int ret=rtspc->open_rtsp("rtsp://admin:[email protected]:554",long(arg), real_callback);
assert(ret==0);
int delay=(int)(rand()%10+1);
usleep(delay*1000);
delete_librtspc( rtspc);
}
}
int main(int argc,char *argv[])
{
signal(SIGPIPE, SIG_IGN);
//单个视频请求
librtspc_i* rtspc=new_librtspc();
assert(rtspc!=NULL);
int ret=rtspc->open_rtsp("rtsp://admin:[email protected]:554",1000, real_callback);
assert(ret==0);
int delay=(int)(rand()%10+1);
usleep(delay*1000);
sleep(1000);
//启动线程进行并发测试
int tasks=100;
pthread_t pid[tasks];
for(long i=0;i<tasks;i++)
{
pthread_create(&pid[i],0,rtspc_thread,(void*)i);
}
for(int i=0;i<tasks;i++)
{
pthread_join(pid[i],0);
printf("%d close\n",i);
}
printf("over\n");
}
编译方式:
g++ main.cpp -L./ -lrtspc -lpthread -o rtspc_demo