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 the 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;
227 src->stats.cycles = -1;
228 src->stats.jitter = 0;
229 src->stats.transit = -1;
230 src->stats.curr_sr = 0;
231 src->stats.sr[0].is_valid = FALSE;
232 src->stats.curr_rr = 0;
233 src->stats.rr[0].is_valid = FALSE;
234 src->stats.prev_rtptime = GST_CLOCK_TIME_NONE;
235 src->stats.prev_rtcptime = GST_CLOCK_TIME_NONE;
236 src->stats.last_rtptime = GST_CLOCK_TIME_NONE;
237 src->stats.last_rtcptime = GST_CLOCK_TIME_NONE;
241 rtp_source_init (RTPSource * src)
243 /* sources are initialy on probation until we receive enough valid RTP
244 * packets or a valid RTCP packet */
245 src->validated = FALSE;
246 src->internal = FALSE;
247 src->probation = DEFAULT_PROBATION;
248 src->curr_probation = src->probation;
249 src->closing = FALSE;
251 src->sdes = gst_structure_new_empty ("application/x-rtp-source-sdes");
254 src->clock_rate = -1;
255 src->packets = g_queue_new ();
256 src->seqnum_base = -1;
257 src->last_rtptime = -1;
259 src->retained_feedback = g_queue_new ();
261 rtp_source_reset (src);
265 rtp_conflicting_address_free (RTPConflictingAddress * addr)
267 g_object_unref (addr->address);
272 rtp_source_finalize (GObject * object)
277 src = RTP_SOURCE_CAST (object);
279 while ((buffer = g_queue_pop_head (src->packets)))
280 gst_buffer_unref (buffer);
281 g_queue_free (src->packets);
283 gst_structure_free (src->sdes);
285 g_free (src->bye_reason);
287 gst_caps_replace (&src->caps, NULL);
289 g_list_foreach (src->conflicting_addresses,
290 (GFunc) rtp_conflicting_address_free, NULL);
291 g_list_free (src->conflicting_addresses);
293 while ((buffer = g_queue_pop_head (src->retained_feedback)))
294 gst_buffer_unref (buffer);
295 g_queue_free (src->retained_feedback);
298 g_object_unref (src->rtp_from);
300 g_object_unref (src->rtcp_from);
302 G_OBJECT_CLASS (rtp_source_parent_class)->finalize (object);
305 static GstStructure *
306 rtp_source_create_stats (RTPSource * src)
309 gboolean is_sender = src->is_sender;
310 gboolean internal = src->internal;
313 guint8 fractionlost = 0;
314 gint32 packetslost = 0;
315 guint32 exthighestseq = 0;
319 guint32 round_trip = 0;
321 GstClockTime time = 0;
324 guint32 packet_count = 0;
325 guint32 octet_count = 0;
328 /* common data for all types of sources */
329 s = gst_structure_new ("application/x-rtp-source-stats",
330 "ssrc", G_TYPE_UINT, (guint) src->ssrc,
331 "internal", G_TYPE_BOOLEAN, internal,
332 "validated", G_TYPE_BOOLEAN, src->validated,
333 "received-bye", G_TYPE_BOOLEAN, src->marked_bye,
334 "is-csrc", G_TYPE_BOOLEAN, src->is_csrc,
335 "is-sender", G_TYPE_BOOLEAN, is_sender,
336 "seqnum-base", G_TYPE_INT, src->seqnum_base,
337 "clock-rate", G_TYPE_INT, src->clock_rate, NULL);
339 /* add address and port */
341 address_str = __g_socket_address_to_string (src->rtp_from);
342 gst_structure_set (s, "rtp-from", G_TYPE_STRING, address_str, NULL);
343 g_free (address_str);
345 if (src->rtcp_from) {
346 address_str = __g_socket_address_to_string (src->rtcp_from);
347 gst_structure_set (s, "rtcp-from", G_TYPE_STRING, address_str, NULL);
348 g_free (address_str);
351 gst_structure_set (s,
352 "octets-sent", G_TYPE_UINT64, src->stats.octets_sent,
353 "packets-sent", G_TYPE_UINT64, src->stats.packets_sent,
354 "octets-received", G_TYPE_UINT64, src->stats.octets_received,
355 "packets-received", G_TYPE_UINT64, src->stats.packets_received,
356 "bitrate", G_TYPE_UINT64, src->bitrate,
357 "packets-lost", G_TYPE_INT,
358 (gint) rtp_stats_get_packets_lost (&src->stats), "jitter", G_TYPE_UINT,
359 (guint) (src->stats.jitter >> 4), NULL);
361 /* get the last SR. */
362 have_sr = rtp_source_get_last_sr (src, &time, &ntptime, &rtptime,
363 &packet_count, &octet_count);
364 gst_structure_set (s,
365 "have-sr", G_TYPE_BOOLEAN, have_sr,
366 "sr-ntptime", G_TYPE_UINT64, ntptime,
367 "sr-rtptime", G_TYPE_UINT, (guint) rtptime,
368 "sr-octet-count", G_TYPE_UINT, (guint) octet_count,
369 "sr-packet-count", G_TYPE_UINT, (guint) packet_count, NULL);
372 /* get the last RB we sent */
373 gst_structure_set (s,
374 "sent-rb", G_TYPE_BOOLEAN, src->last_rr.is_valid,
375 "sent-rb-fractionlost", G_TYPE_UINT, (guint) src->last_rr.fractionlost,
376 "sent-rb-packetslost", G_TYPE_INT, (gint) src->last_rr.packetslost,
377 "sent-rb-exthighestseq", G_TYPE_UINT,
378 (guint) src->last_rr.exthighestseq, "sent-rb-jitter", G_TYPE_UINT,
379 (guint) src->last_rr.jitter, "sent-rb-lsr", G_TYPE_UINT,
380 (guint) src->last_rr.lsr, "sent-rb-dlsr", G_TYPE_UINT,
381 (guint) src->last_rr.dlsr, NULL);
383 /* get the last RB */
384 have_rb = rtp_source_get_last_rb (src, &fractionlost, &packetslost,
385 &exthighestseq, &jitter, &lsr, &dlsr, &round_trip);
387 gst_structure_set (s,
388 "have-rb", G_TYPE_BOOLEAN, have_rb,
389 "rb-fractionlost", G_TYPE_UINT, (guint) fractionlost,
390 "rb-packetslost", G_TYPE_INT, (gint) packetslost,
391 "rb-exthighestseq", G_TYPE_UINT, (guint) exthighestseq,
392 "rb-jitter", G_TYPE_UINT, (guint) jitter,
393 "rb-lsr", G_TYPE_UINT, (guint) lsr,
394 "rb-dlsr", G_TYPE_UINT, (guint) dlsr,
395 "rb-round-trip", G_TYPE_UINT, (guint) round_trip, NULL);
402 * rtp_source_get_sdes_struct:
403 * @src: an #RTPSource
405 * Get the SDES from @src. See the SDES property for more details.
407 * Returns: %GstStructure of type "application/x-rtp-source-sdes". The result is
408 * valid until the SDES items of @src are modified.
411 rtp_source_get_sdes_struct (RTPSource * src)
413 g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
419 sdes_struct_compare_func (GQuark field_id, const GValue * value,
425 old = GST_STRUCTURE (user_data);
426 field = g_quark_to_string (field_id);
428 if (!gst_structure_has_field (old, field))
431 g_assert (G_VALUE_HOLDS_STRING (value));
433 return strcmp (g_value_get_string (value), gst_structure_get_string (old,
438 * rtp_source_set_sdes_struct:
439 * @src: an #RTPSource
440 * @sdes: the SDES structure
442 * Store the @sdes in @src. @sdes must be a structure of type
443 * "application/x-rtp-source-sdes", see the SDES property for more details.
445 * This function takes ownership of @sdes.
447 * Returns: %FALSE if the SDES was unchanged.
450 rtp_source_set_sdes_struct (RTPSource * src, GstStructure * sdes)
454 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
455 g_return_val_if_fail (strcmp (gst_structure_get_name (sdes),
456 "application/x-rtp-source-sdes") == 0, FALSE);
458 changed = !gst_structure_foreach (sdes, sdes_struct_compare_func, src->sdes);
461 gst_structure_free (src->sdes);
464 gst_structure_free (sdes);
471 rtp_source_set_property (GObject * object, guint prop_id,
472 const GValue * value, GParamSpec * pspec)
476 src = RTP_SOURCE (object);
480 src->ssrc = g_value_get_uint (value);
483 src->probation = g_value_get_uint (value);
486 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
492 rtp_source_get_property (GObject * object, guint prop_id,
493 GValue * value, GParamSpec * pspec)
497 src = RTP_SOURCE (object);
501 g_value_set_uint (value, rtp_source_get_ssrc (src));
504 g_value_set_boolean (value, rtp_source_is_as_csrc (src));
506 case PROP_IS_VALIDATED:
507 g_value_set_boolean (value, rtp_source_is_validated (src));
510 g_value_set_boolean (value, rtp_source_is_sender (src));
513 g_value_set_boxed (value, rtp_source_get_sdes_struct (src));
516 g_value_take_boxed (value, rtp_source_create_stats (src));
519 g_value_set_uint (value, src->probation);
522 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
531 * Create a #RTPSource with @ssrc.
533 * Returns: a new #RTPSource. Use g_object_unref() after usage.
536 rtp_source_new (guint32 ssrc)
540 src = g_object_new (RTP_TYPE_SOURCE, NULL);
547 * rtp_source_set_callbacks:
548 * @src: an #RTPSource
549 * @cb: callback functions
550 * @user_data: user data
552 * Set the callbacks for the source.
555 rtp_source_set_callbacks (RTPSource * src, RTPSourceCallbacks * cb,
558 g_return_if_fail (RTP_IS_SOURCE (src));
560 src->callbacks.push_rtp = cb->push_rtp;
561 src->callbacks.clock_rate = cb->clock_rate;
562 src->user_data = user_data;
566 * rtp_source_get_ssrc:
567 * @src: an #RTPSource
569 * Get the SSRC of @source.
571 * Returns: the SSRC of src.
574 rtp_source_get_ssrc (RTPSource * src)
578 g_return_val_if_fail (RTP_IS_SOURCE (src), 0);
586 * rtp_source_set_as_csrc:
587 * @src: an #RTPSource
589 * Configure @src as a CSRC, this will also validate @src.
592 rtp_source_set_as_csrc (RTPSource * src)
594 g_return_if_fail (RTP_IS_SOURCE (src));
596 src->validated = TRUE;
601 * rtp_source_is_as_csrc:
602 * @src: an #RTPSource
604 * Check if @src is a contributing source.
606 * Returns: %TRUE if @src is acting as a contributing source.
609 rtp_source_is_as_csrc (RTPSource * src)
613 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
615 result = src->is_csrc;
621 * rtp_source_is_active:
622 * @src: an #RTPSource
624 * Check if @src is an active source. A source is active if it has been
625 * validated and has not yet received a BYE packet
627 * Returns: %TRUE if @src is an qactive source.
630 rtp_source_is_active (RTPSource * src)
634 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
636 result = RTP_SOURCE_IS_ACTIVE (src);
642 * rtp_source_is_validated:
643 * @src: an #RTPSource
645 * Check if @src is a validated source.
647 * Returns: %TRUE if @src is a validated source.
650 rtp_source_is_validated (RTPSource * src)
654 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
656 result = src->validated;
662 * rtp_source_is_sender:
663 * @src: an #RTPSource
665 * Check if @src is a sending source.
667 * Returns: %TRUE if @src is a sending source.
670 rtp_source_is_sender (RTPSource * src)
674 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
676 result = RTP_SOURCE_IS_SENDER (src);
682 * rtp_source_is_marked_bye:
683 * @src: an #RTPSource
685 * Check if @src is marked as leaving the session with a BYE packet.
687 * Returns: %TRUE if @src has been marked BYE.
690 rtp_source_is_marked_bye (RTPSource * src)
694 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
696 result = RTP_SOURCE_IS_MARKED_BYE (src);
703 * rtp_source_get_bye_reason:
704 * @src: an #RTPSource
706 * Get the BYE reason for @src. Check if the source is marked as leaving the
707 * session with a BYE message first with rtp_source_is_marked_bye().
709 * Returns: The BYE reason or NULL when no reason was given or the source was
710 * not marked BYE yet. g_free() after usage.
713 rtp_source_get_bye_reason (RTPSource * src)
717 g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
719 result = g_strdup (src->bye_reason);
725 * rtp_source_update_caps:
726 * @src: an #RTPSource
729 * Parse @caps and store all relevant information in @source.
732 rtp_source_update_caps (RTPSource * src, GstCaps * caps)
738 /* nothing changed, return */
739 if (caps == NULL || src->caps == caps)
742 s = gst_caps_get_structure (caps, 0);
744 if (gst_structure_get_int (s, "payload", &ival))
748 GST_DEBUG ("got payload %d", src->payload);
750 if (gst_structure_get_int (s, "clock-rate", &ival))
751 src->clock_rate = ival;
753 src->clock_rate = -1;
755 GST_DEBUG ("got clock-rate %d", src->clock_rate);
757 if (gst_structure_get_uint (s, "seqnum-base", &val))
758 src->seqnum_base = val;
760 src->seqnum_base = -1;
762 GST_DEBUG ("got seqnum-base %" G_GINT32_FORMAT, src->seqnum_base);
764 gst_caps_replace (&src->caps, caps);
768 * rtp_source_set_rtp_from:
769 * @src: an #RTPSource
770 * @address: the RTP address to set
772 * Set that @src is receiving RTP packets from @address. This is used for
773 * collistion checking.
776 rtp_source_set_rtp_from (RTPSource * src, GSocketAddress * address)
778 g_return_if_fail (RTP_IS_SOURCE (src));
781 g_object_unref (src->rtp_from);
782 src->rtp_from = G_SOCKET_ADDRESS (g_object_ref (address));
786 * rtp_source_set_rtcp_from:
787 * @src: an #RTPSource
788 * @address: the RTCP address to set
790 * Set that @src is receiving RTCP packets from @address. This is used for
791 * collistion checking.
794 rtp_source_set_rtcp_from (RTPSource * src, GSocketAddress * address)
796 g_return_if_fail (RTP_IS_SOURCE (src));
799 g_object_unref (src->rtcp_from);
800 src->rtcp_from = G_SOCKET_ADDRESS (g_object_ref (address));
804 push_packet (RTPSource * src, GstBuffer * buffer)
806 GstFlowReturn ret = GST_FLOW_OK;
808 /* push queued packets first if any */
809 while (!g_queue_is_empty (src->packets)) {
810 GstBuffer *buffer = GST_BUFFER_CAST (g_queue_pop_head (src->packets));
812 GST_LOG ("pushing queued packet");
813 if (src->callbacks.push_rtp)
814 src->callbacks.push_rtp (src, buffer, src->user_data);
816 gst_buffer_unref (buffer);
818 GST_LOG ("pushing new packet");
820 if (src->callbacks.push_rtp)
821 ret = src->callbacks.push_rtp (src, buffer, src->user_data);
823 gst_buffer_unref (buffer);
829 get_clock_rate (RTPSource * src, guint8 payload)
831 if (src->payload == -1) {
832 /* first payload received, nothing was in the caps, lock on to this payload */
833 src->payload = payload;
834 GST_DEBUG ("first payload %d", payload);
835 } else if (payload != src->payload) {
836 /* we have a different payload than before, reset the clock-rate */
837 GST_DEBUG ("new payload %d", payload);
838 src->payload = payload;
839 src->clock_rate = -1;
840 src->stats.transit = -1;
843 if (src->clock_rate == -1) {
844 gint clock_rate = -1;
846 if (src->callbacks.clock_rate)
847 clock_rate = src->callbacks.clock_rate (src, payload, src->user_data);
849 GST_DEBUG ("got clock-rate %d", clock_rate);
851 src->clock_rate = clock_rate;
853 return src->clock_rate;
856 /* Jitter is the variation in the delay of received packets in a flow. It is
857 * measured by comparing the interval when RTP packets were sent to the interval
858 * at which they were received. For instance, if packet #1 and packet #2 leave
859 * 50 milliseconds apart and arrive 60 milliseconds apart, then the jitter is 10
862 calculate_jitter (RTPSource * src, GstBuffer * buffer,
863 RTPArrivalStats * arrival)
865 GstClockTime running_time;
866 guint32 rtparrival, transit, rtptime;
870 GstRTPBuffer rtp = { NULL };
872 /* get arrival time */
873 if ((running_time = arrival->running_time) == GST_CLOCK_TIME_NONE)
876 if (!gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp))
879 pt = gst_rtp_buffer_get_payload_type (&rtp);
881 GST_LOG ("SSRC %08x got payload %d", src->ssrc, pt);
884 if ((clock_rate = get_clock_rate (src, pt)) == -1) {
885 gst_rtp_buffer_unmap (&rtp);
889 rtptime = gst_rtp_buffer_get_timestamp (&rtp);
891 /* convert arrival time to RTP timestamp units, truncate to 32 bits, we don't
892 * care about the absolute value, just the difference. */
893 rtparrival = gst_util_uint64_scale_int (running_time, clock_rate, GST_SECOND);
895 /* transit time is difference with RTP timestamp */
896 transit = rtparrival - rtptime;
898 /* get ABS diff with previous transit time */
899 if (src->stats.transit != -1) {
900 if (transit > src->stats.transit)
901 diff = transit - src->stats.transit;
903 diff = src->stats.transit - transit;
907 src->stats.transit = transit;
909 /* update jitter, the value we store is scaled up so we can keep precision. */
910 src->stats.jitter += diff - ((src->stats.jitter + 8) >> 4);
912 src->stats.prev_rtptime = src->stats.last_rtptime;
913 src->stats.last_rtptime = rtparrival;
915 GST_LOG ("rtparrival %u, rtptime %u, clock-rate %d, diff %d, jitter: %f",
916 rtparrival, rtptime, clock_rate, diff, (src->stats.jitter) / 16.0);
918 gst_rtp_buffer_unmap (&rtp);
924 GST_WARNING ("cannot get current running_time");
929 GST_WARNING ("invalid RTP packet");
934 GST_WARNING ("cannot get clock-rate for pt %d", pt);
940 init_seq (RTPSource * src, guint16 seq)
942 src->stats.base_seq = seq;
943 src->stats.max_seq = seq;
944 src->stats.bad_seq = RTP_SEQ_MOD + 1; /* so seq == bad_seq is false */
945 src->stats.cycles = 0;
946 src->stats.packets_received = 0;
947 src->stats.octets_received = 0;
948 src->stats.bytes_received = 0;
949 src->stats.prev_received = 0;
950 src->stats.prev_expected = 0;
952 GST_DEBUG ("base_seq %d", seq);
955 #define BITRATE_INTERVAL (2 * GST_SECOND)
958 do_bitrate_estimation (RTPSource * src, GstClockTime running_time,
959 guint64 * bytes_handled)
963 if (src->prev_rtime) {
964 elapsed = running_time - src->prev_rtime;
966 if (elapsed > BITRATE_INTERVAL) {
969 rate = gst_util_uint64_scale (*bytes_handled, 8 * GST_SECOND, elapsed);
971 GST_LOG ("Elapsed %" G_GUINT64_FORMAT ", bytes %" G_GUINT64_FORMAT
972 ", rate %" G_GUINT64_FORMAT, elapsed, *bytes_handled, rate);
974 if (src->bitrate == 0)
977 src->bitrate = ((src->bitrate * 3) + rate) / 4;
979 src->prev_rtime = running_time;
983 GST_LOG ("Reset bitrate measurement");
984 src->prev_rtime = running_time;
990 * rtp_source_process_rtp:
991 * @src: an #RTPSource
992 * @buffer: an RTP buffer
994 * Let @src handle the incomming RTP @buffer.
996 * Returns: a #GstFlowReturn.
999 rtp_source_process_rtp (RTPSource * src, GstBuffer * buffer,
1000 RTPArrivalStats * arrival)
1002 GstFlowReturn result = GST_FLOW_OK;
1003 guint16 seqnr, udelta;
1004 RTPSourceStats *stats;
1006 GstRTPBuffer rtp = { NULL };
1008 g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
1009 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1011 stats = &src->stats;
1013 if (!gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp))
1014 goto invalid_packet;
1016 seqnr = gst_rtp_buffer_get_seq (&rtp);
1017 gst_rtp_buffer_unmap (&rtp);
1019 if (stats->cycles == -1) {
1020 GST_DEBUG ("received first buffer");
1021 /* first time we heard of this source */
1022 init_seq (src, seqnr);
1023 src->stats.max_seq = seqnr - 1;
1024 src->curr_probation = src->probation;
1027 udelta = seqnr - stats->max_seq;
1029 /* if we are still on probation, check seqnum */
1030 if (src->curr_probation) {
1031 expected = src->stats.max_seq + 1;
1033 /* when in probation, we require consecutive seqnums */
1034 if (seqnr == expected) {
1035 /* expected packet */
1036 GST_DEBUG ("probation: seqnr %d == expected %d", seqnr, expected);
1037 src->curr_probation--;
1038 src->stats.max_seq = seqnr;
1039 if (src->curr_probation == 0) {
1040 GST_DEBUG ("probation done!");
1041 init_seq (src, seqnr);
1045 GST_DEBUG ("probation %d: queue buffer", src->curr_probation);
1046 /* when still in probation, keep packets in a list. */
1047 g_queue_push_tail (src->packets, buffer);
1048 /* remove packets from queue if there are too many */
1049 while (g_queue_get_length (src->packets) > RTP_MAX_PROBATION_LEN) {
1050 q = g_queue_pop_head (src->packets);
1051 gst_buffer_unref (q);
1056 /* unexpected seqnum in probation */
1057 goto probation_seqnum;
1059 } else if (udelta < RTP_MAX_DROPOUT) {
1060 /* in order, with permissible gap */
1061 if (seqnr < stats->max_seq) {
1062 /* sequence number wrapped - count another 64K cycle. */
1063 stats->cycles += RTP_SEQ_MOD;
1065 stats->max_seq = seqnr;
1066 } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
1067 /* the sequence number made a very large jump */
1068 if (seqnr == stats->bad_seq) {
1069 /* two sequential packets -- assume that the other side
1070 * restarted without telling us so just re-sync
1071 * (i.e., pretend this was the first packet). */
1072 init_seq (src, seqnr);
1074 /* unacceptable jump */
1075 stats->bad_seq = (seqnr + 1) & (RTP_SEQ_MOD - 1);
1079 /* duplicate or reordered packet, will be filtered by jitterbuffer. */
1080 GST_WARNING ("duplicate or reordered packet (seqnr %d)", seqnr);
1083 src->stats.octets_received += arrival->payload_len;
1084 src->stats.bytes_received += arrival->bytes;
1085 src->stats.packets_received++;
1086 /* for the bitrate estimation */
1087 src->bytes_received += arrival->payload_len;
1088 /* the source that sent the packet must be a sender */
1089 src->is_sender = TRUE;
1090 src->validated = TRUE;
1092 do_bitrate_estimation (src, arrival->running_time, &src->bytes_received);
1094 GST_LOG ("seq %d, PC: %" G_GUINT64_FORMAT ", OC: %" G_GUINT64_FORMAT,
1095 seqnr, src->stats.packets_received, src->stats.octets_received);
1097 /* calculate jitter for the stats */
1098 calculate_jitter (src, buffer, arrival);
1100 /* we're ready to push the RTP packet now */
1101 result = push_packet (src, buffer);
1109 GST_WARNING ("invalid packet received");
1110 gst_buffer_unref (buffer);
1115 GST_WARNING ("unacceptable seqnum received");
1116 gst_buffer_unref (buffer);
1121 GST_WARNING ("probation: seqnr %d != expected %d", seqnr, expected);
1122 src->curr_probation = src->probation;
1123 src->stats.max_seq = seqnr;
1124 gst_buffer_unref (buffer);
1130 * rtp_source_mark_bye:
1131 * @src: an #RTPSource
1132 * @reason: the reason for leaving
1134 * Mark @src in the BYE state. This can happen when the source wants to
1135 * leave the sesssion or when a BYE packets has been received.
1137 * This will make the source inactive.
1140 rtp_source_mark_bye (RTPSource * src, const gchar * reason)
1142 g_return_if_fail (RTP_IS_SOURCE (src));
1144 GST_DEBUG ("marking SSRC %08x as BYE, reason: %s", src->ssrc,
1145 GST_STR_NULL (reason));
1147 /* copy the reason and mark as bye */
1148 g_free (src->bye_reason);
1149 src->bye_reason = g_strdup (reason);
1150 src->marked_bye = TRUE;
1154 set_ssrc (GstBuffer ** buffer, guint idx, RTPSource * src)
1156 GstRTPBuffer rtp = { NULL };
1158 *buffer = gst_buffer_make_writable (*buffer);
1159 if (gst_rtp_buffer_map (*buffer, GST_MAP_WRITE, &rtp)) {
1160 gst_rtp_buffer_set_ssrc (&rtp, src->ssrc);
1161 gst_rtp_buffer_unmap (&rtp);
1167 * rtp_source_send_rtp:
1168 * @src: an #RTPSource
1169 * @data: an RTP buffer or a list of RTP buffers
1170 * @is_list: if @data is a buffer or list
1171 * @running_time: the running time of @data
1173 * Send @data (an RTP buffer or list of buffers) originating from @src.
1174 * This will make @src a sender. This function takes ownership of @data and
1175 * modifies the SSRC in the RTP packet to that of @src when needed.
1177 * Returns: a #GstFlowReturn.
1180 rtp_source_send_rtp (RTPSource * src, gpointer data, gboolean is_list,
1181 GstClockTime running_time)
1183 GstFlowReturn result;
1186 guint64 ext_rtptime;
1187 guint64 rt_diff, rtp_diff;
1188 GstBufferList *list = NULL;
1189 GstBuffer *buffer = NULL;
1192 GstRTPBuffer rtp = { NULL };
1194 g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
1195 g_return_val_if_fail (is_list || GST_IS_BUFFER (data), GST_FLOW_ERROR);
1198 list = GST_BUFFER_LIST_CAST (data);
1200 /* We can grab the caps from the first group, since all
1201 * groups of a buffer list have same caps. */
1202 buffer = gst_buffer_list_get (list, 0);
1206 buffer = GST_BUFFER_CAST (data);
1209 /* we are a sender now */
1210 src->is_sender = TRUE;
1215 /* Each group makes up a network packet. */
1216 packets = gst_buffer_list_length (list);
1217 for (i = 0, len = 0; i < packets; i++) {
1218 if (!gst_rtp_buffer_map (gst_buffer_list_get (list, i), GST_MAP_READ,
1220 goto invalid_packet;
1222 len += gst_rtp_buffer_get_payload_len (&rtp);
1223 gst_rtp_buffer_unmap (&rtp);
1225 /* subsequent info taken from first list member */
1226 gst_rtp_buffer_map (gst_buffer_list_get (list, 0), GST_MAP_READ, &rtp);
1229 if (!gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp))
1230 goto invalid_packet;
1232 len = gst_rtp_buffer_get_payload_len (&rtp);
1235 /* update stats for the SR */
1236 src->stats.packets_sent += packets;
1237 src->stats.octets_sent += len;
1238 src->bytes_sent += len;
1240 do_bitrate_estimation (src, running_time, &src->bytes_sent);
1242 rtptime = gst_rtp_buffer_get_timestamp (&rtp);
1243 ext_rtptime = src->last_rtptime;
1244 ext_rtptime = gst_rtp_buffer_ext_timestamp (&ext_rtptime, rtptime);
1246 GST_LOG ("SSRC %08x, RTP %" G_GUINT64_FORMAT ", running_time %"
1247 GST_TIME_FORMAT, src->ssrc, ext_rtptime, GST_TIME_ARGS (running_time));
1249 if (ext_rtptime > src->last_rtptime) {
1250 rtp_diff = ext_rtptime - src->last_rtptime;
1251 rt_diff = running_time - src->last_rtime;
1253 /* calc the diff so we can detect drift at the sender. This can also be used
1254 * to guestimate the clock rate if the NTP time is locked to the RTP
1255 * timestamps (as is the case when the capture device is providing the clock). */
1256 GST_LOG ("SSRC %08x, diff RTP %" G_GUINT64_FORMAT ", diff running_time %"
1257 GST_TIME_FORMAT, src->ssrc, rtp_diff, GST_TIME_ARGS (rt_diff));
1260 /* we keep track of the last received RTP timestamp and the corresponding
1261 * buffer running_time so that we can use this info when constructing SR reports */
1262 src->last_rtime = running_time;
1263 src->last_rtptime = ext_rtptime;
1266 if (!src->callbacks.push_rtp) {
1267 gst_rtp_buffer_unmap (&rtp);
1271 ssrc = gst_rtp_buffer_get_ssrc (&rtp);
1272 gst_rtp_buffer_unmap (&rtp);
1274 if (ssrc != src->ssrc) {
1275 /* the SSRC of the packet is not correct, make a writable buffer and
1276 * update the SSRC. This could involve a complete copy of the packet when
1277 * it is not writable. Usually the payloader will use caps negotiation to
1278 * get the correct SSRC from the session manager before pushing anything. */
1280 /* FIXME, we don't want to warn yet because we can't inform any payloader
1281 * of the changes SSRC yet because we don't implement pad-alloc. */
1282 GST_LOG ("updating SSRC from %08x to %08x, fix the payloader", ssrc,
1286 list = gst_buffer_list_make_writable (list);
1287 gst_buffer_list_foreach (list, (GstBufferListFunc) set_ssrc, src);
1289 set_ssrc (&buffer, 0, src);
1292 GST_LOG ("pushing RTP %s %" G_GUINT64_FORMAT, is_list ? "list" : "packet",
1293 src->stats.packets_sent);
1295 result = src->callbacks.push_rtp (src, data, src->user_data);
1302 GST_WARNING ("invalid packet received");
1303 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1308 GST_WARNING ("no buffers in buffer list");
1309 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1314 GST_WARNING ("no callback installed, dropping packet");
1315 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1321 * rtp_source_process_sr:
1322 * @src: an #RTPSource
1323 * @time: time of packet arrival
1324 * @ntptime: the NTP time in 32.32 fixed point
1325 * @rtptime: the RTP time
1326 * @packet_count: the packet count
1327 * @octet_count: the octect count
1329 * Update the sender report in @src.
1332 rtp_source_process_sr (RTPSource * src, GstClockTime time, guint64 ntptime,
1333 guint32 rtptime, guint32 packet_count, guint32 octet_count)
1335 RTPSenderReport *curr;
1338 g_return_if_fail (RTP_IS_SOURCE (src));
1340 GST_DEBUG ("got SR packet: SSRC %08x, NTP %08x:%08x, RTP %" G_GUINT32_FORMAT
1341 ", PC %" G_GUINT32_FORMAT ", OC %" G_GUINT32_FORMAT, src->ssrc,
1342 (guint32) (ntptime >> 32), (guint32) (ntptime & 0xffffffff), rtptime,
1343 packet_count, octet_count);
1345 curridx = src->stats.curr_sr ^ 1;
1346 curr = &src->stats.sr[curridx];
1348 /* this is a sender now */
1349 src->is_sender = TRUE;
1351 /* update current */
1352 curr->is_valid = TRUE;
1353 curr->ntptime = ntptime;
1354 curr->rtptime = rtptime;
1355 curr->packet_count = packet_count;
1356 curr->octet_count = octet_count;
1360 src->stats.curr_sr = curridx;
1362 src->stats.prev_rtcptime = src->stats.last_rtcptime;
1363 src->stats.last_rtcptime = time;
1367 * rtp_source_process_rb:
1368 * @src: an #RTPSource
1369 * @ntpnstime: the current time in nanoseconds since 1970
1370 * @fractionlost: fraction lost since last SR/RR
1371 * @packetslost: the cumululative number of packets lost
1372 * @exthighestseq: the extended last sequence number received
1373 * @jitter: the interarrival jitter
1374 * @lsr: the last SR packet from this source
1375 * @dlsr: the delay since last SR packet
1377 * Update the report block in @src.
1380 rtp_source_process_rb (RTPSource * src, guint64 ntpnstime,
1381 guint8 fractionlost, gint32 packetslost, guint32 exthighestseq,
1382 guint32 jitter, guint32 lsr, guint32 dlsr)
1384 RTPReceiverReport *curr;
1389 g_return_if_fail (RTP_IS_SOURCE (src));
1391 GST_DEBUG ("got RB packet: SSRC %08x, FL %2x, PL %d, HS %" G_GUINT32_FORMAT
1392 ", jitter %" G_GUINT32_FORMAT ", LSR %04x:%04x, DLSR %04x:%04x",
1393 src->ssrc, fractionlost, packetslost, exthighestseq, jitter, lsr >> 16,
1394 lsr & 0xffff, dlsr >> 16, dlsr & 0xffff);
1396 curridx = src->stats.curr_rr ^ 1;
1397 curr = &src->stats.rr[curridx];
1399 /* update current */
1400 curr->is_valid = TRUE;
1401 curr->fractionlost = fractionlost;
1402 curr->packetslost = packetslost;
1403 curr->exthighestseq = exthighestseq;
1404 curr->jitter = jitter;
1408 /* convert the NTP time in nanoseconds to 32.32 fixed point */
1409 f_ntp = gst_util_uint64_scale (ntpnstime, (1LL << 32), GST_SECOND);
1410 /* calculate round trip, round the time up */
1411 ntp = ((f_ntp + 0xffff) >> 16) & 0xffffffff;
1414 if (A > 0 && ntp > A)
1418 curr->round_trip = A;
1420 GST_DEBUG ("NTP %04x:%04x, round trip %04x:%04x", ntp >> 16, ntp & 0xffff,
1421 A >> 16, A & 0xffff);
1424 src->stats.curr_rr = curridx;
1428 * rtp_source_get_new_sr:
1429 * @src: an #RTPSource
1430 * @ntpnstime: the current time in nanoseconds since 1970
1431 * @running_time: the current running_time of the pipeline.
1432 * @ntptime: the NTP time in 32.32 fixed point
1433 * @rtptime: the RTP time corresponding to @ntptime
1434 * @packet_count: the packet count
1435 * @octet_count: the octect count
1437 * Get new values to put into a new SR report from this source.
1439 * @running_time and @ntpnstime are captured at the same time and represent the
1440 * running time of the pipeline clock and the absolute current system time in
1441 * nanoseconds respectively. Together with the last running_time and rtp timestamp
1442 * we have observed in the source, we can generate @ntptime and @rtptime for an SR
1443 * packet. @ntptime is basically the fixed point representation of @ntpnstime
1444 * and @rtptime the associated RTP timestamp.
1446 * Returns: %TRUE on success.
1449 rtp_source_get_new_sr (RTPSource * src, guint64 ntpnstime,
1450 GstClockTime running_time, guint64 * ntptime, guint32 * rtptime,
1451 guint32 * packet_count, guint32 * octet_count)
1454 guint64 t_current_ntp;
1455 GstClockTimeDiff diff;
1457 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1459 /* We last saw a buffer with last_rtptime at last_rtime. Given a running_time
1460 * and an NTP time, we can scale the RTP timestamps so that they match the
1461 * given NTP time. for scaling, we assume that the slope of the rtptime vs
1462 * running_time vs ntptime curve is close to 1, which is certainly
1463 * sufficient for the frequency at which we report SR and the rate we send
1464 * out RTP packets. */
1465 t_rtp = src->last_rtptime;
1467 GST_DEBUG ("last_rtime %" GST_TIME_FORMAT ", last_rtptime %"
1468 G_GUINT64_FORMAT, GST_TIME_ARGS (src->last_rtime), t_rtp);
1470 if (src->clock_rate != -1) {
1471 /* get the diff between the clock running_time and the buffer running_time.
1472 * This is the elapsed time, as measured against the pipeline clock, between
1473 * when the rtp timestamp was observed and the current running_time.
1475 * We need to apply this diff to the RTP timestamp to get the RTP timestamp
1476 * for the given ntpnstime. */
1477 diff = GST_CLOCK_DIFF (src->last_rtime, running_time);
1479 /* now translate the diff to RTP time, handle positive and negative cases.
1480 * If there is no diff, we already set rtptime correctly above. */
1482 GST_DEBUG ("running_time %" GST_TIME_FORMAT ", diff %" GST_TIME_FORMAT,
1483 GST_TIME_ARGS (running_time), GST_TIME_ARGS (diff));
1484 t_rtp += gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1487 GST_DEBUG ("running_time %" GST_TIME_FORMAT ", diff -%" GST_TIME_FORMAT,
1488 GST_TIME_ARGS (running_time), GST_TIME_ARGS (diff));
1489 t_rtp -= gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1492 GST_WARNING ("no clock-rate, cannot interpolate rtp time");
1495 /* convert the NTP time in nanoseconds to 32.32 fixed point */
1496 t_current_ntp = gst_util_uint64_scale (ntpnstime, (1LL << 32), GST_SECOND);
1498 GST_DEBUG ("NTP %08x:%08x, RTP %" G_GUINT32_FORMAT,
1499 (guint32) (t_current_ntp >> 32), (guint32) (t_current_ntp & 0xffffffff),
1503 *ntptime = t_current_ntp;
1507 *packet_count = src->stats.packets_sent;
1509 *octet_count = src->stats.octets_sent;
1515 * rtp_source_get_new_rb:
1516 * @src: an #RTPSource
1517 * @time: the current time of the system clock
1518 * @fractionlost: fraction lost since last SR/RR
1519 * @packetslost: the cumululative number of packets lost
1520 * @exthighestseq: the extended last sequence number received
1521 * @jitter: the interarrival jitter
1522 * @lsr: the last SR packet from this source
1523 * @dlsr: the delay since last SR packet
1525 * Get new values to put into a new report block from this source.
1527 * Returns: %TRUE on success.
1530 rtp_source_get_new_rb (RTPSource * src, GstClockTime time,
1531 guint8 * fractionlost, gint32 * packetslost, guint32 * exthighestseq,
1532 guint32 * jitter, guint32 * lsr, guint32 * dlsr)
1534 RTPSourceStats *stats;
1535 guint64 extended_max, expected;
1536 guint64 expected_interval, received_interval, ntptime;
1537 gint64 lost, lost_interval;
1538 guint32 fraction, LSR, DLSR;
1539 GstClockTime sr_time;
1541 stats = &src->stats;
1543 extended_max = stats->cycles + stats->max_seq;
1544 expected = extended_max - stats->base_seq + 1;
1546 GST_DEBUG ("ext_max %" G_GUINT64_FORMAT ", expected %" G_GUINT64_FORMAT
1547 ", received %" G_GUINT64_FORMAT ", base_seq %" G_GUINT32_FORMAT,
1548 extended_max, expected, stats->packets_received, stats->base_seq);
1550 lost = expected - stats->packets_received;
1551 lost = CLAMP (lost, -0x800000, 0x7fffff);
1553 expected_interval = expected - stats->prev_expected;
1554 stats->prev_expected = expected;
1555 received_interval = stats->packets_received - stats->prev_received;
1556 stats->prev_received = stats->packets_received;
1558 lost_interval = expected_interval - received_interval;
1560 if (expected_interval == 0 || lost_interval <= 0)
1563 fraction = (lost_interval << 8) / expected_interval;
1565 GST_DEBUG ("add RR for SSRC %08x", src->ssrc);
1566 /* we scaled the jitter up for additional precision */
1567 GST_DEBUG ("fraction %" G_GUINT32_FORMAT ", lost %" G_GINT64_FORMAT
1568 ", extseq %" G_GUINT64_FORMAT ", jitter %d", fraction, lost,
1569 extended_max, stats->jitter >> 4);
1571 if (rtp_source_get_last_sr (src, &sr_time, &ntptime, NULL, NULL, NULL)) {
1574 /* LSR is middle 32 bits of the last ntptime */
1575 LSR = (ntptime >> 16) & 0xffffffff;
1576 diff = time - sr_time;
1577 GST_DEBUG ("last SR time diff %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
1578 /* DLSR, delay since last SR is expressed in 1/65536 second units */
1579 DLSR = gst_util_uint64_scale_int (diff, 65536, GST_SECOND);
1581 /* No valid SR received, LSR/DLSR are set to 0 then */
1582 GST_DEBUG ("no valid SR received");
1586 GST_DEBUG ("LSR %04x:%04x, DLSR %04x:%04x", LSR >> 16, LSR & 0xffff,
1587 DLSR >> 16, DLSR & 0xffff);
1590 *fractionlost = fraction;
1592 *packetslost = lost;
1594 *exthighestseq = extended_max;
1596 *jitter = stats->jitter >> 4;
1606 * rtp_source_get_last_sr:
1607 * @src: an #RTPSource
1608 * @time: time of packet arrival
1609 * @ntptime: the NTP time in 32.32 fixed point
1610 * @rtptime: the RTP time
1611 * @packet_count: the packet count
1612 * @octet_count: the octect count
1614 * Get the values of the last sender report as set with rtp_source_process_sr().
1616 * Returns: %TRUE if there was a valid SR report.
1619 rtp_source_get_last_sr (RTPSource * src, GstClockTime * time, guint64 * ntptime,
1620 guint32 * rtptime, guint32 * packet_count, guint32 * octet_count)
1622 RTPSenderReport *curr;
1624 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1626 curr = &src->stats.sr[src->stats.curr_sr];
1627 if (!curr->is_valid)
1631 *ntptime = curr->ntptime;
1633 *rtptime = curr->rtptime;
1635 *packet_count = curr->packet_count;
1637 *octet_count = curr->octet_count;
1645 * rtp_source_get_last_rb:
1646 * @src: an #RTPSource
1647 * @fractionlost: fraction lost since last SR/RR
1648 * @packetslost: the cumululative number of packets lost
1649 * @exthighestseq: the extended last sequence number received
1650 * @jitter: the interarrival jitter
1651 * @lsr: the last SR packet from this source
1652 * @dlsr: the delay since last SR packet
1653 * @round_trip: the round trip time
1655 * Get the values of the last RB report set with rtp_source_process_rb().
1657 * Returns: %TRUE if there was a valid SB report.
1660 rtp_source_get_last_rb (RTPSource * src, guint8 * fractionlost,
1661 gint32 * packetslost, guint32 * exthighestseq, guint32 * jitter,
1662 guint32 * lsr, guint32 * dlsr, guint32 * round_trip)
1664 RTPReceiverReport *curr;
1666 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1668 curr = &src->stats.rr[src->stats.curr_rr];
1669 if (!curr->is_valid)
1673 *fractionlost = curr->fractionlost;
1675 *packetslost = curr->packetslost;
1677 *exthighestseq = curr->exthighestseq;
1679 *jitter = curr->jitter;
1685 *round_trip = curr->round_trip;
1691 * rtp_source_find_conflicting_address:
1692 * @src: The source the packet came in
1693 * @address: address to check for
1694 * @time: The time when the packet that is possibly in conflict arrived
1696 * Checks if an address which has a conflict is already known. If it is
1697 * a known conflict, remember the time
1699 * Returns: TRUE if it was a known conflict, FALSE otherwise
1702 rtp_source_find_conflicting_address (RTPSource * src, GSocketAddress * address,
1707 for (item = g_list_first (src->conflicting_addresses);
1708 item; item = g_list_next (item)) {
1709 RTPConflictingAddress *known_conflict = item->data;
1711 if (__g_socket_address_equal (address, known_conflict->address)) {
1712 known_conflict->time = time;
1721 * rtp_source_add_conflicting_address:
1722 * @src: The source the packet came in
1723 * @address: address to remember
1724 * @time: The time when the packet that is in conflict arrived
1726 * Adds a new conflict address
1729 rtp_source_add_conflicting_address (RTPSource * src,
1730 GSocketAddress * address, GstClockTime time)
1732 RTPConflictingAddress *new_conflict;
1734 new_conflict = g_new0 (RTPConflictingAddress, 1);
1736 new_conflict->address = G_SOCKET_ADDRESS (g_object_ref (address));
1737 new_conflict->time = time;
1739 src->conflicting_addresses = g_list_prepend (src->conflicting_addresses,
1744 * rtp_source_timeout:
1745 * @src: The #RTPSource
1746 * @current_time: The current time
1747 * @collision_timeout: The amount of time after which a collision is timed out
1748 * @feedback_retention_window: The running time before which retained feedback
1749 * packets have to be discarded
1751 * This is processed on each RTCP interval. It times out old collisions.
1752 * It also times out old retained feedback packets
1755 rtp_source_timeout (RTPSource * src, GstClockTime current_time,
1756 GstClockTime collision_timeout, GstClockTime feedback_retention_window)
1761 item = g_list_first (src->conflicting_addresses);
1763 RTPConflictingAddress *known_conflict = item->data;
1764 GList *next_item = g_list_next (item);
1766 if (known_conflict->time < current_time - collision_timeout) {
1769 src->conflicting_addresses =
1770 g_list_delete_link (src->conflicting_addresses, item);
1771 buf = __g_socket_address_to_string (known_conflict->address);
1772 GST_DEBUG ("collision %p timed out: %s", known_conflict, buf);
1774 g_object_unref (known_conflict->address);
1775 g_free (known_conflict);
1780 /* Time out AVPF packets that are older than the desired length */
1781 while ((pkt = g_queue_peek_tail (src->retained_feedback)) &&
1782 GST_BUFFER_TIMESTAMP (pkt) < feedback_retention_window)
1783 gst_buffer_unref (g_queue_pop_tail (src->retained_feedback));
1787 compare_buffers (gconstpointer a, gconstpointer b, gpointer user_data)
1789 const GstBuffer *bufa = a;
1790 const GstBuffer *bufb = b;
1792 return GST_BUFFER_TIMESTAMP (bufa) - GST_BUFFER_TIMESTAMP (bufb);
1796 rtp_source_retain_rtcp_packet (RTPSource * src, GstRTCPPacket * packet,
1797 GstClockTime running_time)
1801 buffer = gst_buffer_copy_region (packet->rtcp->buffer, GST_BUFFER_COPY_MEMORY,
1802 packet->offset, (gst_rtcp_packet_get_length (packet) + 1) * 4);
1804 GST_BUFFER_TIMESTAMP (buffer) = running_time;
1806 g_queue_insert_sorted (src->retained_feedback, buffer, compare_buffers, NULL);
1810 rtp_source_has_retained (RTPSource * src, GCompareFunc func, gconstpointer data)
1812 if (g_queue_find_custom (src->retained_feedback, data, func))