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 name
102 * application/x-rtp-source-sdes and may contain the following fields:
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 * other fields may be present and these represent private items in
113 * the SDES where the field name is the prefix.
115 g_object_class_install_property (gobject_class, PROP_SDES,
116 g_param_spec_boxed ("sdes", "SDES",
117 "The SDES information for this source",
118 GST_TYPE_STRUCTURE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
123 * The statistics of the source. This property returns a GstStructure with
124 * name application/x-rtp-source-stats with the following fields:
126 * "ssrc" G_TYPE_UINT The SSRC of this source
127 * "internal" G_TYPE_BOOLEAN If this source is the source of the session
128 * "validated" G_TYPE_BOOLEAN If the source is validated
129 * "received-bye" G_TYPE_BOOLEAN If we received a BYE from this source
130 * "is-csrc" G_TYPE_BOOLEAN If this source was found as CSRC
131 * "is-sender" G_TYPE_BOOLEAN If this source is a sender
132 * "seqnum-base" G_TYPE_INT first seqnum if known
133 * "clock-rate" G_TYPE_INT the clock rate of the media
135 * The following two fields are only present when known.
137 * "rtp-from" G_TYPE_STRING where we received the last RTP packet from
138 * "rtcp-from" G_TYPE_STRING where we received the last RTCP packet from
140 * The following fields make sense for internal sources and will only increase
141 * when "is-sender" is TRUE:
143 * "octets-sent" G_TYPE_UINT64 number of bytes we sent
144 * "packets-sent" G_TYPE_UINT64 number of packets we sent
146 * The following fields make sense for non-internal sources and will only
147 * increase when "is-sender" is TRUE.
149 * "octets-received" G_TYPE_UINT64 total number of bytes received
150 * "packets-received" G_TYPE_UINT64 total number of packets received
152 * Following fields are updated when "is-sender" is TRUE.
154 * "bitrate" G_TYPE_UINT64 bitrate in bits per second
155 * "jitter" G_TYPE_UINT estimated jitter
156 * "packets-lost" G_TYPE_INT estimated amount of packets lost
158 * The last SR report this source sent. This only updates when "is-sender" is
161 * "have-sr" G_TYPE_BOOLEAN the source has sent SR
162 * "sr-ntptime" G_TYPE_UINT64 ntptime of SR
163 * "sr-rtptime" G_TYPE_UINT rtptime of SR
164 * "sr-octet-count" G_TYPE_UINT the number of bytes in the SR
165 * "sr-packet-count" G_TYPE_UINT the number of packets in the SR
167 * The following fields are only present for non-internal sources and
168 * represent the content of the last RB packet that was sent to this source.
169 * These values are only updated when the source is sending.
171 * "sent-rb" G_TYPE_BOOLEAN we have sent an RB
172 * "sent-rb-fractionlost" G_TYPE_UINT calculated lost fraction
173 * "sent-rb-packetslost" G_TYPE_INT lost packets
174 * "sent-rb-exthighestseq" G_TYPE_UINT last seen seqnum
175 * "sent-rb-jitter" G_TYPE_UINT jitter
176 * "sent-rb-lsr" G_TYPE_UINT last SR time
177 * "sent-rb-dlsr" G_TYPE_UINT delay since last SR
179 * The following fields are only present for non-internal sources and
180 * represents the last RB that this source sent. This is only updated
181 * when the source is receiving data and sending RB blocks.
183 * "have-rb" G_TYPE_BOOLEAN the source has sent RB
184 * "rb-fractionlost" G_TYPE_UINT lost fraction
185 * "rb-packetslost" G_TYPE_INT lost packets
186 * "rb-exthighestseq" G_TYPE_UINT highest received seqnum
187 * "rb-jitter" G_TYPE_UINT reception jitter
188 * "rb-lsr" G_TYPE_UINT last SR time
189 * "rb-dlsr" G_TYPE_UINT delay since last SR
191 * The round trip of this source. This is calculated from the last RB
192 * values and the recption time of the last RB packet. Only present for
193 * non-internal sources.
195 * "rb-round-trip" G_TYPE_UINT the round trip time in nanoseconds
197 g_object_class_install_property (gobject_class, PROP_STATS,
198 g_param_spec_boxed ("stats", "Stats",
199 "The stats of this source", GST_TYPE_STRUCTURE,
200 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
202 GST_DEBUG_CATEGORY_INIT (rtp_source_debug, "rtpsource", 0, "RTP Source");
207 * @src: an #RTPSource
209 * Reset the stats of @src.
212 rtp_source_reset (RTPSource * src)
214 src->received_bye = FALSE;
216 src->stats.cycles = -1;
217 src->stats.jitter = 0;
218 src->stats.transit = -1;
219 src->stats.curr_sr = 0;
220 src->stats.curr_rr = 0;
224 rtp_source_init (RTPSource * src)
226 /* sources are initialy on probation until we receive enough valid RTP
227 * packets or a valid RTCP packet */
228 src->validated = FALSE;
229 src->internal = FALSE;
230 src->probation = RTP_DEFAULT_PROBATION;
231 src->closing = FALSE;
233 src->sdes = gst_structure_new_empty ("application/x-rtp-source-sdes");
236 src->clock_rate = -1;
237 src->packets = g_queue_new ();
238 src->seqnum_base = -1;
239 src->last_rtptime = -1;
241 src->retained_feedback = g_queue_new ();
243 rtp_source_reset (src);
247 rtp_source_finalize (GObject * object)
252 src = RTP_SOURCE_CAST (object);
254 while ((buffer = g_queue_pop_head (src->packets)))
255 gst_buffer_unref (buffer);
256 g_queue_free (src->packets);
258 gst_structure_free (src->sdes);
260 g_free (src->bye_reason);
262 gst_caps_replace (&src->caps, NULL);
264 g_list_foreach (src->conflicting_addresses, (GFunc) g_free, NULL);
265 g_list_free (src->conflicting_addresses);
267 while ((buffer = g_queue_pop_head (src->retained_feedback)))
268 gst_buffer_unref (buffer);
269 g_queue_free (src->retained_feedback);
271 G_OBJECT_CLASS (rtp_source_parent_class)->finalize (object);
274 static GstStructure *
275 rtp_source_create_stats (RTPSource * src)
278 gboolean is_sender = src->is_sender;
279 gboolean internal = src->internal;
280 gchar address_str[GST_NETADDRESS_MAX_LEN];
282 guint8 fractionlost = 0;
283 gint32 packetslost = 0;
284 guint32 exthighestseq = 0;
288 guint32 round_trip = 0;
290 GstClockTime time = 0;
293 guint32 packet_count = 0;
294 guint32 octet_count = 0;
297 /* common data for all types of sources */
298 s = gst_structure_new ("application/x-rtp-source-stats",
299 "ssrc", G_TYPE_UINT, (guint) src->ssrc,
300 "internal", G_TYPE_BOOLEAN, internal,
301 "validated", G_TYPE_BOOLEAN, src->validated,
302 "received-bye", G_TYPE_BOOLEAN, src->received_bye,
303 "is-csrc", G_TYPE_BOOLEAN, src->is_csrc,
304 "is-sender", G_TYPE_BOOLEAN, is_sender,
305 "seqnum-base", G_TYPE_INT, src->seqnum_base,
306 "clock-rate", G_TYPE_INT, src->clock_rate, NULL);
308 /* add address and port */
309 if (src->have_rtp_from) {
310 gst_net_address_to_string (&src->rtp_from, address_str,
311 sizeof (address_str));
312 gst_structure_set (s, "rtp-from", G_TYPE_STRING, address_str, NULL);
314 if (src->have_rtcp_from) {
315 gst_net_address_to_string (&src->rtcp_from, address_str,
316 sizeof (address_str));
317 gst_structure_set (s, "rtcp-from", G_TYPE_STRING, address_str, NULL);
320 gst_structure_set (s,
321 "octets-sent", G_TYPE_UINT64, src->stats.octets_sent,
322 "packets-sent", G_TYPE_UINT64, src->stats.packets_sent,
323 "octets-received", G_TYPE_UINT64, src->stats.octets_received,
324 "packets-received", G_TYPE_UINT64, src->stats.packets_received,
325 "bitrate", G_TYPE_UINT64, src->bitrate,
326 "packets-lost", G_TYPE_INT,
327 (gint) rtp_stats_get_packets_lost (&src->stats), "jitter", G_TYPE_UINT,
328 (guint) (src->stats.jitter >> 4), NULL);
330 /* get the last SR. */
331 have_sr = rtp_source_get_last_sr (src, &time, &ntptime, &rtptime,
332 &packet_count, &octet_count);
333 gst_structure_set (s,
334 "have-sr", G_TYPE_BOOLEAN, have_sr,
335 "sr-ntptime", G_TYPE_UINT64, ntptime,
336 "sr-rtptime", G_TYPE_UINT, (guint) rtptime,
337 "sr-octet-count", G_TYPE_UINT, (guint) octet_count,
338 "sr-packet-count", G_TYPE_UINT, (guint) packet_count, NULL);
341 /* get the last RB we sent */
342 gst_structure_set (s,
343 "sent-rb", G_TYPE_BOOLEAN, src->last_rr.is_valid,
344 "sent-rb-fractionlost", G_TYPE_UINT, (guint) src->last_rr.fractionlost,
345 "sent-rb-packetslost", G_TYPE_INT, (gint) src->last_rr.packetslost,
346 "sent-rb-exthighestseq", G_TYPE_UINT,
347 (guint) src->last_rr.exthighestseq, "sent-rb-jitter", G_TYPE_UINT,
348 (guint) src->last_rr.jitter, "sent-rb-lsr", G_TYPE_UINT,
349 (guint) src->last_rr.lsr, "sent-rb-dlsr", G_TYPE_UINT,
350 (guint) src->last_rr.dlsr, NULL);
352 /* get the last RB */
353 have_rb = rtp_source_get_last_rb (src, &fractionlost, &packetslost,
354 &exthighestseq, &jitter, &lsr, &dlsr, &round_trip);
356 gst_structure_set (s,
357 "have-rb", G_TYPE_BOOLEAN, have_rb,
358 "rb-fractionlost", G_TYPE_UINT, (guint) fractionlost,
359 "rb-packetslost", G_TYPE_INT, (gint) packetslost,
360 "rb-exthighestseq", G_TYPE_UINT, (guint) exthighestseq,
361 "rb-jitter", G_TYPE_UINT, (guint) jitter,
362 "rb-lsr", G_TYPE_UINT, (guint) lsr,
363 "rb-dlsr", G_TYPE_UINT, (guint) dlsr,
364 "rb-round-trip", G_TYPE_UINT, (guint) round_trip, NULL);
371 * rtp_source_get_sdes_struct:
372 * @src: an #RTPSource
374 * Get the SDES from @src. See the SDES property for more details.
376 * Returns: %GstStructure of type "application/x-rtp-source-sdes". The result is
377 * valid until the SDES items of @src are modified.
380 rtp_source_get_sdes_struct (RTPSource * src)
382 g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
388 sdes_struct_compare_func (GQuark field_id, const GValue * value,
394 old = GST_STRUCTURE (user_data);
395 field = g_quark_to_string (field_id);
397 if (!gst_structure_has_field (old, field))
400 g_assert (G_VALUE_HOLDS_STRING (value));
402 return strcmp (g_value_get_string (value), gst_structure_get_string (old,
407 * rtp_source_set_sdes:
408 * @src: an #RTPSource
409 * @sdes: the SDES structure
411 * Store the @sdes in @src. @sdes must be a structure of type
412 * "application/x-rtp-source-sdes", see the SDES property for more details.
414 * This function takes ownership of @sdes.
416 * Returns: %FALSE if the SDES was unchanged.
419 rtp_source_set_sdes_struct (RTPSource * src, GstStructure * sdes)
423 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
424 g_return_val_if_fail (strcmp (gst_structure_get_name (sdes),
425 "application/x-rtp-source-sdes") == 0, FALSE);
427 changed = !gst_structure_foreach (sdes, sdes_struct_compare_func, src->sdes);
430 gst_structure_free (src->sdes);
433 gst_structure_free (sdes);
440 rtp_source_set_property (GObject * object, guint prop_id,
441 const GValue * value, GParamSpec * pspec)
445 src = RTP_SOURCE (object);
449 src->ssrc = g_value_get_uint (value);
452 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
458 rtp_source_get_property (GObject * object, guint prop_id,
459 GValue * value, GParamSpec * pspec)
463 src = RTP_SOURCE (object);
467 g_value_set_uint (value, rtp_source_get_ssrc (src));
470 g_value_set_boolean (value, rtp_source_is_as_csrc (src));
472 case PROP_IS_VALIDATED:
473 g_value_set_boolean (value, rtp_source_is_validated (src));
476 g_value_set_boolean (value, rtp_source_is_sender (src));
479 g_value_set_boxed (value, rtp_source_get_sdes_struct (src));
482 g_value_take_boxed (value, rtp_source_create_stats (src));
485 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
494 * Create a #RTPSource with @ssrc.
496 * Returns: a new #RTPSource. Use g_object_unref() after usage.
499 rtp_source_new (guint32 ssrc)
503 src = g_object_new (RTP_TYPE_SOURCE, NULL);
510 * rtp_source_set_callbacks:
511 * @src: an #RTPSource
512 * @cb: callback functions
513 * @user_data: user data
515 * Set the callbacks for the source.
518 rtp_source_set_callbacks (RTPSource * src, RTPSourceCallbacks * cb,
521 g_return_if_fail (RTP_IS_SOURCE (src));
523 src->callbacks.push_rtp = cb->push_rtp;
524 src->callbacks.clock_rate = cb->clock_rate;
525 src->user_data = user_data;
529 * rtp_source_get_ssrc:
530 * @src: an #RTPSource
532 * Get the SSRC of @source.
534 * Returns: the SSRC of src.
537 rtp_source_get_ssrc (RTPSource * src)
541 g_return_val_if_fail (RTP_IS_SOURCE (src), 0);
549 * rtp_source_set_as_csrc:
550 * @src: an #RTPSource
552 * Configure @src as a CSRC, this will also validate @src.
555 rtp_source_set_as_csrc (RTPSource * src)
557 g_return_if_fail (RTP_IS_SOURCE (src));
559 src->validated = TRUE;
564 * rtp_source_is_as_csrc:
565 * @src: an #RTPSource
567 * Check if @src is a contributing source.
569 * Returns: %TRUE if @src is acting as a contributing source.
572 rtp_source_is_as_csrc (RTPSource * src)
576 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
578 result = src->is_csrc;
584 * rtp_source_is_active:
585 * @src: an #RTPSource
587 * Check if @src is an active source. A source is active if it has been
588 * validated and has not yet received a BYE packet
590 * Returns: %TRUE if @src is an qactive source.
593 rtp_source_is_active (RTPSource * src)
597 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
599 result = RTP_SOURCE_IS_ACTIVE (src);
605 * rtp_source_is_validated:
606 * @src: an #RTPSource
608 * Check if @src is a validated source.
610 * Returns: %TRUE if @src is a validated source.
613 rtp_source_is_validated (RTPSource * src)
617 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
619 result = src->validated;
625 * rtp_source_is_sender:
626 * @src: an #RTPSource
628 * Check if @src is a sending source.
630 * Returns: %TRUE if @src is a sending source.
633 rtp_source_is_sender (RTPSource * src)
637 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
639 result = RTP_SOURCE_IS_SENDER (src);
645 * rtp_source_received_bye:
646 * @src: an #RTPSource
648 * Check if @src has receoved a BYE packet.
650 * Returns: %TRUE if @src has received a BYE packet.
653 rtp_source_received_bye (RTPSource * src)
657 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
659 result = src->received_bye;
666 * rtp_source_get_bye_reason:
667 * @src: an #RTPSource
669 * Get the BYE reason for @src. Check if the source receoved a BYE message first
670 * with rtp_source_received_bye().
672 * Returns: The BYE reason or NULL when no reason was given or the source did
673 * not receive a BYE message yet. g_fee() after usage.
676 rtp_source_get_bye_reason (RTPSource * src)
680 g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
682 result = g_strdup (src->bye_reason);
688 * rtp_source_update_caps:
689 * @src: an #RTPSource
692 * Parse @caps and store all relevant information in @source.
695 rtp_source_update_caps (RTPSource * src, GstCaps * caps)
701 /* nothing changed, return */
702 if (caps == NULL || src->caps == caps)
705 s = gst_caps_get_structure (caps, 0);
707 if (gst_structure_get_int (s, "payload", &ival))
711 GST_DEBUG ("got payload %d", src->payload);
713 if (gst_structure_get_int (s, "clock-rate", &ival))
714 src->clock_rate = ival;
716 src->clock_rate = -1;
718 GST_DEBUG ("got clock-rate %d", src->clock_rate);
720 if (gst_structure_get_uint (s, "seqnum-base", &val))
721 src->seqnum_base = val;
723 src->seqnum_base = -1;
725 GST_DEBUG ("got seqnum-base %" G_GINT32_FORMAT, src->seqnum_base);
727 gst_caps_replace (&src->caps, caps);
731 * rtp_source_set_sdes_string:
732 * @src: an #RTPSource
733 * @type: the type of the SDES item
734 * @data: the SDES data
736 * Store an SDES item of @type in @src.
738 * Returns: %FALSE if the SDES item was unchanged or @type is unknown.
741 rtp_source_set_sdes_string (RTPSource * src, GstRTCPSDESType type,
747 field = gst_rtcp_sdes_type_to_name (type);
749 if (gst_structure_has_field (src->sdes, field))
750 old = gst_structure_get_string (src->sdes, field);
754 if (old == NULL && data == NULL)
757 if (old != NULL && data != NULL && strcmp (old, data) == 0)
761 gst_structure_remove_field (src->sdes, field);
763 gst_structure_set (src->sdes, field, G_TYPE_STRING, data, NULL);
769 * rtp_source_get_sdes_string:
770 * @src: an #RTPSource
771 * @type: the type of the SDES item
773 * Get the SDES item of @type from @src.
775 * Returns: a null-terminated copy of the SDES item or NULL when @type was not
776 * valid or the SDES item was unset. g_free() after usage.
779 rtp_source_get_sdes_string (RTPSource * src, GstRTCPSDESType type)
782 const gchar *type_name;
784 g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
786 if (type < 0 || type > GST_RTCP_SDES_PRIV - 1)
789 type_name = gst_rtcp_sdes_type_to_name (type);
791 if (!gst_structure_has_field (src->sdes, type_name))
794 result = g_strdup (gst_structure_get_string (src->sdes, type_name));
800 * rtp_source_set_rtp_from:
801 * @src: an #RTPSource
802 * @address: the RTP address to set
804 * Set that @src is receiving RTP packets from @address. This is used for
805 * collistion checking.
808 rtp_source_set_rtp_from (RTPSource * src, GstNetAddress * address)
810 g_return_if_fail (RTP_IS_SOURCE (src));
812 src->have_rtp_from = TRUE;
813 memcpy (&src->rtp_from, address, sizeof (GstNetAddress));
817 * rtp_source_set_rtcp_from:
818 * @src: an #RTPSource
819 * @address: the RTCP address to set
821 * Set that @src is receiving RTCP packets from @address. This is used for
822 * collistion checking.
825 rtp_source_set_rtcp_from (RTPSource * src, GstNetAddress * address)
827 g_return_if_fail (RTP_IS_SOURCE (src));
829 src->have_rtcp_from = TRUE;
830 memcpy (&src->rtcp_from, address, sizeof (GstNetAddress));
834 push_packet (RTPSource * src, GstBuffer * buffer)
836 GstFlowReturn ret = GST_FLOW_OK;
838 /* push queued packets first if any */
839 while (!g_queue_is_empty (src->packets)) {
840 GstBuffer *buffer = GST_BUFFER_CAST (g_queue_pop_head (src->packets));
842 GST_LOG ("pushing queued packet");
843 if (src->callbacks.push_rtp)
844 src->callbacks.push_rtp (src, buffer, src->user_data);
846 gst_buffer_unref (buffer);
848 GST_LOG ("pushing new packet");
850 if (src->callbacks.push_rtp)
851 ret = src->callbacks.push_rtp (src, buffer, src->user_data);
853 gst_buffer_unref (buffer);
859 get_clock_rate (RTPSource * src, guint8 payload)
861 if (src->payload == -1) {
862 /* first payload received, nothing was in the caps, lock on to this payload */
863 src->payload = payload;
864 GST_DEBUG ("first payload %d", payload);
865 } else if (payload != src->payload) {
866 /* we have a different payload than before, reset the clock-rate */
867 GST_DEBUG ("new payload %d", payload);
868 src->payload = payload;
869 src->clock_rate = -1;
870 src->stats.transit = -1;
873 if (src->clock_rate == -1) {
874 gint clock_rate = -1;
876 if (src->callbacks.clock_rate)
877 clock_rate = src->callbacks.clock_rate (src, payload, src->user_data);
879 GST_DEBUG ("got clock-rate %d", clock_rate);
881 src->clock_rate = clock_rate;
883 return src->clock_rate;
886 /* Jitter is the variation in the delay of received packets in a flow. It is
887 * measured by comparing the interval when RTP packets were sent to the interval
888 * at which they were received. For instance, if packet #1 and packet #2 leave
889 * 50 milliseconds apart and arrive 60 milliseconds apart, then the jitter is 10
892 calculate_jitter (RTPSource * src, GstBuffer * buffer,
893 RTPArrivalStats * arrival)
895 GstClockTime running_time;
896 guint32 rtparrival, transit, rtptime;
900 GstRTPBuffer rtp = { NULL };
902 /* get arrival time */
903 if ((running_time = arrival->running_time) == GST_CLOCK_TIME_NONE)
906 gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp);
907 pt = gst_rtp_buffer_get_payload_type (&rtp);
909 GST_LOG ("SSRC %08x got payload %d", src->ssrc, pt);
912 if ((clock_rate = get_clock_rate (src, pt)) == -1) {
913 gst_rtp_buffer_unmap (&rtp);
917 rtptime = gst_rtp_buffer_get_timestamp (&rtp);
919 /* convert arrival time to RTP timestamp units, truncate to 32 bits, we don't
920 * care about the absolute value, just the difference. */
921 rtparrival = gst_util_uint64_scale_int (running_time, clock_rate, GST_SECOND);
923 /* transit time is difference with RTP timestamp */
924 transit = rtparrival - rtptime;
926 /* get ABS diff with previous transit time */
927 if (src->stats.transit != -1) {
928 if (transit > src->stats.transit)
929 diff = transit - src->stats.transit;
931 diff = src->stats.transit - transit;
935 src->stats.transit = transit;
937 /* update jitter, the value we store is scaled up so we can keep precision. */
938 src->stats.jitter += diff - ((src->stats.jitter + 8) >> 4);
940 src->stats.prev_rtptime = src->stats.last_rtptime;
941 src->stats.last_rtptime = rtparrival;
943 GST_LOG ("rtparrival %u, rtptime %u, clock-rate %d, diff %d, jitter: %f",
944 rtparrival, rtptime, clock_rate, diff, (src->stats.jitter) / 16.0);
946 gst_rtp_buffer_unmap (&rtp);
952 GST_WARNING ("cannot get current running_time");
957 GST_WARNING ("cannot get clock-rate for pt %d", pt);
963 init_seq (RTPSource * src, guint16 seq)
965 src->stats.base_seq = seq;
966 src->stats.max_seq = seq;
967 src->stats.bad_seq = RTP_SEQ_MOD + 1; /* so seq == bad_seq is false */
968 src->stats.cycles = 0;
969 src->stats.packets_received = 0;
970 src->stats.octets_received = 0;
971 src->stats.bytes_received = 0;
972 src->stats.prev_received = 0;
973 src->stats.prev_expected = 0;
975 GST_DEBUG ("base_seq %d", seq);
978 #define BITRATE_INTERVAL (2 * GST_SECOND)
981 do_bitrate_estimation (RTPSource * src, GstClockTime running_time,
982 guint64 * bytes_handled)
986 if (src->prev_rtime) {
987 elapsed = running_time - src->prev_rtime;
989 if (elapsed > BITRATE_INTERVAL) {
992 rate = gst_util_uint64_scale (*bytes_handled, 8 * GST_SECOND, elapsed);
994 GST_LOG ("Elapsed %" G_GUINT64_FORMAT ", bytes %" G_GUINT64_FORMAT
995 ", rate %" G_GUINT64_FORMAT, elapsed, *bytes_handled, rate);
997 if (src->bitrate == 0)
1000 src->bitrate = ((src->bitrate * 3) + rate) / 4;
1002 src->prev_rtime = running_time;
1006 GST_LOG ("Reset bitrate measurement");
1007 src->prev_rtime = running_time;
1013 * rtp_source_process_rtp:
1014 * @src: an #RTPSource
1015 * @buffer: an RTP buffer
1017 * Let @src handle the incomming RTP @buffer.
1019 * Returns: a #GstFlowReturn.
1022 rtp_source_process_rtp (RTPSource * src, GstBuffer * buffer,
1023 RTPArrivalStats * arrival)
1025 GstFlowReturn result = GST_FLOW_OK;
1026 guint16 seqnr, udelta;
1027 RTPSourceStats *stats;
1029 GstRTPBuffer rtp = { NULL };
1031 g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
1032 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1034 stats = &src->stats;
1036 gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp);
1037 seqnr = gst_rtp_buffer_get_seq (&rtp);
1038 gst_rtp_buffer_unmap (&rtp);
1040 if (stats->cycles == -1) {
1041 GST_DEBUG ("received first buffer");
1042 /* first time we heard of this source */
1043 init_seq (src, seqnr);
1044 src->stats.max_seq = seqnr - 1;
1045 src->probation = RTP_DEFAULT_PROBATION;
1048 udelta = seqnr - stats->max_seq;
1050 /* if we are still on probation, check seqnum */
1051 if (src->probation) {
1052 expected = src->stats.max_seq + 1;
1054 /* when in probation, we require consecutive seqnums */
1055 if (seqnr == expected) {
1056 /* expected packet */
1057 GST_DEBUG ("probation: seqnr %d == expected %d", seqnr, expected);
1059 src->stats.max_seq = seqnr;
1060 if (src->probation == 0) {
1061 GST_DEBUG ("probation done!");
1062 init_seq (src, seqnr);
1066 GST_DEBUG ("probation %d: queue buffer", src->probation);
1067 /* when still in probation, keep packets in a list. */
1068 g_queue_push_tail (src->packets, buffer);
1069 /* remove packets from queue if there are too many */
1070 while (g_queue_get_length (src->packets) > RTP_MAX_PROBATION_LEN) {
1071 q = g_queue_pop_head (src->packets);
1072 gst_buffer_unref (q);
1077 /* unexpected seqnum in probation */
1078 goto probation_seqnum;
1080 } else if (udelta < RTP_MAX_DROPOUT) {
1081 /* in order, with permissible gap */
1082 if (seqnr < stats->max_seq) {
1083 /* sequence number wrapped - count another 64K cycle. */
1084 stats->cycles += RTP_SEQ_MOD;
1086 stats->max_seq = seqnr;
1087 } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
1088 /* the sequence number made a very large jump */
1089 if (seqnr == stats->bad_seq) {
1090 /* two sequential packets -- assume that the other side
1091 * restarted without telling us so just re-sync
1092 * (i.e., pretend this was the first packet). */
1093 init_seq (src, seqnr);
1095 /* unacceptable jump */
1096 stats->bad_seq = (seqnr + 1) & (RTP_SEQ_MOD - 1);
1100 /* duplicate or reordered packet, will be filtered by jitterbuffer. */
1101 GST_WARNING ("duplicate or reordered packet");
1104 src->stats.octets_received += arrival->payload_len;
1105 src->stats.bytes_received += arrival->bytes;
1106 src->stats.packets_received++;
1107 /* for the bitrate estimation */
1108 src->bytes_received += arrival->payload_len;
1109 /* the source that sent the packet must be a sender */
1110 src->is_sender = TRUE;
1111 src->validated = TRUE;
1113 do_bitrate_estimation (src, arrival->running_time, &src->bytes_received);
1115 GST_LOG ("seq %d, PC: %" G_GUINT64_FORMAT ", OC: %" G_GUINT64_FORMAT,
1116 seqnr, src->stats.packets_received, src->stats.octets_received);
1118 /* calculate jitter for the stats */
1119 calculate_jitter (src, buffer, arrival);
1121 /* we're ready to push the RTP packet now */
1122 result = push_packet (src, buffer);
1130 GST_WARNING ("unacceptable seqnum received");
1131 gst_buffer_unref (buffer);
1136 GST_WARNING ("probation: seqnr %d != expected %d", seqnr, expected);
1137 src->probation = RTP_DEFAULT_PROBATION;
1138 src->stats.max_seq = seqnr;
1139 gst_buffer_unref (buffer);
1145 * rtp_source_process_bye:
1146 * @src: an #RTPSource
1147 * @reason: the reason for leaving
1149 * Notify @src that a BYE packet has been received. This will make the source
1153 rtp_source_process_bye (RTPSource * src, const gchar * reason)
1155 g_return_if_fail (RTP_IS_SOURCE (src));
1157 GST_DEBUG ("marking SSRC %08x as BYE, reason: %s", src->ssrc,
1158 GST_STR_NULL (reason));
1160 /* copy the reason and mark as received_bye */
1161 g_free (src->bye_reason);
1162 src->bye_reason = g_strdup (reason);
1163 src->received_bye = TRUE;
1167 set_ssrc (GstBuffer ** buffer, guint idx, RTPSource * src)
1169 GstRTPBuffer rtp = { NULL };
1171 *buffer = gst_buffer_make_writable (*buffer);
1172 gst_rtp_buffer_map (*buffer, GST_MAP_WRITE, &rtp);
1173 gst_rtp_buffer_set_ssrc (&rtp, src->ssrc);
1174 gst_rtp_buffer_unmap (&rtp);
1179 * rtp_source_send_rtp:
1180 * @src: an #RTPSource
1181 * @data: an RTP buffer or a list of RTP buffers
1182 * @is_list: if @data is a buffer or list
1183 * @running_time: the running time of @data
1185 * Send @data (an RTP buffer or list of buffers) originating from @src.
1186 * This will make @src a sender. This function takes ownership of @data and
1187 * modifies the SSRC in the RTP packet to that of @src when needed.
1189 * Returns: a #GstFlowReturn.
1192 rtp_source_send_rtp (RTPSource * src, gpointer data, gboolean is_list,
1193 GstClockTime running_time)
1195 GstFlowReturn result;
1198 guint64 ext_rtptime;
1199 guint64 rt_diff, rtp_diff;
1200 GstBufferList *list = NULL;
1201 GstBuffer *buffer = NULL;
1204 GstRTPBuffer rtp = { NULL };
1206 g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
1207 g_return_val_if_fail (is_list || GST_IS_BUFFER (data), GST_FLOW_ERROR);
1210 list = GST_BUFFER_LIST_CAST (data);
1212 /* We can grab the caps from the first group, since all
1213 * groups of a buffer list have same caps. */
1214 buffer = gst_buffer_list_get (list, 0);
1218 buffer = GST_BUFFER_CAST (data);
1221 /* we are a sender now */
1222 src->is_sender = TRUE;
1227 /* Each group makes up a network packet. */
1228 packets = gst_buffer_list_length (list);
1229 for (i = 0, len = 0; i < packets; i++) {
1230 gst_rtp_buffer_map (gst_buffer_list_get (list, i), GST_MAP_READ, &rtp);
1231 len += gst_rtp_buffer_get_payload_len (&rtp);
1232 gst_rtp_buffer_unmap (&rtp);
1234 /* subsequent info taken from first list member */
1235 gst_rtp_buffer_map (gst_buffer_list_get (list, 0), GST_MAP_READ, &rtp);
1238 gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp);
1239 len = gst_rtp_buffer_get_payload_len (&rtp);
1242 /* update stats for the SR */
1243 src->stats.packets_sent += packets;
1244 src->stats.octets_sent += len;
1245 src->bytes_sent += len;
1247 do_bitrate_estimation (src, running_time, &src->bytes_sent);
1249 rtptime = gst_rtp_buffer_get_timestamp (&rtp);
1250 ext_rtptime = src->last_rtptime;
1251 ext_rtptime = gst_rtp_buffer_ext_timestamp (&ext_rtptime, rtptime);
1253 GST_LOG ("SSRC %08x, RTP %" G_GUINT64_FORMAT ", running_time %"
1254 GST_TIME_FORMAT, src->ssrc, ext_rtptime, GST_TIME_ARGS (running_time));
1256 if (ext_rtptime > src->last_rtptime) {
1257 rtp_diff = ext_rtptime - src->last_rtptime;
1258 rt_diff = running_time - src->last_rtime;
1260 /* calc the diff so we can detect drift at the sender. This can also be used
1261 * to guestimate the clock rate if the NTP time is locked to the RTP
1262 * timestamps (as is the case when the capture device is providing the clock). */
1263 GST_LOG ("SSRC %08x, diff RTP %" G_GUINT64_FORMAT ", diff running_time %"
1264 GST_TIME_FORMAT, src->ssrc, rtp_diff, GST_TIME_ARGS (rt_diff));
1267 /* we keep track of the last received RTP timestamp and the corresponding
1268 * buffer running_time so that we can use this info when constructing SR reports */
1269 src->last_rtime = running_time;
1270 src->last_rtptime = ext_rtptime;
1273 if (!src->callbacks.push_rtp) {
1274 gst_rtp_buffer_unmap (&rtp);
1278 ssrc = gst_rtp_buffer_get_ssrc (&rtp);
1279 gst_rtp_buffer_unmap (&rtp);
1281 if (ssrc != src->ssrc) {
1282 /* the SSRC of the packet is not correct, make a writable buffer and
1283 * update the SSRC. This could involve a complete copy of the packet when
1284 * it is not writable. Usually the payloader will use caps negotiation to
1285 * get the correct SSRC from the session manager before pushing anything. */
1287 /* FIXME, we don't want to warn yet because we can't inform any payloader
1288 * of the changes SSRC yet because we don't implement pad-alloc. */
1289 GST_LOG ("updating SSRC from %08x to %08x, fix the payloader", ssrc,
1293 list = gst_buffer_list_make_writable (list);
1294 gst_buffer_list_foreach (list, (GstBufferListFunc) set_ssrc, src);
1296 set_ssrc (&buffer, 0, src);
1299 GST_LOG ("pushing RTP %s %" G_GUINT64_FORMAT, is_list ? "list" : "packet",
1300 src->stats.packets_sent);
1302 result = src->callbacks.push_rtp (src, data, src->user_data);
1309 GST_WARNING ("no buffers in buffer list");
1310 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1315 GST_WARNING ("no callback installed, dropping packet");
1316 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1322 * rtp_source_process_sr:
1323 * @src: an #RTPSource
1324 * @time: time of packet arrival
1325 * @ntptime: the NTP time in 32.32 fixed point
1326 * @rtptime: the RTP time
1327 * @packet_count: the packet count
1328 * @octet_count: the octect count
1330 * Update the sender report in @src.
1333 rtp_source_process_sr (RTPSource * src, GstClockTime time, guint64 ntptime,
1334 guint32 rtptime, guint32 packet_count, guint32 octet_count)
1336 RTPSenderReport *curr;
1339 g_return_if_fail (RTP_IS_SOURCE (src));
1341 GST_DEBUG ("got SR packet: SSRC %08x, NTP %08x:%08x, RTP %" G_GUINT32_FORMAT
1342 ", PC %" G_GUINT32_FORMAT ", OC %" G_GUINT32_FORMAT, src->ssrc,
1343 (guint32) (ntptime >> 32), (guint32) (ntptime & 0xffffffff), rtptime,
1344 packet_count, octet_count);
1346 curridx = src->stats.curr_sr ^ 1;
1347 curr = &src->stats.sr[curridx];
1349 /* this is a sender now */
1350 src->is_sender = TRUE;
1352 /* update current */
1353 curr->is_valid = TRUE;
1354 curr->ntptime = ntptime;
1355 curr->rtptime = rtptime;
1356 curr->packet_count = packet_count;
1357 curr->octet_count = octet_count;
1361 src->stats.curr_sr = curridx;
1363 src->stats.prev_rtcptime = src->stats.last_rtcptime;
1364 src->stats.last_rtcptime = time;
1368 * rtp_source_process_rb:
1369 * @src: an #RTPSource
1370 * @ntpnstime: the current time in nanoseconds since 1970
1371 * @fractionlost: fraction lost since last SR/RR
1372 * @packetslost: the cumululative number of packets lost
1373 * @exthighestseq: the extended last sequence number received
1374 * @jitter: the interarrival jitter
1375 * @lsr: the last SR packet from this source
1376 * @dlsr: the delay since last SR packet
1378 * Update the report block in @src.
1381 rtp_source_process_rb (RTPSource * src, guint64 ntpnstime,
1382 guint8 fractionlost, gint32 packetslost, guint32 exthighestseq,
1383 guint32 jitter, guint32 lsr, guint32 dlsr)
1385 RTPReceiverReport *curr;
1390 g_return_if_fail (RTP_IS_SOURCE (src));
1392 GST_DEBUG ("got RB packet: SSRC %08x, FL %2x, PL %d, HS %" G_GUINT32_FORMAT
1393 ", jitter %" G_GUINT32_FORMAT ", LSR %04x:%04x, DLSR %04x:%04x",
1394 src->ssrc, fractionlost, packetslost, exthighestseq, jitter, lsr >> 16,
1395 lsr & 0xffff, dlsr >> 16, dlsr & 0xffff);
1397 curridx = src->stats.curr_rr ^ 1;
1398 curr = &src->stats.rr[curridx];
1400 /* update current */
1401 curr->is_valid = TRUE;
1402 curr->fractionlost = fractionlost;
1403 curr->packetslost = packetslost;
1404 curr->exthighestseq = exthighestseq;
1405 curr->jitter = jitter;
1409 /* convert the NTP time in nanoseconds to 32.32 fixed point */
1410 f_ntp = gst_util_uint64_scale (ntpnstime, (1LL << 32), GST_SECOND);
1411 /* calculate round trip, round the time up */
1412 ntp = ((f_ntp + 0xffff) >> 16) & 0xffffffff;
1415 if (A > 0 && ntp > A)
1419 curr->round_trip = A;
1421 GST_DEBUG ("NTP %04x:%04x, round trip %04x:%04x", ntp >> 16, ntp & 0xffff,
1422 A >> 16, A & 0xffff);
1425 src->stats.curr_rr = curridx;
1429 * rtp_source_get_new_sr:
1430 * @src: an #RTPSource
1431 * @ntpnstime: the current time in nanoseconds since 1970
1432 * @running_time: the current running_time of the pipeline.
1433 * @ntptime: the NTP time in 32.32 fixed point
1434 * @rtptime: the RTP time corresponding to @ntptime
1435 * @packet_count: the packet count
1436 * @octet_count: the octect count
1438 * Get new values to put into a new SR report from this source.
1440 * @running_time and @ntpnstime are captured at the same time and represent the
1441 * running time of the pipeline clock and the absolute current system time in
1442 * nanoseconds respectively. Together with the last running_time and rtp timestamp
1443 * we have observed in the source, we can generate @ntptime and @rtptime for an SR
1444 * packet. @ntptime is basically the fixed point representation of @ntpnstime
1445 * and @rtptime the associated RTP timestamp.
1447 * Returns: %TRUE on success.
1450 rtp_source_get_new_sr (RTPSource * src, guint64 ntpnstime,
1451 GstClockTime running_time, guint64 * ntptime, guint32 * rtptime,
1452 guint32 * packet_count, guint32 * octet_count)
1455 guint64 t_current_ntp;
1456 GstClockTimeDiff diff;
1458 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1460 /* We last saw a buffer with last_rtptime at last_rtime. Given a running_time
1461 * and an NTP time, we can scale the RTP timestamps so that they match the
1462 * given NTP time. for scaling, we assume that the slope of the rtptime vs
1463 * running_time vs ntptime curve is close to 1, which is certainly
1464 * sufficient for the frequency at which we report SR and the rate we send
1465 * out RTP packets. */
1466 t_rtp = src->last_rtptime;
1468 GST_DEBUG ("last_rtime %" GST_TIME_FORMAT ", last_rtptime %"
1469 G_GUINT64_FORMAT, GST_TIME_ARGS (src->last_rtime), t_rtp);
1471 if (src->clock_rate != -1) {
1472 /* get the diff between the clock running_time and the buffer running_time.
1473 * This is the elapsed time, as measured against the pipeline clock, between
1474 * when the rtp timestamp was observed and the current running_time.
1476 * We need to apply this diff to the RTP timestamp to get the RTP timestamp
1477 * for the given ntpnstime. */
1478 diff = GST_CLOCK_DIFF (src->last_rtime, running_time);
1480 /* now translate the diff to RTP time, handle positive and negative cases.
1481 * If there is no diff, we already set rtptime correctly above. */
1483 GST_DEBUG ("running_time %" GST_TIME_FORMAT ", diff %" GST_TIME_FORMAT,
1484 GST_TIME_ARGS (running_time), GST_TIME_ARGS (diff));
1485 t_rtp += gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1488 GST_DEBUG ("running_time %" GST_TIME_FORMAT ", diff -%" GST_TIME_FORMAT,
1489 GST_TIME_ARGS (running_time), GST_TIME_ARGS (diff));
1490 t_rtp -= gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1493 GST_WARNING ("no clock-rate, cannot interpolate rtp time");
1496 /* convert the NTP time in nanoseconds to 32.32 fixed point */
1497 t_current_ntp = gst_util_uint64_scale (ntpnstime, (1LL << 32), GST_SECOND);
1499 GST_DEBUG ("NTP %08x:%08x, RTP %" G_GUINT32_FORMAT,
1500 (guint32) (t_current_ntp >> 32), (guint32) (t_current_ntp & 0xffffffff),
1504 *ntptime = t_current_ntp;
1508 *packet_count = src->stats.packets_sent;
1510 *octet_count = src->stats.octets_sent;
1516 * rtp_source_get_new_rb:
1517 * @src: an #RTPSource
1518 * @time: the current time of the system clock
1519 * @fractionlost: fraction lost since last SR/RR
1520 * @packetslost: the cumululative number of packets lost
1521 * @exthighestseq: the extended last sequence number received
1522 * @jitter: the interarrival jitter
1523 * @lsr: the last SR packet from this source
1524 * @dlsr: the delay since last SR packet
1526 * Get new values to put into a new report block from this source.
1528 * Returns: %TRUE on success.
1531 rtp_source_get_new_rb (RTPSource * src, GstClockTime time,
1532 guint8 * fractionlost, gint32 * packetslost, guint32 * exthighestseq,
1533 guint32 * jitter, guint32 * lsr, guint32 * dlsr)
1535 RTPSourceStats *stats;
1536 guint64 extended_max, expected;
1537 guint64 expected_interval, received_interval, ntptime;
1538 gint64 lost, lost_interval;
1539 guint32 fraction, LSR, DLSR;
1540 GstClockTime sr_time;
1542 stats = &src->stats;
1544 extended_max = stats->cycles + stats->max_seq;
1545 expected = extended_max - stats->base_seq + 1;
1547 GST_DEBUG ("ext_max %" G_GUINT64_FORMAT ", expected %" G_GUINT64_FORMAT
1548 ", received %" G_GUINT64_FORMAT ", base_seq %" G_GUINT32_FORMAT,
1549 extended_max, expected, stats->packets_received, stats->base_seq);
1551 lost = expected - stats->packets_received;
1552 lost = CLAMP (lost, -0x800000, 0x7fffff);
1554 expected_interval = expected - stats->prev_expected;
1555 stats->prev_expected = expected;
1556 received_interval = stats->packets_received - stats->prev_received;
1557 stats->prev_received = stats->packets_received;
1559 lost_interval = expected_interval - received_interval;
1561 if (expected_interval == 0 || lost_interval <= 0)
1564 fraction = (lost_interval << 8) / expected_interval;
1566 GST_DEBUG ("add RR for SSRC %08x", src->ssrc);
1567 /* we scaled the jitter up for additional precision */
1568 GST_DEBUG ("fraction %" G_GUINT32_FORMAT ", lost %" G_GINT64_FORMAT
1569 ", extseq %" G_GUINT64_FORMAT ", jitter %d", fraction, lost,
1570 extended_max, stats->jitter >> 4);
1572 if (rtp_source_get_last_sr (src, &sr_time, &ntptime, NULL, NULL, NULL)) {
1575 /* LSR is middle 32 bits of the last ntptime */
1576 LSR = (ntptime >> 16) & 0xffffffff;
1577 diff = time - sr_time;
1578 GST_DEBUG ("last SR time diff %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
1579 /* DLSR, delay since last SR is expressed in 1/65536 second units */
1580 DLSR = gst_util_uint64_scale_int (diff, 65536, GST_SECOND);
1582 /* No valid SR received, LSR/DLSR are set to 0 then */
1583 GST_DEBUG ("no valid SR received");
1587 GST_DEBUG ("LSR %04x:%04x, DLSR %04x:%04x", LSR >> 16, LSR & 0xffff,
1588 DLSR >> 16, DLSR & 0xffff);
1591 *fractionlost = fraction;
1593 *packetslost = lost;
1595 *exthighestseq = extended_max;
1597 *jitter = stats->jitter >> 4;
1607 * rtp_source_get_last_sr:
1608 * @src: an #RTPSource
1609 * @time: time of packet arrival
1610 * @ntptime: the NTP time in 32.32 fixed point
1611 * @rtptime: the RTP time
1612 * @packet_count: the packet count
1613 * @octet_count: the octect count
1615 * Get the values of the last sender report as set with rtp_source_process_sr().
1617 * Returns: %TRUE if there was a valid SR report.
1620 rtp_source_get_last_sr (RTPSource * src, GstClockTime * time, guint64 * ntptime,
1621 guint32 * rtptime, guint32 * packet_count, guint32 * octet_count)
1623 RTPSenderReport *curr;
1625 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1627 curr = &src->stats.sr[src->stats.curr_sr];
1628 if (!curr->is_valid)
1632 *ntptime = curr->ntptime;
1634 *rtptime = curr->rtptime;
1636 *packet_count = curr->packet_count;
1638 *octet_count = curr->octet_count;
1646 * rtp_source_get_last_rb:
1647 * @src: an #RTPSource
1648 * @fractionlost: fraction lost since last SR/RR
1649 * @packetslost: the cumululative number of packets lost
1650 * @exthighestseq: the extended last sequence number received
1651 * @jitter: the interarrival jitter
1652 * @lsr: the last SR packet from this source
1653 * @dlsr: the delay since last SR packet
1654 * @round_trip: the round trip time
1656 * Get the values of the last RB report set with rtp_source_process_rb().
1658 * Returns: %TRUE if there was a valid SB report.
1661 rtp_source_get_last_rb (RTPSource * src, guint8 * fractionlost,
1662 gint32 * packetslost, guint32 * exthighestseq, guint32 * jitter,
1663 guint32 * lsr, guint32 * dlsr, guint32 * round_trip)
1665 RTPReceiverReport *curr;
1667 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1669 curr = &src->stats.rr[src->stats.curr_rr];
1670 if (!curr->is_valid)
1674 *fractionlost = curr->fractionlost;
1676 *packetslost = curr->packetslost;
1678 *exthighestseq = curr->exthighestseq;
1680 *jitter = curr->jitter;
1686 *round_trip = curr->round_trip;
1692 * rtp_source_find_conflicting_address:
1693 * @src: The source the packet came in
1694 * @address: address to check for
1695 * @time: The time when the packet that is possibly in conflict arrived
1697 * Checks if an address which has a conflict is already known. If it is
1698 * a known conflict, remember the time
1700 * Returns: TRUE if it was a known conflict, FALSE otherwise
1703 rtp_source_find_conflicting_address (RTPSource * src, GstNetAddress * address,
1708 for (item = g_list_first (src->conflicting_addresses);
1709 item; item = g_list_next (item)) {
1710 RTPConflictingAddress *known_conflict = item->data;
1712 if (gst_net_address_equal (address, &known_conflict->address)) {
1713 known_conflict->time = time;
1722 * rtp_source_add_conflicting_address:
1723 * @src: The source the packet came in
1724 * @address: address to remember
1725 * @time: The time when the packet that is in conflict arrived
1727 * Adds a new conflict address
1730 rtp_source_add_conflicting_address (RTPSource * src,
1731 GstNetAddress * address, GstClockTime time)
1733 RTPConflictingAddress *new_conflict;
1735 new_conflict = g_new0 (RTPConflictingAddress, 1);
1737 memcpy (&new_conflict->address, address, sizeof (GstNetAddress));
1738 new_conflict->time = time;
1740 src->conflicting_addresses = g_list_prepend (src->conflicting_addresses,
1745 * rtp_source_timeout:
1746 * @src: The #RTPSource
1747 * @current_time: The current time
1748 * @collision_timeout: The amount of time after which a collision is timed out
1749 * @feedback_retention_window: The running time before which retained feedback
1750 * packets have to be discarded
1752 * This is processed on each RTCP interval. It times out old collisions.
1753 * It also times out old retained feedback packets
1756 rtp_source_timeout (RTPSource * src, GstClockTime current_time,
1757 GstClockTime collision_timeout, GstClockTime feedback_retention_window)
1762 item = g_list_first (src->conflicting_addresses);
1764 RTPConflictingAddress *known_conflict = item->data;
1765 GList *next_item = g_list_next (item);
1767 if (known_conflict->time < current_time - collision_timeout) {
1770 src->conflicting_addresses =
1771 g_list_delete_link (src->conflicting_addresses, item);
1772 gst_net_address_to_string (&known_conflict->address, buf, 40);
1773 GST_DEBUG ("collision %p timed out: %s", known_conflict, buf);
1774 g_free (known_conflict);
1779 /* Time out AVPF packets that are older than the desired length */
1780 while ((pkt = g_queue_peek_tail (src->retained_feedback)) &&
1781 GST_BUFFER_TIMESTAMP (pkt) < feedback_retention_window)
1782 gst_buffer_unref (g_queue_pop_tail (src->retained_feedback));
1786 compare_buffers (gconstpointer a, gconstpointer b, gpointer user_data)
1788 const GstBuffer *bufa = a;
1789 const GstBuffer *bufb = b;
1791 return GST_BUFFER_TIMESTAMP (bufa) - GST_BUFFER_TIMESTAMP (bufb);
1795 rtp_source_retain_rtcp_packet (RTPSource * src, GstRTCPPacket * packet,
1796 GstClockTime running_time)
1800 buffer = gst_buffer_copy_region (packet->rtcp->buffer, GST_BUFFER_COPY_MEMORY,
1801 packet->offset, (gst_rtcp_packet_get_length (packet) + 1) * 4);
1803 GST_BUFFER_TIMESTAMP (buffer) = running_time;
1805 g_queue_insert_sorted (src->retained_feedback, buffer, compare_buffers, NULL);
1809 rtp_source_has_retained (RTPSource * src, GCompareFunc func, gconstpointer data)
1811 if (g_queue_find_custom (src->retained_feedback, data, func))