2 * Copyright (C) <2007> Wim Taymans <wim@fluendo.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 #include <gst/rtp/gstrtpbuffer.h>
22 #include <gst/rtp/gstrtcpbuffer.h>
24 #include "rtpsource.h"
26 GST_DEBUG_CATEGORY_STATIC (rtp_source_debug);
27 #define GST_CAT_DEFAULT rtp_source_debug
29 #define RTP_MAX_PROBATION_LEN 32
31 /* signals and args */
42 /* GObject vmethods */
43 static void rtp_source_finalize (GObject * object);
45 /* static guint rtp_source_signals[LAST_SIGNAL] = { 0 }; */
47 G_DEFINE_TYPE (RTPSource, rtp_source, G_TYPE_OBJECT);
50 rtp_source_class_init (RTPSourceClass * klass)
52 GObjectClass *gobject_class;
54 gobject_class = (GObjectClass *) klass;
56 gobject_class->finalize = rtp_source_finalize;
58 GST_DEBUG_CATEGORY_INIT (rtp_source_debug, "rtpsource", 0, "RTP Source");
62 rtp_source_init (RTPSource * src)
64 /* sources are initialy on probation until we receive enough valid RTP
65 * packets or a valid RTCP packet */
66 src->validated = FALSE;
67 src->probation = RTP_DEFAULT_PROBATION;
71 src->packets = g_queue_new ();
73 src->stats.cycles = -1;
74 src->stats.jitter = 0;
75 src->stats.transit = -1;
76 src->stats.curr_sr = 0;
77 src->stats.curr_rr = 0;
81 rtp_source_finalize (GObject * object)
86 src = RTP_SOURCE_CAST (object);
88 while ((buffer = g_queue_pop_head (src->packets)))
89 gst_buffer_unref (buffer);
90 g_queue_free (src->packets);
92 G_OBJECT_CLASS (rtp_source_parent_class)->finalize (object);
99 * Create a #RTPSource with @ssrc.
101 * Returns: a new #RTPSource. Use g_object_unref() after usage.
104 rtp_source_new (guint32 ssrc)
108 src = g_object_new (RTP_TYPE_SOURCE, NULL);
115 * rtp_source_set_callbacks:
116 * @src: an #RTPSource
117 * @cb: callback functions
118 * @user_data: user data
120 * Set the callbacks for the source.
123 rtp_source_set_callbacks (RTPSource * src, RTPSourceCallbacks * cb,
126 g_return_if_fail (RTP_IS_SOURCE (src));
128 src->callbacks.push_rtp = cb->push_rtp;
129 src->callbacks.clock_rate = cb->clock_rate;
130 src->user_data = user_data;
134 * rtp_source_set_as_csrc:
135 * @src: an #RTPSource
137 * Configure @src as a CSRC, this will validate the RTpSource.
140 rtp_source_set_as_csrc (RTPSource * src)
142 g_return_if_fail (RTP_IS_SOURCE (src));
144 src->validated = TRUE;
149 * rtp_source_set_rtp_from:
150 * @src: an #RTPSource
151 * @address: the RTP address to set
153 * Set that @src is receiving RTP packets from @address. This is used for
154 * collistion checking.
157 rtp_source_set_rtp_from (RTPSource * src, GstNetAddress * address)
159 g_return_if_fail (RTP_IS_SOURCE (src));
161 src->have_rtp_from = TRUE;
162 memcpy (&src->rtp_from, address, sizeof (GstNetAddress));
166 * rtp_source_set_rtcp_from:
167 * @src: an #RTPSource
168 * @address: the RTCP address to set
170 * Set that @src is receiving RTCP packets from @address. This is used for
171 * collistion checking.
174 rtp_source_set_rtcp_from (RTPSource * src, GstNetAddress * address)
176 g_return_if_fail (RTP_IS_SOURCE (src));
178 src->have_rtcp_from = TRUE;
179 memcpy (&src->rtcp_from, address, sizeof (GstNetAddress));
183 push_packet (RTPSource * src, GstBuffer * buffer)
185 GstFlowReturn ret = GST_FLOW_OK;
187 /* push queued packets first if any */
188 while (!g_queue_is_empty (src->packets)) {
189 GstBuffer *buffer = GST_BUFFER_CAST (g_queue_pop_head (src->packets));
191 GST_DEBUG ("pushing queued packet");
192 if (src->callbacks.push_rtp)
193 src->callbacks.push_rtp (src, buffer, src->user_data);
195 gst_buffer_unref (buffer);
197 GST_DEBUG ("pushing new packet");
199 if (src->callbacks.push_rtp)
200 ret = src->callbacks.push_rtp (src, buffer, src->user_data);
202 gst_buffer_unref (buffer);
208 get_clock_rate (RTPSource * src, guint8 payload)
210 if (payload != src->payload) {
211 gint clock_rate = -1;
213 if (src->callbacks.clock_rate)
214 clock_rate = src->callbacks.clock_rate (src, payload, src->user_data);
216 GST_DEBUG ("new payload %d, got clock-rate %d", payload, clock_rate);
218 src->clock_rate = clock_rate;
219 src->payload = payload;
221 return src->clock_rate;
225 calculate_jitter (RTPSource * src, GstBuffer * buffer,
226 RTPArrivalStats * arrival)
228 GstClockTime current;
229 guint32 rtparrival, transit, rtptime;
234 /* get arrival time */
235 if ((current = arrival->time) == GST_CLOCK_TIME_NONE)
238 pt = gst_rtp_buffer_get_payload_type (buffer);
241 if ((clock_rate = get_clock_rate (src, pt)) == -1)
244 rtptime = gst_rtp_buffer_get_timestamp (buffer);
246 /* convert arrival time to RTP timestamp units */
247 rtparrival = gst_util_uint64_scale_int (current, clock_rate, GST_SECOND);
249 /* transit time is difference with RTP timestamp */
250 transit = rtparrival - rtptime;
252 /* get ABS diff with previous transit time */
253 if (src->stats.transit != -1) {
254 if (transit > src->stats.transit)
255 diff = transit - src->stats.transit;
257 diff = src->stats.transit - transit;
261 src->stats.transit = transit;
263 /* update jitter, the value we store is scaled up so we can keep precision. */
264 src->stats.jitter += diff - ((src->stats.jitter + 8) >> 4);
266 src->stats.prev_rtptime = src->stats.last_rtptime;
267 src->stats.last_rtptime = rtparrival;
269 GST_DEBUG ("rtparrival %u, rtptime %u, clock-rate %d, diff %d, jitter: %u",
270 rtparrival, rtptime, clock_rate, diff, src->stats.jitter);
277 GST_WARNING ("cannot get current time");
282 GST_WARNING ("cannot get clock-rate for pt %d", pt);
288 init_seq (RTPSource * src, guint16 seq)
290 src->stats.base_seq = seq;
291 src->stats.max_seq = seq;
292 src->stats.bad_seq = RTP_SEQ_MOD + 1; /* so seq == bad_seq is false */
293 src->stats.cycles = 0;
294 src->stats.packets_received = 0;
295 src->stats.octets_received = 0;
296 src->stats.bytes_received = 0;
297 src->stats.prev_received = 0;
298 src->stats.prev_expected = 0;
300 GST_DEBUG ("base_seq %d", seq);
304 * rtp_source_process_rtp:
305 * @src: an #RTPSource
306 * @buffer: an RTP buffer
308 * Let @src handle the incomming RTP @buffer.
310 * Returns: a #GstFlowReturn.
313 rtp_source_process_rtp (RTPSource * src, GstBuffer * buffer,
314 RTPArrivalStats * arrival)
316 GstFlowReturn result = GST_FLOW_OK;
317 guint16 seqnr, udelta;
318 RTPSourceStats *stats;
320 g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
321 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
325 seqnr = gst_rtp_buffer_get_seq (buffer);
327 if (stats->cycles == -1) {
328 GST_DEBUG ("received first buffer");
329 /* first time we heard of this source */
330 init_seq (src, seqnr);
331 src->stats.max_seq = seqnr - 1;
332 src->probation = RTP_DEFAULT_PROBATION;
335 udelta = seqnr - stats->max_seq;
337 /* if we are still on probation, check seqnum */
338 if (src->probation) {
341 expected = src->stats.max_seq + 1;
343 /* when in probation, we require consecutive seqnums */
344 if (seqnr == expected) {
345 /* expected packet */
346 GST_DEBUG ("probation: seqnr %d == expected %d", seqnr, expected);
348 src->stats.max_seq = seqnr;
349 if (src->probation == 0) {
350 GST_DEBUG ("probation done!", src->probation);
351 init_seq (src, seqnr);
355 GST_DEBUG ("probation %d: queue buffer", src->probation);
356 /* when still in probation, keep packets in a list. */
357 g_queue_push_tail (src->packets, buffer);
358 /* remove packets from queue if there are too many */
359 while (g_queue_get_length (src->packets) > RTP_MAX_PROBATION_LEN) {
360 q = g_queue_pop_head (src->packets);
361 gst_object_unref (q);
366 GST_DEBUG ("probation: seqnr %d != expected %d", seqnr, expected);
367 src->probation = RTP_DEFAULT_PROBATION;
368 src->stats.max_seq = seqnr;
371 } else if (udelta < RTP_MAX_DROPOUT) {
372 /* in order, with permissible gap */
373 if (seqnr < stats->max_seq) {
374 /* sequence number wrapped - count another 64K cycle. */
375 stats->cycles += RTP_SEQ_MOD;
377 stats->max_seq = seqnr;
378 } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
379 /* the sequence number made a very large jump */
380 if (seqnr == stats->bad_seq) {
381 /* two sequential packets -- assume that the other side
382 * restarted without telling us so just re-sync
383 * (i.e., pretend this was the first packet). */
384 init_seq (src, seqnr);
386 /* unacceptable jump */
387 stats->bad_seq = (seqnr + 1) & (RTP_SEQ_MOD - 1);
391 /* duplicate or reordered packet, will be filtered by jitterbuffer. */
394 src->stats.octets_received += arrival->payload_len;
395 src->stats.bytes_received += arrival->bytes;
396 src->stats.packets_received++;
397 /* the source that sent the packet must be a sender */
398 src->is_sender = TRUE;
399 src->validated = TRUE;
401 GST_DEBUG ("seq %d, PC: %" G_GUINT64_FORMAT ", OC: %" G_GUINT64_FORMAT,
402 seqnr, src->stats.packets_received, src->stats.octets_received);
404 /* calculate jitter */
405 calculate_jitter (src, buffer, arrival);
407 /* we're ready to push the RTP packet now */
408 result = push_packet (src, buffer);
416 GST_WARNING ("unacceptable seqnum received");
422 * rtp_source_process_bye:
423 * @src: an #RTPSource
424 * @reason: the reason for leaving
426 * Notify @src that a BYE packet has been received. This will make the source
430 rtp_source_process_bye (RTPSource * src, const gchar * reason)
432 g_return_if_fail (RTP_IS_SOURCE (src));
434 GST_DEBUG ("marking SSRC %08x as BYE, reason: %s", src->ssrc,
435 GST_STR_NULL (reason));
437 /* copy the reason and mark as received_bye */
438 g_free (src->bye_reason);
439 src->bye_reason = g_strdup (reason);
440 src->received_bye = TRUE;
444 * rtp_source_send_rtp:
445 * @src: an #RTPSource
446 * @buffer: an RTP buffer
448 * Send an RTP @buffer originating from @src. This will make @src a sender.
450 * Returns: a #GstFlowReturn.
453 rtp_source_send_rtp (RTPSource * src, GstBuffer * buffer)
455 GstFlowReturn result = GST_FLOW_OK;
458 g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
459 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
461 len = gst_rtp_buffer_get_payload_len (buffer);
463 /* we are a sender now */
464 src->is_sender = TRUE;
466 /* update stats for the SR */
467 src->stats.packets_sent++;
468 src->stats.octets_sent += len;
472 if (src->callbacks.push_rtp) {
473 GST_DEBUG ("pushing RTP packet %u", src->stats.packets_sent);
474 result = src->callbacks.push_rtp (src, buffer, src->user_data);
476 GST_DEBUG ("no callback installed");
477 gst_buffer_unref (buffer);
484 * rtp_source_process_sr:
485 * @src: an #RTPSource
486 * @ntptime: the NTP time
487 * @rtptime: the RTP time
488 * @packet_count: the packet count
489 * @octet_count: the octect count
490 * @time: time of packet arrival
492 * Update the sender report in @src.
495 rtp_source_process_sr (RTPSource * src, guint64 ntptime, guint32 rtptime,
496 guint32 packet_count, guint32 octet_count, GstClockTime time)
498 RTPSenderReport *curr;
501 g_return_if_fail (RTP_IS_SOURCE (src));
503 GST_DEBUG ("got SR packet: SSRC %08x, NTP %08x:%08x, RTP %u, PC %u, OC %u",
504 src->ssrc, ntptime >> 32, ntptime & 0xffffffff, rtptime, packet_count,
507 curridx = src->stats.curr_sr ^ 1;
508 curr = &src->stats.sr[curridx];
510 /* this is a sender now */
511 src->is_sender = TRUE;
514 curr->is_valid = TRUE;
515 curr->ntptime = ntptime;
516 curr->rtptime = rtptime;
517 curr->packet_count = packet_count;
518 curr->octet_count = octet_count;
522 src->stats.curr_sr = curridx;
526 * rtp_source_process_rb:
527 * @src: an #RTPSource
528 * @fractionlost: fraction lost since last SR/RR
529 * @packetslost: the cumululative number of packets lost
530 * @exthighestseq: the extended last sequence number received
531 * @jitter: the interarrival jitter
532 * @lsr: the last SR packet from this source
533 * @dlsr: the delay since last SR packet
535 * Update the report block in @src.
538 rtp_source_process_rb (RTPSource * src, guint8 fractionlost, gint32 packetslost,
539 guint32 exthighestseq, guint32 jitter, guint32 lsr, guint32 dlsr)
541 RTPReceiverReport *curr;
544 g_return_if_fail (RTP_IS_SOURCE (src));
546 GST_DEBUG ("got RB packet %d: SSRC %08x, FL %u"
547 ", PL %u, HS %u, JITTER %u, LSR %08x, DLSR %08x", src->ssrc, fractionlost,
548 packetslost, exthighestseq, jitter, lsr, dlsr);
550 curridx = src->stats.curr_rr ^ 1;
551 curr = &src->stats.rr[curridx];
554 curr->is_valid = TRUE;
555 curr->fractionlost = fractionlost;
556 curr->packetslost = packetslost;
557 curr->exthighestseq = exthighestseq;
558 curr->jitter = jitter;
563 src->stats.curr_rr = curridx;
567 * rtp_source_get_last_sr:
568 * @src: an #RTPSource
569 * @ntptime: the NTP time
570 * @rtptime: the RTP time
571 * @packet_count: the packet count
572 * @octet_count: the octect count
573 * @time: time of packet arrival
575 * Get the values of the last sender report as set with rtp_source_process_sr().
577 * Returns: %TRUE if there was a valid SR report.
580 rtp_source_get_last_sr (RTPSource * src, guint64 * ntptime, guint32 * rtptime,
581 guint32 * packet_count, guint32 * octet_count, GstClockTime * time)
583 RTPSenderReport *curr;
585 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
587 curr = &src->stats.sr[src->stats.curr_sr];
592 *ntptime = curr->ntptime;
594 *rtptime = curr->rtptime;
596 *packet_count = curr->packet_count;
598 *octet_count = curr->octet_count;
606 * rtp_source_get_last_rb:
607 * @src: an #RTPSource
608 * @fractionlost: fraction lost since last SR/RR
609 * @packetslost: the cumululative number of packets lost
610 * @exthighestseq: the extended last sequence number received
611 * @jitter: the interarrival jitter
612 * @lsr: the last SR packet from this source
613 * @dlsr: the delay since last SR packet
615 * Get the values of the last RB report set with rtp_source_process_rb().
617 * Returns: %TRUE if there was a valid SB report.
620 rtp_source_get_last_rb (RTPSource * src, guint8 * fractionlost,
621 gint32 * packetslost, guint32 * exthighestseq, guint32 * jitter,
622 guint32 * lsr, guint32 * dlsr)
624 RTPReceiverReport *curr;
626 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
628 curr = &src->stats.rr[src->stats.curr_rr];
633 *fractionlost = curr->fractionlost;
635 *packetslost = curr->packetslost;
637 *exthighestseq = curr->exthighestseq;
639 *jitter = curr->jitter;