1 /* GStreamer
2 * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 /**
21 * SECTION:element-rtpbin
22 * @see_also: rtpjitterbuffer, rtpsession, rtpptdemux, rtpssrcdemux
23 *
24 * RTP bin combines the functions of #GstRtpSession, #GstRtpSsrcDemux,
25 * #GstRtpJitterBuffer and #GstRtpPtDemux in one element. It allows for multiple
26 * RTP sessions that will be synchronized together using RTCP SR packets.
27 *
28 * #GstRtpBin is configured with a number of request pads that define the
29 * functionality that is activated, similar to the #GstRtpSession element.
30 *
31 * To use #GstRtpBin as an RTP receiver, request a recv_rtp_sink_\%u pad. The session
32 * number must be specified in the pad name.
33 * Data received on the recv_rtp_sink_\%u pad will be processed in the #GstRtpSession
34 * manager and after being validated forwarded on #GstRtpSsrcDemux element. Each
35 * RTP stream is demuxed based on the SSRC and send to a #GstRtpJitterBuffer. After
36 * the packets are released from the jitterbuffer, they will be forwarded to a
37 * #GstRtpPtDemux element. The #GstRtpPtDemux element will demux the packets based
38 * on the payload type and will create a unique pad recv_rtp_src_\%u_\%u_\%u on
39 * rtpbin with the session number, SSRC and payload type respectively as the pad
40 * name.
41 *
42 * To also use #GstRtpBin as an RTCP receiver, request a recv_rtcp_sink_\%u pad. The
43 * session number must be specified in the pad name.
44 *
45 * If you want the session manager to generate and send RTCP packets, request
46 * the send_rtcp_src_\%u pad with the session number in the pad name. Packet pushed
47 * on this pad contain SR/RR RTCP reports that should be sent to all participants
48 * in the session.
49 *
50 * To use #GstRtpBin as a sender, request a send_rtp_sink_\%u pad, which will
51 * automatically create a send_rtp_src_\%u pad. If the session number is not provided,
52 * the pad from the lowest available session will be returned. The session manager will modify the
53 * SSRC in the RTP packets to its own SSRC and wil forward the packets on the
54 * send_rtp_src_\%u pad after updating its internal state.
55 *
56 * The session manager needs the clock-rate of the payload types it is handling
57 * and will signal the #GstRtpSession::request-pt-map signal when it needs such a
58 * mapping. One can clear the cached values with the #GstRtpSession::clear-pt-map
59 * signal.
60 *
61 * Access to the internal statistics of rtpbin is provided with the
62 * get-internal-session property. This action signal gives access to the
63 * RTPSession object which further provides action signals to retrieve the
64 * internal source and other sources.
65 *
66 * #GstRtpBin also has signals (#GstRtpBin::request-rtp-encoder,
67 * #GstRtpBin::request-rtp-decoder, #GstRtpBin::request-rtcp-encoder and
68 * #GstRtpBin::request-rtp-decoder) to dynamically request for RTP and RTCP encoders
69 * and decoders in order to support SRTP. The encoders must provide the pads
70 * rtp_sink_\%u and rtp_src_\%u for RTP and rtcp_sink_\%u and rtcp_src_\%u for
71 * RTCP. The session number will be used in the pad name. The decoders must provide
72 * rtp_sink and rtp_src for RTP and rtcp_sink and rtcp_src for RTCP. The decoders will
73 * be placed before the #GstRtpSession element, thus they must support SSRC demuxing
74 * internally.
75 *
76 * #GstRtpBin has signals (#GstRtpBin::request-aux-sender and
77 * #GstRtpBin::request-aux-receiver to dynamically request an element that can be
78 * used to create or merge additional RTP streams. AUX elements are needed to
79 * implement FEC or retransmission (such as RFC 4588). An AUX sender must have one
80 * sink_\%u pad that matches the sessionid in the signal and it should have 1 or
81 * more src_\%u pads. For each src_%\u pad, a session will be made (if needed)
82 * and the pad will be linked to the session send_rtp_sink pad. Each session will
83 * then expose its source pad as send_rtp_src_\%u on #GstRtpBin.
84 * An AUX receiver has 1 src_\%u pad that much match the sessionid in the signal
85 * and 1 or more sink_\%u pads. A session will be made for each sink_\%u pad
86 * when the corresponding recv_rtp_sink_\%u pad is requested on #GstRtpBin.
87 *
88 * <refsect2>
89 * <title>Example pipelines</title>
90 * |[
91 * gst-launch-1.0 udpsrc port=5000 caps="application/x-rtp, ..." ! .recv_rtp_sink_0 \
92 * rtpbin ! rtptheoradepay ! theoradec ! xvimagesink
93 * ]| Receive RTP data from port 5000 and send to the session 0 in rtpbin.
94 * |[
95 * gst-launch-1.0 rtpbin name=rtpbin \
96 * v4l2src ! videoconvert ! ffenc_h263 ! rtph263ppay ! rtpbin.send_rtp_sink_0 \
97 * rtpbin.send_rtp_src_0 ! udpsink port=5000 \
98 * rtpbin.send_rtcp_src_0 ! udpsink port=5001 sync=false async=false \
99 * udpsrc port=5005 ! rtpbin.recv_rtcp_sink_0 \
100 * audiotestsrc ! amrnbenc ! rtpamrpay ! rtpbin.send_rtp_sink_1 \
101 * rtpbin.send_rtp_src_1 ! udpsink port=5002 \
102 * rtpbin.send_rtcp_src_1 ! udpsink port=5003 sync=false async=false \
103 * udpsrc port=5007 ! rtpbin.recv_rtcp_sink_1
104 * ]| Encode and payload H263 video captured from a v4l2src. Encode and payload AMR
105 * audio generated from audiotestsrc. The video is sent to session 0 in rtpbin
106 * and the audio is sent to session 1. Video packets are sent on UDP port 5000
107 * and audio packets on port 5002. The video RTCP packets for session 0 are sent
108 * on port 5001 and the audio RTCP packets for session 0 are sent on port 5003.
109 * RTCP packets for session 0 are received on port 5005 and RTCP for session 1
110 * is received on port 5007. Since RTCP packets from the sender should be sent
111 * as soon as possible and do not participate in preroll, sync=false and
112 * async=false is configured on udpsink
113 * |[
114 * gst-launch-1.0 -v rtpbin name=rtpbin \
115 * udpsrc caps="application/x-rtp,media=(string)video,clock-rate=(int)90000,encoding-name=(string)H263-1998" \
116 * port=5000 ! rtpbin.recv_rtp_sink_0 \
117 * rtpbin. ! rtph263pdepay ! ffdec_h263 ! xvimagesink \
118 * udpsrc port=5001 ! rtpbin.recv_rtcp_sink_0 \
119 * rtpbin.send_rtcp_src_0 ! udpsink port=5005 sync=false async=false \
120 * udpsrc caps="application/x-rtp,media=(string)audio,clock-rate=(int)8000,encoding-name=(string)AMR,encoding-params=(string)1,octet-align=(string)1" \
121 * port=5002 ! rtpbin.recv_rtp_sink_1 \
122 * rtpbin. ! rtpamrdepay ! amrnbdec ! alsasink \
123 * udpsrc port=5003 ! rtpbin.recv_rtcp_sink_1 \
124 * rtpbin.send_rtcp_src_1 ! udpsink port=5007 sync=false async=false
125 * ]| Receive H263 on port 5000, send it through rtpbin in session 0, depayload,
126 * decode and display the video.
127 * Receive AMR on port 5002, send it through rtpbin in session 1, depayload,
128 * decode and play the audio.
129 * Receive server RTCP packets for session 0 on port 5001 and RTCP packets for
130 * session 1 on port 5003. These packets will be used for session management and
131 * synchronisation.
132 * Send RTCP reports for session 0 on port 5005 and RTCP reports for session 1
133 * on port 5007.
134 * </refsect2>
135 */
136
137 #ifdef HAVE_CONFIG_H
138 #include "config.h"
139 #endif
140 #include <stdio.h>
141 #include <string.h>
142
143 #include <gst/rtp/gstrtpbuffer.h>
144 #include <gst/rtp/gstrtcpbuffer.h>
145
146 #include "gstrtpbin.h"
147 #include "rtpsession.h"
148 #include "gstrtpsession.h"
149 #include "gstrtpjitterbuffer.h"
150
151 #include <gst/glib-compat-private.h>
152
153 GST_DEBUG_CATEGORY_STATIC (gst_rtp_bin_debug);
154 #define GST_CAT_DEFAULT gst_rtp_bin_debug
155
156 /* sink pads */
157 static GstStaticPadTemplate rtpbin_recv_rtp_sink_template =
158 GST_STATIC_PAD_TEMPLATE ("recv_rtp_sink_%u",
159 GST_PAD_SINK,
160 GST_PAD_REQUEST,
161 GST_STATIC_CAPS ("application/x-rtp;application/x-srtp")
162 );
163
164 static GstStaticPadTemplate rtpbin_recv_rtcp_sink_template =
165 GST_STATIC_PAD_TEMPLATE ("recv_rtcp_sink_%u",
166 GST_PAD_SINK,
167 GST_PAD_REQUEST,
168 GST_STATIC_CAPS ("application/x-rtcp;application/x-srtcp")
169 );
170
171 static GstStaticPadTemplate rtpbin_send_rtp_sink_template =
172 GST_STATIC_PAD_TEMPLATE ("send_rtp_sink_%u",
173 GST_PAD_SINK,
174 GST_PAD_REQUEST,
175 GST_STATIC_CAPS ("application/x-rtp")
176 );
177
178 /* src pads */
179 static GstStaticPadTemplate rtpbin_recv_rtp_src_template =
180 GST_STATIC_PAD_TEMPLATE ("recv_rtp_src_%u_%u_%u",
181 GST_PAD_SRC,
182 GST_PAD_SOMETIMES,
183 GST_STATIC_CAPS ("application/x-rtp")
184 );
185
186 static GstStaticPadTemplate rtpbin_send_rtcp_src_template =
187 GST_STATIC_PAD_TEMPLATE ("send_rtcp_src_%u",
188 GST_PAD_SRC,
189 GST_PAD_REQUEST,
190 GST_STATIC_CAPS ("application/x-rtcp;application/x-srtcp")
191 );
192
193 static GstStaticPadTemplate rtpbin_send_rtp_src_template =
194 GST_STATIC_PAD_TEMPLATE ("send_rtp_src_%u",
195 GST_PAD_SRC,
196 GST_PAD_SOMETIMES,
197 GST_STATIC_CAPS ("application/x-rtp;application/x-srtp")
198 );
199
200 #define GST_RTP_BIN_LOCK(bin) g_mutex_lock (&(bin)->priv->bin_lock)
201 #define GST_RTP_BIN_UNLOCK(bin) g_mutex_unlock (&(bin)->priv->bin_lock)
202
203 /* lock to protect dynamic callbacks, like pad-added and new ssrc. */
204 #define GST_RTP_BIN_DYN_LOCK(bin) g_mutex_lock (&(bin)->priv->dyn_lock)
205 #define GST_RTP_BIN_DYN_UNLOCK(bin) g_mutex_unlock (&(bin)->priv->dyn_lock)
206
207 /* lock for shutdown */
208 #define GST_RTP_BIN_SHUTDOWN_LOCK(bin,label) \
209 G_STMT_START { \
210 if (g_atomic_int_get (&bin->priv->shutdown)) \
211 goto label; \
212 GST_RTP_BIN_DYN_LOCK (bin); \
213 if (g_atomic_int_get (&bin->priv->shutdown)) { \
214 GST_RTP_BIN_DYN_UNLOCK (bin); \
215 goto label; \
216 } \
217 } G_STMT_END
218
219 /* unlock for shutdown */
220 #define GST_RTP_BIN_SHUTDOWN_UNLOCK(bin) \
221 GST_RTP_BIN_DYN_UNLOCK (bin); \
222
223 /* Minimum time offset to apply. This compensates for rounding errors in NTP to
224 * RTP timestamp conversions */
225 #define MIN_TS_OFFSET (4 * GST_MSECOND)
226
227 struct _GstRtpBinPrivate
228 {
229 GMutex bin_lock;
230
231 /* lock protecting dynamic adding/removing */
232 GMutex dyn_lock;
233
234 /* if we are shutting down or not */
235 gint shutdown;
236
237 gboolean autoremove;
238
239 /* NTP time in ns of last SR sync used */
240 guint64 last_ntpnstime;
241
242 /* list of extra elements */
243 GList *elements;
244 };
245
246 /* signals and args */
247 enum
248 {
249 SIGNAL_REQUEST_PT_MAP,
250 SIGNAL_PAYLOAD_TYPE_CHANGE,
251 SIGNAL_CLEAR_PT_MAP,
252 SIGNAL_RESET_SYNC,
253 SIGNAL_GET_SESSION,
254 SIGNAL_GET_INTERNAL_SESSION,
255 SIGNAL_GET_STORAGE,
256 SIGNAL_GET_INTERNAL_STORAGE,
257
258 SIGNAL_ON_NEW_SSRC,
259 SIGNAL_ON_SSRC_COLLISION,
260 SIGNAL_ON_SSRC_VALIDATED,
261 SIGNAL_ON_SSRC_ACTIVE,
262 SIGNAL_ON_SSRC_SDES,
263 SIGNAL_ON_BYE_SSRC,
264 SIGNAL_ON_BYE_TIMEOUT,
265 SIGNAL_ON_TIMEOUT,
266 SIGNAL_ON_SENDER_TIMEOUT,
267 SIGNAL_ON_NPT_STOP,
268
269 SIGNAL_REQUEST_RTP_ENCODER,
270 SIGNAL_REQUEST_RTP_DECODER,
271 SIGNAL_REQUEST_RTCP_ENCODER,
272 SIGNAL_REQUEST_RTCP_DECODER,
273
274 SIGNAL_REQUEST_FEC_DECODER,
275 SIGNAL_REQUEST_FEC_ENCODER,
276
277 SIGNAL_NEW_JITTERBUFFER,
278 SIGNAL_NEW_STORAGE,
279
280 SIGNAL_REQUEST_AUX_SENDER,
281 SIGNAL_REQUEST_AUX_RECEIVER,
282
283 SIGNAL_ON_NEW_SENDER_SSRC,
284 SIGNAL_ON_SENDER_SSRC_ACTIVE,
285
286 SIGNAL_ON_BUNDLED_SSRC,
287
288 LAST_SIGNAL
289 };
290
291 #define DEFAULT_LATENCY_MS 200
292 #define DEFAULT_DROP_ON_LATENCY FALSE
293 #define DEFAULT_SDES NULL
294 #define DEFAULT_DO_LOST FALSE
295 #define DEFAULT_IGNORE_PT FALSE
296 #define DEFAULT_NTP_SYNC FALSE
297 #define DEFAULT_AUTOREMOVE FALSE
298 #define DEFAULT_BUFFER_MODE RTP_JITTER_BUFFER_MODE_SLAVE
299 #define DEFAULT_USE_PIPELINE_CLOCK FALSE
300 #define DEFAULT_RTCP_SYNC GST_RTP_BIN_RTCP_SYNC_ALWAYS
301 #define DEFAULT_RTCP_SYNC_INTERVAL 0
302 #define DEFAULT_DO_SYNC_EVENT FALSE
303 #define DEFAULT_DO_RETRANSMISSION FALSE
304 #define DEFAULT_RTP_PROFILE GST_RTP_PROFILE_AVP
305 #define DEFAULT_NTP_TIME_SOURCE GST_RTP_NTP_TIME_SOURCE_NTP
306 #define DEFAULT_RTCP_SYNC_SEND_TIME TRUE
307 #define DEFAULT_MAX_RTCP_RTP_TIME_DIFF 1000
308 #define DEFAULT_MAX_DROPOUT_TIME 60000
309 #define DEFAULT_MAX_MISORDER_TIME 2000
310 #define DEFAULT_RFC7273_SYNC FALSE
311 #define DEFAULT_MAX_STREAMS G_MAXUINT
312 #define DEFAULT_MAX_TS_OFFSET_ADJUSTMENT G_GUINT64_CONSTANT(0)
313 #define DEFAULT_MAX_TS_OFFSET G_GINT64_CONSTANT(3000000000)
314
315 enum
316 {
317 PROP_0,
318 PROP_LATENCY,
319 PROP_DROP_ON_LATENCY,
320 PROP_SDES,
321 PROP_DO_LOST,
322 PROP_IGNORE_PT,
323 PROP_NTP_SYNC,
324 PROP_RTCP_SYNC,
325 PROP_RTCP_SYNC_INTERVAL,
326 PROP_AUTOREMOVE,
327 PROP_BUFFER_MODE,
328 PROP_USE_PIPELINE_CLOCK,
329 PROP_DO_SYNC_EVENT,
330 PROP_DO_RETRANSMISSION,
331 PROP_RTP_PROFILE,
332 PROP_NTP_TIME_SOURCE,
333 PROP_RTCP_SYNC_SEND_TIME,
334 PROP_MAX_RTCP_RTP_TIME_DIFF,
335 PROP_MAX_DROPOUT_TIME,
336 PROP_MAX_MISORDER_TIME,
337 PROP_RFC7273_SYNC,
338 PROP_MAX_STREAMS,
339 PROP_MAX_TS_OFFSET_ADJUSTMENT,
340 PROP_MAX_TS_OFFSET,
341 };
342
343 #define GST_RTP_BIN_RTCP_SYNC_TYPE (gst_rtp_bin_rtcp_sync_get_type())
344 static GType
gst_rtp_bin_rtcp_sync_get_type(void)345 gst_rtp_bin_rtcp_sync_get_type (void)
346 {
347 static GType rtcp_sync_type = 0;
348 static const GEnumValue rtcp_sync_types[] = {
349 {GST_RTP_BIN_RTCP_SYNC_ALWAYS, "always", "always"},
350 {GST_RTP_BIN_RTCP_SYNC_INITIAL, "initial", "initial"},
351 {GST_RTP_BIN_RTCP_SYNC_RTP, "rtp-info", "rtp-info"},
352 {0, NULL, NULL},
353 };
354
355 if (!rtcp_sync_type) {
356 rtcp_sync_type = g_enum_register_static ("GstRTCPSync", rtcp_sync_types);
357 }
358 return rtcp_sync_type;
359 }
360
361 /* helper objects */
362 typedef struct _GstRtpBinSession GstRtpBinSession;
363 typedef struct _GstRtpBinStream GstRtpBinStream;
364 typedef struct _GstRtpBinClient GstRtpBinClient;
365
366 static guint gst_rtp_bin_signals[LAST_SIGNAL] = { 0 };
367
368 static GstCaps *pt_map_requested (GstElement * element, guint pt,
369 GstRtpBinSession * session);
370 static void payload_type_change (GstElement * element, guint pt,
371 GstRtpBinSession * session);
372 static void remove_recv_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session);
373 static void remove_recv_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session);
374 static void remove_send_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session);
375 static void remove_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session);
376 static void free_client (GstRtpBinClient * client, GstRtpBin * bin);
377 static void free_stream (GstRtpBinStream * stream, GstRtpBin * bin);
378 static GstRtpBinSession *create_session (GstRtpBin * rtpbin, gint id);
379 static GstPad *complete_session_sink (GstRtpBin * rtpbin,
380 GstRtpBinSession * session);
381 static void
382 complete_session_receiver (GstRtpBin * rtpbin, GstRtpBinSession * session,
383 guint sessid);
384 static GstPad *complete_session_rtcp (GstRtpBin * rtpbin,
385 GstRtpBinSession * session, guint sessid);
386
387 /* Manages the RTP stream for one SSRC.
388 *
389 * We pipe the stream (comming from the SSRC demuxer) into a jitterbuffer.
390 * If we see an SDES RTCP packet that links multiple SSRCs together based on a
391 * common CNAME, we create a GstRtpBinClient structure to group the SSRCs
392 * together (see below).
393 */
394 struct _GstRtpBinStream
395 {
396 /* the SSRC of this stream */
397 guint32 ssrc;
398
399 /* parent bin */
400 GstRtpBin *bin;
401
402 /* the session this SSRC belongs to */
403 GstRtpBinSession *session;
404
405 /* the jitterbuffer of the SSRC */
406 GstElement *buffer;
407 gulong buffer_handlesync_sig;
408 gulong buffer_ptreq_sig;
409 gulong buffer_ntpstop_sig;
410 gint percent;
411
412 /* the PT demuxer of the SSRC */
413 GstElement *demux;
414 gulong demux_newpad_sig;
415 gulong demux_padremoved_sig;
416 gulong demux_ptreq_sig;
417 gulong demux_ptchange_sig;
418
419 /* if we have calculated a valid rt_delta for this stream */
420 gboolean have_sync;
421 /* mapping to local RTP and NTP time */
422 gint64 rt_delta;
423 gint64 rtp_delta;
424 /* base rtptime in gst time */
425 gint64 clock_base;
426 };
427
428 #define GST_RTP_SESSION_LOCK(sess) g_mutex_lock (&(sess)->lock)
429 #define GST_RTP_SESSION_UNLOCK(sess) g_mutex_unlock (&(sess)->lock)
430
431 /* Manages the receiving end of the packets.
432 *
433 * There is one such structure for each RTP session (audio/video/...).
434 * We get the RTP/RTCP packets and stuff them into the session manager. From
435 * there they are pushed into an SSRC demuxer that splits the stream based on
436 * SSRC. Each of the SSRC streams go into their own jitterbuffer (managed with
437 * the GstRtpBinStream above).
438 *
439 * Before the SSRC demuxer, a storage element may be inserted for the purpose
440 * of Forward Error Correction.
441 */
442 struct _GstRtpBinSession
443 {
444 /* session id */
445 gint id;
446 /* the parent bin */
447 GstRtpBin *bin;
448 /* the session element */
449 GstElement *session;
450 /* the SSRC demuxer */
451 GstElement *demux;
452 gulong demux_newpad_sig;
453 gulong demux_padremoved_sig;
454
455 /* Fec support */
456 GstElement *storage;
457
458 GMutex lock;
459
460 /* list of GstRtpBinStream */
461 GSList *streams;
462
463 /* list of elements */
464 GSList *elements;
465
466 /* mapping of payload type to caps */
467 GHashTable *ptmap;
468
469 /* the pads of the session */
470 GstPad *recv_rtp_sink;
471 GstPad *recv_rtp_sink_ghost;
472 GstPad *recv_rtp_src;
473 GstPad *recv_rtcp_sink;
474 GstPad *recv_rtcp_sink_ghost;
475 GstPad *sync_src;
476 GstPad *send_rtp_sink;
477 GstPad *send_rtp_sink_ghost;
478 GstPad *send_rtp_src_ghost;
479 GstPad *send_rtcp_src;
480 GstPad *send_rtcp_src_ghost;
481 };
482
483 /* Manages the RTP streams that come from one client and should therefore be
484 * synchronized.
485 */
486 struct _GstRtpBinClient
487 {
488 /* the common CNAME for the streams */
489 gchar *cname;
490 guint cname_len;
491
492 /* the streams */
493 guint nstreams;
494 GSList *streams;
495 };
496
497 /* find a session with the given id. Must be called with RTP_BIN_LOCK */
498 static GstRtpBinSession *
find_session_by_id(GstRtpBin * rtpbin,gint id)499 find_session_by_id (GstRtpBin * rtpbin, gint id)
500 {
501 GSList *walk;
502
503 for (walk = rtpbin->sessions; walk; walk = g_slist_next (walk)) {
504 GstRtpBinSession *sess = (GstRtpBinSession *) walk->data;
505
506 if (sess->id == id)
507 return sess;
508 }
509 return NULL;
510 }
511
512 /* find a session with the given request pad. Must be called with RTP_BIN_LOCK */
513 static GstRtpBinSession *
find_session_by_pad(GstRtpBin * rtpbin,GstPad * pad)514 find_session_by_pad (GstRtpBin * rtpbin, GstPad * pad)
515 {
516 GSList *walk;
517
518 for (walk = rtpbin->sessions; walk; walk = g_slist_next (walk)) {
519 GstRtpBinSession *sess = (GstRtpBinSession *) walk->data;
520
521 if ((sess->recv_rtp_sink_ghost == pad) ||
522 (sess->recv_rtcp_sink_ghost == pad) ||
523 (sess->send_rtp_sink_ghost == pad)
524 || (sess->send_rtcp_src_ghost == pad))
525 return sess;
526 }
527 return NULL;
528 }
529
530 static void
on_new_ssrc(GstElement * session,guint32 ssrc,GstRtpBinSession * sess)531 on_new_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
532 {
533 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC], 0,
534 sess->id, ssrc);
535 }
536
537 static void
on_ssrc_collision(GstElement * session,guint32 ssrc,GstRtpBinSession * sess)538 on_ssrc_collision (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
539 {
540 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION], 0,
541 sess->id, ssrc);
542 }
543
544 static void
on_ssrc_validated(GstElement * session,guint32 ssrc,GstRtpBinSession * sess)545 on_ssrc_validated (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
546 {
547 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
548 sess->id, ssrc);
549 }
550
551 static void
on_ssrc_active(GstElement * session,guint32 ssrc,GstRtpBinSession * sess)552 on_ssrc_active (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
553 {
554 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE], 0,
555 sess->id, ssrc);
556 }
557
558 static void
on_ssrc_sdes(GstElement * session,guint32 ssrc,GstRtpBinSession * sess)559 on_ssrc_sdes (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
560 {
561 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_SDES], 0,
562 sess->id, ssrc);
563 }
564
565 static void
on_bye_ssrc(GstElement * session,guint32 ssrc,GstRtpBinSession * sess)566 on_bye_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
567 {
568 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC], 0,
569 sess->id, ssrc);
570 }
571
572 static void
on_bye_timeout(GstElement * session,guint32 ssrc,GstRtpBinSession * sess)573 on_bye_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
574 {
575 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT], 0,
576 sess->id, ssrc);
577
578 if (sess->bin->priv->autoremove)
579 g_signal_emit_by_name (sess->demux, "clear-ssrc", ssrc, NULL);
580 }
581
582 static void
on_timeout(GstElement * session,guint32 ssrc,GstRtpBinSession * sess)583 on_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
584 {
585 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT], 0,
586 sess->id, ssrc);
587
588 if (sess->bin->priv->autoremove)
589 g_signal_emit_by_name (sess->demux, "clear-ssrc", ssrc, NULL);
590 }
591
592 static void
on_sender_timeout(GstElement * session,guint32 ssrc,GstRtpBinSession * sess)593 on_sender_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
594 {
595 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
596 sess->id, ssrc);
597 }
598
599 static void
on_npt_stop(GstElement * jbuf,GstRtpBinStream * stream)600 on_npt_stop (GstElement * jbuf, GstRtpBinStream * stream)
601 {
602 g_signal_emit (stream->bin, gst_rtp_bin_signals[SIGNAL_ON_NPT_STOP], 0,
603 stream->session->id, stream->ssrc);
604 }
605
606 static void
on_new_sender_ssrc(GstElement * session,guint32 ssrc,GstRtpBinSession * sess)607 on_new_sender_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
608 {
609 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_NEW_SENDER_SSRC], 0,
610 sess->id, ssrc);
611 }
612
613 static void
on_sender_ssrc_active(GstElement * session,guint32 ssrc,GstRtpBinSession * sess)614 on_sender_ssrc_active (GstElement * session, guint32 ssrc,
615 GstRtpBinSession * sess)
616 {
617 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SENDER_SSRC_ACTIVE],
618 0, sess->id, ssrc);
619 }
620
621 /* must be called with the SESSION lock */
622 static GstRtpBinStream *
find_stream_by_ssrc(GstRtpBinSession * session,guint32 ssrc)623 find_stream_by_ssrc (GstRtpBinSession * session, guint32 ssrc)
624 {
625 GSList *walk;
626
627 for (walk = session->streams; walk; walk = g_slist_next (walk)) {
628 GstRtpBinStream *stream = (GstRtpBinStream *) walk->data;
629
630 if (stream->ssrc == ssrc)
631 return stream;
632 }
633 return NULL;
634 }
635
636 static void
ssrc_demux_pad_removed(GstElement * element,guint ssrc,GstPad * pad,GstRtpBinSession * session)637 ssrc_demux_pad_removed (GstElement * element, guint ssrc, GstPad * pad,
638 GstRtpBinSession * session)
639 {
640 GstRtpBinStream *stream = NULL;
641 GstRtpBin *rtpbin;
642
643 rtpbin = session->bin;
644
645 GST_RTP_BIN_LOCK (rtpbin);
646
647 GST_RTP_SESSION_LOCK (session);
648 if ((stream = find_stream_by_ssrc (session, ssrc)))
649 session->streams = g_slist_remove (session->streams, stream);
650 GST_RTP_SESSION_UNLOCK (session);
651
652 if (stream)
653 free_stream (stream, rtpbin);
654
655 GST_RTP_BIN_UNLOCK (rtpbin);
656 }
657
658 /* create a session with the given id. Must be called with RTP_BIN_LOCK */
659 static GstRtpBinSession *
create_session(GstRtpBin * rtpbin,gint id)660 create_session (GstRtpBin * rtpbin, gint id)
661 {
662 GstRtpBinSession *sess;
663 GstElement *session, *demux;
664 GstElement *storage = NULL;
665 GstState target;
666
667 if (!(session = gst_element_factory_make ("rtpsession", NULL)))
668 goto no_session;
669
670 if (!(demux = gst_element_factory_make ("rtpssrcdemux", NULL)))
671 goto no_demux;
672
673 if (!(storage = gst_element_factory_make ("rtpstorage", NULL)))
674 goto no_storage;
675
676 /* need to sink the storage or otherwise signal handlers from bindings will
677 * take ownership of it and we don't own it anymore */
678 gst_object_ref_sink (storage);
679 g_signal_emit (rtpbin, gst_rtp_bin_signals[SIGNAL_NEW_STORAGE], 0, storage,
680 id);
681
682 sess = g_new0 (GstRtpBinSession, 1);
683 g_mutex_init (&sess->lock);
684 sess->id = id;
685 sess->bin = rtpbin;
686 sess->session = session;
687 sess->demux = demux;
688 sess->storage = storage;
689
690 sess->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
691 (GDestroyNotify) gst_caps_unref);
692 rtpbin->sessions = g_slist_prepend (rtpbin->sessions, sess);
693
694 /* configure SDES items */
695 GST_OBJECT_LOCK (rtpbin);
696 g_object_set (session, "sdes", rtpbin->sdes, "rtp-profile",
697 rtpbin->rtp_profile, "rtcp-sync-send-time", rtpbin->rtcp_sync_send_time,
698 NULL);
699 if (rtpbin->use_pipeline_clock)
700 g_object_set (session, "use-pipeline-clock", rtpbin->use_pipeline_clock,
701 NULL);
702 else
703 g_object_set (session, "ntp-time-source", rtpbin->ntp_time_source, NULL);
704
705 g_object_set (session, "max-dropout-time", rtpbin->max_dropout_time,
706 "max-misorder-time", rtpbin->max_misorder_time, NULL);
707 GST_OBJECT_UNLOCK (rtpbin);
708
709 /* provide clock_rate to the session manager when needed */
710 g_signal_connect (session, "request-pt-map",
711 (GCallback) pt_map_requested, sess);
712
713 g_signal_connect (sess->session, "on-new-ssrc",
714 (GCallback) on_new_ssrc, sess);
715 g_signal_connect (sess->session, "on-ssrc-collision",
716 (GCallback) on_ssrc_collision, sess);
717 g_signal_connect (sess->session, "on-ssrc-validated",
718 (GCallback) on_ssrc_validated, sess);
719 g_signal_connect (sess->session, "on-ssrc-active",
720 (GCallback) on_ssrc_active, sess);
721 g_signal_connect (sess->session, "on-ssrc-sdes",
722 (GCallback) on_ssrc_sdes, sess);
723 g_signal_connect (sess->session, "on-bye-ssrc",
724 (GCallback) on_bye_ssrc, sess);
725 g_signal_connect (sess->session, "on-bye-timeout",
726 (GCallback) on_bye_timeout, sess);
727 g_signal_connect (sess->session, "on-timeout", (GCallback) on_timeout, sess);
728 g_signal_connect (sess->session, "on-sender-timeout",
729 (GCallback) on_sender_timeout, sess);
730 g_signal_connect (sess->session, "on-new-sender-ssrc",
731 (GCallback) on_new_sender_ssrc, sess);
732 g_signal_connect (sess->session, "on-sender-ssrc-active",
733 (GCallback) on_sender_ssrc_active, sess);
734
735 gst_bin_add (GST_BIN_CAST (rtpbin), session);
736 gst_bin_add (GST_BIN_CAST (rtpbin), demux);
737 gst_bin_add (GST_BIN_CAST (rtpbin), storage);
738
739 /* unref the storage again, the bin has a reference now and
740 * we don't need it anymore */
741 gst_object_unref (storage);
742
743 GST_OBJECT_LOCK (rtpbin);
744 target = GST_STATE_TARGET (rtpbin);
745 GST_OBJECT_UNLOCK (rtpbin);
746
747 /* change state only to what's needed */
748 gst_element_set_state (demux, target);
749 gst_element_set_state (session, target);
750 gst_element_set_state (storage, target);
751
752 return sess;
753
754 /* ERRORS */
755 no_session:
756 {
757 g_warning ("rtpbin: could not create rtpsession element");
758 return NULL;
759 }
760 no_demux:
761 {
762 gst_object_unref (session);
763 g_warning ("rtpbin: could not create rtpssrcdemux element");
764 return NULL;
765 }
766 no_storage:
767 {
768 gst_object_unref (session);
769 gst_object_unref (demux);
770 g_warning ("rtpbin: could not create rtpstorage element");
771 return NULL;
772 }
773 }
774
775 static gboolean
bin_manage_element(GstRtpBin * bin,GstElement * element)776 bin_manage_element (GstRtpBin * bin, GstElement * element)
777 {
778 GstRtpBinPrivate *priv = bin->priv;
779
780 if (g_list_find (priv->elements, element)) {
781 GST_DEBUG_OBJECT (bin, "requested element %p already in bin", element);
782 } else {
783 GST_DEBUG_OBJECT (bin, "adding requested element %p", element);
784
785 if (g_object_is_floating (element))
786 element = gst_object_ref_sink (element);
787
788 if (!gst_bin_add (GST_BIN_CAST (bin), element))
789 goto add_failed;
790 if (!gst_element_sync_state_with_parent (element))
791 GST_WARNING_OBJECT (bin, "unable to sync element state with rtpbin");
792 }
793 /* we add the element multiple times, each we need an equal number of
794 * removes to really remove the element from the bin */
795 priv->elements = g_list_prepend (priv->elements, element);
796
797 return TRUE;
798
799 /* ERRORS */
800 add_failed:
801 {
802 GST_WARNING_OBJECT (bin, "unable to add element");
803 gst_object_unref (element);
804 return FALSE;
805 }
806 }
807
808 static void
remove_bin_element(GstElement * element,GstRtpBin * bin)809 remove_bin_element (GstElement * element, GstRtpBin * bin)
810 {
811 GstRtpBinPrivate *priv = bin->priv;
812 GList *find;
813
814 find = g_list_find (priv->elements, element);
815 if (find) {
816 priv->elements = g_list_delete_link (priv->elements, find);
817
818 if (!g_list_find (priv->elements, element)) {
819 gst_element_set_locked_state (element, TRUE);
820 gst_bin_remove (GST_BIN_CAST (bin), element);
821 gst_element_set_state (element, GST_STATE_NULL);
822 }
823
824 gst_object_unref (element);
825 }
826 }
827
828 /* called with RTP_BIN_LOCK */
829 static void
free_session(GstRtpBinSession * sess,GstRtpBin * bin)830 free_session (GstRtpBinSession * sess, GstRtpBin * bin)
831 {
832 GST_DEBUG_OBJECT (bin, "freeing session %p", sess);
833
834 gst_element_set_locked_state (sess->demux, TRUE);
835 gst_element_set_locked_state (sess->session, TRUE);
836 gst_element_set_locked_state (sess->storage, TRUE);
837
838 gst_element_set_state (sess->demux, GST_STATE_NULL);
839 gst_element_set_state (sess->session, GST_STATE_NULL);
840 gst_element_set_state (sess->storage, GST_STATE_NULL);
841
842 remove_recv_rtp (bin, sess);
843 remove_recv_rtcp (bin, sess);
844 remove_send_rtp (bin, sess);
845 remove_rtcp (bin, sess);
846
847 gst_bin_remove (GST_BIN_CAST (bin), sess->session);
848 gst_bin_remove (GST_BIN_CAST (bin), sess->demux);
849 gst_bin_remove (GST_BIN_CAST (bin), sess->storage);
850
851 g_slist_foreach (sess->elements, (GFunc) remove_bin_element, bin);
852 g_slist_free (sess->elements);
853
854 g_slist_foreach (sess->streams, (GFunc) free_stream, bin);
855 g_slist_free (sess->streams);
856
857 g_mutex_clear (&sess->lock);
858 g_hash_table_destroy (sess->ptmap);
859
860 g_free (sess);
861 }
862
863 /* get the payload type caps for the specific payload @pt in @session */
864 static GstCaps *
get_pt_map(GstRtpBinSession * session,guint pt)865 get_pt_map (GstRtpBinSession * session, guint pt)
866 {
867 GstCaps *caps = NULL;
868 GstRtpBin *bin;
869 GValue ret = { 0 };
870 GValue args[3] = { {0}, {0}, {0} };
871
872 GST_DEBUG ("searching pt %u in cache", pt);
873
874 GST_RTP_SESSION_LOCK (session);
875
876 /* first look in the cache */
877 caps = g_hash_table_lookup (session->ptmap, GINT_TO_POINTER (pt));
878 if (caps) {
879 gst_caps_ref (caps);
880 goto done;
881 }
882
883 bin = session->bin;
884
885 GST_DEBUG ("emiting signal for pt %u in session %u", pt, session->id);
886
887 /* not in cache, send signal to request caps */
888 g_value_init (&args[0], GST_TYPE_ELEMENT);
889 g_value_set_object (&args[0], bin);
890 g_value_init (&args[1], G_TYPE_UINT);
891 g_value_set_uint (&args[1], session->id);
892 g_value_init (&args[2], G_TYPE_UINT);
893 g_value_set_uint (&args[2], pt);
894
895 g_value_init (&ret, GST_TYPE_CAPS);
896 g_value_set_boxed (&ret, NULL);
897
898 GST_RTP_SESSION_UNLOCK (session);
899
900 g_signal_emitv (args, gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP], 0, &ret);
901
902 GST_RTP_SESSION_LOCK (session);
903
904 g_value_unset (&args[0]);
905 g_value_unset (&args[1]);
906 g_value_unset (&args[2]);
907
908 /* look in the cache again because we let the lock go */
909 caps = g_hash_table_lookup (session->ptmap, GINT_TO_POINTER (pt));
910 if (caps) {
911 gst_caps_ref (caps);
912 g_value_unset (&ret);
913 goto done;
914 }
915
916 caps = (GstCaps *) g_value_dup_boxed (&ret);
917 g_value_unset (&ret);
918 if (!caps)
919 goto no_caps;
920
921 GST_DEBUG ("caching pt %u as %" GST_PTR_FORMAT, pt, caps);
922
923 /* store in cache, take additional ref */
924 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (pt),
925 gst_caps_ref (caps));
926
927 done:
928 GST_RTP_SESSION_UNLOCK (session);
929
930 return caps;
931
932 /* ERRORS */
933 no_caps:
934 {
935 GST_RTP_SESSION_UNLOCK (session);
936 GST_DEBUG ("no pt map could be obtained");
937 return NULL;
938 }
939 }
940
941 static gboolean
return_true(gpointer key,gpointer value,gpointer user_data)942 return_true (gpointer key, gpointer value, gpointer user_data)
943 {
944 return TRUE;
945 }
946
947 static void
gst_rtp_bin_reset_sync(GstRtpBin * rtpbin)948 gst_rtp_bin_reset_sync (GstRtpBin * rtpbin)
949 {
950 GSList *clients, *streams;
951
952 GST_DEBUG_OBJECT (rtpbin, "Reset sync on all clients");
953
954 GST_RTP_BIN_LOCK (rtpbin);
955 for (clients = rtpbin->clients; clients; clients = g_slist_next (clients)) {
956 GstRtpBinClient *client = (GstRtpBinClient *) clients->data;
957
958 /* reset sync on all streams for this client */
959 for (streams = client->streams; streams; streams = g_slist_next (streams)) {
960 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
961
962 /* make use require a new SR packet for this stream before we attempt new
963 * lip-sync */
964 stream->have_sync = FALSE;
965 stream->rt_delta = 0;
966 stream->rtp_delta = 0;
967 stream->clock_base = -100 * GST_SECOND;
968 }
969 }
970 GST_RTP_BIN_UNLOCK (rtpbin);
971 }
972
973 static void
gst_rtp_bin_clear_pt_map(GstRtpBin * bin)974 gst_rtp_bin_clear_pt_map (GstRtpBin * bin)
975 {
976 GSList *sessions, *streams;
977
978 GST_RTP_BIN_LOCK (bin);
979 GST_DEBUG_OBJECT (bin, "clearing pt map");
980 for (sessions = bin->sessions; sessions; sessions = g_slist_next (sessions)) {
981 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
982
983 GST_DEBUG_OBJECT (bin, "clearing session %p", session);
984 g_signal_emit_by_name (session->session, "clear-pt-map", NULL);
985
986 GST_RTP_SESSION_LOCK (session);
987 g_hash_table_foreach_remove (session->ptmap, return_true, NULL);
988
989 for (streams = session->streams; streams; streams = g_slist_next (streams)) {
990 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
991
992 GST_DEBUG_OBJECT (bin, "clearing stream %p", stream);
993 g_signal_emit_by_name (stream->buffer, "clear-pt-map", NULL);
994 if (stream->demux)
995 g_signal_emit_by_name (stream->demux, "clear-pt-map", NULL);
996 }
997 GST_RTP_SESSION_UNLOCK (session);
998 }
999 GST_RTP_BIN_UNLOCK (bin);
1000
1001 /* reset sync too */
1002 gst_rtp_bin_reset_sync (bin);
1003 }
1004
1005 static GstElement *
gst_rtp_bin_get_session(GstRtpBin * bin,guint session_id)1006 gst_rtp_bin_get_session (GstRtpBin * bin, guint session_id)
1007 {
1008 GstRtpBinSession *session;
1009 GstElement *ret = NULL;
1010
1011 GST_RTP_BIN_LOCK (bin);
1012 GST_DEBUG_OBJECT (bin, "retrieving GstRtpSession, index: %u", session_id);
1013 session = find_session_by_id (bin, (gint) session_id);
1014 if (session) {
1015 ret = gst_object_ref (session->session);
1016 }
1017 GST_RTP_BIN_UNLOCK (bin);
1018
1019 return ret;
1020 }
1021
1022 static RTPSession *
gst_rtp_bin_get_internal_session(GstRtpBin * bin,guint session_id)1023 gst_rtp_bin_get_internal_session (GstRtpBin * bin, guint session_id)
1024 {
1025 RTPSession *internal_session = NULL;
1026 GstRtpBinSession *session;
1027
1028 GST_RTP_BIN_LOCK (bin);
1029 GST_DEBUG_OBJECT (bin, "retrieving internal RTPSession object, index: %u",
1030 session_id);
1031 session = find_session_by_id (bin, (gint) session_id);
1032 if (session) {
1033 g_object_get (session->session, "internal-session", &internal_session,
1034 NULL);
1035 }
1036 GST_RTP_BIN_UNLOCK (bin);
1037
1038 return internal_session;
1039 }
1040
1041 static GstElement *
gst_rtp_bin_get_storage(GstRtpBin * bin,guint session_id)1042 gst_rtp_bin_get_storage (GstRtpBin * bin, guint session_id)
1043 {
1044 GstRtpBinSession *session;
1045 GstElement *res = NULL;
1046
1047 GST_RTP_BIN_LOCK (bin);
1048 GST_DEBUG_OBJECT (bin, "retrieving internal storage object, index: %u",
1049 session_id);
1050 session = find_session_by_id (bin, (gint) session_id);
1051 if (session && session->storage) {
1052 res = gst_object_ref (session->storage);
1053 }
1054 GST_RTP_BIN_UNLOCK (bin);
1055
1056 return res;
1057 }
1058
1059 static GObject *
gst_rtp_bin_get_internal_storage(GstRtpBin * bin,guint session_id)1060 gst_rtp_bin_get_internal_storage (GstRtpBin * bin, guint session_id)
1061 {
1062 GObject *internal_storage = NULL;
1063 GstRtpBinSession *session;
1064
1065 GST_RTP_BIN_LOCK (bin);
1066 GST_DEBUG_OBJECT (bin, "retrieving internal storage object, index: %u",
1067 session_id);
1068 session = find_session_by_id (bin, (gint) session_id);
1069 if (session && session->storage) {
1070 g_object_get (session->storage, "internal-storage", &internal_storage,
1071 NULL);
1072 }
1073 GST_RTP_BIN_UNLOCK (bin);
1074
1075 return internal_storage;
1076 }
1077
1078 static GstElement *
gst_rtp_bin_request_encoder(GstRtpBin * bin,guint session_id)1079 gst_rtp_bin_request_encoder (GstRtpBin * bin, guint session_id)
1080 {
1081 GST_DEBUG_OBJECT (bin, "return NULL encoder");
1082 return NULL;
1083 }
1084
1085 static GstElement *
gst_rtp_bin_request_decoder(GstRtpBin * bin,guint session_id)1086 gst_rtp_bin_request_decoder (GstRtpBin * bin, guint session_id)
1087 {
1088 GST_DEBUG_OBJECT (bin, "return NULL decoder");
1089 return NULL;
1090 }
1091
1092 static void
gst_rtp_bin_propagate_property_to_jitterbuffer(GstRtpBin * bin,const gchar * name,const GValue * value)1093 gst_rtp_bin_propagate_property_to_jitterbuffer (GstRtpBin * bin,
1094 const gchar * name, const GValue * value)
1095 {
1096 GSList *sessions, *streams;
1097
1098 GST_RTP_BIN_LOCK (bin);
1099 for (sessions = bin->sessions; sessions; sessions = g_slist_next (sessions)) {
1100 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
1101
1102 GST_RTP_SESSION_LOCK (session);
1103 for (streams = session->streams; streams; streams = g_slist_next (streams)) {
1104 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
1105
1106 g_object_set_property (G_OBJECT (stream->buffer), name, value);
1107 }
1108 GST_RTP_SESSION_UNLOCK (session);
1109 }
1110 GST_RTP_BIN_UNLOCK (bin);
1111 }
1112
1113 static void
gst_rtp_bin_propagate_property_to_session(GstRtpBin * bin,const gchar * name,const GValue * value)1114 gst_rtp_bin_propagate_property_to_session (GstRtpBin * bin,
1115 const gchar * name, const GValue * value)
1116 {
1117 GSList *sessions;
1118
1119 GST_RTP_BIN_LOCK (bin);
1120 for (sessions = bin->sessions; sessions; sessions = g_slist_next (sessions)) {
1121 GstRtpBinSession *sess = (GstRtpBinSession *) sessions->data;
1122
1123 g_object_set_property (G_OBJECT (sess->session), name, value);
1124 }
1125 GST_RTP_BIN_UNLOCK (bin);
1126 }
1127
1128 /* get a client with the given SDES name. Must be called with RTP_BIN_LOCK */
1129 static GstRtpBinClient *
get_client(GstRtpBin * bin,guint8 len,guint8 * data,gboolean * created)1130 get_client (GstRtpBin * bin, guint8 len, guint8 * data, gboolean * created)
1131 {
1132 GstRtpBinClient *result = NULL;
1133 GSList *walk;
1134
1135 for (walk = bin->clients; walk; walk = g_slist_next (walk)) {
1136 GstRtpBinClient *client = (GstRtpBinClient *) walk->data;
1137
1138 if (len != client->cname_len)
1139 continue;
1140
1141 if (!strncmp ((gchar *) data, client->cname, client->cname_len)) {
1142 GST_DEBUG_OBJECT (bin, "found existing client %p with CNAME %s", client,
1143 client->cname);
1144 result = client;
1145 break;
1146 }
1147 }
1148
1149 /* nothing found, create one */
1150 if (result == NULL) {
1151 result = g_new0 (GstRtpBinClient, 1);
1152 result->cname = g_strndup ((gchar *) data, len);
1153 result->cname_len = len;
1154 bin->clients = g_slist_prepend (bin->clients, result);
1155 GST_DEBUG_OBJECT (bin, "created new client %p with CNAME %s", result,
1156 result->cname);
1157 }
1158 return result;
1159 }
1160
1161 static void
free_client(GstRtpBinClient * client,GstRtpBin * bin)1162 free_client (GstRtpBinClient * client, GstRtpBin * bin)
1163 {
1164 GST_DEBUG_OBJECT (bin, "freeing client %p", client);
1165 g_slist_free (client->streams);
1166 g_free (client->cname);
1167 g_free (client);
1168 }
1169
1170 static void
get_current_times(GstRtpBin * bin,GstClockTime * running_time,guint64 * ntpnstime)1171 get_current_times (GstRtpBin * bin, GstClockTime * running_time,
1172 guint64 * ntpnstime)
1173 {
1174 guint64 ntpns = -1;
1175 GstClock *clock;
1176 GstClockTime base_time, rt, clock_time;
1177
1178 GST_OBJECT_LOCK (bin);
1179 if ((clock = GST_ELEMENT_CLOCK (bin))) {
1180 base_time = GST_ELEMENT_CAST (bin)->base_time;
1181 gst_object_ref (clock);
1182 GST_OBJECT_UNLOCK (bin);
1183
1184 /* get current clock time and convert to running time */
1185 clock_time = gst_clock_get_time (clock);
1186 rt = clock_time - base_time;
1187
1188 if (bin->use_pipeline_clock) {
1189 ntpns = rt;
1190 /* add constant to convert from 1970 based time to 1900 based time */
1191 ntpns += (2208988800LL * GST_SECOND);
1192 } else {
1193 switch (bin->ntp_time_source) {
1194 case GST_RTP_NTP_TIME_SOURCE_NTP:
1195 case GST_RTP_NTP_TIME_SOURCE_UNIX:{
1196 GTimeVal current;
1197
1198 /* get current NTP time */
1199 g_get_current_time (¤t);
1200 ntpns = GST_TIMEVAL_TO_TIME (current);
1201
1202 /* add constant to convert from 1970 based time to 1900 based time */
1203 if (bin->ntp_time_source == GST_RTP_NTP_TIME_SOURCE_NTP)
1204 ntpns += (2208988800LL * GST_SECOND);
1205 break;
1206 }
1207 case GST_RTP_NTP_TIME_SOURCE_RUNNING_TIME:
1208 ntpns = rt;
1209 break;
1210 case GST_RTP_NTP_TIME_SOURCE_CLOCK_TIME:
1211 ntpns = clock_time;
1212 break;
1213 default:
1214 ntpns = -1; /* Fix uninited compiler warning */
1215 g_assert_not_reached ();
1216 break;
1217 }
1218 }
1219
1220 gst_object_unref (clock);
1221 } else {
1222 GST_OBJECT_UNLOCK (bin);
1223 rt = -1;
1224 ntpns = -1;
1225 }
1226 if (running_time)
1227 *running_time = rt;
1228 if (ntpnstime)
1229 *ntpnstime = ntpns;
1230 }
1231
1232 static void
stream_set_ts_offset(GstRtpBin * bin,GstRtpBinStream * stream,gint64 ts_offset,gint64 max_ts_offset,gint64 min_ts_offset,gboolean allow_positive_ts_offset)1233 stream_set_ts_offset (GstRtpBin * bin, GstRtpBinStream * stream,
1234 gint64 ts_offset, gint64 max_ts_offset, gint64 min_ts_offset,
1235 gboolean allow_positive_ts_offset)
1236 {
1237 gint64 prev_ts_offset;
1238
1239 g_object_get (stream->buffer, "ts-offset", &prev_ts_offset, NULL);
1240
1241 /* delta changed, see how much */
1242 if (prev_ts_offset != ts_offset) {
1243 gint64 diff;
1244
1245 diff = prev_ts_offset - ts_offset;
1246
1247 GST_DEBUG_OBJECT (bin,
1248 "ts-offset %" G_GINT64_FORMAT ", prev %" G_GINT64_FORMAT
1249 ", diff: %" G_GINT64_FORMAT, ts_offset, prev_ts_offset, diff);
1250
1251 /* ignore minor offsets */
1252 if (ABS (diff) < min_ts_offset) {
1253 GST_DEBUG_OBJECT (bin, "offset too small, ignoring");
1254 return;
1255 }
1256
1257 /* sanity check offset */
1258 if (max_ts_offset > 0) {
1259 if (ts_offset > 0 && !allow_positive_ts_offset) {
1260 GST_DEBUG_OBJECT (bin,
1261 "offset is positive (clocks are out of sync), ignoring");
1262 return;
1263 }
1264 if (ABS (ts_offset) > max_ts_offset) {
1265 GST_DEBUG_OBJECT (bin, "offset too large, ignoring");
1266 return;
1267 }
1268 }
1269
1270 g_object_set (stream->buffer, "ts-offset", ts_offset, NULL);
1271 }
1272 GST_DEBUG_OBJECT (bin, "stream SSRC %08x, delta %" G_GINT64_FORMAT,
1273 stream->ssrc, ts_offset);
1274 }
1275
1276 static void
gst_rtp_bin_send_sync_event(GstRtpBinStream * stream)1277 gst_rtp_bin_send_sync_event (GstRtpBinStream * stream)
1278 {
1279 if (stream->bin->send_sync_event) {
1280 GstEvent *event;
1281 GstPad *srcpad;
1282
1283 GST_DEBUG_OBJECT (stream->bin,
1284 "sending GstRTCPSRReceived event downstream");
1285
1286 event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM,
1287 gst_structure_new_empty ("GstRTCPSRReceived"));
1288
1289 srcpad = gst_element_get_static_pad (stream->buffer, "src");
1290 gst_pad_push_event (srcpad, event);
1291 gst_object_unref (srcpad);
1292 }
1293 }
1294
1295 /* associate a stream to the given CNAME. This will make sure all streams for
1296 * that CNAME are synchronized together.
1297 * Must be called with GST_RTP_BIN_LOCK */
1298 static void
gst_rtp_bin_associate(GstRtpBin * bin,GstRtpBinStream * stream,guint8 len,guint8 * data,guint64 ntptime,guint64 last_extrtptime,guint64 base_rtptime,guint64 base_time,guint clock_rate,gint64 rtp_clock_base)1299 gst_rtp_bin_associate (GstRtpBin * bin, GstRtpBinStream * stream, guint8 len,
1300 guint8 * data, guint64 ntptime, guint64 last_extrtptime,
1301 guint64 base_rtptime, guint64 base_time, guint clock_rate,
1302 gint64 rtp_clock_base)
1303 {
1304 GstRtpBinClient *client;
1305 gboolean created;
1306 GSList *walk;
1307 GstClockTime running_time, running_time_rtp;
1308 guint64 ntpnstime;
1309
1310 /* first find or create the CNAME */
1311 client = get_client (bin, len, data, &created);
1312
1313 /* find stream in the client */
1314 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
1315 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
1316
1317 if (ostream == stream)
1318 break;
1319 }
1320 /* not found, add it to the list */
1321 if (walk == NULL) {
1322 GST_DEBUG_OBJECT (bin,
1323 "new association of SSRC %08x with client %p with CNAME %s",
1324 stream->ssrc, client, client->cname);
1325 client->streams = g_slist_prepend (client->streams, stream);
1326 client->nstreams++;
1327 } else {
1328 GST_DEBUG_OBJECT (bin,
1329 "found association of SSRC %08x with client %p with CNAME %s",
1330 stream->ssrc, client, client->cname);
1331 }
1332
1333 if (!GST_CLOCK_TIME_IS_VALID (last_extrtptime)) {
1334 GST_DEBUG_OBJECT (bin, "invalidated sync data");
1335 if (bin->rtcp_sync == GST_RTP_BIN_RTCP_SYNC_RTP) {
1336 /* we don't need that data, so carry on,
1337 * but make some values look saner */
1338 last_extrtptime = base_rtptime;
1339 } else {
1340 /* nothing we can do with this data in this case */
1341 GST_DEBUG_OBJECT (bin, "bailing out");
1342 return;
1343 }
1344 }
1345
1346 /* Take the extended rtptime we found in the SR packet and map it to the
1347 * local rtptime. The local rtp time is used to construct timestamps on the
1348 * buffers so we will calculate what running_time corresponds to the RTP
1349 * timestamp in the SR packet. */
1350 running_time_rtp = last_extrtptime - base_rtptime;
1351
1352 GST_DEBUG_OBJECT (bin,
1353 "base %" G_GUINT64_FORMAT ", extrtptime %" G_GUINT64_FORMAT
1354 ", local RTP %" G_GUINT64_FORMAT ", clock-rate %d, "
1355 "clock-base %" G_GINT64_FORMAT, base_rtptime,
1356 last_extrtptime, running_time_rtp, clock_rate, rtp_clock_base);
1357
1358 /* calculate local RTP time in gstreamer timestamp, we essentially perform the
1359 * same conversion that a jitterbuffer would use to convert an rtp timestamp
1360 * into a corresponding gstreamer timestamp. Note that the base_time also
1361 * contains the drift between sender and receiver. */
1362 running_time =
1363 gst_util_uint64_scale_int (running_time_rtp, GST_SECOND, clock_rate);
1364 running_time += base_time;
1365
1366 /* convert ntptime to nanoseconds */
1367 ntpnstime = gst_util_uint64_scale (ntptime, GST_SECOND,
1368 (G_GINT64_CONSTANT (1) << 32));
1369
1370 stream->have_sync = TRUE;
1371
1372 GST_DEBUG_OBJECT (bin,
1373 "SR RTP running time %" G_GUINT64_FORMAT ", SR NTP %" G_GUINT64_FORMAT,
1374 running_time, ntpnstime);
1375
1376 /* recalc inter stream playout offset, but only if there is more than one
1377 * stream or we're doing NTP sync. */
1378 if (bin->ntp_sync) {
1379 gint64 ntpdiff, rtdiff;
1380 guint64 local_ntpnstime;
1381 GstClockTime local_running_time;
1382
1383 /* For NTP sync we need to first get a snapshot of running_time and NTP
1384 * time. We know at what running_time we play a certain RTP time, we also
1385 * calculated when we would play the RTP time in the SR packet. Now we need
1386 * to know how the running_time and the NTP time relate to eachother. */
1387 get_current_times (bin, &local_running_time, &local_ntpnstime);
1388
1389 /* see how far away the NTP time is. This is the difference between the
1390 * current NTP time and the NTP time in the last SR packet. */
1391 ntpdiff = local_ntpnstime - ntpnstime;
1392 /* see how far away the running_time is. This is the difference between the
1393 * current running_time and the running_time of the RTP timestamp in the
1394 * last SR packet. */
1395 rtdiff = local_running_time - running_time;
1396
1397 GST_DEBUG_OBJECT (bin,
1398 "local NTP time %" G_GUINT64_FORMAT ", SR NTP time %" G_GUINT64_FORMAT,
1399 local_ntpnstime, ntpnstime);
1400 GST_DEBUG_OBJECT (bin,
1401 "local running time %" G_GUINT64_FORMAT ", SR RTP running time %"
1402 G_GUINT64_FORMAT, local_running_time, running_time);
1403 GST_DEBUG_OBJECT (bin,
1404 "NTP diff %" G_GINT64_FORMAT ", RT diff %" G_GINT64_FORMAT, ntpdiff,
1405 rtdiff);
1406
1407 /* combine to get the final diff to apply to the running_time */
1408 stream->rt_delta = rtdiff - ntpdiff;
1409
1410 stream_set_ts_offset (bin, stream, stream->rt_delta, bin->max_ts_offset,
1411 0, FALSE);
1412 } else {
1413 gint64 min, rtp_min, clock_base = stream->clock_base;
1414 gboolean all_sync, use_rtp;
1415 gboolean rtcp_sync = g_atomic_int_get (&bin->rtcp_sync);
1416
1417 /* calculate delta between server and receiver. ntpnstime is created by
1418 * converting the ntptime in the last SR packet to a gstreamer timestamp. This
1419 * delta expresses the difference to our timeline and the server timeline. The
1420 * difference in itself doesn't mean much but we can combine the delta of
1421 * multiple streams to create a stream specific offset. */
1422 stream->rt_delta = ntpnstime - running_time;
1423
1424 /* calculate the min of all deltas, ignoring streams that did not yet have a
1425 * valid rt_delta because we did not yet receive an SR packet for those
1426 * streams.
1427 * We calculate the mininum because we would like to only apply positive
1428 * offsets to streams, delaying their playback instead of trying to speed up
1429 * other streams (which might be imposible when we have to create negative
1430 * latencies).
1431 * The stream that has the smallest diff is selected as the reference stream,
1432 * all other streams will have a positive offset to this difference. */
1433
1434 /* some alternative setting allow ignoring RTCP as much as possible,
1435 * for servers generating bogus ntp timeline */
1436 min = rtp_min = G_MAXINT64;
1437 use_rtp = FALSE;
1438 if (rtcp_sync == GST_RTP_BIN_RTCP_SYNC_RTP) {
1439 guint64 ext_base;
1440
1441 use_rtp = TRUE;
1442 /* signed version for convienience */
1443 clock_base = base_rtptime;
1444 /* deal with possible wrap-around */
1445 ext_base = base_rtptime;
1446 rtp_clock_base = gst_rtp_buffer_ext_timestamp (&ext_base, rtp_clock_base);
1447 /* sanity check; base rtp and provided clock_base should be close */
1448 if (rtp_clock_base >= clock_base) {
1449 if (rtp_clock_base - clock_base < 10 * clock_rate) {
1450 rtp_clock_base = base_time +
1451 gst_util_uint64_scale_int (rtp_clock_base - clock_base,
1452 GST_SECOND, clock_rate);
1453 } else {
1454 use_rtp = FALSE;
1455 }
1456 } else {
1457 if (clock_base - rtp_clock_base < 10 * clock_rate) {
1458 rtp_clock_base = base_time -
1459 gst_util_uint64_scale_int (clock_base - rtp_clock_base,
1460 GST_SECOND, clock_rate);
1461 } else {
1462 use_rtp = FALSE;
1463 }
1464 }
1465 /* warn and bail for clarity out if no sane values */
1466 if (!use_rtp) {
1467 GST_WARNING_OBJECT (bin, "unable to sync to provided rtptime");
1468 return;
1469 }
1470 /* store to track changes */
1471 clock_base = rtp_clock_base;
1472 /* generate a fake as before,
1473 * now equating rtptime obtained from RTP-Info,
1474 * where the large time represent the otherwise irrelevant npt/ntp time */
1475 stream->rtp_delta = (GST_SECOND << 28) - rtp_clock_base;
1476 } else {
1477 clock_base = rtp_clock_base;
1478 }
1479
1480 all_sync = TRUE;
1481 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
1482 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
1483
1484 if (!ostream->have_sync) {
1485 all_sync = FALSE;
1486 continue;
1487 }
1488
1489 /* change in current stream's base from previously init'ed value
1490 * leads to reset of all stream's base */
1491 if (stream != ostream && stream->clock_base >= 0 &&
1492 (stream->clock_base != clock_base)) {
1493 GST_DEBUG_OBJECT (bin, "reset upon clock base change");
1494 ostream->clock_base = -100 * GST_SECOND;
1495 ostream->rtp_delta = 0;
1496 }
1497
1498 if (ostream->rt_delta < min)
1499 min = ostream->rt_delta;
1500 if (ostream->rtp_delta < rtp_min)
1501 rtp_min = ostream->rtp_delta;
1502 }
1503
1504 /* arrange to re-sync for each stream upon significant change,
1505 * e.g. post-seek */
1506 all_sync = all_sync && (stream->clock_base == clock_base);
1507 stream->clock_base = clock_base;
1508
1509 /* may need init performed above later on, but nothing more to do now */
1510 if (client->nstreams <= 1)
1511 return;
1512
1513 GST_DEBUG_OBJECT (bin, "client %p min delta %" G_GINT64_FORMAT
1514 " all sync %d", client, min, all_sync);
1515 GST_DEBUG_OBJECT (bin, "rtcp sync mode %d, use_rtp %d", rtcp_sync, use_rtp);
1516
1517 switch (rtcp_sync) {
1518 case GST_RTP_BIN_RTCP_SYNC_RTP:
1519 if (!use_rtp)
1520 break;
1521 GST_DEBUG_OBJECT (bin, "using rtp generated reports; "
1522 "client %p min rtp delta %" G_GINT64_FORMAT, client, rtp_min);
1523 /* fall-through */
1524 case GST_RTP_BIN_RTCP_SYNC_INITIAL:
1525 /* if all have been synced already, do not bother further */
1526 if (all_sync) {
1527 GST_DEBUG_OBJECT (bin, "all streams already synced; done");
1528 return;
1529 }
1530 break;
1531 default:
1532 break;
1533 }
1534
1535 /* bail out if we adjusted recently enough */
1536 if (all_sync && (ntpnstime - bin->priv->last_ntpnstime) <
1537 bin->rtcp_sync_interval * GST_MSECOND) {
1538 GST_DEBUG_OBJECT (bin, "discarding RTCP sender packet for sync; "
1539 "previous sender info too recent "
1540 "(previous NTP %" G_GUINT64_FORMAT ")", bin->priv->last_ntpnstime);
1541 return;
1542 }
1543 bin->priv->last_ntpnstime = ntpnstime;
1544
1545 /* calculate offsets for each stream */
1546 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
1547 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
1548 gint64 ts_offset;
1549
1550 /* ignore streams for which we didn't receive an SR packet yet, we
1551 * can't synchronize them yet. We can however sync other streams just
1552 * fine. */
1553 if (!ostream->have_sync)
1554 continue;
1555
1556 /* calculate offset to our reference stream, this should always give a
1557 * positive number. */
1558 if (use_rtp)
1559 ts_offset = ostream->rtp_delta - rtp_min;
1560 else
1561 ts_offset = ostream->rt_delta - min;
1562
1563 stream_set_ts_offset (bin, ostream, ts_offset, bin->max_ts_offset,
1564 MIN_TS_OFFSET, TRUE);
1565 }
1566 }
1567 gst_rtp_bin_send_sync_event (stream);
1568
1569 return;
1570 }
1571
1572 #define GST_RTCP_BUFFER_FOR_PACKETS(b,buffer,packet) \
1573 for ((b) = gst_rtcp_buffer_get_first_packet ((buffer), (packet)); (b); \
1574 (b) = gst_rtcp_packet_move_to_next ((packet)))
1575
1576 #define GST_RTCP_SDES_FOR_ITEMS(b,packet) \
1577 for ((b) = gst_rtcp_packet_sdes_first_item ((packet)); (b); \
1578 (b) = gst_rtcp_packet_sdes_next_item ((packet)))
1579
1580 #define GST_RTCP_SDES_FOR_ENTRIES(b,packet) \
1581 for ((b) = gst_rtcp_packet_sdes_first_entry ((packet)); (b); \
1582 (b) = gst_rtcp_packet_sdes_next_entry ((packet)))
1583
1584 static void
gst_rtp_bin_handle_sync(GstElement * jitterbuffer,GstStructure * s,GstRtpBinStream * stream)1585 gst_rtp_bin_handle_sync (GstElement * jitterbuffer, GstStructure * s,
1586 GstRtpBinStream * stream)
1587 {
1588 GstRtpBin *bin;
1589 GstRTCPPacket packet;
1590 guint32 ssrc;
1591 guint64 ntptime;
1592 gboolean have_sr, have_sdes;
1593 gboolean more;
1594 guint64 base_rtptime;
1595 guint64 base_time;
1596 guint clock_rate;
1597 guint64 clock_base;
1598 guint64 extrtptime;
1599 GstBuffer *buffer;
1600 GstRTCPBuffer rtcp = { NULL, };
1601
1602 bin = stream->bin;
1603
1604 GST_DEBUG_OBJECT (bin, "sync handler called");
1605
1606 /* get the last relation between the rtp timestamps and the gstreamer
1607 * timestamps. We get this info directly from the jitterbuffer which
1608 * constructs gstreamer timestamps from rtp timestamps and so it know exactly
1609 * what the current situation is. */
1610 base_rtptime =
1611 g_value_get_uint64 (gst_structure_get_value (s, "base-rtptime"));
1612 base_time = g_value_get_uint64 (gst_structure_get_value (s, "base-time"));
1613 clock_rate = g_value_get_uint (gst_structure_get_value (s, "clock-rate"));
1614 clock_base = g_value_get_uint64 (gst_structure_get_value (s, "clock-base"));
1615 extrtptime =
1616 g_value_get_uint64 (gst_structure_get_value (s, "sr-ext-rtptime"));
1617 buffer = gst_value_get_buffer (gst_structure_get_value (s, "sr-buffer"));
1618
1619 have_sr = FALSE;
1620 have_sdes = FALSE;
1621
1622 gst_rtcp_buffer_map (buffer, GST_MAP_READ, &rtcp);
1623
1624 GST_RTCP_BUFFER_FOR_PACKETS (more, &rtcp, &packet) {
1625 /* first packet must be SR or RR or else the validate would have failed */
1626 switch (gst_rtcp_packet_get_type (&packet)) {
1627 case GST_RTCP_TYPE_SR:
1628 /* only parse first. There is only supposed to be one SR in the packet
1629 * but we will deal with malformed packets gracefully */
1630 if (have_sr)
1631 break;
1632 /* get NTP and RTP times */
1633 gst_rtcp_packet_sr_get_sender_info (&packet, &ssrc, &ntptime, NULL,
1634 NULL, NULL);
1635
1636 GST_DEBUG_OBJECT (bin, "received sync packet from SSRC %08x", ssrc);
1637 /* ignore SR that is not ours */
1638 if (ssrc != stream->ssrc)
1639 continue;
1640
1641 have_sr = TRUE;
1642 break;
1643 case GST_RTCP_TYPE_SDES:
1644 {
1645 gboolean more_items, more_entries;
1646
1647 /* only deal with first SDES, there is only supposed to be one SDES in
1648 * the RTCP packet but we deal with bad packets gracefully. Also bail
1649 * out if we have not seen an SR item yet. */
1650 if (have_sdes || !have_sr)
1651 break;
1652
1653 GST_RTCP_SDES_FOR_ITEMS (more_items, &packet) {
1654 /* skip items that are not about the SSRC of the sender */
1655 if (gst_rtcp_packet_sdes_get_ssrc (&packet) != ssrc)
1656 continue;
1657
1658 /* find the CNAME entry */
1659 GST_RTCP_SDES_FOR_ENTRIES (more_entries, &packet) {
1660 GstRTCPSDESType type;
1661 guint8 len;
1662 guint8 *data;
1663
1664 gst_rtcp_packet_sdes_get_entry (&packet, &type, &len, &data);
1665
1666 if (type == GST_RTCP_SDES_CNAME) {
1667 GST_RTP_BIN_LOCK (bin);
1668 /* associate the stream to CNAME */
1669 gst_rtp_bin_associate (bin, stream, len, data,
1670 ntptime, extrtptime, base_rtptime, base_time, clock_rate,
1671 clock_base);
1672 GST_RTP_BIN_UNLOCK (bin);
1673 }
1674 }
1675 }
1676 have_sdes = TRUE;
1677 break;
1678 }
1679 default:
1680 /* we can ignore these packets */
1681 break;
1682 }
1683 }
1684 gst_rtcp_buffer_unmap (&rtcp);
1685 }
1686
1687 /* create a new stream with @ssrc in @session. Must be called with
1688 * RTP_SESSION_LOCK. */
1689 static GstRtpBinStream *
create_stream(GstRtpBinSession * session,guint32 ssrc)1690 create_stream (GstRtpBinSession * session, guint32 ssrc)
1691 {
1692 GstElement *buffer, *demux = NULL;
1693 GstRtpBinStream *stream;
1694 GstRtpBin *rtpbin;
1695 GstState target;
1696
1697 rtpbin = session->bin;
1698
1699 if (g_slist_length (session->streams) >= rtpbin->max_streams)
1700 goto max_streams;
1701
1702 if (!(buffer = gst_element_factory_make ("rtpjitterbuffer", NULL)))
1703 goto no_jitterbuffer;
1704
1705 if (!rtpbin->ignore_pt) {
1706 if (!(demux = gst_element_factory_make ("rtpptdemux", NULL)))
1707 goto no_demux;
1708 }
1709
1710 stream = g_new0 (GstRtpBinStream, 1);
1711 stream->ssrc = ssrc;
1712 stream->bin = rtpbin;
1713 stream->session = session;
1714 stream->buffer = buffer;
1715 stream->demux = demux;
1716
1717 stream->have_sync = FALSE;
1718 stream->rt_delta = 0;
1719 stream->rtp_delta = 0;
1720 stream->percent = 100;
1721 stream->clock_base = -100 * GST_SECOND;
1722 session->streams = g_slist_prepend (session->streams, stream);
1723
1724 /* provide clock_rate to the jitterbuffer when needed */
1725 stream->buffer_ptreq_sig = g_signal_connect (buffer, "request-pt-map",
1726 (GCallback) pt_map_requested, session);
1727 stream->buffer_ntpstop_sig = g_signal_connect (buffer, "on-npt-stop",
1728 (GCallback) on_npt_stop, stream);
1729
1730 g_object_set_data (G_OBJECT (buffer), "GstRTPBin.session", session);
1731 g_object_set_data (G_OBJECT (buffer), "GstRTPBin.stream", stream);
1732
1733 /* configure latency and packet lost */
1734 g_object_set (buffer, "latency", rtpbin->latency_ms, NULL);
1735 g_object_set (buffer, "drop-on-latency", rtpbin->drop_on_latency, NULL);
1736 g_object_set (buffer, "do-lost", rtpbin->do_lost, NULL);
1737 g_object_set (buffer, "mode", rtpbin->buffer_mode, NULL);
1738 g_object_set (buffer, "do-retransmission", rtpbin->do_retransmission, NULL);
1739 g_object_set (buffer, "max-rtcp-rtp-time-diff",
1740 rtpbin->max_rtcp_rtp_time_diff, NULL);
1741 g_object_set (buffer, "max-dropout-time", rtpbin->max_dropout_time,
1742 "max-misorder-time", rtpbin->max_misorder_time, NULL);
1743 g_object_set (buffer, "rfc7273-sync", rtpbin->rfc7273_sync, NULL);
1744 g_object_set (buffer, "max-ts-offset-adjustment",
1745 rtpbin->max_ts_offset_adjustment, NULL);
1746
1747 /* need to sink the jitterbufer or otherwise signal handlers from bindings will
1748 * take ownership of it and we don't own it anymore */
1749 gst_object_ref_sink (buffer);
1750 g_signal_emit (rtpbin, gst_rtp_bin_signals[SIGNAL_NEW_JITTERBUFFER], 0,
1751 buffer, session->id, ssrc);
1752
1753 if (!rtpbin->ignore_pt)
1754 gst_bin_add (GST_BIN_CAST (rtpbin), demux);
1755 gst_bin_add (GST_BIN_CAST (rtpbin), buffer);
1756
1757 /* unref the jitterbuffer again, the bin has a reference now and
1758 * we don't need it anymore */
1759 gst_object_unref (buffer);
1760
1761 /* link stuff */
1762 if (demux)
1763 gst_element_link_pads_full (buffer, "src", demux, "sink",
1764 GST_PAD_LINK_CHECK_NOTHING);
1765
1766 if (rtpbin->buffering) {
1767 guint64 last_out;
1768
1769 GST_INFO_OBJECT (rtpbin,
1770 "bin is buffering, set jitterbuffer as not active");
1771 g_signal_emit_by_name (buffer, "set-active", FALSE, (gint64) 0, &last_out);
1772 }
1773
1774
1775 GST_OBJECT_LOCK (rtpbin);
1776 target = GST_STATE_TARGET (rtpbin);
1777 GST_OBJECT_UNLOCK (rtpbin);
1778
1779 /* from sink to source */
1780 if (demux)
1781 gst_element_set_state (demux, target);
1782
1783 gst_element_set_state (buffer, target);
1784
1785 return stream;
1786
1787 /* ERRORS */
1788 max_streams:
1789 {
1790 GST_WARNING_OBJECT (rtpbin, "stream exeeds maximum (%d)",
1791 rtpbin->max_streams);
1792 return NULL;
1793 }
1794 no_jitterbuffer:
1795 {
1796 g_warning ("rtpbin: could not create rtpjitterbuffer element");
1797 return NULL;
1798 }
1799 no_demux:
1800 {
1801 gst_object_unref (buffer);
1802 g_warning ("rtpbin: could not create rtpptdemux element");
1803 return NULL;
1804 }
1805 }
1806
1807 /* called with RTP_BIN_LOCK */
1808 static void
free_stream(GstRtpBinStream * stream,GstRtpBin * bin)1809 free_stream (GstRtpBinStream * stream, GstRtpBin * bin)
1810 {
1811 GSList *clients, *next_client;
1812
1813 GST_DEBUG_OBJECT (bin, "freeing stream %p", stream);
1814
1815 if (stream->demux) {
1816 g_signal_handler_disconnect (stream->demux, stream->demux_newpad_sig);
1817 g_signal_handler_disconnect (stream->demux, stream->demux_ptreq_sig);
1818 g_signal_handler_disconnect (stream->demux, stream->demux_ptchange_sig);
1819 }
1820 g_signal_handler_disconnect (stream->buffer, stream->buffer_handlesync_sig);
1821 g_signal_handler_disconnect (stream->buffer, stream->buffer_ptreq_sig);
1822 g_signal_handler_disconnect (stream->buffer, stream->buffer_ntpstop_sig);
1823
1824 if (stream->demux)
1825 gst_element_set_locked_state (stream->demux, TRUE);
1826 gst_element_set_locked_state (stream->buffer, TRUE);
1827
1828 if (stream->demux)
1829 gst_element_set_state (stream->demux, GST_STATE_NULL);
1830 gst_element_set_state (stream->buffer, GST_STATE_NULL);
1831
1832 /* now remove this signal, we need this while going to NULL because it to
1833 * do some cleanups */
1834 if (stream->demux)
1835 g_signal_handler_disconnect (stream->demux, stream->demux_padremoved_sig);
1836
1837 gst_bin_remove (GST_BIN_CAST (bin), stream->buffer);
1838 if (stream->demux)
1839 gst_bin_remove (GST_BIN_CAST (bin), stream->demux);
1840
1841 for (clients = bin->clients; clients; clients = next_client) {
1842 GstRtpBinClient *client = (GstRtpBinClient *) clients->data;
1843 GSList *streams, *next_stream;
1844
1845 next_client = g_slist_next (clients);
1846
1847 for (streams = client->streams; streams; streams = next_stream) {
1848 GstRtpBinStream *ostream = (GstRtpBinStream *) streams->data;
1849
1850 next_stream = g_slist_next (streams);
1851
1852 if (ostream == stream) {
1853 client->streams = g_slist_delete_link (client->streams, streams);
1854 /* If this was the last stream belonging to this client,
1855 * clean up the client. */
1856 if (--client->nstreams == 0) {
1857 bin->clients = g_slist_delete_link (bin->clients, clients);
1858 free_client (client, bin);
1859 break;
1860 }
1861 }
1862 }
1863 }
1864 g_free (stream);
1865 }
1866
1867 /* GObject vmethods */
1868 static void gst_rtp_bin_dispose (GObject * object);
1869 static void gst_rtp_bin_finalize (GObject * object);
1870 static void gst_rtp_bin_set_property (GObject * object, guint prop_id,
1871 const GValue * value, GParamSpec * pspec);
1872 static void gst_rtp_bin_get_property (GObject * object, guint prop_id,
1873 GValue * value, GParamSpec * pspec);
1874
1875 /* GstElement vmethods */
1876 static GstStateChangeReturn gst_rtp_bin_change_state (GstElement * element,
1877 GstStateChange transition);
1878 static GstPad *gst_rtp_bin_request_new_pad (GstElement * element,
1879 GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
1880 static void gst_rtp_bin_release_pad (GstElement * element, GstPad * pad);
1881 static void gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message);
1882
1883 #define gst_rtp_bin_parent_class parent_class
1884 G_DEFINE_TYPE_WITH_PRIVATE (GstRtpBin, gst_rtp_bin, GST_TYPE_BIN);
1885
1886 static gboolean
_gst_element_accumulator(GSignalInvocationHint * ihint,GValue * return_accu,const GValue * handler_return,gpointer dummy)1887 _gst_element_accumulator (GSignalInvocationHint * ihint,
1888 GValue * return_accu, const GValue * handler_return, gpointer dummy)
1889 {
1890 GstElement *element;
1891
1892 element = g_value_get_object (handler_return);
1893 GST_DEBUG ("got element %" GST_PTR_FORMAT, element);
1894
1895 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
1896 g_value_set_object (return_accu, element);
1897
1898 /* stop emission if we have an element */
1899 return (element == NULL);
1900 }
1901
1902 static gboolean
_gst_caps_accumulator(GSignalInvocationHint * ihint,GValue * return_accu,const GValue * handler_return,gpointer dummy)1903 _gst_caps_accumulator (GSignalInvocationHint * ihint,
1904 GValue * return_accu, const GValue * handler_return, gpointer dummy)
1905 {
1906 GstCaps *caps;
1907
1908 caps = g_value_get_boxed (handler_return);
1909 GST_DEBUG ("got caps %" GST_PTR_FORMAT, caps);
1910
1911 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
1912 g_value_set_boxed (return_accu, caps);
1913
1914 /* stop emission if we have a caps */
1915 return (caps == NULL);
1916 }
1917
1918 static void
gst_rtp_bin_class_init(GstRtpBinClass * klass)1919 gst_rtp_bin_class_init (GstRtpBinClass * klass)
1920 {
1921 GObjectClass *gobject_class;
1922 GstElementClass *gstelement_class;
1923 GstBinClass *gstbin_class;
1924
1925 gobject_class = (GObjectClass *) klass;
1926 gstelement_class = (GstElementClass *) klass;
1927 gstbin_class = (GstBinClass *) klass;
1928
1929 gobject_class->dispose = gst_rtp_bin_dispose;
1930 gobject_class->finalize = gst_rtp_bin_finalize;
1931 gobject_class->set_property = gst_rtp_bin_set_property;
1932 gobject_class->get_property = gst_rtp_bin_get_property;
1933
1934 g_object_class_install_property (gobject_class, PROP_LATENCY,
1935 g_param_spec_uint ("latency", "Buffer latency in ms",
1936 "Default amount of ms to buffer in the jitterbuffers", 0,
1937 G_MAXUINT, DEFAULT_LATENCY_MS,
1938 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1939
1940 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
1941 g_param_spec_boolean ("drop-on-latency",
1942 "Drop buffers when maximum latency is reached",
1943 "Tells the jitterbuffer to never exceed the given latency in size",
1944 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1945
1946 /**
1947 * GstRtpBin::request-pt-map:
1948 * @rtpbin: the object which received the signal
1949 * @session: the session
1950 * @pt: the pt
1951 *
1952 * Request the payload type as #GstCaps for @pt in @session.
1953 */
1954 gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP] =
1955 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
1956 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, request_pt_map),
1957 _gst_caps_accumulator, NULL, g_cclosure_marshal_generic, GST_TYPE_CAPS,
1958 2, G_TYPE_UINT, G_TYPE_UINT);
1959
1960 /**
1961 * GstRtpBin::payload-type-change:
1962 * @rtpbin: the object which received the signal
1963 * @session: the session
1964 * @pt: the pt
1965 *
1966 * Signal that the current payload type changed to @pt in @session.
1967 */
1968 gst_rtp_bin_signals[SIGNAL_PAYLOAD_TYPE_CHANGE] =
1969 g_signal_new ("payload-type-change", G_TYPE_FROM_CLASS (klass),
1970 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, payload_type_change),
1971 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
1972 G_TYPE_UINT);
1973
1974 /**
1975 * GstRtpBin::clear-pt-map:
1976 * @rtpbin: the object which received the signal
1977 *
1978 * Clear all previously cached pt-mapping obtained with
1979 * #GstRtpBin::request-pt-map.
1980 */
1981 gst_rtp_bin_signals[SIGNAL_CLEAR_PT_MAP] =
1982 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
1983 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1984 clear_pt_map), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE,
1985 0, G_TYPE_NONE);
1986
1987 /**
1988 * GstRtpBin::reset-sync:
1989 * @rtpbin: the object which received the signal
1990 *
1991 * Reset all currently configured lip-sync parameters and require new SR
1992 * packets for all streams before lip-sync is attempted again.
1993 */
1994 gst_rtp_bin_signals[SIGNAL_RESET_SYNC] =
1995 g_signal_new ("reset-sync", G_TYPE_FROM_CLASS (klass),
1996 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1997 reset_sync), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE,
1998 0, G_TYPE_NONE);
1999
2000 /**
2001 * GstRtpBin::get-session:
2002 * @rtpbin: the object which received the signal
2003 * @id: the session id
2004 *
2005 * Request the related GstRtpSession as #GstElement related with session @id.
2006 *
2007 * Since: 1.8
2008 */
2009 gst_rtp_bin_signals[SIGNAL_GET_SESSION] =
2010 g_signal_new ("get-session", G_TYPE_FROM_CLASS (klass),
2011 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
2012 get_session), NULL, NULL, g_cclosure_marshal_generic,
2013 GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2014
2015 /**
2016 * GstRtpBin::get-internal-session:
2017 * @rtpbin: the object which received the signal
2018 * @id: the session id
2019 *
2020 * Request the internal RTPSession object as #GObject in session @id.
2021 */
2022 gst_rtp_bin_signals[SIGNAL_GET_INTERNAL_SESSION] =
2023 g_signal_new ("get-internal-session", G_TYPE_FROM_CLASS (klass),
2024 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
2025 get_internal_session), NULL, NULL, g_cclosure_marshal_generic,
2026 RTP_TYPE_SESSION, 1, G_TYPE_UINT);
2027
2028 /**
2029 * GstRtpBin::get-internal-storage:
2030 * @rtpbin: the object which received the signal
2031 * @id: the session id
2032 *
2033 * Request the internal RTPStorage object as #GObject in session @id.
2034 *
2035 * Since: 1.14
2036 */
2037 gst_rtp_bin_signals[SIGNAL_GET_INTERNAL_STORAGE] =
2038 g_signal_new ("get-internal-storage", G_TYPE_FROM_CLASS (klass),
2039 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
2040 get_internal_storage), NULL, NULL, g_cclosure_marshal_generic,
2041 G_TYPE_OBJECT, 1, G_TYPE_UINT);
2042
2043 /**
2044 * GstRtpBin::get-storage:
2045 * @rtpbin: the object which received the signal
2046 * @id: the session id
2047 *
2048 * Request the RTPStorage element as #GObject in session @id.
2049 *
2050 * Since: 1.16
2051 */
2052 gst_rtp_bin_signals[SIGNAL_GET_STORAGE] =
2053 g_signal_new ("get-storage", G_TYPE_FROM_CLASS (klass),
2054 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
2055 get_storage), NULL, NULL, g_cclosure_marshal_generic,
2056 GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2057
2058 /**
2059 * GstRtpBin::on-new-ssrc:
2060 * @rtpbin: the object which received the signal
2061 * @session: the session
2062 * @ssrc: the SSRC
2063 *
2064 * Notify of a new SSRC that entered @session.
2065 */
2066 gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC] =
2067 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
2068 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_new_ssrc),
2069 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
2070 G_TYPE_UINT);
2071 /**
2072 * GstRtpBin::on-ssrc-collision:
2073 * @rtpbin: the object which received the signal
2074 * @session: the session
2075 * @ssrc: the SSRC
2076 *
2077 * Notify when we have an SSRC collision
2078 */
2079 gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION] =
2080 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
2081 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_collision),
2082 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
2083 G_TYPE_UINT);
2084 /**
2085 * GstRtpBin::on-ssrc-validated:
2086 * @rtpbin: the object which received the signal
2087 * @session: the session
2088 * @ssrc: the SSRC
2089 *
2090 * Notify of a new SSRC that became validated.
2091 */
2092 gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED] =
2093 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
2094 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_validated),
2095 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
2096 G_TYPE_UINT);
2097 /**
2098 * GstRtpBin::on-ssrc-active:
2099 * @rtpbin: the object which received the signal
2100 * @session: the session
2101 * @ssrc: the SSRC
2102 *
2103 * Notify of a SSRC that is active, i.e., sending RTCP.
2104 */
2105 gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE] =
2106 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
2107 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_active),
2108 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
2109 G_TYPE_UINT);
2110 /**
2111 * GstRtpBin::on-ssrc-sdes:
2112 * @rtpbin: the object which received the signal
2113 * @session: the session
2114 * @ssrc: the SSRC
2115 *
2116 * Notify of a SSRC that is active, i.e., sending RTCP.
2117 */
2118 gst_rtp_bin_signals[SIGNAL_ON_SSRC_SDES] =
2119 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
2120 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_sdes),
2121 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
2122 G_TYPE_UINT);
2123
2124 /**
2125 * GstRtpBin::on-bye-ssrc:
2126 * @rtpbin: the object which received the signal
2127 * @session: the session
2128 * @ssrc: the SSRC
2129 *
2130 * Notify of an SSRC that became inactive because of a BYE packet.
2131 */
2132 gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC] =
2133 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
2134 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_ssrc),
2135 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
2136 G_TYPE_UINT);
2137 /**
2138 * GstRtpBin::on-bye-timeout:
2139 * @rtpbin: the object which received the signal
2140 * @session: the session
2141 * @ssrc: the SSRC
2142 *
2143 * Notify of an SSRC that has timed out because of BYE
2144 */
2145 gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT] =
2146 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
2147 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_timeout),
2148 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
2149 G_TYPE_UINT);
2150 /**
2151 * GstRtpBin::on-timeout:
2152 * @rtpbin: the object which received the signal
2153 * @session: the session
2154 * @ssrc: the SSRC
2155 *
2156 * Notify of an SSRC that has timed out
2157 */
2158 gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT] =
2159 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
2160 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_timeout),
2161 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
2162 G_TYPE_UINT);
2163 /**
2164 * GstRtpBin::on-sender-timeout:
2165 * @rtpbin: the object which received the signal
2166 * @session: the session
2167 * @ssrc: the SSRC
2168 *
2169 * Notify of a sender SSRC that has timed out and became a receiver
2170 */
2171 gst_rtp_bin_signals[SIGNAL_ON_SENDER_TIMEOUT] =
2172 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
2173 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_sender_timeout),
2174 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
2175 G_TYPE_UINT);
2176
2177 /**
2178 * GstRtpBin::on-npt-stop:
2179 * @rtpbin: the object which received the signal
2180 * @session: the session
2181 * @ssrc: the SSRC
2182 *
2183 * Notify that SSRC sender has sent data up to the configured NPT stop time.
2184 */
2185 gst_rtp_bin_signals[SIGNAL_ON_NPT_STOP] =
2186 g_signal_new ("on-npt-stop", G_TYPE_FROM_CLASS (klass),
2187 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_npt_stop),
2188 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
2189 G_TYPE_UINT);
2190
2191 /**
2192 * GstRtpBin::request-rtp-encoder:
2193 * @rtpbin: the object which received the signal
2194 * @session: the session
2195 *
2196 * Request an RTP encoder element for the given @session. The encoder
2197 * element will be added to the bin if not previously added.
2198 *
2199 * If no handler is connected, no encoder will be used.
2200 *
2201 * Since: 1.4
2202 */
2203 gst_rtp_bin_signals[SIGNAL_REQUEST_RTP_ENCODER] =
2204 g_signal_new ("request-rtp-encoder", G_TYPE_FROM_CLASS (klass),
2205 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2206 request_rtp_encoder), _gst_element_accumulator, NULL,
2207 g_cclosure_marshal_generic, GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2208
2209 /**
2210 * GstRtpBin::request-rtp-decoder:
2211 * @rtpbin: the object which received the signal
2212 * @session: the session
2213 *
2214 * Request an RTP decoder element for the given @session. The decoder
2215 * element will be added to the bin if not previously added.
2216 *
2217 * If no handler is connected, no encoder will be used.
2218 *
2219 * Since: 1.4
2220 */
2221 gst_rtp_bin_signals[SIGNAL_REQUEST_RTP_DECODER] =
2222 g_signal_new ("request-rtp-decoder", G_TYPE_FROM_CLASS (klass),
2223 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2224 request_rtp_decoder), _gst_element_accumulator, NULL,
2225 g_cclosure_marshal_generic, GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2226
2227 /**
2228 * GstRtpBin::request-rtcp-encoder:
2229 * @rtpbin: the object which received the signal
2230 * @session: the session
2231 *
2232 * Request an RTCP encoder element for the given @session. The encoder
2233 * element will be added to the bin if not previously added.
2234 *
2235 * If no handler is connected, no encoder will be used.
2236 *
2237 * Since: 1.4
2238 */
2239 gst_rtp_bin_signals[SIGNAL_REQUEST_RTCP_ENCODER] =
2240 g_signal_new ("request-rtcp-encoder", G_TYPE_FROM_CLASS (klass),
2241 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2242 request_rtcp_encoder), _gst_element_accumulator, NULL,
2243 g_cclosure_marshal_generic, GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2244
2245 /**
2246 * GstRtpBin::request-rtcp-decoder:
2247 * @rtpbin: the object which received the signal
2248 * @session: the session
2249 *
2250 * Request an RTCP decoder element for the given @session. The decoder
2251 * element will be added to the bin if not previously added.
2252 *
2253 * If no handler is connected, no encoder will be used.
2254 *
2255 * Since: 1.4
2256 */
2257 gst_rtp_bin_signals[SIGNAL_REQUEST_RTCP_DECODER] =
2258 g_signal_new ("request-rtcp-decoder", G_TYPE_FROM_CLASS (klass),
2259 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2260 request_rtcp_decoder), _gst_element_accumulator, NULL,
2261 g_cclosure_marshal_generic, GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2262
2263 /**
2264 * GstRtpBin::new-jitterbuffer:
2265 * @rtpbin: the object which received the signal
2266 * @jitterbuffer: the new jitterbuffer
2267 * @session: the session
2268 * @ssrc: the SSRC
2269 *
2270 * Notify that a new @jitterbuffer was created for @session and @ssrc.
2271 * This signal can, for example, be used to configure @jitterbuffer.
2272 *
2273 * Since: 1.4
2274 */
2275 gst_rtp_bin_signals[SIGNAL_NEW_JITTERBUFFER] =
2276 g_signal_new ("new-jitterbuffer", G_TYPE_FROM_CLASS (klass),
2277 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2278 new_jitterbuffer), NULL, NULL, g_cclosure_marshal_generic,
2279 G_TYPE_NONE, 3, GST_TYPE_ELEMENT, G_TYPE_UINT, G_TYPE_UINT);
2280
2281 /**
2282 * GstRtpBin::new-storage:
2283 * @rtpbin: the object which received the signal
2284 * @storage: the new storage
2285 * @session: the session
2286 *
2287 * Notify that a new @storage was created for @session.
2288 * This signal can, for example, be used to configure @storage.
2289 *
2290 * Since: 1.14
2291 */
2292 gst_rtp_bin_signals[SIGNAL_NEW_STORAGE] =
2293 g_signal_new ("new-storage", G_TYPE_FROM_CLASS (klass),
2294 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2295 new_storage), NULL, NULL, g_cclosure_marshal_generic,
2296 G_TYPE_NONE, 2, GST_TYPE_ELEMENT, G_TYPE_UINT);
2297
2298 /**
2299 * GstRtpBin::request-aux-sender:
2300 * @rtpbin: the object which received the signal
2301 * @session: the session
2302 *
2303 * Request an AUX sender element for the given @session. The AUX
2304 * element will be added to the bin.
2305 *
2306 * If no handler is connected, no AUX element will be used.
2307 *
2308 * Since: 1.4
2309 */
2310 gst_rtp_bin_signals[SIGNAL_REQUEST_AUX_SENDER] =
2311 g_signal_new ("request-aux-sender", G_TYPE_FROM_CLASS (klass),
2312 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2313 request_aux_sender), _gst_element_accumulator, NULL,
2314 g_cclosure_marshal_generic, GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2315
2316 /**
2317 * GstRtpBin::request-aux-receiver:
2318 * @rtpbin: the object which received the signal
2319 * @session: the session
2320 *
2321 * Request an AUX receiver element for the given @session. The AUX
2322 * element will be added to the bin.
2323 *
2324 * If no handler is connected, no AUX element will be used.
2325 *
2326 * Since: 1.4
2327 */
2328 gst_rtp_bin_signals[SIGNAL_REQUEST_AUX_RECEIVER] =
2329 g_signal_new ("request-aux-receiver", G_TYPE_FROM_CLASS (klass),
2330 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2331 request_aux_receiver), _gst_element_accumulator, NULL,
2332 g_cclosure_marshal_generic, GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2333
2334 /**
2335 * GstRtpBin::request-fec-decoder:
2336 * @rtpbin: the object which received the signal
2337 * @session: the session index
2338 *
2339 * Request a FEC decoder element for the given @session. The element
2340 * will be added to the bin after the pt demuxer.
2341 *
2342 * If no handler is connected, no FEC decoder will be used.
2343 *
2344 * Since: 1.14
2345 */
2346 gst_rtp_bin_signals[SIGNAL_REQUEST_FEC_DECODER] =
2347 g_signal_new ("request-fec-decoder", G_TYPE_FROM_CLASS (klass),
2348 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2349 request_fec_decoder), _gst_element_accumulator, NULL,
2350 g_cclosure_marshal_generic, GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2351
2352 /**
2353 * GstRtpBin::request-fec-encoder:
2354 * @rtpbin: the object which received the signal
2355 * @session: the session index
2356 *
2357 * Request a FEC encoder element for the given @session. The element
2358 * will be added to the bin after the RTPSession.
2359 *
2360 * If no handler is connected, no FEC encoder will be used.
2361 *
2362 * Since: 1.14
2363 */
2364 gst_rtp_bin_signals[SIGNAL_REQUEST_FEC_ENCODER] =
2365 g_signal_new ("request-fec-encoder", G_TYPE_FROM_CLASS (klass),
2366 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2367 request_fec_encoder), _gst_element_accumulator, NULL,
2368 g_cclosure_marshal_generic, GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2369
2370 /**
2371 * GstRtpBin::on-new-sender-ssrc:
2372 * @rtpbin: the object which received the signal
2373 * @session: the session
2374 * @ssrc: the sender SSRC
2375 *
2376 * Notify of a new sender SSRC that entered @session.
2377 *
2378 * Since: 1.8
2379 */
2380 gst_rtp_bin_signals[SIGNAL_ON_NEW_SENDER_SSRC] =
2381 g_signal_new ("on-new-sender-ssrc", G_TYPE_FROM_CLASS (klass),
2382 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_new_sender_ssrc),
2383 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
2384 G_TYPE_UINT);
2385 /**
2386 * GstRtpBin::on-sender-ssrc-active:
2387 * @rtpbin: the object which received the signal
2388 * @session: the session
2389 * @ssrc: the sender SSRC
2390 *
2391 * Notify of a sender SSRC that is active, i.e., sending RTCP.
2392 *
2393 * Since: 1.8
2394 */
2395 gst_rtp_bin_signals[SIGNAL_ON_SENDER_SSRC_ACTIVE] =
2396 g_signal_new ("on-sender-ssrc-active", G_TYPE_FROM_CLASS (klass),
2397 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2398 on_sender_ssrc_active), NULL, NULL, g_cclosure_marshal_generic,
2399 G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT);
2400
2401 g_object_class_install_property (gobject_class, PROP_SDES,
2402 g_param_spec_boxed ("sdes", "SDES",
2403 "The SDES items of this session",
2404 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2405
2406 g_object_class_install_property (gobject_class, PROP_DO_LOST,
2407 g_param_spec_boolean ("do-lost", "Do Lost",
2408 "Send an event downstream when a packet is lost", DEFAULT_DO_LOST,
2409 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2410
2411 g_object_class_install_property (gobject_class, PROP_AUTOREMOVE,
2412 g_param_spec_boolean ("autoremove", "Auto Remove",
2413 "Automatically remove timed out sources", DEFAULT_AUTOREMOVE,
2414 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2415
2416 g_object_class_install_property (gobject_class, PROP_IGNORE_PT,
2417 g_param_spec_boolean ("ignore-pt", "Ignore PT",
2418 "Do not demultiplex based on PT values", DEFAULT_IGNORE_PT,
2419 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2420
2421 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
2422 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
2423 "Use the pipeline running-time to set the NTP time in the RTCP SR messages "
2424 "(DEPRECATED: Use ntp-time-source property)",
2425 DEFAULT_USE_PIPELINE_CLOCK,
2426 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
2427 /**
2428 * GstRtpBin:buffer-mode:
2429 *
2430 * Control the buffering and timestamping mode used by the jitterbuffer.
2431 */
2432 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
2433 g_param_spec_enum ("buffer-mode", "Buffer Mode",
2434 "Control the buffering algorithm in use", RTP_TYPE_JITTER_BUFFER_MODE,
2435 DEFAULT_BUFFER_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2436 /**
2437 * GstRtpBin:ntp-sync:
2438 *
2439 * Set the NTP time from the sender reports as the running-time on the
2440 * buffers. When both the sender and receiver have sychronized
2441 * running-time, i.e. when the clock and base-time is shared
2442 * between the receivers and the and the senders, this option can be
2443 * used to synchronize receivers on multiple machines.
2444 */
2445 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
2446 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
2447 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
2448 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2449
2450 /**
2451 * GstRtpBin:rtcp-sync:
2452 *
2453 * If not synchronizing (directly) to the NTP clock, determines how to sync
2454 * the various streams.
2455 */
2456 g_object_class_install_property (gobject_class, PROP_RTCP_SYNC,
2457 g_param_spec_enum ("rtcp-sync", "RTCP Sync",
2458 "Use of RTCP SR in synchronization", GST_RTP_BIN_RTCP_SYNC_TYPE,
2459 DEFAULT_RTCP_SYNC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2460
2461 /**
2462 * GstRtpBin:rtcp-sync-interval:
2463 *
2464 * Determines how often to sync streams using RTCP data.
2465 */
2466 g_object_class_install_property (gobject_class, PROP_RTCP_SYNC_INTERVAL,
2467 g_param_spec_uint ("rtcp-sync-interval", "RTCP Sync Interval",
2468 "RTCP SR interval synchronization (ms) (0 = always)",
2469 0, G_MAXUINT, DEFAULT_RTCP_SYNC_INTERVAL,
2470 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2471
2472 g_object_class_install_property (gobject_class, PROP_DO_SYNC_EVENT,
2473 g_param_spec_boolean ("do-sync-event", "Do Sync Event",
2474 "Send event downstream when a stream is synchronized to the sender",
2475 DEFAULT_DO_SYNC_EVENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2476
2477 /**
2478 * GstRtpBin:do-retransmission:
2479 *
2480 * Enables RTP retransmission on all streams. To control retransmission on
2481 * a per-SSRC basis, connect to the #GstRtpBin::new-jitterbuffer signal and
2482 * set the #GstRtpJitterBuffer::do-retransmission property on the
2483 * #GstRtpJitterBuffer object instead.
2484 */
2485 g_object_class_install_property (gobject_class, PROP_DO_RETRANSMISSION,
2486 g_param_spec_boolean ("do-retransmission", "Do retransmission",
2487 "Enable retransmission on all streams",
2488 DEFAULT_DO_RETRANSMISSION,
2489 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2490
2491 /**
2492 * GstRtpBin:rtp-profile:
2493 *
2494 * Sets the default RTP profile of newly created RTP sessions. The
2495 * profile can be changed afterwards on a per-session basis.
2496 */
2497 g_object_class_install_property (gobject_class, PROP_RTP_PROFILE,
2498 g_param_spec_enum ("rtp-profile", "RTP Profile",
2499 "Default RTP profile of newly created sessions",
2500 GST_TYPE_RTP_PROFILE, DEFAULT_RTP_PROFILE,
2501 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2502
2503 g_object_class_install_property (gobject_class, PROP_NTP_TIME_SOURCE,
2504 g_param_spec_enum ("ntp-time-source", "NTP Time Source",
2505 "NTP time source for RTCP packets",
2506 gst_rtp_ntp_time_source_get_type (), DEFAULT_NTP_TIME_SOURCE,
2507 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2508
2509 g_object_class_install_property (gobject_class, PROP_RTCP_SYNC_SEND_TIME,
2510 g_param_spec_boolean ("rtcp-sync-send-time", "RTCP Sync Send Time",
2511 "Use send time or capture time for RTCP sync "
2512 "(TRUE = send time, FALSE = capture time)",
2513 DEFAULT_RTCP_SYNC_SEND_TIME,
2514 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2515
2516 g_object_class_install_property (gobject_class, PROP_MAX_RTCP_RTP_TIME_DIFF,
2517 g_param_spec_int ("max-rtcp-rtp-time-diff", "Max RTCP RTP Time Diff",
2518 "Maximum amount of time in ms that the RTP time in RTCP SRs "
2519 "is allowed to be ahead (-1 disabled)", -1, G_MAXINT,
2520 DEFAULT_MAX_RTCP_RTP_TIME_DIFF,
2521 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2522
2523 g_object_class_install_property (gobject_class, PROP_MAX_DROPOUT_TIME,
2524 g_param_spec_uint ("max-dropout-time", "Max dropout time",
2525 "The maximum time (milliseconds) of missing packets tolerated.",
2526 0, G_MAXUINT, DEFAULT_MAX_DROPOUT_TIME,
2527 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2528
2529 g_object_class_install_property (gobject_class, PROP_MAX_MISORDER_TIME,
2530 g_param_spec_uint ("max-misorder-time", "Max misorder time",
2531 "The maximum time (milliseconds) of misordered packets tolerated.",
2532 0, G_MAXUINT, DEFAULT_MAX_MISORDER_TIME,
2533 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2534
2535 g_object_class_install_property (gobject_class, PROP_RFC7273_SYNC,
2536 g_param_spec_boolean ("rfc7273-sync", "Sync on RFC7273 clock",
2537 "Synchronize received streams to the RFC7273 clock "
2538 "(requires clock and offset to be provided)", DEFAULT_RFC7273_SYNC,
2539 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2540
2541 g_object_class_install_property (gobject_class, PROP_MAX_STREAMS,
2542 g_param_spec_uint ("max-streams", "Max Streams",
2543 "The maximum number of streams to create for one session",
2544 0, G_MAXUINT, DEFAULT_MAX_STREAMS,
2545 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2546
2547 /**
2548 * GstRtpBin:max-ts-offset-adjustment:
2549 *
2550 * Syncing time stamps to NTP time adds a time offset. This parameter
2551 * specifies the maximum number of nanoseconds per frame that this time offset
2552 * may be adjusted with. This is used to avoid sudden large changes to time
2553 * stamps.
2554 *
2555 * Since: 1.14
2556 */
2557 g_object_class_install_property (gobject_class, PROP_MAX_TS_OFFSET_ADJUSTMENT,
2558 g_param_spec_uint64 ("max-ts-offset-adjustment",
2559 "Max Timestamp Offset Adjustment",
2560 "The maximum number of nanoseconds per frame that time stamp offsets "
2561 "may be adjusted (0 = no limit).", 0, G_MAXUINT64,
2562 DEFAULT_MAX_TS_OFFSET_ADJUSTMENT, G_PARAM_READWRITE |
2563 G_PARAM_STATIC_STRINGS));
2564
2565 /**
2566 * GstRtpBin:max-ts-offset:
2567 *
2568 * Used to set an upper limit of how large a time offset may be. This
2569 * is used to protect against unrealistic values as a result of either
2570 * client,server or clock issues.
2571 *
2572 * Since: 1.14
2573 */
2574 g_object_class_install_property (gobject_class, PROP_MAX_TS_OFFSET,
2575 g_param_spec_int64 ("max-ts-offset", "Max TS Offset",
2576 "The maximum absolute value of the time offset in (nanoseconds). "
2577 "Note, if the ntp-sync parameter is set the default value is "
2578 "changed to 0 (no limit)", 0, G_MAXINT64, DEFAULT_MAX_TS_OFFSET,
2579 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2580
2581 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_bin_change_state);
2582 gstelement_class->request_new_pad =
2583 GST_DEBUG_FUNCPTR (gst_rtp_bin_request_new_pad);
2584 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_bin_release_pad);
2585
2586 /* sink pads */
2587 gst_element_class_add_static_pad_template (gstelement_class,
2588 &rtpbin_recv_rtp_sink_template);
2589 gst_element_class_add_static_pad_template (gstelement_class,
2590 &rtpbin_recv_rtcp_sink_template);
2591 gst_element_class_add_static_pad_template (gstelement_class,
2592 &rtpbin_send_rtp_sink_template);
2593
2594 /* src pads */
2595 gst_element_class_add_static_pad_template (gstelement_class,
2596 &rtpbin_recv_rtp_src_template);
2597 gst_element_class_add_static_pad_template (gstelement_class,
2598 &rtpbin_send_rtcp_src_template);
2599 gst_element_class_add_static_pad_template (gstelement_class,
2600 &rtpbin_send_rtp_src_template);
2601
2602 gst_element_class_set_static_metadata (gstelement_class, "RTP Bin",
2603 "Filter/Network/RTP",
2604 "Real-Time Transport Protocol bin",
2605 "Wim Taymans <wim.taymans@gmail.com>");
2606
2607 gstbin_class->handle_message = GST_DEBUG_FUNCPTR (gst_rtp_bin_handle_message);
2608
2609 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_bin_clear_pt_map);
2610 klass->reset_sync = GST_DEBUG_FUNCPTR (gst_rtp_bin_reset_sync);
2611 klass->get_session = GST_DEBUG_FUNCPTR (gst_rtp_bin_get_session);
2612 klass->get_internal_session =
2613 GST_DEBUG_FUNCPTR (gst_rtp_bin_get_internal_session);
2614 klass->get_storage = GST_DEBUG_FUNCPTR (gst_rtp_bin_get_storage);
2615 klass->get_internal_storage =
2616 GST_DEBUG_FUNCPTR (gst_rtp_bin_get_internal_storage);
2617 klass->request_rtp_encoder = GST_DEBUG_FUNCPTR (gst_rtp_bin_request_encoder);
2618 klass->request_rtp_decoder = GST_DEBUG_FUNCPTR (gst_rtp_bin_request_decoder);
2619 klass->request_rtcp_encoder = GST_DEBUG_FUNCPTR (gst_rtp_bin_request_encoder);
2620 klass->request_rtcp_decoder = GST_DEBUG_FUNCPTR (gst_rtp_bin_request_decoder);
2621
2622 GST_DEBUG_CATEGORY_INIT (gst_rtp_bin_debug, "rtpbin", 0, "RTP bin");
2623 }
2624
2625 static void
gst_rtp_bin_init(GstRtpBin * rtpbin)2626 gst_rtp_bin_init (GstRtpBin * rtpbin)
2627 {
2628 gchar *cname;
2629
2630 rtpbin->priv = gst_rtp_bin_get_instance_private (rtpbin);
2631 g_mutex_init (&rtpbin->priv->bin_lock);
2632 g_mutex_init (&rtpbin->priv->dyn_lock);
2633
2634 rtpbin->latency_ms = DEFAULT_LATENCY_MS;
2635 rtpbin->latency_ns = DEFAULT_LATENCY_MS * GST_MSECOND;
2636 rtpbin->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
2637 rtpbin->do_lost = DEFAULT_DO_LOST;
2638 rtpbin->ignore_pt = DEFAULT_IGNORE_PT;
2639 rtpbin->ntp_sync = DEFAULT_NTP_SYNC;
2640 rtpbin->rtcp_sync = DEFAULT_RTCP_SYNC;
2641 rtpbin->rtcp_sync_interval = DEFAULT_RTCP_SYNC_INTERVAL;
2642 rtpbin->priv->autoremove = DEFAULT_AUTOREMOVE;
2643 rtpbin->buffer_mode = DEFAULT_BUFFER_MODE;
2644 rtpbin->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
2645 rtpbin->send_sync_event = DEFAULT_DO_SYNC_EVENT;
2646 rtpbin->do_retransmission = DEFAULT_DO_RETRANSMISSION;
2647 rtpbin->rtp_profile = DEFAULT_RTP_PROFILE;
2648 rtpbin->ntp_time_source = DEFAULT_NTP_TIME_SOURCE;
2649 rtpbin->rtcp_sync_send_time = DEFAULT_RTCP_SYNC_SEND_TIME;
2650 rtpbin->max_rtcp_rtp_time_diff = DEFAULT_MAX_RTCP_RTP_TIME_DIFF;
2651 rtpbin->max_dropout_time = DEFAULT_MAX_DROPOUT_TIME;
2652 rtpbin->max_misorder_time = DEFAULT_MAX_MISORDER_TIME;
2653 rtpbin->rfc7273_sync = DEFAULT_RFC7273_SYNC;
2654 rtpbin->max_streams = DEFAULT_MAX_STREAMS;
2655 rtpbin->max_ts_offset_adjustment = DEFAULT_MAX_TS_OFFSET_ADJUSTMENT;
2656 rtpbin->max_ts_offset = DEFAULT_MAX_TS_OFFSET;
2657 rtpbin->max_ts_offset_is_set = FALSE;
2658
2659 /* some default SDES entries */
2660 cname = g_strdup_printf ("user%u@host-%x", g_random_int (), g_random_int ());
2661 rtpbin->sdes = gst_structure_new ("application/x-rtp-source-sdes",
2662 "cname", G_TYPE_STRING, cname, "tool", G_TYPE_STRING, "GStreamer", NULL);
2663 g_free (cname);
2664 }
2665
2666 static void
gst_rtp_bin_dispose(GObject * object)2667 gst_rtp_bin_dispose (GObject * object)
2668 {
2669 GstRtpBin *rtpbin;
2670
2671 rtpbin = GST_RTP_BIN (object);
2672
2673 GST_RTP_BIN_LOCK (rtpbin);
2674 GST_DEBUG_OBJECT (object, "freeing sessions");
2675 g_slist_foreach (rtpbin->sessions, (GFunc) free_session, rtpbin);
2676 g_slist_free (rtpbin->sessions);
2677 rtpbin->sessions = NULL;
2678 GST_RTP_BIN_UNLOCK (rtpbin);
2679
2680 G_OBJECT_CLASS (parent_class)->dispose (object);
2681 }
2682
2683 static void
gst_rtp_bin_finalize(GObject * object)2684 gst_rtp_bin_finalize (GObject * object)
2685 {
2686 GstRtpBin *rtpbin;
2687
2688 rtpbin = GST_RTP_BIN (object);
2689
2690 if (rtpbin->sdes)
2691 gst_structure_free (rtpbin->sdes);
2692
2693 g_mutex_clear (&rtpbin->priv->bin_lock);
2694 g_mutex_clear (&rtpbin->priv->dyn_lock);
2695
2696 G_OBJECT_CLASS (parent_class)->finalize (object);
2697 }
2698
2699
2700 static void
gst_rtp_bin_set_sdes_struct(GstRtpBin * bin,const GstStructure * sdes)2701 gst_rtp_bin_set_sdes_struct (GstRtpBin * bin, const GstStructure * sdes)
2702 {
2703 GSList *item;
2704
2705 if (sdes == NULL)
2706 return;
2707
2708 GST_RTP_BIN_LOCK (bin);
2709
2710 GST_OBJECT_LOCK (bin);
2711 if (bin->sdes)
2712 gst_structure_free (bin->sdes);
2713 bin->sdes = gst_structure_copy (sdes);
2714 GST_OBJECT_UNLOCK (bin);
2715
2716 /* store in all sessions */
2717 for (item = bin->sessions; item; item = g_slist_next (item)) {
2718 GstRtpBinSession *session = item->data;
2719 g_object_set (session->session, "sdes", sdes, NULL);
2720 }
2721
2722 GST_RTP_BIN_UNLOCK (bin);
2723 }
2724
2725 static GstStructure *
gst_rtp_bin_get_sdes_struct(GstRtpBin * bin)2726 gst_rtp_bin_get_sdes_struct (GstRtpBin * bin)
2727 {
2728 GstStructure *result;
2729
2730 GST_OBJECT_LOCK (bin);
2731 result = gst_structure_copy (bin->sdes);
2732 GST_OBJECT_UNLOCK (bin);
2733
2734 return result;
2735 }
2736
2737 static void
gst_rtp_bin_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)2738 gst_rtp_bin_set_property (GObject * object, guint prop_id,
2739 const GValue * value, GParamSpec * pspec)
2740 {
2741 GstRtpBin *rtpbin;
2742
2743 rtpbin = GST_RTP_BIN (object);
2744
2745 switch (prop_id) {
2746 case PROP_LATENCY:
2747 GST_RTP_BIN_LOCK (rtpbin);
2748 rtpbin->latency_ms = g_value_get_uint (value);
2749 rtpbin->latency_ns = rtpbin->latency_ms * GST_MSECOND;
2750 GST_RTP_BIN_UNLOCK (rtpbin);
2751 /* propagate the property down to the jitterbuffer */
2752 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "latency", value);
2753 break;
2754 case PROP_DROP_ON_LATENCY:
2755 GST_RTP_BIN_LOCK (rtpbin);
2756 rtpbin->drop_on_latency = g_value_get_boolean (value);
2757 GST_RTP_BIN_UNLOCK (rtpbin);
2758 /* propagate the property down to the jitterbuffer */
2759 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin,
2760 "drop-on-latency", value);
2761 break;
2762 case PROP_SDES:
2763 gst_rtp_bin_set_sdes_struct (rtpbin, g_value_get_boxed (value));
2764 break;
2765 case PROP_DO_LOST:
2766 GST_RTP_BIN_LOCK (rtpbin);
2767 rtpbin->do_lost = g_value_get_boolean (value);
2768 GST_RTP_BIN_UNLOCK (rtpbin);
2769 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "do-lost", value);
2770 break;
2771 case PROP_NTP_SYNC:
2772 rtpbin->ntp_sync = g_value_get_boolean (value);
2773 /* The default value of max_ts_offset depends on ntp_sync. If user
2774 * hasn't set it then change default value */
2775 if (!rtpbin->max_ts_offset_is_set) {
2776 if (rtpbin->ntp_sync) {
2777 rtpbin->max_ts_offset = 0;
2778 } else {
2779 rtpbin->max_ts_offset = DEFAULT_MAX_TS_OFFSET;
2780 }
2781 }
2782 break;
2783 case PROP_RTCP_SYNC:
2784 g_atomic_int_set (&rtpbin->rtcp_sync, g_value_get_enum (value));
2785 break;
2786 case PROP_RTCP_SYNC_INTERVAL:
2787 rtpbin->rtcp_sync_interval = g_value_get_uint (value);
2788 break;
2789 case PROP_IGNORE_PT:
2790 rtpbin->ignore_pt = g_value_get_boolean (value);
2791 break;
2792 case PROP_AUTOREMOVE:
2793 rtpbin->priv->autoremove = g_value_get_boolean (value);
2794 break;
2795 case PROP_USE_PIPELINE_CLOCK:
2796 {
2797 GSList *sessions;
2798 GST_RTP_BIN_LOCK (rtpbin);
2799 rtpbin->use_pipeline_clock = g_value_get_boolean (value);
2800 for (sessions = rtpbin->sessions; sessions;
2801 sessions = g_slist_next (sessions)) {
2802 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
2803
2804 g_object_set (G_OBJECT (session->session),
2805 "use-pipeline-clock", rtpbin->use_pipeline_clock, NULL);
2806 }
2807 GST_RTP_BIN_UNLOCK (rtpbin);
2808 }
2809 break;
2810 case PROP_DO_SYNC_EVENT:
2811 rtpbin->send_sync_event = g_value_get_boolean (value);
2812 break;
2813 case PROP_BUFFER_MODE:
2814 GST_RTP_BIN_LOCK (rtpbin);
2815 rtpbin->buffer_mode = g_value_get_enum (value);
2816 GST_RTP_BIN_UNLOCK (rtpbin);
2817 /* propagate the property down to the jitterbuffer */
2818 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "mode", value);
2819 break;
2820 case PROP_DO_RETRANSMISSION:
2821 GST_RTP_BIN_LOCK (rtpbin);
2822 rtpbin->do_retransmission = g_value_get_boolean (value);
2823 GST_RTP_BIN_UNLOCK (rtpbin);
2824 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin,
2825 "do-retransmission", value);
2826 break;
2827 case PROP_RTP_PROFILE:
2828 rtpbin->rtp_profile = g_value_get_enum (value);
2829 break;
2830 case PROP_NTP_TIME_SOURCE:{
2831 GSList *sessions;
2832 GST_RTP_BIN_LOCK (rtpbin);
2833 rtpbin->ntp_time_source = g_value_get_enum (value);
2834 for (sessions = rtpbin->sessions; sessions;
2835 sessions = g_slist_next (sessions)) {
2836 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
2837
2838 g_object_set (G_OBJECT (session->session),
2839 "ntp-time-source", rtpbin->ntp_time_source, NULL);
2840 }
2841 GST_RTP_BIN_UNLOCK (rtpbin);
2842 break;
2843 }
2844 case PROP_RTCP_SYNC_SEND_TIME:{
2845 GSList *sessions;
2846 GST_RTP_BIN_LOCK (rtpbin);
2847 rtpbin->rtcp_sync_send_time = g_value_get_boolean (value);
2848 for (sessions = rtpbin->sessions; sessions;
2849 sessions = g_slist_next (sessions)) {
2850 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
2851
2852 g_object_set (G_OBJECT (session->session),
2853 "rtcp-sync-send-time", rtpbin->rtcp_sync_send_time, NULL);
2854 }
2855 GST_RTP_BIN_UNLOCK (rtpbin);
2856 break;
2857 }
2858 case PROP_MAX_RTCP_RTP_TIME_DIFF:
2859 GST_RTP_BIN_LOCK (rtpbin);
2860 rtpbin->max_rtcp_rtp_time_diff = g_value_get_int (value);
2861 GST_RTP_BIN_UNLOCK (rtpbin);
2862 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin,
2863 "max-rtcp-rtp-time-diff", value);
2864 break;
2865 case PROP_MAX_DROPOUT_TIME:
2866 GST_RTP_BIN_LOCK (rtpbin);
2867 rtpbin->max_dropout_time = g_value_get_uint (value);
2868 GST_RTP_BIN_UNLOCK (rtpbin);
2869 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin,
2870 "max-dropout-time", value);
2871 gst_rtp_bin_propagate_property_to_session (rtpbin, "max-dropout-time",
2872 value);
2873 break;
2874 case PROP_MAX_MISORDER_TIME:
2875 GST_RTP_BIN_LOCK (rtpbin);
2876 rtpbin->max_misorder_time = g_value_get_uint (value);
2877 GST_RTP_BIN_UNLOCK (rtpbin);
2878 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin,
2879 "max-misorder-time", value);
2880 gst_rtp_bin_propagate_property_to_session (rtpbin, "max-misorder-time",
2881 value);
2882 break;
2883 case PROP_RFC7273_SYNC:
2884 rtpbin->rfc7273_sync = g_value_get_boolean (value);
2885 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin,
2886 "rfc7273-sync", value);
2887 break;
2888 case PROP_MAX_STREAMS:
2889 rtpbin->max_streams = g_value_get_uint (value);
2890 break;
2891 case PROP_MAX_TS_OFFSET_ADJUSTMENT:
2892 rtpbin->max_ts_offset_adjustment = g_value_get_uint64 (value);
2893 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin,
2894 "max-ts-offset-adjustment", value);
2895 break;
2896 case PROP_MAX_TS_OFFSET:
2897 rtpbin->max_ts_offset = g_value_get_int64 (value);
2898 rtpbin->max_ts_offset_is_set = TRUE;
2899 break;
2900 default:
2901 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2902 break;
2903 }
2904 }
2905
2906 static void
gst_rtp_bin_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)2907 gst_rtp_bin_get_property (GObject * object, guint prop_id,
2908 GValue * value, GParamSpec * pspec)
2909 {
2910 GstRtpBin *rtpbin;
2911
2912 rtpbin = GST_RTP_BIN (object);
2913
2914 switch (prop_id) {
2915 case PROP_LATENCY:
2916 GST_RTP_BIN_LOCK (rtpbin);
2917 g_value_set_uint (value, rtpbin->latency_ms);
2918 GST_RTP_BIN_UNLOCK (rtpbin);
2919 break;
2920 case PROP_DROP_ON_LATENCY:
2921 GST_RTP_BIN_LOCK (rtpbin);
2922 g_value_set_boolean (value, rtpbin->drop_on_latency);
2923 GST_RTP_BIN_UNLOCK (rtpbin);
2924 break;
2925 case PROP_SDES:
2926 g_value_take_boxed (value, gst_rtp_bin_get_sdes_struct (rtpbin));
2927 break;
2928 case PROP_DO_LOST:
2929 GST_RTP_BIN_LOCK (rtpbin);
2930 g_value_set_boolean (value, rtpbin->do_lost);
2931 GST_RTP_BIN_UNLOCK (rtpbin);
2932 break;
2933 case PROP_IGNORE_PT:
2934 g_value_set_boolean (value, rtpbin->ignore_pt);
2935 break;
2936 case PROP_NTP_SYNC:
2937 g_value_set_boolean (value, rtpbin->ntp_sync);
2938 break;
2939 case PROP_RTCP_SYNC:
2940 g_value_set_enum (value, g_atomic_int_get (&rtpbin->rtcp_sync));
2941 break;
2942 case PROP_RTCP_SYNC_INTERVAL:
2943 g_value_set_uint (value, rtpbin->rtcp_sync_interval);
2944 break;
2945 case PROP_AUTOREMOVE:
2946 g_value_set_boolean (value, rtpbin->priv->autoremove);
2947 break;
2948 case PROP_BUFFER_MODE:
2949 g_value_set_enum (value, rtpbin->buffer_mode);
2950 break;
2951 case PROP_USE_PIPELINE_CLOCK:
2952 g_value_set_boolean (value, rtpbin->use_pipeline_clock);
2953 break;
2954 case PROP_DO_SYNC_EVENT:
2955 g_value_set_boolean (value, rtpbin->send_sync_event);
2956 break;
2957 case PROP_DO_RETRANSMISSION:
2958 GST_RTP_BIN_LOCK (rtpbin);
2959 g_value_set_boolean (value, rtpbin->do_retransmission);
2960 GST_RTP_BIN_UNLOCK (rtpbin);
2961 break;
2962 case PROP_RTP_PROFILE:
2963 g_value_set_enum (value, rtpbin->rtp_profile);
2964 break;
2965 case PROP_NTP_TIME_SOURCE:
2966 g_value_set_enum (value, rtpbin->ntp_time_source);
2967 break;
2968 case PROP_RTCP_SYNC_SEND_TIME:
2969 g_value_set_boolean (value, rtpbin->rtcp_sync_send_time);
2970 break;
2971 case PROP_MAX_RTCP_RTP_TIME_DIFF:
2972 GST_RTP_BIN_LOCK (rtpbin);
2973 g_value_set_int (value, rtpbin->max_rtcp_rtp_time_diff);
2974 GST_RTP_BIN_UNLOCK (rtpbin);
2975 break;
2976 case PROP_MAX_DROPOUT_TIME:
2977 g_value_set_uint (value, rtpbin->max_dropout_time);
2978 break;
2979 case PROP_MAX_MISORDER_TIME:
2980 g_value_set_uint (value, rtpbin->max_misorder_time);
2981 break;
2982 case PROP_RFC7273_SYNC:
2983 g_value_set_boolean (value, rtpbin->rfc7273_sync);
2984 break;
2985 case PROP_MAX_STREAMS:
2986 g_value_set_uint (value, rtpbin->max_streams);
2987 break;
2988 case PROP_MAX_TS_OFFSET_ADJUSTMENT:
2989 g_value_set_uint64 (value, rtpbin->max_ts_offset_adjustment);
2990 break;
2991 case PROP_MAX_TS_OFFSET:
2992 g_value_set_int64 (value, rtpbin->max_ts_offset);
2993 break;
2994 default:
2995 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2996 break;
2997 }
2998 }
2999
3000 static void
gst_rtp_bin_handle_message(GstBin * bin,GstMessage * message)3001 gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message)
3002 {
3003 GstRtpBin *rtpbin;
3004
3005 rtpbin = GST_RTP_BIN (bin);
3006
3007 switch (GST_MESSAGE_TYPE (message)) {
3008 case GST_MESSAGE_ELEMENT:
3009 {
3010 const GstStructure *s = gst_message_get_structure (message);
3011
3012 /* we change the structure name and add the session ID to it */
3013 if (gst_structure_has_name (s, "application/x-rtp-source-sdes")) {
3014 GstRtpBinSession *sess;
3015
3016 /* find the session we set it as object data */
3017 sess = g_object_get_data (G_OBJECT (GST_MESSAGE_SRC (message)),
3018 "GstRTPBin.session");
3019
3020 if (G_LIKELY (sess)) {
3021 message = gst_message_make_writable (message);
3022 s = gst_message_get_structure (message);
3023 gst_structure_set ((GstStructure *) s, "session", G_TYPE_UINT,
3024 sess->id, NULL);
3025 }
3026 }
3027 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
3028 break;
3029 }
3030 case GST_MESSAGE_BUFFERING:
3031 {
3032 gint percent;
3033 gint min_percent = 100;
3034 GSList *sessions, *streams;
3035 GstRtpBinStream *stream;
3036 gboolean change = FALSE, active = FALSE;
3037 GstClockTime min_out_time;
3038 GstBufferingMode mode;
3039 gint avg_in, avg_out;
3040 gint64 buffering_left;
3041
3042 gst_message_parse_buffering (message, &percent);
3043 gst_message_parse_buffering_stats (message, &mode, &avg_in, &avg_out,
3044 &buffering_left);
3045
3046 stream =
3047 g_object_get_data (G_OBJECT (GST_MESSAGE_SRC (message)),
3048 "GstRTPBin.stream");
3049
3050 GST_DEBUG_OBJECT (bin, "got percent %d from stream %p", percent, stream);
3051
3052 /* get the stream */
3053 if (G_LIKELY (stream)) {
3054 GST_RTP_BIN_LOCK (rtpbin);
3055 /* fill in the percent */
3056 stream->percent = percent;
3057
3058 /* calculate the min value for all streams */
3059 for (sessions = rtpbin->sessions; sessions;
3060 sessions = g_slist_next (sessions)) {
3061 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
3062
3063 GST_RTP_SESSION_LOCK (session);
3064 if (session->streams) {
3065 for (streams = session->streams; streams;
3066 streams = g_slist_next (streams)) {
3067 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
3068
3069 GST_DEBUG_OBJECT (bin, "stream %p percent %d", stream,
3070 stream->percent);
3071
3072 /* find min percent */
3073 if (min_percent > stream->percent)
3074 min_percent = stream->percent;
3075 }
3076 } else {
3077 GST_INFO_OBJECT (bin,
3078 "session has no streams, setting min_percent to 0");
3079 min_percent = 0;
3080 }
3081 GST_RTP_SESSION_UNLOCK (session);
3082 }
3083 GST_DEBUG_OBJECT (bin, "min percent %d", min_percent);
3084
3085 if (rtpbin->buffering) {
3086 if (min_percent == 100) {
3087 rtpbin->buffering = FALSE;
3088 active = TRUE;
3089 change = TRUE;
3090 }
3091 } else {
3092 if (min_percent < 100) {
3093 /* pause the streams */
3094 rtpbin->buffering = TRUE;
3095 active = FALSE;
3096 change = TRUE;
3097 }
3098 }
3099 GST_RTP_BIN_UNLOCK (rtpbin);
3100
3101 gst_message_unref (message);
3102
3103 /* make a new buffering message with the min value */
3104 message =
3105 gst_message_new_buffering (GST_OBJECT_CAST (bin), min_percent);
3106 gst_message_set_buffering_stats (message, mode, avg_in, avg_out,
3107 buffering_left);
3108
3109 if (G_UNLIKELY (change)) {
3110 GstClock *clock;
3111 guint64 running_time = 0;
3112 guint64 offset = 0;
3113
3114 /* figure out the running time when we have a clock */
3115 if (G_LIKELY ((clock =
3116 gst_element_get_clock (GST_ELEMENT_CAST (bin))))) {
3117 guint64 now, base_time;
3118
3119 now = gst_clock_get_time (clock);
3120 base_time = gst_element_get_base_time (GST_ELEMENT_CAST (bin));
3121 running_time = now - base_time;
3122 gst_object_unref (clock);
3123 }
3124 GST_DEBUG_OBJECT (bin,
3125 "running time now %" GST_TIME_FORMAT,
3126 GST_TIME_ARGS (running_time));
3127
3128 GST_RTP_BIN_LOCK (rtpbin);
3129
3130 /* when we reactivate, calculate the offsets so that all streams have
3131 * an output time that is at least as big as the running_time */
3132 offset = 0;
3133 if (active) {
3134 if (running_time > rtpbin->buffer_start) {
3135 offset = running_time - rtpbin->buffer_start;
3136 if (offset >= rtpbin->latency_ns)
3137 offset -= rtpbin->latency_ns;
3138 else
3139 offset = 0;
3140 }
3141 }
3142
3143 /* pause all streams */
3144 min_out_time = -1;
3145 for (sessions = rtpbin->sessions; sessions;
3146 sessions = g_slist_next (sessions)) {
3147 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
3148
3149 GST_RTP_SESSION_LOCK (session);
3150 for (streams = session->streams; streams;
3151 streams = g_slist_next (streams)) {
3152 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
3153 GstElement *element = stream->buffer;
3154 guint64 last_out;
3155
3156 g_signal_emit_by_name (element, "set-active", active, offset,
3157 &last_out);
3158
3159 if (!active) {
3160 g_object_get (element, "percent", &stream->percent, NULL);
3161
3162 if (last_out == -1)
3163 last_out = 0;
3164 if (min_out_time == -1 || last_out < min_out_time)
3165 min_out_time = last_out;
3166 }
3167
3168 GST_DEBUG_OBJECT (bin,
3169 "setting %p to %d, offset %" GST_TIME_FORMAT ", last %"
3170 GST_TIME_FORMAT ", percent %d", element, active,
3171 GST_TIME_ARGS (offset), GST_TIME_ARGS (last_out),
3172 stream->percent);
3173 }
3174 GST_RTP_SESSION_UNLOCK (session);
3175 }
3176 GST_DEBUG_OBJECT (bin,
3177 "min out time %" GST_TIME_FORMAT, GST_TIME_ARGS (min_out_time));
3178
3179 /* the buffer_start is the min out time of all paused jitterbuffers */
3180 if (!active)
3181 rtpbin->buffer_start = min_out_time;
3182
3183 GST_RTP_BIN_UNLOCK (rtpbin);
3184 }
3185 }
3186 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
3187 break;
3188 }
3189 default:
3190 {
3191 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
3192 break;
3193 }
3194 }
3195 }
3196
3197 static GstStateChangeReturn
gst_rtp_bin_change_state(GstElement * element,GstStateChange transition)3198 gst_rtp_bin_change_state (GstElement * element, GstStateChange transition)
3199 {
3200 GstStateChangeReturn res;
3201 GstRtpBin *rtpbin;
3202 GstRtpBinPrivate *priv;
3203
3204 rtpbin = GST_RTP_BIN (element);
3205 priv = rtpbin->priv;
3206
3207 switch (transition) {
3208 case GST_STATE_CHANGE_NULL_TO_READY:
3209 break;
3210 case GST_STATE_CHANGE_READY_TO_PAUSED:
3211 priv->last_ntpnstime = 0;
3212 GST_LOG_OBJECT (rtpbin, "clearing shutdown flag");
3213 g_atomic_int_set (&priv->shutdown, 0);
3214 break;
3215 case GST_STATE_CHANGE_PAUSED_TO_READY:
3216 GST_LOG_OBJECT (rtpbin, "setting shutdown flag");
3217 g_atomic_int_set (&priv->shutdown, 1);
3218 /* wait for all callbacks to end by taking the lock. No new callbacks will
3219 * be able to happen as we set the shutdown flag. */
3220 GST_RTP_BIN_DYN_LOCK (rtpbin);
3221 GST_LOG_OBJECT (rtpbin, "dynamic lock taken, we can continue shutdown");
3222 GST_RTP_BIN_DYN_UNLOCK (rtpbin);
3223 break;
3224 default:
3225 break;
3226 }
3227
3228 res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3229
3230 switch (transition) {
3231 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3232 break;
3233 case GST_STATE_CHANGE_PAUSED_TO_READY:
3234 break;
3235 case GST_STATE_CHANGE_READY_TO_NULL:
3236 break;
3237 default:
3238 break;
3239 }
3240 return res;
3241 }
3242
3243 static GstElement *
session_request_element(GstRtpBinSession * session,guint signal)3244 session_request_element (GstRtpBinSession * session, guint signal)
3245 {
3246 GstElement *element = NULL;
3247 GstRtpBin *bin = session->bin;
3248
3249 g_signal_emit (bin, gst_rtp_bin_signals[signal], 0, session->id, &element);
3250
3251 if (element) {
3252 if (!bin_manage_element (bin, element))
3253 goto manage_failed;
3254 session->elements = g_slist_prepend (session->elements, element);
3255 }
3256 return element;
3257
3258 /* ERRORS */
3259 manage_failed:
3260 {
3261 GST_WARNING_OBJECT (bin, "unable to manage element");
3262 gst_object_unref (element);
3263 return NULL;
3264 }
3265 }
3266
3267 static gboolean
copy_sticky_events(GstPad * pad,GstEvent ** event,gpointer user_data)3268 copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
3269 {
3270 GstPad *gpad = GST_PAD_CAST (user_data);
3271
3272 GST_DEBUG_OBJECT (gpad, "store sticky event %" GST_PTR_FORMAT, *event);
3273 gst_pad_store_sticky_event (gpad, *event);
3274
3275 return TRUE;
3276 }
3277
3278 static void
expose_recv_src_pad(GstRtpBin * rtpbin,GstPad * pad,GstRtpBinStream * stream,guint8 pt)3279 expose_recv_src_pad (GstRtpBin * rtpbin, GstPad * pad, GstRtpBinStream * stream,
3280 guint8 pt)
3281 {
3282 GstElementClass *klass;
3283 GstPadTemplate *templ;
3284 gchar *padname;
3285 GstPad *gpad;
3286
3287 gst_object_ref (pad);
3288
3289 if (stream->session->storage) {
3290 GstElement *fec_decoder =
3291 session_request_element (stream->session, SIGNAL_REQUEST_FEC_DECODER);
3292
3293 if (fec_decoder) {
3294 GstPad *sinkpad, *srcpad;
3295 GstPadLinkReturn ret;
3296
3297 sinkpad = gst_element_get_static_pad (fec_decoder, "sink");
3298
3299 if (!sinkpad)
3300 goto fec_decoder_sink_failed;
3301
3302 ret = gst_pad_link (pad, sinkpad);
3303 gst_object_unref (sinkpad);
3304
3305 if (ret != GST_PAD_LINK_OK)
3306 goto fec_decoder_link_failed;
3307
3308 srcpad = gst_element_get_static_pad (fec_decoder, "src");
3309
3310 if (!srcpad)
3311 goto fec_decoder_src_failed;
3312
3313 gst_pad_sticky_events_foreach (pad, copy_sticky_events, srcpad);
3314 gst_object_unref (pad);
3315 pad = srcpad;
3316 }
3317 }
3318
3319 GST_RTP_BIN_SHUTDOWN_LOCK (rtpbin, shutdown);
3320
3321 /* ghost the pad to the parent */
3322 klass = GST_ELEMENT_GET_CLASS (rtpbin);
3323 templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%u_%u_%u");
3324 padname = g_strdup_printf ("recv_rtp_src_%u_%u_%u",
3325 stream->session->id, stream->ssrc, pt);
3326 gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
3327 g_free (padname);
3328 g_object_set_data (G_OBJECT (pad), "GstRTPBin.ghostpad", gpad);
3329
3330 gst_pad_set_active (gpad, TRUE);
3331 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
3332
3333 gst_pad_sticky_events_foreach (pad, copy_sticky_events, gpad);
3334 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
3335
3336 done:
3337 gst_object_unref (pad);
3338
3339 return;
3340
3341 shutdown:
3342 {
3343 GST_DEBUG ("ignoring, we are shutting down");
3344 goto done;
3345 }
3346 fec_decoder_sink_failed:
3347 {
3348 g_warning ("rtpbin: failed to get fec encoder sink pad for session %u",
3349 stream->session->id);
3350 goto done;
3351 }
3352 fec_decoder_src_failed:
3353 {
3354 g_warning ("rtpbin: failed to get fec encoder src pad for session %u",
3355 stream->session->id);
3356 goto done;
3357 }
3358 fec_decoder_link_failed:
3359 {
3360 g_warning ("rtpbin: failed to link fec decoder for session %u",
3361 stream->session->id);
3362 goto done;
3363 }
3364 }
3365
3366 /* a new pad (SSRC) was created in @session. This signal is emited from the
3367 * payload demuxer. */
3368 static void
new_payload_found(GstElement * element,guint pt,GstPad * pad,GstRtpBinStream * stream)3369 new_payload_found (GstElement * element, guint pt, GstPad * pad,
3370 GstRtpBinStream * stream)
3371 {
3372 GstRtpBin *rtpbin;
3373
3374 rtpbin = stream->bin;
3375
3376 GST_DEBUG_OBJECT (rtpbin, "new payload pad %u", pt);
3377
3378 expose_recv_src_pad (rtpbin, pad, stream, pt);
3379 }
3380
3381 static void
payload_pad_removed(GstElement * element,GstPad * pad,GstRtpBinStream * stream)3382 payload_pad_removed (GstElement * element, GstPad * pad,
3383 GstRtpBinStream * stream)
3384 {
3385 GstRtpBin *rtpbin;
3386 GstPad *gpad;
3387
3388 rtpbin = stream->bin;
3389
3390 GST_DEBUG ("payload pad removed");
3391
3392 GST_RTP_BIN_DYN_LOCK (rtpbin);
3393 if ((gpad = g_object_get_data (G_OBJECT (pad), "GstRTPBin.ghostpad"))) {
3394 g_object_set_data (G_OBJECT (pad), "GstRTPBin.ghostpad", NULL);
3395
3396 gst_pad_set_active (gpad, FALSE);
3397 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin), gpad);
3398 }
3399 GST_RTP_BIN_DYN_UNLOCK (rtpbin);
3400 }
3401
3402 static GstCaps *
pt_map_requested(GstElement * element,guint pt,GstRtpBinSession * session)3403 pt_map_requested (GstElement * element, guint pt, GstRtpBinSession * session)
3404 {
3405 GstRtpBin *rtpbin;
3406 GstCaps *caps;
3407
3408 rtpbin = session->bin;
3409
3410 GST_DEBUG_OBJECT (rtpbin, "payload map requested for pt %u in session %u", pt,
3411 session->id);
3412
3413 caps = get_pt_map (session, pt);
3414 if (!caps)
3415 goto no_caps;
3416
3417 return caps;
3418
3419 /* ERRORS */
3420 no_caps:
3421 {
3422 GST_DEBUG_OBJECT (rtpbin, "could not get caps");
3423 return NULL;
3424 }
3425 }
3426
3427 static GstCaps *
ptdemux_pt_map_requested(GstElement * element,guint pt,GstRtpBinSession * session)3428 ptdemux_pt_map_requested (GstElement * element, guint pt,
3429 GstRtpBinSession * session)
3430 {
3431 GstCaps *ret = pt_map_requested (element, pt, session);
3432
3433 if (ret && gst_caps_get_size (ret) == 1) {
3434 const GstStructure *s = gst_caps_get_structure (ret, 0);
3435 gboolean is_fec;
3436
3437 if (gst_structure_get_boolean (s, "is-fec", &is_fec) && is_fec) {
3438 GValue v = G_VALUE_INIT;
3439 GValue v2 = G_VALUE_INIT;
3440
3441 GST_INFO_OBJECT (session->bin, "Will ignore FEC pt %u in session %u", pt,
3442 session->id);
3443 g_value_init (&v, GST_TYPE_ARRAY);
3444 g_value_init (&v2, G_TYPE_INT);
3445 g_object_get_property (G_OBJECT (element), "ignored-payload-types", &v);
3446 g_value_set_int (&v2, pt);
3447 gst_value_array_append_value (&v, &v2);
3448 g_value_unset (&v2);
3449 g_object_set_property (G_OBJECT (element), "ignored-payload-types", &v);
3450 g_value_unset (&v);
3451 }
3452 }
3453
3454 return ret;
3455 }
3456
3457 static void
payload_type_change(GstElement * element,guint pt,GstRtpBinSession * session)3458 payload_type_change (GstElement * element, guint pt, GstRtpBinSession * session)
3459 {
3460 GST_DEBUG_OBJECT (session->bin,
3461 "emiting signal for pt type changed to %u in session %u", pt,
3462 session->id);
3463
3464 g_signal_emit (session->bin, gst_rtp_bin_signals[SIGNAL_PAYLOAD_TYPE_CHANGE],
3465 0, session->id, pt);
3466 }
3467
3468 /* emitted when caps changed for the session */
3469 static void
caps_changed(GstPad * pad,GParamSpec * pspec,GstRtpBinSession * session)3470 caps_changed (GstPad * pad, GParamSpec * pspec, GstRtpBinSession * session)
3471 {
3472 GstRtpBin *bin;
3473 GstCaps *caps;
3474 gint payload;
3475 const GstStructure *s;
3476
3477 bin = session->bin;
3478
3479 g_object_get (pad, "caps", &caps, NULL);
3480
3481 if (caps == NULL)
3482 return;
3483
3484 GST_DEBUG_OBJECT (bin, "got caps %" GST_PTR_FORMAT, caps);
3485
3486 s = gst_caps_get_structure (caps, 0);
3487
3488 /* get payload, finish when it's not there */
3489 if (!gst_structure_get_int (s, "payload", &payload)) {
3490 gst_caps_unref (caps);
3491 return;
3492 }
3493
3494 GST_RTP_SESSION_LOCK (session);
3495 GST_DEBUG_OBJECT (bin, "insert caps for payload %d", payload);
3496 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (payload), caps);
3497 GST_RTP_SESSION_UNLOCK (session);
3498 }
3499
3500 /* a new pad (SSRC) was created in @session */
3501 static void
new_ssrc_pad_found(GstElement * element,guint ssrc,GstPad * pad,GstRtpBinSession * session)3502 new_ssrc_pad_found (GstElement * element, guint ssrc, GstPad * pad,
3503 GstRtpBinSession * session)
3504 {
3505 GstRtpBin *rtpbin;
3506 GstRtpBinStream *stream;
3507 GstPad *sinkpad, *srcpad;
3508 gchar *padname;
3509
3510 rtpbin = session->bin;
3511
3512 GST_DEBUG_OBJECT (rtpbin, "new SSRC pad %08x, %s:%s", ssrc,
3513 GST_DEBUG_PAD_NAME (pad));
3514
3515 GST_RTP_BIN_SHUTDOWN_LOCK (rtpbin, shutdown);
3516
3517 GST_RTP_SESSION_LOCK (session);
3518
3519 /* create new stream */
3520 stream = create_stream (session, ssrc);
3521 if (!stream)
3522 goto no_stream;
3523
3524 /* get pad and link */
3525 GST_DEBUG_OBJECT (rtpbin, "linking jitterbuffer RTP");
3526 padname = g_strdup_printf ("src_%u", ssrc);
3527 srcpad = gst_element_get_static_pad (element, padname);
3528 g_free (padname);
3529 sinkpad = gst_element_get_static_pad (stream->buffer, "sink");
3530 gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_NOTHING);
3531 gst_object_unref (sinkpad);
3532 gst_object_unref (srcpad);
3533
3534 GST_DEBUG_OBJECT (rtpbin, "linking jitterbuffer RTCP");
3535 padname = g_strdup_printf ("rtcp_src_%u", ssrc);
3536 srcpad = gst_element_get_static_pad (element, padname);
3537 g_free (padname);
3538 sinkpad = gst_element_get_request_pad (stream->buffer, "sink_rtcp");
3539 gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_NOTHING);
3540 gst_object_unref (sinkpad);
3541 gst_object_unref (srcpad);
3542
3543 /* connect to the RTCP sync signal from the jitterbuffer */
3544 GST_DEBUG_OBJECT (rtpbin, "connecting sync signal");
3545 stream->buffer_handlesync_sig = g_signal_connect (stream->buffer,
3546 "handle-sync", (GCallback) gst_rtp_bin_handle_sync, stream);
3547
3548 if (stream->demux) {
3549 /* connect to the new-pad signal of the payload demuxer, this will expose the
3550 * new pad by ghosting it. */
3551 stream->demux_newpad_sig = g_signal_connect (stream->demux,
3552 "new-payload-type", (GCallback) new_payload_found, stream);
3553 stream->demux_padremoved_sig = g_signal_connect (stream->demux,
3554 "pad-removed", (GCallback) payload_pad_removed, stream);
3555
3556 /* connect to the request-pt-map signal. This signal will be emitted by the
3557 * demuxer so that it can apply a proper caps on the buffers for the
3558 * depayloaders. */
3559 stream->demux_ptreq_sig = g_signal_connect (stream->demux,
3560 "request-pt-map", (GCallback) ptdemux_pt_map_requested, session);
3561 /* connect to the signal so it can be forwarded. */
3562 stream->demux_ptchange_sig = g_signal_connect (stream->demux,
3563 "payload-type-change", (GCallback) payload_type_change, session);
3564
3565 GST_RTP_SESSION_UNLOCK (session);
3566 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
3567 } else {
3568 /* add rtpjitterbuffer src pad to pads */
3569 GstPad *pad;
3570
3571 pad = gst_element_get_static_pad (stream->buffer, "src");
3572
3573 GST_RTP_SESSION_UNLOCK (session);
3574 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
3575
3576 expose_recv_src_pad (rtpbin, pad, stream, 255);
3577
3578 gst_object_unref (pad);
3579 }
3580
3581 return;
3582
3583 /* ERRORS */
3584 shutdown:
3585 {
3586 GST_DEBUG_OBJECT (rtpbin, "we are shutting down");
3587 return;
3588 }
3589 no_stream:
3590 {
3591 GST_RTP_SESSION_UNLOCK (session);
3592 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
3593 GST_DEBUG_OBJECT (rtpbin, "could not create stream");
3594 return;
3595 }
3596 }
3597
3598 static GstPad *
complete_session_sink(GstRtpBin * rtpbin,GstRtpBinSession * session)3599 complete_session_sink (GstRtpBin * rtpbin, GstRtpBinSession * session)
3600 {
3601 guint sessid = session->id;
3602 GstPad *recv_rtp_sink;
3603 GstElement *decoder;
3604
3605 g_assert (!session->recv_rtp_sink);
3606
3607 /* get recv_rtp pad and store */
3608 session->recv_rtp_sink =
3609 gst_element_get_request_pad (session->session, "recv_rtp_sink");
3610 if (session->recv_rtp_sink == NULL)
3611 goto pad_failed;
3612
3613 g_signal_connect (session->recv_rtp_sink, "notify::caps",
3614 (GCallback) caps_changed, session);
3615
3616 GST_DEBUG_OBJECT (rtpbin, "requesting RTP decoder");
3617 decoder = session_request_element (session, SIGNAL_REQUEST_RTP_DECODER);
3618 if (decoder) {
3619 GstPad *decsrc, *decsink;
3620 GstPadLinkReturn ret;
3621
3622 GST_DEBUG_OBJECT (rtpbin, "linking RTP decoder");
3623 decsink = gst_element_get_static_pad (decoder, "rtp_sink");
3624 if (decsink == NULL)
3625 goto dec_sink_failed;
3626
3627 recv_rtp_sink = decsink;
3628
3629 decsrc = gst_element_get_static_pad (decoder, "rtp_src");
3630 if (decsrc == NULL)
3631 goto dec_src_failed;
3632
3633 ret = gst_pad_link (decsrc, session->recv_rtp_sink);
3634
3635 gst_object_unref (decsrc);
3636
3637 if (ret != GST_PAD_LINK_OK)
3638 goto dec_link_failed;
3639
3640 } else {
3641 GST_DEBUG_OBJECT (rtpbin, "no RTP decoder given");
3642 recv_rtp_sink = gst_object_ref (session->recv_rtp_sink);
3643 }
3644
3645 return recv_rtp_sink;
3646
3647 /* ERRORS */
3648 pad_failed:
3649 {
3650 g_warning ("rtpbin: failed to get session recv_rtp_sink pad");
3651 return NULL;
3652 }
3653 dec_sink_failed:
3654 {
3655 g_warning ("rtpbin: failed to get decoder sink pad for session %u", sessid);
3656 return NULL;
3657 }
3658 dec_src_failed:
3659 {
3660 g_warning ("rtpbin: failed to get decoder src pad for session %u", sessid);
3661 gst_object_unref (recv_rtp_sink);
3662 return NULL;
3663 }
3664 dec_link_failed:
3665 {
3666 g_warning ("rtpbin: failed to link rtp decoder for session %u", sessid);
3667 gst_object_unref (recv_rtp_sink);
3668 return NULL;
3669 }
3670 }
3671
3672 static void
complete_session_receiver(GstRtpBin * rtpbin,GstRtpBinSession * session,guint sessid)3673 complete_session_receiver (GstRtpBin * rtpbin, GstRtpBinSession * session,
3674 guint sessid)
3675 {
3676 GstElement *aux;
3677 GstPad *recv_rtp_src;
3678
3679 g_assert (!session->recv_rtp_src);
3680
3681 session->recv_rtp_src =
3682 gst_element_get_static_pad (session->session, "recv_rtp_src");
3683 if (session->recv_rtp_src == NULL)
3684 goto pad_failed;
3685
3686 /* find out if we need AUX elements */
3687 aux = session_request_element (session, SIGNAL_REQUEST_AUX_RECEIVER);
3688 if (aux) {
3689 gchar *pname;
3690 GstPad *auxsink;
3691 GstPadLinkReturn ret;
3692
3693 GST_DEBUG_OBJECT (rtpbin, "linking AUX receiver");
3694
3695 pname = g_strdup_printf ("sink_%u", sessid);
3696 auxsink = gst_element_get_static_pad (aux, pname);
3697 g_free (pname);
3698 if (auxsink == NULL)
3699 goto aux_sink_failed;
3700
3701 ret = gst_pad_link (session->recv_rtp_src, auxsink);
3702 gst_object_unref (auxsink);
3703 if (ret != GST_PAD_LINK_OK)
3704 goto aux_link_failed;
3705
3706 /* this can be NULL when this AUX element is not to be linked any further */
3707 pname = g_strdup_printf ("src_%u", sessid);
3708 recv_rtp_src = gst_element_get_static_pad (aux, pname);
3709 g_free (pname);
3710 } else {
3711 recv_rtp_src = gst_object_ref (session->recv_rtp_src);
3712 }
3713
3714 /* Add a storage element if needed */
3715 if (recv_rtp_src && session->storage) {
3716 GstPadLinkReturn ret;
3717 GstPad *sinkpad = gst_element_get_static_pad (session->storage, "sink");
3718
3719 ret = gst_pad_link (recv_rtp_src, sinkpad);
3720
3721 gst_object_unref (sinkpad);
3722 gst_object_unref (recv_rtp_src);
3723
3724 if (ret != GST_PAD_LINK_OK)
3725 goto storage_link_failed;
3726
3727 recv_rtp_src = gst_element_get_static_pad (session->storage, "src");
3728 }
3729
3730 if (recv_rtp_src) {
3731 GstPad *sinkdpad;
3732
3733 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTP sink pad");
3734 sinkdpad = gst_element_get_static_pad (session->demux, "sink");
3735 GST_DEBUG_OBJECT (rtpbin, "linking demuxer RTP sink pad");
3736 gst_pad_link_full (recv_rtp_src, sinkdpad, GST_PAD_LINK_CHECK_NOTHING);
3737 gst_object_unref (sinkdpad);
3738 gst_object_unref (recv_rtp_src);
3739
3740 /* connect to the new-ssrc-pad signal of the SSRC demuxer */
3741 session->demux_newpad_sig = g_signal_connect (session->demux,
3742 "new-ssrc-pad", (GCallback) new_ssrc_pad_found, session);
3743 session->demux_padremoved_sig = g_signal_connect (session->demux,
3744 "removed-ssrc-pad", (GCallback) ssrc_demux_pad_removed, session);
3745 }
3746
3747 return;
3748
3749 pad_failed:
3750 {
3751 g_warning ("rtpbin: failed to get session recv_rtp_src pad");
3752 return;
3753 }
3754 aux_sink_failed:
3755 {
3756 g_warning ("rtpbin: failed to get AUX sink pad for session %u", sessid);
3757 return;
3758 }
3759 aux_link_failed:
3760 {
3761 g_warning ("rtpbin: failed to link AUX pad to session %u", sessid);
3762 return;
3763 }
3764 storage_link_failed:
3765 {
3766 g_warning ("rtpbin: failed to link storage");
3767 return;
3768 }
3769 }
3770
3771 /* Create a pad for receiving RTP for the session in @name. Must be called with
3772 * RTP_BIN_LOCK.
3773 */
3774 static GstPad *
create_recv_rtp(GstRtpBin * rtpbin,GstPadTemplate * templ,const gchar * name)3775 create_recv_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
3776 {
3777 guint sessid;
3778 GstRtpBinSession *session;
3779 GstPad *recv_rtp_sink;
3780
3781 /* first get the session number */
3782 if (name == NULL || sscanf (name, "recv_rtp_sink_%u", &sessid) != 1)
3783 goto no_name;
3784
3785 GST_DEBUG_OBJECT (rtpbin, "finding session %u", sessid);
3786
3787 /* get or create session */
3788 session = find_session_by_id (rtpbin, sessid);
3789 if (!session) {
3790 GST_DEBUG_OBJECT (rtpbin, "creating session %u", sessid);
3791 /* create session now */
3792 session = create_session (rtpbin, sessid);
3793 if (session == NULL)
3794 goto create_error;
3795 }
3796
3797 /* check if pad was requested */
3798 if (session->recv_rtp_sink_ghost != NULL)
3799 return session->recv_rtp_sink_ghost;
3800
3801 /* setup the session sink pad */
3802 recv_rtp_sink = complete_session_sink (rtpbin, session);
3803 if (!recv_rtp_sink)
3804 goto session_sink_failed;
3805
3806 GST_DEBUG_OBJECT (rtpbin, "ghosting session sink pad");
3807 session->recv_rtp_sink_ghost =
3808 gst_ghost_pad_new_from_template (name, recv_rtp_sink, templ);
3809 gst_object_unref (recv_rtp_sink);
3810 gst_pad_set_active (session->recv_rtp_sink_ghost, TRUE);
3811 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->recv_rtp_sink_ghost);
3812
3813 complete_session_receiver (rtpbin, session, sessid);
3814
3815 return session->recv_rtp_sink_ghost;
3816
3817 /* ERRORS */
3818 no_name:
3819 {
3820 g_warning ("rtpbin: invalid name given");
3821 return NULL;
3822 }
3823 create_error:
3824 {
3825 /* create_session already warned */
3826 return NULL;
3827 }
3828 session_sink_failed:
3829 {
3830 /* warning already done */
3831 return NULL;
3832 }
3833 }
3834
3835 static void
remove_recv_rtp(GstRtpBin * rtpbin,GstRtpBinSession * session)3836 remove_recv_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session)
3837 {
3838 if (session->demux_newpad_sig) {
3839 g_signal_handler_disconnect (session->demux, session->demux_newpad_sig);
3840 session->demux_newpad_sig = 0;
3841 }
3842 if (session->demux_padremoved_sig) {
3843 g_signal_handler_disconnect (session->demux, session->demux_padremoved_sig);
3844 session->demux_padremoved_sig = 0;
3845 }
3846 if (session->recv_rtp_src) {
3847 gst_object_unref (session->recv_rtp_src);
3848 session->recv_rtp_src = NULL;
3849 }
3850 if (session->recv_rtp_sink) {
3851 gst_element_release_request_pad (session->session, session->recv_rtp_sink);
3852 gst_object_unref (session->recv_rtp_sink);
3853 session->recv_rtp_sink = NULL;
3854 }
3855 if (session->recv_rtp_sink_ghost) {
3856 gst_pad_set_active (session->recv_rtp_sink_ghost, FALSE);
3857 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
3858 session->recv_rtp_sink_ghost);
3859 session->recv_rtp_sink_ghost = NULL;
3860 }
3861 }
3862
3863 static GstPad *
complete_session_rtcp(GstRtpBin * rtpbin,GstRtpBinSession * session,guint sessid)3864 complete_session_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session,
3865 guint sessid)
3866 {
3867 GstElement *decoder;
3868 GstPad *sinkdpad;
3869 GstPad *decsink = NULL;
3870
3871 /* get recv_rtp pad and store */
3872 GST_DEBUG_OBJECT (rtpbin, "getting RTCP sink pad");
3873 session->recv_rtcp_sink =
3874 gst_element_get_request_pad (session->session, "recv_rtcp_sink");
3875 if (session->recv_rtcp_sink == NULL)
3876 goto pad_failed;
3877
3878 GST_DEBUG_OBJECT (rtpbin, "getting RTCP decoder");
3879 decoder = session_request_element (session, SIGNAL_REQUEST_RTCP_DECODER);
3880 if (decoder) {
3881 GstPad *decsrc;
3882 GstPadLinkReturn ret;
3883
3884 GST_DEBUG_OBJECT (rtpbin, "linking RTCP decoder");
3885 decsink = gst_element_get_static_pad (decoder, "rtcp_sink");
3886 decsrc = gst_element_get_static_pad (decoder, "rtcp_src");
3887
3888 if (decsink == NULL)
3889 goto dec_sink_failed;
3890
3891 if (decsrc == NULL)
3892 goto dec_src_failed;
3893
3894 ret = gst_pad_link (decsrc, session->recv_rtcp_sink);
3895
3896 gst_object_unref (decsrc);
3897
3898 if (ret != GST_PAD_LINK_OK)
3899 goto dec_link_failed;
3900 } else {
3901 GST_DEBUG_OBJECT (rtpbin, "no RTCP decoder given");
3902 decsink = gst_object_ref (session->recv_rtcp_sink);
3903 }
3904
3905 /* get srcpad, link to SSRCDemux */
3906 GST_DEBUG_OBJECT (rtpbin, "getting sync src pad");
3907 session->sync_src = gst_element_get_static_pad (session->session, "sync_src");
3908 if (session->sync_src == NULL)
3909 goto src_pad_failed;
3910
3911 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTCP sink pad");
3912 sinkdpad = gst_element_get_static_pad (session->demux, "rtcp_sink");
3913 gst_pad_link_full (session->sync_src, sinkdpad, GST_PAD_LINK_CHECK_NOTHING);
3914 gst_object_unref (sinkdpad);
3915
3916 return decsink;
3917
3918 pad_failed:
3919 {
3920 g_warning ("rtpbin: failed to get session rtcp_sink pad");
3921 return NULL;
3922 }
3923 dec_sink_failed:
3924 {
3925 g_warning ("rtpbin: failed to get decoder sink pad for session %u", sessid);
3926 return NULL;
3927 }
3928 dec_src_failed:
3929 {
3930 g_warning ("rtpbin: failed to get decoder src pad for session %u", sessid);
3931 goto cleanup;
3932 }
3933 dec_link_failed:
3934 {
3935 g_warning ("rtpbin: failed to link rtcp decoder for session %u", sessid);
3936 goto cleanup;
3937 }
3938 src_pad_failed:
3939 {
3940 g_warning ("rtpbin: failed to get session sync_src pad");
3941 }
3942
3943 cleanup:
3944 gst_object_unref (decsink);
3945 return NULL;
3946 }
3947
3948 /* Create a pad for receiving RTCP for the session in @name. Must be called with
3949 * RTP_BIN_LOCK.
3950 */
3951 static GstPad *
create_recv_rtcp(GstRtpBin * rtpbin,GstPadTemplate * templ,const gchar * name)3952 create_recv_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ,
3953 const gchar * name)
3954 {
3955 guint sessid;
3956 GstRtpBinSession *session;
3957 GstPad *decsink = NULL;
3958
3959 /* first get the session number */
3960 if (name == NULL || sscanf (name, "recv_rtcp_sink_%u", &sessid) != 1)
3961 goto no_name;
3962
3963 GST_DEBUG_OBJECT (rtpbin, "finding session %u", sessid);
3964
3965 /* get or create the session */
3966 session = find_session_by_id (rtpbin, sessid);
3967 if (!session) {
3968 GST_DEBUG_OBJECT (rtpbin, "creating session %u", sessid);
3969 /* create session now */
3970 session = create_session (rtpbin, sessid);
3971 if (session == NULL)
3972 goto create_error;
3973 }
3974
3975 /* check if pad was requested */
3976 if (session->recv_rtcp_sink_ghost != NULL)
3977 return session->recv_rtcp_sink_ghost;
3978
3979 decsink = complete_session_rtcp (rtpbin, session, sessid);
3980 if (!decsink)
3981 goto create_error;
3982
3983 session->recv_rtcp_sink_ghost =
3984 gst_ghost_pad_new_from_template (name, decsink, templ);
3985 gst_object_unref (decsink);
3986 gst_pad_set_active (session->recv_rtcp_sink_ghost, TRUE);
3987 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin),
3988 session->recv_rtcp_sink_ghost);
3989
3990 return session->recv_rtcp_sink_ghost;
3991
3992 /* ERRORS */
3993 no_name:
3994 {
3995 g_warning ("rtpbin: invalid name given");
3996 return NULL;
3997 }
3998 create_error:
3999 {
4000 /* create_session already warned */
4001 return NULL;
4002 }
4003 }
4004
4005 static void
remove_recv_rtcp(GstRtpBin * rtpbin,GstRtpBinSession * session)4006 remove_recv_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session)
4007 {
4008 if (session->recv_rtcp_sink_ghost) {
4009 gst_pad_set_active (session->recv_rtcp_sink_ghost, FALSE);
4010 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
4011 session->recv_rtcp_sink_ghost);
4012 session->recv_rtcp_sink_ghost = NULL;
4013 }
4014 if (session->sync_src) {
4015 /* releasing the request pad should also unref the sync pad */
4016 gst_object_unref (session->sync_src);
4017 session->sync_src = NULL;
4018 }
4019 if (session->recv_rtcp_sink) {
4020 gst_element_release_request_pad (session->session, session->recv_rtcp_sink);
4021 gst_object_unref (session->recv_rtcp_sink);
4022 session->recv_rtcp_sink = NULL;
4023 }
4024 }
4025
4026 static gboolean
complete_session_src(GstRtpBin * rtpbin,GstRtpBinSession * session)4027 complete_session_src (GstRtpBin * rtpbin, GstRtpBinSession * session)
4028 {
4029 gchar *gname;
4030 guint sessid = session->id;
4031 GstPad *send_rtp_src;
4032 GstElement *encoder;
4033 GstElementClass *klass;
4034 GstPadTemplate *templ;
4035 gboolean ret = FALSE;
4036
4037 /* get srcpad */
4038 send_rtp_src = gst_element_get_static_pad (session->session, "send_rtp_src");
4039
4040 if (send_rtp_src == NULL)
4041 goto no_srcpad;
4042
4043 GST_DEBUG_OBJECT (rtpbin, "getting RTP encoder");
4044 encoder = session_request_element (session, SIGNAL_REQUEST_RTP_ENCODER);
4045 if (encoder) {
4046 gchar *ename;
4047 GstPad *encsrc, *encsink;
4048 GstPadLinkReturn ret;
4049
4050 GST_DEBUG_OBJECT (rtpbin, "linking RTP encoder");
4051 ename = g_strdup_printf ("rtp_src_%u", sessid);
4052 encsrc = gst_element_get_static_pad (encoder, ename);
4053 g_free (ename);
4054
4055 if (encsrc == NULL)
4056 goto enc_src_failed;
4057
4058 ename = g_strdup_printf ("rtp_sink_%u", sessid);
4059 encsink = gst_element_get_static_pad (encoder, ename);
4060 g_free (ename);
4061 if (encsink == NULL)
4062 goto enc_sink_failed;
4063
4064 ret = gst_pad_link (send_rtp_src, encsink);
4065 gst_object_unref (encsink);
4066 gst_object_unref (send_rtp_src);
4067
4068 send_rtp_src = encsrc;
4069
4070 if (ret != GST_PAD_LINK_OK)
4071 goto enc_link_failed;
4072 } else {
4073 GST_DEBUG_OBJECT (rtpbin, "no RTP encoder given");
4074 }
4075
4076 /* ghost the new source pad */
4077 klass = GST_ELEMENT_GET_CLASS (rtpbin);
4078 gname = g_strdup_printf ("send_rtp_src_%u", sessid);
4079 templ = gst_element_class_get_pad_template (klass, "send_rtp_src_%u");
4080 session->send_rtp_src_ghost =
4081 gst_ghost_pad_new_from_template (gname, send_rtp_src, templ);
4082 gst_pad_set_active (session->send_rtp_src_ghost, TRUE);
4083 gst_pad_sticky_events_foreach (send_rtp_src, copy_sticky_events,
4084 session->send_rtp_src_ghost);
4085 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtp_src_ghost);
4086 g_free (gname);
4087
4088 ret = TRUE;
4089
4090 done:
4091 if (send_rtp_src)
4092 gst_object_unref (send_rtp_src);
4093
4094 return ret;
4095
4096 /* ERRORS */
4097 no_srcpad:
4098 {
4099 g_warning ("rtpbin: failed to get rtp source pad for session %u", sessid);
4100 goto done;
4101 }
4102 enc_src_failed:
4103 {
4104 g_warning ("rtpbin: failed to get %" GST_PTR_FORMAT
4105 " src pad for session %u", encoder, sessid);
4106 goto done;
4107 }
4108 enc_sink_failed:
4109 {
4110 g_warning ("rtpbin: failed to get %" GST_PTR_FORMAT
4111 " sink pad for session %u", encoder, sessid);
4112 goto done;
4113 }
4114 enc_link_failed:
4115 {
4116 g_warning ("rtpbin: failed to link %" GST_PTR_FORMAT " for session %u",
4117 encoder, sessid);
4118 goto done;
4119 }
4120 }
4121
4122 static gboolean
setup_aux_sender_fold(const GValue * item,GValue * result,gpointer user_data)4123 setup_aux_sender_fold (const GValue * item, GValue * result, gpointer user_data)
4124 {
4125 GstPad *pad;
4126 gchar *name;
4127 guint sessid;
4128 GstRtpBinSession *session = user_data, *newsess;
4129 GstRtpBin *rtpbin = session->bin;
4130 GstPadLinkReturn ret;
4131
4132 pad = g_value_get_object (item);
4133 name = gst_pad_get_name (pad);
4134
4135 if (name == NULL || sscanf (name, "src_%u", &sessid) != 1)
4136 goto no_name;
4137
4138 g_free (name);
4139
4140 newsess = find_session_by_id (rtpbin, sessid);
4141 if (newsess == NULL) {
4142 /* create new session */
4143 newsess = create_session (rtpbin, sessid);
4144 if (newsess == NULL)
4145 goto create_error;
4146 } else if (newsess->send_rtp_sink != NULL)
4147 goto existing_session;
4148
4149 /* get send_rtp pad and store */
4150 newsess->send_rtp_sink =
4151 gst_element_get_request_pad (newsess->session, "send_rtp_sink");
4152 if (newsess->send_rtp_sink == NULL)
4153 goto pad_failed;
4154
4155 ret = gst_pad_link (pad, newsess->send_rtp_sink);
4156 if (ret != GST_PAD_LINK_OK)
4157 goto aux_link_failed;
4158
4159 if (!complete_session_src (rtpbin, newsess))
4160 goto session_src_failed;
4161
4162 return TRUE;
4163
4164 /* ERRORS */
4165 no_name:
4166 {
4167 GST_WARNING ("ignoring invalid pad name %s", GST_STR_NULL (name));
4168 g_free (name);
4169 return TRUE;
4170 }
4171 create_error:
4172 {
4173 /* create_session already warned */
4174 return FALSE;
4175 }
4176 existing_session:
4177 {
4178 GST_DEBUG_OBJECT (rtpbin,
4179 "skipping src_%i setup, since it is already configured.", sessid);
4180 return TRUE;
4181 }
4182 pad_failed:
4183 {
4184 g_warning ("rtpbin: failed to get session pad for session %u", sessid);
4185 return FALSE;
4186 }
4187 aux_link_failed:
4188 {
4189 g_warning ("rtpbin: failed to link AUX for session %u", sessid);
4190 return FALSE;
4191 }
4192 session_src_failed:
4193 {
4194 g_warning ("rtpbin: failed to complete AUX for session %u", sessid);
4195 return FALSE;
4196 }
4197 }
4198
4199 static gboolean
setup_aux_sender(GstRtpBin * rtpbin,GstRtpBinSession * session,GstElement * aux)4200 setup_aux_sender (GstRtpBin * rtpbin, GstRtpBinSession * session,
4201 GstElement * aux)
4202 {
4203 GstIterator *it;
4204 GValue result = { 0, };
4205 GstIteratorResult res;
4206
4207 it = gst_element_iterate_src_pads (aux);
4208 res = gst_iterator_fold (it, setup_aux_sender_fold, &result, session);
4209 gst_iterator_free (it);
4210
4211 return res == GST_ITERATOR_DONE;
4212 }
4213
4214 /* Create a pad for sending RTP for the session in @name. Must be called with
4215 * RTP_BIN_LOCK.
4216 */
4217 static GstPad *
create_send_rtp(GstRtpBin * rtpbin,GstPadTemplate * templ,const gchar * name)4218 create_send_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
4219 {
4220 gchar *pname;
4221 guint sessid;
4222 GstPad *send_rtp_sink;
4223 GstElement *aux;
4224 GstElement *encoder;
4225 GstElement *prev = NULL;
4226 GstRtpBinSession *session;
4227
4228 /* first get the session number */
4229 if (name == NULL || sscanf (name, "send_rtp_sink_%u", &sessid) != 1)
4230 goto no_name;
4231
4232 /* get or create session */
4233 session = find_session_by_id (rtpbin, sessid);
4234 if (!session) {
4235 /* create session now */
4236 session = create_session (rtpbin, sessid);
4237 if (session == NULL)
4238 goto create_error;
4239 }
4240
4241 /* check if pad was requested */
4242 if (session->send_rtp_sink_ghost != NULL)
4243 return session->send_rtp_sink_ghost;
4244
4245 /* check if we are already using this session as a sender */
4246 if (session->send_rtp_sink != NULL)
4247 goto existing_session;
4248
4249 encoder = session_request_element (session, SIGNAL_REQUEST_FEC_ENCODER);
4250
4251 if (encoder) {
4252 GST_DEBUG_OBJECT (rtpbin, "Linking FEC encoder");
4253
4254 send_rtp_sink = gst_element_get_static_pad (encoder, "sink");
4255
4256 if (!send_rtp_sink)
4257 goto enc_sink_failed;
4258
4259 prev = encoder;
4260 }
4261
4262 GST_DEBUG_OBJECT (rtpbin, "getting RTP AUX sender");
4263 aux = session_request_element (session, SIGNAL_REQUEST_AUX_SENDER);
4264 if (aux) {
4265 GstPad *sinkpad;
4266 GST_DEBUG_OBJECT (rtpbin, "linking AUX sender");
4267 if (!setup_aux_sender (rtpbin, session, aux))
4268 goto aux_session_failed;
4269
4270 pname = g_strdup_printf ("sink_%u", sessid);
4271 sinkpad = gst_element_get_static_pad (aux, pname);
4272 g_free (pname);
4273
4274 if (sinkpad == NULL)
4275 goto aux_sink_failed;
4276
4277 if (!prev) {
4278 send_rtp_sink = sinkpad;
4279 } else {
4280 GstPad *srcpad = gst_element_get_static_pad (prev, "src");
4281 GstPadLinkReturn ret;
4282
4283 ret = gst_pad_link (srcpad, sinkpad);
4284 gst_object_unref (srcpad);
4285 if (ret != GST_PAD_LINK_OK) {
4286 goto aux_link_failed;
4287 }
4288 }
4289 prev = aux;
4290 } else {
4291 /* get send_rtp pad and store */
4292 session->send_rtp_sink =
4293 gst_element_get_request_pad (session->session, "send_rtp_sink");
4294 if (session->send_rtp_sink == NULL)
4295 goto pad_failed;
4296
4297 if (!complete_session_src (rtpbin, session))
4298 goto session_src_failed;
4299
4300 if (!prev) {
4301 send_rtp_sink = gst_object_ref (session->send_rtp_sink);
4302 } else {
4303 GstPad *srcpad = gst_element_get_static_pad (prev, "src");
4304 GstPadLinkReturn ret;
4305
4306 ret = gst_pad_link (srcpad, session->send_rtp_sink);
4307 gst_object_unref (srcpad);
4308 if (ret != GST_PAD_LINK_OK)
4309 goto session_link_failed;
4310 }
4311 }
4312
4313 session->send_rtp_sink_ghost =
4314 gst_ghost_pad_new_from_template (name, send_rtp_sink, templ);
4315 gst_object_unref (send_rtp_sink);
4316 gst_pad_set_active (session->send_rtp_sink_ghost, TRUE);
4317 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtp_sink_ghost);
4318
4319 return session->send_rtp_sink_ghost;
4320
4321 /* ERRORS */
4322 no_name:
4323 {
4324 g_warning ("rtpbin: invalid name given");
4325 return NULL;
4326 }
4327 create_error:
4328 {
4329 /* create_session already warned */
4330 return NULL;
4331 }
4332 existing_session:
4333 {
4334 g_warning ("rtpbin: session %u is already in use", sessid);
4335 return NULL;
4336 }
4337 aux_session_failed:
4338 {
4339 g_warning ("rtpbin: failed to get AUX sink pad for session %u", sessid);
4340 return NULL;
4341 }
4342 aux_sink_failed:
4343 {
4344 g_warning ("rtpbin: failed to get AUX sink pad for session %u", sessid);
4345 return NULL;
4346 }
4347 aux_link_failed:
4348 {
4349 g_warning ("rtpbin: failed to link %" GST_PTR_FORMAT " for session %u",
4350 aux, sessid);
4351 return NULL;
4352 }
4353 pad_failed:
4354 {
4355 g_warning ("rtpbin: failed to get session pad for session %u", sessid);
4356 return NULL;
4357 }
4358 session_src_failed:
4359 {
4360 g_warning ("rtpbin: failed to setup source pads for session %u", sessid);
4361 return NULL;
4362 }
4363 session_link_failed:
4364 {
4365 g_warning ("rtpbin: failed to link %" GST_PTR_FORMAT " for session %u",
4366 session, sessid);
4367 return NULL;
4368 }
4369 enc_sink_failed:
4370 {
4371 g_warning ("rtpbin: failed to get %" GST_PTR_FORMAT
4372 " sink pad for session %u", encoder, sessid);
4373 return NULL;
4374 }
4375 }
4376
4377 static void
remove_send_rtp(GstRtpBin * rtpbin,GstRtpBinSession * session)4378 remove_send_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session)
4379 {
4380 if (session->send_rtp_src_ghost) {
4381 gst_pad_set_active (session->send_rtp_src_ghost, FALSE);
4382 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
4383 session->send_rtp_src_ghost);
4384 session->send_rtp_src_ghost = NULL;
4385 }
4386 if (session->send_rtp_sink) {
4387 gst_element_release_request_pad (GST_ELEMENT_CAST (session->session),
4388 session->send_rtp_sink);
4389 gst_object_unref (session->send_rtp_sink);
4390 session->send_rtp_sink = NULL;
4391 }
4392 if (session->send_rtp_sink_ghost) {
4393 gst_pad_set_active (session->send_rtp_sink_ghost, FALSE);
4394 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
4395 session->send_rtp_sink_ghost);
4396 session->send_rtp_sink_ghost = NULL;
4397 }
4398 }
4399
4400 /* Create a pad for sending RTCP for the session in @name. Must be called with
4401 * RTP_BIN_LOCK.
4402 */
4403 static GstPad *
create_send_rtcp(GstRtpBin * rtpbin,GstPadTemplate * templ,const gchar * name)4404 create_send_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ,
4405 const gchar * name)
4406 {
4407 guint sessid;
4408 GstPad *encsrc;
4409 GstElement *encoder;
4410 GstRtpBinSession *session;
4411
4412 /* first get the session number */
4413 if (name == NULL || sscanf (name, "send_rtcp_src_%u", &sessid) != 1)
4414 goto no_name;
4415
4416 /* get or create session */
4417 session = find_session_by_id (rtpbin, sessid);
4418 if (!session) {
4419 GST_DEBUG_OBJECT (rtpbin, "creating session %u", sessid);
4420 /* create session now */
4421 session = create_session (rtpbin, sessid);
4422 if (session == NULL)
4423 goto create_error;
4424 }
4425
4426 /* check if pad was requested */
4427 if (session->send_rtcp_src_ghost != NULL)
4428 return session->send_rtcp_src_ghost;
4429
4430 /* get rtcp_src pad and store */
4431 session->send_rtcp_src =
4432 gst_element_get_request_pad (session->session, "send_rtcp_src");
4433 if (session->send_rtcp_src == NULL)
4434 goto pad_failed;
4435
4436 GST_DEBUG_OBJECT (rtpbin, "getting RTCP encoder");
4437 encoder = session_request_element (session, SIGNAL_REQUEST_RTCP_ENCODER);
4438 if (encoder) {
4439 gchar *ename;
4440 GstPad *encsink;
4441 GstPadLinkReturn ret;
4442
4443 GST_DEBUG_OBJECT (rtpbin, "linking RTCP encoder");
4444
4445 ename = g_strdup_printf ("rtcp_src_%u", sessid);
4446 encsrc = gst_element_get_static_pad (encoder, ename);
4447 g_free (ename);
4448 if (encsrc == NULL)
4449 goto enc_src_failed;
4450
4451 ename = g_strdup_printf ("rtcp_sink_%u", sessid);
4452 encsink = gst_element_get_static_pad (encoder, ename);
4453 g_free (ename);
4454 if (encsink == NULL)
4455 goto enc_sink_failed;
4456
4457 ret = gst_pad_link (session->send_rtcp_src, encsink);
4458 gst_object_unref (encsink);
4459
4460 if (ret != GST_PAD_LINK_OK)
4461 goto enc_link_failed;
4462 } else {
4463 GST_DEBUG_OBJECT (rtpbin, "no RTCP encoder given");
4464 encsrc = gst_object_ref (session->send_rtcp_src);
4465 }
4466
4467 session->send_rtcp_src_ghost =
4468 gst_ghost_pad_new_from_template (name, encsrc, templ);
4469 gst_object_unref (encsrc);
4470 gst_pad_set_active (session->send_rtcp_src_ghost, TRUE);
4471 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtcp_src_ghost);
4472
4473 return session->send_rtcp_src_ghost;
4474
4475 /* ERRORS */
4476 no_name:
4477 {
4478 g_warning ("rtpbin: invalid name given");
4479 return NULL;
4480 }
4481 create_error:
4482 {
4483 /* create_session already warned */
4484 return NULL;
4485 }
4486 pad_failed:
4487 {
4488 g_warning ("rtpbin: failed to get rtcp pad for session %u", sessid);
4489 return NULL;
4490 }
4491 enc_src_failed:
4492 {
4493 g_warning ("rtpbin: failed to get encoder src pad for session %u", sessid);
4494 return NULL;
4495 }
4496 enc_sink_failed:
4497 {
4498 g_warning ("rtpbin: failed to get encoder sink pad for session %u", sessid);
4499 gst_object_unref (encsrc);
4500 return NULL;
4501 }
4502 enc_link_failed:
4503 {
4504 g_warning ("rtpbin: failed to link rtcp encoder for session %u", sessid);
4505 gst_object_unref (encsrc);
4506 return NULL;
4507 }
4508 }
4509
4510 static void
remove_rtcp(GstRtpBin * rtpbin,GstRtpBinSession * session)4511 remove_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session)
4512 {
4513 if (session->send_rtcp_src_ghost) {
4514 gst_pad_set_active (session->send_rtcp_src_ghost, FALSE);
4515 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
4516 session->send_rtcp_src_ghost);
4517 session->send_rtcp_src_ghost = NULL;
4518 }
4519 if (session->send_rtcp_src) {
4520 gst_element_release_request_pad (session->session, session->send_rtcp_src);
4521 gst_object_unref (session->send_rtcp_src);
4522 session->send_rtcp_src = NULL;
4523 }
4524 }
4525
4526 /* If the requested name is NULL we should create a name with
4527 * the session number assuming we want the lowest posible session
4528 * with a free pad like the template */
4529 static gchar *
gst_rtp_bin_get_free_pad_name(GstElement * element,GstPadTemplate * templ)4530 gst_rtp_bin_get_free_pad_name (GstElement * element, GstPadTemplate * templ)
4531 {
4532 gboolean name_found = FALSE;
4533 gint session = 0;
4534 GstIterator *pad_it = NULL;
4535 gchar *pad_name = NULL;
4536 GValue data = { 0, };
4537
4538 GST_DEBUG_OBJECT (element, "find a free pad name for template");
4539 while (!name_found) {
4540 gboolean done = FALSE;
4541
4542 g_free (pad_name);
4543 pad_name = g_strdup_printf (templ->name_template, session++);
4544 pad_it = gst_element_iterate_pads (GST_ELEMENT (element));
4545 name_found = TRUE;
4546 while (!done) {
4547 switch (gst_iterator_next (pad_it, &data)) {
4548 case GST_ITERATOR_OK:
4549 {
4550 GstPad *pad;
4551 gchar *name;
4552
4553 pad = g_value_get_object (&data);
4554 name = gst_pad_get_name (pad);
4555
4556 if (strcmp (name, pad_name) == 0) {
4557 done = TRUE;
4558 name_found = FALSE;
4559 }
4560 g_free (name);
4561 g_value_reset (&data);
4562 break;
4563 }
4564 case GST_ITERATOR_ERROR:
4565 case GST_ITERATOR_RESYNC:
4566 /* restart iteration */
4567 done = TRUE;
4568 name_found = FALSE;
4569 session = 0;
4570 break;
4571 case GST_ITERATOR_DONE:
4572 done = TRUE;
4573 break;
4574 }
4575 }
4576 g_value_unset (&data);
4577 gst_iterator_free (pad_it);
4578 }
4579
4580 GST_DEBUG_OBJECT (element, "free pad name found: '%s'", pad_name);
4581 return pad_name;
4582 }
4583
4584 /*
4585 */
4586 static GstPad *
gst_rtp_bin_request_new_pad(GstElement * element,GstPadTemplate * templ,const gchar * name,const GstCaps * caps)4587 gst_rtp_bin_request_new_pad (GstElement * element,
4588 GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
4589 {
4590 GstRtpBin *rtpbin;
4591 GstElementClass *klass;
4592 GstPad *result;
4593
4594 gchar *pad_name = NULL;
4595
4596 g_return_val_if_fail (templ != NULL, NULL);
4597 g_return_val_if_fail (GST_IS_RTP_BIN (element), NULL);
4598
4599 rtpbin = GST_RTP_BIN (element);
4600 klass = GST_ELEMENT_GET_CLASS (element);
4601
4602 GST_RTP_BIN_LOCK (rtpbin);
4603
4604 if (name == NULL) {
4605 /* use a free pad name */
4606 pad_name = gst_rtp_bin_get_free_pad_name (element, templ);
4607 } else {
4608 /* use the provided name */
4609 pad_name = g_strdup (name);
4610 }
4611
4612 GST_DEBUG_OBJECT (rtpbin, "Trying to request a pad with name %s", pad_name);
4613
4614 /* figure out the template */
4615 if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink_%u")) {
4616 result = create_recv_rtp (rtpbin, templ, pad_name);
4617 } else if (templ == gst_element_class_get_pad_template (klass,
4618 "recv_rtcp_sink_%u")) {
4619 result = create_recv_rtcp (rtpbin, templ, pad_name);
4620 } else if (templ == gst_element_class_get_pad_template (klass,
4621 "send_rtp_sink_%u")) {
4622 result = create_send_rtp (rtpbin, templ, pad_name);
4623 } else if (templ == gst_element_class_get_pad_template (klass,
4624 "send_rtcp_src_%u")) {
4625 result = create_send_rtcp (rtpbin, templ, pad_name);
4626 } else
4627 goto wrong_template;
4628
4629 g_free (pad_name);
4630 GST_RTP_BIN_UNLOCK (rtpbin);
4631
4632 return result;
4633
4634 /* ERRORS */
4635 wrong_template:
4636 {
4637 g_free (pad_name);
4638 GST_RTP_BIN_UNLOCK (rtpbin);
4639 g_warning ("rtpbin: this is not our template");
4640 return NULL;
4641 }
4642 }
4643
4644 static void
gst_rtp_bin_release_pad(GstElement * element,GstPad * pad)4645 gst_rtp_bin_release_pad (GstElement * element, GstPad * pad)
4646 {
4647 GstRtpBinSession *session;
4648 GstRtpBin *rtpbin;
4649
4650 g_return_if_fail (GST_IS_GHOST_PAD (pad));
4651 g_return_if_fail (GST_IS_RTP_BIN (element));
4652
4653 rtpbin = GST_RTP_BIN (element);
4654
4655 GST_RTP_BIN_LOCK (rtpbin);
4656 GST_DEBUG_OBJECT (rtpbin, "Trying to release pad %s:%s",
4657 GST_DEBUG_PAD_NAME (pad));
4658
4659 if (!(session = find_session_by_pad (rtpbin, pad)))
4660 goto unknown_pad;
4661
4662 if (session->recv_rtp_sink_ghost == pad) {
4663 remove_recv_rtp (rtpbin, session);
4664 } else if (session->recv_rtcp_sink_ghost == pad) {
4665 remove_recv_rtcp (rtpbin, session);
4666 } else if (session->send_rtp_sink_ghost == pad) {
4667 remove_send_rtp (rtpbin, session);
4668 } else if (session->send_rtcp_src_ghost == pad) {
4669 remove_rtcp (rtpbin, session);
4670 }
4671
4672 /* no more request pads, free the complete session */
4673 if (session->recv_rtp_sink_ghost == NULL
4674 && session->recv_rtcp_sink_ghost == NULL
4675 && session->send_rtp_sink_ghost == NULL
4676 && session->send_rtcp_src_ghost == NULL) {
4677 GST_DEBUG_OBJECT (rtpbin, "no more pads for session %p", session);
4678 rtpbin->sessions = g_slist_remove (rtpbin->sessions, session);
4679 free_session (session, rtpbin);
4680 }
4681 GST_RTP_BIN_UNLOCK (rtpbin);
4682
4683 return;
4684
4685 /* ERROR */
4686 unknown_pad:
4687 {
4688 GST_RTP_BIN_UNLOCK (rtpbin);
4689 g_warning ("rtpbin: %s:%s is not one of our request pads",
4690 GST_DEBUG_PAD_NAME (pad));
4691 return;
4692 }
4693 }
4694