2 * Copyright (C) <2006> Philippe Khalaf <philippe.kalaf@collabora.co.uk>
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 * SECTION:gstbasertpaudiopayload
22 * @short_description: Base class for audio RTP payloader
24 * Provides a base class for audio RTP payloaders for frame or sample based
25 * audio codecs (constant bitrate)
27 * This class derives from GstBaseRTPPayload. It can be used for payloading
28 * audio codecs. It will only work with constant bitrate codecs. It supports
29 * both frame based and sample based codecs. It takes care of packing up the
30 * audio data into RTP packets and filling up the headers accordingly. The
31 * payloading is done based on the maximum MTU (mtu) and the maximum time per
32 * packet (max-ptime). The general idea is to divide large data buffers into
33 * smaller RTP packets. The RTP packet size is the minimum of either the MTU,
34 * max-ptime (if set) or available data. The RTP packet size is always larger or
35 * equal to min-ptime (if set). If min-ptime is not set, any residual data is
36 * sent in a last RTP packet. In the case of frame based codecs, the resulting
37 * RTP packets always contain full frames.
40 * <title>Usage</title>
42 * To use this base class, your child element needs to call either
43 * gst_base_rtp_audio_payload_set_frame_based() or
44 * gst_base_rtp_audio_payload_set_sample_based(). This is usually done in the
45 * element's _init() function. Then, the child element must call either
46 * gst_base_rtp_audio_payload_set_frame_options(),
47 * gst_base_rtp_audio_payload_set_sample_options() or
48 * gst_base_rtp_audio_payload_set_samplebits_options. Since
49 * GstBaseRTPAudioPayload derives from GstBaseRTPPayload, the child element
50 * must set any variables or call/override any functions required by that base
51 * class. The child element does not need to override any other functions
52 * specific to GstBaseRTPAudioPayload.
63 #include <gst/rtp/gstrtpbuffer.h>
64 #include <gst/base/gstadapter.h>
66 #include "gstbasertpaudiopayload.h"
68 GST_DEBUG_CATEGORY_STATIC (basertpaudiopayload_debug);
69 #define GST_CAT_DEFAULT (basertpaudiopayload_debug)
71 #define DEFAULT_BUFFER_LIST FALSE
80 /* function to convert bytes to a time */
81 typedef GstClockTime (*GetBytesToTimeFunc) (GstBaseRTPAudioPayload * payload,
83 /* function to convert bytes to a RTP time */
84 typedef guint32 (*GetBytesToRTPTimeFunc) (GstBaseRTPAudioPayload * payload,
86 /* function to convert time to bytes */
87 typedef guint64 (*GetTimeToBytesFunc) (GstBaseRTPAudioPayload * payload,
90 struct _GstBaseRTPAudioPayloadPrivate
92 GetBytesToTimeFunc bytes_to_time;
93 GetBytesToRTPTimeFunc bytes_to_rtptime;
94 GetTimeToBytesFunc time_to_bytes;
98 GstClockTime frame_duration_ns;
101 GstClockTime last_timestamp;
102 guint32 last_rtptime;
106 guint cached_min_ptime;
107 guint cached_max_ptime;
109 guint cached_min_length;
110 guint cached_max_length;
111 guint cached_ptime_multiple;
114 gboolean buffer_list;
118 #define GST_BASE_RTP_AUDIO_PAYLOAD_GET_PRIVATE(o) \
119 (G_TYPE_INSTANCE_GET_PRIVATE ((o), GST_TYPE_BASE_RTP_AUDIO_PAYLOAD, \
120 GstBaseRTPAudioPayloadPrivate))
122 static void gst_base_rtp_audio_payload_finalize (GObject * object);
124 static void gst_base_rtp_audio_payload_set_property (GObject * object,
125 guint prop_id, const GValue * value, GParamSpec * pspec);
126 static void gst_base_rtp_audio_payload_get_property (GObject * object,
127 guint prop_id, GValue * value, GParamSpec * pspec);
129 /* bytes to time functions */
131 gst_base_rtp_audio_payload_frame_bytes_to_time (GstBaseRTPAudioPayload *
132 payload, guint64 bytes);
134 gst_base_rtp_audio_payload_sample_bytes_to_time (GstBaseRTPAudioPayload *
135 payload, guint64 bytes);
137 /* bytes to RTP time functions */
139 gst_base_rtp_audio_payload_frame_bytes_to_rtptime (GstBaseRTPAudioPayload *
140 payload, guint64 bytes);
142 gst_base_rtp_audio_payload_sample_bytes_to_rtptime (GstBaseRTPAudioPayload *
143 payload, guint64 bytes);
145 /* time to bytes functions */
147 gst_base_rtp_audio_payload_frame_time_to_bytes (GstBaseRTPAudioPayload *
148 payload, GstClockTime time);
150 gst_base_rtp_audio_payload_sample_time_to_bytes (GstBaseRTPAudioPayload *
151 payload, GstClockTime time);
153 static GstFlowReturn gst_base_rtp_audio_payload_handle_buffer (GstBaseRTPPayload
154 * payload, GstBuffer * buffer);
156 static GstStateChangeReturn gst_base_rtp_payload_audio_change_state (GstElement
157 * element, GstStateChange transition);
159 static gboolean gst_base_rtp_payload_audio_handle_event (GstPad * pad,
162 GST_BOILERPLATE (GstBaseRTPAudioPayload, gst_base_rtp_audio_payload,
163 GstBaseRTPPayload, GST_TYPE_BASE_RTP_PAYLOAD);
166 gst_base_rtp_audio_payload_base_init (gpointer klass)
171 gst_base_rtp_audio_payload_class_init (GstBaseRTPAudioPayloadClass * klass)
173 GObjectClass *gobject_class;
174 GstElementClass *gstelement_class;
175 GstBaseRTPPayloadClass *gstbasertppayload_class;
177 g_type_class_add_private (klass, sizeof (GstBaseRTPAudioPayloadPrivate));
179 gobject_class = (GObjectClass *) klass;
180 gstelement_class = (GstElementClass *) klass;
181 gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
183 gobject_class->finalize = gst_base_rtp_audio_payload_finalize;
184 gobject_class->set_property = gst_base_rtp_audio_payload_set_property;
185 gobject_class->get_property = gst_base_rtp_audio_payload_get_property;
187 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BUFFER_LIST,
188 g_param_spec_boolean ("buffer-list", "Buffer List",
190 DEFAULT_BUFFER_LIST, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
192 gstelement_class->change_state =
193 GST_DEBUG_FUNCPTR (gst_base_rtp_payload_audio_change_state);
195 gstbasertppayload_class->handle_buffer =
196 GST_DEBUG_FUNCPTR (gst_base_rtp_audio_payload_handle_buffer);
197 gstbasertppayload_class->handle_event =
198 GST_DEBUG_FUNCPTR (gst_base_rtp_payload_audio_handle_event);
200 GST_DEBUG_CATEGORY_INIT (basertpaudiopayload_debug, "basertpaudiopayload", 0,
201 "base audio RTP payloader");
205 gst_base_rtp_audio_payload_init (GstBaseRTPAudioPayload * payload,
206 GstBaseRTPAudioPayloadClass * klass)
208 payload->priv = GST_BASE_RTP_AUDIO_PAYLOAD_GET_PRIVATE (payload);
210 /* these need to be set by child object if frame based */
211 payload->frame_size = 0;
212 payload->frame_duration = 0;
214 /* these need to be set by child object if sample based */
215 payload->sample_size = 0;
217 payload->priv->adapter = gst_adapter_new ();
219 payload->priv->buffer_list = DEFAULT_BUFFER_LIST;
223 gst_base_rtp_audio_payload_finalize (GObject * object)
225 GstBaseRTPAudioPayload *payload;
227 payload = GST_BASE_RTP_AUDIO_PAYLOAD (object);
229 g_object_unref (payload->priv->adapter);
231 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
235 gst_base_rtp_audio_payload_set_property (GObject * object,
236 guint prop_id, const GValue * value, GParamSpec * pspec)
238 GstBaseRTPAudioPayload *payload;
240 payload = GST_BASE_RTP_AUDIO_PAYLOAD (object);
243 case PROP_BUFFER_LIST:
244 payload->priv->buffer_list = g_value_get_boolean (value);
247 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
253 gst_base_rtp_audio_payload_get_property (GObject * object,
254 guint prop_id, GValue * value, GParamSpec * pspec)
256 GstBaseRTPAudioPayload *payload;
258 payload = GST_BASE_RTP_AUDIO_PAYLOAD (object);
261 case PROP_BUFFER_LIST:
262 g_value_set_boolean (value, payload->priv->buffer_list);
265 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
271 * gst_base_rtp_audio_payload_set_frame_based:
272 * @basertpaudiopayload: a pointer to the element.
274 * Tells #GstBaseRTPAudioPayload that the child element is for a frame based
278 gst_base_rtp_audio_payload_set_frame_based (GstBaseRTPAudioPayload *
281 g_return_if_fail (basertpaudiopayload != NULL);
282 g_return_if_fail (basertpaudiopayload->priv->time_to_bytes == NULL);
283 g_return_if_fail (basertpaudiopayload->priv->bytes_to_time == NULL);
284 g_return_if_fail (basertpaudiopayload->priv->bytes_to_rtptime == NULL);
286 basertpaudiopayload->priv->bytes_to_time =
287 gst_base_rtp_audio_payload_frame_bytes_to_time;
288 basertpaudiopayload->priv->bytes_to_rtptime =
289 gst_base_rtp_audio_payload_frame_bytes_to_rtptime;
290 basertpaudiopayload->priv->time_to_bytes =
291 gst_base_rtp_audio_payload_frame_time_to_bytes;
295 * gst_base_rtp_audio_payload_set_sample_based:
296 * @basertpaudiopayload: a pointer to the element.
298 * Tells #GstBaseRTPAudioPayload that the child element is for a sample based
302 gst_base_rtp_audio_payload_set_sample_based (GstBaseRTPAudioPayload *
305 g_return_if_fail (basertpaudiopayload != NULL);
306 g_return_if_fail (basertpaudiopayload->priv->time_to_bytes == NULL);
307 g_return_if_fail (basertpaudiopayload->priv->bytes_to_time == NULL);
308 g_return_if_fail (basertpaudiopayload->priv->bytes_to_rtptime == NULL);
310 basertpaudiopayload->priv->bytes_to_time =
311 gst_base_rtp_audio_payload_sample_bytes_to_time;
312 basertpaudiopayload->priv->bytes_to_rtptime =
313 gst_base_rtp_audio_payload_sample_bytes_to_rtptime;
314 basertpaudiopayload->priv->time_to_bytes =
315 gst_base_rtp_audio_payload_sample_time_to_bytes;
319 * gst_base_rtp_audio_payload_set_frame_options:
320 * @basertpaudiopayload: a pointer to the element.
321 * @frame_duration: The duraction of an audio frame in milliseconds.
322 * @frame_size: The size of an audio frame in bytes.
324 * Sets the options for frame based audio codecs.
328 gst_base_rtp_audio_payload_set_frame_options (GstBaseRTPAudioPayload
329 * basertpaudiopayload, gint frame_duration, gint frame_size)
331 GstBaseRTPAudioPayloadPrivate *priv;
333 g_return_if_fail (basertpaudiopayload != NULL);
335 priv = basertpaudiopayload->priv;
337 basertpaudiopayload->frame_duration = frame_duration;
338 priv->frame_duration_ns = frame_duration * GST_MSECOND;
339 basertpaudiopayload->frame_size = frame_size;
340 priv->align = frame_size;
342 gst_adapter_clear (priv->adapter);
344 GST_DEBUG_OBJECT (basertpaudiopayload, "frame set to %d ms and size %d",
345 frame_duration, frame_size);
349 * gst_base_rtp_audio_payload_set_sample_options:
350 * @basertpaudiopayload: a pointer to the element.
351 * @sample_size: Size per sample in bytes.
353 * Sets the options for sample based audio codecs.
356 gst_base_rtp_audio_payload_set_sample_options (GstBaseRTPAudioPayload
357 * basertpaudiopayload, gint sample_size)
359 g_return_if_fail (basertpaudiopayload != NULL);
361 /* sample_size is in bits internally */
362 gst_base_rtp_audio_payload_set_samplebits_options (basertpaudiopayload,
367 * gst_base_rtp_audio_payload_set_samplebits_options:
368 * @basertpaudiopayload: a pointer to the element.
369 * @sample_size: Size per sample in bits.
371 * Sets the options for sample based audio codecs.
376 gst_base_rtp_audio_payload_set_samplebits_options (GstBaseRTPAudioPayload
377 * basertpaudiopayload, gint sample_size)
380 GstBaseRTPAudioPayloadPrivate *priv;
382 g_return_if_fail (basertpaudiopayload != NULL);
384 priv = basertpaudiopayload->priv;
386 basertpaudiopayload->sample_size = sample_size;
388 /* sample_size is in bits and is converted into multiple bytes */
389 fragment_size = sample_size;
390 while ((fragment_size % 8) != 0)
391 fragment_size += fragment_size;
392 priv->fragment_size = fragment_size / 8;
393 priv->align = priv->fragment_size;
395 gst_adapter_clear (priv->adapter);
397 GST_DEBUG_OBJECT (basertpaudiopayload,
398 "Samplebits set to sample size %d bits", sample_size);
402 gst_base_rtp_audio_payload_set_meta (GstBaseRTPAudioPayload * payload,
403 GstBuffer * buffer, guint payload_len, GstClockTime timestamp)
405 GstBaseRTPPayload *basepayload;
406 GstBaseRTPAudioPayloadPrivate *priv;
408 basepayload = GST_BASE_RTP_PAYLOAD_CAST (payload);
409 priv = payload->priv;
411 /* set payload type */
412 gst_rtp_buffer_set_payload_type (buffer, basepayload->pt);
413 /* set marker bit for disconts */
415 GST_DEBUG_OBJECT (payload, "Setting marker and DISCONT");
416 gst_rtp_buffer_set_marker (buffer, TRUE);
417 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
418 priv->discont = FALSE;
420 GST_BUFFER_TIMESTAMP (buffer) = timestamp;
422 /* get the offset in RTP time */
423 GST_BUFFER_OFFSET (buffer) = priv->bytes_to_rtptime (payload, priv->offset);
425 priv->offset += payload_len;
427 /* Set the duration from the size */
428 GST_BUFFER_DURATION (buffer) = priv->bytes_to_time (payload, payload_len);
430 /* remember the last rtptime/timestamp pair. We will use this to realign our
431 * RTP timestamp after a buffer discont */
432 priv->last_rtptime = GST_BUFFER_OFFSET (buffer);
433 priv->last_timestamp = timestamp;
437 * gst_base_rtp_audio_payload_push:
438 * @baseaudiopayload: a #GstBaseRTPPayload
439 * @data: data to set as payload
440 * @payload_len: length of payload
441 * @timestamp: a #GstClockTime
443 * Create an RTP buffer and store @payload_len bytes of @data as the
444 * payload. Set the timestamp on the new buffer to @timestamp before pushing
445 * the buffer downstream.
447 * Returns: a #GstFlowReturn
452 gst_base_rtp_audio_payload_push (GstBaseRTPAudioPayload * baseaudiopayload,
453 const guint8 * data, guint payload_len, GstClockTime timestamp)
455 GstBaseRTPPayload *basepayload;
460 basepayload = GST_BASE_RTP_PAYLOAD (baseaudiopayload);
462 GST_DEBUG_OBJECT (baseaudiopayload, "Pushing %d bytes ts %" GST_TIME_FORMAT,
463 payload_len, GST_TIME_ARGS (timestamp));
465 /* create buffer to hold the payload */
466 outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
469 payload = gst_rtp_buffer_get_payload (outbuf);
470 memcpy (payload, data, payload_len);
473 gst_base_rtp_audio_payload_set_meta (baseaudiopayload, outbuf, payload_len,
476 ret = gst_basertppayload_push (basepayload, outbuf);
482 gst_base_rtp_audio_payload_push_buffer (GstBaseRTPAudioPayload *
483 baseaudiopayload, GstBuffer * buffer, GstClockTime timestamp)
485 GstBaseRTPPayload *basepayload;
486 GstBaseRTPAudioPayloadPrivate *priv;
492 priv = baseaudiopayload->priv;
493 basepayload = GST_BASE_RTP_PAYLOAD (baseaudiopayload);
495 payload_len = GST_BUFFER_SIZE (buffer);
497 GST_DEBUG_OBJECT (baseaudiopayload, "Pushing %d bytes ts %" GST_TIME_FORMAT,
498 payload_len, GST_TIME_ARGS (timestamp));
500 if (priv->buffer_list) {
501 /* create just the RTP header buffer */
502 outbuf = gst_rtp_buffer_new_allocate (0, 0, 0);
504 /* create buffer to hold the payload */
505 outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
509 gst_base_rtp_audio_payload_set_meta (baseaudiopayload, outbuf, payload_len,
512 if (priv->buffer_list) {
514 GstBufferListIterator *it;
516 list = gst_buffer_list_new ();
517 it = gst_buffer_list_iterate (list);
519 /* add both buffers to the buffer list */
520 gst_buffer_list_iterator_add_group (it);
521 gst_buffer_list_iterator_add (it, outbuf);
522 gst_buffer_list_iterator_add (it, buffer);
524 gst_buffer_list_iterator_free (it);
526 GST_DEBUG_OBJECT (baseaudiopayload, "Pushing list %p", list);
527 ret = gst_basertppayload_push_list (basepayload, list);
530 payload = gst_rtp_buffer_get_payload (outbuf);
531 memcpy (payload, GST_BUFFER_DATA (buffer), payload_len);
532 gst_buffer_unref (buffer);
534 GST_DEBUG_OBJECT (baseaudiopayload, "Pushing buffer %p", outbuf);
535 ret = gst_basertppayload_push (basepayload, outbuf);
542 * gst_base_rtp_audio_payload_flush:
543 * @baseaudiopayload: a #GstBaseRTPPayload
544 * @payload_len: length of payload
545 * @timestamp: a #GstClockTime
547 * Create an RTP buffer and store @payload_len bytes of the adapter as the
548 * payload. Set the timestamp on the new buffer to @timestamp before pushing
549 * the buffer downstream.
551 * If @payload_len is -1, all pending bytes will be flushed. If @timestamp is
552 * -1, the timestamp will be calculated automatically.
554 * Returns: a #GstFlowReturn
559 gst_base_rtp_audio_payload_flush (GstBaseRTPAudioPayload * baseaudiopayload,
560 guint payload_len, GstClockTime timestamp)
562 GstBaseRTPPayload *basepayload;
563 GstBaseRTPAudioPayloadPrivate *priv;
570 priv = baseaudiopayload->priv;
571 adapter = priv->adapter;
573 basepayload = GST_BASE_RTP_PAYLOAD (baseaudiopayload);
575 if (payload_len == -1)
576 payload_len = gst_adapter_available (adapter);
578 /* nothing to do, just return */
579 if (payload_len == 0)
582 if (timestamp == -1) {
583 /* calculate the timestamp */
584 timestamp = gst_adapter_prev_timestamp (adapter, &distance);
586 GST_LOG_OBJECT (baseaudiopayload,
587 "last timestamp %" GST_TIME_FORMAT ", distance %" G_GUINT64_FORMAT,
588 GST_TIME_ARGS (timestamp), distance);
590 if (GST_CLOCK_TIME_IS_VALID (timestamp) && distance > 0) {
591 /* convert the number of bytes since the last timestamp to time and add to
592 * the last seen timestamp */
593 timestamp += priv->bytes_to_time (baseaudiopayload, distance);
597 GST_DEBUG_OBJECT (baseaudiopayload, "Pushing %d bytes ts %" GST_TIME_FORMAT,
598 payload_len, GST_TIME_ARGS (timestamp));
600 if (priv->buffer_list && gst_adapter_available_fast (adapter) >= payload_len) {
602 /* we can quickly take a buffer out of the adapter without having to copy
604 buffer = gst_adapter_take_buffer (adapter, payload_len);
607 gst_base_rtp_audio_payload_push_buffer (baseaudiopayload, buffer,
610 /* create buffer to hold the payload */
611 outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
614 payload = gst_rtp_buffer_get_payload (outbuf);
615 gst_adapter_copy (adapter, payload, 0, payload_len);
616 gst_adapter_flush (adapter, payload_len);
619 gst_base_rtp_audio_payload_set_meta (baseaudiopayload, outbuf, payload_len,
622 ret = gst_basertppayload_push (basepayload, outbuf);
628 #define ALIGN_DOWN(val,len) ((val) - ((val) % (len)))
630 /* calculate the min and max length of a packet. This depends on the configured
631 * mtu and min/max_ptime values. We cache those so that we don't have to redo
632 * all the calculations */
634 gst_base_rtp_audio_payload_get_lengths (GstBaseRTPPayload *
635 basepayload, guint * min_payload_len, guint * max_payload_len,
638 GstBaseRTPAudioPayload *payload;
639 GstBaseRTPAudioPayloadPrivate *priv;
641 guint maxptime_octets;
642 guint minptime_octets;
643 guint ptime_mult_octets;
645 payload = GST_BASE_RTP_AUDIO_PAYLOAD_CAST (basepayload);
646 priv = payload->priv;
648 if (priv->align == 0)
651 mtu = GST_BASE_RTP_PAYLOAD_MTU (payload);
653 /* check cached values */
654 if (G_LIKELY (priv->cached_mtu == mtu
655 && priv->cached_ptime_multiple ==
656 basepayload->abidata.ABI.ptime_multiple
657 && priv->cached_ptime == basepayload->abidata.ABI.ptime
658 && priv->cached_max_ptime == basepayload->max_ptime
659 && priv->cached_min_ptime == basepayload->min_ptime)) {
660 /* if nothing changed, return cached values */
661 *min_payload_len = priv->cached_min_length;
662 *max_payload_len = priv->cached_max_length;
663 *align = priv->cached_align;
667 ptime_mult_octets = priv->time_to_bytes (payload,
668 basepayload->abidata.ABI.ptime_multiple);
669 *align = ALIGN_DOWN (MAX (priv->align, ptime_mult_octets), priv->align);
672 if (basepayload->max_ptime != -1) {
673 maxptime_octets = priv->time_to_bytes (payload, basepayload->max_ptime);
675 maxptime_octets = G_MAXUINT;
678 max_mtu = gst_rtp_buffer_calc_payload_len (mtu, 0, 0);
679 /* round down to alignment */
680 max_mtu = ALIGN_DOWN (max_mtu, *align);
682 /* combine max ptime and max payload length */
683 *max_payload_len = MIN (max_mtu, maxptime_octets);
685 /* min number of bytes based on a given ptime */
686 minptime_octets = priv->time_to_bytes (payload, basepayload->min_ptime);
687 /* must be at least one frame size */
688 *min_payload_len = MAX (minptime_octets, *align);
690 if (*min_payload_len > *max_payload_len)
691 *min_payload_len = *max_payload_len;
693 /* If the ptime is specified in the caps, tried to adhere to it exactly */
694 if (basepayload->abidata.ABI.ptime) {
695 guint ptime_in_bytes = priv->time_to_bytes (payload,
696 basepayload->abidata.ABI.ptime);
698 /* clip to computed min and max lengths */
699 ptime_in_bytes = MAX (*min_payload_len, ptime_in_bytes);
700 ptime_in_bytes = MIN (*max_payload_len, ptime_in_bytes);
702 *min_payload_len = *max_payload_len = ptime_in_bytes;
706 priv->cached_mtu = mtu;
707 priv->cached_ptime = basepayload->abidata.ABI.ptime;
708 priv->cached_min_ptime = basepayload->min_ptime;
709 priv->cached_max_ptime = basepayload->max_ptime;
710 priv->cached_ptime_multiple = basepayload->abidata.ABI.ptime_multiple;
711 priv->cached_min_length = *min_payload_len;
712 priv->cached_max_length = *max_payload_len;
713 priv->cached_align = *align;
718 /* frame conversions functions */
720 gst_base_rtp_audio_payload_frame_bytes_to_time (GstBaseRTPAudioPayload *
721 payload, guint64 bytes)
725 framecount = bytes / payload->frame_size;
726 if (G_UNLIKELY (bytes % payload->frame_size))
729 return framecount * payload->priv->frame_duration_ns;
733 gst_base_rtp_audio_payload_frame_bytes_to_rtptime (GstBaseRTPAudioPayload *
734 payload, guint64 bytes)
739 framecount = bytes / payload->frame_size;
740 if (G_UNLIKELY (bytes % payload->frame_size))
743 time = framecount * payload->priv->frame_duration_ns;
745 return gst_util_uint64_scale_int (time,
746 GST_BASE_RTP_PAYLOAD (payload)->clock_rate, GST_SECOND);
750 gst_base_rtp_audio_payload_frame_time_to_bytes (GstBaseRTPAudioPayload *
751 payload, GstClockTime time)
753 return gst_util_uint64_scale (time, payload->frame_size,
754 payload->priv->frame_duration_ns);
757 /* sample conversion functions */
759 gst_base_rtp_audio_payload_sample_bytes_to_time (GstBaseRTPAudioPayload *
760 payload, guint64 bytes)
764 /* avoid division when we can */
765 if (G_LIKELY (payload->sample_size != 8))
766 rtptime = gst_util_uint64_scale_int (bytes, 8, payload->sample_size);
770 return gst_util_uint64_scale_int (rtptime, GST_SECOND,
771 GST_BASE_RTP_PAYLOAD (payload)->clock_rate);
775 gst_base_rtp_audio_payload_sample_bytes_to_rtptime (GstBaseRTPAudioPayload *
776 payload, guint64 bytes)
778 /* avoid division when we can */
779 if (G_LIKELY (payload->sample_size != 8))
780 return gst_util_uint64_scale_int (bytes, 8, payload->sample_size);
786 gst_base_rtp_audio_payload_sample_time_to_bytes (GstBaseRTPAudioPayload *
787 payload, guint64 time)
791 samples = gst_util_uint64_scale_int (time,
792 GST_BASE_RTP_PAYLOAD (payload)->clock_rate, GST_SECOND);
794 /* avoid multiplication when we can */
795 if (G_LIKELY (payload->sample_size != 8))
796 return gst_util_uint64_scale_int (samples, payload->sample_size, 8);
802 gst_base_rtp_audio_payload_handle_buffer (GstBaseRTPPayload *
803 basepayload, GstBuffer * buffer)
805 GstBaseRTPAudioPayload *payload;
806 GstBaseRTPAudioPayloadPrivate *priv;
810 guint min_payload_len;
811 guint max_payload_len;
815 GstClockTime timestamp;
819 payload = GST_BASE_RTP_AUDIO_PAYLOAD_CAST (basepayload);
820 priv = payload->priv;
822 timestamp = GST_BUFFER_TIMESTAMP (buffer);
823 discont = GST_BUFFER_IS_DISCONT (buffer);
826 GST_DEBUG_OBJECT (payload, "Got DISCONT");
827 /* flush everything out of the adapter, mark DISCONT */
828 ret = gst_base_rtp_audio_payload_flush (payload, -1, -1);
829 priv->discont = TRUE;
831 /* get the distance between the timestamp gap and produce the same gap in
832 * the RTP timestamps */
833 if (priv->last_timestamp != -1 && timestamp != -1) {
834 /* we had a last timestamp, compare it to the new timestamp and update the
835 * offset counter for RTP timestamps. The effect is that we will produce
836 * output buffers containing the same RTP timestamp gap as the gap
837 * between the GST timestamps. */
838 if (timestamp > priv->last_timestamp) {
841 /* we're only going to apply a positive gap, otherwise we let the marker
842 * bit do its thing. simply convert to bytes and add the current
844 diff = timestamp - priv->last_timestamp;
845 bytes = priv->time_to_bytes (payload, diff);
846 priv->offset += bytes;
848 GST_DEBUG_OBJECT (payload,
849 "elapsed time %" GST_TIME_FORMAT ", bytes %" G_GUINT64_FORMAT
850 ", new offset %" G_GUINT64_FORMAT, GST_TIME_ARGS (diff), bytes,
856 if (!gst_base_rtp_audio_payload_get_lengths (basepayload, &min_payload_len,
857 &max_payload_len, &align))
860 GST_DEBUG_OBJECT (payload,
861 "Calculated min_payload_len %u and max_payload_len %u",
862 min_payload_len, max_payload_len);
864 size = GST_BUFFER_SIZE (buffer);
866 /* shortcut, we don't need to use the adapter when the packet can be pushed
867 * through directly. */
868 available = gst_adapter_available (priv->adapter);
870 GST_DEBUG_OBJECT (payload, "got buffer size %u, available %u",
873 if (available == 0 && (size >= min_payload_len && size <= max_payload_len) &&
874 (size % align == 0)) {
875 /* If buffer fits on an RTP packet, let's just push it through
876 * this will check against max_ptime and max_mtu */
877 GST_DEBUG_OBJECT (payload, "Fast packet push");
878 ret = gst_base_rtp_audio_payload_push_buffer (payload, buffer, timestamp);
880 /* push the buffer in the adapter */
881 gst_adapter_push (priv->adapter, buffer);
884 GST_DEBUG_OBJECT (payload, "available now %u", available);
886 /* as long as we have full frames */
887 while (available >= min_payload_len) {
888 /* get multiple of alignment */
889 payload_len = MIN (max_payload_len, available);
890 payload_len = ALIGN_DOWN (payload_len, align);
892 /* and flush out the bytes from the adapter, automatically set the
894 ret = gst_base_rtp_audio_payload_flush (payload, payload_len, -1);
896 available -= payload_len;
897 GST_DEBUG_OBJECT (payload, "available after push %u", available);
905 GST_ELEMENT_ERROR (payload, STREAM, NOT_IMPLEMENTED, (NULL),
906 ("subclass did not configure us properly"));
907 gst_buffer_unref (buffer);
908 return GST_FLOW_ERROR;
912 static GstStateChangeReturn
913 gst_base_rtp_payload_audio_change_state (GstElement * element,
914 GstStateChange transition)
916 GstBaseRTPAudioPayload *basertppayload;
917 GstStateChangeReturn ret;
919 basertppayload = GST_BASE_RTP_AUDIO_PAYLOAD (element);
921 switch (transition) {
922 case GST_STATE_CHANGE_READY_TO_PAUSED:
923 basertppayload->priv->cached_mtu = -1;
924 basertppayload->priv->last_rtptime = -1;
925 basertppayload->priv->last_timestamp = -1;
931 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
933 switch (transition) {
934 case GST_STATE_CHANGE_PAUSED_TO_READY:
935 gst_adapter_clear (basertppayload->priv->adapter);
945 gst_base_rtp_payload_audio_handle_event (GstPad * pad, GstEvent * event)
947 GstBaseRTPAudioPayload *payload;
948 gboolean res = FALSE;
950 payload = GST_BASE_RTP_AUDIO_PAYLOAD (gst_pad_get_parent (pad));
952 switch (GST_EVENT_TYPE (event)) {
954 /* flush remaining bytes in the adapter */
955 gst_base_rtp_audio_payload_flush (payload, -1, -1);
957 case GST_EVENT_FLUSH_STOP:
958 gst_adapter_clear (payload->priv->adapter);
964 gst_object_unref (payload);
966 /* return FALSE to let parent handle the remainder of the event */
971 * gst_base_rtp_audio_payload_get_adapter:
972 * @basertpaudiopayload: a #GstBaseRTPAudioPayload
974 * Gets the internal adapter used by the depayloader.
976 * Returns: a #GstAdapter.
981 gst_base_rtp_audio_payload_get_adapter (GstBaseRTPAudioPayload
982 * basertpaudiopayload)
986 if ((adapter = basertpaudiopayload->priv->adapter))
987 g_object_ref (adapter);