Advertisement
Guest User

Untitled

a guest
Aug 27th, 2012
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.24 KB | None | 0 0
  1. #include <gdk-pixbuf/gdk-pixbuf.h>
  2. #include <gst/gst.h>
  3. #include <gst/app/gstappsrc.h>
  4. #include <gst/video/video.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8.  
  9.  
  10. typedef struct {
  11.  GstPipeline *pipeline;
  12.  GstElement *appsrc;
  13.  GstElement *src;
  14.  GstElement *encoder;
  15.  GstElement *streamer;
  16.  GMainLoop *loop;
  17.  GTimer* timer;
  18.  guint sourceid;
  19. } gst_app_t;
  20.  
  21. static gst_app_t gst_app;
  22.  
  23.  
  24. static gboolean read_data(gst_app_t* app) {
  25.     GstFlowReturn ret;
  26.     gdouble ms;
  27.     static gboolean white = FALSE;
  28.  
  29.     ms = g_timer_elapsed(app->timer, NULL);
  30.     //GST_INFO("Entering read_data(app=%p), elapsed seconds=%f.", app, ms);
  31.  
  32.     if (ms > 1.0/15.0) {
  33.         GstBuffer* buffer;
  34.         gboolean ok = TRUE;
  35.  
  36.         buffer = gst_buffer_new();
  37.  
  38.         GST_INFO("Pushing new data into pipe, elapsed seconds=%f.", ms);
  39.  
  40.         // this makes the image black/white
  41.         memset(GST_BUFFER_DATA(buffer), white ? 0xff : 0x0, GST_BUFFER_SIZE(buffer));
  42.         GST_BUFFER_SIZE(buffer) = 320 * 240 * 3 * sizeof(guchar);
  43.  
  44.         white = !white;
  45.         g_signal_emit_by_name(app->appsrc, "push-buffer", buffer, &ret);
  46.         gst_buffer_unref(buffer);
  47.  
  48.         if (GST_FLOW_OK != ret) {
  49.             // some error, stop sending data
  50.             GST_INFO("some error: %d", ret);
  51.             ok = FALSE;
  52.         }
  53.  
  54.         g_timer_start(app->timer);
  55.     }
  56.     //GST_INFO("Exiting read_data(app=%p).", app);
  57.     return TRUE;
  58. }
  59.  
  60.  
  61. static void start_feed(GstElement* pipeline, guint size, gst_app_t* app) {
  62.     GST_INFO("Entering start_feed(pipeline=%p, size=%d, app=%p).", pipeline, size, app);
  63.     if (0 == app->sourceid) {
  64.         GST_INFO("Start feeding");
  65.         app->sourceid = g_idle_add((GSourceFunc)read_data, app);
  66.     }
  67. }
  68.  
  69.  
  70. static void stop_feed(GstElement* pipeline, gst_app_t* app) {
  71.     GST_INFO("Entering stop_feed(pipeline=%p, app=%p).", pipeline, app);
  72.     if (0 != app->sourceid) {
  73.         GST_INFO("Stop feeding");
  74.         g_source_remove(app->sourceid);
  75.         app->sourceid = 0;
  76.     }
  77. }
  78.  
  79.  
  80. static gboolean bus_message(GstBus* bus, GstMessage* message, gst_app_t* app) {
  81.     GST_INFO("Entering bus_message(bus=%p, message=%s, app=%p).", bus, gst_message_type_get_name(GST_MESSAGE_TYPE(message)), app);
  82.     switch(GST_MESSAGE_TYPE(message)) {
  83.         case GST_MESSAGE_ERROR: {
  84.             GError* err = NULL;
  85.             gchar* dbg_info = NULL;
  86.  
  87.             gst_message_parse_error(message, &err, &dbg_info);
  88.             g_printerr("ERROR from element %s: %s\n",
  89.             GST_OBJECT_NAME(message->src), err->message);
  90.             g_printerr("Debugging info: %s\n",(dbg_info) ? dbg_info : "none");
  91.             g_error_free(err);
  92.             g_free(dbg_info);
  93.             g_main_loop_quit(app->loop);
  94.             break;
  95.         }
  96.         case GST_MESSAGE_EOS:
  97.             GST_INFO("End-of-stream message received, quitting main loop.");
  98.             g_main_loop_quit(app->loop);
  99.             break;
  100.         default:
  101.             break;
  102.     }
  103.     return TRUE;
  104. }
  105.  
  106.  
  107. int main(int argc, char* argv[]) {
  108.     gst_app_t* app = &gst_app;
  109.     GstBus* bus;
  110.     GstCaps* caps;
  111.     int USE_TESTSRC = 0;
  112.     int i = 0;
  113.  
  114.     for (i = 0; i < argc; i++) {
  115.         if (!strcmp(argv[i], "--testsrc")) {
  116.             USE_TESTSRC = 1;
  117.         }
  118.     }
  119.  
  120.     gst_init(&argc, &argv);
  121.  
  122.     app->loop = g_main_loop_new(NULL, TRUE);
  123.     app->timer = g_timer_new();
  124.  
  125.     if (USE_TESTSRC) {
  126.         app->pipeline = (GstPipeline*)gst_parse_launch("videotestsrc      ! videorate ! ffmpegcolorspace ! videoscale method=1 ! video/x-raw-yuv,width=320,height=240,format=(fourcc)UYVY,framerate=\(fraction\)15/1 ! TIVidenc1 codecName=h264enc engineName=codecServer ! rtph264pay pt=96 ! udpsink host=192.168.1.112 port=5000", NULL);
  127.     } else {
  128.         app->pipeline = (GstPipeline*)gst_parse_launch("appsrc name=mysrc ! videorate ! ffmpegcolorspace ! videoscale method=1 ! video/x-raw-yuv,width=320,height=240,format=(fourcc)UYVY,framerate=\(fraction\)15/1 ! TIVidenc1 codecName=h264enc engineName=codecServer ! rtph264pay pt=96 ! udpsink host=192.168.1.112 port=5000", NULL);
  129.     }
  130.  
  131.     g_assert (app->pipeline);
  132.  
  133.     bus = gst_pipeline_get_bus(GST_PIPELINE(app->pipeline));
  134.     g_assert(bus);
  135.     gst_bus_add_watch (bus, (GstBusFunc) bus_message, app);
  136.  
  137.     if (!USE_TESTSRC) {
  138.         app->appsrc = gst_bin_get_by_name (GST_BIN(app->pipeline), "mysrc");
  139.         g_assert(app->appsrc);
  140.         g_assert(GST_IS_APP_SRC(app->appsrc));
  141.         g_signal_connect (app->appsrc, "need-data",  G_CALLBACK (start_feed), app);
  142.         g_signal_connect (app->appsrc, "enough-data", G_CALLBACK (stop_feed), app);
  143.  
  144.         caps = gst_video_format_new_caps(GST_VIDEO_FORMAT_UYVY, 320, 240, 0, 1, 4, 3);
  145.         gst_app_src_set_caps(GST_APP_SRC(app->appsrc), caps);
  146.         //gst_app_src_set_max_bytes(GST_APP_SRC(app->appsrc), 320 * 240 * 3 * 100);
  147.     }
  148.  
  149.     GST_INFO("Setting pipeline %p to playing.", app->pipeline);
  150.     gst_element_set_state ((GstElement*)app->pipeline, GST_STATE_PLAYING);
  151.  
  152.     g_main_loop_run (app->loop);
  153.  
  154.     GST_INFO("Stopping pipeline %p.", app->pipeline);
  155.  
  156.     gst_element_set_state ((GstElement*)app->pipeline, GST_STATE_NULL);
  157.  
  158.     gst_object_unref (bus);
  159.     g_main_loop_unref (app->loop);
  160.  
  161.     return 0;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement