Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. #include <string>
  2. #include <stdio.h>
  3. #include <gst/gst.h>
  4. #include <gio/gio.h>
  5. #include <boost/thread.hpp>
  6.  
  7. static void on_pad_added(GstElement *decodebin,
  8. GstPad *pad,
  9. gpointer data) {
  10. GstElement *convert = (GstElement *) data;
  11.  
  12. GstCaps *caps;
  13. GstStructure *str;
  14. GstPad *audiopad;
  15.  
  16. audiopad = gst_element_get_static_pad(convert, "sink");
  17. if (GST_PAD_IS_LINKED(audiopad)) {
  18. g_object_unref(audiopad);
  19. return;
  20. }
  21.  
  22. caps = gst_pad_get_caps(pad);
  23. str = gst_caps_get_structure(caps, 0);
  24. printf("here %sn",gst_structure_get_name(str));
  25. if (!g_strrstr(gst_structure_get_name(str), "audio")) {
  26. gst_caps_unref(caps);
  27. gst_object_unref(audiopad);
  28. return;
  29. }
  30. gst_caps_unref(caps);
  31. gst_pad_link(pad, audiopad);
  32. g_object_unref(audiopad);
  33. }
  34.  
  35. static gboolean bus_call(GstBus *bus,
  36. GstMessage *msg,
  37. gpointer data) {
  38. GMainLoop *loop = (GMainLoop*)data;
  39.  
  40. switch (GST_MESSAGE_TYPE(msg)) {
  41. case GST_MESSAGE_EOS:
  42. g_print ("End of streamn");
  43. g_main_loop_quit(loop);
  44. break;
  45. case GST_MESSAGE_ERROR: {
  46. gchar *debug;
  47. GError *error;
  48.  
  49. gst_message_parse_error(msg, &error, &debug);
  50. g_free (debug);
  51.  
  52. g_printerr("Error: %sn", error->message);
  53. g_error_free(error);
  54.  
  55. g_main_loop_quit(loop);
  56. break;
  57. }
  58. default:
  59. break;
  60. }
  61. return true;
  62. }
  63.  
  64. int main (int argc, char **argv) {
  65. gst_init(&argc, &argv);
  66.  
  67. GstElement *pipeline, *source, *decode, *sink, *convert;
  68. int rate = 44100;
  69. int channels = 1;
  70. int depth = 16;
  71. bool output_signed = true;
  72. GMainLoop *loop;
  73. GstBus *bus;
  74. guint bus_watch_id;
  75. GMemoryOutputStream *stream;
  76. gpointer out_data;
  77.  
  78. // loop
  79. loop = g_main_loop_new(NULL, false);
  80. // pipeline
  81. pipeline = gst_pipeline_new("test_pipeline");
  82. // sink
  83. stream = G_MEMORY_OUTPUT_STREAM(g_memory_output_stream_new(NULL, 0, (GReallocFunc)g_realloc, (GDestroyNotify)g_free));
  84. sink = gst_element_factory_make ("giostreamsink", "sink");
  85. g_object_set(G_OBJECT(sink), "stream", stream, NULL);
  86. // source
  87. source = gst_element_factory_make("filesrc", "source");
  88. g_object_set(G_OBJECT(source), "location", "/home/sam/Desktop/audio/Alarm.wav", NULL);
  89. // convert
  90. convert = gst_element_factory_make("audioconvert", "convert");
  91. // decode
  92. decode = gst_element_factory_make("decodebin", "decoder");
  93. // link decode to convert
  94. g_signal_connect(decode, "pad-added", G_CALLBACK(on_pad_added), convert);
  95.  
  96. // bus
  97. bus = gst_pipeline_get_bus(GST_PIPELINE (pipeline));
  98. bus_watch_id = gst_bus_add_watch(bus, bus_call, loop);
  99. gst_object_unref(bus);
  100.  
  101. // add elements into pipeline
  102. gst_bin_add_many(GST_BIN(pipeline), source, decode, convert, sink, NULL);
  103. // link source to decode
  104. gst_element_link(source, decode);
  105. // caps
  106. GstCaps *caps;
  107. caps = gst_caps_new_simple("audio/x-raw-int",
  108. "rate", G_TYPE_INT, rate,
  109. "channels", G_TYPE_INT, channels,
  110. "width", G_TYPE_INT, depth,
  111. "depth", G_TYPE_INT, depth,
  112. "signed", G_TYPE_BOOLEAN, output_signed,
  113. NULL);
  114. // link convert to sink
  115. gst_element_link_filtered(convert, sink, caps);
  116. gst_caps_unref(caps);
  117. // start playing
  118. gst_element_set_state(GST_ELEMENT(pipeline), GST_STATE_PLAYING);
  119.  
  120. // iterate
  121. g_print("Running...n");
  122. g_main_loop_run(loop);
  123.  
  124. // out of the main loop, clean up nicely
  125. g_print("Returned, stopping playbackn");
  126. gst_element_set_state(pipeline, GST_STATE_NULL);
  127.  
  128. g_print("Deleting pipelinen");
  129. gst_object_unref(GST_OBJECT(pipeline));
  130. g_source_remove (bus_watch_id);
  131. g_main_loop_unref(loop);
  132.  
  133. // get data
  134. g_print("get datan");
  135. out_data = g_memory_output_stream_get_data(G_MEMORY_OUTPUT_STREAM(stream));
  136.  
  137. unsigned long size = g_memory_output_stream_get_size(G_MEMORY_OUTPUT_STREAM(stream));
  138. unsigned long sizeData = g_memory_output_stream_get_data_size(G_MEMORY_OUTPUT_STREAM(stream));
  139. std::cout << "stream size: " << size << std::endl;
  140. std::cout << "stream data size: " << sizeData << std::endl;
  141.  
  142. for (int i = 0; i < 5; ++i) {
  143. // std::cout << out_data[i] << std::endl; // not working
  144. }
  145. return 0;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement