1 /* GStreamer
2 * Copyright (C) 2009 Wim Taymans <wim.taymans@gmail.com>
3 *
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.
8 *
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.
13 *
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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #include <string.h>
21 #include <math.h>
22
23 #include <gst/gst.h>
24
25 /*
26 * A simple RTP receiver
27 *
28 * receives alaw encoded RTP audio on port 5002, RTCP is received on port 5003.
29 * the receiver RTCP reports are sent to port 5007
30 *
31 * .-------. .----------. .---------. .-------. .--------.
32 * RTP |udpsrc | | rtpbin | |pcmadepay| |alawdec| |alsasink|
33 * port=5002 | src->recv_rtp recv_rtp->sink src->sink src->sink |
34 * '-------' | | '---------' '-------' '--------'
35 * | |
36 * | | .-------.
37 * | | |udpsink| RTCP
38 * | send_rtcp->sink | port=5007
39 * .-------. | | '-------' sync=false
40 * RTCP |udpsrc | | | async=false
41 * port=5003 | src->recv_rtcp |
42 * '-------' '----------'
43 */
44
45 /* the caps of the sender RTP stream. This is usually negotiated out of band with
46 * SDP or RTSP. */
47 #define AUDIO_CAPS "application/x-rtp,media=(string)audio,clock-rate=(int)8000,encoding-name=(string)PCMA"
48
49 #define AUDIO_DEPAY "rtppcmadepay"
50 #define AUDIO_DEC "alawdec"
51 #define AUDIO_SINK "autoaudiosink"
52
53 /* the destination machine to send RTCP to. This is the address of the sender and
54 * is used to send back the RTCP reports of this receiver. If the data is sent
55 * from another machine, change this address. */
56 #define DEST_HOST "127.0.0.1"
57
58 /* print the stats of a source */
59 static void
print_source_stats(GObject * source)60 print_source_stats (GObject * source)
61 {
62 GstStructure *stats;
63 gchar *str;
64
65 g_return_if_fail (source != NULL);
66
67 /* get the source stats */
68 g_object_get (source, "stats", &stats, NULL);
69
70 /* simply dump the stats structure */
71 str = gst_structure_to_string (stats);
72 g_print ("source stats: %s\n", str);
73
74 gst_structure_free (stats);
75 g_free (str);
76 }
77
78 /* will be called when rtpbin signals on-ssrc-active. It means that an RTCP
79 * packet was received from another source. */
80 static void
on_ssrc_active_cb(GstElement * rtpbin,guint sessid,guint ssrc,GstElement * depay)81 on_ssrc_active_cb (GstElement * rtpbin, guint sessid, guint ssrc,
82 GstElement * depay)
83 {
84 GObject *session, *osrc;
85
86 g_print ("got RTCP from session %u, SSRC %u\n", sessid, ssrc);
87
88 /* get the right session */
89 g_signal_emit_by_name (rtpbin, "get-internal-session", sessid, &session);
90
91 #if 0
92 /* FIXME: This is broken in rtpbin */
93 /* get the internal source (the SSRC allocated to us, the receiver */
94 g_object_get (session, "internal-source", &isrc, NULL);
95 print_source_stats (isrc);
96 g_object_unref (isrc);
97 #endif
98
99 /* get the remote source that sent us RTCP */
100 g_signal_emit_by_name (session, "get-source-by-ssrc", ssrc, &osrc);
101 print_source_stats (osrc);
102 g_object_unref (osrc);
103 g_object_unref (session);
104 }
105
106 /* will be called when rtpbin has validated a payload that we can depayload */
107 static void
pad_added_cb(GstElement * rtpbin,GstPad * new_pad,GstElement * depay)108 pad_added_cb (GstElement * rtpbin, GstPad * new_pad, GstElement * depay)
109 {
110 GstPad *sinkpad;
111 GstPadLinkReturn lres;
112
113 g_print ("new payload on pad: %s\n", GST_PAD_NAME (new_pad));
114
115 sinkpad = gst_element_get_static_pad (depay, "sink");
116 g_assert (sinkpad);
117
118 lres = gst_pad_link (new_pad, sinkpad);
119 g_assert (lres == GST_PAD_LINK_OK);
120 gst_object_unref (sinkpad);
121 }
122
123 /* build a pipeline equivalent to:
124 *
125 * gst-launch-1.0 -v rtpbin name=rtpbin \
126 * udpsrc caps=$AUDIO_CAPS port=5002 ! rtpbin.recv_rtp_sink_0 \
127 * rtpbin. ! rtppcmadepay ! alawdec ! audioconvert ! audioresample ! autoaudiosink \
128 * udpsrc port=5003 ! rtpbin.recv_rtcp_sink_0 \
129 * rtpbin.send_rtcp_src_0 ! udpsink port=5007 host=$DEST sync=false async=false
130 */
131 int
main(int argc,char * argv[])132 main (int argc, char *argv[])
133 {
134 GstElement *rtpbin, *rtpsrc, *rtcpsrc, *rtcpsink;
135 GstElement *audiodepay, *audiodec, *audiores, *audioconv, *audiosink;
136 GstElement *pipeline;
137 GMainLoop *loop;
138 GstCaps *caps;
139 gboolean res;
140 GstPadLinkReturn lres;
141 GstPad *srcpad, *sinkpad;
142
143 /* always init first */
144 gst_init (&argc, &argv);
145
146 /* the pipeline to hold everything */
147 pipeline = gst_pipeline_new (NULL);
148 g_assert (pipeline);
149
150 /* the udp src and source we will use for RTP and RTCP */
151 rtpsrc = gst_element_factory_make ("udpsrc", "rtpsrc");
152 g_assert (rtpsrc);
153 g_object_set (rtpsrc, "port", 5002, NULL);
154 /* we need to set caps on the udpsrc for the RTP data */
155 caps = gst_caps_from_string (AUDIO_CAPS);
156 g_object_set (rtpsrc, "caps", caps, NULL);
157 gst_caps_unref (caps);
158
159 rtcpsrc = gst_element_factory_make ("udpsrc", "rtcpsrc");
160 g_assert (rtcpsrc);
161 g_object_set (rtcpsrc, "port", 5003, NULL);
162
163 rtcpsink = gst_element_factory_make ("udpsink", "rtcpsink");
164 g_assert (rtcpsink);
165 g_object_set (rtcpsink, "port", 5007, "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);
168
169 gst_bin_add_many (GST_BIN (pipeline), rtpsrc, rtcpsrc, rtcpsink, NULL);
170
171 /* the depayloading and decoding */
172 audiodepay = gst_element_factory_make (AUDIO_DEPAY, "audiodepay");
173 g_assert (audiodepay);
174 audiodec = gst_element_factory_make (AUDIO_DEC, "audiodec");
175 g_assert (audiodec);
176 /* the audio playback and format conversion */
177 audioconv = gst_element_factory_make ("audioconvert", "audioconv");
178 g_assert (audioconv);
179 audiores = gst_element_factory_make ("audioresample", "audiores");
180 g_assert (audiores);
181 audiosink = gst_element_factory_make (AUDIO_SINK, "audiosink");
182 g_assert (audiosink);
183
184 /* add depayloading and playback to the pipeline and link */
185 gst_bin_add_many (GST_BIN (pipeline), audiodepay, audiodec, audioconv,
186 audiores, audiosink, NULL);
187
188 res = gst_element_link_many (audiodepay, audiodec, audioconv, audiores,
189 audiosink, NULL);
190 g_assert (res == TRUE);
191
192 /* the rtpbin element */
193 rtpbin = gst_element_factory_make ("rtpbin", "rtpbin");
194 g_assert (rtpbin);
195
196 gst_bin_add (GST_BIN (pipeline), rtpbin);
197
198 /* now link all to the rtpbin, start by getting an RTP sinkpad for session 0 */
199 srcpad = gst_element_get_static_pad (rtpsrc, "src");
200 sinkpad = gst_element_request_pad_simple (rtpbin, "recv_rtp_sink_0");
201 lres = gst_pad_link (srcpad, sinkpad);
202 g_assert (lres == GST_PAD_LINK_OK);
203 gst_object_unref (srcpad);
204
205 /* get an RTCP sinkpad in session 0 */
206 srcpad = gst_element_get_static_pad (rtcpsrc, "src");
207 sinkpad = gst_element_request_pad_simple (rtpbin, "recv_rtcp_sink_0");
208 lres = gst_pad_link (srcpad, sinkpad);
209 g_assert (lres == GST_PAD_LINK_OK);
210 gst_object_unref (srcpad);
211 gst_object_unref (sinkpad);
212
213 /* get an RTCP srcpad for sending RTCP back to the sender */
214 srcpad = gst_element_request_pad_simple (rtpbin, "send_rtcp_src_0");
215 sinkpad = gst_element_get_static_pad (rtcpsink, "sink");
216 lres = gst_pad_link (srcpad, sinkpad);
217 g_assert (lres == GST_PAD_LINK_OK);
218 gst_object_unref (sinkpad);
219
220 /* the RTP pad that we have to connect to the depayloader will be created
221 * dynamically so we connect to the pad-added signal, pass the depayloader as
222 * user_data so that we can link to it. */
223 g_signal_connect (rtpbin, "pad-added", G_CALLBACK (pad_added_cb), audiodepay);
224
225 /* give some stats when we receive RTCP */
226 g_signal_connect (rtpbin, "on-ssrc-active", G_CALLBACK (on_ssrc_active_cb),
227 audiodepay);
228
229 /* set the pipeline to playing */
230 g_print ("starting receiver pipeline\n");
231 gst_element_set_state (pipeline, GST_STATE_PLAYING);
232
233 /* we need to run a GLib main loop to get the messages */
234 loop = g_main_loop_new (NULL, FALSE);
235 g_main_loop_run (loop);
236
237 g_print ("stopping receiver pipeline\n");
238 gst_element_set_state (pipeline, GST_STATE_NULL);
239
240 gst_object_unref (pipeline);
241
242 return 0;
243 }
244