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 NULL
55 /* GObject vmethods */
56 static void rtp_source_finalize (GObject * object);
57 static void rtp_source_set_property (GObject * object, guint prop_id,
58 const GValue * value, GParamSpec * pspec);
59 static void rtp_source_get_property (GObject * object, guint prop_id,
60 GValue * value, GParamSpec * pspec);
62 /* static guint rtp_source_signals[LAST_SIGNAL] = { 0 }; */
64 G_DEFINE_TYPE (RTPSource, rtp_source, G_TYPE_OBJECT);
67 rtp_source_class_init (RTPSourceClass * klass)
69 GObjectClass *gobject_class;
71 gobject_class = (GObjectClass *) klass;
73 gobject_class->finalize = rtp_source_finalize;
75 gobject_class->set_property = rtp_source_set_property;
76 gobject_class->get_property = rtp_source_get_property;
78 g_object_class_install_property (gobject_class, PROP_SSRC,
79 g_param_spec_uint ("ssrc", "SSRC",
80 "The SSRC of this source", 0, G_MAXUINT, DEFAULT_SSRC,
81 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
83 g_object_class_install_property (gobject_class, PROP_IS_CSRC,
84 g_param_spec_boolean ("is-csrc", "Is CSRC",
85 "If this SSRC is acting as a contributing source",
86 DEFAULT_IS_CSRC, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
88 g_object_class_install_property (gobject_class, PROP_IS_VALIDATED,
89 g_param_spec_boolean ("is-validated", "Is Validated",
90 "If this SSRC is validated", DEFAULT_IS_VALIDATED,
91 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
93 g_object_class_install_property (gobject_class, PROP_IS_SENDER,
94 g_param_spec_boolean ("is-sender", "Is Sender",
95 "If this SSRC is a sender", DEFAULT_IS_SENDER,
96 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
101 * The current SDES items of the source. Returns a structure with the
104 * 'cname' G_TYPE_STRING : The canonical name
105 * 'name' G_TYPE_STRING : The user name
106 * 'email' G_TYPE_STRING : The user's electronic mail address
107 * 'phone' G_TYPE_STRING : The user's phone number
108 * 'location' G_TYPE_STRING : The geographic user location
109 * 'tool' G_TYPE_STRING : The name of application or tool
110 * 'note' G_TYPE_STRING : A notice about the source
112 g_object_class_install_property (gobject_class, PROP_SDES,
113 g_param_spec_boxed ("sdes", "SDES",
114 "The SDES information for this source",
115 GST_TYPE_STRUCTURE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
120 * The statistics of the source. This property returns a GstStructure with
121 * name application/x-rtp-source-stats with the following fields:
124 g_object_class_install_property (gobject_class, PROP_STATS,
125 g_param_spec_boxed ("stats", "Stats",
126 "The stats of this source", GST_TYPE_STRUCTURE,
127 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
129 GST_DEBUG_CATEGORY_INIT (rtp_source_debug, "rtpsource", 0, "RTP Source");
134 * @src: an #RTPSource
136 * Reset the stats of @src.
139 rtp_source_reset (RTPSource * src)
141 src->received_bye = FALSE;
143 src->stats.cycles = -1;
144 src->stats.jitter = 0;
145 src->stats.transit = -1;
146 src->stats.curr_sr = 0;
147 src->stats.curr_rr = 0;
151 rtp_source_init (RTPSource * src)
153 /* sources are initialy on probation until we receive enough valid RTP
154 * packets or a valid RTCP packet */
155 src->validated = FALSE;
156 src->internal = FALSE;
157 src->probation = RTP_DEFAULT_PROBATION;
160 src->clock_rate = -1;
161 src->packets = g_queue_new ();
162 src->seqnum_base = -1;
163 src->last_rtptime = -1;
165 rtp_source_reset (src);
169 rtp_source_finalize (GObject * object)
175 src = RTP_SOURCE_CAST (object);
177 while ((buffer = g_queue_pop_head (src->packets)))
178 gst_buffer_unref (buffer);
179 g_queue_free (src->packets);
181 for (i = 0; i < 9; i++)
182 g_free (src->sdes[i]);
184 g_free (src->bye_reason);
186 gst_caps_replace (&src->caps, NULL);
188 G_OBJECT_CLASS (rtp_source_parent_class)->finalize (object);
191 #define MAX_ADDRESS 64
193 make_address_string (GstNetAddress * addr, gchar * dest, gulong n)
195 switch (gst_netaddress_get_net_type (addr)) {
196 case GST_NET_TYPE_IP4:
201 gst_netaddress_get_ip4_address (addr, &address, &port);
203 g_snprintf (dest, n, "%d.%d.%d.%d:%d", (address >> 24) & 0xff,
204 (address >> 16) & 0xff, (address >> 8) & 0xff, address & 0xff,
208 case GST_NET_TYPE_IP6:
213 gst_netaddress_get_ip6_address (addr, address, &port);
215 g_snprintf (dest, n, "[%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x]:%d",
216 (address[0] << 8) | address[1], (address[2] << 8) | address[3],
217 (address[4] << 8) | address[5], (address[6] << 8) | address[7],
218 (address[8] << 8) | address[9], (address[10] << 8) | address[11],
219 (address[12] << 8) | address[13], (address[14] << 8) | address[15],
229 static GstStructure *
230 rtp_source_create_stats (RTPSource * src)
233 gboolean is_sender = src->is_sender;
234 gboolean internal = src->internal;
235 gchar address_str[MAX_ADDRESS];
237 /* common data for all types of sources */
238 s = gst_structure_new ("application/x-rtp-source-stats",
239 "ssrc", G_TYPE_UINT, (guint) src->ssrc,
240 "internal", G_TYPE_BOOLEAN, internal,
241 "validated", G_TYPE_BOOLEAN, src->validated,
242 "received-bye", G_TYPE_BOOLEAN, src->received_bye,
243 "is-csrc", G_TYPE_BOOLEAN, src->is_csrc,
244 "is-sender", G_TYPE_BOOLEAN, is_sender, NULL);
246 /* add address and port */
247 if (src->have_rtp_from) {
248 make_address_string (&src->rtp_from, address_str, sizeof (address_str));
249 gst_structure_set (s, "rtp-from", G_TYPE_STRING, address_str, NULL);
251 if (src->have_rtcp_from) {
252 make_address_string (&src->rtcp_from, address_str, sizeof (address_str));
253 gst_structure_set (s, "rtcp-from", G_TYPE_STRING, address_str, NULL);
257 /* our internal source */
259 /* if we are sending, report about how much we sent, other sources will
260 * have a RB with info on reception. */
261 gst_structure_set (s,
262 "octets-sent", G_TYPE_UINT64, src->stats.octets_sent,
263 "packets-sent", G_TYPE_UINT64, src->stats.packets_sent,
264 "bitrate", G_TYPE_UINT64, src->bitrate, NULL);
266 /* if we are not sending we have nothing more to report */
270 guint8 fractionlost = 0;
271 gint32 packetslost = 0;
272 guint32 exthighestseq = 0;
276 guint32 round_trip = 0;
281 GstClockTime time = 0;
284 guint32 packet_count = 0;
285 guint32 octet_count = 0;
287 /* this source is sending to us, get the last SR. */
288 have_sr = rtp_source_get_last_sr (src, &time, &ntptime, &rtptime,
289 &packet_count, &octet_count);
290 gst_structure_set (s,
291 "octets-received", G_TYPE_UINT64, src->stats.octets_received,
292 "packets-received", G_TYPE_UINT64, src->stats.packets_received,
293 "have-sr", G_TYPE_BOOLEAN, have_sr,
294 "sr-ntptime", G_TYPE_UINT64, ntptime,
295 "sr-rtptime", G_TYPE_UINT, (guint) rtptime,
296 "sr-octet-count", G_TYPE_UINT, (guint) octet_count,
297 "sr-packet-count", G_TYPE_UINT, (guint) packet_count, NULL);
299 /* we might be sending to this SSRC so we report about how it is
300 * receiving our data */
301 have_rb = rtp_source_get_last_rb (src, &fractionlost, &packetslost,
302 &exthighestseq, &jitter, &lsr, &dlsr, &round_trip);
304 gst_structure_set (s,
305 "have-rb", G_TYPE_BOOLEAN, have_rb,
306 "rb-fractionlost", G_TYPE_UINT, (guint) fractionlost,
307 "rb-packetslost", G_TYPE_INT, (gint) packetslost,
308 "rb-exthighestseq", G_TYPE_UINT, (guint) exthighestseq,
309 "rb-jitter", G_TYPE_UINT, (guint) jitter,
310 "rb-lsr", G_TYPE_UINT, (guint) lsr,
311 "rb-dlsr", G_TYPE_UINT, (guint) dlsr,
312 "rb-round-trip", G_TYPE_UINT, (guint) round_trip, NULL);
318 static GstStructure *
319 rtp_source_create_sdes (RTPSource * src)
324 s = gst_structure_new ("application/x-rtp-source-sdes", NULL);
326 if ((str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_CNAME))) {
327 gst_structure_set (s, "cname", G_TYPE_STRING, str, NULL);
330 if ((str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_NAME))) {
331 gst_structure_set (s, "name", G_TYPE_STRING, str, NULL);
334 if ((str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_EMAIL))) {
335 gst_structure_set (s, "email", G_TYPE_STRING, str, NULL);
338 if ((str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_PHONE))) {
339 gst_structure_set (s, "phone", G_TYPE_STRING, str, NULL);
342 if ((str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_LOC))) {
343 gst_structure_set (s, "location", G_TYPE_STRING, str, NULL);
346 if ((str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_TOOL))) {
347 gst_structure_set (s, "tool", G_TYPE_STRING, str, NULL);
350 if ((str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_NOTE))) {
351 gst_structure_set (s, "note", G_TYPE_STRING, str, NULL);
358 rtp_source_set_property (GObject * object, guint prop_id,
359 const GValue * value, GParamSpec * pspec)
363 src = RTP_SOURCE (object);
367 src->ssrc = g_value_get_uint (value);
370 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
376 rtp_source_get_property (GObject * object, guint prop_id,
377 GValue * value, GParamSpec * pspec)
381 src = RTP_SOURCE (object);
385 g_value_set_uint (value, rtp_source_get_ssrc (src));
388 g_value_set_boolean (value, rtp_source_is_as_csrc (src));
390 case PROP_IS_VALIDATED:
391 g_value_set_boolean (value, rtp_source_is_validated (src));
394 g_value_set_boolean (value, rtp_source_is_sender (src));
397 g_value_take_boxed (value, rtp_source_create_sdes (src));
400 g_value_take_boxed (value, rtp_source_create_stats (src));
403 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
412 * Create a #RTPSource with @ssrc.
414 * Returns: a new #RTPSource. Use g_object_unref() after usage.
417 rtp_source_new (guint32 ssrc)
421 src = g_object_new (RTP_TYPE_SOURCE, NULL);
428 * rtp_source_set_callbacks:
429 * @src: an #RTPSource
430 * @cb: callback functions
431 * @user_data: user data
433 * Set the callbacks for the source.
436 rtp_source_set_callbacks (RTPSource * src, RTPSourceCallbacks * cb,
439 g_return_if_fail (RTP_IS_SOURCE (src));
441 src->callbacks.push_rtp = cb->push_rtp;
442 src->callbacks.clock_rate = cb->clock_rate;
443 src->user_data = user_data;
447 * rtp_source_get_ssrc:
448 * @src: an #RTPSource
450 * Get the SSRC of @source.
452 * Returns: the SSRC of src.
455 rtp_source_get_ssrc (RTPSource * src)
459 g_return_val_if_fail (RTP_IS_SOURCE (src), 0);
467 * rtp_source_set_as_csrc:
468 * @src: an #RTPSource
470 * Configure @src as a CSRC, this will also validate @src.
473 rtp_source_set_as_csrc (RTPSource * src)
475 g_return_if_fail (RTP_IS_SOURCE (src));
477 src->validated = TRUE;
482 * rtp_source_is_as_csrc:
483 * @src: an #RTPSource
485 * Check if @src is a contributing source.
487 * Returns: %TRUE if @src is acting as a contributing source.
490 rtp_source_is_as_csrc (RTPSource * src)
494 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
496 result = src->is_csrc;
502 * rtp_source_is_active:
503 * @src: an #RTPSource
505 * Check if @src is an active source. A source is active if it has been
506 * validated and has not yet received a BYE packet
508 * Returns: %TRUE if @src is an qactive source.
511 rtp_source_is_active (RTPSource * src)
515 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
517 result = RTP_SOURCE_IS_ACTIVE (src);
523 * rtp_source_is_validated:
524 * @src: an #RTPSource
526 * Check if @src is a validated source.
528 * Returns: %TRUE if @src is a validated source.
531 rtp_source_is_validated (RTPSource * src)
535 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
537 result = src->validated;
543 * rtp_source_is_sender:
544 * @src: an #RTPSource
546 * Check if @src is a sending source.
548 * Returns: %TRUE if @src is a sending source.
551 rtp_source_is_sender (RTPSource * src)
555 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
557 result = RTP_SOURCE_IS_SENDER (src);
563 * rtp_source_received_bye:
564 * @src: an #RTPSource
566 * Check if @src has receoved a BYE packet.
568 * Returns: %TRUE if @src has received a BYE packet.
571 rtp_source_received_bye (RTPSource * src)
575 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
577 result = src->received_bye;
584 * rtp_source_get_bye_reason:
585 * @src: an #RTPSource
587 * Get the BYE reason for @src. Check if the source receoved a BYE message first
588 * with rtp_source_received_bye().
590 * Returns: The BYE reason or NULL when no reason was given or the source did
591 * not receive a BYE message yet. g_fee() after usage.
594 rtp_source_get_bye_reason (RTPSource * src)
598 g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
600 result = g_strdup (src->bye_reason);
606 * rtp_source_update_caps:
607 * @src: an #RTPSource
610 * Parse @caps and store all relevant information in @source.
613 rtp_source_update_caps (RTPSource * src, GstCaps * caps)
619 /* nothing changed, return */
620 if (src->caps == caps)
623 s = gst_caps_get_structure (caps, 0);
625 if (gst_structure_get_int (s, "payload", &ival))
629 GST_DEBUG ("got payload %d", src->payload);
631 if (gst_structure_get_int (s, "clock-rate", &ival))
632 src->clock_rate = ival;
634 src->clock_rate = -1;
636 GST_DEBUG ("got clock-rate %d", src->clock_rate);
638 if (gst_structure_get_uint (s, "seqnum-base", &val))
639 src->seqnum_base = val;
641 src->seqnum_base = -1;
643 GST_DEBUG ("got seqnum-base %" G_GINT32_FORMAT, src->seqnum_base);
645 gst_caps_replace (&src->caps, caps);
649 * rtp_source_set_sdes:
650 * @src: an #RTPSource
651 * @type: the type of the SDES item
652 * @data: the SDES data
653 * @len: the SDES length
655 * Store an SDES item of @type in @src.
657 * Returns: %FALSE if the SDES item was unchanged or @type is unknown.
660 rtp_source_set_sdes (RTPSource * src, GstRTCPSDESType type,
661 const guint8 * data, guint len)
665 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
667 if (type < 0 || type > GST_RTCP_SDES_PRIV)
670 old = src->sdes[type];
672 /* lengths are the same, check if the data is the same */
673 if ((src->sdes_len[type] == len))
674 if (data != NULL && old != NULL && (memcmp (old, data, len) == 0))
677 /* NULL data, make sure we store 0 length or if no length is given,
682 g_free (src->sdes[type]);
683 src->sdes[type] = g_memdup (data, len);
684 src->sdes_len[type] = len;
690 * rtp_source_set_sdes_string:
691 * @src: an #RTPSource
692 * @type: the type of the SDES item
693 * @data: the SDES data
695 * Store an SDES item of @type in @src. This function is similar to
696 * rtp_source_set_sdes() but takes a null-terminated string for convenience.
698 * Returns: %FALSE if the SDES item was unchanged or @type is unknown.
701 rtp_source_set_sdes_string (RTPSource * src, GstRTCPSDESType type,
712 result = rtp_source_set_sdes (src, type, (guint8 *) data, len);
718 * rtp_source_get_sdes:
719 * @src: an #RTPSource
720 * @type: the type of the SDES item
721 * @data: location to store the SDES data or NULL
722 * @len: location to store the SDES length or NULL
724 * Get the SDES item of @type from @src. Note that @data does not always point
725 * to a null-terminated string, use rtp_source_get_sdes_string() to retrieve a
726 * null-terminated string instead.
728 * @data remains valid until the next call to rtp_source_set_sdes().
730 * Returns: %TRUE if @type was valid and @data and @len contain valid
731 * data. @data can be NULL when the item was unset.
734 rtp_source_get_sdes (RTPSource * src, GstRTCPSDESType type, guint8 ** data,
737 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
739 if (type < 0 || type > GST_RTCP_SDES_PRIV)
743 *data = src->sdes[type];
745 *len = src->sdes_len[type];
751 * rtp_source_get_sdes_string:
752 * @src: an #RTPSource
753 * @type: the type of the SDES item
755 * Get the SDES item of @type from @src.
757 * Returns: a null-terminated copy of the SDES item or NULL when @type was not
758 * valid or the SDES item was unset. g_free() after usage.
761 rtp_source_get_sdes_string (RTPSource * src, GstRTCPSDESType type)
765 g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
767 if (type < 0 || type > GST_RTCP_SDES_PRIV)
770 result = g_strndup ((const gchar *) src->sdes[type], src->sdes_len[type]);
776 * rtp_source_set_rtp_from:
777 * @src: an #RTPSource
778 * @address: the RTP address to set
780 * Set that @src is receiving RTP packets from @address. This is used for
781 * collistion checking.
784 rtp_source_set_rtp_from (RTPSource * src, GstNetAddress * address)
786 g_return_if_fail (RTP_IS_SOURCE (src));
788 src->have_rtp_from = TRUE;
789 memcpy (&src->rtp_from, address, sizeof (GstNetAddress));
793 * rtp_source_set_rtcp_from:
794 * @src: an #RTPSource
795 * @address: the RTCP address to set
797 * Set that @src is receiving RTCP packets from @address. This is used for
798 * collistion checking.
801 rtp_source_set_rtcp_from (RTPSource * src, GstNetAddress * address)
803 g_return_if_fail (RTP_IS_SOURCE (src));
805 src->have_rtcp_from = TRUE;
806 memcpy (&src->rtcp_from, address, sizeof (GstNetAddress));
810 push_packet (RTPSource * src, GstBuffer * buffer)
812 GstFlowReturn ret = GST_FLOW_OK;
814 /* push queued packets first if any */
815 while (!g_queue_is_empty (src->packets)) {
816 GstBuffer *buffer = GST_BUFFER_CAST (g_queue_pop_head (src->packets));
818 GST_LOG ("pushing queued packet");
819 if (src->callbacks.push_rtp)
820 src->callbacks.push_rtp (src, buffer, src->user_data);
822 gst_buffer_unref (buffer);
824 GST_LOG ("pushing new packet");
826 if (src->callbacks.push_rtp)
827 ret = src->callbacks.push_rtp (src, buffer, src->user_data);
829 gst_buffer_unref (buffer);
835 get_clock_rate (RTPSource * src, guint8 payload)
837 if (src->payload == -1) {
838 /* first payload received, nothing was in the caps, lock on to this payload */
839 src->payload = payload;
840 GST_DEBUG ("first payload %d", payload);
841 } else if (payload != src->payload) {
842 /* we have a different payload than before, reset the clock-rate */
843 GST_DEBUG ("new payload %d", payload);
844 src->payload = payload;
845 src->clock_rate = -1;
846 src->stats.transit = -1;
849 if (src->clock_rate == -1) {
850 gint clock_rate = -1;
852 if (src->callbacks.clock_rate)
853 clock_rate = src->callbacks.clock_rate (src, payload, src->user_data);
855 GST_DEBUG ("got clock-rate %d", clock_rate);
857 src->clock_rate = clock_rate;
859 return src->clock_rate;
862 /* Jitter is the variation in the delay of received packets in a flow. It is
863 * measured by comparing the interval when RTP packets were sent to the interval
864 * at which they were received. For instance, if packet #1 and packet #2 leave
865 * 50 milliseconds apart and arrive 60 milliseconds apart, then the jitter is 10
868 calculate_jitter (RTPSource * src, GstBuffer * buffer,
869 RTPArrivalStats * arrival)
872 guint32 rtparrival, transit, rtptime;
877 /* get arrival time */
878 if ((ntpnstime = arrival->ntpnstime) == GST_CLOCK_TIME_NONE)
881 pt = gst_rtp_buffer_get_payload_type (buffer);
883 GST_LOG ("SSRC %08x got payload %d", src->ssrc, pt);
886 if ((clock_rate = get_clock_rate (src, pt)) == -1)
889 rtptime = gst_rtp_buffer_get_timestamp (buffer);
891 /* convert arrival time to RTP timestamp units, truncate to 32 bits, we don't
892 * care about the absolute value, just the difference. */
893 rtparrival = gst_util_uint64_scale_int (ntpnstime, clock_rate, GST_SECOND);
895 /* transit time is difference with RTP timestamp */
896 transit = rtparrival - rtptime;
898 /* get ABS diff with previous transit time */
899 if (src->stats.transit != -1) {
900 if (transit > src->stats.transit)
901 diff = transit - src->stats.transit;
903 diff = src->stats.transit - transit;
907 src->stats.transit = transit;
909 /* update jitter, the value we store is scaled up so we can keep precision. */
910 src->stats.jitter += diff - ((src->stats.jitter + 8) >> 4);
912 src->stats.prev_rtptime = src->stats.last_rtptime;
913 src->stats.last_rtptime = rtparrival;
915 GST_LOG ("rtparrival %u, rtptime %u, clock-rate %d, diff %d, jitter: %f",
916 rtparrival, rtptime, clock_rate, diff, (src->stats.jitter) / 16.0);
923 GST_WARNING ("cannot get current time");
928 GST_WARNING ("cannot get clock-rate for pt %d", pt);
934 init_seq (RTPSource * src, guint16 seq)
936 src->stats.base_seq = seq;
937 src->stats.max_seq = seq;
938 src->stats.bad_seq = RTP_SEQ_MOD + 1; /* so seq == bad_seq is false */
939 src->stats.cycles = 0;
940 src->stats.packets_received = 0;
941 src->stats.octets_received = 0;
942 src->stats.bytes_received = 0;
943 src->stats.prev_received = 0;
944 src->stats.prev_expected = 0;
946 GST_DEBUG ("base_seq %d", seq);
950 * rtp_source_process_rtp:
951 * @src: an #RTPSource
952 * @buffer: an RTP buffer
954 * Let @src handle the incomming RTP @buffer.
956 * Returns: a #GstFlowReturn.
959 rtp_source_process_rtp (RTPSource * src, GstBuffer * buffer,
960 RTPArrivalStats * arrival)
962 GstFlowReturn result = GST_FLOW_OK;
963 guint16 seqnr, udelta;
964 RTPSourceStats *stats;
966 g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
967 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
971 seqnr = gst_rtp_buffer_get_seq (buffer);
973 rtp_source_update_caps (src, GST_BUFFER_CAPS (buffer));
975 if (stats->cycles == -1) {
976 GST_DEBUG ("received first buffer");
977 /* first time we heard of this source */
978 init_seq (src, seqnr);
979 src->stats.max_seq = seqnr - 1;
980 src->probation = RTP_DEFAULT_PROBATION;
983 udelta = seqnr - stats->max_seq;
985 /* if we are still on probation, check seqnum */
986 if (src->probation) {
989 expected = src->stats.max_seq + 1;
991 /* when in probation, we require consecutive seqnums */
992 if (seqnr == expected) {
993 /* expected packet */
994 GST_DEBUG ("probation: seqnr %d == expected %d", seqnr, expected);
996 src->stats.max_seq = seqnr;
997 if (src->probation == 0) {
998 GST_DEBUG ("probation done!");
999 init_seq (src, seqnr);
1003 GST_DEBUG ("probation %d: queue buffer", src->probation);
1004 /* when still in probation, keep packets in a list. */
1005 g_queue_push_tail (src->packets, buffer);
1006 /* remove packets from queue if there are too many */
1007 while (g_queue_get_length (src->packets) > RTP_MAX_PROBATION_LEN) {
1008 q = g_queue_pop_head (src->packets);
1009 gst_buffer_unref (q);
1014 GST_DEBUG ("probation: seqnr %d != expected %d", seqnr, expected);
1015 src->probation = RTP_DEFAULT_PROBATION;
1016 src->stats.max_seq = seqnr;
1019 } else if (udelta < RTP_MAX_DROPOUT) {
1020 /* in order, with permissible gap */
1021 if (seqnr < stats->max_seq) {
1022 /* sequence number wrapped - count another 64K cycle. */
1023 stats->cycles += RTP_SEQ_MOD;
1025 stats->max_seq = seqnr;
1026 } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
1027 /* the sequence number made a very large jump */
1028 if (seqnr == stats->bad_seq) {
1029 /* two sequential packets -- assume that the other side
1030 * restarted without telling us so just re-sync
1031 * (i.e., pretend this was the first packet). */
1032 init_seq (src, seqnr);
1034 /* unacceptable jump */
1035 stats->bad_seq = (seqnr + 1) & (RTP_SEQ_MOD - 1);
1039 /* duplicate or reordered packet, will be filtered by jitterbuffer. */
1040 GST_WARNING ("duplicate or reordered packet");
1043 src->stats.octets_received += arrival->payload_len;
1044 src->stats.bytes_received += arrival->bytes;
1045 src->stats.packets_received++;
1046 /* the source that sent the packet must be a sender */
1047 src->is_sender = TRUE;
1048 src->validated = TRUE;
1050 GST_LOG ("seq %d, PC: %" G_GUINT64_FORMAT ", OC: %" G_GUINT64_FORMAT,
1051 seqnr, src->stats.packets_received, src->stats.octets_received);
1053 /* calculate jitter for the stats */
1054 calculate_jitter (src, buffer, arrival);
1056 /* we're ready to push the RTP packet now */
1057 result = push_packet (src, buffer);
1065 GST_WARNING ("unacceptable seqnum received");
1071 * rtp_source_process_bye:
1072 * @src: an #RTPSource
1073 * @reason: the reason for leaving
1075 * Notify @src that a BYE packet has been received. This will make the source
1079 rtp_source_process_bye (RTPSource * src, const gchar * reason)
1081 g_return_if_fail (RTP_IS_SOURCE (src));
1083 GST_DEBUG ("marking SSRC %08x as BYE, reason: %s", src->ssrc,
1084 GST_STR_NULL (reason));
1086 /* copy the reason and mark as received_bye */
1087 g_free (src->bye_reason);
1088 src->bye_reason = g_strdup (reason);
1089 src->received_bye = TRUE;
1093 * rtp_source_send_rtp:
1094 * @src: an #RTPSource
1095 * @buffer: an RTP buffer
1096 * @ntpnstime: the NTP time when this buffer was captured in nanoseconds. This
1097 * is the buffer timestamp converted to NTP time.
1099 * Send an RTP @buffer originating from @src. This will make @src a sender.
1100 * This function takes ownership of @buffer and modifies the SSRC in the RTP
1101 * packet to that of @src when needed.
1103 * Returns: a #GstFlowReturn.
1106 rtp_source_send_rtp (RTPSource * src, GstBuffer * buffer, guint64 ntpnstime)
1108 GstFlowReturn result = GST_FLOW_OK;
1111 guint64 ext_rtptime;
1112 guint64 ntp_diff, rtp_diff;
1115 g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
1116 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1118 len = gst_rtp_buffer_get_payload_len (buffer);
1120 rtp_source_update_caps (src, GST_BUFFER_CAPS (buffer));
1122 /* we are a sender now */
1123 src->is_sender = TRUE;
1125 /* update stats for the SR */
1126 src->stats.packets_sent++;
1127 src->stats.octets_sent += len;
1128 src->bytes_sent += len;
1130 if (src->prev_ntpnstime) {
1131 elapsed = ntpnstime - src->prev_ntpnstime;
1133 if (elapsed > (G_GINT64_CONSTANT (1) << 31)) {
1137 gst_util_uint64_scale (src->bytes_sent, elapsed,
1138 (G_GINT64_CONSTANT (1) << 29));
1140 GST_LOG ("Elapsed %" G_GUINT64_FORMAT ", bytes %" G_GUINT64_FORMAT
1141 ", rate %" G_GUINT64_FORMAT, elapsed, src->bytes_sent, rate);
1143 if (src->bitrate == 0)
1144 src->bitrate = rate;
1146 src->bitrate = ((src->bitrate * 3) + rate) / 4;
1148 src->prev_ntpnstime = ntpnstime;
1149 src->bytes_sent = 0;
1152 GST_LOG ("Reset bitrate measurement");
1153 src->prev_ntpnstime = ntpnstime;
1157 rtptime = gst_rtp_buffer_get_timestamp (buffer);
1158 ext_rtptime = src->last_rtptime;
1159 ext_rtptime = gst_rtp_buffer_ext_timestamp (&ext_rtptime, rtptime);
1161 GST_LOG ("SSRC %08x, RTP %" G_GUINT64_FORMAT ", NTP %" GST_TIME_FORMAT,
1162 src->ssrc, ext_rtptime, GST_TIME_ARGS (ntpnstime));
1164 if (ext_rtptime > src->last_rtptime) {
1165 rtp_diff = ext_rtptime - src->last_rtptime;
1166 ntp_diff = ntpnstime - src->last_ntpnstime;
1168 /* calc the diff so we can detect drift at the sender. This can also be used
1169 * to guestimate the clock rate if the NTP time is locked to the RTP
1170 * timestamps (as is the case when the capture device is providing the clock). */
1171 GST_LOG ("SSRC %08x, diff RTP %" G_GUINT64_FORMAT ", diff NTP %"
1172 GST_TIME_FORMAT, src->ssrc, rtp_diff, GST_TIME_ARGS (ntp_diff));
1175 /* we keep track of the last received RTP timestamp and the corresponding
1176 * NTP timestamp so that we can use this info when constructing SR reports */
1177 src->last_rtptime = ext_rtptime;
1178 src->last_ntpnstime = ntpnstime;
1181 if (src->callbacks.push_rtp) {
1184 ssrc = gst_rtp_buffer_get_ssrc (buffer);
1185 if (ssrc != src->ssrc) {
1186 /* the SSRC of the packet is not correct, make a writable buffer and
1187 * update the SSRC. This could involve a complete copy of the packet when
1188 * it is not writable. Usually the payloader will use caps negotiation to
1189 * get the correct SSRC from the session manager before pushing anything. */
1190 buffer = gst_buffer_make_writable (buffer);
1192 /* FIXME, we don't want to warn yet because we can't inform any payloader
1193 * of the changes SSRC yet because we don't implement pad-alloc. */
1194 GST_LOG ("updating SSRC from %08x to %08x, fix the payloader", ssrc,
1196 gst_rtp_buffer_set_ssrc (buffer, src->ssrc);
1198 GST_LOG ("pushing RTP packet %" G_GUINT64_FORMAT, src->stats.packets_sent);
1199 result = src->callbacks.push_rtp (src, buffer, src->user_data);
1201 GST_WARNING ("no callback installed, dropping packet");
1202 gst_buffer_unref (buffer);
1209 * rtp_source_process_sr:
1210 * @src: an #RTPSource
1211 * @time: time of packet arrival
1212 * @ntptime: the NTP time in 32.32 fixed point
1213 * @rtptime: the RTP time
1214 * @packet_count: the packet count
1215 * @octet_count: the octect count
1217 * Update the sender report in @src.
1220 rtp_source_process_sr (RTPSource * src, GstClockTime time, guint64 ntptime,
1221 guint32 rtptime, guint32 packet_count, guint32 octet_count)
1223 RTPSenderReport *curr;
1226 g_return_if_fail (RTP_IS_SOURCE (src));
1228 GST_DEBUG ("got SR packet: SSRC %08x, NTP %08x:%08x, RTP %" G_GUINT32_FORMAT
1229 ", PC %" G_GUINT32_FORMAT ", OC %" G_GUINT32_FORMAT, src->ssrc,
1230 (guint32) (ntptime >> 32), (guint32) (ntptime & 0xffffffff), rtptime,
1231 packet_count, octet_count);
1233 curridx = src->stats.curr_sr ^ 1;
1234 curr = &src->stats.sr[curridx];
1236 /* this is a sender now */
1237 src->is_sender = TRUE;
1239 /* update current */
1240 curr->is_valid = TRUE;
1241 curr->ntptime = ntptime;
1242 curr->rtptime = rtptime;
1243 curr->packet_count = packet_count;
1244 curr->octet_count = octet_count;
1248 src->stats.curr_sr = curridx;
1252 * rtp_source_process_rb:
1253 * @src: an #RTPSource
1254 * @time: the current time in nanoseconds since 1970
1255 * @fractionlost: fraction lost since last SR/RR
1256 * @packetslost: the cumululative number of packets lost
1257 * @exthighestseq: the extended last sequence number received
1258 * @jitter: the interarrival jitter
1259 * @lsr: the last SR packet from this source
1260 * @dlsr: the delay since last SR packet
1262 * Update the report block in @src.
1265 rtp_source_process_rb (RTPSource * src, GstClockTime time, guint8 fractionlost,
1266 gint32 packetslost, guint32 exthighestseq, guint32 jitter, guint32 lsr,
1269 RTPReceiverReport *curr;
1273 g_return_if_fail (RTP_IS_SOURCE (src));
1275 GST_DEBUG ("got RB packet: SSRC %08x, FL %2x, PL %d, HS %" G_GUINT32_FORMAT
1276 ", jitter %" G_GUINT32_FORMAT ", LSR %04x:%04x, DLSR %04x:%04x",
1277 src->ssrc, fractionlost, packetslost, exthighestseq, jitter, lsr >> 16,
1278 lsr & 0xffff, dlsr >> 16, dlsr & 0xffff);
1280 curridx = src->stats.curr_rr ^ 1;
1281 curr = &src->stats.rr[curridx];
1283 /* update current */
1284 curr->is_valid = TRUE;
1285 curr->fractionlost = fractionlost;
1286 curr->packetslost = packetslost;
1287 curr->exthighestseq = exthighestseq;
1288 curr->jitter = jitter;
1292 /* calculate round trip, round the time up */
1293 ntp = ((gst_rtcp_unix_to_ntp (time) + 0xffff) >> 16) & 0xffffffff;
1295 if (A > 0 && ntp > A)
1299 curr->round_trip = A;
1301 GST_DEBUG ("NTP %04x:%04x, round trip %04x:%04x", ntp >> 16, ntp & 0xffff,
1302 A >> 16, A & 0xffff);
1305 src->stats.curr_rr = curridx;
1309 * rtp_source_get_new_sr:
1310 * @src: an #RTPSource
1311 * @ntpnstime: the current time in nanoseconds since 1970
1312 * @ntptime: the NTP time in 32.32 fixed point
1313 * @rtptime: the RTP time corresponding to @ntptime
1314 * @packet_count: the packet count
1315 * @octet_count: the octect count
1317 * Get new values to put into a new SR report from this source.
1319 * Returns: %TRUE on success.
1322 rtp_source_get_new_sr (RTPSource * src, guint64 ntpnstime,
1323 guint64 * ntptime, guint32 * rtptime, guint32 * packet_count,
1324 guint32 * octet_count)
1327 guint64 t_current_ntp;
1328 GstClockTimeDiff diff;
1330 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1332 /* use the sync params to interpolate the date->time member to rtptime. We
1333 * use the last sent timestamp and rtptime as reference points. We assume
1334 * that the slope of the rtptime vs timestamp curve is 1, which is certainly
1335 * sufficient for the frequency at which we report SR and the rate we send
1336 * out RTP packets. */
1337 t_rtp = src->last_rtptime;
1339 GST_DEBUG ("last_ntpnstime %" GST_TIME_FORMAT ", last_rtptime %"
1340 G_GUINT64_FORMAT, GST_TIME_ARGS (src->last_ntpnstime), t_rtp);
1342 if (src->clock_rate != -1) {
1343 /* get the diff with the SR time */
1344 diff = GST_CLOCK_DIFF (src->last_ntpnstime, ntpnstime);
1346 /* now translate the diff to RTP time, handle positive and negative cases.
1347 * If there is no diff, we already set rtptime correctly above. */
1349 GST_DEBUG ("ntpnstime %" GST_TIME_FORMAT ", diff %" GST_TIME_FORMAT,
1350 GST_TIME_ARGS (ntpnstime), GST_TIME_ARGS (diff));
1351 t_rtp += gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1354 GST_DEBUG ("ntpnstime %" GST_TIME_FORMAT ", diff -%" GST_TIME_FORMAT,
1355 GST_TIME_ARGS (ntpnstime), GST_TIME_ARGS (diff));
1356 t_rtp -= gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1359 GST_WARNING ("no clock-rate, cannot interpolate rtp time");
1362 /* convert the NTP time in nanoseconds to 32.32 fixed point */
1363 t_current_ntp = gst_util_uint64_scale (ntpnstime, (1LL << 32), GST_SECOND);
1365 GST_DEBUG ("NTP %08x:%08x, RTP %" G_GUINT32_FORMAT,
1366 (guint32) (t_current_ntp >> 32), (guint32) (t_current_ntp & 0xffffffff),
1370 *ntptime = t_current_ntp;
1374 *packet_count = src->stats.packets_sent;
1376 *octet_count = src->stats.octets_sent;
1382 * rtp_source_get_new_rb:
1383 * @src: an #RTPSource
1384 * @time: the current time of the system clock
1385 * @fractionlost: fraction lost since last SR/RR
1386 * @packetslost: the cumululative number of packets lost
1387 * @exthighestseq: the extended last sequence number received
1388 * @jitter: the interarrival jitter
1389 * @lsr: the last SR packet from this source
1390 * @dlsr: the delay since last SR packet
1392 * Get new values to put into a new report block from this source.
1394 * Returns: %TRUE on success.
1397 rtp_source_get_new_rb (RTPSource * src, GstClockTime time,
1398 guint8 * fractionlost, gint32 * packetslost, guint32 * exthighestseq,
1399 guint32 * jitter, guint32 * lsr, guint32 * dlsr)
1401 RTPSourceStats *stats;
1402 guint64 extended_max, expected;
1403 guint64 expected_interval, received_interval, ntptime;
1404 gint64 lost, lost_interval;
1405 guint32 fraction, LSR, DLSR;
1406 GstClockTime sr_time;
1408 stats = &src->stats;
1410 extended_max = stats->cycles + stats->max_seq;
1411 expected = extended_max - stats->base_seq + 1;
1413 GST_DEBUG ("ext_max %" G_GUINT64_FORMAT ", expected %" G_GUINT64_FORMAT
1414 ", received %" G_GUINT64_FORMAT ", base_seq %" G_GUINT32_FORMAT,
1415 extended_max, expected, stats->packets_received, stats->base_seq);
1417 lost = expected - stats->packets_received;
1418 lost = CLAMP (lost, -0x800000, 0x7fffff);
1420 expected_interval = expected - stats->prev_expected;
1421 stats->prev_expected = expected;
1422 received_interval = stats->packets_received - stats->prev_received;
1423 stats->prev_received = stats->packets_received;
1425 lost_interval = expected_interval - received_interval;
1427 if (expected_interval == 0 || lost_interval <= 0)
1430 fraction = (lost_interval << 8) / expected_interval;
1432 GST_DEBUG ("add RR for SSRC %08x", src->ssrc);
1433 /* we scaled the jitter up for additional precision */
1434 GST_DEBUG ("fraction %" G_GUINT32_FORMAT ", lost %" G_GINT64_FORMAT
1435 ", extseq %" G_GUINT64_FORMAT ", jitter %d", fraction, lost,
1436 extended_max, stats->jitter >> 4);
1438 if (rtp_source_get_last_sr (src, &sr_time, &ntptime, NULL, NULL, NULL)) {
1441 /* LSR is middle 32 bits of the last ntptime */
1442 LSR = (ntptime >> 16) & 0xffffffff;
1443 diff = time - sr_time;
1444 GST_DEBUG ("last SR time diff %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
1445 /* DLSR, delay since last SR is expressed in 1/65536 second units */
1446 DLSR = gst_util_uint64_scale_int (diff, 65536, GST_SECOND);
1448 /* No valid SR received, LSR/DLSR are set to 0 then */
1449 GST_DEBUG ("no valid SR received");
1453 GST_DEBUG ("LSR %04x:%04x, DLSR %04x:%04x", LSR >> 16, LSR & 0xffff,
1454 DLSR >> 16, DLSR & 0xffff);
1457 *fractionlost = fraction;
1459 *packetslost = lost;
1461 *exthighestseq = extended_max;
1463 *jitter = stats->jitter >> 4;
1473 * rtp_source_get_last_sr:
1474 * @src: an #RTPSource
1475 * @time: time of packet arrival
1476 * @ntptime: the NTP time in 32.32 fixed point
1477 * @rtptime: the RTP time
1478 * @packet_count: the packet count
1479 * @octet_count: the octect count
1481 * Get the values of the last sender report as set with rtp_source_process_sr().
1483 * Returns: %TRUE if there was a valid SR report.
1486 rtp_source_get_last_sr (RTPSource * src, GstClockTime * time, guint64 * ntptime,
1487 guint32 * rtptime, guint32 * packet_count, guint32 * octet_count)
1489 RTPSenderReport *curr;
1491 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1493 curr = &src->stats.sr[src->stats.curr_sr];
1494 if (!curr->is_valid)
1498 *ntptime = curr->ntptime;
1500 *rtptime = curr->rtptime;
1502 *packet_count = curr->packet_count;
1504 *octet_count = curr->octet_count;
1512 * rtp_source_get_last_rb:
1513 * @src: an #RTPSource
1514 * @fractionlost: fraction lost since last SR/RR
1515 * @packetslost: the cumululative number of packets lost
1516 * @exthighestseq: the extended last sequence number received
1517 * @jitter: the interarrival jitter
1518 * @lsr: the last SR packet from this source
1519 * @dlsr: the delay since last SR packet
1520 * @round_trip: the round trip time
1522 * Get the values of the last RB report set with rtp_source_process_rb().
1524 * Returns: %TRUE if there was a valid SB report.
1527 rtp_source_get_last_rb (RTPSource * src, guint8 * fractionlost,
1528 gint32 * packetslost, guint32 * exthighestseq, guint32 * jitter,
1529 guint32 * lsr, guint32 * dlsr, guint32 * round_trip)
1531 RTPReceiverReport *curr;
1533 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1535 curr = &src->stats.rr[src->stats.curr_rr];
1536 if (!curr->is_valid)
1540 *fractionlost = curr->fractionlost;
1542 *packetslost = curr->packetslost;
1544 *exthighestseq = curr->exthighestseq;
1546 *jitter = curr->jitter;
1552 *round_trip = curr->round_trip;