Advertisement
Guest User

Untitled

a guest
Oct 19th, 2021
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1. #include <gst/gst.h>
  2. #include <stdio.h>
  3. #include <pthread.h>
  4.  
  5. typedef struct _CustomData {
  6. GstElement *playbin; /* Our one and only element */
  7. GstStreamCollection *collection;
  8.  
  9. gint n_video; /* Number of embedded video streams */
  10. gint n_audio; /* Number of embedded audio streams */
  11. gint n_text; /* Number of embedded subtitle streams */
  12.  
  13. gint current_video; /* Currently playing video stream */
  14. gint current_audio; /* Currently playing audio stream */
  15. gint current_text; /* Currently playing subtitle stream */
  16.  
  17. GMainLoop *main_loop; /* GLib's Main Loop */
  18. } CustomData;
  19.  
  20.  
  21. typedef enum {
  22. GST_PLAY_FLAG_VIDEO = (1 << 0), /* We want video output */
  23. GST_PLAY_FLAG_AUDIO = (1 << 1), /* We want audio output */
  24. GST_PLAY_FLAG_TEXT = (1 << 2) /* We want subtitle output */
  25. } GstPlayFlags;
  26.  
  27. static CustomData *data;
  28. gchar *g_filename;
  29. gboolean isplaying;
  30. GMainLoop *loop;
  31. GstBus *bus ;
  32. GstMessage *msg ;
  33. gint flags;
  34. GstStateChangeReturn ret;
  35. gchar *uria;
  36. gchar *urib;
  37. gchar *uri1;
  38.  
  39. void printcurrent_timestamp() {
  40. struct timeval te;
  41. gettimeofday(&te, NULL);
  42. long long milliseconds = te.tv_sec*1000LL + te.tv_usec/1000;
  43. g_print("milliseconds: %lld\n", milliseconds);
  44. }
  45.  
  46. void play(gchar *uritoplay)
  47. {
  48. if (data->playbin) {
  49. gst_element_set_state (data->playbin, GST_STATE_NULL);
  50. gst_object_unref (data->playbin);
  51. data->playbin = NULL;
  52. g_free (data);
  53. data = g_new0 (CustomData, 1);
  54. }
  55. data->playbin = gst_element_factory_make ("playbin3", "playbin");
  56. if (!data->playbin) {
  57. g_printerr ("Not all elements could be created.\n");
  58. return;
  59. }
  60. g_object_set(data->playbin, "uri", uritoplay, NULL);
  61. g_object_get (data->playbin, "flags", &flags, NULL);
  62. flags |= GST_PLAY_FLAG_VIDEO;
  63. flags &= ~GST_PLAY_FLAG_TEXT;
  64. flags &= ~GST_PLAY_FLAG_AUDIO;
  65. g_object_set (data->playbin, "flags", flags, NULL);
  66.  
  67. GstElement *sink;
  68. sink = gst_parse_bin_from_description ("kmssink plane-properties=s,zpos=3", TRUE, NULL);
  69. g_object_set (data->playbin, "video-sink", sink, NULL);
  70. gst_object_unref(sink);
  71.  
  72. ret = gst_element_set_state (data->playbin, GST_STATE_PLAYING);
  73. if (ret == GST_STATE_CHANGE_FAILURE) {
  74. g_printerr ("Unable to set the pipeline to the playing state.\n");
  75. gst_object_unref (data->playbin);
  76. return;
  77. }
  78. }
  79.  
  80. int main(int argc, char *argv[]) {
  81.  
  82. int looper = 0;
  83. gboolean isloopa= TRUE;
  84.  
  85. data = g_new0 (CustomData, 1);
  86.  
  87. gst_init (NULL, NULL);
  88.  
  89. uria = gst_filename_to_uri ("/root/FileA.mp4", NULL);
  90. urib = gst_filename_to_uri ("/root/FileB.mp4", NULL);
  91.  
  92. play(uria);
  93. bus = gst_element_get_bus(data->playbin);
  94.  
  95. for(;;)
  96. {
  97. looper++;
  98. //EVERY 10s video uri is changed and pipeline reinitialised
  99. if(looper==10000){
  100. g_print("10000!\n");
  101. looper=0;
  102. if(isloopa==TRUE) {
  103. isloopa= FALSE;
  104. play(urib);
  105. } else {
  106. isloopa= TRUE;
  107. play(uria);
  108. }
  109. gst_object_unref(bus);
  110. bus = gst_element_get_bus(data->playbin);
  111. }
  112. //BLOCKS FOR 1ms
  113. msg = gst_bus_timed_pop_filtered(bus, 1000000, (GstMessageType)(GST_MESSAGE_ERROR | GST_MESSAGE_EOS | GST_MESSAGE_WARNING | GST_MESSAGE_INFO));
  114. if(msg != NULL) {
  115. GError *err;
  116. gchar *debug_info;
  117.  
  118. switch (GST_MESSAGE_TYPE (msg)) {
  119. case GST_MESSAGE_ERROR:
  120.  
  121. gst_message_parse_error (msg, &err, &debug_info);
  122. g_printerr ("Error received from element %s: %s\n", GST_OBJECT_NAME (msg->src), err->message);
  123. g_printerr ("Debugging information: %s\n", debug_info ? debug_info : "none");
  124. if(strstr(err->message, "Failed to decode frame")){
  125. gst_print ("1M Frame - Restart\n");
  126. play(uria);
  127. gst_message_unref (msg);
  128. gst_object_unref(bus);
  129. bus = gst_element_get_bus(data->playbin);
  130. }
  131. g_clear_error (&err);
  132. g_free (debug_info);
  133.  
  134. break;
  135. case GST_MESSAGE_EOS:
  136. g_print ("End-Of-Stream reached. SEEK.\n"); printcurrent_timestamp();
  137. gst_message_unref (msg);
  138. if(gst_element_seek(data->playbin, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH, GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE) ==FALSE) {
  139. g_print("SEEK FAILED!\n");
  140. }
  141. break;
  142. default:
  143. gst_message_unref (msg);
  144. break;
  145. }
  146. }
  147. }
  148. /* Free resources */
  149. gst_element_set_state (data->playbin, GST_STATE_NULL);
  150. gst_object_unref (data->playbin);
  151. return 0;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement