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
56 /* GObject vmethods */
57 static void rtp_source_finalize (GObject * object);
58 static void rtp_source_set_property (GObject * object, guint prop_id,
59 const GValue * value, GParamSpec * pspec);
60 static void rtp_source_get_property (GObject * object, guint prop_id,
61 GValue * value, GParamSpec * pspec);
63 /* static guint rtp_source_signals[LAST_SIGNAL] = { 0 }; */
65 G_DEFINE_TYPE (RTPSource, rtp_source, G_TYPE_OBJECT);
68 rtp_source_class_init (RTPSourceClass * klass)
70 GObjectClass *gobject_class;
72 gobject_class = (GObjectClass *) klass;
74 gobject_class->finalize = rtp_source_finalize;
76 gobject_class->set_property = rtp_source_set_property;
77 gobject_class->get_property = rtp_source_get_property;
79 g_object_class_install_property (gobject_class, PROP_SSRC,
80 g_param_spec_uint ("ssrc", "SSRC",
81 "The SSRC of this source", 0, G_MAXUINT, DEFAULT_SSRC,
82 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
84 g_object_class_install_property (gobject_class, PROP_IS_CSRC,
85 g_param_spec_boolean ("is-csrc", "Is CSRC",
86 "If this SSRC is acting as a contributing source",
87 DEFAULT_IS_CSRC, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
89 g_object_class_install_property (gobject_class, PROP_IS_VALIDATED,
90 g_param_spec_boolean ("is-validated", "Is Validated",
91 "If this SSRC is validated", DEFAULT_IS_VALIDATED,
92 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
94 g_object_class_install_property (gobject_class, PROP_IS_SENDER,
95 g_param_spec_boolean ("is-sender", "Is Sender",
96 "If this SSRC is a sender", DEFAULT_IS_SENDER,
97 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
102 * The current SDES items of the source. Returns a structure with name
103 * application/x-rtp-source-sdes and may contain the following fields:
105 * 'cname' G_TYPE_STRING : The canonical name
106 * 'name' G_TYPE_STRING : The user name
107 * 'email' G_TYPE_STRING : The user's electronic mail address
108 * 'phone' G_TYPE_STRING : The user's phone number
109 * 'location' G_TYPE_STRING : The geographic user location
110 * 'tool' G_TYPE_STRING : The name of application or tool
111 * 'note' G_TYPE_STRING : A notice about the source
113 * other fields may be present and these represent private items in
114 * the SDES where the field name is the prefix.
116 g_object_class_install_property (gobject_class, PROP_SDES,
117 g_param_spec_boxed ("sdes", "SDES",
118 "The SDES information for this source",
119 GST_TYPE_STRUCTURE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
124 * The statistics of the source. This property returns a GstStructure with
125 * name application/x-rtp-source-stats with the following fields:
127 * "ssrc" G_TYPE_UINT The SSRC of this source
128 * "internal" G_TYPE_BOOLEAN If this source is a source of the session
129 * "validated" G_TYPE_BOOLEAN If the source is validated
130 * "received-bye" G_TYPE_BOOLEAN If we received a BYE from this source
131 * "is-csrc" G_TYPE_BOOLEAN If this source was found as CSRC
132 * "is-sender" G_TYPE_BOOLEAN If this source is a sender
133 * "seqnum-base" G_TYPE_INT first seqnum if known
134 * "clock-rate" G_TYPE_INT the clock rate of the media
136 * The following two fields are only present when known.
138 * "rtp-from" G_TYPE_STRING where we received the last RTP packet from
139 * "rtcp-from" G_TYPE_STRING where we received the last RTCP packet from
141 * The following fields make sense for internal sources and will only increase
142 * when "is-sender" is TRUE:
144 * "octets-sent" G_TYPE_UINT64 number of bytes we sent
145 * "packets-sent" G_TYPE_UINT64 number of packets we sent
147 * The following fields make sense for non-internal sources and will only
148 * increase when "is-sender" is TRUE.
150 * "octets-received" G_TYPE_UINT64 total number of bytes received
151 * "packets-received" G_TYPE_UINT64 total number of packets received
153 * Following fields are updated when "is-sender" is TRUE.
155 * "bitrate" G_TYPE_UINT64 bitrate in bits per second
156 * "jitter" G_TYPE_UINT estimated jitter
157 * "packets-lost" G_TYPE_INT estimated amount of packets lost
159 * The last SR report this source sent. This only updates when "is-sender" is
162 * "have-sr" G_TYPE_BOOLEAN the source has sent SR
163 * "sr-ntptime" G_TYPE_UINT64 ntptime of SR
164 * "sr-rtptime" G_TYPE_UINT rtptime of SR
165 * "sr-octet-count" G_TYPE_UINT the number of bytes in the SR
166 * "sr-packet-count" G_TYPE_UINT the number of packets in the SR
168 * The following fields are only present for non-internal sources and
169 * represent the content of the last RB packet that was sent to this source.
170 * These values are only updated when the source is sending.
172 * "sent-rb" G_TYPE_BOOLEAN we have sent an RB
173 * "sent-rb-fractionlost" G_TYPE_UINT calculated lost fraction
174 * "sent-rb-packetslost" G_TYPE_INT lost packets
175 * "sent-rb-exthighestseq" G_TYPE_UINT last seen seqnum
176 * "sent-rb-jitter" G_TYPE_UINT jitter
177 * "sent-rb-lsr" G_TYPE_UINT last SR time
178 * "sent-rb-dlsr" G_TYPE_UINT delay since last SR
180 * The following fields are only present for non-internal sources and
181 * represents the last RB that this source sent. This is only updated
182 * when the source is receiving data and sending RB blocks.
184 * "have-rb" G_TYPE_BOOLEAN the source has sent RB
185 * "rb-fractionlost" G_TYPE_UINT lost fraction
186 * "rb-packetslost" G_TYPE_INT lost packets
187 * "rb-exthighestseq" G_TYPE_UINT highest received seqnum
188 * "rb-jitter" G_TYPE_UINT reception jitter
189 * "rb-lsr" G_TYPE_UINT last SR time
190 * "rb-dlsr" G_TYPE_UINT delay since last SR
192 * The round trip of this source. This is calculated from the last RB
193 * values and the recption time of the last RB packet. Only present for
194 * non-internal sources.
196 * "rb-round-trip" G_TYPE_UINT the round trip time in nanoseconds
198 g_object_class_install_property (gobject_class, PROP_STATS,
199 g_param_spec_boxed ("stats", "Stats",
200 "The stats of this source", GST_TYPE_STRUCTURE,
201 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
203 g_object_class_install_property (gobject_class, PROP_PROBATION,
204 g_param_spec_uint ("probation", "Number of probations",
205 "Consecutive packet sequence numbers to accept the source",
206 0, G_MAXUINT, DEFAULT_PROBATION,
207 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
209 GST_DEBUG_CATEGORY_INIT (rtp_source_debug, "rtpsource", 0, "RTP Source");
214 * @src: an #RTPSource
216 * Reset the stats of @src.
219 rtp_source_reset (RTPSource * src)
221 src->marked_bye = FALSE;
223 g_free (src->bye_reason);
224 src->bye_reason = NULL;
225 src->sent_bye = FALSE;
226 g_hash_table_remove_all (src->reported_in_sr_of);
228 src->stats.cycles = -1;
229 src->stats.jitter = 0;
230 src->stats.transit = -1;
231 src->stats.curr_sr = 0;
232 src->stats.sr[0].is_valid = FALSE;
233 src->stats.curr_rr = 0;
234 src->stats.rr[0].is_valid = FALSE;
235 src->stats.prev_rtptime = GST_CLOCK_TIME_NONE;
236 src->stats.prev_rtcptime = GST_CLOCK_TIME_NONE;
237 src->stats.last_rtptime = GST_CLOCK_TIME_NONE;
238 src->stats.last_rtcptime = GST_CLOCK_TIME_NONE;
239 g_array_set_size (src->nacks, 0);
241 src->stats.sent_pli_count = 0;
242 src->stats.sent_fir_count = 0;
246 rtp_source_init (RTPSource * src)
248 /* sources are initialy on probation until we receive enough valid RTP
249 * packets or a valid RTCP packet */
250 src->validated = FALSE;
251 src->internal = FALSE;
252 src->probation = DEFAULT_PROBATION;
253 src->curr_probation = src->probation;
254 src->closing = FALSE;
256 src->sdes = gst_structure_new_empty ("application/x-rtp-source-sdes");
259 src->clock_rate = -1;
260 src->packets = g_queue_new ();
261 src->seqnum_offset = -1;
262 src->last_rtptime = -1;
264 src->retained_feedback = g_queue_new ();
265 src->nacks = g_array_new (FALSE, FALSE, sizeof (guint32));
267 src->reported_in_sr_of = g_hash_table_new (g_direct_hash, g_direct_equal);
269 rtp_source_reset (src);
273 rtp_conflicting_address_free (RTPConflictingAddress * addr)
275 g_object_unref (addr->address);
276 g_slice_free (RTPConflictingAddress, addr);
280 rtp_source_finalize (GObject * object)
285 src = RTP_SOURCE_CAST (object);
287 while ((buffer = g_queue_pop_head (src->packets)))
288 gst_buffer_unref (buffer);
289 g_queue_free (src->packets);
291 gst_structure_free (src->sdes);
293 g_free (src->bye_reason);
295 gst_caps_replace (&src->caps, NULL);
297 g_list_free_full (src->conflicting_addresses,
298 (GDestroyNotify) rtp_conflicting_address_free);
299 while ((buffer = g_queue_pop_head (src->retained_feedback)))
300 gst_buffer_unref (buffer);
301 g_queue_free (src->retained_feedback);
303 g_array_free (src->nacks, TRUE);
306 g_object_unref (src->rtp_from);
308 g_object_unref (src->rtcp_from);
310 g_hash_table_unref (src->reported_in_sr_of);
312 G_OBJECT_CLASS (rtp_source_parent_class)->finalize (object);
315 static GstStructure *
316 rtp_source_create_stats (RTPSource * src)
319 gboolean is_sender = src->is_sender;
320 gboolean internal = src->internal;
323 guint8 fractionlost = 0;
324 gint32 packetslost = 0;
325 guint32 exthighestseq = 0;
329 guint32 round_trip = 0;
331 GstClockTime time = 0;
334 guint32 packet_count = 0;
335 guint32 octet_count = 0;
338 /* common data for all types of sources */
339 s = gst_structure_new ("application/x-rtp-source-stats",
340 "ssrc", G_TYPE_UINT, (guint) src->ssrc,
341 "internal", G_TYPE_BOOLEAN, internal,
342 "validated", G_TYPE_BOOLEAN, src->validated,
343 "received-bye", G_TYPE_BOOLEAN, src->marked_bye,
344 "is-csrc", G_TYPE_BOOLEAN, src->is_csrc,
345 "is-sender", G_TYPE_BOOLEAN, is_sender,
346 "seqnum-base", G_TYPE_INT, src->seqnum_offset,
347 "clock-rate", G_TYPE_INT, src->clock_rate, NULL);
349 /* add address and port */
351 address_str = __g_socket_address_to_string (src->rtp_from);
352 gst_structure_set (s, "rtp-from", G_TYPE_STRING, address_str, NULL);
353 g_free (address_str);
355 if (src->rtcp_from) {
356 address_str = __g_socket_address_to_string (src->rtcp_from);
357 gst_structure_set (s, "rtcp-from", G_TYPE_STRING, address_str, NULL);
358 g_free (address_str);
361 gst_structure_set (s,
362 "octets-sent", G_TYPE_UINT64, src->stats.octets_sent,
363 "packets-sent", G_TYPE_UINT64, src->stats.packets_sent,
364 "octets-received", G_TYPE_UINT64, src->stats.octets_received,
365 "packets-received", G_TYPE_UINT64, src->stats.packets_received,
366 "bitrate", G_TYPE_UINT64, src->bitrate,
367 "packets-lost", G_TYPE_INT,
368 (gint) rtp_stats_get_packets_lost (&src->stats), "jitter", G_TYPE_UINT,
369 (guint) (src->stats.jitter >> 4),
370 "sent-pli-count", G_TYPE_UINT, src->stats.sent_pli_count,
371 "recv-pli-count", G_TYPE_UINT, src->stats.recv_pli_count,
372 "sent-fir-count", G_TYPE_UINT, src->stats.sent_fir_count,
373 "recv-fir-count", G_TYPE_UINT, src->stats.recv_fir_count, NULL);
375 /* get the last SR. */
376 have_sr = rtp_source_get_last_sr (src, &time, &ntptime, &rtptime,
377 &packet_count, &octet_count);
378 gst_structure_set (s,
379 "have-sr", G_TYPE_BOOLEAN, have_sr,
380 "sr-ntptime", G_TYPE_UINT64, ntptime,
381 "sr-rtptime", G_TYPE_UINT, (guint) rtptime,
382 "sr-octet-count", G_TYPE_UINT, (guint) octet_count,
383 "sr-packet-count", G_TYPE_UINT, (guint) packet_count, NULL);
386 /* get the last RB we sent */
387 gst_structure_set (s,
388 "sent-rb", G_TYPE_BOOLEAN, src->last_rr.is_valid,
389 "sent-rb-fractionlost", G_TYPE_UINT, (guint) src->last_rr.fractionlost,
390 "sent-rb-packetslost", G_TYPE_INT, (gint) src->last_rr.packetslost,
391 "sent-rb-exthighestseq", G_TYPE_UINT,
392 (guint) src->last_rr.exthighestseq, "sent-rb-jitter", G_TYPE_UINT,
393 (guint) src->last_rr.jitter, "sent-rb-lsr", G_TYPE_UINT,
394 (guint) src->last_rr.lsr, "sent-rb-dlsr", G_TYPE_UINT,
395 (guint) src->last_rr.dlsr, NULL);
397 /* get the last RB */
398 have_rb = rtp_source_get_last_rb (src, &fractionlost, &packetslost,
399 &exthighestseq, &jitter, &lsr, &dlsr, &round_trip);
401 gst_structure_set (s,
402 "have-rb", G_TYPE_BOOLEAN, have_rb,
403 "rb-fractionlost", G_TYPE_UINT, (guint) fractionlost,
404 "rb-packetslost", G_TYPE_INT, (gint) packetslost,
405 "rb-exthighestseq", G_TYPE_UINT, (guint) exthighestseq,
406 "rb-jitter", G_TYPE_UINT, (guint) jitter,
407 "rb-lsr", G_TYPE_UINT, (guint) lsr,
408 "rb-dlsr", G_TYPE_UINT, (guint) dlsr,
409 "rb-round-trip", G_TYPE_UINT, (guint) round_trip, NULL);
416 * rtp_source_get_sdes_struct:
417 * @src: an #RTPSource
419 * Get the SDES from @src. See the SDES property for more details.
421 * Returns: %GstStructure of type "application/x-rtp-source-sdes". The result is
422 * valid until the SDES items of @src are modified.
425 rtp_source_get_sdes_struct (RTPSource * src)
427 g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
433 sdes_struct_compare_func (GQuark field_id, const GValue * value,
439 old = GST_STRUCTURE (user_data);
440 field = g_quark_to_string (field_id);
442 if (!gst_structure_has_field (old, field))
445 g_assert (G_VALUE_HOLDS_STRING (value));
447 return strcmp (g_value_get_string (value), gst_structure_get_string (old,
452 * rtp_source_set_sdes_struct:
453 * @src: an #RTPSource
454 * @sdes: the SDES structure
456 * Store the @sdes in @src. @sdes must be a structure of type
457 * "application/x-rtp-source-sdes", see the SDES property for more details.
459 * This function takes ownership of @sdes.
461 * Returns: %FALSE if the SDES was unchanged.
464 rtp_source_set_sdes_struct (RTPSource * src, GstStructure * sdes)
468 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
469 g_return_val_if_fail (strcmp (gst_structure_get_name (sdes),
470 "application/x-rtp-source-sdes") == 0, FALSE);
472 changed = !gst_structure_foreach (sdes, sdes_struct_compare_func, src->sdes);
475 gst_structure_free (src->sdes);
478 gst_structure_free (sdes);
484 rtp_source_set_property (GObject * object, guint prop_id,
485 const GValue * value, GParamSpec * pspec)
489 src = RTP_SOURCE (object);
493 src->ssrc = g_value_get_uint (value);
496 src->probation = g_value_get_uint (value);
499 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
505 rtp_source_get_property (GObject * object, guint prop_id,
506 GValue * value, GParamSpec * pspec)
510 src = RTP_SOURCE (object);
514 g_value_set_uint (value, rtp_source_get_ssrc (src));
517 g_value_set_boolean (value, rtp_source_is_as_csrc (src));
519 case PROP_IS_VALIDATED:
520 g_value_set_boolean (value, rtp_source_is_validated (src));
523 g_value_set_boolean (value, rtp_source_is_sender (src));
526 g_value_set_boxed (value, rtp_source_get_sdes_struct (src));
529 g_value_take_boxed (value, rtp_source_create_stats (src));
532 g_value_set_uint (value, src->probation);
535 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
544 * Create a #RTPSource with @ssrc.
546 * Returns: a new #RTPSource. Use g_object_unref() after usage.
549 rtp_source_new (guint32 ssrc)
553 src = g_object_new (RTP_TYPE_SOURCE, NULL);
560 * rtp_source_set_callbacks:
561 * @src: an #RTPSource
562 * @cb: callback functions
563 * @user_data: user data
565 * Set the callbacks for the source.
568 rtp_source_set_callbacks (RTPSource * src, RTPSourceCallbacks * cb,
571 g_return_if_fail (RTP_IS_SOURCE (src));
573 src->callbacks.push_rtp = cb->push_rtp;
574 src->callbacks.clock_rate = cb->clock_rate;
575 src->user_data = user_data;
579 * rtp_source_get_ssrc:
580 * @src: an #RTPSource
582 * Get the SSRC of @source.
584 * Returns: the SSRC of src.
587 rtp_source_get_ssrc (RTPSource * src)
591 g_return_val_if_fail (RTP_IS_SOURCE (src), 0);
599 * rtp_source_set_as_csrc:
600 * @src: an #RTPSource
602 * Configure @src as a CSRC, this will also validate @src.
605 rtp_source_set_as_csrc (RTPSource * src)
607 g_return_if_fail (RTP_IS_SOURCE (src));
609 src->validated = TRUE;
614 * rtp_source_is_as_csrc:
615 * @src: an #RTPSource
617 * Check if @src is a contributing source.
619 * Returns: %TRUE if @src is acting as a contributing source.
622 rtp_source_is_as_csrc (RTPSource * src)
626 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
628 result = src->is_csrc;
634 * rtp_source_is_active:
635 * @src: an #RTPSource
637 * Check if @src is an active source. A source is active if it has been
638 * validated and has not yet received a BYE packet
640 * Returns: %TRUE if @src is an qactive source.
643 rtp_source_is_active (RTPSource * src)
647 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
649 result = RTP_SOURCE_IS_ACTIVE (src);
655 * rtp_source_is_validated:
656 * @src: an #RTPSource
658 * Check if @src is a validated source.
660 * Returns: %TRUE if @src is a validated source.
663 rtp_source_is_validated (RTPSource * src)
667 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
669 result = src->validated;
675 * rtp_source_is_sender:
676 * @src: an #RTPSource
678 * Check if @src is a sending source.
680 * Returns: %TRUE if @src is a sending source.
683 rtp_source_is_sender (RTPSource * src)
687 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
689 result = RTP_SOURCE_IS_SENDER (src);
695 * rtp_source_is_marked_bye:
696 * @src: an #RTPSource
698 * Check if @src is marked as leaving the session with a BYE packet.
700 * Returns: %TRUE if @src has been marked BYE.
703 rtp_source_is_marked_bye (RTPSource * src)
707 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
709 result = RTP_SOURCE_IS_MARKED_BYE (src);
716 * rtp_source_get_bye_reason:
717 * @src: an #RTPSource
719 * Get the BYE reason for @src. Check if the source is marked as leaving the
720 * session with a BYE message first with rtp_source_is_marked_bye().
722 * Returns: The BYE reason or NULL when no reason was given or the source was
723 * not marked BYE yet. g_free() after usage.
726 rtp_source_get_bye_reason (RTPSource * src)
730 g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
732 result = g_strdup (src->bye_reason);
738 * rtp_source_update_caps:
739 * @src: an #RTPSource
742 * Parse @caps and store all relevant information in @source.
745 rtp_source_update_caps (RTPSource * src, GstCaps * caps)
752 /* nothing changed, return */
753 if (caps == NULL || src->caps == caps)
756 s = gst_caps_get_structure (caps, 0);
758 rtx = (gst_structure_get_uint (s, "rtx-ssrc", &val) && val == src->ssrc);
760 if (gst_structure_get_int (s, rtx ? "rtx-payload" : "payload", &ival))
765 GST_DEBUG ("got %spayload %d", rtx ? "rtx " : "", src->payload);
767 if (gst_structure_get_int (s, "clock-rate", &ival))
768 src->clock_rate = ival;
770 src->clock_rate = -1;
772 GST_DEBUG ("got clock-rate %d", src->clock_rate);
774 if (gst_structure_get_uint (s, rtx ? "rtx-seqnum-offset" : "seqnum-offset",
776 src->seqnum_offset = val;
778 src->seqnum_offset = -1;
780 GST_DEBUG ("got %sseqnum-offset %" G_GINT32_FORMAT, rtx ? "rtx " : "",
783 gst_caps_replace (&src->caps, caps);
787 * rtp_source_set_rtp_from:
788 * @src: an #RTPSource
789 * @address: the RTP address to set
791 * Set that @src is receiving RTP packets from @address. This is used for
792 * collistion checking.
795 rtp_source_set_rtp_from (RTPSource * src, GSocketAddress * address)
797 g_return_if_fail (RTP_IS_SOURCE (src));
800 g_object_unref (src->rtp_from);
801 src->rtp_from = G_SOCKET_ADDRESS (g_object_ref (address));
805 * rtp_source_set_rtcp_from:
806 * @src: an #RTPSource
807 * @address: the RTCP address to set
809 * Set that @src is receiving RTCP packets from @address. This is used for
810 * collistion checking.
813 rtp_source_set_rtcp_from (RTPSource * src, GSocketAddress * address)
815 g_return_if_fail (RTP_IS_SOURCE (src));
818 g_object_unref (src->rtcp_from);
819 src->rtcp_from = G_SOCKET_ADDRESS (g_object_ref (address));
823 push_packet (RTPSource * src, GstBuffer * buffer)
825 GstFlowReturn ret = GST_FLOW_OK;
827 /* push queued packets first if any */
828 while (!g_queue_is_empty (src->packets)) {
829 GstBuffer *buffer = GST_BUFFER_CAST (g_queue_pop_head (src->packets));
831 GST_LOG ("pushing queued packet");
832 if (src->callbacks.push_rtp)
833 src->callbacks.push_rtp (src, buffer, src->user_data);
835 gst_buffer_unref (buffer);
837 GST_LOG ("pushing new packet");
839 if (src->callbacks.push_rtp)
840 ret = src->callbacks.push_rtp (src, buffer, src->user_data);
842 gst_buffer_unref (buffer);
848 get_clock_rate (RTPSource * src, guint8 payload)
850 if (src->payload == -1) {
851 /* first payload received, nothing was in the caps, lock on to this payload */
852 src->payload = payload;
853 GST_DEBUG ("first payload %d", payload);
854 } else if (payload != src->payload) {
855 /* we have a different payload than before, reset the clock-rate */
856 GST_DEBUG ("new payload %d", payload);
857 src->payload = payload;
858 src->clock_rate = -1;
859 src->stats.transit = -1;
862 if (src->clock_rate == -1) {
863 gint clock_rate = -1;
865 if (src->callbacks.clock_rate)
866 clock_rate = src->callbacks.clock_rate (src, payload, src->user_data);
868 GST_DEBUG ("got clock-rate %d", clock_rate);
870 src->clock_rate = clock_rate;
872 return src->clock_rate;
875 /* Jitter is the variation in the delay of received packets in a flow. It is
876 * measured by comparing the interval when RTP packets were sent to the interval
877 * at which they were received. For instance, if packet #1 and packet #2 leave
878 * 50 milliseconds apart and arrive 60 milliseconds apart, then the jitter is 10
881 calculate_jitter (RTPSource * src, RTPPacketInfo * pinfo)
883 GstClockTime running_time;
884 guint32 rtparrival, transit, rtptime;
889 /* get arrival time */
890 if ((running_time = pinfo->running_time) == GST_CLOCK_TIME_NONE)
895 GST_LOG ("SSRC %08x got payload %d", src->ssrc, pt);
898 if ((clock_rate = get_clock_rate (src, pt)) == -1)
901 rtptime = pinfo->rtptime;
903 /* convert arrival time to RTP timestamp units, truncate to 32 bits, we don't
904 * care about the absolute value, just the difference. */
905 rtparrival = gst_util_uint64_scale_int (running_time, clock_rate, GST_SECOND);
907 /* transit time is difference with RTP timestamp */
908 transit = rtparrival - rtptime;
910 /* get ABS diff with previous transit time */
911 if (src->stats.transit != -1) {
912 if (transit > src->stats.transit)
913 diff = transit - src->stats.transit;
915 diff = src->stats.transit - transit;
919 src->stats.transit = transit;
921 /* update jitter, the value we store is scaled up so we can keep precision. */
922 src->stats.jitter += diff - ((src->stats.jitter + 8) >> 4);
924 src->stats.prev_rtptime = src->stats.last_rtptime;
925 src->stats.last_rtptime = rtparrival;
927 GST_LOG ("rtparrival %u, rtptime %u, clock-rate %d, diff %d, jitter: %f",
928 rtparrival, rtptime, clock_rate, diff, (src->stats.jitter) / 16.0);
935 GST_WARNING ("cannot get current running_time");
940 GST_WARNING ("cannot get clock-rate for pt %d", pt);
946 init_seq (RTPSource * src, guint16 seq)
948 src->stats.base_seq = seq;
949 src->stats.max_seq = seq;
950 src->stats.bad_seq = RTP_SEQ_MOD + 1; /* so seq == bad_seq is false */
951 src->stats.cycles = 0;
952 src->stats.packets_received = 0;
953 src->stats.octets_received = 0;
954 src->stats.bytes_received = 0;
955 src->stats.prev_received = 0;
956 src->stats.prev_expected = 0;
957 src->stats.recv_pli_count = 0;
958 src->stats.recv_fir_count = 0;
960 GST_DEBUG ("base_seq %d", seq);
963 #define BITRATE_INTERVAL (2 * GST_SECOND)
966 do_bitrate_estimation (RTPSource * src, GstClockTime running_time,
967 guint64 * bytes_handled)
971 if (src->prev_rtime) {
972 elapsed = running_time - src->prev_rtime;
974 if (elapsed > BITRATE_INTERVAL) {
977 rate = gst_util_uint64_scale (*bytes_handled, 8 * GST_SECOND, elapsed);
979 GST_LOG ("Elapsed %" G_GUINT64_FORMAT ", bytes %" G_GUINT64_FORMAT
980 ", rate %" G_GUINT64_FORMAT, elapsed, *bytes_handled, rate);
982 if (src->bitrate == 0)
985 src->bitrate = ((src->bitrate * 3) + rate) / 4;
987 src->prev_rtime = running_time;
991 GST_LOG ("Reset bitrate measurement");
992 src->prev_rtime = running_time;
998 update_receiver_stats (RTPSource * src, RTPPacketInfo * pinfo)
1000 guint16 seqnr, udelta;
1001 RTPSourceStats *stats;
1004 stats = &src->stats;
1006 seqnr = pinfo->seqnum;
1008 if (stats->cycles == -1) {
1009 GST_DEBUG ("received first packet");
1010 /* first time we heard of this source */
1011 init_seq (src, seqnr);
1012 src->stats.max_seq = seqnr - 1;
1013 src->curr_probation = src->probation;
1016 udelta = seqnr - stats->max_seq;
1018 /* if we are still on probation, check seqnum */
1019 if (src->curr_probation) {
1020 expected = src->stats.max_seq + 1;
1022 /* when in probation, we require consecutive seqnums */
1023 if (seqnr == expected) {
1024 /* expected packet */
1025 GST_DEBUG ("probation: seqnr %d == expected %d", seqnr, expected);
1026 src->curr_probation--;
1027 src->stats.max_seq = seqnr;
1028 if (src->curr_probation == 0) {
1029 GST_DEBUG ("probation done!");
1030 init_seq (src, seqnr);
1034 GST_DEBUG ("probation %d: queue packet", src->curr_probation);
1035 /* when still in probation, keep packets in a list. */
1036 g_queue_push_tail (src->packets, pinfo->data);
1038 /* remove packets from queue if there are too many */
1039 while (g_queue_get_length (src->packets) > RTP_MAX_PROBATION_LEN) {
1040 q = g_queue_pop_head (src->packets);
1041 gst_buffer_unref (q);
1046 /* unexpected seqnum in probation */
1047 goto probation_seqnum;
1049 } else if (udelta < RTP_MAX_DROPOUT) {
1050 /* in order, with permissible gap */
1051 if (seqnr < stats->max_seq) {
1052 /* sequence number wrapped - count another 64K cycle. */
1053 stats->cycles += RTP_SEQ_MOD;
1055 stats->max_seq = seqnr;
1056 } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
1057 /* the sequence number made a very large jump */
1058 if (seqnr == stats->bad_seq) {
1059 /* two sequential packets -- assume that the other side
1060 * restarted without telling us so just re-sync
1061 * (i.e., pretend this was the first packet). */
1062 init_seq (src, seqnr);
1064 /* unacceptable jump */
1065 stats->bad_seq = (seqnr + 1) & (RTP_SEQ_MOD - 1);
1069 /* duplicate or reordered packet, will be filtered by jitterbuffer. */
1070 GST_WARNING ("duplicate or reordered packet (seqnr %d)", seqnr);
1073 src->stats.octets_received += pinfo->payload_len;
1074 src->stats.bytes_received += pinfo->bytes;
1075 src->stats.packets_received++;
1076 /* for the bitrate estimation */
1077 src->bytes_received += pinfo->payload_len;
1079 GST_LOG ("seq %d, PC: %" G_GUINT64_FORMAT ", OC: %" G_GUINT64_FORMAT,
1080 seqnr, src->stats.packets_received, src->stats.octets_received);
1091 GST_WARNING ("unacceptable seqnum received");
1096 GST_WARNING ("probation: seqnr %d != expected %d", seqnr, expected);
1097 src->curr_probation = src->probation;
1098 src->stats.max_seq = seqnr;
1104 * rtp_source_process_rtp:
1105 * @src: an #RTPSource
1106 * @pinfo: an #RTPPacketInfo
1108 * Let @src handle the incomming RTP packet described in @pinfo.
1110 * Returns: a #GstFlowReturn.
1113 rtp_source_process_rtp (RTPSource * src, RTPPacketInfo * pinfo)
1115 GstFlowReturn result;
1117 g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
1118 g_return_val_if_fail (pinfo != NULL, GST_FLOW_ERROR);
1120 if (!update_receiver_stats (src, pinfo))
1123 /* the source that sent the packet must be a sender */
1124 src->is_sender = TRUE;
1125 src->validated = TRUE;
1127 do_bitrate_estimation (src, pinfo->running_time, &src->bytes_received);
1129 /* calculate jitter for the stats */
1130 calculate_jitter (src, pinfo);
1132 /* we're ready to push the RTP packet now */
1133 result = push_packet (src, pinfo->data);
1140 * rtp_source_mark_bye:
1141 * @src: an #RTPSource
1142 * @reason: the reason for leaving
1144 * Mark @src in the BYE state. This can happen when the source wants to
1145 * leave the sesssion or when a BYE packets has been received.
1147 * This will make the source inactive.
1150 rtp_source_mark_bye (RTPSource * src, const gchar * reason)
1152 g_return_if_fail (RTP_IS_SOURCE (src));
1154 GST_DEBUG ("marking SSRC %08x as BYE, reason: %s", src->ssrc,
1155 GST_STR_NULL (reason));
1157 /* copy the reason and mark as bye */
1158 g_free (src->bye_reason);
1159 src->bye_reason = g_strdup (reason);
1160 src->marked_bye = TRUE;
1164 * rtp_source_send_rtp:
1165 * @src: an #RTPSource
1166 * @data: an RTP buffer or a list of RTP buffers
1167 * @is_list: if @data is a buffer or list
1168 * @running_time: the running time of @data
1170 * Send @data (an RTP buffer or list of buffers) originating from @src.
1171 * This will make @src a sender. This function takes ownership of @data and
1172 * modifies the SSRC in the RTP packet to that of @src when needed.
1174 * Returns: a #GstFlowReturn.
1177 rtp_source_send_rtp (RTPSource * src, RTPPacketInfo * pinfo)
1179 GstFlowReturn result;
1180 GstClockTime running_time;
1182 guint64 ext_rtptime;
1183 guint64 rt_diff, rtp_diff;
1185 g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
1187 /* we are a sender now */
1188 src->is_sender = TRUE;
1190 /* update stats for the SR */
1191 src->stats.packets_sent += pinfo->packets;
1192 src->stats.octets_sent += pinfo->payload_len;
1193 src->bytes_sent += pinfo->payload_len;
1194 /* we are also a receiver of our packets */
1195 update_receiver_stats (src, pinfo);
1197 running_time = pinfo->running_time;
1199 do_bitrate_estimation (src, running_time, &src->bytes_sent);
1201 rtptime = pinfo->rtptime;
1203 ext_rtptime = src->last_rtptime;
1204 ext_rtptime = gst_rtp_buffer_ext_timestamp (&ext_rtptime, rtptime);
1206 GST_LOG ("SSRC %08x, RTP %" G_GUINT64_FORMAT ", running_time %"
1207 GST_TIME_FORMAT, src->ssrc, ext_rtptime, GST_TIME_ARGS (running_time));
1209 if (ext_rtptime > src->last_rtptime) {
1210 rtp_diff = ext_rtptime - src->last_rtptime;
1211 rt_diff = running_time - src->last_rtime;
1213 /* calc the diff so we can detect drift at the sender. This can also be used
1214 * to guestimate the clock rate if the NTP time is locked to the RTP
1215 * timestamps (as is the case when the capture device is providing the clock). */
1216 GST_LOG ("SSRC %08x, diff RTP %" G_GUINT64_FORMAT ", diff running_time %"
1217 GST_TIME_FORMAT, src->ssrc, rtp_diff, GST_TIME_ARGS (rt_diff));
1220 /* we keep track of the last received RTP timestamp and the corresponding
1221 * buffer running_time so that we can use this info when constructing SR reports */
1222 src->last_rtime = running_time;
1223 src->last_rtptime = ext_rtptime;
1226 if (!src->callbacks.push_rtp)
1229 GST_LOG ("pushing RTP %s %" G_GUINT64_FORMAT,
1230 pinfo->is_list ? "list" : "packet", src->stats.packets_sent);
1232 result = src->callbacks.push_rtp (src, pinfo->data, src->user_data);
1240 GST_WARNING ("no callback installed, dropping packet");
1246 * rtp_source_process_sr:
1247 * @src: an #RTPSource
1248 * @time: time of packet arrival
1249 * @ntptime: the NTP time in 32.32 fixed point
1250 * @rtptime: the RTP time
1251 * @packet_count: the packet count
1252 * @octet_count: the octect count
1254 * Update the sender report in @src.
1257 rtp_source_process_sr (RTPSource * src, GstClockTime time, guint64 ntptime,
1258 guint32 rtptime, guint32 packet_count, guint32 octet_count)
1260 RTPSenderReport *curr;
1263 g_return_if_fail (RTP_IS_SOURCE (src));
1265 GST_DEBUG ("got SR packet: SSRC %08x, NTP %08x:%08x, RTP %" G_GUINT32_FORMAT
1266 ", PC %" G_GUINT32_FORMAT ", OC %" G_GUINT32_FORMAT, src->ssrc,
1267 (guint32) (ntptime >> 32), (guint32) (ntptime & 0xffffffff), rtptime,
1268 packet_count, octet_count);
1270 curridx = src->stats.curr_sr ^ 1;
1271 curr = &src->stats.sr[curridx];
1273 /* this is a sender now */
1274 src->is_sender = TRUE;
1276 /* update current */
1277 curr->is_valid = TRUE;
1278 curr->ntptime = ntptime;
1279 curr->rtptime = rtptime;
1280 curr->packet_count = packet_count;
1281 curr->octet_count = octet_count;
1285 src->stats.curr_sr = curridx;
1287 src->stats.prev_rtcptime = src->stats.last_rtcptime;
1288 src->stats.last_rtcptime = time;
1292 * rtp_source_process_rb:
1293 * @src: an #RTPSource
1294 * @ntpnstime: the current time in nanoseconds since 1970
1295 * @fractionlost: fraction lost since last SR/RR
1296 * @packetslost: the cumululative number of packets lost
1297 * @exthighestseq: the extended last sequence number received
1298 * @jitter: the interarrival jitter
1299 * @lsr: the last SR packet from this source
1300 * @dlsr: the delay since last SR packet
1302 * Update the report block in @src.
1305 rtp_source_process_rb (RTPSource * src, guint64 ntpnstime,
1306 guint8 fractionlost, gint32 packetslost, guint32 exthighestseq,
1307 guint32 jitter, guint32 lsr, guint32 dlsr)
1309 RTPReceiverReport *curr;
1314 g_return_if_fail (RTP_IS_SOURCE (src));
1316 GST_DEBUG ("got RB packet: SSRC %08x, FL %2x, PL %d, HS %" G_GUINT32_FORMAT
1317 ", jitter %" G_GUINT32_FORMAT ", LSR %04x:%04x, DLSR %04x:%04x",
1318 src->ssrc, fractionlost, packetslost, exthighestseq, jitter, lsr >> 16,
1319 lsr & 0xffff, dlsr >> 16, dlsr & 0xffff);
1321 curridx = src->stats.curr_rr ^ 1;
1322 curr = &src->stats.rr[curridx];
1324 /* update current */
1325 curr->is_valid = TRUE;
1326 curr->fractionlost = fractionlost;
1327 curr->packetslost = packetslost;
1328 curr->exthighestseq = exthighestseq;
1329 curr->jitter = jitter;
1333 /* convert the NTP time in nanoseconds to 32.32 fixed point */
1334 f_ntp = gst_util_uint64_scale (ntpnstime, (1LL << 32), GST_SECOND);
1335 /* calculate round trip, round the time up */
1336 ntp = ((f_ntp + 0xffff) >> 16) & 0xffffffff;
1339 if (A > 0 && ntp > A)
1343 curr->round_trip = A;
1345 GST_DEBUG ("NTP %04x:%04x, round trip %04x:%04x", ntp >> 16, ntp & 0xffff,
1346 A >> 16, A & 0xffff);
1349 src->stats.curr_rr = curridx;
1353 * rtp_source_get_new_sr:
1354 * @src: an #RTPSource
1355 * @ntpnstime: the current time in nanoseconds since 1970
1356 * @running_time: the current running_time of the pipeline.
1357 * @ntptime: the NTP time in 32.32 fixed point
1358 * @rtptime: the RTP time corresponding to @ntptime
1359 * @packet_count: the packet count
1360 * @octet_count: the octect count
1362 * Get new values to put into a new SR report from this source.
1364 * @running_time and @ntpnstime are captured at the same time and represent the
1365 * running time of the pipeline clock and the absolute current system time in
1366 * nanoseconds respectively. Together with the last running_time and rtp timestamp
1367 * we have observed in the source, we can generate @ntptime and @rtptime for an SR
1368 * packet. @ntptime is basically the fixed point representation of @ntpnstime
1369 * and @rtptime the associated RTP timestamp.
1371 * Returns: %TRUE on success.
1374 rtp_source_get_new_sr (RTPSource * src, guint64 ntpnstime,
1375 GstClockTime running_time, guint64 * ntptime, guint32 * rtptime,
1376 guint32 * packet_count, guint32 * octet_count)
1379 guint64 t_current_ntp;
1380 GstClockTimeDiff diff;
1382 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1384 /* We last saw a buffer with last_rtptime at last_rtime. Given a running_time
1385 * and an NTP time, we can scale the RTP timestamps so that they match the
1386 * given NTP time. for scaling, we assume that the slope of the rtptime vs
1387 * running_time vs ntptime curve is close to 1, which is certainly
1388 * sufficient for the frequency at which we report SR and the rate we send
1389 * out RTP packets. */
1390 t_rtp = src->last_rtptime;
1392 GST_DEBUG ("last_rtime %" GST_TIME_FORMAT ", last_rtptime %"
1393 G_GUINT64_FORMAT, GST_TIME_ARGS (src->last_rtime), t_rtp);
1395 if (src->clock_rate != -1) {
1396 /* get the diff between the clock running_time and the buffer running_time.
1397 * This is the elapsed time, as measured against the pipeline clock, between
1398 * when the rtp timestamp was observed and the current running_time.
1400 * We need to apply this diff to the RTP timestamp to get the RTP timestamp
1401 * for the given ntpnstime. */
1402 diff = GST_CLOCK_DIFF (src->last_rtime, running_time);
1404 /* now translate the diff to RTP time, handle positive and negative cases.
1405 * If there is no diff, we already set rtptime correctly above. */
1407 GST_DEBUG ("running_time %" GST_TIME_FORMAT ", diff %" GST_TIME_FORMAT,
1408 GST_TIME_ARGS (running_time), GST_TIME_ARGS (diff));
1409 t_rtp += gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1412 GST_DEBUG ("running_time %" GST_TIME_FORMAT ", diff -%" GST_TIME_FORMAT,
1413 GST_TIME_ARGS (running_time), GST_TIME_ARGS (diff));
1414 t_rtp -= gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1417 GST_WARNING ("no clock-rate, cannot interpolate rtp time");
1420 /* convert the NTP time in nanoseconds to 32.32 fixed point */
1421 t_current_ntp = gst_util_uint64_scale (ntpnstime, (1LL << 32), GST_SECOND);
1423 GST_DEBUG ("NTP %08x:%08x, RTP %" G_GUINT32_FORMAT,
1424 (guint32) (t_current_ntp >> 32), (guint32) (t_current_ntp & 0xffffffff),
1428 *ntptime = t_current_ntp;
1432 *packet_count = src->stats.packets_sent;
1434 *octet_count = src->stats.octets_sent;
1440 * rtp_source_get_new_rb:
1441 * @src: an #RTPSource
1442 * @time: the current time of the system clock
1443 * @fractionlost: fraction lost since last SR/RR
1444 * @packetslost: the cumululative number of packets lost
1445 * @exthighestseq: the extended last sequence number received
1446 * @jitter: the interarrival jitter
1447 * @lsr: the last SR packet from this source
1448 * @dlsr: the delay since last SR packet
1450 * Get new values to put into a new report block from this source.
1452 * Returns: %TRUE on success.
1455 rtp_source_get_new_rb (RTPSource * src, GstClockTime time,
1456 guint8 * fractionlost, gint32 * packetslost, guint32 * exthighestseq,
1457 guint32 * jitter, guint32 * lsr, guint32 * dlsr)
1459 RTPSourceStats *stats;
1460 guint64 extended_max, expected;
1461 guint64 expected_interval, received_interval, ntptime;
1462 gint64 lost, lost_interval;
1463 guint32 fraction, LSR, DLSR;
1464 GstClockTime sr_time;
1466 stats = &src->stats;
1468 extended_max = stats->cycles + stats->max_seq;
1469 expected = extended_max - stats->base_seq + 1;
1471 GST_DEBUG ("ext_max %" G_GUINT64_FORMAT ", expected %" G_GUINT64_FORMAT
1472 ", received %" G_GUINT64_FORMAT ", base_seq %" G_GUINT32_FORMAT,
1473 extended_max, expected, stats->packets_received, stats->base_seq);
1475 lost = expected - stats->packets_received;
1476 lost = CLAMP (lost, -0x800000, 0x7fffff);
1478 expected_interval = expected - stats->prev_expected;
1479 stats->prev_expected = expected;
1480 received_interval = stats->packets_received - stats->prev_received;
1481 stats->prev_received = stats->packets_received;
1483 lost_interval = expected_interval - received_interval;
1485 if (expected_interval == 0 || lost_interval <= 0)
1488 fraction = (lost_interval << 8) / expected_interval;
1490 GST_DEBUG ("add RR for SSRC %08x", src->ssrc);
1491 /* we scaled the jitter up for additional precision */
1492 GST_DEBUG ("fraction %" G_GUINT32_FORMAT ", lost %" G_GINT64_FORMAT
1493 ", extseq %" G_GUINT64_FORMAT ", jitter %d", fraction, lost,
1494 extended_max, stats->jitter >> 4);
1496 if (rtp_source_get_last_sr (src, &sr_time, &ntptime, NULL, NULL, NULL)) {
1499 /* LSR is middle 32 bits of the last ntptime */
1500 LSR = (ntptime >> 16) & 0xffffffff;
1501 diff = time - sr_time;
1502 GST_DEBUG ("last SR time diff %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
1503 /* DLSR, delay since last SR is expressed in 1/65536 second units */
1504 DLSR = gst_util_uint64_scale_int (diff, 65536, GST_SECOND);
1506 /* No valid SR received, LSR/DLSR are set to 0 then */
1507 GST_DEBUG ("no valid SR received");
1511 GST_DEBUG ("LSR %04x:%04x, DLSR %04x:%04x", LSR >> 16, LSR & 0xffff,
1512 DLSR >> 16, DLSR & 0xffff);
1515 *fractionlost = fraction;
1517 *packetslost = lost;
1519 *exthighestseq = extended_max;
1521 *jitter = stats->jitter >> 4;
1531 * rtp_source_get_last_sr:
1532 * @src: an #RTPSource
1533 * @time: time of packet arrival
1534 * @ntptime: the NTP time in 32.32 fixed point
1535 * @rtptime: the RTP time
1536 * @packet_count: the packet count
1537 * @octet_count: the octect count
1539 * Get the values of the last sender report as set with rtp_source_process_sr().
1541 * Returns: %TRUE if there was a valid SR report.
1544 rtp_source_get_last_sr (RTPSource * src, GstClockTime * time, guint64 * ntptime,
1545 guint32 * rtptime, guint32 * packet_count, guint32 * octet_count)
1547 RTPSenderReport *curr;
1549 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1551 curr = &src->stats.sr[src->stats.curr_sr];
1552 if (!curr->is_valid)
1556 *ntptime = curr->ntptime;
1558 *rtptime = curr->rtptime;
1560 *packet_count = curr->packet_count;
1562 *octet_count = curr->octet_count;
1570 * rtp_source_get_last_rb:
1571 * @src: an #RTPSource
1572 * @fractionlost: fraction lost since last SR/RR
1573 * @packetslost: the cumululative number of packets lost
1574 * @exthighestseq: the extended last sequence number received
1575 * @jitter: the interarrival jitter
1576 * @lsr: the last SR packet from this source
1577 * @dlsr: the delay since last SR packet
1578 * @round_trip: the round trip time
1580 * Get the values of the last RB report set with rtp_source_process_rb().
1582 * Returns: %TRUE if there was a valid SB report.
1585 rtp_source_get_last_rb (RTPSource * src, guint8 * fractionlost,
1586 gint32 * packetslost, guint32 * exthighestseq, guint32 * jitter,
1587 guint32 * lsr, guint32 * dlsr, guint32 * round_trip)
1589 RTPReceiverReport *curr;
1591 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1593 curr = &src->stats.rr[src->stats.curr_rr];
1594 if (!curr->is_valid)
1598 *fractionlost = curr->fractionlost;
1600 *packetslost = curr->packetslost;
1602 *exthighestseq = curr->exthighestseq;
1604 *jitter = curr->jitter;
1610 *round_trip = curr->round_trip;
1616 find_conflicting_address (GList * conflicting_addresses,
1617 GSocketAddress * address, GstClockTime time)
1621 for (item = conflicting_addresses; item; item = g_list_next (item)) {
1622 RTPConflictingAddress *known_conflict = item->data;
1624 if (__g_socket_address_equal (address, known_conflict->address)) {
1625 known_conflict->time = time;
1634 add_conflicting_address (GList * conflicting_addresses,
1635 GSocketAddress * address, GstClockTime time)
1637 RTPConflictingAddress *new_conflict;
1639 new_conflict = g_slice_new (RTPConflictingAddress);
1641 new_conflict->address = G_SOCKET_ADDRESS (g_object_ref (address));
1642 new_conflict->time = time;
1644 return g_list_prepend (conflicting_addresses, new_conflict);
1648 timeout_conflicting_addresses (GList * conflicting_addresses,
1649 GstClockTime current_time)
1652 /* "a relatively long time" -- RFC 3550 section 8.2 */
1653 const GstClockTime collision_timeout =
1654 RTP_STATS_MIN_INTERVAL * GST_SECOND * 10;
1656 item = g_list_first (conflicting_addresses);
1658 RTPConflictingAddress *known_conflict = item->data;
1659 GList *next_item = g_list_next (item);
1661 if (known_conflict->time < current_time - collision_timeout) {
1664 conflicting_addresses = g_list_delete_link (conflicting_addresses, item);
1665 buf = __g_socket_address_to_string (known_conflict->address);
1666 GST_DEBUG ("collision %p timed out: %s", known_conflict, buf);
1668 rtp_conflicting_address_free (known_conflict);
1673 return conflicting_addresses;
1677 * rtp_source_find_conflicting_address:
1678 * @src: The source the packet came in
1679 * @address: address to check for
1680 * @time: The time when the packet that is possibly in conflict arrived
1682 * Checks if an address which has a conflict is already known. If it is
1683 * a known conflict, remember the time
1685 * Returns: TRUE if it was a known conflict, FALSE otherwise
1688 rtp_source_find_conflicting_address (RTPSource * src, GSocketAddress * address,
1691 return find_conflicting_address (src->conflicting_addresses, address, time);
1695 * rtp_source_add_conflicting_address:
1696 * @src: The source the packet came in
1697 * @address: address to remember
1698 * @time: The time when the packet that is in conflict arrived
1700 * Adds a new conflict address
1703 rtp_source_add_conflicting_address (RTPSource * src,
1704 GSocketAddress * address, GstClockTime time)
1706 src->conflicting_addresses =
1707 add_conflicting_address (src->conflicting_addresses, address, time);
1711 * rtp_source_timeout:
1712 * @src: The #RTPSource
1713 * @current_time: The current time
1714 * @feedback_retention_window: The running time before which retained feedback
1715 * packets have to be discarded
1717 * This is processed on each RTCP interval. It times out old collisions.
1718 * It also times out old retained feedback packets
1721 rtp_source_timeout (RTPSource * src, GstClockTime current_time,
1722 GstClockTime feedback_retention_window)
1726 src->conflicting_addresses =
1727 timeout_conflicting_addresses (src->conflicting_addresses, current_time);
1729 /* Time out AVPF packets that are older than the desired length */
1730 while ((pkt = g_queue_peek_tail (src->retained_feedback)) &&
1731 GST_BUFFER_TIMESTAMP (pkt) < feedback_retention_window)
1732 gst_buffer_unref (g_queue_pop_tail (src->retained_feedback));
1736 compare_buffers (gconstpointer a, gconstpointer b, gpointer user_data)
1738 const GstBuffer *bufa = a;
1739 const GstBuffer *bufb = b;
1741 return GST_BUFFER_TIMESTAMP (bufa) - GST_BUFFER_TIMESTAMP (bufb);
1745 rtp_source_retain_rtcp_packet (RTPSource * src, GstRTCPPacket * packet,
1746 GstClockTime running_time)
1750 buffer = gst_buffer_copy_region (packet->rtcp->buffer, GST_BUFFER_COPY_MEMORY,
1751 packet->offset, (gst_rtcp_packet_get_length (packet) + 1) * 4);
1753 GST_BUFFER_TIMESTAMP (buffer) = running_time;
1755 g_queue_insert_sorted (src->retained_feedback, buffer, compare_buffers, NULL);
1759 rtp_source_has_retained (RTPSource * src, GCompareFunc func, gconstpointer data)
1761 if (g_queue_find_custom (src->retained_feedback, data, func))
1768 * @src: The #RTPSource
1771 * Register that @seqnum has not been received from @src.
1774 rtp_source_register_nack (RTPSource * src, guint16 seqnum)
1777 guint32 dword = seqnum << 16;
1780 len = src->nacks->len;
1781 for (i = 0; i < len; i++) {
1785 tdword = g_array_index (src->nacks, guint32, i);
1786 tseq = tdword >> 16;
1788 diff = gst_rtp_buffer_compare_seqnum (tseq, seqnum);
1792 /* we already have this seqnum */
1795 /* it comes before the recorded seqnum, FIXME, we could merge it
1796 * if not to far away */
1798 GST_DEBUG ("insert NACK #%u at %u", seqnum, i);
1799 g_array_insert_val (src->nacks, i, dword);
1800 } else if (diff < 16) {
1801 /* we can merge it */
1802 dword = g_array_index (src->nacks, guint32, i);
1803 dword |= 1 << (diff - 1);
1804 GST_DEBUG ("merge NACK #%u at %u with NACK #%u -> 0x%08x", seqnum, i,
1805 dword >> 16, dword);
1806 g_array_index (src->nacks, guint32, i) = dword;
1808 GST_DEBUG ("append NACK #%u", seqnum);
1809 g_array_append_val (src->nacks, dword);
1811 src->send_nack = TRUE;
1815 * @src: The #RTPSource
1816 * @n_nacks: result number of nacks
1818 * Get the registered NACKS since the last rtp_source_clear_nacks().
1820 * Returns: an array of @n_nacks seqnum values.
1823 rtp_source_get_nacks (RTPSource * src, guint * n_nacks)
1826 *n_nacks = src->nacks->len;
1828 return (guint32 *) src->nacks->data;
1832 rtp_source_clear_nacks (RTPSource * src)
1834 g_array_set_size (src->nacks, 0);
1835 src->send_nack = FALSE;