tizen 2.3 release
[framework/multimedia/gst-plugins-base0.10.git] / gst-libs / gst / rtp / gstbasertpaudiopayload.c
1 /* GStreamer
2  * Copyright (C) <2006> Philippe Khalaf <philippe.kalaf@collabora.co.uk>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /**
21  * SECTION:gstbasertpaudiopayload
22  * @short_description: Base class for audio RTP payloader
23  *
24  * Provides a base class for audio RTP payloaders for frame or sample based
25  * audio codecs (constant bitrate)
26  *
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.
38  *
39  * <refsect2>
40  * <title>Usage</title>
41  * <para>
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.
53  * </para>
54  * </refsect2>
55  */
56
57 #ifdef HAVE_CONFIG_H
58 #include "config.h"
59 #endif
60
61 #include <stdlib.h>
62 #include <string.h>
63 #include <gst/rtp/gstrtpbuffer.h>
64 #include <gst/base/gstadapter.h>
65
66 #include "gstbasertpaudiopayload.h"
67
68 GST_DEBUG_CATEGORY_STATIC (basertpaudiopayload_debug);
69 #define GST_CAT_DEFAULT (basertpaudiopayload_debug)
70
71 #define DEFAULT_BUFFER_LIST             FALSE
72
73 enum
74 {
75   PROP_0,
76   PROP_BUFFER_LIST,
77   PROP_LAST
78 };
79
80 /* function to convert bytes to a time */
81 typedef GstClockTime (*GetBytesToTimeFunc) (GstBaseRTPAudioPayload * payload,
82     guint64 bytes);
83 /* function to convert bytes to a RTP time */
84 typedef guint32 (*GetBytesToRTPTimeFunc) (GstBaseRTPAudioPayload * payload,
85     guint64 bytes);
86 /* function to convert time to bytes */
87 typedef guint64 (*GetTimeToBytesFunc) (GstBaseRTPAudioPayload * payload,
88     GstClockTime time);
89
90 struct _GstBaseRTPAudioPayloadPrivate
91 {
92   GetBytesToTimeFunc bytes_to_time;
93   GetBytesToRTPTimeFunc bytes_to_rtptime;
94   GetTimeToBytesFunc time_to_bytes;
95
96   GstAdapter *adapter;
97   guint fragment_size;
98   GstClockTime frame_duration_ns;
99   gboolean discont;
100   guint64 offset;
101   GstClockTime last_timestamp;
102   guint32 last_rtptime;
103   guint align;
104
105   guint cached_mtu;
106   guint cached_min_ptime;
107   guint cached_max_ptime;
108   guint cached_ptime;
109   guint cached_min_length;
110   guint cached_max_length;
111   guint cached_ptime_multiple;
112   guint cached_align;
113
114   gboolean buffer_list;
115 };
116
117
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))
121
122 static void gst_base_rtp_audio_payload_finalize (GObject * object);
123
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);
128
129 /* bytes to time functions */
130 static GstClockTime
131 gst_base_rtp_audio_payload_frame_bytes_to_time (GstBaseRTPAudioPayload *
132     payload, guint64 bytes);
133 static GstClockTime
134 gst_base_rtp_audio_payload_sample_bytes_to_time (GstBaseRTPAudioPayload *
135     payload, guint64 bytes);
136
137 /* bytes to RTP time functions */
138 static guint32
139 gst_base_rtp_audio_payload_frame_bytes_to_rtptime (GstBaseRTPAudioPayload *
140     payload, guint64 bytes);
141 static guint32
142 gst_base_rtp_audio_payload_sample_bytes_to_rtptime (GstBaseRTPAudioPayload *
143     payload, guint64 bytes);
144
145 /* time to bytes functions */
146 static guint64
147 gst_base_rtp_audio_payload_frame_time_to_bytes (GstBaseRTPAudioPayload *
148     payload, GstClockTime time);
149 static guint64
150 gst_base_rtp_audio_payload_sample_time_to_bytes (GstBaseRTPAudioPayload *
151     payload, GstClockTime time);
152
153 static GstFlowReturn gst_base_rtp_audio_payload_handle_buffer (GstBaseRTPPayload
154     * payload, GstBuffer * buffer);
155
156 static GstStateChangeReturn gst_base_rtp_payload_audio_change_state (GstElement
157     * element, GstStateChange transition);
158
159 static gboolean gst_base_rtp_payload_audio_handle_event (GstPad * pad,
160     GstEvent * event);
161
162 GST_BOILERPLATE (GstBaseRTPAudioPayload, gst_base_rtp_audio_payload,
163     GstBaseRTPPayload, GST_TYPE_BASE_RTP_PAYLOAD);
164
165 static void
166 gst_base_rtp_audio_payload_base_init (gpointer klass)
167 {
168 }
169
170 static void
171 gst_base_rtp_audio_payload_class_init (GstBaseRTPAudioPayloadClass * klass)
172 {
173   GObjectClass *gobject_class;
174   GstElementClass *gstelement_class;
175   GstBaseRTPPayloadClass *gstbasertppayload_class;
176
177   g_type_class_add_private (klass, sizeof (GstBaseRTPAudioPayloadPrivate));
178
179   gobject_class = (GObjectClass *) klass;
180   gstelement_class = (GstElementClass *) klass;
181   gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
182
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;
186
187   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BUFFER_LIST,
188       g_param_spec_boolean ("buffer-list", "Buffer List",
189           "Use Buffer Lists",
190           DEFAULT_BUFFER_LIST, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
191
192   gstelement_class->change_state =
193       GST_DEBUG_FUNCPTR (gst_base_rtp_payload_audio_change_state);
194
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);
199
200   GST_DEBUG_CATEGORY_INIT (basertpaudiopayload_debug, "basertpaudiopayload", 0,
201       "base audio RTP payloader");
202 }
203
204 static void
205 gst_base_rtp_audio_payload_init (GstBaseRTPAudioPayload * payload,
206     GstBaseRTPAudioPayloadClass * klass)
207 {
208   payload->priv = GST_BASE_RTP_AUDIO_PAYLOAD_GET_PRIVATE (payload);
209
210   /* these need to be set by child object if frame based */
211   payload->frame_size = 0;
212   payload->frame_duration = 0;
213
214   /* these need to be set by child object if sample based */
215   payload->sample_size = 0;
216
217   payload->priv->adapter = gst_adapter_new ();
218
219   payload->priv->buffer_list = DEFAULT_BUFFER_LIST;
220 }
221
222 static void
223 gst_base_rtp_audio_payload_finalize (GObject * object)
224 {
225   GstBaseRTPAudioPayload *payload;
226
227   payload = GST_BASE_RTP_AUDIO_PAYLOAD (object);
228
229   g_object_unref (payload->priv->adapter);
230
231   GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
232 }
233
234 static void
235 gst_base_rtp_audio_payload_set_property (GObject * object,
236     guint prop_id, const GValue * value, GParamSpec * pspec)
237 {
238   GstBaseRTPAudioPayload *payload;
239
240   payload = GST_BASE_RTP_AUDIO_PAYLOAD (object);
241
242   switch (prop_id) {
243     case PROP_BUFFER_LIST:
244       payload->priv->buffer_list = g_value_get_boolean (value);
245       break;
246     default:
247       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
248       break;
249   }
250 }
251
252 static void
253 gst_base_rtp_audio_payload_get_property (GObject * object,
254     guint prop_id, GValue * value, GParamSpec * pspec)
255 {
256   GstBaseRTPAudioPayload *payload;
257
258   payload = GST_BASE_RTP_AUDIO_PAYLOAD (object);
259
260   switch (prop_id) {
261     case PROP_BUFFER_LIST:
262       g_value_set_boolean (value, payload->priv->buffer_list);
263       break;
264     default:
265       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
266       break;
267   }
268 }
269
270 /**
271  * gst_base_rtp_audio_payload_set_frame_based:
272  * @basertpaudiopayload: a pointer to the element.
273  *
274  * Tells #GstBaseRTPAudioPayload that the child element is for a frame based
275  * audio codec
276  */
277 void
278 gst_base_rtp_audio_payload_set_frame_based (GstBaseRTPAudioPayload *
279     basertpaudiopayload)
280 {
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);
285
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;
292 }
293
294 /**
295  * gst_base_rtp_audio_payload_set_sample_based:
296  * @basertpaudiopayload: a pointer to the element.
297  *
298  * Tells #GstBaseRTPAudioPayload that the child element is for a sample based
299  * audio codec
300  */
301 void
302 gst_base_rtp_audio_payload_set_sample_based (GstBaseRTPAudioPayload *
303     basertpaudiopayload)
304 {
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);
309
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;
316 }
317
318 /**
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.
323  *
324  * Sets the options for frame based audio codecs.
325  *
326  */
327 void
328 gst_base_rtp_audio_payload_set_frame_options (GstBaseRTPAudioPayload
329     * basertpaudiopayload, gint frame_duration, gint frame_size)
330 {
331   GstBaseRTPAudioPayloadPrivate *priv;
332
333   g_return_if_fail (basertpaudiopayload != NULL);
334
335   priv = basertpaudiopayload->priv;
336
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;
341
342   gst_adapter_clear (priv->adapter);
343
344   GST_DEBUG_OBJECT (basertpaudiopayload, "frame set to %d ms and size %d",
345       frame_duration, frame_size);
346 }
347
348 /**
349  * gst_base_rtp_audio_payload_set_sample_options:
350  * @basertpaudiopayload: a pointer to the element.
351  * @sample_size: Size per sample in bytes.
352  *
353  * Sets the options for sample based audio codecs.
354  */
355 void
356 gst_base_rtp_audio_payload_set_sample_options (GstBaseRTPAudioPayload
357     * basertpaudiopayload, gint sample_size)
358 {
359   g_return_if_fail (basertpaudiopayload != NULL);
360
361   /* sample_size is in bits internally */
362   gst_base_rtp_audio_payload_set_samplebits_options (basertpaudiopayload,
363       sample_size * 8);
364 }
365
366 /**
367  * gst_base_rtp_audio_payload_set_samplebits_options:
368  * @basertpaudiopayload: a pointer to the element.
369  * @sample_size: Size per sample in bits.
370  *
371  * Sets the options for sample based audio codecs.
372  *
373  * Since: 0.10.18
374  */
375 void
376 gst_base_rtp_audio_payload_set_samplebits_options (GstBaseRTPAudioPayload
377     * basertpaudiopayload, gint sample_size)
378 {
379   guint fragment_size;
380   GstBaseRTPAudioPayloadPrivate *priv;
381
382   g_return_if_fail (basertpaudiopayload != NULL);
383
384   priv = basertpaudiopayload->priv;
385
386   basertpaudiopayload->sample_size = sample_size;
387
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;
394
395   gst_adapter_clear (priv->adapter);
396
397   GST_DEBUG_OBJECT (basertpaudiopayload,
398       "Samplebits set to sample size %d bits", sample_size);
399 }
400
401 static void
402 gst_base_rtp_audio_payload_set_meta (GstBaseRTPAudioPayload * payload,
403     GstBuffer * buffer, guint payload_len, GstClockTime timestamp)
404 {
405   GstBaseRTPPayload *basepayload;
406   GstBaseRTPAudioPayloadPrivate *priv;
407
408   basepayload = GST_BASE_RTP_PAYLOAD_CAST (payload);
409   priv = payload->priv;
410
411   /* set payload type */
412   gst_rtp_buffer_set_payload_type (buffer, basepayload->pt);
413   /* set marker bit for disconts */
414   if (priv->discont) {
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;
419   }
420   GST_BUFFER_TIMESTAMP (buffer) = timestamp;
421
422   /* get the offset in RTP time */
423   GST_BUFFER_OFFSET (buffer) = priv->bytes_to_rtptime (payload, priv->offset);
424
425   priv->offset += payload_len;
426
427   /* Set the duration from the size */
428   GST_BUFFER_DURATION (buffer) = priv->bytes_to_time (payload, payload_len);
429
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;
434 }
435
436 /**
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
442  *
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.
446  *
447  * Returns: a #GstFlowReturn
448  *
449  * Since: 0.10.13
450  */
451 GstFlowReturn
452 gst_base_rtp_audio_payload_push (GstBaseRTPAudioPayload * baseaudiopayload,
453     const guint8 * data, guint payload_len, GstClockTime timestamp)
454 {
455   GstBaseRTPPayload *basepayload;
456   GstBuffer *outbuf;
457   guint8 *payload;
458   GstFlowReturn ret;
459
460   basepayload = GST_BASE_RTP_PAYLOAD (baseaudiopayload);
461
462   GST_DEBUG_OBJECT (baseaudiopayload, "Pushing %d bytes ts %" GST_TIME_FORMAT,
463       payload_len, GST_TIME_ARGS (timestamp));
464
465   /* create buffer to hold the payload */
466   outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
467
468   /* copy payload */
469   payload = gst_rtp_buffer_get_payload (outbuf);
470   memcpy (payload, data, payload_len);
471
472   /* set metadata */
473   gst_base_rtp_audio_payload_set_meta (baseaudiopayload, outbuf, payload_len,
474       timestamp);
475
476   ret = gst_basertppayload_push (basepayload, outbuf);
477
478   return ret;
479 }
480
481 static GstFlowReturn
482 gst_base_rtp_audio_payload_push_buffer (GstBaseRTPAudioPayload *
483     baseaudiopayload, GstBuffer * buffer, GstClockTime timestamp)
484 {
485   GstBaseRTPPayload *basepayload;
486   GstBaseRTPAudioPayloadPrivate *priv;
487   GstBuffer *outbuf;
488   guint8 *payload;
489   guint payload_len;
490   GstFlowReturn ret;
491
492   priv = baseaudiopayload->priv;
493   basepayload = GST_BASE_RTP_PAYLOAD (baseaudiopayload);
494
495   payload_len = GST_BUFFER_SIZE (buffer);
496
497   GST_DEBUG_OBJECT (baseaudiopayload, "Pushing %d bytes ts %" GST_TIME_FORMAT,
498       payload_len, GST_TIME_ARGS (timestamp));
499
500   if (priv->buffer_list) {
501     /* create just the RTP header buffer */
502     outbuf = gst_rtp_buffer_new_allocate (0, 0, 0);
503   } else {
504     /* create buffer to hold the payload */
505     outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
506   }
507
508   /* set metadata */
509   gst_base_rtp_audio_payload_set_meta (baseaudiopayload, outbuf, payload_len,
510       timestamp);
511
512   if (priv->buffer_list) {
513     GstBufferList *list;
514     GstBufferListIterator *it;
515
516     list = gst_buffer_list_new ();
517     it = gst_buffer_list_iterate (list);
518
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);
523
524     gst_buffer_list_iterator_free (it);
525
526     GST_DEBUG_OBJECT (baseaudiopayload, "Pushing list %p", list);
527     ret = gst_basertppayload_push_list (basepayload, list);
528   } else {
529     /* copy payload */
530     payload = gst_rtp_buffer_get_payload (outbuf);
531     memcpy (payload, GST_BUFFER_DATA (buffer), payload_len);
532     gst_buffer_unref (buffer);
533
534     GST_DEBUG_OBJECT (baseaudiopayload, "Pushing buffer %p", outbuf);
535     ret = gst_basertppayload_push (basepayload, outbuf);
536   }
537
538   return ret;
539 }
540
541 /**
542  * gst_base_rtp_audio_payload_flush:
543  * @baseaudiopayload: a #GstBaseRTPPayload
544  * @payload_len: length of payload
545  * @timestamp: a #GstClockTime
546  *
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.
550  *
551  * If @payload_len is -1, all pending bytes will be flushed. If @timestamp is
552  * -1, the timestamp will be calculated automatically.
553  *
554  * Returns: a #GstFlowReturn
555  *
556  * Since: 0.10.25
557  */
558 GstFlowReturn
559 gst_base_rtp_audio_payload_flush (GstBaseRTPAudioPayload * baseaudiopayload,
560     guint payload_len, GstClockTime timestamp)
561 {
562   GstBaseRTPPayload *basepayload;
563   GstBaseRTPAudioPayloadPrivate *priv;
564   GstBuffer *outbuf;
565   guint8 *payload;
566   GstFlowReturn ret;
567   GstAdapter *adapter;
568   guint64 distance;
569
570   priv = baseaudiopayload->priv;
571   adapter = priv->adapter;
572
573   basepayload = GST_BASE_RTP_PAYLOAD (baseaudiopayload);
574
575   if (payload_len == -1)
576     payload_len = gst_adapter_available (adapter);
577
578   /* nothing to do, just return */
579   if (payload_len == 0)
580     return GST_FLOW_OK;
581
582   if (timestamp == -1) {
583     /* calculate the timestamp */
584     timestamp = gst_adapter_prev_timestamp (adapter, &distance);
585
586     GST_LOG_OBJECT (baseaudiopayload,
587         "last timestamp %" GST_TIME_FORMAT ", distance %" G_GUINT64_FORMAT,
588         GST_TIME_ARGS (timestamp), distance);
589
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);
594     }
595   }
596
597   GST_DEBUG_OBJECT (baseaudiopayload, "Pushing %d bytes ts %" GST_TIME_FORMAT,
598       payload_len, GST_TIME_ARGS (timestamp));
599
600   if (priv->buffer_list && gst_adapter_available_fast (adapter) >= payload_len) {
601     GstBuffer *buffer;
602     /* we can quickly take a buffer out of the adapter without having to copy
603      * anything. */
604     buffer = gst_adapter_take_buffer (adapter, payload_len);
605
606     ret =
607         gst_base_rtp_audio_payload_push_buffer (baseaudiopayload, buffer,
608         timestamp);
609   } else {
610     /* create buffer to hold the payload */
611     outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
612
613     /* copy payload */
614     payload = gst_rtp_buffer_get_payload (outbuf);
615     gst_adapter_copy (adapter, payload, 0, payload_len);
616     gst_adapter_flush (adapter, payload_len);
617
618     /* set metadata */
619     gst_base_rtp_audio_payload_set_meta (baseaudiopayload, outbuf, payload_len,
620         timestamp);
621
622     ret = gst_basertppayload_push (basepayload, outbuf);
623   }
624
625   return ret;
626 }
627
628 #define ALIGN_DOWN(val,len) ((val) - ((val) % (len)))
629
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 */
633 static gboolean
634 gst_base_rtp_audio_payload_get_lengths (GstBaseRTPPayload *
635     basepayload, guint * min_payload_len, guint * max_payload_len,
636     guint * align)
637 {
638   GstBaseRTPAudioPayload *payload;
639   GstBaseRTPAudioPayloadPrivate *priv;
640   guint max_mtu, mtu;
641   guint maxptime_octets;
642   guint minptime_octets;
643   guint ptime_mult_octets;
644
645   payload = GST_BASE_RTP_AUDIO_PAYLOAD_CAST (basepayload);
646   priv = payload->priv;
647
648   if (priv->align == 0)
649     return FALSE;
650
651   mtu = GST_BASE_RTP_PAYLOAD_MTU (payload);
652
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;
664     return TRUE;
665   }
666
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);
670
671   /* ptime max */
672   if (basepayload->max_ptime != -1) {
673     maxptime_octets = priv->time_to_bytes (payload, basepayload->max_ptime);
674   } else {
675     maxptime_octets = G_MAXUINT;
676   }
677   /* MTU max */
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);
681
682   /* combine max ptime and max payload length */
683   *max_payload_len = MIN (max_mtu, maxptime_octets);
684
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);
689
690   if (*min_payload_len > *max_payload_len)
691     *min_payload_len = *max_payload_len;
692
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);
697
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);
701
702     *min_payload_len = *max_payload_len = ptime_in_bytes;
703   }
704
705   /* cache values */
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;
714
715   return TRUE;
716 }
717
718 /* frame conversions functions */
719 static GstClockTime
720 gst_base_rtp_audio_payload_frame_bytes_to_time (GstBaseRTPAudioPayload *
721     payload, guint64 bytes)
722 {
723   guint64 framecount;
724
725   framecount = bytes / payload->frame_size;
726   if (G_UNLIKELY (bytes % payload->frame_size))
727     framecount++;
728
729   return framecount * payload->priv->frame_duration_ns;
730 }
731
732 static guint32
733 gst_base_rtp_audio_payload_frame_bytes_to_rtptime (GstBaseRTPAudioPayload *
734     payload, guint64 bytes)
735 {
736   guint64 framecount;
737   guint64 time;
738
739   framecount = bytes / payload->frame_size;
740   if (G_UNLIKELY (bytes % payload->frame_size))
741     framecount++;
742
743   time = framecount * payload->priv->frame_duration_ns;
744
745   return gst_util_uint64_scale_int (time,
746       GST_BASE_RTP_PAYLOAD (payload)->clock_rate, GST_SECOND);
747 }
748
749 static guint64
750 gst_base_rtp_audio_payload_frame_time_to_bytes (GstBaseRTPAudioPayload *
751     payload, GstClockTime time)
752 {
753   return gst_util_uint64_scale (time, payload->frame_size,
754       payload->priv->frame_duration_ns);
755 }
756
757 /* sample conversion functions */
758 static GstClockTime
759 gst_base_rtp_audio_payload_sample_bytes_to_time (GstBaseRTPAudioPayload *
760     payload, guint64 bytes)
761 {
762   guint64 rtptime;
763
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);
767   else
768     rtptime = bytes;
769
770   return gst_util_uint64_scale_int (rtptime, GST_SECOND,
771       GST_BASE_RTP_PAYLOAD (payload)->clock_rate);
772 }
773
774 static guint32
775 gst_base_rtp_audio_payload_sample_bytes_to_rtptime (GstBaseRTPAudioPayload *
776     payload, guint64 bytes)
777 {
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);
781   else
782     return bytes;
783 }
784
785 static guint64
786 gst_base_rtp_audio_payload_sample_time_to_bytes (GstBaseRTPAudioPayload *
787     payload, guint64 time)
788 {
789   guint64 samples;
790
791   samples = gst_util_uint64_scale_int (time,
792       GST_BASE_RTP_PAYLOAD (payload)->clock_rate, GST_SECOND);
793
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);
797   else
798     return samples;
799 }
800
801 static GstFlowReturn
802 gst_base_rtp_audio_payload_handle_buffer (GstBaseRTPPayload *
803     basepayload, GstBuffer * buffer)
804 {
805   GstBaseRTPAudioPayload *payload;
806   GstBaseRTPAudioPayloadPrivate *priv;
807   guint payload_len;
808   GstFlowReturn ret;
809   guint available;
810   guint min_payload_len;
811   guint max_payload_len;
812   guint align;
813   guint size;
814   gboolean discont;
815   GstClockTime timestamp;
816
817   ret = GST_FLOW_OK;
818
819   payload = GST_BASE_RTP_AUDIO_PAYLOAD_CAST (basepayload);
820   priv = payload->priv;
821
822   timestamp = GST_BUFFER_TIMESTAMP (buffer);
823   discont = GST_BUFFER_IS_DISCONT (buffer);
824   if (discont) {
825
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;
830
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) {
839         GstClockTime diff;
840         guint64 bytes;
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
843          * offset */
844         diff = timestamp - priv->last_timestamp;
845         bytes = priv->time_to_bytes (payload, diff);
846         priv->offset += bytes;
847
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,
851             priv->offset);
852       }
853     }
854   }
855
856   if (!gst_base_rtp_audio_payload_get_lengths (basepayload, &min_payload_len,
857           &max_payload_len, &align))
858     goto config_error;
859
860   GST_DEBUG_OBJECT (payload,
861       "Calculated min_payload_len %u and max_payload_len %u",
862       min_payload_len, max_payload_len);
863
864   size = GST_BUFFER_SIZE (buffer);
865
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);
869
870   GST_DEBUG_OBJECT (payload, "got buffer size %u, available %u",
871       size, available);
872
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);
879   } else {
880     /* push the buffer in the adapter */
881     gst_adapter_push (priv->adapter, buffer);
882     available += size;
883
884     GST_DEBUG_OBJECT (payload, "available now %u", available);
885
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);
891
892       /* and flush out the bytes from the adapter, automatically set the
893        * timestamp. */
894       ret = gst_base_rtp_audio_payload_flush (payload, payload_len, -1);
895
896       available -= payload_len;
897       GST_DEBUG_OBJECT (payload, "available after push %u", available);
898     }
899   }
900   return ret;
901
902   /* ERRORS */
903 config_error:
904   {
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;
909   }
910 }
911
912 static GstStateChangeReturn
913 gst_base_rtp_payload_audio_change_state (GstElement * element,
914     GstStateChange transition)
915 {
916   GstBaseRTPAudioPayload *basertppayload;
917   GstStateChangeReturn ret;
918
919   basertppayload = GST_BASE_RTP_AUDIO_PAYLOAD (element);
920
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;
926       break;
927     default:
928       break;
929   }
930
931   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
932
933   switch (transition) {
934     case GST_STATE_CHANGE_PAUSED_TO_READY:
935       gst_adapter_clear (basertppayload->priv->adapter);
936       break;
937     default:
938       break;
939   }
940
941   return ret;
942 }
943
944 static gboolean
945 gst_base_rtp_payload_audio_handle_event (GstPad * pad, GstEvent * event)
946 {
947   GstBaseRTPAudioPayload *payload;
948   gboolean res = FALSE;
949
950   payload = GST_BASE_RTP_AUDIO_PAYLOAD (gst_pad_get_parent (pad));
951
952   switch (GST_EVENT_TYPE (event)) {
953     case GST_EVENT_EOS:
954       /* flush remaining bytes in the adapter */
955       gst_base_rtp_audio_payload_flush (payload, -1, -1);
956       break;
957     case GST_EVENT_FLUSH_STOP:
958       gst_adapter_clear (payload->priv->adapter);
959       break;
960     default:
961       break;
962   }
963
964   gst_object_unref (payload);
965
966   /* return FALSE to let parent handle the remainder of the event */
967   return res;
968 }
969
970 /**
971  * gst_base_rtp_audio_payload_get_adapter:
972  * @basertpaudiopayload: a #GstBaseRTPAudioPayload
973  *
974  * Gets the internal adapter used by the depayloader.
975  *
976  * Returns: a #GstAdapter.
977  *
978  * Since: 0.10.13
979  */
980 GstAdapter *
981 gst_base_rtp_audio_payload_get_adapter (GstBaseRTPAudioPayload
982     * basertpaudiopayload)
983 {
984   GstAdapter *adapter;
985
986   if ((adapter = basertpaudiopayload->priv->adapter))
987     g_object_ref (adapter);
988
989   return adapter;
990 }