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 in the form user@host
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 8-bit 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 (seconds in NTP Short Format, 16.16 fixed point)
196 * "sent-rb-dlsr" G_TYPE_UINT delay since last SR (seconds 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 8-bit 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 (seconds in NTP Short Format, 16.16 fixed point)
208 * "rb-dlsr" G_TYPE_UINT delay since last SR (seconds 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 (seconds 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);
258 g_queue_foreach (src->retained_feedback, (GFunc) gst_buffer_unref, NULL);
259 g_queue_clear (src->retained_feedback);
260 src->last_rtptime = -1;
262 src->stats.cycles = -1;
263 src->stats.jitter = 0;
264 src->stats.transit = -1;
265 src->stats.curr_sr = 0;
266 src->stats.sr[0].is_valid = FALSE;
267 src->stats.curr_rr = 0;
268 src->stats.rr[0].is_valid = FALSE;
269 src->stats.prev_rtptime = GST_CLOCK_TIME_NONE;
270 src->stats.prev_rtcptime = GST_CLOCK_TIME_NONE;
271 src->stats.last_rtptime = GST_CLOCK_TIME_NONE;
272 src->stats.last_rtcptime = GST_CLOCK_TIME_NONE;
273 g_array_set_size (src->nacks, 0);
275 src->stats.sent_pli_count = 0;
276 src->stats.sent_fir_count = 0;
277 src->stats.sent_nack_count = 0;
278 src->stats.recv_nack_count = 0;
282 rtp_source_init (RTPSource * src)
284 /* sources are initialy on probation until we receive enough valid RTP
285 * packets or a valid RTCP packet */
286 src->validated = FALSE;
287 src->internal = FALSE;
288 src->probation = DEFAULT_PROBATION;
289 src->curr_probation = src->probation;
290 src->closing = FALSE;
291 src->max_dropout_time = DEFAULT_MAX_DROPOUT_TIME;
292 src->max_misorder_time = DEFAULT_MAX_MISORDER_TIME;
294 src->sdes = gst_structure_new_empty ("application/x-rtp-source-sdes");
297 src->clock_rate = -1;
298 src->packets = g_queue_new ();
299 src->seqnum_offset = -1;
301 src->retained_feedback = g_queue_new ();
302 src->nacks = g_array_new (FALSE, FALSE, sizeof (guint32));
304 src->reported_in_sr_of = g_hash_table_new (g_direct_hash, g_direct_equal);
306 src->last_keyframe_request = GST_CLOCK_TIME_NONE;
308 rtp_source_reset (src);
314 rtp_conflicting_address_free (RTPConflictingAddress * addr)
316 g_object_unref (addr->address);
317 g_slice_free (RTPConflictingAddress, addr);
321 rtp_source_finalize (GObject * object)
325 src = RTP_SOURCE_CAST (object);
327 g_queue_foreach (src->packets, (GFunc) gst_buffer_unref, NULL);
328 g_queue_free (src->packets);
330 gst_structure_free (src->sdes);
332 g_free (src->bye_reason);
334 gst_caps_replace (&src->caps, NULL);
336 g_list_free_full (src->conflicting_addresses,
337 (GDestroyNotify) rtp_conflicting_address_free);
338 g_queue_foreach (src->retained_feedback, (GFunc) gst_buffer_unref, NULL);
339 g_queue_free (src->retained_feedback);
341 g_array_free (src->nacks, TRUE);
344 g_object_unref (src->rtp_from);
346 g_object_unref (src->rtcp_from);
348 g_hash_table_unref (src->reported_in_sr_of);
350 G_OBJECT_CLASS (rtp_source_parent_class)->finalize (object);
353 static GstStructure *
354 rtp_source_create_stats (RTPSource * src)
357 gboolean is_sender = src->is_sender;
358 gboolean internal = src->internal;
361 guint8 fractionlost = 0;
362 gint32 packetslost = 0;
363 guint32 exthighestseq = 0;
367 guint32 round_trip = 0;
369 GstClockTime time = 0;
372 guint32 packet_count = 0;
373 guint32 octet_count = 0;
376 /* common data for all types of sources */
377 s = gst_structure_new ("application/x-rtp-source-stats",
378 "ssrc", G_TYPE_UINT, (guint) src->ssrc,
379 "internal", G_TYPE_BOOLEAN, internal,
380 "validated", G_TYPE_BOOLEAN, src->validated,
381 "received-bye", G_TYPE_BOOLEAN, src->marked_bye,
382 "is-csrc", G_TYPE_BOOLEAN, src->is_csrc,
383 "is-sender", G_TYPE_BOOLEAN, is_sender,
384 "seqnum-base", G_TYPE_INT, src->seqnum_offset,
385 "clock-rate", G_TYPE_INT, src->clock_rate, NULL);
387 /* add address and port */
389 address_str = __g_socket_address_to_string (src->rtp_from);
390 gst_structure_set (s, "rtp-from", G_TYPE_STRING, address_str, NULL);
391 g_free (address_str);
393 if (src->rtcp_from) {
394 address_str = __g_socket_address_to_string (src->rtcp_from);
395 gst_structure_set (s, "rtcp-from", G_TYPE_STRING, address_str, NULL);
396 g_free (address_str);
399 gst_structure_set (s,
400 "octets-sent", G_TYPE_UINT64, src->stats.octets_sent,
401 "packets-sent", G_TYPE_UINT64, src->stats.packets_sent,
402 "octets-received", G_TYPE_UINT64, src->stats.octets_received,
403 "packets-received", G_TYPE_UINT64, src->stats.packets_received,
404 "bitrate", G_TYPE_UINT64, src->bitrate,
405 "packets-lost", G_TYPE_INT,
406 (gint) rtp_stats_get_packets_lost (&src->stats), "jitter", G_TYPE_UINT,
407 (guint) (src->stats.jitter >> 4),
408 "sent-pli-count", G_TYPE_UINT, src->stats.sent_pli_count,
409 "recv-pli-count", G_TYPE_UINT, src->stats.recv_pli_count,
410 "sent-fir-count", G_TYPE_UINT, src->stats.sent_fir_count,
411 "recv-fir-count", G_TYPE_UINT, src->stats.recv_fir_count,
412 "sent-nack-count", G_TYPE_UINT, src->stats.sent_nack_count,
413 "recv-nack-count", G_TYPE_UINT, src->stats.recv_nack_count, NULL);
415 /* get the last SR. */
416 have_sr = rtp_source_get_last_sr (src, &time, &ntptime, &rtptime,
417 &packet_count, &octet_count);
418 gst_structure_set (s,
419 "have-sr", G_TYPE_BOOLEAN, have_sr,
420 "sr-ntptime", G_TYPE_UINT64, ntptime,
421 "sr-rtptime", G_TYPE_UINT, (guint) rtptime,
422 "sr-octet-count", G_TYPE_UINT, (guint) octet_count,
423 "sr-packet-count", G_TYPE_UINT, (guint) packet_count, NULL);
426 /* get the last RB we sent */
427 gst_structure_set (s,
428 "sent-rb", G_TYPE_BOOLEAN, src->last_rr.is_valid,
429 "sent-rb-fractionlost", G_TYPE_UINT, (guint) src->last_rr.fractionlost,
430 "sent-rb-packetslost", G_TYPE_INT, (gint) src->last_rr.packetslost,
431 "sent-rb-exthighestseq", G_TYPE_UINT,
432 (guint) src->last_rr.exthighestseq, "sent-rb-jitter", G_TYPE_UINT,
433 (guint) src->last_rr.jitter, "sent-rb-lsr", G_TYPE_UINT,
434 (guint) src->last_rr.lsr, "sent-rb-dlsr", G_TYPE_UINT,
435 (guint) src->last_rr.dlsr, NULL);
437 /* get the last RB */
438 have_rb = rtp_source_get_last_rb (src, &fractionlost, &packetslost,
439 &exthighestseq, &jitter, &lsr, &dlsr, &round_trip);
441 gst_structure_set (s,
442 "have-rb", G_TYPE_BOOLEAN, have_rb,
443 "rb-fractionlost", G_TYPE_UINT, (guint) fractionlost,
444 "rb-packetslost", G_TYPE_INT, (gint) packetslost,
445 "rb-exthighestseq", G_TYPE_UINT, (guint) exthighestseq,
446 "rb-jitter", G_TYPE_UINT, (guint) jitter,
447 "rb-lsr", G_TYPE_UINT, (guint) lsr,
448 "rb-dlsr", G_TYPE_UINT, (guint) dlsr,
449 "rb-round-trip", G_TYPE_UINT, (guint) round_trip, NULL);
456 * rtp_source_get_sdes_struct:
457 * @src: an #RTPSource
459 * Get the SDES from @src. See the SDES property for more details.
461 * Returns: %GstStructure of type "application/x-rtp-source-sdes". The result is
462 * valid until the SDES items of @src are modified.
465 rtp_source_get_sdes_struct (RTPSource * src)
467 g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
473 sdes_struct_compare_func (GQuark field_id, const GValue * value,
479 old = GST_STRUCTURE (user_data);
480 field = g_quark_to_string (field_id);
482 if (!gst_structure_has_field (old, field))
485 g_assert (G_VALUE_HOLDS_STRING (value));
487 return strcmp (g_value_get_string (value), gst_structure_get_string (old,
492 * rtp_source_set_sdes_struct:
493 * @src: an #RTPSource
494 * @sdes: the SDES structure
496 * Store the @sdes in @src. @sdes must be a structure of type
497 * "application/x-rtp-source-sdes", see the SDES property for more details.
499 * This function takes ownership of @sdes.
501 * Returns: %FALSE if the SDES was unchanged.
504 rtp_source_set_sdes_struct (RTPSource * src, GstStructure * sdes)
508 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
509 g_return_val_if_fail (strcmp (gst_structure_get_name (sdes),
510 "application/x-rtp-source-sdes") == 0, FALSE);
512 changed = !gst_structure_foreach (sdes, sdes_struct_compare_func, src->sdes);
515 gst_structure_free (src->sdes);
518 gst_structure_free (sdes);
524 rtp_source_set_property (GObject * object, guint prop_id,
525 const GValue * value, GParamSpec * pspec)
529 src = RTP_SOURCE (object);
533 src->ssrc = g_value_get_uint (value);
536 src->probation = g_value_get_uint (value);
538 case PROP_MAX_DROPOUT_TIME:
539 src->max_dropout_time = g_value_get_uint (value);
541 case PROP_MAX_MISORDER_TIME:
542 src->max_misorder_time = g_value_get_uint (value);
545 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
551 rtp_source_get_property (GObject * object, guint prop_id,
552 GValue * value, GParamSpec * pspec)
556 src = RTP_SOURCE (object);
560 g_value_set_uint (value, rtp_source_get_ssrc (src));
563 g_value_set_boolean (value, rtp_source_is_as_csrc (src));
565 case PROP_IS_VALIDATED:
566 g_value_set_boolean (value, rtp_source_is_validated (src));
569 g_value_set_boolean (value, rtp_source_is_sender (src));
572 g_value_set_boxed (value, rtp_source_get_sdes_struct (src));
575 g_value_take_boxed (value, rtp_source_create_stats (src));
578 g_value_set_uint (value, src->probation);
580 case PROP_MAX_DROPOUT_TIME:
581 g_value_set_uint (value, src->max_dropout_time);
583 case PROP_MAX_MISORDER_TIME:
584 g_value_set_uint (value, src->max_misorder_time);
587 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
596 * Create a #RTPSource with @ssrc.
598 * Returns: a new #RTPSource. Use g_object_unref() after usage.
601 rtp_source_new (guint32 ssrc)
605 src = g_object_new (RTP_TYPE_SOURCE, NULL);
612 * rtp_source_set_callbacks:
613 * @src: an #RTPSource
614 * @cb: callback functions
615 * @user_data: user data
617 * Set the callbacks for the source.
620 rtp_source_set_callbacks (RTPSource * src, RTPSourceCallbacks * cb,
623 g_return_if_fail (RTP_IS_SOURCE (src));
625 src->callbacks.push_rtp = cb->push_rtp;
626 src->callbacks.clock_rate = cb->clock_rate;
627 src->user_data = user_data;
631 * rtp_source_get_ssrc:
632 * @src: an #RTPSource
634 * Get the SSRC of @source.
636 * Returns: the SSRC of src.
639 rtp_source_get_ssrc (RTPSource * src)
643 g_return_val_if_fail (RTP_IS_SOURCE (src), 0);
651 * rtp_source_set_as_csrc:
652 * @src: an #RTPSource
654 * Configure @src as a CSRC, this will also validate @src.
657 rtp_source_set_as_csrc (RTPSource * src)
659 g_return_if_fail (RTP_IS_SOURCE (src));
661 src->validated = TRUE;
666 * rtp_source_is_as_csrc:
667 * @src: an #RTPSource
669 * Check if @src is a contributing source.
671 * Returns: %TRUE if @src is acting as a contributing source.
674 rtp_source_is_as_csrc (RTPSource * src)
678 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
680 result = src->is_csrc;
686 * rtp_source_is_active:
687 * @src: an #RTPSource
689 * Check if @src is an active source. A source is active if it has been
690 * validated and has not yet received a BYE packet
692 * Returns: %TRUE if @src is an qactive source.
695 rtp_source_is_active (RTPSource * src)
699 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
701 result = RTP_SOURCE_IS_ACTIVE (src);
707 * rtp_source_is_validated:
708 * @src: an #RTPSource
710 * Check if @src is a validated source.
712 * Returns: %TRUE if @src is a validated source.
715 rtp_source_is_validated (RTPSource * src)
719 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
721 result = src->validated;
727 * rtp_source_is_sender:
728 * @src: an #RTPSource
730 * Check if @src is a sending source.
732 * Returns: %TRUE if @src is a sending source.
735 rtp_source_is_sender (RTPSource * src)
739 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
741 result = RTP_SOURCE_IS_SENDER (src);
747 * rtp_source_is_marked_bye:
748 * @src: an #RTPSource
750 * Check if @src is marked as leaving the session with a BYE packet.
752 * Returns: %TRUE if @src has been marked BYE.
755 rtp_source_is_marked_bye (RTPSource * src)
759 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
761 result = RTP_SOURCE_IS_MARKED_BYE (src);
768 * rtp_source_get_bye_reason:
769 * @src: an #RTPSource
771 * Get the BYE reason for @src. Check if the source is marked as leaving the
772 * session with a BYE message first with rtp_source_is_marked_bye().
774 * Returns: The BYE reason or NULL when no reason was given or the source was
775 * not marked BYE yet. g_free() after usage.
778 rtp_source_get_bye_reason (RTPSource * src)
782 g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
784 result = g_strdup (src->bye_reason);
790 * rtp_source_update_caps:
791 * @src: an #RTPSource
794 * Parse @caps and store all relevant information in @source.
797 rtp_source_update_caps (RTPSource * src, GstCaps * caps)
804 /* nothing changed, return */
805 if (caps == NULL || src->caps == caps)
808 s = gst_caps_get_structure (caps, 0);
810 rtx = (gst_structure_get_uint (s, "rtx-ssrc", &val) && val == src->ssrc);
812 if (gst_structure_get_int (s, rtx ? "rtx-payload" : "payload", &ival))
817 GST_DEBUG ("got %spayload %d", rtx ? "rtx " : "", src->payload);
819 if (gst_structure_get_int (s, "clock-rate", &ival))
820 src->clock_rate = ival;
822 src->clock_rate = -1;
824 GST_DEBUG ("got clock-rate %d", src->clock_rate);
826 if (gst_structure_get_uint (s, rtx ? "rtx-seqnum-offset" : "seqnum-offset",
828 src->seqnum_offset = val;
830 src->seqnum_offset = -1;
832 GST_DEBUG ("got %sseqnum-offset %" G_GINT32_FORMAT, rtx ? "rtx " : "",
835 gst_caps_replace (&src->caps, caps);
839 * rtp_source_set_rtp_from:
840 * @src: an #RTPSource
841 * @address: the RTP address to set
843 * Set that @src is receiving RTP packets from @address. This is used for
844 * collistion checking.
847 rtp_source_set_rtp_from (RTPSource * src, GSocketAddress * address)
849 g_return_if_fail (RTP_IS_SOURCE (src));
852 g_object_unref (src->rtp_from);
853 src->rtp_from = G_SOCKET_ADDRESS (g_object_ref (address));
857 * rtp_source_set_rtcp_from:
858 * @src: an #RTPSource
859 * @address: the RTCP address to set
861 * Set that @src is receiving RTCP packets from @address. This is used for
862 * collistion checking.
865 rtp_source_set_rtcp_from (RTPSource * src, GSocketAddress * address)
867 g_return_if_fail (RTP_IS_SOURCE (src));
870 g_object_unref (src->rtcp_from);
871 src->rtcp_from = G_SOCKET_ADDRESS (g_object_ref (address));
875 push_packet (RTPSource * src, GstBuffer * buffer)
877 GstFlowReturn ret = GST_FLOW_OK;
879 /* push queued packets first if any */
880 while (!g_queue_is_empty (src->packets)) {
881 GstBuffer *buffer = GST_BUFFER_CAST (g_queue_pop_head (src->packets));
883 GST_LOG ("pushing queued packet");
884 if (src->callbacks.push_rtp)
885 src->callbacks.push_rtp (src, buffer, src->user_data);
887 gst_buffer_unref (buffer);
889 GST_LOG ("pushing new packet");
891 if (src->callbacks.push_rtp)
892 ret = src->callbacks.push_rtp (src, buffer, src->user_data);
894 gst_buffer_unref (buffer);
900 get_clock_rate (RTPSource * src, guint8 payload)
902 if (src->payload == -1) {
903 /* first payload received, nothing was in the caps, lock on to this payload */
904 src->payload = payload;
905 GST_DEBUG ("first payload %d", payload);
906 } else if (payload != src->payload) {
907 /* we have a different payload than before, reset the clock-rate */
908 GST_DEBUG ("new payload %d", payload);
909 src->payload = payload;
910 src->clock_rate = -1;
911 src->stats.transit = -1;
914 if (src->clock_rate == -1) {
915 gint clock_rate = -1;
917 if (src->callbacks.clock_rate)
918 clock_rate = src->callbacks.clock_rate (src, payload, src->user_data);
920 GST_DEBUG ("got clock-rate %d", clock_rate);
922 src->clock_rate = clock_rate;
923 gst_rtp_packet_rate_ctx_reset (&src->packet_rate_ctx, clock_rate);
925 return src->clock_rate;
928 /* Jitter is the variation in the delay of received packets in a flow. It is
929 * measured by comparing the interval when RTP packets were sent to the interval
930 * at which they were received. For instance, if packet #1 and packet #2 leave
931 * 50 milliseconds apart and arrive 60 milliseconds apart, then the jitter is 10
934 calculate_jitter (RTPSource * src, RTPPacketInfo * pinfo)
936 GstClockTime running_time;
937 guint32 rtparrival, transit, rtptime;
942 /* get arrival time */
943 if ((running_time = pinfo->running_time) == GST_CLOCK_TIME_NONE)
948 GST_LOG ("SSRC %08x got payload %d", src->ssrc, pt);
951 if ((clock_rate = get_clock_rate (src, pt)) == -1)
954 rtptime = pinfo->rtptime;
956 /* convert arrival time to RTP timestamp units, truncate to 32 bits, we don't
957 * care about the absolute value, just the difference. */
958 rtparrival = gst_util_uint64_scale_int (running_time, clock_rate, GST_SECOND);
960 /* transit time is difference with RTP timestamp */
961 transit = rtparrival - rtptime;
963 /* get ABS diff with previous transit time */
964 if (src->stats.transit != -1) {
965 if (transit > src->stats.transit)
966 diff = transit - src->stats.transit;
968 diff = src->stats.transit - transit;
972 src->stats.transit = transit;
974 /* update jitter, the value we store is scaled up so we can keep precision. */
975 src->stats.jitter += diff - ((src->stats.jitter + 8) >> 4);
977 src->stats.prev_rtptime = src->stats.last_rtptime;
978 src->stats.last_rtptime = rtparrival;
980 GST_LOG ("rtparrival %u, rtptime %u, clock-rate %d, diff %d, jitter: %f",
981 rtparrival, rtptime, clock_rate, diff, (src->stats.jitter) / 16.0);
988 GST_WARNING ("cannot get current running_time");
993 GST_WARNING ("cannot get clock-rate for pt %d", pt);
999 init_seq (RTPSource * src, guint16 seq)
1001 src->stats.base_seq = seq;
1002 src->stats.max_seq = seq;
1003 src->stats.bad_seq = RTP_SEQ_MOD + 1; /* so seq == bad_seq is false */
1004 src->stats.cycles = 0;
1005 src->stats.packets_received = 0;
1006 src->stats.octets_received = 0;
1007 src->stats.bytes_received = 0;
1008 src->stats.prev_received = 0;
1009 src->stats.prev_expected = 0;
1010 src->stats.recv_pli_count = 0;
1011 src->stats.recv_fir_count = 0;
1013 GST_DEBUG ("base_seq %d", seq);
1016 #define BITRATE_INTERVAL (2 * GST_SECOND)
1019 do_bitrate_estimation (RTPSource * src, GstClockTime running_time,
1020 guint64 * bytes_handled)
1024 if (src->prev_rtime) {
1025 elapsed = running_time - src->prev_rtime;
1027 if (elapsed > BITRATE_INTERVAL) {
1030 rate = gst_util_uint64_scale (*bytes_handled, 8 * GST_SECOND, elapsed);
1032 GST_LOG ("Elapsed %" G_GUINT64_FORMAT ", bytes %" G_GUINT64_FORMAT
1033 ", rate %" G_GUINT64_FORMAT, elapsed, *bytes_handled, rate);
1035 if (src->bitrate == 0)
1036 src->bitrate = rate;
1038 src->bitrate = ((src->bitrate * 3) + rate) / 4;
1040 src->prev_rtime = running_time;
1044 GST_LOG ("Reset bitrate measurement");
1045 src->prev_rtime = running_time;
1051 update_receiver_stats (RTPSource * src, RTPPacketInfo * pinfo,
1052 gboolean is_receive)
1054 guint16 seqnr, expected;
1055 RTPSourceStats *stats;
1057 gint32 packet_rate, max_dropout, max_misorder;
1059 stats = &src->stats;
1061 seqnr = pinfo->seqnum;
1064 gst_rtp_packet_rate_ctx_update (&src->packet_rate_ctx, pinfo->seqnum,
1067 gst_rtp_packet_rate_ctx_get_max_dropout (&src->packet_rate_ctx,
1068 src->max_dropout_time);
1070 gst_rtp_packet_rate_ctx_get_max_misorder (&src->packet_rate_ctx,
1071 src->max_misorder_time);
1072 GST_TRACE ("SSRC %08x, packet_rate: %d, max_dropout: %d, max_misorder: %d",
1073 src->ssrc, packet_rate, max_dropout, max_misorder);
1075 if (stats->cycles == -1) {
1076 GST_DEBUG ("received first packet");
1077 /* first time we heard of this source */
1078 init_seq (src, seqnr);
1079 src->stats.max_seq = seqnr - 1;
1080 src->curr_probation = src->probation;
1084 expected = src->stats.max_seq + 1;
1085 delta = gst_rtp_buffer_compare_seqnum (expected, seqnr);
1087 /* if we are still on probation, check seqnum */
1088 if (src->curr_probation) {
1089 /* when in probation, we require consecutive seqnums */
1091 /* expected packet */
1092 GST_DEBUG ("probation: seqnr %d == expected %d", seqnr, expected);
1093 src->curr_probation--;
1094 if (seqnr < stats->max_seq) {
1095 /* sequence number wrapped - count another 64K cycle. */
1096 stats->cycles += RTP_SEQ_MOD;
1098 src->stats.max_seq = seqnr;
1100 if (src->curr_probation == 0) {
1101 GST_DEBUG ("probation done!");
1102 init_seq (src, seqnr);
1106 GST_DEBUG ("probation %d: queue packet", src->curr_probation);
1107 /* when still in probation, keep packets in a list. */
1108 g_queue_push_tail (src->packets, pinfo->data);
1110 /* remove packets from queue if there are too many */
1111 while (g_queue_get_length (src->packets) > RTP_MAX_PROBATION_LEN) {
1112 q = g_queue_pop_head (src->packets);
1113 gst_buffer_unref (q);
1118 /* unexpected seqnum in probation */
1119 goto probation_seqnum;
1121 } else if (delta >= 0 && delta < max_dropout) {
1122 /* Clear bad packets */
1123 stats->bad_seq = RTP_SEQ_MOD + 1; /* so seq == bad_seq is false */
1124 g_queue_foreach (src->packets, (GFunc) gst_buffer_unref, NULL);
1125 g_queue_clear (src->packets);
1127 /* in order, with permissible gap */
1128 if (seqnr < stats->max_seq) {
1129 /* sequence number wrapped - count another 64K cycle. */
1130 stats->cycles += RTP_SEQ_MOD;
1132 stats->max_seq = seqnr;
1133 } else if (delta < -max_misorder || delta >= max_dropout) {
1134 /* the sequence number made a very large jump */
1135 if (seqnr == stats->bad_seq && src->packets->head) {
1136 /* two sequential packets -- assume that the other side
1137 * restarted without telling us so just re-sync
1138 * (i.e., pretend this was the first packet). */
1139 init_seq (src, seqnr);
1141 /* unacceptable jump */
1142 stats->bad_seq = (seqnr + 1) & (RTP_SEQ_MOD - 1);
1143 g_queue_foreach (src->packets, (GFunc) gst_buffer_unref, NULL);
1144 g_queue_clear (src->packets);
1145 g_queue_push_tail (src->packets, pinfo->data);
1149 } else { /* delta < 0 && delta >= -max_misorder */
1150 /* Clear bad packets */
1151 stats->bad_seq = RTP_SEQ_MOD + 1; /* so seq == bad_seq is false */
1152 g_queue_foreach (src->packets, (GFunc) gst_buffer_unref, NULL);
1153 g_queue_clear (src->packets);
1155 /* duplicate or reordered packet, will be filtered by jitterbuffer. */
1156 GST_INFO ("duplicate or reordered packet (seqnr %u, expected %u)",
1161 src->stats.octets_received += pinfo->payload_len;
1162 src->stats.bytes_received += pinfo->bytes;
1163 src->stats.packets_received++;
1164 /* for the bitrate estimation */
1165 src->bytes_received += pinfo->payload_len;
1167 GST_LOG ("seq %u, PC: %" G_GUINT64_FORMAT ", OC: %" G_GUINT64_FORMAT,
1168 seqnr, src->stats.packets_received, src->stats.octets_received);
1180 ("unacceptable seqnum received (seqnr %u, delta %d, packet_rate: %d, max_dropout: %d, max_misorder: %d)",
1181 seqnr, delta, packet_rate, max_dropout, max_misorder);
1186 GST_WARNING ("probation: seqnr %d != expected %d", seqnr, expected);
1187 src->curr_probation = src->probation;
1188 src->stats.max_seq = seqnr;
1194 * rtp_source_process_rtp:
1195 * @src: an #RTPSource
1196 * @pinfo: an #RTPPacketInfo
1198 * Let @src handle the incomming RTP packet described in @pinfo.
1200 * Returns: a #GstFlowReturn.
1203 rtp_source_process_rtp (RTPSource * src, RTPPacketInfo * pinfo)
1205 GstFlowReturn result;
1207 g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
1208 g_return_val_if_fail (pinfo != NULL, GST_FLOW_ERROR);
1210 if (!update_receiver_stats (src, pinfo, TRUE))
1213 /* the source that sent the packet must be a sender */
1214 src->is_sender = TRUE;
1215 src->validated = TRUE;
1217 do_bitrate_estimation (src, pinfo->running_time, &src->bytes_received);
1219 /* calculate jitter for the stats */
1220 calculate_jitter (src, pinfo);
1222 /* we're ready to push the RTP packet now */
1223 result = push_packet (src, pinfo->data);
1230 * rtp_source_mark_bye:
1231 * @src: an #RTPSource
1232 * @reason: the reason for leaving
1234 * Mark @src in the BYE state. This can happen when the source wants to
1235 * leave the sesssion or when a BYE packets has been received.
1237 * This will make the source inactive.
1240 rtp_source_mark_bye (RTPSource * src, const gchar * reason)
1242 g_return_if_fail (RTP_IS_SOURCE (src));
1244 GST_DEBUG ("marking SSRC %08x as BYE, reason: %s", src->ssrc,
1245 GST_STR_NULL (reason));
1247 /* copy the reason and mark as bye */
1248 g_free (src->bye_reason);
1249 src->bye_reason = g_strdup (reason);
1250 src->marked_bye = TRUE;
1254 * rtp_source_send_rtp:
1255 * @src: an #RTPSource
1256 * @data: an RTP buffer or a list of RTP buffers
1257 * @is_list: if @data is a buffer or list
1258 * @running_time: the running time of @data
1260 * Send @data (an RTP buffer or list of buffers) originating from @src.
1261 * This will make @src a sender. This function takes ownership of @data and
1262 * modifies the SSRC in the RTP packet to that of @src when needed.
1264 * Returns: a #GstFlowReturn.
1267 rtp_source_send_rtp (RTPSource * src, RTPPacketInfo * pinfo)
1269 GstFlowReturn result;
1270 GstClockTime running_time;
1272 guint64 ext_rtptime;
1273 guint64 rt_diff, rtp_diff;
1275 g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
1277 /* we are a sender now */
1278 src->is_sender = TRUE;
1280 /* we are also a receiver of our packets */
1281 if (!update_receiver_stats (src, pinfo, FALSE))
1284 if (src->pt_set && src->pt != pinfo->pt) {
1285 GST_WARNING ("Changing pt from %u to %u for SSRC %u", src->pt, pinfo->pt,
1289 src->pt = pinfo->pt;
1292 /* update stats for the SR */
1293 src->stats.packets_sent += pinfo->packets;
1294 src->stats.octets_sent += pinfo->payload_len;
1295 src->bytes_sent += pinfo->payload_len;
1297 running_time = pinfo->running_time;
1299 do_bitrate_estimation (src, running_time, &src->bytes_sent);
1301 rtptime = pinfo->rtptime;
1303 ext_rtptime = src->last_rtptime;
1304 ext_rtptime = gst_rtp_buffer_ext_timestamp (&ext_rtptime, rtptime);
1306 GST_LOG ("SSRC %08x, RTP %" G_GUINT64_FORMAT ", running_time %"
1307 GST_TIME_FORMAT, src->ssrc, ext_rtptime, GST_TIME_ARGS (running_time));
1309 if (ext_rtptime > src->last_rtptime) {
1310 rtp_diff = ext_rtptime - src->last_rtptime;
1311 rt_diff = running_time - src->last_rtime;
1313 /* calc the diff so we can detect drift at the sender. This can also be used
1314 * to guestimate the clock rate if the NTP time is locked to the RTP
1315 * timestamps (as is the case when the capture device is providing the clock). */
1316 GST_LOG ("SSRC %08x, diff RTP %" G_GUINT64_FORMAT ", diff running_time %"
1317 GST_TIME_FORMAT, src->ssrc, rtp_diff, GST_TIME_ARGS (rt_diff));
1320 /* we keep track of the last received RTP timestamp and the corresponding
1321 * buffer running_time so that we can use this info when constructing SR reports */
1322 src->last_rtime = running_time;
1323 src->last_rtptime = ext_rtptime;
1326 if (!src->callbacks.push_rtp)
1329 GST_LOG ("pushing RTP %s %" G_GUINT64_FORMAT,
1330 pinfo->is_list ? "list" : "packet", src->stats.packets_sent);
1332 result = src->callbacks.push_rtp (src, pinfo->data, src->user_data);
1340 GST_WARNING ("no callback installed, dropping packet");
1346 * rtp_source_process_sr:
1347 * @src: an #RTPSource
1348 * @time: time of packet arrival
1349 * @ntptime: the NTP time (in NTP Timestamp Format, 32.32 fixed point)
1350 * @rtptime: the RTP time (in clock rate units)
1351 * @packet_count: the packet count
1352 * @octet_count: the octet count
1354 * Update the sender report in @src.
1357 rtp_source_process_sr (RTPSource * src, GstClockTime time, guint64 ntptime,
1358 guint32 rtptime, guint32 packet_count, guint32 octet_count)
1360 RTPSenderReport *curr;
1363 g_return_if_fail (RTP_IS_SOURCE (src));
1365 GST_DEBUG ("got SR packet: SSRC %08x, NTP %08x:%08x, RTP %" G_GUINT32_FORMAT
1366 ", PC %" G_GUINT32_FORMAT ", OC %" G_GUINT32_FORMAT, src->ssrc,
1367 (guint32) (ntptime >> 32), (guint32) (ntptime & 0xffffffff), rtptime,
1368 packet_count, octet_count);
1370 curridx = src->stats.curr_sr ^ 1;
1371 curr = &src->stats.sr[curridx];
1373 /* this is a sender now */
1374 src->is_sender = TRUE;
1376 /* update current */
1377 curr->is_valid = TRUE;
1378 curr->ntptime = ntptime;
1379 curr->rtptime = rtptime;
1380 curr->packet_count = packet_count;
1381 curr->octet_count = octet_count;
1385 src->stats.curr_sr = curridx;
1387 src->stats.prev_rtcptime = src->stats.last_rtcptime;
1388 src->stats.last_rtcptime = time;
1392 * rtp_source_process_rb:
1393 * @src: an #RTPSource
1394 * @ntpnstime: the current time in nanoseconds since 1970
1395 * @fractionlost: fraction lost since last SR/RR
1396 * @packetslost: the cumulative number of packets lost
1397 * @exthighestseq: the extended last sequence number received
1398 * @jitter: the interarrival jitter (in clock rate units)
1399 * @lsr: the time of the last SR packet on this source
1400 * (in NTP Short Format, 16.16 fixed point)
1401 * @dlsr: the delay since the last SR packet
1402 * (in NTP Short Format, 16.16 fixed point)
1404 * Update the report block in @src.
1407 rtp_source_process_rb (RTPSource * src, guint64 ntpnstime,
1408 guint8 fractionlost, gint32 packetslost, guint32 exthighestseq,
1409 guint32 jitter, guint32 lsr, guint32 dlsr)
1411 RTPReceiverReport *curr;
1416 g_return_if_fail (RTP_IS_SOURCE (src));
1418 GST_DEBUG ("got RB packet: SSRC %08x, FL %2x, PL %d, HS %" G_GUINT32_FORMAT
1419 ", jitter %" G_GUINT32_FORMAT ", LSR %04x:%04x, DLSR %04x:%04x",
1420 src->ssrc, fractionlost, packetslost, exthighestseq, jitter, lsr >> 16,
1421 lsr & 0xffff, dlsr >> 16, dlsr & 0xffff);
1423 curridx = src->stats.curr_rr ^ 1;
1424 curr = &src->stats.rr[curridx];
1426 /* update current */
1427 curr->is_valid = TRUE;
1428 curr->fractionlost = fractionlost;
1429 curr->packetslost = packetslost;
1430 curr->exthighestseq = exthighestseq;
1431 curr->jitter = jitter;
1435 /* convert the NTP time in nanoseconds to 32.32 fixed point */
1436 f_ntp = gst_util_uint64_scale (ntpnstime, (1LL << 32), GST_SECOND);
1437 /* calculate round trip, round the time up */
1438 ntp = ((f_ntp + 0xffff) >> 16) & 0xffffffff;
1441 if (A > 0 && ntp > A)
1445 curr->round_trip = A;
1447 GST_DEBUG ("NTP %04x:%04x, round trip %04x:%04x", ntp >> 16, ntp & 0xffff,
1448 A >> 16, A & 0xffff);
1451 src->stats.curr_rr = curridx;
1455 * rtp_source_get_new_sr:
1456 * @src: an #RTPSource
1457 * @ntpnstime: the current time in nanoseconds since 1970
1458 * @running_time: the current running_time of the pipeline
1459 * @ntptime: the NTP time (in NTP Timestamp Format, 32.32 fixed point)
1460 * @rtptime: the RTP time corresponding to @ntptime (in clock rate units)
1461 * @packet_count: the packet count
1462 * @octet_count: the octet count
1464 * Get new values to put into a new SR report from this source.
1466 * @running_time and @ntpnstime are captured at the same time and represent the
1467 * running time of the pipeline clock and the absolute current system time in
1468 * nanoseconds respectively. Together with the last running_time and RTP timestamp
1469 * we have observed in the source, we can generate @ntptime and @rtptime for an SR
1470 * packet. @ntptime is basically the fixed point representation of @ntpnstime
1471 * and @rtptime the associated RTP timestamp.
1473 * Returns: %TRUE on success.
1476 rtp_source_get_new_sr (RTPSource * src, guint64 ntpnstime,
1477 GstClockTime running_time, guint64 * ntptime, guint32 * rtptime,
1478 guint32 * packet_count, guint32 * octet_count)
1481 guint64 t_current_ntp;
1482 GstClockTimeDiff diff;
1484 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1486 /* We last saw a buffer with last_rtptime at last_rtime. Given a running_time
1487 * and an NTP time, we can scale the RTP timestamps so that they match the
1488 * given NTP time. for scaling, we assume that the slope of the rtptime vs
1489 * running_time vs ntptime curve is close to 1, which is certainly
1490 * sufficient for the frequency at which we report SR and the rate we send
1491 * out RTP packets. */
1492 t_rtp = src->last_rtptime;
1494 GST_DEBUG ("last_rtime %" GST_TIME_FORMAT ", last_rtptime %"
1495 G_GUINT64_FORMAT, GST_TIME_ARGS (src->last_rtime), t_rtp);
1497 if (src->clock_rate == -1 && src->pt_set) {
1498 GST_INFO ("no clock-rate, getting for pt %u and SSRC %u", src->pt,
1500 get_clock_rate (src, src->pt);
1503 if (src->clock_rate != -1) {
1504 /* get the diff between the clock running_time and the buffer running_time.
1505 * This is the elapsed time, as measured against the pipeline clock, between
1506 * when the rtp timestamp was observed and the current running_time.
1508 * We need to apply this diff to the RTP timestamp to get the RTP timestamp
1509 * for the given ntpnstime. */
1510 diff = GST_CLOCK_DIFF (src->last_rtime, running_time);
1511 GST_DEBUG ("running_time %" GST_TIME_FORMAT ", diff %" GST_STIME_FORMAT,
1512 GST_TIME_ARGS (running_time), GST_STIME_ARGS (diff));
1514 /* now translate the diff to RTP time, handle positive and negative cases.
1515 * If there is no diff, we already set rtptime correctly above. */
1517 t_rtp += gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1520 t_rtp -= gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1523 GST_WARNING ("no clock-rate, cannot interpolate rtp time for SSRC %u",
1527 /* convert the NTP time in nanoseconds to 32.32 fixed point */
1528 t_current_ntp = gst_util_uint64_scale (ntpnstime, (1LL << 32), GST_SECOND);
1530 GST_DEBUG ("NTP %08x:%08x, RTP %" G_GUINT32_FORMAT,
1531 (guint32) (t_current_ntp >> 32), (guint32) (t_current_ntp & 0xffffffff),
1535 *ntptime = t_current_ntp;
1539 *packet_count = src->stats.packets_sent;
1541 *octet_count = src->stats.octets_sent;
1547 * rtp_source_get_new_rb:
1548 * @src: an #RTPSource
1549 * @time: the current time of the system clock
1550 * @fractionlost: fraction lost since last SR/RR
1551 * @packetslost: the cumulative number of packets lost
1552 * @exthighestseq: the extended last sequence number received
1553 * @jitter: the interarrival jitter (in clock rate units)
1554 * @lsr: the time of the last SR packet on this source
1555 * (in NTP Short Format, 16.16 fixed point)
1556 * @dlsr: the delay since the last SR packet
1557 * (in NTP Short Format, 16.16 fixed point)
1559 * Get new values to put into a new report block from this source.
1561 * Returns: %TRUE on success.
1564 rtp_source_get_new_rb (RTPSource * src, GstClockTime time,
1565 guint8 * fractionlost, gint32 * packetslost, guint32 * exthighestseq,
1566 guint32 * jitter, guint32 * lsr, guint32 * dlsr)
1568 RTPSourceStats *stats;
1569 guint64 extended_max, expected;
1570 guint64 expected_interval, received_interval, ntptime;
1571 gint64 lost, lost_interval;
1572 guint32 fraction, LSR, DLSR;
1573 GstClockTime sr_time;
1575 stats = &src->stats;
1577 extended_max = stats->cycles + stats->max_seq;
1578 expected = extended_max - stats->base_seq + 1;
1580 GST_DEBUG ("ext_max %" G_GUINT64_FORMAT ", expected %" G_GUINT64_FORMAT
1581 ", received %" G_GUINT64_FORMAT ", base_seq %" G_GUINT32_FORMAT,
1582 extended_max, expected, stats->packets_received, stats->base_seq);
1584 lost = expected - stats->packets_received;
1585 lost = CLAMP (lost, -0x800000, 0x7fffff);
1587 expected_interval = expected - stats->prev_expected;
1588 stats->prev_expected = expected;
1589 received_interval = stats->packets_received - stats->prev_received;
1590 stats->prev_received = stats->packets_received;
1592 lost_interval = expected_interval - received_interval;
1594 if (expected_interval == 0 || lost_interval <= 0)
1597 fraction = (lost_interval << 8) / expected_interval;
1599 GST_DEBUG ("add RR for SSRC %08x", src->ssrc);
1600 /* we scaled the jitter up for additional precision */
1601 GST_DEBUG ("fraction %" G_GUINT32_FORMAT ", lost %" G_GINT64_FORMAT
1602 ", extseq %" G_GUINT64_FORMAT ", jitter %d", fraction, lost,
1603 extended_max, stats->jitter >> 4);
1605 if (rtp_source_get_last_sr (src, &sr_time, &ntptime, NULL, NULL, NULL)) {
1608 /* LSR is middle 32 bits of the last ntptime */
1609 LSR = (ntptime >> 16) & 0xffffffff;
1610 diff = time - sr_time;
1611 GST_DEBUG ("last SR time diff %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
1612 /* DLSR, delay since last SR is expressed in 1/65536 second units */
1613 DLSR = gst_util_uint64_scale_int (diff, 65536, GST_SECOND);
1615 /* No valid SR received, LSR/DLSR are set to 0 then */
1616 GST_DEBUG ("no valid SR received");
1620 GST_DEBUG ("LSR %04x:%04x, DLSR %04x:%04x", LSR >> 16, LSR & 0xffff,
1621 DLSR >> 16, DLSR & 0xffff);
1624 *fractionlost = fraction;
1626 *packetslost = lost;
1628 *exthighestseq = extended_max;
1630 *jitter = stats->jitter >> 4;
1640 * rtp_source_get_last_sr:
1641 * @src: an #RTPSource
1642 * @time: time of packet arrival
1643 * @ntptime: the NTP time (in NTP Timestamp Format, 32.32 fixed point)
1644 * @rtptime: the RTP time (in clock rate units)
1645 * @packet_count: the packet count
1646 * @octet_count: the octet count
1648 * Get the values of the last sender report as set with rtp_source_process_sr().
1650 * Returns: %TRUE if there was a valid SR report.
1653 rtp_source_get_last_sr (RTPSource * src, GstClockTime * time, guint64 * ntptime,
1654 guint32 * rtptime, guint32 * packet_count, guint32 * octet_count)
1656 RTPSenderReport *curr;
1658 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1660 curr = &src->stats.sr[src->stats.curr_sr];
1661 if (!curr->is_valid)
1665 *ntptime = curr->ntptime;
1667 *rtptime = curr->rtptime;
1669 *packet_count = curr->packet_count;
1671 *octet_count = curr->octet_count;
1679 * rtp_source_get_last_rb:
1680 * @src: an #RTPSource
1681 * @fractionlost: fraction lost since last SR/RR
1682 * @packetslost: the cumulative number of packets lost
1683 * @exthighestseq: the extended last sequence number received
1684 * @jitter: the interarrival jitter (in clock rate units)
1685 * @lsr: the time of the last SR packet on this source
1686 * (in NTP Short Format, 16.16 fixed point)
1687 * @dlsr: the delay since the last SR packet
1688 * (in NTP Short Format, 16.16 fixed point)
1689 * @round_trip: the round-trip time
1690 * (in NTP Short Format, 16.16 fixed point)
1692 * Get the values of the last RB report set with rtp_source_process_rb().
1694 * Returns: %TRUE if there was a valid SB report.
1697 rtp_source_get_last_rb (RTPSource * src, guint8 * fractionlost,
1698 gint32 * packetslost, guint32 * exthighestseq, guint32 * jitter,
1699 guint32 * lsr, guint32 * dlsr, guint32 * round_trip)
1701 RTPReceiverReport *curr;
1703 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1705 curr = &src->stats.rr[src->stats.curr_rr];
1706 if (!curr->is_valid)
1710 *fractionlost = curr->fractionlost;
1712 *packetslost = curr->packetslost;
1714 *exthighestseq = curr->exthighestseq;
1716 *jitter = curr->jitter;
1722 *round_trip = curr->round_trip;
1728 find_conflicting_address (GList * conflicting_addresses,
1729 GSocketAddress * address, GstClockTime time)
1733 for (item = conflicting_addresses; item; item = g_list_next (item)) {
1734 RTPConflictingAddress *known_conflict = item->data;
1736 if (__g_socket_address_equal (address, known_conflict->address)) {
1737 known_conflict->time = time;
1746 add_conflicting_address (GList * conflicting_addresses,
1747 GSocketAddress * address, GstClockTime time)
1749 RTPConflictingAddress *new_conflict;
1751 new_conflict = g_slice_new (RTPConflictingAddress);
1753 new_conflict->address = G_SOCKET_ADDRESS (g_object_ref (address));
1754 new_conflict->time = time;
1756 return g_list_prepend (conflicting_addresses, new_conflict);
1760 timeout_conflicting_addresses (GList * conflicting_addresses,
1761 GstClockTime current_time)
1764 /* "a relatively long time" -- RFC 3550 section 8.2 */
1765 const GstClockTime collision_timeout =
1766 RTP_STATS_MIN_INTERVAL * GST_SECOND * 10;
1768 item = g_list_first (conflicting_addresses);
1770 RTPConflictingAddress *known_conflict = item->data;
1771 GList *next_item = g_list_next (item);
1773 if (known_conflict->time < current_time - collision_timeout) {
1776 conflicting_addresses = g_list_delete_link (conflicting_addresses, item);
1777 buf = __g_socket_address_to_string (known_conflict->address);
1778 GST_DEBUG ("collision %p timed out: %s", known_conflict, buf);
1780 rtp_conflicting_address_free (known_conflict);
1785 return conflicting_addresses;
1789 * rtp_source_find_conflicting_address:
1790 * @src: The source the packet came in
1791 * @address: address to check for
1792 * @time: The time when the packet that is possibly in conflict arrived
1794 * Checks if an address which has a conflict is already known. If it is
1795 * a known conflict, remember the time
1797 * Returns: TRUE if it was a known conflict, FALSE otherwise
1800 rtp_source_find_conflicting_address (RTPSource * src, GSocketAddress * address,
1803 return find_conflicting_address (src->conflicting_addresses, address, time);
1807 * rtp_source_add_conflicting_address:
1808 * @src: The source the packet came in
1809 * @address: address to remember
1810 * @time: The time when the packet that is in conflict arrived
1812 * Adds a new conflict address
1815 rtp_source_add_conflicting_address (RTPSource * src,
1816 GSocketAddress * address, GstClockTime time)
1818 src->conflicting_addresses =
1819 add_conflicting_address (src->conflicting_addresses, address, time);
1823 * rtp_source_timeout:
1824 * @src: The #RTPSource
1825 * @current_time: The current time
1826 * @feedback_retention_window: The running time before which retained feedback
1827 * packets have to be discarded
1829 * This is processed on each RTCP interval. It times out old collisions.
1830 * It also times out old retained feedback packets
1833 rtp_source_timeout (RTPSource * src, GstClockTime current_time,
1834 GstClockTime feedback_retention_window)
1838 src->conflicting_addresses =
1839 timeout_conflicting_addresses (src->conflicting_addresses, current_time);
1841 /* Time out AVPF packets that are older than the desired length */
1842 while ((pkt = g_queue_peek_tail (src->retained_feedback)) &&
1843 GST_BUFFER_PTS (pkt) < feedback_retention_window)
1844 gst_buffer_unref (g_queue_pop_tail (src->retained_feedback));
1848 compare_buffers (gconstpointer a, gconstpointer b, gpointer user_data)
1850 const GstBuffer *bufa = a;
1851 const GstBuffer *bufb = b;
1853 g_return_val_if_fail (GST_BUFFER_PTS (bufa) != GST_CLOCK_TIME_NONE, -1);
1854 g_return_val_if_fail (GST_BUFFER_PTS (bufb) != GST_CLOCK_TIME_NONE, 1);
1856 if (GST_BUFFER_PTS (bufa) < GST_BUFFER_PTS (bufb)) {
1858 } else if (GST_BUFFER_PTS (bufa) > GST_BUFFER_PTS (bufb)) {
1866 rtp_source_retain_rtcp_packet (RTPSource * src, GstRTCPPacket * packet,
1867 GstClockTime running_time)
1871 g_return_if_fail (running_time != GST_CLOCK_TIME_NONE);
1873 buffer = gst_buffer_copy_region (packet->rtcp->buffer, GST_BUFFER_COPY_MEMORY,
1874 packet->offset, (gst_rtcp_packet_get_length (packet) + 1) * 4);
1876 GST_BUFFER_PTS (buffer) = running_time;
1878 g_queue_insert_sorted (src->retained_feedback, buffer, compare_buffers, NULL);
1882 rtp_source_has_retained (RTPSource * src, GCompareFunc func, gconstpointer data)
1884 if (g_queue_find_custom (src->retained_feedback, data, func))
1891 * rtp_source_register_nack:
1892 * @src: The #RTPSource
1895 * Register that @seqnum has not been received from @src.
1898 rtp_source_register_nack (RTPSource * src, guint16 seqnum)
1901 guint32 dword = seqnum << 16;
1904 len = src->nacks->len;
1905 for (i = 0; i < len; i++) {
1909 tdword = g_array_index (src->nacks, guint32, i);
1910 tseq = tdword >> 16;
1912 diff = gst_rtp_buffer_compare_seqnum (tseq, seqnum);
1916 /* we already have this seqnum */
1919 /* it comes before the recorded seqnum, FIXME, we could merge it
1920 * if not to far away */
1922 GST_DEBUG ("insert NACK #%u at %u", seqnum, i);
1923 g_array_insert_val (src->nacks, i, dword);
1924 } else if (diff < 16) {
1925 /* we can merge it */
1926 dword = g_array_index (src->nacks, guint32, i);
1927 dword |= 1 << (diff - 1);
1928 GST_DEBUG ("merge NACK #%u at %u with NACK #%u -> 0x%08x", seqnum, i,
1929 dword >> 16, dword);
1930 g_array_index (src->nacks, guint32, i) = dword;
1932 GST_DEBUG ("append NACK #%u", seqnum);
1933 g_array_append_val (src->nacks, dword);
1935 src->send_nack = TRUE;
1939 * rtp_source_get_nacks:
1940 * @src: The #RTPSource
1941 * @n_nacks: result number of nacks
1943 * Get the registered NACKS since the last rtp_source_clear_nacks().
1945 * Returns: an array of @n_nacks seqnum values.
1948 rtp_source_get_nacks (RTPSource * src, guint * n_nacks)
1951 *n_nacks = src->nacks->len;
1953 return (guint32 *) src->nacks->data;
1957 rtp_source_clear_nacks (RTPSource * src)
1959 g_array_set_size (src->nacks, 0);
1960 src->send_nack = FALSE;