Advertisement
Guest User

Untitled

a guest
Jun 9th, 2021
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.79 KB | None | 0 0
  1. #include <gst/gst.h>
  2. #include <stdio.h>
  3.  
  4. /* Structure to contain all our information, so we can pass it to callbacks */
  5. typedef struct _VideoCompositor {
  6. GstElement *pipeline;
  7. GstElement *background;
  8. GstElement *encoder;
  9. GstElement *muxer;
  10. GstElement *convert;
  11. GstElement *sink;
  12. } VideoCompositor;
  13.  
  14. /* Handler for the pad-added signal */
  15. static void pad_added_handler (GstElement *src, GstPad *pad, VideoCompositor *data);
  16.  
  17. int main(int argc, char *argv[]) {
  18. GstBus *bus;
  19. GstMessage *msg;
  20. GstStateChangeReturn ret;
  21. gboolean terminate = FALSE;
  22.  
  23. VideoCompositor videoCompositor;
  24.  
  25. /* Initialize GStreamer */
  26. gst_init (&argc, &argv);
  27.  
  28. /* Compositor init */
  29. videoCompositor.background = gst_element_factory_make ("uridecodebin", "background");
  30. videoCompositor.encoder = gst_element_factory_make("x264enc", "x264enc");
  31. videoCompositor.muxer = gst_element_factory_make("mp4mux", "mp4mux");
  32. videoCompositor.sink = gst_element_factory_make("filesink", "finalsink");
  33.  
  34. videoCompositor.pipeline = gst_pipeline_new ("compositor_pipeline");
  35.  
  36. if (!videoCompositor.pipeline || !videoCompositor.background || !videoCompositor.sink) {
  37. g_printerr ("Not all elements could be created.\n");
  38. return -1;
  39. }
  40.  
  41. gst_bin_add_many (GST_BIN (videoCompositor.pipeline), videoCompositor.background, videoCompositor.encoder, videoCompositor.muxer, videoCompositor.sink, NULL);
  42.  
  43. if (!gst_element_link_many (videoCompositor.encoder, videoCompositor.muxer, videoCompositor.sink, NULL)) {
  44. g_printerr ("Elements could not be linked.\n");
  45. gst_object_unref (videoCompositor.pipeline);
  46. return -1;
  47. }
  48.  
  49. g_object_set (videoCompositor.background, "uri", "file:///Users/vinayak/video-fuzzing/compositor/resources/cars/1.mp4", NULL);
  50. g_object_set (videoCompositor.sink, "location", "./file.mp4", NULL);
  51.  
  52. /* Connect to the pad-added signal */
  53. g_signal_connect (videoCompositor.background, "pad-added", G_CALLBACK (pad_added_handler), &videoCompositor);
  54.  
  55. ret = gst_element_set_state (videoCompositor.pipeline, GST_STATE_PLAYING);
  56. if (ret == GST_STATE_CHANGE_FAILURE) {
  57. g_printerr ("Unable to set the pipeline to the playing state.\n");
  58. gst_object_unref (videoCompositor.pipeline);
  59. return -1;
  60. }
  61.  
  62. /* Listen to the bus */
  63. bus = gst_element_get_bus (videoCompositor.pipeline);
  64. do {
  65. msg = gst_bus_timed_pop_filtered (bus, 100000000 * GST_MSECOND,
  66. GST_MESSAGE_STATE_CHANGED | GST_MESSAGE_ERROR | GST_MESSAGE_EOS | GST_MESSAGE_DURATION);
  67.  
  68. /* Parse message */
  69. if (msg != NULL) {
  70. GError *err;
  71. gchar *debug_info;
  72.  
  73. switch (GST_MESSAGE_TYPE (msg)) {
  74. case GST_MESSAGE_DURATION:
  75. gst_element_set_state (videoCompositor.pipeline, GST_STATE_NULL);
  76. terminate = TRUE;
  77. break;
  78. case GST_MESSAGE_ERROR:
  79. gst_message_parse_error (msg, &err, &debug_info);
  80. g_printerr ("Error received from element %s: %s\n", GST_OBJECT_NAME (msg->src), err->message);
  81. g_printerr ("Debugging information: %s\n", debug_info ? debug_info : "none");
  82. g_clear_error (&err);
  83. g_free (debug_info);
  84. terminate = TRUE;
  85. break;
  86. case GST_MESSAGE_EOS:
  87. g_print ("End-Of-Stream reached.\n");
  88.  
  89. gst_element_set_state (videoCompositor.pipeline, GST_STATE_NULL);
  90. terminate = TRUE;
  91. break;
  92. case GST_MESSAGE_STATE_CHANGED:
  93. /* We are only interested in state-changed messages from the pipeline */
  94. if (GST_MESSAGE_SRC (msg) == GST_OBJECT (videoCompositor.pipeline)) {
  95. GstState old_state, new_state, pending_state;
  96. gst_message_parse_state_changed (msg, &old_state, &new_state, &pending_state);
  97. g_print ("Pipeline state changed from %s to %s:\n",
  98. gst_element_state_get_name (old_state), gst_element_state_get_name (new_state));
  99. }
  100. break;
  101. default:
  102. /* We should not reach here */
  103. g_printerr ("Unexpected message received.\n");
  104. break;
  105. }
  106. gst_message_unref (msg);
  107. }
  108. } while (!terminate);
  109. g_print("ended exec");
  110. /* Free resources */
  111. gst_object_unref (bus);
  112. gst_element_set_state (videoCompositor.pipeline, GST_STATE_NULL);
  113. gst_object_unref (videoCompositor.pipeline);
  114. return 0;
  115. }
  116.  
  117. /* This function will be called by the pad-added signal */
  118. static void pad_added_handler (GstElement *src, GstPad *new_pad, VideoCompositor *data) {
  119. GstPad *sink_pad = gst_element_get_request_pad (data->encoder, "sink");
  120. GstPadLinkReturn ret;
  121. GstCaps *new_pad_caps = NULL;
  122. GstStructure *new_pad_struct = NULL;
  123. const gchar *new_pad_type = NULL;
  124.  
  125. g_print ("Received new pad '%s' from '%s':\n", GST_PAD_NAME (new_pad), GST_ELEMENT_NAME (src));
  126.  
  127. /* If our converter is already linked, we have nothing to do here */
  128. if (gst_pad_is_linked (sink_pad)) {
  129. g_print ("We are already linked. Ignoring.\n");
  130. goto exit;
  131. }
  132.  
  133. /* Check the new pad's type */
  134. new_pad_caps = gst_pad_get_current_caps (new_pad);
  135. new_pad_struct = gst_caps_get_structure (new_pad_caps, 0);
  136. new_pad_type = gst_structure_get_name (new_pad_struct);
  137. if (!g_str_has_prefix (new_pad_type, "video/x-raw")) {
  138. g_print ("It has type '%s' which is not raw video. Ignoring.\n", new_pad_type);
  139. goto exit;
  140. }
  141.  
  142. /* Attempt the link */
  143. ret = gst_pad_link (new_pad, sink_pad);
  144. if (GST_PAD_LINK_FAILED (ret)) {
  145. g_print ("Type is '%s' but link failed.\n", new_pad_type);
  146. } else {
  147. g_print ("Link succeeded (type '%s').\n", new_pad_type);
  148. }
  149.  
  150. exit:
  151. /* Unreference the new pad's caps, if we got them */
  152. if (new_pad_caps != NULL)
  153. gst_caps_unref (new_pad_caps);
  154.  
  155. /* Unreference the sink pad */
  156. gst_object_unref (sink_pad);
  157. }
  158.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement