2 * Copyright (C) <2007> 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.
21 * SECTION:element-gstrtpbin
22 * @short_description: handle media from one RTP bin
23 * @see_also: gstrtpjitterbuffer, gstrtpsession, gstrtpptdemux, gstrtpssrcdemux
27 * RTP bin combines the functions of gstrtpsession, gstrtpssrcdemux, gstrtpjitterbuffer
28 * and gstrtpptdemux in one element. It allows for multiple RTP sessions that will
29 * be synchronized together using RTCP SR packets.
32 * gstrtpbin is configured with a number of request pads that define the
33 * functionality that is activated, similar to the gstrtpsession element.
36 * To use gstrtpbin as an RTP receiver, request a recv_rtp_sink_%%d pad. The session
37 * number must be specified in the pad name.
38 * Data received on the recv_rtp_sink_%%d pad will be processed in the gstrtpsession
39 * manager and after being validated forwarded on gstrtpssrcdemuxer element. Each
40 * RTP stream is demuxed based on the SSRC and send to a gstrtpjitterbuffer. After
41 * the packets are released from the jitterbuffer, they will be forwarded to a
42 * gstrtpptdemuxer element. The gstrtpptdemuxer element will demux the packets based
43 * on the payload type and will create a unique pad recv_rtp_src_%%d_%%d_%%d on
44 * gstrtpbin with the session number, SSRC and payload type respectively as the pad
48 * To also use gstrtpbin as an RTCP receiver, request a recv_rtcp_sink_%%d pad. The
49 * session number must be specified in the pad name.
52 * If you want the session manager to generate and send RTCP packets, request
53 * the send_rtcp_src_%%d pad with the session number in the pad name. Packet pushed
54 * on this pad contain SR/RR RTCP reports that should be sent to all participants
58 * To use gstrtpbin as a sender, request a send_rtp_sink_%%d pad, which will
59 * automatically create a send_rtp_src_%%d pad. If the session number is not provided,
60 * the pad from the lowest available session will be returned. The session manager will modify the
61 * SSRC in the RTP packets to its own SSRC and wil forward the packets on the
62 * send_rtp_src_%%d pad after updating its internal state.
65 * The session manager needs the clock-rate of the payload types it is handling
66 * and will signal the GstRtpSession::request-pt-map signal when it needs such a
67 * mapping. One can clear the cached values with the GstRtpSession::clear-pt-map
70 * <title>Example pipelines</title>
73 * gst-launch udpsrc port=5000 caps="application/x-rtp, ..." ! .recv_rtp_sink_0 \
74 * gstrtpbin ! rtptheoradepay ! theoradec ! xvimagesink
76 * Receive RTP data from port 5000 and send to the session 0 in gstrtpbin.
80 * gst-launch gstrtpbin name=rtpbin \
81 * v4l2src ! ffmpegcolorspace ! ffenc_h263 ! rtph263ppay ! rtpbin.send_rtp_sink_0 \
82 * rtpbin.send_rtp_src_0 ! udpsink port=5000 \
83 * rtpbin.send_rtcp_src_0 ! udpsink port=5001 sync=false async=false \
84 * udpsrc port=5005 ! rtpbin.recv_rtcp_sink_0 \
85 * audiotestsrc ! amrnbenc ! rtpamrpay ! rtpbin.send_rtp_sink_1 \
86 * rtpbin.send_rtp_src_1 ! udpsink port=5002 \
87 * rtpbin.send_rtcp_src_1 ! udpsink port=5003 sync=false async=false \
88 * udpsrc port=5007 ! rtpbin.recv_rtcp_sink_1
90 * Encode and payload H263 video captured from a v4l2src. Encode and payload AMR
91 * audio generated from audiotestsrc. The video is sent to session 0 in rtpbin
92 * and the audio is sent to session 1. Video packets are sent on UDP port 5000
93 * and audio packets on port 5002. The video RTCP packets for session 0 are sent
94 * on port 5001 and the audio RTCP packets for session 0 are sent on port 5003.
95 * RTCP packets for session 0 are received on port 5005 and RTCP for session 1
96 * is received on port 5007. Since RTCP packets from the sender should be sent
97 * as soon as possible and do not participate in preroll, sync=false and
98 * async=false is configured on udpsink
102 * gst-launch -v gstrtpbin name=rtpbin \
103 * udpsrc caps="application/x-rtp,media=(string)video,clock-rate=(int)90000,encoding-name=(string)H263-1998" \
104 * port=5000 ! rtpbin.recv_rtp_sink_0 \
105 * rtpbin. ! rtph263pdepay ! ffdec_h263 ! xvimagesink \
106 * udpsrc port=5001 ! rtpbin.recv_rtcp_sink_0 \
107 * rtpbin.send_rtcp_src_0 ! udpsink port=5005 sync=false async=false \
108 * udpsrc caps="application/x-rtp,media=(string)audio,clock-rate=(int)8000,encoding-name=(string)AMR,encoding-params=(string)1,octet-align=(string)1" \
109 * port=5002 ! rtpbin.recv_rtp_sink_1 \
110 * rtpbin. ! rtpamrdepay ! amrnbdec ! alsasink \
111 * udpsrc port=5003 ! rtpbin.recv_rtcp_sink_1 \
112 * rtpbin.send_rtcp_src_1 ! udpsink port=5007 sync=false async=false
114 * Receive H263 on port 5000, send it through rtpbin in session 0, depayload,
115 * decode and display the video.
116 * Receive AMR on port 5002, send it through rtpbin in session 1, depayload,
117 * decode and play the audio.
118 * Receive server RTCP packets for session 0 on port 5001 and RTCP packets for
119 * session 1 on port 5003. These packets will be used for session management and
121 * Send RTCP reports for session 0 on port 5005 and RTCP reports for session 1
126 * Last reviewed on 2007-08-30 (0.10.6)
134 #include <gst/rtp/gstrtpbuffer.h>
135 #include <gst/rtp/gstrtcpbuffer.h>
137 #include "gstrtpbin-marshal.h"
138 #include "gstrtpbin.h"
140 GST_DEBUG_CATEGORY_STATIC (gst_rtp_bin_debug);
141 #define GST_CAT_DEFAULT gst_rtp_bin_debug
143 /* elementfactory information */
144 static const GstElementDetails rtpbin_details = GST_ELEMENT_DETAILS ("RTP Bin",
145 "Filter/Network/RTP",
146 "Implement an RTP bin",
147 "Wim Taymans <wim.taymans@gmail.com>");
150 static GstStaticPadTemplate rtpbin_recv_rtp_sink_template =
151 GST_STATIC_PAD_TEMPLATE ("recv_rtp_sink_%d",
154 GST_STATIC_CAPS ("application/x-rtp")
157 static GstStaticPadTemplate rtpbin_recv_rtcp_sink_template =
158 GST_STATIC_PAD_TEMPLATE ("recv_rtcp_sink_%d",
161 GST_STATIC_CAPS ("application/x-rtcp")
164 static GstStaticPadTemplate rtpbin_send_rtp_sink_template =
165 GST_STATIC_PAD_TEMPLATE ("send_rtp_sink_%d",
168 GST_STATIC_CAPS ("application/x-rtp")
172 static GstStaticPadTemplate rtpbin_recv_rtp_src_template =
173 GST_STATIC_PAD_TEMPLATE ("recv_rtp_src_%d_%d_%d",
176 GST_STATIC_CAPS ("application/x-rtp")
179 static GstStaticPadTemplate rtpbin_send_rtcp_src_template =
180 GST_STATIC_PAD_TEMPLATE ("send_rtcp_src_%d",
183 GST_STATIC_CAPS ("application/x-rtcp")
186 static GstStaticPadTemplate rtpbin_send_rtp_src_template =
187 GST_STATIC_PAD_TEMPLATE ("send_rtp_src_%d",
190 GST_STATIC_CAPS ("application/x-rtp")
193 /* padtemplate for the internal pad */
194 static GstStaticPadTemplate rtpbin_sync_sink_template =
195 GST_STATIC_PAD_TEMPLATE ("sink_%d",
198 GST_STATIC_CAPS ("application/x-rtcp")
201 #define GST_RTP_BIN_GET_PRIVATE(obj) \
202 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTP_BIN, GstRtpBinPrivate))
204 #define GST_RTP_BIN_LOCK(bin) g_mutex_lock ((bin)->priv->bin_lock)
205 #define GST_RTP_BIN_UNLOCK(bin) g_mutex_unlock ((bin)->priv->bin_lock)
207 struct _GstRtpBinPrivate
211 GstClockTime ntp_ns_base;
214 /* signals and args */
217 SIGNAL_REQUEST_PT_MAP,
221 SIGNAL_ON_SSRC_COLLISION,
222 SIGNAL_ON_SSRC_VALIDATED,
223 SIGNAL_ON_SSRC_ACTIVE,
226 SIGNAL_ON_BYE_TIMEOUT,
231 #define DEFAULT_LATENCY_MS 200
232 #define DEFAULT_SDES_CNAME NULL
233 #define DEFAULT_SDES_NAME NULL
234 #define DEFAULT_SDES_EMAIL NULL
235 #define DEFAULT_SDES_PHONE NULL
236 #define DEFAULT_SDES_LOCATION NULL
237 #define DEFAULT_SDES_TOOL NULL
238 #define DEFAULT_SDES_NOTE NULL
255 typedef struct _GstRtpBinSession GstRtpBinSession;
256 typedef struct _GstRtpBinStream GstRtpBinStream;
257 typedef struct _GstRtpBinClient GstRtpBinClient;
259 static guint gst_rtp_bin_signals[LAST_SIGNAL] = { 0 };
261 static GstCaps *pt_map_requested (GstElement * element, guint pt,
262 GstRtpBinSession * session);
263 static const gchar *sdes_type_to_name (GstRTCPSDESType type);
264 static void gst_rtp_bin_set_sdes_string (GstRtpBin * bin,
265 GstRTCPSDESType type, const gchar * data);
267 static void free_stream (GstRtpBinStream * stream);
269 /* Manages the RTP stream for one SSRC.
271 * We pipe the stream (comming from the SSRC demuxer) into a jitterbuffer.
272 * If we see an SDES RTCP packet that links multiple SSRCs together based on a
273 * common CNAME, we create a GstRtpBinClient structure to group the SSRCs
274 * together (see below).
276 struct _GstRtpBinStream
278 /* the SSRC of this stream */
284 /* the session this SSRC belongs to */
285 GstRtpBinSession *session;
287 /* the jitterbuffer of the SSRC */
290 /* the PT demuxer of the SSRC */
292 gulong demux_newpad_sig;
293 gulong demux_ptreq_sig;
294 gulong demux_pt_change_sig;
296 /* the internal pad we use to get RTCP sync messages */
300 guint64 last_extrtptime;
302 /* mapping to local RTP and NTP time */
311 gint64 prev_ts_offset;
315 #define GST_RTP_SESSION_LOCK(sess) g_mutex_lock ((sess)->lock)
316 #define GST_RTP_SESSION_UNLOCK(sess) g_mutex_unlock ((sess)->lock)
318 /* Manages the receiving end of the packets.
320 * There is one such structure for each RTP session (audio/video/...).
321 * We get the RTP/RTCP packets and stuff them into the session manager. From
322 * there they are pushed into an SSRC demuxer that splits the stream based on
323 * SSRC. Each of the SSRC streams go into their own jitterbuffer (managed with
324 * the GstRtpBinStream above).
326 struct _GstRtpBinSession
332 /* the session element */
334 /* the SSRC demuxer */
336 gulong demux_newpad_sig;
340 /* list of GstRtpBinStream */
343 /* mapping of payload type to caps */
346 /* the pads of the session */
347 GstPad *recv_rtp_sink;
348 GstPad *recv_rtp_src;
349 GstPad *recv_rtcp_sink;
351 GstPad *send_rtp_sink;
352 GstPad *send_rtp_src;
353 GstPad *send_rtcp_src;
356 /* Manages the RTP streams that come from one client and should therefore be
359 struct _GstRtpBinClient
361 /* the common CNAME for the streams */
372 /* find a session with the given id. Must be called with RTP_BIN_LOCK */
373 static GstRtpBinSession *
374 find_session_by_id (GstRtpBin * rtpbin, gint id)
378 for (walk = rtpbin->sessions; walk; walk = g_slist_next (walk)) {
379 GstRtpBinSession *sess = (GstRtpBinSession *) walk->data;
388 on_new_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
390 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC], 0,
395 on_ssrc_collision (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
397 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION], 0,
402 on_ssrc_validated (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
404 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
409 on_ssrc_active (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
411 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE], 0,
416 on_ssrc_sdes (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
418 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_SDES], 0,
423 on_bye_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
425 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC], 0,
430 on_bye_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
432 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT], 0,
437 on_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
439 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT], 0,
443 /* create a session with the given id. Must be called with RTP_BIN_LOCK */
444 static GstRtpBinSession *
445 create_session (GstRtpBin * rtpbin, gint id)
447 GstRtpBinSession *sess;
448 GstElement *session, *demux;
451 if (!(session = gst_element_factory_make ("gstrtpsession", NULL)))
454 if (!(demux = gst_element_factory_make ("gstrtpssrcdemux", NULL)))
457 sess = g_new0 (GstRtpBinSession, 1);
458 sess->lock = g_mutex_new ();
461 sess->session = session;
463 sess->ptmap = g_hash_table_new (NULL, NULL);
464 rtpbin->sessions = g_slist_prepend (rtpbin->sessions, sess);
466 /* set NTP base or new session */
467 g_object_set (session, "ntp-ns-base", rtpbin->priv->ntp_ns_base, NULL);
468 /* configure SDES items */
469 GST_OBJECT_LOCK (rtpbin);
470 for (i = GST_RTCP_SDES_CNAME; i < GST_RTCP_SDES_PRIV; i++) {
471 g_object_set (session, sdes_type_to_name (i), rtpbin->sdes[i], NULL);
473 GST_OBJECT_UNLOCK (rtpbin);
475 /* provide clock_rate to the session manager when needed */
476 g_signal_connect (session, "request-pt-map",
477 (GCallback) pt_map_requested, sess);
479 g_signal_connect (sess->session, "on-new-ssrc",
480 (GCallback) on_new_ssrc, sess);
481 g_signal_connect (sess->session, "on-ssrc-collision",
482 (GCallback) on_ssrc_collision, sess);
483 g_signal_connect (sess->session, "on-ssrc-validated",
484 (GCallback) on_ssrc_validated, sess);
485 g_signal_connect (sess->session, "on-ssrc-active",
486 (GCallback) on_ssrc_active, sess);
487 g_signal_connect (sess->session, "on-ssrc-sdes",
488 (GCallback) on_ssrc_sdes, sess);
489 g_signal_connect (sess->session, "on-bye-ssrc",
490 (GCallback) on_bye_ssrc, sess);
491 g_signal_connect (sess->session, "on-bye-timeout",
492 (GCallback) on_bye_timeout, sess);
493 g_signal_connect (sess->session, "on-timeout", (GCallback) on_timeout, sess);
495 /* FIXME, change state only to what's needed */
496 gst_bin_add (GST_BIN_CAST (rtpbin), session);
497 gst_element_set_state (session, GST_STATE_PLAYING);
498 gst_bin_add (GST_BIN_CAST (rtpbin), demux);
499 gst_element_set_state (demux, GST_STATE_PLAYING);
506 g_warning ("gstrtpbin: could not create gstrtpsession element");
511 gst_object_unref (session);
512 g_warning ("gstrtpbin: could not create gstrtpssrcdemux element");
518 free_session (GstRtpBinSession * sess)
524 gst_element_set_state (sess->session, GST_STATE_NULL);
525 gst_element_set_state (sess->demux, GST_STATE_NULL);
527 gst_bin_remove (GST_BIN_CAST (bin), sess->session);
528 gst_bin_remove (GST_BIN_CAST (bin), sess->demux);
530 g_slist_foreach (sess->streams, (GFunc) free_stream, NULL);
531 g_slist_free (sess->streams);
533 g_mutex_free (sess->lock);
534 g_hash_table_destroy (sess->ptmap);
536 bin->sessions = g_slist_remove (bin->sessions, sess);
542 static GstRtpBinStream *
543 find_stream_by_ssrc (GstRtpBinSession * session, guint32 ssrc)
547 for (walk = session->streams; walk; walk = g_slist_next (walk)) {
548 GstRtpBinStream *stream = (GstRtpBinStream *) walk->data;
550 if (stream->ssrc == ssrc)
557 /* get the payload type caps for the specific payload @pt in @session */
559 get_pt_map (GstRtpBinSession * session, guint pt)
561 GstCaps *caps = NULL;
564 GValue args[3] = { {0}, {0}, {0} };
566 GST_DEBUG ("searching pt %d in cache", pt);
568 GST_RTP_SESSION_LOCK (session);
570 /* first look in the cache */
571 caps = g_hash_table_lookup (session->ptmap, GINT_TO_POINTER (pt));
577 GST_DEBUG ("emiting signal for pt %d in session %d", pt, session->id);
579 /* not in cache, send signal to request caps */
580 g_value_init (&args[0], GST_TYPE_ELEMENT);
581 g_value_set_object (&args[0], bin);
582 g_value_init (&args[1], G_TYPE_UINT);
583 g_value_set_uint (&args[1], session->id);
584 g_value_init (&args[2], G_TYPE_UINT);
585 g_value_set_uint (&args[2], pt);
587 g_value_init (&ret, GST_TYPE_CAPS);
588 g_value_set_boxed (&ret, NULL);
590 g_signal_emitv (args, gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP], 0, &ret);
592 caps = (GstCaps *) g_value_get_boxed (&ret);
596 GST_DEBUG ("caching pt %d as %" GST_PTR_FORMAT, pt, caps);
599 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (pt), caps);
603 GST_RTP_SESSION_UNLOCK (session);
610 GST_RTP_SESSION_UNLOCK (session);
611 GST_DEBUG ("no pt map could be obtained");
617 return_true (gpointer key, gpointer value, gpointer user_data)
623 gst_rtp_bin_clear_pt_map (GstRtpBin * bin)
625 GSList *sessions, *streams;
627 GST_RTP_BIN_LOCK (bin);
628 GST_DEBUG_OBJECT (bin, "clearing pt map");
629 for (sessions = bin->sessions; sessions; sessions = g_slist_next (sessions)) {
630 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
632 GST_DEBUG_OBJECT (bin, "clearing session %p", session);
633 g_signal_emit_by_name (session->session, "clear-pt-map", NULL);
635 GST_RTP_SESSION_LOCK (session);
636 g_hash_table_foreach_remove (session->ptmap, return_true, NULL);
638 for (streams = session->streams; streams; streams = g_slist_next (streams)) {
639 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
641 GST_DEBUG_OBJECT (bin, "clearing stream %p", stream);
642 g_signal_emit_by_name (stream->buffer, "clear-pt-map", NULL);
643 g_signal_emit_by_name (stream->demux, "clear-pt-map", NULL);
645 GST_RTP_SESSION_UNLOCK (session);
647 GST_RTP_BIN_UNLOCK (bin);
650 static GstRtpBinClient *
651 get_client (GstRtpBin * bin, guint8 len, guint8 * data, gboolean * created)
653 GstRtpBinClient *result = NULL;
656 for (walk = bin->clients; walk; walk = g_slist_next (walk)) {
657 GstRtpBinClient *client = (GstRtpBinClient *) walk->data;
659 if (len != client->cname_len)
662 if (!strncmp ((gchar *) data, client->cname, client->cname_len)) {
663 GST_DEBUG_OBJECT (bin, "found existing client %p with CNAME %s", client,
670 /* nothing found, create one */
671 if (result == NULL) {
672 result = g_new0 (GstRtpBinClient, 1);
673 result->cname = g_strndup ((gchar *) data, len);
674 result->cname_len = len;
675 result->min_delta = G_MAXINT64;
676 bin->clients = g_slist_prepend (bin->clients, result);
677 GST_DEBUG_OBJECT (bin, "created new client %p with CNAME %s", result,
684 free_client (GstRtpBinClient * client)
686 g_free (client->cname);
690 /* associate a stream to the given CNAME. This will make sure all streams for
691 * that CNAME are synchronized together. */
693 gst_rtp_bin_associate (GstRtpBin * bin, GstRtpBinStream * stream, guint8 len,
696 GstRtpBinClient *client;
700 /* first find or create the CNAME */
701 client = get_client (bin, len, data, &created);
703 /* find stream in the client */
704 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
705 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
707 if (ostream == stream)
710 /* not found, add it to the list */
712 GST_DEBUG_OBJECT (bin,
713 "new association of SSRC %08x with client %p with CNAME %s",
714 stream->ssrc, client, client->cname);
715 client->streams = g_slist_prepend (client->streams, stream);
718 GST_DEBUG_OBJECT (bin,
719 "found association of SSRC %08x with client %p with CNAME %s",
720 stream->ssrc, client, client->cname);
723 /* we can only continue if we know the local clock-base and clock-rate */
724 if (stream->clock_base == -1)
727 if (stream->clock_rate <= 0) {
729 GstCaps *caps = NULL;
730 GstStructure *s = NULL;
732 GST_RTP_SESSION_LOCK (stream->session);
733 pt = stream->last_pt;
734 GST_RTP_SESSION_UNLOCK (stream->session);
739 caps = get_pt_map (stream->session, pt);
743 s = gst_caps_get_structure (caps, 0);
744 gst_structure_get_int (s, "clock-rate", &stream->clock_rate);
745 gst_caps_unref (caps);
747 if (stream->clock_rate <= 0)
751 /* map last RTP time to local timeline using our clock-base */
752 stream->local_rtp = stream->last_extrtptime - stream->clock_base;
754 GST_DEBUG_OBJECT (bin,
755 "base %" G_GUINT64_FORMAT ", extrtptime %" G_GUINT64_FORMAT
756 ", local RTP %" G_GUINT64_FORMAT ", clock-rate %d", stream->clock_base,
757 stream->last_extrtptime, stream->local_rtp, stream->clock_rate);
759 /* calculate local NTP time in gstreamer timestamp */
761 gst_util_uint64_scale_int (stream->local_rtp, GST_SECOND,
763 /* calculate delta between server and receiver */
764 stream->unix_delta = stream->last_unix - stream->local_unix;
766 GST_DEBUG_OBJECT (bin,
767 "local UNIX %" G_GUINT64_FORMAT ", remote UNIX %" G_GUINT64_FORMAT
768 ", delta %" G_GINT64_FORMAT, stream->local_unix, stream->last_unix,
771 /* recalc inter stream playout offset, but only if there are more than one
773 if (client->nstreams > 1) {
776 /* calculate the min of all deltas */
778 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
779 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
781 if (ostream->unix_delta < min)
782 min = ostream->unix_delta;
785 GST_DEBUG_OBJECT (bin, "client %p min delta %" G_GINT64_FORMAT, client,
788 /* calculate offsets for each stream */
789 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
790 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
792 ostream->ts_offset = ostream->unix_delta - min;
794 /* delta changed, see how much */
795 if (ostream->prev_ts_offset != ostream->ts_offset) {
798 if (ostream->prev_ts_offset > ostream->ts_offset)
799 diff = ostream->prev_ts_offset - ostream->ts_offset;
801 diff = ostream->ts_offset - ostream->prev_ts_offset;
803 /* only change diff when it changed more than 1 millisecond. This
804 * compensates for rounding errors in NTP to RTP timestamp
806 if (diff > GST_MSECOND)
807 g_object_set (ostream->buffer, "ts-offset", ostream->ts_offset, NULL);
809 ostream->prev_ts_offset = ostream->ts_offset;
811 GST_DEBUG_OBJECT (bin, "stream SSRC %08x, delta %" G_GINT64_FORMAT,
812 ostream->ssrc, ostream->ts_offset);
819 GST_WARNING_OBJECT (bin, "we have no clock-base");
824 GST_WARNING_OBJECT (bin, "we have no clock-rate");
829 #define GST_RTCP_BUFFER_FOR_PACKETS(b,buffer,packet) \
830 for ((b) = gst_rtcp_buffer_get_first_packet ((buffer), (packet)); (b); \
831 (b) = gst_rtcp_packet_move_to_next ((packet)))
833 #define GST_RTCP_SDES_FOR_ITEMS(b,packet) \
834 for ((b) = gst_rtcp_packet_sdes_first_item ((packet)); (b); \
835 (b) = gst_rtcp_packet_sdes_next_item ((packet)))
837 #define GST_RTCP_SDES_FOR_ENTRIES(b,packet) \
838 for ((b) = gst_rtcp_packet_sdes_first_entry ((packet)); (b); \
839 (b) = gst_rtcp_packet_sdes_next_entry ((packet)))
842 gst_rtp_bin_sync_chain (GstPad * pad, GstBuffer * buffer)
844 GstFlowReturn ret = GST_FLOW_OK;
845 GstRtpBinStream *stream;
847 GstRTCPPacket packet;
851 gboolean have_sr, have_sdes;
854 stream = gst_pad_get_element_private (pad);
857 GST_DEBUG_OBJECT (bin, "received sync packet");
859 if (!gst_rtcp_buffer_validate (buffer))
864 GST_RTCP_BUFFER_FOR_PACKETS (more, buffer, &packet) {
865 /* first packet must be SR or RR or else the validate would have failed */
866 switch (gst_rtcp_packet_get_type (&packet)) {
867 case GST_RTCP_TYPE_SR:
868 /* only parse first. There is only supposed to be one SR in the packet
869 * but we will deal with malformed packets gracefully */
872 /* get NTP and RTP times */
873 gst_rtcp_packet_sr_get_sender_info (&packet, &ssrc, &ntptime, &rtptime,
876 GST_DEBUG_OBJECT (bin, "received sync packet from SSRC %08x", ssrc);
877 /* ignore SR that is not ours */
878 if (ssrc != stream->ssrc)
883 /* store values in the stream */
884 stream->have_sync = TRUE;
885 stream->last_unix = gst_rtcp_ntp_to_unix (ntptime);
886 /* use extended timestamp */
887 gst_rtp_buffer_ext_timestamp (&stream->last_extrtptime, rtptime);
889 case GST_RTCP_TYPE_SDES:
891 gboolean more_items, more_entries;
893 /* only deal with first SDES, there is only supposed to be one SDES in
894 * the RTCP packet but we deal with bad packets gracefully. Also bail
895 * out if we have not seen an SR item yet. */
896 if (have_sdes || !have_sr)
899 GST_RTCP_SDES_FOR_ITEMS (more_items, &packet) {
900 /* skip items that are not about the SSRC of the sender */
901 if (gst_rtcp_packet_sdes_get_ssrc (&packet) != ssrc)
904 /* find the CNAME entry */
905 GST_RTCP_SDES_FOR_ENTRIES (more_entries, &packet) {
906 GstRTCPSDESType type;
910 gst_rtcp_packet_sdes_get_entry (&packet, &type, &len, &data);
912 if (type == GST_RTCP_SDES_CNAME) {
913 stream->clock_base = GST_BUFFER_OFFSET (buffer);
914 /* associate the stream to CNAME */
915 gst_rtp_bin_associate (bin, stream, len, data);
923 /* we can ignore these packets */
928 gst_buffer_unref (buffer);
935 /* this is fatal and should be filtered earlier */
936 GST_ELEMENT_ERROR (bin, STREAM, DECODE, (NULL),
937 ("invalid RTCP packet received"));
938 gst_buffer_unref (buffer);
939 return GST_FLOW_ERROR;
943 /* create a new stream with @ssrc in @session. Must be called with
944 * RTP_SESSION_LOCK. */
945 static GstRtpBinStream *
946 create_stream (GstRtpBinSession * session, guint32 ssrc)
948 GstElement *buffer, *demux;
949 GstRtpBinStream *stream;
950 GstPadTemplate *templ;
953 if (!(buffer = gst_element_factory_make ("gstrtpjitterbuffer", NULL)))
954 goto no_jitterbuffer;
956 if (!(demux = gst_element_factory_make ("gstrtpptdemux", NULL)))
959 stream = g_new0 (GstRtpBinStream, 1);
961 stream->bin = session->bin;
962 stream->session = session;
963 stream->buffer = buffer;
964 stream->demux = demux;
965 stream->last_extrtptime = -1;
966 stream->last_pt = -1;
967 stream->have_sync = FALSE;
968 session->streams = g_slist_prepend (session->streams, stream);
970 /* make an internal sinkpad for RTCP sync packets. Take ownership of the
971 * pad. We will link this pad later. */
972 padname = g_strdup_printf ("sync_%d", ssrc);
973 templ = gst_static_pad_template_get (&rtpbin_sync_sink_template);
974 stream->sync_pad = gst_pad_new_from_template (templ, padname);
975 gst_object_unref (templ);
977 gst_object_ref (stream->sync_pad);
978 gst_object_sink (stream->sync_pad);
979 gst_pad_set_element_private (stream->sync_pad, stream);
980 gst_pad_set_chain_function (stream->sync_pad, gst_rtp_bin_sync_chain);
981 gst_pad_set_active (stream->sync_pad, TRUE);
983 /* provide clock_rate to the jitterbuffer when needed */
984 g_signal_connect (buffer, "request-pt-map",
985 (GCallback) pt_map_requested, session);
987 /* configure latency */
988 g_object_set (buffer, "latency", session->bin->latency, NULL);
990 gst_bin_add (GST_BIN_CAST (session->bin), buffer);
991 gst_element_set_state (buffer, GST_STATE_PLAYING);
992 gst_bin_add (GST_BIN_CAST (session->bin), demux);
993 gst_element_set_state (demux, GST_STATE_PLAYING);
996 gst_element_link (buffer, demux);
1003 g_warning ("gstrtpbin: could not create gstrtpjitterbuffer element");
1008 gst_object_unref (buffer);
1009 g_warning ("gstrtpbin: could not create gstrtpptdemux element");
1015 free_stream (GstRtpBinStream * stream)
1017 GstRtpBinSession *session;
1019 session = stream->session;
1021 gst_element_set_state (stream->buffer, GST_STATE_NULL);
1022 gst_element_set_state (stream->demux, GST_STATE_NULL);
1024 gst_bin_remove (GST_BIN_CAST (session->bin), stream->buffer);
1025 gst_bin_remove (GST_BIN_CAST (session->bin), stream->demux);
1027 gst_object_unref (stream->sync_pad);
1029 session->streams = g_slist_remove (session->streams, stream);
1034 /* GObject vmethods */
1035 static void gst_rtp_bin_dispose (GObject * object);
1036 static void gst_rtp_bin_finalize (GObject * object);
1037 static void gst_rtp_bin_set_property (GObject * object, guint prop_id,
1038 const GValue * value, GParamSpec * pspec);
1039 static void gst_rtp_bin_get_property (GObject * object, guint prop_id,
1040 GValue * value, GParamSpec * pspec);
1042 /* GstElement vmethods */
1043 static GstClock *gst_rtp_bin_provide_clock (GstElement * element);
1044 static GstStateChangeReturn gst_rtp_bin_change_state (GstElement * element,
1045 GstStateChange transition);
1046 static GstPad *gst_rtp_bin_request_new_pad (GstElement * element,
1047 GstPadTemplate * templ, const gchar * name);
1048 static void gst_rtp_bin_release_pad (GstElement * element, GstPad * pad);
1049 static void gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message);
1050 static void gst_rtp_bin_clear_pt_map (GstRtpBin * bin);
1052 GST_BOILERPLATE (GstRtpBin, gst_rtp_bin, GstBin, GST_TYPE_BIN);
1055 gst_rtp_bin_base_init (gpointer klass)
1057 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
1060 gst_element_class_add_pad_template (element_class,
1061 gst_static_pad_template_get (&rtpbin_recv_rtp_sink_template));
1062 gst_element_class_add_pad_template (element_class,
1063 gst_static_pad_template_get (&rtpbin_recv_rtcp_sink_template));
1064 gst_element_class_add_pad_template (element_class,
1065 gst_static_pad_template_get (&rtpbin_send_rtp_sink_template));
1068 gst_element_class_add_pad_template (element_class,
1069 gst_static_pad_template_get (&rtpbin_recv_rtp_src_template));
1070 gst_element_class_add_pad_template (element_class,
1071 gst_static_pad_template_get (&rtpbin_send_rtcp_src_template));
1072 gst_element_class_add_pad_template (element_class,
1073 gst_static_pad_template_get (&rtpbin_send_rtp_src_template));
1075 gst_element_class_set_details (element_class, &rtpbin_details);
1079 gst_rtp_bin_class_init (GstRtpBinClass * klass)
1081 GObjectClass *gobject_class;
1082 GstElementClass *gstelement_class;
1083 GstBinClass *gstbin_class;
1085 gobject_class = (GObjectClass *) klass;
1086 gstelement_class = (GstElementClass *) klass;
1087 gstbin_class = (GstBinClass *) klass;
1089 g_type_class_add_private (klass, sizeof (GstRtpBinPrivate));
1091 gobject_class->dispose = gst_rtp_bin_dispose;
1092 gobject_class->finalize = gst_rtp_bin_finalize;
1093 gobject_class->set_property = gst_rtp_bin_set_property;
1094 gobject_class->get_property = gst_rtp_bin_get_property;
1096 g_object_class_install_property (gobject_class, PROP_LATENCY,
1097 g_param_spec_uint ("latency", "Buffer latency in ms",
1098 "Default amount of ms to buffer in the jitterbuffers", 0,
1099 G_MAXUINT, DEFAULT_LATENCY_MS, G_PARAM_READWRITE));
1102 * GstRtpBin::request-pt-map:
1103 * @rtpbin: the object which received the signal
1104 * @session: the session
1107 * Request the payload type as #GstCaps for @pt in @session.
1109 gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP] =
1110 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
1111 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, request_pt_map),
1112 NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT_UINT, GST_TYPE_CAPS, 2,
1113 G_TYPE_UINT, G_TYPE_UINT);
1115 * GstRtpBin::clear-pt-map:
1116 * @rtpbin: the object which received the signal
1118 * Clear all previously cached pt-mapping obtained with
1119 * GstRtpBin::request-pt-map.
1121 gst_rtp_bin_signals[SIGNAL_CLEAR_PT_MAP] =
1122 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
1123 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1124 clear_pt_map), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE,
1128 * GstRtpBin::on-new-ssrc:
1129 * @rtpbin: the object which received the signal
1130 * @session: the session
1133 * Notify of a new SSRC that entered @session.
1135 gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC] =
1136 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
1137 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_new_ssrc),
1138 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1139 G_TYPE_UINT, G_TYPE_UINT);
1141 * GstRtpBin::on-ssrc-collision:
1142 * @rtpbin: the object which received the signal
1143 * @session: the session
1146 * Notify when we have an SSRC collision
1148 gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION] =
1149 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
1150 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_collision),
1151 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1152 G_TYPE_UINT, G_TYPE_UINT);
1154 * GstRtpBin::on-ssrc-validated:
1155 * @rtpbin: the object which received the signal
1156 * @session: the session
1159 * Notify of a new SSRC that became validated.
1161 gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED] =
1162 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
1163 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_validated),
1164 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1165 G_TYPE_UINT, G_TYPE_UINT);
1167 * GstRtpBin::on-ssrc-active:
1168 * @rtpbin: the object which received the signal
1169 * @session: the session
1172 * Notify of a SSRC that is active, i.e., sending RTCP.
1174 gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE] =
1175 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
1176 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_active),
1177 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1178 G_TYPE_UINT, G_TYPE_UINT);
1180 * GstRtpBin::on-ssrc-sdes:
1181 * @rtpbin: the object which received the signal
1182 * @session: the session
1185 * Notify of a SSRC that is active, i.e., sending RTCP.
1187 gst_rtp_bin_signals[SIGNAL_ON_SSRC_SDES] =
1188 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
1189 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_sdes),
1190 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1191 G_TYPE_UINT, G_TYPE_UINT);
1194 * GstRtpBin::on-bye-ssrc:
1195 * @rtpbin: the object which received the signal
1196 * @session: the session
1199 * Notify of an SSRC that became inactive because of a BYE packet.
1201 gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC] =
1202 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
1203 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_ssrc),
1204 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1205 G_TYPE_UINT, G_TYPE_UINT);
1207 * GstRtpBin::on-bye-timeout:
1208 * @rtpbin: the object which received the signal
1209 * @session: the session
1212 * Notify of an SSRC that has timed out because of BYE
1214 gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT] =
1215 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
1216 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_timeout),
1217 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1218 G_TYPE_UINT, G_TYPE_UINT);
1220 * GstRtpBin::on-timeout:
1221 * @rtpbin: the object which received the signal
1222 * @session: the session
1225 * Notify of an SSRC that has timed out
1227 gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT] =
1228 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
1229 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_timeout),
1230 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1231 G_TYPE_UINT, G_TYPE_UINT);
1233 g_object_class_install_property (gobject_class, PROP_SDES_CNAME,
1234 g_param_spec_string ("sdes-cname", "SDES CNAME",
1235 "The CNAME to put in SDES messages of this session",
1236 DEFAULT_SDES_CNAME, G_PARAM_READWRITE));
1238 g_object_class_install_property (gobject_class, PROP_SDES_NAME,
1239 g_param_spec_string ("sdes-name", "SDES NAME",
1240 "The NAME to put in SDES messages of this session",
1241 DEFAULT_SDES_NAME, G_PARAM_READWRITE));
1243 g_object_class_install_property (gobject_class, PROP_SDES_EMAIL,
1244 g_param_spec_string ("sdes-email", "SDES EMAIL",
1245 "The EMAIL to put in SDES messages of this session",
1246 DEFAULT_SDES_EMAIL, G_PARAM_READWRITE));
1248 g_object_class_install_property (gobject_class, PROP_SDES_PHONE,
1249 g_param_spec_string ("sdes-phone", "SDES PHONE",
1250 "The PHONE to put in SDES messages of this session",
1251 DEFAULT_SDES_PHONE, G_PARAM_READWRITE));
1253 g_object_class_install_property (gobject_class, PROP_SDES_LOCATION,
1254 g_param_spec_string ("sdes-location", "SDES LOCATION",
1255 "The LOCATION to put in SDES messages of this session",
1256 DEFAULT_SDES_LOCATION, G_PARAM_READWRITE));
1258 g_object_class_install_property (gobject_class, PROP_SDES_TOOL,
1259 g_param_spec_string ("sdes-tool", "SDES TOOL",
1260 "The TOOL to put in SDES messages of this session",
1261 DEFAULT_SDES_TOOL, G_PARAM_READWRITE));
1263 g_object_class_install_property (gobject_class, PROP_SDES_NOTE,
1264 g_param_spec_string ("sdes-note", "SDES NOTE",
1265 "The NOTE to put in SDES messages of this session",
1266 DEFAULT_SDES_NOTE, G_PARAM_READWRITE));
1268 gstelement_class->provide_clock =
1269 GST_DEBUG_FUNCPTR (gst_rtp_bin_provide_clock);
1270 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_bin_change_state);
1271 gstelement_class->request_new_pad =
1272 GST_DEBUG_FUNCPTR (gst_rtp_bin_request_new_pad);
1273 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_bin_release_pad);
1275 gstbin_class->handle_message = GST_DEBUG_FUNCPTR (gst_rtp_bin_handle_message);
1277 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_bin_clear_pt_map);
1279 GST_DEBUG_CATEGORY_INIT (gst_rtp_bin_debug, "rtpbin", 0, "RTP bin");
1283 gst_rtp_bin_init (GstRtpBin * rtpbin, GstRtpBinClass * klass)
1287 rtpbin->priv = GST_RTP_BIN_GET_PRIVATE (rtpbin);
1288 rtpbin->priv->bin_lock = g_mutex_new ();
1289 rtpbin->provided_clock = gst_system_clock_obtain ();
1290 rtpbin->latency = DEFAULT_LATENCY_MS;
1292 /* some default SDES entries */
1293 str = g_strdup_printf ("%s@%s", g_get_user_name (), g_get_host_name ());
1294 gst_rtp_bin_set_sdes_string (rtpbin, GST_RTCP_SDES_CNAME, str);
1297 gst_rtp_bin_set_sdes_string (rtpbin, GST_RTCP_SDES_NAME, g_get_real_name ());
1298 gst_rtp_bin_set_sdes_string (rtpbin, GST_RTCP_SDES_TOOL, "GStreamer");
1302 gst_rtp_bin_dispose (GObject * object)
1306 rtpbin = GST_RTP_BIN (object);
1308 g_slist_foreach (rtpbin->sessions, (GFunc) free_session, NULL);
1309 g_slist_free (rtpbin->sessions);
1310 rtpbin->sessions = NULL;
1311 g_slist_foreach (rtpbin->clients, (GFunc) free_client, NULL);
1312 g_slist_free (rtpbin->clients);
1313 rtpbin->clients = NULL;
1315 G_OBJECT_CLASS (parent_class)->dispose (object);
1319 gst_rtp_bin_finalize (GObject * object)
1324 rtpbin = GST_RTP_BIN (object);
1326 for (i = 0; i < 9; i++)
1327 g_free (rtpbin->sdes[i]);
1329 g_mutex_free (rtpbin->priv->bin_lock);
1330 gst_object_unref (rtpbin->provided_clock);
1332 G_OBJECT_CLASS (parent_class)->finalize (object);
1335 static const gchar *
1336 sdes_type_to_name (GstRTCPSDESType type)
1338 const gchar *result;
1341 case GST_RTCP_SDES_CNAME:
1342 result = "sdes-cname";
1344 case GST_RTCP_SDES_NAME:
1345 result = "sdes-name";
1347 case GST_RTCP_SDES_EMAIL:
1348 result = "sdes-email";
1350 case GST_RTCP_SDES_PHONE:
1351 result = "sdes-phone";
1353 case GST_RTCP_SDES_LOC:
1354 result = "sdes-location";
1356 case GST_RTCP_SDES_TOOL:
1357 result = "sdes-tool";
1359 case GST_RTCP_SDES_NOTE:
1360 result = "sdes-note";
1362 case GST_RTCP_SDES_PRIV:
1363 result = "sdes-priv";
1373 gst_rtp_bin_set_sdes_string (GstRtpBin * bin, GstRTCPSDESType type,
1379 if (type < 0 || type > 8)
1382 GST_OBJECT_LOCK (bin);
1383 g_free (bin->sdes[type]);
1384 bin->sdes[type] = g_strdup (data);
1385 name = sdes_type_to_name (type);
1386 /* store in all sessions */
1387 for (item = bin->sessions; item; item = g_slist_next (item))
1388 g_object_set (item->data, name, bin->sdes[type], NULL);
1389 GST_OBJECT_UNLOCK (bin);
1393 gst_rtp_bin_get_sdes_string (GstRtpBin * bin, GstRTCPSDESType type)
1397 if (type < 0 || type > 8)
1400 GST_OBJECT_LOCK (bin);
1401 result = g_strdup (bin->sdes[type]);
1402 GST_OBJECT_UNLOCK (bin);
1408 gst_rtp_bin_set_property (GObject * object, guint prop_id,
1409 const GValue * value, GParamSpec * pspec)
1413 rtpbin = GST_RTP_BIN (object);
1417 GST_RTP_BIN_LOCK (rtpbin);
1418 rtpbin->latency = g_value_get_uint (value);
1419 GST_RTP_BIN_UNLOCK (rtpbin);
1421 case PROP_SDES_CNAME:
1422 gst_rtp_bin_set_sdes_string (rtpbin, GST_RTCP_SDES_CNAME,
1423 g_value_get_string (value));
1425 case PROP_SDES_NAME:
1426 gst_rtp_bin_set_sdes_string (rtpbin, GST_RTCP_SDES_NAME,
1427 g_value_get_string (value));
1429 case PROP_SDES_EMAIL:
1430 gst_rtp_bin_set_sdes_string (rtpbin, GST_RTCP_SDES_EMAIL,
1431 g_value_get_string (value));
1433 case PROP_SDES_PHONE:
1434 gst_rtp_bin_set_sdes_string (rtpbin, GST_RTCP_SDES_PHONE,
1435 g_value_get_string (value));
1437 case PROP_SDES_LOCATION:
1438 gst_rtp_bin_set_sdes_string (rtpbin, GST_RTCP_SDES_LOC,
1439 g_value_get_string (value));
1441 case PROP_SDES_TOOL:
1442 gst_rtp_bin_set_sdes_string (rtpbin, GST_RTCP_SDES_TOOL,
1443 g_value_get_string (value));
1445 case PROP_SDES_NOTE:
1446 gst_rtp_bin_set_sdes_string (rtpbin, GST_RTCP_SDES_NOTE,
1447 g_value_get_string (value));
1450 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1456 gst_rtp_bin_get_property (GObject * object, guint prop_id,
1457 GValue * value, GParamSpec * pspec)
1461 rtpbin = GST_RTP_BIN (object);
1465 GST_RTP_BIN_LOCK (rtpbin);
1466 g_value_set_uint (value, rtpbin->latency);
1467 GST_RTP_BIN_UNLOCK (rtpbin);
1469 case PROP_SDES_CNAME:
1470 g_value_take_string (value, gst_rtp_bin_get_sdes_string (rtpbin,
1471 GST_RTCP_SDES_CNAME));
1473 case PROP_SDES_NAME:
1474 g_value_take_string (value, gst_rtp_bin_get_sdes_string (rtpbin,
1475 GST_RTCP_SDES_NAME));
1477 case PROP_SDES_EMAIL:
1478 g_value_take_string (value, gst_rtp_bin_get_sdes_string (rtpbin,
1479 GST_RTCP_SDES_EMAIL));
1481 case PROP_SDES_PHONE:
1482 g_value_take_string (value, gst_rtp_bin_get_sdes_string (rtpbin,
1483 GST_RTCP_SDES_PHONE));
1485 case PROP_SDES_LOCATION:
1486 g_value_take_string (value, gst_rtp_bin_get_sdes_string (rtpbin,
1487 GST_RTCP_SDES_LOC));
1489 case PROP_SDES_TOOL:
1490 g_value_take_string (value, gst_rtp_bin_get_sdes_string (rtpbin,
1491 GST_RTCP_SDES_TOOL));
1493 case PROP_SDES_NOTE:
1494 g_value_take_string (value, gst_rtp_bin_get_sdes_string (rtpbin,
1495 GST_RTCP_SDES_NOTE));
1498 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1504 gst_rtp_bin_provide_clock (GstElement * element)
1508 rtpbin = GST_RTP_BIN (element);
1510 return GST_CLOCK_CAST (gst_object_ref (rtpbin->provided_clock));
1514 gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message)
1518 rtpbin = GST_RTP_BIN (bin);
1520 switch (GST_MESSAGE_TYPE (message)) {
1521 case GST_MESSAGE_ELEMENT:
1523 const GstStructure *s = gst_message_get_structure (message);
1525 /* we change the structure name and add the session ID to it */
1526 if (gst_structure_has_name (s, "GstRTPSessionSDES")) {
1529 /* find the session, the message source has it */
1530 for (walk = rtpbin->sessions; walk; walk = g_slist_next (walk)) {
1531 GstRtpBinSession *sess = (GstRtpBinSession *) walk->data;
1533 /* if we found the session, change message. else we exit the loop and
1534 * leave the message unchanged */
1535 if (GST_OBJECT_CAST (sess->session) == GST_MESSAGE_SRC (message)) {
1536 message = gst_message_make_writable (message);
1537 s = gst_message_get_structure (message);
1539 gst_structure_set_name ((GstStructure *) s, "GstRTPBinSDES");
1541 gst_structure_set ((GstStructure *) s, "session", G_TYPE_UINT,
1547 /* fallthrough to forward the modified message to the parent */
1551 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
1558 calc_ntp_ns_base (GstRtpBin * bin)
1564 /* get the current time and convert it to NTP time in nanoseconds */
1565 g_get_current_time (¤t);
1566 now = GST_TIMEVAL_TO_TIME (current);
1567 now += (2208988800LL * GST_SECOND);
1569 GST_RTP_BIN_LOCK (bin);
1570 bin->priv->ntp_ns_base = now;
1571 for (walk = bin->sessions; walk; walk = g_slist_next (walk)) {
1572 GstRtpBinSession *session = (GstRtpBinSession *) walk->data;
1574 g_object_set (session->session, "ntp-ns-base", now, NULL);
1576 GST_RTP_BIN_UNLOCK (bin);
1581 static GstStateChangeReturn
1582 gst_rtp_bin_change_state (GstElement * element, GstStateChange transition)
1584 GstStateChangeReturn res;
1587 rtpbin = GST_RTP_BIN (element);
1589 switch (transition) {
1590 case GST_STATE_CHANGE_NULL_TO_READY:
1592 case GST_STATE_CHANGE_READY_TO_PAUSED:
1594 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1595 calc_ntp_ns_base (rtpbin);
1601 res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1603 switch (transition) {
1604 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1606 case GST_STATE_CHANGE_PAUSED_TO_READY:
1608 case GST_STATE_CHANGE_READY_TO_NULL:
1616 /* a new pad (SSRC) was created in @session */
1618 new_payload_found (GstElement * element, guint pt, GstPad * pad,
1619 GstRtpBinStream * stream)
1622 GstElementClass *klass;
1623 GstPadTemplate *templ;
1627 rtpbin = stream->bin;
1629 GST_DEBUG ("new payload pad %d", pt);
1631 /* ghost the pad to the parent */
1632 klass = GST_ELEMENT_GET_CLASS (rtpbin);
1633 templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%d_%d_%d");
1634 padname = g_strdup_printf ("recv_rtp_src_%d_%u_%d",
1635 stream->session->id, stream->ssrc, pt);
1636 gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
1639 gst_pad_set_caps (gpad, GST_PAD_CAPS (pad));
1640 gst_pad_set_active (gpad, TRUE);
1641 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
1645 pt_map_requested (GstElement * element, guint pt, GstRtpBinSession * session)
1650 rtpbin = session->bin;
1652 GST_DEBUG_OBJECT (rtpbin, "payload map requested for pt %d in session %d", pt,
1655 caps = get_pt_map (session, pt);
1664 GST_DEBUG_OBJECT (rtpbin, "could not get caps");
1669 /* emited when caps changed for the session */
1671 caps_changed (GstPad * pad, GParamSpec * pspec, GstRtpBinSession * session)
1676 const GstStructure *s;
1680 g_object_get (pad, "caps", &caps, NULL);
1685 GST_DEBUG_OBJECT (bin, "got caps %" GST_PTR_FORMAT, caps);
1687 s = gst_caps_get_structure (caps, 0);
1689 /* get payload, finish when it's not there */
1690 if (!gst_structure_get_int (s, "payload", &payload))
1693 GST_RTP_SESSION_LOCK (session);
1694 GST_DEBUG_OBJECT (bin, "insert caps for payload %d", payload);
1695 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (payload), caps);
1696 GST_RTP_SESSION_UNLOCK (session);
1699 /* Stores the last payload type received on a particular stream */
1701 payload_type_change (GstElement * element, guint pt, GstRtpBinStream * stream)
1703 GST_RTP_SESSION_LOCK (stream->session);
1704 stream->last_pt = pt;
1705 GST_RTP_SESSION_UNLOCK (stream->session);
1708 /* a new pad (SSRC) was created in @session */
1710 new_ssrc_pad_found (GstElement * element, guint ssrc, GstPad * pad,
1711 GstRtpBinSession * session)
1713 GstRtpBinStream *stream;
1714 GstPad *sinkpad, *srcpad;
1718 GST_DEBUG_OBJECT (session->bin, "new SSRC pad %08x", ssrc);
1720 GST_RTP_SESSION_LOCK (session);
1722 /* create new stream */
1723 stream = create_stream (session, ssrc);
1727 /* get the caps of the pad, we need the clock-rate and base_time if any. */
1728 if ((caps = gst_pad_get_caps (pad))) {
1729 const GstStructure *s;
1732 GST_DEBUG_OBJECT (session->bin, "pad has caps %" GST_PTR_FORMAT, caps);
1734 s = gst_caps_get_structure (caps, 0);
1736 if (!gst_structure_get_int (s, "clock-rate", &stream->clock_rate)) {
1737 stream->clock_rate = -1;
1739 GST_WARNING_OBJECT (session->bin,
1740 "Caps have no clock rate %s from pad %s:%s",
1741 gst_caps_to_string (caps), GST_DEBUG_PAD_NAME (pad));
1744 if (gst_structure_get_uint (s, "clock-base", &val))
1745 stream->clock_base = val;
1747 stream->clock_base = -1;
1750 /* get pad and link */
1751 GST_DEBUG_OBJECT (session->bin, "linking jitterbuffer");
1752 padname = g_strdup_printf ("src_%d", ssrc);
1753 srcpad = gst_element_get_pad (element, padname);
1755 sinkpad = gst_element_get_static_pad (stream->buffer, "sink");
1756 gst_pad_link (srcpad, sinkpad);
1757 gst_object_unref (sinkpad);
1759 /* get the RTCP sync pad */
1760 GST_DEBUG_OBJECT (session->bin, "linking sync pad");
1761 padname = g_strdup_printf ("rtcp_src_%d", ssrc);
1762 srcpad = gst_element_get_pad (element, padname);
1764 gst_pad_link (srcpad, stream->sync_pad);
1765 gst_object_unref (srcpad);
1767 /* connect to the new-pad signal of the payload demuxer, this will expose the
1768 * new pad by ghosting it. */
1769 stream->demux_newpad_sig = g_signal_connect (stream->demux,
1770 "new-payload-type", (GCallback) new_payload_found, stream);
1771 /* connect to the request-pt-map signal. This signal will be emited by the
1772 * demuxer so that it can apply a proper caps on the buffers for the
1774 stream->demux_ptreq_sig = g_signal_connect (stream->demux,
1775 "request-pt-map", (GCallback) pt_map_requested, session);
1776 /* connect to the payload-type-change signal so that we can know which
1777 * PT is the current PT so that the jitterbuffer can be matched to the right
1779 stream->demux_pt_change_sig = g_signal_connect (stream->demux,
1780 "payload-type-change", (GCallback) payload_type_change, stream);
1782 GST_RTP_SESSION_UNLOCK (session);
1789 GST_RTP_SESSION_UNLOCK (session);
1790 GST_DEBUG_OBJECT (session->bin, "could not create stream");
1795 /* Create a pad for receiving RTP for the session in @name. Must be called with
1799 create_recv_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
1801 GstPad *result, *sinkdpad;
1803 GstRtpBinSession *session;
1804 GstPadLinkReturn lres;
1806 /* first get the session number */
1807 if (name == NULL || sscanf (name, "recv_rtp_sink_%d", &sessid) != 1)
1810 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
1812 /* get or create session */
1813 session = find_session_by_id (rtpbin, sessid);
1815 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
1816 /* create session now */
1817 session = create_session (rtpbin, sessid);
1818 if (session == NULL)
1822 /* check if pad was requested */
1823 if (session->recv_rtp_sink != NULL)
1826 GST_DEBUG_OBJECT (rtpbin, "getting RTP sink pad");
1827 /* get recv_rtp pad and store */
1828 session->recv_rtp_sink =
1829 gst_element_get_request_pad (session->session, "recv_rtp_sink");
1830 if (session->recv_rtp_sink == NULL)
1833 g_signal_connect (session->recv_rtp_sink, "notify::caps",
1834 (GCallback) caps_changed, session);
1836 GST_DEBUG_OBJECT (rtpbin, "getting RTP src pad");
1837 /* get srcpad, link to SSRCDemux */
1838 session->recv_rtp_src =
1839 gst_element_get_static_pad (session->session, "recv_rtp_src");
1840 if (session->recv_rtp_src == NULL)
1843 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTP sink pad");
1844 sinkdpad = gst_element_get_static_pad (session->demux, "sink");
1845 GST_DEBUG_OBJECT (rtpbin, "linking demuxer RTP sink pad");
1846 lres = gst_pad_link (session->recv_rtp_src, sinkdpad);
1847 gst_object_unref (sinkdpad);
1848 if (lres != GST_PAD_LINK_OK)
1851 /* connect to the new-ssrc-pad signal of the SSRC demuxer */
1852 session->demux_newpad_sig = g_signal_connect (session->demux,
1853 "new-ssrc-pad", (GCallback) new_ssrc_pad_found, session);
1855 GST_DEBUG_OBJECT (rtpbin, "ghosting session sink pad");
1857 gst_ghost_pad_new_from_template (name, session->recv_rtp_sink, templ);
1858 gst_pad_set_active (result, TRUE);
1859 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
1866 g_warning ("gstrtpbin: invalid name given");
1871 /* create_session already warned */
1876 g_warning ("gstrtpbin: recv_rtp pad already requested for session %d",
1882 g_warning ("gstrtpbin: failed to get session pad");
1887 g_warning ("gstrtpbin: failed to link pads");
1892 /* Create a pad for receiving RTCP for the session in @name. Must be called with
1896 create_recv_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ,
1901 GstRtpBinSession *session;
1903 GstPadLinkReturn lres;
1905 /* first get the session number */
1906 if (name == NULL || sscanf (name, "recv_rtcp_sink_%d", &sessid) != 1)
1909 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
1911 /* get or create the session */
1912 session = find_session_by_id (rtpbin, sessid);
1914 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
1915 /* create session now */
1916 session = create_session (rtpbin, sessid);
1917 if (session == NULL)
1921 /* check if pad was requested */
1922 if (session->recv_rtcp_sink != NULL)
1925 /* get recv_rtp pad and store */
1926 GST_DEBUG_OBJECT (rtpbin, "getting RTCP sink pad");
1927 session->recv_rtcp_sink =
1928 gst_element_get_request_pad (session->session, "recv_rtcp_sink");
1929 if (session->recv_rtcp_sink == NULL)
1932 /* get srcpad, link to SSRCDemux */
1933 GST_DEBUG_OBJECT (rtpbin, "getting sync src pad");
1934 session->sync_src = gst_element_get_static_pad (session->session, "sync_src");
1935 if (session->sync_src == NULL)
1938 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTCP sink pad");
1939 sinkdpad = gst_element_get_static_pad (session->demux, "rtcp_sink");
1940 lres = gst_pad_link (session->sync_src, sinkdpad);
1941 gst_object_unref (sinkdpad);
1942 if (lres != GST_PAD_LINK_OK)
1946 gst_ghost_pad_new_from_template (name, session->recv_rtcp_sink, templ);
1947 gst_pad_set_active (result, TRUE);
1948 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
1955 g_warning ("gstrtpbin: invalid name given");
1960 /* create_session already warned */
1965 g_warning ("gstrtpbin: recv_rtcp pad already requested for session %d",
1971 g_warning ("gstrtpbin: failed to get session pad");
1976 g_warning ("gstrtpbin: failed to link pads");
1981 /* Create a pad for sending RTP for the session in @name. Must be called with
1985 create_send_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
1987 GstPad *result, *srcghost;
1990 GstRtpBinSession *session;
1991 GstElementClass *klass;
1993 /* first get the session number */
1994 if (name == NULL || sscanf (name, "send_rtp_sink_%d", &sessid) != 1)
1997 /* get or create session */
1998 session = find_session_by_id (rtpbin, sessid);
2000 /* create session now */
2001 session = create_session (rtpbin, sessid);
2002 if (session == NULL)
2006 /* check if pad was requested */
2007 if (session->send_rtp_sink != NULL)
2010 /* get send_rtp pad and store */
2011 session->send_rtp_sink =
2012 gst_element_get_request_pad (session->session, "send_rtp_sink");
2013 if (session->send_rtp_sink == NULL)
2017 gst_ghost_pad_new_from_template (name, session->send_rtp_sink, templ);
2018 gst_pad_set_active (result, TRUE);
2019 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
2022 session->send_rtp_src =
2023 gst_element_get_static_pad (session->session, "send_rtp_src");
2024 if (session->send_rtp_src == NULL)
2027 /* ghost the new source pad */
2028 klass = GST_ELEMENT_GET_CLASS (rtpbin);
2029 gname = g_strdup_printf ("send_rtp_src_%d", sessid);
2030 templ = gst_element_class_get_pad_template (klass, "send_rtp_src_%d");
2032 gst_ghost_pad_new_from_template (gname, session->send_rtp_src, templ);
2033 gst_pad_set_active (srcghost, TRUE);
2034 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), srcghost);
2042 g_warning ("gstrtpbin: invalid name given");
2047 /* create_session already warned */
2052 g_warning ("gstrtpbin: send_rtp pad already requested for session %d",
2058 g_warning ("gstrtpbin: failed to get session pad for session %d", sessid);
2063 g_warning ("gstrtpbin: failed to get rtp source pad for session %d",
2069 /* Create a pad for sending RTCP for the session in @name. Must be called with
2073 create_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
2077 GstRtpBinSession *session;
2079 /* first get the session number */
2080 if (name == NULL || sscanf (name, "send_rtcp_src_%d", &sessid) != 1)
2083 /* get or create session */
2084 session = find_session_by_id (rtpbin, sessid);
2088 /* check if pad was requested */
2089 if (session->send_rtcp_src != NULL)
2092 /* get rtcp_src pad and store */
2093 session->send_rtcp_src =
2094 gst_element_get_request_pad (session->session, "send_rtcp_src");
2095 if (session->send_rtcp_src == NULL)
2099 gst_ghost_pad_new_from_template (name, session->send_rtcp_src, templ);
2100 gst_pad_set_active (result, TRUE);
2101 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
2108 g_warning ("gstrtpbin: invalid name given");
2113 g_warning ("gstrtpbin: session with id %d does not exist", sessid);
2118 g_warning ("gstrtpbin: send_rtcp_src pad already requested for session %d",
2124 g_warning ("gstrtpbin: failed to get rtcp pad for session %d", sessid);
2129 /* If the requested name is NULL we should create a name with
2130 * the session number assuming we want the lowest posible session
2131 * with a free pad like the template */
2133 gst_rtp_bin_get_free_pad_name (GstElement * element, GstPadTemplate * templ)
2135 gboolean name_found = FALSE;
2138 GstIterator *pad_it = NULL;
2139 gchar *pad_name = NULL;
2141 GST_DEBUG_OBJECT (element, "find a free pad name for template");
2142 while (!name_found) {
2144 pad_name = g_strdup_printf (templ->name_template, session++);
2145 pad_it = gst_element_iterate_pads (GST_ELEMENT (element));
2147 while (gst_iterator_next (pad_it, (gpointer) & pad) == GST_ITERATOR_OK) {
2148 if (strcmp (gst_pad_get_name (pad), pad_name) == 0)
2151 gst_iterator_free (pad_it);
2154 GST_DEBUG_OBJECT (element, "free pad name found: '%s'", pad_name);
2161 gst_rtp_bin_request_new_pad (GstElement * element,
2162 GstPadTemplate * templ, const gchar * name)
2165 GstElementClass *klass;
2167 gchar *pad_name = NULL;
2169 g_return_val_if_fail (templ != NULL, NULL);
2170 g_return_val_if_fail (GST_IS_RTP_BIN (element), NULL);
2172 rtpbin = GST_RTP_BIN (element);
2173 klass = GST_ELEMENT_GET_CLASS (element);
2175 GST_RTP_BIN_LOCK (rtpbin);
2178 /* use a free pad name */
2179 pad_name = gst_rtp_bin_get_free_pad_name (element, templ);
2181 /* use the provided name */
2182 pad_name = g_strdup (name);
2185 GST_DEBUG ("Trying to request a pad with name %s", pad_name);
2187 /* figure out the template */
2188 if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink_%d")) {
2189 result = create_recv_rtp (rtpbin, templ, pad_name);
2190 } else if (templ == gst_element_class_get_pad_template (klass,
2191 "recv_rtcp_sink_%d")) {
2192 result = create_recv_rtcp (rtpbin, templ, pad_name);
2193 } else if (templ == gst_element_class_get_pad_template (klass,
2194 "send_rtp_sink_%d")) {
2195 result = create_send_rtp (rtpbin, templ, pad_name);
2196 } else if (templ == gst_element_class_get_pad_template (klass,
2197 "send_rtcp_src_%d")) {
2198 result = create_rtcp (rtpbin, templ, pad_name);
2200 goto wrong_template;
2203 GST_RTP_BIN_UNLOCK (rtpbin);
2211 GST_RTP_BIN_UNLOCK (rtpbin);
2212 g_warning ("gstrtpbin: this is not our template");
2218 gst_rtp_bin_release_pad (GstElement * element, GstPad * pad)