Advertisement
Guest User

Untitled

a guest
May 6th, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.45 KB | None | 0 0
  1. #include <gst/gst.h>
  2.  
  3. #include <string.h>
  4.  
  5. #include <gst/app/gstappsrc.h>
  6. #include <gst/app/gstappsink.h>
  7.  
  8. /* these are the caps we are going to pass through the appsink and appsrc */
  9.  
  10. const gchar *audio_caps =
  11. "audio/x-raw,format=S16LE,channels=1,rate=8000, layout=interleaved";
  12. const gchar *video_caps =
  13. "video/x-raw,format=I420,width=640,height=320,framerate=30/1";
  14.  
  15. typedef struct
  16. {
  17.     GMainLoop *loop;
  18.     GstElement *source;
  19.     GstElement *sink;
  20. } ProgramData;
  21.  
  22. /* called when the appsink notifies us that there is a new buffer ready for
  23. * processing */
  24. static GstFlowReturn
  25. on_new_sample_from_sink(GstElement * elt, ProgramData * data)
  26. {
  27.     GstSample *sample;
  28.     GstBuffer *app_buffer, *buffer;
  29.     GstElement *source;
  30.  
  31.     /* get the sample from appsink */
  32.     sample = gst_app_sink_pull_sample(GST_APP_SINK(elt));
  33.     buffer = gst_sample_get_buffer(sample);
  34.  
  35.     /* make a copy */
  36.     app_buffer = gst_buffer_copy(buffer);
  37.  
  38.     /* we don't need the appsink sample anymore */
  39.     gst_sample_unref(sample);
  40.  
  41.     /* get source an push new buffer */
  42.     source = gst_bin_get_by_name(GST_BIN(data->sink), "testsource");
  43.     return gst_app_src_push_buffer(GST_APP_SRC(source), app_buffer);
  44. }
  45.  
  46. /* called when we get a GstMessage from the source pipeline when we get EOS, we
  47. * notify the appsrc of it. */
  48. static gboolean
  49. on_source_message(GstBus * bus, GstMessage * message, ProgramData * data)
  50. {
  51.     GstElement *source;
  52.  
  53.     switch (GST_MESSAGE_TYPE(message)) {
  54.     case GST_MESSAGE_EOS:
  55.         g_print("The source got dry\n");
  56.         source = gst_bin_get_by_name(GST_BIN(data->sink), "testsource");
  57.         gst_app_src_end_of_stream(GST_APP_SRC(source));
  58.         break;
  59.     case GST_MESSAGE_ERROR:
  60.         g_print("Received error\n");
  61.         g_main_loop_quit(data->loop);
  62.         break;
  63.     default:
  64.         break;
  65.     }
  66.     return TRUE;
  67. }
  68.  
  69. /* called when we get a GstMessage from the sink pipeline when we get EOS, we
  70. * exit the mainloop and this testapp. */
  71. static gboolean
  72. on_sink_message(GstBus * bus, GstMessage * message, ProgramData * data)
  73. {
  74.     /* nil */
  75.     switch (GST_MESSAGE_TYPE(message)) {
  76.     case GST_MESSAGE_EOS:
  77.         g_print("Finished playback\n");
  78.         g_main_loop_quit(data->loop);
  79.         break;
  80.     case GST_MESSAGE_ERROR:
  81.         g_print("Received error\n");
  82.         g_main_loop_quit(data->loop);
  83.         break;
  84.     default:
  85.         break;
  86.     }
  87.     return TRUE;
  88. }
  89.  
  90. int
  91. main(int argc, char *argv[])
  92. {
  93.     gchar *filename = NULL;
  94.     ProgramData *data = NULL;
  95.     gchar *string = NULL;
  96.     GstBus *bus = NULL;
  97.     GstElement *testsink = NULL;
  98.     GstElement *testsource = NULL;
  99.  
  100.     gst_init(&argc, &argv);
  101.  
  102.     /*if (argc == 2)
  103.         filename = g_strdup(argv[1]);
  104.     else
  105.         filename = g_strdup("/usr/share/sounds/ekiga/ring.wav");*/
  106.  
  107.     data = g_new0(ProgramData, 1);
  108.  
  109.     data->loop = g_main_loop_new(NULL, FALSE);
  110.  
  111.     /* setting up source pipeline, we read from a file and convert to our desired
  112.     * caps. */
  113.     string =
  114.         g_strdup_printf
  115.         (
  116.         "videotestsrc pattern=\"blink\" ! appsink caps=\"%s\" name=testsink",
  117.         //"audiotestsrc ! appsink caps=\"%s\" name=testsink",
  118.         //"videotestsrc ! autovideosink caps=\"%s\"",
  119.          video_caps);
  120.         //audio_caps);
  121.     //g_free(filename);
  122.     data->source = gst_parse_launch(string, NULL);
  123.     g_free(string);
  124.  
  125.     if (data->source == NULL) {
  126.         g_print("Bad source\n");
  127.         return -1;
  128.     }
  129.  
  130.     /* to be notified of messages from this pipeline, mostly EOS */
  131.     bus = gst_element_get_bus(data->source);
  132.     gst_bus_add_watch(bus, (GstBusFunc)on_source_message, data);
  133.     gst_object_unref(bus);
  134.  
  135.     /* we use appsink in push mode, it sends us a signal when data is available
  136.     * and we pull out the data in the signal callback. We want the appsink to
  137.     * push as fast as it can, hence the sync=false */
  138.     testsink = gst_bin_get_by_name(GST_BIN(data->source), "testsink");
  139.     g_object_set(G_OBJECT(testsink), "emit-signals", TRUE, "sync", FALSE, NULL);
  140.     g_signal_connect(testsink, "new-sample",
  141.         G_CALLBACK(on_new_sample_from_sink), data);
  142.     gst_object_unref(testsink);
  143.  
  144.     /* setting up sink pipeline, we push audio data into this pipeline that will
  145.     * then play it back using the default audio sink. We have no blocking
  146.     * behaviour on the src which means that we will push the entire file into
  147.     * memory. */
  148.     string =
  149.         g_strdup_printf(
  150.         "appsrc name=testsource caps=\"%s\"  ! autovideosink sync=false",
  151.         //"appsrc name=testsource caps=\"%s\" ! autoaudiosink",
  152.         video_caps);
  153.         //audio_caps);
  154.        
  155.     data->sink = gst_parse_launch(string, NULL);
  156.     g_free(string);
  157.  
  158.     if (data->sink == NULL) {
  159.         g_print("Bad sink\n");
  160.         return -1;
  161.     }
  162.  
  163.     testsource = gst_bin_get_by_name(GST_BIN(data->sink), "testsource");
  164.     /* configure for time-based format */
  165.     g_object_set(testsource, "format", GST_FORMAT_TIME, NULL);
  166.     /* uncomment the next line to block when appsrc has buffered enough */
  167.     g_object_set (testsource, "block", TRUE, NULL);
  168.     gst_object_unref(testsource);
  169.  
  170.     bus = gst_element_get_bus(data->sink);
  171.     gst_bus_add_watch(bus, (GstBusFunc)on_sink_message, data);
  172.     gst_object_unref(bus);
  173.  
  174.     /* launching things */
  175.     gst_element_set_state(data->sink, GST_STATE_PLAYING);
  176.     gst_element_set_state(data->source, GST_STATE_PLAYING);
  177.  
  178.     /* let's run !, this loop will quit when the sink pipeline goes EOS or when an
  179.     * error occurs in the source or sink pipelines. */
  180.     g_print("Let's run!\n");
  181.     g_main_loop_run(data->loop);
  182.     g_print("Going out\n");
  183.  
  184.     gst_element_set_state(data->source, GST_STATE_NULL);
  185.     gst_element_set_state(data->sink, GST_STATE_NULL);
  186.  
  187.     gst_object_unref(data->source);
  188.     gst_object_unref(data->sink);
  189.     g_main_loop_unref(data->loop);
  190.     g_free(data);
  191.  
  192.     return 0;
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement