Merge branch 'master' into 0.11
[platform/upstream/gstreamer.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 #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);
165
166 static void
167 gst_base_rtp_audio_payload_class_init (GstBaseRTPAudioPayloadClass * klass)
168 {
169   GObjectClass *gobject_class;
170   GstElementClass *gstelement_class;
171   GstBaseRTPPayloadClass *gstbasertppayload_class;
172
173   g_type_class_add_private (klass, sizeof (GstBaseRTPAudioPayloadPrivate));
174
175   gobject_class = (GObjectClass *) klass;
176   gstelement_class = (GstElementClass *) klass;
177   gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
178
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;
182
183   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BUFFER_LIST,
184       g_param_spec_boolean ("buffer-list", "Buffer List",
185           "Use Buffer Lists",
186           DEFAULT_BUFFER_LIST, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
187
188   gstelement_class->change_state =
189       GST_DEBUG_FUNCPTR (gst_base_rtp_payload_audio_change_state);
190
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);
195
196   GST_DEBUG_CATEGORY_INIT (basertpaudiopayload_debug, "basertpaudiopayload", 0,
197       "base audio RTP payloader");
198 }
199
200 static void
201 gst_base_rtp_audio_payload_init (GstBaseRTPAudioPayload * payload)
202 {
203   payload->priv = GST_BASE_RTP_AUDIO_PAYLOAD_GET_PRIVATE (payload);
204
205   /* these need to be set by child object if frame based */
206   payload->frame_size = 0;
207   payload->frame_duration = 0;
208
209   /* these need to be set by child object if sample based */
210   payload->sample_size = 0;
211
212   payload->priv->adapter = gst_adapter_new ();
213
214   payload->priv->buffer_list = DEFAULT_BUFFER_LIST;
215 }
216
217 static void
218 gst_base_rtp_audio_payload_finalize (GObject * object)
219 {
220   GstBaseRTPAudioPayload *payload;
221
222   payload = GST_BASE_RTP_AUDIO_PAYLOAD (object);
223
224   g_object_unref (payload->priv->adapter);
225
226   GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
227 }
228
229 static void
230 gst_base_rtp_audio_payload_set_property (GObject * object,
231     guint prop_id, const GValue * value, GParamSpec * pspec)
232 {
233   GstBaseRTPAudioPayload *payload;
234
235   payload = GST_BASE_RTP_AUDIO_PAYLOAD (object);
236
237   switch (prop_id) {
238     case PROP_BUFFER_LIST:
239       payload->priv->buffer_list = g_value_get_boolean (value);
240       break;
241     default:
242       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
243       break;
244   }
245 }
246
247 static void
248 gst_base_rtp_audio_payload_get_property (GObject * object,
249     guint prop_id, GValue * value, GParamSpec * pspec)
250 {
251   GstBaseRTPAudioPayload *payload;
252
253   payload = GST_BASE_RTP_AUDIO_PAYLOAD (object);
254
255   switch (prop_id) {
256     case PROP_BUFFER_LIST:
257       g_value_set_boolean (value, payload->priv->buffer_list);
258       break;
259     default:
260       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
261       break;
262   }
263 }
264
265 /**
266  * gst_base_rtp_audio_payload_set_frame_based:
267  * @basertpaudiopayload: a pointer to the element.
268  *
269  * Tells #GstBaseRTPAudioPayload that the child element is for a frame based
270  * audio codec
271  */
272 void
273 gst_base_rtp_audio_payload_set_frame_based (GstBaseRTPAudioPayload *
274     basertpaudiopayload)
275 {
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);
280
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;
287 }
288
289 /**
290  * gst_base_rtp_audio_payload_set_sample_based:
291  * @basertpaudiopayload: a pointer to the element.
292  *
293  * Tells #GstBaseRTPAudioPayload that the child element is for a sample based
294  * audio codec
295  */
296 void
297 gst_base_rtp_audio_payload_set_sample_based (GstBaseRTPAudioPayload *
298     basertpaudiopayload)
299 {
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);
304
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;
311 }
312
313 /**
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.
318  *
319  * Sets the options for frame based audio codecs.
320  *
321  */
322 void
323 gst_base_rtp_audio_payload_set_frame_options (GstBaseRTPAudioPayload
324     * basertpaudiopayload, gint frame_duration, gint frame_size)
325 {
326   GstBaseRTPAudioPayloadPrivate *priv;
327
328   g_return_if_fail (basertpaudiopayload != NULL);
329
330   priv = basertpaudiopayload->priv;
331
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;
336
337   gst_adapter_clear (priv->adapter);
338
339   GST_DEBUG_OBJECT (basertpaudiopayload, "frame set to %d ms and size %d",
340       frame_duration, frame_size);
341 }
342
343 /**
344  * gst_base_rtp_audio_payload_set_sample_options:
345  * @basertpaudiopayload: a pointer to the element.
346  * @sample_size: Size per sample in bytes.
347  *
348  * Sets the options for sample based audio codecs.
349  */
350 void
351 gst_base_rtp_audio_payload_set_sample_options (GstBaseRTPAudioPayload
352     * basertpaudiopayload, gint sample_size)
353 {
354   g_return_if_fail (basertpaudiopayload != NULL);
355
356   /* sample_size is in bits internally */
357   gst_base_rtp_audio_payload_set_samplebits_options (basertpaudiopayload,
358       sample_size * 8);
359 }
360
361 /**
362  * gst_base_rtp_audio_payload_set_samplebits_options:
363  * @basertpaudiopayload: a pointer to the element.
364  * @sample_size: Size per sample in bits.
365  *
366  * Sets the options for sample based audio codecs.
367  *
368  * Since: 0.10.18
369  */
370 void
371 gst_base_rtp_audio_payload_set_samplebits_options (GstBaseRTPAudioPayload
372     * basertpaudiopayload, gint sample_size)
373 {
374   guint fragment_size;
375   GstBaseRTPAudioPayloadPrivate *priv;
376
377   g_return_if_fail (basertpaudiopayload != NULL);
378
379   priv = basertpaudiopayload->priv;
380
381   basertpaudiopayload->sample_size = sample_size;
382
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;
389
390   gst_adapter_clear (priv->adapter);
391
392   GST_DEBUG_OBJECT (basertpaudiopayload,
393       "Samplebits set to sample size %d bits", sample_size);
394 }
395
396 static void
397 gst_base_rtp_audio_payload_set_meta (GstBaseRTPAudioPayload * payload,
398     GstBuffer * buffer, guint payload_len, GstClockTime timestamp)
399 {
400   GstBaseRTPPayload *basepayload;
401   GstBaseRTPAudioPayloadPrivate *priv;
402   GstRTPBuffer rtp;
403
404   basepayload = GST_BASE_RTP_PAYLOAD_CAST (payload);
405   priv = payload->priv;
406
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 */
411   if (priv->discont) {
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;
416   }
417   gst_rtp_buffer_unmap (&rtp);
418
419   GST_BUFFER_TIMESTAMP (buffer) = timestamp;
420
421   /* get the offset in RTP time */
422   GST_BUFFER_OFFSET (buffer) = priv->bytes_to_rtptime (payload, priv->offset);
423
424   priv->offset += payload_len;
425
426   /* Set the duration from the size */
427   GST_BUFFER_DURATION (buffer) = priv->bytes_to_time (payload, payload_len);
428
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;
433 }
434
435 /**
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
441  *
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.
445  *
446  * Returns: a #GstFlowReturn
447  *
448  * Since: 0.10.13
449  */
450 GstFlowReturn
451 gst_base_rtp_audio_payload_push (GstBaseRTPAudioPayload * baseaudiopayload,
452     const guint8 * data, guint payload_len, GstClockTime timestamp)
453 {
454   GstBaseRTPPayload *basepayload;
455   GstBuffer *outbuf;
456   guint8 *payload;
457   GstFlowReturn ret;
458   GstRTPBuffer rtp;
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   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);
473
474   /* set metadata */
475   gst_base_rtp_audio_payload_set_meta (baseaudiopayload, outbuf, payload_len,
476       timestamp);
477
478   ret = gst_basertppayload_push (basepayload, outbuf);
479
480   return ret;
481 }
482
483 static GstFlowReturn
484 gst_base_rtp_audio_payload_push_buffer (GstBaseRTPAudioPayload *
485     baseaudiopayload, GstBuffer * buffer, GstClockTime timestamp)
486 {
487   GstBaseRTPPayload *basepayload;
488   GstBaseRTPAudioPayloadPrivate *priv;
489   GstBuffer *outbuf;
490   guint8 *payload;
491   guint payload_len;
492   GstFlowReturn ret;
493
494   priv = baseaudiopayload->priv;
495   basepayload = GST_BASE_RTP_PAYLOAD (baseaudiopayload);
496
497   payload_len = gst_buffer_get_size (buffer);
498
499   GST_DEBUG_OBJECT (baseaudiopayload, "Pushing %d bytes ts %" GST_TIME_FORMAT,
500       payload_len, GST_TIME_ARGS (timestamp));
501
502   if (priv->buffer_list) {
503     /* create just the RTP header buffer */
504     outbuf = gst_rtp_buffer_new_allocate (0, 0, 0);
505   } else {
506     /* create buffer to hold the payload */
507     outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
508   }
509
510   /* set metadata */
511   gst_base_rtp_audio_payload_set_meta (baseaudiopayload, outbuf, payload_len,
512       timestamp);
513
514   if (priv->buffer_list) {
515     GstBufferList *list;
516     guint i, len;
517
518     list = gst_buffer_list_new ();
519     len = gst_buffer_list_len (list);
520
521     for (i = 0; i < len; i++) {
522       /* FIXME */
523       g_warning ("bufferlist not implemented");
524       gst_buffer_list_add (list, outbuf);
525       gst_buffer_list_add (list, buffer);
526     }
527
528     GST_DEBUG_OBJECT (baseaudiopayload, "Pushing list %p", list);
529     ret = gst_basertppayload_push_list (basepayload, list);
530   } else {
531     GstRTPBuffer rtp;
532
533     /* copy payload */
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);
538
539     gst_buffer_unref (buffer);
540
541     GST_DEBUG_OBJECT (baseaudiopayload, "Pushing buffer %p", outbuf);
542     ret = gst_basertppayload_push (basepayload, outbuf);
543   }
544
545   return ret;
546 }
547
548 /**
549  * gst_base_rtp_audio_payload_flush:
550  * @baseaudiopayload: a #GstBaseRTPPayload
551  * @payload_len: length of payload
552  * @timestamp: a #GstClockTime
553  *
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.
557  *
558  * If @payload_len is -1, all pending bytes will be flushed. If @timestamp is
559  * -1, the timestamp will be calculated automatically.
560  *
561  * Returns: a #GstFlowReturn
562  *
563  * Since: 0.10.25
564  */
565 GstFlowReturn
566 gst_base_rtp_audio_payload_flush (GstBaseRTPAudioPayload * baseaudiopayload,
567     guint payload_len, GstClockTime timestamp)
568 {
569   GstBaseRTPPayload *basepayload;
570   GstBaseRTPAudioPayloadPrivate *priv;
571   GstBuffer *outbuf;
572   guint8 *payload;
573   GstFlowReturn ret;
574   GstAdapter *adapter;
575   guint64 distance;
576
577   priv = baseaudiopayload->priv;
578   adapter = priv->adapter;
579
580   basepayload = GST_BASE_RTP_PAYLOAD (baseaudiopayload);
581
582   if (payload_len == -1)
583     payload_len = gst_adapter_available (adapter);
584
585   /* nothing to do, just return */
586   if (payload_len == 0)
587     return GST_FLOW_OK;
588
589   if (timestamp == -1) {
590     /* calculate the timestamp */
591     timestamp = gst_adapter_prev_timestamp (adapter, &distance);
592
593     GST_LOG_OBJECT (baseaudiopayload,
594         "last timestamp %" GST_TIME_FORMAT ", distance %" G_GUINT64_FORMAT,
595         GST_TIME_ARGS (timestamp), distance);
596
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);
601     }
602   }
603
604   GST_DEBUG_OBJECT (baseaudiopayload, "Pushing %d bytes ts %" GST_TIME_FORMAT,
605       payload_len, GST_TIME_ARGS (timestamp));
606
607   if (priv->buffer_list && gst_adapter_available_fast (adapter) >= payload_len) {
608     GstBuffer *buffer;
609     /* we can quickly take a buffer out of the adapter without having to copy
610      * anything. */
611     buffer = gst_adapter_take_buffer (adapter, payload_len);
612
613     ret =
614         gst_base_rtp_audio_payload_push_buffer (baseaudiopayload, buffer,
615         timestamp);
616   } else {
617     GstRTPBuffer rtp;
618
619     /* create buffer to hold the payload */
620     outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
621
622     /* copy payload */
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);
628
629     /* set metadata */
630     gst_base_rtp_audio_payload_set_meta (baseaudiopayload, outbuf, payload_len,
631         timestamp);
632
633     ret = gst_basertppayload_push (basepayload, outbuf);
634   }
635
636   return ret;
637 }
638
639 #define ALIGN_DOWN(val,len) ((val) - ((val) % (len)))
640
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 */
644 static gboolean
645 gst_base_rtp_audio_payload_get_lengths (GstBaseRTPPayload *
646     basepayload, guint * min_payload_len, guint * max_payload_len,
647     guint * align)
648 {
649   GstBaseRTPAudioPayload *payload;
650   GstBaseRTPAudioPayloadPrivate *priv;
651   guint max_mtu, mtu;
652   guint maxptime_octets;
653   guint minptime_octets;
654   guint ptime_mult_octets;
655
656   payload = GST_BASE_RTP_AUDIO_PAYLOAD_CAST (basepayload);
657   priv = payload->priv;
658
659   if (priv->align == 0)
660     return FALSE;
661
662   mtu = GST_BASE_RTP_PAYLOAD_MTU (payload);
663
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;
675     return TRUE;
676   }
677
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);
681
682   /* ptime max */
683   if (basepayload->max_ptime != -1) {
684     maxptime_octets = priv->time_to_bytes (payload, basepayload->max_ptime);
685   } else {
686     maxptime_octets = G_MAXUINT;
687   }
688   /* MTU max */
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);
692
693   /* combine max ptime and max payload length */
694   *max_payload_len = MIN (max_mtu, maxptime_octets);
695
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);
700
701   if (*min_payload_len > *max_payload_len)
702     *min_payload_len = *max_payload_len;
703
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);
708
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);
712
713     *min_payload_len = *max_payload_len = ptime_in_bytes;
714   }
715
716   /* cache values */
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;
725
726   return TRUE;
727 }
728
729 /* frame conversions functions */
730 static GstClockTime
731 gst_base_rtp_audio_payload_frame_bytes_to_time (GstBaseRTPAudioPayload *
732     payload, guint64 bytes)
733 {
734   guint64 framecount;
735
736   framecount = bytes / payload->frame_size;
737   if (G_UNLIKELY (bytes % payload->frame_size))
738     framecount++;
739
740   return framecount * payload->priv->frame_duration_ns;
741 }
742
743 static guint32
744 gst_base_rtp_audio_payload_frame_bytes_to_rtptime (GstBaseRTPAudioPayload *
745     payload, guint64 bytes)
746 {
747   guint64 framecount;
748   guint64 time;
749
750   framecount = bytes / payload->frame_size;
751   if (G_UNLIKELY (bytes % payload->frame_size))
752     framecount++;
753
754   time = framecount * payload->priv->frame_duration_ns;
755
756   return gst_util_uint64_scale_int (time,
757       GST_BASE_RTP_PAYLOAD (payload)->clock_rate, GST_SECOND);
758 }
759
760 static guint64
761 gst_base_rtp_audio_payload_frame_time_to_bytes (GstBaseRTPAudioPayload *
762     payload, GstClockTime time)
763 {
764   return gst_util_uint64_scale (time, payload->frame_size,
765       payload->priv->frame_duration_ns);
766 }
767
768 /* sample conversion functions */
769 static GstClockTime
770 gst_base_rtp_audio_payload_sample_bytes_to_time (GstBaseRTPAudioPayload *
771     payload, guint64 bytes)
772 {
773   guint64 rtptime;
774
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);
778   else
779     rtptime = bytes;
780
781   return gst_util_uint64_scale_int (rtptime, GST_SECOND,
782       GST_BASE_RTP_PAYLOAD (payload)->clock_rate);
783 }
784
785 static guint32
786 gst_base_rtp_audio_payload_sample_bytes_to_rtptime (GstBaseRTPAudioPayload *
787     payload, guint64 bytes)
788 {
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);
792   else
793     return bytes;
794 }
795
796 static guint64
797 gst_base_rtp_audio_payload_sample_time_to_bytes (GstBaseRTPAudioPayload *
798     payload, guint64 time)
799 {
800   guint64 samples;
801
802   samples = gst_util_uint64_scale_int (time,
803       GST_BASE_RTP_PAYLOAD (payload)->clock_rate, GST_SECOND);
804
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);
808   else
809     return samples;
810 }
811
812 static GstFlowReturn
813 gst_base_rtp_audio_payload_handle_buffer (GstBaseRTPPayload *
814     basepayload, GstBuffer * buffer)
815 {
816   GstBaseRTPAudioPayload *payload;
817   GstBaseRTPAudioPayloadPrivate *priv;
818   guint payload_len;
819   GstFlowReturn ret;
820   guint available;
821   guint min_payload_len;
822   guint max_payload_len;
823   guint align;
824   guint size;
825   gboolean discont;
826   GstClockTime timestamp;
827
828   ret = GST_FLOW_OK;
829
830   payload = GST_BASE_RTP_AUDIO_PAYLOAD_CAST (basepayload);
831   priv = payload->priv;
832
833   timestamp = GST_BUFFER_TIMESTAMP (buffer);
834   discont = GST_BUFFER_IS_DISCONT (buffer);
835   if (discont) {
836
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;
841
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) {
850         GstClockTime diff;
851         guint64 bytes;
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
854          * offset */
855         diff = timestamp - priv->last_timestamp;
856         bytes = priv->time_to_bytes (payload, diff);
857         priv->offset += bytes;
858
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,
862             priv->offset);
863       }
864     }
865   }
866
867   if (!gst_base_rtp_audio_payload_get_lengths (basepayload, &min_payload_len,
868           &max_payload_len, &align))
869     goto config_error;
870
871   GST_DEBUG_OBJECT (payload,
872       "Calculated min_payload_len %u and max_payload_len %u",
873       min_payload_len, max_payload_len);
874
875   size = gst_buffer_get_size (buffer);
876
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);
880
881   GST_DEBUG_OBJECT (payload, "got buffer size %u, available %u",
882       size, available);
883
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);
890   } else {
891     /* push the buffer in the adapter */
892     gst_adapter_push (priv->adapter, buffer);
893     available += size;
894
895     GST_DEBUG_OBJECT (payload, "available now %u", available);
896
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);
902
903       /* and flush out the bytes from the adapter, automatically set the
904        * timestamp. */
905       ret = gst_base_rtp_audio_payload_flush (payload, payload_len, -1);
906
907       available -= payload_len;
908       GST_DEBUG_OBJECT (payload, "available after push %u", available);
909     }
910   }
911   return ret;
912
913   /* ERRORS */
914 config_error:
915   {
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;
920   }
921 }
922
923 static GstStateChangeReturn
924 gst_base_rtp_payload_audio_change_state (GstElement * element,
925     GstStateChange transition)
926 {
927   GstBaseRTPAudioPayload *basertppayload;
928   GstStateChangeReturn ret;
929
930   basertppayload = GST_BASE_RTP_AUDIO_PAYLOAD (element);
931
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;
937       break;
938     default:
939       break;
940   }
941
942   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
943
944   switch (transition) {
945     case GST_STATE_CHANGE_PAUSED_TO_READY:
946       gst_adapter_clear (basertppayload->priv->adapter);
947       break;
948     default:
949       break;
950   }
951
952   return ret;
953 }
954
955 static gboolean
956 gst_base_rtp_payload_audio_handle_event (GstPad * pad, GstEvent * event)
957 {
958   GstBaseRTPAudioPayload *payload;
959   gboolean res = FALSE;
960
961   payload = GST_BASE_RTP_AUDIO_PAYLOAD (gst_pad_get_parent (pad));
962
963   switch (GST_EVENT_TYPE (event)) {
964     case GST_EVENT_EOS:
965       /* flush remaining bytes in the adapter */
966       gst_base_rtp_audio_payload_flush (payload, -1, -1);
967       break;
968     case GST_EVENT_FLUSH_STOP:
969       gst_adapter_clear (payload->priv->adapter);
970       break;
971     default:
972       break;
973   }
974
975   gst_object_unref (payload);
976
977   /* return FALSE to let parent handle the remainder of the event */
978   return res;
979 }
980
981 /**
982  * gst_base_rtp_audio_payload_get_adapter:
983  * @basertpaudiopayload: a #GstBaseRTPAudioPayload
984  *
985  * Gets the internal adapter used by the depayloader.
986  *
987  * Returns: a #GstAdapter.
988  *
989  * Since: 0.10.13
990  */
991 GstAdapter *
992 gst_base_rtp_audio_payload_get_adapter (GstBaseRTPAudioPayload
993     * basertpaudiopayload)
994 {
995   GstAdapter *adapter;
996
997   if ((adapter = basertpaudiopayload->priv->adapter))
998     g_object_ref (adapter);
999
1000   return adapter;
1001 }