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);
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;
274 src->stats.sent_nack_count = 0;
275 src->stats.recv_nack_count = 0;
279 rtp_source_init (RTPSource * src)
281 /* sources are initialy on probation until we receive enough valid RTP
282 * packets or a valid RTCP packet */
283 src->validated = FALSE;
284 src->internal = FALSE;
285 src->probation = DEFAULT_PROBATION;
286 src->curr_probation = src->probation;
287 src->closing = FALSE;
288 src->max_dropout_time = DEFAULT_MAX_DROPOUT_TIME;
289 src->max_misorder_time = DEFAULT_MAX_MISORDER_TIME;
291 src->sdes = gst_structure_new_empty ("application/x-rtp-source-sdes");
294 src->clock_rate = -1;
295 src->packets = g_queue_new ();
296 src->seqnum_offset = -1;
297 src->last_rtptime = -1;
299 src->retained_feedback = g_queue_new ();
300 src->nacks = g_array_new (FALSE, FALSE, sizeof (guint32));
302 src->reported_in_sr_of = g_hash_table_new (g_direct_hash, g_direct_equal);
304 rtp_source_reset (src);
308 rtp_conflicting_address_free (RTPConflictingAddress * addr)
310 g_object_unref (addr->address);
311 g_slice_free (RTPConflictingAddress, addr);
315 rtp_source_finalize (GObject * object)
319 src = RTP_SOURCE_CAST (object);
321 g_queue_foreach (src->packets, (GFunc) gst_buffer_unref, NULL);
322 g_queue_free (src->packets);
324 gst_structure_free (src->sdes);
326 g_free (src->bye_reason);
328 gst_caps_replace (&src->caps, NULL);
330 g_list_free_full (src->conflicting_addresses,
331 (GDestroyNotify) rtp_conflicting_address_free);
332 g_queue_foreach (src->retained_feedback, (GFunc) gst_buffer_unref, NULL);
333 g_queue_free (src->retained_feedback);
335 g_array_free (src->nacks, TRUE);
338 g_object_unref (src->rtp_from);
340 g_object_unref (src->rtcp_from);
342 g_hash_table_unref (src->reported_in_sr_of);
344 G_OBJECT_CLASS (rtp_source_parent_class)->finalize (object);
347 static GstStructure *
348 rtp_source_create_stats (RTPSource * src)
351 gboolean is_sender = src->is_sender;
352 gboolean internal = src->internal;
355 guint8 fractionlost = 0;
356 gint32 packetslost = 0;
357 guint32 exthighestseq = 0;
361 guint32 round_trip = 0;
363 GstClockTime time = 0;
366 guint32 packet_count = 0;
367 guint32 octet_count = 0;
370 /* common data for all types of sources */
371 s = gst_structure_new ("application/x-rtp-source-stats",
372 "ssrc", G_TYPE_UINT, (guint) src->ssrc,
373 "internal", G_TYPE_BOOLEAN, internal,
374 "validated", G_TYPE_BOOLEAN, src->validated,
375 "received-bye", G_TYPE_BOOLEAN, src->marked_bye,
376 "is-csrc", G_TYPE_BOOLEAN, src->is_csrc,
377 "is-sender", G_TYPE_BOOLEAN, is_sender,
378 "seqnum-base", G_TYPE_INT, src->seqnum_offset,
379 "clock-rate", G_TYPE_INT, src->clock_rate, NULL);
381 /* add address and port */
383 address_str = __g_socket_address_to_string (src->rtp_from);
384 gst_structure_set (s, "rtp-from", G_TYPE_STRING, address_str, NULL);
385 g_free (address_str);
387 if (src->rtcp_from) {
388 address_str = __g_socket_address_to_string (src->rtcp_from);
389 gst_structure_set (s, "rtcp-from", G_TYPE_STRING, address_str, NULL);
390 g_free (address_str);
393 gst_structure_set (s,
394 "octets-sent", G_TYPE_UINT64, src->stats.octets_sent,
395 "packets-sent", G_TYPE_UINT64, src->stats.packets_sent,
396 "octets-received", G_TYPE_UINT64, src->stats.octets_received,
397 "packets-received", G_TYPE_UINT64, src->stats.packets_received,
398 "bitrate", G_TYPE_UINT64, src->bitrate,
399 "packets-lost", G_TYPE_INT,
400 (gint) rtp_stats_get_packets_lost (&src->stats), "jitter", G_TYPE_UINT,
401 (guint) (src->stats.jitter >> 4),
402 "sent-pli-count", G_TYPE_UINT, src->stats.sent_pli_count,
403 "recv-pli-count", G_TYPE_UINT, src->stats.recv_pli_count,
404 "sent-fir-count", G_TYPE_UINT, src->stats.sent_fir_count,
405 "recv-fir-count", G_TYPE_UINT, src->stats.recv_fir_count,
406 "sent-nack-count", G_TYPE_UINT, src->stats.sent_nack_count,
407 "recv-nack-count", G_TYPE_UINT, src->stats.recv_nack_count, NULL);
409 /* get the last SR. */
410 have_sr = rtp_source_get_last_sr (src, &time, &ntptime, &rtptime,
411 &packet_count, &octet_count);
412 gst_structure_set (s,
413 "have-sr", G_TYPE_BOOLEAN, have_sr,
414 "sr-ntptime", G_TYPE_UINT64, ntptime,
415 "sr-rtptime", G_TYPE_UINT, (guint) rtptime,
416 "sr-octet-count", G_TYPE_UINT, (guint) octet_count,
417 "sr-packet-count", G_TYPE_UINT, (guint) packet_count, NULL);
420 /* get the last RB we sent */
421 gst_structure_set (s,
422 "sent-rb", G_TYPE_BOOLEAN, src->last_rr.is_valid,
423 "sent-rb-fractionlost", G_TYPE_UINT, (guint) src->last_rr.fractionlost,
424 "sent-rb-packetslost", G_TYPE_INT, (gint) src->last_rr.packetslost,
425 "sent-rb-exthighestseq", G_TYPE_UINT,
426 (guint) src->last_rr.exthighestseq, "sent-rb-jitter", G_TYPE_UINT,
427 (guint) src->last_rr.jitter, "sent-rb-lsr", G_TYPE_UINT,
428 (guint) src->last_rr.lsr, "sent-rb-dlsr", G_TYPE_UINT,
429 (guint) src->last_rr.dlsr, NULL);
431 /* get the last RB */
432 have_rb = rtp_source_get_last_rb (src, &fractionlost, &packetslost,
433 &exthighestseq, &jitter, &lsr, &dlsr, &round_trip);
435 gst_structure_set (s,
436 "have-rb", G_TYPE_BOOLEAN, have_rb,
437 "rb-fractionlost", G_TYPE_UINT, (guint) fractionlost,
438 "rb-packetslost", G_TYPE_INT, (gint) packetslost,
439 "rb-exthighestseq", G_TYPE_UINT, (guint) exthighestseq,
440 "rb-jitter", G_TYPE_UINT, (guint) jitter,
441 "rb-lsr", G_TYPE_UINT, (guint) lsr,
442 "rb-dlsr", G_TYPE_UINT, (guint) dlsr,
443 "rb-round-trip", G_TYPE_UINT, (guint) round_trip, NULL);
450 * rtp_source_get_sdes_struct:
451 * @src: an #RTPSource
453 * Get the SDES from @src. See the SDES property for more details.
455 * Returns: %GstStructure of type "application/x-rtp-source-sdes". The result is
456 * valid until the SDES items of @src are modified.
459 rtp_source_get_sdes_struct (RTPSource * src)
461 g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
467 sdes_struct_compare_func (GQuark field_id, const GValue * value,
473 old = GST_STRUCTURE (user_data);
474 field = g_quark_to_string (field_id);
476 if (!gst_structure_has_field (old, field))
479 g_assert (G_VALUE_HOLDS_STRING (value));
481 return strcmp (g_value_get_string (value), gst_structure_get_string (old,
486 * rtp_source_set_sdes_struct:
487 * @src: an #RTPSource
488 * @sdes: the SDES structure
490 * Store the @sdes in @src. @sdes must be a structure of type
491 * "application/x-rtp-source-sdes", see the SDES property for more details.
493 * This function takes ownership of @sdes.
495 * Returns: %FALSE if the SDES was unchanged.
498 rtp_source_set_sdes_struct (RTPSource * src, GstStructure * sdes)
502 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
503 g_return_val_if_fail (strcmp (gst_structure_get_name (sdes),
504 "application/x-rtp-source-sdes") == 0, FALSE);
506 changed = !gst_structure_foreach (sdes, sdes_struct_compare_func, src->sdes);
509 gst_structure_free (src->sdes);
512 gst_structure_free (sdes);
518 rtp_source_set_property (GObject * object, guint prop_id,
519 const GValue * value, GParamSpec * pspec)
523 src = RTP_SOURCE (object);
527 src->ssrc = g_value_get_uint (value);
530 src->probation = g_value_get_uint (value);
532 case PROP_MAX_DROPOUT_TIME:
533 src->max_dropout_time = g_value_get_uint (value);
535 case PROP_MAX_MISORDER_TIME:
536 src->max_misorder_time = g_value_get_uint (value);
539 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
545 rtp_source_get_property (GObject * object, guint prop_id,
546 GValue * value, GParamSpec * pspec)
550 src = RTP_SOURCE (object);
554 g_value_set_uint (value, rtp_source_get_ssrc (src));
557 g_value_set_boolean (value, rtp_source_is_as_csrc (src));
559 case PROP_IS_VALIDATED:
560 g_value_set_boolean (value, rtp_source_is_validated (src));
563 g_value_set_boolean (value, rtp_source_is_sender (src));
566 g_value_set_boxed (value, rtp_source_get_sdes_struct (src));
569 g_value_take_boxed (value, rtp_source_create_stats (src));
572 g_value_set_uint (value, src->probation);
574 case PROP_MAX_DROPOUT_TIME:
575 g_value_set_uint (value, src->max_dropout_time);
577 case PROP_MAX_MISORDER_TIME:
578 g_value_set_uint (value, src->max_misorder_time);
581 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
590 * Create a #RTPSource with @ssrc.
592 * Returns: a new #RTPSource. Use g_object_unref() after usage.
595 rtp_source_new (guint32 ssrc)
599 src = g_object_new (RTP_TYPE_SOURCE, NULL);
606 * rtp_source_set_callbacks:
607 * @src: an #RTPSource
608 * @cb: callback functions
609 * @user_data: user data
611 * Set the callbacks for the source.
614 rtp_source_set_callbacks (RTPSource * src, RTPSourceCallbacks * cb,
617 g_return_if_fail (RTP_IS_SOURCE (src));
619 src->callbacks.push_rtp = cb->push_rtp;
620 src->callbacks.clock_rate = cb->clock_rate;
621 src->user_data = user_data;
625 * rtp_source_get_ssrc:
626 * @src: an #RTPSource
628 * Get the SSRC of @source.
630 * Returns: the SSRC of src.
633 rtp_source_get_ssrc (RTPSource * src)
637 g_return_val_if_fail (RTP_IS_SOURCE (src), 0);
645 * rtp_source_set_as_csrc:
646 * @src: an #RTPSource
648 * Configure @src as a CSRC, this will also validate @src.
651 rtp_source_set_as_csrc (RTPSource * src)
653 g_return_if_fail (RTP_IS_SOURCE (src));
655 src->validated = TRUE;
660 * rtp_source_is_as_csrc:
661 * @src: an #RTPSource
663 * Check if @src is a contributing source.
665 * Returns: %TRUE if @src is acting as a contributing source.
668 rtp_source_is_as_csrc (RTPSource * src)
672 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
674 result = src->is_csrc;
680 * rtp_source_is_active:
681 * @src: an #RTPSource
683 * Check if @src is an active source. A source is active if it has been
684 * validated and has not yet received a BYE packet
686 * Returns: %TRUE if @src is an qactive source.
689 rtp_source_is_active (RTPSource * src)
693 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
695 result = RTP_SOURCE_IS_ACTIVE (src);
701 * rtp_source_is_validated:
702 * @src: an #RTPSource
704 * Check if @src is a validated source.
706 * Returns: %TRUE if @src is a validated source.
709 rtp_source_is_validated (RTPSource * src)
713 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
715 result = src->validated;
721 * rtp_source_is_sender:
722 * @src: an #RTPSource
724 * Check if @src is a sending source.
726 * Returns: %TRUE if @src is a sending source.
729 rtp_source_is_sender (RTPSource * src)
733 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
735 result = RTP_SOURCE_IS_SENDER (src);
741 * rtp_source_is_marked_bye:
742 * @src: an #RTPSource
744 * Check if @src is marked as leaving the session with a BYE packet.
746 * Returns: %TRUE if @src has been marked BYE.
749 rtp_source_is_marked_bye (RTPSource * src)
753 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
755 result = RTP_SOURCE_IS_MARKED_BYE (src);
762 * rtp_source_get_bye_reason:
763 * @src: an #RTPSource
765 * Get the BYE reason for @src. Check if the source is marked as leaving the
766 * session with a BYE message first with rtp_source_is_marked_bye().
768 * Returns: The BYE reason or NULL when no reason was given or the source was
769 * not marked BYE yet. g_free() after usage.
772 rtp_source_get_bye_reason (RTPSource * src)
776 g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
778 result = g_strdup (src->bye_reason);
784 * rtp_source_update_caps:
785 * @src: an #RTPSource
788 * Parse @caps and store all relevant information in @source.
791 rtp_source_update_caps (RTPSource * src, GstCaps * caps)
798 /* nothing changed, return */
799 if (caps == NULL || src->caps == caps)
802 s = gst_caps_get_structure (caps, 0);
804 rtx = (gst_structure_get_uint (s, "rtx-ssrc", &val) && val == src->ssrc);
806 if (gst_structure_get_int (s, rtx ? "rtx-payload" : "payload", &ival))
811 GST_DEBUG ("got %spayload %d", rtx ? "rtx " : "", src->payload);
813 if (gst_structure_get_int (s, "clock-rate", &ival))
814 src->clock_rate = ival;
816 src->clock_rate = -1;
818 GST_DEBUG ("got clock-rate %d", src->clock_rate);
820 if (gst_structure_get_uint (s, rtx ? "rtx-seqnum-offset" : "seqnum-offset",
822 src->seqnum_offset = val;
824 src->seqnum_offset = -1;
826 GST_DEBUG ("got %sseqnum-offset %" G_GINT32_FORMAT, rtx ? "rtx " : "",
829 gst_caps_replace (&src->caps, caps);
833 * rtp_source_set_rtp_from:
834 * @src: an #RTPSource
835 * @address: the RTP address to set
837 * Set that @src is receiving RTP packets from @address. This is used for
838 * collistion checking.
841 rtp_source_set_rtp_from (RTPSource * src, GSocketAddress * address)
843 g_return_if_fail (RTP_IS_SOURCE (src));
846 g_object_unref (src->rtp_from);
847 src->rtp_from = G_SOCKET_ADDRESS (g_object_ref (address));
851 * rtp_source_set_rtcp_from:
852 * @src: an #RTPSource
853 * @address: the RTCP address to set
855 * Set that @src is receiving RTCP packets from @address. This is used for
856 * collistion checking.
859 rtp_source_set_rtcp_from (RTPSource * src, GSocketAddress * address)
861 g_return_if_fail (RTP_IS_SOURCE (src));
864 g_object_unref (src->rtcp_from);
865 src->rtcp_from = G_SOCKET_ADDRESS (g_object_ref (address));
869 push_packet (RTPSource * src, GstBuffer * buffer)
871 GstFlowReturn ret = GST_FLOW_OK;
873 /* push queued packets first if any */
874 while (!g_queue_is_empty (src->packets)) {
875 GstBuffer *buffer = GST_BUFFER_CAST (g_queue_pop_head (src->packets));
877 GST_LOG ("pushing queued packet");
878 if (src->callbacks.push_rtp)
879 src->callbacks.push_rtp (src, buffer, src->user_data);
881 gst_buffer_unref (buffer);
883 GST_LOG ("pushing new packet");
885 if (src->callbacks.push_rtp)
886 ret = src->callbacks.push_rtp (src, buffer, src->user_data);
888 gst_buffer_unref (buffer);
894 get_clock_rate (RTPSource * src, guint8 payload)
896 if (src->payload == -1) {
897 /* first payload received, nothing was in the caps, lock on to this payload */
898 src->payload = payload;
899 GST_DEBUG ("first payload %d", payload);
900 } else if (payload != src->payload) {
901 /* we have a different payload than before, reset the clock-rate */
902 GST_DEBUG ("new payload %d", payload);
903 src->payload = payload;
904 src->clock_rate = -1;
905 src->stats.transit = -1;
908 if (src->clock_rate == -1) {
909 gint clock_rate = -1;
911 if (src->callbacks.clock_rate)
912 clock_rate = src->callbacks.clock_rate (src, payload, src->user_data);
914 GST_DEBUG ("got clock-rate %d", clock_rate);
916 src->clock_rate = clock_rate;
917 gst_rtp_packet_rate_ctx_reset (&src->packet_rate_ctx, clock_rate);
919 return src->clock_rate;
922 /* Jitter is the variation in the delay of received packets in a flow. It is
923 * measured by comparing the interval when RTP packets were sent to the interval
924 * at which they were received. For instance, if packet #1 and packet #2 leave
925 * 50 milliseconds apart and arrive 60 milliseconds apart, then the jitter is 10
928 calculate_jitter (RTPSource * src, RTPPacketInfo * pinfo)
930 GstClockTime running_time;
931 guint32 rtparrival, transit, rtptime;
936 /* get arrival time */
937 if ((running_time = pinfo->running_time) == GST_CLOCK_TIME_NONE)
942 GST_LOG ("SSRC %08x got payload %d", src->ssrc, pt);
945 if ((clock_rate = get_clock_rate (src, pt)) == -1)
948 rtptime = pinfo->rtptime;
950 /* convert arrival time to RTP timestamp units, truncate to 32 bits, we don't
951 * care about the absolute value, just the difference. */
952 rtparrival = gst_util_uint64_scale_int (running_time, clock_rate, GST_SECOND);
954 /* transit time is difference with RTP timestamp */
955 transit = rtparrival - rtptime;
957 /* get ABS diff with previous transit time */
958 if (src->stats.transit != -1) {
959 if (transit > src->stats.transit)
960 diff = transit - src->stats.transit;
962 diff = src->stats.transit - transit;
966 src->stats.transit = transit;
968 /* update jitter, the value we store is scaled up so we can keep precision. */
969 src->stats.jitter += diff - ((src->stats.jitter + 8) >> 4);
971 src->stats.prev_rtptime = src->stats.last_rtptime;
972 src->stats.last_rtptime = rtparrival;
974 GST_LOG ("rtparrival %u, rtptime %u, clock-rate %d, diff %d, jitter: %f",
975 rtparrival, rtptime, clock_rate, diff, (src->stats.jitter) / 16.0);
982 GST_WARNING ("cannot get current running_time");
987 GST_WARNING ("cannot get clock-rate for pt %d", pt);
993 init_seq (RTPSource * src, guint16 seq)
995 src->stats.base_seq = seq;
996 src->stats.max_seq = seq;
997 src->stats.bad_seq = RTP_SEQ_MOD + 1; /* so seq == bad_seq is false */
998 src->stats.cycles = 0;
999 src->stats.packets_received = 0;
1000 src->stats.octets_received = 0;
1001 src->stats.bytes_received = 0;
1002 src->stats.prev_received = 0;
1003 src->stats.prev_expected = 0;
1004 src->stats.recv_pli_count = 0;
1005 src->stats.recv_fir_count = 0;
1007 GST_DEBUG ("base_seq %d", seq);
1010 #define BITRATE_INTERVAL (2 * GST_SECOND)
1013 do_bitrate_estimation (RTPSource * src, GstClockTime running_time,
1014 guint64 * bytes_handled)
1018 if (src->prev_rtime) {
1019 elapsed = running_time - src->prev_rtime;
1021 if (elapsed > BITRATE_INTERVAL) {
1024 rate = gst_util_uint64_scale (*bytes_handled, 8 * GST_SECOND, elapsed);
1026 GST_LOG ("Elapsed %" G_GUINT64_FORMAT ", bytes %" G_GUINT64_FORMAT
1027 ", rate %" G_GUINT64_FORMAT, elapsed, *bytes_handled, rate);
1029 if (src->bitrate == 0)
1030 src->bitrate = rate;
1032 src->bitrate = ((src->bitrate * 3) + rate) / 4;
1034 src->prev_rtime = running_time;
1038 GST_LOG ("Reset bitrate measurement");
1039 src->prev_rtime = running_time;
1045 update_receiver_stats (RTPSource * src, RTPPacketInfo * pinfo,
1046 gboolean is_receive)
1048 guint16 seqnr, expected;
1049 RTPSourceStats *stats;
1051 gint32 packet_rate, max_dropout, max_misorder;
1053 stats = &src->stats;
1055 seqnr = pinfo->seqnum;
1058 gst_rtp_packet_rate_ctx_update (&src->packet_rate_ctx, pinfo->seqnum,
1061 gst_rtp_packet_rate_ctx_get_max_dropout (&src->packet_rate_ctx,
1062 src->max_dropout_time);
1064 gst_rtp_packet_rate_ctx_get_max_misorder (&src->packet_rate_ctx,
1065 src->max_misorder_time);
1066 GST_TRACE ("SSRC %08x, packet_rate: %d, max_dropout: %d, max_misorder: %d",
1067 src->ssrc, packet_rate, max_dropout, max_misorder);
1069 if (stats->cycles == -1) {
1070 GST_DEBUG ("received first packet");
1071 /* first time we heard of this source */
1072 init_seq (src, seqnr);
1073 src->stats.max_seq = seqnr - 1;
1074 src->curr_probation = src->probation;
1078 expected = src->stats.max_seq + 1;
1079 delta = gst_rtp_buffer_compare_seqnum (expected, seqnr);
1081 /* if we are still on probation, check seqnum */
1082 if (src->curr_probation) {
1083 /* when in probation, we require consecutive seqnums */
1085 /* expected packet */
1086 GST_DEBUG ("probation: seqnr %d == expected %d", seqnr, expected);
1087 src->curr_probation--;
1088 if (seqnr < stats->max_seq) {
1089 /* sequence number wrapped - count another 64K cycle. */
1090 stats->cycles += RTP_SEQ_MOD;
1092 src->stats.max_seq = seqnr;
1094 if (src->curr_probation == 0) {
1095 GST_DEBUG ("probation done!");
1096 init_seq (src, seqnr);
1100 GST_DEBUG ("probation %d: queue packet", src->curr_probation);
1101 /* when still in probation, keep packets in a list. */
1102 g_queue_push_tail (src->packets, pinfo->data);
1104 /* remove packets from queue if there are too many */
1105 while (g_queue_get_length (src->packets) > RTP_MAX_PROBATION_LEN) {
1106 q = g_queue_pop_head (src->packets);
1107 gst_buffer_unref (q);
1112 /* unexpected seqnum in probation */
1113 goto probation_seqnum;
1115 } else if (delta >= 0 && delta < max_dropout) {
1116 /* Clear bad packets */
1117 stats->bad_seq = RTP_SEQ_MOD + 1; /* so seq == bad_seq is false */
1118 g_queue_foreach (src->packets, (GFunc) gst_buffer_unref, NULL);
1119 g_queue_clear (src->packets);
1121 /* in order, with permissible gap */
1122 if (seqnr < stats->max_seq) {
1123 /* sequence number wrapped - count another 64K cycle. */
1124 stats->cycles += RTP_SEQ_MOD;
1126 stats->max_seq = seqnr;
1127 } else if (delta < -max_misorder || delta >= max_dropout) {
1128 /* the sequence number made a very large jump */
1129 if (seqnr == stats->bad_seq && src->packets->head) {
1130 /* two sequential packets -- assume that the other side
1131 * restarted without telling us so just re-sync
1132 * (i.e., pretend this was the first packet). */
1133 init_seq (src, seqnr);
1135 /* unacceptable jump */
1136 stats->bad_seq = (seqnr + 1) & (RTP_SEQ_MOD - 1);
1137 g_queue_foreach (src->packets, (GFunc) gst_buffer_unref, NULL);
1138 g_queue_clear (src->packets);
1139 g_queue_push_tail (src->packets, pinfo->data);
1143 } else { /* delta < 0 && delta >= -max_misorder */
1144 /* Clear bad packets */
1145 stats->bad_seq = RTP_SEQ_MOD + 1; /* so seq == bad_seq is false */
1146 g_queue_foreach (src->packets, (GFunc) gst_buffer_unref, NULL);
1147 g_queue_clear (src->packets);
1149 /* duplicate or reordered packet, will be filtered by jitterbuffer. */
1150 GST_INFO ("duplicate or reordered packet (seqnr %u, expected %u)",
1155 src->stats.octets_received += pinfo->payload_len;
1156 src->stats.bytes_received += pinfo->bytes;
1157 src->stats.packets_received++;
1158 /* for the bitrate estimation */
1159 src->bytes_received += pinfo->payload_len;
1161 GST_LOG ("seq %u, PC: %" G_GUINT64_FORMAT ", OC: %" G_GUINT64_FORMAT,
1162 seqnr, src->stats.packets_received, src->stats.octets_received);
1174 ("unacceptable seqnum received (seqnr %u, delta %d, packet_rate: %d, max_dropout: %d, max_misorder: %d)",
1175 seqnr, delta, packet_rate, max_dropout, max_misorder);
1180 GST_WARNING ("probation: seqnr %d != expected %d", seqnr, expected);
1181 src->curr_probation = src->probation;
1182 src->stats.max_seq = seqnr;
1188 * rtp_source_process_rtp:
1189 * @src: an #RTPSource
1190 * @pinfo: an #RTPPacketInfo
1192 * Let @src handle the incomming RTP packet described in @pinfo.
1194 * Returns: a #GstFlowReturn.
1197 rtp_source_process_rtp (RTPSource * src, RTPPacketInfo * pinfo)
1199 GstFlowReturn result;
1201 g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
1202 g_return_val_if_fail (pinfo != NULL, GST_FLOW_ERROR);
1204 if (!update_receiver_stats (src, pinfo, TRUE))
1207 /* the source that sent the packet must be a sender */
1208 src->is_sender = TRUE;
1209 src->validated = TRUE;
1211 do_bitrate_estimation (src, pinfo->running_time, &src->bytes_received);
1213 /* calculate jitter for the stats */
1214 calculate_jitter (src, pinfo);
1216 /* we're ready to push the RTP packet now */
1217 result = push_packet (src, pinfo->data);
1224 * rtp_source_mark_bye:
1225 * @src: an #RTPSource
1226 * @reason: the reason for leaving
1228 * Mark @src in the BYE state. This can happen when the source wants to
1229 * leave the sesssion or when a BYE packets has been received.
1231 * This will make the source inactive.
1234 rtp_source_mark_bye (RTPSource * src, const gchar * reason)
1236 g_return_if_fail (RTP_IS_SOURCE (src));
1238 GST_DEBUG ("marking SSRC %08x as BYE, reason: %s", src->ssrc,
1239 GST_STR_NULL (reason));
1241 /* copy the reason and mark as bye */
1242 g_free (src->bye_reason);
1243 src->bye_reason = g_strdup (reason);
1244 src->marked_bye = TRUE;
1248 * rtp_source_send_rtp:
1249 * @src: an #RTPSource
1250 * @data: an RTP buffer or a list of RTP buffers
1251 * @is_list: if @data is a buffer or list
1252 * @running_time: the running time of @data
1254 * Send @data (an RTP buffer or list of buffers) originating from @src.
1255 * This will make @src a sender. This function takes ownership of @data and
1256 * modifies the SSRC in the RTP packet to that of @src when needed.
1258 * Returns: a #GstFlowReturn.
1261 rtp_source_send_rtp (RTPSource * src, RTPPacketInfo * pinfo)
1263 GstFlowReturn result;
1264 GstClockTime running_time;
1266 guint64 ext_rtptime;
1267 guint64 rt_diff, rtp_diff;
1269 g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
1271 /* we are a sender now */
1272 src->is_sender = TRUE;
1274 /* we are also a receiver of our packets */
1275 if (!update_receiver_stats (src, pinfo, FALSE))
1278 /* update stats for the SR */
1279 src->stats.packets_sent += pinfo->packets;
1280 src->stats.octets_sent += pinfo->payload_len;
1281 src->bytes_sent += pinfo->payload_len;
1283 running_time = pinfo->running_time;
1285 do_bitrate_estimation (src, running_time, &src->bytes_sent);
1287 rtptime = pinfo->rtptime;
1289 ext_rtptime = src->last_rtptime;
1290 ext_rtptime = gst_rtp_buffer_ext_timestamp (&ext_rtptime, rtptime);
1292 GST_LOG ("SSRC %08x, RTP %" G_GUINT64_FORMAT ", running_time %"
1293 GST_TIME_FORMAT, src->ssrc, ext_rtptime, GST_TIME_ARGS (running_time));
1295 if (ext_rtptime > src->last_rtptime) {
1296 rtp_diff = ext_rtptime - src->last_rtptime;
1297 rt_diff = running_time - src->last_rtime;
1299 /* calc the diff so we can detect drift at the sender. This can also be used
1300 * to guestimate the clock rate if the NTP time is locked to the RTP
1301 * timestamps (as is the case when the capture device is providing the clock). */
1302 GST_LOG ("SSRC %08x, diff RTP %" G_GUINT64_FORMAT ", diff running_time %"
1303 GST_TIME_FORMAT, src->ssrc, rtp_diff, GST_TIME_ARGS (rt_diff));
1306 /* we keep track of the last received RTP timestamp and the corresponding
1307 * buffer running_time so that we can use this info when constructing SR reports */
1308 src->last_rtime = running_time;
1309 src->last_rtptime = ext_rtptime;
1312 if (!src->callbacks.push_rtp)
1315 GST_LOG ("pushing RTP %s %" G_GUINT64_FORMAT,
1316 pinfo->is_list ? "list" : "packet", src->stats.packets_sent);
1318 result = src->callbacks.push_rtp (src, pinfo->data, src->user_data);
1326 GST_WARNING ("no callback installed, dropping packet");
1332 * rtp_source_process_sr:
1333 * @src: an #RTPSource
1334 * @time: time of packet arrival
1335 * @ntptime: the NTP time (in NTP Timestamp Format, 32.32 fixed point)
1336 * @rtptime: the RTP time (in clock rate units)
1337 * @packet_count: the packet count
1338 * @octet_count: the octet count
1340 * Update the sender report in @src.
1343 rtp_source_process_sr (RTPSource * src, GstClockTime time, guint64 ntptime,
1344 guint32 rtptime, guint32 packet_count, guint32 octet_count)
1346 RTPSenderReport *curr;
1349 g_return_if_fail (RTP_IS_SOURCE (src));
1351 GST_DEBUG ("got SR packet: SSRC %08x, NTP %08x:%08x, RTP %" G_GUINT32_FORMAT
1352 ", PC %" G_GUINT32_FORMAT ", OC %" G_GUINT32_FORMAT, src->ssrc,
1353 (guint32) (ntptime >> 32), (guint32) (ntptime & 0xffffffff), rtptime,
1354 packet_count, octet_count);
1356 curridx = src->stats.curr_sr ^ 1;
1357 curr = &src->stats.sr[curridx];
1359 /* this is a sender now */
1360 src->is_sender = TRUE;
1362 /* update current */
1363 curr->is_valid = TRUE;
1364 curr->ntptime = ntptime;
1365 curr->rtptime = rtptime;
1366 curr->packet_count = packet_count;
1367 curr->octet_count = octet_count;
1371 src->stats.curr_sr = curridx;
1373 src->stats.prev_rtcptime = src->stats.last_rtcptime;
1374 src->stats.last_rtcptime = time;
1378 * rtp_source_process_rb:
1379 * @src: an #RTPSource
1380 * @ntpnstime: the current time in nanoseconds since 1970
1381 * @fractionlost: fraction lost since last SR/RR
1382 * @packetslost: the cumulative number of packets lost
1383 * @exthighestseq: the extended last sequence number received
1384 * @jitter: the interarrival jitter (in clock rate units)
1385 * @lsr: the time of the last SR packet on this source
1386 * (in NTP Short Format, 16.16 fixed point)
1387 * @dlsr: the delay since the last SR packet
1388 * (in NTP Short Format, 16.16 fixed point)
1390 * Update the report block in @src.
1393 rtp_source_process_rb (RTPSource * src, guint64 ntpnstime,
1394 guint8 fractionlost, gint32 packetslost, guint32 exthighestseq,
1395 guint32 jitter, guint32 lsr, guint32 dlsr)
1397 RTPReceiverReport *curr;
1402 g_return_if_fail (RTP_IS_SOURCE (src));
1404 GST_DEBUG ("got RB packet: SSRC %08x, FL %2x, PL %d, HS %" G_GUINT32_FORMAT
1405 ", jitter %" G_GUINT32_FORMAT ", LSR %04x:%04x, DLSR %04x:%04x",
1406 src->ssrc, fractionlost, packetslost, exthighestseq, jitter, lsr >> 16,
1407 lsr & 0xffff, dlsr >> 16, dlsr & 0xffff);
1409 curridx = src->stats.curr_rr ^ 1;
1410 curr = &src->stats.rr[curridx];
1412 /* update current */
1413 curr->is_valid = TRUE;
1414 curr->fractionlost = fractionlost;
1415 curr->packetslost = packetslost;
1416 curr->exthighestseq = exthighestseq;
1417 curr->jitter = jitter;
1421 /* convert the NTP time in nanoseconds to 32.32 fixed point */
1422 f_ntp = gst_util_uint64_scale (ntpnstime, (1LL << 32), GST_SECOND);
1423 /* calculate round trip, round the time up */
1424 ntp = ((f_ntp + 0xffff) >> 16) & 0xffffffff;
1427 if (A > 0 && ntp > A)
1431 curr->round_trip = A;
1433 GST_DEBUG ("NTP %04x:%04x, round trip %04x:%04x", ntp >> 16, ntp & 0xffff,
1434 A >> 16, A & 0xffff);
1437 src->stats.curr_rr = curridx;
1441 * rtp_source_get_new_sr:
1442 * @src: an #RTPSource
1443 * @ntpnstime: the current time in nanoseconds since 1970
1444 * @running_time: the current running_time of the pipeline
1445 * @ntptime: the NTP time (in NTP Timestamp Format, 32.32 fixed point)
1446 * @rtptime: the RTP time corresponding to @ntptime (in clock rate units)
1447 * @packet_count: the packet count
1448 * @octet_count: the octet count
1450 * Get new values to put into a new SR report from this source.
1452 * @running_time and @ntpnstime are captured at the same time and represent the
1453 * running time of the pipeline clock and the absolute current system time in
1454 * nanoseconds respectively. Together with the last running_time and RTP timestamp
1455 * we have observed in the source, we can generate @ntptime and @rtptime for an SR
1456 * packet. @ntptime is basically the fixed point representation of @ntpnstime
1457 * and @rtptime the associated RTP timestamp.
1459 * Returns: %TRUE on success.
1462 rtp_source_get_new_sr (RTPSource * src, guint64 ntpnstime,
1463 GstClockTime running_time, guint64 * ntptime, guint32 * rtptime,
1464 guint32 * packet_count, guint32 * octet_count)
1467 guint64 t_current_ntp;
1468 GstClockTimeDiff diff;
1470 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1472 /* We last saw a buffer with last_rtptime at last_rtime. Given a running_time
1473 * and an NTP time, we can scale the RTP timestamps so that they match the
1474 * given NTP time. for scaling, we assume that the slope of the rtptime vs
1475 * running_time vs ntptime curve is close to 1, which is certainly
1476 * sufficient for the frequency at which we report SR and the rate we send
1477 * out RTP packets. */
1478 t_rtp = src->last_rtptime;
1480 GST_DEBUG ("last_rtime %" GST_TIME_FORMAT ", last_rtptime %"
1481 G_GUINT64_FORMAT, GST_TIME_ARGS (src->last_rtime), t_rtp);
1483 if (src->clock_rate != -1) {
1484 /* get the diff between the clock running_time and the buffer running_time.
1485 * This is the elapsed time, as measured against the pipeline clock, between
1486 * when the rtp timestamp was observed and the current running_time.
1488 * We need to apply this diff to the RTP timestamp to get the RTP timestamp
1489 * for the given ntpnstime. */
1490 diff = GST_CLOCK_DIFF (src->last_rtime, running_time);
1491 GST_DEBUG ("running_time %" GST_TIME_FORMAT ", diff %" GST_STIME_FORMAT,
1492 GST_TIME_ARGS (running_time), GST_STIME_ARGS (diff));
1494 /* now translate the diff to RTP time, handle positive and negative cases.
1495 * If there is no diff, we already set rtptime correctly above. */
1497 t_rtp += gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1500 t_rtp -= gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1503 GST_WARNING ("no clock-rate, cannot interpolate rtp time for SSRC %u",
1507 /* convert the NTP time in nanoseconds to 32.32 fixed point */
1508 t_current_ntp = gst_util_uint64_scale (ntpnstime, (1LL << 32), GST_SECOND);
1510 GST_DEBUG ("NTP %08x:%08x, RTP %" G_GUINT32_FORMAT,
1511 (guint32) (t_current_ntp >> 32), (guint32) (t_current_ntp & 0xffffffff),
1515 *ntptime = t_current_ntp;
1519 *packet_count = src->stats.packets_sent;
1521 *octet_count = src->stats.octets_sent;
1527 * rtp_source_get_new_rb:
1528 * @src: an #RTPSource
1529 * @time: the current time of the system clock
1530 * @fractionlost: fraction lost since last SR/RR
1531 * @packetslost: the cumulative number of packets lost
1532 * @exthighestseq: the extended last sequence number received
1533 * @jitter: the interarrival jitter (in clock rate units)
1534 * @lsr: the time of the last SR packet on this source
1535 * (in NTP Short Format, 16.16 fixed point)
1536 * @dlsr: the delay since the last SR packet
1537 * (in NTP Short Format, 16.16 fixed point)
1539 * Get new values to put into a new report block from this source.
1541 * Returns: %TRUE on success.
1544 rtp_source_get_new_rb (RTPSource * src, GstClockTime time,
1545 guint8 * fractionlost, gint32 * packetslost, guint32 * exthighestseq,
1546 guint32 * jitter, guint32 * lsr, guint32 * dlsr)
1548 RTPSourceStats *stats;
1549 guint64 extended_max, expected;
1550 guint64 expected_interval, received_interval, ntptime;
1551 gint64 lost, lost_interval;
1552 guint32 fraction, LSR, DLSR;
1553 GstClockTime sr_time;
1555 stats = &src->stats;
1557 extended_max = stats->cycles + stats->max_seq;
1558 expected = extended_max - stats->base_seq + 1;
1560 GST_DEBUG ("ext_max %" G_GUINT64_FORMAT ", expected %" G_GUINT64_FORMAT
1561 ", received %" G_GUINT64_FORMAT ", base_seq %" G_GUINT32_FORMAT,
1562 extended_max, expected, stats->packets_received, stats->base_seq);
1564 lost = expected - stats->packets_received;
1565 lost = CLAMP (lost, -0x800000, 0x7fffff);
1567 expected_interval = expected - stats->prev_expected;
1568 stats->prev_expected = expected;
1569 received_interval = stats->packets_received - stats->prev_received;
1570 stats->prev_received = stats->packets_received;
1572 lost_interval = expected_interval - received_interval;
1574 if (expected_interval == 0 || lost_interval <= 0)
1577 fraction = (lost_interval << 8) / expected_interval;
1579 GST_DEBUG ("add RR for SSRC %08x", src->ssrc);
1580 /* we scaled the jitter up for additional precision */
1581 GST_DEBUG ("fraction %" G_GUINT32_FORMAT ", lost %" G_GINT64_FORMAT
1582 ", extseq %" G_GUINT64_FORMAT ", jitter %d", fraction, lost,
1583 extended_max, stats->jitter >> 4);
1585 if (rtp_source_get_last_sr (src, &sr_time, &ntptime, NULL, NULL, NULL)) {
1588 /* LSR is middle 32 bits of the last ntptime */
1589 LSR = (ntptime >> 16) & 0xffffffff;
1590 diff = time - sr_time;
1591 GST_DEBUG ("last SR time diff %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
1592 /* DLSR, delay since last SR is expressed in 1/65536 second units */
1593 DLSR = gst_util_uint64_scale_int (diff, 65536, GST_SECOND);
1595 /* No valid SR received, LSR/DLSR are set to 0 then */
1596 GST_DEBUG ("no valid SR received");
1600 GST_DEBUG ("LSR %04x:%04x, DLSR %04x:%04x", LSR >> 16, LSR & 0xffff,
1601 DLSR >> 16, DLSR & 0xffff);
1604 *fractionlost = fraction;
1606 *packetslost = lost;
1608 *exthighestseq = extended_max;
1610 *jitter = stats->jitter >> 4;
1620 * rtp_source_get_last_sr:
1621 * @src: an #RTPSource
1622 * @time: time of packet arrival
1623 * @ntptime: the NTP time (in NTP Timestamp Format, 32.32 fixed point)
1624 * @rtptime: the RTP time (in clock rate units)
1625 * @packet_count: the packet count
1626 * @octet_count: the octet count
1628 * Get the values of the last sender report as set with rtp_source_process_sr().
1630 * Returns: %TRUE if there was a valid SR report.
1633 rtp_source_get_last_sr (RTPSource * src, GstClockTime * time, guint64 * ntptime,
1634 guint32 * rtptime, guint32 * packet_count, guint32 * octet_count)
1636 RTPSenderReport *curr;
1638 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1640 curr = &src->stats.sr[src->stats.curr_sr];
1641 if (!curr->is_valid)
1645 *ntptime = curr->ntptime;
1647 *rtptime = curr->rtptime;
1649 *packet_count = curr->packet_count;
1651 *octet_count = curr->octet_count;
1659 * rtp_source_get_last_rb:
1660 * @src: an #RTPSource
1661 * @fractionlost: fraction lost since last SR/RR
1662 * @packetslost: the cumulative number of packets lost
1663 * @exthighestseq: the extended last sequence number received
1664 * @jitter: the interarrival jitter (in clock rate units)
1665 * @lsr: the time of the last SR packet on this source
1666 * (in NTP Short Format, 16.16 fixed point)
1667 * @dlsr: the delay since the last SR packet
1668 * (in NTP Short Format, 16.16 fixed point)
1669 * @round_trip: the round-trip time
1670 * (in NTP Short Format, 16.16 fixed point)
1672 * Get the values of the last RB report set with rtp_source_process_rb().
1674 * Returns: %TRUE if there was a valid SB report.
1677 rtp_source_get_last_rb (RTPSource * src, guint8 * fractionlost,
1678 gint32 * packetslost, guint32 * exthighestseq, guint32 * jitter,
1679 guint32 * lsr, guint32 * dlsr, guint32 * round_trip)
1681 RTPReceiverReport *curr;
1683 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1685 curr = &src->stats.rr[src->stats.curr_rr];
1686 if (!curr->is_valid)
1690 *fractionlost = curr->fractionlost;
1692 *packetslost = curr->packetslost;
1694 *exthighestseq = curr->exthighestseq;
1696 *jitter = curr->jitter;
1702 *round_trip = curr->round_trip;
1708 find_conflicting_address (GList * conflicting_addresses,
1709 GSocketAddress * address, GstClockTime time)
1713 for (item = conflicting_addresses; item; item = g_list_next (item)) {
1714 RTPConflictingAddress *known_conflict = item->data;
1716 if (__g_socket_address_equal (address, known_conflict->address)) {
1717 known_conflict->time = time;
1726 add_conflicting_address (GList * conflicting_addresses,
1727 GSocketAddress * address, GstClockTime time)
1729 RTPConflictingAddress *new_conflict;
1731 new_conflict = g_slice_new (RTPConflictingAddress);
1733 new_conflict->address = G_SOCKET_ADDRESS (g_object_ref (address));
1734 new_conflict->time = time;
1736 return g_list_prepend (conflicting_addresses, new_conflict);
1740 timeout_conflicting_addresses (GList * conflicting_addresses,
1741 GstClockTime current_time)
1744 /* "a relatively long time" -- RFC 3550 section 8.2 */
1745 const GstClockTime collision_timeout =
1746 RTP_STATS_MIN_INTERVAL * GST_SECOND * 10;
1748 item = g_list_first (conflicting_addresses);
1750 RTPConflictingAddress *known_conflict = item->data;
1751 GList *next_item = g_list_next (item);
1753 if (known_conflict->time < current_time - collision_timeout) {
1756 conflicting_addresses = g_list_delete_link (conflicting_addresses, item);
1757 buf = __g_socket_address_to_string (known_conflict->address);
1758 GST_DEBUG ("collision %p timed out: %s", known_conflict, buf);
1760 rtp_conflicting_address_free (known_conflict);
1765 return conflicting_addresses;
1769 * rtp_source_find_conflicting_address:
1770 * @src: The source the packet came in
1771 * @address: address to check for
1772 * @time: The time when the packet that is possibly in conflict arrived
1774 * Checks if an address which has a conflict is already known. If it is
1775 * a known conflict, remember the time
1777 * Returns: TRUE if it was a known conflict, FALSE otherwise
1780 rtp_source_find_conflicting_address (RTPSource * src, GSocketAddress * address,
1783 return find_conflicting_address (src->conflicting_addresses, address, time);
1787 * rtp_source_add_conflicting_address:
1788 * @src: The source the packet came in
1789 * @address: address to remember
1790 * @time: The time when the packet that is in conflict arrived
1792 * Adds a new conflict address
1795 rtp_source_add_conflicting_address (RTPSource * src,
1796 GSocketAddress * address, GstClockTime time)
1798 src->conflicting_addresses =
1799 add_conflicting_address (src->conflicting_addresses, address, time);
1803 * rtp_source_timeout:
1804 * @src: The #RTPSource
1805 * @current_time: The current time
1806 * @feedback_retention_window: The running time before which retained feedback
1807 * packets have to be discarded
1809 * This is processed on each RTCP interval. It times out old collisions.
1810 * It also times out old retained feedback packets
1813 rtp_source_timeout (RTPSource * src, GstClockTime current_time,
1814 GstClockTime feedback_retention_window)
1818 src->conflicting_addresses =
1819 timeout_conflicting_addresses (src->conflicting_addresses, current_time);
1821 /* Time out AVPF packets that are older than the desired length */
1822 while ((pkt = g_queue_peek_tail (src->retained_feedback)) &&
1823 GST_BUFFER_PTS (pkt) < feedback_retention_window)
1824 gst_buffer_unref (g_queue_pop_tail (src->retained_feedback));
1828 compare_buffers (gconstpointer a, gconstpointer b, gpointer user_data)
1830 const GstBuffer *bufa = a;
1831 const GstBuffer *bufb = b;
1833 return GST_BUFFER_PTS (bufa) - GST_BUFFER_PTS (bufb);
1837 rtp_source_retain_rtcp_packet (RTPSource * src, GstRTCPPacket * packet,
1838 GstClockTime running_time)
1842 buffer = gst_buffer_copy_region (packet->rtcp->buffer, GST_BUFFER_COPY_MEMORY,
1843 packet->offset, (gst_rtcp_packet_get_length (packet) + 1) * 4);
1845 GST_BUFFER_PTS (buffer) = running_time;
1847 g_queue_insert_sorted (src->retained_feedback, buffer, compare_buffers, NULL);
1851 rtp_source_has_retained (RTPSource * src, GCompareFunc func, gconstpointer data)
1853 if (g_queue_find_custom (src->retained_feedback, data, func))
1860 * rtp_source_register_nack:
1861 * @src: The #RTPSource
1864 * Register that @seqnum has not been received from @src.
1867 rtp_source_register_nack (RTPSource * src, guint16 seqnum)
1870 guint32 dword = seqnum << 16;
1873 len = src->nacks->len;
1874 for (i = 0; i < len; i++) {
1878 tdword = g_array_index (src->nacks, guint32, i);
1879 tseq = tdword >> 16;
1881 diff = gst_rtp_buffer_compare_seqnum (tseq, seqnum);
1885 /* we already have this seqnum */
1888 /* it comes before the recorded seqnum, FIXME, we could merge it
1889 * if not to far away */
1891 GST_DEBUG ("insert NACK #%u at %u", seqnum, i);
1892 g_array_insert_val (src->nacks, i, dword);
1893 } else if (diff < 16) {
1894 /* we can merge it */
1895 dword = g_array_index (src->nacks, guint32, i);
1896 dword |= 1 << (diff - 1);
1897 GST_DEBUG ("merge NACK #%u at %u with NACK #%u -> 0x%08x", seqnum, i,
1898 dword >> 16, dword);
1899 g_array_index (src->nacks, guint32, i) = dword;
1901 GST_DEBUG ("append NACK #%u", seqnum);
1902 g_array_append_val (src->nacks, dword);
1904 src->send_nack = TRUE;
1908 * rtp_source_get_nacks:
1909 * @src: The #RTPSource
1910 * @n_nacks: result number of nacks
1912 * Get the registered NACKS since the last rtp_source_clear_nacks().
1914 * Returns: an array of @n_nacks seqnum values.
1917 rtp_source_get_nacks (RTPSource * src, guint * n_nacks)
1920 *n_nacks = src->nacks->len;
1922 return (guint32 *) src->nacks->data;
1926 rtp_source_clear_nacks (RTPSource * src)
1928 g_array_set_size (src->nacks, 0);
1929 src->send_nack = FALSE;