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 #define gst_base_rtp_audio_payload_parent_class parent_class
163 G_DEFINE_TYPE (GstBaseRTPAudioPayload, gst_base_rtp_audio_payload,
164 GST_TYPE_BASE_RTP_PAYLOAD);
167 gst_base_rtp_audio_payload_class_init (GstBaseRTPAudioPayloadClass * klass)
169 GObjectClass *gobject_class;
170 GstElementClass *gstelement_class;
171 GstBaseRTPPayloadClass *gstbasertppayload_class;
173 g_type_class_add_private (klass, sizeof (GstBaseRTPAudioPayloadPrivate));
175 gobject_class = (GObjectClass *) klass;
176 gstelement_class = (GstElementClass *) klass;
177 gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
179 gobject_class->finalize = gst_base_rtp_audio_payload_finalize;
180 gobject_class->set_property = gst_base_rtp_audio_payload_set_property;
181 gobject_class->get_property = gst_base_rtp_audio_payload_get_property;
183 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BUFFER_LIST,
184 g_param_spec_boolean ("buffer-list", "Buffer List",
186 DEFAULT_BUFFER_LIST, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
188 gstelement_class->change_state =
189 GST_DEBUG_FUNCPTR (gst_base_rtp_payload_audio_change_state);
191 gstbasertppayload_class->handle_buffer =
192 GST_DEBUG_FUNCPTR (gst_base_rtp_audio_payload_handle_buffer);
193 gstbasertppayload_class->handle_event =
194 GST_DEBUG_FUNCPTR (gst_base_rtp_payload_audio_handle_event);
196 GST_DEBUG_CATEGORY_INIT (basertpaudiopayload_debug, "basertpaudiopayload", 0,
197 "base audio RTP payloader");
201 gst_base_rtp_audio_payload_init (GstBaseRTPAudioPayload * payload)
203 payload->priv = GST_BASE_RTP_AUDIO_PAYLOAD_GET_PRIVATE (payload);
205 /* these need to be set by child object if frame based */
206 payload->frame_size = 0;
207 payload->frame_duration = 0;
209 /* these need to be set by child object if sample based */
210 payload->sample_size = 0;
212 payload->priv->adapter = gst_adapter_new ();
214 payload->priv->buffer_list = DEFAULT_BUFFER_LIST;
218 gst_base_rtp_audio_payload_finalize (GObject * object)
220 GstBaseRTPAudioPayload *payload;
222 payload = GST_BASE_RTP_AUDIO_PAYLOAD (object);
224 g_object_unref (payload->priv->adapter);
226 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
230 gst_base_rtp_audio_payload_set_property (GObject * object,
231 guint prop_id, const GValue * value, GParamSpec * pspec)
233 GstBaseRTPAudioPayload *payload;
235 payload = GST_BASE_RTP_AUDIO_PAYLOAD (object);
238 case PROP_BUFFER_LIST:
239 payload->priv->buffer_list = g_value_get_boolean (value);
242 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
248 gst_base_rtp_audio_payload_get_property (GObject * object,
249 guint prop_id, GValue * value, GParamSpec * pspec)
251 GstBaseRTPAudioPayload *payload;
253 payload = GST_BASE_RTP_AUDIO_PAYLOAD (object);
256 case PROP_BUFFER_LIST:
257 g_value_set_boolean (value, payload->priv->buffer_list);
260 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
266 * gst_base_rtp_audio_payload_set_frame_based:
267 * @basertpaudiopayload: a pointer to the element.
269 * Tells #GstBaseRTPAudioPayload that the child element is for a frame based
273 gst_base_rtp_audio_payload_set_frame_based (GstBaseRTPAudioPayload *
276 g_return_if_fail (basertpaudiopayload != NULL);
277 g_return_if_fail (basertpaudiopayload->priv->time_to_bytes == NULL);
278 g_return_if_fail (basertpaudiopayload->priv->bytes_to_time == NULL);
279 g_return_if_fail (basertpaudiopayload->priv->bytes_to_rtptime == NULL);
281 basertpaudiopayload->priv->bytes_to_time =
282 gst_base_rtp_audio_payload_frame_bytes_to_time;
283 basertpaudiopayload->priv->bytes_to_rtptime =
284 gst_base_rtp_audio_payload_frame_bytes_to_rtptime;
285 basertpaudiopayload->priv->time_to_bytes =
286 gst_base_rtp_audio_payload_frame_time_to_bytes;
290 * gst_base_rtp_audio_payload_set_sample_based:
291 * @basertpaudiopayload: a pointer to the element.
293 * Tells #GstBaseRTPAudioPayload that the child element is for a sample based
297 gst_base_rtp_audio_payload_set_sample_based (GstBaseRTPAudioPayload *
300 g_return_if_fail (basertpaudiopayload != NULL);
301 g_return_if_fail (basertpaudiopayload->priv->time_to_bytes == NULL);
302 g_return_if_fail (basertpaudiopayload->priv->bytes_to_time == NULL);
303 g_return_if_fail (basertpaudiopayload->priv->bytes_to_rtptime == NULL);
305 basertpaudiopayload->priv->bytes_to_time =
306 gst_base_rtp_audio_payload_sample_bytes_to_time;
307 basertpaudiopayload->priv->bytes_to_rtptime =
308 gst_base_rtp_audio_payload_sample_bytes_to_rtptime;
309 basertpaudiopayload->priv->time_to_bytes =
310 gst_base_rtp_audio_payload_sample_time_to_bytes;
314 * gst_base_rtp_audio_payload_set_frame_options:
315 * @basertpaudiopayload: a pointer to the element.
316 * @frame_duration: The duraction of an audio frame in milliseconds.
317 * @frame_size: The size of an audio frame in bytes.
319 * Sets the options for frame based audio codecs.
323 gst_base_rtp_audio_payload_set_frame_options (GstBaseRTPAudioPayload
324 * basertpaudiopayload, gint frame_duration, gint frame_size)
326 GstBaseRTPAudioPayloadPrivate *priv;
328 g_return_if_fail (basertpaudiopayload != NULL);
330 priv = basertpaudiopayload->priv;
332 basertpaudiopayload->frame_duration = frame_duration;
333 priv->frame_duration_ns = frame_duration * GST_MSECOND;
334 basertpaudiopayload->frame_size = frame_size;
335 priv->align = frame_size;
337 gst_adapter_clear (priv->adapter);
339 GST_DEBUG_OBJECT (basertpaudiopayload, "frame set to %d ms and size %d",
340 frame_duration, frame_size);
344 * gst_base_rtp_audio_payload_set_sample_options:
345 * @basertpaudiopayload: a pointer to the element.
346 * @sample_size: Size per sample in bytes.
348 * Sets the options for sample based audio codecs.
351 gst_base_rtp_audio_payload_set_sample_options (GstBaseRTPAudioPayload
352 * basertpaudiopayload, gint sample_size)
354 g_return_if_fail (basertpaudiopayload != NULL);
356 /* sample_size is in bits internally */
357 gst_base_rtp_audio_payload_set_samplebits_options (basertpaudiopayload,
362 * gst_base_rtp_audio_payload_set_samplebits_options:
363 * @basertpaudiopayload: a pointer to the element.
364 * @sample_size: Size per sample in bits.
366 * Sets the options for sample based audio codecs.
371 gst_base_rtp_audio_payload_set_samplebits_options (GstBaseRTPAudioPayload
372 * basertpaudiopayload, gint sample_size)
375 GstBaseRTPAudioPayloadPrivate *priv;
377 g_return_if_fail (basertpaudiopayload != NULL);
379 priv = basertpaudiopayload->priv;
381 basertpaudiopayload->sample_size = sample_size;
383 /* sample_size is in bits and is converted into multiple bytes */
384 fragment_size = sample_size;
385 while ((fragment_size % 8) != 0)
386 fragment_size += fragment_size;
387 priv->fragment_size = fragment_size / 8;
388 priv->align = priv->fragment_size;
390 gst_adapter_clear (priv->adapter);
392 GST_DEBUG_OBJECT (basertpaudiopayload,
393 "Samplebits set to sample size %d bits", sample_size);
397 gst_base_rtp_audio_payload_set_meta (GstBaseRTPAudioPayload * payload,
398 GstBuffer * buffer, guint payload_len, GstClockTime timestamp)
400 GstBaseRTPPayload *basepayload;
401 GstBaseRTPAudioPayloadPrivate *priv;
404 basepayload = GST_BASE_RTP_PAYLOAD_CAST (payload);
405 priv = payload->priv;
407 /* set payload type */
408 gst_rtp_buffer_map (buffer, GST_MAP_WRITE, &rtp);
409 gst_rtp_buffer_set_payload_type (&rtp, basepayload->pt);
410 /* set marker bit for disconts */
412 GST_DEBUG_OBJECT (payload, "Setting marker and DISCONT");
413 gst_rtp_buffer_set_marker (&rtp, TRUE);
414 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
415 priv->discont = FALSE;
417 gst_rtp_buffer_unmap (&rtp);
419 GST_BUFFER_TIMESTAMP (buffer) = timestamp;
421 /* get the offset in RTP time */
422 GST_BUFFER_OFFSET (buffer) = priv->bytes_to_rtptime (payload, priv->offset);
424 priv->offset += payload_len;
426 /* Set the duration from the size */
427 GST_BUFFER_DURATION (buffer) = priv->bytes_to_time (payload, payload_len);
429 /* remember the last rtptime/timestamp pair. We will use this to realign our
430 * RTP timestamp after a buffer discont */
431 priv->last_rtptime = GST_BUFFER_OFFSET (buffer);
432 priv->last_timestamp = timestamp;
436 * gst_base_rtp_audio_payload_push:
437 * @baseaudiopayload: a #GstBaseRTPPayload
438 * @data: data to set as payload
439 * @payload_len: length of payload
440 * @timestamp: a #GstClockTime
442 * Create an RTP buffer and store @payload_len bytes of @data as the
443 * payload. Set the timestamp on the new buffer to @timestamp before pushing
444 * the buffer downstream.
446 * Returns: a #GstFlowReturn
451 gst_base_rtp_audio_payload_push (GstBaseRTPAudioPayload * baseaudiopayload,
452 const guint8 * data, guint payload_len, GstClockTime timestamp)
454 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 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
470 payload = gst_rtp_buffer_get_payload (&rtp);
471 memcpy (payload, data, payload_len);
472 gst_rtp_buffer_unmap (&rtp);
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_get_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) {
518 list = gst_buffer_list_new ();
519 len = gst_buffer_list_len (list);
521 for (i = 0; i < len; i++) {
523 g_warning ("bufferlist not implemented");
524 gst_buffer_list_add (list, outbuf);
525 gst_buffer_list_add (list, buffer);
528 GST_DEBUG_OBJECT (baseaudiopayload, "Pushing list %p", list);
529 ret = gst_basertppayload_push_list (basepayload, list);
534 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
535 payload = gst_rtp_buffer_get_payload (&rtp);
536 gst_buffer_extract (buffer, 0, payload, payload_len);
537 gst_rtp_buffer_unmap (&rtp);
539 gst_buffer_unref (buffer);
541 GST_DEBUG_OBJECT (baseaudiopayload, "Pushing buffer %p", outbuf);
542 ret = gst_basertppayload_push (basepayload, outbuf);
549 * gst_base_rtp_audio_payload_flush:
550 * @baseaudiopayload: a #GstBaseRTPPayload
551 * @payload_len: length of payload
552 * @timestamp: a #GstClockTime
554 * Create an RTP buffer and store @payload_len bytes of the adapter as the
555 * payload. Set the timestamp on the new buffer to @timestamp before pushing
556 * the buffer downstream.
558 * If @payload_len is -1, all pending bytes will be flushed. If @timestamp is
559 * -1, the timestamp will be calculated automatically.
561 * Returns: a #GstFlowReturn
566 gst_base_rtp_audio_payload_flush (GstBaseRTPAudioPayload * baseaudiopayload,
567 guint payload_len, GstClockTime timestamp)
569 GstBaseRTPPayload *basepayload;
570 GstBaseRTPAudioPayloadPrivate *priv;
577 priv = baseaudiopayload->priv;
578 adapter = priv->adapter;
580 basepayload = GST_BASE_RTP_PAYLOAD (baseaudiopayload);
582 if (payload_len == -1)
583 payload_len = gst_adapter_available (adapter);
585 /* nothing to do, just return */
586 if (payload_len == 0)
589 if (timestamp == -1) {
590 /* calculate the timestamp */
591 timestamp = gst_adapter_prev_timestamp (adapter, &distance);
593 GST_LOG_OBJECT (baseaudiopayload,
594 "last timestamp %" GST_TIME_FORMAT ", distance %" G_GUINT64_FORMAT,
595 GST_TIME_ARGS (timestamp), distance);
597 if (GST_CLOCK_TIME_IS_VALID (timestamp) && distance > 0) {
598 /* convert the number of bytes since the last timestamp to time and add to
599 * the last seen timestamp */
600 timestamp += priv->bytes_to_time (baseaudiopayload, distance);
604 GST_DEBUG_OBJECT (baseaudiopayload, "Pushing %d bytes ts %" GST_TIME_FORMAT,
605 payload_len, GST_TIME_ARGS (timestamp));
607 if (priv->buffer_list && gst_adapter_available_fast (adapter) >= payload_len) {
609 /* we can quickly take a buffer out of the adapter without having to copy
611 buffer = gst_adapter_take_buffer (adapter, payload_len);
614 gst_base_rtp_audio_payload_push_buffer (baseaudiopayload, buffer,
619 /* create buffer to hold the payload */
620 outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
623 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
624 payload = gst_rtp_buffer_get_payload (&rtp);
625 gst_adapter_copy (adapter, payload, 0, payload_len);
626 gst_adapter_flush (adapter, payload_len);
627 gst_rtp_buffer_unmap (&rtp);
630 gst_base_rtp_audio_payload_set_meta (baseaudiopayload, outbuf, payload_len,
633 ret = gst_basertppayload_push (basepayload, outbuf);
639 #define ALIGN_DOWN(val,len) ((val) - ((val) % (len)))
641 /* calculate the min and max length of a packet. This depends on the configured
642 * mtu and min/max_ptime values. We cache those so that we don't have to redo
643 * all the calculations */
645 gst_base_rtp_audio_payload_get_lengths (GstBaseRTPPayload *
646 basepayload, guint * min_payload_len, guint * max_payload_len,
649 GstBaseRTPAudioPayload *payload;
650 GstBaseRTPAudioPayloadPrivate *priv;
652 guint maxptime_octets;
653 guint minptime_octets;
654 guint ptime_mult_octets;
656 payload = GST_BASE_RTP_AUDIO_PAYLOAD_CAST (basepayload);
657 priv = payload->priv;
659 if (priv->align == 0)
662 mtu = GST_BASE_RTP_PAYLOAD_MTU (payload);
664 /* check cached values */
665 if (G_LIKELY (priv->cached_mtu == mtu
666 && priv->cached_ptime_multiple ==
667 basepayload->abidata.ABI.ptime_multiple
668 && priv->cached_ptime == basepayload->abidata.ABI.ptime
669 && priv->cached_max_ptime == basepayload->max_ptime
670 && priv->cached_min_ptime == basepayload->min_ptime)) {
671 /* if nothing changed, return cached values */
672 *min_payload_len = priv->cached_min_length;
673 *max_payload_len = priv->cached_max_length;
674 *align = priv->cached_align;
678 ptime_mult_octets = priv->time_to_bytes (payload,
679 basepayload->abidata.ABI.ptime_multiple);
680 *align = ALIGN_DOWN (MAX (priv->align, ptime_mult_octets), priv->align);
683 if (basepayload->max_ptime != -1) {
684 maxptime_octets = priv->time_to_bytes (payload, basepayload->max_ptime);
686 maxptime_octets = G_MAXUINT;
689 max_mtu = gst_rtp_buffer_calc_payload_len (mtu, 0, 0);
690 /* round down to alignment */
691 max_mtu = ALIGN_DOWN (max_mtu, *align);
693 /* combine max ptime and max payload length */
694 *max_payload_len = MIN (max_mtu, maxptime_octets);
696 /* min number of bytes based on a given ptime */
697 minptime_octets = priv->time_to_bytes (payload, basepayload->min_ptime);
698 /* must be at least one frame size */
699 *min_payload_len = MAX (minptime_octets, *align);
701 if (*min_payload_len > *max_payload_len)
702 *min_payload_len = *max_payload_len;
704 /* If the ptime is specified in the caps, tried to adhere to it exactly */
705 if (basepayload->abidata.ABI.ptime) {
706 guint ptime_in_bytes = priv->time_to_bytes (payload,
707 basepayload->abidata.ABI.ptime);
709 /* clip to computed min and max lengths */
710 ptime_in_bytes = MAX (*min_payload_len, ptime_in_bytes);
711 ptime_in_bytes = MIN (*max_payload_len, ptime_in_bytes);
713 *min_payload_len = *max_payload_len = ptime_in_bytes;
717 priv->cached_mtu = mtu;
718 priv->cached_ptime = basepayload->abidata.ABI.ptime;
719 priv->cached_min_ptime = basepayload->min_ptime;
720 priv->cached_max_ptime = basepayload->max_ptime;
721 priv->cached_ptime_multiple = basepayload->abidata.ABI.ptime_multiple;
722 priv->cached_min_length = *min_payload_len;
723 priv->cached_max_length = *max_payload_len;
724 priv->cached_align = *align;
729 /* frame conversions functions */
731 gst_base_rtp_audio_payload_frame_bytes_to_time (GstBaseRTPAudioPayload *
732 payload, guint64 bytes)
736 framecount = bytes / payload->frame_size;
737 if (G_UNLIKELY (bytes % payload->frame_size))
740 return framecount * payload->priv->frame_duration_ns;
744 gst_base_rtp_audio_payload_frame_bytes_to_rtptime (GstBaseRTPAudioPayload *
745 payload, guint64 bytes)
750 framecount = bytes / payload->frame_size;
751 if (G_UNLIKELY (bytes % payload->frame_size))
754 time = framecount * payload->priv->frame_duration_ns;
756 return gst_util_uint64_scale_int (time,
757 GST_BASE_RTP_PAYLOAD (payload)->clock_rate, GST_SECOND);
761 gst_base_rtp_audio_payload_frame_time_to_bytes (GstBaseRTPAudioPayload *
762 payload, GstClockTime time)
764 return gst_util_uint64_scale (time, payload->frame_size,
765 payload->priv->frame_duration_ns);
768 /* sample conversion functions */
770 gst_base_rtp_audio_payload_sample_bytes_to_time (GstBaseRTPAudioPayload *
771 payload, guint64 bytes)
775 /* avoid division when we can */
776 if (G_LIKELY (payload->sample_size != 8))
777 rtptime = gst_util_uint64_scale_int (bytes, 8, payload->sample_size);
781 return gst_util_uint64_scale_int (rtptime, GST_SECOND,
782 GST_BASE_RTP_PAYLOAD (payload)->clock_rate);
786 gst_base_rtp_audio_payload_sample_bytes_to_rtptime (GstBaseRTPAudioPayload *
787 payload, guint64 bytes)
789 /* avoid division when we can */
790 if (G_LIKELY (payload->sample_size != 8))
791 return gst_util_uint64_scale_int (bytes, 8, payload->sample_size);
797 gst_base_rtp_audio_payload_sample_time_to_bytes (GstBaseRTPAudioPayload *
798 payload, guint64 time)
802 samples = gst_util_uint64_scale_int (time,
803 GST_BASE_RTP_PAYLOAD (payload)->clock_rate, GST_SECOND);
805 /* avoid multiplication when we can */
806 if (G_LIKELY (payload->sample_size != 8))
807 return gst_util_uint64_scale_int (samples, payload->sample_size, 8);
813 gst_base_rtp_audio_payload_handle_buffer (GstBaseRTPPayload *
814 basepayload, GstBuffer * buffer)
816 GstBaseRTPAudioPayload *payload;
817 GstBaseRTPAudioPayloadPrivate *priv;
821 guint min_payload_len;
822 guint max_payload_len;
826 GstClockTime timestamp;
830 payload = GST_BASE_RTP_AUDIO_PAYLOAD_CAST (basepayload);
831 priv = payload->priv;
833 timestamp = GST_BUFFER_TIMESTAMP (buffer);
834 discont = GST_BUFFER_IS_DISCONT (buffer);
837 GST_DEBUG_OBJECT (payload, "Got DISCONT");
838 /* flush everything out of the adapter, mark DISCONT */
839 ret = gst_base_rtp_audio_payload_flush (payload, -1, -1);
840 priv->discont = TRUE;
842 /* get the distance between the timestamp gap and produce the same gap in
843 * the RTP timestamps */
844 if (priv->last_timestamp != -1 && timestamp != -1) {
845 /* we had a last timestamp, compare it to the new timestamp and update the
846 * offset counter for RTP timestamps. The effect is that we will produce
847 * output buffers containing the same RTP timestamp gap as the gap
848 * between the GST timestamps. */
849 if (timestamp > priv->last_timestamp) {
852 /* we're only going to apply a positive gap, otherwise we let the marker
853 * bit do its thing. simply convert to bytes and add the the current
855 diff = timestamp - priv->last_timestamp;
856 bytes = priv->time_to_bytes (payload, diff);
857 priv->offset += bytes;
859 GST_DEBUG_OBJECT (payload,
860 "elapsed time %" GST_TIME_FORMAT ", bytes %" G_GUINT64_FORMAT
861 ", new offset %" G_GUINT64_FORMAT, GST_TIME_ARGS (diff), bytes,
867 if (!gst_base_rtp_audio_payload_get_lengths (basepayload, &min_payload_len,
868 &max_payload_len, &align))
871 GST_DEBUG_OBJECT (payload,
872 "Calculated min_payload_len %u and max_payload_len %u",
873 min_payload_len, max_payload_len);
875 size = gst_buffer_get_size (buffer);
877 /* shortcut, we don't need to use the adapter when the packet can be pushed
878 * through directly. */
879 available = gst_adapter_available (priv->adapter);
881 GST_DEBUG_OBJECT (payload, "got buffer size %u, available %u",
884 if (available == 0 && (size >= min_payload_len && size <= max_payload_len) &&
885 (size % align == 0)) {
886 /* If buffer fits on an RTP packet, let's just push it through
887 * this will check against max_ptime and max_mtu */
888 GST_DEBUG_OBJECT (payload, "Fast packet push");
889 ret = gst_base_rtp_audio_payload_push_buffer (payload, buffer, timestamp);
891 /* push the buffer in the adapter */
892 gst_adapter_push (priv->adapter, buffer);
895 GST_DEBUG_OBJECT (payload, "available now %u", available);
897 /* as long as we have full frames */
898 while (available >= min_payload_len) {
899 /* get multiple of alignment */
900 payload_len = MIN (max_payload_len, available);
901 payload_len = ALIGN_DOWN (payload_len, align);
903 /* and flush out the bytes from the adapter, automatically set the
905 ret = gst_base_rtp_audio_payload_flush (payload, payload_len, -1);
907 available -= payload_len;
908 GST_DEBUG_OBJECT (payload, "available after push %u", available);
916 GST_ELEMENT_ERROR (payload, STREAM, NOT_IMPLEMENTED, (NULL),
917 ("subclass did not configure us properly"));
918 gst_buffer_unref (buffer);
919 return GST_FLOW_ERROR;
923 static GstStateChangeReturn
924 gst_base_rtp_payload_audio_change_state (GstElement * element,
925 GstStateChange transition)
927 GstBaseRTPAudioPayload *basertppayload;
928 GstStateChangeReturn ret;
930 basertppayload = GST_BASE_RTP_AUDIO_PAYLOAD (element);
932 switch (transition) {
933 case GST_STATE_CHANGE_READY_TO_PAUSED:
934 basertppayload->priv->cached_mtu = -1;
935 basertppayload->priv->last_rtptime = -1;
936 basertppayload->priv->last_timestamp = -1;
942 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
944 switch (transition) {
945 case GST_STATE_CHANGE_PAUSED_TO_READY:
946 gst_adapter_clear (basertppayload->priv->adapter);
956 gst_base_rtp_payload_audio_handle_event (GstPad * pad, GstEvent * event)
958 GstBaseRTPAudioPayload *payload;
959 gboolean res = FALSE;
961 payload = GST_BASE_RTP_AUDIO_PAYLOAD (gst_pad_get_parent (pad));
963 switch (GST_EVENT_TYPE (event)) {
965 /* flush remaining bytes in the adapter */
966 gst_base_rtp_audio_payload_flush (payload, -1, -1);
968 case GST_EVENT_FLUSH_STOP:
969 gst_adapter_clear (payload->priv->adapter);
975 gst_object_unref (payload);
977 /* return FALSE to let parent handle the remainder of the event */
982 * gst_base_rtp_audio_payload_get_adapter:
983 * @basertpaudiopayload: a #GstBaseRTPAudioPayload
985 * Gets the internal adapter used by the depayloader.
987 * Returns: a #GstAdapter.
992 gst_base_rtp_audio_payload_get_adapter (GstBaseRTPAudioPayload
993 * basertpaudiopayload)
997 if ((adapter = basertpaudiopayload->priv->adapter))
998 g_object_ref (adapter);