Advertisement
Guest User

Untitled

a guest
May 24th, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. #include <gst/gst.h>
  2. #include <glib.h>
  3.  
  4. typedef struct {
  5. GMainLoop* loop;
  6. GstElement* pipeline;
  7. } LoopAndPipeline;
  8.  
  9. static gboolean bus_call(GstBus *bus, GstMessage *msg, gpointer data)
  10. {
  11. LoopAndPipeline *loop_and_pipeline = (LoopAndPipeline*) data;
  12.  
  13. GMainLoop *loop = loop_and_pipeline->loop;
  14. GstElement *pipeline = loop_and_pipeline->pipeline;
  15.  
  16. switch (GST_MESSAGE_TYPE (msg)) {
  17.  
  18. case GST_MESSAGE_EOS:
  19. g_print ("End of stream\n");
  20. gst_element_set_state(pipeline, GST_STATE_READY);
  21. gst_element_set_state(pipeline, GST_STATE_PLAYING);
  22. break;
  23.  
  24. case GST_MESSAGE_ERROR: {
  25. gchar *debug;
  26. GError *error;
  27.  
  28. gst_message_parse_error (msg, &error, &debug);
  29. g_free (debug);
  30.  
  31. g_printerr ("Error: %s\n", error->message);
  32. g_error_free (error);
  33.  
  34. g_main_loop_quit (loop);
  35. break;
  36. }
  37. default:
  38. break;
  39. }
  40.  
  41. return TRUE;
  42. }
  43.  
  44. static void on_pad_added (GstElement *element, GstPad *pad, gpointer data)
  45. {
  46. GstPad *sinkpad;
  47. GstElement *decoder = (GstElement *) data;
  48.  
  49. /* We can now link this pad with the vorbis-decoder sink pad */
  50. g_print ("Dynamic pad created, linking demuxer/decoder\n");
  51.  
  52. sinkpad = gst_element_get_static_pad (decoder, "sink");
  53.  
  54. gst_pad_link (pad, sinkpad);
  55.  
  56. gst_object_unref (sinkpad);
  57. }
  58.  
  59. int
  60. main (int argc, char *argv[])
  61. {
  62. GMainLoop *loop;
  63. GstBus *bus;
  64. guint bus_watch_id;
  65.  
  66. /* Initialisation */
  67. gst_init (&argc, &argv);
  68.  
  69. loop = g_main_loop_new (NULL, FALSE);
  70.  
  71.  
  72. /* Check input arguments */
  73. if (argc != 3) {
  74. g_printerr ("Usage: %s <video> <v4l2loopback device>\n", argv[0]);
  75. return -1;
  76. }
  77.  
  78. /* Create gstreamer elements */
  79. GstElement *pipeline = gst_pipeline_new ("audio-player");
  80. GstElement *source = gst_element_factory_make ("filesrc", NULL);
  81. GstElement *decode = gst_element_factory_make ("decodebin", NULL);
  82. GstElement *convert = gst_element_factory_make ("videoconvert", NULL);
  83. GstElement *sink = gst_element_factory_make ("v4l2sink", NULL);
  84.  
  85. if (!pipeline || !source || !decode || !convert || !sink) {
  86. g_printerr ("One element could not be created. Exiting.\n");
  87. return -1;
  88. }
  89.  
  90. /* Set up the pipeline */
  91. g_object_set (G_OBJECT (source), "location", argv[1], NULL);
  92. g_object_set(G_OBJECT (sink), "device", argv[2], NULL);
  93.  
  94. /* we add a message handler */
  95. LoopAndPipeline loop_and_pipeline;
  96. loop_and_pipeline.loop = loop;
  97. loop_and_pipeline.pipeline = pipeline;
  98.  
  99. bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
  100. bus_watch_id = gst_bus_add_watch (bus, bus_call, &main_loop_and_pipeline);
  101. gst_object_unref (bus);
  102.  
  103. /* we add all elements into the pipeline */
  104. gst_bin_add_many (GST_BIN (pipeline), source, decode, convert, sink, NULL);
  105.  
  106. /* we link the elements together */
  107. gst_element_link (source, decode);
  108. GstCaps *caps = gst_caps_new_simple ("video/x-raw", "format", G_TYPE_STRING, "YUY2", NULL);
  109. gst_element_link_filtered (convert, sink, caps);
  110. g_signal_connect (decode, "pad-added", G_CALLBACK (on_pad_added), convert);
  111.  
  112. /* note that the demuxer will be linked to the decoder dynamically.
  113. The reason is that Ogg may contain various streams (for example
  114. audio and video). The source pad(s) will be created at run time,
  115. by the demuxer when it detects the amount and nature of streams.
  116. Therefore we connect a callback function which will be executed
  117. when the "pad-added" is emitted.*/
  118.  
  119.  
  120. /* Set the pipeline to "playing" state*/
  121. g_print ("Now playing: %s\n", argv[1]);
  122. gst_element_set_state (pipeline, GST_STATE_PLAYING);
  123.  
  124. /* Iterate */
  125. g_print ("Running...\n");
  126. g_main_loop_run (loop);
  127.  
  128. /* Out of the main loop, clean up nicely */
  129. g_print ("Returned, stopping playback\n");
  130. gst_element_set_state (pipeline, GST_STATE_NULL);
  131.  
  132. g_print ("Deleting pipeline\n");
  133. gst_object_unref (GST_OBJECT (pipeline));
  134. g_source_remove (bus_watch_id);
  135. g_main_loop_unref (loop);
  136.  
  137. return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement