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 src->last_keyframe_request = GST_CLOCK_TIME_NONE;
306 rtp_source_reset (src);
310 rtp_conflicting_address_free (RTPConflictingAddress * addr)
312 g_object_unref (addr->address);
313 g_slice_free (RTPConflictingAddress, addr);
317 rtp_source_finalize (GObject * object)
321 src = RTP_SOURCE_CAST (object);
323 g_queue_foreach (src->packets, (GFunc) gst_buffer_unref, NULL);
324 g_queue_free (src->packets);
326 gst_structure_free (src->sdes);
328 g_free (src->bye_reason);
330 gst_caps_replace (&src->caps, NULL);
332 g_list_free_full (src->conflicting_addresses,
333 (GDestroyNotify) rtp_conflicting_address_free);
334 g_queue_foreach (src->retained_feedback, (GFunc) gst_buffer_unref, NULL);
335 g_queue_free (src->retained_feedback);
337 g_array_free (src->nacks, TRUE);
340 g_object_unref (src->rtp_from);
342 g_object_unref (src->rtcp_from);
344 g_hash_table_unref (src->reported_in_sr_of);
346 G_OBJECT_CLASS (rtp_source_parent_class)->finalize (object);
349 static GstStructure *
350 rtp_source_create_stats (RTPSource * src)
353 gboolean is_sender = src->is_sender;
354 gboolean internal = src->internal;
357 guint8 fractionlost = 0;
358 gint32 packetslost = 0;
359 guint32 exthighestseq = 0;
363 guint32 round_trip = 0;
365 GstClockTime time = 0;
368 guint32 packet_count = 0;
369 guint32 octet_count = 0;
372 /* common data for all types of sources */
373 s = gst_structure_new ("application/x-rtp-source-stats",
374 "ssrc", G_TYPE_UINT, (guint) src->ssrc,
375 "internal", G_TYPE_BOOLEAN, internal,
376 "validated", G_TYPE_BOOLEAN, src->validated,
377 "received-bye", G_TYPE_BOOLEAN, src->marked_bye,
378 "is-csrc", G_TYPE_BOOLEAN, src->is_csrc,
379 "is-sender", G_TYPE_BOOLEAN, is_sender,
380 "seqnum-base", G_TYPE_INT, src->seqnum_offset,
381 "clock-rate", G_TYPE_INT, src->clock_rate, NULL);
383 /* add address and port */
385 address_str = __g_socket_address_to_string (src->rtp_from);
386 gst_structure_set (s, "rtp-from", G_TYPE_STRING, address_str, NULL);
387 g_free (address_str);
389 if (src->rtcp_from) {
390 address_str = __g_socket_address_to_string (src->rtcp_from);
391 gst_structure_set (s, "rtcp-from", G_TYPE_STRING, address_str, NULL);
392 g_free (address_str);
395 gst_structure_set (s,
396 "octets-sent", G_TYPE_UINT64, src->stats.octets_sent,
397 "packets-sent", G_TYPE_UINT64, src->stats.packets_sent,
398 "octets-received", G_TYPE_UINT64, src->stats.octets_received,
399 "packets-received", G_TYPE_UINT64, src->stats.packets_received,
400 "bitrate", G_TYPE_UINT64, src->bitrate,
401 "packets-lost", G_TYPE_INT,
402 (gint) rtp_stats_get_packets_lost (&src->stats), "jitter", G_TYPE_UINT,
403 (guint) (src->stats.jitter >> 4),
404 "sent-pli-count", G_TYPE_UINT, src->stats.sent_pli_count,
405 "recv-pli-count", G_TYPE_UINT, src->stats.recv_pli_count,
406 "sent-fir-count", G_TYPE_UINT, src->stats.sent_fir_count,
407 "recv-fir-count", G_TYPE_UINT, src->stats.recv_fir_count,
408 "sent-nack-count", G_TYPE_UINT, src->stats.sent_nack_count,
409 "recv-nack-count", G_TYPE_UINT, src->stats.recv_nack_count, NULL);
411 /* get the last SR. */
412 have_sr = rtp_source_get_last_sr (src, &time, &ntptime, &rtptime,
413 &packet_count, &octet_count);
414 gst_structure_set (s,
415 "have-sr", G_TYPE_BOOLEAN, have_sr,
416 "sr-ntptime", G_TYPE_UINT64, ntptime,
417 "sr-rtptime", G_TYPE_UINT, (guint) rtptime,
418 "sr-octet-count", G_TYPE_UINT, (guint) octet_count,
419 "sr-packet-count", G_TYPE_UINT, (guint) packet_count, NULL);
422 /* get the last RB we sent */
423 gst_structure_set (s,
424 "sent-rb", G_TYPE_BOOLEAN, src->last_rr.is_valid,
425 "sent-rb-fractionlost", G_TYPE_UINT, (guint) src->last_rr.fractionlost,
426 "sent-rb-packetslost", G_TYPE_INT, (gint) src->last_rr.packetslost,
427 "sent-rb-exthighestseq", G_TYPE_UINT,
428 (guint) src->last_rr.exthighestseq, "sent-rb-jitter", G_TYPE_UINT,
429 (guint) src->last_rr.jitter, "sent-rb-lsr", G_TYPE_UINT,
430 (guint) src->last_rr.lsr, "sent-rb-dlsr", G_TYPE_UINT,
431 (guint) src->last_rr.dlsr, NULL);
433 /* get the last RB */
434 have_rb = rtp_source_get_last_rb (src, &fractionlost, &packetslost,
435 &exthighestseq, &jitter, &lsr, &dlsr, &round_trip);
437 gst_structure_set (s,
438 "have-rb", G_TYPE_BOOLEAN, have_rb,
439 "rb-fractionlost", G_TYPE_UINT, (guint) fractionlost,
440 "rb-packetslost", G_TYPE_INT, (gint) packetslost,
441 "rb-exthighestseq", G_TYPE_UINT, (guint) exthighestseq,
442 "rb-jitter", G_TYPE_UINT, (guint) jitter,
443 "rb-lsr", G_TYPE_UINT, (guint) lsr,
444 "rb-dlsr", G_TYPE_UINT, (guint) dlsr,
445 "rb-round-trip", G_TYPE_UINT, (guint) round_trip, NULL);
452 * rtp_source_get_sdes_struct:
453 * @src: an #RTPSource
455 * Get the SDES from @src. See the SDES property for more details.
457 * Returns: %GstStructure of type "application/x-rtp-source-sdes". The result is
458 * valid until the SDES items of @src are modified.
461 rtp_source_get_sdes_struct (RTPSource * src)
463 g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
469 sdes_struct_compare_func (GQuark field_id, const GValue * value,
475 old = GST_STRUCTURE (user_data);
476 field = g_quark_to_string (field_id);
478 if (!gst_structure_has_field (old, field))
481 g_assert (G_VALUE_HOLDS_STRING (value));
483 return strcmp (g_value_get_string (value), gst_structure_get_string (old,
488 * rtp_source_set_sdes_struct:
489 * @src: an #RTPSource
490 * @sdes: the SDES structure
492 * Store the @sdes in @src. @sdes must be a structure of type
493 * "application/x-rtp-source-sdes", see the SDES property for more details.
495 * This function takes ownership of @sdes.
497 * Returns: %FALSE if the SDES was unchanged.
500 rtp_source_set_sdes_struct (RTPSource * src, GstStructure * sdes)
504 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
505 g_return_val_if_fail (strcmp (gst_structure_get_name (sdes),
506 "application/x-rtp-source-sdes") == 0, FALSE);
508 changed = !gst_structure_foreach (sdes, sdes_struct_compare_func, src->sdes);
511 gst_structure_free (src->sdes);
514 gst_structure_free (sdes);
520 rtp_source_set_property (GObject * object, guint prop_id,
521 const GValue * value, GParamSpec * pspec)
525 src = RTP_SOURCE (object);
529 src->ssrc = g_value_get_uint (value);
532 src->probation = g_value_get_uint (value);
534 case PROP_MAX_DROPOUT_TIME:
535 src->max_dropout_time = g_value_get_uint (value);
537 case PROP_MAX_MISORDER_TIME:
538 src->max_misorder_time = g_value_get_uint (value);
541 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
547 rtp_source_get_property (GObject * object, guint prop_id,
548 GValue * value, GParamSpec * pspec)
552 src = RTP_SOURCE (object);
556 g_value_set_uint (value, rtp_source_get_ssrc (src));
559 g_value_set_boolean (value, rtp_source_is_as_csrc (src));
561 case PROP_IS_VALIDATED:
562 g_value_set_boolean (value, rtp_source_is_validated (src));
565 g_value_set_boolean (value, rtp_source_is_sender (src));
568 g_value_set_boxed (value, rtp_source_get_sdes_struct (src));
571 g_value_take_boxed (value, rtp_source_create_stats (src));
574 g_value_set_uint (value, src->probation);
576 case PROP_MAX_DROPOUT_TIME:
577 g_value_set_uint (value, src->max_dropout_time);
579 case PROP_MAX_MISORDER_TIME:
580 g_value_set_uint (value, src->max_misorder_time);
583 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
592 * Create a #RTPSource with @ssrc.
594 * Returns: a new #RTPSource. Use g_object_unref() after usage.
597 rtp_source_new (guint32 ssrc)
601 src = g_object_new (RTP_TYPE_SOURCE, NULL);
608 * rtp_source_set_callbacks:
609 * @src: an #RTPSource
610 * @cb: callback functions
611 * @user_data: user data
613 * Set the callbacks for the source.
616 rtp_source_set_callbacks (RTPSource * src, RTPSourceCallbacks * cb,
619 g_return_if_fail (RTP_IS_SOURCE (src));
621 src->callbacks.push_rtp = cb->push_rtp;
622 src->callbacks.clock_rate = cb->clock_rate;
623 src->user_data = user_data;
627 * rtp_source_get_ssrc:
628 * @src: an #RTPSource
630 * Get the SSRC of @source.
632 * Returns: the SSRC of src.
635 rtp_source_get_ssrc (RTPSource * src)
639 g_return_val_if_fail (RTP_IS_SOURCE (src), 0);
647 * rtp_source_set_as_csrc:
648 * @src: an #RTPSource
650 * Configure @src as a CSRC, this will also validate @src.
653 rtp_source_set_as_csrc (RTPSource * src)
655 g_return_if_fail (RTP_IS_SOURCE (src));
657 src->validated = TRUE;
662 * rtp_source_is_as_csrc:
663 * @src: an #RTPSource
665 * Check if @src is a contributing source.
667 * Returns: %TRUE if @src is acting as a contributing source.
670 rtp_source_is_as_csrc (RTPSource * src)
674 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
676 result = src->is_csrc;
682 * rtp_source_is_active:
683 * @src: an #RTPSource
685 * Check if @src is an active source. A source is active if it has been
686 * validated and has not yet received a BYE packet
688 * Returns: %TRUE if @src is an qactive source.
691 rtp_source_is_active (RTPSource * src)
695 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
697 result = RTP_SOURCE_IS_ACTIVE (src);
703 * rtp_source_is_validated:
704 * @src: an #RTPSource
706 * Check if @src is a validated source.
708 * Returns: %TRUE if @src is a validated source.
711 rtp_source_is_validated (RTPSource * src)
715 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
717 result = src->validated;
723 * rtp_source_is_sender:
724 * @src: an #RTPSource
726 * Check if @src is a sending source.
728 * Returns: %TRUE if @src is a sending source.
731 rtp_source_is_sender (RTPSource * src)
735 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
737 result = RTP_SOURCE_IS_SENDER (src);
743 * rtp_source_is_marked_bye:
744 * @src: an #RTPSource
746 * Check if @src is marked as leaving the session with a BYE packet.
748 * Returns: %TRUE if @src has been marked BYE.
751 rtp_source_is_marked_bye (RTPSource * src)
755 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
757 result = RTP_SOURCE_IS_MARKED_BYE (src);
764 * rtp_source_get_bye_reason:
765 * @src: an #RTPSource
767 * Get the BYE reason for @src. Check if the source is marked as leaving the
768 * session with a BYE message first with rtp_source_is_marked_bye().
770 * Returns: The BYE reason or NULL when no reason was given or the source was
771 * not marked BYE yet. g_free() after usage.
774 rtp_source_get_bye_reason (RTPSource * src)
778 g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
780 result = g_strdup (src->bye_reason);
786 * rtp_source_update_caps:
787 * @src: an #RTPSource
790 * Parse @caps and store all relevant information in @source.
793 rtp_source_update_caps (RTPSource * src, GstCaps * caps)
800 /* nothing changed, return */
801 if (caps == NULL || src->caps == caps)
804 s = gst_caps_get_structure (caps, 0);
806 rtx = (gst_structure_get_uint (s, "rtx-ssrc", &val) && val == src->ssrc);
808 if (gst_structure_get_int (s, rtx ? "rtx-payload" : "payload", &ival))
813 GST_DEBUG ("got %spayload %d", rtx ? "rtx " : "", src->payload);
815 if (gst_structure_get_int (s, "clock-rate", &ival))
816 src->clock_rate = ival;
818 src->clock_rate = -1;
820 GST_DEBUG ("got clock-rate %d", src->clock_rate);
822 if (gst_structure_get_uint (s, rtx ? "rtx-seqnum-offset" : "seqnum-offset",
824 src->seqnum_offset = val;
826 src->seqnum_offset = -1;
828 GST_DEBUG ("got %sseqnum-offset %" G_GINT32_FORMAT, rtx ? "rtx " : "",
831 gst_caps_replace (&src->caps, caps);
835 * rtp_source_set_rtp_from:
836 * @src: an #RTPSource
837 * @address: the RTP address to set
839 * Set that @src is receiving RTP packets from @address. This is used for
840 * collistion checking.
843 rtp_source_set_rtp_from (RTPSource * src, GSocketAddress * address)
845 g_return_if_fail (RTP_IS_SOURCE (src));
848 g_object_unref (src->rtp_from);
849 src->rtp_from = G_SOCKET_ADDRESS (g_object_ref (address));
853 * rtp_source_set_rtcp_from:
854 * @src: an #RTPSource
855 * @address: the RTCP address to set
857 * Set that @src is receiving RTCP packets from @address. This is used for
858 * collistion checking.
861 rtp_source_set_rtcp_from (RTPSource * src, GSocketAddress * address)
863 g_return_if_fail (RTP_IS_SOURCE (src));
866 g_object_unref (src->rtcp_from);
867 src->rtcp_from = G_SOCKET_ADDRESS (g_object_ref (address));
871 push_packet (RTPSource * src, GstBuffer * buffer)
873 GstFlowReturn ret = GST_FLOW_OK;
875 /* push queued packets first if any */
876 while (!g_queue_is_empty (src->packets)) {
877 GstBuffer *buffer = GST_BUFFER_CAST (g_queue_pop_head (src->packets));
879 GST_LOG ("pushing queued packet");
880 if (src->callbacks.push_rtp)
881 src->callbacks.push_rtp (src, buffer, src->user_data);
883 gst_buffer_unref (buffer);
885 GST_LOG ("pushing new packet");
887 if (src->callbacks.push_rtp)
888 ret = src->callbacks.push_rtp (src, buffer, src->user_data);
890 gst_buffer_unref (buffer);
896 get_clock_rate (RTPSource * src, guint8 payload)
898 if (src->payload == -1) {
899 /* first payload received, nothing was in the caps, lock on to this payload */
900 src->payload = payload;
901 GST_DEBUG ("first payload %d", payload);
902 } else if (payload != src->payload) {
903 /* we have a different payload than before, reset the clock-rate */
904 GST_DEBUG ("new payload %d", payload);
905 src->payload = payload;
906 src->clock_rate = -1;
907 src->stats.transit = -1;
910 if (src->clock_rate == -1) {
911 gint clock_rate = -1;
913 if (src->callbacks.clock_rate)
914 clock_rate = src->callbacks.clock_rate (src, payload, src->user_data);
916 GST_DEBUG ("got clock-rate %d", clock_rate);
918 src->clock_rate = clock_rate;
919 gst_rtp_packet_rate_ctx_reset (&src->packet_rate_ctx, clock_rate);
921 return src->clock_rate;
924 /* Jitter is the variation in the delay of received packets in a flow. It is
925 * measured by comparing the interval when RTP packets were sent to the interval
926 * at which they were received. For instance, if packet #1 and packet #2 leave
927 * 50 milliseconds apart and arrive 60 milliseconds apart, then the jitter is 10
930 calculate_jitter (RTPSource * src, RTPPacketInfo * pinfo)
932 GstClockTime running_time;
933 guint32 rtparrival, transit, rtptime;
938 /* get arrival time */
939 if ((running_time = pinfo->running_time) == GST_CLOCK_TIME_NONE)
944 GST_LOG ("SSRC %08x got payload %d", src->ssrc, pt);
947 if ((clock_rate = get_clock_rate (src, pt)) == -1)
950 rtptime = pinfo->rtptime;
952 /* convert arrival time to RTP timestamp units, truncate to 32 bits, we don't
953 * care about the absolute value, just the difference. */
954 rtparrival = gst_util_uint64_scale_int (running_time, clock_rate, GST_SECOND);
956 /* transit time is difference with RTP timestamp */
957 transit = rtparrival - rtptime;
959 /* get ABS diff with previous transit time */
960 if (src->stats.transit != -1) {
961 if (transit > src->stats.transit)
962 diff = transit - src->stats.transit;
964 diff = src->stats.transit - transit;
968 src->stats.transit = transit;
970 /* update jitter, the value we store is scaled up so we can keep precision. */
971 src->stats.jitter += diff - ((src->stats.jitter + 8) >> 4);
973 src->stats.prev_rtptime = src->stats.last_rtptime;
974 src->stats.last_rtptime = rtparrival;
976 GST_LOG ("rtparrival %u, rtptime %u, clock-rate %d, diff %d, jitter: %f",
977 rtparrival, rtptime, clock_rate, diff, (src->stats.jitter) / 16.0);
984 GST_WARNING ("cannot get current running_time");
989 GST_WARNING ("cannot get clock-rate for pt %d", pt);
995 init_seq (RTPSource * src, guint16 seq)
997 src->stats.base_seq = seq;
998 src->stats.max_seq = seq;
999 src->stats.bad_seq = RTP_SEQ_MOD + 1; /* so seq == bad_seq is false */
1000 src->stats.cycles = 0;
1001 src->stats.packets_received = 0;
1002 src->stats.octets_received = 0;
1003 src->stats.bytes_received = 0;
1004 src->stats.prev_received = 0;
1005 src->stats.prev_expected = 0;
1006 src->stats.recv_pli_count = 0;
1007 src->stats.recv_fir_count = 0;
1009 GST_DEBUG ("base_seq %d", seq);
1012 #define BITRATE_INTERVAL (2 * GST_SECOND)
1015 do_bitrate_estimation (RTPSource * src, GstClockTime running_time,
1016 guint64 * bytes_handled)
1020 if (src->prev_rtime) {
1021 elapsed = running_time - src->prev_rtime;
1023 if (elapsed > BITRATE_INTERVAL) {
1026 rate = gst_util_uint64_scale (*bytes_handled, 8 * GST_SECOND, elapsed);
1028 GST_LOG ("Elapsed %" G_GUINT64_FORMAT ", bytes %" G_GUINT64_FORMAT
1029 ", rate %" G_GUINT64_FORMAT, elapsed, *bytes_handled, rate);
1031 if (src->bitrate == 0)
1032 src->bitrate = rate;
1034 src->bitrate = ((src->bitrate * 3) + rate) / 4;
1036 src->prev_rtime = running_time;
1040 GST_LOG ("Reset bitrate measurement");
1041 src->prev_rtime = running_time;
1047 update_receiver_stats (RTPSource * src, RTPPacketInfo * pinfo,
1048 gboolean is_receive)
1050 guint16 seqnr, expected;
1051 RTPSourceStats *stats;
1053 gint32 packet_rate, max_dropout, max_misorder;
1055 stats = &src->stats;
1057 seqnr = pinfo->seqnum;
1060 gst_rtp_packet_rate_ctx_update (&src->packet_rate_ctx, pinfo->seqnum,
1063 gst_rtp_packet_rate_ctx_get_max_dropout (&src->packet_rate_ctx,
1064 src->max_dropout_time);
1066 gst_rtp_packet_rate_ctx_get_max_misorder (&src->packet_rate_ctx,
1067 src->max_misorder_time);
1068 GST_TRACE ("SSRC %08x, packet_rate: %d, max_dropout: %d, max_misorder: %d",
1069 src->ssrc, packet_rate, max_dropout, max_misorder);
1071 if (stats->cycles == -1) {
1072 GST_DEBUG ("received first packet");
1073 /* first time we heard of this source */
1074 init_seq (src, seqnr);
1075 src->stats.max_seq = seqnr - 1;
1076 src->curr_probation = src->probation;
1080 expected = src->stats.max_seq + 1;
1081 delta = gst_rtp_buffer_compare_seqnum (expected, seqnr);
1083 /* if we are still on probation, check seqnum */
1084 if (src->curr_probation) {
1085 /* when in probation, we require consecutive seqnums */
1087 /* expected packet */
1088 GST_DEBUG ("probation: seqnr %d == expected %d", seqnr, expected);
1089 src->curr_probation--;
1090 if (seqnr < stats->max_seq) {
1091 /* sequence number wrapped - count another 64K cycle. */
1092 stats->cycles += RTP_SEQ_MOD;
1094 src->stats.max_seq = seqnr;
1096 if (src->curr_probation == 0) {
1097 GST_DEBUG ("probation done!");
1098 init_seq (src, seqnr);
1102 GST_DEBUG ("probation %d: queue packet", src->curr_probation);
1103 /* when still in probation, keep packets in a list. */
1104 g_queue_push_tail (src->packets, pinfo->data);
1106 /* remove packets from queue if there are too many */
1107 while (g_queue_get_length (src->packets) > RTP_MAX_PROBATION_LEN) {
1108 q = g_queue_pop_head (src->packets);
1109 gst_buffer_unref (q);
1114 /* unexpected seqnum in probation */
1115 goto probation_seqnum;
1117 } else if (delta >= 0 && delta < max_dropout) {
1118 /* Clear bad packets */
1119 stats->bad_seq = RTP_SEQ_MOD + 1; /* so seq == bad_seq is false */
1120 g_queue_foreach (src->packets, (GFunc) gst_buffer_unref, NULL);
1121 g_queue_clear (src->packets);
1123 /* in order, with permissible gap */
1124 if (seqnr < stats->max_seq) {
1125 /* sequence number wrapped - count another 64K cycle. */
1126 stats->cycles += RTP_SEQ_MOD;
1128 stats->max_seq = seqnr;
1129 } else if (delta < -max_misorder || delta >= max_dropout) {
1130 /* the sequence number made a very large jump */
1131 if (seqnr == stats->bad_seq && src->packets->head) {
1132 /* two sequential packets -- assume that the other side
1133 * restarted without telling us so just re-sync
1134 * (i.e., pretend this was the first packet). */
1135 init_seq (src, seqnr);
1137 /* unacceptable jump */
1138 stats->bad_seq = (seqnr + 1) & (RTP_SEQ_MOD - 1);
1139 g_queue_foreach (src->packets, (GFunc) gst_buffer_unref, NULL);
1140 g_queue_clear (src->packets);
1141 g_queue_push_tail (src->packets, pinfo->data);
1145 } else { /* delta < 0 && delta >= -max_misorder */
1146 /* Clear bad packets */
1147 stats->bad_seq = RTP_SEQ_MOD + 1; /* so seq == bad_seq is false */
1148 g_queue_foreach (src->packets, (GFunc) gst_buffer_unref, NULL);
1149 g_queue_clear (src->packets);
1151 /* duplicate or reordered packet, will be filtered by jitterbuffer. */
1152 GST_INFO ("duplicate or reordered packet (seqnr %u, expected %u)",
1157 src->stats.octets_received += pinfo->payload_len;
1158 src->stats.bytes_received += pinfo->bytes;
1159 src->stats.packets_received++;
1160 /* for the bitrate estimation */
1161 src->bytes_received += pinfo->payload_len;
1163 GST_LOG ("seq %u, PC: %" G_GUINT64_FORMAT ", OC: %" G_GUINT64_FORMAT,
1164 seqnr, src->stats.packets_received, src->stats.octets_received);
1176 ("unacceptable seqnum received (seqnr %u, delta %d, packet_rate: %d, max_dropout: %d, max_misorder: %d)",
1177 seqnr, delta, packet_rate, max_dropout, max_misorder);
1182 GST_WARNING ("probation: seqnr %d != expected %d", seqnr, expected);
1183 src->curr_probation = src->probation;
1184 src->stats.max_seq = seqnr;
1190 * rtp_source_process_rtp:
1191 * @src: an #RTPSource
1192 * @pinfo: an #RTPPacketInfo
1194 * Let @src handle the incomming RTP packet described in @pinfo.
1196 * Returns: a #GstFlowReturn.
1199 rtp_source_process_rtp (RTPSource * src, RTPPacketInfo * pinfo)
1201 GstFlowReturn result;
1203 g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
1204 g_return_val_if_fail (pinfo != NULL, GST_FLOW_ERROR);
1206 if (!update_receiver_stats (src, pinfo, TRUE))
1209 /* the source that sent the packet must be a sender */
1210 src->is_sender = TRUE;
1211 src->validated = TRUE;
1213 do_bitrate_estimation (src, pinfo->running_time, &src->bytes_received);
1215 /* calculate jitter for the stats */
1216 calculate_jitter (src, pinfo);
1218 /* we're ready to push the RTP packet now */
1219 result = push_packet (src, pinfo->data);
1226 * rtp_source_mark_bye:
1227 * @src: an #RTPSource
1228 * @reason: the reason for leaving
1230 * Mark @src in the BYE state. This can happen when the source wants to
1231 * leave the sesssion or when a BYE packets has been received.
1233 * This will make the source inactive.
1236 rtp_source_mark_bye (RTPSource * src, const gchar * reason)
1238 g_return_if_fail (RTP_IS_SOURCE (src));
1240 GST_DEBUG ("marking SSRC %08x as BYE, reason: %s", src->ssrc,
1241 GST_STR_NULL (reason));
1243 /* copy the reason and mark as bye */
1244 g_free (src->bye_reason);
1245 src->bye_reason = g_strdup (reason);
1246 src->marked_bye = TRUE;
1250 * rtp_source_send_rtp:
1251 * @src: an #RTPSource
1252 * @data: an RTP buffer or a list of RTP buffers
1253 * @is_list: if @data is a buffer or list
1254 * @running_time: the running time of @data
1256 * Send @data (an RTP buffer or list of buffers) originating from @src.
1257 * This will make @src a sender. This function takes ownership of @data and
1258 * modifies the SSRC in the RTP packet to that of @src when needed.
1260 * Returns: a #GstFlowReturn.
1263 rtp_source_send_rtp (RTPSource * src, RTPPacketInfo * pinfo)
1265 GstFlowReturn result;
1266 GstClockTime running_time;
1268 guint64 ext_rtptime;
1269 guint64 rt_diff, rtp_diff;
1271 g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
1273 /* we are a sender now */
1274 src->is_sender = TRUE;
1276 /* we are also a receiver of our packets */
1277 if (!update_receiver_stats (src, pinfo, FALSE))
1280 /* update stats for the SR */
1281 src->stats.packets_sent += pinfo->packets;
1282 src->stats.octets_sent += pinfo->payload_len;
1283 src->bytes_sent += pinfo->payload_len;
1285 running_time = pinfo->running_time;
1287 do_bitrate_estimation (src, running_time, &src->bytes_sent);
1289 rtptime = pinfo->rtptime;
1291 ext_rtptime = src->last_rtptime;
1292 ext_rtptime = gst_rtp_buffer_ext_timestamp (&ext_rtptime, rtptime);
1294 GST_LOG ("SSRC %08x, RTP %" G_GUINT64_FORMAT ", running_time %"
1295 GST_TIME_FORMAT, src->ssrc, ext_rtptime, GST_TIME_ARGS (running_time));
1297 if (ext_rtptime > src->last_rtptime) {
1298 rtp_diff = ext_rtptime - src->last_rtptime;
1299 rt_diff = running_time - src->last_rtime;
1301 /* calc the diff so we can detect drift at the sender. This can also be used
1302 * to guestimate the clock rate if the NTP time is locked to the RTP
1303 * timestamps (as is the case when the capture device is providing the clock). */
1304 GST_LOG ("SSRC %08x, diff RTP %" G_GUINT64_FORMAT ", diff running_time %"
1305 GST_TIME_FORMAT, src->ssrc, rtp_diff, GST_TIME_ARGS (rt_diff));
1308 /* we keep track of the last received RTP timestamp and the corresponding
1309 * buffer running_time so that we can use this info when constructing SR reports */
1310 src->last_rtime = running_time;
1311 src->last_rtptime = ext_rtptime;
1314 if (!src->callbacks.push_rtp)
1317 GST_LOG ("pushing RTP %s %" G_GUINT64_FORMAT,
1318 pinfo->is_list ? "list" : "packet", src->stats.packets_sent);
1320 result = src->callbacks.push_rtp (src, pinfo->data, src->user_data);
1328 GST_WARNING ("no callback installed, dropping packet");
1334 * rtp_source_process_sr:
1335 * @src: an #RTPSource
1336 * @time: time of packet arrival
1337 * @ntptime: the NTP time (in NTP Timestamp Format, 32.32 fixed point)
1338 * @rtptime: the RTP time (in clock rate units)
1339 * @packet_count: the packet count
1340 * @octet_count: the octet count
1342 * Update the sender report in @src.
1345 rtp_source_process_sr (RTPSource * src, GstClockTime time, guint64 ntptime,
1346 guint32 rtptime, guint32 packet_count, guint32 octet_count)
1348 RTPSenderReport *curr;
1351 g_return_if_fail (RTP_IS_SOURCE (src));
1353 GST_DEBUG ("got SR packet: SSRC %08x, NTP %08x:%08x, RTP %" G_GUINT32_FORMAT
1354 ", PC %" G_GUINT32_FORMAT ", OC %" G_GUINT32_FORMAT, src->ssrc,
1355 (guint32) (ntptime >> 32), (guint32) (ntptime & 0xffffffff), rtptime,
1356 packet_count, octet_count);
1358 curridx = src->stats.curr_sr ^ 1;
1359 curr = &src->stats.sr[curridx];
1361 /* this is a sender now */
1362 src->is_sender = TRUE;
1364 /* update current */
1365 curr->is_valid = TRUE;
1366 curr->ntptime = ntptime;
1367 curr->rtptime = rtptime;
1368 curr->packet_count = packet_count;
1369 curr->octet_count = octet_count;
1373 src->stats.curr_sr = curridx;
1375 src->stats.prev_rtcptime = src->stats.last_rtcptime;
1376 src->stats.last_rtcptime = time;
1380 * rtp_source_process_rb:
1381 * @src: an #RTPSource
1382 * @ntpnstime: the current time in nanoseconds since 1970
1383 * @fractionlost: fraction lost since last SR/RR
1384 * @packetslost: the cumulative number of packets lost
1385 * @exthighestseq: the extended last sequence number received
1386 * @jitter: the interarrival jitter (in clock rate units)
1387 * @lsr: the time of the last SR packet on this source
1388 * (in NTP Short Format, 16.16 fixed point)
1389 * @dlsr: the delay since the last SR packet
1390 * (in NTP Short Format, 16.16 fixed point)
1392 * Update the report block in @src.
1395 rtp_source_process_rb (RTPSource * src, guint64 ntpnstime,
1396 guint8 fractionlost, gint32 packetslost, guint32 exthighestseq,
1397 guint32 jitter, guint32 lsr, guint32 dlsr)
1399 RTPReceiverReport *curr;
1404 g_return_if_fail (RTP_IS_SOURCE (src));
1406 GST_DEBUG ("got RB packet: SSRC %08x, FL %2x, PL %d, HS %" G_GUINT32_FORMAT
1407 ", jitter %" G_GUINT32_FORMAT ", LSR %04x:%04x, DLSR %04x:%04x",
1408 src->ssrc, fractionlost, packetslost, exthighestseq, jitter, lsr >> 16,
1409 lsr & 0xffff, dlsr >> 16, dlsr & 0xffff);
1411 curridx = src->stats.curr_rr ^ 1;
1412 curr = &src->stats.rr[curridx];
1414 /* update current */
1415 curr->is_valid = TRUE;
1416 curr->fractionlost = fractionlost;
1417 curr->packetslost = packetslost;
1418 curr->exthighestseq = exthighestseq;
1419 curr->jitter = jitter;
1423 /* convert the NTP time in nanoseconds to 32.32 fixed point */
1424 f_ntp = gst_util_uint64_scale (ntpnstime, (1LL << 32), GST_SECOND);
1425 /* calculate round trip, round the time up */
1426 ntp = ((f_ntp + 0xffff) >> 16) & 0xffffffff;
1429 if (A > 0 && ntp > A)
1433 curr->round_trip = A;
1435 GST_DEBUG ("NTP %04x:%04x, round trip %04x:%04x", ntp >> 16, ntp & 0xffff,
1436 A >> 16, A & 0xffff);
1439 src->stats.curr_rr = curridx;
1443 * rtp_source_get_new_sr:
1444 * @src: an #RTPSource
1445 * @ntpnstime: the current time in nanoseconds since 1970
1446 * @running_time: the current running_time of the pipeline
1447 * @ntptime: the NTP time (in NTP Timestamp Format, 32.32 fixed point)
1448 * @rtptime: the RTP time corresponding to @ntptime (in clock rate units)
1449 * @packet_count: the packet count
1450 * @octet_count: the octet count
1452 * Get new values to put into a new SR report from this source.
1454 * @running_time and @ntpnstime are captured at the same time and represent the
1455 * running time of the pipeline clock and the absolute current system time in
1456 * nanoseconds respectively. Together with the last running_time and RTP timestamp
1457 * we have observed in the source, we can generate @ntptime and @rtptime for an SR
1458 * packet. @ntptime is basically the fixed point representation of @ntpnstime
1459 * and @rtptime the associated RTP timestamp.
1461 * Returns: %TRUE on success.
1464 rtp_source_get_new_sr (RTPSource * src, guint64 ntpnstime,
1465 GstClockTime running_time, guint64 * ntptime, guint32 * rtptime,
1466 guint32 * packet_count, guint32 * octet_count)
1469 guint64 t_current_ntp;
1470 GstClockTimeDiff diff;
1472 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1474 /* We last saw a buffer with last_rtptime at last_rtime. Given a running_time
1475 * and an NTP time, we can scale the RTP timestamps so that they match the
1476 * given NTP time. for scaling, we assume that the slope of the rtptime vs
1477 * running_time vs ntptime curve is close to 1, which is certainly
1478 * sufficient for the frequency at which we report SR and the rate we send
1479 * out RTP packets. */
1480 t_rtp = src->last_rtptime;
1482 GST_DEBUG ("last_rtime %" GST_TIME_FORMAT ", last_rtptime %"
1483 G_GUINT64_FORMAT, GST_TIME_ARGS (src->last_rtime), t_rtp);
1485 if (src->clock_rate != -1) {
1486 /* get the diff between the clock running_time and the buffer running_time.
1487 * This is the elapsed time, as measured against the pipeline clock, between
1488 * when the rtp timestamp was observed and the current running_time.
1490 * We need to apply this diff to the RTP timestamp to get the RTP timestamp
1491 * for the given ntpnstime. */
1492 diff = GST_CLOCK_DIFF (src->last_rtime, running_time);
1493 GST_DEBUG ("running_time %" GST_TIME_FORMAT ", diff %" GST_STIME_FORMAT,
1494 GST_TIME_ARGS (running_time), GST_STIME_ARGS (diff));
1496 /* now translate the diff to RTP time, handle positive and negative cases.
1497 * If there is no diff, we already set rtptime correctly above. */
1499 t_rtp += gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1502 t_rtp -= gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1505 GST_WARNING ("no clock-rate, cannot interpolate rtp time for SSRC %u",
1509 /* convert the NTP time in nanoseconds to 32.32 fixed point */
1510 t_current_ntp = gst_util_uint64_scale (ntpnstime, (1LL << 32), GST_SECOND);
1512 GST_DEBUG ("NTP %08x:%08x, RTP %" G_GUINT32_FORMAT,
1513 (guint32) (t_current_ntp >> 32), (guint32) (t_current_ntp & 0xffffffff),
1517 *ntptime = t_current_ntp;
1521 *packet_count = src->stats.packets_sent;
1523 *octet_count = src->stats.octets_sent;
1529 * rtp_source_get_new_rb:
1530 * @src: an #RTPSource
1531 * @time: the current time of the system clock
1532 * @fractionlost: fraction lost since last SR/RR
1533 * @packetslost: the cumulative number of packets lost
1534 * @exthighestseq: the extended last sequence number received
1535 * @jitter: the interarrival jitter (in clock rate units)
1536 * @lsr: the time of the last SR packet on this source
1537 * (in NTP Short Format, 16.16 fixed point)
1538 * @dlsr: the delay since the last SR packet
1539 * (in NTP Short Format, 16.16 fixed point)
1541 * Get new values to put into a new report block from this source.
1543 * Returns: %TRUE on success.
1546 rtp_source_get_new_rb (RTPSource * src, GstClockTime time,
1547 guint8 * fractionlost, gint32 * packetslost, guint32 * exthighestseq,
1548 guint32 * jitter, guint32 * lsr, guint32 * dlsr)
1550 RTPSourceStats *stats;
1551 guint64 extended_max, expected;
1552 guint64 expected_interval, received_interval, ntptime;
1553 gint64 lost, lost_interval;
1554 guint32 fraction, LSR, DLSR;
1555 GstClockTime sr_time;
1557 stats = &src->stats;
1559 extended_max = stats->cycles + stats->max_seq;
1560 expected = extended_max - stats->base_seq + 1;
1562 GST_DEBUG ("ext_max %" G_GUINT64_FORMAT ", expected %" G_GUINT64_FORMAT
1563 ", received %" G_GUINT64_FORMAT ", base_seq %" G_GUINT32_FORMAT,
1564 extended_max, expected, stats->packets_received, stats->base_seq);
1566 lost = expected - stats->packets_received;
1567 lost = CLAMP (lost, -0x800000, 0x7fffff);
1569 expected_interval = expected - stats->prev_expected;
1570 stats->prev_expected = expected;
1571 received_interval = stats->packets_received - stats->prev_received;
1572 stats->prev_received = stats->packets_received;
1574 lost_interval = expected_interval - received_interval;
1576 if (expected_interval == 0 || lost_interval <= 0)
1579 fraction = (lost_interval << 8) / expected_interval;
1581 GST_DEBUG ("add RR for SSRC %08x", src->ssrc);
1582 /* we scaled the jitter up for additional precision */
1583 GST_DEBUG ("fraction %" G_GUINT32_FORMAT ", lost %" G_GINT64_FORMAT
1584 ", extseq %" G_GUINT64_FORMAT ", jitter %d", fraction, lost,
1585 extended_max, stats->jitter >> 4);
1587 if (rtp_source_get_last_sr (src, &sr_time, &ntptime, NULL, NULL, NULL)) {
1590 /* LSR is middle 32 bits of the last ntptime */
1591 LSR = (ntptime >> 16) & 0xffffffff;
1592 diff = time - sr_time;
1593 GST_DEBUG ("last SR time diff %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
1594 /* DLSR, delay since last SR is expressed in 1/65536 second units */
1595 DLSR = gst_util_uint64_scale_int (diff, 65536, GST_SECOND);
1597 /* No valid SR received, LSR/DLSR are set to 0 then */
1598 GST_DEBUG ("no valid SR received");
1602 GST_DEBUG ("LSR %04x:%04x, DLSR %04x:%04x", LSR >> 16, LSR & 0xffff,
1603 DLSR >> 16, DLSR & 0xffff);
1606 *fractionlost = fraction;
1608 *packetslost = lost;
1610 *exthighestseq = extended_max;
1612 *jitter = stats->jitter >> 4;
1622 * rtp_source_get_last_sr:
1623 * @src: an #RTPSource
1624 * @time: time of packet arrival
1625 * @ntptime: the NTP time (in NTP Timestamp Format, 32.32 fixed point)
1626 * @rtptime: the RTP time (in clock rate units)
1627 * @packet_count: the packet count
1628 * @octet_count: the octet count
1630 * Get the values of the last sender report as set with rtp_source_process_sr().
1632 * Returns: %TRUE if there was a valid SR report.
1635 rtp_source_get_last_sr (RTPSource * src, GstClockTime * time, guint64 * ntptime,
1636 guint32 * rtptime, guint32 * packet_count, guint32 * octet_count)
1638 RTPSenderReport *curr;
1640 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1642 curr = &src->stats.sr[src->stats.curr_sr];
1643 if (!curr->is_valid)
1647 *ntptime = curr->ntptime;
1649 *rtptime = curr->rtptime;
1651 *packet_count = curr->packet_count;
1653 *octet_count = curr->octet_count;
1661 * rtp_source_get_last_rb:
1662 * @src: an #RTPSource
1663 * @fractionlost: fraction lost since last SR/RR
1664 * @packetslost: the cumulative number of packets lost
1665 * @exthighestseq: the extended last sequence number received
1666 * @jitter: the interarrival jitter (in clock rate units)
1667 * @lsr: the time of the last SR packet on this source
1668 * (in NTP Short Format, 16.16 fixed point)
1669 * @dlsr: the delay since the last SR packet
1670 * (in NTP Short Format, 16.16 fixed point)
1671 * @round_trip: the round-trip time
1672 * (in NTP Short Format, 16.16 fixed point)
1674 * Get the values of the last RB report set with rtp_source_process_rb().
1676 * Returns: %TRUE if there was a valid SB report.
1679 rtp_source_get_last_rb (RTPSource * src, guint8 * fractionlost,
1680 gint32 * packetslost, guint32 * exthighestseq, guint32 * jitter,
1681 guint32 * lsr, guint32 * dlsr, guint32 * round_trip)
1683 RTPReceiverReport *curr;
1685 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1687 curr = &src->stats.rr[src->stats.curr_rr];
1688 if (!curr->is_valid)
1692 *fractionlost = curr->fractionlost;
1694 *packetslost = curr->packetslost;
1696 *exthighestseq = curr->exthighestseq;
1698 *jitter = curr->jitter;
1704 *round_trip = curr->round_trip;
1710 find_conflicting_address (GList * conflicting_addresses,
1711 GSocketAddress * address, GstClockTime time)
1715 for (item = conflicting_addresses; item; item = g_list_next (item)) {
1716 RTPConflictingAddress *known_conflict = item->data;
1718 if (__g_socket_address_equal (address, known_conflict->address)) {
1719 known_conflict->time = time;
1728 add_conflicting_address (GList * conflicting_addresses,
1729 GSocketAddress * address, GstClockTime time)
1731 RTPConflictingAddress *new_conflict;
1733 new_conflict = g_slice_new (RTPConflictingAddress);
1735 new_conflict->address = G_SOCKET_ADDRESS (g_object_ref (address));
1736 new_conflict->time = time;
1738 return g_list_prepend (conflicting_addresses, new_conflict);
1742 timeout_conflicting_addresses (GList * conflicting_addresses,
1743 GstClockTime current_time)
1746 /* "a relatively long time" -- RFC 3550 section 8.2 */
1747 const GstClockTime collision_timeout =
1748 RTP_STATS_MIN_INTERVAL * GST_SECOND * 10;
1750 item = g_list_first (conflicting_addresses);
1752 RTPConflictingAddress *known_conflict = item->data;
1753 GList *next_item = g_list_next (item);
1755 if (known_conflict->time < current_time - collision_timeout) {
1758 conflicting_addresses = g_list_delete_link (conflicting_addresses, item);
1759 buf = __g_socket_address_to_string (known_conflict->address);
1760 GST_DEBUG ("collision %p timed out: %s", known_conflict, buf);
1762 rtp_conflicting_address_free (known_conflict);
1767 return conflicting_addresses;
1771 * rtp_source_find_conflicting_address:
1772 * @src: The source the packet came in
1773 * @address: address to check for
1774 * @time: The time when the packet that is possibly in conflict arrived
1776 * Checks if an address which has a conflict is already known. If it is
1777 * a known conflict, remember the time
1779 * Returns: TRUE if it was a known conflict, FALSE otherwise
1782 rtp_source_find_conflicting_address (RTPSource * src, GSocketAddress * address,
1785 return find_conflicting_address (src->conflicting_addresses, address, time);
1789 * rtp_source_add_conflicting_address:
1790 * @src: The source the packet came in
1791 * @address: address to remember
1792 * @time: The time when the packet that is in conflict arrived
1794 * Adds a new conflict address
1797 rtp_source_add_conflicting_address (RTPSource * src,
1798 GSocketAddress * address, GstClockTime time)
1800 src->conflicting_addresses =
1801 add_conflicting_address (src->conflicting_addresses, address, time);
1805 * rtp_source_timeout:
1806 * @src: The #RTPSource
1807 * @current_time: The current time
1808 * @feedback_retention_window: The running time before which retained feedback
1809 * packets have to be discarded
1811 * This is processed on each RTCP interval. It times out old collisions.
1812 * It also times out old retained feedback packets
1815 rtp_source_timeout (RTPSource * src, GstClockTime current_time,
1816 GstClockTime feedback_retention_window)
1820 src->conflicting_addresses =
1821 timeout_conflicting_addresses (src->conflicting_addresses, current_time);
1823 /* Time out AVPF packets that are older than the desired length */
1824 while ((pkt = g_queue_peek_tail (src->retained_feedback)) &&
1825 GST_BUFFER_PTS (pkt) < feedback_retention_window)
1826 gst_buffer_unref (g_queue_pop_tail (src->retained_feedback));
1830 compare_buffers (gconstpointer a, gconstpointer b, gpointer user_data)
1832 const GstBuffer *bufa = a;
1833 const GstBuffer *bufb = b;
1835 return GST_BUFFER_PTS (bufa) - GST_BUFFER_PTS (bufb);
1839 rtp_source_retain_rtcp_packet (RTPSource * src, GstRTCPPacket * packet,
1840 GstClockTime running_time)
1844 buffer = gst_buffer_copy_region (packet->rtcp->buffer, GST_BUFFER_COPY_MEMORY,
1845 packet->offset, (gst_rtcp_packet_get_length (packet) + 1) * 4);
1847 GST_BUFFER_PTS (buffer) = running_time;
1849 g_queue_insert_sorted (src->retained_feedback, buffer, compare_buffers, NULL);
1853 rtp_source_has_retained (RTPSource * src, GCompareFunc func, gconstpointer data)
1855 if (g_queue_find_custom (src->retained_feedback, data, func))
1862 * rtp_source_register_nack:
1863 * @src: The #RTPSource
1866 * Register that @seqnum has not been received from @src.
1869 rtp_source_register_nack (RTPSource * src, guint16 seqnum)
1872 guint32 dword = seqnum << 16;
1875 len = src->nacks->len;
1876 for (i = 0; i < len; i++) {
1880 tdword = g_array_index (src->nacks, guint32, i);
1881 tseq = tdword >> 16;
1883 diff = gst_rtp_buffer_compare_seqnum (tseq, seqnum);
1887 /* we already have this seqnum */
1890 /* it comes before the recorded seqnum, FIXME, we could merge it
1891 * if not to far away */
1893 GST_DEBUG ("insert NACK #%u at %u", seqnum, i);
1894 g_array_insert_val (src->nacks, i, dword);
1895 } else if (diff < 16) {
1896 /* we can merge it */
1897 dword = g_array_index (src->nacks, guint32, i);
1898 dword |= 1 << (diff - 1);
1899 GST_DEBUG ("merge NACK #%u at %u with NACK #%u -> 0x%08x", seqnum, i,
1900 dword >> 16, dword);
1901 g_array_index (src->nacks, guint32, i) = dword;
1903 GST_DEBUG ("append NACK #%u", seqnum);
1904 g_array_append_val (src->nacks, dword);
1906 src->send_nack = TRUE;
1910 * rtp_source_get_nacks:
1911 * @src: The #RTPSource
1912 * @n_nacks: result number of nacks
1914 * Get the registered NACKS since the last rtp_source_clear_nacks().
1916 * Returns: an array of @n_nacks seqnum values.
1919 rtp_source_get_nacks (RTPSource * src, guint * n_nacks)
1922 *n_nacks = src->nacks->len;
1924 return (guint32 *) src->nacks->data;
1928 rtp_source_clear_nacks (RTPSource * src)
1930 g_array_set_size (src->nacks, 0);
1931 src->send_nack = FALSE;