2 * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
3 * Copyright (C) 2015 Kurento (http://kurento.org/)
4 * @author: Miguel ParĂs <mparisdiaz@gmail.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 #include <gst/rtp/gstrtpbuffer.h>
24 #include <gst/rtp/gstrtcpbuffer.h>
26 #include "rtpsource.h"
28 GST_DEBUG_CATEGORY_STATIC (rtp_source_debug);
29 #define GST_CAT_DEFAULT rtp_source_debug
31 #define RTP_MAX_PROBATION_LEN 32
33 /* signals and args */
39 #define DEFAULT_SSRC 0
40 #define DEFAULT_IS_CSRC FALSE
41 #define DEFAULT_IS_VALIDATED FALSE
42 #define DEFAULT_IS_SENDER FALSE
43 #define DEFAULT_SDES NULL
44 #define DEFAULT_PROBATION RTP_DEFAULT_PROBATION
45 #define DEFAULT_MAX_DROPOUT_TIME 60000
46 #define DEFAULT_MAX_MISORDER_TIME 2000
58 PROP_MAX_DROPOUT_TIME,
59 PROP_MAX_MISORDER_TIME
62 /* GObject vmethods */
63 static void rtp_source_finalize (GObject * object);
64 static void rtp_source_set_property (GObject * object, guint prop_id,
65 const GValue * value, GParamSpec * pspec);
66 static void rtp_source_get_property (GObject * object, guint prop_id,
67 GValue * value, GParamSpec * pspec);
69 /* static guint rtp_source_signals[LAST_SIGNAL] = { 0 }; */
71 G_DEFINE_TYPE (RTPSource, rtp_source, G_TYPE_OBJECT);
74 rtp_source_class_init (RTPSourceClass * klass)
76 GObjectClass *gobject_class;
78 gobject_class = (GObjectClass *) klass;
80 gobject_class->finalize = rtp_source_finalize;
82 gobject_class->set_property = rtp_source_set_property;
83 gobject_class->get_property = rtp_source_get_property;
85 g_object_class_install_property (gobject_class, PROP_SSRC,
86 g_param_spec_uint ("ssrc", "SSRC",
87 "The SSRC of this source", 0, G_MAXUINT, DEFAULT_SSRC,
88 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
90 g_object_class_install_property (gobject_class, PROP_IS_CSRC,
91 g_param_spec_boolean ("is-csrc", "Is CSRC",
92 "If this SSRC is acting as a contributing source",
93 DEFAULT_IS_CSRC, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
95 g_object_class_install_property (gobject_class, PROP_IS_VALIDATED,
96 g_param_spec_boolean ("is-validated", "Is Validated",
97 "If this SSRC is validated", DEFAULT_IS_VALIDATED,
98 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
100 g_object_class_install_property (gobject_class, PROP_IS_SENDER,
101 g_param_spec_boolean ("is-sender", "Is Sender",
102 "If this SSRC is a sender", DEFAULT_IS_SENDER,
103 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
108 * The current SDES items of the source. Returns a structure with name
109 * application/x-rtp-source-sdes and may contain the following fields:
111 * 'cname' G_TYPE_STRING : The canonical name
112 * 'name' G_TYPE_STRING : The user name
113 * 'email' G_TYPE_STRING : The user's electronic mail address
114 * 'phone' G_TYPE_STRING : The user's phone number
115 * 'location' G_TYPE_STRING : The geographic user location
116 * 'tool' G_TYPE_STRING : The name of application or tool
117 * 'note' G_TYPE_STRING : A notice about the source
119 * Other fields may be present and these represent private items in
120 * the SDES where the field name is the prefix.
122 g_object_class_install_property (gobject_class, PROP_SDES,
123 g_param_spec_boxed ("sdes", "SDES",
124 "The SDES information for this source",
125 GST_TYPE_STRUCTURE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
130 * This property returns a GstStructure named application/x-rtp-source-stats with
131 * fields useful for statistics and diagnostics.
133 * Take note of each respective field's units:
135 * - NTP times are in the appropriate 32-bit or 64-bit fixed-point format
136 * starting from January 1, 1970 (except for timespans).
137 * - RTP times are in clock rate units (i.e. clock rate = 1 second)
138 * starting at a random offset.
139 * - For fields indicating packet loss, note that late packets are not considered lost,
140 * and duplicates are not taken into account. Hence, the loss may be negative
141 * if there are duplicates.
143 * The following fields are always present.
145 * "ssrc" G_TYPE_UINT the SSRC of this source
146 * "internal" G_TYPE_BOOLEAN this source is a source of the session
147 * "validated" G_TYPE_BOOLEAN the source is validated
148 * "received-bye" G_TYPE_BOOLEAN we received a BYE from this source
149 * "is-csrc" G_TYPE_BOOLEAN this source was found as CSRC
150 * "is-sender" G_TYPE_BOOLEAN this source is a sender
151 * "seqnum-base" G_TYPE_INT first seqnum if known
152 * "clock-rate" G_TYPE_INT the clock rate of the media
154 * The following fields are only present when known.
156 * "rtp-from" G_TYPE_STRING where we received the last RTP packet from
157 * "rtcp-from" G_TYPE_STRING where we received the last RTCP packet from
159 * The following fields make sense for internal sources and will only increase
160 * when "is-sender" is TRUE.
162 * "octets-sent" G_TYPE_UINT64 number of bytes we sent
163 * "packets-sent" G_TYPE_UINT64 number of packets we sent
165 * The following fields make sense for non-internal sources and will only
166 * increase when "is-sender" is TRUE.
168 * "octets-received" G_TYPE_UINT64 total number of bytes received
169 * "packets-received" G_TYPE_UINT64 total number of packets received
171 * Following fields are updated when "is-sender" is TRUE.
173 * "bitrate" G_TYPE_UINT64 bitrate in bits per second
174 * "jitter" G_TYPE_UINT estimated jitter (in clock rate units)
175 * "packets-lost" G_TYPE_INT estimated amount of packets lost
177 * The last SR report this source sent. This only updates when "is-sender" is
180 * "have-sr" G_TYPE_BOOLEAN the source has sent SR
181 * "sr-ntptime" G_TYPE_UINT64 NTP time of SR (in NTP Timestamp Format, 32.32 fixed point)
182 * "sr-rtptime" G_TYPE_UINT RTP time of SR (in clock rate units)
183 * "sr-octet-count" G_TYPE_UINT the number of bytes in the SR
184 * "sr-packet-count" G_TYPE_UINT the number of packets in the SR
186 * The following fields are only present for non-internal sources and
187 * represent the content of the last RB packet that was sent to this source.
188 * These values are only updated when the source is sending.
190 * "sent-rb" G_TYPE_BOOLEAN we have sent an RB
191 * "sent-rb-fractionlost" G_TYPE_UINT calculated lost fraction
192 * "sent-rb-packetslost" G_TYPE_INT lost packets
193 * "sent-rb-exthighestseq" G_TYPE_UINT last seen seqnum
194 * "sent-rb-jitter" G_TYPE_UINT jitter (in clock rate units)
195 * "sent-rb-lsr" G_TYPE_UINT last SR time (in NTP Short Format, 16.16 fixed point)
196 * "sent-rb-dlsr" G_TYPE_UINT delay since last SR (in NTP Short Format, 16.16 fixed point)
198 * The following fields are only present for non-internal sources and
199 * represents the last RB that this source sent. This is only updated
200 * when the source is receiving data and sending RB blocks.
202 * "have-rb" G_TYPE_BOOLEAN the source has sent RB
203 * "rb-fractionlost" G_TYPE_UINT lost fraction
204 * "rb-packetslost" G_TYPE_INT lost packets
205 * "rb-exthighestseq" G_TYPE_UINT highest received seqnum
206 * "rb-jitter" G_TYPE_UINT reception jitter (in clock rate units)
207 * "rb-lsr" G_TYPE_UINT last SR time (in NTP Short Format, 16.16 fixed point)
208 * "rb-dlsr" G_TYPE_UINT delay since last SR (in NTP Short Format, 16.16 fixed point)
210 * The round trip of this source is calculated from the last RB
211 * values and the reception time of the last RB packet. It is only present for
212 * non-internal sources.
214 * "rb-round-trip" G_TYPE_UINT the round-trip time (in NTP Short Format, 16.16 fixed point)
217 g_object_class_install_property (gobject_class, PROP_STATS,
218 g_param_spec_boxed ("stats", "Stats",
219 "The stats of this source", GST_TYPE_STRUCTURE,
220 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
222 g_object_class_install_property (gobject_class, PROP_PROBATION,
223 g_param_spec_uint ("probation", "Number of probations",
224 "Consecutive packet sequence numbers to accept the source",
225 0, G_MAXUINT, DEFAULT_PROBATION,
226 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
228 g_object_class_install_property (gobject_class, PROP_MAX_DROPOUT_TIME,
229 g_param_spec_uint ("max-dropout-time", "Max dropout time",
230 "The maximum time (milliseconds) of missing packets tolerated.",
231 0, G_MAXUINT, DEFAULT_MAX_DROPOUT_TIME,
232 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
234 g_object_class_install_property (gobject_class, PROP_MAX_MISORDER_TIME,
235 g_param_spec_uint ("max-misorder-time", "Max misorder time",
236 "The maximum time (milliseconds) of misordered packets tolerated.",
237 0, G_MAXUINT, DEFAULT_MAX_MISORDER_TIME,
238 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
240 GST_DEBUG_CATEGORY_INIT (rtp_source_debug, "rtpsource", 0, "RTP Source");
245 * @src: an #RTPSource
247 * Reset the stats of @src.
250 rtp_source_reset (RTPSource * src)
252 src->marked_bye = FALSE;
254 g_free (src->bye_reason);
255 src->bye_reason = NULL;
256 src->sent_bye = FALSE;
257 g_hash_table_remove_all (src->reported_in_sr_of);
259 src->stats.cycles = -1;
260 src->stats.jitter = 0;
261 src->stats.transit = -1;
262 src->stats.curr_sr = 0;
263 src->stats.sr[0].is_valid = FALSE;
264 src->stats.curr_rr = 0;
265 src->stats.rr[0].is_valid = FALSE;
266 src->stats.prev_rtptime = GST_CLOCK_TIME_NONE;
267 src->stats.prev_rtcptime = GST_CLOCK_TIME_NONE;
268 src->stats.last_rtptime = GST_CLOCK_TIME_NONE;
269 src->stats.last_rtcptime = GST_CLOCK_TIME_NONE;
270 g_array_set_size (src->nacks, 0);
272 src->stats.sent_pli_count = 0;
273 src->stats.sent_fir_count = 0;
277 rtp_source_init (RTPSource * src)
279 /* sources are initialy on probation until we receive enough valid RTP
280 * packets or a valid RTCP packet */
281 src->validated = FALSE;
282 src->internal = FALSE;
283 src->probation = DEFAULT_PROBATION;
284 src->curr_probation = src->probation;
285 src->closing = FALSE;
286 src->max_dropout_time = DEFAULT_MAX_DROPOUT_TIME;
287 src->max_misorder_time = DEFAULT_MAX_MISORDER_TIME;
289 src->sdes = gst_structure_new_empty ("application/x-rtp-source-sdes");
292 src->clock_rate = -1;
293 src->packets = g_queue_new ();
294 src->seqnum_offset = -1;
295 src->last_rtptime = -1;
297 src->retained_feedback = g_queue_new ();
298 src->nacks = g_array_new (FALSE, FALSE, sizeof (guint32));
300 src->reported_in_sr_of = g_hash_table_new (g_direct_hash, g_direct_equal);
302 rtp_source_reset (src);
306 rtp_conflicting_address_free (RTPConflictingAddress * addr)
308 g_object_unref (addr->address);
309 g_slice_free (RTPConflictingAddress, addr);
313 rtp_source_finalize (GObject * object)
317 src = RTP_SOURCE_CAST (object);
319 g_queue_foreach (src->packets, (GFunc) gst_buffer_unref, NULL);
320 g_queue_free (src->packets);
322 gst_structure_free (src->sdes);
324 g_free (src->bye_reason);
326 gst_caps_replace (&src->caps, NULL);
328 g_list_free_full (src->conflicting_addresses,
329 (GDestroyNotify) rtp_conflicting_address_free);
330 g_queue_foreach (src->retained_feedback, (GFunc) gst_buffer_unref, NULL);
331 g_queue_free (src->retained_feedback);
333 g_array_free (src->nacks, TRUE);
336 g_object_unref (src->rtp_from);
338 g_object_unref (src->rtcp_from);
340 g_hash_table_unref (src->reported_in_sr_of);
342 G_OBJECT_CLASS (rtp_source_parent_class)->finalize (object);
345 static GstStructure *
346 rtp_source_create_stats (RTPSource * src)
349 gboolean is_sender = src->is_sender;
350 gboolean internal = src->internal;
353 guint8 fractionlost = 0;
354 gint32 packetslost = 0;
355 guint32 exthighestseq = 0;
359 guint32 round_trip = 0;
361 GstClockTime time = 0;
364 guint32 packet_count = 0;
365 guint32 octet_count = 0;
368 /* common data for all types of sources */
369 s = gst_structure_new ("application/x-rtp-source-stats",
370 "ssrc", G_TYPE_UINT, (guint) src->ssrc,
371 "internal", G_TYPE_BOOLEAN, internal,
372 "validated", G_TYPE_BOOLEAN, src->validated,
373 "received-bye", G_TYPE_BOOLEAN, src->marked_bye,
374 "is-csrc", G_TYPE_BOOLEAN, src->is_csrc,
375 "is-sender", G_TYPE_BOOLEAN, is_sender,
376 "seqnum-base", G_TYPE_INT, src->seqnum_offset,
377 "clock-rate", G_TYPE_INT, src->clock_rate, NULL);
379 /* add address and port */
381 address_str = __g_socket_address_to_string (src->rtp_from);
382 gst_structure_set (s, "rtp-from", G_TYPE_STRING, address_str, NULL);
383 g_free (address_str);
385 if (src->rtcp_from) {
386 address_str = __g_socket_address_to_string (src->rtcp_from);
387 gst_structure_set (s, "rtcp-from", G_TYPE_STRING, address_str, NULL);
388 g_free (address_str);
391 gst_structure_set (s,
392 "octets-sent", G_TYPE_UINT64, src->stats.octets_sent,
393 "packets-sent", G_TYPE_UINT64, src->stats.packets_sent,
394 "octets-received", G_TYPE_UINT64, src->stats.octets_received,
395 "packets-received", G_TYPE_UINT64, src->stats.packets_received,
396 "bitrate", G_TYPE_UINT64, src->bitrate,
397 "packets-lost", G_TYPE_INT,
398 (gint) rtp_stats_get_packets_lost (&src->stats), "jitter", G_TYPE_UINT,
399 (guint) (src->stats.jitter >> 4),
400 "sent-pli-count", G_TYPE_UINT, src->stats.sent_pli_count,
401 "recv-pli-count", G_TYPE_UINT, src->stats.recv_pli_count,
402 "sent-fir-count", G_TYPE_UINT, src->stats.sent_fir_count,
403 "recv-fir-count", G_TYPE_UINT, src->stats.recv_fir_count, NULL);
405 /* get the last SR. */
406 have_sr = rtp_source_get_last_sr (src, &time, &ntptime, &rtptime,
407 &packet_count, &octet_count);
408 gst_structure_set (s,
409 "have-sr", G_TYPE_BOOLEAN, have_sr,
410 "sr-ntptime", G_TYPE_UINT64, ntptime,
411 "sr-rtptime", G_TYPE_UINT, (guint) rtptime,
412 "sr-octet-count", G_TYPE_UINT, (guint) octet_count,
413 "sr-packet-count", G_TYPE_UINT, (guint) packet_count, NULL);
416 /* get the last RB we sent */
417 gst_structure_set (s,
418 "sent-rb", G_TYPE_BOOLEAN, src->last_rr.is_valid,
419 "sent-rb-fractionlost", G_TYPE_UINT, (guint) src->last_rr.fractionlost,
420 "sent-rb-packetslost", G_TYPE_INT, (gint) src->last_rr.packetslost,
421 "sent-rb-exthighestseq", G_TYPE_UINT,
422 (guint) src->last_rr.exthighestseq, "sent-rb-jitter", G_TYPE_UINT,
423 (guint) src->last_rr.jitter, "sent-rb-lsr", G_TYPE_UINT,
424 (guint) src->last_rr.lsr, "sent-rb-dlsr", G_TYPE_UINT,
425 (guint) src->last_rr.dlsr, NULL);
427 /* get the last RB */
428 have_rb = rtp_source_get_last_rb (src, &fractionlost, &packetslost,
429 &exthighestseq, &jitter, &lsr, &dlsr, &round_trip);
431 gst_structure_set (s,
432 "have-rb", G_TYPE_BOOLEAN, have_rb,
433 "rb-fractionlost", G_TYPE_UINT, (guint) fractionlost,
434 "rb-packetslost", G_TYPE_INT, (gint) packetslost,
435 "rb-exthighestseq", G_TYPE_UINT, (guint) exthighestseq,
436 "rb-jitter", G_TYPE_UINT, (guint) jitter,
437 "rb-lsr", G_TYPE_UINT, (guint) lsr,
438 "rb-dlsr", G_TYPE_UINT, (guint) dlsr,
439 "rb-round-trip", G_TYPE_UINT, (guint) round_trip, NULL);
446 * rtp_source_get_sdes_struct:
447 * @src: an #RTPSource
449 * Get the SDES from @src. See the SDES property for more details.
451 * Returns: %GstStructure of type "application/x-rtp-source-sdes". The result is
452 * valid until the SDES items of @src are modified.
455 rtp_source_get_sdes_struct (RTPSource * src)
457 g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
463 sdes_struct_compare_func (GQuark field_id, const GValue * value,
469 old = GST_STRUCTURE (user_data);
470 field = g_quark_to_string (field_id);
472 if (!gst_structure_has_field (old, field))
475 g_assert (G_VALUE_HOLDS_STRING (value));
477 return strcmp (g_value_get_string (value), gst_structure_get_string (old,
482 * rtp_source_set_sdes_struct:
483 * @src: an #RTPSource
484 * @sdes: the SDES structure
486 * Store the @sdes in @src. @sdes must be a structure of type
487 * "application/x-rtp-source-sdes", see the SDES property for more details.
489 * This function takes ownership of @sdes.
491 * Returns: %FALSE if the SDES was unchanged.
494 rtp_source_set_sdes_struct (RTPSource * src, GstStructure * sdes)
498 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
499 g_return_val_if_fail (strcmp (gst_structure_get_name (sdes),
500 "application/x-rtp-source-sdes") == 0, FALSE);
502 changed = !gst_structure_foreach (sdes, sdes_struct_compare_func, src->sdes);
505 gst_structure_free (src->sdes);
508 gst_structure_free (sdes);
514 rtp_source_set_property (GObject * object, guint prop_id,
515 const GValue * value, GParamSpec * pspec)
519 src = RTP_SOURCE (object);
523 src->ssrc = g_value_get_uint (value);
526 src->probation = g_value_get_uint (value);
528 case PROP_MAX_DROPOUT_TIME:
529 src->max_dropout_time = g_value_get_uint (value);
531 case PROP_MAX_MISORDER_TIME:
532 src->max_misorder_time = g_value_get_uint (value);
535 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
541 rtp_source_get_property (GObject * object, guint prop_id,
542 GValue * value, GParamSpec * pspec)
546 src = RTP_SOURCE (object);
550 g_value_set_uint (value, rtp_source_get_ssrc (src));
553 g_value_set_boolean (value, rtp_source_is_as_csrc (src));
555 case PROP_IS_VALIDATED:
556 g_value_set_boolean (value, rtp_source_is_validated (src));
559 g_value_set_boolean (value, rtp_source_is_sender (src));
562 g_value_set_boxed (value, rtp_source_get_sdes_struct (src));
565 g_value_take_boxed (value, rtp_source_create_stats (src));
568 g_value_set_uint (value, src->probation);
570 case PROP_MAX_DROPOUT_TIME:
571 g_value_set_uint (value, src->max_dropout_time);
573 case PROP_MAX_MISORDER_TIME:
574 g_value_set_uint (value, src->max_misorder_time);
577 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
586 * Create a #RTPSource with @ssrc.
588 * Returns: a new #RTPSource. Use g_object_unref() after usage.
591 rtp_source_new (guint32 ssrc)
595 src = g_object_new (RTP_TYPE_SOURCE, NULL);
602 * rtp_source_set_callbacks:
603 * @src: an #RTPSource
604 * @cb: callback functions
605 * @user_data: user data
607 * Set the callbacks for the source.
610 rtp_source_set_callbacks (RTPSource * src, RTPSourceCallbacks * cb,
613 g_return_if_fail (RTP_IS_SOURCE (src));
615 src->callbacks.push_rtp = cb->push_rtp;
616 src->callbacks.clock_rate = cb->clock_rate;
617 src->user_data = user_data;
621 * rtp_source_get_ssrc:
622 * @src: an #RTPSource
624 * Get the SSRC of @source.
626 * Returns: the SSRC of src.
629 rtp_source_get_ssrc (RTPSource * src)
633 g_return_val_if_fail (RTP_IS_SOURCE (src), 0);
641 * rtp_source_set_as_csrc:
642 * @src: an #RTPSource
644 * Configure @src as a CSRC, this will also validate @src.
647 rtp_source_set_as_csrc (RTPSource * src)
649 g_return_if_fail (RTP_IS_SOURCE (src));
651 src->validated = TRUE;
656 * rtp_source_is_as_csrc:
657 * @src: an #RTPSource
659 * Check if @src is a contributing source.
661 * Returns: %TRUE if @src is acting as a contributing source.
664 rtp_source_is_as_csrc (RTPSource * src)
668 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
670 result = src->is_csrc;
676 * rtp_source_is_active:
677 * @src: an #RTPSource
679 * Check if @src is an active source. A source is active if it has been
680 * validated and has not yet received a BYE packet
682 * Returns: %TRUE if @src is an qactive source.
685 rtp_source_is_active (RTPSource * src)
689 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
691 result = RTP_SOURCE_IS_ACTIVE (src);
697 * rtp_source_is_validated:
698 * @src: an #RTPSource
700 * Check if @src is a validated source.
702 * Returns: %TRUE if @src is a validated source.
705 rtp_source_is_validated (RTPSource * src)
709 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
711 result = src->validated;
717 * rtp_source_is_sender:
718 * @src: an #RTPSource
720 * Check if @src is a sending source.
722 * Returns: %TRUE if @src is a sending source.
725 rtp_source_is_sender (RTPSource * src)
729 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
731 result = RTP_SOURCE_IS_SENDER (src);
737 * rtp_source_is_marked_bye:
738 * @src: an #RTPSource
740 * Check if @src is marked as leaving the session with a BYE packet.
742 * Returns: %TRUE if @src has been marked BYE.
745 rtp_source_is_marked_bye (RTPSource * src)
749 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
751 result = RTP_SOURCE_IS_MARKED_BYE (src);
758 * rtp_source_get_bye_reason:
759 * @src: an #RTPSource
761 * Get the BYE reason for @src. Check if the source is marked as leaving the
762 * session with a BYE message first with rtp_source_is_marked_bye().
764 * Returns: The BYE reason or NULL when no reason was given or the source was
765 * not marked BYE yet. g_free() after usage.
768 rtp_source_get_bye_reason (RTPSource * src)
772 g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
774 result = g_strdup (src->bye_reason);
780 * rtp_source_update_caps:
781 * @src: an #RTPSource
784 * Parse @caps and store all relevant information in @source.
787 rtp_source_update_caps (RTPSource * src, GstCaps * caps)
794 /* nothing changed, return */
795 if (caps == NULL || src->caps == caps)
798 s = gst_caps_get_structure (caps, 0);
800 rtx = (gst_structure_get_uint (s, "rtx-ssrc", &val) && val == src->ssrc);
802 if (gst_structure_get_int (s, rtx ? "rtx-payload" : "payload", &ival))
807 GST_DEBUG ("got %spayload %d", rtx ? "rtx " : "", src->payload);
809 if (gst_structure_get_int (s, "clock-rate", &ival))
810 src->clock_rate = ival;
812 src->clock_rate = -1;
814 GST_DEBUG ("got clock-rate %d", src->clock_rate);
816 if (gst_structure_get_uint (s, rtx ? "rtx-seqnum-offset" : "seqnum-offset",
818 src->seqnum_offset = val;
820 src->seqnum_offset = -1;
822 GST_DEBUG ("got %sseqnum-offset %" G_GINT32_FORMAT, rtx ? "rtx " : "",
825 gst_caps_replace (&src->caps, caps);
829 * rtp_source_set_rtp_from:
830 * @src: an #RTPSource
831 * @address: the RTP address to set
833 * Set that @src is receiving RTP packets from @address. This is used for
834 * collistion checking.
837 rtp_source_set_rtp_from (RTPSource * src, GSocketAddress * address)
839 g_return_if_fail (RTP_IS_SOURCE (src));
842 g_object_unref (src->rtp_from);
843 src->rtp_from = G_SOCKET_ADDRESS (g_object_ref (address));
847 * rtp_source_set_rtcp_from:
848 * @src: an #RTPSource
849 * @address: the RTCP address to set
851 * Set that @src is receiving RTCP packets from @address. This is used for
852 * collistion checking.
855 rtp_source_set_rtcp_from (RTPSource * src, GSocketAddress * address)
857 g_return_if_fail (RTP_IS_SOURCE (src));
860 g_object_unref (src->rtcp_from);
861 src->rtcp_from = G_SOCKET_ADDRESS (g_object_ref (address));
865 push_packet (RTPSource * src, GstBuffer * buffer)
867 GstFlowReturn ret = GST_FLOW_OK;
869 /* push queued packets first if any */
870 while (!g_queue_is_empty (src->packets)) {
871 GstBuffer *buffer = GST_BUFFER_CAST (g_queue_pop_head (src->packets));
873 GST_LOG ("pushing queued packet");
874 if (src->callbacks.push_rtp)
875 src->callbacks.push_rtp (src, buffer, src->user_data);
877 gst_buffer_unref (buffer);
879 GST_LOG ("pushing new packet");
881 if (src->callbacks.push_rtp)
882 ret = src->callbacks.push_rtp (src, buffer, src->user_data);
884 gst_buffer_unref (buffer);
890 get_clock_rate (RTPSource * src, guint8 payload)
892 if (src->payload == -1) {
893 /* first payload received, nothing was in the caps, lock on to this payload */
894 src->payload = payload;
895 GST_DEBUG ("first payload %d", payload);
896 } else if (payload != src->payload) {
897 /* we have a different payload than before, reset the clock-rate */
898 GST_DEBUG ("new payload %d", payload);
899 src->payload = payload;
900 src->clock_rate = -1;
901 src->stats.transit = -1;
904 if (src->clock_rate == -1) {
905 gint clock_rate = -1;
907 if (src->callbacks.clock_rate)
908 clock_rate = src->callbacks.clock_rate (src, payload, src->user_data);
910 GST_DEBUG ("got clock-rate %d", clock_rate);
912 src->clock_rate = clock_rate;
913 gst_rtp_packet_rate_ctx_reset (&src->packet_rate_ctx, clock_rate);
915 return src->clock_rate;
918 /* Jitter is the variation in the delay of received packets in a flow. It is
919 * measured by comparing the interval when RTP packets were sent to the interval
920 * at which they were received. For instance, if packet #1 and packet #2 leave
921 * 50 milliseconds apart and arrive 60 milliseconds apart, then the jitter is 10
924 calculate_jitter (RTPSource * src, RTPPacketInfo * pinfo)
926 GstClockTime running_time;
927 guint32 rtparrival, transit, rtptime;
932 /* get arrival time */
933 if ((running_time = pinfo->running_time) == GST_CLOCK_TIME_NONE)
938 GST_LOG ("SSRC %08x got payload %d", src->ssrc, pt);
941 if ((clock_rate = get_clock_rate (src, pt)) == -1)
944 rtptime = pinfo->rtptime;
946 /* convert arrival time to RTP timestamp units, truncate to 32 bits, we don't
947 * care about the absolute value, just the difference. */
948 rtparrival = gst_util_uint64_scale_int (running_time, clock_rate, GST_SECOND);
950 /* transit time is difference with RTP timestamp */
951 transit = rtparrival - rtptime;
953 /* get ABS diff with previous transit time */
954 if (src->stats.transit != -1) {
955 if (transit > src->stats.transit)
956 diff = transit - src->stats.transit;
958 diff = src->stats.transit - transit;
962 src->stats.transit = transit;
964 /* update jitter, the value we store is scaled up so we can keep precision. */
965 src->stats.jitter += diff - ((src->stats.jitter + 8) >> 4);
967 src->stats.prev_rtptime = src->stats.last_rtptime;
968 src->stats.last_rtptime = rtparrival;
970 GST_LOG ("rtparrival %u, rtptime %u, clock-rate %d, diff %d, jitter: %f",
971 rtparrival, rtptime, clock_rate, diff, (src->stats.jitter) / 16.0);
978 GST_WARNING ("cannot get current running_time");
983 GST_WARNING ("cannot get clock-rate for pt %d", pt);
989 init_seq (RTPSource * src, guint16 seq)
991 src->stats.base_seq = seq;
992 src->stats.max_seq = seq;
993 src->stats.bad_seq = RTP_SEQ_MOD + 1; /* so seq == bad_seq is false */
994 src->stats.cycles = 0;
995 src->stats.packets_received = 0;
996 src->stats.octets_received = 0;
997 src->stats.bytes_received = 0;
998 src->stats.prev_received = 0;
999 src->stats.prev_expected = 0;
1000 src->stats.recv_pli_count = 0;
1001 src->stats.recv_fir_count = 0;
1003 GST_DEBUG ("base_seq %d", seq);
1006 #define BITRATE_INTERVAL (2 * GST_SECOND)
1009 do_bitrate_estimation (RTPSource * src, GstClockTime running_time,
1010 guint64 * bytes_handled)
1014 if (src->prev_rtime) {
1015 elapsed = running_time - src->prev_rtime;
1017 if (elapsed > BITRATE_INTERVAL) {
1020 rate = gst_util_uint64_scale (*bytes_handled, 8 * GST_SECOND, elapsed);
1022 GST_LOG ("Elapsed %" G_GUINT64_FORMAT ", bytes %" G_GUINT64_FORMAT
1023 ", rate %" G_GUINT64_FORMAT, elapsed, *bytes_handled, rate);
1025 if (src->bitrate == 0)
1026 src->bitrate = rate;
1028 src->bitrate = ((src->bitrate * 3) + rate) / 4;
1030 src->prev_rtime = running_time;
1034 GST_LOG ("Reset bitrate measurement");
1035 src->prev_rtime = running_time;
1041 update_receiver_stats (RTPSource * src, RTPPacketInfo * pinfo,
1042 gboolean is_receive)
1044 guint16 seqnr, expected;
1045 RTPSourceStats *stats;
1047 gint32 packet_rate, max_dropout, max_misorder;
1049 stats = &src->stats;
1051 seqnr = pinfo->seqnum;
1054 gst_rtp_packet_rate_ctx_update (&src->packet_rate_ctx, pinfo->seqnum,
1057 gst_rtp_packet_rate_ctx_get_max_dropout (&src->packet_rate_ctx,
1058 src->max_dropout_time);
1060 gst_rtp_packet_rate_ctx_get_max_misorder (&src->packet_rate_ctx,
1061 src->max_misorder_time);
1062 GST_TRACE ("SSRC %08x, packet_rate: %d, max_dropout: %d, max_misorder: %d",
1063 src->ssrc, packet_rate, max_dropout, max_misorder);
1065 if (stats->cycles == -1) {
1066 GST_DEBUG ("received first packet");
1067 /* first time we heard of this source */
1068 init_seq (src, seqnr);
1069 src->stats.max_seq = seqnr - 1;
1070 src->curr_probation = src->probation;
1074 expected = src->stats.max_seq + 1;
1075 delta = gst_rtp_buffer_compare_seqnum (expected, seqnr);
1077 /* if we are still on probation, check seqnum */
1078 if (src->curr_probation) {
1079 /* when in probation, we require consecutive seqnums */
1081 /* expected packet */
1082 GST_DEBUG ("probation: seqnr %d == expected %d", seqnr, expected);
1083 src->curr_probation--;
1084 if (seqnr < stats->max_seq) {
1085 /* sequence number wrapped - count another 64K cycle. */
1086 stats->cycles += RTP_SEQ_MOD;
1088 src->stats.max_seq = seqnr;
1090 if (src->curr_probation == 0) {
1091 GST_DEBUG ("probation done!");
1092 init_seq (src, seqnr);
1096 GST_DEBUG ("probation %d: queue packet", src->curr_probation);
1097 /* when still in probation, keep packets in a list. */
1098 g_queue_push_tail (src->packets, pinfo->data);
1100 /* remove packets from queue if there are too many */
1101 while (g_queue_get_length (src->packets) > RTP_MAX_PROBATION_LEN) {
1102 q = g_queue_pop_head (src->packets);
1103 gst_buffer_unref (q);
1108 /* unexpected seqnum in probation */
1109 goto probation_seqnum;
1111 } else if (delta >= 0 && delta < max_dropout) {
1112 /* Clear bad packets */
1113 stats->bad_seq = RTP_SEQ_MOD + 1; /* so seq == bad_seq is false */
1114 g_queue_foreach (src->packets, (GFunc) gst_buffer_unref, NULL);
1115 g_queue_clear (src->packets);
1117 /* in order, with permissible gap */
1118 if (seqnr < stats->max_seq) {
1119 /* sequence number wrapped - count another 64K cycle. */
1120 stats->cycles += RTP_SEQ_MOD;
1122 stats->max_seq = seqnr;
1123 } else if (delta < -max_misorder || delta >= max_dropout) {
1124 /* the sequence number made a very large jump */
1125 if (seqnr == stats->bad_seq && src->packets->head) {
1126 /* two sequential packets -- assume that the other side
1127 * restarted without telling us so just re-sync
1128 * (i.e., pretend this was the first packet). */
1129 init_seq (src, seqnr);
1131 /* unacceptable jump */
1132 stats->bad_seq = (seqnr + 1) & (RTP_SEQ_MOD - 1);
1133 g_queue_foreach (src->packets, (GFunc) gst_buffer_unref, NULL);
1134 g_queue_clear (src->packets);
1135 g_queue_push_tail (src->packets, pinfo->data);
1139 } else { /* delta < 0 && delta >= -max_misorder */
1140 /* Clear bad packets */
1141 stats->bad_seq = RTP_SEQ_MOD + 1; /* so seq == bad_seq is false */
1142 g_queue_foreach (src->packets, (GFunc) gst_buffer_unref, NULL);
1143 g_queue_clear (src->packets);
1145 /* duplicate or reordered packet, will be filtered by jitterbuffer. */
1146 GST_WARNING ("duplicate or reordered packet (seqnr %u, expected %u)",
1151 src->stats.octets_received += pinfo->payload_len;
1152 src->stats.bytes_received += pinfo->bytes;
1153 src->stats.packets_received++;
1154 /* for the bitrate estimation */
1155 src->bytes_received += pinfo->payload_len;
1157 GST_LOG ("seq %u, PC: %" G_GUINT64_FORMAT ", OC: %" G_GUINT64_FORMAT,
1158 seqnr, src->stats.packets_received, src->stats.octets_received);
1170 ("unacceptable seqnum received (seqnr %u, delta %d, packet_rate: %d, max_dropout: %d, max_misorder: %d)",
1171 seqnr, delta, packet_rate, max_dropout, max_misorder);
1176 GST_WARNING ("probation: seqnr %d != expected %d", seqnr, expected);
1177 src->curr_probation = src->probation;
1178 src->stats.max_seq = seqnr;
1184 * rtp_source_process_rtp:
1185 * @src: an #RTPSource
1186 * @pinfo: an #RTPPacketInfo
1188 * Let @src handle the incomming RTP packet described in @pinfo.
1190 * Returns: a #GstFlowReturn.
1193 rtp_source_process_rtp (RTPSource * src, RTPPacketInfo * pinfo)
1195 GstFlowReturn result;
1197 g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
1198 g_return_val_if_fail (pinfo != NULL, GST_FLOW_ERROR);
1200 if (!update_receiver_stats (src, pinfo, TRUE))
1203 /* the source that sent the packet must be a sender */
1204 src->is_sender = TRUE;
1205 src->validated = TRUE;
1207 do_bitrate_estimation (src, pinfo->running_time, &src->bytes_received);
1209 /* calculate jitter for the stats */
1210 calculate_jitter (src, pinfo);
1212 /* we're ready to push the RTP packet now */
1213 result = push_packet (src, pinfo->data);
1220 * rtp_source_mark_bye:
1221 * @src: an #RTPSource
1222 * @reason: the reason for leaving
1224 * Mark @src in the BYE state. This can happen when the source wants to
1225 * leave the sesssion or when a BYE packets has been received.
1227 * This will make the source inactive.
1230 rtp_source_mark_bye (RTPSource * src, const gchar * reason)
1232 g_return_if_fail (RTP_IS_SOURCE (src));
1234 GST_DEBUG ("marking SSRC %08x as BYE, reason: %s", src->ssrc,
1235 GST_STR_NULL (reason));
1237 /* copy the reason and mark as bye */
1238 g_free (src->bye_reason);
1239 src->bye_reason = g_strdup (reason);
1240 src->marked_bye = TRUE;
1244 * rtp_source_send_rtp:
1245 * @src: an #RTPSource
1246 * @data: an RTP buffer or a list of RTP buffers
1247 * @is_list: if @data is a buffer or list
1248 * @running_time: the running time of @data
1250 * Send @data (an RTP buffer or list of buffers) originating from @src.
1251 * This will make @src a sender. This function takes ownership of @data and
1252 * modifies the SSRC in the RTP packet to that of @src when needed.
1254 * Returns: a #GstFlowReturn.
1257 rtp_source_send_rtp (RTPSource * src, RTPPacketInfo * pinfo)
1259 GstFlowReturn result;
1260 GstClockTime running_time;
1262 guint64 ext_rtptime;
1263 guint64 rt_diff, rtp_diff;
1265 g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
1267 /* we are a sender now */
1268 src->is_sender = TRUE;
1270 /* we are also a receiver of our packets */
1271 if (!update_receiver_stats (src, pinfo, FALSE))
1274 /* update stats for the SR */
1275 src->stats.packets_sent += pinfo->packets;
1276 src->stats.octets_sent += pinfo->payload_len;
1277 src->bytes_sent += pinfo->payload_len;
1279 running_time = pinfo->running_time;
1281 do_bitrate_estimation (src, running_time, &src->bytes_sent);
1283 rtptime = pinfo->rtptime;
1285 ext_rtptime = src->last_rtptime;
1286 ext_rtptime = gst_rtp_buffer_ext_timestamp (&ext_rtptime, rtptime);
1288 GST_LOG ("SSRC %08x, RTP %" G_GUINT64_FORMAT ", running_time %"
1289 GST_TIME_FORMAT, src->ssrc, ext_rtptime, GST_TIME_ARGS (running_time));
1291 if (ext_rtptime > src->last_rtptime) {
1292 rtp_diff = ext_rtptime - src->last_rtptime;
1293 rt_diff = running_time - src->last_rtime;
1295 /* calc the diff so we can detect drift at the sender. This can also be used
1296 * to guestimate the clock rate if the NTP time is locked to the RTP
1297 * timestamps (as is the case when the capture device is providing the clock). */
1298 GST_LOG ("SSRC %08x, diff RTP %" G_GUINT64_FORMAT ", diff running_time %"
1299 GST_TIME_FORMAT, src->ssrc, rtp_diff, GST_TIME_ARGS (rt_diff));
1302 /* we keep track of the last received RTP timestamp and the corresponding
1303 * buffer running_time so that we can use this info when constructing SR reports */
1304 src->last_rtime = running_time;
1305 src->last_rtptime = ext_rtptime;
1308 if (!src->callbacks.push_rtp)
1311 GST_LOG ("pushing RTP %s %" G_GUINT64_FORMAT,
1312 pinfo->is_list ? "list" : "packet", src->stats.packets_sent);
1314 result = src->callbacks.push_rtp (src, pinfo->data, src->user_data);
1322 GST_WARNING ("no callback installed, dropping packet");
1328 * rtp_source_process_sr:
1329 * @src: an #RTPSource
1330 * @time: time of packet arrival
1331 * @ntptime: the NTP time (in NTP Timestamp Format, 32.32 fixed point)
1332 * @rtptime: the RTP time (in clock rate units)
1333 * @packet_count: the packet count
1334 * @octet_count: the octet count
1336 * Update the sender report in @src.
1339 rtp_source_process_sr (RTPSource * src, GstClockTime time, guint64 ntptime,
1340 guint32 rtptime, guint32 packet_count, guint32 octet_count)
1342 RTPSenderReport *curr;
1345 g_return_if_fail (RTP_IS_SOURCE (src));
1347 GST_DEBUG ("got SR packet: SSRC %08x, NTP %08x:%08x, RTP %" G_GUINT32_FORMAT
1348 ", PC %" G_GUINT32_FORMAT ", OC %" G_GUINT32_FORMAT, src->ssrc,
1349 (guint32) (ntptime >> 32), (guint32) (ntptime & 0xffffffff), rtptime,
1350 packet_count, octet_count);
1352 curridx = src->stats.curr_sr ^ 1;
1353 curr = &src->stats.sr[curridx];
1355 /* this is a sender now */
1356 src->is_sender = TRUE;
1358 /* update current */
1359 curr->is_valid = TRUE;
1360 curr->ntptime = ntptime;
1361 curr->rtptime = rtptime;
1362 curr->packet_count = packet_count;
1363 curr->octet_count = octet_count;
1367 src->stats.curr_sr = curridx;
1369 src->stats.prev_rtcptime = src->stats.last_rtcptime;
1370 src->stats.last_rtcptime = time;
1374 * rtp_source_process_rb:
1375 * @src: an #RTPSource
1376 * @ntpnstime: the current time in nanoseconds since 1970
1377 * @fractionlost: fraction lost since last SR/RR
1378 * @packetslost: the cumulative number of packets lost
1379 * @exthighestseq: the extended last sequence number received
1380 * @jitter: the interarrival jitter (in clock rate units)
1381 * @lsr: the time of the last SR packet on this source
1382 * (in NTP Short Format, 16.16 fixed point)
1383 * @dlsr: the delay since the last SR packet
1384 * (in NTP Short Format, 16.16 fixed point)
1386 * Update the report block in @src.
1389 rtp_source_process_rb (RTPSource * src, guint64 ntpnstime,
1390 guint8 fractionlost, gint32 packetslost, guint32 exthighestseq,
1391 guint32 jitter, guint32 lsr, guint32 dlsr)
1393 RTPReceiverReport *curr;
1398 g_return_if_fail (RTP_IS_SOURCE (src));
1400 GST_DEBUG ("got RB packet: SSRC %08x, FL %2x, PL %d, HS %" G_GUINT32_FORMAT
1401 ", jitter %" G_GUINT32_FORMAT ", LSR %04x:%04x, DLSR %04x:%04x",
1402 src->ssrc, fractionlost, packetslost, exthighestseq, jitter, lsr >> 16,
1403 lsr & 0xffff, dlsr >> 16, dlsr & 0xffff);
1405 curridx = src->stats.curr_rr ^ 1;
1406 curr = &src->stats.rr[curridx];
1408 /* update current */
1409 curr->is_valid = TRUE;
1410 curr->fractionlost = fractionlost;
1411 curr->packetslost = packetslost;
1412 curr->exthighestseq = exthighestseq;
1413 curr->jitter = jitter;
1417 /* convert the NTP time in nanoseconds to 32.32 fixed point */
1418 f_ntp = gst_util_uint64_scale (ntpnstime, (1LL << 32), GST_SECOND);
1419 /* calculate round trip, round the time up */
1420 ntp = ((f_ntp + 0xffff) >> 16) & 0xffffffff;
1423 if (A > 0 && ntp > A)
1427 curr->round_trip = A;
1429 GST_DEBUG ("NTP %04x:%04x, round trip %04x:%04x", ntp >> 16, ntp & 0xffff,
1430 A >> 16, A & 0xffff);
1433 src->stats.curr_rr = curridx;
1437 * rtp_source_get_new_sr:
1438 * @src: an #RTPSource
1439 * @ntpnstime: the current time in nanoseconds since 1970
1440 * @running_time: the current running_time of the pipeline
1441 * @ntptime: the NTP time (in NTP Timestamp Format, 32.32 fixed point)
1442 * @rtptime: the RTP time corresponding to @ntptime (in clock rate units)
1443 * @packet_count: the packet count
1444 * @octet_count: the octet count
1446 * Get new values to put into a new SR report from this source.
1448 * @running_time and @ntpnstime are captured at the same time and represent the
1449 * running time of the pipeline clock and the absolute current system time in
1450 * nanoseconds respectively. Together with the last running_time and RTP timestamp
1451 * we have observed in the source, we can generate @ntptime and @rtptime for an SR
1452 * packet. @ntptime is basically the fixed point representation of @ntpnstime
1453 * and @rtptime the associated RTP timestamp.
1455 * Returns: %TRUE on success.
1458 rtp_source_get_new_sr (RTPSource * src, guint64 ntpnstime,
1459 GstClockTime running_time, guint64 * ntptime, guint32 * rtptime,
1460 guint32 * packet_count, guint32 * octet_count)
1463 guint64 t_current_ntp;
1464 GstClockTimeDiff diff;
1466 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1468 /* We last saw a buffer with last_rtptime at last_rtime. Given a running_time
1469 * and an NTP time, we can scale the RTP timestamps so that they match the
1470 * given NTP time. for scaling, we assume that the slope of the rtptime vs
1471 * running_time vs ntptime curve is close to 1, which is certainly
1472 * sufficient for the frequency at which we report SR and the rate we send
1473 * out RTP packets. */
1474 t_rtp = src->last_rtptime;
1476 GST_DEBUG ("last_rtime %" GST_TIME_FORMAT ", last_rtptime %"
1477 G_GUINT64_FORMAT, GST_TIME_ARGS (src->last_rtime), t_rtp);
1479 if (src->clock_rate != -1) {
1480 /* get the diff between the clock running_time and the buffer running_time.
1481 * This is the elapsed time, as measured against the pipeline clock, between
1482 * when the rtp timestamp was observed and the current running_time.
1484 * We need to apply this diff to the RTP timestamp to get the RTP timestamp
1485 * for the given ntpnstime. */
1486 diff = GST_CLOCK_DIFF (src->last_rtime, running_time);
1487 GST_DEBUG ("running_time %" GST_TIME_FORMAT ", diff %" GST_STIME_FORMAT,
1488 GST_TIME_ARGS (running_time), GST_STIME_ARGS (diff));
1490 /* now translate the diff to RTP time, handle positive and negative cases.
1491 * If there is no diff, we already set rtptime correctly above. */
1493 t_rtp += gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1496 t_rtp -= gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1499 GST_WARNING ("no clock-rate, cannot interpolate rtp time");
1502 /* convert the NTP time in nanoseconds to 32.32 fixed point */
1503 t_current_ntp = gst_util_uint64_scale (ntpnstime, (1LL << 32), GST_SECOND);
1505 GST_DEBUG ("NTP %08x:%08x, RTP %" G_GUINT32_FORMAT,
1506 (guint32) (t_current_ntp >> 32), (guint32) (t_current_ntp & 0xffffffff),
1510 *ntptime = t_current_ntp;
1514 *packet_count = src->stats.packets_sent;
1516 *octet_count = src->stats.octets_sent;
1522 * rtp_source_get_new_rb:
1523 * @src: an #RTPSource
1524 * @time: the current time of the system clock
1525 * @fractionlost: fraction lost since last SR/RR
1526 * @packetslost: the cumulative number of packets lost
1527 * @exthighestseq: the extended last sequence number received
1528 * @jitter: the interarrival jitter (in clock rate units)
1529 * @lsr: the time of the last SR packet on this source
1530 * (in NTP Short Format, 16.16 fixed point)
1531 * @dlsr: the delay since the last SR packet
1532 * (in NTP Short Format, 16.16 fixed point)
1534 * Get new values to put into a new report block from this source.
1536 * Returns: %TRUE on success.
1539 rtp_source_get_new_rb (RTPSource * src, GstClockTime time,
1540 guint8 * fractionlost, gint32 * packetslost, guint32 * exthighestseq,
1541 guint32 * jitter, guint32 * lsr, guint32 * dlsr)
1543 RTPSourceStats *stats;
1544 guint64 extended_max, expected;
1545 guint64 expected_interval, received_interval, ntptime;
1546 gint64 lost, lost_interval;
1547 guint32 fraction, LSR, DLSR;
1548 GstClockTime sr_time;
1550 stats = &src->stats;
1552 extended_max = stats->cycles + stats->max_seq;
1553 expected = extended_max - stats->base_seq + 1;
1555 GST_DEBUG ("ext_max %" G_GUINT64_FORMAT ", expected %" G_GUINT64_FORMAT
1556 ", received %" G_GUINT64_FORMAT ", base_seq %" G_GUINT32_FORMAT,
1557 extended_max, expected, stats->packets_received, stats->base_seq);
1559 lost = expected - stats->packets_received;
1560 lost = CLAMP (lost, -0x800000, 0x7fffff);
1562 expected_interval = expected - stats->prev_expected;
1563 stats->prev_expected = expected;
1564 received_interval = stats->packets_received - stats->prev_received;
1565 stats->prev_received = stats->packets_received;
1567 lost_interval = expected_interval - received_interval;
1569 if (expected_interval == 0 || lost_interval <= 0)
1572 fraction = (lost_interval << 8) / expected_interval;
1574 GST_DEBUG ("add RR for SSRC %08x", src->ssrc);
1575 /* we scaled the jitter up for additional precision */
1576 GST_DEBUG ("fraction %" G_GUINT32_FORMAT ", lost %" G_GINT64_FORMAT
1577 ", extseq %" G_GUINT64_FORMAT ", jitter %d", fraction, lost,
1578 extended_max, stats->jitter >> 4);
1580 if (rtp_source_get_last_sr (src, &sr_time, &ntptime, NULL, NULL, NULL)) {
1583 /* LSR is middle 32 bits of the last ntptime */
1584 LSR = (ntptime >> 16) & 0xffffffff;
1585 diff = time - sr_time;
1586 GST_DEBUG ("last SR time diff %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
1587 /* DLSR, delay since last SR is expressed in 1/65536 second units */
1588 DLSR = gst_util_uint64_scale_int (diff, 65536, GST_SECOND);
1590 /* No valid SR received, LSR/DLSR are set to 0 then */
1591 GST_DEBUG ("no valid SR received");
1595 GST_DEBUG ("LSR %04x:%04x, DLSR %04x:%04x", LSR >> 16, LSR & 0xffff,
1596 DLSR >> 16, DLSR & 0xffff);
1599 *fractionlost = fraction;
1601 *packetslost = lost;
1603 *exthighestseq = extended_max;
1605 *jitter = stats->jitter >> 4;
1615 * rtp_source_get_last_sr:
1616 * @src: an #RTPSource
1617 * @time: time of packet arrival
1618 * @ntptime: the NTP time (in NTP Timestamp Format, 32.32 fixed point)
1619 * @rtptime: the RTP time (in clock rate units)
1620 * @packet_count: the packet count
1621 * @octet_count: the octet count
1623 * Get the values of the last sender report as set with rtp_source_process_sr().
1625 * Returns: %TRUE if there was a valid SR report.
1628 rtp_source_get_last_sr (RTPSource * src, GstClockTime * time, guint64 * ntptime,
1629 guint32 * rtptime, guint32 * packet_count, guint32 * octet_count)
1631 RTPSenderReport *curr;
1633 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1635 curr = &src->stats.sr[src->stats.curr_sr];
1636 if (!curr->is_valid)
1640 *ntptime = curr->ntptime;
1642 *rtptime = curr->rtptime;
1644 *packet_count = curr->packet_count;
1646 *octet_count = curr->octet_count;
1654 * rtp_source_get_last_rb:
1655 * @src: an #RTPSource
1656 * @fractionlost: fraction lost since last SR/RR
1657 * @packetslost: the cumulative number of packets lost
1658 * @exthighestseq: the extended last sequence number received
1659 * @jitter: the interarrival jitter (in clock rate units)
1660 * @lsr: the time of the last SR packet on this source
1661 * (in NTP Short Format, 16.16 fixed point)
1662 * @dlsr: the delay since the last SR packet
1663 * (in NTP Short Format, 16.16 fixed point)
1664 * @round_trip: the round-trip time
1665 * (in NTP Short Format, 16.16 fixed point)
1667 * Get the values of the last RB report set with rtp_source_process_rb().
1669 * Returns: %TRUE if there was a valid SB report.
1672 rtp_source_get_last_rb (RTPSource * src, guint8 * fractionlost,
1673 gint32 * packetslost, guint32 * exthighestseq, guint32 * jitter,
1674 guint32 * lsr, guint32 * dlsr, guint32 * round_trip)
1676 RTPReceiverReport *curr;
1678 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1680 curr = &src->stats.rr[src->stats.curr_rr];
1681 if (!curr->is_valid)
1685 *fractionlost = curr->fractionlost;
1687 *packetslost = curr->packetslost;
1689 *exthighestseq = curr->exthighestseq;
1691 *jitter = curr->jitter;
1697 *round_trip = curr->round_trip;
1703 find_conflicting_address (GList * conflicting_addresses,
1704 GSocketAddress * address, GstClockTime time)
1708 for (item = conflicting_addresses; item; item = g_list_next (item)) {
1709 RTPConflictingAddress *known_conflict = item->data;
1711 if (__g_socket_address_equal (address, known_conflict->address)) {
1712 known_conflict->time = time;
1721 add_conflicting_address (GList * conflicting_addresses,
1722 GSocketAddress * address, GstClockTime time)
1724 RTPConflictingAddress *new_conflict;
1726 new_conflict = g_slice_new (RTPConflictingAddress);
1728 new_conflict->address = G_SOCKET_ADDRESS (g_object_ref (address));
1729 new_conflict->time = time;
1731 return g_list_prepend (conflicting_addresses, new_conflict);
1735 timeout_conflicting_addresses (GList * conflicting_addresses,
1736 GstClockTime current_time)
1739 /* "a relatively long time" -- RFC 3550 section 8.2 */
1740 const GstClockTime collision_timeout =
1741 RTP_STATS_MIN_INTERVAL * GST_SECOND * 10;
1743 item = g_list_first (conflicting_addresses);
1745 RTPConflictingAddress *known_conflict = item->data;
1746 GList *next_item = g_list_next (item);
1748 if (known_conflict->time < current_time - collision_timeout) {
1751 conflicting_addresses = g_list_delete_link (conflicting_addresses, item);
1752 buf = __g_socket_address_to_string (known_conflict->address);
1753 GST_DEBUG ("collision %p timed out: %s", known_conflict, buf);
1755 rtp_conflicting_address_free (known_conflict);
1760 return conflicting_addresses;
1764 * rtp_source_find_conflicting_address:
1765 * @src: The source the packet came in
1766 * @address: address to check for
1767 * @time: The time when the packet that is possibly in conflict arrived
1769 * Checks if an address which has a conflict is already known. If it is
1770 * a known conflict, remember the time
1772 * Returns: TRUE if it was a known conflict, FALSE otherwise
1775 rtp_source_find_conflicting_address (RTPSource * src, GSocketAddress * address,
1778 return find_conflicting_address (src->conflicting_addresses, address, time);
1782 * rtp_source_add_conflicting_address:
1783 * @src: The source the packet came in
1784 * @address: address to remember
1785 * @time: The time when the packet that is in conflict arrived
1787 * Adds a new conflict address
1790 rtp_source_add_conflicting_address (RTPSource * src,
1791 GSocketAddress * address, GstClockTime time)
1793 src->conflicting_addresses =
1794 add_conflicting_address (src->conflicting_addresses, address, time);
1798 * rtp_source_timeout:
1799 * @src: The #RTPSource
1800 * @current_time: The current time
1801 * @feedback_retention_window: The running time before which retained feedback
1802 * packets have to be discarded
1804 * This is processed on each RTCP interval. It times out old collisions.
1805 * It also times out old retained feedback packets
1808 rtp_source_timeout (RTPSource * src, GstClockTime current_time,
1809 GstClockTime feedback_retention_window)
1813 src->conflicting_addresses =
1814 timeout_conflicting_addresses (src->conflicting_addresses, current_time);
1816 /* Time out AVPF packets that are older than the desired length */
1817 while ((pkt = g_queue_peek_tail (src->retained_feedback)) &&
1818 GST_BUFFER_PTS (pkt) < feedback_retention_window)
1819 gst_buffer_unref (g_queue_pop_tail (src->retained_feedback));
1823 compare_buffers (gconstpointer a, gconstpointer b, gpointer user_data)
1825 const GstBuffer *bufa = a;
1826 const GstBuffer *bufb = b;
1828 return GST_BUFFER_PTS (bufa) - GST_BUFFER_PTS (bufb);
1832 rtp_source_retain_rtcp_packet (RTPSource * src, GstRTCPPacket * packet,
1833 GstClockTime running_time)
1837 buffer = gst_buffer_copy_region (packet->rtcp->buffer, GST_BUFFER_COPY_MEMORY,
1838 packet->offset, (gst_rtcp_packet_get_length (packet) + 1) * 4);
1840 GST_BUFFER_PTS (buffer) = running_time;
1842 g_queue_insert_sorted (src->retained_feedback, buffer, compare_buffers, NULL);
1846 rtp_source_has_retained (RTPSource * src, GCompareFunc func, gconstpointer data)
1848 if (g_queue_find_custom (src->retained_feedback, data, func))
1855 * rtp_source_register_nack:
1856 * @src: The #RTPSource
1859 * Register that @seqnum has not been received from @src.
1862 rtp_source_register_nack (RTPSource * src, guint16 seqnum)
1865 guint32 dword = seqnum << 16;
1868 len = src->nacks->len;
1869 for (i = 0; i < len; i++) {
1873 tdword = g_array_index (src->nacks, guint32, i);
1874 tseq = tdword >> 16;
1876 diff = gst_rtp_buffer_compare_seqnum (tseq, seqnum);
1880 /* we already have this seqnum */
1883 /* it comes before the recorded seqnum, FIXME, we could merge it
1884 * if not to far away */
1886 GST_DEBUG ("insert NACK #%u at %u", seqnum, i);
1887 g_array_insert_val (src->nacks, i, dword);
1888 } else if (diff < 16) {
1889 /* we can merge it */
1890 dword = g_array_index (src->nacks, guint32, i);
1891 dword |= 1 << (diff - 1);
1892 GST_DEBUG ("merge NACK #%u at %u with NACK #%u -> 0x%08x", seqnum, i,
1893 dword >> 16, dword);
1894 g_array_index (src->nacks, guint32, i) = dword;
1896 GST_DEBUG ("append NACK #%u", seqnum);
1897 g_array_append_val (src->nacks, dword);
1899 src->send_nack = TRUE;
1903 * rtp_source_get_nacks:
1904 * @src: The #RTPSource
1905 * @n_nacks: result number of nacks
1907 * Get the registered NACKS since the last rtp_source_clear_nacks().
1909 * Returns: an array of @n_nacks seqnum values.
1912 rtp_source_get_nacks (RTPSource * src, guint * n_nacks)
1915 *n_nacks = src->nacks->len;
1917 return (guint32 *) src->nacks->data;
1921 rtp_source_clear_nacks (RTPSource * src)
1923 g_array_set_size (src->nacks, 0);
1924 src->send_nack = FALSE;