1 /* RTP muxer element for GStreamer
2 *
3 * gstrtpmux.c:
4 *
5 * Copyright (C) <2007-2010> Nokia Corporation.
6 * Contact: Zeeshan Ali <zeeshan.ali@nokia.com>
7 * Copyright (C) <2007-2010> Collabora Ltd
8 * Contact: Olivier Crete <olivier.crete@collabora.co.uk>
9 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
10 * 2000,2005 Wim Taymans <wim@fluendo.com>
11 *
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Library General Public
14 * License as published by the Free Software Foundation; either
15 * version 2 of the License, or (at your option) any later version.
16 *
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Library General Public License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public
23 * License along with this library; if not, write to the
24 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
25 * Boston, MA 02110-1301, USA.
26 */
27
28 /**
29 * SECTION:element-rtpmux
30 * @title: rtpmux
31 * @see_also: rtpdtmfmux
32 *
33 * The rtp muxer takes multiple RTP streams having the same clock-rate and
34 * muxes into a single stream with a single SSRC.
35 *
36 * ## Example pipelines
37 * |[
38 * gst-launch-1.0 rtpmux name=mux ! udpsink host=127.0.0.1 port=8888 \
39 * alsasrc ! alawenc ! rtppcmapay ! \
40 * application/x-rtp, payload=8, rate=8000 ! mux.sink_0 \
41 * audiotestsrc is-live=1 ! \
42 * mulawenc ! rtppcmupay ! \
43 * application/x-rtp, payload=0, rate=8000 ! mux.sink_1
44 * ]|
45 * In this example, an audio stream is captured from ALSA and another is
46 * generated, both are encoded into different payload types and muxed together
47 * so they can be sent on the same port.
48 *
49 */
50
51 #ifdef HAVE_CONFIG_H
52 #include "config.h"
53 #endif
54
55 #include <gst/gst.h>
56 #include <gst/rtp/gstrtpbuffer.h>
57 #include <string.h>
58
59 #include "gstrtpmux.h"
60
61 GST_DEBUG_CATEGORY_STATIC (gst_rtp_mux_debug);
62 #define GST_CAT_DEFAULT gst_rtp_mux_debug
63
64 enum
65 {
66 PROP_0,
67 PROP_TIMESTAMP_OFFSET,
68 PROP_SEQNUM_OFFSET,
69 PROP_SEQNUM,
70 PROP_SSRC
71 };
72
73 #define DEFAULT_TIMESTAMP_OFFSET -1
74 #define DEFAULT_SEQNUM_OFFSET -1
75 #define DEFAULT_SSRC -1
76
77 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
78 GST_PAD_SRC,
79 GST_PAD_ALWAYS,
80 GST_STATIC_CAPS ("application/x-rtp")
81 );
82
83 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%u",
84 GST_PAD_SINK,
85 GST_PAD_REQUEST,
86 GST_STATIC_CAPS ("application/x-rtp")
87 );
88
89 static GstPad *gst_rtp_mux_request_new_pad (GstElement * element,
90 GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
91 static void gst_rtp_mux_release_pad (GstElement * element, GstPad * pad);
92 static GstFlowReturn gst_rtp_mux_chain (GstPad * pad, GstObject * parent,
93 GstBuffer * buffer);
94 static GstFlowReturn gst_rtp_mux_chain_list (GstPad * pad, GstObject * parent,
95 GstBufferList * bufferlist);
96 static gboolean gst_rtp_mux_setcaps (GstPad * pad, GstRTPMux * rtp_mux,
97 GstCaps * caps);
98 static gboolean gst_rtp_mux_sink_event (GstPad * pad, GstObject * parent,
99 GstEvent * event);
100 static gboolean gst_rtp_mux_sink_query (GstPad * pad, GstObject * parent,
101 GstQuery * query);
102
103 static GstStateChangeReturn gst_rtp_mux_change_state (GstElement *
104 element, GstStateChange transition);
105
106 static void gst_rtp_mux_set_property (GObject * object, guint prop_id,
107 const GValue * value, GParamSpec * pspec);
108 static void gst_rtp_mux_get_property (GObject * object, guint prop_id,
109 GValue * value, GParamSpec * pspec);
110 static void gst_rtp_mux_dispose (GObject * object);
111
112 static gboolean gst_rtp_mux_src_event_real (GstRTPMux * rtp_mux,
113 GstEvent * event);
114
115 G_DEFINE_TYPE_WITH_CODE (GstRTPMux, gst_rtp_mux, GST_TYPE_ELEMENT,
116 GST_DEBUG_CATEGORY_INIT (gst_rtp_mux_debug, "rtpmux", 0, "rtp muxer"));
117 GST_ELEMENT_REGISTER_DEFINE (rtpmux, "rtpmux", GST_RANK_NONE, GST_TYPE_RTP_MUX);
118
119 static void
gst_rtp_mux_class_init(GstRTPMuxClass * klass)120 gst_rtp_mux_class_init (GstRTPMuxClass * klass)
121 {
122 GObjectClass *gobject_class;
123 GstElementClass *gstelement_class;
124
125 gobject_class = (GObjectClass *) klass;
126 gstelement_class = (GstElementClass *) klass;
127
128
129 gst_element_class_add_static_pad_template (gstelement_class, &src_factory);
130 gst_element_class_add_static_pad_template (gstelement_class, &sink_factory);
131
132 gst_element_class_set_static_metadata (gstelement_class, "RTP muxer",
133 "Codec/Muxer",
134 "multiplex N rtp streams into one", "Zeeshan Ali <first.last@nokia.com>");
135
136 gobject_class->get_property = gst_rtp_mux_get_property;
137 gobject_class->set_property = gst_rtp_mux_set_property;
138 gobject_class->dispose = gst_rtp_mux_dispose;
139
140 klass->src_event = gst_rtp_mux_src_event_real;
141
142 g_object_class_install_property (G_OBJECT_CLASS (klass),
143 PROP_TIMESTAMP_OFFSET, g_param_spec_int ("timestamp-offset",
144 "Timestamp Offset",
145 "Offset to add to all outgoing timestamps (-1 = random)", -1,
146 G_MAXINT, DEFAULT_TIMESTAMP_OFFSET,
147 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
148 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM_OFFSET,
149 g_param_spec_int ("seqnum-offset", "Sequence number Offset",
150 "Offset to add to all outgoing seqnum (-1 = random)", -1, G_MAXINT,
151 DEFAULT_SEQNUM_OFFSET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
152 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM,
153 g_param_spec_uint ("seqnum", "Sequence number",
154 "The RTP sequence number of the last processed packet",
155 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
156 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SSRC,
157 g_param_spec_uint ("ssrc", "SSRC",
158 "The SSRC of the packets (default == random)",
159 0, G_MAXUINT, DEFAULT_SSRC,
160 GST_PARAM_MUTABLE_PLAYING | G_PARAM_READWRITE |
161 G_PARAM_STATIC_STRINGS));
162
163 gstelement_class->request_new_pad =
164 GST_DEBUG_FUNCPTR (gst_rtp_mux_request_new_pad);
165 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_mux_release_pad);
166 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_mux_change_state);
167 }
168
169 static void
gst_rtp_mux_dispose(GObject * object)170 gst_rtp_mux_dispose (GObject * object)
171 {
172 GstRTPMux *rtp_mux = GST_RTP_MUX (object);
173 GList *item;
174
175 g_clear_object (&rtp_mux->last_pad);
176
177 restart:
178 for (item = GST_ELEMENT_PADS (object); item; item = g_list_next (item)) {
179 GstPad *pad = GST_PAD (item->data);
180 if (GST_PAD_IS_SINK (pad)) {
181 gst_element_release_request_pad (GST_ELEMENT (object), pad);
182 goto restart;
183 }
184 }
185
186 G_OBJECT_CLASS (gst_rtp_mux_parent_class)->dispose (object);
187 }
188
189 static gboolean
gst_rtp_mux_src_event(GstPad * pad,GstObject * parent,GstEvent * event)190 gst_rtp_mux_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
191 {
192 GstRTPMux *rtp_mux = GST_RTP_MUX (parent);
193 GstRTPMuxClass *klass;
194 gboolean ret;
195
196 klass = GST_RTP_MUX_GET_CLASS (rtp_mux);
197
198 ret = klass->src_event (rtp_mux, event);
199
200 return ret;
201 }
202
203 static gboolean
gst_rtp_mux_src_event_real(GstRTPMux * rtp_mux,GstEvent * event)204 gst_rtp_mux_src_event_real (GstRTPMux * rtp_mux, GstEvent * event)
205 {
206 switch (GST_EVENT_TYPE (event)) {
207 case GST_EVENT_CUSTOM_UPSTREAM:
208 {
209 const GstStructure *s = gst_event_get_structure (event);
210
211 if (gst_structure_has_name (s, "GstRTPCollision")) {
212 guint ssrc = 0;
213
214 if (!gst_structure_get_uint (s, "ssrc", &ssrc))
215 ssrc = -1;
216
217 GST_DEBUG_OBJECT (rtp_mux, "collided ssrc: %x", ssrc);
218
219 /* choose another ssrc for our stream */
220 GST_OBJECT_LOCK (rtp_mux);
221 if (ssrc == rtp_mux->current_ssrc) {
222 GstCaps *caps;
223 guint suggested_ssrc = 0;
224 guint32 new_ssrc;
225
226 if (gst_structure_get_uint (s, "suggested-ssrc", &suggested_ssrc))
227 rtp_mux->current_ssrc = suggested_ssrc;
228
229 while (ssrc == rtp_mux->current_ssrc)
230 rtp_mux->current_ssrc = g_random_int ();
231
232 new_ssrc = rtp_mux->current_ssrc;
233 GST_INFO_OBJECT (rtp_mux, "New ssrc after collision %x (was: %x)",
234 new_ssrc, ssrc);
235 GST_OBJECT_UNLOCK (rtp_mux);
236
237 caps = gst_pad_get_current_caps (rtp_mux->srcpad);
238 caps = gst_caps_make_writable (caps);
239 gst_caps_set_simple (caps, "ssrc", G_TYPE_UINT, new_ssrc, NULL);
240 gst_pad_set_caps (rtp_mux->srcpad, caps);
241 gst_caps_unref (caps);
242 } else {
243 GST_OBJECT_UNLOCK (rtp_mux);
244 }
245 }
246 break;
247 }
248 default:
249 break;
250 }
251
252
253 return gst_pad_event_default (rtp_mux->srcpad, GST_OBJECT (rtp_mux), event);
254 }
255
256 static void
gst_rtp_mux_init(GstRTPMux * rtp_mux)257 gst_rtp_mux_init (GstRTPMux * rtp_mux)
258 {
259 GstElementClass *klass = GST_ELEMENT_GET_CLASS (rtp_mux);
260
261 rtp_mux->srcpad =
262 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
263 "src"), "src");
264 gst_pad_set_event_function (rtp_mux->srcpad,
265 GST_DEBUG_FUNCPTR (gst_rtp_mux_src_event));
266 gst_pad_use_fixed_caps (rtp_mux->srcpad);
267 gst_element_add_pad (GST_ELEMENT (rtp_mux), rtp_mux->srcpad);
268
269 rtp_mux->ssrc = DEFAULT_SSRC;
270 rtp_mux->current_ssrc = DEFAULT_SSRC;
271 rtp_mux->ts_offset = DEFAULT_TIMESTAMP_OFFSET;
272 rtp_mux->seqnum_offset = DEFAULT_SEQNUM_OFFSET;
273
274 rtp_mux->last_stop = GST_CLOCK_TIME_NONE;
275 }
276
277 static void
gst_rtp_mux_setup_sinkpad(GstRTPMux * rtp_mux,GstPad * sinkpad)278 gst_rtp_mux_setup_sinkpad (GstRTPMux * rtp_mux, GstPad * sinkpad)
279 {
280 GstRTPMuxPadPrivate *padpriv = g_slice_new0 (GstRTPMuxPadPrivate);
281
282 /* setup some pad functions */
283 gst_pad_set_chain_function (sinkpad, GST_DEBUG_FUNCPTR (gst_rtp_mux_chain));
284 gst_pad_set_chain_list_function (sinkpad,
285 GST_DEBUG_FUNCPTR (gst_rtp_mux_chain_list));
286 gst_pad_set_event_function (sinkpad,
287 GST_DEBUG_FUNCPTR (gst_rtp_mux_sink_event));
288 gst_pad_set_query_function (sinkpad,
289 GST_DEBUG_FUNCPTR (gst_rtp_mux_sink_query));
290
291
292 gst_segment_init (&padpriv->segment, GST_FORMAT_UNDEFINED);
293
294 gst_pad_set_element_private (sinkpad, padpriv);
295
296 gst_pad_set_active (sinkpad, TRUE);
297 gst_element_add_pad (GST_ELEMENT (rtp_mux), sinkpad);
298 }
299
300 static GstPad *
gst_rtp_mux_request_new_pad(GstElement * element,GstPadTemplate * templ,const gchar * req_name,const GstCaps * caps)301 gst_rtp_mux_request_new_pad (GstElement * element,
302 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
303 {
304 GstRTPMux *rtp_mux;
305 GstPad *newpad;
306
307 g_return_val_if_fail (templ != NULL, NULL);
308 g_return_val_if_fail (GST_IS_RTP_MUX (element), NULL);
309
310 rtp_mux = GST_RTP_MUX (element);
311
312 if (templ->direction != GST_PAD_SINK) {
313 GST_WARNING_OBJECT (rtp_mux, "request pad that is not a SINK pad");
314 return NULL;
315 }
316
317 newpad = gst_pad_new_from_template (templ, req_name);
318 if (newpad)
319 gst_rtp_mux_setup_sinkpad (rtp_mux, newpad);
320 else
321 GST_WARNING_OBJECT (rtp_mux, "failed to create request pad");
322
323 return newpad;
324 }
325
326 static void
gst_rtp_mux_release_pad(GstElement * element,GstPad * pad)327 gst_rtp_mux_release_pad (GstElement * element, GstPad * pad)
328 {
329 GstRTPMuxPadPrivate *padpriv;
330
331 GST_OBJECT_LOCK (element);
332 padpriv = gst_pad_get_element_private (pad);
333 gst_pad_set_element_private (pad, NULL);
334 GST_OBJECT_UNLOCK (element);
335
336 gst_element_remove_pad (element, pad);
337
338 if (padpriv) {
339 g_slice_free (GstRTPMuxPadPrivate, padpriv);
340 }
341 }
342
343 /* Put our own timestamp-offset on the buffer */
344 static void
gst_rtp_mux_readjust_rtp_timestamp_locked(GstRTPMux * rtp_mux,GstRTPMuxPadPrivate * padpriv,GstRTPBuffer * rtpbuffer)345 gst_rtp_mux_readjust_rtp_timestamp_locked (GstRTPMux * rtp_mux,
346 GstRTPMuxPadPrivate * padpriv, GstRTPBuffer * rtpbuffer)
347 {
348 guint32 ts;
349 guint32 sink_ts_base = 0;
350
351 if (padpriv && padpriv->have_timestamp_offset)
352 sink_ts_base = padpriv->timestamp_offset;
353
354 ts = gst_rtp_buffer_get_timestamp (rtpbuffer) - sink_ts_base +
355 rtp_mux->ts_base;
356 GST_LOG_OBJECT (rtp_mux, "Re-adjusting RTP ts %u to %u",
357 gst_rtp_buffer_get_timestamp (rtpbuffer), ts);
358 gst_rtp_buffer_set_timestamp (rtpbuffer, ts);
359 }
360
361 static gboolean
process_buffer_locked(GstRTPMux * rtp_mux,GstRTPMuxPadPrivate * padpriv,GstRTPBuffer * rtpbuffer)362 process_buffer_locked (GstRTPMux * rtp_mux, GstRTPMuxPadPrivate * padpriv,
363 GstRTPBuffer * rtpbuffer)
364 {
365 GstRTPMuxClass *klass = GST_RTP_MUX_GET_CLASS (rtp_mux);
366
367 if (klass->accept_buffer_locked)
368 if (!klass->accept_buffer_locked (rtp_mux, padpriv, rtpbuffer))
369 return FALSE;
370
371 rtp_mux->seqnum++;
372 gst_rtp_buffer_set_seq (rtpbuffer, rtp_mux->seqnum);
373
374 gst_rtp_buffer_set_ssrc (rtpbuffer, rtp_mux->current_ssrc);
375 gst_rtp_mux_readjust_rtp_timestamp_locked (rtp_mux, padpriv, rtpbuffer);
376 GST_LOG_OBJECT (rtp_mux,
377 "Pushing packet size %" G_GSIZE_FORMAT ", seq=%d, ts=%u, ssrc=%x",
378 rtpbuffer->map[0].size, rtp_mux->seqnum,
379 gst_rtp_buffer_get_timestamp (rtpbuffer), rtp_mux->current_ssrc);
380
381 if (padpriv) {
382 if (padpriv->segment.format == GST_FORMAT_TIME) {
383 GST_BUFFER_PTS (rtpbuffer->buffer) =
384 gst_segment_to_running_time (&padpriv->segment, GST_FORMAT_TIME,
385 GST_BUFFER_PTS (rtpbuffer->buffer));
386 GST_BUFFER_DTS (rtpbuffer->buffer) =
387 gst_segment_to_running_time (&padpriv->segment, GST_FORMAT_TIME,
388 GST_BUFFER_DTS (rtpbuffer->buffer));
389 }
390 }
391
392 return TRUE;
393 }
394
395 struct BufferListData
396 {
397 GstRTPMux *rtp_mux;
398 GstRTPMuxPadPrivate *padpriv;
399 gboolean drop;
400 };
401
402 static gboolean
process_list_item(GstBuffer ** buffer,guint idx,gpointer user_data)403 process_list_item (GstBuffer ** buffer, guint idx, gpointer user_data)
404 {
405 struct BufferListData *bd = user_data;
406 GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
407
408 *buffer = gst_buffer_make_writable (*buffer);
409
410 gst_rtp_buffer_map (*buffer, GST_MAP_READWRITE, &rtpbuffer);
411
412 bd->drop = !process_buffer_locked (bd->rtp_mux, bd->padpriv, &rtpbuffer);
413
414 gst_rtp_buffer_unmap (&rtpbuffer);
415
416 if (bd->drop)
417 return FALSE;
418
419 if (GST_BUFFER_DURATION_IS_VALID (*buffer) &&
420 GST_BUFFER_PTS_IS_VALID (*buffer))
421 bd->rtp_mux->last_stop = GST_BUFFER_PTS (*buffer) +
422 GST_BUFFER_DURATION (*buffer);
423 else
424 bd->rtp_mux->last_stop = GST_CLOCK_TIME_NONE;
425
426 return TRUE;
427 }
428
429 static gboolean resend_events (GstPad * pad, GstEvent ** event,
430 gpointer user_data);
431
432 static GstFlowReturn
gst_rtp_mux_chain_list(GstPad * pad,GstObject * parent,GstBufferList * bufferlist)433 gst_rtp_mux_chain_list (GstPad * pad, GstObject * parent,
434 GstBufferList * bufferlist)
435 {
436 GstRTPMux *rtp_mux;
437 GstFlowReturn ret;
438 GstRTPMuxPadPrivate *padpriv;
439 gboolean changed = FALSE;
440 struct BufferListData bd;
441
442 rtp_mux = GST_RTP_MUX (parent);
443
444 if (gst_pad_check_reconfigure (rtp_mux->srcpad)) {
445 GstCaps *current_caps = gst_pad_get_current_caps (pad);
446
447 if (!gst_rtp_mux_setcaps (pad, rtp_mux, current_caps)) {
448 gst_pad_mark_reconfigure (rtp_mux->srcpad);
449 if (GST_PAD_IS_FLUSHING (rtp_mux->srcpad))
450 ret = GST_FLOW_FLUSHING;
451 else
452 ret = GST_FLOW_NOT_NEGOTIATED;
453 gst_buffer_list_unref (bufferlist);
454 goto out;
455 }
456 gst_caps_unref (current_caps);
457 }
458
459 GST_OBJECT_LOCK (rtp_mux);
460
461 padpriv = gst_pad_get_element_private (pad);
462 if (!padpriv) {
463 GST_OBJECT_UNLOCK (rtp_mux);
464 ret = GST_FLOW_NOT_LINKED;
465 gst_buffer_list_unref (bufferlist);
466 goto out;
467 }
468
469 bd.rtp_mux = rtp_mux;
470 bd.padpriv = padpriv;
471 bd.drop = FALSE;
472
473 bufferlist = gst_buffer_list_make_writable (bufferlist);
474 gst_buffer_list_foreach (bufferlist, process_list_item, &bd);
475
476 if (!bd.drop && pad != rtp_mux->last_pad) {
477 changed = TRUE;
478 g_clear_object (&rtp_mux->last_pad);
479 rtp_mux->last_pad = g_object_ref (pad);
480 }
481
482 GST_OBJECT_UNLOCK (rtp_mux);
483
484 if (changed)
485 gst_pad_sticky_events_foreach (pad, resend_events, rtp_mux);
486
487 if (bd.drop) {
488 gst_buffer_list_unref (bufferlist);
489 ret = GST_FLOW_OK;
490 } else {
491 ret = gst_pad_push_list (rtp_mux->srcpad, bufferlist);
492 }
493
494 out:
495
496 return ret;
497 }
498
499 static gboolean
resend_events(GstPad * pad,GstEvent ** event,gpointer user_data)500 resend_events (GstPad * pad, GstEvent ** event, gpointer user_data)
501 {
502 GstRTPMux *rtp_mux = user_data;
503
504 if (GST_EVENT_TYPE (*event) == GST_EVENT_CAPS) {
505 GstCaps *caps;
506
507 gst_event_parse_caps (*event, &caps);
508 gst_rtp_mux_setcaps (pad, rtp_mux, caps);
509 } else if (GST_EVENT_TYPE (*event) == GST_EVENT_SEGMENT) {
510 GstSegment new_segment;
511 gst_segment_init (&new_segment, GST_FORMAT_TIME);
512 gst_pad_push_event (rtp_mux->srcpad, gst_event_new_segment (&new_segment));
513 } else {
514 gst_pad_push_event (rtp_mux->srcpad, gst_event_ref (*event));
515 }
516
517 return TRUE;
518 }
519
520 static GstFlowReturn
gst_rtp_mux_chain(GstPad * pad,GstObject * parent,GstBuffer * buffer)521 gst_rtp_mux_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
522 {
523 GstRTPMux *rtp_mux;
524 GstFlowReturn ret;
525 GstRTPMuxPadPrivate *padpriv;
526 gboolean drop;
527 gboolean changed = FALSE;
528 GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
529
530 rtp_mux = GST_RTP_MUX (parent);
531
532 if (gst_pad_check_reconfigure (rtp_mux->srcpad)) {
533 GstCaps *current_caps = gst_pad_get_current_caps (pad);
534
535 if (!gst_rtp_mux_setcaps (pad, rtp_mux, current_caps)) {
536 gst_pad_mark_reconfigure (rtp_mux->srcpad);
537 if (GST_PAD_IS_FLUSHING (rtp_mux->srcpad))
538 ret = GST_FLOW_FLUSHING;
539 else
540 ret = GST_FLOW_NOT_NEGOTIATED;
541 gst_buffer_unref (buffer);
542 goto out;
543 }
544 gst_caps_unref (current_caps);
545 }
546
547 GST_OBJECT_LOCK (rtp_mux);
548 padpriv = gst_pad_get_element_private (pad);
549
550 if (!padpriv) {
551 GST_OBJECT_UNLOCK (rtp_mux);
552 gst_buffer_unref (buffer);
553 return GST_FLOW_NOT_LINKED;
554 }
555
556 buffer = gst_buffer_make_writable (buffer);
557
558 if (!gst_rtp_buffer_map (buffer, GST_MAP_READWRITE, &rtpbuffer)) {
559 GST_OBJECT_UNLOCK (rtp_mux);
560 gst_buffer_unref (buffer);
561 GST_ERROR_OBJECT (rtp_mux, "Invalid RTP buffer");
562 return GST_FLOW_ERROR;
563 }
564
565 drop = !process_buffer_locked (rtp_mux, padpriv, &rtpbuffer);
566
567 gst_rtp_buffer_unmap (&rtpbuffer);
568
569 if (!drop) {
570 if (pad != rtp_mux->last_pad) {
571 changed = TRUE;
572 g_clear_object (&rtp_mux->last_pad);
573 rtp_mux->last_pad = g_object_ref (pad);
574 }
575
576 if (GST_BUFFER_DURATION_IS_VALID (buffer) &&
577 GST_BUFFER_PTS_IS_VALID (buffer))
578 rtp_mux->last_stop = GST_BUFFER_PTS (buffer) +
579 GST_BUFFER_DURATION (buffer);
580 else
581 rtp_mux->last_stop = GST_CLOCK_TIME_NONE;
582 }
583
584 GST_OBJECT_UNLOCK (rtp_mux);
585
586 if (changed)
587 gst_pad_sticky_events_foreach (pad, resend_events, rtp_mux);
588
589 if (drop) {
590 gst_buffer_unref (buffer);
591 ret = GST_FLOW_OK;
592 } else {
593 ret = gst_pad_push (rtp_mux->srcpad, buffer);
594 }
595
596 out:
597 return ret;
598 }
599
600 static gboolean
gst_rtp_mux_setcaps(GstPad * pad,GstRTPMux * rtp_mux,GstCaps * caps)601 gst_rtp_mux_setcaps (GstPad * pad, GstRTPMux * rtp_mux, GstCaps * caps)
602 {
603 GstStructure *structure;
604 gboolean ret = FALSE;
605 GstRTPMuxPadPrivate *padpriv;
606 GstCaps *peercaps;
607
608 if (caps == NULL)
609 return FALSE;
610
611 if (!gst_caps_is_fixed (caps))
612 return FALSE;
613
614 peercaps = gst_pad_peer_query_caps (rtp_mux->srcpad, NULL);
615 if (peercaps) {
616 GstCaps *tcaps, *othercaps;;
617 tcaps = gst_pad_get_pad_template_caps (pad);
618 othercaps = gst_caps_intersect_full (peercaps, tcaps,
619 GST_CAPS_INTERSECT_FIRST);
620
621 if (gst_caps_get_size (othercaps) > 0) {
622 structure = gst_caps_get_structure (othercaps, 0);
623 GST_OBJECT_LOCK (rtp_mux);
624 if (gst_structure_get_uint (structure, "ssrc", &rtp_mux->current_ssrc)) {
625 GST_INFO_OBJECT (pad, "Use downstream ssrc: %x", rtp_mux->current_ssrc);
626 rtp_mux->have_ssrc = TRUE;
627 }
628 if (gst_structure_get_uint (structure,
629 "timestamp-offset", &rtp_mux->ts_base)) {
630 GST_INFO_OBJECT (pad, "Use downstream timestamp-offset: %u",
631 rtp_mux->ts_base);
632 }
633 GST_OBJECT_UNLOCK (rtp_mux);
634 }
635
636 gst_caps_unref (othercaps);
637
638 gst_caps_unref (peercaps);
639 gst_caps_unref (tcaps);
640 }
641
642 structure = gst_caps_get_structure (caps, 0);
643
644 if (!structure)
645 return FALSE;
646
647 GST_OBJECT_LOCK (rtp_mux);
648 padpriv = gst_pad_get_element_private (pad);
649 if (padpriv &&
650 gst_structure_get_uint (structure, "timestamp-offset",
651 &padpriv->timestamp_offset)) {
652 padpriv->have_timestamp_offset = TRUE;
653 }
654
655 caps = gst_caps_copy (caps);
656
657 /* if we don't have a specified ssrc, first try to take one from the caps,
658 and if that fails, generate one */
659 if (rtp_mux->ssrc == DEFAULT_SSRC) {
660 if (rtp_mux->current_ssrc == DEFAULT_SSRC) {
661 if (!gst_structure_get_uint (structure, "ssrc", &rtp_mux->current_ssrc)) {
662 rtp_mux->current_ssrc = g_random_int ();
663 GST_INFO_OBJECT (rtp_mux, "Set random ssrc %x", rtp_mux->current_ssrc);
664 }
665 }
666 } else {
667 rtp_mux->current_ssrc = rtp_mux->ssrc;
668 GST_INFO_OBJECT (rtp_mux, "Set ssrc %x", rtp_mux->current_ssrc);
669 }
670
671 gst_caps_set_simple (caps,
672 "timestamp-offset", G_TYPE_UINT, rtp_mux->ts_base,
673 "seqnum-offset", G_TYPE_UINT, rtp_mux->seqnum_base,
674 "ssrc", G_TYPE_UINT, rtp_mux->current_ssrc, NULL);
675
676 GST_OBJECT_UNLOCK (rtp_mux);
677
678 if (rtp_mux->send_stream_start) {
679 gchar s_id[32];
680
681 /* stream-start (FIXME: create id based on input ids) */
682 g_snprintf (s_id, sizeof (s_id), "interleave-%08x", g_random_int ());
683 gst_pad_push_event (rtp_mux->srcpad, gst_event_new_stream_start (s_id));
684
685 rtp_mux->send_stream_start = FALSE;
686 }
687
688 GST_DEBUG_OBJECT (rtp_mux,
689 "setting caps %" GST_PTR_FORMAT " on src pad..", caps);
690 ret = gst_pad_set_caps (rtp_mux->srcpad, caps);
691
692
693 gst_caps_unref (caps);
694
695 return ret;
696 }
697
698 static void
clear_caps(GstCaps * caps,gboolean only_clock_rate)699 clear_caps (GstCaps * caps, gboolean only_clock_rate)
700 {
701 gint i, j;
702
703 /* Lets only match on the clock-rate */
704 for (i = 0; i < gst_caps_get_size (caps); i++) {
705 GstStructure *s = gst_caps_get_structure (caps, i);
706
707 for (j = 0; j < gst_structure_n_fields (s); j++) {
708 const gchar *name = gst_structure_nth_field_name (s, j);
709
710 if (strcmp (name, "clock-rate") && (only_clock_rate ||
711 (strcmp (name, "ssrc")))) {
712 gst_structure_remove_field (s, name);
713 j--;
714 }
715 }
716 }
717 }
718
719 static gboolean
same_clock_rate_fold(const GValue * item,GValue * ret,gpointer user_data)720 same_clock_rate_fold (const GValue * item, GValue * ret, gpointer user_data)
721 {
722 GstPad *mypad = user_data;
723 GstPad *pad = g_value_get_object (item);
724 GstCaps *peercaps;
725 GstCaps *accumcaps;
726
727 if (pad == mypad)
728 return TRUE;
729
730 accumcaps = g_value_get_boxed (ret);
731 peercaps = gst_pad_peer_query_caps (pad, accumcaps);
732 if (!peercaps) {
733 g_warning ("no peercaps");
734 return TRUE;
735 }
736 peercaps = gst_caps_make_writable (peercaps);
737 clear_caps (peercaps, TRUE);
738
739 g_value_take_boxed (ret, peercaps);
740
741 return !gst_caps_is_empty (peercaps);
742 }
743
744 static GstCaps *
gst_rtp_mux_getcaps(GstPad * pad,GstRTPMux * mux,GstCaps * filter)745 gst_rtp_mux_getcaps (GstPad * pad, GstRTPMux * mux, GstCaps * filter)
746 {
747 GstCaps *caps = NULL;
748 GstIterator *iter = NULL;
749 GValue v = { 0 };
750 GstIteratorResult res;
751 GstCaps *peercaps;
752 GstCaps *othercaps;
753 GstCaps *tcaps;
754 const GstStructure *structure;
755
756 peercaps = gst_pad_peer_query_caps (mux->srcpad, NULL);
757
758 if (peercaps) {
759 tcaps = gst_pad_get_pad_template_caps (pad);
760 othercaps = gst_caps_intersect_full (peercaps, tcaps,
761 GST_CAPS_INTERSECT_FIRST);
762 gst_caps_unref (peercaps);
763 } else {
764 tcaps = gst_pad_get_pad_template_caps (mux->srcpad);
765 if (filter)
766 othercaps = gst_caps_intersect_full (filter, tcaps,
767 GST_CAPS_INTERSECT_FIRST);
768 else
769 othercaps = gst_caps_copy (tcaps);
770 }
771 gst_caps_unref (tcaps);
772
773 GST_LOG_OBJECT (pad, "Intersected srcpad-peercaps and template caps: %"
774 GST_PTR_FORMAT, othercaps);
775
776 structure = gst_caps_get_structure (othercaps, 0);
777 if (mux->ssrc == DEFAULT_SSRC) {
778 if (gst_structure_get_uint (structure, "ssrc", &mux->current_ssrc))
779 GST_DEBUG_OBJECT (pad, "Use downstream ssrc: %x", mux->current_ssrc);
780 }
781
782 clear_caps (othercaps, TRUE);
783
784 g_value_init (&v, GST_TYPE_CAPS);
785
786 iter = gst_element_iterate_sink_pads (GST_ELEMENT (mux));
787 do {
788 gst_value_set_caps (&v, othercaps);
789 res = gst_iterator_fold (iter, same_clock_rate_fold, &v, pad);
790 gst_iterator_resync (iter);
791 } while (res == GST_ITERATOR_RESYNC);
792 gst_iterator_free (iter);
793
794 caps = gst_caps_intersect ((GstCaps *) gst_value_get_caps (&v), othercaps);
795
796 g_value_unset (&v);
797 gst_caps_unref (othercaps);
798
799 if (res == GST_ITERATOR_ERROR) {
800 gst_caps_unref (caps);
801 caps = gst_caps_new_empty ();
802 }
803
804
805 return caps;
806 }
807
808 static gboolean
gst_rtp_mux_sink_query(GstPad * pad,GstObject * parent,GstQuery * query)809 gst_rtp_mux_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
810 {
811 GstRTPMux *mux = GST_RTP_MUX (parent);
812 gboolean res = FALSE;
813
814 switch (GST_QUERY_TYPE (query)) {
815 case GST_QUERY_CAPS:
816 {
817 GstCaps *filter, *caps;
818
819 gst_query_parse_caps (query, &filter);
820 GST_LOG_OBJECT (pad, "Received caps-query with filter-caps: %"
821 GST_PTR_FORMAT, filter);
822 caps = gst_rtp_mux_getcaps (pad, mux, filter);
823 gst_query_set_caps_result (query, caps);
824 GST_LOG_OBJECT (mux, "Answering caps-query with caps: %"
825 GST_PTR_FORMAT, caps);
826 gst_caps_unref (caps);
827 res = TRUE;
828 break;
829 }
830 default:
831 res = gst_pad_query_default (pad, parent, query);
832 break;
833 }
834
835 return res;
836 }
837
838 static void
gst_rtp_mux_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)839 gst_rtp_mux_get_property (GObject * object,
840 guint prop_id, GValue * value, GParamSpec * pspec)
841 {
842 GstRTPMux *rtp_mux;
843
844 rtp_mux = GST_RTP_MUX (object);
845
846 GST_OBJECT_LOCK (rtp_mux);
847 switch (prop_id) {
848 case PROP_TIMESTAMP_OFFSET:
849 g_value_set_int (value, rtp_mux->ts_offset);
850 break;
851 case PROP_SEQNUM_OFFSET:
852 g_value_set_int (value, rtp_mux->seqnum_offset);
853 break;
854 case PROP_SEQNUM:
855 g_value_set_uint (value, rtp_mux->seqnum);
856 break;
857 case PROP_SSRC:
858 g_value_set_uint (value, rtp_mux->ssrc);
859 break;
860 default:
861 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
862 break;
863 }
864 GST_OBJECT_UNLOCK (rtp_mux);
865 }
866
867 static void
gst_rtp_mux_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)868 gst_rtp_mux_set_property (GObject * object,
869 guint prop_id, const GValue * value, GParamSpec * pspec)
870 {
871 GstRTPMux *rtp_mux;
872
873 rtp_mux = GST_RTP_MUX (object);
874
875 switch (prop_id) {
876 case PROP_TIMESTAMP_OFFSET:
877 rtp_mux->ts_offset = g_value_get_int (value);
878 break;
879 case PROP_SEQNUM_OFFSET:
880 rtp_mux->seqnum_offset = g_value_get_int (value);
881 break;
882 case PROP_SSRC:
883 GST_OBJECT_LOCK (rtp_mux);
884 rtp_mux->ssrc = g_value_get_uint (value);
885 rtp_mux->current_ssrc = rtp_mux->ssrc;
886 rtp_mux->have_ssrc = TRUE;
887 GST_DEBUG_OBJECT (rtp_mux, "ssrc prop set to %x", rtp_mux->ssrc);
888 GST_OBJECT_UNLOCK (rtp_mux);
889 break;
890 default:
891 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
892 break;
893 }
894 }
895
896 static gboolean
gst_rtp_mux_sink_event(GstPad * pad,GstObject * parent,GstEvent * event)897 gst_rtp_mux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
898 {
899 GstRTPMux *mux = GST_RTP_MUX (parent);
900 gboolean is_pad;
901 gboolean ret;
902
903 GST_OBJECT_LOCK (mux);
904 is_pad = (pad == mux->last_pad);
905 GST_OBJECT_UNLOCK (mux);
906
907 switch (GST_EVENT_TYPE (event)) {
908 case GST_EVENT_CAPS:
909 {
910 GstCaps *caps;
911
912 gst_event_parse_caps (event, &caps);
913 GST_LOG_OBJECT (pad, "Received caps-event with caps: %"
914 GST_PTR_FORMAT, caps);
915 ret = gst_rtp_mux_setcaps (pad, mux, caps);
916 gst_event_unref (event);
917 return ret;
918 }
919 case GST_EVENT_FLUSH_STOP:
920 {
921 GST_OBJECT_LOCK (mux);
922 mux->last_stop = GST_CLOCK_TIME_NONE;
923 GST_OBJECT_UNLOCK (mux);
924 break;
925 }
926 case GST_EVENT_SEGMENT:
927 {
928 GstRTPMuxPadPrivate *padpriv;
929
930 GST_OBJECT_LOCK (mux);
931 padpriv = gst_pad_get_element_private (pad);
932
933 if (padpriv) {
934 gst_event_copy_segment (event, &padpriv->segment);
935 }
936 GST_OBJECT_UNLOCK (mux);
937
938 if (is_pad) {
939 GstSegment new_segment;
940 gst_segment_init (&new_segment, GST_FORMAT_TIME);
941 gst_event_unref (event);
942 event = gst_event_new_segment (&new_segment);
943 }
944 break;
945 }
946 default:
947 break;
948 }
949
950 if (is_pad) {
951 return gst_pad_push_event (mux->srcpad, event);
952 } else {
953 gst_event_unref (event);
954 return TRUE;
955 }
956 }
957
958 static void
gst_rtp_mux_ready_to_paused(GstRTPMux * rtp_mux)959 gst_rtp_mux_ready_to_paused (GstRTPMux * rtp_mux)
960 {
961
962 GST_OBJECT_LOCK (rtp_mux);
963
964 g_clear_object (&rtp_mux->last_pad);
965 rtp_mux->send_stream_start = TRUE;
966
967 if (rtp_mux->seqnum_offset == -1)
968 rtp_mux->seqnum_base = g_random_int_range (0, G_MAXUINT16);
969 else
970 rtp_mux->seqnum_base = rtp_mux->seqnum_offset;
971 rtp_mux->seqnum = rtp_mux->seqnum_base;
972
973 if (rtp_mux->ts_offset == -1)
974 rtp_mux->ts_base = g_random_int ();
975 else
976 rtp_mux->ts_base = rtp_mux->ts_offset;
977
978 rtp_mux->last_stop = GST_CLOCK_TIME_NONE;
979
980 if (rtp_mux->have_ssrc)
981 rtp_mux->current_ssrc = rtp_mux->ssrc;
982
983 GST_DEBUG_OBJECT (rtp_mux, "set timestamp-offset to %u", rtp_mux->ts_base);
984
985 GST_OBJECT_UNLOCK (rtp_mux);
986 }
987
988 static GstStateChangeReturn
gst_rtp_mux_change_state(GstElement * element,GstStateChange transition)989 gst_rtp_mux_change_state (GstElement * element, GstStateChange transition)
990 {
991 GstRTPMux *rtp_mux;
992 GstStateChangeReturn ret;
993
994 rtp_mux = GST_RTP_MUX (element);
995
996 switch (transition) {
997 case GST_STATE_CHANGE_READY_TO_PAUSED:
998 gst_rtp_mux_ready_to_paused (rtp_mux);
999 break;
1000 default:
1001 break;
1002 }
1003
1004 ret = GST_ELEMENT_CLASS (gst_rtp_mux_parent_class)->change_state (element,
1005 transition);
1006
1007 switch (transition) {
1008 case GST_STATE_CHANGE_PAUSED_TO_READY:
1009 g_clear_object (&rtp_mux->last_pad);
1010 break;
1011 default:
1012 break;
1013 }
1014
1015 return ret;
1016 }
1017