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 G_OBJECT_CLASS (rtp_source_parent_class)->finalize (object);
204 rtp_source_set_property (GObject * object, guint prop_id,
205 const GValue * value, GParamSpec * pspec)
209 src = RTP_SOURCE (object);
213 src->ssrc = g_value_get_uint (value);
215 case PROP_SDES_CNAME:
216 rtp_source_set_sdes_string (src, GST_RTCP_SDES_CNAME,
217 g_value_get_string (value));
220 rtp_source_set_sdes_string (src, GST_RTCP_SDES_NAME,
221 g_value_get_string (value));
223 case PROP_SDES_EMAIL:
224 rtp_source_set_sdes_string (src, GST_RTCP_SDES_EMAIL,
225 g_value_get_string (value));
227 case PROP_SDES_PHONE:
228 rtp_source_set_sdes_string (src, GST_RTCP_SDES_PHONE,
229 g_value_get_string (value));
231 case PROP_SDES_LOCATION:
232 rtp_source_set_sdes_string (src, GST_RTCP_SDES_LOC,
233 g_value_get_string (value));
236 rtp_source_set_sdes_string (src, GST_RTCP_SDES_TOOL,
237 g_value_get_string (value));
240 rtp_source_set_sdes_string (src, GST_RTCP_SDES_NOTE,
241 g_value_get_string (value));
244 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
250 rtp_source_get_property (GObject * object, guint prop_id,
251 GValue * value, GParamSpec * pspec)
255 src = RTP_SOURCE (object);
259 g_value_set_uint (value, rtp_source_get_ssrc (src));
262 g_value_set_boolean (value, rtp_source_is_as_csrc (src));
264 case PROP_IS_VALIDATED:
265 g_value_set_boolean (value, rtp_source_is_validated (src));
268 g_value_set_boolean (value, rtp_source_is_sender (src));
270 case PROP_SDES_CNAME:
271 g_value_take_string (value, rtp_source_get_sdes_string (src,
272 GST_RTCP_SDES_CNAME));
275 g_value_take_string (value, rtp_source_get_sdes_string (src,
276 GST_RTCP_SDES_NAME));
278 case PROP_SDES_EMAIL:
279 g_value_take_string (value, rtp_source_get_sdes_string (src,
280 GST_RTCP_SDES_EMAIL));
282 case PROP_SDES_PHONE:
283 g_value_take_string (value, rtp_source_get_sdes_string (src,
284 GST_RTCP_SDES_PHONE));
286 case PROP_SDES_LOCATION:
287 g_value_take_string (value, rtp_source_get_sdes_string (src,
291 g_value_take_string (value, rtp_source_get_sdes_string (src,
292 GST_RTCP_SDES_TOOL));
295 g_value_take_string (value, rtp_source_get_sdes_string (src,
296 GST_RTCP_SDES_NOTE));
299 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
308 * Create a #RTPSource with @ssrc.
310 * Returns: a new #RTPSource. Use g_object_unref() after usage.
313 rtp_source_new (guint32 ssrc)
317 src = g_object_new (RTP_TYPE_SOURCE, NULL);
324 * rtp_source_set_callbacks:
325 * @src: an #RTPSource
326 * @cb: callback functions
327 * @user_data: user data
329 * Set the callbacks for the source.
332 rtp_source_set_callbacks (RTPSource * src, RTPSourceCallbacks * cb,
335 g_return_if_fail (RTP_IS_SOURCE (src));
337 src->callbacks.push_rtp = cb->push_rtp;
338 src->callbacks.clock_rate = cb->clock_rate;
339 src->user_data = user_data;
343 * rtp_source_get_ssrc:
344 * @src: an #RTPSource
346 * Get the SSRC of @source.
348 * Returns: the SSRC of src.
351 rtp_source_get_ssrc (RTPSource * src)
355 g_return_val_if_fail (RTP_IS_SOURCE (src), 0);
363 * rtp_source_set_as_csrc:
364 * @src: an #RTPSource
366 * Configure @src as a CSRC, this will also validate @src.
369 rtp_source_set_as_csrc (RTPSource * src)
371 g_return_if_fail (RTP_IS_SOURCE (src));
373 src->validated = TRUE;
378 * rtp_source_is_as_csrc:
379 * @src: an #RTPSource
381 * Check if @src is a contributing source.
383 * Returns: %TRUE if @src is acting as a contributing source.
386 rtp_source_is_as_csrc (RTPSource * src)
390 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
392 result = src->is_csrc;
398 * rtp_source_is_active:
399 * @src: an #RTPSource
401 * Check if @src is an active source. A source is active if it has been
402 * validated and has not yet received a BYE packet
404 * Returns: %TRUE if @src is an qactive source.
407 rtp_source_is_active (RTPSource * src)
411 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
413 result = RTP_SOURCE_IS_ACTIVE (src);
419 * rtp_source_is_validated:
420 * @src: an #RTPSource
422 * Check if @src is a validated source.
424 * Returns: %TRUE if @src is a validated source.
427 rtp_source_is_validated (RTPSource * src)
431 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
433 result = src->validated;
439 * rtp_source_is_sender:
440 * @src: an #RTPSource
442 * Check if @src is a sending source.
444 * Returns: %TRUE if @src is a sending source.
447 rtp_source_is_sender (RTPSource * src)
451 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
453 result = RTP_SOURCE_IS_SENDER (src);
459 * rtp_source_received_bye:
460 * @src: an #RTPSource
462 * Check if @src has receoved a BYE packet.
464 * Returns: %TRUE if @src has received a BYE packet.
467 rtp_source_received_bye (RTPSource * src)
471 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
473 result = src->received_bye;
480 * rtp_source_get_bye_reason:
481 * @src: an #RTPSource
483 * Get the BYE reason for @src. Check if the source receoved a BYE message first
484 * with rtp_source_received_bye().
486 * Returns: The BYE reason or NULL when no reason was given or the source did
487 * not receive a BYE message yet. g_fee() after usage.
490 rtp_source_get_bye_reason (RTPSource * src)
494 g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
496 result = g_strdup (src->bye_reason);
502 * rtp_source_update_caps:
503 * @src: an #RTPSource
506 * Parse @caps and store all relevant information in @source.
509 rtp_source_update_caps (RTPSource * src, GstCaps * caps)
515 /* nothing changed, return */
516 if (src->caps == caps)
519 s = gst_caps_get_structure (caps, 0);
521 if (gst_structure_get_int (s, "payload", &ival))
523 GST_DEBUG ("got payload %d", src->payload);
525 gst_structure_get_int (s, "clock-rate", &src->clock_rate);
526 GST_DEBUG ("got clock-rate %d", src->clock_rate);
528 if (gst_structure_get_uint (s, "clock-base", &val))
529 src->clock_base = val;
530 GST_DEBUG ("got clock-base %" G_GINT64_FORMAT, src->clock_base);
532 if (gst_structure_get_uint (s, "seqnum-base", &val))
533 src->seqnum_base = val;
534 GST_DEBUG ("got seqnum-base %" G_GINT32_FORMAT, src->seqnum_base);
536 gst_caps_replace (&src->caps, caps);
540 * rtp_source_set_sdes:
541 * @src: an #RTPSource
542 * @type: the type of the SDES item
543 * @data: the SDES data
544 * @len: the SDES length
546 * Store an SDES item of @type in @src.
548 * Returns: %FALSE if the SDES item was unchanged or @type is unknown.
551 rtp_source_set_sdes (RTPSource * src, GstRTCPSDESType type,
552 const guint8 * data, guint len)
556 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
558 if (type < 0 || type > GST_RTCP_SDES_PRIV)
561 old = src->sdes[type];
563 /* lengths are the same, check if the data is the same */
564 if ((src->sdes_len[type] == len))
565 if (data != NULL && old != NULL && (memcmp (old, data, len) == 0))
568 /* NULL data, make sure we store 0 length or if no length is given,
573 g_free (src->sdes[type]);
574 src->sdes[type] = g_memdup (data, len);
575 src->sdes_len[type] = len;
581 * rtp_source_set_sdes_string:
582 * @src: an #RTPSource
583 * @type: the type of the SDES item
584 * @data: the SDES data
586 * Store an SDES item of @type in @src. This function is similar to
587 * rtp_source_set_sdes() but takes a null-terminated string for convenience.
589 * Returns: %FALSE if the SDES item was unchanged or @type is unknown.
592 rtp_source_set_sdes_string (RTPSource * src, GstRTCPSDESType type,
603 result = rtp_source_set_sdes (src, type, (guint8 *) data, len);
609 * rtp_source_get_sdes:
610 * @src: an #RTPSource
611 * @type: the type of the SDES item
612 * @data: location to store the SDES data or NULL
613 * @len: location to store the SDES length or NULL
615 * Get the SDES item of @type from @src. Note that @data does not always point
616 * to a null-terminated string, use rtp_source_get_sdes_string() to retrieve a
617 * null-terminated string instead.
619 * @data remains valid until the next call to rtp_source_set_sdes().
621 * Returns: %TRUE if @type was valid and @data and @len contain valid
622 * data. @data can be NULL when the item was unset.
625 rtp_source_get_sdes (RTPSource * src, GstRTCPSDESType type, guint8 ** data,
628 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
630 if (type < 0 || type > GST_RTCP_SDES_PRIV)
634 *data = src->sdes[type];
636 *len = src->sdes_len[type];
642 * rtp_source_get_sdes_string:
643 * @src: an #RTPSource
644 * @type: the type of the SDES item
646 * Get the SDES item of @type from @src.
648 * Returns: a null-terminated copy of the SDES item or NULL when @type was not
649 * valid or the SDES item was unset. g_free() after usage.
652 rtp_source_get_sdes_string (RTPSource * src, GstRTCPSDESType type)
656 g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
658 if (type < 0 || type > GST_RTCP_SDES_PRIV)
661 result = g_strndup ((const gchar *) src->sdes[type], src->sdes_len[type]);
667 * rtp_source_set_rtp_from:
668 * @src: an #RTPSource
669 * @address: the RTP address to set
671 * Set that @src is receiving RTP packets from @address. This is used for
672 * collistion checking.
675 rtp_source_set_rtp_from (RTPSource * src, GstNetAddress * address)
677 g_return_if_fail (RTP_IS_SOURCE (src));
679 src->have_rtp_from = TRUE;
680 memcpy (&src->rtp_from, address, sizeof (GstNetAddress));
684 * rtp_source_set_rtcp_from:
685 * @src: an #RTPSource
686 * @address: the RTCP address to set
688 * Set that @src is receiving RTCP packets from @address. This is used for
689 * collistion checking.
692 rtp_source_set_rtcp_from (RTPSource * src, GstNetAddress * address)
694 g_return_if_fail (RTP_IS_SOURCE (src));
696 src->have_rtcp_from = TRUE;
697 memcpy (&src->rtcp_from, address, sizeof (GstNetAddress));
701 push_packet (RTPSource * src, GstBuffer * buffer)
703 GstFlowReturn ret = GST_FLOW_OK;
705 /* push queued packets first if any */
706 while (!g_queue_is_empty (src->packets)) {
707 GstBuffer *buffer = GST_BUFFER_CAST (g_queue_pop_head (src->packets));
709 GST_DEBUG ("pushing queued packet");
710 if (src->callbacks.push_rtp)
711 src->callbacks.push_rtp (src, buffer, src->user_data);
713 gst_buffer_unref (buffer);
715 GST_DEBUG ("pushing new packet");
717 if (src->callbacks.push_rtp)
718 ret = src->callbacks.push_rtp (src, buffer, src->user_data);
720 gst_buffer_unref (buffer);
726 get_clock_rate (RTPSource * src, guint8 payload)
728 if (src->clock_rate == -1) {
729 gint clock_rate = -1;
731 if (src->callbacks.clock_rate)
732 clock_rate = src->callbacks.clock_rate (src, payload, src->user_data);
734 GST_DEBUG ("new payload %d, got clock-rate %d", payload, clock_rate);
736 src->clock_rate = clock_rate;
738 src->payload = payload;
740 return src->clock_rate;
743 /* Jitter is the variation in the delay of received packets in a flow. It is
744 * measured by comparing the interval when RTP packets were sent to the interval
745 * at which they were received. For instance, if packet #1 and packet #2 leave
746 * 50 milliseconds apart and arrive 60 milliseconds apart, then the jitter is 10
749 calculate_jitter (RTPSource * src, GstBuffer * buffer,
750 RTPArrivalStats * arrival)
753 guint32 rtparrival, transit, rtptime;
758 /* get arrival time */
759 if ((ntpnstime = arrival->ntpnstime) == GST_CLOCK_TIME_NONE)
762 pt = gst_rtp_buffer_get_payload_type (buffer);
764 GST_DEBUG ("SSRC %08x got payload %d", src->ssrc, pt);
767 if ((clock_rate = get_clock_rate (src, pt)) == -1)
770 rtptime = gst_rtp_buffer_get_timestamp (buffer);
772 /* no clock-base, take first rtptime as base */
773 if (src->clock_base == -1) {
774 GST_DEBUG ("using clock-base of %" G_GUINT32_FORMAT, rtptime);
775 src->clock_base = rtptime;
776 src->clock_base_time = arrival->timestamp;
779 /* convert arrival time to RTP timestamp units, truncate to 32 bits, we don't
780 * care about the absolute value, just the difference. */
781 rtparrival = gst_util_uint64_scale_int (ntpnstime, clock_rate, GST_SECOND);
783 /* transit time is difference with RTP timestamp */
784 transit = rtparrival - rtptime;
786 /* get ABS diff with previous transit time */
787 if (src->stats.transit != -1) {
788 if (transit > src->stats.transit)
789 diff = transit - src->stats.transit;
791 diff = src->stats.transit - transit;
795 src->stats.transit = transit;
797 /* update jitter, the value we store is scaled up so we can keep precision. */
798 src->stats.jitter += diff - ((src->stats.jitter + 8) >> 4);
800 src->stats.prev_rtptime = src->stats.last_rtptime;
801 src->stats.last_rtptime = rtparrival;
803 GST_DEBUG ("rtparrival %u, rtptime %u, clock-rate %d, diff %d, jitter: %f",
804 rtparrival, rtptime, clock_rate, diff, (src->stats.jitter) / 16.0);
811 GST_WARNING ("cannot get current time");
816 GST_WARNING ("cannot get clock-rate for pt %d", pt);
822 init_seq (RTPSource * src, guint16 seq)
824 src->stats.base_seq = seq;
825 src->stats.max_seq = seq;
826 src->stats.bad_seq = RTP_SEQ_MOD + 1; /* so seq == bad_seq is false */
827 src->stats.cycles = 0;
828 src->stats.packets_received = 0;
829 src->stats.octets_received = 0;
830 src->stats.bytes_received = 0;
831 src->stats.prev_received = 0;
832 src->stats.prev_expected = 0;
834 GST_DEBUG ("base_seq %d", seq);
838 * rtp_source_process_rtp:
839 * @src: an #RTPSource
840 * @buffer: an RTP buffer
842 * Let @src handle the incomming RTP @buffer.
844 * Returns: a #GstFlowReturn.
847 rtp_source_process_rtp (RTPSource * src, GstBuffer * buffer,
848 RTPArrivalStats * arrival)
850 GstFlowReturn result = GST_FLOW_OK;
851 guint16 seqnr, udelta;
852 RTPSourceStats *stats;
854 g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
855 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
859 seqnr = gst_rtp_buffer_get_seq (buffer);
861 rtp_source_update_caps (src, GST_BUFFER_CAPS (buffer));
863 if (stats->cycles == -1) {
864 GST_DEBUG ("received first buffer");
865 /* first time we heard of this source */
866 init_seq (src, seqnr);
867 src->stats.max_seq = seqnr - 1;
868 src->probation = RTP_DEFAULT_PROBATION;
871 udelta = seqnr - stats->max_seq;
873 /* if we are still on probation, check seqnum */
874 if (src->probation) {
877 expected = src->stats.max_seq + 1;
879 /* when in probation, we require consecutive seqnums */
880 if (seqnr == expected) {
881 /* expected packet */
882 GST_DEBUG ("probation: seqnr %d == expected %d", seqnr, expected);
884 src->stats.max_seq = seqnr;
885 if (src->probation == 0) {
886 GST_DEBUG ("probation done!");
887 init_seq (src, seqnr);
891 GST_DEBUG ("probation %d: queue buffer", src->probation);
892 /* when still in probation, keep packets in a list. */
893 g_queue_push_tail (src->packets, buffer);
894 /* remove packets from queue if there are too many */
895 while (g_queue_get_length (src->packets) > RTP_MAX_PROBATION_LEN) {
896 q = g_queue_pop_head (src->packets);
897 gst_buffer_unref (q);
902 GST_DEBUG ("probation: seqnr %d != expected %d", seqnr, expected);
903 src->probation = RTP_DEFAULT_PROBATION;
904 src->stats.max_seq = seqnr;
907 } else if (udelta < RTP_MAX_DROPOUT) {
908 /* in order, with permissible gap */
909 if (seqnr < stats->max_seq) {
910 /* sequence number wrapped - count another 64K cycle. */
911 stats->cycles += RTP_SEQ_MOD;
913 stats->max_seq = seqnr;
914 } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
915 /* the sequence number made a very large jump */
916 if (seqnr == stats->bad_seq) {
917 /* two sequential packets -- assume that the other side
918 * restarted without telling us so just re-sync
919 * (i.e., pretend this was the first packet). */
920 init_seq (src, seqnr);
922 /* unacceptable jump */
923 stats->bad_seq = (seqnr + 1) & (RTP_SEQ_MOD - 1);
927 /* duplicate or reordered packet, will be filtered by jitterbuffer. */
928 GST_WARNING ("duplicate or reordered packet");
931 src->stats.octets_received += arrival->payload_len;
932 src->stats.bytes_received += arrival->bytes;
933 src->stats.packets_received++;
934 /* the source that sent the packet must be a sender */
935 src->is_sender = TRUE;
936 src->validated = TRUE;
938 GST_DEBUG ("seq %d, PC: %" G_GUINT64_FORMAT ", OC: %" G_GUINT64_FORMAT,
939 seqnr, src->stats.packets_received, src->stats.octets_received);
941 /* calculate jitter and perform skew correction */
942 calculate_jitter (src, buffer, arrival);
944 /* we're ready to push the RTP packet now */
945 result = push_packet (src, buffer);
953 GST_WARNING ("unacceptable seqnum received");
959 * rtp_source_process_bye:
960 * @src: an #RTPSource
961 * @reason: the reason for leaving
963 * Notify @src that a BYE packet has been received. This will make the source
967 rtp_source_process_bye (RTPSource * src, const gchar * reason)
969 g_return_if_fail (RTP_IS_SOURCE (src));
971 GST_DEBUG ("marking SSRC %08x as BYE, reason: %s", src->ssrc,
972 GST_STR_NULL (reason));
974 /* copy the reason and mark as received_bye */
975 g_free (src->bye_reason);
976 src->bye_reason = g_strdup (reason);
977 src->received_bye = TRUE;
981 * rtp_source_send_rtp:
982 * @src: an #RTPSource
983 * @buffer: an RTP buffer
984 * @ntpnstime: the NTP time when this buffer was captured in nanoseconds
986 * Send an RTP @buffer originating from @src. This will make @src a sender.
987 * This function takes ownership of @buffer and modifies the SSRC in the RTP
988 * packet to that of @src when needed.
990 * Returns: a #GstFlowReturn.
993 rtp_source_send_rtp (RTPSource * src, GstBuffer * buffer, guint64 ntpnstime)
995 GstFlowReturn result = GST_FLOW_OK;
999 guint64 ntp_diff, rtp_diff;
1001 g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
1002 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1004 len = gst_rtp_buffer_get_payload_len (buffer);
1006 rtp_source_update_caps (src, GST_BUFFER_CAPS (buffer));
1008 /* we are a sender now */
1009 src->is_sender = TRUE;
1011 /* update stats for the SR */
1012 src->stats.packets_sent++;
1013 src->stats.octets_sent += len;
1015 rtptime = gst_rtp_buffer_get_timestamp (buffer);
1016 ext_rtptime = src->last_rtptime;
1017 ext_rtptime = gst_rtp_buffer_ext_timestamp (&ext_rtptime, rtptime);
1019 GST_DEBUG ("SSRC %08x, RTP %" G_GUINT64_FORMAT ", NTP %" GST_TIME_FORMAT,
1020 src->ssrc, ext_rtptime, GST_TIME_ARGS (ntpnstime));
1022 if (ext_rtptime > src->last_rtptime) {
1023 rtp_diff = ext_rtptime - src->last_rtptime;
1024 ntp_diff = ntpnstime - src->last_ntpnstime;
1026 /* calc the diff so we can detect drift at the sender. This can also be used
1027 * to guestimate the clock rate if the NTP time is locked to the RTP
1028 * timestamps (as is the case when the capture device is providing the clock). */
1029 GST_DEBUG ("SSRC %08x, diff RTP %" G_GUINT64_FORMAT ", diff NTP %"
1030 GST_TIME_FORMAT, src->ssrc, rtp_diff, GST_TIME_ARGS (ntp_diff));
1033 /* we keep track of the last received RTP timestamp and the corresponding
1034 * NTP timestamp so that we can use this info when constructing SR reports */
1035 src->last_rtptime = ext_rtptime;
1036 src->last_ntpnstime = ntpnstime;
1039 if (src->callbacks.push_rtp) {
1042 ssrc = gst_rtp_buffer_get_ssrc (buffer);
1043 if (ssrc != src->ssrc) {
1044 /* the SSRC of the packet is not correct, make a writable buffer and
1045 * update the SSRC. This could involve a complete copy of the packet when
1046 * it is not writable. Usually the payloader will use caps negotiation to
1047 * get the correct SSRC from the session manager before pushing anything. */
1048 buffer = gst_buffer_make_writable (buffer);
1050 GST_WARNING ("updating SSRC from %08x to %08x, fix the payloader", ssrc,
1052 gst_rtp_buffer_set_ssrc (buffer, src->ssrc);
1054 GST_DEBUG ("pushing RTP packet %" G_GUINT64_FORMAT,
1055 src->stats.packets_sent);
1056 result = src->callbacks.push_rtp (src, buffer, src->user_data);
1058 GST_WARNING ("no callback installed, dropping packet");
1059 gst_buffer_unref (buffer);
1066 * rtp_source_process_sr:
1067 * @src: an #RTPSource
1068 * @time: time of packet arrival
1069 * @ntptime: the NTP time in 32.32 fixed point
1070 * @rtptime: the RTP time
1071 * @packet_count: the packet count
1072 * @octet_count: the octect count
1074 * Update the sender report in @src.
1077 rtp_source_process_sr (RTPSource * src, GstClockTime time, guint64 ntptime,
1078 guint32 rtptime, guint32 packet_count, guint32 octet_count)
1080 RTPSenderReport *curr;
1083 g_return_if_fail (RTP_IS_SOURCE (src));
1085 GST_DEBUG ("got SR packet: SSRC %08x, NTP %08x:%08x, RTP %" G_GUINT32_FORMAT
1086 ", PC %" G_GUINT32_FORMAT ", OC %" G_GUINT32_FORMAT, src->ssrc,
1087 (guint32) (ntptime >> 32), (guint32) (ntptime & 0xffffffff), rtptime,
1088 packet_count, octet_count);
1090 curridx = src->stats.curr_sr ^ 1;
1091 curr = &src->stats.sr[curridx];
1093 /* this is a sender now */
1094 src->is_sender = TRUE;
1096 /* update current */
1097 curr->is_valid = TRUE;
1098 curr->ntptime = ntptime;
1099 curr->rtptime = rtptime;
1100 curr->packet_count = packet_count;
1101 curr->octet_count = octet_count;
1105 src->stats.curr_sr = curridx;
1109 * rtp_source_process_rb:
1110 * @src: an #RTPSource
1111 * @time: the current time in nanoseconds since 1970
1112 * @fractionlost: fraction lost since last SR/RR
1113 * @packetslost: the cumululative number of packets lost
1114 * @exthighestseq: the extended last sequence number received
1115 * @jitter: the interarrival jitter
1116 * @lsr: the last SR packet from this source
1117 * @dlsr: the delay since last SR packet
1119 * Update the report block in @src.
1122 rtp_source_process_rb (RTPSource * src, GstClockTime time, guint8 fractionlost,
1123 gint32 packetslost, guint32 exthighestseq, guint32 jitter, guint32 lsr,
1126 RTPReceiverReport *curr;
1130 g_return_if_fail (RTP_IS_SOURCE (src));
1132 GST_DEBUG ("got RB packet: SSRC %08x, FL %2x, PL %d, HS %" G_GUINT32_FORMAT
1133 ", jitter %" G_GUINT32_FORMAT ", LSR %04x:%04x, DLSR %04x:%04x",
1134 src->ssrc, fractionlost, packetslost, exthighestseq, jitter, lsr >> 16,
1135 lsr & 0xffff, dlsr >> 16, dlsr & 0xffff);
1137 curridx = src->stats.curr_rr ^ 1;
1138 curr = &src->stats.rr[curridx];
1140 /* update current */
1141 curr->is_valid = TRUE;
1142 curr->fractionlost = fractionlost;
1143 curr->packetslost = packetslost;
1144 curr->exthighestseq = exthighestseq;
1145 curr->jitter = jitter;
1149 /* calculate round trip */
1150 ntp = (gst_rtcp_unix_to_ntp (time) >> 16) & 0xffffffff;
1153 curr->round_trip = A;
1155 GST_DEBUG ("NTP %04x:%04x, round trip %04x:%04x", ntp >> 16, ntp & 0xffff,
1156 A >> 16, A & 0xffff);
1159 src->stats.curr_rr = curridx;
1163 * rtp_source_get_new_sr:
1164 * @src: an #RTPSource
1165 * @ntpnstime: the current time in nanoseconds since 1970
1166 * @ntptime: the NTP time in 32.32 fixed point
1167 * @rtptime: the RTP time corresponding to @ntptime
1168 * @packet_count: the packet count
1169 * @octet_count: the octect count
1171 * Get new values to put into a new SR report from this source.
1173 * Returns: %TRUE on success.
1176 rtp_source_get_new_sr (RTPSource * src, guint64 ntpnstime,
1177 guint64 * ntptime, guint32 * rtptime, guint32 * packet_count,
1178 guint32 * octet_count)
1181 guint64 t_current_ntp;
1182 GstClockTimeDiff diff;
1184 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1186 /* use the sync params to interpollate the date->time member to rtptime. We
1187 * use the last sent timestamp and rtptime as reference points. We assume
1188 * that the slope of the rtptime vs timestamp curve is 1, which is certainly
1189 * sufficient for the frequency at which we report SR and the rate we send
1190 * out RTP packets. */
1191 t_rtp = src->last_rtptime;
1193 GST_DEBUG ("last_ntpnstime %" GST_TIME_FORMAT ", last_rtptime %"
1194 G_GUINT64_FORMAT, GST_TIME_ARGS (src->last_ntpnstime), t_rtp);
1196 if (src->clock_rate != -1) {
1197 /* get the diff with the SR time */
1198 diff = GST_CLOCK_DIFF (src->last_ntpnstime, ntpnstime);
1200 /* now translate the diff to RTP time, handle positive and negative cases.
1201 * If there is no diff, we already set rtptime correctly above. */
1203 GST_DEBUG ("ntpnstime %" GST_TIME_FORMAT ", diff %" GST_TIME_FORMAT,
1204 GST_TIME_ARGS (ntpnstime), GST_TIME_ARGS (diff));
1205 t_rtp += gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1208 GST_DEBUG ("ntpnstime %" GST_TIME_FORMAT ", diff -%" GST_TIME_FORMAT,
1209 GST_TIME_ARGS (ntpnstime), GST_TIME_ARGS (diff));
1210 t_rtp -= gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1213 GST_WARNING ("no clock-rate, cannot interpollate rtp time");
1216 /* convert the NTP time in nanoseconds to 32.32 fixed point */
1217 t_current_ntp = gst_util_uint64_scale (ntpnstime, (1LL << 32), GST_SECOND);
1219 GST_DEBUG ("NTP %08x:%08x, RTP %" G_GUINT32_FORMAT,
1220 (guint32) (t_current_ntp >> 32), (guint32) (t_current_ntp & 0xffffffff),
1224 *ntptime = t_current_ntp;
1228 *packet_count = src->stats.packets_sent;
1230 *octet_count = src->stats.octets_sent;
1236 * rtp_source_get_new_rb:
1237 * @src: an #RTPSource
1238 * @ntpnstime: the current time in nanoseconds since 1970
1239 * @fractionlost: fraction lost since last SR/RR
1240 * @packetslost: the cumululative number of packets lost
1241 * @exthighestseq: the extended last sequence number received
1242 * @jitter: the interarrival jitter
1243 * @lsr: the last SR packet from this source
1244 * @dlsr: the delay since last SR packet
1246 * Get new values to put into a new report block from this source.
1248 * Returns: %TRUE on success.
1251 rtp_source_get_new_rb (RTPSource * src, guint64 ntpnstime,
1252 guint8 * fractionlost, gint32 * packetslost, guint32 * exthighestseq,
1253 guint32 * jitter, guint32 * lsr, guint32 * dlsr)
1255 RTPSourceStats *stats;
1256 guint64 extended_max, expected;
1257 guint64 expected_interval, received_interval, ntptime;
1258 gint64 lost, lost_interval;
1259 guint32 fraction, LSR, DLSR;
1260 GstClockTime sr_time;
1262 stats = &src->stats;
1264 extended_max = stats->cycles + stats->max_seq;
1265 expected = extended_max - stats->base_seq + 1;
1267 GST_DEBUG ("ext_max %" G_GUINT64_FORMAT ", expected %" G_GUINT64_FORMAT
1268 ", received %" G_GUINT64_FORMAT ", base_seq %" G_GUINT32_FORMAT,
1269 extended_max, expected, stats->packets_received, stats->base_seq);
1271 lost = expected - stats->packets_received;
1272 lost = CLAMP (lost, -0x800000, 0x7fffff);
1274 expected_interval = expected - stats->prev_expected;
1275 stats->prev_expected = expected;
1276 received_interval = stats->packets_received - stats->prev_received;
1277 stats->prev_received = stats->packets_received;
1279 lost_interval = expected_interval - received_interval;
1281 if (expected_interval == 0 || lost_interval <= 0)
1284 fraction = (lost_interval << 8) / expected_interval;
1286 GST_DEBUG ("add RR for SSRC %08x", src->ssrc);
1287 /* we scaled the jitter up for additional precision */
1288 GST_DEBUG ("fraction %" G_GUINT32_FORMAT ", lost %" G_GINT64_FORMAT
1289 ", extseq %" G_GUINT64_FORMAT ", jitter %d", fraction, lost,
1290 extended_max, stats->jitter >> 4);
1292 if (rtp_source_get_last_sr (src, &sr_time, &ntptime, NULL, NULL, NULL)) {
1295 /* LSR is middle 32 bits of the last ntptime */
1296 LSR = (ntptime >> 16) & 0xffffffff;
1297 diff = ntpnstime - sr_time;
1298 GST_DEBUG ("last SR time diff %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
1299 /* DLSR, delay since last SR is expressed in 1/65536 second units */
1300 DLSR = gst_util_uint64_scale_int (diff, 65536, GST_SECOND);
1302 /* No valid SR received, LSR/DLSR are set to 0 then */
1303 GST_DEBUG ("no valid SR received");
1307 GST_DEBUG ("LSR %04x:%04x, DLSR %04x:%04x", LSR >> 16, LSR & 0xffff,
1308 DLSR >> 16, DLSR & 0xffff);
1311 *fractionlost = fraction;
1313 *packetslost = lost;
1315 *exthighestseq = extended_max;
1317 *jitter = stats->jitter >> 4;
1327 * rtp_source_get_last_sr:
1328 * @src: an #RTPSource
1329 * @time: time of packet arrival
1330 * @ntptime: the NTP time in 32.32 fixed point
1331 * @rtptime: the RTP time
1332 * @packet_count: the packet count
1333 * @octet_count: the octect count
1335 * Get the values of the last sender report as set with rtp_source_process_sr().
1337 * Returns: %TRUE if there was a valid SR report.
1340 rtp_source_get_last_sr (RTPSource * src, GstClockTime * time, guint64 * ntptime,
1341 guint32 * rtptime, guint32 * packet_count, guint32 * octet_count)
1343 RTPSenderReport *curr;
1345 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1347 curr = &src->stats.sr[src->stats.curr_sr];
1348 if (!curr->is_valid)
1352 *ntptime = curr->ntptime;
1354 *rtptime = curr->rtptime;
1356 *packet_count = curr->packet_count;
1358 *octet_count = curr->octet_count;
1366 * rtp_source_get_last_rb:
1367 * @src: an #RTPSource
1368 * @fractionlost: fraction lost since last SR/RR
1369 * @packetslost: the cumululative number of packets lost
1370 * @exthighestseq: the extended last sequence number received
1371 * @jitter: the interarrival jitter
1372 * @lsr: the last SR packet from this source
1373 * @dlsr: the delay since last SR packet
1375 * Get the values of the last RB report set with rtp_source_process_rb().
1377 * Returns: %TRUE if there was a valid SB report.
1380 rtp_source_get_last_rb (RTPSource * src, guint8 * fractionlost,
1381 gint32 * packetslost, guint32 * exthighestseq, guint32 * jitter,
1382 guint32 * lsr, guint32 * dlsr)
1384 RTPReceiverReport *curr;
1386 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1388 curr = &src->stats.rr[src->stats.curr_rr];
1389 if (!curr->is_valid)
1393 *fractionlost = curr->fractionlost;
1395 *packetslost = curr->packetslost;
1397 *exthighestseq = curr->exthighestseq;
1399 *jitter = curr->jitter;