Debian Codegraph @

sys/osxaudio/gstatdec.c

raw
1 /* GStreamer
2 * Copyright (C) 2013 Alessandro Decina <[email protected]>
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., 51 Franklin Street, Suite 500,
17 * Boston, MA 02110-1335, USA.
18 */
19 /**
20 * SECTION:element-atdec
21 * @title: atdec
22 *
23 * AudioToolbox based decoder.
24 *
25 * ## Example launch line
26 * |[
27 * gst-launch-1.0 -v filesrc location=file.mov ! qtdemux ! queue ! aacparse ! atdec ! autoaudiosink
28 * ]|
29 * Decode aac audio from a mov file
30 *
31 */
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36
37 #include <gst/gst.h>
38 #include <gst/audio/gstaudiodecoder.h>
39 #include "gstatdec.h"
40
41 GST_DEBUG_CATEGORY_STATIC (gst_atdec_debug_category);
42 #define GST_CAT_DEFAULT gst_atdec_debug_category
43
44 static void gst_atdec_set_property (GObject * object,
45 guint property_id, const GValue * value, GParamSpec * pspec);
46 static void gst_atdec_get_property (GObject * object,
47 guint property_id, GValue * value, GParamSpec * pspec);
48 static void gst_atdec_finalize (GObject * object);
49
50 static gboolean gst_atdec_start (GstAudioDecoder * decoder);
51 static gboolean gst_atdec_stop (GstAudioDecoder * decoder);
52 static gboolean gst_atdec_set_format (GstAudioDecoder * decoder,
53 GstCaps * caps);
54 static GstFlowReturn gst_atdec_handle_frame (GstAudioDecoder * decoder,
55 GstBuffer * buffer);
56 static void gst_atdec_flush (GstAudioDecoder * decoder, gboolean hard);
57 static void gst_atdec_buffer_emptied (void *user_data,
58 AudioQueueRef queue, AudioQueueBufferRef buffer);
59
60 enum
61 {
62 PROP_0
63 };
64
65 static GstStaticPadTemplate gst_atdec_src_template =
66 GST_STATIC_PAD_TEMPLATE ("src",
67 GST_PAD_SRC,
68 GST_PAD_ALWAYS,
69 GST_STATIC_CAPS (GST_AUDIO_CAPS_MAKE ("S16LE") ", layout=interleaved;"
70 GST_AUDIO_CAPS_MAKE ("F32LE") ", layout=interleaved")
71 );
72
73 static GstStaticPadTemplate gst_atdec_sink_template =
74 GST_STATIC_PAD_TEMPLATE ("sink",
75 GST_PAD_SINK,
76 GST_PAD_ALWAYS,
77 GST_STATIC_CAPS ("audio/mpeg, mpegversion=4, framed=true, channels=[1,max];"
78 "audio/mpeg, mpegversion=1, layer=[1, 3]")
79 );
80
81 G_DEFINE_TYPE_WITH_CODE (GstATDec, gst_atdec, GST_TYPE_AUDIO_DECODER,
82 GST_DEBUG_CATEGORY_INIT (gst_atdec_debug_category, "atdec", 0,
83 "debug category for atdec element"));
84 GST_ELEMENT_REGISTER_DEFINE (atdec, "atdec", GST_RANK_MARGINAL, GST_TYPE_ATDEC);
85
86 static GstStaticCaps aac_caps = GST_STATIC_CAPS ("audio/mpeg, mpegversion=4");
87 static GstStaticCaps mp3_caps =
88 GST_STATIC_CAPS ("audio/mpeg, mpegversion=1, layer=[1, 3]");
89 static GstStaticCaps raw_caps = GST_STATIC_CAPS ("audio/x-raw");
90
91 static void
92 gst_atdec_class_init (GstATDecClass * klass)
93 {
94 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
95 GstAudioDecoderClass *audio_decoder_class = GST_AUDIO_DECODER_CLASS (klass);
96
97 gst_element_class_add_static_pad_template (GST_ELEMENT_CLASS (klass),
98 &gst_atdec_src_template);
99 gst_element_class_add_static_pad_template (GST_ELEMENT_CLASS (klass),
100 &gst_atdec_sink_template);
101
102 gst_element_class_set_static_metadata (GST_ELEMENT_CLASS (klass),
103 "AudioToolbox based audio decoder",
104 "Codec/Decoder/Audio",
105 "AudioToolbox based audio decoder",
106 "Alessandro Decina <[email protected]>");
107
108 gobject_class->set_property = gst_atdec_set_property;
109 gobject_class->get_property = gst_atdec_get_property;
110 gobject_class->finalize = gst_atdec_finalize;
111 audio_decoder_class->start = GST_DEBUG_FUNCPTR (gst_atdec_start);
112 audio_decoder_class->stop = GST_DEBUG_FUNCPTR (gst_atdec_stop);
113 audio_decoder_class->set_format = GST_DEBUG_FUNCPTR (gst_atdec_set_format);
114 audio_decoder_class->handle_frame =
115 GST_DEBUG_FUNCPTR (gst_atdec_handle_frame);
116 audio_decoder_class->flush = GST_DEBUG_FUNCPTR (gst_atdec_flush);
117 }
118
119 static void
120 gst_atdec_init (GstATDec * atdec)
121 {
122 gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (atdec), TRUE);
123 atdec->queue = NULL;
124 }
125
126 void
127 gst_atdec_set_property (GObject * object, guint property_id,
128 const GValue * value, GParamSpec * pspec)
129 {
130 GstATDec *atdec = GST_ATDEC (object);
131
132 GST_DEBUG_OBJECT (atdec, "set_property");
133
134 switch (property_id) {
135 default:
136 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
137 break;
138 }
139 }
140
141 void
142 gst_atdec_get_property (GObject * object, guint property_id,
143 GValue * value, GParamSpec * pspec)
144 {
145 GstATDec *atdec = GST_ATDEC (object);
146
147 GST_DEBUG_OBJECT (atdec, "get_property");
148
149 switch (property_id) {
150 default:
151 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
152 break;
153 }
154 }
155
156 static void
157 gst_atdec_destroy_queue (GstATDec * atdec, gboolean drain)
158 {
159 AudioQueueStop (atdec->queue, drain);
160 AudioQueueDispose (atdec->queue, true);
161 atdec->queue = NULL;
162 atdec->output_position = 0;
163 atdec->input_position = 0;
164 }
165
166 void
167 gst_atdec_finalize (GObject * object)
168 {
169 GstATDec *atdec = GST_ATDEC (object);
170
171 GST_DEBUG_OBJECT (atdec, "finalize");
172
173 if (atdec->queue)
174 gst_atdec_destroy_queue (atdec, FALSE);
175
176 G_OBJECT_CLASS (gst_atdec_parent_class)->finalize (object);
177 }
178
179 static gboolean
180 gst_atdec_start (GstAudioDecoder * decoder)
181 {
182 GstATDec *atdec = GST_ATDEC (decoder);
183
184 GST_DEBUG_OBJECT (atdec, "start");
185 atdec->output_position = 0;
186 atdec->input_position = 0;
187
188 return TRUE;
189 }
190
191 static gboolean
192 gst_atdec_stop (GstAudioDecoder * decoder)
193 {
194 GstATDec *atdec = GST_ATDEC (decoder);
195
196 gst_atdec_destroy_queue (atdec, FALSE);
197
198 return TRUE;
199 }
200
201 static gboolean
202 can_intersect_static_caps (GstCaps * caps, GstStaticCaps * caps1)
203 {
204 GstCaps *tmp;
205 gboolean ret;
206
207 tmp = gst_static_caps_get (caps1);
208 ret = gst_caps_can_intersect (caps, tmp);
209 gst_caps_unref (tmp);
210
211 return ret;
212 }
213
214 static gboolean
215 gst_caps_to_at_format (GstCaps * caps, AudioStreamBasicDescription * format)
216 {
217 int channels = 0;
218 int rate = 0;
219 GstStructure *structure;
220
221 memset (format, 0, sizeof (AudioStreamBasicDescription));
222
223 structure = gst_caps_get_structure (caps, 0);
224 gst_structure_get_int (structure, "rate", &rate);
225 gst_structure_get_int (structure, "channels", &channels);
226 format->mSampleRate = rate;
227 format->mChannelsPerFrame = channels;
228
229 if (can_intersect_static_caps (caps, &aac_caps)) {
230 format->mFormatID = kAudioFormatMPEG4AAC;
231 format->mFramesPerPacket = 1024;
232 } else if (can_intersect_static_caps (caps, &mp3_caps)) {
233 gint layer, mpegaudioversion = 1;
234
235 gst_structure_get_int (structure, "layer", &layer);
236 gst_structure_get_int (structure, "mpegaudioversion", &mpegaudioversion);
237 switch (layer) {
238 case 1:
239 format->mFormatID = kAudioFormatMPEGLayer1;
240 format->mFramesPerPacket = 384;
241 break;
242 case 2:
243 format->mFormatID = kAudioFormatMPEGLayer2;
244 format->mFramesPerPacket = 1152;
245 break;
246 case 3:
247 format->mFormatID = kAudioFormatMPEGLayer3;
248 format->mFramesPerPacket = (mpegaudioversion == 1 ? 1152 : 576);
249 break;
250 default:
251 g_warn_if_reached ();
252 format->mFormatID = kAudioFormatMPEGLayer3;
253 format->mFramesPerPacket = 1152;
254 break;
255 }
256 } else if (can_intersect_static_caps (caps, &raw_caps)) {
257 GstAudioFormat audio_format;
258 const char *audio_format_str;
259
260 format->mFormatID = kAudioFormatLinearPCM;
261 format->mFramesPerPacket = 1;
262
263 audio_format_str = gst_structure_get_string (structure, "format");
264 if (!audio_format_str)
265 audio_format_str = "S16LE";
266
267 audio_format = gst_audio_format_from_string (audio_format_str);
268 switch (audio_format) {
269 case GST_AUDIO_FORMAT_S16LE:
270 format->mFormatFlags =
271 kLinearPCMFormatFlagIsPacked | kLinearPCMFormatFlagIsSignedInteger;
272 format->mBitsPerChannel = 16;
273 format->mBytesPerPacket = format->mBytesPerFrame = 2 * channels;
274 break;
275 case GST_AUDIO_FORMAT_F32LE:
276 format->mFormatFlags =
277 kLinearPCMFormatFlagIsPacked | kLinearPCMFormatFlagIsFloat;
278 format->mBitsPerChannel = 32;
279 format->mBytesPerPacket = format->mBytesPerFrame = 4 * channels;
280 break;
281 default:
282 g_warn_if_reached ();
283 break;
284 }
285 }
286
287 return TRUE;
288 }
289
290 /* These are the position orders that AudioToolbox outputs,
291 * derived experimentally.
292 */
293 /* *INDENT-OFF* */
294 static const struct
295 {
296 gint channels;
297 GstAudioChannelPosition positions[8];
298 }
299 channel_layouts[] = {
300 {3, {
301 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
302 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
303 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
304 }},
305 {4, {
306 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
307 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
308 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
309 GST_AUDIO_CHANNEL_POSITION_REAR_CENTER,
310 }},
311 {5, {
312 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
313 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
314 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
315 GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
316 GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
317 }},
318 {6, {
319 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
320 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
321 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
322 GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
323 GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
324 GST_AUDIO_CHANNEL_POSITION_LFE1,
325 }},
326 {8, {
327 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
328 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER,
329 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER,
330 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
331 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
332 GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
333 GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
334 GST_AUDIO_CHANNEL_POSITION_LFE1,
335 }},
336 };
337 /* *INDENT-ON* */
338
339 static void
340 gst_atdec_get_channel_positions (GstATDec * atdec, gint channels,
341 GstAudioChannelPosition * positions)
342 {
343 guint64 mask;
344
345 for (guint i = 0; i < G_N_ELEMENTS (channel_layouts); ++i) {
346 if (channel_layouts[i].channels == channels) {
347 memcpy (positions, channel_layouts[i].positions,
348 channels * sizeof (*positions));
349 return;
350 }
351 }
352
353 GST_WARNING_OBJECT (atdec, "Unknown channel count %u", channels);
354 mask = gst_audio_channel_get_fallback_mask (channels);
355 gst_audio_channel_positions_from_mask (channels, mask, positions);
356 }
357
358 static gboolean
359 gst_atdec_set_format (GstAudioDecoder * decoder, GstCaps * caps)
360 {
361 OSStatus status;
362 AudioStreamBasicDescription input_format = { 0 };
363 AudioStreamBasicDescription output_format = { 0 };
364 GstAudioInfo output_info = { 0 };
365 AudioChannelLayout output_layout = { 0 };
366 GstCaps *output_caps;
367 AudioTimeStamp timestamp = { 0 };
368 AudioQueueBufferRef output_buffer;
369 GstATDec *atdec = GST_ATDEC (decoder);
370
371 GST_DEBUG_OBJECT (atdec, "set_format");
372
373 if (atdec->queue)
374 gst_atdec_destroy_queue (atdec, TRUE);
375
376 /* configure input_format from caps */
377 gst_caps_to_at_format (caps, &input_format);
378 /* Remember the number of samples per frame */
379 atdec->spf = input_format.mFramesPerPacket;
380
381 /* negotiate output caps */
382 output_caps = gst_pad_get_allowed_caps (GST_AUDIO_DECODER_SRC_PAD (atdec));
383 if (!output_caps)
384 output_caps =
385 gst_pad_get_pad_template_caps (GST_AUDIO_DECODER_SRC_PAD (atdec));
386 output_caps = gst_caps_fixate (output_caps);
387
388 gst_caps_set_simple (output_caps,
389 "rate", G_TYPE_INT, (int) input_format.mSampleRate,
390 "channels", G_TYPE_INT, input_format.mChannelsPerFrame, NULL);
391
392 /* The layout passed to AudioQueueSetOfflineRenderFormat() is ignored, and
393 * setting kAudioQueueProperty_ChannelLayout has no effect either.
394 * The actual layout is derived experimentally here.
395 * It's not in a valid order for GStreamer, so we have to reorder in
396 * gst_atdec_handle_frame().
397 */
398
399 if (input_format.mChannelsPerFrame > 2) {
400 guint64 mask;
401
402 gst_atdec_get_channel_positions (atdec, input_format.mChannelsPerFrame,
403 atdec->at_channel_positions);
404 gst_audio_channel_positions_to_mask (atdec->at_channel_positions,
405 input_format.mChannelsPerFrame, FALSE, &mask);
406 /* gst_audio_info_from_caps() below will convert the mask back into a
407 * valid order, which we will use when reordering. */
408 gst_caps_set_simple (output_caps, "channel-mask", GST_TYPE_BITMASK, mask,
409 NULL);
410 }
411
412 /* configure output_format from caps */
413 gst_caps_to_at_format (output_caps, &output_format);
414
415 /* set the format we want to negotiate downstream */
416 gst_audio_info_from_caps (&output_info, output_caps);
417 gst_audio_decoder_set_output_caps (decoder, output_caps);
418 gst_caps_unref (output_caps);
419
420 status = AudioQueueNewOutput (&input_format, gst_atdec_buffer_emptied,
421 atdec, NULL, NULL, 0, &atdec->queue);
422 if (status)
423 goto create_queue_error;
424
425 output_layout.mChannelLayoutTag = kAudioChannelLayoutTag_Unknown;
426 status = AudioQueueSetOfflineRenderFormat (atdec->queue,
427 &output_format, &output_layout);
428 if (status)
429 goto set_format_error;
430
431 status = AudioQueueStart (atdec->queue, NULL);
432 if (status)
433 goto start_error;
434
435 timestamp.mFlags = kAudioTimeStampSampleTimeValid;
436 timestamp.mSampleTime = 0;
437
438 status =
439 AudioQueueAllocateBuffer (atdec->queue, atdec->spf * output_info.bpf,
440 &output_buffer);
441 if (status)
442 goto allocate_output_error;
443
444 status = AudioQueueOfflineRender (atdec->queue, &timestamp, output_buffer, 0);
445 if (status)
446 goto offline_render_error;
447
448 AudioQueueFreeBuffer (atdec->queue, output_buffer);
449
450 return TRUE;
451
452 create_queue_error:
453 GST_ELEMENT_ERROR (atdec, STREAM, FORMAT, (NULL),
454 ("AudioQueueNewOutput returned error: %d", (gint) status));
455 return FALSE;
456
457 set_format_error:
458 GST_ELEMENT_ERROR (atdec, STREAM, FORMAT, (NULL),
459 ("AudioQueueSetOfflineRenderFormat returned error: %d", (gint) status));
460 gst_atdec_destroy_queue (atdec, FALSE);
461 return FALSE;
462
463 start_error:
464 GST_ELEMENT_ERROR (atdec, STREAM, FORMAT, (NULL),
465 ("AudioQueueStart returned error: %d", (gint) status));
466 gst_atdec_destroy_queue (atdec, FALSE);
467 return FALSE;
468
469 allocate_output_error:
470 GST_ELEMENT_ERROR (atdec, STREAM, FORMAT, (NULL),
471 ("AudioQueueAllocateBuffer returned error: %d", (gint) status));
472 gst_atdec_destroy_queue (atdec, FALSE);
473 return FALSE;
474
475 offline_render_error:
476 GST_ELEMENT_ERROR (atdec, STREAM, FORMAT, (NULL),
477 ("AudioQueueOfflineRender returned error: %d", (gint) status));
478 AudioQueueFreeBuffer (atdec->queue, output_buffer);
479 gst_atdec_destroy_queue (atdec, FALSE);
480 return FALSE;
481 }
482
483 static void
484 gst_atdec_buffer_emptied (void *user_data, AudioQueueRef queue,
485 AudioQueueBufferRef buffer)
486 {
487 AudioQueueFreeBuffer (queue, buffer);
488 }
489
490 static GstFlowReturn
491 gst_atdec_offline_render (GstATDec * atdec, GstAudioInfo * audio_info)
492 {
493 OSStatus status;
494 AudioTimeStamp timestamp = { 0 };
495 AudioQueueBufferRef output_buffer;
496 GstFlowReturn flow_ret = GST_FLOW_OK;
497 GstBuffer *out;
498 guint out_frames;
499
500 /* figure out how many frames we need to pull out of the queue */
501 out_frames = atdec->input_position - atdec->output_position;
502 if (out_frames > atdec->spf)
503 out_frames = atdec->spf;
504 status = AudioQueueAllocateBuffer (atdec->queue, out_frames * audio_info->bpf,
505 &output_buffer);
506 if (status)
507 goto allocate_output_failed;
508
509 /* pull the frames */
510 timestamp.mFlags = kAudioTimeStampSampleTimeValid;
511 timestamp.mSampleTime = atdec->output_position;
512 status =
513 AudioQueueOfflineRender (atdec->queue, &timestamp, output_buffer,
514 out_frames);
515 if (status)
516 goto offline_render_failed;
517
518 if (output_buffer->mAudioDataByteSize) {
519 if (output_buffer->mAudioDataByteSize % audio_info->bpf != 0)
520 goto invalid_buffer_size;
521
522 GST_DEBUG_OBJECT (atdec,
523 "Got output buffer of size %u at position %" G_GUINT64_FORMAT,
524 (guint) output_buffer->mAudioDataByteSize, atdec->output_position);
525 atdec->output_position +=
526 output_buffer->mAudioDataByteSize / audio_info->bpf;
527
528 out =
529 gst_audio_decoder_allocate_output_buffer (GST_AUDIO_DECODER (atdec),
530 output_buffer->mAudioDataByteSize);
531
532 gst_buffer_fill (out, 0, output_buffer->mAudioData,
533 output_buffer->mAudioDataByteSize);
534
535 if (GST_AUDIO_INFO_CHANNELS (audio_info) > 2)
536 gst_audio_buffer_reorder_channels (out,
537 GST_AUDIO_INFO_FORMAT (audio_info),
538 GST_AUDIO_INFO_CHANNELS (audio_info),
539 atdec->at_channel_positions, audio_info->position);
540
541 flow_ret =
542 gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (atdec), out, 1);
543 GST_DEBUG_OBJECT (atdec, "Finished buffer: %s",
544 gst_flow_get_name (flow_ret));
545 } else {
546 GST_DEBUG_OBJECT (atdec, "Got empty output buffer");
547 flow_ret = GST_FLOW_CUSTOM_SUCCESS;
548 }
549
550 AudioQueueFreeBuffer (atdec->queue, output_buffer);
551
552 return flow_ret;
553
554 allocate_output_failed:
555 {
556 GST_ELEMENT_ERROR (atdec, STREAM, DECODE, (NULL),
557 ("AudioQueueAllocateBuffer returned error: %d", (gint) status));
558 return GST_FLOW_ERROR;
559 }
560
561 offline_render_failed:
562 {
563 AudioQueueFreeBuffer (atdec->queue, output_buffer);
564
565 GST_AUDIO_DECODER_ERROR (atdec, 1, STREAM, DECODE, (NULL),
566 ("AudioQueueOfflineRender returned error: %d", (gint) status),
567 flow_ret);
568
569 return flow_ret;
570 }
571
572 invalid_buffer_size:
573 {
574 GST_AUDIO_DECODER_ERROR (atdec, 1, STREAM, DECODE, (NULL),
575 ("AudioQueueOfflineRender returned invalid buffer size: %u (bpf %d)",
576 (guint) output_buffer->mAudioDataByteSize, audio_info->bpf),
577 flow_ret);
578
579 AudioQueueFreeBuffer (atdec->queue, output_buffer);
580
581 return flow_ret;
582 }
583 }
584
585 static GstFlowReturn
586 gst_atdec_handle_frame (GstAudioDecoder * decoder, GstBuffer * buffer)
587 {
588 OSStatus status;
589 AudioStreamPacketDescription packet;
590 AudioQueueBufferRef input_buffer;
591 GstAudioInfo *audio_info;
592 int size;
593 GstFlowReturn flow_ret = GST_FLOW_OK;
594 GstATDec *atdec = GST_ATDEC (decoder);
595
596 audio_info = gst_audio_decoder_get_audio_info (decoder);
597
598 if (buffer == NULL) {
599 GST_DEBUG_OBJECT (atdec, "Draining");
600 AudioQueueFlush (atdec->queue);
601
602 while (atdec->input_position > atdec->output_position
603 && flow_ret == GST_FLOW_OK) {
604 flow_ret = gst_atdec_offline_render (atdec, audio_info);
605 }
606
607 if (flow_ret == GST_FLOW_CUSTOM_SUCCESS)
608 flow_ret = GST_FLOW_OK;
609
610 return flow_ret;
611 }
612
613 /* copy the input buffer into an AudioQueueBuffer */
614 size = gst_buffer_get_size (buffer);
615 GST_DEBUG_OBJECT (atdec,
616 "Handling buffer of size %u at timestamp %" GST_TIME_FORMAT, (guint) size,
617 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)));
618 status = AudioQueueAllocateBuffer (atdec->queue, size, &input_buffer);
619 if (status)
620 goto allocate_input_failed;
621 gst_buffer_extract (buffer, 0, input_buffer->mAudioData, size);
622 input_buffer->mAudioDataByteSize = size;
623
624 /* assume framed input */
625 packet.mStartOffset = 0;
626 packet.mVariableFramesInPacket = 1;
627 packet.mDataByteSize = size;
628
629 /* enqueue the buffer. It will get free'd once the gst_atdec_buffer_emptied
630 * callback is called
631 */
632 status = AudioQueueEnqueueBuffer (atdec->queue, input_buffer, 1, &packet);
633 if (status)
634 goto enqueue_buffer_failed;
635
636 atdec->input_position += atdec->spf;
637
638 flow_ret = gst_atdec_offline_render (atdec, audio_info);
639 if (flow_ret == GST_FLOW_CUSTOM_SUCCESS)
640 flow_ret = GST_FLOW_OK;
641
642 return flow_ret;
643
644 allocate_input_failed:
645 {
646 GST_ELEMENT_ERROR (atdec, STREAM, DECODE, (NULL),
647 ("AudioQueueAllocateBuffer returned error: %d", (gint) status));
648 return GST_FLOW_ERROR;
649 }
650
651 enqueue_buffer_failed:
652 {
653 GST_AUDIO_DECODER_ERROR (atdec, 1, STREAM, DECODE, (NULL),
654 ("AudioQueueEnqueueBuffer returned error: %d", (gint) status),
655 flow_ret);
656 return flow_ret;
657 }
658 }
659
660 static void
661 gst_atdec_flush (GstAudioDecoder * decoder, gboolean hard)
662 {
663 GstATDec *atdec = GST_ATDEC (decoder);
664
665 GST_DEBUG_OBJECT (atdec, "Flushing");
666 AudioQueueReset (atdec->queue);
667 atdec->output_position = 0;
668 atdec->input_position = 0;
669 }