/tests/icles/playback/test7.c

https://github.com/rikaunite/gst-opera_gst-plugins-base · C · 179 lines · 123 code · 37 blank · 19 comment · 10 complexity · 5c7abef3cdf4b68c39b1d2be128a5821 MD5 · raw file

  1. /* GStreamer
  2. * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the
  16. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. * Boston, MA 02111-1307, USA.
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #ifdef HAVE_STDLIB_H
  23. #include <stdlib.h> /* exit() */
  24. #endif
  25. #include <gst/gst.h>
  26. #define UPDATE_INTERVAL 500
  27. static int arg_count;
  28. static int max_count;
  29. static gboolean
  30. update_scale (GstElement * element)
  31. {
  32. gint64 duration = -1;
  33. gint64 position = -1;
  34. GstFormat format = GST_FORMAT_TIME;
  35. gchar dur_str[32], pos_str[32];
  36. if (gst_element_query_position (element, &format, &position) &&
  37. position != -1) {
  38. g_snprintf (pos_str, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (position));
  39. } else {
  40. g_snprintf (pos_str, 32, "-:--:--.---------");
  41. }
  42. if (gst_element_query_duration (element, &format, &duration) &&
  43. duration != -1) {
  44. g_snprintf (dur_str, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (duration));
  45. } else {
  46. g_snprintf (dur_str, 32, "-:--:--.---------");
  47. }
  48. g_print ("%s / %s\n", pos_str, dur_str);
  49. return TRUE;
  50. }
  51. static void
  52. warning_cb (GstBus * bus, GstMessage * msg, gpointer foo)
  53. {
  54. GError *err = NULL;
  55. gchar *dbg = NULL;
  56. gst_message_parse_warning (msg, &err, &dbg);
  57. g_printerr ("WARNING: %s (%s)\n", err->message, (dbg) ? dbg : "no details");
  58. g_error_free (err);
  59. g_free (dbg);
  60. }
  61. static void
  62. error_cb (GstBus * bus, GstMessage * msg, GMainLoop * main_loop)
  63. {
  64. GError *err = NULL;
  65. gchar *dbg = NULL;
  66. gst_message_parse_error (msg, &err, &dbg);
  67. g_printerr ("ERROR: %s (%s)\n", err->message, (dbg) ? dbg : "no details");
  68. g_main_loop_quit (main_loop);
  69. g_error_free (err);
  70. g_free (dbg);
  71. }
  72. static void
  73. eos_cb (GstBus * bus, GstMessage * msg, GMainLoop * main_loop)
  74. {
  75. g_print ("EOS\n");
  76. g_main_loop_quit (main_loop);
  77. }
  78. static void
  79. new_clock_cb (GstBus * bus, GstMessage * msg, gpointer nothing)
  80. {
  81. GstClock *clock;
  82. gst_message_parse_new_clock (msg, &clock);
  83. g_print ("NEW CLOCK: %s\n", GST_OBJECT_NAME (clock));
  84. }
  85. static void
  86. clock_lost_cb (GstBus * bus, GstMessage * msg, GstElement * playbin)
  87. {
  88. GstClock *clock;
  89. gst_message_parse_clock_lost (msg, &clock);
  90. g_print ("CLOCK LOST: %s\n", GST_OBJECT_NAME (clock));
  91. gst_element_set_state (playbin, GST_STATE_PAUSED);
  92. gst_element_set_state (playbin, GST_STATE_PLAYING);
  93. }
  94. static void
  95. about_to_finish_cb (GstElement * element, gchar * uri[])
  96. {
  97. if (arg_count < max_count) {
  98. g_object_set (G_OBJECT (element), "uri", uri[arg_count], NULL);
  99. arg_count++;
  100. }
  101. }
  102. gint
  103. main (gint argc, gchar * argv[])
  104. {
  105. GstStateChangeReturn res;
  106. GstElement *player;
  107. GMainLoop *loop;
  108. GstBus *bus;
  109. gst_init (&argc, &argv);
  110. loop = g_main_loop_new (NULL, TRUE);
  111. if (argc < 2) {
  112. g_print ("usage: %s <uri> [<uri> ... ]\n", argv[0]);
  113. exit (-1);
  114. }
  115. player = gst_element_factory_make ("playbin2", "player");
  116. g_assert (player);
  117. bus = gst_pipeline_get_bus (GST_PIPELINE (player));
  118. gst_bus_add_signal_watch (bus);
  119. g_signal_connect (bus, "message::eos", G_CALLBACK (eos_cb), loop);
  120. g_signal_connect (bus, "message::error", G_CALLBACK (error_cb), loop);
  121. g_signal_connect (bus, "message::warning", G_CALLBACK (warning_cb), NULL);
  122. g_signal_connect (bus, "message::new-clock", G_CALLBACK (new_clock_cb), NULL);
  123. g_signal_connect (bus, "message::clock-lost", G_CALLBACK (clock_lost_cb),
  124. player);
  125. g_object_set (G_OBJECT (player), "uri", argv[1], NULL);
  126. arg_count = 2;
  127. max_count = argc;
  128. g_signal_connect (player, "about-to-finish", G_CALLBACK (about_to_finish_cb),
  129. argv);
  130. res = gst_element_set_state (player, GST_STATE_PLAYING);
  131. if (res == GST_STATE_CHANGE_FAILURE) {
  132. g_print ("could not play\n");
  133. return -1;
  134. }
  135. g_timeout_add (UPDATE_INTERVAL, (GSourceFunc) update_scale, player);
  136. g_main_loop_run (loop);
  137. /* tidy up */
  138. gst_element_set_state (player, GST_STATE_NULL);
  139. gst_object_unref (player);
  140. gst_object_unref (bus);
  141. return 0;
  142. }