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. The session number must be specified when
60 * requesting the sink pad. 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;
295 /* the internal pad we use to get RTCP sync messages */
299 guint64 last_extrtptime;
301 /* mapping to local RTP and NTP time */
310 gint64 prev_ts_offset;
313 #define GST_RTP_SESSION_LOCK(sess) g_mutex_lock ((sess)->lock)
314 #define GST_RTP_SESSION_UNLOCK(sess) g_mutex_unlock ((sess)->lock)
316 /* Manages the receiving end of the packets.
318 * There is one such structure for each RTP session (audio/video/...).
319 * We get the RTP/RTCP packets and stuff them into the session manager. From
320 * there they are pushed into an SSRC demuxer that splits the stream based on
321 * SSRC. Each of the SSRC streams go into their own jitterbuffer (managed with
322 * the GstRtpBinStream above).
324 struct _GstRtpBinSession
330 /* the session element */
332 /* the SSRC demuxer */
334 gulong demux_newpad_sig;
338 /* list of GstRtpBinStream */
341 /* mapping of payload type to caps */
344 /* the pads of the session */
345 GstPad *recv_rtp_sink;
346 GstPad *recv_rtp_src;
347 GstPad *recv_rtcp_sink;
349 GstPad *send_rtp_sink;
350 GstPad *send_rtp_src;
351 GstPad *send_rtcp_src;
354 /* Manages the RTP streams that come from one client and should therefore be
357 struct _GstRtpBinClient
359 /* the common CNAME for the streams */
370 /* find a session with the given id. Must be called with RTP_BIN_LOCK */
371 static GstRtpBinSession *
372 find_session_by_id (GstRtpBin * rtpbin, gint id)
376 for (walk = rtpbin->sessions; walk; walk = g_slist_next (walk)) {
377 GstRtpBinSession *sess = (GstRtpBinSession *) walk->data;
386 on_new_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
388 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC], 0,
393 on_ssrc_collision (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
395 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION], 0,
400 on_ssrc_validated (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
402 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
407 on_ssrc_active (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
409 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE], 0,
414 on_ssrc_sdes (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
416 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_SDES], 0,
421 on_bye_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
423 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC], 0,
428 on_bye_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
430 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT], 0,
435 on_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
437 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT], 0,
441 /* create a session with the given id. Must be called with RTP_BIN_LOCK */
442 static GstRtpBinSession *
443 create_session (GstRtpBin * rtpbin, gint id)
445 GstRtpBinSession *sess;
446 GstElement *session, *demux;
449 if (!(session = gst_element_factory_make ("gstrtpsession", NULL)))
452 if (!(demux = gst_element_factory_make ("gstrtpssrcdemux", NULL)))
455 sess = g_new0 (GstRtpBinSession, 1);
456 sess->lock = g_mutex_new ();
459 sess->session = session;
461 sess->ptmap = g_hash_table_new (NULL, NULL);
462 rtpbin->sessions = g_slist_prepend (rtpbin->sessions, sess);
464 /* set NTP base or new session */
465 g_object_set (session, "ntp-ns-base", rtpbin->priv->ntp_ns_base, NULL);
466 /* configure SDES items */
467 GST_OBJECT_LOCK (rtpbin);
468 for (i = GST_RTCP_SDES_CNAME; i < GST_RTCP_SDES_PRIV; i++) {
469 g_object_set (session, sdes_type_to_name (i), rtpbin->sdes[i], NULL);
471 GST_OBJECT_UNLOCK (rtpbin);
473 /* provide clock_rate to the session manager when needed */
474 g_signal_connect (session, "request-pt-map",
475 (GCallback) pt_map_requested, sess);
477 g_signal_connect (sess->session, "on-new-ssrc",
478 (GCallback) on_new_ssrc, sess);
479 g_signal_connect (sess->session, "on-ssrc-collision",
480 (GCallback) on_ssrc_collision, sess);
481 g_signal_connect (sess->session, "on-ssrc-validated",
482 (GCallback) on_ssrc_validated, sess);
483 g_signal_connect (sess->session, "on-ssrc-active",
484 (GCallback) on_ssrc_active, sess);
485 g_signal_connect (sess->session, "on-ssrc-sdes",
486 (GCallback) on_ssrc_sdes, sess);
487 g_signal_connect (sess->session, "on-bye-ssrc",
488 (GCallback) on_bye_ssrc, sess);
489 g_signal_connect (sess->session, "on-bye-timeout",
490 (GCallback) on_bye_timeout, sess);
491 g_signal_connect (sess->session, "on-timeout", (GCallback) on_timeout, sess);
493 /* FIXME, change state only to what's needed */
494 gst_bin_add (GST_BIN_CAST (rtpbin), session);
495 gst_element_set_state (session, GST_STATE_PLAYING);
496 gst_bin_add (GST_BIN_CAST (rtpbin), demux);
497 gst_element_set_state (demux, GST_STATE_PLAYING);
504 g_warning ("gstrtpbin: could not create gstrtpsession element");
509 gst_object_unref (session);
510 g_warning ("gstrtpbin: could not create gstrtpssrcdemux element");
516 free_session (GstRtpBinSession * sess)
522 gst_element_set_state (sess->session, GST_STATE_NULL);
523 gst_element_set_state (sess->demux, GST_STATE_NULL);
525 gst_bin_remove (GST_BIN_CAST (bin), sess->session);
526 gst_bin_remove (GST_BIN_CAST (bin), sess->demux);
528 g_slist_foreach (sess->streams, (GFunc) free_stream, NULL);
529 g_slist_free (sess->streams);
531 g_mutex_free (sess->lock);
532 g_hash_table_destroy (sess->ptmap);
534 bin->sessions = g_slist_remove (bin->sessions, sess);
540 static GstRtpBinStream *
541 find_stream_by_ssrc (GstRtpBinSession * session, guint32 ssrc)
545 for (walk = session->streams; walk; walk = g_slist_next (walk)) {
546 GstRtpBinStream *stream = (GstRtpBinStream *) walk->data;
548 if (stream->ssrc == ssrc)
555 /* get the payload type caps for the specific payload @pt in @session */
557 get_pt_map (GstRtpBinSession * session, guint pt)
559 GstCaps *caps = NULL;
562 GValue args[3] = { {0}, {0}, {0} };
564 GST_DEBUG ("searching pt %d in cache", pt);
566 GST_RTP_SESSION_LOCK (session);
568 /* first look in the cache */
569 caps = g_hash_table_lookup (session->ptmap, GINT_TO_POINTER (pt));
575 GST_DEBUG ("emiting signal for pt %d in session %d", pt, session->id);
577 /* not in cache, send signal to request caps */
578 g_value_init (&args[0], GST_TYPE_ELEMENT);
579 g_value_set_object (&args[0], bin);
580 g_value_init (&args[1], G_TYPE_UINT);
581 g_value_set_uint (&args[1], session->id);
582 g_value_init (&args[2], G_TYPE_UINT);
583 g_value_set_uint (&args[2], pt);
585 g_value_init (&ret, GST_TYPE_CAPS);
586 g_value_set_boxed (&ret, NULL);
588 g_signal_emitv (args, gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP], 0, &ret);
590 caps = (GstCaps *) g_value_get_boxed (&ret);
594 GST_DEBUG ("caching pt %d as %" GST_PTR_FORMAT, pt, caps);
597 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (pt), caps);
601 GST_RTP_SESSION_UNLOCK (session);
608 GST_RTP_SESSION_UNLOCK (session);
609 GST_DEBUG ("no pt map could be obtained");
615 return_true (gpointer key, gpointer value, gpointer user_data)
621 gst_rtp_bin_clear_pt_map (GstRtpBin * bin)
623 GSList *sessions, *streams;
625 GST_RTP_BIN_LOCK (bin);
626 GST_DEBUG_OBJECT (bin, "clearing pt map");
627 for (sessions = bin->sessions; sessions; sessions = g_slist_next (sessions)) {
628 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
630 GST_DEBUG_OBJECT (bin, "clearing session %p", session);
631 g_signal_emit_by_name (session->session, "clear-pt-map", NULL);
633 GST_RTP_SESSION_LOCK (session);
634 g_hash_table_foreach_remove (session->ptmap, return_true, NULL);
636 for (streams = session->streams; streams; streams = g_slist_next (streams)) {
637 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
639 GST_DEBUG_OBJECT (bin, "clearing stream %p", stream);
640 g_signal_emit_by_name (stream->buffer, "clear-pt-map", NULL);
641 g_signal_emit_by_name (stream->demux, "clear-pt-map", NULL);
643 GST_RTP_SESSION_UNLOCK (session);
645 GST_RTP_BIN_UNLOCK (bin);
648 static GstRtpBinClient *
649 get_client (GstRtpBin * bin, guint8 len, guint8 * data, gboolean * created)
651 GstRtpBinClient *result = NULL;
654 for (walk = bin->clients; walk; walk = g_slist_next (walk)) {
655 GstRtpBinClient *client = (GstRtpBinClient *) walk->data;
657 if (len != client->cname_len)
660 if (!strncmp ((gchar *) data, client->cname, client->cname_len)) {
661 GST_DEBUG_OBJECT (bin, "found existing client %p with CNAME %s", client,
668 /* nothing found, create one */
669 if (result == NULL) {
670 result = g_new0 (GstRtpBinClient, 1);
671 result->cname = g_strndup ((gchar *) data, len);
672 result->cname_len = len;
673 result->min_delta = G_MAXINT64;
674 bin->clients = g_slist_prepend (bin->clients, result);
675 GST_DEBUG_OBJECT (bin, "created new client %p with CNAME %s", result,
682 free_client (GstRtpBinClient * client)
684 g_free (client->cname);
688 /* associate a stream to the given CNAME. This will make sure all streams for
689 * that CNAME are synchronized together. */
691 gst_rtp_bin_associate (GstRtpBin * bin, GstRtpBinStream * stream, guint8 len,
694 GstRtpBinClient *client;
698 /* first find or create the CNAME */
699 client = get_client (bin, len, data, &created);
701 /* find stream in the client */
702 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
703 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
705 if (ostream == stream)
708 /* not found, add it to the list */
710 GST_DEBUG_OBJECT (bin,
711 "new association of SSRC %08x with client %p with CNAME %s",
712 stream->ssrc, client, client->cname);
713 client->streams = g_slist_prepend (client->streams, stream);
716 GST_DEBUG_OBJECT (bin,
717 "found association of SSRC %08x with client %p with CNAME %s",
718 stream->ssrc, client, client->cname);
721 /* we can only continue if we know the local clock-base and clock-rate */
722 if (stream->clock_base == -1)
724 if (stream->clock_rate <= 0)
727 /* map last RTP time to local timeline using our clock-base */
728 stream->local_rtp = stream->last_extrtptime - stream->clock_base;
730 GST_DEBUG_OBJECT (bin,
731 "base %" G_GUINT64_FORMAT ", extrtptime %" G_GUINT64_FORMAT
732 ", local RTP %" G_GUINT64_FORMAT ", clock-rate %d", stream->clock_base,
733 stream->last_extrtptime, stream->local_rtp, stream->clock_rate);
735 /* calculate local NTP time in gstreamer timestamp */
737 gst_util_uint64_scale_int (stream->local_rtp, GST_SECOND,
739 /* calculate delta between server and receiver */
740 stream->unix_delta = stream->last_unix - stream->local_unix;
742 GST_DEBUG_OBJECT (bin,
743 "local UNIX %" G_GUINT64_FORMAT ", remote UNIX %" G_GUINT64_FORMAT
744 ", delta %" G_GINT64_FORMAT, stream->local_unix, stream->last_unix,
747 /* recalc inter stream playout offset, but only if there are more than one
749 if (client->nstreams > 1) {
752 /* calculate the min of all deltas */
754 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
755 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
757 if (ostream->unix_delta < min)
758 min = ostream->unix_delta;
761 GST_DEBUG_OBJECT (bin, "client %p min delta %" G_GINT64_FORMAT, client,
764 /* calculate offsets for each stream */
765 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
766 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
768 ostream->ts_offset = ostream->unix_delta - min;
770 /* delta changed, see how much */
771 if (ostream->prev_ts_offset != ostream->ts_offset) {
774 if (ostream->prev_ts_offset > ostream->ts_offset)
775 diff = ostream->prev_ts_offset - ostream->ts_offset;
777 diff = ostream->ts_offset - ostream->prev_ts_offset;
779 /* only change diff when it changed more than 1 millisecond. This
780 * compensates for rounding errors in NTP to RTP timestamp
782 if (diff > GST_MSECOND)
783 g_object_set (ostream->buffer, "ts-offset", ostream->ts_offset, NULL);
785 ostream->prev_ts_offset = ostream->ts_offset;
787 GST_DEBUG_OBJECT (bin, "stream SSRC %08x, delta %" G_GINT64_FORMAT,
788 ostream->ssrc, ostream->ts_offset);
795 GST_WARNING_OBJECT (bin, "we have no clock-base");
800 GST_WARNING_OBJECT (bin, "we have no clock-rate");
805 #define GST_RTCP_BUFFER_FOR_PACKETS(b,buffer,packet) \
806 for ((b) = gst_rtcp_buffer_get_first_packet ((buffer), (packet)); (b); \
807 (b) = gst_rtcp_packet_move_to_next ((packet)))
809 #define GST_RTCP_SDES_FOR_ITEMS(b,packet) \
810 for ((b) = gst_rtcp_packet_sdes_first_item ((packet)); (b); \
811 (b) = gst_rtcp_packet_sdes_next_item ((packet)))
813 #define GST_RTCP_SDES_FOR_ENTRIES(b,packet) \
814 for ((b) = gst_rtcp_packet_sdes_first_entry ((packet)); (b); \
815 (b) = gst_rtcp_packet_sdes_next_entry ((packet)))
818 gst_rtp_bin_sync_chain (GstPad * pad, GstBuffer * buffer)
820 GstFlowReturn ret = GST_FLOW_OK;
821 GstRtpBinStream *stream;
823 GstRTCPPacket packet;
827 gboolean have_sr, have_sdes;
830 stream = gst_pad_get_element_private (pad);
833 GST_DEBUG_OBJECT (bin, "received sync packet");
835 if (!gst_rtcp_buffer_validate (buffer))
840 GST_RTCP_BUFFER_FOR_PACKETS (more, buffer, &packet) {
841 /* first packet must be SR or RR or else the validate would have failed */
842 switch (gst_rtcp_packet_get_type (&packet)) {
843 case GST_RTCP_TYPE_SR:
844 /* only parse first. There is only supposed to be one SR in the packet
845 * but we will deal with malformed packets gracefully */
848 /* get NTP and RTP times */
849 gst_rtcp_packet_sr_get_sender_info (&packet, &ssrc, &ntptime, &rtptime,
852 GST_DEBUG_OBJECT (bin, "received sync packet from SSRC %08x", ssrc);
853 /* ignore SR that is not ours */
854 if (ssrc != stream->ssrc)
859 /* store values in the stream */
860 stream->have_sync = TRUE;
861 stream->last_unix = gst_rtcp_ntp_to_unix (ntptime);
862 /* use extended timestamp */
863 gst_rtp_buffer_ext_timestamp (&stream->last_extrtptime, rtptime);
865 case GST_RTCP_TYPE_SDES:
867 gboolean more_items, more_entries;
869 /* only deal with first SDES, there is only supposed to be one SDES in
870 * the RTCP packet but we deal with bad packets gracefully. Also bail
871 * out if we have not seen an SR item yet. */
872 if (have_sdes || !have_sr)
875 GST_RTCP_SDES_FOR_ITEMS (more_items, &packet) {
876 /* skip items that are not about the SSRC of the sender */
877 if (gst_rtcp_packet_sdes_get_ssrc (&packet) != ssrc)
880 /* find the CNAME entry */
881 GST_RTCP_SDES_FOR_ENTRIES (more_entries, &packet) {
882 GstRTCPSDESType type;
886 gst_rtcp_packet_sdes_get_entry (&packet, &type, &len, &data);
888 if (type == GST_RTCP_SDES_CNAME) {
889 stream->clock_base = GST_BUFFER_OFFSET (buffer);
890 /* associate the stream to CNAME */
891 gst_rtp_bin_associate (bin, stream, len, data);
899 /* we can ignore these packets */
904 gst_buffer_unref (buffer);
911 /* this is fatal and should be filtered earlier */
912 GST_ELEMENT_ERROR (bin, STREAM, DECODE, (NULL),
913 ("invalid RTCP packet received"));
914 gst_buffer_unref (buffer);
915 return GST_FLOW_ERROR;
919 /* create a new stream with @ssrc in @session. Must be called with
920 * RTP_SESSION_LOCK. */
921 static GstRtpBinStream *
922 create_stream (GstRtpBinSession * session, guint32 ssrc)
924 GstElement *buffer, *demux;
925 GstRtpBinStream *stream;
926 GstPadTemplate *templ;
929 if (!(buffer = gst_element_factory_make ("gstrtpjitterbuffer", NULL)))
930 goto no_jitterbuffer;
932 if (!(demux = gst_element_factory_make ("gstrtpptdemux", NULL)))
935 stream = g_new0 (GstRtpBinStream, 1);
937 stream->bin = session->bin;
938 stream->session = session;
939 stream->buffer = buffer;
940 stream->demux = demux;
941 stream->last_extrtptime = -1;
942 stream->have_sync = FALSE;
943 session->streams = g_slist_prepend (session->streams, stream);
945 /* make an internal sinkpad for RTCP sync packets. Take ownership of the
946 * pad. We will link this pad later. */
947 padname = g_strdup_printf ("sync_%d", ssrc);
948 templ = gst_static_pad_template_get (&rtpbin_sync_sink_template);
949 stream->sync_pad = gst_pad_new_from_template (templ, padname);
950 gst_object_unref (templ);
952 gst_object_ref (stream->sync_pad);
953 gst_object_sink (stream->sync_pad);
954 gst_pad_set_element_private (stream->sync_pad, stream);
955 gst_pad_set_chain_function (stream->sync_pad, gst_rtp_bin_sync_chain);
956 gst_pad_set_active (stream->sync_pad, TRUE);
958 /* provide clock_rate to the jitterbuffer when needed */
959 g_signal_connect (buffer, "request-pt-map",
960 (GCallback) pt_map_requested, session);
962 /* configure latency */
963 g_object_set (buffer, "latency", session->bin->latency, NULL);
965 gst_bin_add (GST_BIN_CAST (session->bin), buffer);
966 gst_element_set_state (buffer, GST_STATE_PLAYING);
967 gst_bin_add (GST_BIN_CAST (session->bin), demux);
968 gst_element_set_state (demux, GST_STATE_PLAYING);
971 gst_element_link (buffer, demux);
978 g_warning ("gstrtpbin: could not create gstrtpjitterbuffer element");
983 gst_object_unref (buffer);
984 g_warning ("gstrtpbin: could not create gstrtpptdemux element");
990 free_stream (GstRtpBinStream * stream)
992 GstRtpBinSession *session;
994 session = stream->session;
996 gst_element_set_state (stream->buffer, GST_STATE_NULL);
997 gst_element_set_state (stream->demux, GST_STATE_NULL);
999 gst_bin_remove (GST_BIN_CAST (session->bin), stream->buffer);
1000 gst_bin_remove (GST_BIN_CAST (session->bin), stream->demux);
1002 gst_object_unref (stream->sync_pad);
1004 session->streams = g_slist_remove (session->streams, stream);
1009 /* GObject vmethods */
1010 static void gst_rtp_bin_dispose (GObject * object);
1011 static void gst_rtp_bin_finalize (GObject * object);
1012 static void gst_rtp_bin_set_property (GObject * object, guint prop_id,
1013 const GValue * value, GParamSpec * pspec);
1014 static void gst_rtp_bin_get_property (GObject * object, guint prop_id,
1015 GValue * value, GParamSpec * pspec);
1017 /* GstElement vmethods */
1018 static GstClock *gst_rtp_bin_provide_clock (GstElement * element);
1019 static GstStateChangeReturn gst_rtp_bin_change_state (GstElement * element,
1020 GstStateChange transition);
1021 static GstPad *gst_rtp_bin_request_new_pad (GstElement * element,
1022 GstPadTemplate * templ, const gchar * name);
1023 static void gst_rtp_bin_release_pad (GstElement * element, GstPad * pad);
1024 static void gst_rtp_bin_clear_pt_map (GstRtpBin * bin);
1026 GST_BOILERPLATE (GstRtpBin, gst_rtp_bin, GstBin, GST_TYPE_BIN);
1029 gst_rtp_bin_base_init (gpointer klass)
1031 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
1034 gst_element_class_add_pad_template (element_class,
1035 gst_static_pad_template_get (&rtpbin_recv_rtp_sink_template));
1036 gst_element_class_add_pad_template (element_class,
1037 gst_static_pad_template_get (&rtpbin_recv_rtcp_sink_template));
1038 gst_element_class_add_pad_template (element_class,
1039 gst_static_pad_template_get (&rtpbin_send_rtp_sink_template));
1042 gst_element_class_add_pad_template (element_class,
1043 gst_static_pad_template_get (&rtpbin_recv_rtp_src_template));
1044 gst_element_class_add_pad_template (element_class,
1045 gst_static_pad_template_get (&rtpbin_send_rtcp_src_template));
1046 gst_element_class_add_pad_template (element_class,
1047 gst_static_pad_template_get (&rtpbin_send_rtp_src_template));
1049 gst_element_class_set_details (element_class, &rtpbin_details);
1053 gst_rtp_bin_class_init (GstRtpBinClass * klass)
1055 GObjectClass *gobject_class;
1056 GstElementClass *gstelement_class;
1058 gobject_class = (GObjectClass *) klass;
1059 gstelement_class = (GstElementClass *) klass;
1061 g_type_class_add_private (klass, sizeof (GstRtpBinPrivate));
1063 gobject_class->dispose = gst_rtp_bin_dispose;
1064 gobject_class->finalize = gst_rtp_bin_finalize;
1065 gobject_class->set_property = gst_rtp_bin_set_property;
1066 gobject_class->get_property = gst_rtp_bin_get_property;
1068 g_object_class_install_property (gobject_class, PROP_LATENCY,
1069 g_param_spec_uint ("latency", "Buffer latency in ms",
1070 "Default amount of ms to buffer in the jitterbuffers", 0,
1071 G_MAXUINT, DEFAULT_LATENCY_MS, G_PARAM_READWRITE));
1074 * GstRtpBin::request-pt-map:
1075 * @rtpbin: the object which received the signal
1076 * @session: the session
1079 * Request the payload type as #GstCaps for @pt in @session.
1081 gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP] =
1082 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
1083 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, request_pt_map),
1084 NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT_UINT, GST_TYPE_CAPS, 2,
1085 G_TYPE_UINT, G_TYPE_UINT);
1087 * GstRtpBin::clear-pt-map:
1088 * @rtpbin: the object which received the signal
1090 * Clear all previously cached pt-mapping obtained with
1091 * GstRtpBin::request-pt-map.
1093 gst_rtp_bin_signals[SIGNAL_CLEAR_PT_MAP] =
1094 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
1095 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1096 clear_pt_map), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE,
1100 * GstRtpBin::on-new-ssrc:
1101 * @rtpbin: the object which received the signal
1102 * @session: the session
1105 * Notify of a new SSRC that entered @session.
1107 gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC] =
1108 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
1109 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_new_ssrc),
1110 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1111 G_TYPE_UINT, G_TYPE_UINT);
1113 * GstRtpBin::on-ssrc-collision:
1114 * @rtpbin: the object which received the signal
1115 * @session: the session
1118 * Notify when we have an SSRC collision
1120 gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION] =
1121 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
1122 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_collision),
1123 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1124 G_TYPE_UINT, G_TYPE_UINT);
1126 * GstRtpBin::on-ssrc-validated:
1127 * @rtpbin: the object which received the signal
1128 * @session: the session
1131 * Notify of a new SSRC that became validated.
1133 gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED] =
1134 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
1135 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_validated),
1136 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1137 G_TYPE_UINT, G_TYPE_UINT);
1139 * GstRtpBin::on-ssrc-active:
1140 * @rtpbin: the object which received the signal
1141 * @session: the session
1144 * Notify of a SSRC that is active, i.e., sending RTCP.
1146 gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE] =
1147 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
1148 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_active),
1149 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1150 G_TYPE_UINT, G_TYPE_UINT);
1152 * GstRtpBin::on-ssrc-sdes:
1153 * @rtpbin: the object which received the signal
1154 * @session: the session
1157 * Notify of a SSRC that is active, i.e., sending RTCP.
1159 gst_rtp_bin_signals[SIGNAL_ON_SSRC_SDES] =
1160 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
1161 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_sdes),
1162 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1163 G_TYPE_UINT, G_TYPE_UINT);
1166 * GstRtpBin::on-bye-ssrc:
1167 * @rtpbin: the object which received the signal
1168 * @session: the session
1171 * Notify of an SSRC that became inactive because of a BYE packet.
1173 gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC] =
1174 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
1175 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_ssrc),
1176 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1177 G_TYPE_UINT, G_TYPE_UINT);
1179 * GstRtpBin::on-bye-timeout:
1180 * @rtpbin: the object which received the signal
1181 * @session: the session
1184 * Notify of an SSRC that has timed out because of BYE
1186 gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT] =
1187 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
1188 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_timeout),
1189 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1190 G_TYPE_UINT, G_TYPE_UINT);
1192 * GstRtpBin::on-timeout:
1193 * @rtpbin: the object which received the signal
1194 * @session: the session
1197 * Notify of an SSRC that has timed out
1199 gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT] =
1200 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
1201 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_timeout),
1202 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1203 G_TYPE_UINT, G_TYPE_UINT);
1205 g_object_class_install_property (gobject_class, PROP_SDES_CNAME,
1206 g_param_spec_string ("sdes-cname", "SDES CNAME",
1207 "The CNAME to put in SDES messages of this session",
1208 DEFAULT_SDES_CNAME, G_PARAM_READWRITE));
1210 g_object_class_install_property (gobject_class, PROP_SDES_NAME,
1211 g_param_spec_string ("sdes-name", "SDES NAME",
1212 "The NAME to put in SDES messages of this session",
1213 DEFAULT_SDES_NAME, G_PARAM_READWRITE));
1215 g_object_class_install_property (gobject_class, PROP_SDES_EMAIL,
1216 g_param_spec_string ("sdes-email", "SDES EMAIL",
1217 "The EMAIL to put in SDES messages of this session",
1218 DEFAULT_SDES_EMAIL, G_PARAM_READWRITE));
1220 g_object_class_install_property (gobject_class, PROP_SDES_PHONE,
1221 g_param_spec_string ("sdes-phone", "SDES PHONE",
1222 "The PHONE to put in SDES messages of this session",
1223 DEFAULT_SDES_PHONE, G_PARAM_READWRITE));
1225 g_object_class_install_property (gobject_class, PROP_SDES_LOCATION,
1226 g_param_spec_string ("sdes-location", "SDES LOCATION",
1227 "The LOCATION to put in SDES messages of this session",
1228 DEFAULT_SDES_LOCATION, G_PARAM_READWRITE));
1230 g_object_class_install_property (gobject_class, PROP_SDES_TOOL,
1231 g_param_spec_string ("sdes-tool", "SDES TOOL",
1232 "The TOOL to put in SDES messages of this session",
1233 DEFAULT_SDES_TOOL, G_PARAM_READWRITE));
1235 g_object_class_install_property (gobject_class, PROP_SDES_NOTE,
1236 g_param_spec_string ("sdes-note", "SDES NOTE",
1237 "The NOTE to put in SDES messages of this session",
1238 DEFAULT_SDES_NOTE, G_PARAM_READWRITE));
1240 gstelement_class->provide_clock =
1241 GST_DEBUG_FUNCPTR (gst_rtp_bin_provide_clock);
1242 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_bin_change_state);
1243 gstelement_class->request_new_pad =
1244 GST_DEBUG_FUNCPTR (gst_rtp_bin_request_new_pad);
1245 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_bin_release_pad);
1247 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_bin_clear_pt_map);
1249 GST_DEBUG_CATEGORY_INIT (gst_rtp_bin_debug, "rtpbin", 0, "RTP bin");
1253 gst_rtp_bin_init (GstRtpBin * rtpbin, GstRtpBinClass * klass)
1257 rtpbin->priv = GST_RTP_BIN_GET_PRIVATE (rtpbin);
1258 rtpbin->priv->bin_lock = g_mutex_new ();
1259 rtpbin->provided_clock = gst_system_clock_obtain ();
1260 rtpbin->latency = DEFAULT_LATENCY_MS;
1262 /* some default SDES entries */
1263 str = g_strdup_printf ("%s@%s", g_get_user_name (), g_get_host_name ());
1264 gst_rtp_bin_set_sdes_string (rtpbin, GST_RTCP_SDES_CNAME, str);
1267 gst_rtp_bin_set_sdes_string (rtpbin, GST_RTCP_SDES_NAME, g_get_real_name ());
1268 gst_rtp_bin_set_sdes_string (rtpbin, GST_RTCP_SDES_TOOL, "GStreamer");
1272 gst_rtp_bin_dispose (GObject * object)
1276 rtpbin = GST_RTP_BIN (object);
1278 g_slist_foreach (rtpbin->sessions, (GFunc) free_session, NULL);
1279 g_slist_free (rtpbin->sessions);
1280 rtpbin->sessions = NULL;
1281 g_slist_foreach (rtpbin->clients, (GFunc) free_client, NULL);
1282 g_slist_free (rtpbin->clients);
1283 rtpbin->clients = NULL;
1285 G_OBJECT_CLASS (parent_class)->dispose (object);
1289 gst_rtp_bin_finalize (GObject * object)
1293 rtpbin = GST_RTP_BIN (object);
1295 g_mutex_free (rtpbin->priv->bin_lock);
1296 gst_object_unref (rtpbin->provided_clock);
1298 G_OBJECT_CLASS (parent_class)->finalize (object);
1301 static const gchar *
1302 sdes_type_to_name (GstRTCPSDESType type)
1304 const gchar *result;
1307 case GST_RTCP_SDES_CNAME:
1308 result = "sdes-cname";
1310 case GST_RTCP_SDES_NAME:
1311 result = "sdes-name";
1313 case GST_RTCP_SDES_EMAIL:
1314 result = "sdes-email";
1316 case GST_RTCP_SDES_PHONE:
1317 result = "sdes-phone";
1319 case GST_RTCP_SDES_LOC:
1320 result = "sdes-location";
1322 case GST_RTCP_SDES_TOOL:
1323 result = "sdes-tool";
1325 case GST_RTCP_SDES_NOTE:
1326 result = "sdes-note";
1328 case GST_RTCP_SDES_PRIV:
1329 result = "sdes-priv";
1339 gst_rtp_bin_set_sdes_string (GstRtpBin * bin, GstRTCPSDESType type,
1345 GST_OBJECT_LOCK (bin);
1346 g_free (bin->sdes[type]);
1347 bin->sdes[type] = g_strdup (data);
1348 name = sdes_type_to_name (type);
1349 /* store in all sessions */
1350 for (item = bin->sessions; item; item = g_slist_next (item))
1351 g_object_set (item->data, name, bin->sdes[type], NULL);
1352 GST_OBJECT_UNLOCK (bin);
1356 gst_rtp_bin_get_sdes_string (GstRtpBin * bin, GstRTCPSDESType type)
1360 GST_OBJECT_LOCK (bin);
1361 result = g_strdup (bin->sdes[type]);
1362 GST_OBJECT_UNLOCK (bin);
1368 gst_rtp_bin_set_property (GObject * object, guint prop_id,
1369 const GValue * value, GParamSpec * pspec)
1373 rtpbin = GST_RTP_BIN (object);
1377 GST_RTP_BIN_LOCK (rtpbin);
1378 rtpbin->latency = g_value_get_uint (value);
1379 GST_RTP_BIN_UNLOCK (rtpbin);
1381 case PROP_SDES_CNAME:
1382 gst_rtp_bin_set_sdes_string (rtpbin, GST_RTCP_SDES_CNAME,
1383 g_value_get_string (value));
1385 case PROP_SDES_NAME:
1386 gst_rtp_bin_set_sdes_string (rtpbin, GST_RTCP_SDES_NAME,
1387 g_value_get_string (value));
1389 case PROP_SDES_EMAIL:
1390 gst_rtp_bin_set_sdes_string (rtpbin, GST_RTCP_SDES_EMAIL,
1391 g_value_get_string (value));
1393 case PROP_SDES_PHONE:
1394 gst_rtp_bin_set_sdes_string (rtpbin, GST_RTCP_SDES_PHONE,
1395 g_value_get_string (value));
1397 case PROP_SDES_LOCATION:
1398 gst_rtp_bin_set_sdes_string (rtpbin, GST_RTCP_SDES_LOC,
1399 g_value_get_string (value));
1401 case PROP_SDES_TOOL:
1402 gst_rtp_bin_set_sdes_string (rtpbin, GST_RTCP_SDES_TOOL,
1403 g_value_get_string (value));
1405 case PROP_SDES_NOTE:
1406 gst_rtp_bin_set_sdes_string (rtpbin, GST_RTCP_SDES_NOTE,
1407 g_value_get_string (value));
1410 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1416 gst_rtp_bin_get_property (GObject * object, guint prop_id,
1417 GValue * value, GParamSpec * pspec)
1421 rtpbin = GST_RTP_BIN (object);
1425 GST_RTP_BIN_LOCK (rtpbin);
1426 g_value_set_uint (value, rtpbin->latency);
1427 GST_RTP_BIN_UNLOCK (rtpbin);
1429 case PROP_SDES_CNAME:
1430 g_value_take_string (value, gst_rtp_bin_get_sdes_string (rtpbin,
1431 GST_RTCP_SDES_CNAME));
1433 case PROP_SDES_NAME:
1434 g_value_take_string (value, gst_rtp_bin_get_sdes_string (rtpbin,
1435 GST_RTCP_SDES_NAME));
1437 case PROP_SDES_EMAIL:
1438 g_value_take_string (value, gst_rtp_bin_get_sdes_string (rtpbin,
1439 GST_RTCP_SDES_EMAIL));
1441 case PROP_SDES_PHONE:
1442 g_value_take_string (value, gst_rtp_bin_get_sdes_string (rtpbin,
1443 GST_RTCP_SDES_PHONE));
1445 case PROP_SDES_LOCATION:
1446 g_value_take_string (value, gst_rtp_bin_get_sdes_string (rtpbin,
1447 GST_RTCP_SDES_LOC));
1449 case PROP_SDES_TOOL:
1450 g_value_take_string (value, gst_rtp_bin_get_sdes_string (rtpbin,
1451 GST_RTCP_SDES_TOOL));
1453 case PROP_SDES_NOTE:
1454 g_value_take_string (value, gst_rtp_bin_get_sdes_string (rtpbin,
1455 GST_RTCP_SDES_NOTE));
1458 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1464 gst_rtp_bin_provide_clock (GstElement * element)
1468 rtpbin = GST_RTP_BIN (element);
1470 return GST_CLOCK_CAST (gst_object_ref (rtpbin->provided_clock));
1474 calc_ntp_ns_base (GstRtpBin * bin)
1480 /* get the current time and convert it to NTP time in nanoseconds */
1481 g_get_current_time (¤t);
1482 now = GST_TIMEVAL_TO_TIME (current);
1483 now += (2208988800LL * GST_SECOND);
1485 GST_RTP_BIN_LOCK (bin);
1486 bin->priv->ntp_ns_base = now;
1487 for (walk = bin->sessions; walk; walk = g_slist_next (walk)) {
1488 GstRtpBinSession *session = (GstRtpBinSession *) walk->data;
1490 g_object_set (session->session, "ntp-ns-base", now, NULL);
1492 GST_RTP_BIN_UNLOCK (bin);
1497 static GstStateChangeReturn
1498 gst_rtp_bin_change_state (GstElement * element, GstStateChange transition)
1500 GstStateChangeReturn res;
1503 rtpbin = GST_RTP_BIN (element);
1505 switch (transition) {
1506 case GST_STATE_CHANGE_NULL_TO_READY:
1508 case GST_STATE_CHANGE_READY_TO_PAUSED:
1510 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1511 calc_ntp_ns_base (rtpbin);
1517 res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1519 switch (transition) {
1520 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1522 case GST_STATE_CHANGE_PAUSED_TO_READY:
1524 case GST_STATE_CHANGE_READY_TO_NULL:
1532 /* a new pad (SSRC) was created in @session */
1534 new_payload_found (GstElement * element, guint pt, GstPad * pad,
1535 GstRtpBinStream * stream)
1538 GstElementClass *klass;
1539 GstPadTemplate *templ;
1543 rtpbin = stream->bin;
1545 GST_DEBUG ("new payload pad %d", pt);
1547 /* ghost the pad to the parent */
1548 klass = GST_ELEMENT_GET_CLASS (rtpbin);
1549 templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%d_%d_%d");
1550 padname = g_strdup_printf ("recv_rtp_src_%d_%u_%d",
1551 stream->session->id, stream->ssrc, pt);
1552 gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
1555 gst_pad_set_caps (gpad, GST_PAD_CAPS (pad));
1556 gst_pad_set_active (gpad, TRUE);
1557 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
1561 pt_map_requested (GstElement * element, guint pt, GstRtpBinSession * session)
1566 rtpbin = session->bin;
1568 GST_DEBUG_OBJECT (rtpbin, "payload map requested for pt %d in session %d", pt,
1571 caps = get_pt_map (session, pt);
1580 GST_DEBUG_OBJECT (rtpbin, "could not get caps");
1585 /* emited when caps changed for the session */
1587 caps_changed (GstPad * pad, GParamSpec * pspec, GstRtpBinSession * session)
1592 const GstStructure *s;
1596 g_object_get (pad, "caps", &caps, NULL);
1601 GST_DEBUG_OBJECT (bin, "got caps %" GST_PTR_FORMAT, caps);
1603 s = gst_caps_get_structure (caps, 0);
1605 /* get payload, finish when it's not there */
1606 if (!gst_structure_get_int (s, "payload", &payload))
1609 GST_RTP_SESSION_LOCK (session);
1610 GST_DEBUG_OBJECT (bin, "insert caps for payload %d", payload);
1611 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (payload), caps);
1612 GST_RTP_SESSION_UNLOCK (session);
1615 /* a new pad (SSRC) was created in @session */
1617 new_ssrc_pad_found (GstElement * element, guint ssrc, GstPad * pad,
1618 GstRtpBinSession * session)
1620 GstRtpBinStream *stream;
1621 GstPad *sinkpad, *srcpad;
1625 GST_DEBUG_OBJECT (session->bin, "new SSRC pad %08x", ssrc);
1627 GST_RTP_SESSION_LOCK (session);
1629 /* create new stream */
1630 stream = create_stream (session, ssrc);
1634 /* get the caps of the pad, we need the clock-rate and base_time if any. */
1635 if ((caps = gst_pad_get_caps (pad))) {
1636 const GstStructure *s;
1639 GST_DEBUG_OBJECT (session->bin, "pad has caps %" GST_PTR_FORMAT, caps);
1641 s = gst_caps_get_structure (caps, 0);
1643 if (!gst_structure_get_int (s, "clock-rate", &stream->clock_rate))
1644 stream->clock_rate = -1;
1646 if (gst_structure_get_uint (s, "clock-base", &val))
1647 stream->clock_base = val;
1649 stream->clock_base = -1;
1652 /* get pad and link */
1653 GST_DEBUG_OBJECT (session->bin, "linking jitterbuffer");
1654 padname = g_strdup_printf ("src_%d", ssrc);
1655 srcpad = gst_element_get_pad (element, padname);
1657 sinkpad = gst_element_get_static_pad (stream->buffer, "sink");
1658 gst_pad_link (srcpad, sinkpad);
1659 gst_object_unref (sinkpad);
1661 /* get the RTCP sync pad */
1662 GST_DEBUG_OBJECT (session->bin, "linking sync pad");
1663 padname = g_strdup_printf ("rtcp_src_%d", ssrc);
1664 srcpad = gst_element_get_pad (element, padname);
1666 gst_pad_link (srcpad, stream->sync_pad);
1667 gst_object_unref (srcpad);
1669 /* connect to the new-pad signal of the payload demuxer, this will expose the
1670 * new pad by ghosting it. */
1671 stream->demux_newpad_sig = g_signal_connect (stream->demux,
1672 "new-payload-type", (GCallback) new_payload_found, stream);
1673 /* connect to the request-pt-map signal. This signal will be emited by the
1674 * demuxer so that it can apply a proper caps on the buffers for the
1676 stream->demux_ptreq_sig = g_signal_connect (stream->demux,
1677 "request-pt-map", (GCallback) pt_map_requested, session);
1679 GST_RTP_SESSION_UNLOCK (session);
1686 GST_RTP_SESSION_UNLOCK (session);
1687 GST_DEBUG_OBJECT (session->bin, "could not create stream");
1692 /* Create a pad for receiving RTP for the session in @name. Must be called with
1696 create_recv_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
1698 GstPad *result, *sinkdpad;
1700 GstRtpBinSession *session;
1701 GstPadLinkReturn lres;
1703 /* first get the session number */
1704 if (name == NULL || sscanf (name, "recv_rtp_sink_%d", &sessid) != 1)
1707 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
1709 /* get or create session */
1710 session = find_session_by_id (rtpbin, sessid);
1712 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
1713 /* create session now */
1714 session = create_session (rtpbin, sessid);
1715 if (session == NULL)
1719 /* check if pad was requested */
1720 if (session->recv_rtp_sink != NULL)
1723 GST_DEBUG_OBJECT (rtpbin, "getting RTP sink pad");
1724 /* get recv_rtp pad and store */
1725 session->recv_rtp_sink =
1726 gst_element_get_request_pad (session->session, "recv_rtp_sink");
1727 if (session->recv_rtp_sink == NULL)
1730 g_signal_connect (session->recv_rtp_sink, "notify::caps",
1731 (GCallback) caps_changed, session);
1733 GST_DEBUG_OBJECT (rtpbin, "getting RTP src pad");
1734 /* get srcpad, link to SSRCDemux */
1735 session->recv_rtp_src =
1736 gst_element_get_static_pad (session->session, "recv_rtp_src");
1737 if (session->recv_rtp_src == NULL)
1740 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTP sink pad");
1741 sinkdpad = gst_element_get_static_pad (session->demux, "sink");
1742 GST_DEBUG_OBJECT (rtpbin, "linking demuxer RTP sink pad");
1743 lres = gst_pad_link (session->recv_rtp_src, sinkdpad);
1744 gst_object_unref (sinkdpad);
1745 if (lres != GST_PAD_LINK_OK)
1748 /* connect to the new-ssrc-pad signal of the SSRC demuxer */
1749 session->demux_newpad_sig = g_signal_connect (session->demux,
1750 "new-ssrc-pad", (GCallback) new_ssrc_pad_found, session);
1752 GST_DEBUG_OBJECT (rtpbin, "ghosting session sink pad");
1754 gst_ghost_pad_new_from_template (name, session->recv_rtp_sink, templ);
1755 gst_pad_set_active (result, TRUE);
1756 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
1763 g_warning ("gstrtpbin: invalid name given");
1768 /* create_session already warned */
1773 g_warning ("gstrtpbin: recv_rtp pad already requested for session %d",
1779 g_warning ("gstrtpbin: failed to get session pad");
1784 g_warning ("gstrtpbin: failed to link pads");
1789 /* Create a pad for receiving RTCP for the session in @name. Must be called with
1793 create_recv_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ,
1798 GstRtpBinSession *session;
1800 GstPadLinkReturn lres;
1802 /* first get the session number */
1803 if (name == NULL || sscanf (name, "recv_rtcp_sink_%d", &sessid) != 1)
1806 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
1808 /* get or create the session */
1809 session = find_session_by_id (rtpbin, sessid);
1811 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
1812 /* create session now */
1813 session = create_session (rtpbin, sessid);
1814 if (session == NULL)
1818 /* check if pad was requested */
1819 if (session->recv_rtcp_sink != NULL)
1822 /* get recv_rtp pad and store */
1823 GST_DEBUG_OBJECT (rtpbin, "getting RTCP sink pad");
1824 session->recv_rtcp_sink =
1825 gst_element_get_request_pad (session->session, "recv_rtcp_sink");
1826 if (session->recv_rtcp_sink == NULL)
1829 /* get srcpad, link to SSRCDemux */
1830 GST_DEBUG_OBJECT (rtpbin, "getting sync src pad");
1831 session->sync_src = gst_element_get_static_pad (session->session, "sync_src");
1832 if (session->sync_src == NULL)
1835 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTCP sink pad");
1836 sinkdpad = gst_element_get_static_pad (session->demux, "rtcp_sink");
1837 lres = gst_pad_link (session->sync_src, sinkdpad);
1838 gst_object_unref (sinkdpad);
1839 if (lres != GST_PAD_LINK_OK)
1843 gst_ghost_pad_new_from_template (name, session->recv_rtcp_sink, templ);
1844 gst_pad_set_active (result, TRUE);
1845 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
1852 g_warning ("gstrtpbin: invalid name given");
1857 /* create_session already warned */
1862 g_warning ("gstrtpbin: recv_rtcp pad already requested for session %d",
1868 g_warning ("gstrtpbin: failed to get session pad");
1873 g_warning ("gstrtpbin: failed to link pads");
1878 /* Create a pad for sending RTP for the session in @name. Must be called with
1882 create_send_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
1884 GstPad *result, *srcghost;
1887 GstRtpBinSession *session;
1888 GstElementClass *klass;
1890 /* first get the session number */
1891 if (name == NULL || sscanf (name, "send_rtp_sink_%d", &sessid) != 1)
1894 /* get or create session */
1895 session = find_session_by_id (rtpbin, sessid);
1897 /* create session now */
1898 session = create_session (rtpbin, sessid);
1899 if (session == NULL)
1903 /* check if pad was requested */
1904 if (session->send_rtp_sink != NULL)
1907 /* get send_rtp pad and store */
1908 session->send_rtp_sink =
1909 gst_element_get_request_pad (session->session, "send_rtp_sink");
1910 if (session->send_rtp_sink == NULL)
1914 gst_ghost_pad_new_from_template (name, session->send_rtp_sink, templ);
1915 gst_pad_set_active (result, TRUE);
1916 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
1919 session->send_rtp_src =
1920 gst_element_get_static_pad (session->session, "send_rtp_src");
1921 if (session->send_rtp_src == NULL)
1924 /* ghost the new source pad */
1925 klass = GST_ELEMENT_GET_CLASS (rtpbin);
1926 gname = g_strdup_printf ("send_rtp_src_%d", sessid);
1927 templ = gst_element_class_get_pad_template (klass, "send_rtp_src_%d");
1929 gst_ghost_pad_new_from_template (gname, session->send_rtp_src, templ);
1930 gst_pad_set_active (srcghost, TRUE);
1931 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), srcghost);
1939 g_warning ("gstrtpbin: invalid name given");
1944 /* create_session already warned */
1949 g_warning ("gstrtpbin: send_rtp pad already requested for session %d",
1955 g_warning ("gstrtpbin: failed to get session pad for session %d", sessid);
1960 g_warning ("gstrtpbin: failed to get rtp source pad for session %d",
1966 /* Create a pad for sending RTCP for the session in @name. Must be called with
1970 create_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
1974 GstRtpBinSession *session;
1976 /* first get the session number */
1977 if (name == NULL || sscanf (name, "send_rtcp_src_%d", &sessid) != 1)
1980 /* get or create session */
1981 session = find_session_by_id (rtpbin, sessid);
1985 /* check if pad was requested */
1986 if (session->send_rtcp_src != NULL)
1989 /* get rtcp_src pad and store */
1990 session->send_rtcp_src =
1991 gst_element_get_request_pad (session->session, "send_rtcp_src");
1992 if (session->send_rtcp_src == NULL)
1996 gst_ghost_pad_new_from_template (name, session->send_rtcp_src, templ);
1997 gst_pad_set_active (result, TRUE);
1998 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
2005 g_warning ("gstrtpbin: invalid name given");
2010 g_warning ("gstrtpbin: session with id %d does not exist", sessid);
2015 g_warning ("gstrtpbin: send_rtcp_src pad already requested for session %d",
2021 g_warning ("gstrtpbin: failed to get rtcp pad for session %d", sessid);
2029 gst_rtp_bin_request_new_pad (GstElement * element,
2030 GstPadTemplate * templ, const gchar * name)
2033 GstElementClass *klass;
2036 g_return_val_if_fail (templ != NULL, NULL);
2037 g_return_val_if_fail (GST_IS_RTP_BIN (element), NULL);
2039 rtpbin = GST_RTP_BIN (element);
2040 klass = GST_ELEMENT_GET_CLASS (element);
2042 GST_RTP_BIN_LOCK (rtpbin);
2044 /* figure out the template */
2045 if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink_%d")) {
2046 result = create_recv_rtp (rtpbin, templ, name);
2047 } else if (templ == gst_element_class_get_pad_template (klass,
2048 "recv_rtcp_sink_%d")) {
2049 result = create_recv_rtcp (rtpbin, templ, name);
2050 } else if (templ == gst_element_class_get_pad_template (klass,
2051 "send_rtp_sink_%d")) {
2052 result = create_send_rtp (rtpbin, templ, name);
2053 } else if (templ == gst_element_class_get_pad_template (klass,
2054 "send_rtcp_src_%d")) {
2055 result = create_rtcp (rtpbin, templ, name);
2057 goto wrong_template;
2059 GST_RTP_BIN_UNLOCK (rtpbin);
2066 GST_RTP_BIN_UNLOCK (rtpbin);
2067 g_warning ("gstrtpbin: this is not our template");
2073 gst_rtp_bin_release_pad (GstElement * element, GstPad * pad)