7cb9c1372f1a90435cff3910d35e2e92e20f449d
[platform/upstream/gstreamer.git] / subprojects / gst-docs / examples / tutorials / basic-tutorial-7.c
1 #include <gst/gst.h>
2
3 int
4 main (int argc, char *argv[])
5 {
6   GstElement *pipeline, *audio_source, *tee, *audio_queue, *audio_convert,
7       *audio_resample, *audio_sink;
8   GstElement *video_queue, *visual, *video_convert, *video_sink;
9   GstBus *bus;
10   GstMessage *msg;
11   GstPad *tee_audio_pad, *tee_video_pad;
12   GstPad *queue_audio_pad, *queue_video_pad;
13
14   /* Initialize GStreamer */
15   gst_init (&argc, &argv);
16
17   /* Create the elements */
18   audio_source = gst_element_factory_make ("audiotestsrc", "audio_source");
19   tee = gst_element_factory_make ("tee", "tee");
20   audio_queue = gst_element_factory_make ("queue", "audio_queue");
21   audio_convert = gst_element_factory_make ("audioconvert", "audio_convert");
22   audio_resample = gst_element_factory_make ("audioresample", "audio_resample");
23   audio_sink = gst_element_factory_make ("autoaudiosink", "audio_sink");
24   video_queue = gst_element_factory_make ("queue", "video_queue");
25   visual = gst_element_factory_make ("wavescope", "visual");
26   video_convert = gst_element_factory_make ("videoconvert", "video_convert");
27   video_sink = gst_element_factory_make ("autovideosink", "video_sink");
28
29   /* Create the empty pipeline */
30   pipeline = gst_pipeline_new ("test-pipeline");
31
32   if (!pipeline || !audio_source || !tee || !audio_queue || !audio_convert
33       || !audio_resample || !audio_sink || !video_queue || !visual
34       || !video_convert || !video_sink) {
35     g_printerr ("Not all elements could be created.\n");
36     return -1;
37   }
38
39   /* Configure elements */
40   g_object_set (audio_source, "freq", 215.0f, NULL);
41   g_object_set (visual, "shader", 0, "style", 1, NULL);
42
43   /* Link all elements that can be automatically linked because they have "Always" pads */
44   gst_bin_add_many (GST_BIN (pipeline), audio_source, tee, audio_queue,
45       audio_convert, audio_resample, audio_sink, video_queue, visual,
46       video_convert, video_sink, NULL);
47   if (gst_element_link_many (audio_source, tee, NULL) != TRUE
48       || gst_element_link_many (audio_queue, audio_convert, audio_resample,
49           audio_sink, NULL) != TRUE
50       || gst_element_link_many (video_queue, visual, video_convert, video_sink,
51           NULL) != TRUE) {
52     g_printerr ("Elements could not be linked.\n");
53     gst_object_unref (pipeline);
54     return -1;
55   }
56
57   /* Manually link the Tee, which has "Request" pads */
58   tee_audio_pad = gst_element_request_pad_simple (tee, "src_%u");
59   g_print ("Obtained request pad %s for audio branch.\n",
60       gst_pad_get_name (tee_audio_pad));
61   queue_audio_pad = gst_element_get_static_pad (audio_queue, "sink");
62   tee_video_pad = gst_element_request_pad_simple (tee, "src_%u");
63   g_print ("Obtained request pad %s for video branch.\n",
64       gst_pad_get_name (tee_video_pad));
65   queue_video_pad = gst_element_get_static_pad (video_queue, "sink");
66   if (gst_pad_link (tee_audio_pad, queue_audio_pad) != GST_PAD_LINK_OK ||
67       gst_pad_link (tee_video_pad, queue_video_pad) != GST_PAD_LINK_OK) {
68     g_printerr ("Tee could not be linked.\n");
69     gst_object_unref (pipeline);
70     return -1;
71   }
72   gst_object_unref (queue_audio_pad);
73   gst_object_unref (queue_video_pad);
74
75   /* Start playing the pipeline */
76   gst_element_set_state (pipeline, GST_STATE_PLAYING);
77
78   /* Wait until error or EOS */
79   bus = gst_element_get_bus (pipeline);
80   msg =
81       gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE,
82       GST_MESSAGE_ERROR | GST_MESSAGE_EOS);
83
84   /* Release the request pads from the Tee, and unref them */
85   gst_element_release_request_pad (tee, tee_audio_pad);
86   gst_element_release_request_pad (tee, tee_video_pad);
87   gst_object_unref (tee_audio_pad);
88   gst_object_unref (tee_video_pad);
89
90   /* Free resources */
91   if (msg != NULL)
92     gst_message_unref (msg);
93   gst_object_unref (bus);
94   gst_element_set_state (pipeline, GST_STATE_NULL);
95
96   gst_object_unref (pipeline);
97   return 0;
98 }