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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, 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
42 #define DEFAULT_PROBATION RTP_DEFAULT_PROBATION
57 /* GObject vmethods */
58 static void rtp_source_finalize (GObject * object);
59 static void rtp_source_set_property (GObject * object, guint prop_id,
60 const GValue * value, GParamSpec * pspec);
61 static void rtp_source_get_property (GObject * object, guint prop_id,
62 GValue * value, GParamSpec * pspec);
64 /* static guint rtp_source_signals[LAST_SIGNAL] = { 0 }; */
66 G_DEFINE_TYPE (RTPSource, rtp_source, G_TYPE_OBJECT);
69 rtp_source_class_init (RTPSourceClass * klass)
71 GObjectClass *gobject_class;
73 gobject_class = (GObjectClass *) klass;
75 gobject_class->finalize = rtp_source_finalize;
77 gobject_class->set_property = rtp_source_set_property;
78 gobject_class->get_property = rtp_source_get_property;
80 g_object_class_install_property (gobject_class, PROP_SSRC,
81 g_param_spec_uint ("ssrc", "SSRC",
82 "The SSRC of this source", 0, G_MAXUINT, DEFAULT_SSRC,
83 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
85 g_object_class_install_property (gobject_class, PROP_IS_CSRC,
86 g_param_spec_boolean ("is-csrc", "Is CSRC",
87 "If this SSRC is acting as a contributing source",
88 DEFAULT_IS_CSRC, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
90 g_object_class_install_property (gobject_class, PROP_IS_VALIDATED,
91 g_param_spec_boolean ("is-validated", "Is Validated",
92 "If this SSRC is validated", DEFAULT_IS_VALIDATED,
93 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
95 g_object_class_install_property (gobject_class, PROP_IS_SENDER,
96 g_param_spec_boolean ("is-sender", "Is Sender",
97 "If this SSRC is a sender", DEFAULT_IS_SENDER,
98 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
103 * The current SDES items of the source. Returns a structure with name
104 * application/x-rtp-source-sdes and may contain the following fields:
106 * 'cname' G_TYPE_STRING : The canonical name
107 * 'name' G_TYPE_STRING : The user name
108 * 'email' G_TYPE_STRING : The user's electronic mail address
109 * 'phone' G_TYPE_STRING : The user's phone number
110 * 'location' G_TYPE_STRING : The geographic user location
111 * 'tool' G_TYPE_STRING : The name of application or tool
112 * 'note' G_TYPE_STRING : A notice about the source
114 * other fields may be present and these represent private items in
115 * the SDES where the field name is the prefix.
117 g_object_class_install_property (gobject_class, PROP_SDES,
118 g_param_spec_boxed ("sdes", "SDES",
119 "The SDES information for this source",
120 GST_TYPE_STRUCTURE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
125 * The statistics of the source. This property returns a GstStructure with
126 * name application/x-rtp-source-stats with the following fields:
128 * "ssrc" G_TYPE_UINT The SSRC of this source
129 * "internal" G_TYPE_BOOLEAN If this source is a source of the session
130 * "validated" G_TYPE_BOOLEAN If the source is validated
131 * "received-bye" G_TYPE_BOOLEAN If we received a BYE from this source
132 * "is-csrc" G_TYPE_BOOLEAN If this source was found as CSRC
133 * "is-sender" G_TYPE_BOOLEAN If this source is a sender
134 * "seqnum-base" G_TYPE_INT first seqnum if known
135 * "clock-rate" G_TYPE_INT the clock rate of the media
137 * The following two fields are only present when known.
139 * "rtp-from" G_TYPE_STRING where we received the last RTP packet from
140 * "rtcp-from" G_TYPE_STRING where we received the last RTCP packet from
142 * The following fields make sense for internal sources and will only increase
143 * when "is-sender" is TRUE:
145 * "octets-sent" G_TYPE_UINT64 number of bytes we sent
146 * "packets-sent" G_TYPE_UINT64 number of packets we sent
148 * The following fields make sense for non-internal sources and will only
149 * increase when "is-sender" is TRUE.
151 * "octets-received" G_TYPE_UINT64 total number of bytes received
152 * "packets-received" G_TYPE_UINT64 total number of packets received
154 * Following fields are updated when "is-sender" is TRUE.
156 * "bitrate" G_TYPE_UINT64 bitrate in bits per second
157 * "jitter" G_TYPE_UINT estimated jitter
158 * "packets-lost" G_TYPE_INT estimated amount of packets lost
160 * The last SR report this source sent. This only updates when "is-sender" is
163 * "have-sr" G_TYPE_BOOLEAN the source has sent SR
164 * "sr-ntptime" G_TYPE_UINT64 ntptime of SR
165 * "sr-rtptime" G_TYPE_UINT rtptime of SR
166 * "sr-octet-count" G_TYPE_UINT the number of bytes in the SR
167 * "sr-packet-count" G_TYPE_UINT the number of packets in the SR
169 * The following fields are only present for non-internal sources and
170 * represent the content of the last RB packet that was sent to this source.
171 * These values are only updated when the source is sending.
173 * "sent-rb" G_TYPE_BOOLEAN we have sent an RB
174 * "sent-rb-fractionlost" G_TYPE_UINT calculated lost fraction
175 * "sent-rb-packetslost" G_TYPE_INT lost packets
176 * "sent-rb-exthighestseq" G_TYPE_UINT last seen seqnum
177 * "sent-rb-jitter" G_TYPE_UINT jitter
178 * "sent-rb-lsr" G_TYPE_UINT last SR time
179 * "sent-rb-dlsr" G_TYPE_UINT delay since last SR
181 * The following fields are only present for non-internal sources and
182 * represents the last RB that this source sent. This is only updated
183 * when the source is receiving data and sending RB blocks.
185 * "have-rb" G_TYPE_BOOLEAN the source has sent RB
186 * "rb-fractionlost" G_TYPE_UINT lost fraction
187 * "rb-packetslost" G_TYPE_INT lost packets
188 * "rb-exthighestseq" G_TYPE_UINT highest received seqnum
189 * "rb-jitter" G_TYPE_UINT reception jitter
190 * "rb-lsr" G_TYPE_UINT last SR time
191 * "rb-dlsr" G_TYPE_UINT delay since last SR
193 * The round trip of this source. This is calculated from the last RB
194 * values and the recption time of the last RB packet. Only present for
195 * non-internal sources.
197 * "rb-round-trip" G_TYPE_UINT the round trip time in nanoseconds
199 g_object_class_install_property (gobject_class, PROP_STATS,
200 g_param_spec_boxed ("stats", "Stats",
201 "The stats of this source", GST_TYPE_STRUCTURE,
202 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
204 g_object_class_install_property (gobject_class, PROP_PROBATION,
205 g_param_spec_uint ("probation", "Number of probations",
206 "Consecutive packet sequence numbers to accept the source",
207 0, G_MAXUINT, DEFAULT_PROBATION,
208 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
210 GST_DEBUG_CATEGORY_INIT (rtp_source_debug, "rtpsource", 0, "RTP Source");
215 * @src: an #RTPSource
217 * Reset the stats of @src.
220 rtp_source_reset (RTPSource * src)
222 src->marked_bye = FALSE;
224 g_free (src->bye_reason);
225 src->bye_reason = NULL;
226 src->sent_bye = FALSE;
228 src->stats.cycles = -1;
229 src->stats.jitter = 0;
230 src->stats.transit = -1;
231 src->stats.curr_sr = 0;
232 src->stats.sr[0].is_valid = FALSE;
233 src->stats.curr_rr = 0;
234 src->stats.rr[0].is_valid = FALSE;
235 src->stats.prev_rtptime = GST_CLOCK_TIME_NONE;
236 src->stats.prev_rtcptime = GST_CLOCK_TIME_NONE;
237 src->stats.last_rtptime = GST_CLOCK_TIME_NONE;
238 src->stats.last_rtcptime = GST_CLOCK_TIME_NONE;
239 g_array_set_size (src->nacks, 0);
243 rtp_source_init (RTPSource * src)
245 /* sources are initialy on probation until we receive enough valid RTP
246 * packets or a valid RTCP packet */
247 src->validated = FALSE;
248 src->internal = FALSE;
249 src->probation = DEFAULT_PROBATION;
250 src->curr_probation = src->probation;
251 src->closing = FALSE;
253 src->sdes = gst_structure_new_empty ("application/x-rtp-source-sdes");
256 src->clock_rate = -1;
257 src->packets = g_queue_new ();
258 src->seqnum_base = -1;
259 src->last_rtptime = -1;
261 src->retained_feedback = g_queue_new ();
262 src->nacks = g_array_new (FALSE, FALSE, sizeof (guint32));
264 rtp_source_reset (src);
268 rtp_conflicting_address_free (RTPConflictingAddress * addr)
270 g_object_unref (addr->address);
275 rtp_source_finalize (GObject * object)
280 src = RTP_SOURCE_CAST (object);
282 while ((buffer = g_queue_pop_head (src->packets)))
283 gst_buffer_unref (buffer);
284 g_queue_free (src->packets);
286 gst_structure_free (src->sdes);
288 g_free (src->bye_reason);
290 gst_caps_replace (&src->caps, NULL);
292 g_list_foreach (src->conflicting_addresses,
293 (GFunc) rtp_conflicting_address_free, NULL);
294 g_list_free (src->conflicting_addresses);
296 while ((buffer = g_queue_pop_head (src->retained_feedback)))
297 gst_buffer_unref (buffer);
298 g_queue_free (src->retained_feedback);
300 g_array_free (src->nacks, TRUE);
303 g_object_unref (src->rtp_from);
305 g_object_unref (src->rtcp_from);
307 G_OBJECT_CLASS (rtp_source_parent_class)->finalize (object);
310 static GstStructure *
311 rtp_source_create_stats (RTPSource * src)
314 gboolean is_sender = src->is_sender;
315 gboolean internal = src->internal;
318 guint8 fractionlost = 0;
319 gint32 packetslost = 0;
320 guint32 exthighestseq = 0;
324 guint32 round_trip = 0;
326 GstClockTime time = 0;
329 guint32 packet_count = 0;
330 guint32 octet_count = 0;
333 /* common data for all types of sources */
334 s = gst_structure_new ("application/x-rtp-source-stats",
335 "ssrc", G_TYPE_UINT, (guint) src->ssrc,
336 "internal", G_TYPE_BOOLEAN, internal,
337 "validated", G_TYPE_BOOLEAN, src->validated,
338 "received-bye", G_TYPE_BOOLEAN, src->marked_bye,
339 "is-csrc", G_TYPE_BOOLEAN, src->is_csrc,
340 "is-sender", G_TYPE_BOOLEAN, is_sender,
341 "seqnum-base", G_TYPE_INT, src->seqnum_base,
342 "clock-rate", G_TYPE_INT, src->clock_rate, NULL);
344 /* add address and port */
346 address_str = __g_socket_address_to_string (src->rtp_from);
347 gst_structure_set (s, "rtp-from", G_TYPE_STRING, address_str, NULL);
348 g_free (address_str);
350 if (src->rtcp_from) {
351 address_str = __g_socket_address_to_string (src->rtcp_from);
352 gst_structure_set (s, "rtcp-from", G_TYPE_STRING, address_str, NULL);
353 g_free (address_str);
356 gst_structure_set (s,
357 "octets-sent", G_TYPE_UINT64, src->stats.octets_sent,
358 "packets-sent", G_TYPE_UINT64, src->stats.packets_sent,
359 "octets-received", G_TYPE_UINT64, src->stats.octets_received,
360 "packets-received", G_TYPE_UINT64, src->stats.packets_received,
361 "bitrate", G_TYPE_UINT64, src->bitrate,
362 "packets-lost", G_TYPE_INT,
363 (gint) rtp_stats_get_packets_lost (&src->stats), "jitter", G_TYPE_UINT,
364 (guint) (src->stats.jitter >> 4), NULL);
366 /* get the last SR. */
367 have_sr = rtp_source_get_last_sr (src, &time, &ntptime, &rtptime,
368 &packet_count, &octet_count);
369 gst_structure_set (s,
370 "have-sr", G_TYPE_BOOLEAN, have_sr,
371 "sr-ntptime", G_TYPE_UINT64, ntptime,
372 "sr-rtptime", G_TYPE_UINT, (guint) rtptime,
373 "sr-octet-count", G_TYPE_UINT, (guint) octet_count,
374 "sr-packet-count", G_TYPE_UINT, (guint) packet_count, NULL);
377 /* get the last RB we sent */
378 gst_structure_set (s,
379 "sent-rb", G_TYPE_BOOLEAN, src->last_rr.is_valid,
380 "sent-rb-fractionlost", G_TYPE_UINT, (guint) src->last_rr.fractionlost,
381 "sent-rb-packetslost", G_TYPE_INT, (gint) src->last_rr.packetslost,
382 "sent-rb-exthighestseq", G_TYPE_UINT,
383 (guint) src->last_rr.exthighestseq, "sent-rb-jitter", G_TYPE_UINT,
384 (guint) src->last_rr.jitter, "sent-rb-lsr", G_TYPE_UINT,
385 (guint) src->last_rr.lsr, "sent-rb-dlsr", G_TYPE_UINT,
386 (guint) src->last_rr.dlsr, NULL);
388 /* get the last RB */
389 have_rb = rtp_source_get_last_rb (src, &fractionlost, &packetslost,
390 &exthighestseq, &jitter, &lsr, &dlsr, &round_trip);
392 gst_structure_set (s,
393 "have-rb", G_TYPE_BOOLEAN, have_rb,
394 "rb-fractionlost", G_TYPE_UINT, (guint) fractionlost,
395 "rb-packetslost", G_TYPE_INT, (gint) packetslost,
396 "rb-exthighestseq", G_TYPE_UINT, (guint) exthighestseq,
397 "rb-jitter", G_TYPE_UINT, (guint) jitter,
398 "rb-lsr", G_TYPE_UINT, (guint) lsr,
399 "rb-dlsr", G_TYPE_UINT, (guint) dlsr,
400 "rb-round-trip", G_TYPE_UINT, (guint) round_trip, NULL);
407 * rtp_source_get_sdes_struct:
408 * @src: an #RTPSource
410 * Get the SDES from @src. See the SDES property for more details.
412 * Returns: %GstStructure of type "application/x-rtp-source-sdes". The result is
413 * valid until the SDES items of @src are modified.
416 rtp_source_get_sdes_struct (RTPSource * src)
418 g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
424 sdes_struct_compare_func (GQuark field_id, const GValue * value,
430 old = GST_STRUCTURE (user_data);
431 field = g_quark_to_string (field_id);
433 if (!gst_structure_has_field (old, field))
436 g_assert (G_VALUE_HOLDS_STRING (value));
438 return strcmp (g_value_get_string (value), gst_structure_get_string (old,
443 * rtp_source_set_sdes_struct:
444 * @src: an #RTPSource
445 * @sdes: the SDES structure
447 * Store the @sdes in @src. @sdes must be a structure of type
448 * "application/x-rtp-source-sdes", see the SDES property for more details.
450 * This function takes ownership of @sdes.
452 * Returns: %FALSE if the SDES was unchanged.
455 rtp_source_set_sdes_struct (RTPSource * src, GstStructure * sdes)
459 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
460 g_return_val_if_fail (strcmp (gst_structure_get_name (sdes),
461 "application/x-rtp-source-sdes") == 0, FALSE);
463 changed = !gst_structure_foreach (sdes, sdes_struct_compare_func, src->sdes);
466 gst_structure_free (src->sdes);
469 gst_structure_free (sdes);
475 rtp_source_set_property (GObject * object, guint prop_id,
476 const GValue * value, GParamSpec * pspec)
480 src = RTP_SOURCE (object);
484 src->ssrc = g_value_get_uint (value);
487 src->probation = g_value_get_uint (value);
490 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
496 rtp_source_get_property (GObject * object, guint prop_id,
497 GValue * value, GParamSpec * pspec)
501 src = RTP_SOURCE (object);
505 g_value_set_uint (value, rtp_source_get_ssrc (src));
508 g_value_set_boolean (value, rtp_source_is_as_csrc (src));
510 case PROP_IS_VALIDATED:
511 g_value_set_boolean (value, rtp_source_is_validated (src));
514 g_value_set_boolean (value, rtp_source_is_sender (src));
517 g_value_set_boxed (value, rtp_source_get_sdes_struct (src));
520 g_value_take_boxed (value, rtp_source_create_stats (src));
523 g_value_set_uint (value, src->probation);
526 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
535 * Create a #RTPSource with @ssrc.
537 * Returns: a new #RTPSource. Use g_object_unref() after usage.
540 rtp_source_new (guint32 ssrc)
544 src = g_object_new (RTP_TYPE_SOURCE, NULL);
551 * rtp_source_set_callbacks:
552 * @src: an #RTPSource
553 * @cb: callback functions
554 * @user_data: user data
556 * Set the callbacks for the source.
559 rtp_source_set_callbacks (RTPSource * src, RTPSourceCallbacks * cb,
562 g_return_if_fail (RTP_IS_SOURCE (src));
564 src->callbacks.push_rtp = cb->push_rtp;
565 src->callbacks.clock_rate = cb->clock_rate;
566 src->user_data = user_data;
570 * rtp_source_get_ssrc:
571 * @src: an #RTPSource
573 * Get the SSRC of @source.
575 * Returns: the SSRC of src.
578 rtp_source_get_ssrc (RTPSource * src)
582 g_return_val_if_fail (RTP_IS_SOURCE (src), 0);
590 * rtp_source_set_as_csrc:
591 * @src: an #RTPSource
593 * Configure @src as a CSRC, this will also validate @src.
596 rtp_source_set_as_csrc (RTPSource * src)
598 g_return_if_fail (RTP_IS_SOURCE (src));
600 src->validated = TRUE;
605 * rtp_source_is_as_csrc:
606 * @src: an #RTPSource
608 * Check if @src is a contributing source.
610 * Returns: %TRUE if @src is acting as a contributing source.
613 rtp_source_is_as_csrc (RTPSource * src)
617 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
619 result = src->is_csrc;
625 * rtp_source_is_active:
626 * @src: an #RTPSource
628 * Check if @src is an active source. A source is active if it has been
629 * validated and has not yet received a BYE packet
631 * Returns: %TRUE if @src is an qactive source.
634 rtp_source_is_active (RTPSource * src)
638 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
640 result = RTP_SOURCE_IS_ACTIVE (src);
646 * rtp_source_is_validated:
647 * @src: an #RTPSource
649 * Check if @src is a validated source.
651 * Returns: %TRUE if @src is a validated source.
654 rtp_source_is_validated (RTPSource * src)
658 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
660 result = src->validated;
666 * rtp_source_is_sender:
667 * @src: an #RTPSource
669 * Check if @src is a sending source.
671 * Returns: %TRUE if @src is a sending source.
674 rtp_source_is_sender (RTPSource * src)
678 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
680 result = RTP_SOURCE_IS_SENDER (src);
686 * rtp_source_is_marked_bye:
687 * @src: an #RTPSource
689 * Check if @src is marked as leaving the session with a BYE packet.
691 * Returns: %TRUE if @src has been marked BYE.
694 rtp_source_is_marked_bye (RTPSource * src)
698 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
700 result = RTP_SOURCE_IS_MARKED_BYE (src);
707 * rtp_source_get_bye_reason:
708 * @src: an #RTPSource
710 * Get the BYE reason for @src. Check if the source is marked as leaving the
711 * session with a BYE message first with rtp_source_is_marked_bye().
713 * Returns: The BYE reason or NULL when no reason was given or the source was
714 * not marked BYE yet. g_free() after usage.
717 rtp_source_get_bye_reason (RTPSource * src)
721 g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
723 result = g_strdup (src->bye_reason);
729 * rtp_source_update_caps:
730 * @src: an #RTPSource
733 * Parse @caps and store all relevant information in @source.
736 rtp_source_update_caps (RTPSource * src, GstCaps * caps)
742 /* nothing changed, return */
743 if (caps == NULL || src->caps == caps)
746 s = gst_caps_get_structure (caps, 0);
748 if (gst_structure_get_int (s, "payload", &ival))
752 GST_DEBUG ("got payload %d", src->payload);
754 if (gst_structure_get_int (s, "clock-rate", &ival))
755 src->clock_rate = ival;
757 src->clock_rate = -1;
759 GST_DEBUG ("got clock-rate %d", src->clock_rate);
761 if (gst_structure_get_uint (s, "seqnum-base", &val))
762 src->seqnum_base = val;
764 src->seqnum_base = -1;
766 GST_DEBUG ("got seqnum-base %" G_GINT32_FORMAT, src->seqnum_base);
768 gst_caps_replace (&src->caps, caps);
772 * rtp_source_set_rtp_from:
773 * @src: an #RTPSource
774 * @address: the RTP address to set
776 * Set that @src is receiving RTP packets from @address. This is used for
777 * collistion checking.
780 rtp_source_set_rtp_from (RTPSource * src, GSocketAddress * address)
782 g_return_if_fail (RTP_IS_SOURCE (src));
785 g_object_unref (src->rtp_from);
786 src->rtp_from = G_SOCKET_ADDRESS (g_object_ref (address));
790 * rtp_source_set_rtcp_from:
791 * @src: an #RTPSource
792 * @address: the RTCP address to set
794 * Set that @src is receiving RTCP packets from @address. This is used for
795 * collistion checking.
798 rtp_source_set_rtcp_from (RTPSource * src, GSocketAddress * address)
800 g_return_if_fail (RTP_IS_SOURCE (src));
803 g_object_unref (src->rtcp_from);
804 src->rtcp_from = G_SOCKET_ADDRESS (g_object_ref (address));
808 push_packet (RTPSource * src, GstBuffer * buffer)
810 GstFlowReturn ret = GST_FLOW_OK;
812 /* push queued packets first if any */
813 while (!g_queue_is_empty (src->packets)) {
814 GstBuffer *buffer = GST_BUFFER_CAST (g_queue_pop_head (src->packets));
816 GST_LOG ("pushing queued packet");
817 if (src->callbacks.push_rtp)
818 src->callbacks.push_rtp (src, buffer, src->user_data);
820 gst_buffer_unref (buffer);
822 GST_LOG ("pushing new packet");
824 if (src->callbacks.push_rtp)
825 ret = src->callbacks.push_rtp (src, buffer, src->user_data);
827 gst_buffer_unref (buffer);
833 get_clock_rate (RTPSource * src, guint8 payload)
835 if (src->payload == -1) {
836 /* first payload received, nothing was in the caps, lock on to this payload */
837 src->payload = payload;
838 GST_DEBUG ("first payload %d", payload);
839 } else if (payload != src->payload) {
840 /* we have a different payload than before, reset the clock-rate */
841 GST_DEBUG ("new payload %d", payload);
842 src->payload = payload;
843 src->clock_rate = -1;
844 src->stats.transit = -1;
847 if (src->clock_rate == -1) {
848 gint clock_rate = -1;
850 if (src->callbacks.clock_rate)
851 clock_rate = src->callbacks.clock_rate (src, payload, src->user_data);
853 GST_DEBUG ("got clock-rate %d", clock_rate);
855 src->clock_rate = clock_rate;
857 return src->clock_rate;
860 /* Jitter is the variation in the delay of received packets in a flow. It is
861 * measured by comparing the interval when RTP packets were sent to the interval
862 * at which they were received. For instance, if packet #1 and packet #2 leave
863 * 50 milliseconds apart and arrive 60 milliseconds apart, then the jitter is 10
866 calculate_jitter (RTPSource * src, GstBuffer * buffer, RTPPacketInfo * pinfo)
868 GstClockTime running_time;
869 guint32 rtparrival, transit, rtptime;
873 GstRTPBuffer rtp = { NULL };
875 /* get arrival time */
876 if ((running_time = pinfo->running_time) == GST_CLOCK_TIME_NONE)
879 if (!gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp))
882 pt = gst_rtp_buffer_get_payload_type (&rtp);
884 GST_LOG ("SSRC %08x got payload %d", src->ssrc, pt);
887 if ((clock_rate = get_clock_rate (src, pt)) == -1) {
888 gst_rtp_buffer_unmap (&rtp);
892 rtptime = gst_rtp_buffer_get_timestamp (&rtp);
894 /* convert arrival time to RTP timestamp units, truncate to 32 bits, we don't
895 * care about the absolute value, just the difference. */
896 rtparrival = gst_util_uint64_scale_int (running_time, clock_rate, GST_SECOND);
898 /* transit time is difference with RTP timestamp */
899 transit = rtparrival - rtptime;
901 /* get ABS diff with previous transit time */
902 if (src->stats.transit != -1) {
903 if (transit > src->stats.transit)
904 diff = transit - src->stats.transit;
906 diff = src->stats.transit - transit;
910 src->stats.transit = transit;
912 /* update jitter, the value we store is scaled up so we can keep precision. */
913 src->stats.jitter += diff - ((src->stats.jitter + 8) >> 4);
915 src->stats.prev_rtptime = src->stats.last_rtptime;
916 src->stats.last_rtptime = rtparrival;
918 GST_LOG ("rtparrival %u, rtptime %u, clock-rate %d, diff %d, jitter: %f",
919 rtparrival, rtptime, clock_rate, diff, (src->stats.jitter) / 16.0);
921 gst_rtp_buffer_unmap (&rtp);
927 GST_WARNING ("cannot get current running_time");
932 GST_WARNING ("invalid RTP packet");
937 GST_WARNING ("cannot get clock-rate for pt %d", pt);
943 init_seq (RTPSource * src, guint16 seq)
945 src->stats.base_seq = seq;
946 src->stats.max_seq = seq;
947 src->stats.bad_seq = RTP_SEQ_MOD + 1; /* so seq == bad_seq is false */
948 src->stats.cycles = 0;
949 src->stats.packets_received = 0;
950 src->stats.octets_received = 0;
951 src->stats.bytes_received = 0;
952 src->stats.prev_received = 0;
953 src->stats.prev_expected = 0;
955 GST_DEBUG ("base_seq %d", seq);
958 #define BITRATE_INTERVAL (2 * GST_SECOND)
961 do_bitrate_estimation (RTPSource * src, GstClockTime running_time,
962 guint64 * bytes_handled)
966 if (src->prev_rtime) {
967 elapsed = running_time - src->prev_rtime;
969 if (elapsed > BITRATE_INTERVAL) {
972 rate = gst_util_uint64_scale (*bytes_handled, 8 * GST_SECOND, elapsed);
974 GST_LOG ("Elapsed %" G_GUINT64_FORMAT ", bytes %" G_GUINT64_FORMAT
975 ", rate %" G_GUINT64_FORMAT, elapsed, *bytes_handled, rate);
977 if (src->bitrate == 0)
980 src->bitrate = ((src->bitrate * 3) + rate) / 4;
982 src->prev_rtime = running_time;
986 GST_LOG ("Reset bitrate measurement");
987 src->prev_rtime = running_time;
993 * rtp_source_process_rtp:
994 * @src: an #RTPSource
995 * @buffer: an RTP buffer
997 * Let @src handle the incomming RTP @buffer.
999 * Returns: a #GstFlowReturn.
1002 rtp_source_process_rtp (RTPSource * src, GstBuffer * buffer,
1003 RTPPacketInfo * pinfo)
1005 GstFlowReturn result = GST_FLOW_OK;
1006 guint16 seqnr, udelta;
1007 RTPSourceStats *stats;
1009 GstRTPBuffer rtp = { NULL };
1011 g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
1012 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1014 stats = &src->stats;
1016 if (!gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp))
1017 goto invalid_packet;
1019 seqnr = gst_rtp_buffer_get_seq (&rtp);
1020 gst_rtp_buffer_unmap (&rtp);
1022 if (stats->cycles == -1) {
1023 GST_DEBUG ("received first buffer");
1024 /* first time we heard of this source */
1025 init_seq (src, seqnr);
1026 src->stats.max_seq = seqnr - 1;
1027 src->curr_probation = src->probation;
1030 udelta = seqnr - stats->max_seq;
1032 /* if we are still on probation, check seqnum */
1033 if (src->curr_probation) {
1034 expected = src->stats.max_seq + 1;
1036 /* when in probation, we require consecutive seqnums */
1037 if (seqnr == expected) {
1038 /* expected packet */
1039 GST_DEBUG ("probation: seqnr %d == expected %d", seqnr, expected);
1040 src->curr_probation--;
1041 src->stats.max_seq = seqnr;
1042 if (src->curr_probation == 0) {
1043 GST_DEBUG ("probation done!");
1044 init_seq (src, seqnr);
1048 GST_DEBUG ("probation %d: queue buffer", src->curr_probation);
1049 /* when still in probation, keep packets in a list. */
1050 g_queue_push_tail (src->packets, buffer);
1051 /* remove packets from queue if there are too many */
1052 while (g_queue_get_length (src->packets) > RTP_MAX_PROBATION_LEN) {
1053 q = g_queue_pop_head (src->packets);
1054 gst_buffer_unref (q);
1059 /* unexpected seqnum in probation */
1060 goto probation_seqnum;
1062 } else if (udelta < RTP_MAX_DROPOUT) {
1063 /* in order, with permissible gap */
1064 if (seqnr < stats->max_seq) {
1065 /* sequence number wrapped - count another 64K cycle. */
1066 stats->cycles += RTP_SEQ_MOD;
1068 stats->max_seq = seqnr;
1069 } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
1070 /* the sequence number made a very large jump */
1071 if (seqnr == stats->bad_seq) {
1072 /* two sequential packets -- assume that the other side
1073 * restarted without telling us so just re-sync
1074 * (i.e., pretend this was the first packet). */
1075 init_seq (src, seqnr);
1077 /* unacceptable jump */
1078 stats->bad_seq = (seqnr + 1) & (RTP_SEQ_MOD - 1);
1082 /* duplicate or reordered packet, will be filtered by jitterbuffer. */
1083 GST_WARNING ("duplicate or reordered packet (seqnr %d)", seqnr);
1086 src->stats.octets_received += pinfo->payload_len;
1087 src->stats.bytes_received += pinfo->bytes;
1088 src->stats.packets_received++;
1089 /* for the bitrate estimation */
1090 src->bytes_received += pinfo->payload_len;
1091 /* the source that sent the packet must be a sender */
1092 src->is_sender = TRUE;
1093 src->validated = TRUE;
1095 do_bitrate_estimation (src, pinfo->running_time, &src->bytes_received);
1097 GST_LOG ("seq %d, PC: %" G_GUINT64_FORMAT ", OC: %" G_GUINT64_FORMAT,
1098 seqnr, src->stats.packets_received, src->stats.octets_received);
1100 /* calculate jitter for the stats */
1101 calculate_jitter (src, buffer, pinfo);
1103 /* we're ready to push the RTP packet now */
1104 result = push_packet (src, buffer);
1112 GST_WARNING ("invalid packet received");
1113 gst_buffer_unref (buffer);
1118 GST_WARNING ("unacceptable seqnum received");
1119 gst_buffer_unref (buffer);
1124 GST_WARNING ("probation: seqnr %d != expected %d", seqnr, expected);
1125 src->curr_probation = src->probation;
1126 src->stats.max_seq = seqnr;
1127 gst_buffer_unref (buffer);
1133 * rtp_source_mark_bye:
1134 * @src: an #RTPSource
1135 * @reason: the reason for leaving
1137 * Mark @src in the BYE state. This can happen when the source wants to
1138 * leave the sesssion or when a BYE packets has been received.
1140 * This will make the source inactive.
1143 rtp_source_mark_bye (RTPSource * src, const gchar * reason)
1145 g_return_if_fail (RTP_IS_SOURCE (src));
1147 GST_DEBUG ("marking SSRC %08x as BYE, reason: %s", src->ssrc,
1148 GST_STR_NULL (reason));
1150 /* copy the reason and mark as bye */
1151 g_free (src->bye_reason);
1152 src->bye_reason = g_strdup (reason);
1153 src->marked_bye = TRUE;
1157 * rtp_source_send_rtp:
1158 * @src: an #RTPSource
1159 * @data: an RTP buffer or a list of RTP buffers
1160 * @is_list: if @data is a buffer or list
1161 * @running_time: the running time of @data
1163 * Send @data (an RTP buffer or list of buffers) originating from @src.
1164 * This will make @src a sender. This function takes ownership of @data and
1165 * modifies the SSRC in the RTP packet to that of @src when needed.
1167 * Returns: a #GstFlowReturn.
1170 rtp_source_send_rtp (RTPSource * src, gpointer data, gboolean is_list,
1171 GstClockTime running_time)
1173 GstFlowReturn result;
1176 guint64 ext_rtptime;
1177 guint64 rt_diff, rtp_diff;
1179 GstRTPBuffer rtp = { NULL };
1181 g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
1182 g_return_val_if_fail (is_list || GST_IS_BUFFER (data), GST_FLOW_ERROR);
1184 /* we are a sender now */
1185 src->is_sender = TRUE;
1188 GstBufferList *list = GST_BUFFER_LIST_CAST (data);
1191 /* Each group makes up a network packet. */
1192 packets = gst_buffer_list_length (list);
1196 for (i = 0, len = 0; i < packets; i++) {
1197 if (!gst_rtp_buffer_map (gst_buffer_list_get (list, i), GST_MAP_READ,
1199 goto invalid_packet;
1201 len += gst_rtp_buffer_get_payload_len (&rtp);
1202 gst_rtp_buffer_unmap (&rtp);
1204 /* subsequent info taken from first list member */
1205 gst_rtp_buffer_map (gst_buffer_list_get (list, 0), GST_MAP_READ, &rtp);
1207 GstBuffer *buffer = GST_BUFFER_CAST (data);
1209 if (!gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp))
1210 goto invalid_packet;
1212 len = gst_rtp_buffer_get_payload_len (&rtp);
1215 /* update stats for the SR */
1216 src->stats.packets_sent += packets;
1217 src->stats.octets_sent += len;
1218 src->bytes_sent += len;
1220 do_bitrate_estimation (src, running_time, &src->bytes_sent);
1222 rtptime = gst_rtp_buffer_get_timestamp (&rtp);
1223 gst_rtp_buffer_unmap (&rtp);
1225 ext_rtptime = src->last_rtptime;
1226 ext_rtptime = gst_rtp_buffer_ext_timestamp (&ext_rtptime, rtptime);
1228 GST_LOG ("SSRC %08x, RTP %" G_GUINT64_FORMAT ", running_time %"
1229 GST_TIME_FORMAT, src->ssrc, ext_rtptime, GST_TIME_ARGS (running_time));
1231 if (ext_rtptime > src->last_rtptime) {
1232 rtp_diff = ext_rtptime - src->last_rtptime;
1233 rt_diff = running_time - src->last_rtime;
1235 /* calc the diff so we can detect drift at the sender. This can also be used
1236 * to guestimate the clock rate if the NTP time is locked to the RTP
1237 * timestamps (as is the case when the capture device is providing the clock). */
1238 GST_LOG ("SSRC %08x, diff RTP %" G_GUINT64_FORMAT ", diff running_time %"
1239 GST_TIME_FORMAT, src->ssrc, rtp_diff, GST_TIME_ARGS (rt_diff));
1242 /* we keep track of the last received RTP timestamp and the corresponding
1243 * buffer running_time so that we can use this info when constructing SR reports */
1244 src->last_rtime = running_time;
1245 src->last_rtptime = ext_rtptime;
1248 if (!src->callbacks.push_rtp)
1251 GST_LOG ("pushing RTP %s %" G_GUINT64_FORMAT, is_list ? "list" : "packet",
1252 src->stats.packets_sent);
1254 result = src->callbacks.push_rtp (src, data, src->user_data);
1261 GST_WARNING ("invalid packet received");
1262 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1267 GST_WARNING ("no buffers in buffer list");
1268 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1273 GST_WARNING ("no callback installed, dropping packet");
1274 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1280 * rtp_source_process_sr:
1281 * @src: an #RTPSource
1282 * @time: time of packet arrival
1283 * @ntptime: the NTP time in 32.32 fixed point
1284 * @rtptime: the RTP time
1285 * @packet_count: the packet count
1286 * @octet_count: the octect count
1288 * Update the sender report in @src.
1291 rtp_source_process_sr (RTPSource * src, GstClockTime time, guint64 ntptime,
1292 guint32 rtptime, guint32 packet_count, guint32 octet_count)
1294 RTPSenderReport *curr;
1297 g_return_if_fail (RTP_IS_SOURCE (src));
1299 GST_DEBUG ("got SR packet: SSRC %08x, NTP %08x:%08x, RTP %" G_GUINT32_FORMAT
1300 ", PC %" G_GUINT32_FORMAT ", OC %" G_GUINT32_FORMAT, src->ssrc,
1301 (guint32) (ntptime >> 32), (guint32) (ntptime & 0xffffffff), rtptime,
1302 packet_count, octet_count);
1304 curridx = src->stats.curr_sr ^ 1;
1305 curr = &src->stats.sr[curridx];
1307 /* this is a sender now */
1308 src->is_sender = TRUE;
1310 /* update current */
1311 curr->is_valid = TRUE;
1312 curr->ntptime = ntptime;
1313 curr->rtptime = rtptime;
1314 curr->packet_count = packet_count;
1315 curr->octet_count = octet_count;
1319 src->stats.curr_sr = curridx;
1321 src->stats.prev_rtcptime = src->stats.last_rtcptime;
1322 src->stats.last_rtcptime = time;
1326 * rtp_source_process_rb:
1327 * @src: an #RTPSource
1328 * @ntpnstime: the current time in nanoseconds since 1970
1329 * @fractionlost: fraction lost since last SR/RR
1330 * @packetslost: the cumululative number of packets lost
1331 * @exthighestseq: the extended last sequence number received
1332 * @jitter: the interarrival jitter
1333 * @lsr: the last SR packet from this source
1334 * @dlsr: the delay since last SR packet
1336 * Update the report block in @src.
1339 rtp_source_process_rb (RTPSource * src, guint64 ntpnstime,
1340 guint8 fractionlost, gint32 packetslost, guint32 exthighestseq,
1341 guint32 jitter, guint32 lsr, guint32 dlsr)
1343 RTPReceiverReport *curr;
1348 g_return_if_fail (RTP_IS_SOURCE (src));
1350 GST_DEBUG ("got RB packet: SSRC %08x, FL %2x, PL %d, HS %" G_GUINT32_FORMAT
1351 ", jitter %" G_GUINT32_FORMAT ", LSR %04x:%04x, DLSR %04x:%04x",
1352 src->ssrc, fractionlost, packetslost, exthighestseq, jitter, lsr >> 16,
1353 lsr & 0xffff, dlsr >> 16, dlsr & 0xffff);
1355 curridx = src->stats.curr_rr ^ 1;
1356 curr = &src->stats.rr[curridx];
1358 /* update current */
1359 curr->is_valid = TRUE;
1360 curr->fractionlost = fractionlost;
1361 curr->packetslost = packetslost;
1362 curr->exthighestseq = exthighestseq;
1363 curr->jitter = jitter;
1367 /* convert the NTP time in nanoseconds to 32.32 fixed point */
1368 f_ntp = gst_util_uint64_scale (ntpnstime, (1LL << 32), GST_SECOND);
1369 /* calculate round trip, round the time up */
1370 ntp = ((f_ntp + 0xffff) >> 16) & 0xffffffff;
1373 if (A > 0 && ntp > A)
1377 curr->round_trip = A;
1379 GST_DEBUG ("NTP %04x:%04x, round trip %04x:%04x", ntp >> 16, ntp & 0xffff,
1380 A >> 16, A & 0xffff);
1383 src->stats.curr_rr = curridx;
1387 * rtp_source_get_new_sr:
1388 * @src: an #RTPSource
1389 * @ntpnstime: the current time in nanoseconds since 1970
1390 * @running_time: the current running_time of the pipeline.
1391 * @ntptime: the NTP time in 32.32 fixed point
1392 * @rtptime: the RTP time corresponding to @ntptime
1393 * @packet_count: the packet count
1394 * @octet_count: the octect count
1396 * Get new values to put into a new SR report from this source.
1398 * @running_time and @ntpnstime are captured at the same time and represent the
1399 * running time of the pipeline clock and the absolute current system time in
1400 * nanoseconds respectively. Together with the last running_time and rtp timestamp
1401 * we have observed in the source, we can generate @ntptime and @rtptime for an SR
1402 * packet. @ntptime is basically the fixed point representation of @ntpnstime
1403 * and @rtptime the associated RTP timestamp.
1405 * Returns: %TRUE on success.
1408 rtp_source_get_new_sr (RTPSource * src, guint64 ntpnstime,
1409 GstClockTime running_time, guint64 * ntptime, guint32 * rtptime,
1410 guint32 * packet_count, guint32 * octet_count)
1413 guint64 t_current_ntp;
1414 GstClockTimeDiff diff;
1416 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1418 /* We last saw a buffer with last_rtptime at last_rtime. Given a running_time
1419 * and an NTP time, we can scale the RTP timestamps so that they match the
1420 * given NTP time. for scaling, we assume that the slope of the rtptime vs
1421 * running_time vs ntptime curve is close to 1, which is certainly
1422 * sufficient for the frequency at which we report SR and the rate we send
1423 * out RTP packets. */
1424 t_rtp = src->last_rtptime;
1426 GST_DEBUG ("last_rtime %" GST_TIME_FORMAT ", last_rtptime %"
1427 G_GUINT64_FORMAT, GST_TIME_ARGS (src->last_rtime), t_rtp);
1429 if (src->clock_rate != -1) {
1430 /* get the diff between the clock running_time and the buffer running_time.
1431 * This is the elapsed time, as measured against the pipeline clock, between
1432 * when the rtp timestamp was observed and the current running_time.
1434 * We need to apply this diff to the RTP timestamp to get the RTP timestamp
1435 * for the given ntpnstime. */
1436 diff = GST_CLOCK_DIFF (src->last_rtime, running_time);
1438 /* now translate the diff to RTP time, handle positive and negative cases.
1439 * If there is no diff, we already set rtptime correctly above. */
1441 GST_DEBUG ("running_time %" GST_TIME_FORMAT ", diff %" GST_TIME_FORMAT,
1442 GST_TIME_ARGS (running_time), GST_TIME_ARGS (diff));
1443 t_rtp += gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1446 GST_DEBUG ("running_time %" GST_TIME_FORMAT ", diff -%" GST_TIME_FORMAT,
1447 GST_TIME_ARGS (running_time), GST_TIME_ARGS (diff));
1448 t_rtp -= gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1451 GST_WARNING ("no clock-rate, cannot interpolate rtp time");
1454 /* convert the NTP time in nanoseconds to 32.32 fixed point */
1455 t_current_ntp = gst_util_uint64_scale (ntpnstime, (1LL << 32), GST_SECOND);
1457 GST_DEBUG ("NTP %08x:%08x, RTP %" G_GUINT32_FORMAT,
1458 (guint32) (t_current_ntp >> 32), (guint32) (t_current_ntp & 0xffffffff),
1462 *ntptime = t_current_ntp;
1466 *packet_count = src->stats.packets_sent;
1468 *octet_count = src->stats.octets_sent;
1474 * rtp_source_get_new_rb:
1475 * @src: an #RTPSource
1476 * @time: the current time of the system clock
1477 * @fractionlost: fraction lost since last SR/RR
1478 * @packetslost: the cumululative number of packets lost
1479 * @exthighestseq: the extended last sequence number received
1480 * @jitter: the interarrival jitter
1481 * @lsr: the last SR packet from this source
1482 * @dlsr: the delay since last SR packet
1484 * Get new values to put into a new report block from this source.
1486 * Returns: %TRUE on success.
1489 rtp_source_get_new_rb (RTPSource * src, GstClockTime time,
1490 guint8 * fractionlost, gint32 * packetslost, guint32 * exthighestseq,
1491 guint32 * jitter, guint32 * lsr, guint32 * dlsr)
1493 RTPSourceStats *stats;
1494 guint64 extended_max, expected;
1495 guint64 expected_interval, received_interval, ntptime;
1496 gint64 lost, lost_interval;
1497 guint32 fraction, LSR, DLSR;
1498 GstClockTime sr_time;
1500 stats = &src->stats;
1502 extended_max = stats->cycles + stats->max_seq;
1503 expected = extended_max - stats->base_seq + 1;
1505 GST_DEBUG ("ext_max %" G_GUINT64_FORMAT ", expected %" G_GUINT64_FORMAT
1506 ", received %" G_GUINT64_FORMAT ", base_seq %" G_GUINT32_FORMAT,
1507 extended_max, expected, stats->packets_received, stats->base_seq);
1509 lost = expected - stats->packets_received;
1510 lost = CLAMP (lost, -0x800000, 0x7fffff);
1512 expected_interval = expected - stats->prev_expected;
1513 stats->prev_expected = expected;
1514 received_interval = stats->packets_received - stats->prev_received;
1515 stats->prev_received = stats->packets_received;
1517 lost_interval = expected_interval - received_interval;
1519 if (expected_interval == 0 || lost_interval <= 0)
1522 fraction = (lost_interval << 8) / expected_interval;
1524 GST_DEBUG ("add RR for SSRC %08x", src->ssrc);
1525 /* we scaled the jitter up for additional precision */
1526 GST_DEBUG ("fraction %" G_GUINT32_FORMAT ", lost %" G_GINT64_FORMAT
1527 ", extseq %" G_GUINT64_FORMAT ", jitter %d", fraction, lost,
1528 extended_max, stats->jitter >> 4);
1530 if (rtp_source_get_last_sr (src, &sr_time, &ntptime, NULL, NULL, NULL)) {
1533 /* LSR is middle 32 bits of the last ntptime */
1534 LSR = (ntptime >> 16) & 0xffffffff;
1535 diff = time - sr_time;
1536 GST_DEBUG ("last SR time diff %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
1537 /* DLSR, delay since last SR is expressed in 1/65536 second units */
1538 DLSR = gst_util_uint64_scale_int (diff, 65536, GST_SECOND);
1540 /* No valid SR received, LSR/DLSR are set to 0 then */
1541 GST_DEBUG ("no valid SR received");
1545 GST_DEBUG ("LSR %04x:%04x, DLSR %04x:%04x", LSR >> 16, LSR & 0xffff,
1546 DLSR >> 16, DLSR & 0xffff);
1549 *fractionlost = fraction;
1551 *packetslost = lost;
1553 *exthighestseq = extended_max;
1555 *jitter = stats->jitter >> 4;
1565 * rtp_source_get_last_sr:
1566 * @src: an #RTPSource
1567 * @time: time of packet arrival
1568 * @ntptime: the NTP time in 32.32 fixed point
1569 * @rtptime: the RTP time
1570 * @packet_count: the packet count
1571 * @octet_count: the octect count
1573 * Get the values of the last sender report as set with rtp_source_process_sr().
1575 * Returns: %TRUE if there was a valid SR report.
1578 rtp_source_get_last_sr (RTPSource * src, GstClockTime * time, guint64 * ntptime,
1579 guint32 * rtptime, guint32 * packet_count, guint32 * octet_count)
1581 RTPSenderReport *curr;
1583 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1585 curr = &src->stats.sr[src->stats.curr_sr];
1586 if (!curr->is_valid)
1590 *ntptime = curr->ntptime;
1592 *rtptime = curr->rtptime;
1594 *packet_count = curr->packet_count;
1596 *octet_count = curr->octet_count;
1604 * rtp_source_get_last_rb:
1605 * @src: an #RTPSource
1606 * @fractionlost: fraction lost since last SR/RR
1607 * @packetslost: the cumululative number of packets lost
1608 * @exthighestseq: the extended last sequence number received
1609 * @jitter: the interarrival jitter
1610 * @lsr: the last SR packet from this source
1611 * @dlsr: the delay since last SR packet
1612 * @round_trip: the round trip time
1614 * Get the values of the last RB report set with rtp_source_process_rb().
1616 * Returns: %TRUE if there was a valid SB report.
1619 rtp_source_get_last_rb (RTPSource * src, guint8 * fractionlost,
1620 gint32 * packetslost, guint32 * exthighestseq, guint32 * jitter,
1621 guint32 * lsr, guint32 * dlsr, guint32 * round_trip)
1623 RTPReceiverReport *curr;
1625 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1627 curr = &src->stats.rr[src->stats.curr_rr];
1628 if (!curr->is_valid)
1632 *fractionlost = curr->fractionlost;
1634 *packetslost = curr->packetslost;
1636 *exthighestseq = curr->exthighestseq;
1638 *jitter = curr->jitter;
1644 *round_trip = curr->round_trip;
1650 * rtp_source_find_conflicting_address:
1651 * @src: The source the packet came in
1652 * @address: address to check for
1653 * @time: The time when the packet that is possibly in conflict arrived
1655 * Checks if an address which has a conflict is already known. If it is
1656 * a known conflict, remember the time
1658 * Returns: TRUE if it was a known conflict, FALSE otherwise
1661 rtp_source_find_conflicting_address (RTPSource * src, GSocketAddress * address,
1666 for (item = g_list_first (src->conflicting_addresses);
1667 item; item = g_list_next (item)) {
1668 RTPConflictingAddress *known_conflict = item->data;
1670 if (__g_socket_address_equal (address, known_conflict->address)) {
1671 known_conflict->time = time;
1680 * rtp_source_add_conflicting_address:
1681 * @src: The source the packet came in
1682 * @address: address to remember
1683 * @time: The time when the packet that is in conflict arrived
1685 * Adds a new conflict address
1688 rtp_source_add_conflicting_address (RTPSource * src,
1689 GSocketAddress * address, GstClockTime time)
1691 RTPConflictingAddress *new_conflict;
1693 new_conflict = g_new0 (RTPConflictingAddress, 1);
1695 new_conflict->address = G_SOCKET_ADDRESS (g_object_ref (address));
1696 new_conflict->time = time;
1698 src->conflicting_addresses = g_list_prepend (src->conflicting_addresses,
1703 * rtp_source_timeout:
1704 * @src: The #RTPSource
1705 * @current_time: The current time
1706 * @collision_timeout: The amount of time after which a collision is timed out
1707 * @feedback_retention_window: The running time before which retained feedback
1708 * packets have to be discarded
1710 * This is processed on each RTCP interval. It times out old collisions.
1711 * It also times out old retained feedback packets
1714 rtp_source_timeout (RTPSource * src, GstClockTime current_time,
1715 GstClockTime collision_timeout, GstClockTime feedback_retention_window)
1720 item = g_list_first (src->conflicting_addresses);
1722 RTPConflictingAddress *known_conflict = item->data;
1723 GList *next_item = g_list_next (item);
1725 if (known_conflict->time < current_time - collision_timeout) {
1728 src->conflicting_addresses =
1729 g_list_delete_link (src->conflicting_addresses, item);
1730 buf = __g_socket_address_to_string (known_conflict->address);
1731 GST_DEBUG ("collision %p timed out: %s", known_conflict, buf);
1733 g_object_unref (known_conflict->address);
1734 g_free (known_conflict);
1739 /* Time out AVPF packets that are older than the desired length */
1740 while ((pkt = g_queue_peek_tail (src->retained_feedback)) &&
1741 GST_BUFFER_TIMESTAMP (pkt) < feedback_retention_window)
1742 gst_buffer_unref (g_queue_pop_tail (src->retained_feedback));
1746 compare_buffers (gconstpointer a, gconstpointer b, gpointer user_data)
1748 const GstBuffer *bufa = a;
1749 const GstBuffer *bufb = b;
1751 return GST_BUFFER_TIMESTAMP (bufa) - GST_BUFFER_TIMESTAMP (bufb);
1755 rtp_source_retain_rtcp_packet (RTPSource * src, GstRTCPPacket * packet,
1756 GstClockTime running_time)
1760 buffer = gst_buffer_copy_region (packet->rtcp->buffer, GST_BUFFER_COPY_MEMORY,
1761 packet->offset, (gst_rtcp_packet_get_length (packet) + 1) * 4);
1763 GST_BUFFER_TIMESTAMP (buffer) = running_time;
1765 g_queue_insert_sorted (src->retained_feedback, buffer, compare_buffers, NULL);
1769 rtp_source_has_retained (RTPSource * src, GCompareFunc func, gconstpointer data)
1771 if (g_queue_find_custom (src->retained_feedback, data, func))
1778 * @src: The #RTPSource
1781 * Register that @seqnum has not been received from @src.
1784 rtp_source_register_nack (RTPSource * src, guint16 seqnum)
1787 guint32 dword = seqnum << 16;
1790 len = src->nacks->len;
1791 for (i = 0; i < len; i++) {
1795 tdword = g_array_index (src->nacks, guint32, i);
1796 tseq = tdword >> 16;
1798 diff = gst_rtp_buffer_compare_seqnum (tseq, seqnum);
1802 /* we already have this seqnum */
1805 /* it comes before the recorded seqnum, FIXME, we could merge it
1806 * if not to far away */
1808 GST_DEBUG ("insert NACK #%u at %u", seqnum, i);
1809 g_array_insert_val (src->nacks, i, dword);
1810 } else if (diff < 16) {
1811 /* we can merge it */
1812 dword = g_array_index (src->nacks, guint32, i);
1813 dword |= 1 << (diff - 1);
1814 GST_DEBUG ("merge NACK #%u at %u with NACK #%u -> 0x%08x", seqnum, i,
1815 dword >> 16, dword);
1816 g_array_index (src->nacks, guint32, i) = dword;
1818 GST_DEBUG ("append NACK #%u", seqnum);
1819 g_array_append_val (src->nacks, dword);
1821 src->send_nack = TRUE;
1825 * @src: The #RTPSource
1826 * @n_nacks: result number of nacks
1828 * Get the registered NACKS since the last rtp_source_clear_nacks().
1830 * Returns: an array of @n_nacks seqnum values.
1833 rtp_source_get_nacks (RTPSource * src, guint * n_nacks)
1836 *n_nacks = src->nacks->len;
1838 return (guint32 *) src->nacks->data;
1842 rtp_source_clear_nacks (RTPSource * src)
1844 g_array_set_size (src->nacks, 0);
1845 src->send_nack = FALSE;