2 * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 #include <gst/rtp/gstrtpbuffer.h>
22 #include <gst/rtp/gstrtcpbuffer.h>
24 #include "rtpsource.h"
26 GST_DEBUG_CATEGORY_STATIC (rtp_source_debug);
27 #define GST_CAT_DEFAULT rtp_source_debug
29 #define RTP_MAX_PROBATION_LEN 32
31 /* signals and args */
37 #define DEFAULT_SSRC 0
38 #define DEFAULT_IS_CSRC FALSE
39 #define DEFAULT_IS_VALIDATED FALSE
40 #define DEFAULT_IS_SENDER FALSE
41 #define DEFAULT_SDES_CNAME NULL
42 #define DEFAULT_SDES_NAME NULL
43 #define DEFAULT_SDES_EMAIL NULL
44 #define DEFAULT_SDES_PHONE NULL
45 #define DEFAULT_SDES_LOCATION NULL
46 #define DEFAULT_SDES_TOOL NULL
47 #define DEFAULT_SDES_NOTE NULL
66 /* GObject vmethods */
67 static void rtp_source_finalize (GObject * object);
68 static void rtp_source_set_property (GObject * object, guint prop_id,
69 const GValue * value, GParamSpec * pspec);
70 static void rtp_source_get_property (GObject * object, guint prop_id,
71 GValue * value, GParamSpec * pspec);
73 /* static guint rtp_source_signals[LAST_SIGNAL] = { 0 }; */
75 G_DEFINE_TYPE (RTPSource, rtp_source, G_TYPE_OBJECT);
78 rtp_source_class_init (RTPSourceClass * klass)
80 GObjectClass *gobject_class;
82 gobject_class = (GObjectClass *) klass;
84 gobject_class->finalize = rtp_source_finalize;
86 gobject_class->set_property = rtp_source_set_property;
87 gobject_class->get_property = rtp_source_get_property;
89 g_object_class_install_property (gobject_class, PROP_SSRC,
90 g_param_spec_uint ("ssrc", "SSRC",
91 "The SSRC of this source", 0, G_MAXUINT,
92 DEFAULT_SSRC, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
94 g_object_class_install_property (gobject_class, PROP_IS_CSRC,
95 g_param_spec_boolean ("is-csrc", "Is CSRC",
96 "If this SSRC is acting as a contributing source",
97 DEFAULT_IS_CSRC, G_PARAM_READABLE));
99 g_object_class_install_property (gobject_class, PROP_IS_VALIDATED,
100 g_param_spec_boolean ("is-validated", "Is Validated",
101 "If this SSRC is validated", DEFAULT_IS_VALIDATED, G_PARAM_READABLE));
103 g_object_class_install_property (gobject_class, PROP_IS_SENDER,
104 g_param_spec_boolean ("is-sender", "Is Sender",
105 "If this SSRC is a sender", DEFAULT_IS_SENDER, G_PARAM_READABLE));
107 g_object_class_install_property (gobject_class, PROP_SDES_CNAME,
108 g_param_spec_string ("sdes-cname", "SDES CNAME",
109 "The CNAME to put in SDES messages of this source",
110 DEFAULT_SDES_CNAME, G_PARAM_READWRITE));
112 g_object_class_install_property (gobject_class, PROP_SDES_NAME,
113 g_param_spec_string ("sdes-name", "SDES NAME",
114 "The NAME to put in SDES messages of this source",
115 DEFAULT_SDES_NAME, G_PARAM_READWRITE));
117 g_object_class_install_property (gobject_class, PROP_SDES_EMAIL,
118 g_param_spec_string ("sdes-email", "SDES EMAIL",
119 "The EMAIL to put in SDES messages of this source",
120 DEFAULT_SDES_EMAIL, G_PARAM_READWRITE));
122 g_object_class_install_property (gobject_class, PROP_SDES_PHONE,
123 g_param_spec_string ("sdes-phone", "SDES PHONE",
124 "The PHONE to put in SDES messages of this source",
125 DEFAULT_SDES_PHONE, G_PARAM_READWRITE));
127 g_object_class_install_property (gobject_class, PROP_SDES_LOCATION,
128 g_param_spec_string ("sdes-location", "SDES LOCATION",
129 "The LOCATION to put in SDES messages of this source",
130 DEFAULT_SDES_LOCATION, G_PARAM_READWRITE));
132 g_object_class_install_property (gobject_class, PROP_SDES_TOOL,
133 g_param_spec_string ("sdes-tool", "SDES TOOL",
134 "The TOOL to put in SDES messages of this source",
135 DEFAULT_SDES_TOOL, G_PARAM_READWRITE));
137 g_object_class_install_property (gobject_class, PROP_SDES_NOTE,
138 g_param_spec_string ("sdes-note", "SDES NOTE",
139 "The NOTE to put in SDES messages of this source",
140 DEFAULT_SDES_NOTE, G_PARAM_READWRITE));
142 GST_DEBUG_CATEGORY_INIT (rtp_source_debug, "rtpsource", 0, "RTP Source");
147 * @src: an #RTPSource
149 * Reset the stats of @src.
152 rtp_source_reset (RTPSource * src)
154 src->received_bye = FALSE;
156 src->stats.cycles = -1;
157 src->stats.jitter = 0;
158 src->stats.transit = -1;
159 src->stats.curr_sr = 0;
160 src->stats.curr_rr = 0;
164 rtp_source_init (RTPSource * src)
166 /* sources are initialy on probation until we receive enough valid RTP
167 * packets or a valid RTCP packet */
168 src->validated = FALSE;
169 src->probation = RTP_DEFAULT_PROBATION;
172 src->clock_rate = -1;
173 src->clock_base = -1;
174 src->clock_base_time = -1;
175 src->packets = g_queue_new ();
176 src->seqnum_base = -1;
177 src->last_rtptime = -1;
179 rtp_source_reset (src);
183 rtp_source_finalize (GObject * object)
189 src = RTP_SOURCE_CAST (object);
191 while ((buffer = g_queue_pop_head (src->packets)))
192 gst_buffer_unref (buffer);
193 g_queue_free (src->packets);
195 for (i = 0; i < 9; i++)
196 g_free (src->sdes[i]);
198 g_free (src->bye_reason);
200 gst_caps_replace (&src->caps, NULL);
202 G_OBJECT_CLASS (rtp_source_parent_class)->finalize (object);
206 rtp_source_set_property (GObject * object, guint prop_id,
207 const GValue * value, GParamSpec * pspec)
211 src = RTP_SOURCE (object);
215 src->ssrc = g_value_get_uint (value);
217 case PROP_SDES_CNAME:
218 rtp_source_set_sdes_string (src, GST_RTCP_SDES_CNAME,
219 g_value_get_string (value));
222 rtp_source_set_sdes_string (src, GST_RTCP_SDES_NAME,
223 g_value_get_string (value));
225 case PROP_SDES_EMAIL:
226 rtp_source_set_sdes_string (src, GST_RTCP_SDES_EMAIL,
227 g_value_get_string (value));
229 case PROP_SDES_PHONE:
230 rtp_source_set_sdes_string (src, GST_RTCP_SDES_PHONE,
231 g_value_get_string (value));
233 case PROP_SDES_LOCATION:
234 rtp_source_set_sdes_string (src, GST_RTCP_SDES_LOC,
235 g_value_get_string (value));
238 rtp_source_set_sdes_string (src, GST_RTCP_SDES_TOOL,
239 g_value_get_string (value));
242 rtp_source_set_sdes_string (src, GST_RTCP_SDES_NOTE,
243 g_value_get_string (value));
246 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
252 rtp_source_get_property (GObject * object, guint prop_id,
253 GValue * value, GParamSpec * pspec)
257 src = RTP_SOURCE (object);
261 g_value_set_uint (value, rtp_source_get_ssrc (src));
264 g_value_set_boolean (value, rtp_source_is_as_csrc (src));
266 case PROP_IS_VALIDATED:
267 g_value_set_boolean (value, rtp_source_is_validated (src));
270 g_value_set_boolean (value, rtp_source_is_sender (src));
272 case PROP_SDES_CNAME:
273 g_value_take_string (value, rtp_source_get_sdes_string (src,
274 GST_RTCP_SDES_CNAME));
277 g_value_take_string (value, rtp_source_get_sdes_string (src,
278 GST_RTCP_SDES_NAME));
280 case PROP_SDES_EMAIL:
281 g_value_take_string (value, rtp_source_get_sdes_string (src,
282 GST_RTCP_SDES_EMAIL));
284 case PROP_SDES_PHONE:
285 g_value_take_string (value, rtp_source_get_sdes_string (src,
286 GST_RTCP_SDES_PHONE));
288 case PROP_SDES_LOCATION:
289 g_value_take_string (value, rtp_source_get_sdes_string (src,
293 g_value_take_string (value, rtp_source_get_sdes_string (src,
294 GST_RTCP_SDES_TOOL));
297 g_value_take_string (value, rtp_source_get_sdes_string (src,
298 GST_RTCP_SDES_NOTE));
301 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
310 * Create a #RTPSource with @ssrc.
312 * Returns: a new #RTPSource. Use g_object_unref() after usage.
315 rtp_source_new (guint32 ssrc)
319 src = g_object_new (RTP_TYPE_SOURCE, NULL);
326 * rtp_source_set_callbacks:
327 * @src: an #RTPSource
328 * @cb: callback functions
329 * @user_data: user data
331 * Set the callbacks for the source.
334 rtp_source_set_callbacks (RTPSource * src, RTPSourceCallbacks * cb,
337 g_return_if_fail (RTP_IS_SOURCE (src));
339 src->callbacks.push_rtp = cb->push_rtp;
340 src->callbacks.clock_rate = cb->clock_rate;
341 src->user_data = user_data;
345 * rtp_source_get_ssrc:
346 * @src: an #RTPSource
348 * Get the SSRC of @source.
350 * Returns: the SSRC of src.
353 rtp_source_get_ssrc (RTPSource * src)
357 g_return_val_if_fail (RTP_IS_SOURCE (src), 0);
365 * rtp_source_set_as_csrc:
366 * @src: an #RTPSource
368 * Configure @src as a CSRC, this will also validate @src.
371 rtp_source_set_as_csrc (RTPSource * src)
373 g_return_if_fail (RTP_IS_SOURCE (src));
375 src->validated = TRUE;
380 * rtp_source_is_as_csrc:
381 * @src: an #RTPSource
383 * Check if @src is a contributing source.
385 * Returns: %TRUE if @src is acting as a contributing source.
388 rtp_source_is_as_csrc (RTPSource * src)
392 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
394 result = src->is_csrc;
400 * rtp_source_is_active:
401 * @src: an #RTPSource
403 * Check if @src is an active source. A source is active if it has been
404 * validated and has not yet received a BYE packet
406 * Returns: %TRUE if @src is an qactive source.
409 rtp_source_is_active (RTPSource * src)
413 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
415 result = RTP_SOURCE_IS_ACTIVE (src);
421 * rtp_source_is_validated:
422 * @src: an #RTPSource
424 * Check if @src is a validated source.
426 * Returns: %TRUE if @src is a validated source.
429 rtp_source_is_validated (RTPSource * src)
433 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
435 result = src->validated;
441 * rtp_source_is_sender:
442 * @src: an #RTPSource
444 * Check if @src is a sending source.
446 * Returns: %TRUE if @src is a sending source.
449 rtp_source_is_sender (RTPSource * src)
453 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
455 result = RTP_SOURCE_IS_SENDER (src);
461 * rtp_source_received_bye:
462 * @src: an #RTPSource
464 * Check if @src has receoved a BYE packet.
466 * Returns: %TRUE if @src has received a BYE packet.
469 rtp_source_received_bye (RTPSource * src)
473 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
475 result = src->received_bye;
482 * rtp_source_get_bye_reason:
483 * @src: an #RTPSource
485 * Get the BYE reason for @src. Check if the source receoved a BYE message first
486 * with rtp_source_received_bye().
488 * Returns: The BYE reason or NULL when no reason was given or the source did
489 * not receive a BYE message yet. g_fee() after usage.
492 rtp_source_get_bye_reason (RTPSource * src)
496 g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
498 result = g_strdup (src->bye_reason);
504 * rtp_source_update_caps:
505 * @src: an #RTPSource
508 * Parse @caps and store all relevant information in @source.
511 rtp_source_update_caps (RTPSource * src, GstCaps * caps)
517 /* nothing changed, return */
518 if (src->caps == caps)
521 s = gst_caps_get_structure (caps, 0);
523 if (gst_structure_get_int (s, "payload", &ival))
525 GST_DEBUG ("got payload %d", src->payload);
527 gst_structure_get_int (s, "clock-rate", &src->clock_rate);
528 GST_DEBUG ("got clock-rate %d", src->clock_rate);
530 if (gst_structure_get_uint (s, "clock-base", &val))
531 src->clock_base = val;
532 GST_DEBUG ("got clock-base %" G_GINT64_FORMAT, src->clock_base);
534 if (gst_structure_get_uint (s, "seqnum-base", &val))
535 src->seqnum_base = val;
536 GST_DEBUG ("got seqnum-base %" G_GINT32_FORMAT, src->seqnum_base);
538 gst_caps_replace (&src->caps, caps);
542 * rtp_source_set_sdes:
543 * @src: an #RTPSource
544 * @type: the type of the SDES item
545 * @data: the SDES data
546 * @len: the SDES length
548 * Store an SDES item of @type in @src.
550 * Returns: %FALSE if the SDES item was unchanged or @type is unknown.
553 rtp_source_set_sdes (RTPSource * src, GstRTCPSDESType type,
554 const guint8 * data, guint len)
558 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
560 if (type < 0 || type > GST_RTCP_SDES_PRIV)
563 old = src->sdes[type];
565 /* lengths are the same, check if the data is the same */
566 if ((src->sdes_len[type] == len))
567 if (data != NULL && old != NULL && (memcmp (old, data, len) == 0))
570 /* NULL data, make sure we store 0 length or if no length is given,
575 g_free (src->sdes[type]);
576 src->sdes[type] = g_memdup (data, len);
577 src->sdes_len[type] = len;
583 * rtp_source_set_sdes_string:
584 * @src: an #RTPSource
585 * @type: the type of the SDES item
586 * @data: the SDES data
588 * Store an SDES item of @type in @src. This function is similar to
589 * rtp_source_set_sdes() but takes a null-terminated string for convenience.
591 * Returns: %FALSE if the SDES item was unchanged or @type is unknown.
594 rtp_source_set_sdes_string (RTPSource * src, GstRTCPSDESType type,
605 result = rtp_source_set_sdes (src, type, (guint8 *) data, len);
611 * rtp_source_get_sdes:
612 * @src: an #RTPSource
613 * @type: the type of the SDES item
614 * @data: location to store the SDES data or NULL
615 * @len: location to store the SDES length or NULL
617 * Get the SDES item of @type from @src. Note that @data does not always point
618 * to a null-terminated string, use rtp_source_get_sdes_string() to retrieve a
619 * null-terminated string instead.
621 * @data remains valid until the next call to rtp_source_set_sdes().
623 * Returns: %TRUE if @type was valid and @data and @len contain valid
624 * data. @data can be NULL when the item was unset.
627 rtp_source_get_sdes (RTPSource * src, GstRTCPSDESType type, guint8 ** data,
630 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
632 if (type < 0 || type > GST_RTCP_SDES_PRIV)
636 *data = src->sdes[type];
638 *len = src->sdes_len[type];
644 * rtp_source_get_sdes_string:
645 * @src: an #RTPSource
646 * @type: the type of the SDES item
648 * Get the SDES item of @type from @src.
650 * Returns: a null-terminated copy of the SDES item or NULL when @type was not
651 * valid or the SDES item was unset. g_free() after usage.
654 rtp_source_get_sdes_string (RTPSource * src, GstRTCPSDESType type)
658 g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
660 if (type < 0 || type > GST_RTCP_SDES_PRIV)
663 result = g_strndup ((const gchar *) src->sdes[type], src->sdes_len[type]);
669 * rtp_source_set_rtp_from:
670 * @src: an #RTPSource
671 * @address: the RTP address to set
673 * Set that @src is receiving RTP packets from @address. This is used for
674 * collistion checking.
677 rtp_source_set_rtp_from (RTPSource * src, GstNetAddress * address)
679 g_return_if_fail (RTP_IS_SOURCE (src));
681 src->have_rtp_from = TRUE;
682 memcpy (&src->rtp_from, address, sizeof (GstNetAddress));
686 * rtp_source_set_rtcp_from:
687 * @src: an #RTPSource
688 * @address: the RTCP address to set
690 * Set that @src is receiving RTCP packets from @address. This is used for
691 * collistion checking.
694 rtp_source_set_rtcp_from (RTPSource * src, GstNetAddress * address)
696 g_return_if_fail (RTP_IS_SOURCE (src));
698 src->have_rtcp_from = TRUE;
699 memcpy (&src->rtcp_from, address, sizeof (GstNetAddress));
703 push_packet (RTPSource * src, GstBuffer * buffer)
705 GstFlowReturn ret = GST_FLOW_OK;
707 /* push queued packets first if any */
708 while (!g_queue_is_empty (src->packets)) {
709 GstBuffer *buffer = GST_BUFFER_CAST (g_queue_pop_head (src->packets));
711 GST_LOG ("pushing queued packet");
712 if (src->callbacks.push_rtp)
713 src->callbacks.push_rtp (src, buffer, src->user_data);
715 gst_buffer_unref (buffer);
717 GST_LOG ("pushing new packet");
719 if (src->callbacks.push_rtp)
720 ret = src->callbacks.push_rtp (src, buffer, src->user_data);
722 gst_buffer_unref (buffer);
728 get_clock_rate (RTPSource * src, guint8 payload)
730 if (src->clock_rate == -1) {
731 gint clock_rate = -1;
733 if (src->callbacks.clock_rate)
734 clock_rate = src->callbacks.clock_rate (src, payload, src->user_data);
736 GST_DEBUG ("new payload %d, got clock-rate %d", payload, clock_rate);
738 src->clock_rate = clock_rate;
740 src->payload = payload;
742 return src->clock_rate;
745 /* Jitter is the variation in the delay of received packets in a flow. It is
746 * measured by comparing the interval when RTP packets were sent to the interval
747 * at which they were received. For instance, if packet #1 and packet #2 leave
748 * 50 milliseconds apart and arrive 60 milliseconds apart, then the jitter is 10
751 calculate_jitter (RTPSource * src, GstBuffer * buffer,
752 RTPArrivalStats * arrival)
755 guint32 rtparrival, transit, rtptime;
760 /* get arrival time */
761 if ((ntpnstime = arrival->ntpnstime) == GST_CLOCK_TIME_NONE)
764 pt = gst_rtp_buffer_get_payload_type (buffer);
766 GST_LOG ("SSRC %08x got payload %d", src->ssrc, pt);
769 if ((clock_rate = get_clock_rate (src, pt)) == -1)
772 rtptime = gst_rtp_buffer_get_timestamp (buffer);
774 /* no clock-base, take first rtptime as base */
775 if (src->clock_base == -1) {
776 GST_DEBUG ("using clock-base of %" G_GUINT32_FORMAT, rtptime);
777 src->clock_base = rtptime;
778 src->clock_base_time = arrival->timestamp;
781 /* convert arrival time to RTP timestamp units, truncate to 32 bits, we don't
782 * care about the absolute value, just the difference. */
783 rtparrival = gst_util_uint64_scale_int (ntpnstime, clock_rate, GST_SECOND);
785 /* transit time is difference with RTP timestamp */
786 transit = rtparrival - rtptime;
788 /* get ABS diff with previous transit time */
789 if (src->stats.transit != -1) {
790 if (transit > src->stats.transit)
791 diff = transit - src->stats.transit;
793 diff = src->stats.transit - transit;
797 src->stats.transit = transit;
799 /* update jitter, the value we store is scaled up so we can keep precision. */
800 src->stats.jitter += diff - ((src->stats.jitter + 8) >> 4);
802 src->stats.prev_rtptime = src->stats.last_rtptime;
803 src->stats.last_rtptime = rtparrival;
805 GST_LOG ("rtparrival %u, rtptime %u, clock-rate %d, diff %d, jitter: %f",
806 rtparrival, rtptime, clock_rate, diff, (src->stats.jitter) / 16.0);
813 GST_WARNING ("cannot get current time");
818 GST_WARNING ("cannot get clock-rate for pt %d", pt);
824 init_seq (RTPSource * src, guint16 seq)
826 src->stats.base_seq = seq;
827 src->stats.max_seq = seq;
828 src->stats.bad_seq = RTP_SEQ_MOD + 1; /* so seq == bad_seq is false */
829 src->stats.cycles = 0;
830 src->stats.packets_received = 0;
831 src->stats.octets_received = 0;
832 src->stats.bytes_received = 0;
833 src->stats.prev_received = 0;
834 src->stats.prev_expected = 0;
836 GST_DEBUG ("base_seq %d", seq);
840 * rtp_source_process_rtp:
841 * @src: an #RTPSource
842 * @buffer: an RTP buffer
844 * Let @src handle the incomming RTP @buffer.
846 * Returns: a #GstFlowReturn.
849 rtp_source_process_rtp (RTPSource * src, GstBuffer * buffer,
850 RTPArrivalStats * arrival)
852 GstFlowReturn result = GST_FLOW_OK;
853 guint16 seqnr, udelta;
854 RTPSourceStats *stats;
856 g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
857 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
861 seqnr = gst_rtp_buffer_get_seq (buffer);
863 rtp_source_update_caps (src, GST_BUFFER_CAPS (buffer));
865 if (stats->cycles == -1) {
866 GST_DEBUG ("received first buffer");
867 /* first time we heard of this source */
868 init_seq (src, seqnr);
869 src->stats.max_seq = seqnr - 1;
870 src->probation = RTP_DEFAULT_PROBATION;
873 udelta = seqnr - stats->max_seq;
875 /* if we are still on probation, check seqnum */
876 if (src->probation) {
879 expected = src->stats.max_seq + 1;
881 /* when in probation, we require consecutive seqnums */
882 if (seqnr == expected) {
883 /* expected packet */
884 GST_DEBUG ("probation: seqnr %d == expected %d", seqnr, expected);
886 src->stats.max_seq = seqnr;
887 if (src->probation == 0) {
888 GST_DEBUG ("probation done!");
889 init_seq (src, seqnr);
893 GST_DEBUG ("probation %d: queue buffer", src->probation);
894 /* when still in probation, keep packets in a list. */
895 g_queue_push_tail (src->packets, buffer);
896 /* remove packets from queue if there are too many */
897 while (g_queue_get_length (src->packets) > RTP_MAX_PROBATION_LEN) {
898 q = g_queue_pop_head (src->packets);
899 gst_buffer_unref (q);
904 GST_DEBUG ("probation: seqnr %d != expected %d", seqnr, expected);
905 src->probation = RTP_DEFAULT_PROBATION;
906 src->stats.max_seq = seqnr;
909 } else if (udelta < RTP_MAX_DROPOUT) {
910 /* in order, with permissible gap */
911 if (seqnr < stats->max_seq) {
912 /* sequence number wrapped - count another 64K cycle. */
913 stats->cycles += RTP_SEQ_MOD;
915 stats->max_seq = seqnr;
916 } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
917 /* the sequence number made a very large jump */
918 if (seqnr == stats->bad_seq) {
919 /* two sequential packets -- assume that the other side
920 * restarted without telling us so just re-sync
921 * (i.e., pretend this was the first packet). */
922 init_seq (src, seqnr);
924 /* unacceptable jump */
925 stats->bad_seq = (seqnr + 1) & (RTP_SEQ_MOD - 1);
926 src->clock_base = -1;
930 /* duplicate or reordered packet, will be filtered by jitterbuffer. */
931 GST_WARNING ("duplicate or reordered packet");
932 src->clock_base = -1;
935 src->stats.octets_received += arrival->payload_len;
936 src->stats.bytes_received += arrival->bytes;
937 src->stats.packets_received++;
938 /* the source that sent the packet must be a sender */
939 src->is_sender = TRUE;
940 src->validated = TRUE;
942 GST_LOG ("seq %d, PC: %" G_GUINT64_FORMAT ", OC: %" G_GUINT64_FORMAT,
943 seqnr, src->stats.packets_received, src->stats.octets_received);
945 /* calculate jitter for the stats */
946 calculate_jitter (src, buffer, arrival);
948 /* we're ready to push the RTP packet now */
949 result = push_packet (src, buffer);
957 GST_WARNING ("unacceptable seqnum received");
963 * rtp_source_process_bye:
964 * @src: an #RTPSource
965 * @reason: the reason for leaving
967 * Notify @src that a BYE packet has been received. This will make the source
971 rtp_source_process_bye (RTPSource * src, const gchar * reason)
973 g_return_if_fail (RTP_IS_SOURCE (src));
975 GST_DEBUG ("marking SSRC %08x as BYE, reason: %s", src->ssrc,
976 GST_STR_NULL (reason));
978 /* copy the reason and mark as received_bye */
979 g_free (src->bye_reason);
980 src->bye_reason = g_strdup (reason);
981 src->received_bye = TRUE;
985 * rtp_source_send_rtp:
986 * @src: an #RTPSource
987 * @buffer: an RTP buffer
988 * @ntpnstime: the NTP time when this buffer was captured in nanoseconds
990 * Send an RTP @buffer originating from @src. This will make @src a sender.
991 * This function takes ownership of @buffer and modifies the SSRC in the RTP
992 * packet to that of @src when needed.
994 * Returns: a #GstFlowReturn.
997 rtp_source_send_rtp (RTPSource * src, GstBuffer * buffer, guint64 ntpnstime)
999 GstFlowReturn result = GST_FLOW_OK;
1002 guint64 ext_rtptime;
1003 guint64 ntp_diff, rtp_diff;
1005 g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
1006 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1008 len = gst_rtp_buffer_get_payload_len (buffer);
1010 rtp_source_update_caps (src, GST_BUFFER_CAPS (buffer));
1012 /* we are a sender now */
1013 src->is_sender = TRUE;
1015 /* update stats for the SR */
1016 src->stats.packets_sent++;
1017 src->stats.octets_sent += len;
1019 rtptime = gst_rtp_buffer_get_timestamp (buffer);
1020 ext_rtptime = src->last_rtptime;
1021 ext_rtptime = gst_rtp_buffer_ext_timestamp (&ext_rtptime, rtptime);
1023 GST_LOG ("SSRC %08x, RTP %" G_GUINT64_FORMAT ", NTP %" GST_TIME_FORMAT,
1024 src->ssrc, ext_rtptime, GST_TIME_ARGS (ntpnstime));
1026 if (ext_rtptime > src->last_rtptime) {
1027 rtp_diff = ext_rtptime - src->last_rtptime;
1028 ntp_diff = ntpnstime - src->last_ntpnstime;
1030 /* calc the diff so we can detect drift at the sender. This can also be used
1031 * to guestimate the clock rate if the NTP time is locked to the RTP
1032 * timestamps (as is the case when the capture device is providing the clock). */
1033 GST_LOG ("SSRC %08x, diff RTP %" G_GUINT64_FORMAT ", diff NTP %"
1034 GST_TIME_FORMAT, src->ssrc, rtp_diff, GST_TIME_ARGS (ntp_diff));
1037 /* we keep track of the last received RTP timestamp and the corresponding
1038 * NTP timestamp so that we can use this info when constructing SR reports */
1039 src->last_rtptime = ext_rtptime;
1040 src->last_ntpnstime = ntpnstime;
1043 if (src->callbacks.push_rtp) {
1046 ssrc = gst_rtp_buffer_get_ssrc (buffer);
1047 if (ssrc != src->ssrc) {
1048 /* the SSRC of the packet is not correct, make a writable buffer and
1049 * update the SSRC. This could involve a complete copy of the packet when
1050 * it is not writable. Usually the payloader will use caps negotiation to
1051 * get the correct SSRC from the session manager before pushing anything. */
1052 buffer = gst_buffer_make_writable (buffer);
1054 GST_WARNING ("updating SSRC from %08x to %08x, fix the payloader", ssrc,
1056 gst_rtp_buffer_set_ssrc (buffer, src->ssrc);
1058 GST_LOG ("pushing RTP packet %" G_GUINT64_FORMAT, src->stats.packets_sent);
1059 result = src->callbacks.push_rtp (src, buffer, src->user_data);
1061 GST_WARNING ("no callback installed, dropping packet");
1062 gst_buffer_unref (buffer);
1069 * rtp_source_process_sr:
1070 * @src: an #RTPSource
1071 * @time: time of packet arrival
1072 * @ntptime: the NTP time in 32.32 fixed point
1073 * @rtptime: the RTP time
1074 * @packet_count: the packet count
1075 * @octet_count: the octect count
1077 * Update the sender report in @src.
1080 rtp_source_process_sr (RTPSource * src, GstClockTime time, guint64 ntptime,
1081 guint32 rtptime, guint32 packet_count, guint32 octet_count)
1083 RTPSenderReport *curr;
1086 g_return_if_fail (RTP_IS_SOURCE (src));
1088 GST_DEBUG ("got SR packet: SSRC %08x, NTP %08x:%08x, RTP %" G_GUINT32_FORMAT
1089 ", PC %" G_GUINT32_FORMAT ", OC %" G_GUINT32_FORMAT, src->ssrc,
1090 (guint32) (ntptime >> 32), (guint32) (ntptime & 0xffffffff), rtptime,
1091 packet_count, octet_count);
1093 curridx = src->stats.curr_sr ^ 1;
1094 curr = &src->stats.sr[curridx];
1096 /* this is a sender now */
1097 src->is_sender = TRUE;
1099 /* update current */
1100 curr->is_valid = TRUE;
1101 curr->ntptime = ntptime;
1102 curr->rtptime = rtptime;
1103 curr->packet_count = packet_count;
1104 curr->octet_count = octet_count;
1108 src->stats.curr_sr = curridx;
1112 * rtp_source_process_rb:
1113 * @src: an #RTPSource
1114 * @time: the current time in nanoseconds since 1970
1115 * @fractionlost: fraction lost since last SR/RR
1116 * @packetslost: the cumululative number of packets lost
1117 * @exthighestseq: the extended last sequence number received
1118 * @jitter: the interarrival jitter
1119 * @lsr: the last SR packet from this source
1120 * @dlsr: the delay since last SR packet
1122 * Update the report block in @src.
1125 rtp_source_process_rb (RTPSource * src, GstClockTime time, guint8 fractionlost,
1126 gint32 packetslost, guint32 exthighestseq, guint32 jitter, guint32 lsr,
1129 RTPReceiverReport *curr;
1133 g_return_if_fail (RTP_IS_SOURCE (src));
1135 GST_DEBUG ("got RB packet: SSRC %08x, FL %2x, PL %d, HS %" G_GUINT32_FORMAT
1136 ", jitter %" G_GUINT32_FORMAT ", LSR %04x:%04x, DLSR %04x:%04x",
1137 src->ssrc, fractionlost, packetslost, exthighestseq, jitter, lsr >> 16,
1138 lsr & 0xffff, dlsr >> 16, dlsr & 0xffff);
1140 curridx = src->stats.curr_rr ^ 1;
1141 curr = &src->stats.rr[curridx];
1143 /* update current */
1144 curr->is_valid = TRUE;
1145 curr->fractionlost = fractionlost;
1146 curr->packetslost = packetslost;
1147 curr->exthighestseq = exthighestseq;
1148 curr->jitter = jitter;
1152 /* calculate round trip */
1153 ntp = (gst_rtcp_unix_to_ntp (time) >> 16) & 0xffffffff;
1156 curr->round_trip = A;
1158 GST_DEBUG ("NTP %04x:%04x, round trip %04x:%04x", ntp >> 16, ntp & 0xffff,
1159 A >> 16, A & 0xffff);
1162 src->stats.curr_rr = curridx;
1166 * rtp_source_get_new_sr:
1167 * @src: an #RTPSource
1168 * @ntpnstime: the current time in nanoseconds since 1970
1169 * @ntptime: the NTP time in 32.32 fixed point
1170 * @rtptime: the RTP time corresponding to @ntptime
1171 * @packet_count: the packet count
1172 * @octet_count: the octect count
1174 * Get new values to put into a new SR report from this source.
1176 * Returns: %TRUE on success.
1179 rtp_source_get_new_sr (RTPSource * src, guint64 ntpnstime,
1180 guint64 * ntptime, guint32 * rtptime, guint32 * packet_count,
1181 guint32 * octet_count)
1184 guint64 t_current_ntp;
1185 GstClockTimeDiff diff;
1187 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1189 /* use the sync params to interpolate the date->time member to rtptime. We
1190 * use the last sent timestamp and rtptime as reference points. We assume
1191 * that the slope of the rtptime vs timestamp curve is 1, which is certainly
1192 * sufficient for the frequency at which we report SR and the rate we send
1193 * out RTP packets. */
1194 t_rtp = src->last_rtptime;
1196 GST_DEBUG ("last_ntpnstime %" GST_TIME_FORMAT ", last_rtptime %"
1197 G_GUINT64_FORMAT, GST_TIME_ARGS (src->last_ntpnstime), t_rtp);
1199 if (src->clock_rate != -1) {
1200 /* get the diff with the SR time */
1201 diff = GST_CLOCK_DIFF (src->last_ntpnstime, ntpnstime);
1203 /* now translate the diff to RTP time, handle positive and negative cases.
1204 * If there is no diff, we already set rtptime correctly above. */
1206 GST_DEBUG ("ntpnstime %" GST_TIME_FORMAT ", diff %" GST_TIME_FORMAT,
1207 GST_TIME_ARGS (ntpnstime), GST_TIME_ARGS (diff));
1208 t_rtp += gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1211 GST_DEBUG ("ntpnstime %" GST_TIME_FORMAT ", diff -%" GST_TIME_FORMAT,
1212 GST_TIME_ARGS (ntpnstime), GST_TIME_ARGS (diff));
1213 t_rtp -= gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1216 GST_WARNING ("no clock-rate, cannot interpolate rtp time");
1219 /* convert the NTP time in nanoseconds to 32.32 fixed point */
1220 t_current_ntp = gst_util_uint64_scale (ntpnstime, (1LL << 32), GST_SECOND);
1222 GST_DEBUG ("NTP %08x:%08x, RTP %" G_GUINT32_FORMAT,
1223 (guint32) (t_current_ntp >> 32), (guint32) (t_current_ntp & 0xffffffff),
1227 *ntptime = t_current_ntp;
1231 *packet_count = src->stats.packets_sent;
1233 *octet_count = src->stats.octets_sent;
1239 * rtp_source_get_new_rb:
1240 * @src: an #RTPSource
1241 * @ntpnstime: the current time in nanoseconds since 1970
1242 * @fractionlost: fraction lost since last SR/RR
1243 * @packetslost: the cumululative number of packets lost
1244 * @exthighestseq: the extended last sequence number received
1245 * @jitter: the interarrival jitter
1246 * @lsr: the last SR packet from this source
1247 * @dlsr: the delay since last SR packet
1249 * Get new values to put into a new report block from this source.
1251 * Returns: %TRUE on success.
1254 rtp_source_get_new_rb (RTPSource * src, guint64 ntpnstime,
1255 guint8 * fractionlost, gint32 * packetslost, guint32 * exthighestseq,
1256 guint32 * jitter, guint32 * lsr, guint32 * dlsr)
1258 RTPSourceStats *stats;
1259 guint64 extended_max, expected;
1260 guint64 expected_interval, received_interval, ntptime;
1261 gint64 lost, lost_interval;
1262 guint32 fraction, LSR, DLSR;
1263 GstClockTime sr_time;
1265 stats = &src->stats;
1267 extended_max = stats->cycles + stats->max_seq;
1268 expected = extended_max - stats->base_seq + 1;
1270 GST_DEBUG ("ext_max %" G_GUINT64_FORMAT ", expected %" G_GUINT64_FORMAT
1271 ", received %" G_GUINT64_FORMAT ", base_seq %" G_GUINT32_FORMAT,
1272 extended_max, expected, stats->packets_received, stats->base_seq);
1274 lost = expected - stats->packets_received;
1275 lost = CLAMP (lost, -0x800000, 0x7fffff);
1277 expected_interval = expected - stats->prev_expected;
1278 stats->prev_expected = expected;
1279 received_interval = stats->packets_received - stats->prev_received;
1280 stats->prev_received = stats->packets_received;
1282 lost_interval = expected_interval - received_interval;
1284 if (expected_interval == 0 || lost_interval <= 0)
1287 fraction = (lost_interval << 8) / expected_interval;
1289 GST_DEBUG ("add RR for SSRC %08x", src->ssrc);
1290 /* we scaled the jitter up for additional precision */
1291 GST_DEBUG ("fraction %" G_GUINT32_FORMAT ", lost %" G_GINT64_FORMAT
1292 ", extseq %" G_GUINT64_FORMAT ", jitter %d", fraction, lost,
1293 extended_max, stats->jitter >> 4);
1295 if (rtp_source_get_last_sr (src, &sr_time, &ntptime, NULL, NULL, NULL)) {
1298 /* LSR is middle 32 bits of the last ntptime */
1299 LSR = (ntptime >> 16) & 0xffffffff;
1300 diff = ntpnstime - sr_time;
1301 GST_DEBUG ("last SR time diff %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
1302 /* DLSR, delay since last SR is expressed in 1/65536 second units */
1303 DLSR = gst_util_uint64_scale_int (diff, 65536, GST_SECOND);
1305 /* No valid SR received, LSR/DLSR are set to 0 then */
1306 GST_DEBUG ("no valid SR received");
1310 GST_DEBUG ("LSR %04x:%04x, DLSR %04x:%04x", LSR >> 16, LSR & 0xffff,
1311 DLSR >> 16, DLSR & 0xffff);
1314 *fractionlost = fraction;
1316 *packetslost = lost;
1318 *exthighestseq = extended_max;
1320 *jitter = stats->jitter >> 4;
1330 * rtp_source_get_last_sr:
1331 * @src: an #RTPSource
1332 * @time: time of packet arrival
1333 * @ntptime: the NTP time in 32.32 fixed point
1334 * @rtptime: the RTP time
1335 * @packet_count: the packet count
1336 * @octet_count: the octect count
1338 * Get the values of the last sender report as set with rtp_source_process_sr().
1340 * Returns: %TRUE if there was a valid SR report.
1343 rtp_source_get_last_sr (RTPSource * src, GstClockTime * time, guint64 * ntptime,
1344 guint32 * rtptime, guint32 * packet_count, guint32 * octet_count)
1346 RTPSenderReport *curr;
1348 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1350 curr = &src->stats.sr[src->stats.curr_sr];
1351 if (!curr->is_valid)
1355 *ntptime = curr->ntptime;
1357 *rtptime = curr->rtptime;
1359 *packet_count = curr->packet_count;
1361 *octet_count = curr->octet_count;
1369 * rtp_source_get_last_rb:
1370 * @src: an #RTPSource
1371 * @fractionlost: fraction lost since last SR/RR
1372 * @packetslost: the cumululative number of packets lost
1373 * @exthighestseq: the extended last sequence number received
1374 * @jitter: the interarrival jitter
1375 * @lsr: the last SR packet from this source
1376 * @dlsr: the delay since last SR packet
1378 * Get the values of the last RB report set with rtp_source_process_rb().
1380 * Returns: %TRUE if there was a valid SB report.
1383 rtp_source_get_last_rb (RTPSource * src, guint8 * fractionlost,
1384 gint32 * packetslost, guint32 * exthighestseq, guint32 * jitter,
1385 guint32 * lsr, guint32 * dlsr)
1387 RTPReceiverReport *curr;
1389 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1391 curr = &src->stats.rr[src->stats.curr_rr];
1392 if (!curr->is_valid)
1396 *fractionlost = curr->fractionlost;
1398 *packetslost = curr->packetslost;
1400 *exthighestseq = curr->exthighestseq;
1402 *jitter = curr->jitter;