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:

[html] view plain copy print ?
  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. }  
不完善的地方希望大家指出来,谢谢!!

你可能感兴趣的:(Gstreamer实现摄像头的远程采集,udp传输,本地显示和保存为AVI文件 发送端)