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->packets = g_queue_new ();
174 src->seqnum_base = -1;
175 src->last_rtptime = -1;
177 rtp_source_reset (src);
181 rtp_source_finalize (GObject * object)
187 src = RTP_SOURCE_CAST (object);
189 while ((buffer = g_queue_pop_head (src->packets)))
190 gst_buffer_unref (buffer);
191 g_queue_free (src->packets);
193 for (i = 0; i < 9; i++)
194 g_free (src->sdes[i]);
196 g_free (src->bye_reason);
198 gst_caps_replace (&src->caps, NULL);
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, "seqnum-base", &val))
529 src->seqnum_base = val;
530 GST_DEBUG ("got seqnum-base %" G_GINT32_FORMAT, src->seqnum_base);
532 gst_caps_replace (&src->caps, caps);
536 * rtp_source_set_sdes:
537 * @src: an #RTPSource
538 * @type: the type of the SDES item
539 * @data: the SDES data
540 * @len: the SDES length
542 * Store an SDES item of @type in @src.
544 * Returns: %FALSE if the SDES item was unchanged or @type is unknown.
547 rtp_source_set_sdes (RTPSource * src, GstRTCPSDESType type,
548 const guint8 * data, guint len)
552 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
554 if (type < 0 || type > GST_RTCP_SDES_PRIV)
557 old = src->sdes[type];
559 /* lengths are the same, check if the data is the same */
560 if ((src->sdes_len[type] == len))
561 if (data != NULL && old != NULL && (memcmp (old, data, len) == 0))
564 /* NULL data, make sure we store 0 length or if no length is given,
569 g_free (src->sdes[type]);
570 src->sdes[type] = g_memdup (data, len);
571 src->sdes_len[type] = len;
577 * rtp_source_set_sdes_string:
578 * @src: an #RTPSource
579 * @type: the type of the SDES item
580 * @data: the SDES data
582 * Store an SDES item of @type in @src. This function is similar to
583 * rtp_source_set_sdes() but takes a null-terminated string for convenience.
585 * Returns: %FALSE if the SDES item was unchanged or @type is unknown.
588 rtp_source_set_sdes_string (RTPSource * src, GstRTCPSDESType type,
599 result = rtp_source_set_sdes (src, type, (guint8 *) data, len);
605 * rtp_source_get_sdes:
606 * @src: an #RTPSource
607 * @type: the type of the SDES item
608 * @data: location to store the SDES data or NULL
609 * @len: location to store the SDES length or NULL
611 * Get the SDES item of @type from @src. Note that @data does not always point
612 * to a null-terminated string, use rtp_source_get_sdes_string() to retrieve a
613 * null-terminated string instead.
615 * @data remains valid until the next call to rtp_source_set_sdes().
617 * Returns: %TRUE if @type was valid and @data and @len contain valid
618 * data. @data can be NULL when the item was unset.
621 rtp_source_get_sdes (RTPSource * src, GstRTCPSDESType type, guint8 ** data,
624 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
626 if (type < 0 || type > GST_RTCP_SDES_PRIV)
630 *data = src->sdes[type];
632 *len = src->sdes_len[type];
638 * rtp_source_get_sdes_string:
639 * @src: an #RTPSource
640 * @type: the type of the SDES item
642 * Get the SDES item of @type from @src.
644 * Returns: a null-terminated copy of the SDES item or NULL when @type was not
645 * valid or the SDES item was unset. g_free() after usage.
648 rtp_source_get_sdes_string (RTPSource * src, GstRTCPSDESType type)
652 g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
654 if (type < 0 || type > GST_RTCP_SDES_PRIV)
657 result = g_strndup ((const gchar *) src->sdes[type], src->sdes_len[type]);
663 * rtp_source_set_rtp_from:
664 * @src: an #RTPSource
665 * @address: the RTP address to set
667 * Set that @src is receiving RTP packets from @address. This is used for
668 * collistion checking.
671 rtp_source_set_rtp_from (RTPSource * src, GstNetAddress * address)
673 g_return_if_fail (RTP_IS_SOURCE (src));
675 src->have_rtp_from = TRUE;
676 memcpy (&src->rtp_from, address, sizeof (GstNetAddress));
680 * rtp_source_set_rtcp_from:
681 * @src: an #RTPSource
682 * @address: the RTCP address to set
684 * Set that @src is receiving RTCP packets from @address. This is used for
685 * collistion checking.
688 rtp_source_set_rtcp_from (RTPSource * src, GstNetAddress * address)
690 g_return_if_fail (RTP_IS_SOURCE (src));
692 src->have_rtcp_from = TRUE;
693 memcpy (&src->rtcp_from, address, sizeof (GstNetAddress));
697 push_packet (RTPSource * src, GstBuffer * buffer)
699 GstFlowReturn ret = GST_FLOW_OK;
701 /* push queued packets first if any */
702 while (!g_queue_is_empty (src->packets)) {
703 GstBuffer *buffer = GST_BUFFER_CAST (g_queue_pop_head (src->packets));
705 GST_LOG ("pushing queued packet");
706 if (src->callbacks.push_rtp)
707 src->callbacks.push_rtp (src, buffer, src->user_data);
709 gst_buffer_unref (buffer);
711 GST_LOG ("pushing new packet");
713 if (src->callbacks.push_rtp)
714 ret = src->callbacks.push_rtp (src, buffer, src->user_data);
716 gst_buffer_unref (buffer);
722 get_clock_rate (RTPSource * src, guint8 payload)
724 if (src->clock_rate == -1) {
725 gint clock_rate = -1;
727 if (src->callbacks.clock_rate)
728 clock_rate = src->callbacks.clock_rate (src, payload, src->user_data);
730 GST_DEBUG ("new payload %d, got clock-rate %d", payload, clock_rate);
732 src->clock_rate = clock_rate;
734 src->payload = payload;
736 return src->clock_rate;
739 /* Jitter is the variation in the delay of received packets in a flow. It is
740 * measured by comparing the interval when RTP packets were sent to the interval
741 * at which they were received. For instance, if packet #1 and packet #2 leave
742 * 50 milliseconds apart and arrive 60 milliseconds apart, then the jitter is 10
745 calculate_jitter (RTPSource * src, GstBuffer * buffer,
746 RTPArrivalStats * arrival)
749 guint32 rtparrival, transit, rtptime;
754 /* get arrival time */
755 if ((ntpnstime = arrival->ntpnstime) == GST_CLOCK_TIME_NONE)
758 pt = gst_rtp_buffer_get_payload_type (buffer);
760 GST_LOG ("SSRC %08x got payload %d", src->ssrc, pt);
763 if ((clock_rate = get_clock_rate (src, pt)) == -1)
766 rtptime = gst_rtp_buffer_get_timestamp (buffer);
768 /* convert arrival time to RTP timestamp units, truncate to 32 bits, we don't
769 * care about the absolute value, just the difference. */
770 rtparrival = gst_util_uint64_scale_int (ntpnstime, clock_rate, GST_SECOND);
772 /* transit time is difference with RTP timestamp */
773 transit = rtparrival - rtptime;
775 /* get ABS diff with previous transit time */
776 if (src->stats.transit != -1) {
777 if (transit > src->stats.transit)
778 diff = transit - src->stats.transit;
780 diff = src->stats.transit - transit;
784 src->stats.transit = transit;
786 /* update jitter, the value we store is scaled up so we can keep precision. */
787 src->stats.jitter += diff - ((src->stats.jitter + 8) >> 4);
789 src->stats.prev_rtptime = src->stats.last_rtptime;
790 src->stats.last_rtptime = rtparrival;
792 GST_LOG ("rtparrival %u, rtptime %u, clock-rate %d, diff %d, jitter: %f",
793 rtparrival, rtptime, clock_rate, diff, (src->stats.jitter) / 16.0);
800 GST_WARNING ("cannot get current time");
805 GST_WARNING ("cannot get clock-rate for pt %d", pt);
811 init_seq (RTPSource * src, guint16 seq)
813 src->stats.base_seq = seq;
814 src->stats.max_seq = seq;
815 src->stats.bad_seq = RTP_SEQ_MOD + 1; /* so seq == bad_seq is false */
816 src->stats.cycles = 0;
817 src->stats.packets_received = 0;
818 src->stats.octets_received = 0;
819 src->stats.bytes_received = 0;
820 src->stats.prev_received = 0;
821 src->stats.prev_expected = 0;
823 GST_DEBUG ("base_seq %d", seq);
827 * rtp_source_process_rtp:
828 * @src: an #RTPSource
829 * @buffer: an RTP buffer
831 * Let @src handle the incomming RTP @buffer.
833 * Returns: a #GstFlowReturn.
836 rtp_source_process_rtp (RTPSource * src, GstBuffer * buffer,
837 RTPArrivalStats * arrival)
839 GstFlowReturn result = GST_FLOW_OK;
840 guint16 seqnr, udelta;
841 RTPSourceStats *stats;
843 g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
844 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
848 seqnr = gst_rtp_buffer_get_seq (buffer);
850 rtp_source_update_caps (src, GST_BUFFER_CAPS (buffer));
852 if (stats->cycles == -1) {
853 GST_DEBUG ("received first buffer");
854 /* first time we heard of this source */
855 init_seq (src, seqnr);
856 src->stats.max_seq = seqnr - 1;
857 src->probation = RTP_DEFAULT_PROBATION;
860 udelta = seqnr - stats->max_seq;
862 /* if we are still on probation, check seqnum */
863 if (src->probation) {
866 expected = src->stats.max_seq + 1;
868 /* when in probation, we require consecutive seqnums */
869 if (seqnr == expected) {
870 /* expected packet */
871 GST_DEBUG ("probation: seqnr %d == expected %d", seqnr, expected);
873 src->stats.max_seq = seqnr;
874 if (src->probation == 0) {
875 GST_DEBUG ("probation done!");
876 init_seq (src, seqnr);
880 GST_DEBUG ("probation %d: queue buffer", src->probation);
881 /* when still in probation, keep packets in a list. */
882 g_queue_push_tail (src->packets, buffer);
883 /* remove packets from queue if there are too many */
884 while (g_queue_get_length (src->packets) > RTP_MAX_PROBATION_LEN) {
885 q = g_queue_pop_head (src->packets);
886 gst_buffer_unref (q);
891 GST_DEBUG ("probation: seqnr %d != expected %d", seqnr, expected);
892 src->probation = RTP_DEFAULT_PROBATION;
893 src->stats.max_seq = seqnr;
896 } else if (udelta < RTP_MAX_DROPOUT) {
897 /* in order, with permissible gap */
898 if (seqnr < stats->max_seq) {
899 /* sequence number wrapped - count another 64K cycle. */
900 stats->cycles += RTP_SEQ_MOD;
902 stats->max_seq = seqnr;
903 } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
904 /* the sequence number made a very large jump */
905 if (seqnr == stats->bad_seq) {
906 /* two sequential packets -- assume that the other side
907 * restarted without telling us so just re-sync
908 * (i.e., pretend this was the first packet). */
909 init_seq (src, seqnr);
911 /* unacceptable jump */
912 stats->bad_seq = (seqnr + 1) & (RTP_SEQ_MOD - 1);
916 /* duplicate or reordered packet, will be filtered by jitterbuffer. */
917 GST_WARNING ("duplicate or reordered packet");
920 src->stats.octets_received += arrival->payload_len;
921 src->stats.bytes_received += arrival->bytes;
922 src->stats.packets_received++;
923 /* the source that sent the packet must be a sender */
924 src->is_sender = TRUE;
925 src->validated = TRUE;
927 GST_LOG ("seq %d, PC: %" G_GUINT64_FORMAT ", OC: %" G_GUINT64_FORMAT,
928 seqnr, src->stats.packets_received, src->stats.octets_received);
930 /* calculate jitter for the stats */
931 calculate_jitter (src, buffer, arrival);
933 /* we're ready to push the RTP packet now */
934 result = push_packet (src, buffer);
942 GST_WARNING ("unacceptable seqnum received");
948 * rtp_source_process_bye:
949 * @src: an #RTPSource
950 * @reason: the reason for leaving
952 * Notify @src that a BYE packet has been received. This will make the source
956 rtp_source_process_bye (RTPSource * src, const gchar * reason)
958 g_return_if_fail (RTP_IS_SOURCE (src));
960 GST_DEBUG ("marking SSRC %08x as BYE, reason: %s", src->ssrc,
961 GST_STR_NULL (reason));
963 /* copy the reason and mark as received_bye */
964 g_free (src->bye_reason);
965 src->bye_reason = g_strdup (reason);
966 src->received_bye = TRUE;
970 * rtp_source_send_rtp:
971 * @src: an #RTPSource
972 * @buffer: an RTP buffer
973 * @ntpnstime: the NTP time when this buffer was captured in nanoseconds
975 * Send an RTP @buffer originating from @src. This will make @src a sender.
976 * This function takes ownership of @buffer and modifies the SSRC in the RTP
977 * packet to that of @src when needed.
979 * Returns: a #GstFlowReturn.
982 rtp_source_send_rtp (RTPSource * src, GstBuffer * buffer, guint64 ntpnstime)
984 GstFlowReturn result = GST_FLOW_OK;
988 guint64 ntp_diff, rtp_diff;
990 g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
991 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
993 len = gst_rtp_buffer_get_payload_len (buffer);
995 rtp_source_update_caps (src, GST_BUFFER_CAPS (buffer));
997 /* we are a sender now */
998 src->is_sender = TRUE;
1000 /* update stats for the SR */
1001 src->stats.packets_sent++;
1002 src->stats.octets_sent += len;
1004 rtptime = gst_rtp_buffer_get_timestamp (buffer);
1005 ext_rtptime = src->last_rtptime;
1006 ext_rtptime = gst_rtp_buffer_ext_timestamp (&ext_rtptime, rtptime);
1008 GST_LOG ("SSRC %08x, RTP %" G_GUINT64_FORMAT ", NTP %" GST_TIME_FORMAT,
1009 src->ssrc, ext_rtptime, GST_TIME_ARGS (ntpnstime));
1011 if (ext_rtptime > src->last_rtptime) {
1012 rtp_diff = ext_rtptime - src->last_rtptime;
1013 ntp_diff = ntpnstime - src->last_ntpnstime;
1015 /* calc the diff so we can detect drift at the sender. This can also be used
1016 * to guestimate the clock rate if the NTP time is locked to the RTP
1017 * timestamps (as is the case when the capture device is providing the clock). */
1018 GST_LOG ("SSRC %08x, diff RTP %" G_GUINT64_FORMAT ", diff NTP %"
1019 GST_TIME_FORMAT, src->ssrc, rtp_diff, GST_TIME_ARGS (ntp_diff));
1022 /* we keep track of the last received RTP timestamp and the corresponding
1023 * NTP timestamp so that we can use this info when constructing SR reports */
1024 src->last_rtptime = ext_rtptime;
1025 src->last_ntpnstime = ntpnstime;
1028 if (src->callbacks.push_rtp) {
1031 ssrc = gst_rtp_buffer_get_ssrc (buffer);
1032 if (ssrc != src->ssrc) {
1033 /* the SSRC of the packet is not correct, make a writable buffer and
1034 * update the SSRC. This could involve a complete copy of the packet when
1035 * it is not writable. Usually the payloader will use caps negotiation to
1036 * get the correct SSRC from the session manager before pushing anything. */
1037 buffer = gst_buffer_make_writable (buffer);
1039 GST_WARNING ("updating SSRC from %08x to %08x, fix the payloader", ssrc,
1041 gst_rtp_buffer_set_ssrc (buffer, src->ssrc);
1043 GST_LOG ("pushing RTP packet %" G_GUINT64_FORMAT, src->stats.packets_sent);
1044 result = src->callbacks.push_rtp (src, buffer, src->user_data);
1046 GST_WARNING ("no callback installed, dropping packet");
1047 gst_buffer_unref (buffer);
1054 * rtp_source_process_sr:
1055 * @src: an #RTPSource
1056 * @time: time of packet arrival
1057 * @ntptime: the NTP time in 32.32 fixed point
1058 * @rtptime: the RTP time
1059 * @packet_count: the packet count
1060 * @octet_count: the octect count
1062 * Update the sender report in @src.
1065 rtp_source_process_sr (RTPSource * src, GstClockTime time, guint64 ntptime,
1066 guint32 rtptime, guint32 packet_count, guint32 octet_count)
1068 RTPSenderReport *curr;
1071 g_return_if_fail (RTP_IS_SOURCE (src));
1073 GST_DEBUG ("got SR packet: SSRC %08x, NTP %08x:%08x, RTP %" G_GUINT32_FORMAT
1074 ", PC %" G_GUINT32_FORMAT ", OC %" G_GUINT32_FORMAT, src->ssrc,
1075 (guint32) (ntptime >> 32), (guint32) (ntptime & 0xffffffff), rtptime,
1076 packet_count, octet_count);
1078 curridx = src->stats.curr_sr ^ 1;
1079 curr = &src->stats.sr[curridx];
1081 /* this is a sender now */
1082 src->is_sender = TRUE;
1084 /* update current */
1085 curr->is_valid = TRUE;
1086 curr->ntptime = ntptime;
1087 curr->rtptime = rtptime;
1088 curr->packet_count = packet_count;
1089 curr->octet_count = octet_count;
1093 src->stats.curr_sr = curridx;
1097 * rtp_source_process_rb:
1098 * @src: an #RTPSource
1099 * @time: the current time in nanoseconds since 1970
1100 * @fractionlost: fraction lost since last SR/RR
1101 * @packetslost: the cumululative number of packets lost
1102 * @exthighestseq: the extended last sequence number received
1103 * @jitter: the interarrival jitter
1104 * @lsr: the last SR packet from this source
1105 * @dlsr: the delay since last SR packet
1107 * Update the report block in @src.
1110 rtp_source_process_rb (RTPSource * src, GstClockTime time, guint8 fractionlost,
1111 gint32 packetslost, guint32 exthighestseq, guint32 jitter, guint32 lsr,
1114 RTPReceiverReport *curr;
1118 g_return_if_fail (RTP_IS_SOURCE (src));
1120 GST_DEBUG ("got RB packet: SSRC %08x, FL %2x, PL %d, HS %" G_GUINT32_FORMAT
1121 ", jitter %" G_GUINT32_FORMAT ", LSR %04x:%04x, DLSR %04x:%04x",
1122 src->ssrc, fractionlost, packetslost, exthighestseq, jitter, lsr >> 16,
1123 lsr & 0xffff, dlsr >> 16, dlsr & 0xffff);
1125 curridx = src->stats.curr_rr ^ 1;
1126 curr = &src->stats.rr[curridx];
1128 /* update current */
1129 curr->is_valid = TRUE;
1130 curr->fractionlost = fractionlost;
1131 curr->packetslost = packetslost;
1132 curr->exthighestseq = exthighestseq;
1133 curr->jitter = jitter;
1137 /* calculate round trip */
1138 ntp = (gst_rtcp_unix_to_ntp (time) >> 16) & 0xffffffff;
1141 curr->round_trip = A;
1143 GST_DEBUG ("NTP %04x:%04x, round trip %04x:%04x", ntp >> 16, ntp & 0xffff,
1144 A >> 16, A & 0xffff);
1147 src->stats.curr_rr = curridx;
1151 * rtp_source_get_new_sr:
1152 * @src: an #RTPSource
1153 * @ntpnstime: the current time in nanoseconds since 1970
1154 * @ntptime: the NTP time in 32.32 fixed point
1155 * @rtptime: the RTP time corresponding to @ntptime
1156 * @packet_count: the packet count
1157 * @octet_count: the octect count
1159 * Get new values to put into a new SR report from this source.
1161 * Returns: %TRUE on success.
1164 rtp_source_get_new_sr (RTPSource * src, guint64 ntpnstime,
1165 guint64 * ntptime, guint32 * rtptime, guint32 * packet_count,
1166 guint32 * octet_count)
1169 guint64 t_current_ntp;
1170 GstClockTimeDiff diff;
1172 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1174 /* use the sync params to interpolate the date->time member to rtptime. We
1175 * use the last sent timestamp and rtptime as reference points. We assume
1176 * that the slope of the rtptime vs timestamp curve is 1, which is certainly
1177 * sufficient for the frequency at which we report SR and the rate we send
1178 * out RTP packets. */
1179 t_rtp = src->last_rtptime;
1181 GST_DEBUG ("last_ntpnstime %" GST_TIME_FORMAT ", last_rtptime %"
1182 G_GUINT64_FORMAT, GST_TIME_ARGS (src->last_ntpnstime), t_rtp);
1184 if (src->clock_rate != -1) {
1185 /* get the diff with the SR time */
1186 diff = GST_CLOCK_DIFF (src->last_ntpnstime, ntpnstime);
1188 /* now translate the diff to RTP time, handle positive and negative cases.
1189 * If there is no diff, we already set rtptime correctly above. */
1191 GST_DEBUG ("ntpnstime %" GST_TIME_FORMAT ", diff %" GST_TIME_FORMAT,
1192 GST_TIME_ARGS (ntpnstime), GST_TIME_ARGS (diff));
1193 t_rtp += gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1196 GST_DEBUG ("ntpnstime %" GST_TIME_FORMAT ", diff -%" GST_TIME_FORMAT,
1197 GST_TIME_ARGS (ntpnstime), GST_TIME_ARGS (diff));
1198 t_rtp -= gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1201 GST_WARNING ("no clock-rate, cannot interpolate rtp time");
1204 /* convert the NTP time in nanoseconds to 32.32 fixed point */
1205 t_current_ntp = gst_util_uint64_scale (ntpnstime, (1LL << 32), GST_SECOND);
1207 GST_DEBUG ("NTP %08x:%08x, RTP %" G_GUINT32_FORMAT,
1208 (guint32) (t_current_ntp >> 32), (guint32) (t_current_ntp & 0xffffffff),
1212 *ntptime = t_current_ntp;
1216 *packet_count = src->stats.packets_sent;
1218 *octet_count = src->stats.octets_sent;
1224 * rtp_source_get_new_rb:
1225 * @src: an #RTPSource
1226 * @ntpnstime: the current time in nanoseconds since 1970
1227 * @fractionlost: fraction lost since last SR/RR
1228 * @packetslost: the cumululative number of packets lost
1229 * @exthighestseq: the extended last sequence number received
1230 * @jitter: the interarrival jitter
1231 * @lsr: the last SR packet from this source
1232 * @dlsr: the delay since last SR packet
1234 * Get new values to put into a new report block from this source.
1236 * Returns: %TRUE on success.
1239 rtp_source_get_new_rb (RTPSource * src, guint64 ntpnstime,
1240 guint8 * fractionlost, gint32 * packetslost, guint32 * exthighestseq,
1241 guint32 * jitter, guint32 * lsr, guint32 * dlsr)
1243 RTPSourceStats *stats;
1244 guint64 extended_max, expected;
1245 guint64 expected_interval, received_interval, ntptime;
1246 gint64 lost, lost_interval;
1247 guint32 fraction, LSR, DLSR;
1248 GstClockTime sr_time;
1250 stats = &src->stats;
1252 extended_max = stats->cycles + stats->max_seq;
1253 expected = extended_max - stats->base_seq + 1;
1255 GST_DEBUG ("ext_max %" G_GUINT64_FORMAT ", expected %" G_GUINT64_FORMAT
1256 ", received %" G_GUINT64_FORMAT ", base_seq %" G_GUINT32_FORMAT,
1257 extended_max, expected, stats->packets_received, stats->base_seq);
1259 lost = expected - stats->packets_received;
1260 lost = CLAMP (lost, -0x800000, 0x7fffff);
1262 expected_interval = expected - stats->prev_expected;
1263 stats->prev_expected = expected;
1264 received_interval = stats->packets_received - stats->prev_received;
1265 stats->prev_received = stats->packets_received;
1267 lost_interval = expected_interval - received_interval;
1269 if (expected_interval == 0 || lost_interval <= 0)
1272 fraction = (lost_interval << 8) / expected_interval;
1274 GST_DEBUG ("add RR for SSRC %08x", src->ssrc);
1275 /* we scaled the jitter up for additional precision */
1276 GST_DEBUG ("fraction %" G_GUINT32_FORMAT ", lost %" G_GINT64_FORMAT
1277 ", extseq %" G_GUINT64_FORMAT ", jitter %d", fraction, lost,
1278 extended_max, stats->jitter >> 4);
1280 if (rtp_source_get_last_sr (src, &sr_time, &ntptime, NULL, NULL, NULL)) {
1283 /* LSR is middle 32 bits of the last ntptime */
1284 LSR = (ntptime >> 16) & 0xffffffff;
1285 diff = ntpnstime - sr_time;
1286 GST_DEBUG ("last SR time diff %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
1287 /* DLSR, delay since last SR is expressed in 1/65536 second units */
1288 DLSR = gst_util_uint64_scale_int (diff, 65536, GST_SECOND);
1290 /* No valid SR received, LSR/DLSR are set to 0 then */
1291 GST_DEBUG ("no valid SR received");
1295 GST_DEBUG ("LSR %04x:%04x, DLSR %04x:%04x", LSR >> 16, LSR & 0xffff,
1296 DLSR >> 16, DLSR & 0xffff);
1299 *fractionlost = fraction;
1301 *packetslost = lost;
1303 *exthighestseq = extended_max;
1305 *jitter = stats->jitter >> 4;
1315 * rtp_source_get_last_sr:
1316 * @src: an #RTPSource
1317 * @time: time of packet arrival
1318 * @ntptime: the NTP time in 32.32 fixed point
1319 * @rtptime: the RTP time
1320 * @packet_count: the packet count
1321 * @octet_count: the octect count
1323 * Get the values of the last sender report as set with rtp_source_process_sr().
1325 * Returns: %TRUE if there was a valid SR report.
1328 rtp_source_get_last_sr (RTPSource * src, GstClockTime * time, guint64 * ntptime,
1329 guint32 * rtptime, guint32 * packet_count, guint32 * octet_count)
1331 RTPSenderReport *curr;
1333 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1335 curr = &src->stats.sr[src->stats.curr_sr];
1336 if (!curr->is_valid)
1340 *ntptime = curr->ntptime;
1342 *rtptime = curr->rtptime;
1344 *packet_count = curr->packet_count;
1346 *octet_count = curr->octet_count;
1354 * rtp_source_get_last_rb:
1355 * @src: an #RTPSource
1356 * @fractionlost: fraction lost since last SR/RR
1357 * @packetslost: the cumululative number of packets lost
1358 * @exthighestseq: the extended last sequence number received
1359 * @jitter: the interarrival jitter
1360 * @lsr: the last SR packet from this source
1361 * @dlsr: the delay since last SR packet
1363 * Get the values of the last RB report set with rtp_source_process_rb().
1365 * Returns: %TRUE if there was a valid SB report.
1368 rtp_source_get_last_rb (RTPSource * src, guint8 * fractionlost,
1369 gint32 * packetslost, guint32 * exthighestseq, guint32 * jitter,
1370 guint32 * lsr, guint32 * dlsr)
1372 RTPReceiverReport *curr;
1374 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1376 curr = &src->stats.rr[src->stats.curr_rr];
1377 if (!curr->is_valid)
1381 *fractionlost = curr->fractionlost;
1383 *packetslost = curr->packetslost;
1385 *exthighestseq = curr->exthighestseq;
1387 *jitter = curr->jitter;