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->packets = g_queue_new ();
175 src->seqnum_base = -1;
176 src->last_rtptime = -1;
178 rtp_source_reset (src);
182 rtp_source_finalize (GObject * object)
188 src = RTP_SOURCE_CAST (object);
190 while ((buffer = g_queue_pop_head (src->packets)))
191 gst_buffer_unref (buffer);
192 g_queue_free (src->packets);
194 for (i = 0; i < 9; i++)
195 g_free (src->sdes[i]);
197 g_free (src->bye_reason);
199 G_OBJECT_CLASS (rtp_source_parent_class)->finalize (object);
203 rtp_source_set_property (GObject * object, guint prop_id,
204 const GValue * value, GParamSpec * pspec)
208 src = RTP_SOURCE (object);
212 src->ssrc = g_value_get_uint (value);
214 case PROP_SDES_CNAME:
215 rtp_source_set_sdes_string (src, GST_RTCP_SDES_CNAME,
216 g_value_get_string (value));
219 rtp_source_set_sdes_string (src, GST_RTCP_SDES_NAME,
220 g_value_get_string (value));
222 case PROP_SDES_EMAIL:
223 rtp_source_set_sdes_string (src, GST_RTCP_SDES_EMAIL,
224 g_value_get_string (value));
226 case PROP_SDES_PHONE:
227 rtp_source_set_sdes_string (src, GST_RTCP_SDES_PHONE,
228 g_value_get_string (value));
230 case PROP_SDES_LOCATION:
231 rtp_source_set_sdes_string (src, GST_RTCP_SDES_LOC,
232 g_value_get_string (value));
235 rtp_source_set_sdes_string (src, GST_RTCP_SDES_TOOL,
236 g_value_get_string (value));
239 rtp_source_set_sdes_string (src, GST_RTCP_SDES_NOTE,
240 g_value_get_string (value));
243 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
249 rtp_source_get_property (GObject * object, guint prop_id,
250 GValue * value, GParamSpec * pspec)
254 src = RTP_SOURCE (object);
258 g_value_set_uint (value, rtp_source_get_ssrc (src));
261 g_value_set_boolean (value, rtp_source_is_as_csrc (src));
263 case PROP_IS_VALIDATED:
264 g_value_set_boolean (value, rtp_source_is_validated (src));
267 g_value_set_boolean (value, rtp_source_is_sender (src));
269 case PROP_SDES_CNAME:
270 g_value_take_string (value, rtp_source_get_sdes_string (src,
271 GST_RTCP_SDES_CNAME));
274 g_value_take_string (value, rtp_source_get_sdes_string (src,
275 GST_RTCP_SDES_NAME));
277 case PROP_SDES_EMAIL:
278 g_value_take_string (value, rtp_source_get_sdes_string (src,
279 GST_RTCP_SDES_EMAIL));
281 case PROP_SDES_PHONE:
282 g_value_take_string (value, rtp_source_get_sdes_string (src,
283 GST_RTCP_SDES_PHONE));
285 case PROP_SDES_LOCATION:
286 g_value_take_string (value, rtp_source_get_sdes_string (src,
290 g_value_take_string (value, rtp_source_get_sdes_string (src,
291 GST_RTCP_SDES_TOOL));
294 g_value_take_string (value, rtp_source_get_sdes_string (src,
295 GST_RTCP_SDES_NOTE));
298 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
307 * Create a #RTPSource with @ssrc.
309 * Returns: a new #RTPSource. Use g_object_unref() after usage.
312 rtp_source_new (guint32 ssrc)
316 src = g_object_new (RTP_TYPE_SOURCE, NULL);
323 * rtp_source_set_callbacks:
324 * @src: an #RTPSource
325 * @cb: callback functions
326 * @user_data: user data
328 * Set the callbacks for the source.
331 rtp_source_set_callbacks (RTPSource * src, RTPSourceCallbacks * cb,
334 g_return_if_fail (RTP_IS_SOURCE (src));
336 src->callbacks.push_rtp = cb->push_rtp;
337 src->callbacks.clock_rate = cb->clock_rate;
338 src->user_data = user_data;
342 * rtp_source_get_ssrc:
343 * @src: an #RTPSource
345 * Get the SSRC of @source.
347 * Returns: the SSRC of src.
350 rtp_source_get_ssrc (RTPSource * src)
354 g_return_val_if_fail (RTP_IS_SOURCE (src), 0);
362 * rtp_source_set_as_csrc:
363 * @src: an #RTPSource
365 * Configure @src as a CSRC, this will also validate @src.
368 rtp_source_set_as_csrc (RTPSource * src)
370 g_return_if_fail (RTP_IS_SOURCE (src));
372 src->validated = TRUE;
377 * rtp_source_is_as_csrc:
378 * @src: an #RTPSource
380 * Check if @src is a contributing source.
382 * Returns: %TRUE if @src is acting as a contributing source.
385 rtp_source_is_as_csrc (RTPSource * src)
389 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
391 result = src->is_csrc;
397 * rtp_source_is_active:
398 * @src: an #RTPSource
400 * Check if @src is an active source. A source is active if it has been
401 * validated and has not yet received a BYE packet
403 * Returns: %TRUE if @src is an qactive source.
406 rtp_source_is_active (RTPSource * src)
410 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
412 result = RTP_SOURCE_IS_ACTIVE (src);
418 * rtp_source_is_validated:
419 * @src: an #RTPSource
421 * Check if @src is a validated source.
423 * Returns: %TRUE if @src is a validated source.
426 rtp_source_is_validated (RTPSource * src)
430 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
432 result = src->validated;
438 * rtp_source_is_sender:
439 * @src: an #RTPSource
441 * Check if @src is a sending source.
443 * Returns: %TRUE if @src is a sending source.
446 rtp_source_is_sender (RTPSource * src)
450 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
452 result = RTP_SOURCE_IS_SENDER (src);
458 * rtp_source_received_bye:
459 * @src: an #RTPSource
461 * Check if @src has receoved a BYE packet.
463 * Returns: %TRUE if @src has received a BYE packet.
466 rtp_source_received_bye (RTPSource * src)
470 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
472 result = src->received_bye;
479 * rtp_source_get_bye_reason:
480 * @src: an #RTPSource
482 * Get the BYE reason for @src. Check if the source receoved a BYE message first
483 * with rtp_source_received_bye().
485 * Returns: The BYE reason or NULL when no reason was given or the source did
486 * not receive a BYE message yet. g_fee() after usage.
489 rtp_source_get_bye_reason (RTPSource * src)
493 g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
495 result = g_strdup (src->bye_reason);
501 * rtp_source_update_caps:
502 * @src: an #RTPSource
505 * Parse @caps and store all relevant information in @source.
508 rtp_source_update_caps (RTPSource * src, GstCaps * caps)
514 /* nothing changed, return */
515 if (src->caps == caps)
518 s = gst_caps_get_structure (caps, 0);
520 if (gst_structure_get_int (s, "payload", &ival))
522 GST_DEBUG ("got payload %d", src->payload);
524 gst_structure_get_int (s, "clock-rate", &src->clock_rate);
525 GST_DEBUG ("got clock-rate %d", src->clock_rate);
527 if (gst_structure_get_uint (s, "clock-base", &val))
528 src->clock_base = val;
529 GST_DEBUG ("got clock-base %" G_GINT64_FORMAT, src->clock_base);
531 if (gst_structure_get_uint (s, "seqnum-base", &val))
532 src->seqnum_base = val;
533 GST_DEBUG ("got seqnum-base %" G_GINT32_FORMAT, src->seqnum_base);
535 gst_caps_replace (&src->caps, caps);
539 * rtp_source_set_sdes:
540 * @src: an #RTPSource
541 * @type: the type of the SDES item
542 * @data: the SDES data
543 * @len: the SDES length
545 * Store an SDES item of @type in @src.
547 * Returns: %FALSE if the SDES item was unchanged or @type is unknown.
550 rtp_source_set_sdes (RTPSource * src, GstRTCPSDESType type,
551 const guint8 * data, guint len)
555 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
557 if (type < 0 || type > GST_RTCP_SDES_PRIV)
560 old = src->sdes[type];
562 /* lengths are the same, check if the data is the same */
563 if ((src->sdes_len[type] == len))
564 if (data != NULL && old != NULL && (memcmp (old, data, len) == 0))
567 /* NULL data, make sure we store 0 length or if no length is given,
572 g_free (src->sdes[type]);
573 src->sdes[type] = g_memdup (data, len);
574 src->sdes_len[type] = len;
580 * rtp_source_set_sdes_string:
581 * @src: an #RTPSource
582 * @type: the type of the SDES item
583 * @data: the SDES data
585 * Store an SDES item of @type in @src. This function is similar to
586 * rtp_source_set_sdes() but takes a null-terminated string for convenience.
588 * Returns: %FALSE if the SDES item was unchanged or @type is unknown.
591 rtp_source_set_sdes_string (RTPSource * src, GstRTCPSDESType type,
602 result = rtp_source_set_sdes (src, type, (guint8 *) data, len);
608 * rtp_source_get_sdes:
609 * @src: an #RTPSource
610 * @type: the type of the SDES item
611 * @data: location to store the SDES data or NULL
612 * @len: location to store the SDES length or NULL
614 * Get the SDES item of @type from @src. Note that @data does not always point
615 * to a null-terminated string, use rtp_source_get_sdes_string() to retrieve a
616 * null-terminated string instead.
618 * @data remains valid until the next call to rtp_source_set_sdes().
620 * Returns: %TRUE if @type was valid and @data and @len contain valid
621 * data. @data can be NULL when the item was unset.
624 rtp_source_get_sdes (RTPSource * src, GstRTCPSDESType type, guint8 ** data,
627 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
629 if (type < 0 || type > GST_RTCP_SDES_PRIV)
633 *data = src->sdes[type];
635 *len = src->sdes_len[type];
641 * rtp_source_get_sdes_string:
642 * @src: an #RTPSource
643 * @type: the type of the SDES item
645 * Get the SDES item of @type from @src.
647 * Returns: a null-terminated copy of the SDES item or NULL when @type was not
648 * valid or the SDES item was unset. g_free() after usage.
651 rtp_source_get_sdes_string (RTPSource * src, GstRTCPSDESType type)
655 g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
657 if (type < 0 || type > GST_RTCP_SDES_PRIV)
660 result = g_strndup ((const gchar *) src->sdes[type], src->sdes_len[type]);
666 * rtp_source_set_rtp_from:
667 * @src: an #RTPSource
668 * @address: the RTP address to set
670 * Set that @src is receiving RTP packets from @address. This is used for
671 * collistion checking.
674 rtp_source_set_rtp_from (RTPSource * src, GstNetAddress * address)
676 g_return_if_fail (RTP_IS_SOURCE (src));
678 src->have_rtp_from = TRUE;
679 memcpy (&src->rtp_from, address, sizeof (GstNetAddress));
683 * rtp_source_set_rtcp_from:
684 * @src: an #RTPSource
685 * @address: the RTCP address to set
687 * Set that @src is receiving RTCP packets from @address. This is used for
688 * collistion checking.
691 rtp_source_set_rtcp_from (RTPSource * src, GstNetAddress * address)
693 g_return_if_fail (RTP_IS_SOURCE (src));
695 src->have_rtcp_from = TRUE;
696 memcpy (&src->rtcp_from, address, sizeof (GstNetAddress));
700 push_packet (RTPSource * src, GstBuffer * buffer)
702 GstFlowReturn ret = GST_FLOW_OK;
704 /* push queued packets first if any */
705 while (!g_queue_is_empty (src->packets)) {
706 GstBuffer *buffer = GST_BUFFER_CAST (g_queue_pop_head (src->packets));
708 GST_DEBUG ("pushing queued packet");
709 if (src->callbacks.push_rtp)
710 src->callbacks.push_rtp (src, buffer, src->user_data);
712 gst_buffer_unref (buffer);
714 GST_DEBUG ("pushing new packet");
716 if (src->callbacks.push_rtp)
717 ret = src->callbacks.push_rtp (src, buffer, src->user_data);
719 gst_buffer_unref (buffer);
725 get_clock_rate (RTPSource * src, guint8 payload)
727 if (src->clock_rate == -1) {
728 gint clock_rate = -1;
730 if (src->callbacks.clock_rate)
731 clock_rate = src->callbacks.clock_rate (src, payload, src->user_data);
733 GST_DEBUG ("new payload %d, got clock-rate %d", payload, clock_rate);
735 src->clock_rate = clock_rate;
737 src->payload = payload;
739 return src->clock_rate;
742 /* Jitter is the variation in the delay of received packets in a flow. It is
743 * measured by comparing the interval when RTP packets were sent to the interval
744 * at which they were received. For instance, if packet #1 and packet #2 leave
745 * 50 milliseconds apart and arrive 60 milliseconds apart, then the jitter is 10
748 calculate_jitter (RTPSource * src, GstBuffer * buffer,
749 RTPArrivalStats * arrival)
752 guint32 rtparrival, transit, rtptime;
757 /* get arrival time */
758 if ((ntpnstime = arrival->ntpnstime) == GST_CLOCK_TIME_NONE)
761 pt = gst_rtp_buffer_get_payload_type (buffer);
763 GST_DEBUG ("SSRC %08x got payload %d", src->ssrc, pt);
766 if ((clock_rate = get_clock_rate (src, pt)) == -1)
769 rtptime = gst_rtp_buffer_get_timestamp (buffer);
771 /* no clock-base, take first rtptime as base */
772 if (src->clock_base == -1) {
773 GST_DEBUG ("using clock-base of %" G_GUINT32_FORMAT, rtptime);
774 src->clock_base = rtptime;
777 /* convert arrival time to RTP timestamp units, truncate to 32 bits, we don't
778 * care about the absolute value, just the difference. */
779 rtparrival = gst_util_uint64_scale_int (ntpnstime, clock_rate, GST_SECOND);
781 /* transit time is difference with RTP timestamp */
782 transit = rtparrival - rtptime;
784 /* get ABS diff with previous transit time */
785 if (src->stats.transit != -1) {
786 if (transit > src->stats.transit)
787 diff = transit - src->stats.transit;
789 diff = src->stats.transit - transit;
793 src->stats.transit = transit;
795 /* update jitter, the value we store is scaled up so we can keep precision. */
796 src->stats.jitter += diff - ((src->stats.jitter + 8) >> 4);
798 src->stats.prev_rtptime = src->stats.last_rtptime;
799 src->stats.last_rtptime = rtparrival;
801 GST_DEBUG ("rtparrival %u, rtptime %u, clock-rate %d, diff %d, jitter: %f",
802 rtparrival, rtptime, clock_rate, diff, (src->stats.jitter) / 16.0);
809 GST_WARNING ("cannot get current time");
814 GST_WARNING ("cannot get clock-rate for pt %d", pt);
820 init_seq (RTPSource * src, guint16 seq)
822 src->stats.base_seq = seq;
823 src->stats.max_seq = seq;
824 src->stats.bad_seq = RTP_SEQ_MOD + 1; /* so seq == bad_seq is false */
825 src->stats.cycles = 0;
826 src->stats.packets_received = 0;
827 src->stats.octets_received = 0;
828 src->stats.bytes_received = 0;
829 src->stats.prev_received = 0;
830 src->stats.prev_expected = 0;
832 GST_DEBUG ("base_seq %d", seq);
836 * rtp_source_process_rtp:
837 * @src: an #RTPSource
838 * @buffer: an RTP buffer
840 * Let @src handle the incomming RTP @buffer.
842 * Returns: a #GstFlowReturn.
845 rtp_source_process_rtp (RTPSource * src, GstBuffer * buffer,
846 RTPArrivalStats * arrival)
848 GstFlowReturn result = GST_FLOW_OK;
849 guint16 seqnr, udelta;
850 RTPSourceStats *stats;
852 g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
853 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
857 seqnr = gst_rtp_buffer_get_seq (buffer);
859 rtp_source_update_caps (src, GST_BUFFER_CAPS (buffer));
861 if (stats->cycles == -1) {
862 GST_DEBUG ("received first buffer");
863 /* first time we heard of this source */
864 init_seq (src, seqnr);
865 src->stats.max_seq = seqnr - 1;
866 src->probation = RTP_DEFAULT_PROBATION;
869 udelta = seqnr - stats->max_seq;
871 /* if we are still on probation, check seqnum */
872 if (src->probation) {
875 expected = src->stats.max_seq + 1;
877 /* when in probation, we require consecutive seqnums */
878 if (seqnr == expected) {
879 /* expected packet */
880 GST_DEBUG ("probation: seqnr %d == expected %d", seqnr, expected);
882 src->stats.max_seq = seqnr;
883 if (src->probation == 0) {
884 GST_DEBUG ("probation done!");
885 init_seq (src, seqnr);
889 GST_DEBUG ("probation %d: queue buffer", src->probation);
890 /* when still in probation, keep packets in a list. */
891 g_queue_push_tail (src->packets, buffer);
892 /* remove packets from queue if there are too many */
893 while (g_queue_get_length (src->packets) > RTP_MAX_PROBATION_LEN) {
894 q = g_queue_pop_head (src->packets);
895 gst_buffer_unref (q);
900 GST_DEBUG ("probation: seqnr %d != expected %d", seqnr, expected);
901 src->probation = RTP_DEFAULT_PROBATION;
902 src->stats.max_seq = seqnr;
905 } else if (udelta < RTP_MAX_DROPOUT) {
906 /* in order, with permissible gap */
907 if (seqnr < stats->max_seq) {
908 /* sequence number wrapped - count another 64K cycle. */
909 stats->cycles += RTP_SEQ_MOD;
911 stats->max_seq = seqnr;
912 } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
913 /* the sequence number made a very large jump */
914 if (seqnr == stats->bad_seq) {
915 /* two sequential packets -- assume that the other side
916 * restarted without telling us so just re-sync
917 * (i.e., pretend this was the first packet). */
918 init_seq (src, seqnr);
920 /* unacceptable jump */
921 stats->bad_seq = (seqnr + 1) & (RTP_SEQ_MOD - 1);
925 /* duplicate or reordered packet, will be filtered by jitterbuffer. */
926 GST_WARNING ("duplicate or reordered packet");
929 src->stats.octets_received += arrival->payload_len;
930 src->stats.bytes_received += arrival->bytes;
931 src->stats.packets_received++;
932 /* the source that sent the packet must be a sender */
933 src->is_sender = TRUE;
934 src->validated = TRUE;
936 GST_DEBUG ("seq %d, PC: %" G_GUINT64_FORMAT ", OC: %" G_GUINT64_FORMAT,
937 seqnr, src->stats.packets_received, src->stats.octets_received);
939 /* calculate jitter and perform skew correction */
940 calculate_jitter (src, buffer, arrival);
942 /* we're ready to push the RTP packet now */
943 result = push_packet (src, buffer);
951 GST_WARNING ("unacceptable seqnum received");
957 * rtp_source_process_bye:
958 * @src: an #RTPSource
959 * @reason: the reason for leaving
961 * Notify @src that a BYE packet has been received. This will make the source
965 rtp_source_process_bye (RTPSource * src, const gchar * reason)
967 g_return_if_fail (RTP_IS_SOURCE (src));
969 GST_DEBUG ("marking SSRC %08x as BYE, reason: %s", src->ssrc,
970 GST_STR_NULL (reason));
972 /* copy the reason and mark as received_bye */
973 g_free (src->bye_reason);
974 src->bye_reason = g_strdup (reason);
975 src->received_bye = TRUE;
979 * rtp_source_send_rtp:
980 * @src: an #RTPSource
981 * @buffer: an RTP buffer
982 * @ntpnstime: the NTP time when this buffer was captured in nanoseconds
984 * Send an RTP @buffer originating from @src. This will make @src a sender.
985 * This function takes ownership of @buffer and modifies the SSRC in the RTP
986 * packet to that of @src when needed.
988 * Returns: a #GstFlowReturn.
991 rtp_source_send_rtp (RTPSource * src, GstBuffer * buffer, guint64 ntpnstime)
993 GstFlowReturn result = GST_FLOW_OK;
997 guint64 ntp_diff, rtp_diff;
999 g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
1000 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1002 len = gst_rtp_buffer_get_payload_len (buffer);
1004 rtp_source_update_caps (src, GST_BUFFER_CAPS (buffer));
1006 /* we are a sender now */
1007 src->is_sender = TRUE;
1009 /* update stats for the SR */
1010 src->stats.packets_sent++;
1011 src->stats.octets_sent += len;
1013 rtptime = gst_rtp_buffer_get_timestamp (buffer);
1014 ext_rtptime = src->last_rtptime;
1015 ext_rtptime = gst_rtp_buffer_ext_timestamp (&ext_rtptime, rtptime);
1017 GST_DEBUG ("SSRC %08x, RTP %" G_GUINT64_FORMAT ", NTP %" GST_TIME_FORMAT,
1018 src->ssrc, ext_rtptime, GST_TIME_ARGS (ntpnstime));
1020 if (ext_rtptime > src->last_rtptime) {
1021 rtp_diff = ext_rtptime - src->last_rtptime;
1022 ntp_diff = ntpnstime - src->last_ntpnstime;
1024 /* calc the diff so we can detect drift at the sender. This can also be used
1025 * to guestimate the clock rate if the NTP time is locked to the RTP
1026 * timestamps (as is the case when the capture device is providing the clock). */
1027 GST_DEBUG ("SSRC %08x, diff RTP %" G_GUINT64_FORMAT ", diff NTP %"
1028 GST_TIME_FORMAT, src->ssrc, rtp_diff, GST_TIME_ARGS (ntp_diff));
1031 /* we keep track of the last received RTP timestamp and the corresponding
1032 * NTP timestamp so that we can use this info when constructing SR reports */
1033 src->last_rtptime = ext_rtptime;
1034 src->last_ntpnstime = ntpnstime;
1037 if (src->callbacks.push_rtp) {
1040 ssrc = gst_rtp_buffer_get_ssrc (buffer);
1041 if (ssrc != src->ssrc) {
1042 /* the SSRC of the packet is not correct, make a writable buffer and
1043 * update the SSRC. This could involve a complete copy of the packet when
1044 * it is not writable. Usually the payloader will use caps negotiation to
1045 * get the correct SSRC from the session manager before pushing anything. */
1046 buffer = gst_buffer_make_writable (buffer);
1048 GST_WARNING ("updating SSRC from %08x to %08x, fix the payloader", ssrc,
1050 gst_rtp_buffer_set_ssrc (buffer, src->ssrc);
1052 GST_DEBUG ("pushing RTP packet %" G_GUINT64_FORMAT,
1053 src->stats.packets_sent);
1054 result = src->callbacks.push_rtp (src, buffer, src->user_data);
1056 GST_WARNING ("no callback installed, dropping packet");
1057 gst_buffer_unref (buffer);
1064 * rtp_source_process_sr:
1065 * @src: an #RTPSource
1066 * @time: time of packet arrival
1067 * @ntptime: the NTP time in 32.32 fixed point
1068 * @rtptime: the RTP time
1069 * @packet_count: the packet count
1070 * @octet_count: the octect count
1072 * Update the sender report in @src.
1075 rtp_source_process_sr (RTPSource * src, GstClockTime time, guint64 ntptime,
1076 guint32 rtptime, guint32 packet_count, guint32 octet_count)
1078 RTPSenderReport *curr;
1081 g_return_if_fail (RTP_IS_SOURCE (src));
1083 GST_DEBUG ("got SR packet: SSRC %08x, NTP %08x:%08x, RTP %" G_GUINT32_FORMAT
1084 ", PC %" G_GUINT32_FORMAT ", OC %" G_GUINT32_FORMAT, src->ssrc,
1085 (guint32) (ntptime >> 32), (guint32) (ntptime & 0xffffffff), rtptime,
1086 packet_count, octet_count);
1088 curridx = src->stats.curr_sr ^ 1;
1089 curr = &src->stats.sr[curridx];
1091 /* this is a sender now */
1092 src->is_sender = TRUE;
1094 /* update current */
1095 curr->is_valid = TRUE;
1096 curr->ntptime = ntptime;
1097 curr->rtptime = rtptime;
1098 curr->packet_count = packet_count;
1099 curr->octet_count = octet_count;
1103 src->stats.curr_sr = curridx;
1107 * rtp_source_process_rb:
1108 * @src: an #RTPSource
1109 * @time: the current time in nanoseconds since 1970
1110 * @fractionlost: fraction lost since last SR/RR
1111 * @packetslost: the cumululative number of packets lost
1112 * @exthighestseq: the extended last sequence number received
1113 * @jitter: the interarrival jitter
1114 * @lsr: the last SR packet from this source
1115 * @dlsr: the delay since last SR packet
1117 * Update the report block in @src.
1120 rtp_source_process_rb (RTPSource * src, GstClockTime time, guint8 fractionlost,
1121 gint32 packetslost, guint32 exthighestseq, guint32 jitter, guint32 lsr,
1124 RTPReceiverReport *curr;
1128 g_return_if_fail (RTP_IS_SOURCE (src));
1130 GST_DEBUG ("got RB packet: SSRC %08x, FL %2x, PL %d, HS %" G_GUINT32_FORMAT
1131 ", jitter %" G_GUINT32_FORMAT ", LSR %04x:%04x, DLSR %04x:%04x",
1132 src->ssrc, fractionlost, packetslost, exthighestseq, jitter, lsr >> 16,
1133 lsr & 0xffff, dlsr >> 16, dlsr & 0xffff);
1135 curridx = src->stats.curr_rr ^ 1;
1136 curr = &src->stats.rr[curridx];
1138 /* update current */
1139 curr->is_valid = TRUE;
1140 curr->fractionlost = fractionlost;
1141 curr->packetslost = packetslost;
1142 curr->exthighestseq = exthighestseq;
1143 curr->jitter = jitter;
1147 /* calculate round trip */
1148 ntp = (gst_rtcp_unix_to_ntp (time) >> 16) & 0xffffffff;
1151 curr->round_trip = A;
1153 GST_DEBUG ("NTP %04x:%04x, round trip %04x:%04x", ntp >> 16, ntp & 0xffff,
1154 A >> 16, A & 0xffff);
1157 src->stats.curr_rr = curridx;
1161 * rtp_source_get_new_sr:
1162 * @src: an #RTPSource
1163 * @ntpnstime: the current time in nanoseconds since 1970
1164 * @ntptime: the NTP time in 32.32 fixed point
1165 * @rtptime: the RTP time corresponding to @ntptime
1166 * @packet_count: the packet count
1167 * @octet_count: the octect count
1169 * Get new values to put into a new SR report from this source.
1171 * Returns: %TRUE on success.
1174 rtp_source_get_new_sr (RTPSource * src, guint64 ntpnstime,
1175 guint64 * ntptime, guint32 * rtptime, guint32 * packet_count,
1176 guint32 * octet_count)
1179 guint64 t_current_ntp;
1180 GstClockTimeDiff diff;
1182 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1184 /* use the sync params to interpollate the date->time member to rtptime. We
1185 * use the last sent timestamp and rtptime as reference points. We assume
1186 * that the slope of the rtptime vs timestamp curve is 1, which is certainly
1187 * sufficient for the frequency at which we report SR and the rate we send
1188 * out RTP packets. */
1189 t_rtp = src->last_rtptime;
1191 GST_DEBUG ("last_ntpnstime %" GST_TIME_FORMAT ", last_rtptime %"
1192 G_GUINT64_FORMAT, GST_TIME_ARGS (src->last_ntpnstime), t_rtp);
1194 if (src->clock_rate != -1) {
1195 /* get the diff with the SR time */
1196 diff = GST_CLOCK_DIFF (src->last_ntpnstime, ntpnstime);
1198 /* now translate the diff to RTP time, handle positive and negative cases.
1199 * If there is no diff, we already set rtptime correctly above. */
1201 GST_DEBUG ("ntpnstime %" GST_TIME_FORMAT ", diff %" GST_TIME_FORMAT,
1202 GST_TIME_ARGS (ntpnstime), GST_TIME_ARGS (diff));
1203 t_rtp += gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
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_WARNING ("no clock-rate, cannot interpollate rtp time");
1214 /* convert the NTP time in nanoseconds to 32.32 fixed point */
1215 t_current_ntp = gst_util_uint64_scale (ntpnstime, (1LL << 32), GST_SECOND);
1217 GST_DEBUG ("NTP %08x:%08x, RTP %" G_GUINT32_FORMAT,
1218 (guint32) (t_current_ntp >> 32), (guint32) (t_current_ntp & 0xffffffff),
1222 *ntptime = t_current_ntp;
1226 *packet_count = src->stats.packets_sent;
1228 *octet_count = src->stats.octets_sent;
1234 * rtp_source_get_new_rb:
1235 * @src: an #RTPSource
1236 * @ntpnstime: the current time in nanoseconds since 1970
1237 * @fractionlost: fraction lost since last SR/RR
1238 * @packetslost: the cumululative number of packets lost
1239 * @exthighestseq: the extended last sequence number received
1240 * @jitter: the interarrival jitter
1241 * @lsr: the last SR packet from this source
1242 * @dlsr: the delay since last SR packet
1244 * Get new values to put into a new report block from this source.
1246 * Returns: %TRUE on success.
1249 rtp_source_get_new_rb (RTPSource * src, guint64 ntpnstime,
1250 guint8 * fractionlost, gint32 * packetslost, guint32 * exthighestseq,
1251 guint32 * jitter, guint32 * lsr, guint32 * dlsr)
1253 RTPSourceStats *stats;
1254 guint64 extended_max, expected;
1255 guint64 expected_interval, received_interval, ntptime;
1256 gint64 lost, lost_interval;
1257 guint32 fraction, LSR, DLSR;
1258 GstClockTime sr_time;
1260 stats = &src->stats;
1262 extended_max = stats->cycles + stats->max_seq;
1263 expected = extended_max - stats->base_seq + 1;
1265 GST_DEBUG ("ext_max %" G_GUINT64_FORMAT ", expected %" G_GUINT64_FORMAT
1266 ", received %" G_GUINT64_FORMAT ", base_seq %" G_GUINT32_FORMAT,
1267 extended_max, expected, stats->packets_received, stats->base_seq);
1269 lost = expected - stats->packets_received;
1270 lost = CLAMP (lost, -0x800000, 0x7fffff);
1272 expected_interval = expected - stats->prev_expected;
1273 stats->prev_expected = expected;
1274 received_interval = stats->packets_received - stats->prev_received;
1275 stats->prev_received = stats->packets_received;
1277 lost_interval = expected_interval - received_interval;
1279 if (expected_interval == 0 || lost_interval <= 0)
1282 fraction = (lost_interval << 8) / expected_interval;
1284 GST_DEBUG ("add RR for SSRC %08x", src->ssrc);
1285 /* we scaled the jitter up for additional precision */
1286 GST_DEBUG ("fraction %" G_GUINT32_FORMAT ", lost %" G_GINT64_FORMAT
1287 ", extseq %" G_GUINT64_FORMAT ", jitter %d", fraction, lost,
1288 extended_max, stats->jitter >> 4);
1290 if (rtp_source_get_last_sr (src, &sr_time, &ntptime, NULL, NULL, NULL)) {
1293 /* LSR is middle 32 bits of the last ntptime */
1294 LSR = (ntptime >> 16) & 0xffffffff;
1295 diff = ntpnstime - sr_time;
1296 GST_DEBUG ("last SR time diff %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
1297 /* DLSR, delay since last SR is expressed in 1/65536 second units */
1298 DLSR = gst_util_uint64_scale_int (diff, 65536, GST_SECOND);
1300 /* No valid SR received, LSR/DLSR are set to 0 then */
1301 GST_DEBUG ("no valid SR received");
1305 GST_DEBUG ("LSR %04x:%04x, DLSR %04x:%04x", LSR >> 16, LSR & 0xffff,
1306 DLSR >> 16, DLSR & 0xffff);
1309 *fractionlost = fraction;
1311 *packetslost = lost;
1313 *exthighestseq = extended_max;
1315 *jitter = stats->jitter >> 4;
1325 * rtp_source_get_last_sr:
1326 * @src: an #RTPSource
1327 * @time: time of packet arrival
1328 * @ntptime: the NTP time in 32.32 fixed point
1329 * @rtptime: the RTP time
1330 * @packet_count: the packet count
1331 * @octet_count: the octect count
1333 * Get the values of the last sender report as set with rtp_source_process_sr().
1335 * Returns: %TRUE if there was a valid SR report.
1338 rtp_source_get_last_sr (RTPSource * src, GstClockTime * time, guint64 * ntptime,
1339 guint32 * rtptime, guint32 * packet_count, guint32 * octet_count)
1341 RTPSenderReport *curr;
1343 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1345 curr = &src->stats.sr[src->stats.curr_sr];
1346 if (!curr->is_valid)
1350 *ntptime = curr->ntptime;
1352 *rtptime = curr->rtptime;
1354 *packet_count = curr->packet_count;
1356 *octet_count = curr->octet_count;
1364 * rtp_source_get_last_rb:
1365 * @src: an #RTPSource
1366 * @fractionlost: fraction lost since last SR/RR
1367 * @packetslost: the cumululative number of packets lost
1368 * @exthighestseq: the extended last sequence number received
1369 * @jitter: the interarrival jitter
1370 * @lsr: the last SR packet from this source
1371 * @dlsr: the delay since last SR packet
1373 * Get the values of the last RB report set with rtp_source_process_rb().
1375 * Returns: %TRUE if there was a valid SB report.
1378 rtp_source_get_last_rb (RTPSource * src, guint8 * fractionlost,
1379 gint32 * packetslost, guint32 * exthighestseq, guint32 * jitter,
1380 guint32 * lsr, guint32 * dlsr)
1382 RTPReceiverReport *curr;
1384 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1386 curr = &src->stats.rr[src->stats.curr_rr];
1387 if (!curr->is_valid)
1391 *fractionlost = curr->fractionlost;
1393 *packetslost = curr->packetslost;
1395 *exthighestseq = curr->exthighestseq;
1397 *jitter = curr->jitter;