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);
202 address = g_ntohl (address);
204 g_snprintf (dest, n, "%d.%d.%d.%d:%d", (address >> 24) & 0xff,
205 (address >> 16) & 0xff, (address >> 8) & 0xff, address & 0xff,
209 case GST_NET_TYPE_IP6:
214 gst_netaddress_get_ip6_address (addr, address, &port);
216 g_snprintf (dest, n, "[%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x]:%d",
217 (address[0] << 8) | address[1], (address[2] << 8) | address[3],
218 (address[4] << 8) | address[5], (address[6] << 8) | address[7],
219 (address[8] << 8) | address[9], (address[10] << 8) | address[11],
220 (address[12] << 8) | address[13], (address[14] << 8) | address[15],
230 static GstStructure *
231 rtp_source_create_stats (RTPSource * src)
234 gboolean is_sender = src->is_sender;
235 gboolean internal = src->internal;
236 gchar address_str[MAX_ADDRESS];
238 /* common data for all types of sources */
239 s = gst_structure_new ("application/x-rtp-source-stats",
240 "ssrc", G_TYPE_UINT, (guint) src->ssrc,
241 "internal", G_TYPE_BOOLEAN, internal,
242 "validated", G_TYPE_BOOLEAN, src->validated,
243 "received-bye", G_TYPE_BOOLEAN, src->received_bye,
244 "is-csrc", G_TYPE_BOOLEAN, src->is_csrc,
245 "is-sender", G_TYPE_BOOLEAN, is_sender, NULL);
247 /* add address and port */
248 if (src->have_rtp_from) {
249 make_address_string (&src->rtp_from, address_str, sizeof (address_str));
250 gst_structure_set (s, "rtp-from", G_TYPE_STRING, address_str, NULL);
252 if (src->have_rtcp_from) {
253 make_address_string (&src->rtcp_from, address_str, sizeof (address_str));
254 gst_structure_set (s, "rtcp-from", G_TYPE_STRING, address_str, NULL);
258 /* our internal source */
260 /* if we are sending, report about how much we sent, other sources will
261 * have a RB with info on reception. */
262 gst_structure_set (s,
263 "octets-sent", G_TYPE_UINT64, src->stats.octets_sent,
264 "packets-sent", G_TYPE_UINT64, src->stats.packets_sent,
265 "bitrate", G_TYPE_UINT64, src->bitrate, NULL);
267 /* if we are not sending we have nothing more to report */
271 guint8 fractionlost = 0;
272 gint32 packetslost = 0;
273 guint32 exthighestseq = 0;
277 guint32 round_trip = 0;
282 GstClockTime time = 0;
285 guint32 packet_count = 0;
286 guint32 octet_count = 0;
288 /* this source is sending to us, get the last SR. */
289 have_sr = rtp_source_get_last_sr (src, &time, &ntptime, &rtptime,
290 &packet_count, &octet_count);
291 gst_structure_set (s,
292 "octets-received", G_TYPE_UINT64, src->stats.octets_received,
293 "packets-received", G_TYPE_UINT64, src->stats.packets_received,
294 "have-sr", G_TYPE_BOOLEAN, have_sr,
295 "sr-ntptime", G_TYPE_UINT64, ntptime,
296 "sr-rtptime", G_TYPE_UINT, (guint) rtptime,
297 "sr-octet-count", G_TYPE_UINT, (guint) octet_count,
298 "sr-packet-count", G_TYPE_UINT, (guint) packet_count, NULL);
300 /* we might be sending to this SSRC so we report about how it is
301 * receiving our data */
302 have_rb = rtp_source_get_last_rb (src, &fractionlost, &packetslost,
303 &exthighestseq, &jitter, &lsr, &dlsr, &round_trip);
305 gst_structure_set (s,
306 "have-rb", G_TYPE_BOOLEAN, have_rb,
307 "rb-fractionlost", G_TYPE_UINT, (guint) fractionlost,
308 "rb-packetslost", G_TYPE_INT, (gint) packetslost,
309 "rb-exthighestseq", G_TYPE_UINT, (guint) exthighestseq,
310 "rb-jitter", G_TYPE_UINT, (guint) jitter,
311 "rb-lsr", G_TYPE_UINT, (guint) lsr,
312 "rb-dlsr", G_TYPE_UINT, (guint) dlsr,
313 "rb-round-trip", G_TYPE_UINT, (guint) round_trip, NULL);
319 static GstStructure *
320 rtp_source_create_sdes (RTPSource * src)
325 s = gst_structure_new ("application/x-rtp-source-sdes",
326 "ssrc", G_TYPE_UINT, (guint) src->ssrc, NULL);
328 if ((str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_CNAME))) {
329 gst_structure_set (s, "cname", G_TYPE_STRING, str, NULL);
332 if ((str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_NAME))) {
333 gst_structure_set (s, "name", G_TYPE_STRING, str, NULL);
336 if ((str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_EMAIL))) {
337 gst_structure_set (s, "email", G_TYPE_STRING, str, NULL);
340 if ((str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_PHONE))) {
341 gst_structure_set (s, "phone", G_TYPE_STRING, str, NULL);
344 if ((str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_LOC))) {
345 gst_structure_set (s, "location", G_TYPE_STRING, str, NULL);
348 if ((str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_TOOL))) {
349 gst_structure_set (s, "tool", G_TYPE_STRING, str, NULL);
352 if ((str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_NOTE))) {
353 gst_structure_set (s, "note", G_TYPE_STRING, str, NULL);
360 rtp_source_set_property (GObject * object, guint prop_id,
361 const GValue * value, GParamSpec * pspec)
365 src = RTP_SOURCE (object);
369 src->ssrc = g_value_get_uint (value);
372 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
378 rtp_source_get_property (GObject * object, guint prop_id,
379 GValue * value, GParamSpec * pspec)
383 src = RTP_SOURCE (object);
387 g_value_set_uint (value, rtp_source_get_ssrc (src));
390 g_value_set_boolean (value, rtp_source_is_as_csrc (src));
392 case PROP_IS_VALIDATED:
393 g_value_set_boolean (value, rtp_source_is_validated (src));
396 g_value_set_boolean (value, rtp_source_is_sender (src));
399 g_value_take_boxed (value, rtp_source_create_sdes (src));
402 g_value_take_boxed (value, rtp_source_create_stats (src));
405 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
414 * Create a #RTPSource with @ssrc.
416 * Returns: a new #RTPSource. Use g_object_unref() after usage.
419 rtp_source_new (guint32 ssrc)
423 src = g_object_new (RTP_TYPE_SOURCE, NULL);
430 * rtp_source_set_callbacks:
431 * @src: an #RTPSource
432 * @cb: callback functions
433 * @user_data: user data
435 * Set the callbacks for the source.
438 rtp_source_set_callbacks (RTPSource * src, RTPSourceCallbacks * cb,
441 g_return_if_fail (RTP_IS_SOURCE (src));
443 src->callbacks.push_rtp = cb->push_rtp;
444 src->callbacks.clock_rate = cb->clock_rate;
445 src->user_data = user_data;
449 * rtp_source_get_ssrc:
450 * @src: an #RTPSource
452 * Get the SSRC of @source.
454 * Returns: the SSRC of src.
457 rtp_source_get_ssrc (RTPSource * src)
461 g_return_val_if_fail (RTP_IS_SOURCE (src), 0);
469 * rtp_source_set_as_csrc:
470 * @src: an #RTPSource
472 * Configure @src as a CSRC, this will also validate @src.
475 rtp_source_set_as_csrc (RTPSource * src)
477 g_return_if_fail (RTP_IS_SOURCE (src));
479 src->validated = TRUE;
484 * rtp_source_is_as_csrc:
485 * @src: an #RTPSource
487 * Check if @src is a contributing source.
489 * Returns: %TRUE if @src is acting as a contributing source.
492 rtp_source_is_as_csrc (RTPSource * src)
496 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
498 result = src->is_csrc;
504 * rtp_source_is_active:
505 * @src: an #RTPSource
507 * Check if @src is an active source. A source is active if it has been
508 * validated and has not yet received a BYE packet
510 * Returns: %TRUE if @src is an qactive source.
513 rtp_source_is_active (RTPSource * src)
517 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
519 result = RTP_SOURCE_IS_ACTIVE (src);
525 * rtp_source_is_validated:
526 * @src: an #RTPSource
528 * Check if @src is a validated source.
530 * Returns: %TRUE if @src is a validated source.
533 rtp_source_is_validated (RTPSource * src)
537 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
539 result = src->validated;
545 * rtp_source_is_sender:
546 * @src: an #RTPSource
548 * Check if @src is a sending source.
550 * Returns: %TRUE if @src is a sending source.
553 rtp_source_is_sender (RTPSource * src)
557 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
559 result = RTP_SOURCE_IS_SENDER (src);
565 * rtp_source_received_bye:
566 * @src: an #RTPSource
568 * Check if @src has receoved a BYE packet.
570 * Returns: %TRUE if @src has received a BYE packet.
573 rtp_source_received_bye (RTPSource * src)
577 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
579 result = src->received_bye;
586 * rtp_source_get_bye_reason:
587 * @src: an #RTPSource
589 * Get the BYE reason for @src. Check if the source receoved a BYE message first
590 * with rtp_source_received_bye().
592 * Returns: The BYE reason or NULL when no reason was given or the source did
593 * not receive a BYE message yet. g_fee() after usage.
596 rtp_source_get_bye_reason (RTPSource * src)
600 g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
602 result = g_strdup (src->bye_reason);
608 * rtp_source_update_caps:
609 * @src: an #RTPSource
612 * Parse @caps and store all relevant information in @source.
615 rtp_source_update_caps (RTPSource * src, GstCaps * caps)
621 /* nothing changed, return */
622 if (src->caps == caps)
625 s = gst_caps_get_structure (caps, 0);
627 if (gst_structure_get_int (s, "payload", &ival))
631 GST_DEBUG ("got payload %d", src->payload);
633 if (gst_structure_get_int (s, "clock-rate", &ival))
634 src->clock_rate = ival;
636 src->clock_rate = -1;
638 GST_DEBUG ("got clock-rate %d", src->clock_rate);
640 if (gst_structure_get_uint (s, "seqnum-base", &val))
641 src->seqnum_base = val;
643 src->seqnum_base = -1;
645 GST_DEBUG ("got seqnum-base %" G_GINT32_FORMAT, src->seqnum_base);
647 gst_caps_replace (&src->caps, caps);
651 * rtp_source_set_sdes:
652 * @src: an #RTPSource
653 * @type: the type of the SDES item
654 * @data: the SDES data
655 * @len: the SDES length
657 * Store an SDES item of @type in @src.
659 * Returns: %FALSE if the SDES item was unchanged or @type is unknown.
662 rtp_source_set_sdes (RTPSource * src, GstRTCPSDESType type,
663 const guint8 * data, guint len)
667 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
669 if (type < 0 || type > GST_RTCP_SDES_PRIV)
672 old = src->sdes[type];
674 /* lengths are the same, check if the data is the same */
675 if ((src->sdes_len[type] == len))
676 if (data != NULL && old != NULL && (memcmp (old, data, len) == 0))
679 /* NULL data, make sure we store 0 length or if no length is given,
684 g_free (src->sdes[type]);
685 src->sdes[type] = g_memdup (data, len);
686 src->sdes_len[type] = len;
692 * rtp_source_set_sdes_string:
693 * @src: an #RTPSource
694 * @type: the type of the SDES item
695 * @data: the SDES data
697 * Store an SDES item of @type in @src. This function is similar to
698 * rtp_source_set_sdes() but takes a null-terminated string for convenience.
700 * Returns: %FALSE if the SDES item was unchanged or @type is unknown.
703 rtp_source_set_sdes_string (RTPSource * src, GstRTCPSDESType type,
714 result = rtp_source_set_sdes (src, type, (guint8 *) data, len);
720 * rtp_source_get_sdes:
721 * @src: an #RTPSource
722 * @type: the type of the SDES item
723 * @data: location to store the SDES data or NULL
724 * @len: location to store the SDES length or NULL
726 * Get the SDES item of @type from @src. Note that @data does not always point
727 * to a null-terminated string, use rtp_source_get_sdes_string() to retrieve a
728 * null-terminated string instead.
730 * @data remains valid until the next call to rtp_source_set_sdes().
732 * Returns: %TRUE if @type was valid and @data and @len contain valid
733 * data. @data can be NULL when the item was unset.
736 rtp_source_get_sdes (RTPSource * src, GstRTCPSDESType type, guint8 ** data,
739 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
741 if (type < 0 || type > GST_RTCP_SDES_PRIV)
745 *data = src->sdes[type];
747 *len = src->sdes_len[type];
753 * rtp_source_get_sdes_string:
754 * @src: an #RTPSource
755 * @type: the type of the SDES item
757 * Get the SDES item of @type from @src.
759 * Returns: a null-terminated copy of the SDES item or NULL when @type was not
760 * valid or the SDES item was unset. g_free() after usage.
763 rtp_source_get_sdes_string (RTPSource * src, GstRTCPSDESType type)
767 g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
769 if (type < 0 || type > GST_RTCP_SDES_PRIV)
772 result = g_strndup ((const gchar *) src->sdes[type], src->sdes_len[type]);
778 * rtp_source_set_rtp_from:
779 * @src: an #RTPSource
780 * @address: the RTP address to set
782 * Set that @src is receiving RTP packets from @address. This is used for
783 * collistion checking.
786 rtp_source_set_rtp_from (RTPSource * src, GstNetAddress * address)
788 g_return_if_fail (RTP_IS_SOURCE (src));
790 src->have_rtp_from = TRUE;
791 memcpy (&src->rtp_from, address, sizeof (GstNetAddress));
795 * rtp_source_set_rtcp_from:
796 * @src: an #RTPSource
797 * @address: the RTCP address to set
799 * Set that @src is receiving RTCP packets from @address. This is used for
800 * collistion checking.
803 rtp_source_set_rtcp_from (RTPSource * src, GstNetAddress * address)
805 g_return_if_fail (RTP_IS_SOURCE (src));
807 src->have_rtcp_from = TRUE;
808 memcpy (&src->rtcp_from, address, sizeof (GstNetAddress));
812 push_packet (RTPSource * src, GstBuffer * buffer)
814 GstFlowReturn ret = GST_FLOW_OK;
816 /* push queued packets first if any */
817 while (!g_queue_is_empty (src->packets)) {
818 GstBuffer *buffer = GST_BUFFER_CAST (g_queue_pop_head (src->packets));
820 GST_LOG ("pushing queued packet");
821 if (src->callbacks.push_rtp)
822 src->callbacks.push_rtp (src, buffer, src->user_data);
824 gst_buffer_unref (buffer);
826 GST_LOG ("pushing new packet");
828 if (src->callbacks.push_rtp)
829 ret = src->callbacks.push_rtp (src, buffer, src->user_data);
831 gst_buffer_unref (buffer);
837 get_clock_rate (RTPSource * src, guint8 payload)
839 if (src->payload == -1) {
840 /* first payload received, nothing was in the caps, lock on to this payload */
841 src->payload = payload;
842 GST_DEBUG ("first payload %d", payload);
843 } else if (payload != src->payload) {
844 /* we have a different payload than before, reset the clock-rate */
845 GST_DEBUG ("new payload %d", payload);
846 src->payload = payload;
847 src->clock_rate = -1;
848 src->stats.transit = -1;
851 if (src->clock_rate == -1) {
852 gint clock_rate = -1;
854 if (src->callbacks.clock_rate)
855 clock_rate = src->callbacks.clock_rate (src, payload, src->user_data);
857 GST_DEBUG ("got clock-rate %d", clock_rate);
859 src->clock_rate = clock_rate;
861 return src->clock_rate;
864 /* Jitter is the variation in the delay of received packets in a flow. It is
865 * measured by comparing the interval when RTP packets were sent to the interval
866 * at which they were received. For instance, if packet #1 and packet #2 leave
867 * 50 milliseconds apart and arrive 60 milliseconds apart, then the jitter is 10
870 calculate_jitter (RTPSource * src, GstBuffer * buffer,
871 RTPArrivalStats * arrival)
874 guint32 rtparrival, transit, rtptime;
879 /* get arrival time */
880 if ((ntpnstime = arrival->ntpnstime) == GST_CLOCK_TIME_NONE)
883 pt = gst_rtp_buffer_get_payload_type (buffer);
885 GST_LOG ("SSRC %08x got payload %d", src->ssrc, pt);
888 if ((clock_rate = get_clock_rate (src, pt)) == -1)
891 rtptime = gst_rtp_buffer_get_timestamp (buffer);
893 /* convert arrival time to RTP timestamp units, truncate to 32 bits, we don't
894 * care about the absolute value, just the difference. */
895 rtparrival = gst_util_uint64_scale_int (ntpnstime, clock_rate, GST_SECOND);
897 /* transit time is difference with RTP timestamp */
898 transit = rtparrival - rtptime;
900 /* get ABS diff with previous transit time */
901 if (src->stats.transit != -1) {
902 if (transit > src->stats.transit)
903 diff = transit - src->stats.transit;
905 diff = src->stats.transit - transit;
909 src->stats.transit = transit;
911 /* update jitter, the value we store is scaled up so we can keep precision. */
912 src->stats.jitter += diff - ((src->stats.jitter + 8) >> 4);
914 src->stats.prev_rtptime = src->stats.last_rtptime;
915 src->stats.last_rtptime = rtparrival;
917 GST_LOG ("rtparrival %u, rtptime %u, clock-rate %d, diff %d, jitter: %f",
918 rtparrival, rtptime, clock_rate, diff, (src->stats.jitter) / 16.0);
925 GST_WARNING ("cannot get current time");
930 GST_WARNING ("cannot get clock-rate for pt %d", pt);
936 init_seq (RTPSource * src, guint16 seq)
938 src->stats.base_seq = seq;
939 src->stats.max_seq = seq;
940 src->stats.bad_seq = RTP_SEQ_MOD + 1; /* so seq == bad_seq is false */
941 src->stats.cycles = 0;
942 src->stats.packets_received = 0;
943 src->stats.octets_received = 0;
944 src->stats.bytes_received = 0;
945 src->stats.prev_received = 0;
946 src->stats.prev_expected = 0;
948 GST_DEBUG ("base_seq %d", seq);
952 * rtp_source_process_rtp:
953 * @src: an #RTPSource
954 * @buffer: an RTP buffer
956 * Let @src handle the incomming RTP @buffer.
958 * Returns: a #GstFlowReturn.
961 rtp_source_process_rtp (RTPSource * src, GstBuffer * buffer,
962 RTPArrivalStats * arrival)
964 GstFlowReturn result = GST_FLOW_OK;
965 guint16 seqnr, udelta;
966 RTPSourceStats *stats;
968 g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
969 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
973 seqnr = gst_rtp_buffer_get_seq (buffer);
975 rtp_source_update_caps (src, GST_BUFFER_CAPS (buffer));
977 if (stats->cycles == -1) {
978 GST_DEBUG ("received first buffer");
979 /* first time we heard of this source */
980 init_seq (src, seqnr);
981 src->stats.max_seq = seqnr - 1;
982 src->probation = RTP_DEFAULT_PROBATION;
985 udelta = seqnr - stats->max_seq;
987 /* if we are still on probation, check seqnum */
988 if (src->probation) {
991 expected = src->stats.max_seq + 1;
993 /* when in probation, we require consecutive seqnums */
994 if (seqnr == expected) {
995 /* expected packet */
996 GST_DEBUG ("probation: seqnr %d == expected %d", seqnr, expected);
998 src->stats.max_seq = seqnr;
999 if (src->probation == 0) {
1000 GST_DEBUG ("probation done!");
1001 init_seq (src, seqnr);
1005 GST_DEBUG ("probation %d: queue buffer", src->probation);
1006 /* when still in probation, keep packets in a list. */
1007 g_queue_push_tail (src->packets, buffer);
1008 /* remove packets from queue if there are too many */
1009 while (g_queue_get_length (src->packets) > RTP_MAX_PROBATION_LEN) {
1010 q = g_queue_pop_head (src->packets);
1011 gst_buffer_unref (q);
1016 GST_DEBUG ("probation: seqnr %d != expected %d", seqnr, expected);
1017 src->probation = RTP_DEFAULT_PROBATION;
1018 src->stats.max_seq = seqnr;
1021 } else if (udelta < RTP_MAX_DROPOUT) {
1022 /* in order, with permissible gap */
1023 if (seqnr < stats->max_seq) {
1024 /* sequence number wrapped - count another 64K cycle. */
1025 stats->cycles += RTP_SEQ_MOD;
1027 stats->max_seq = seqnr;
1028 } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
1029 /* the sequence number made a very large jump */
1030 if (seqnr == stats->bad_seq) {
1031 /* two sequential packets -- assume that the other side
1032 * restarted without telling us so just re-sync
1033 * (i.e., pretend this was the first packet). */
1034 init_seq (src, seqnr);
1036 /* unacceptable jump */
1037 stats->bad_seq = (seqnr + 1) & (RTP_SEQ_MOD - 1);
1041 /* duplicate or reordered packet, will be filtered by jitterbuffer. */
1042 GST_WARNING ("duplicate or reordered packet");
1045 src->stats.octets_received += arrival->payload_len;
1046 src->stats.bytes_received += arrival->bytes;
1047 src->stats.packets_received++;
1048 /* the source that sent the packet must be a sender */
1049 src->is_sender = TRUE;
1050 src->validated = TRUE;
1052 GST_LOG ("seq %d, PC: %" G_GUINT64_FORMAT ", OC: %" G_GUINT64_FORMAT,
1053 seqnr, src->stats.packets_received, src->stats.octets_received);
1055 /* calculate jitter for the stats */
1056 calculate_jitter (src, buffer, arrival);
1058 /* we're ready to push the RTP packet now */
1059 result = push_packet (src, buffer);
1067 GST_WARNING ("unacceptable seqnum received");
1073 * rtp_source_process_bye:
1074 * @src: an #RTPSource
1075 * @reason: the reason for leaving
1077 * Notify @src that a BYE packet has been received. This will make the source
1081 rtp_source_process_bye (RTPSource * src, const gchar * reason)
1083 g_return_if_fail (RTP_IS_SOURCE (src));
1085 GST_DEBUG ("marking SSRC %08x as BYE, reason: %s", src->ssrc,
1086 GST_STR_NULL (reason));
1088 /* copy the reason and mark as received_bye */
1089 g_free (src->bye_reason);
1090 src->bye_reason = g_strdup (reason);
1091 src->received_bye = TRUE;
1095 * rtp_source_send_rtp:
1096 * @src: an #RTPSource
1097 * @buffer: an RTP buffer
1098 * @ntpnstime: the NTP time when this buffer was captured in nanoseconds. This
1099 * is the buffer timestamp converted to NTP time.
1101 * Send an RTP @buffer originating from @src. This will make @src a sender.
1102 * This function takes ownership of @buffer and modifies the SSRC in the RTP
1103 * packet to that of @src when needed.
1105 * Returns: a #GstFlowReturn.
1108 rtp_source_send_rtp (RTPSource * src, GstBuffer * buffer, guint64 ntpnstime)
1110 GstFlowReturn result = GST_FLOW_OK;
1113 guint64 ext_rtptime;
1114 guint64 ntp_diff, rtp_diff;
1117 g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
1118 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1120 len = gst_rtp_buffer_get_payload_len (buffer);
1122 rtp_source_update_caps (src, GST_BUFFER_CAPS (buffer));
1124 /* we are a sender now */
1125 src->is_sender = TRUE;
1127 /* update stats for the SR */
1128 src->stats.packets_sent++;
1129 src->stats.octets_sent += len;
1130 src->bytes_sent += len;
1132 if (src->prev_ntpnstime) {
1133 elapsed = ntpnstime - src->prev_ntpnstime;
1135 if (elapsed > (G_GINT64_CONSTANT (1) << 31)) {
1139 gst_util_uint64_scale (src->bytes_sent, elapsed,
1140 (G_GINT64_CONSTANT (1) << 29));
1142 GST_LOG ("Elapsed %" G_GUINT64_FORMAT ", bytes %" G_GUINT64_FORMAT
1143 ", rate %" G_GUINT64_FORMAT, elapsed, src->bytes_sent, rate);
1145 if (src->bitrate == 0)
1146 src->bitrate = rate;
1148 src->bitrate = ((src->bitrate * 3) + rate) / 4;
1150 src->prev_ntpnstime = ntpnstime;
1151 src->bytes_sent = 0;
1154 GST_LOG ("Reset bitrate measurement");
1155 src->prev_ntpnstime = ntpnstime;
1159 rtptime = gst_rtp_buffer_get_timestamp (buffer);
1160 ext_rtptime = src->last_rtptime;
1161 ext_rtptime = gst_rtp_buffer_ext_timestamp (&ext_rtptime, rtptime);
1163 GST_LOG ("SSRC %08x, RTP %" G_GUINT64_FORMAT ", NTP %" GST_TIME_FORMAT,
1164 src->ssrc, ext_rtptime, GST_TIME_ARGS (ntpnstime));
1166 if (ext_rtptime > src->last_rtptime) {
1167 rtp_diff = ext_rtptime - src->last_rtptime;
1168 ntp_diff = ntpnstime - src->last_ntpnstime;
1170 /* calc the diff so we can detect drift at the sender. This can also be used
1171 * to guestimate the clock rate if the NTP time is locked to the RTP
1172 * timestamps (as is the case when the capture device is providing the clock). */
1173 GST_LOG ("SSRC %08x, diff RTP %" G_GUINT64_FORMAT ", diff NTP %"
1174 GST_TIME_FORMAT, src->ssrc, rtp_diff, GST_TIME_ARGS (ntp_diff));
1177 /* we keep track of the last received RTP timestamp and the corresponding
1178 * NTP timestamp so that we can use this info when constructing SR reports */
1179 src->last_rtptime = ext_rtptime;
1180 src->last_ntpnstime = ntpnstime;
1183 if (src->callbacks.push_rtp) {
1186 ssrc = gst_rtp_buffer_get_ssrc (buffer);
1187 if (ssrc != src->ssrc) {
1188 /* the SSRC of the packet is not correct, make a writable buffer and
1189 * update the SSRC. This could involve a complete copy of the packet when
1190 * it is not writable. Usually the payloader will use caps negotiation to
1191 * get the correct SSRC from the session manager before pushing anything. */
1192 buffer = gst_buffer_make_writable (buffer);
1194 /* FIXME, we don't want to warn yet because we can't inform any payloader
1195 * of the changes SSRC yet because we don't implement pad-alloc. */
1196 GST_LOG ("updating SSRC from %08x to %08x, fix the payloader", ssrc,
1198 gst_rtp_buffer_set_ssrc (buffer, src->ssrc);
1200 GST_LOG ("pushing RTP packet %" G_GUINT64_FORMAT, src->stats.packets_sent);
1201 result = src->callbacks.push_rtp (src, buffer, src->user_data);
1203 GST_WARNING ("no callback installed, dropping packet");
1204 gst_buffer_unref (buffer);
1211 * rtp_source_process_sr:
1212 * @src: an #RTPSource
1213 * @time: time of packet arrival
1214 * @ntptime: the NTP time in 32.32 fixed point
1215 * @rtptime: the RTP time
1216 * @packet_count: the packet count
1217 * @octet_count: the octect count
1219 * Update the sender report in @src.
1222 rtp_source_process_sr (RTPSource * src, GstClockTime time, guint64 ntptime,
1223 guint32 rtptime, guint32 packet_count, guint32 octet_count)
1225 RTPSenderReport *curr;
1228 g_return_if_fail (RTP_IS_SOURCE (src));
1230 GST_DEBUG ("got SR packet: SSRC %08x, NTP %08x:%08x, RTP %" G_GUINT32_FORMAT
1231 ", PC %" G_GUINT32_FORMAT ", OC %" G_GUINT32_FORMAT, src->ssrc,
1232 (guint32) (ntptime >> 32), (guint32) (ntptime & 0xffffffff), rtptime,
1233 packet_count, octet_count);
1235 curridx = src->stats.curr_sr ^ 1;
1236 curr = &src->stats.sr[curridx];
1238 /* this is a sender now */
1239 src->is_sender = TRUE;
1241 /* update current */
1242 curr->is_valid = TRUE;
1243 curr->ntptime = ntptime;
1244 curr->rtptime = rtptime;
1245 curr->packet_count = packet_count;
1246 curr->octet_count = octet_count;
1250 src->stats.curr_sr = curridx;
1254 * rtp_source_process_rb:
1255 * @src: an #RTPSource
1256 * @time: the current time in nanoseconds since 1970
1257 * @fractionlost: fraction lost since last SR/RR
1258 * @packetslost: the cumululative number of packets lost
1259 * @exthighestseq: the extended last sequence number received
1260 * @jitter: the interarrival jitter
1261 * @lsr: the last SR packet from this source
1262 * @dlsr: the delay since last SR packet
1264 * Update the report block in @src.
1267 rtp_source_process_rb (RTPSource * src, GstClockTime time, guint8 fractionlost,
1268 gint32 packetslost, guint32 exthighestseq, guint32 jitter, guint32 lsr,
1271 RTPReceiverReport *curr;
1275 g_return_if_fail (RTP_IS_SOURCE (src));
1277 GST_DEBUG ("got RB packet: SSRC %08x, FL %2x, PL %d, HS %" G_GUINT32_FORMAT
1278 ", jitter %" G_GUINT32_FORMAT ", LSR %04x:%04x, DLSR %04x:%04x",
1279 src->ssrc, fractionlost, packetslost, exthighestseq, jitter, lsr >> 16,
1280 lsr & 0xffff, dlsr >> 16, dlsr & 0xffff);
1282 curridx = src->stats.curr_rr ^ 1;
1283 curr = &src->stats.rr[curridx];
1285 /* update current */
1286 curr->is_valid = TRUE;
1287 curr->fractionlost = fractionlost;
1288 curr->packetslost = packetslost;
1289 curr->exthighestseq = exthighestseq;
1290 curr->jitter = jitter;
1294 /* calculate round trip, round the time up */
1295 ntp = ((gst_rtcp_unix_to_ntp (time) + 0xffff) >> 16) & 0xffffffff;
1297 if (A > 0 && ntp > A)
1301 curr->round_trip = A;
1303 GST_DEBUG ("NTP %04x:%04x, round trip %04x:%04x", ntp >> 16, ntp & 0xffff,
1304 A >> 16, A & 0xffff);
1307 src->stats.curr_rr = curridx;
1311 * rtp_source_get_new_sr:
1312 * @src: an #RTPSource
1313 * @ntpnstime: the current time in nanoseconds since 1970
1314 * @ntptime: the NTP time in 32.32 fixed point
1315 * @rtptime: the RTP time corresponding to @ntptime
1316 * @packet_count: the packet count
1317 * @octet_count: the octect count
1319 * Get new values to put into a new SR report from this source.
1321 * Returns: %TRUE on success.
1324 rtp_source_get_new_sr (RTPSource * src, guint64 ntpnstime,
1325 guint64 * ntptime, guint32 * rtptime, guint32 * packet_count,
1326 guint32 * octet_count)
1329 guint64 t_current_ntp;
1330 GstClockTimeDiff diff;
1332 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1334 /* use the sync params to interpolate the date->time member to rtptime. We
1335 * use the last sent timestamp and rtptime as reference points. We assume
1336 * that the slope of the rtptime vs timestamp curve is 1, which is certainly
1337 * sufficient for the frequency at which we report SR and the rate we send
1338 * out RTP packets. */
1339 t_rtp = src->last_rtptime;
1341 GST_DEBUG ("last_ntpnstime %" GST_TIME_FORMAT ", last_rtptime %"
1342 G_GUINT64_FORMAT, GST_TIME_ARGS (src->last_ntpnstime), t_rtp);
1344 if (src->clock_rate != -1) {
1345 /* get the diff with the SR time */
1346 diff = GST_CLOCK_DIFF (src->last_ntpnstime, ntpnstime);
1348 /* now translate the diff to RTP time, handle positive and negative cases.
1349 * If there is no diff, we already set rtptime correctly above. */
1351 GST_DEBUG ("ntpnstime %" GST_TIME_FORMAT ", diff %" GST_TIME_FORMAT,
1352 GST_TIME_ARGS (ntpnstime), GST_TIME_ARGS (diff));
1353 t_rtp += gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1356 GST_DEBUG ("ntpnstime %" GST_TIME_FORMAT ", diff -%" GST_TIME_FORMAT,
1357 GST_TIME_ARGS (ntpnstime), GST_TIME_ARGS (diff));
1358 t_rtp -= gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1361 GST_WARNING ("no clock-rate, cannot interpolate rtp time");
1364 /* convert the NTP time in nanoseconds to 32.32 fixed point */
1365 t_current_ntp = gst_util_uint64_scale (ntpnstime, (1LL << 32), GST_SECOND);
1367 GST_DEBUG ("NTP %08x:%08x, RTP %" G_GUINT32_FORMAT,
1368 (guint32) (t_current_ntp >> 32), (guint32) (t_current_ntp & 0xffffffff),
1372 *ntptime = t_current_ntp;
1376 *packet_count = src->stats.packets_sent;
1378 *octet_count = src->stats.octets_sent;
1384 * rtp_source_get_new_rb:
1385 * @src: an #RTPSource
1386 * @time: the current time of the system clock
1387 * @fractionlost: fraction lost since last SR/RR
1388 * @packetslost: the cumululative number of packets lost
1389 * @exthighestseq: the extended last sequence number received
1390 * @jitter: the interarrival jitter
1391 * @lsr: the last SR packet from this source
1392 * @dlsr: the delay since last SR packet
1394 * Get new values to put into a new report block from this source.
1396 * Returns: %TRUE on success.
1399 rtp_source_get_new_rb (RTPSource * src, GstClockTime time,
1400 guint8 * fractionlost, gint32 * packetslost, guint32 * exthighestseq,
1401 guint32 * jitter, guint32 * lsr, guint32 * dlsr)
1403 RTPSourceStats *stats;
1404 guint64 extended_max, expected;
1405 guint64 expected_interval, received_interval, ntptime;
1406 gint64 lost, lost_interval;
1407 guint32 fraction, LSR, DLSR;
1408 GstClockTime sr_time;
1410 stats = &src->stats;
1412 extended_max = stats->cycles + stats->max_seq;
1413 expected = extended_max - stats->base_seq + 1;
1415 GST_DEBUG ("ext_max %" G_GUINT64_FORMAT ", expected %" G_GUINT64_FORMAT
1416 ", received %" G_GUINT64_FORMAT ", base_seq %" G_GUINT32_FORMAT,
1417 extended_max, expected, stats->packets_received, stats->base_seq);
1419 lost = expected - stats->packets_received;
1420 lost = CLAMP (lost, -0x800000, 0x7fffff);
1422 expected_interval = expected - stats->prev_expected;
1423 stats->prev_expected = expected;
1424 received_interval = stats->packets_received - stats->prev_received;
1425 stats->prev_received = stats->packets_received;
1427 lost_interval = expected_interval - received_interval;
1429 if (expected_interval == 0 || lost_interval <= 0)
1432 fraction = (lost_interval << 8) / expected_interval;
1434 GST_DEBUG ("add RR for SSRC %08x", src->ssrc);
1435 /* we scaled the jitter up for additional precision */
1436 GST_DEBUG ("fraction %" G_GUINT32_FORMAT ", lost %" G_GINT64_FORMAT
1437 ", extseq %" G_GUINT64_FORMAT ", jitter %d", fraction, lost,
1438 extended_max, stats->jitter >> 4);
1440 if (rtp_source_get_last_sr (src, &sr_time, &ntptime, NULL, NULL, NULL)) {
1443 /* LSR is middle 32 bits of the last ntptime */
1444 LSR = (ntptime >> 16) & 0xffffffff;
1445 diff = time - sr_time;
1446 GST_DEBUG ("last SR time diff %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
1447 /* DLSR, delay since last SR is expressed in 1/65536 second units */
1448 DLSR = gst_util_uint64_scale_int (diff, 65536, GST_SECOND);
1450 /* No valid SR received, LSR/DLSR are set to 0 then */
1451 GST_DEBUG ("no valid SR received");
1455 GST_DEBUG ("LSR %04x:%04x, DLSR %04x:%04x", LSR >> 16, LSR & 0xffff,
1456 DLSR >> 16, DLSR & 0xffff);
1459 *fractionlost = fraction;
1461 *packetslost = lost;
1463 *exthighestseq = extended_max;
1465 *jitter = stats->jitter >> 4;
1475 * rtp_source_get_last_sr:
1476 * @src: an #RTPSource
1477 * @time: time of packet arrival
1478 * @ntptime: the NTP time in 32.32 fixed point
1479 * @rtptime: the RTP time
1480 * @packet_count: the packet count
1481 * @octet_count: the octect count
1483 * Get the values of the last sender report as set with rtp_source_process_sr().
1485 * Returns: %TRUE if there was a valid SR report.
1488 rtp_source_get_last_sr (RTPSource * src, GstClockTime * time, guint64 * ntptime,
1489 guint32 * rtptime, guint32 * packet_count, guint32 * octet_count)
1491 RTPSenderReport *curr;
1493 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1495 curr = &src->stats.sr[src->stats.curr_sr];
1496 if (!curr->is_valid)
1500 *ntptime = curr->ntptime;
1502 *rtptime = curr->rtptime;
1504 *packet_count = curr->packet_count;
1506 *octet_count = curr->octet_count;
1514 * rtp_source_get_last_rb:
1515 * @src: an #RTPSource
1516 * @fractionlost: fraction lost since last SR/RR
1517 * @packetslost: the cumululative number of packets lost
1518 * @exthighestseq: the extended last sequence number received
1519 * @jitter: the interarrival jitter
1520 * @lsr: the last SR packet from this source
1521 * @dlsr: the delay since last SR packet
1522 * @round_trip: the round trip time
1524 * Get the values of the last RB report set with rtp_source_process_rb().
1526 * Returns: %TRUE if there was a valid SB report.
1529 rtp_source_get_last_rb (RTPSource * src, guint8 * fractionlost,
1530 gint32 * packetslost, guint32 * exthighestseq, guint32 * jitter,
1531 guint32 * lsr, guint32 * dlsr, guint32 * round_trip)
1533 RTPReceiverReport *curr;
1535 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1537 curr = &src->stats.rr[src->stats.curr_rr];
1538 if (!curr->is_valid)
1542 *fractionlost = curr->fractionlost;
1544 *packetslost = curr->packetslost;
1546 *exthighestseq = curr->exthighestseq;
1548 *jitter = curr->jitter;
1554 *round_trip = curr->round_trip;