Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.49 KB | None | 0 0
  1. /*
  2. ============================================================================
  3. Name : gstreamer01_uri.c
  4. Author : EC
  5. Version : simple hello-world for audio-files only
  6. USAGE : <uri with ogg-file> as arg, e.g. file:///home/pi/Music/cp.ogg
  7. INFO : docs.gstreamer.com :
  8. Copyright : Your copyright notice
  9. Description : Hello World in C, Ansi-style
  10. : hello_world-Bsp für GStreamer
  11. ============================================================================
  12. */
  13.  
  14. #include <gst/gst.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17.  
  18. /* Structure to contain all our information, so we can pass it to callbacks */
  19. typedef struct _CustomData {
  20. GstElement *pipeline;
  21. GstElement *source;
  22. GstElement *audio_converter;
  23. GstElement *audio_sink;
  24. GstElement *video_converter;
  25. GstElement *video_sink;
  26. } CustomData;
  27.  
  28. /* Handler for the pad-added signal */
  29. static void pad_added_handler(GstElement *src, GstPad *pad, CustomData *data);
  30.  
  31. int main(int argc, char *argv[]) {
  32. CustomData data;
  33. GstBus *bus;
  34. GstMessage *msg;
  35. GstStateChangeReturn ret;
  36. gboolean terminate = FALSE;
  37.  
  38. /* Check input arguments */
  39. if (argc != 2) {
  40. g_printerr("Usage: %s <Ogg URI>\n", argv[0]);
  41. return -1;
  42. }
  43. g_print("Trying to play: %s\n", argv[1]);
  44.  
  45. /* Initialize GStreamer */
  46. gst_init(&argc, &argv);
  47.  
  48. /* Create the elements */
  49. data.source = gst_element_factory_make("uridecodebin", "source");
  50. data.audio_converter = gst_element_factory_make("audioconvert",
  51. "audio_converter");
  52. data.audio_sink = gst_element_factory_make("autoaudiosink", "audio_sink");
  53. data.video_converter = gst_element_factory_make("videoconvert",
  54. "video_converter");
  55. data.video_sink = gst_element_factory_make("ximagesink", "video_sink");
  56.  
  57. /* Create the empty pipeline */
  58. data.pipeline = gst_pipeline_new("test-pipeline");
  59.  
  60. if (!data.pipeline || !data.source || !data.audio_converter
  61. || !data.audio_sink || !data.video_converter || !data.video_sink) {
  62. g_printerr("Not all elements could be created.\n");
  63. return -1;
  64. }
  65.  
  66. /* Build the pipeline. Note that we are NOT linking the source at this
  67. * point. We will do it later. */
  68. gst_bin_add_many(GST_BIN(data.pipeline), data.source, data.audio_converter,
  69. data.audio_sink, data.video_sink, data.video_converter, NULL);
  70. if (!gst_element_link(data.audio_converter, data.audio_sink)) {
  71. g_printerr("Elements could not be linked.\n");
  72. gst_object_unref(data.pipeline);
  73. return -1;
  74. }
  75.  
  76. //Same for video
  77. if (!gst_element_link(data.video_converter, data.video_sink)) {
  78. g_printerr("Elements could not be linked.\n");
  79. gst_object_unref(data.pipeline);
  80. return -1;
  81. }
  82.  
  83. /* Set the URI to play */
  84. // g_object_set (data.source, "uri", "http://docs.gstreamer.com/media/sintel_trailer-480p.webm", NULL);
  85. g_object_set(data.source, "uri", argv[1], NULL);
  86.  
  87. /* Connect to the pad-added signal */
  88. g_signal_connect(data.source, "pad-added", G_CALLBACK(pad_added_handler),
  89. &data);
  90.  
  91. /* Start playing */
  92. ret = gst_element_set_state(data.pipeline, GST_STATE_PLAYING);
  93. if (ret == GST_STATE_CHANGE_FAILURE) {
  94. g_printerr("Unable to set the pipeline to the playing state.\n");
  95. gst_object_unref(data.pipeline);
  96. return -1;
  97. }
  98.  
  99. /* Listen to the bus */
  100. bus = gst_element_get_bus(data.pipeline);
  101. do {
  102. msg = gst_bus_timed_pop_filtered(bus, GST_CLOCK_TIME_NONE,
  103. GST_MESSAGE_STATE_CHANGED | GST_MESSAGE_ERROR
  104. | GST_MESSAGE_EOS);
  105.  
  106. /* Parse message */
  107. if (msg != NULL) {
  108. GError *err;
  109. gchar *debug_info;
  110.  
  111. switch (GST_MESSAGE_TYPE(msg)) {
  112. case GST_MESSAGE_ERROR:
  113. gst_message_parse_error(msg, &err, &debug_info);
  114. g_printerr("Error received from element %s: %s\n",
  115. GST_OBJECT_NAME(msg->src), err->message);
  116. g_printerr("Debugging information: %s\n",
  117. debug_info ? debug_info : "none");
  118. g_clear_error(&err);
  119. g_free(debug_info);
  120. terminate = TRUE;
  121. break;
  122. case GST_MESSAGE_EOS:
  123. g_print("End-Of-Stream reached.\n");
  124. terminate = TRUE;
  125. break;
  126. case GST_MESSAGE_STATE_CHANGED:
  127. /* We are only interested in state-changed messages from the pipeline */
  128. if (GST_MESSAGE_SRC(msg) == GST_OBJECT(data.pipeline)) {
  129. GstState old_state, new_state, pending_state;
  130. gst_message_parse_state_changed(msg, &old_state, &new_state,
  131. &pending_state);
  132. g_print("Pipeline state changed from %s to %s:\n",
  133. gst_element_state_get_name(old_state),
  134. gst_element_state_get_name(new_state));
  135. }
  136. break;
  137. default:
  138. /* We should not reach here */
  139. g_printerr("Unexpected message received.\n");
  140. break;
  141. }
  142. gst_message_unref(msg);
  143. }
  144. } while (!terminate);
  145.  
  146. /* Free resources */
  147. gst_object_unref(bus);
  148. gst_element_set_state(data.pipeline, GST_STATE_NULL);
  149. gst_object_unref(data.pipeline);
  150. return 0;
  151. }
  152.  
  153. /* This function will be called by the pad-added signal */
  154. static void pad_added_handler(GstElement *src, GstPad *new_pad,
  155. CustomData *data) {
  156. GstPad *audio_sink_pad = gst_element_get_static_pad(data->audio_converter,
  157. "sink");
  158. GstPad *video_sink_pad = gst_element_get_static_pad(data->video_converter,
  159. "sink");
  160. GstPadLinkReturn ret;
  161. GstCaps *new_pad_caps = NULL;
  162. GstStructure *new_pad_struct = NULL;
  163. const gchar *new_pad_type = NULL;
  164.  
  165. g_print("Received new pad '%s' from '%s':\n", GST_PAD_NAME(new_pad),
  166. GST_ELEMENT_NAME(src));
  167.  
  168.  
  169. /* Check the new pad's type */
  170. new_pad_caps = gst_pad_query_caps(new_pad, NULL);
  171. new_pad_struct = gst_caps_get_structure(new_pad_caps, 0);
  172. new_pad_type = gst_structure_get_name(new_pad_struct);
  173.  
  174. if (g_str_has_prefix(new_pad_type, "audio/x-raw")) {
  175. if (gst_pad_is_linked(audio_sink_pad)) {
  176. g_print("Audio pad linked already\n");
  177. goto exit;
  178. }
  179. ret = gst_pad_link(new_pad, audio_sink_pad);
  180. } else if (g_str_has_prefix(new_pad_type, "video/x-raw")) {
  181. g_print("Linking the video pad to the video converter sink\n");
  182. g_print("Is pad linked: %d\n", gst_pad_is_linked(video_sink_pad));
  183. if (gst_pad_is_linked(video_sink_pad)) {
  184. g_print("Video pad linked already\n");
  185. goto exit;
  186. }
  187. ret = gst_pad_link(new_pad, video_sink_pad);
  188. }
  189. if (GST_PAD_LINK_FAILED(ret)) {
  190. g_print(" Type is '%s' but link failed.\n", new_pad_type);
  191. } else {
  192. g_print(" Link succeeded (type '%s').\n", new_pad_type);
  193. }
  194.  
  195. exit:
  196. /* Unreference the new pad's caps, if we got them */
  197. if (new_pad_caps != NULL)
  198. gst_caps_unref(new_pad_caps);
  199.  
  200. /* Unreference the sink pad */
  201. gst_object_unref(audio_sink_pad);
  202. gst_object_unref(video_sink_pad);
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement