2
* Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.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 */
37
#define DEFAULT_SSRC 0
38
#define DEFAULT_IS_CSRC FALSE
39
#define DEFAULT_IS_VALIDATED FALSE
40
#define DEFAULT_IS_SENDER FALSE
41
#define DEFAULT_SDES NULL
55
/* GObject vmethods */
56
static void rtp_source_finalize (GObject * object);
57
static void rtp_source_set_property (GObject * object, guint prop_id,
58
const GValue * value, GParamSpec * pspec);
59
static void rtp_source_get_property (GObject * object, guint prop_id,
60
GValue * value, GParamSpec * pspec);
62
/* static guint rtp_source_signals[LAST_SIGNAL] = { 0 }; */
64
G_DEFINE_TYPE (RTPSource, rtp_source, G_TYPE_OBJECT);
67
rtp_source_class_init (RTPSourceClass * klass)
69
GObjectClass *gobject_class;
71
gobject_class = (GObjectClass *) klass;
73
gobject_class->finalize = rtp_source_finalize;
75
gobject_class->set_property = rtp_source_set_property;
76
gobject_class->get_property = rtp_source_get_property;
78
g_object_class_install_property (gobject_class, PROP_SSRC,
79
g_param_spec_uint ("ssrc", "SSRC",
80
"The SSRC of this source", 0, G_MAXUINT, DEFAULT_SSRC,
81
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
83
g_object_class_install_property (gobject_class, PROP_IS_CSRC,
84
g_param_spec_boolean ("is-csrc", "Is CSRC",
85
"If this SSRC is acting as a contributing source",
86
DEFAULT_IS_CSRC, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
88
g_object_class_install_property (gobject_class, PROP_IS_VALIDATED,
89
g_param_spec_boolean ("is-validated", "Is Validated",
90
"If this SSRC is validated", DEFAULT_IS_VALIDATED,
91
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
93
g_object_class_install_property (gobject_class, PROP_IS_SENDER,
94
g_param_spec_boolean ("is-sender", "Is Sender",
95
"If this SSRC is a sender", DEFAULT_IS_SENDER,
96
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
101
* The current SDES items of the source. Returns a structure with the
104
* 'cname' G_TYPE_STRING : The canonical name
105
* 'name' G_TYPE_STRING : The user name
106
* 'email' G_TYPE_STRING : The user's electronic mail address
107
* 'phone' G_TYPE_STRING : The user's phone number
108
* 'location' G_TYPE_STRING : The geographic user location
109
* 'tool' G_TYPE_STRING : The name of application or tool
110
* 'note' G_TYPE_STRING : A notice about the source
112
g_object_class_install_property (gobject_class, PROP_SDES,
113
g_param_spec_boxed ("sdes", "SDES",
114
"The SDES information for this source",
115
GST_TYPE_STRUCTURE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
120
* The statistics of the source. This property returns a GstStructure with
121
* name application/x-rtp-source-stats with the following fields:
124
g_object_class_install_property (gobject_class, PROP_STATS,
125
g_param_spec_boxed ("stats", "Stats",
126
"The stats of this source", GST_TYPE_STRUCTURE,
127
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
129
GST_DEBUG_CATEGORY_INIT (rtp_source_debug, "rtpsource", 0, "RTP Source");
134
* @src: an #RTPSource
136
* Reset the stats of @src.
139
rtp_source_reset (RTPSource * src)
141
src->received_bye = FALSE;
143
src->stats.cycles = -1;
144
src->stats.jitter = 0;
145
src->stats.transit = -1;
146
src->stats.curr_sr = 0;
147
src->stats.curr_rr = 0;
151
rtp_source_init (RTPSource * src)
153
/* sources are initialy on probation until we receive enough valid RTP
154
* packets or a valid RTCP packet */
155
src->validated = FALSE;
156
src->internal = FALSE;
157
src->probation = RTP_DEFAULT_PROBATION;
160
src->clock_rate = -1;
161
src->packets = g_queue_new ();
162
src->seqnum_base = -1;
163
src->last_rtptime = -1;
165
rtp_source_reset (src);
169
rtp_source_finalize (GObject * object)
175
src = RTP_SOURCE_CAST (object);
177
while ((buffer = g_queue_pop_head (src->packets)))
178
gst_buffer_unref (buffer);
179
g_queue_free (src->packets);
181
for (i = 0; i < 9; i++)
182
g_free (src->sdes[i]);
184
g_free (src->bye_reason);
186
gst_caps_replace (&src->caps, NULL);
188
G_OBJECT_CLASS (rtp_source_parent_class)->finalize (object);
191
static GstStructure *
192
rtp_source_create_stats (RTPSource * src)
195
gboolean is_sender = src->is_sender;
196
gboolean internal = src->internal;
198
/* common data for all types of sources */
199
s = gst_structure_new ("application/x-rtp-source-stats",
200
"ssrc", G_TYPE_UINT, (guint) src->ssrc,
201
"internal", G_TYPE_BOOLEAN, internal,
202
"validated", G_TYPE_BOOLEAN, src->validated,
203
"received-bye", G_TYPE_BOOLEAN, src->received_bye,
204
"is-csrc", G_TYPE_BOOLEAN, src->is_csrc,
205
"is-sender", G_TYPE_BOOLEAN, is_sender, NULL);
208
/* our internal source */
210
/* if we are sending, report about how much we sent, other sources will
211
* have a RB with info on reception. */
212
gst_structure_set (s,
213
"octets-sent", G_TYPE_UINT64, src->stats.octets_sent,
214
"packets-sent", G_TYPE_UINT64, src->stats.packets_sent,
215
"bitrate", G_TYPE_UINT64, src->bitrate, NULL);
217
/* if we are not sending we have nothing more to report */
221
guint8 fractionlost = 0;
222
gint32 packetslost = 0;
223
guint32 exthighestseq = 0;
227
guint32 round_trip = 0;
232
GstClockTime time = 0;
235
guint32 packet_count = 0;
236
guint32 octet_count = 0;
238
/* this source is sending to us, get the last SR. */
239
have_sr = rtp_source_get_last_sr (src, &time, &ntptime, &rtptime,
240
&packet_count, &octet_count);
241
gst_structure_set (s,
242
"octets-received", G_TYPE_UINT64, src->stats.octets_received,
243
"packets-received", G_TYPE_UINT64, src->stats.packets_received,
244
"have-sr", G_TYPE_BOOLEAN, have_sr,
245
"sr-ntptime", G_TYPE_UINT64, ntptime,
246
"sr-rtptime", G_TYPE_UINT, (guint) rtptime,
247
"sr-octet-count", G_TYPE_UINT, (guint) octet_count,
248
"sr-packet-count", G_TYPE_UINT, (guint) packet_count, NULL);
250
/* we might be sending to this SSRC so we report about how it is
251
* receiving our data */
252
have_rb = rtp_source_get_last_rb (src, &fractionlost, &packetslost,
253
&exthighestseq, &jitter, &lsr, &dlsr, &round_trip);
255
gst_structure_set (s,
256
"have-rb", G_TYPE_BOOLEAN, have_rb,
257
"rb-fractionlost", G_TYPE_UINT, (guint) fractionlost,
258
"rb-packetslost", G_TYPE_INT, (gint) packetslost,
259
"rb-exthighestseq", G_TYPE_UINT, (guint) exthighestseq,
260
"rb-jitter", G_TYPE_UINT, (guint) jitter,
261
"rb-lsr", G_TYPE_UINT, (guint) lsr,
262
"rb-dlsr", G_TYPE_UINT, (guint) dlsr,
263
"rb-round-trip", G_TYPE_UINT, (guint) round_trip, NULL);
269
static GstStructure *
270
rtp_source_create_sdes (RTPSource * src)
275
s = gst_structure_new ("application/x-rtp-source-sdes", NULL);
277
if ((str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_CNAME))) {
278
gst_structure_set (s, "cname", G_TYPE_STRING, str, NULL);
281
if ((str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_NAME))) {
282
gst_structure_set (s, "name", G_TYPE_STRING, str, NULL);
285
if ((str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_EMAIL))) {
286
gst_structure_set (s, "email", G_TYPE_STRING, str, NULL);
289
if ((str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_PHONE))) {
290
gst_structure_set (s, "phone", G_TYPE_STRING, str, NULL);
293
if ((str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_LOC))) {
294
gst_structure_set (s, "location", G_TYPE_STRING, str, NULL);
297
if ((str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_TOOL))) {
298
gst_structure_set (s, "tool", G_TYPE_STRING, str, NULL);
301
if ((str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_NOTE))) {
302
gst_structure_set (s, "note", G_TYPE_STRING, str, NULL);
309
rtp_source_set_property (GObject * object, guint prop_id,
310
const GValue * value, GParamSpec * pspec)
314
src = RTP_SOURCE (object);
318
src->ssrc = g_value_get_uint (value);
321
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
327
rtp_source_get_property (GObject * object, guint prop_id,
328
GValue * value, GParamSpec * pspec)
332
src = RTP_SOURCE (object);
336
g_value_set_uint (value, rtp_source_get_ssrc (src));
339
g_value_set_boolean (value, rtp_source_is_as_csrc (src));
341
case PROP_IS_VALIDATED:
342
g_value_set_boolean (value, rtp_source_is_validated (src));
345
g_value_set_boolean (value, rtp_source_is_sender (src));
348
g_value_take_boxed (value, rtp_source_create_sdes (src));
351
g_value_take_boxed (value, rtp_source_create_stats (src));
354
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
363
* Create a #RTPSource with @ssrc.
365
* Returns: a new #RTPSource. Use g_object_unref() after usage.
368
rtp_source_new (guint32 ssrc)
372
src = g_object_new (RTP_TYPE_SOURCE, NULL);
379
* rtp_source_set_callbacks:
380
* @src: an #RTPSource
381
* @cb: callback functions
382
* @user_data: user data
384
* Set the callbacks for the source.
387
rtp_source_set_callbacks (RTPSource * src, RTPSourceCallbacks * cb,
390
g_return_if_fail (RTP_IS_SOURCE (src));
392
src->callbacks.push_rtp = cb->push_rtp;
393
src->callbacks.clock_rate = cb->clock_rate;
394
src->user_data = user_data;
398
* rtp_source_get_ssrc:
399
* @src: an #RTPSource
401
* Get the SSRC of @source.
403
* Returns: the SSRC of src.
406
rtp_source_get_ssrc (RTPSource * src)
410
g_return_val_if_fail (RTP_IS_SOURCE (src), 0);
418
* rtp_source_set_as_csrc:
419
* @src: an #RTPSource
421
* Configure @src as a CSRC, this will also validate @src.
424
rtp_source_set_as_csrc (RTPSource * src)
426
g_return_if_fail (RTP_IS_SOURCE (src));
428
src->validated = TRUE;
433
* rtp_source_is_as_csrc:
434
* @src: an #RTPSource
436
* Check if @src is a contributing source.
438
* Returns: %TRUE if @src is acting as a contributing source.
441
rtp_source_is_as_csrc (RTPSource * src)
445
g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
447
result = src->is_csrc;
453
* rtp_source_is_active:
454
* @src: an #RTPSource
456
* Check if @src is an active source. A source is active if it has been
457
* validated and has not yet received a BYE packet
459
* Returns: %TRUE if @src is an qactive source.
462
rtp_source_is_active (RTPSource * src)
466
g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
468
result = RTP_SOURCE_IS_ACTIVE (src);
474
* rtp_source_is_validated:
475
* @src: an #RTPSource
477
* Check if @src is a validated source.
479
* Returns: %TRUE if @src is a validated source.
482
rtp_source_is_validated (RTPSource * src)
486
g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
488
result = src->validated;
494
* rtp_source_is_sender:
495
* @src: an #RTPSource
497
* Check if @src is a sending source.
499
* Returns: %TRUE if @src is a sending source.
502
rtp_source_is_sender (RTPSource * src)
506
g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
508
result = RTP_SOURCE_IS_SENDER (src);
514
* rtp_source_received_bye:
515
* @src: an #RTPSource
517
* Check if @src has receoved a BYE packet.
519
* Returns: %TRUE if @src has received a BYE packet.
522
rtp_source_received_bye (RTPSource * src)
526
g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
528
result = src->received_bye;
535
* rtp_source_get_bye_reason:
536
* @src: an #RTPSource
538
* Get the BYE reason for @src. Check if the source receoved a BYE message first
539
* with rtp_source_received_bye().
541
* Returns: The BYE reason or NULL when no reason was given or the source did
542
* not receive a BYE message yet. g_fee() after usage.
545
rtp_source_get_bye_reason (RTPSource * src)
549
g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
551
result = g_strdup (src->bye_reason);
557
* rtp_source_update_caps:
558
* @src: an #RTPSource
561
* Parse @caps and store all relevant information in @source.
564
rtp_source_update_caps (RTPSource * src, GstCaps * caps)
570
/* nothing changed, return */
571
if (src->caps == caps)
574
s = gst_caps_get_structure (caps, 0);
576
if (gst_structure_get_int (s, "payload", &ival))
580
GST_DEBUG ("got payload %d", src->payload);
582
if (gst_structure_get_int (s, "clock-rate", &ival))
583
src->clock_rate = ival;
585
src->clock_rate = -1;
587
GST_DEBUG ("got clock-rate %d", src->clock_rate);
589
if (gst_structure_get_uint (s, "seqnum-base", &val))
590
src->seqnum_base = val;
592
src->seqnum_base = -1;
594
GST_DEBUG ("got seqnum-base %" G_GINT32_FORMAT, src->seqnum_base);
596
gst_caps_replace (&src->caps, caps);
600
* rtp_source_set_sdes:
601
* @src: an #RTPSource
602
* @type: the type of the SDES item
603
* @data: the SDES data
604
* @len: the SDES length
606
* Store an SDES item of @type in @src.
608
* Returns: %FALSE if the SDES item was unchanged or @type is unknown.
611
rtp_source_set_sdes (RTPSource * src, GstRTCPSDESType type,
612
const guint8 * data, guint len)
616
g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
618
if (type < 0 || type > GST_RTCP_SDES_PRIV)
621
old = src->sdes[type];
623
/* lengths are the same, check if the data is the same */
624
if ((src->sdes_len[type] == len))
625
if (data != NULL && old != NULL && (memcmp (old, data, len) == 0))
628
/* NULL data, make sure we store 0 length or if no length is given,
633
g_free (src->sdes[type]);
634
src->sdes[type] = g_memdup (data, len);
635
src->sdes_len[type] = len;
641
* rtp_source_set_sdes_string:
642
* @src: an #RTPSource
643
* @type: the type of the SDES item
644
* @data: the SDES data
646
* Store an SDES item of @type in @src. This function is similar to
647
* rtp_source_set_sdes() but takes a null-terminated string for convenience.
649
* Returns: %FALSE if the SDES item was unchanged or @type is unknown.
652
rtp_source_set_sdes_string (RTPSource * src, GstRTCPSDESType type,
663
result = rtp_source_set_sdes (src, type, (guint8 *) data, len);
669
* rtp_source_get_sdes:
670
* @src: an #RTPSource
671
* @type: the type of the SDES item
672
* @data: location to store the SDES data or NULL
673
* @len: location to store the SDES length or NULL
675
* Get the SDES item of @type from @src. Note that @data does not always point
676
* to a null-terminated string, use rtp_source_get_sdes_string() to retrieve a
677
* null-terminated string instead.
679
* @data remains valid until the next call to rtp_source_set_sdes().
681
* Returns: %TRUE if @type was valid and @data and @len contain valid
682
* data. @data can be NULL when the item was unset.
685
rtp_source_get_sdes (RTPSource * src, GstRTCPSDESType type, guint8 ** data,
688
g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
690
if (type < 0 || type > GST_RTCP_SDES_PRIV)
694
*data = src->sdes[type];
696
*len = src->sdes_len[type];
702
* rtp_source_get_sdes_string:
703
* @src: an #RTPSource
704
* @type: the type of the SDES item
706
* Get the SDES item of @type from @src.
708
* Returns: a null-terminated copy of the SDES item or NULL when @type was not
709
* valid or the SDES item was unset. g_free() after usage.
712
rtp_source_get_sdes_string (RTPSource * src, GstRTCPSDESType type)
716
g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
718
if (type < 0 || type > GST_RTCP_SDES_PRIV)
721
result = g_strndup ((const gchar *) src->sdes[type], src->sdes_len[type]);
727
* rtp_source_set_rtp_from:
728
* @src: an #RTPSource
729
* @address: the RTP address to set
731
* Set that @src is receiving RTP packets from @address. This is used for
732
* collistion checking.
735
rtp_source_set_rtp_from (RTPSource * src, GstNetAddress * address)
737
g_return_if_fail (RTP_IS_SOURCE (src));
739
src->have_rtp_from = TRUE;
740
memcpy (&src->rtp_from, address, sizeof (GstNetAddress));
744
* rtp_source_set_rtcp_from:
745
* @src: an #RTPSource
746
* @address: the RTCP address to set
748
* Set that @src is receiving RTCP packets from @address. This is used for
749
* collistion checking.
752
rtp_source_set_rtcp_from (RTPSource * src, GstNetAddress * address)
754
g_return_if_fail (RTP_IS_SOURCE (src));
756
src->have_rtcp_from = TRUE;
757
memcpy (&src->rtcp_from, address, sizeof (GstNetAddress));
761
push_packet (RTPSource * src, GstBuffer * buffer)
763
GstFlowReturn ret = GST_FLOW_OK;
765
/* push queued packets first if any */
766
while (!g_queue_is_empty (src->packets)) {
767
GstBuffer *buffer = GST_BUFFER_CAST (g_queue_pop_head (src->packets));
769
GST_LOG ("pushing queued packet");
770
if (src->callbacks.push_rtp)
771
src->callbacks.push_rtp (src, buffer, src->user_data);
773
gst_buffer_unref (buffer);
775
GST_LOG ("pushing new packet");
777
if (src->callbacks.push_rtp)
778
ret = src->callbacks.push_rtp (src, buffer, src->user_data);
780
gst_buffer_unref (buffer);
786
get_clock_rate (RTPSource * src, guint8 payload)
788
if (src->payload == -1) {
789
/* first payload received, nothing was in the caps, lock on to this payload */
790
src->payload = payload;
791
GST_DEBUG ("first payload %d", payload);
792
} else if (payload != src->payload) {
793
/* we have a different payload than before, reset the clock-rate */
794
GST_DEBUG ("new payload %d", payload);
795
src->payload = payload;
796
src->clock_rate = -1;
797
src->stats.transit = -1;
800
if (src->clock_rate == -1) {
801
gint clock_rate = -1;
803
if (src->callbacks.clock_rate)
804
clock_rate = src->callbacks.clock_rate (src, payload, src->user_data);
806
GST_DEBUG ("got clock-rate %d", clock_rate);
808
src->clock_rate = clock_rate;
810
return src->clock_rate;
813
/* Jitter is the variation in the delay of received packets in a flow. It is
814
* measured by comparing the interval when RTP packets were sent to the interval
815
* at which they were received. For instance, if packet #1 and packet #2 leave
816
* 50 milliseconds apart and arrive 60 milliseconds apart, then the jitter is 10
819
calculate_jitter (RTPSource * src, GstBuffer * buffer,
820
RTPArrivalStats * arrival)
823
guint32 rtparrival, transit, rtptime;
828
/* get arrival time */
829
if ((ntpnstime = arrival->ntpnstime) == GST_CLOCK_TIME_NONE)
832
pt = gst_rtp_buffer_get_payload_type (buffer);
834
GST_LOG ("SSRC %08x got payload %d", src->ssrc, pt);
837
if ((clock_rate = get_clock_rate (src, pt)) == -1)
840
rtptime = gst_rtp_buffer_get_timestamp (buffer);
842
/* convert arrival time to RTP timestamp units, truncate to 32 bits, we don't
843
* care about the absolute value, just the difference. */
844
rtparrival = gst_util_uint64_scale_int (ntpnstime, clock_rate, GST_SECOND);
846
/* transit time is difference with RTP timestamp */
847
transit = rtparrival - rtptime;
849
/* get ABS diff with previous transit time */
850
if (src->stats.transit != -1) {
851
if (transit > src->stats.transit)
852
diff = transit - src->stats.transit;
854
diff = src->stats.transit - transit;
858
src->stats.transit = transit;
860
/* update jitter, the value we store is scaled up so we can keep precision. */
861
src->stats.jitter += diff - ((src->stats.jitter + 8) >> 4);
863
src->stats.prev_rtptime = src->stats.last_rtptime;
864
src->stats.last_rtptime = rtparrival;
866
GST_LOG ("rtparrival %u, rtptime %u, clock-rate %d, diff %d, jitter: %f",
867
rtparrival, rtptime, clock_rate, diff, (src->stats.jitter) / 16.0);
874
GST_WARNING ("cannot get current time");
879
GST_WARNING ("cannot get clock-rate for pt %d", pt);
885
init_seq (RTPSource * src, guint16 seq)
887
src->stats.base_seq = seq;
888
src->stats.max_seq = seq;
889
src->stats.bad_seq = RTP_SEQ_MOD + 1; /* so seq == bad_seq is false */
890
src->stats.cycles = 0;
891
src->stats.packets_received = 0;
892
src->stats.octets_received = 0;
893
src->stats.bytes_received = 0;
894
src->stats.prev_received = 0;
895
src->stats.prev_expected = 0;
897
GST_DEBUG ("base_seq %d", seq);
901
* rtp_source_process_rtp:
902
* @src: an #RTPSource
903
* @buffer: an RTP buffer
905
* Let @src handle the incomming RTP @buffer.
907
* Returns: a #GstFlowReturn.
910
rtp_source_process_rtp (RTPSource * src, GstBuffer * buffer,
911
RTPArrivalStats * arrival)
913
GstFlowReturn result = GST_FLOW_OK;
914
guint16 seqnr, udelta;
915
RTPSourceStats *stats;
917
g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
918
g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
922
seqnr = gst_rtp_buffer_get_seq (buffer);
924
rtp_source_update_caps (src, GST_BUFFER_CAPS (buffer));
926
if (stats->cycles == -1) {
927
GST_DEBUG ("received first buffer");
928
/* first time we heard of this source */
929
init_seq (src, seqnr);
930
src->stats.max_seq = seqnr - 1;
931
src->probation = RTP_DEFAULT_PROBATION;
934
udelta = seqnr - stats->max_seq;
936
/* if we are still on probation, check seqnum */
937
if (src->probation) {
940
expected = src->stats.max_seq + 1;
942
/* when in probation, we require consecutive seqnums */
943
if (seqnr == expected) {
944
/* expected packet */
945
GST_DEBUG ("probation: seqnr %d == expected %d", seqnr, expected);
947
src->stats.max_seq = seqnr;
948
if (src->probation == 0) {
949
GST_DEBUG ("probation done!");
950
init_seq (src, seqnr);
954
GST_DEBUG ("probation %d: queue buffer", src->probation);
955
/* when still in probation, keep packets in a list. */
956
g_queue_push_tail (src->packets, buffer);
957
/* remove packets from queue if there are too many */
958
while (g_queue_get_length (src->packets) > RTP_MAX_PROBATION_LEN) {
959
q = g_queue_pop_head (src->packets);
960
gst_buffer_unref (q);
965
GST_DEBUG ("probation: seqnr %d != expected %d", seqnr, expected);
966
src->probation = RTP_DEFAULT_PROBATION;
967
src->stats.max_seq = seqnr;
970
} else if (udelta < RTP_MAX_DROPOUT) {
971
/* in order, with permissible gap */
972
if (seqnr < stats->max_seq) {
973
/* sequence number wrapped - count another 64K cycle. */
974
stats->cycles += RTP_SEQ_MOD;
976
stats->max_seq = seqnr;
977
} else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
978
/* the sequence number made a very large jump */
979
if (seqnr == stats->bad_seq) {
980
/* two sequential packets -- assume that the other side
981
* restarted without telling us so just re-sync
982
* (i.e., pretend this was the first packet). */
983
init_seq (src, seqnr);
985
/* unacceptable jump */
986
stats->bad_seq = (seqnr + 1) & (RTP_SEQ_MOD - 1);
990
/* duplicate or reordered packet, will be filtered by jitterbuffer. */
991
GST_WARNING ("duplicate or reordered packet");
994
src->stats.octets_received += arrival->payload_len;
995
src->stats.bytes_received += arrival->bytes;
996
src->stats.packets_received++;
997
/* the source that sent the packet must be a sender */
998
src->is_sender = TRUE;
999
src->validated = TRUE;
1001
GST_LOG ("seq %d, PC: %" G_GUINT64_FORMAT ", OC: %" G_GUINT64_FORMAT,
1002
seqnr, src->stats.packets_received, src->stats.octets_received);
1004
/* calculate jitter for the stats */
1005
calculate_jitter (src, buffer, arrival);
1007
/* we're ready to push the RTP packet now */
1008
result = push_packet (src, buffer);
1016
GST_WARNING ("unacceptable seqnum received");
1022
* rtp_source_process_bye:
1023
* @src: an #RTPSource
1024
* @reason: the reason for leaving
1026
* Notify @src that a BYE packet has been received. This will make the source
1030
rtp_source_process_bye (RTPSource * src, const gchar * reason)
1032
g_return_if_fail (RTP_IS_SOURCE (src));
1034
GST_DEBUG ("marking SSRC %08x as BYE, reason: %s", src->ssrc,
1035
GST_STR_NULL (reason));
1037
/* copy the reason and mark as received_bye */
1038
g_free (src->bye_reason);
1039
src->bye_reason = g_strdup (reason);
1040
src->received_bye = TRUE;
1044
* rtp_source_send_rtp:
1045
* @src: an #RTPSource
1046
* @buffer: an RTP buffer
1047
* @ntpnstime: the NTP time when this buffer was captured in nanoseconds. This
1048
* is the buffer timestamp converted to NTP time.
1050
* Send an RTP @buffer originating from @src. This will make @src a sender.
1051
* This function takes ownership of @buffer and modifies the SSRC in the RTP
1052
* packet to that of @src when needed.
1054
* Returns: a #GstFlowReturn.
1057
rtp_source_send_rtp (RTPSource * src, GstBuffer * buffer, guint64 ntpnstime)
1059
GstFlowReturn result = GST_FLOW_OK;
1062
guint64 ext_rtptime;
1063
guint64 ntp_diff, rtp_diff;
1066
g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
1067
g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1069
len = gst_rtp_buffer_get_payload_len (buffer);
1071
rtp_source_update_caps (src, GST_BUFFER_CAPS (buffer));
1073
/* we are a sender now */
1074
src->is_sender = TRUE;
1076
/* update stats for the SR */
1077
src->stats.packets_sent++;
1078
src->stats.octets_sent += len;
1079
src->bytes_sent += len;
1081
if (src->prev_ntpnstime) {
1082
elapsed = ntpnstime - src->prev_ntpnstime;
1084
if (elapsed > (G_GINT64_CONSTANT (1) << 31)) {
1088
gst_util_uint64_scale (src->bytes_sent, elapsed,
1089
(G_GINT64_CONSTANT (1) << 29));
1091
GST_LOG ("Elapsed %" G_GUINT64_FORMAT ", bytes %" G_GUINT64_FORMAT
1092
", rate %" G_GUINT64_FORMAT, elapsed, src->bytes_sent, rate);
1094
if (src->bitrate == 0)
1095
src->bitrate = rate;
1097
src->bitrate = ((src->bitrate * 3) + rate) / 4;
1099
src->prev_ntpnstime = ntpnstime;
1100
src->bytes_sent = 0;
1103
GST_LOG ("Reset bitrate measurement");
1104
src->prev_ntpnstime = ntpnstime;
1108
rtptime = gst_rtp_buffer_get_timestamp (buffer);
1109
ext_rtptime = src->last_rtptime;
1110
ext_rtptime = gst_rtp_buffer_ext_timestamp (&ext_rtptime, rtptime);
1112
GST_LOG ("SSRC %08x, RTP %" G_GUINT64_FORMAT ", NTP %" GST_TIME_FORMAT,
1113
src->ssrc, ext_rtptime, GST_TIME_ARGS (ntpnstime));
1115
if (ext_rtptime > src->last_rtptime) {
1116
rtp_diff = ext_rtptime - src->last_rtptime;
1117
ntp_diff = ntpnstime - src->last_ntpnstime;
1119
/* calc the diff so we can detect drift at the sender. This can also be used
1120
* to guestimate the clock rate if the NTP time is locked to the RTP
1121
* timestamps (as is the case when the capture device is providing the clock). */
1122
GST_LOG ("SSRC %08x, diff RTP %" G_GUINT64_FORMAT ", diff NTP %"
1123
GST_TIME_FORMAT, src->ssrc, rtp_diff, GST_TIME_ARGS (ntp_diff));
1126
/* we keep track of the last received RTP timestamp and the corresponding
1127
* NTP timestamp so that we can use this info when constructing SR reports */
1128
src->last_rtptime = ext_rtptime;
1129
src->last_ntpnstime = ntpnstime;
1132
if (src->callbacks.push_rtp) {
1135
ssrc = gst_rtp_buffer_get_ssrc (buffer);
1136
if (ssrc != src->ssrc) {
1137
/* the SSRC of the packet is not correct, make a writable buffer and
1138
* update the SSRC. This could involve a complete copy of the packet when
1139
* it is not writable. Usually the payloader will use caps negotiation to
1140
* get the correct SSRC from the session manager before pushing anything. */
1141
buffer = gst_buffer_make_writable (buffer);
1143
GST_WARNING ("updating SSRC from %08x to %08x, fix the payloader", ssrc,
1145
gst_rtp_buffer_set_ssrc (buffer, src->ssrc);
1147
GST_LOG ("pushing RTP packet %" G_GUINT64_FORMAT, src->stats.packets_sent);
1148
result = src->callbacks.push_rtp (src, buffer, src->user_data);
1150
GST_WARNING ("no callback installed, dropping packet");
1151
gst_buffer_unref (buffer);
1158
* rtp_source_process_sr:
1159
* @src: an #RTPSource
1160
* @time: time of packet arrival
1161
* @ntptime: the NTP time in 32.32 fixed point
1162
* @rtptime: the RTP time
1163
* @packet_count: the packet count
1164
* @octet_count: the octect count
1166
* Update the sender report in @src.
1169
rtp_source_process_sr (RTPSource * src, GstClockTime time, guint64 ntptime,
1170
guint32 rtptime, guint32 packet_count, guint32 octet_count)
1172
RTPSenderReport *curr;
1175
g_return_if_fail (RTP_IS_SOURCE (src));
1177
GST_DEBUG ("got SR packet: SSRC %08x, NTP %08x:%08x, RTP %" G_GUINT32_FORMAT
1178
", PC %" G_GUINT32_FORMAT ", OC %" G_GUINT32_FORMAT, src->ssrc,
1179
(guint32) (ntptime >> 32), (guint32) (ntptime & 0xffffffff), rtptime,
1180
packet_count, octet_count);
1182
curridx = src->stats.curr_sr ^ 1;
1183
curr = &src->stats.sr[curridx];
1185
/* this is a sender now */
1186
src->is_sender = TRUE;
1188
/* update current */
1189
curr->is_valid = TRUE;
1190
curr->ntptime = ntptime;
1191
curr->rtptime = rtptime;
1192
curr->packet_count = packet_count;
1193
curr->octet_count = octet_count;
1197
src->stats.curr_sr = curridx;
1201
* rtp_source_process_rb:
1202
* @src: an #RTPSource
1203
* @time: the current time in nanoseconds since 1970
1204
* @fractionlost: fraction lost since last SR/RR
1205
* @packetslost: the cumululative number of packets lost
1206
* @exthighestseq: the extended last sequence number received
1207
* @jitter: the interarrival jitter
1208
* @lsr: the last SR packet from this source
1209
* @dlsr: the delay since last SR packet
1211
* Update the report block in @src.
1214
rtp_source_process_rb (RTPSource * src, GstClockTime time, guint8 fractionlost,
1215
gint32 packetslost, guint32 exthighestseq, guint32 jitter, guint32 lsr,
1218
RTPReceiverReport *curr;
1222
g_return_if_fail (RTP_IS_SOURCE (src));
1224
GST_DEBUG ("got RB packet: SSRC %08x, FL %2x, PL %d, HS %" G_GUINT32_FORMAT
1225
", jitter %" G_GUINT32_FORMAT ", LSR %04x:%04x, DLSR %04x:%04x",
1226
src->ssrc, fractionlost, packetslost, exthighestseq, jitter, lsr >> 16,
1227
lsr & 0xffff, dlsr >> 16, dlsr & 0xffff);
1229
curridx = src->stats.curr_rr ^ 1;
1230
curr = &src->stats.rr[curridx];
1232
/* update current */
1233
curr->is_valid = TRUE;
1234
curr->fractionlost = fractionlost;
1235
curr->packetslost = packetslost;
1236
curr->exthighestseq = exthighestseq;
1237
curr->jitter = jitter;
1241
/* calculate round trip, round the time up */
1242
ntp = ((gst_rtcp_unix_to_ntp (time) + 0xffff) >> 16) & 0xffffffff;
1244
if (A > 0 && ntp > A)
1248
curr->round_trip = A;
1250
GST_DEBUG ("NTP %04x:%04x, round trip %04x:%04x", ntp >> 16, ntp & 0xffff,
1251
A >> 16, A & 0xffff);
1254
src->stats.curr_rr = curridx;
1258
* rtp_source_get_new_sr:
1259
* @src: an #RTPSource
1260
* @ntpnstime: the current time in nanoseconds since 1970
1261
* @ntptime: the NTP time in 32.32 fixed point
1262
* @rtptime: the RTP time corresponding to @ntptime
1263
* @packet_count: the packet count
1264
* @octet_count: the octect count
1266
* Get new values to put into a new SR report from this source.
1268
* Returns: %TRUE on success.
1271
rtp_source_get_new_sr (RTPSource * src, guint64 ntpnstime,
1272
guint64 * ntptime, guint32 * rtptime, guint32 * packet_count,
1273
guint32 * octet_count)
1276
guint64 t_current_ntp;
1277
GstClockTimeDiff diff;
1279
g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1281
/* use the sync params to interpolate the date->time member to rtptime. We
1282
* use the last sent timestamp and rtptime as reference points. We assume
1283
* that the slope of the rtptime vs timestamp curve is 1, which is certainly
1284
* sufficient for the frequency at which we report SR and the rate we send
1285
* out RTP packets. */
1286
t_rtp = src->last_rtptime;
1288
GST_DEBUG ("last_ntpnstime %" GST_TIME_FORMAT ", last_rtptime %"
1289
G_GUINT64_FORMAT, GST_TIME_ARGS (src->last_ntpnstime), t_rtp);
1291
if (src->clock_rate != -1) {
1292
/* get the diff with the SR time */
1293
diff = GST_CLOCK_DIFF (src->last_ntpnstime, ntpnstime);
1295
/* now translate the diff to RTP time, handle positive and negative cases.
1296
* If there is no diff, we already set rtptime correctly above. */
1298
GST_DEBUG ("ntpnstime %" GST_TIME_FORMAT ", diff %" GST_TIME_FORMAT,
1299
GST_TIME_ARGS (ntpnstime), GST_TIME_ARGS (diff));
1300
t_rtp += gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1303
GST_DEBUG ("ntpnstime %" GST_TIME_FORMAT ", diff -%" GST_TIME_FORMAT,
1304
GST_TIME_ARGS (ntpnstime), GST_TIME_ARGS (diff));
1305
t_rtp -= gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1308
GST_WARNING ("no clock-rate, cannot interpolate rtp time");
1311
/* convert the NTP time in nanoseconds to 32.32 fixed point */
1312
t_current_ntp = gst_util_uint64_scale (ntpnstime, (1LL << 32), GST_SECOND);
1314
GST_DEBUG ("NTP %08x:%08x, RTP %" G_GUINT32_FORMAT,
1315
(guint32) (t_current_ntp >> 32), (guint32) (t_current_ntp & 0xffffffff),
1319
*ntptime = t_current_ntp;
1323
*packet_count = src->stats.packets_sent;
1325
*octet_count = src->stats.octets_sent;
1331
* rtp_source_get_new_rb:
1332
* @src: an #RTPSource
1333
* @time: the current time of the system clock
1334
* @fractionlost: fraction lost since last SR/RR
1335
* @packetslost: the cumululative number of packets lost
1336
* @exthighestseq: the extended last sequence number received
1337
* @jitter: the interarrival jitter
1338
* @lsr: the last SR packet from this source
1339
* @dlsr: the delay since last SR packet
1341
* Get new values to put into a new report block from this source.
1343
* Returns: %TRUE on success.
1346
rtp_source_get_new_rb (RTPSource * src, GstClockTime time,
1347
guint8 * fractionlost, gint32 * packetslost, guint32 * exthighestseq,
1348
guint32 * jitter, guint32 * lsr, guint32 * dlsr)
1350
RTPSourceStats *stats;
1351
guint64 extended_max, expected;
1352
guint64 expected_interval, received_interval, ntptime;
1353
gint64 lost, lost_interval;
1354
guint32 fraction, LSR, DLSR;
1355
GstClockTime sr_time;
1357
stats = &src->stats;
1359
extended_max = stats->cycles + stats->max_seq;
1360
expected = extended_max - stats->base_seq + 1;
1362
GST_DEBUG ("ext_max %" G_GUINT64_FORMAT ", expected %" G_GUINT64_FORMAT
1363
", received %" G_GUINT64_FORMAT ", base_seq %" G_GUINT32_FORMAT,
1364
extended_max, expected, stats->packets_received, stats->base_seq);
1366
lost = expected - stats->packets_received;
1367
lost = CLAMP (lost, -0x800000, 0x7fffff);
1369
expected_interval = expected - stats->prev_expected;
1370
stats->prev_expected = expected;
1371
received_interval = stats->packets_received - stats->prev_received;
1372
stats->prev_received = stats->packets_received;
1374
lost_interval = expected_interval - received_interval;
1376
if (expected_interval == 0 || lost_interval <= 0)
1379
fraction = (lost_interval << 8) / expected_interval;
1381
GST_DEBUG ("add RR for SSRC %08x", src->ssrc);
1382
/* we scaled the jitter up for additional precision */
1383
GST_DEBUG ("fraction %" G_GUINT32_FORMAT ", lost %" G_GINT64_FORMAT
1384
", extseq %" G_GUINT64_FORMAT ", jitter %d", fraction, lost,
1385
extended_max, stats->jitter >> 4);
1387
if (rtp_source_get_last_sr (src, &sr_time, &ntptime, NULL, NULL, NULL)) {
1390
/* LSR is middle 32 bits of the last ntptime */
1391
LSR = (ntptime >> 16) & 0xffffffff;
1392
diff = time - sr_time;
1393
GST_DEBUG ("last SR time diff %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
1394
/* DLSR, delay since last SR is expressed in 1/65536 second units */
1395
DLSR = gst_util_uint64_scale_int (diff, 65536, GST_SECOND);
1397
/* No valid SR received, LSR/DLSR are set to 0 then */
1398
GST_DEBUG ("no valid SR received");
1402
GST_DEBUG ("LSR %04x:%04x, DLSR %04x:%04x", LSR >> 16, LSR & 0xffff,
1403
DLSR >> 16, DLSR & 0xffff);
1406
*fractionlost = fraction;
1408
*packetslost = lost;
1410
*exthighestseq = extended_max;
1412
*jitter = stats->jitter >> 4;
1422
* rtp_source_get_last_sr:
1423
* @src: an #RTPSource
1424
* @time: time of packet arrival
1425
* @ntptime: the NTP time in 32.32 fixed point
1426
* @rtptime: the RTP time
1427
* @packet_count: the packet count
1428
* @octet_count: the octect count
1430
* Get the values of the last sender report as set with rtp_source_process_sr().
1432
* Returns: %TRUE if there was a valid SR report.
1435
rtp_source_get_last_sr (RTPSource * src, GstClockTime * time, guint64 * ntptime,
1436
guint32 * rtptime, guint32 * packet_count, guint32 * octet_count)
1438
RTPSenderReport *curr;
1440
g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1442
curr = &src->stats.sr[src->stats.curr_sr];
1443
if (!curr->is_valid)
1447
*ntptime = curr->ntptime;
1449
*rtptime = curr->rtptime;
1451
*packet_count = curr->packet_count;
1453
*octet_count = curr->octet_count;
1461
* rtp_source_get_last_rb:
1462
* @src: an #RTPSource
1463
* @fractionlost: fraction lost since last SR/RR
1464
* @packetslost: the cumululative number of packets lost
1465
* @exthighestseq: the extended last sequence number received
1466
* @jitter: the interarrival jitter
1467
* @lsr: the last SR packet from this source
1468
* @dlsr: the delay since last SR packet
1469
* @round_trip: the round trip time
1471
* Get the values of the last RB report set with rtp_source_process_rb().
1473
* Returns: %TRUE if there was a valid SB report.
1476
rtp_source_get_last_rb (RTPSource * src, guint8 * fractionlost,
1477
gint32 * packetslost, guint32 * exthighestseq, guint32 * jitter,
1478
guint32 * lsr, guint32 * dlsr, guint32 * round_trip)
1480
RTPReceiverReport *curr;
1482
g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1484
curr = &src->stats.rr[src->stats.curr_rr];
1485
if (!curr->is_valid)
1489
*fractionlost = curr->fractionlost;
1491
*packetslost = curr->packetslost;
1493
*exthighestseq = curr->exthighestseq;
1495
*jitter = curr->jitter;
1501
*round_trip = curr->round_trip;