2 * Copyright (C) 2009 Wim Taymans <wim.taymans@gmail.com>
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.
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.
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.
27 * sends the output of alsasrc as alaw encoded RTP on port 5002, RTCP is sent on
28 * port 5003. The destination is 127.0.0.1.
29 * the receiver RTCP reports are received on port 5007
31 * .-------. .-------. .-------. .----------. .-------.
32 * |alsasrc| |alawenc| |pcmapay| | rtpbin | |udpsink| RTP
33 * | src->sink src->sink src->send_rtp send_rtp->sink | port=5002
34 * '-------' '-------' '-------' | | '-------'
38 * | send_rtcp->sink | port=5003
39 * .-------. | | '-------' sync=false
40 * RTCP |udpsrc | | | async=false
41 * port=5007 | src->recv_rtcp |
42 * '-------' '----------'
45 /* change this to send the RTP data and RTCP to another host */
46 #define DEST_HOST "127.0.0.1"
48 /* #define AUDIO_SRC "alsasrc" */
49 #define AUDIO_SRC "audiotestsrc"
51 /* the encoder and payloader elements */
52 #define AUDIO_ENC "alawenc"
53 #define AUDIO_PAY "rtppcmapay"
55 /* print the stats of a source */
57 print_source_stats (GObject * source)
62 /* get the source stats */
63 g_object_get (source, "stats", &stats, NULL);
65 /* simply dump the stats structure */
66 str = gst_structure_to_string (stats);
67 g_print ("source stats: %s\n", str);
69 gst_structure_free (stats);
73 /* this function is called every second and dumps the RTP manager stats */
75 print_stats (GstElement * rtpbin)
82 g_print ("***********************************\n");
85 g_signal_emit_by_name (rtpbin, "get-internal-session", 0, &session);
87 /* print all the sources in the session, this includes the internal source */
88 g_object_get (session, "sources", &arr, NULL);
90 for (i = 0; i < arr->n_values; i++) {
93 val = g_value_array_get_nth (arr, i);
94 source = g_value_get_object (val);
96 print_source_stats (source);
98 g_value_array_free (arr);
100 g_object_unref (session);
105 /* build a pipeline equivalent to:
107 * gst-launch -v gstrtpbin name=rtpbin \
108 * $AUDIO_SRC ! audioconvert ! audioresample ! $AUDIO_ENC ! $AUDIO_PAY ! rtpbin.send_rtp_sink_0 \
109 * rtpbin.send_rtp_src_0 ! udpsink port=5002 host=$DEST \
110 * rtpbin.send_rtcp_src_0 ! udpsink port=5003 host=$DEST sync=false async=false \
111 * udpsrc port=5007 ! rtpbin.recv_rtcp_sink_0
114 main (int argc, char *argv[])
116 GstElement *audiosrc, *audioconv, *audiores, *audioenc, *audiopay;
117 GstElement *rtpbin, *rtpsink, *rtcpsink, *rtcpsrc;
118 GstElement *pipeline;
120 GstPad *srcpad, *sinkpad;
122 /* always init first */
123 gst_init (&argc, &argv);
125 /* the pipeline to hold everything */
126 pipeline = gst_pipeline_new (NULL);
129 /* the audio capture and format conversion */
130 audiosrc = gst_element_factory_make (AUDIO_SRC, "audiosrc");
132 audioconv = gst_element_factory_make ("audioconvert", "audioconv");
133 g_assert (audioconv);
134 audiores = gst_element_factory_make ("audioresample", "audiores");
136 /* the encoding and payloading */
137 audioenc = gst_element_factory_make (AUDIO_ENC, "audioenc");
139 audiopay = gst_element_factory_make (AUDIO_PAY, "audiopay");
142 /* add capture and payloading to the pipeline and link */
143 gst_bin_add_many (GST_BIN (pipeline), audiosrc, audioconv, audiores,
144 audioenc, audiopay, NULL);
146 if (!gst_element_link_many (audiosrc, audioconv, audiores, audioenc,
148 g_error ("Failed to link audiosrc, audioconv, audioresample, "
149 "audio encoder and audio payloader");
152 /* the rtpbin element */
153 rtpbin = gst_element_factory_make ("gstrtpbin", "rtpbin");
156 gst_bin_add (GST_BIN (pipeline), rtpbin);
158 /* the udp sinks and source we will use for RTP and RTCP */
159 rtpsink = gst_element_factory_make ("udpsink", "rtpsink");
161 g_object_set (rtpsink, "port", 5002, "host", DEST_HOST, NULL);
163 rtcpsink = gst_element_factory_make ("udpsink", "rtcpsink");
165 g_object_set (rtcpsink, "port", 5003, "host", DEST_HOST, NULL);
166 /* no need for synchronisation or preroll on the RTCP sink */
167 g_object_set (rtcpsink, "async", FALSE, "sync", FALSE, NULL);
169 rtcpsrc = gst_element_factory_make ("udpsrc", "rtcpsrc");
171 g_object_set (rtcpsrc, "port", 5007, NULL);
173 gst_bin_add_many (GST_BIN (pipeline), rtpsink, rtcpsink, rtcpsrc, NULL);
175 /* now link all to the rtpbin, start by getting an RTP sinkpad for session 0 */
176 sinkpad = gst_element_get_request_pad (rtpbin, "send_rtp_sink_0");
177 srcpad = gst_element_get_static_pad (audiopay, "src");
178 if (gst_pad_link (srcpad, sinkpad) != GST_PAD_LINK_OK)
179 g_error ("Failed to link audio payloader to rtpbin");
180 gst_object_unref (srcpad);
182 /* get the RTP srcpad that was created when we requested the sinkpad above and
183 * link it to the rtpsink sinkpad*/
184 srcpad = gst_element_get_static_pad (rtpbin, "send_rtp_src_0");
185 sinkpad = gst_element_get_static_pad (rtpsink, "sink");
186 if (gst_pad_link (srcpad, sinkpad) != GST_PAD_LINK_OK)
187 g_error ("Failed to link rtpbin to rtpsink");
188 gst_object_unref (srcpad);
189 gst_object_unref (sinkpad);
191 /* get an RTCP srcpad for sending RTCP to the receiver */
192 srcpad = gst_element_get_request_pad (rtpbin, "send_rtcp_src_0");
193 sinkpad = gst_element_get_static_pad (rtcpsink, "sink");
194 if (gst_pad_link (srcpad, sinkpad) != GST_PAD_LINK_OK)
195 g_error ("Failed to link rtpbin to rtcpsink");
196 gst_object_unref (sinkpad);
198 /* we also want to receive RTCP, request an RTCP sinkpad for session 0 and
199 * link it to the srcpad of the udpsrc for RTCP */
200 srcpad = gst_element_get_static_pad (rtcpsrc, "src");
201 sinkpad = gst_element_get_request_pad (rtpbin, "recv_rtcp_sink_0");
202 if (gst_pad_link (srcpad, sinkpad) != GST_PAD_LINK_OK)
203 g_error ("Failed to link rtcpsrc to rtpbin");
204 gst_object_unref (srcpad);
206 /* set the pipeline to playing */
207 g_print ("starting sender pipeline\n");
208 gst_element_set_state (pipeline, GST_STATE_PLAYING);
210 /* print stats every second */
211 g_timeout_add (1000, (GSourceFunc) print_stats, rtpbin);
213 /* we need to run a GLib main loop to get the messages */
214 loop = g_main_loop_new (NULL, FALSE);
215 g_main_loop_run (loop);
217 g_print ("stopping sender pipeline\n");
218 gst_element_set_state (pipeline, GST_STATE_NULL);