2 * Copyright (C) 2013 Collabora Ltd.
3 * @author Torrie Fischer <torrie.fischer@collabora.co.uk>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
21 #include <gst/rtp/rtp.h>
25 * RTP receiver with RFC4588 retransmission handling enabled
27 * In this example we have two RTP sessions, one for video and one for audio.
28 * Video is received on port 5000, with its RTCP stream received on port 5001
29 * and sent on port 5005. Audio is received on port 5005, with its RTCP stream
30 * received on port 5006 and sent on port 5011.
32 * In both sessions, we set "rtprtxreceive" as the session's "aux" element
33 * in rtpbin, which enables RFC4588 retransmission handling for that session.
35 * .-------. .----------. .-----------. .---------. .-------------.
36 * RTP |udpsrc | | rtpbin | |theoradepay| |theoradec| |autovideosink|
37 * port=5000 | src->recv_rtp_0 recv_rtp_0->sink src->sink src->sink |
38 * '-------' | | '-----------' '---------' '-------------'
42 * | send_rtcp_0->sink | port=5005
43 * .-------. | | '-------' sync=false
44 * RTCP |udpsrc | | | async=false
45 * port=5001 | src->recv_rtcp_0 |
48 * .-------. | | .---------. .-------. .-------------.
49 * RTP |udpsrc | | | |pcmadepay| |alawdec| |autoaudiosink|
50 * port=5006 | src->recv_rtp_1 recv_rtp_1->sink src->sink src->sink |
51 * '-------' | | '---------' '-------' '-------------'
55 * | send_rtcp_1->sink | port=5011
56 * .-------. | | '-------' sync=false
57 * RTCP |udpsrc | | | async=false
58 * port=5007 | src->recv_rtcp_1 |
59 * '-------' '----------'
63 GMainLoop *loop = NULL;
65 typedef struct _SessionData
76 session_ref (SessionData * data)
78 g_atomic_int_inc (&data->ref);
83 session_unref (gpointer data)
85 SessionData *session = (SessionData *) data;
86 if (g_atomic_int_dec_and_test (&session->ref)) {
87 g_object_unref (session->rtpbin);
88 gst_caps_unref (session->caps);
94 session_new (guint sessionNum)
96 SessionData *ret = g_new0 (SessionData, 1);
97 ret->sessionNum = sessionNum;
98 return session_ref (ret);
102 setup_ghost_sink (GstElement * sink, GstBin * bin)
104 GstPad *sinkPad = gst_element_get_static_pad (sink, "sink");
105 GstPad *binPad = gst_ghost_pad_new ("sink", sinkPad);
106 gst_element_add_pad (GST_ELEMENT (bin), binPad);
110 make_audio_session (guint sessionNum)
112 SessionData *ret = session_new (sessionNum);
113 GstBin *bin = GST_BIN (gst_bin_new ("audio"));
114 GstElement *queue = gst_element_factory_make ("queue", NULL);
115 GstElement *sink = gst_element_factory_make ("autoaudiosink", NULL);
116 GstElement *audioconvert = gst_element_factory_make ("audioconvert", NULL);
117 GstElement *audioresample = gst_element_factory_make ("audioresample", NULL);
118 GstElement *depayloader = gst_element_factory_make ("rtppcmadepay", NULL);
119 GstElement *decoder = gst_element_factory_make ("alawdec", NULL);
121 gst_bin_add_many (bin, queue, depayloader, decoder, audioconvert,
122 audioresample, sink, NULL);
123 gst_element_link_many (queue, depayloader, decoder, audioconvert,
124 audioresample, sink, NULL);
126 setup_ghost_sink (queue, bin);
128 ret->output = GST_ELEMENT (bin);
129 ret->caps = gst_caps_new_simple ("application/x-rtp",
130 "media", G_TYPE_STRING, "audio",
131 "clock-rate", G_TYPE_INT, 8000,
132 "encoding-name", G_TYPE_STRING, "PCMA", NULL);
140 make_video_session (guint sessionNum)
142 SessionData *ret = session_new (sessionNum);
143 GstBin *bin = GST_BIN (gst_bin_new ("video"));
144 GstElement *queue = gst_element_factory_make ("queue", NULL);
145 GstElement *depayloader = gst_element_factory_make ("rtptheoradepay", NULL);
146 GstElement *decoder = gst_element_factory_make ("theoradec", NULL);
147 GstElement *converter = gst_element_factory_make ("videoconvert", NULL);
148 GstElement *sink = gst_element_factory_make ("autovideosink", NULL);
150 gst_bin_add_many (bin, depayloader, decoder, converter, queue, sink, NULL);
151 gst_element_link_many (queue, depayloader, decoder, converter, sink, NULL);
153 setup_ghost_sink (queue, bin);
155 ret->output = GST_ELEMENT (bin);
156 ret->caps = gst_caps_new_simple ("application/x-rtp",
157 "media", G_TYPE_STRING, "video",
158 "clock-rate", G_TYPE_INT, 90000,
159 "encoding-name", G_TYPE_STRING, "THEORA", NULL);
167 request_pt_map (GstElement * rtpbin, guint session, guint pt,
170 SessionData *data = (SessionData *) user_data;
172 g_print ("Looking for caps for pt %u in session %u, have %u\n", pt, session,
174 if (session == data->sessionNum) {
177 if (pt == data->pt) {
178 caps = gst_caps_ref (data->caps);
179 } else if (pt == data->rtxPt) {
180 caps = gst_caps_copy (data->caps);
181 gst_caps_set_simple (caps, "encoding-name", G_TYPE_STRING, "rtx", NULL);
186 caps_str = gst_caps_to_string (caps);
187 g_print ("Returning %s\n", caps_str);
196 cb_eos (GstBus * bus, GstMessage * message, gpointer data)
198 g_print ("Got EOS\n");
199 g_main_loop_quit (loop);
203 cb_state (GstBus * bus, GstMessage * message, gpointer data)
205 GstObject *pipe = GST_OBJECT (data);
206 GstState old, new, pending;
207 gst_message_parse_state_changed (message, &old, &new, &pending);
208 if (message->src == pipe) {
209 g_print ("Pipeline %s changed state from %s to %s\n",
210 GST_OBJECT_NAME (message->src),
211 gst_element_state_get_name (old), gst_element_state_get_name (new));
216 cb_warning (GstBus * bus, GstMessage * message, gpointer data)
218 GError *error = NULL;
219 gst_message_parse_warning (message, &error, NULL);
220 g_printerr ("Got warning from %s: %s\n", GST_OBJECT_NAME (message->src),
222 g_error_free (error);
226 cb_error (GstBus * bus, GstMessage * message, gpointer data)
228 GError *error = NULL;
229 gst_message_parse_error (message, &error, NULL);
230 g_printerr ("Got error from %s: %s\n", GST_OBJECT_NAME (message->src),
232 g_error_free (error);
233 g_main_loop_quit (loop);
237 handle_new_stream (GstElement * element, GstPad * newPad, gpointer data)
239 SessionData *session = (SessionData *) data;
243 padName = gst_pad_get_name (newPad);
244 myPrefix = g_strdup_printf ("recv_rtp_src_%u", session->sessionNum);
246 g_print ("New pad: %s, looking for %s_*\n", padName, myPrefix);
248 if (g_str_has_prefix (padName, myPrefix)) {
249 GstPad *outputSinkPad;
252 parent = GST_ELEMENT (gst_element_get_parent (session->rtpbin));
253 gst_bin_add (GST_BIN (parent), session->output);
254 gst_element_sync_state_with_parent (session->output);
255 gst_object_unref (parent);
257 outputSinkPad = gst_element_get_static_pad (session->output, "sink");
258 g_assert_cmpint (gst_pad_link (newPad, outputSinkPad), ==, GST_PAD_LINK_OK);
259 gst_object_unref (outputSinkPad);
261 g_print ("Linked!\n");
268 request_aux_receiver (GstElement * rtpbin, guint sessid, SessionData * session)
270 GstElement *rtx, *bin;
273 GstStructure *pt_map;
276 if (sessid != session->sessionNum)
279 GST_INFO ("creating AUX receiver for session %u", sessid);
280 bin = gst_bin_new (NULL);
281 rtx = gst_element_factory_make ("rtprtxreceive", NULL);
283 media_pt = g_strdup_printf ("%d", session->pt);
284 pt_map = gst_structure_new ("application/x-rtp-pt-map",
285 media_pt, G_TYPE_UINT, session->rtxPt, NULL);
286 g_object_set (rtx, "payload-type-map", pt_map, NULL);
287 gst_structure_free (pt_map);
290 gst_bin_add (GST_BIN (bin), rtx);
292 pad = gst_element_get_static_pad (rtx, "src");
293 name = g_strdup_printf ("src_%u", sessid);
294 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
296 gst_object_unref (pad);
298 pad = gst_element_get_static_pad (rtx, "sink");
299 name = g_strdup_printf ("sink_%u", sessid);
300 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
302 gst_object_unref (pad);
308 join_session (GstElement * pipeline, GstElement * rtpBin, SessionData * session)
312 GstElement *rtcpSink;
316 g_print ("Joining session %p\n", session);
318 session->rtpbin = g_object_ref (rtpBin);
320 basePort = 5000 + (session->sessionNum * 6);
322 rtpSrc = gst_element_factory_make ("udpsrc", NULL);
323 rtcpSrc = gst_element_factory_make ("udpsrc", NULL);
324 rtcpSink = gst_element_factory_make ("udpsink", NULL);
325 g_object_set (rtpSrc, "port", basePort, "caps", session->caps, NULL);
326 g_object_set (rtcpSink, "port", basePort + 5, "host", "127.0.0.1", "sync",
327 FALSE, "async", FALSE, NULL);
328 g_object_set (rtcpSrc, "port", basePort + 1, NULL);
330 g_print ("Connecting to %i/%i/%i\n", basePort, basePort + 1, basePort + 5);
332 /* enable RFC4588 retransmission handling by setting rtprtxreceive
333 * as the "aux" element of rtpbin */
334 g_signal_connect (rtpBin, "request-aux-receiver",
335 (GCallback) request_aux_receiver, session);
337 gst_bin_add_many (GST_BIN (pipeline), rtpSrc, rtcpSrc, rtcpSink, NULL);
339 g_signal_connect_data (rtpBin, "pad-added", G_CALLBACK (handle_new_stream),
340 session_ref (session), (GClosureNotify) session_unref, 0);
342 g_signal_connect_data (rtpBin, "request-pt-map", G_CALLBACK (request_pt_map),
343 session_ref (session), (GClosureNotify) session_unref, 0);
345 padName = g_strdup_printf ("recv_rtp_sink_%u", session->sessionNum);
346 gst_element_link_pads (rtpSrc, "src", rtpBin, padName);
349 padName = g_strdup_printf ("recv_rtcp_sink_%u", session->sessionNum);
350 gst_element_link_pads (rtcpSrc, "src", rtpBin, padName);
353 padName = g_strdup_printf ("send_rtcp_src_%u", session->sessionNum);
354 gst_element_link_pads (rtpBin, padName, rtcpSink, "sink");
357 session_unref (session);
361 main (int argc, char **argv)
364 SessionData *videoSession;
365 SessionData *audioSession;
369 gst_init (&argc, &argv);
371 loop = g_main_loop_new (NULL, FALSE);
372 pipe = GST_PIPELINE (gst_pipeline_new (NULL));
374 bus = gst_element_get_bus (GST_ELEMENT (pipe));
375 g_signal_connect (bus, "message::error", G_CALLBACK (cb_error), pipe);
376 g_signal_connect (bus, "message::warning", G_CALLBACK (cb_warning), pipe);
377 g_signal_connect (bus, "message::state-changed", G_CALLBACK (cb_state), pipe);
378 g_signal_connect (bus, "message::eos", G_CALLBACK (cb_eos), NULL);
379 gst_bus_add_signal_watch (bus);
380 gst_object_unref (bus);
382 rtpBin = gst_element_factory_make ("rtpbin", NULL);
383 gst_bin_add (GST_BIN (pipe), rtpBin);
384 g_object_set (rtpBin, "latency", 200, "do-retransmission", TRUE,
385 "rtp-profile", GST_RTP_PROFILE_AVPF, NULL);
387 videoSession = make_video_session (0);
388 audioSession = make_audio_session (1);
390 join_session (GST_ELEMENT (pipe), rtpBin, videoSession);
391 join_session (GST_ELEMENT (pipe), rtpBin, audioSession);
393 g_print ("starting client pipeline\n");
394 gst_element_set_state (GST_ELEMENT (pipe), GST_STATE_PLAYING);
396 g_main_loop_run (loop);
398 g_print ("stopping client pipeline\n");
399 gst_element_set_state (GST_ELEMENT (pipe), GST_STATE_NULL);
401 gst_object_unref (pipe);
402 g_main_loop_unref (loop);