Gstreamer实现摄像头的远程采集,udp传输,本地显示和保存为AVI文件 发送端

经过两个星期的努力终于完成 Gstreamer实现摄像头的远程采集,udp传输,本地显示和保存为AVI文件,的C语言程序,现在分享给大家,欢迎大家评论指正

由于本程序存在录制时间短但保存成文件的播放长度很长的问题,希望知道的高手们指点一下解决的方法,在此先谢谢了!!!!

send:

gst-launch-0.10 -v gstrtpbin name=rtpbin v4l2src device=/dev/video0 ! videorate ! videoscale ! ffmpegcolorspace ! 'video/x-raw-yuv, width=(int)320, height=(int)240, framerate=(fraction)15/1' !  rtpvrawpay ! rtpbin.send_rtp_sink_0 rtpbin.send_rtp_src_0 ! multiudpsink clients="127.0.0.1:9996" rtpbin.send_rtcp_src_0 ! multiudpsink clients="127.0.0.1:9997" sync=false async=false udpsrc port=10000 ! rtpbin.recv_rtcp_sink_0

C code:

  1. #include <string.h>  
  2. #include <math.h>  
  3.   
  4. #include <gst/gst.h>  
  5. #define HOST_IP "127.0.0.1"  
  6. #define PORT 9996  
  7. #define Video_dev "/dev/video0"  
  8. #define Video_Caps "video/x-raw-yuv, width=(int)320, height=(int)240, framerate=(fraction)30/1"  
  9.   
  10.   
  11. int   
  12. main(int argc,char *argv[])  
  13. {  
  14.     GstElement *vsource,*vrate,*vscale,*vconvert;  
  15.     GstElement *vrtpbin,*vrtpsink,*vrtppay;  
  16.     GstElement *pipeline;     
  17.     GMainLoop *loop;  
  18.     GstCaps *caps;  
  19.     GstPad *srcpad,*sinkpad;  
  20.   
  21.     gst_init(&argc,&argv);  
  22.   
  23.     pipeline=gst_pipeline_new(NULL);  
  24.     g_assert(pipeline);  
  25.   
  26.     vsource=gst_element_factory_make("v4l2src","vsource");  
  27.     g_assert(vsource);  
  28.     vrate=gst_element_factory_make("videorate","vrate");  
  29.     g_assert(vrate);  
  30.     vscale=gst_element_factory_make("videoscale","vscal");  
  31.     g_assert(vscale);  
  32.     vconvert=gst_element_factory_make("ffmpegcolorspace","vconvert");  
  33.     g_assert(vconvert);  
  34.       
  35.   
  36.   
  37.     vrtppay=gst_element_factory_make("rtpvrawpay","vrtppay");  
  38.     g_assert(vrtppay);  
  39.     g_object_set(G_OBJECT(vsource),"device", Video_dev, NULL);  
  40.       
  41.   
  42.     gst_bin_add_many(GST_BIN(pipeline),vsource,vrate,vscale,vconvert,vrtppay,NULL);  
  43.       
  44.     caps=gst_caps_from_string(Video_Caps);  
  45.   
  46.   
  47.     if(!gst_element_link_many(vsource,vrate,vscale,vconvert,NULL)){  
  48.         g_error("Failed to link ");  
  49.     }  
  50.     if(!gst_element_link_filtered(vconvert,vrtppay,caps))  
  51.     {  
  52.         g_error("Failed to link caps");  
  53.     }   
  54.     gst_caps_unref(caps);  
  55.       
  56.   
  57.     vrtpbin=gst_element_factory_make("gstrtpbin","vrtpbin");  
  58.     g_assert(vrtpbin);  
  59.     gst_bin_add(GST_BIN(pipeline),vrtpbin);  
  60.       
  61.     vrtpsink=gst_element_factory_make("udpsink","vrtpsink");  
  62.     g_assert(vrtpsink);  
  63.     g_object_set(vrtpsink,"port",PORT,"host",HOST_IP,NULL);  
  64.     gst_bin_add_many(GST_BIN(pipeline),vrtpsink,NULL);  
  65.   
  66.     sinkpad=gst_element_get_request_pad(vrtpbin,"send_rtp_sink_0");  
  67.     srcpad=gst_element_get_static_pad(vrtppay,"src");  
  68.     if(gst_pad_link(srcpad,sinkpad)!=GST_PAD_LINK_OK)  
  69.         g_error("Failed to link video payloader to vrtpbin");  
  70.     gst_object_unref(srcpad);  
  71.     srcpad=gst_element_get_static_pad(vrtpbin,"send_rtp_src_0");  
  72.     sinkpad=gst_element_get_static_pad(vrtpsink,"sink");  
  73.     if(gst_pad_link(srcpad,sinkpad)!=GST_PAD_LINK_OK)  
  74.     g_error("Failed to link vrtpbin to vrtpsink");  
  75.     gst_object_unref(srcpad);  
  76.     gst_object_unref(sinkpad);  
  77.   
  78.   
  79.     g_print("starting sender pipeline\n");  
  80. //  gst_element_set_state(pipeline,SGT_STATE_PLAYING);  
  81.     gst_element_set_state (pipeline, GST_STATE_PLAYING);  
  82.     loop=g_main_loop_new(NULL,FALSE);  
  83.     g_main_loop_run(loop);  
  84.     g_print("stopping sender pipeline\n");  
  85.     gst_element_set_state(pipeline,GST_STATE_NULL);  
  86.     return 0;     
  87.   
  88. }  
