Guest User

Untitled

a guest
Jun 22nd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. // testpipeline.c
  2. // create a dummy pipeline to view the camera feed
  3. //
  4.  
  5. #include <gst/gst.h>
  6.  
  7. GMainLoop *main_loop;
  8.  
  9. static gboolean my_bus_callback (GstBus *bus, GstMessage *msg, gpointer data) {
  10. switch (GST_MESSAGE_TYPE (msg)) {
  11. case GST_MESSAGE_ERROR: {
  12. GError *err;
  13. gchar *debug;
  14.  
  15. gst_message_parse_error (msg, &err, &debug);
  16. g_print ("Error: %s\n", err->message);
  17. g_error_free (err);
  18. g_free (debug);
  19.  
  20. g_main_loop_quit (main_loop);
  21. break;
  22. }
  23. }
  24.  
  25. return TRUE;
  26. }
  27.  
  28. GstElement *__video_tee;
  29.  
  30. gboolean build_video_source (GstElement *pipeline) {
  31. // build the pipeline that goes from source to tee
  32. g_debug ("Building video source ...");
  33. GstElement *bin = gst_bin_new ("video-source");
  34.  
  35. GstElement *src = gst_element_factory_make ("v4l2src", NULL);
  36. GstElement *caps = gst_element_factory_make ("capsfilter", NULL);
  37. GstElement *col = gst_element_factory_make ("ffmpegcolorspace", NULL);
  38.  
  39. if (!src || !caps || !col) {
  40. g_warning ("Failed to create one of the elements for basic video source.");
  41.  
  42. gst_object_unref (bin);
  43.  
  44. gst_object_unref (src);
  45. gst_object_unref (caps);
  46. gst_object_unref (col);
  47.  
  48. return FALSE;
  49. }
  50.  
  51. GstCaps *c = gst_caps_new_simple ("video/x-raw-yuv",
  52. "width", G_TYPE_INT, 640,
  53. "height", G_TYPE_INT, 480,
  54. "framerate", GST_TYPE_FRACTION, 30, 1,
  55. NULL);
  56.  
  57. g_object_set (G_OBJECT (caps), "caps", c, NULL);
  58. gst_caps_unref (c);
  59.  
  60. g_debug ("Attaching source elements ...");
  61. gst_bin_add_many (GST_BIN(bin), src, caps, col, NULL);
  62. if (!gst_element_link_many (src, caps, col, NULL)) {
  63. g_warning ("Failed to link video source elements.");
  64.  
  65. gst_object_unref (bin);
  66. return FALSE;
  67. }
  68.  
  69. GstPad *colspace_src = gst_element_get_static_pad (col, "src");
  70. g_assert (colspace_src);
  71.  
  72. // create our ghost pad for the outside world
  73. GstPad *gp = gst_ghost_pad_new ("src", colspace_src);
  74. if (!gp) {
  75. g_warning ("Failed to create ghost pad for video source.");
  76.  
  77. gst_object_unref (colspace_src);
  78. gst_object_unref (bin);
  79. }
  80.  
  81. gst_object_unref (colspace_src);
  82.  
  83. // Now create our tee
  84. __video_tee = gst_element_factory_make ("tee", NULL);
  85. if (!__video_tee) {
  86. g_warning ("Failed to create splitter tee.");
  87.  
  88. gst_object_unref (bin);
  89. return FALSE;
  90. }
  91.  
  92. GstPad *tee_sink = gst_element_get_static_pad (__video_tee, "sink");
  93. if (!tee_sink) {
  94. g_warning ("Failed to query video splitter tee for sink pad.");
  95.  
  96. gst_object_unref (bin);
  97. return FALSE;
  98. }
  99.  
  100. gst_bin_add_many (GST_BIN(pipeline), bin, __video_tee, NULL);
  101. if (GST_PAD_LINK_FAILED (gst_pad_link (gp, tee_sink))) {
  102. g_warning ("Failed to attach video source to splitter tee.");
  103.  
  104. gst_bin_remove_many (GST_BIN (pipeline), bin, __video_tee, NULL);
  105. gst_object_unref (bin);
  106. gst_object_unref (__video_tee);
  107.  
  108. return FALSE;
  109. }
  110.  
  111. return TRUE;
  112. }
  113.  
  114. gboolean attach_video_view (GstElement *pipeline) {
  115. g_debug ("Attaching video view ...");
  116.  
  117. GstElement *view_bin = gst_bin_new ("view-bin");
  118.  
  119. GstElement *sink = gst_element_factory_make ("xvimagesink", NULL);
  120. GstElement *col = gst_element_factory_make ("ffmpegcolorspace", NULL);
  121. GstElement *queue = gst_element_factory_make ("queue", NULL);
  122.  
  123. if (!sink || !col || !queue) {
  124. g_warning ("Failed to create basic viewer elements.");
  125.  
  126. gst_object_unref (sink);
  127. gst_object_unref (col);
  128. gst_object_unref (queue);
  129.  
  130. return FALSE;
  131. }
  132.  
  133. gst_bin_add_many (GST_BIN (view_bin), queue, col, sink, NULL);
  134. if (!gst_element_link_many (queue, col, sink, NULL)) {
  135. g_warning ("Failed to link viewer elements.");
  136.  
  137. gst_object_unref (view_bin);
  138. return FALSE;
  139. }
  140.  
  141. gst_bin_add_many (GST_BIN (pipeline), view_bin, NULL);
  142.  
  143. GstPad *queue_sink = gst_element_get_static_pad (queue, "sink");
  144. g_assert (queue_sink != NULL);
  145.  
  146. GstPad *gp = gst_ghost_pad_new ("src", queue_sink);
  147. gst_object_unref (queue_sink);
  148.  
  149. // Now request a new pad from the tee
  150. GstPad *tee_src = gst_element_get_request_pad (__video_tee, "src%d");
  151. g_assert (tee_src);
  152.  
  153. if (GST_PAD_LINK_FAILED (gst_pad_link (tee_src, gp))) {
  154. g_warning ("Failed to link tee source to viewer pipeline");
  155.  
  156. return FALSE;
  157. }
  158.  
  159. // otherwise we're good!
  160. }
  161.  
  162. int main (int argc, char *argv[]) {
  163. // make a dummy pipeline and run it
  164. //
  165. g_debug ("Starting up ...");
  166. main_loop = g_main_loop_new (NULL, FALSE);
  167.  
  168. gst_init (&argc, &argv);
  169.  
  170. g_debug ("Building pipeline ...");
  171. GstElement *pipeline = gst_pipeline_new ("mypipeline");
  172.  
  173. if (!build_video_source (pipeline)) {
  174. g_warning ("Failed to build video source.");
  175.  
  176. gst_object_unref (pipeline);
  177. return -1;
  178. }
  179.  
  180. if (!attach_video_view (pipeline)) {
  181. g_warning ("Failed to build video viewer.");
  182.  
  183. gst_object_unref (pipeline);
  184. return -1;
  185. }
  186.  
  187. g_debug ("Attaching hooks to pipeline ...");
  188. GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
  189. gst_bus_add_watch (bus, my_bus_callback, NULL);
  190. gst_object_unref (bus);
  191.  
  192.  
  193. g_debug ("Done building pipeline. Starting up ...");
  194. gst_element_set_state (pipeline, GST_STATE_PLAYING);
  195.  
  196. g_debug ("Starting main loop ..");
  197. g_main_loop_run (main_loop);
  198.  
  199. gst_element_set_state (pipeline, GST_STATE_NULL);
  200. gst_object_unref (pipeline);
  201. g_main_loop_unref (main_loop);
  202.  
  203. return 0;
  204. }
Add Comment
Please, Sign In to add comment