Advertisement
mantielero

Untitled

Jan 9th, 2022
1,863
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Nim 5.34 KB | None | 0 0
  1. # https://gstreamer.freedesktop.org/documentation/tutorials/basic/dynamic-pipelines.html?gi-language=c
  2. import gintro/[gst,gobject,glib]
  3.  
  4. #[
  5. /* Structure to contain all our information, so we can pass it to callbacks */
  6. typedef struct _CustomData {
  7.   GstElement *pipeline;
  8.   GstElement *source;
  9.   GstElement *convert;
  10.   GstElement *resample;
  11.   GstElement *sink;
  12. } CustomData;
  13.  
  14. /* Handler for the pad-added signal */
  15. static void pad_added_handler (GstElement *src, GstPad *pad, CustomData *data);    
  16. ]#
  17.  
  18.  
  19. proc main =
  20.   #var pipeline: gst.Element
  21.   var bus: gst.Bus
  22.   var msg: gst.Message    
  23.   var ret: gst.StateChangeReturn
  24.  
  25. #[
  26.   CustomData data;
  27.   gboolean terminate = FALSE;    
  28. ]#  
  29.  
  30.   # Initialize GStreamer
  31.   gst.init()      # gst_init (&argc, &argv);
  32.  
  33.   # Create the elements
  34.   let source = make("uridecodebin", "source")
  35.   let convert = make("audioconvert", "convert")
  36.   let resample = make("audioresample", "resample")
  37.   let sink = make("autoaudiosink", "sink")  
  38.  
  39.   # Create the empty pipeline
  40.   let pipeline = newPipeline("test-pipeline")
  41.   assert( source != nil || convert != nil || resample || ! nil, "Not all elements could be created.")
  42.   assert( sink != nil )
  43.   assert( pipeline != nil )
  44.  
  45.   # Build the pipeline.
  46.   ret = pipeline.add(source)
  47.   ret = pipeline.add(convert)
  48.   ret = pipeline.add(resample)
  49.   ret = pipeline.add(sink)
  50.  
  51.   # Note that we are NOT linking the source at this
  52.   # point. We will do it later.
  53.   ret = convert.link(resample)
  54.   ret = resample.link(link)
  55.   assert( ret != nil, "Elements could not be linked." )
  56.  
  57.   #  Set the URI to play
  58.   # g_object_set (data.source, "uri", "https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm", NULL);
  59.   setProperty(source, "uri", "https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm")
  60.  
  61.  
  62.  
  63.   # Connect to the pad-added signal
  64.   # g_signal_connect (data.source, "pad-added", G_CALLBACK (pad_added_handler), &data);
  65.  
  66.   # Start playing
  67.   let stateChange = pipeline.setState(playing)
  68.   assert(stateChange != nil, "Unable to set the pipeline to the playing state")
  69.  
  70.   # Listen to the bus
  71.  
  72. #[
  73.  
  74.  
  75.   /*  */
  76.   bus = gst_element_get_bus (data.pipeline);
  77.   do {
  78.     msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE,
  79.         GST_MESSAGE_STATE_CHANGED | GST_MESSAGE_ERROR | GST_MESSAGE_EOS);
  80.  
  81.     /* Parse message */
  82.     if (msg != NULL) {
  83.       GError *err;
  84.       gchar *debug_info;
  85.  
  86.       switch (GST_MESSAGE_TYPE (msg)) {
  87.         case GST_MESSAGE_ERROR:
  88.           gst_message_parse_error (msg, &err, &debug_info);
  89.           g_printerr ("Error received from element %s: %s\n", GST_OBJECT_NAME (msg->src), err->message);
  90.           g_printerr ("Debugging information: %s\n", debug_info ? debug_info : "none");
  91.           g_clear_error (&err);
  92.           g_free (debug_info);
  93.           terminate = TRUE;
  94.           break;
  95.         case GST_MESSAGE_EOS:
  96.           g_print ("End-Of-Stream reached.\n");
  97.           terminate = TRUE;
  98.           break;
  99.         case GST_MESSAGE_STATE_CHANGED:
  100.           /* We are only interested in state-changed messages from the pipeline */
  101.           if (GST_MESSAGE_SRC (msg) == GST_OBJECT (data.pipeline)) {
  102.             GstState old_state, new_state, pending_state;
  103.             gst_message_parse_state_changed (msg, &old_state, &new_state, &pending_state);
  104.             g_print ("Pipeline state changed from %s to %s:\n",
  105.                 gst_element_state_get_name (old_state), gst_element_state_get_name (new_state));
  106.           }
  107.           break;
  108.         default:
  109.           /* We should not reach here */
  110.           g_printerr ("Unexpected message received.\n");
  111.           break;
  112.       }
  113.       gst_message_unref (msg);
  114.     }
  115.   } while (!terminate);
  116.  
  117.   /* Free resources */
  118.   gst_object_unref (bus);
  119.   gst_element_set_state (data.pipeline, GST_STATE_NULL);
  120.   gst_object_unref (data.pipeline);
  121.   return 0;
  122. }
  123.  
  124. /* This function will be called by the pad-added signal */
  125. static void pad_added_handler (GstElement *src, GstPad *new_pad, CustomData *data) {
  126.   GstPad *sink_pad = gst_element_get_static_pad (data->convert, "sink");
  127.   GstPadLinkReturn ret;
  128.   GstCaps *new_pad_caps = NULL;
  129.   GstStructure *new_pad_struct = NULL;
  130.   const gchar *new_pad_type = NULL;
  131.  
  132.   g_print ("Received new pad '%s' from '%s':\n", GST_PAD_NAME (new_pad), GST_ELEMENT_NAME (src));
  133.  
  134.   /* If our converter is already linked, we have nothing to do here */
  135.   if (gst_pad_is_linked (sink_pad)) {
  136.     g_print ("We are already linked. Ignoring.\n");
  137.     goto exit;
  138.   }
  139.  
  140.   /* Check the new pad's type */
  141.   new_pad_caps = gst_pad_get_current_caps (new_pad);
  142.   new_pad_struct = gst_caps_get_structure (new_pad_caps, 0);
  143.   new_pad_type = gst_structure_get_name (new_pad_struct);
  144.   if (!g_str_has_prefix (new_pad_type, "audio/x-raw")) {
  145.     g_print ("It has type '%s' which is not raw audio. Ignoring.\n", new_pad_type);
  146.     goto exit;
  147.   }
  148.  
  149.   /* Attempt the link */
  150.   ret = gst_pad_link (new_pad, sink_pad);
  151.   if (GST_PAD_LINK_FAILED (ret)) {
  152.     g_print ("Type is '%s' but link failed.\n", new_pad_type);
  153.   } else {
  154.     g_print ("Link succeeded (type '%s').\n", new_pad_type);
  155.   }
  156.  
  157. exit:
  158.   /* Unreference the new pad's caps, if we got them */
  159.   if (new_pad_caps != NULL)
  160.     gst_caps_unref (new_pad_caps);
  161.  
  162.   /* Unreference the sink pad */
  163.   gst_object_unref (sink_pad);
  164.  
  165. ]#
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement