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
26 * Provides a base class for audio RTP payloaders for frame or sample based
27 * audio codecs (constant bitrate)
30 * This class derives from GstBaseRTPPayload. It can be used for payloading
31 * audio codecs. It will only work with constant bitrate codecs. It supports
32 * both frame based and sample based codecs. It takes care of packing up the
33 * audio data into RTP packets and filling up the headers accordingly. The
34 * payloading is done based on the maximum MTU (mtu) and the maximum time per
35 * packet (max-ptime). The general idea is to divide large data buffers into
36 * smaller RTP packets. The RTP packet size is the minimum of either the MTU,
37 * max-ptime (if set) or available data. The RTP packet size is always larger or
38 * equal to min-ptime (if set). If min-ptime is not set, any residual data is
39 * sent in a last RTP packet. In the case of frame based codecs, the resulting
40 * RTP packets always contain full frames.
42 * <title>Usage</title>
44 * To use this base class, your child element needs to call either
45 * gst_base_rtp_audio_payload_set_frame_based() or
46 * gst_base_rtp_audio_payload_set_sample_based(). This is usually done in the
47 * element's _init() function. Then, the child element must call either
48 * gst_base_rtp_audio_payload_set_frame_options(),
49 * gst_base_rtp_audio_payload_set_sample_options() or
50 * gst_base_rtp_audio_payload_set_samplebits_options. Since
51 * GstBaseRTPAudioPayload derives from GstBaseRTPPayload, the child element
52 * must set any variables or call/override any functions required by that base
53 * class. The child element does not need to override any other functions
54 * specific to GstBaseRTPAudioPayload.
65 #include <gst/rtp/gstrtpbuffer.h>
66 #include <gst/base/gstadapter.h>
68 #include "gstbasertpaudiopayload.h"
70 GST_DEBUG_CATEGORY_STATIC (basertpaudiopayload_debug);
71 #define GST_CAT_DEFAULT (basertpaudiopayload_debug)
73 #define DEFAULT_BUFFER_LIST FALSE
82 /* function to convert bytes to a time */
83 typedef GstClockTime (*GetBytesToTimeFunc) (GstBaseRTPAudioPayload * payload,
85 /* function to convert bytes to a RTP time */
86 typedef guint32 (*GetBytesToRTPTimeFunc) (GstBaseRTPAudioPayload * payload,
88 /* function to convert time to bytes */
89 typedef guint64 (*GetTimeToBytesFunc) (GstBaseRTPAudioPayload * payload,
92 struct _GstBaseRTPAudioPayloadPrivate
94 GetBytesToTimeFunc bytes_to_time;
95 GetBytesToRTPTimeFunc bytes_to_rtptime;
96 GetTimeToBytesFunc time_to_bytes;
100 GstClockTime frame_duration_ns;
103 GstClockTime last_timestamp;
104 guint32 last_rtptime;
108 guint cached_min_ptime;
109 guint cached_max_ptime;
111 guint cached_min_length;
112 guint cached_max_length;
113 guint cached_ptime_multiple;
116 gboolean buffer_list;
120 #define GST_BASE_RTP_AUDIO_PAYLOAD_GET_PRIVATE(o) \
121 (G_TYPE_INSTANCE_GET_PRIVATE ((o), GST_TYPE_BASE_RTP_AUDIO_PAYLOAD, \
122 GstBaseRTPAudioPayloadPrivate))
124 static void gst_base_rtp_audio_payload_finalize (GObject * object);
126 static void gst_base_rtp_audio_payload_set_property (GObject * object,
127 guint prop_id, const GValue * value, GParamSpec * pspec);
128 static void gst_base_rtp_audio_payload_get_property (GObject * object,
129 guint prop_id, GValue * value, GParamSpec * pspec);
131 /* bytes to time functions */
133 gst_base_rtp_audio_payload_frame_bytes_to_time (GstBaseRTPAudioPayload *
134 payload, guint64 bytes);
136 gst_base_rtp_audio_payload_sample_bytes_to_time (GstBaseRTPAudioPayload *
137 payload, guint64 bytes);
139 /* bytes to RTP time functions */
141 gst_base_rtp_audio_payload_frame_bytes_to_rtptime (GstBaseRTPAudioPayload *
142 payload, guint64 bytes);
144 gst_base_rtp_audio_payload_sample_bytes_to_rtptime (GstBaseRTPAudioPayload *
145 payload, guint64 bytes);
147 /* time to bytes functions */
149 gst_base_rtp_audio_payload_frame_time_to_bytes (GstBaseRTPAudioPayload *
150 payload, GstClockTime time);
152 gst_base_rtp_audio_payload_sample_time_to_bytes (GstBaseRTPAudioPayload *
153 payload, GstClockTime time);
155 static GstFlowReturn gst_base_rtp_audio_payload_handle_buffer (GstBaseRTPPayload
156 * payload, GstBuffer * buffer);
158 static GstStateChangeReturn gst_base_rtp_payload_audio_change_state (GstElement
159 * element, GstStateChange transition);
161 static gboolean gst_base_rtp_payload_audio_handle_event (GstPad * pad,
164 GST_BOILERPLATE (GstBaseRTPAudioPayload, gst_base_rtp_audio_payload,
165 GstBaseRTPPayload, GST_TYPE_BASE_RTP_PAYLOAD);
168 gst_base_rtp_audio_payload_base_init (gpointer klass)
173 gst_base_rtp_audio_payload_class_init (GstBaseRTPAudioPayloadClass * klass)
175 GObjectClass *gobject_class;
176 GstElementClass *gstelement_class;
177 GstBaseRTPPayloadClass *gstbasertppayload_class;
179 g_type_class_add_private (klass, sizeof (GstBaseRTPAudioPayloadPrivate));
181 gobject_class = (GObjectClass *) klass;
182 gstelement_class = (GstElementClass *) klass;
183 gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
185 gobject_class->finalize = gst_base_rtp_audio_payload_finalize;
186 gobject_class->set_property = gst_base_rtp_audio_payload_set_property;
187 gobject_class->get_property = gst_base_rtp_audio_payload_get_property;
189 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BUFFER_LIST,
190 g_param_spec_boolean ("buffer-list", "Buffer List",
192 DEFAULT_BUFFER_LIST, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
194 gstelement_class->change_state =
195 GST_DEBUG_FUNCPTR (gst_base_rtp_payload_audio_change_state);
197 gstbasertppayload_class->handle_buffer =
198 GST_DEBUG_FUNCPTR (gst_base_rtp_audio_payload_handle_buffer);
199 gstbasertppayload_class->handle_event =
200 GST_DEBUG_FUNCPTR (gst_base_rtp_payload_audio_handle_event);
202 GST_DEBUG_CATEGORY_INIT (basertpaudiopayload_debug, "basertpaudiopayload", 0,
203 "base audio RTP payloader");
207 gst_base_rtp_audio_payload_init (GstBaseRTPAudioPayload * payload,
208 GstBaseRTPAudioPayloadClass * klass)
210 payload->priv = GST_BASE_RTP_AUDIO_PAYLOAD_GET_PRIVATE (payload);
212 /* these need to be set by child object if frame based */
213 payload->frame_size = 0;
214 payload->frame_duration = 0;
216 /* these need to be set by child object if sample based */
217 payload->sample_size = 0;
219 payload->priv->adapter = gst_adapter_new ();
221 payload->priv->buffer_list = DEFAULT_BUFFER_LIST;
225 gst_base_rtp_audio_payload_finalize (GObject * object)
227 GstBaseRTPAudioPayload *payload;
229 payload = GST_BASE_RTP_AUDIO_PAYLOAD (object);
231 g_object_unref (payload->priv->adapter);
233 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
237 gst_base_rtp_audio_payload_set_property (GObject * object,
238 guint prop_id, const GValue * value, GParamSpec * pspec)
240 GstBaseRTPAudioPayload *payload;
242 payload = GST_BASE_RTP_AUDIO_PAYLOAD (object);
245 case PROP_BUFFER_LIST:
246 payload->priv->buffer_list = g_value_get_boolean (value);
249 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
255 gst_base_rtp_audio_payload_get_property (GObject * object,
256 guint prop_id, GValue * value, GParamSpec * pspec)
258 GstBaseRTPAudioPayload *payload;
260 payload = GST_BASE_RTP_AUDIO_PAYLOAD (object);
263 case PROP_BUFFER_LIST:
264 g_value_set_boolean (value, payload->priv->buffer_list);
267 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
273 * gst_base_rtp_audio_payload_set_frame_based:
274 * @basertpaudiopayload: a pointer to the element.
276 * Tells #GstBaseRTPAudioPayload that the child element is for a frame based
280 gst_base_rtp_audio_payload_set_frame_based (GstBaseRTPAudioPayload *
283 g_return_if_fail (basertpaudiopayload != NULL);
284 g_return_if_fail (basertpaudiopayload->priv->time_to_bytes == NULL);
285 g_return_if_fail (basertpaudiopayload->priv->bytes_to_time == NULL);
286 g_return_if_fail (basertpaudiopayload->priv->bytes_to_rtptime == NULL);
288 basertpaudiopayload->priv->bytes_to_time =
289 gst_base_rtp_audio_payload_frame_bytes_to_time;
290 basertpaudiopayload->priv->bytes_to_rtptime =
291 gst_base_rtp_audio_payload_frame_bytes_to_rtptime;
292 basertpaudiopayload->priv->time_to_bytes =
293 gst_base_rtp_audio_payload_frame_time_to_bytes;
297 * gst_base_rtp_audio_payload_set_sample_based:
298 * @basertpaudiopayload: a pointer to the element.
300 * Tells #GstBaseRTPAudioPayload that the child element is for a sample based
304 gst_base_rtp_audio_payload_set_sample_based (GstBaseRTPAudioPayload *
307 g_return_if_fail (basertpaudiopayload != NULL);
308 g_return_if_fail (basertpaudiopayload->priv->time_to_bytes == NULL);
309 g_return_if_fail (basertpaudiopayload->priv->bytes_to_time == NULL);
310 g_return_if_fail (basertpaudiopayload->priv->bytes_to_rtptime == NULL);
312 basertpaudiopayload->priv->bytes_to_time =
313 gst_base_rtp_audio_payload_sample_bytes_to_time;
314 basertpaudiopayload->priv->bytes_to_rtptime =
315 gst_base_rtp_audio_payload_sample_bytes_to_rtptime;
316 basertpaudiopayload->priv->time_to_bytes =
317 gst_base_rtp_audio_payload_sample_time_to_bytes;
321 * gst_base_rtp_audio_payload_set_frame_options:
322 * @basertpaudiopayload: a pointer to the element.
323 * @frame_duration: The duraction of an audio frame in milliseconds.
324 * @frame_size: The size of an audio frame in bytes.
326 * Sets the options for frame based audio codecs.
330 gst_base_rtp_audio_payload_set_frame_options (GstBaseRTPAudioPayload
331 * basertpaudiopayload, gint frame_duration, gint frame_size)
333 GstBaseRTPAudioPayloadPrivate *priv;
335 g_return_if_fail (basertpaudiopayload != NULL);
337 priv = basertpaudiopayload->priv;
339 basertpaudiopayload->frame_duration = frame_duration;
340 priv->frame_duration_ns = frame_duration * GST_MSECOND;
341 basertpaudiopayload->frame_size = frame_size;
342 priv->align = frame_size;
344 gst_adapter_clear (priv->adapter);
346 GST_DEBUG_OBJECT (basertpaudiopayload, "frame set to %d ms and size %d",
347 frame_duration, frame_size);
351 * gst_base_rtp_audio_payload_set_sample_options:
352 * @basertpaudiopayload: a pointer to the element.
353 * @sample_size: Size per sample in bytes.
355 * Sets the options for sample based audio codecs.
358 gst_base_rtp_audio_payload_set_sample_options (GstBaseRTPAudioPayload
359 * basertpaudiopayload, gint sample_size)
361 g_return_if_fail (basertpaudiopayload != NULL);
363 /* sample_size is in bits internally */
364 gst_base_rtp_audio_payload_set_samplebits_options (basertpaudiopayload,
369 * gst_base_rtp_audio_payload_set_samplebits_options:
370 * @basertpaudiopayload: a pointer to the element.
371 * @sample_size: Size per sample in bits.
373 * Sets the options for sample based audio codecs.
378 gst_base_rtp_audio_payload_set_samplebits_options (GstBaseRTPAudioPayload
379 * basertpaudiopayload, gint sample_size)
382 GstBaseRTPAudioPayloadPrivate *priv;
384 g_return_if_fail (basertpaudiopayload != NULL);
386 priv = basertpaudiopayload->priv;
388 basertpaudiopayload->sample_size = sample_size;
390 /* sample_size is in bits and is converted into multiple bytes */
391 fragment_size = sample_size;
392 while ((fragment_size % 8) != 0)
393 fragment_size += fragment_size;
394 priv->fragment_size = fragment_size / 8;
395 priv->align = priv->fragment_size;
397 gst_adapter_clear (priv->adapter);
399 GST_DEBUG_OBJECT (basertpaudiopayload,
400 "Samplebits set to sample size %d bits", sample_size);
404 gst_base_rtp_audio_payload_set_meta (GstBaseRTPAudioPayload * payload,
405 GstBuffer * buffer, guint payload_len, GstClockTime timestamp)
407 GstBaseRTPPayload *basepayload;
408 GstBaseRTPAudioPayloadPrivate *priv;
410 basepayload = GST_BASE_RTP_PAYLOAD_CAST (payload);
411 priv = payload->priv;
413 /* set payload type */
414 gst_rtp_buffer_set_payload_type (buffer, basepayload->pt);
415 /* set marker bit for disconts */
417 GST_DEBUG_OBJECT (payload, "Setting marker and DISCONT");
418 gst_rtp_buffer_set_marker (buffer, TRUE);
419 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
420 priv->discont = FALSE;
422 GST_BUFFER_TIMESTAMP (buffer) = timestamp;
424 /* get the offset in RTP time */
425 GST_BUFFER_OFFSET (buffer) = priv->bytes_to_rtptime (payload, priv->offset);
427 priv->offset += payload_len;
429 /* Set the duration from the size */
430 GST_BUFFER_DURATION (buffer) = priv->bytes_to_time (payload, payload_len);
432 /* remember the last rtptime/timestamp pair. We will use this to realign our
433 * RTP timestamp after a buffer discont */
434 priv->last_rtptime = GST_BUFFER_OFFSET (buffer);
435 priv->last_timestamp = timestamp;
439 * gst_base_rtp_audio_payload_push:
440 * @baseaudiopayload: a #GstBaseRTPPayload
441 * @data: data to set as payload
442 * @payload_len: length of payload
443 * @timestamp: a #GstClockTime
445 * Create an RTP buffer and store @payload_len bytes of @data as the
446 * payload. Set the timestamp on the new buffer to @timestamp before pushing
447 * the buffer downstream.
449 * Returns: a #GstFlowReturn
454 gst_base_rtp_audio_payload_push (GstBaseRTPAudioPayload * baseaudiopayload,
455 const guint8 * data, guint payload_len, GstClockTime timestamp)
457 GstBaseRTPPayload *basepayload;
462 basepayload = GST_BASE_RTP_PAYLOAD (baseaudiopayload);
464 GST_DEBUG_OBJECT (baseaudiopayload, "Pushing %d bytes ts %" GST_TIME_FORMAT,
465 payload_len, GST_TIME_ARGS (timestamp));
467 /* create buffer to hold the payload */
468 outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
471 payload = gst_rtp_buffer_get_payload (outbuf);
472 memcpy (payload, data, payload_len);
475 gst_base_rtp_audio_payload_set_meta (baseaudiopayload, outbuf, payload_len,
478 ret = gst_basertppayload_push (basepayload, outbuf);
484 gst_base_rtp_audio_payload_push_buffer (GstBaseRTPAudioPayload *
485 baseaudiopayload, GstBuffer * buffer, GstClockTime timestamp)
487 GstBaseRTPPayload *basepayload;
488 GstBaseRTPAudioPayloadPrivate *priv;
494 priv = baseaudiopayload->priv;
495 basepayload = GST_BASE_RTP_PAYLOAD (baseaudiopayload);
497 payload_len = GST_BUFFER_SIZE (buffer);
499 GST_DEBUG_OBJECT (baseaudiopayload, "Pushing %d bytes ts %" GST_TIME_FORMAT,
500 payload_len, GST_TIME_ARGS (timestamp));
502 if (priv->buffer_list) {
503 /* create just the RTP header buffer */
504 outbuf = gst_rtp_buffer_new_allocate (0, 0, 0);
506 /* create buffer to hold the payload */
507 outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
511 gst_base_rtp_audio_payload_set_meta (baseaudiopayload, outbuf, payload_len,
514 if (priv->buffer_list) {
516 GstBufferListIterator *it;
518 list = gst_buffer_list_new ();
519 it = gst_buffer_list_iterate (list);
521 /* add both buffers to the buffer list */
522 gst_buffer_list_iterator_add_group (it);
523 gst_buffer_list_iterator_add (it, outbuf);
524 gst_buffer_list_iterator_add (it, buffer);
526 gst_buffer_list_iterator_free (it);
528 GST_DEBUG_OBJECT (baseaudiopayload, "Pushing list %p", list);
529 ret = gst_basertppayload_push_list (basepayload, list);
532 payload = gst_rtp_buffer_get_payload (outbuf);
533 memcpy (payload, GST_BUFFER_DATA (buffer), payload_len);
534 gst_buffer_unref (buffer);
536 GST_DEBUG_OBJECT (baseaudiopayload, "Pushing buffer %p", outbuf);
537 ret = gst_basertppayload_push (basepayload, outbuf);
544 * gst_base_rtp_audio_payload_flush:
545 * @baseaudiopayload: a #GstBaseRTPPayload
546 * @payload_len: length of payload
547 * @timestamp: a #GstClockTime
549 * Create an RTP buffer and store @payload_len bytes of the adapter as the
550 * payload. Set the timestamp on the new buffer to @timestamp before pushing
551 * the buffer downstream.
553 * If @payload_len is -1, all pending bytes will be flushed. If @timestamp is
554 * -1, the timestamp will be calculated automatically.
556 * Returns: a #GstFlowReturn
561 gst_base_rtp_audio_payload_flush (GstBaseRTPAudioPayload * baseaudiopayload,
562 guint payload_len, GstClockTime timestamp)
564 GstBaseRTPPayload *basepayload;
565 GstBaseRTPAudioPayloadPrivate *priv;
572 priv = baseaudiopayload->priv;
573 adapter = priv->adapter;
575 basepayload = GST_BASE_RTP_PAYLOAD (baseaudiopayload);
577 if (payload_len == -1)
578 payload_len = gst_adapter_available (adapter);
580 /* nothing to do, just return */
581 if (payload_len == 0)
584 if (timestamp == -1) {
585 /* calculate the timestamp */
586 timestamp = gst_adapter_prev_timestamp (adapter, &distance);
588 GST_LOG_OBJECT (baseaudiopayload,
589 "last timestamp %" GST_TIME_FORMAT ", distance %" G_GUINT64_FORMAT,
590 GST_TIME_ARGS (timestamp), distance);
592 if (GST_CLOCK_TIME_IS_VALID (timestamp) && distance > 0) {
593 /* convert the number of bytes since the last timestamp to time and add to
594 * the last seen timestamp */
595 timestamp += priv->bytes_to_time (baseaudiopayload, distance);
599 GST_DEBUG_OBJECT (baseaudiopayload, "Pushing %d bytes ts %" GST_TIME_FORMAT,
600 payload_len, GST_TIME_ARGS (timestamp));
602 if (priv->buffer_list && gst_adapter_available_fast (adapter) >= payload_len) {
604 /* we can quickly take a buffer out of the adapter without having to copy
606 buffer = gst_adapter_take_buffer (adapter, payload_len);
609 gst_base_rtp_audio_payload_push_buffer (baseaudiopayload, buffer,
612 /* create buffer to hold the payload */
613 outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
616 payload = gst_rtp_buffer_get_payload (outbuf);
617 gst_adapter_copy (adapter, payload, 0, payload_len);
618 gst_adapter_flush (adapter, payload_len);
621 gst_base_rtp_audio_payload_set_meta (baseaudiopayload, outbuf, payload_len,
624 ret = gst_basertppayload_push (basepayload, outbuf);
630 #define ALIGN_DOWN(val,len) ((val) - ((val) % (len)))
632 /* calculate the min and max length of a packet. This depends on the configured
633 * mtu and min/max_ptime values. We cache those so that we don't have to redo
634 * all the calculations */
636 gst_base_rtp_audio_payload_get_lengths (GstBaseRTPPayload *
637 basepayload, guint * min_payload_len, guint * max_payload_len,
640 GstBaseRTPAudioPayload *payload;
641 GstBaseRTPAudioPayloadPrivate *priv;
643 guint maxptime_octets;
644 guint minptime_octets;
645 guint ptime_mult_octets;
647 payload = GST_BASE_RTP_AUDIO_PAYLOAD_CAST (basepayload);
648 priv = payload->priv;
650 if (priv->align == 0)
653 mtu = GST_BASE_RTP_PAYLOAD_MTU (payload);
655 /* check cached values */
656 if (G_LIKELY (priv->cached_mtu == mtu
657 && priv->cached_ptime_multiple ==
658 basepayload->abidata.ABI.ptime_multiple
659 && priv->cached_ptime == basepayload->abidata.ABI.ptime
660 && priv->cached_max_ptime == basepayload->max_ptime
661 && priv->cached_min_ptime == basepayload->min_ptime)) {
662 /* if nothing changed, return cached values */
663 *min_payload_len = priv->cached_min_length;
664 *max_payload_len = priv->cached_max_length;
665 *align = priv->cached_align;
669 ptime_mult_octets = priv->time_to_bytes (payload,
670 basepayload->abidata.ABI.ptime_multiple);
671 *align = ALIGN_DOWN (MAX (priv->align, ptime_mult_octets), priv->align);
674 if (basepayload->max_ptime != -1) {
675 maxptime_octets = priv->time_to_bytes (payload, basepayload->max_ptime);
677 maxptime_octets = G_MAXUINT;
680 max_mtu = gst_rtp_buffer_calc_payload_len (mtu, 0, 0);
681 /* round down to alignment */
682 max_mtu = ALIGN_DOWN (max_mtu, *align);
684 /* combine max ptime and max payload length */
685 *max_payload_len = MIN (max_mtu, maxptime_octets);
687 /* min number of bytes based on a given ptime */
688 minptime_octets = priv->time_to_bytes (payload, basepayload->min_ptime);
689 /* must be at least one frame size */
690 *min_payload_len = MAX (minptime_octets, *align);
692 if (*min_payload_len > *max_payload_len)
693 *min_payload_len = *max_payload_len;
695 /* If the ptime is specified in the caps, tried to adhere to it exactly */
696 if (basepayload->abidata.ABI.ptime) {
697 guint ptime_in_bytes = priv->time_to_bytes (payload,
698 basepayload->abidata.ABI.ptime);
700 /* clip to computed min and max lengths */
701 ptime_in_bytes = MAX (*min_payload_len, ptime_in_bytes);
702 ptime_in_bytes = MIN (*max_payload_len, ptime_in_bytes);
704 *min_payload_len = *max_payload_len = ptime_in_bytes;
708 priv->cached_mtu = mtu;
709 priv->cached_ptime = basepayload->abidata.ABI.ptime;
710 priv->cached_min_ptime = basepayload->min_ptime;
711 priv->cached_max_ptime = basepayload->max_ptime;
712 priv->cached_ptime_multiple = basepayload->abidata.ABI.ptime_multiple;
713 priv->cached_min_length = *min_payload_len;
714 priv->cached_max_length = *max_payload_len;
715 priv->cached_align = *align;
720 /* frame conversions functions */
722 gst_base_rtp_audio_payload_frame_bytes_to_time (GstBaseRTPAudioPayload *
723 payload, guint64 bytes)
727 framecount = bytes / payload->frame_size;
728 if (G_UNLIKELY (bytes % payload->frame_size))
731 return framecount * payload->priv->frame_duration_ns;
735 gst_base_rtp_audio_payload_frame_bytes_to_rtptime (GstBaseRTPAudioPayload *
736 payload, guint64 bytes)
741 framecount = bytes / payload->frame_size;
742 if (G_UNLIKELY (bytes % payload->frame_size))
745 time = framecount * payload->priv->frame_duration_ns;
747 return gst_util_uint64_scale_int (time,
748 GST_BASE_RTP_PAYLOAD (payload)->clock_rate, GST_SECOND);
752 gst_base_rtp_audio_payload_frame_time_to_bytes (GstBaseRTPAudioPayload *
753 payload, GstClockTime time)
755 return gst_util_uint64_scale (time, payload->frame_size,
756 payload->priv->frame_duration_ns);
759 /* sample conversion functions */
761 gst_base_rtp_audio_payload_sample_bytes_to_time (GstBaseRTPAudioPayload *
762 payload, guint64 bytes)
766 /* avoid division when we can */
767 if (G_LIKELY (payload->sample_size != 8))
768 rtptime = gst_util_uint64_scale_int (bytes, 8, payload->sample_size);
772 return gst_util_uint64_scale_int (rtptime, GST_SECOND,
773 GST_BASE_RTP_PAYLOAD (payload)->clock_rate);
777 gst_base_rtp_audio_payload_sample_bytes_to_rtptime (GstBaseRTPAudioPayload *
778 payload, guint64 bytes)
780 /* avoid division when we can */
781 if (G_LIKELY (payload->sample_size != 8))
782 return gst_util_uint64_scale_int (bytes, 8, payload->sample_size);
788 gst_base_rtp_audio_payload_sample_time_to_bytes (GstBaseRTPAudioPayload *
789 payload, guint64 time)
793 samples = gst_util_uint64_scale_int (time,
794 GST_BASE_RTP_PAYLOAD (payload)->clock_rate, GST_SECOND);
796 /* avoid multiplication when we can */
797 if (G_LIKELY (payload->sample_size != 8))
798 return gst_util_uint64_scale_int (samples, payload->sample_size, 8);
804 gst_base_rtp_audio_payload_handle_buffer (GstBaseRTPPayload *
805 basepayload, GstBuffer * buffer)
807 GstBaseRTPAudioPayload *payload;
808 GstBaseRTPAudioPayloadPrivate *priv;
812 guint min_payload_len;
813 guint max_payload_len;
817 GstClockTime timestamp;
821 payload = GST_BASE_RTP_AUDIO_PAYLOAD_CAST (basepayload);
822 priv = payload->priv;
824 timestamp = GST_BUFFER_TIMESTAMP (buffer);
825 discont = GST_BUFFER_IS_DISCONT (buffer);
828 GST_DEBUG_OBJECT (payload, "Got DISCONT");
829 /* flush everything out of the adapter, mark DISCONT */
830 ret = gst_base_rtp_audio_payload_flush (payload, -1, -1);
831 priv->discont = TRUE;
833 /* get the distance between the timestamp gap and produce the same gap in
834 * the RTP timestamps */
835 if (priv->last_timestamp != -1 && timestamp != -1) {
836 /* we had a last timestamp, compare it to the new timestamp and update the
837 * offset counter for RTP timestamps. The effect is that we will produce
838 * output buffers containing the same RTP timestamp gap as the gap
839 * between the GST timestamps. */
840 if (timestamp > priv->last_timestamp) {
843 /* we're only going to apply a positive gap, otherwise we let the marker
844 * bit do its thing. simply convert to bytes and add the the current
846 diff = timestamp - priv->last_timestamp;
847 bytes = priv->time_to_bytes (payload, diff);
848 priv->offset += bytes;
850 GST_DEBUG_OBJECT (payload,
851 "elapsed time %" GST_TIME_FORMAT ", bytes %" G_GUINT64_FORMAT
852 ", new offset %" G_GUINT64_FORMAT, GST_TIME_ARGS (diff), bytes,
858 if (!gst_base_rtp_audio_payload_get_lengths (basepayload, &min_payload_len,
859 &max_payload_len, &align))
862 GST_DEBUG_OBJECT (payload,
863 "Calculated min_payload_len %u and max_payload_len %u",
864 min_payload_len, max_payload_len);
866 size = GST_BUFFER_SIZE (buffer);
868 /* shortcut, we don't need to use the adapter when the packet can be pushed
869 * through directly. */
870 available = gst_adapter_available (priv->adapter);
872 GST_DEBUG_OBJECT (payload, "got buffer size %u, available %u",
875 if (available == 0 && (size >= min_payload_len && size <= max_payload_len) &&
876 (size % align == 0)) {
877 /* If buffer fits on an RTP packet, let's just push it through
878 * this will check against max_ptime and max_mtu */
879 GST_DEBUG_OBJECT (payload, "Fast packet push");
880 ret = gst_base_rtp_audio_payload_push_buffer (payload, buffer, timestamp);
882 /* push the buffer in the adapter */
883 gst_adapter_push (priv->adapter, buffer);
886 GST_DEBUG_OBJECT (payload, "available now %u", available);
888 /* as long as we have full frames */
889 while (available >= min_payload_len) {
890 /* get multiple of alignment */
891 payload_len = MIN (max_payload_len, available);
892 payload_len = ALIGN_DOWN (payload_len, align);
894 /* and flush out the bytes from the adapter, automatically set the
896 ret = gst_base_rtp_audio_payload_flush (payload, payload_len, -1);
898 available -= payload_len;
899 GST_DEBUG_OBJECT (payload, "available after push %u", available);
907 GST_ELEMENT_ERROR (payload, STREAM, NOT_IMPLEMENTED, (NULL),
908 ("subclass did not configure us properly"));
909 gst_buffer_unref (buffer);
910 return GST_FLOW_ERROR;
914 static GstStateChangeReturn
915 gst_base_rtp_payload_audio_change_state (GstElement * element,
916 GstStateChange transition)
918 GstBaseRTPAudioPayload *basertppayload;
919 GstStateChangeReturn ret;
921 basertppayload = GST_BASE_RTP_AUDIO_PAYLOAD (element);
923 switch (transition) {
924 case GST_STATE_CHANGE_READY_TO_PAUSED:
925 basertppayload->priv->cached_mtu = -1;
926 basertppayload->priv->last_rtptime = -1;
927 basertppayload->priv->last_timestamp = -1;
933 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
935 switch (transition) {
936 case GST_STATE_CHANGE_PAUSED_TO_READY:
937 gst_adapter_clear (basertppayload->priv->adapter);
947 gst_base_rtp_payload_audio_handle_event (GstPad * pad, GstEvent * event)
949 GstBaseRTPAudioPayload *payload;
950 gboolean res = FALSE;
952 payload = GST_BASE_RTP_AUDIO_PAYLOAD (gst_pad_get_parent (pad));
954 switch (GST_EVENT_TYPE (event)) {
956 /* flush remaining bytes in the adapter */
957 gst_base_rtp_audio_payload_flush (payload, -1, -1);
959 case GST_EVENT_FLUSH_STOP:
960 gst_adapter_clear (payload->priv->adapter);
966 gst_object_unref (payload);
968 /* return FALSE to let parent handle the remainder of the event */
973 * gst_base_rtp_audio_payload_get_adapter:
974 * @basertpaudiopayload: a #GstBaseRTPAudioPayload
976 * Gets the internal adapter used by the depayloader.
978 * Returns: a #GstAdapter.
983 gst_base_rtp_audio_payload_get_adapter (GstBaseRTPAudioPayload
984 * basertpaudiopayload)
988 if ((adapter = basertpaudiopayload->priv->adapter))
989 g_object_ref (adapter);