2 * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 #include <gst/rtp/gstrtpbuffer.h>
22 #include <gst/rtp/gstrtcpbuffer.h>
24 #include "rtpsource.h"
26 GST_DEBUG_CATEGORY_STATIC (rtp_source_debug);
27 #define GST_CAT_DEFAULT rtp_source_debug
29 #define RTP_MAX_PROBATION_LEN 32
31 /* signals and args */
37 #define DEFAULT_SSRC 0
38 #define DEFAULT_IS_CSRC FALSE
39 #define DEFAULT_IS_VALIDATED FALSE
40 #define DEFAULT_IS_SENDER FALSE
41 #define DEFAULT_SDES NULL
55 /* GObject vmethods */
56 static void rtp_source_finalize (GObject * object);
57 static void rtp_source_set_property (GObject * object, guint prop_id,
58 const GValue * value, GParamSpec * pspec);
59 static void rtp_source_get_property (GObject * object, guint prop_id,
60 GValue * value, GParamSpec * pspec);
62 /* static guint rtp_source_signals[LAST_SIGNAL] = { 0 }; */
64 G_DEFINE_TYPE (RTPSource, rtp_source, G_TYPE_OBJECT);
67 rtp_source_class_init (RTPSourceClass * klass)
69 GObjectClass *gobject_class;
71 gobject_class = (GObjectClass *) klass;
73 gobject_class->finalize = rtp_source_finalize;
75 gobject_class->set_property = rtp_source_set_property;
76 gobject_class->get_property = rtp_source_get_property;
78 g_object_class_install_property (gobject_class, PROP_SSRC,
79 g_param_spec_uint ("ssrc", "SSRC",
80 "The SSRC of this source", 0, G_MAXUINT, DEFAULT_SSRC,
81 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
83 g_object_class_install_property (gobject_class, PROP_IS_CSRC,
84 g_param_spec_boolean ("is-csrc", "Is CSRC",
85 "If this SSRC is acting as a contributing source",
86 DEFAULT_IS_CSRC, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
88 g_object_class_install_property (gobject_class, PROP_IS_VALIDATED,
89 g_param_spec_boolean ("is-validated", "Is Validated",
90 "If this SSRC is validated", DEFAULT_IS_VALIDATED,
91 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
93 g_object_class_install_property (gobject_class, PROP_IS_SENDER,
94 g_param_spec_boolean ("is-sender", "Is Sender",
95 "If this SSRC is a sender", DEFAULT_IS_SENDER,
96 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
101 * The current SDES items of the source. Returns a structure with name
102 * application/x-rtp-source-sdes and may contain the following fields:
104 * 'cname' G_TYPE_STRING : The canonical name
105 * 'name' G_TYPE_STRING : The user name
106 * 'email' G_TYPE_STRING : The user's electronic mail address
107 * 'phone' G_TYPE_STRING : The user's phone number
108 * 'location' G_TYPE_STRING : The geographic user location
109 * 'tool' G_TYPE_STRING : The name of application or tool
110 * 'note' G_TYPE_STRING : A notice about the source
112 * other fields may be present and these represent private items in
113 * the SDES where the field name is the prefix.
115 g_object_class_install_property (gobject_class, PROP_SDES,
116 g_param_spec_boxed ("sdes", "SDES",
117 "The SDES information for this source",
118 GST_TYPE_STRUCTURE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
123 * The statistics of the source. This property returns a GstStructure with
124 * name application/x-rtp-source-stats with the following fields:
126 * "ssrc" G_TYPE_UINT The SSRC of this source
127 * "internal" G_TYPE_BOOLEAN If this source is the source of the session
128 * "validated" G_TYPE_BOOLEAN If the source is validated
129 * "received-bye" G_TYPE_BOOLEAN If we received a BYE from this source
130 * "is-csrc" G_TYPE_BOOLEAN If this source was found as CSRC
131 * "is-sender" G_TYPE_BOOLEAN If this source is a sender
132 * "seqnum-base" G_TYPE_INT first seqnum if known
133 * "clock-rate" G_TYPE_INT the clock rate of the media
135 * The following two fields are only present when known.
137 * "rtp-from" G_TYPE_STRING where we received the last RTP packet from
138 * "rtcp-from" G_TYPE_STRING where we received the last RTCP packet from
140 * The following fields make sense for internal sources and will only increase
141 * when "is-sender" is TRUE:
143 * "octets-sent" G_TYPE_UINT64 number of bytes we sent
144 * "packets-sent" G_TYPE_UINT64 number of packets we sent
146 * The following fields make sense for non-internal sources and will only
147 * increase when "is-sender" is TRUE.
149 * "octets-received" G_TYPE_UINT64 total number of bytes received
150 * "packets-received" G_TYPE_UINT64 total number of packets received
152 * Following fields are updated when "is-sender" is TRUE.
154 * "bitrate" G_TYPE_UINT64 bitrate in bits per second
155 * "jitter" G_TYPE_UINT estimated jitter
156 * "packets-lost" G_TYPE_INT estimated amount of packets lost
158 * The last SR report this source sent. This only updates when "is-sender" is
161 * "have-sr" G_TYPE_BOOLEAN the source has sent SR
162 * "sr-ntptime" G_TYPE_UINT64 ntptime of SR
163 * "sr-rtptime" G_TYPE_UINT rtptime of SR
164 * "sr-octet-count" G_TYPE_UINT the number of bytes in the SR
165 * "sr-packet-count" G_TYPE_UINT the number of packets in the SR
167 * The following fields are only present for non-internal sources and
168 * represent the content of the last RB packet that was sent to this source.
169 * These values are only updated when the source is sending.
171 * "sent-rb" G_TYPE_BOOLEAN we have sent an RB
172 * "sent-rb-fractionlost" G_TYPE_UINT calculated lost fraction
173 * "sent-rb-packetslost" G_TYPE_INT lost packets
174 * "sent-rb-exthighestseq" G_TYPE_UINT last seen seqnum
175 * "sent-rb-jitter" G_TYPE_UINT jitter
176 * "sent-rb-lsr" G_TYPE_UINT last SR time
177 * "sent-rb-dlsr" G_TYPE_UINT delay since last SR
179 * The following fields are only present for non-internal sources and
180 * represents the last RB that this source sent. This is only updated
181 * when the source is receiving data and sending RB blocks.
183 * "have-rb" G_TYPE_BOOLEAN the source has sent RB
184 * "rb-fractionlost" G_TYPE_UINT lost fraction
185 * "rb-packetslost" G_TYPE_INT lost packets
186 * "rb-exthighestseq" G_TYPE_UINT highest received seqnum
187 * "rb-jitter" G_TYPE_UINT reception jitter
188 * "rb-lsr" G_TYPE_UINT last SR time
189 * "rb-dlsr" G_TYPE_UINT delay since last SR
191 * The round trip of this source. This is calculated from the last RB
192 * values and the recption time of the last RB packet. Only present for
193 * non-internal sources.
195 * "rb-round-trip" G_TYPE_UINT the round trip time in nanoseconds
197 g_object_class_install_property (gobject_class, PROP_STATS,
198 g_param_spec_boxed ("stats", "Stats",
199 "The stats of this source", GST_TYPE_STRUCTURE,
200 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
202 GST_DEBUG_CATEGORY_INIT (rtp_source_debug, "rtpsource", 0, "RTP Source");
207 * @src: an #RTPSource
209 * Reset the stats of @src.
212 rtp_source_reset (RTPSource * src)
214 src->received_bye = FALSE;
216 src->stats.cycles = -1;
217 src->stats.jitter = 0;
218 src->stats.transit = -1;
219 src->stats.curr_sr = 0;
220 src->stats.curr_rr = 0;
224 rtp_source_init (RTPSource * src)
226 /* sources are initialy on probation until we receive enough valid RTP
227 * packets or a valid RTCP packet */
228 src->validated = FALSE;
229 src->internal = FALSE;
230 src->probation = RTP_DEFAULT_PROBATION;
231 src->closing = FALSE;
233 src->sdes = gst_structure_new_empty ("application/x-rtp-source-sdes");
236 src->clock_rate = -1;
237 src->packets = g_queue_new ();
238 src->seqnum_base = -1;
239 src->last_rtptime = -1;
241 src->retained_feedback = g_queue_new ();
243 rtp_source_reset (src);
247 rtp_conflicting_address_free (RTPConflictingAddress * addr)
249 g_object_unref (addr->address);
254 rtp_source_finalize (GObject * object)
259 src = RTP_SOURCE_CAST (object);
261 while ((buffer = g_queue_pop_head (src->packets)))
262 gst_buffer_unref (buffer);
263 g_queue_free (src->packets);
265 gst_structure_free (src->sdes);
267 g_free (src->bye_reason);
269 gst_caps_replace (&src->caps, NULL);
271 g_list_foreach (src->conflicting_addresses,
272 (GFunc) rtp_conflicting_address_free, NULL);
273 g_list_free (src->conflicting_addresses);
275 while ((buffer = g_queue_pop_head (src->retained_feedback)))
276 gst_buffer_unref (buffer);
277 g_queue_free (src->retained_feedback);
280 g_object_unref (src->rtp_from);
282 g_object_unref (src->rtcp_from);
284 G_OBJECT_CLASS (rtp_source_parent_class)->finalize (object);
287 static GstStructure *
288 rtp_source_create_stats (RTPSource * src)
291 gboolean is_sender = src->is_sender;
292 gboolean internal = src->internal;
295 guint8 fractionlost = 0;
296 gint32 packetslost = 0;
297 guint32 exthighestseq = 0;
301 guint32 round_trip = 0;
303 GstClockTime time = 0;
306 guint32 packet_count = 0;
307 guint32 octet_count = 0;
310 /* common data for all types of sources */
311 s = gst_structure_new ("application/x-rtp-source-stats",
312 "ssrc", G_TYPE_UINT, (guint) src->ssrc,
313 "internal", G_TYPE_BOOLEAN, internal,
314 "validated", G_TYPE_BOOLEAN, src->validated,
315 "received-bye", G_TYPE_BOOLEAN, src->received_bye,
316 "is-csrc", G_TYPE_BOOLEAN, src->is_csrc,
317 "is-sender", G_TYPE_BOOLEAN, is_sender,
318 "seqnum-base", G_TYPE_INT, src->seqnum_base,
319 "clock-rate", G_TYPE_INT, src->clock_rate, NULL);
321 /* add address and port */
323 address_str = __g_socket_address_to_string (src->rtp_from);
324 gst_structure_set (s, "rtp-from", G_TYPE_STRING, address_str, NULL);
325 g_free (address_str);
327 if (src->rtcp_from) {
328 address_str = __g_socket_address_to_string (src->rtcp_from);
329 gst_structure_set (s, "rtcp-from", G_TYPE_STRING, address_str, NULL);
330 g_free (address_str);
333 gst_structure_set (s,
334 "octets-sent", G_TYPE_UINT64, src->stats.octets_sent,
335 "packets-sent", G_TYPE_UINT64, src->stats.packets_sent,
336 "octets-received", G_TYPE_UINT64, src->stats.octets_received,
337 "packets-received", G_TYPE_UINT64, src->stats.packets_received,
338 "bitrate", G_TYPE_UINT64, src->bitrate,
339 "packets-lost", G_TYPE_INT,
340 (gint) rtp_stats_get_packets_lost (&src->stats), "jitter", G_TYPE_UINT,
341 (guint) (src->stats.jitter >> 4), NULL);
343 /* get the last SR. */
344 have_sr = rtp_source_get_last_sr (src, &time, &ntptime, &rtptime,
345 &packet_count, &octet_count);
346 gst_structure_set (s,
347 "have-sr", G_TYPE_BOOLEAN, have_sr,
348 "sr-ntptime", G_TYPE_UINT64, ntptime,
349 "sr-rtptime", G_TYPE_UINT, (guint) rtptime,
350 "sr-octet-count", G_TYPE_UINT, (guint) octet_count,
351 "sr-packet-count", G_TYPE_UINT, (guint) packet_count, NULL);
354 /* get the last RB we sent */
355 gst_structure_set (s,
356 "sent-rb", G_TYPE_BOOLEAN, src->last_rr.is_valid,
357 "sent-rb-fractionlost", G_TYPE_UINT, (guint) src->last_rr.fractionlost,
358 "sent-rb-packetslost", G_TYPE_INT, (gint) src->last_rr.packetslost,
359 "sent-rb-exthighestseq", G_TYPE_UINT,
360 (guint) src->last_rr.exthighestseq, "sent-rb-jitter", G_TYPE_UINT,
361 (guint) src->last_rr.jitter, "sent-rb-lsr", G_TYPE_UINT,
362 (guint) src->last_rr.lsr, "sent-rb-dlsr", G_TYPE_UINT,
363 (guint) src->last_rr.dlsr, NULL);
365 /* get the last RB */
366 have_rb = rtp_source_get_last_rb (src, &fractionlost, &packetslost,
367 &exthighestseq, &jitter, &lsr, &dlsr, &round_trip);
369 gst_structure_set (s,
370 "have-rb", G_TYPE_BOOLEAN, have_rb,
371 "rb-fractionlost", G_TYPE_UINT, (guint) fractionlost,
372 "rb-packetslost", G_TYPE_INT, (gint) packetslost,
373 "rb-exthighestseq", G_TYPE_UINT, (guint) exthighestseq,
374 "rb-jitter", G_TYPE_UINT, (guint) jitter,
375 "rb-lsr", G_TYPE_UINT, (guint) lsr,
376 "rb-dlsr", G_TYPE_UINT, (guint) dlsr,
377 "rb-round-trip", G_TYPE_UINT, (guint) round_trip, NULL);
384 * rtp_source_get_sdes_struct:
385 * @src: an #RTPSource
387 * Get the SDES from @src. See the SDES property for more details.
389 * Returns: %GstStructure of type "application/x-rtp-source-sdes". The result is
390 * valid until the SDES items of @src are modified.
393 rtp_source_get_sdes_struct (RTPSource * src)
395 g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
401 sdes_struct_compare_func (GQuark field_id, const GValue * value,
407 old = GST_STRUCTURE (user_data);
408 field = g_quark_to_string (field_id);
410 if (!gst_structure_has_field (old, field))
413 g_assert (G_VALUE_HOLDS_STRING (value));
415 return strcmp (g_value_get_string (value), gst_structure_get_string (old,
420 * rtp_source_set_sdes:
421 * @src: an #RTPSource
422 * @sdes: the SDES structure
424 * Store the @sdes in @src. @sdes must be a structure of type
425 * "application/x-rtp-source-sdes", see the SDES property for more details.
427 * This function takes ownership of @sdes.
429 * Returns: %FALSE if the SDES was unchanged.
432 rtp_source_set_sdes_struct (RTPSource * src, GstStructure * sdes)
436 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
437 g_return_val_if_fail (strcmp (gst_structure_get_name (sdes),
438 "application/x-rtp-source-sdes") == 0, FALSE);
440 changed = !gst_structure_foreach (sdes, sdes_struct_compare_func, src->sdes);
443 gst_structure_free (src->sdes);
446 gst_structure_free (sdes);
453 rtp_source_set_property (GObject * object, guint prop_id,
454 const GValue * value, GParamSpec * pspec)
458 src = RTP_SOURCE (object);
462 src->ssrc = g_value_get_uint (value);
465 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
471 rtp_source_get_property (GObject * object, guint prop_id,
472 GValue * value, GParamSpec * pspec)
476 src = RTP_SOURCE (object);
480 g_value_set_uint (value, rtp_source_get_ssrc (src));
483 g_value_set_boolean (value, rtp_source_is_as_csrc (src));
485 case PROP_IS_VALIDATED:
486 g_value_set_boolean (value, rtp_source_is_validated (src));
489 g_value_set_boolean (value, rtp_source_is_sender (src));
492 g_value_set_boxed (value, rtp_source_get_sdes_struct (src));
495 g_value_take_boxed (value, rtp_source_create_stats (src));
498 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
507 * Create a #RTPSource with @ssrc.
509 * Returns: a new #RTPSource. Use g_object_unref() after usage.
512 rtp_source_new (guint32 ssrc)
516 src = g_object_new (RTP_TYPE_SOURCE, NULL);
523 * rtp_source_set_callbacks:
524 * @src: an #RTPSource
525 * @cb: callback functions
526 * @user_data: user data
528 * Set the callbacks for the source.
531 rtp_source_set_callbacks (RTPSource * src, RTPSourceCallbacks * cb,
534 g_return_if_fail (RTP_IS_SOURCE (src));
536 src->callbacks.push_rtp = cb->push_rtp;
537 src->callbacks.clock_rate = cb->clock_rate;
538 src->user_data = user_data;
542 * rtp_source_get_ssrc:
543 * @src: an #RTPSource
545 * Get the SSRC of @source.
547 * Returns: the SSRC of src.
550 rtp_source_get_ssrc (RTPSource * src)
554 g_return_val_if_fail (RTP_IS_SOURCE (src), 0);
562 * rtp_source_set_as_csrc:
563 * @src: an #RTPSource
565 * Configure @src as a CSRC, this will also validate @src.
568 rtp_source_set_as_csrc (RTPSource * src)
570 g_return_if_fail (RTP_IS_SOURCE (src));
572 src->validated = TRUE;
577 * rtp_source_is_as_csrc:
578 * @src: an #RTPSource
580 * Check if @src is a contributing source.
582 * Returns: %TRUE if @src is acting as a contributing source.
585 rtp_source_is_as_csrc (RTPSource * src)
589 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
591 result = src->is_csrc;
597 * rtp_source_is_active:
598 * @src: an #RTPSource
600 * Check if @src is an active source. A source is active if it has been
601 * validated and has not yet received a BYE packet
603 * Returns: %TRUE if @src is an qactive source.
606 rtp_source_is_active (RTPSource * src)
610 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
612 result = RTP_SOURCE_IS_ACTIVE (src);
618 * rtp_source_is_validated:
619 * @src: an #RTPSource
621 * Check if @src is a validated source.
623 * Returns: %TRUE if @src is a validated source.
626 rtp_source_is_validated (RTPSource * src)
630 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
632 result = src->validated;
638 * rtp_source_is_sender:
639 * @src: an #RTPSource
641 * Check if @src is a sending source.
643 * Returns: %TRUE if @src is a sending source.
646 rtp_source_is_sender (RTPSource * src)
650 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
652 result = RTP_SOURCE_IS_SENDER (src);
658 * rtp_source_received_bye:
659 * @src: an #RTPSource
661 * Check if @src has receoved a BYE packet.
663 * Returns: %TRUE if @src has received a BYE packet.
666 rtp_source_received_bye (RTPSource * src)
670 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
672 result = src->received_bye;
679 * rtp_source_get_bye_reason:
680 * @src: an #RTPSource
682 * Get the BYE reason for @src. Check if the source receoved a BYE message first
683 * with rtp_source_received_bye().
685 * Returns: The BYE reason or NULL when no reason was given or the source did
686 * not receive a BYE message yet. g_fee() after usage.
689 rtp_source_get_bye_reason (RTPSource * src)
693 g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
695 result = g_strdup (src->bye_reason);
701 * rtp_source_update_caps:
702 * @src: an #RTPSource
705 * Parse @caps and store all relevant information in @source.
708 rtp_source_update_caps (RTPSource * src, GstCaps * caps)
714 /* nothing changed, return */
715 if (caps == NULL || src->caps == caps)
718 s = gst_caps_get_structure (caps, 0);
720 if (gst_structure_get_int (s, "payload", &ival))
724 GST_DEBUG ("got payload %d", src->payload);
726 if (gst_structure_get_int (s, "clock-rate", &ival))
727 src->clock_rate = ival;
729 src->clock_rate = -1;
731 GST_DEBUG ("got clock-rate %d", src->clock_rate);
733 if (gst_structure_get_uint (s, "seqnum-base", &val))
734 src->seqnum_base = val;
736 src->seqnum_base = -1;
738 GST_DEBUG ("got seqnum-base %" G_GINT32_FORMAT, src->seqnum_base);
740 gst_caps_replace (&src->caps, caps);
744 * rtp_source_set_sdes_string:
745 * @src: an #RTPSource
746 * @type: the type of the SDES item
747 * @data: the SDES data
749 * Store an SDES item of @type in @src.
751 * Returns: %FALSE if the SDES item was unchanged or @type is unknown.
754 rtp_source_set_sdes_string (RTPSource * src, GstRTCPSDESType type,
760 field = gst_rtcp_sdes_type_to_name (type);
762 if (gst_structure_has_field (src->sdes, field))
763 old = gst_structure_get_string (src->sdes, field);
767 if (old == NULL && data == NULL)
770 if (old != NULL && data != NULL && strcmp (old, data) == 0)
774 gst_structure_remove_field (src->sdes, field);
776 gst_structure_set (src->sdes, field, G_TYPE_STRING, data, NULL);
782 * rtp_source_get_sdes_string:
783 * @src: an #RTPSource
784 * @type: the type of the SDES item
786 * Get the SDES item of @type from @src.
788 * Returns: a null-terminated copy of the SDES item or NULL when @type was not
789 * valid or the SDES item was unset. g_free() after usage.
792 rtp_source_get_sdes_string (RTPSource * src, GstRTCPSDESType type)
795 const gchar *type_name;
797 g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
799 if (type < 0 || type > GST_RTCP_SDES_PRIV - 1)
802 type_name = gst_rtcp_sdes_type_to_name (type);
804 if (!gst_structure_has_field (src->sdes, type_name))
807 result = g_strdup (gst_structure_get_string (src->sdes, type_name));
813 * rtp_source_set_rtp_from:
814 * @src: an #RTPSource
815 * @address: the RTP address to set
817 * Set that @src is receiving RTP packets from @address. This is used for
818 * collistion checking.
821 rtp_source_set_rtp_from (RTPSource * src, GSocketAddress * address)
823 g_return_if_fail (RTP_IS_SOURCE (src));
826 g_object_unref (src->rtp_from);
827 src->rtp_from = G_SOCKET_ADDRESS (g_object_ref (address));
831 * rtp_source_set_rtcp_from:
832 * @src: an #RTPSource
833 * @address: the RTCP address to set
835 * Set that @src is receiving RTCP packets from @address. This is used for
836 * collistion checking.
839 rtp_source_set_rtcp_from (RTPSource * src, GSocketAddress * address)
841 g_return_if_fail (RTP_IS_SOURCE (src));
844 g_object_unref (src->rtcp_from);
845 src->rtcp_from = G_SOCKET_ADDRESS (g_object_ref (address));
849 push_packet (RTPSource * src, GstBuffer * buffer)
851 GstFlowReturn ret = GST_FLOW_OK;
853 /* push queued packets first if any */
854 while (!g_queue_is_empty (src->packets)) {
855 GstBuffer *buffer = GST_BUFFER_CAST (g_queue_pop_head (src->packets));
857 GST_LOG ("pushing queued packet");
858 if (src->callbacks.push_rtp)
859 src->callbacks.push_rtp (src, buffer, src->user_data);
861 gst_buffer_unref (buffer);
863 GST_LOG ("pushing new packet");
865 if (src->callbacks.push_rtp)
866 ret = src->callbacks.push_rtp (src, buffer, src->user_data);
868 gst_buffer_unref (buffer);
874 get_clock_rate (RTPSource * src, guint8 payload)
876 if (src->payload == -1) {
877 /* first payload received, nothing was in the caps, lock on to this payload */
878 src->payload = payload;
879 GST_DEBUG ("first payload %d", payload);
880 } else if (payload != src->payload) {
881 /* we have a different payload than before, reset the clock-rate */
882 GST_DEBUG ("new payload %d", payload);
883 src->payload = payload;
884 src->clock_rate = -1;
885 src->stats.transit = -1;
888 if (src->clock_rate == -1) {
889 gint clock_rate = -1;
891 if (src->callbacks.clock_rate)
892 clock_rate = src->callbacks.clock_rate (src, payload, src->user_data);
894 GST_DEBUG ("got clock-rate %d", clock_rate);
896 src->clock_rate = clock_rate;
898 return src->clock_rate;
901 /* Jitter is the variation in the delay of received packets in a flow. It is
902 * measured by comparing the interval when RTP packets were sent to the interval
903 * at which they were received. For instance, if packet #1 and packet #2 leave
904 * 50 milliseconds apart and arrive 60 milliseconds apart, then the jitter is 10
907 calculate_jitter (RTPSource * src, GstBuffer * buffer,
908 RTPArrivalStats * arrival)
910 GstClockTime running_time;
911 guint32 rtparrival, transit, rtptime;
915 GstRTPBuffer rtp = { NULL };
917 /* get arrival time */
918 if ((running_time = arrival->running_time) == GST_CLOCK_TIME_NONE)
921 gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp);
922 pt = gst_rtp_buffer_get_payload_type (&rtp);
924 GST_LOG ("SSRC %08x got payload %d", src->ssrc, pt);
927 if ((clock_rate = get_clock_rate (src, pt)) == -1) {
928 gst_rtp_buffer_unmap (&rtp);
932 rtptime = gst_rtp_buffer_get_timestamp (&rtp);
934 /* convert arrival time to RTP timestamp units, truncate to 32 bits, we don't
935 * care about the absolute value, just the difference. */
936 rtparrival = gst_util_uint64_scale_int (running_time, clock_rate, GST_SECOND);
938 /* transit time is difference with RTP timestamp */
939 transit = rtparrival - rtptime;
941 /* get ABS diff with previous transit time */
942 if (src->stats.transit != -1) {
943 if (transit > src->stats.transit)
944 diff = transit - src->stats.transit;
946 diff = src->stats.transit - transit;
950 src->stats.transit = transit;
952 /* update jitter, the value we store is scaled up so we can keep precision. */
953 src->stats.jitter += diff - ((src->stats.jitter + 8) >> 4);
955 src->stats.prev_rtptime = src->stats.last_rtptime;
956 src->stats.last_rtptime = rtparrival;
958 GST_LOG ("rtparrival %u, rtptime %u, clock-rate %d, diff %d, jitter: %f",
959 rtparrival, rtptime, clock_rate, diff, (src->stats.jitter) / 16.0);
961 gst_rtp_buffer_unmap (&rtp);
967 GST_WARNING ("cannot get current running_time");
972 GST_WARNING ("cannot get clock-rate for pt %d", pt);
978 init_seq (RTPSource * src, guint16 seq)
980 src->stats.base_seq = seq;
981 src->stats.max_seq = seq;
982 src->stats.bad_seq = RTP_SEQ_MOD + 1; /* so seq == bad_seq is false */
983 src->stats.cycles = 0;
984 src->stats.packets_received = 0;
985 src->stats.octets_received = 0;
986 src->stats.bytes_received = 0;
987 src->stats.prev_received = 0;
988 src->stats.prev_expected = 0;
990 GST_DEBUG ("base_seq %d", seq);
993 #define BITRATE_INTERVAL (2 * GST_SECOND)
996 do_bitrate_estimation (RTPSource * src, GstClockTime running_time,
997 guint64 * bytes_handled)
1001 if (src->prev_rtime) {
1002 elapsed = running_time - src->prev_rtime;
1004 if (elapsed > BITRATE_INTERVAL) {
1007 rate = gst_util_uint64_scale (*bytes_handled, 8 * GST_SECOND, elapsed);
1009 GST_LOG ("Elapsed %" G_GUINT64_FORMAT ", bytes %" G_GUINT64_FORMAT
1010 ", rate %" G_GUINT64_FORMAT, elapsed, *bytes_handled, rate);
1012 if (src->bitrate == 0)
1013 src->bitrate = rate;
1015 src->bitrate = ((src->bitrate * 3) + rate) / 4;
1017 src->prev_rtime = running_time;
1021 GST_LOG ("Reset bitrate measurement");
1022 src->prev_rtime = running_time;
1028 * rtp_source_process_rtp:
1029 * @src: an #RTPSource
1030 * @buffer: an RTP buffer
1032 * Let @src handle the incomming RTP @buffer.
1034 * Returns: a #GstFlowReturn.
1037 rtp_source_process_rtp (RTPSource * src, GstBuffer * buffer,
1038 RTPArrivalStats * arrival)
1040 GstFlowReturn result = GST_FLOW_OK;
1041 guint16 seqnr, udelta;
1042 RTPSourceStats *stats;
1044 GstRTPBuffer rtp = { NULL };
1046 g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
1047 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1049 stats = &src->stats;
1051 gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp);
1052 seqnr = gst_rtp_buffer_get_seq (&rtp);
1053 gst_rtp_buffer_unmap (&rtp);
1055 if (stats->cycles == -1) {
1056 GST_DEBUG ("received first buffer");
1057 /* first time we heard of this source */
1058 init_seq (src, seqnr);
1059 src->stats.max_seq = seqnr - 1;
1060 src->probation = RTP_DEFAULT_PROBATION;
1063 udelta = seqnr - stats->max_seq;
1065 /* if we are still on probation, check seqnum */
1066 if (src->probation) {
1067 expected = src->stats.max_seq + 1;
1069 /* when in probation, we require consecutive seqnums */
1070 if (seqnr == expected) {
1071 /* expected packet */
1072 GST_DEBUG ("probation: seqnr %d == expected %d", seqnr, expected);
1074 src->stats.max_seq = seqnr;
1075 if (src->probation == 0) {
1076 GST_DEBUG ("probation done!");
1077 init_seq (src, seqnr);
1081 GST_DEBUG ("probation %d: queue buffer", src->probation);
1082 /* when still in probation, keep packets in a list. */
1083 g_queue_push_tail (src->packets, buffer);
1084 /* remove packets from queue if there are too many */
1085 while (g_queue_get_length (src->packets) > RTP_MAX_PROBATION_LEN) {
1086 q = g_queue_pop_head (src->packets);
1087 gst_buffer_unref (q);
1092 /* unexpected seqnum in probation */
1093 goto probation_seqnum;
1095 } else if (udelta < RTP_MAX_DROPOUT) {
1096 /* in order, with permissible gap */
1097 if (seqnr < stats->max_seq) {
1098 /* sequence number wrapped - count another 64K cycle. */
1099 stats->cycles += RTP_SEQ_MOD;
1101 stats->max_seq = seqnr;
1102 } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
1103 /* the sequence number made a very large jump */
1104 if (seqnr == stats->bad_seq) {
1105 /* two sequential packets -- assume that the other side
1106 * restarted without telling us so just re-sync
1107 * (i.e., pretend this was the first packet). */
1108 init_seq (src, seqnr);
1110 /* unacceptable jump */
1111 stats->bad_seq = (seqnr + 1) & (RTP_SEQ_MOD - 1);
1115 /* duplicate or reordered packet, will be filtered by jitterbuffer. */
1116 GST_WARNING ("duplicate or reordered packet");
1119 src->stats.octets_received += arrival->payload_len;
1120 src->stats.bytes_received += arrival->bytes;
1121 src->stats.packets_received++;
1122 /* for the bitrate estimation */
1123 src->bytes_received += arrival->payload_len;
1124 /* the source that sent the packet must be a sender */
1125 src->is_sender = TRUE;
1126 src->validated = TRUE;
1128 do_bitrate_estimation (src, arrival->running_time, &src->bytes_received);
1130 GST_LOG ("seq %d, PC: %" G_GUINT64_FORMAT ", OC: %" G_GUINT64_FORMAT,
1131 seqnr, src->stats.packets_received, src->stats.octets_received);
1133 /* calculate jitter for the stats */
1134 calculate_jitter (src, buffer, arrival);
1136 /* we're ready to push the RTP packet now */
1137 result = push_packet (src, buffer);
1145 GST_WARNING ("unacceptable seqnum received");
1146 gst_buffer_unref (buffer);
1151 GST_WARNING ("probation: seqnr %d != expected %d", seqnr, expected);
1152 src->probation = RTP_DEFAULT_PROBATION;
1153 src->stats.max_seq = seqnr;
1154 gst_buffer_unref (buffer);
1160 * rtp_source_process_bye:
1161 * @src: an #RTPSource
1162 * @reason: the reason for leaving
1164 * Notify @src that a BYE packet has been received. This will make the source
1168 rtp_source_process_bye (RTPSource * src, const gchar * reason)
1170 g_return_if_fail (RTP_IS_SOURCE (src));
1172 GST_DEBUG ("marking SSRC %08x as BYE, reason: %s", src->ssrc,
1173 GST_STR_NULL (reason));
1175 /* copy the reason and mark as received_bye */
1176 g_free (src->bye_reason);
1177 src->bye_reason = g_strdup (reason);
1178 src->received_bye = TRUE;
1182 set_ssrc (GstBuffer ** buffer, guint idx, RTPSource * src)
1184 GstRTPBuffer rtp = { NULL };
1186 *buffer = gst_buffer_make_writable (*buffer);
1187 gst_rtp_buffer_map (*buffer, GST_MAP_WRITE, &rtp);
1188 gst_rtp_buffer_set_ssrc (&rtp, src->ssrc);
1189 gst_rtp_buffer_unmap (&rtp);
1194 * rtp_source_send_rtp:
1195 * @src: an #RTPSource
1196 * @data: an RTP buffer or a list of RTP buffers
1197 * @is_list: if @data is a buffer or list
1198 * @running_time: the running time of @data
1200 * Send @data (an RTP buffer or list of buffers) originating from @src.
1201 * This will make @src a sender. This function takes ownership of @data and
1202 * modifies the SSRC in the RTP packet to that of @src when needed.
1204 * Returns: a #GstFlowReturn.
1207 rtp_source_send_rtp (RTPSource * src, gpointer data, gboolean is_list,
1208 GstClockTime running_time)
1210 GstFlowReturn result;
1213 guint64 ext_rtptime;
1214 guint64 rt_diff, rtp_diff;
1215 GstBufferList *list = NULL;
1216 GstBuffer *buffer = NULL;
1219 GstRTPBuffer rtp = { NULL };
1221 g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
1222 g_return_val_if_fail (is_list || GST_IS_BUFFER (data), GST_FLOW_ERROR);
1225 list = GST_BUFFER_LIST_CAST (data);
1227 /* We can grab the caps from the first group, since all
1228 * groups of a buffer list have same caps. */
1229 buffer = gst_buffer_list_get (list, 0);
1233 buffer = GST_BUFFER_CAST (data);
1236 /* we are a sender now */
1237 src->is_sender = TRUE;
1242 /* Each group makes up a network packet. */
1243 packets = gst_buffer_list_length (list);
1244 for (i = 0, len = 0; i < packets; i++) {
1245 gst_rtp_buffer_map (gst_buffer_list_get (list, i), GST_MAP_READ, &rtp);
1246 len += gst_rtp_buffer_get_payload_len (&rtp);
1247 gst_rtp_buffer_unmap (&rtp);
1249 /* subsequent info taken from first list member */
1250 gst_rtp_buffer_map (gst_buffer_list_get (list, 0), GST_MAP_READ, &rtp);
1253 gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp);
1254 len = gst_rtp_buffer_get_payload_len (&rtp);
1257 /* update stats for the SR */
1258 src->stats.packets_sent += packets;
1259 src->stats.octets_sent += len;
1260 src->bytes_sent += len;
1262 do_bitrate_estimation (src, running_time, &src->bytes_sent);
1264 rtptime = gst_rtp_buffer_get_timestamp (&rtp);
1265 ext_rtptime = src->last_rtptime;
1266 ext_rtptime = gst_rtp_buffer_ext_timestamp (&ext_rtptime, rtptime);
1268 GST_LOG ("SSRC %08x, RTP %" G_GUINT64_FORMAT ", running_time %"
1269 GST_TIME_FORMAT, src->ssrc, ext_rtptime, GST_TIME_ARGS (running_time));
1271 if (ext_rtptime > src->last_rtptime) {
1272 rtp_diff = ext_rtptime - src->last_rtptime;
1273 rt_diff = running_time - src->last_rtime;
1275 /* calc the diff so we can detect drift at the sender. This can also be used
1276 * to guestimate the clock rate if the NTP time is locked to the RTP
1277 * timestamps (as is the case when the capture device is providing the clock). */
1278 GST_LOG ("SSRC %08x, diff RTP %" G_GUINT64_FORMAT ", diff running_time %"
1279 GST_TIME_FORMAT, src->ssrc, rtp_diff, GST_TIME_ARGS (rt_diff));
1282 /* we keep track of the last received RTP timestamp and the corresponding
1283 * buffer running_time so that we can use this info when constructing SR reports */
1284 src->last_rtime = running_time;
1285 src->last_rtptime = ext_rtptime;
1288 if (!src->callbacks.push_rtp) {
1289 gst_rtp_buffer_unmap (&rtp);
1293 ssrc = gst_rtp_buffer_get_ssrc (&rtp);
1294 gst_rtp_buffer_unmap (&rtp);
1296 if (ssrc != src->ssrc) {
1297 /* the SSRC of the packet is not correct, make a writable buffer and
1298 * update the SSRC. This could involve a complete copy of the packet when
1299 * it is not writable. Usually the payloader will use caps negotiation to
1300 * get the correct SSRC from the session manager before pushing anything. */
1302 /* FIXME, we don't want to warn yet because we can't inform any payloader
1303 * of the changes SSRC yet because we don't implement pad-alloc. */
1304 GST_LOG ("updating SSRC from %08x to %08x, fix the payloader", ssrc,
1308 list = gst_buffer_list_make_writable (list);
1309 gst_buffer_list_foreach (list, (GstBufferListFunc) set_ssrc, src);
1311 set_ssrc (&buffer, 0, src);
1314 GST_LOG ("pushing RTP %s %" G_GUINT64_FORMAT, is_list ? "list" : "packet",
1315 src->stats.packets_sent);
1317 result = src->callbacks.push_rtp (src, data, src->user_data);
1324 GST_WARNING ("no buffers in buffer list");
1325 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1330 GST_WARNING ("no callback installed, dropping packet");
1331 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1337 * rtp_source_process_sr:
1338 * @src: an #RTPSource
1339 * @time: time of packet arrival
1340 * @ntptime: the NTP time in 32.32 fixed point
1341 * @rtptime: the RTP time
1342 * @packet_count: the packet count
1343 * @octet_count: the octect count
1345 * Update the sender report in @src.
1348 rtp_source_process_sr (RTPSource * src, GstClockTime time, guint64 ntptime,
1349 guint32 rtptime, guint32 packet_count, guint32 octet_count)
1351 RTPSenderReport *curr;
1354 g_return_if_fail (RTP_IS_SOURCE (src));
1356 GST_DEBUG ("got SR packet: SSRC %08x, NTP %08x:%08x, RTP %" G_GUINT32_FORMAT
1357 ", PC %" G_GUINT32_FORMAT ", OC %" G_GUINT32_FORMAT, src->ssrc,
1358 (guint32) (ntptime >> 32), (guint32) (ntptime & 0xffffffff), rtptime,
1359 packet_count, octet_count);
1361 curridx = src->stats.curr_sr ^ 1;
1362 curr = &src->stats.sr[curridx];
1364 /* this is a sender now */
1365 src->is_sender = TRUE;
1367 /* update current */
1368 curr->is_valid = TRUE;
1369 curr->ntptime = ntptime;
1370 curr->rtptime = rtptime;
1371 curr->packet_count = packet_count;
1372 curr->octet_count = octet_count;
1376 src->stats.curr_sr = curridx;
1378 src->stats.prev_rtcptime = src->stats.last_rtcptime;
1379 src->stats.last_rtcptime = time;
1383 * rtp_source_process_rb:
1384 * @src: an #RTPSource
1385 * @ntpnstime: the current time in nanoseconds since 1970
1386 * @fractionlost: fraction lost since last SR/RR
1387 * @packetslost: the cumululative number of packets lost
1388 * @exthighestseq: the extended last sequence number received
1389 * @jitter: the interarrival jitter
1390 * @lsr: the last SR packet from this source
1391 * @dlsr: the delay since last SR packet
1393 * Update the report block in @src.
1396 rtp_source_process_rb (RTPSource * src, guint64 ntpnstime,
1397 guint8 fractionlost, gint32 packetslost, guint32 exthighestseq,
1398 guint32 jitter, guint32 lsr, guint32 dlsr)
1400 RTPReceiverReport *curr;
1405 g_return_if_fail (RTP_IS_SOURCE (src));
1407 GST_DEBUG ("got RB packet: SSRC %08x, FL %2x, PL %d, HS %" G_GUINT32_FORMAT
1408 ", jitter %" G_GUINT32_FORMAT ", LSR %04x:%04x, DLSR %04x:%04x",
1409 src->ssrc, fractionlost, packetslost, exthighestseq, jitter, lsr >> 16,
1410 lsr & 0xffff, dlsr >> 16, dlsr & 0xffff);
1412 curridx = src->stats.curr_rr ^ 1;
1413 curr = &src->stats.rr[curridx];
1415 /* update current */
1416 curr->is_valid = TRUE;
1417 curr->fractionlost = fractionlost;
1418 curr->packetslost = packetslost;
1419 curr->exthighestseq = exthighestseq;
1420 curr->jitter = jitter;
1424 /* convert the NTP time in nanoseconds to 32.32 fixed point */
1425 f_ntp = gst_util_uint64_scale (ntpnstime, (1LL << 32), GST_SECOND);
1426 /* calculate round trip, round the time up */
1427 ntp = ((f_ntp + 0xffff) >> 16) & 0xffffffff;
1430 if (A > 0 && ntp > A)
1434 curr->round_trip = A;
1436 GST_DEBUG ("NTP %04x:%04x, round trip %04x:%04x", ntp >> 16, ntp & 0xffff,
1437 A >> 16, A & 0xffff);
1440 src->stats.curr_rr = curridx;
1444 * rtp_source_get_new_sr:
1445 * @src: an #RTPSource
1446 * @ntpnstime: the current time in nanoseconds since 1970
1447 * @running_time: the current running_time of the pipeline.
1448 * @ntptime: the NTP time in 32.32 fixed point
1449 * @rtptime: the RTP time corresponding to @ntptime
1450 * @packet_count: the packet count
1451 * @octet_count: the octect count
1453 * Get new values to put into a new SR report from this source.
1455 * @running_time and @ntpnstime are captured at the same time and represent the
1456 * running time of the pipeline clock and the absolute current system time in
1457 * nanoseconds respectively. Together with the last running_time and rtp timestamp
1458 * we have observed in the source, we can generate @ntptime and @rtptime for an SR
1459 * packet. @ntptime is basically the fixed point representation of @ntpnstime
1460 * and @rtptime the associated RTP timestamp.
1462 * Returns: %TRUE on success.
1465 rtp_source_get_new_sr (RTPSource * src, guint64 ntpnstime,
1466 GstClockTime running_time, guint64 * ntptime, guint32 * rtptime,
1467 guint32 * packet_count, guint32 * octet_count)
1470 guint64 t_current_ntp;
1471 GstClockTimeDiff diff;
1473 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1475 /* We last saw a buffer with last_rtptime at last_rtime. Given a running_time
1476 * and an NTP time, we can scale the RTP timestamps so that they match the
1477 * given NTP time. for scaling, we assume that the slope of the rtptime vs
1478 * running_time vs ntptime curve is close to 1, which is certainly
1479 * sufficient for the frequency at which we report SR and the rate we send
1480 * out RTP packets. */
1481 t_rtp = src->last_rtptime;
1483 GST_DEBUG ("last_rtime %" GST_TIME_FORMAT ", last_rtptime %"
1484 G_GUINT64_FORMAT, GST_TIME_ARGS (src->last_rtime), t_rtp);
1486 if (src->clock_rate != -1) {
1487 /* get the diff between the clock running_time and the buffer running_time.
1488 * This is the elapsed time, as measured against the pipeline clock, between
1489 * when the rtp timestamp was observed and the current running_time.
1491 * We need to apply this diff to the RTP timestamp to get the RTP timestamp
1492 * for the given ntpnstime. */
1493 diff = GST_CLOCK_DIFF (src->last_rtime, running_time);
1495 /* now translate the diff to RTP time, handle positive and negative cases.
1496 * If there is no diff, we already set rtptime correctly above. */
1498 GST_DEBUG ("running_time %" GST_TIME_FORMAT ", diff %" GST_TIME_FORMAT,
1499 GST_TIME_ARGS (running_time), GST_TIME_ARGS (diff));
1500 t_rtp += gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1503 GST_DEBUG ("running_time %" GST_TIME_FORMAT ", diff -%" GST_TIME_FORMAT,
1504 GST_TIME_ARGS (running_time), GST_TIME_ARGS (diff));
1505 t_rtp -= gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1508 GST_WARNING ("no clock-rate, cannot interpolate rtp time");
1511 /* convert the NTP time in nanoseconds to 32.32 fixed point */
1512 t_current_ntp = gst_util_uint64_scale (ntpnstime, (1LL << 32), GST_SECOND);
1514 GST_DEBUG ("NTP %08x:%08x, RTP %" G_GUINT32_FORMAT,
1515 (guint32) (t_current_ntp >> 32), (guint32) (t_current_ntp & 0xffffffff),
1519 *ntptime = t_current_ntp;
1523 *packet_count = src->stats.packets_sent;
1525 *octet_count = src->stats.octets_sent;
1531 * rtp_source_get_new_rb:
1532 * @src: an #RTPSource
1533 * @time: the current time of the system clock
1534 * @fractionlost: fraction lost since last SR/RR
1535 * @packetslost: the cumululative number of packets lost
1536 * @exthighestseq: the extended last sequence number received
1537 * @jitter: the interarrival jitter
1538 * @lsr: the last SR packet from this source
1539 * @dlsr: the delay since last SR packet
1541 * Get new values to put into a new report block from this source.
1543 * Returns: %TRUE on success.
1546 rtp_source_get_new_rb (RTPSource * src, GstClockTime time,
1547 guint8 * fractionlost, gint32 * packetslost, guint32 * exthighestseq,
1548 guint32 * jitter, guint32 * lsr, guint32 * dlsr)
1550 RTPSourceStats *stats;
1551 guint64 extended_max, expected;
1552 guint64 expected_interval, received_interval, ntptime;
1553 gint64 lost, lost_interval;
1554 guint32 fraction, LSR, DLSR;
1555 GstClockTime sr_time;
1557 stats = &src->stats;
1559 extended_max = stats->cycles + stats->max_seq;
1560 expected = extended_max - stats->base_seq + 1;
1562 GST_DEBUG ("ext_max %" G_GUINT64_FORMAT ", expected %" G_GUINT64_FORMAT
1563 ", received %" G_GUINT64_FORMAT ", base_seq %" G_GUINT32_FORMAT,
1564 extended_max, expected, stats->packets_received, stats->base_seq);
1566 lost = expected - stats->packets_received;
1567 lost = CLAMP (lost, -0x800000, 0x7fffff);
1569 expected_interval = expected - stats->prev_expected;
1570 stats->prev_expected = expected;
1571 received_interval = stats->packets_received - stats->prev_received;
1572 stats->prev_received = stats->packets_received;
1574 lost_interval = expected_interval - received_interval;
1576 if (expected_interval == 0 || lost_interval <= 0)
1579 fraction = (lost_interval << 8) / expected_interval;
1581 GST_DEBUG ("add RR for SSRC %08x", src->ssrc);
1582 /* we scaled the jitter up for additional precision */
1583 GST_DEBUG ("fraction %" G_GUINT32_FORMAT ", lost %" G_GINT64_FORMAT
1584 ", extseq %" G_GUINT64_FORMAT ", jitter %d", fraction, lost,
1585 extended_max, stats->jitter >> 4);
1587 if (rtp_source_get_last_sr (src, &sr_time, &ntptime, NULL, NULL, NULL)) {
1590 /* LSR is middle 32 bits of the last ntptime */
1591 LSR = (ntptime >> 16) & 0xffffffff;
1592 diff = time - sr_time;
1593 GST_DEBUG ("last SR time diff %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
1594 /* DLSR, delay since last SR is expressed in 1/65536 second units */
1595 DLSR = gst_util_uint64_scale_int (diff, 65536, GST_SECOND);
1597 /* No valid SR received, LSR/DLSR are set to 0 then */
1598 GST_DEBUG ("no valid SR received");
1602 GST_DEBUG ("LSR %04x:%04x, DLSR %04x:%04x", LSR >> 16, LSR & 0xffff,
1603 DLSR >> 16, DLSR & 0xffff);
1606 *fractionlost = fraction;
1608 *packetslost = lost;
1610 *exthighestseq = extended_max;
1612 *jitter = stats->jitter >> 4;
1622 * rtp_source_get_last_sr:
1623 * @src: an #RTPSource
1624 * @time: time of packet arrival
1625 * @ntptime: the NTP time in 32.32 fixed point
1626 * @rtptime: the RTP time
1627 * @packet_count: the packet count
1628 * @octet_count: the octect count
1630 * Get the values of the last sender report as set with rtp_source_process_sr().
1632 * Returns: %TRUE if there was a valid SR report.
1635 rtp_source_get_last_sr (RTPSource * src, GstClockTime * time, guint64 * ntptime,
1636 guint32 * rtptime, guint32 * packet_count, guint32 * octet_count)
1638 RTPSenderReport *curr;
1640 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1642 curr = &src->stats.sr[src->stats.curr_sr];
1643 if (!curr->is_valid)
1647 *ntptime = curr->ntptime;
1649 *rtptime = curr->rtptime;
1651 *packet_count = curr->packet_count;
1653 *octet_count = curr->octet_count;
1661 * rtp_source_get_last_rb:
1662 * @src: an #RTPSource
1663 * @fractionlost: fraction lost since last SR/RR
1664 * @packetslost: the cumululative number of packets lost
1665 * @exthighestseq: the extended last sequence number received
1666 * @jitter: the interarrival jitter
1667 * @lsr: the last SR packet from this source
1668 * @dlsr: the delay since last SR packet
1669 * @round_trip: the round trip time
1671 * Get the values of the last RB report set with rtp_source_process_rb().
1673 * Returns: %TRUE if there was a valid SB report.
1676 rtp_source_get_last_rb (RTPSource * src, guint8 * fractionlost,
1677 gint32 * packetslost, guint32 * exthighestseq, guint32 * jitter,
1678 guint32 * lsr, guint32 * dlsr, guint32 * round_trip)
1680 RTPReceiverReport *curr;
1682 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1684 curr = &src->stats.rr[src->stats.curr_rr];
1685 if (!curr->is_valid)
1689 *fractionlost = curr->fractionlost;
1691 *packetslost = curr->packetslost;
1693 *exthighestseq = curr->exthighestseq;
1695 *jitter = curr->jitter;
1701 *round_trip = curr->round_trip;
1707 * rtp_source_find_conflicting_address:
1708 * @src: The source the packet came in
1709 * @address: address to check for
1710 * @time: The time when the packet that is possibly in conflict arrived
1712 * Checks if an address which has a conflict is already known. If it is
1713 * a known conflict, remember the time
1715 * Returns: TRUE if it was a known conflict, FALSE otherwise
1718 rtp_source_find_conflicting_address (RTPSource * src, GSocketAddress * address,
1723 for (item = g_list_first (src->conflicting_addresses);
1724 item; item = g_list_next (item)) {
1725 RTPConflictingAddress *known_conflict = item->data;
1727 if (__g_socket_address_equal (address, known_conflict->address)) {
1728 known_conflict->time = time;
1737 * rtp_source_add_conflicting_address:
1738 * @src: The source the packet came in
1739 * @address: address to remember
1740 * @time: The time when the packet that is in conflict arrived
1742 * Adds a new conflict address
1745 rtp_source_add_conflicting_address (RTPSource * src,
1746 GSocketAddress * address, GstClockTime time)
1748 RTPConflictingAddress *new_conflict;
1750 new_conflict = g_new0 (RTPConflictingAddress, 1);
1752 new_conflict->address = G_SOCKET_ADDRESS (g_object_ref (address));
1753 new_conflict->time = time;
1755 src->conflicting_addresses = g_list_prepend (src->conflicting_addresses,
1760 * rtp_source_timeout:
1761 * @src: The #RTPSource
1762 * @current_time: The current time
1763 * @collision_timeout: The amount of time after which a collision is timed out
1764 * @feedback_retention_window: The running time before which retained feedback
1765 * packets have to be discarded
1767 * This is processed on each RTCP interval. It times out old collisions.
1768 * It also times out old retained feedback packets
1771 rtp_source_timeout (RTPSource * src, GstClockTime current_time,
1772 GstClockTime collision_timeout, GstClockTime feedback_retention_window)
1777 item = g_list_first (src->conflicting_addresses);
1779 RTPConflictingAddress *known_conflict = item->data;
1780 GList *next_item = g_list_next (item);
1782 if (known_conflict->time < current_time - collision_timeout) {
1785 src->conflicting_addresses =
1786 g_list_delete_link (src->conflicting_addresses, item);
1787 buf = __g_socket_address_to_string (known_conflict->address);
1788 GST_DEBUG ("collision %p timed out: %s", known_conflict, buf);
1790 g_object_unref (known_conflict->address);
1791 g_free (known_conflict);
1796 /* Time out AVPF packets that are older than the desired length */
1797 while ((pkt = g_queue_peek_tail (src->retained_feedback)) &&
1798 GST_BUFFER_TIMESTAMP (pkt) < feedback_retention_window)
1799 gst_buffer_unref (g_queue_pop_tail (src->retained_feedback));
1803 compare_buffers (gconstpointer a, gconstpointer b, gpointer user_data)
1805 const GstBuffer *bufa = a;
1806 const GstBuffer *bufb = b;
1808 return GST_BUFFER_TIMESTAMP (bufa) - GST_BUFFER_TIMESTAMP (bufb);
1812 rtp_source_retain_rtcp_packet (RTPSource * src, GstRTCPPacket * packet,
1813 GstClockTime running_time)
1817 buffer = gst_buffer_copy_region (packet->rtcp->buffer, GST_BUFFER_COPY_MEMORY,
1818 packet->offset, (gst_rtcp_packet_get_length (packet) + 1) * 4);
1820 GST_BUFFER_TIMESTAMP (buffer) = running_time;
1822 g_queue_insert_sorted (src->retained_feedback, buffer, compare_buffers, NULL);
1826 rtp_source_has_retained (RTPSource * src, GCompareFunc func, gconstpointer data)
1828 if (g_queue_find_custom (src->retained_feedback, data, func))