#include <string.h>
#include <math.h>

#include <gst/gst.h>
#define HOST_IP "127.0.0.1"
#define PORT 9996
#define Video_dev "/dev/video0"
#define Video_Caps "video/x-raw-yuv, width=(int)320, height=(int)240, framerate=(fraction)30/1"


int 
main(int argc,char *argv[])
{
	GstElement *vsource,*vrate,*vscale,*vconvert;
	GstElement *vrtpbin,*vrtpsink,*vrtppay;
	GstElement *pipeline;	
	GMainLoop *loop;
	GstCaps *caps;
	GstPad *srcpad,*sinkpad;

	gst_init(&argc,&argv);

	pipeline=gst_pipeline_new(NULL);
	g_assert(pipeline);

	vsource=gst_element_factory_make("v4l2src","vsource");
	g_assert(vsource);
	vrate=gst_element_factory_make("videorate","vrate");
	g_assert(vrate);
	vscale=gst_element_factory_make("videoscale","vscal");
	g_assert(vscale);
	vconvert=gst_element_factory_make("ffmpegcolorspace","vconvert");
	g_assert(vconvert);
	


	vrtppay=gst_element_factory_make("rtpvrawpay","vrtppay");
	g_assert(vrtppay);
	g_object_set(G_OBJECT(vsource),"device", Video_dev, NULL);
	

	gst_bin_add_many(GST_BIN(pipeline),vsource,vrate,vscale,vconvert,vrtppay,NULL);
	
	caps=gst_caps_from_string(Video_Caps);


	if(!gst_element_link_many(vsource,vrate,vscale,vconvert,NULL)){
		g_error("Failed to link ");
	}
	if(!gst_element_link_filtered(vconvert,vrtppay,caps))
	{
		g_error("Failed to link caps");
	} 
	gst_caps_unref(caps);
	

	vrtpbin=gst_element_factory_make("gstrtpbin","vrtpbin");
	g_assert(vrtpbin);
	gst_bin_add(GST_BIN(pipeline),vrtpbin);
	
	vrtpsink=gst_element_factory_make("udpsink","vrtpsink");
	g_assert(vrtpsink);
	g_object_set(vrtpsink,"port",PORT,"host",HOST_IP,NULL);
	gst_bin_add_many(GST_BIN(pipeline),vrtpsink,NULL);

	sinkpad=gst_element_get_request_pad(vrtpbin,"send_rtp_sink_0");
	srcpad=gst_element_get_static_pad(vrtppay,"src");
	if(gst_pad_link(srcpad,sinkpad)!=GST_PAD_LINK_OK)
		g_error("Failed to link video payloader to vrtpbin");
	gst_object_unref(srcpad);
	srcpad=gst_element_get_static_pad(vrtpbin,"send_rtp_src_0");
	sinkpad=gst_element_get_static_pad(vrtpsink,"sink");
	if(gst_pad_link(srcpad,sinkpad)!=GST_PAD_LINK_OK)
	g_error("Failed to link vrtpbin to vrtpsink");
	gst_object_unref(srcpad);
	gst_object_unref(sinkpad);


	g_print("starting sender pipeline\n");
//	gst_element_set_state(pipeline,SGT_STATE_PLAYING);
	gst_element_set_state (pipeline, GST_STATE_PLAYING);
	loop=g_main_loop_new(NULL,FALSE);
	g_main_loop_run(loop);
	g_print("stopping sender pipeline\n");
	gst_element_set_state(pipeline,GST_STATE_NULL);
	return 0;	

}
不完善的地方希望大家指出来,谢谢!!
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在 Linux 开发板上实现摄像头采集图像并实时显示在屏幕上,需要按以下步骤操作: 1. 确认摄像头已经被 Linux 系统正确识别并加载了对应的驱动程序。 2. 查找摄像头对应的设备文件,一般在 /dev 目录下,例如 /dev/video0。 3. 安装并配置视频采集软件,如 V4L2(Video for Linux 2)。 4. 打开摄像头设备文件,设置采集参数并开始采集。 5. 通过视频渲染软件(如 GStreamer)将采集到的视频数据显示在屏幕上。 具体步骤如下: 1. 确认摄像头已经被 Linux 系统正确识别并加载了对应的驱动程序。可以通过 dmesg 命令或 /var/log/messages 文件查看系统日志,确认摄像头是否被正确识别。 2. 查找摄像头对应的设备文件,一般在 /dev 目录下,例如 /dev/video0。可以通过 ls /dev/video* 命令查找。 3. 安装并配置视频采集软件,如 V4L2。V4L2 是 Linux 下常用的视频采集软件,可以通过 apt-get 命令或源码编译安装。安装完成后,需要配置 V4L2 的参数,如采集分辨率、帧率、色彩空间等。 4. 打开摄像头设备文件,设置采集参数并开始采集。可以使用 V4L2 提供的命令行工具 v4l2-ctl 或自己编写程序实现。在采集过程中,可以设置采集缓冲区、采集帧数等参数。 5. 通过视频渲染软件(如 GStreamer)将采集到的视频数据显示在屏幕上。可以使用 GStreamer 提供的命令行工具 gst-launch 或自己编写程序实现。在渲染过程中,需要设置视频格式、显示窗口等参数。 以上是在 Linux 开发板上实现摄像头采集图像并实时显示在屏幕上的大致步骤,具体操作还需要根据实际情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值