devendradhanal

Untitled

Aug 31st, 2012
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.50 KB | None | 0 0
  1. /* filesrc_to_rtmpsink.cpp
  2. * Created on: 24-Aug-2012
  3. * Author: dev*/
  4. //#include <gst/gst.h>
  5. /////////////////////////////////////////////////// 1
  6. #include <gst/gst.h>
  7. #include <gst/app/gstappsink.h>
  8. #include <gst/app/gstappsrc.h>
  9. ///////////////////////////////////////////////////
  10. //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @2
  11. #include <cv.h>
  12. #include <highgui.h>
  13. #include <iostream>
  14. using namespace cv;
  15. using namespace std;
  16. //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'
  17. #include <stdio.h>
  18. /////////////////////////////////////////////////// 2
  19. static GMainLoop *loop = NULL;
  20. static GstElement *pipeline1 = NULL;
  21. static GstElement *pipeline2 = NULL;
  22. ///////////////////////////////////////////////////
  23. //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @1
  24. static int IMG_width = 480;
  25. static int IMG_height = 360;
  26. static IplImage *img;
  27. static uchar *IMG_data;
  28. //double scale = 4;
  29. //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  30. /////////////////////////////////////////////////// 3
  31. static const char app_src_name[] = "app-src_01";
  32. static const char app_sink_name[] = "app-sink_01";
  33. ///////////////////////////////////////////////////
  34. /////////////////////////////////////////////////// 4
  35. static GstFlowReturn new_buffer (GstAppSink *app_sink, gpointer user_data){
  36. GstBuffer *buffer = gst_app_sink_pull_buffer( (GstAppSink*) gst_bin_get_by_name( GST_BIN(pipeline1), app_sink_name));
  37. // //=======================================================================
  38. //
  39. g_debug("appsink: buffer timestamp(%llu)\t duration(%llu)\t offset(%llu)\t size(%d)\n", GST_BUFFER_TIMESTAMP(buffer), GST_BUFFER_DURATION(buffer), GST_BUFFER_OFFSET(buffer), GST_BUFFER_SIZE(buffer) );
  40. printf("timestamp = %lu \t duration = %lu \t offset = %lu \t size = %d \n",GST_BUFFER_TIMESTAMP(buffer), GST_BUFFER_DURATION(buffer), GST_BUFFER_OFFSET(buffer), GST_BUFFER_SIZE(buffer));
  41.  
  42. //debugging
  43. if (!gst_bin_get_by_name( GST_BIN(pipeline1), app_sink_name))
  44. {
  45. g_print("app-sink não está disponível!\n");
  46. }
  47. if (!gst_bin_get_by_name( GST_BIN(pipeline2), app_src_name))
  48. {
  49. g_print("app-src não está disponível!\n");
  50. }
  51. //=======================================================================
  52. IMG_data = (uchar*) img->imageData;
  53. memcpy(IMG_data, GST_BUFFER_DATA(buffer), GST_BUFFER_SIZE(buffer));
  54. cvConvertImage(img,img,CV_CVTIMG_SWAP_RB);
  55. ////////////detectAndDraw(img);
  56. //========>PROCESS THE FRAME HERE<========
  57. // cvNamedWindow("FRAME BEIGN PROCESED",CV_WINDOW_KEEPRATIO);
  58. cvCircle(img,Point(350,350),50,Scalar(0,0,255),2,CV_AA,1);
  59. // cvShowImage("FRAME BEIGN PROCESED",img);
  60. cvConvertImage(img,img,CV_CVTIMG_SWAP_RB);
  61. memcpy(GST_BUFFER_DATA(buffer),IMG_data, GST_BUFFER_SIZE(buffer));
  62. gst_app_src_push_buffer( GST_APP_SRC( gst_bin_get_by_name(GST_BIN(pipeline2),app_src_name)) , buffer);
  63. return GST_FLOW_OK;
  64. }
  65. ///////////////////////////////////////////////////
  66. /////////////////////////////////////////////////// 5
  67. static gboolean bus_call(GstBus *bus, GstMessage *msg, gpointer data)
  68. {
  69. gchar *userdata = (gchar *) data;
  70. switch(GST_MESSAGE_TYPE(msg))
  71. {
  72. case GST_MESSAGE_EOS:
  73. {
  74. if ( g_ascii_strcasecmp(userdata, gst_element_get_name(pipeline1)) == 0)
  75. gst_app_src_end_of_stream( GST_APP_SRC( gst_bin_get_by_name( GST_BIN(pipeline2), app_src_name ) ) );
  76. if ( g_ascii_strcasecmp(userdata, gst_element_get_name(pipeline2)) == 0)
  77. g_main_loop_quit(loop);
  78. break;
  79. }
  80. case GST_MESSAGE_ERROR:
  81. {
  82. gchar *debug;
  83. GError *error;
  84. gst_message_parse_error(msg, &error, &debug);
  85. g_free(debug);
  86. g_error_free(error);
  87. g_main_loop_quit(loop);
  88. break;
  89. }
  90. case GST_MESSAGE_STATE_CHANGED :
  91. {
  92. GstState oldstate;
  93. GstState newstate;
  94. GstState pending;
  95. gst_message_parse_state_changed (msg,&oldstate,&newstate,&pending);
  96. g_debug("pipeline:%s old:%s new:%s pending:%s", userdata,gst_element_state_get_name(oldstate),gst_element_state_get_name(newstate),gst_element_state_get_name(pending));
  97. break;
  98. }
  99. case GST_MESSAGE_WARNING:
  100. {
  101. gchar *debug;
  102. GError *error;
  103. gst_message_parse_warning (msg,&error,&debug);
  104. g_warning("pipeline:%s",userdata);
  105. g_warning("debug: %s", debug);
  106. g_warning("error: %s", error->message);
  107. g_free (debug);
  108. g_error_free (error);
  109. break;
  110. }
  111. default:
  112. break;
  113. }
  114. return TRUE;
  115. }
  116. ///////////////////////////////////////////////////
  117. int main(int argc, char **argv)
  118. {
  119.  
  120. /////////////////////////////////////////////////// 6
  121. GError *error = NULL;
  122. GstBus *bus = NULL;
  123. GstAppSinkCallbacks callbacks;
  124. gchar pipeline1_str[556];
  125. gchar pipeline2_str[556];
  126. ///////////////////////////////////////////////////
  127. //#################################################
  128. char input_filename[556];
  129. //#################################################
  130.  
  131. // strcpy(input_filename,argv[1]);
  132. strcpy(input_filename,"mynewstream");
  133. // mynewstream.flv
  134.  
  135. //cvcvcvcvcvcvcvcvcvcvcvcvcvcvcvcvcvcvcvcvcvcvcvcv
  136. img = cvCreateImage( cvSize(IMG_width,IMG_height), IPL_DEPTH_8U, 3);
  137. //cvcvcvcvcvcvcvcvcvcvcvcvcvcvcvcvcvcvcvcvcvcvcvcv
  138. /////////////////////////////////////////////////// 7
  139. gst_init(&argc, &argv);
  140. loop = g_main_loop_new(NULL,FALSE);
  141. ///////////////////////////////////////////////////
  142. int res = 0;
  143. //need this for pipeline creation
  144. res = sprintf(pipeline1_str, "rtmpsrc location=\'rtmp://127.0.0.1:1935/flvplayback/%s live=1\' num-buffers=2147483647 ! queue ! decodebin2 ! videorate ! videoscale ! ffmpegcolorspace ! video/x-raw-rgb, width=%d, height=%d ! appsink name=%s ", input_filename,IMG_width, IMG_height, app_sink_name);
  145. //or need this for pipeline creation
  146. // res = sprintf(pipeline1_str, "filesrc location=\"/opt/lampp/htdocs/flvplayback/20051210-w50s.flv\" ! queue ! decodebin ! videorate ! videoscale ! ffmpegcolorspace ! video/x-raw-rgb, width=%d, height=%d ! appsink name=\"%s\"",IMG_width, IMG_height, app_sink_name);
  147. // res = sprintf(pipeline1_str, "filesrc location=/home/dev/o.flv ! queue ! decodebin ! videorate ! videoscale ! ffmpegcolorspace ! video/x-raw-rgb, width=%d, height=%d ! appsink name=%s",IMG_width, IMG_height, app_sink_name);
  148.  
  149. if (res < 0)
  150. return -1;
  151.  
  152. //for giving to rtmp i.e. live stream
  153. //res = sprintf(pipeline2_str, "appsrc name=\"%s\" is-live=1 num-buffers=2147483647 ! queue ! videoparse format=14 width=%d height=%d framerate=25/1 ! videorate ! videoscale ! ffmpegcolorspace ! video/x-raw-yuv, width=%d, height=%d ! queue ! ffenc_flv ! flvmux ! rtmpsink location=\"rtmp://127.0.0.1/flvplayback/rtmp-%s\"", app_src_name, IMG_width, IMG_height, IMG_width, IMG_height);
  154. // for storing in file i.e. filesink
  155. // res = sprintf(pipeline2_str,"appsrc name=\"%s\" ! queue ! videoparse format=14 width=%d height=%d framerate=25/1 ! videorate ! videoscale ! ffmpegcolorspace ! video/x-raw-yuv, width=%d, height=%d ! queue ! ffenc_flv ! flvmux streamable=true ! filesink location=\"/home/dev/rtmp-outputop.flv\"", app_src_name, IMG_width, IMG_height, IMG_width, IMG_height);
  156. // res = sprintf(pipeline2_str,"appsrc name=%s ! queue ! videoparse format=14 width=%d height=%d framerate=25/1 ! videorate ! videoscale ! ffmpegcolorspace ! video/x-raw-yuv, width=%d, height=%d ! queue ! ffenc_flv ! flvmux streamable=true ! filesink location=/opt/lampp/htdocs/flvplayback/new%s.flv", app_src_name, IMG_width, IMG_height, IMG_width, IMG_height,input_filename);
  157.  
  158. // res = sprintf(pipeline2_str,"appsrc name=%s ! queue ! videoparse format=14 width=%d height=%d framerate=25/1 ! videorate ! videoscale ! ffmpegcolorspace ! video/x-raw-yuv, width=%d, height=%d ! queue ! ffenc_flv ! flvmux streamable=true ! rtmpsink location=\'rtmp://127.0.0.1/flvplayback/new%s live=1\'", app_src_name, IMG_width, IMG_height, IMG_width, IMG_height,input_filename);
  159. res = sprintf(pipeline2_str,"appsrc name=%s ! queue ! videoparse format=14 width=%d height=%d framerate=25/1 ! videorate ! videoscale ! ffmpegcolorspace ! video/x-raw-yuv, width=%d, height=%d ! queue ! ffenc_flv ! flvmux streamable=true ! rtmpsink location=\'rtmp://127.0.0.1/flvplayback/rtmp-mymovie live=1\' ", app_src_name, IMG_width, IMG_height, IMG_width, IMG_height);
  160. // rtmp-mymovie
  161. // printf(pipeline2_str);
  162. //aboce line make this program work but trying below now 24 aug 5 pm
  163. // res = sprintf(pipeline2_str,"appsrc name=%s ! queue ! videoparse format=14 width=%d height=%d framerate=25/1 ! videorate ! videoscale ! ffmpegcolorspace ! video/x-raw-yuv, width=%d, height=%d ! queue ! ffenc_flv ! flvmux streamable=true ! rtmpsink location=rtmp://127.0.0.1/flvplayback/new%s", app_src_name, IMG_width, IMG_height, IMG_width, IMG_height,input_filename);
  164. //above line doest work well right now
  165.  
  166. if (res < 0)
  167. return -1;
  168.  
  169. /////////////////////////////////////////////////// 8
  170. pipeline1 = gst_parse_launch(pipeline1_str, &error);
  171.  
  172. if (error)
  173. return -1;
  174.  
  175. pipeline2 = gst_parse_launch(pipeline2_str, &error);
  176.  
  177. if (error)
  178. return -1;
  179.  
  180. if (!gst_bin_get_by_name( GST_BIN(pipeline1), app_sink_name))
  181. return -1;
  182.  
  183. if (!gst_bin_get_by_name( GST_BIN(pipeline2), app_src_name))
  184. return -1;
  185.  
  186. bus = gst_pipeline_get_bus( GST_PIPELINE(pipeline1) );
  187.  
  188. gst_bus_add_watch(bus, bus_call, gst_element_get_name(pipeline1));
  189.  
  190. gst_object_unref(bus);
  191.  
  192. bus = gst_pipeline_get_bus( GST_PIPELINE(pipeline2) );
  193.  
  194. gst_bus_add_watch(bus, bus_call, gst_element_get_name(pipeline2));
  195.  
  196. gst_object_unref(bus);
  197.  
  198. ////putttonig cvNamedWindow here
  199. // cvNamedWindow("FRAME BEIGN PROCESED",CV_WINDOW_KEEPRATIO);
  200.  
  201.  
  202. callbacks.eos = NULL;
  203. callbacks.new_preroll = NULL;
  204. callbacks.new_buffer = new_buffer;
  205.  
  206. gst_app_sink_set_callbacks( (GstAppSink*) gst_bin_get_by_name(GST_BIN(pipeline1), app_sink_name), &callbacks, NULL, NULL);
  207.  
  208. gst_element_set_state(pipeline1, GST_STATE_PLAYING);
  209. gst_element_set_state(pipeline2, GST_STATE_PLAYING);
  210.  
  211. g_main_loop_run(loop);
  212.  
  213. gst_element_set_state(pipeline1, GST_STATE_NULL);
  214. gst_element_set_state(pipeline2, GST_STATE_NULL);
  215.  
  216. gst_object_unref( GST_OBJECT(pipeline1) );
  217. gst_object_unref( GST_OBJECT(pipeline2) );
  218. ///////////////////////////////////////////////////
  219.  
  220.  
  221. cvReleaseImage(&img);
  222.  
  223. /////////////////////////////////////////////////// 9
  224. g_main_loop_unref(loop);
  225. ///////////////////////////////////////////////////
  226. }
Add Comment
Please, Sign In to add comment