2 * Copyright (C) 2011, Hewlett-Packard Development Company, L.P.
3 * Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>, Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation
8 * version 2.1 of the License.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "gstomxaudioenc.h"
30 GST_DEBUG_CATEGORY_STATIC (gst_omx_audio_enc_debug_category);
31 #define GST_CAT_DEFAULT gst_omx_audio_enc_debug_category
34 static void gst_omx_audio_enc_finalize (GObject * object);
36 static GstStateChangeReturn
37 gst_omx_audio_enc_change_state (GstElement * element,
38 GstStateChange transition);
40 static gboolean gst_omx_audio_enc_open (GstAudioEncoder * encoder);
41 static gboolean gst_omx_audio_enc_close (GstAudioEncoder * encoder);
42 static gboolean gst_omx_audio_enc_start (GstAudioEncoder * encoder);
43 static gboolean gst_omx_audio_enc_stop (GstAudioEncoder * encoder);
44 static gboolean gst_omx_audio_enc_set_format (GstAudioEncoder * encoder,
46 static GstFlowReturn gst_omx_audio_enc_handle_frame (GstAudioEncoder *
47 encoder, GstBuffer * buffer);
48 static void gst_omx_audio_enc_flush (GstAudioEncoder * encoder);
50 static GstFlowReturn gst_omx_audio_enc_drain (GstOMXAudioEnc * self);
57 /* class initialization */
60 GST_DEBUG_CATEGORY_INIT (gst_omx_audio_enc_debug_category, "omxaudioenc", 0, \
61 "debug category for gst-omx audio encoder base class");
63 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GstOMXAudioEnc, gst_omx_audio_enc,
64 GST_TYPE_AUDIO_ENCODER, DEBUG_INIT);
67 gst_omx_audio_enc_class_init (GstOMXAudioEncClass * klass)
69 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
70 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
71 GstAudioEncoderClass *audio_encoder_class = GST_AUDIO_ENCODER_CLASS (klass);
73 gobject_class->finalize = gst_omx_audio_enc_finalize;
75 element_class->change_state =
76 GST_DEBUG_FUNCPTR (gst_omx_audio_enc_change_state);
78 audio_encoder_class->open = GST_DEBUG_FUNCPTR (gst_omx_audio_enc_open);
79 audio_encoder_class->close = GST_DEBUG_FUNCPTR (gst_omx_audio_enc_close);
80 audio_encoder_class->start = GST_DEBUG_FUNCPTR (gst_omx_audio_enc_start);
81 audio_encoder_class->stop = GST_DEBUG_FUNCPTR (gst_omx_audio_enc_stop);
82 audio_encoder_class->flush = GST_DEBUG_FUNCPTR (gst_omx_audio_enc_flush);
83 audio_encoder_class->set_format =
84 GST_DEBUG_FUNCPTR (gst_omx_audio_enc_set_format);
85 audio_encoder_class->handle_frame =
86 GST_DEBUG_FUNCPTR (gst_omx_audio_enc_handle_frame);
88 klass->cdata.type = GST_OMX_COMPONENT_TYPE_FILTER;
89 klass->cdata.default_sink_template_caps = "audio/x-raw, "
90 "rate = (int) [ 1, MAX ], "
91 "channels = (int) [ 1, " G_STRINGIFY (OMX_AUDIO_MAXCHANNELS) " ], "
92 "format = (string) { S8, U8, S16LE, S16BE, U16LE, U16BE, "
93 "S24LE, S24BE, U24LE, U24BE, S32LE, S32BE, U32LE, U32BE }";
97 gst_omx_audio_enc_init (GstOMXAudioEnc * self)
99 g_mutex_init (&self->drain_lock);
100 g_cond_init (&self->drain_cond);
104 gst_omx_audio_enc_open (GstAudioEncoder * encoder)
106 GstOMXAudioEnc *self = GST_OMX_AUDIO_ENC (encoder);
107 GstOMXAudioEncClass *klass = GST_OMX_AUDIO_ENC_GET_CLASS (self);
108 gint in_port_index, out_port_index;
111 gst_omx_component_new (GST_OBJECT_CAST (self), klass->cdata.core_name,
112 klass->cdata.component_name, klass->cdata.component_role,
114 self->started = FALSE;
119 if (gst_omx_component_get_state (self->enc,
120 GST_CLOCK_TIME_NONE) != OMX_StateLoaded)
123 in_port_index = klass->cdata.in_port_index;
124 out_port_index = klass->cdata.out_port_index;
126 if (in_port_index == -1 || out_port_index == -1) {
127 OMX_PORT_PARAM_TYPE param;
130 GST_OMX_INIT_STRUCT (¶m);
133 gst_omx_component_get_parameter (self->enc, OMX_IndexParamAudioInit,
135 if (err != OMX_ErrorNone) {
136 GST_WARNING_OBJECT (self, "Couldn't get port information: %s (0x%08x)",
137 gst_omx_error_to_string (err), err);
142 GST_DEBUG_OBJECT (self, "Detected %u ports, starting at %u",
143 (guint) param.nPorts, (guint) param.nStartPortNumber);
144 in_port_index = param.nStartPortNumber + 0;
145 out_port_index = param.nStartPortNumber + 1;
149 self->enc_in_port = gst_omx_component_add_port (self->enc, in_port_index);
150 self->enc_out_port = gst_omx_component_add_port (self->enc, out_port_index);
152 if (!self->enc_in_port || !self->enc_out_port)
160 gst_omx_audio_enc_shutdown (GstOMXAudioEnc * self)
164 GST_DEBUG_OBJECT (self, "Shutting down encoder");
166 state = gst_omx_component_get_state (self->enc, 0);
167 if (state > OMX_StateLoaded || state == OMX_StateInvalid) {
168 if (state > OMX_StateIdle) {
169 gst_omx_component_set_state (self->enc, OMX_StateIdle);
170 gst_omx_component_get_state (self->enc, 5 * GST_SECOND);
172 gst_omx_component_set_state (self->enc, OMX_StateLoaded);
173 gst_omx_port_deallocate_buffers (self->enc_in_port);
174 gst_omx_port_deallocate_buffers (self->enc_out_port);
175 if (state > OMX_StateLoaded)
176 gst_omx_component_get_state (self->enc, 5 * GST_SECOND);
183 gst_omx_audio_enc_close (GstAudioEncoder * encoder)
185 GstOMXAudioEnc *self = GST_OMX_AUDIO_ENC (encoder);
187 GST_DEBUG_OBJECT (self, "Closing encoder");
189 if (!gst_omx_audio_enc_shutdown (self))
192 self->enc_in_port = NULL;
193 self->enc_out_port = NULL;
195 gst_omx_component_free (self->enc);
202 gst_omx_audio_enc_finalize (GObject * object)
204 GstOMXAudioEnc *self = GST_OMX_AUDIO_ENC (object);
206 g_mutex_clear (&self->drain_lock);
207 g_cond_clear (&self->drain_cond);
209 G_OBJECT_CLASS (gst_omx_audio_enc_parent_class)->finalize (object);
212 static GstStateChangeReturn
213 gst_omx_audio_enc_change_state (GstElement * element, GstStateChange transition)
215 GstOMXAudioEnc *self;
216 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
218 g_return_val_if_fail (GST_IS_OMX_AUDIO_ENC (element),
219 GST_STATE_CHANGE_FAILURE);
220 self = GST_OMX_AUDIO_ENC (element);
222 switch (transition) {
223 case GST_STATE_CHANGE_NULL_TO_READY:
225 case GST_STATE_CHANGE_READY_TO_PAUSED:
226 self->downstream_flow_ret = GST_FLOW_OK;
228 self->draining = FALSE;
229 self->started = FALSE;
231 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
233 case GST_STATE_CHANGE_PAUSED_TO_READY:
234 if (self->enc_in_port)
235 gst_omx_port_set_flushing (self->enc_in_port, 5 * GST_SECOND, TRUE);
236 if (self->enc_out_port)
237 gst_omx_port_set_flushing (self->enc_out_port, 5 * GST_SECOND, TRUE);
239 g_mutex_lock (&self->drain_lock);
240 self->draining = FALSE;
241 g_cond_broadcast (&self->drain_cond);
242 g_mutex_unlock (&self->drain_lock);
249 GST_ELEMENT_CLASS (gst_omx_audio_enc_parent_class)->change_state (element,
252 if (ret == GST_STATE_CHANGE_FAILURE)
255 switch (transition) {
256 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
258 case GST_STATE_CHANGE_PAUSED_TO_READY:
259 self->downstream_flow_ret = GST_FLOW_FLUSHING;
260 self->started = FALSE;
262 if (!gst_omx_audio_enc_shutdown (self))
263 ret = GST_STATE_CHANGE_FAILURE;
265 case GST_STATE_CHANGE_READY_TO_NULL:
275 gst_omx_audio_enc_loop (GstOMXAudioEnc * self)
277 GstOMXAudioEncClass *klass;
278 GstOMXPort *port = self->enc_out_port;
279 GstOMXBuffer *buf = NULL;
280 GstFlowReturn flow_ret = GST_FLOW_OK;
281 GstOMXAcquireBufferReturn acq_return;
284 klass = GST_OMX_AUDIO_ENC_GET_CLASS (self);
286 acq_return = gst_omx_port_acquire_buffer (port, &buf);
287 if (acq_return == GST_OMX_ACQUIRE_BUFFER_ERROR) {
288 goto component_error;
289 } else if (acq_return == GST_OMX_ACQUIRE_BUFFER_FLUSHING) {
291 } else if (acq_return == GST_OMX_ACQUIRE_BUFFER_EOS) {
295 if (!gst_pad_has_current_caps (GST_AUDIO_ENCODER_SRC_PAD (self))
296 || acq_return == GST_OMX_ACQUIRE_BUFFER_RECONFIGURE) {
298 gst_audio_encoder_get_audio_info (GST_AUDIO_ENCODER (self));
301 GST_DEBUG_OBJECT (self, "Port settings have changed, updating caps");
303 /* Reallocate all buffers */
304 if (acq_return == GST_OMX_ACQUIRE_BUFFER_RECONFIGURE) {
305 err = gst_omx_port_set_enabled (port, FALSE);
306 if (err != OMX_ErrorNone)
307 goto reconfigure_error;
309 err = gst_omx_port_wait_buffers_released (port, 5 * GST_SECOND);
310 if (err != OMX_ErrorNone)
311 goto reconfigure_error;
313 err = gst_omx_port_deallocate_buffers (port);
314 if (err != OMX_ErrorNone)
315 goto reconfigure_error;
317 err = gst_omx_port_wait_enabled (port, 1 * GST_SECOND);
318 if (err != OMX_ErrorNone)
319 goto reconfigure_error;
323 GST_AUDIO_ENCODER_STREAM_LOCK (self);
325 caps = klass->get_caps (self, self->enc_out_port, info);
328 gst_omx_port_release_buffer (self->enc_out_port, buf);
329 GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
333 GST_DEBUG_OBJECT (self, "Setting output caps: %" GST_PTR_FORMAT, caps);
335 if (!gst_pad_set_caps (GST_AUDIO_ENCODER_SRC_PAD (self), caps)) {
336 gst_caps_unref (caps);
338 gst_omx_port_release_buffer (self->enc_out_port, buf);
339 GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
342 gst_caps_unref (caps);
344 GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
346 if (acq_return == GST_OMX_ACQUIRE_BUFFER_RECONFIGURE) {
347 err = gst_omx_port_set_enabled (port, TRUE);
348 if (err != OMX_ErrorNone)
349 goto reconfigure_error;
351 err = gst_omx_port_allocate_buffers (port);
352 if (err != OMX_ErrorNone)
353 goto reconfigure_error;
355 err = gst_omx_port_wait_enabled (port, 5 * GST_SECOND);
356 if (err != OMX_ErrorNone)
357 goto reconfigure_error;
359 err = gst_omx_port_populate (port);
360 if (err != OMX_ErrorNone)
361 goto reconfigure_error;
363 err = gst_omx_port_mark_reconfigured (port);
364 if (err != OMX_ErrorNone)
365 goto reconfigure_error;
368 /* Now get a buffer */
369 if (acq_return != GST_OMX_ACQUIRE_BUFFER_OK) {
374 g_assert (acq_return == GST_OMX_ACQUIRE_BUFFER_OK);
376 g_assert ((klass->cdata.hacks & GST_OMX_HACK_NO_EMPTY_EOS_BUFFER));
377 GST_AUDIO_ENCODER_STREAM_LOCK (self);
381 GST_DEBUG_OBJECT (self, "Handling buffer: 0x%08x %" G_GUINT64_FORMAT,
382 (guint) buf->omx_buf->nFlags, (guint64) buf->omx_buf->nTimeStamp);
384 /* This prevents a deadlock between the srcpad stream
385 * lock and the videocodec stream lock, if ::reset()
386 * is called at the wrong time
388 if (gst_omx_port_is_flushing (self->enc_out_port)) {
389 GST_DEBUG_OBJECT (self, "Flushing");
390 gst_omx_port_release_buffer (self->enc_out_port, buf);
394 GST_AUDIO_ENCODER_STREAM_LOCK (self);
396 if ((buf->omx_buf->nFlags & OMX_BUFFERFLAG_CODECCONFIG)
397 && buf->omx_buf->nFilledLen > 0) {
399 GstBuffer *codec_data;
400 GstMapInfo map = GST_MAP_INFO_INIT;
402 GST_DEBUG_OBJECT (self, "Handling codec data");
404 gst_caps_copy (gst_pad_get_current_caps (GST_AUDIO_ENCODER_SRC_PAD
406 codec_data = gst_buffer_new_and_alloc (buf->omx_buf->nFilledLen);
408 gst_buffer_map (codec_data, &map, GST_MAP_WRITE);
410 buf->omx_buf->pBuffer + buf->omx_buf->nOffset,
411 buf->omx_buf->nFilledLen);
412 gst_buffer_unmap (codec_data, &map);
414 gst_caps_set_simple (caps, "codec_data", GST_TYPE_BUFFER, codec_data, NULL);
415 if (!gst_pad_set_caps (GST_AUDIO_ENCODER_SRC_PAD (self), caps)) {
416 gst_caps_unref (caps);
418 gst_omx_port_release_buffer (self->enc_out_port, buf);
419 GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
422 gst_caps_unref (caps);
423 flow_ret = GST_FLOW_OK;
424 } else if (buf->omx_buf->nFilledLen > 0) {
428 GST_DEBUG_OBJECT (self, "Handling output data");
431 klass->get_num_samples (self, self->enc_out_port,
432 gst_audio_encoder_get_audio_info (GST_AUDIO_ENCODER (self)), buf);
434 if (buf->omx_buf->nFilledLen > 0) {
435 GstMapInfo map = GST_MAP_INFO_INIT;
436 outbuf = gst_buffer_new_and_alloc (buf->omx_buf->nFilledLen);
438 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
441 buf->omx_buf->pBuffer + buf->omx_buf->nOffset,
442 buf->omx_buf->nFilledLen);
443 gst_buffer_unmap (outbuf, &map);
446 outbuf = gst_buffer_new ();
449 GST_BUFFER_TIMESTAMP (outbuf) =
450 gst_util_uint64_scale (buf->omx_buf->nTimeStamp, GST_SECOND,
451 OMX_TICKS_PER_SECOND);
452 if (buf->omx_buf->nTickCount != 0)
453 GST_BUFFER_DURATION (outbuf) =
454 gst_util_uint64_scale (buf->omx_buf->nTickCount, GST_SECOND,
455 OMX_TICKS_PER_SECOND);
458 gst_audio_encoder_finish_frame (GST_AUDIO_ENCODER (self),
462 GST_DEBUG_OBJECT (self, "Handled output data");
464 GST_DEBUG_OBJECT (self, "Finished frame: %s", gst_flow_get_name (flow_ret));
466 err = gst_omx_port_release_buffer (port, buf);
467 if (err != OMX_ErrorNone)
470 self->downstream_flow_ret = flow_ret;
472 if (flow_ret != GST_FLOW_OK)
475 GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
481 GST_ELEMENT_ERROR (self, LIBRARY, FAILED, (NULL),
482 ("OpenMAX component in error state %s (0x%08x)",
483 gst_omx_component_get_last_error_string (self->enc),
484 gst_omx_component_get_last_error (self->enc)));
485 gst_pad_push_event (GST_AUDIO_ENCODER_SRC_PAD (self), gst_event_new_eos ());
486 gst_pad_pause_task (GST_AUDIO_ENCODER_SRC_PAD (self));
487 self->downstream_flow_ret = GST_FLOW_ERROR;
488 self->started = FALSE;
493 GST_DEBUG_OBJECT (self, "Flushing -- stopping task");
494 gst_pad_pause_task (GST_AUDIO_ENCODER_SRC_PAD (self));
495 self->downstream_flow_ret = GST_FLOW_FLUSHING;
496 self->started = FALSE;
501 g_mutex_lock (&self->drain_lock);
502 if (self->draining) {
503 GST_DEBUG_OBJECT (self, "Drained");
504 self->draining = FALSE;
505 g_cond_broadcast (&self->drain_cond);
506 flow_ret = GST_FLOW_OK;
507 gst_pad_pause_task (GST_AUDIO_ENCODER_SRC_PAD (self));
509 GST_DEBUG_OBJECT (self, "Component signalled EOS");
510 flow_ret = GST_FLOW_EOS;
512 g_mutex_unlock (&self->drain_lock);
514 GST_AUDIO_ENCODER_STREAM_LOCK (self);
515 self->downstream_flow_ret = flow_ret;
517 /* Here we fallback and pause the task for the EOS case */
518 if (flow_ret != GST_FLOW_OK)
521 GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
527 if (flow_ret == GST_FLOW_EOS) {
528 GST_DEBUG_OBJECT (self, "EOS");
530 gst_pad_push_event (GST_AUDIO_ENCODER_SRC_PAD (self),
531 gst_event_new_eos ());
532 gst_pad_pause_task (GST_AUDIO_ENCODER_SRC_PAD (self));
533 self->started = FALSE;
534 } else if (flow_ret < GST_FLOW_EOS) {
535 GST_ELEMENT_ERROR (self, STREAM, FAILED, ("Internal data stream error."),
536 ("stream stopped, reason %s", gst_flow_get_name (flow_ret)));
538 gst_pad_push_event (GST_AUDIO_ENCODER_SRC_PAD (self),
539 gst_event_new_eos ());
540 gst_pad_pause_task (GST_AUDIO_ENCODER_SRC_PAD (self));
541 self->started = FALSE;
542 } else if (flow_ret == GST_FLOW_FLUSHING) {
543 GST_DEBUG_OBJECT (self, "Flushing -- stopping task");
544 gst_pad_pause_task (GST_AUDIO_ENCODER_SRC_PAD (self));
545 self->started = FALSE;
547 GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
552 GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS, (NULL),
553 ("Unable to reconfigure output port"));
554 gst_pad_push_event (GST_AUDIO_ENCODER_SRC_PAD (self), gst_event_new_eos ());
555 gst_pad_pause_task (GST_AUDIO_ENCODER_SRC_PAD (self));
556 self->downstream_flow_ret = GST_FLOW_NOT_NEGOTIATED;
557 self->started = FALSE;
562 GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS, (NULL), ("Failed to set caps"));
563 gst_pad_push_event (GST_AUDIO_ENCODER_SRC_PAD (self), gst_event_new_eos ());
564 gst_pad_pause_task (GST_AUDIO_ENCODER_SRC_PAD (self));
565 self->downstream_flow_ret = GST_FLOW_NOT_NEGOTIATED;
566 self->started = FALSE;
571 GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS, (NULL),
572 ("Failed to relase output buffer to component: %s (0x%08x)",
573 gst_omx_error_to_string (err), err));
574 gst_pad_push_event (GST_AUDIO_ENCODER_SRC_PAD (self), gst_event_new_eos ());
575 gst_pad_pause_task (GST_AUDIO_ENCODER_SRC_PAD (self));
576 self->downstream_flow_ret = GST_FLOW_ERROR;
577 self->started = FALSE;
578 GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
584 gst_omx_audio_enc_start (GstAudioEncoder * encoder)
586 GstOMXAudioEnc *self;
588 self = GST_OMX_AUDIO_ENC (encoder);
590 self->last_upstream_ts = 0;
591 self->downstream_flow_ret = GST_FLOW_OK;
597 gst_omx_audio_enc_stop (GstAudioEncoder * encoder)
599 GstOMXAudioEnc *self;
601 self = GST_OMX_AUDIO_ENC (encoder);
603 GST_DEBUG_OBJECT (self, "Stopping encoder");
605 gst_omx_port_set_flushing (self->enc_in_port, 5 * GST_SECOND, TRUE);
606 gst_omx_port_set_flushing (self->enc_out_port, 5 * GST_SECOND, TRUE);
608 gst_pad_stop_task (GST_AUDIO_ENCODER_SRC_PAD (encoder));
610 if (gst_omx_component_get_state (self->enc, 0) > OMX_StateIdle)
611 gst_omx_component_set_state (self->enc, OMX_StateIdle);
613 self->downstream_flow_ret = GST_FLOW_FLUSHING;
614 self->started = FALSE;
616 g_mutex_lock (&self->drain_lock);
617 self->draining = FALSE;
618 g_cond_broadcast (&self->drain_cond);
619 g_mutex_unlock (&self->drain_lock);
621 gst_omx_component_get_state (self->enc, 5 * GST_SECOND);
627 gst_omx_audio_enc_set_format (GstAudioEncoder * encoder, GstAudioInfo * info)
629 GstOMXAudioEnc *self;
630 GstOMXAudioEncClass *klass;
631 gboolean needs_disable = FALSE;
632 OMX_PARAM_PORTDEFINITIONTYPE port_def;
633 OMX_AUDIO_PARAM_PCMMODETYPE pcm_param;
637 self = GST_OMX_AUDIO_ENC (encoder);
638 klass = GST_OMX_AUDIO_ENC_GET_CLASS (encoder);
640 GST_DEBUG_OBJECT (self, "Setting new caps");
642 /* Set audio encoder base class properties */
643 gst_audio_encoder_set_frame_samples_min (encoder,
644 gst_util_uint64_scale_ceil (OMX_MIN_PCMPAYLOAD_MSEC,
645 GST_MSECOND * info->rate, GST_SECOND));
646 gst_audio_encoder_set_frame_samples_max (encoder, 0);
648 gst_omx_port_get_port_definition (self->enc_in_port, &port_def);
651 gst_omx_component_get_state (self->enc,
652 GST_CLOCK_TIME_NONE) != OMX_StateLoaded;
653 /* If the component is not in Loaded state and a real format change happens
654 * we have to disable the port and re-allocate all buffers. If no real
655 * format change happened we can just exit here.
658 GST_DEBUG_OBJECT (self, "Need to disable and drain encoder");
659 gst_omx_audio_enc_drain (self);
660 gst_omx_port_set_flushing (self->enc_out_port, 5 * GST_SECOND, TRUE);
662 /* Wait until the srcpad loop is finished,
663 * unlock GST_AUDIO_ENCODER_STREAM_LOCK to prevent deadlocks
664 * caused by using this lock from inside the loop function */
665 GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
666 gst_pad_stop_task (GST_AUDIO_ENCODER_SRC_PAD (encoder));
667 GST_AUDIO_ENCODER_STREAM_LOCK (self);
669 if (klass->cdata.hacks & GST_OMX_HACK_NO_COMPONENT_RECONFIGURE) {
670 GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
671 gst_omx_audio_enc_stop (GST_AUDIO_ENCODER (self));
672 gst_omx_audio_enc_close (GST_AUDIO_ENCODER (self));
673 GST_AUDIO_ENCODER_STREAM_LOCK (self);
675 if (!gst_omx_audio_enc_open (GST_AUDIO_ENCODER (self)))
677 needs_disable = FALSE;
679 if (gst_omx_port_set_enabled (self->enc_in_port, FALSE) != OMX_ErrorNone)
681 if (gst_omx_port_set_enabled (self->enc_out_port, FALSE) != OMX_ErrorNone)
683 if (gst_omx_port_wait_buffers_released (self->enc_in_port,
684 5 * GST_SECOND) != OMX_ErrorNone)
686 if (gst_omx_port_wait_buffers_released (self->enc_out_port,
687 1 * GST_SECOND) != OMX_ErrorNone)
689 if (gst_omx_port_deallocate_buffers (self->enc_in_port) != OMX_ErrorNone)
691 if (gst_omx_port_deallocate_buffers (self->enc_out_port) != OMX_ErrorNone)
693 if (gst_omx_port_wait_enabled (self->enc_in_port,
694 1 * GST_SECOND) != OMX_ErrorNone)
696 if (gst_omx_port_wait_enabled (self->enc_out_port,
697 1 * GST_SECOND) != OMX_ErrorNone)
701 GST_DEBUG_OBJECT (self, "Encoder drained and disabled");
704 port_def.format.audio.eEncoding = OMX_AUDIO_CodingPCM;
705 GST_DEBUG_OBJECT (self, "Setting inport port definition");
706 if (gst_omx_port_update_port_definition (self->enc_in_port,
707 &port_def) != OMX_ErrorNone)
710 GST_OMX_INIT_STRUCT (&pcm_param);
711 pcm_param.nPortIndex = self->enc_in_port->index;
712 pcm_param.nChannels = info->channels;
714 ((info->finfo->flags & GST_AUDIO_FORMAT_FLAG_SIGNED) ?
715 OMX_NumericalDataSigned : OMX_NumericalDataUnsigned);
717 ((info->finfo->endianness == G_LITTLE_ENDIAN) ?
718 OMX_EndianLittle : OMX_EndianBig);
719 pcm_param.bInterleaved = OMX_TRUE;
720 pcm_param.nBitPerSample = info->finfo->width;
721 pcm_param.nSamplingRate = info->rate;
722 pcm_param.ePCMMode = OMX_AUDIO_PCMModeLinear;
724 for (i = 0; i < pcm_param.nChannels; i++) {
725 OMX_AUDIO_CHANNELTYPE pos;
727 switch (info->position[i]) {
728 case GST_AUDIO_CHANNEL_POSITION_MONO:
729 case GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER:
730 pos = OMX_AUDIO_ChannelCF;
732 case GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT:
733 pos = OMX_AUDIO_ChannelLF;
735 case GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT:
736 pos = OMX_AUDIO_ChannelRF;
738 case GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT:
739 pos = OMX_AUDIO_ChannelLS;
741 case GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT:
742 pos = OMX_AUDIO_ChannelRS;
744 case GST_AUDIO_CHANNEL_POSITION_LFE1:
745 pos = OMX_AUDIO_ChannelLFE;
747 case GST_AUDIO_CHANNEL_POSITION_REAR_CENTER:
748 pos = OMX_AUDIO_ChannelCS;
750 case GST_AUDIO_CHANNEL_POSITION_REAR_LEFT:
751 pos = OMX_AUDIO_ChannelLR;
753 case GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT:
754 pos = OMX_AUDIO_ChannelRR;
757 pos = OMX_AUDIO_ChannelNone;
760 pcm_param.eChannelMapping[i] = pos;
763 GST_DEBUG_OBJECT (self, "Setting PCM parameters");
765 gst_omx_component_set_parameter (self->enc, OMX_IndexParamAudioPcm,
767 if (err != OMX_ErrorNone) {
768 GST_ERROR_OBJECT (self, "Failed to set PCM parameters: %s (0x%08x)",
769 gst_omx_error_to_string (err), err);
773 if (klass->set_format) {
774 if (!klass->set_format (self, self->enc_in_port, info)) {
775 GST_ERROR_OBJECT (self, "Subclass failed to set the new format");
780 GST_DEBUG_OBJECT (self, "Updating outport port definition");
781 if (gst_omx_port_update_port_definition (self->enc_out_port,
782 NULL) != OMX_ErrorNone)
785 GST_DEBUG_OBJECT (self, "Enabling component");
787 if (gst_omx_port_set_enabled (self->enc_in_port, TRUE) != OMX_ErrorNone)
789 if (gst_omx_port_allocate_buffers (self->enc_in_port) != OMX_ErrorNone)
792 if ((klass->cdata.hacks & GST_OMX_HACK_NO_DISABLE_OUTPORT)) {
793 if (gst_omx_port_set_enabled (self->enc_out_port, TRUE) != OMX_ErrorNone)
795 if (gst_omx_port_allocate_buffers (self->enc_out_port) != OMX_ErrorNone)
798 if (gst_omx_port_wait_enabled (self->enc_out_port,
799 5 * GST_SECOND) != OMX_ErrorNone)
803 if (gst_omx_port_wait_enabled (self->enc_in_port,
804 5 * GST_SECOND) != OMX_ErrorNone)
806 if (gst_omx_port_mark_reconfigured (self->enc_in_port) != OMX_ErrorNone)
809 if (!(klass->cdata.hacks & GST_OMX_HACK_NO_DISABLE_OUTPORT)) {
810 /* Disable output port */
811 if (gst_omx_port_set_enabled (self->enc_out_port, FALSE) != OMX_ErrorNone)
814 if (gst_omx_port_wait_enabled (self->enc_out_port,
815 1 * GST_SECOND) != OMX_ErrorNone)
818 if (gst_omx_component_set_state (self->enc,
819 OMX_StateIdle) != OMX_ErrorNone)
822 /* Need to allocate buffers to reach Idle state */
823 if (gst_omx_port_allocate_buffers (self->enc_in_port) != OMX_ErrorNone)
826 if (gst_omx_component_set_state (self->enc,
827 OMX_StateIdle) != OMX_ErrorNone)
830 /* Need to allocate buffers to reach Idle state */
831 if (gst_omx_port_allocate_buffers (self->enc_in_port) != OMX_ErrorNone)
833 if (gst_omx_port_allocate_buffers (self->enc_out_port) != OMX_ErrorNone)
837 if (gst_omx_component_get_state (self->enc,
838 GST_CLOCK_TIME_NONE) != OMX_StateIdle)
841 if (gst_omx_component_set_state (self->enc,
842 OMX_StateExecuting) != OMX_ErrorNone)
845 if (gst_omx_component_get_state (self->enc,
846 GST_CLOCK_TIME_NONE) != OMX_StateExecuting)
850 /* Unset flushing to allow ports to accept data again */
851 gst_omx_port_set_flushing (self->enc_in_port, 5 * GST_SECOND, FALSE);
852 gst_omx_port_set_flushing (self->enc_out_port, 5 * GST_SECOND, FALSE);
854 if (gst_omx_component_get_last_error (self->enc) != OMX_ErrorNone) {
855 GST_ERROR_OBJECT (self, "Component in error state: %s (0x%08x)",
856 gst_omx_component_get_last_error_string (self->enc),
857 gst_omx_component_get_last_error (self->enc));
861 /* Start the srcpad loop again */
862 GST_DEBUG_OBJECT (self, "Starting task again");
863 self->downstream_flow_ret = GST_FLOW_OK;
864 gst_pad_start_task (GST_AUDIO_ENCODER_SRC_PAD (self),
865 (GstTaskFunction) gst_omx_audio_enc_loop, encoder, NULL);
871 gst_omx_audio_enc_flush (GstAudioEncoder * encoder)
873 GstOMXAudioEnc *self;
875 self = GST_OMX_AUDIO_ENC (encoder);
877 GST_DEBUG_OBJECT (self, "Resetting encoder");
879 gst_omx_port_set_flushing (self->enc_in_port, 5 * GST_SECOND, TRUE);
880 gst_omx_port_set_flushing (self->enc_out_port, 5 * GST_SECOND, TRUE);
882 /* Wait until the srcpad loop is finished */
883 GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
884 GST_PAD_STREAM_LOCK (GST_AUDIO_ENCODER_SRC_PAD (self));
885 GST_PAD_STREAM_UNLOCK (GST_AUDIO_ENCODER_SRC_PAD (self));
886 GST_AUDIO_ENCODER_STREAM_LOCK (self);
888 gst_omx_port_set_flushing (self->enc_in_port, 5 * GST_SECOND, FALSE);
889 gst_omx_port_set_flushing (self->enc_out_port, 5 * GST_SECOND, FALSE);
890 gst_omx_port_populate (self->enc_out_port);
892 /* Start the srcpad loop again */
893 self->last_upstream_ts = 0;
894 self->downstream_flow_ret = GST_FLOW_OK;
895 self->started = FALSE;
896 gst_pad_start_task (GST_AUDIO_ENCODER_SRC_PAD (self),
897 (GstTaskFunction) gst_omx_audio_enc_loop, encoder, NULL);
901 gst_omx_audio_enc_handle_frame (GstAudioEncoder * encoder, GstBuffer * inbuf)
903 GstOMXAcquireBufferReturn acq_ret = GST_OMX_ACQUIRE_BUFFER_ERROR;
904 GstOMXAudioEnc *self;
909 GstClockTime timestamp, duration, timestamp_offset = 0;
912 self = GST_OMX_AUDIO_ENC (encoder);
914 if (self->downstream_flow_ret != GST_FLOW_OK) {
915 return self->downstream_flow_ret;
919 return gst_omx_audio_enc_drain (self);
921 GST_DEBUG_OBJECT (self, "Handling frame");
923 timestamp = GST_BUFFER_TIMESTAMP (inbuf);
924 duration = GST_BUFFER_DURATION (inbuf);
926 port = self->enc_in_port;
928 size = gst_buffer_get_size (inbuf);
929 while (offset < size) {
930 /* Make sure to release the base class stream lock, otherwise
931 * _loop() can't call _finish_frame() and we might block forever
932 * because no input buffers are released */
933 GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
934 acq_ret = gst_omx_port_acquire_buffer (port, &buf);
936 if (acq_ret == GST_OMX_ACQUIRE_BUFFER_ERROR) {
937 GST_AUDIO_ENCODER_STREAM_LOCK (self);
938 goto component_error;
939 } else if (acq_ret == GST_OMX_ACQUIRE_BUFFER_FLUSHING) {
940 GST_AUDIO_ENCODER_STREAM_LOCK (self);
942 } else if (acq_ret == GST_OMX_ACQUIRE_BUFFER_RECONFIGURE) {
943 /* Reallocate all buffers */
944 err = gst_omx_port_set_enabled (port, FALSE);
945 if (err != OMX_ErrorNone) {
946 GST_AUDIO_ENCODER_STREAM_LOCK (self);
947 goto reconfigure_error;
950 err = gst_omx_port_wait_buffers_released (port, 5 * GST_SECOND);
951 if (err != OMX_ErrorNone) {
952 GST_AUDIO_ENCODER_STREAM_LOCK (self);
953 goto reconfigure_error;
956 err = gst_omx_port_deallocate_buffers (port);
957 if (err != OMX_ErrorNone) {
958 GST_AUDIO_ENCODER_STREAM_LOCK (self);
959 goto reconfigure_error;
962 err = gst_omx_port_wait_enabled (port, 1 * GST_SECOND);
963 if (err != OMX_ErrorNone) {
964 GST_AUDIO_ENCODER_STREAM_LOCK (self);
965 goto reconfigure_error;
968 err = gst_omx_port_set_enabled (port, TRUE);
969 if (err != OMX_ErrorNone) {
970 GST_AUDIO_ENCODER_STREAM_LOCK (self);
971 goto reconfigure_error;
974 err = gst_omx_port_allocate_buffers (port);
975 if (err != OMX_ErrorNone) {
976 GST_AUDIO_ENCODER_STREAM_LOCK (self);
977 goto reconfigure_error;
980 err = gst_omx_port_wait_enabled (port, 5 * GST_SECOND);
981 if (err != OMX_ErrorNone) {
982 GST_AUDIO_ENCODER_STREAM_LOCK (self);
983 goto reconfigure_error;
986 err = gst_omx_port_mark_reconfigured (port);
987 if (err != OMX_ErrorNone) {
988 GST_AUDIO_ENCODER_STREAM_LOCK (self);
989 goto reconfigure_error;
992 /* Now get a new buffer and fill it */
993 GST_AUDIO_ENCODER_STREAM_LOCK (self);
996 GST_AUDIO_ENCODER_STREAM_LOCK (self);
998 g_assert (acq_ret == GST_OMX_ACQUIRE_BUFFER_OK && buf != NULL);
1000 if (self->downstream_flow_ret != GST_FLOW_OK) {
1001 gst_omx_port_release_buffer (port, buf);
1002 return self->downstream_flow_ret;
1005 if (buf->omx_buf->nAllocLen - buf->omx_buf->nOffset <= 0) {
1006 gst_omx_port_release_buffer (port, buf);
1010 GST_DEBUG_OBJECT (self, "Handling frame at offset %d", offset);
1012 /* Copy the buffer content in chunks of size as requested
1014 buf->omx_buf->nFilledLen =
1015 MIN (size - offset, buf->omx_buf->nAllocLen - buf->omx_buf->nOffset);
1016 gst_buffer_extract (inbuf, offset,
1017 buf->omx_buf->pBuffer + buf->omx_buf->nOffset,
1018 buf->omx_buf->nFilledLen);
1020 /* Interpolate timestamps if we're passing the buffer
1021 * in multiple chunks */
1022 if (offset != 0 && duration != GST_CLOCK_TIME_NONE) {
1023 timestamp_offset = gst_util_uint64_scale (offset, duration, size);
1026 if (timestamp != GST_CLOCK_TIME_NONE) {
1027 buf->omx_buf->nTimeStamp =
1028 gst_util_uint64_scale (timestamp + timestamp_offset,
1029 OMX_TICKS_PER_SECOND, GST_SECOND);
1030 self->last_upstream_ts = timestamp + timestamp_offset;
1032 if (duration != GST_CLOCK_TIME_NONE) {
1033 buf->omx_buf->nTickCount =
1034 gst_util_uint64_scale (buf->omx_buf->nFilledLen, duration, size);
1035 buf->omx_buf->nTickCount =
1036 gst_util_uint64_scale (buf->omx_buf->nTickCount,
1037 OMX_TICKS_PER_SECOND, GST_SECOND);
1038 self->last_upstream_ts += duration;
1041 offset += buf->omx_buf->nFilledLen;
1042 self->started = TRUE;
1043 err = gst_omx_port_release_buffer (port, buf);
1044 if (err != OMX_ErrorNone)
1048 GST_DEBUG_OBJECT (self, "Passed frame to component");
1050 return self->downstream_flow_ret;
1054 GST_ELEMENT_ERROR (self, LIBRARY, FAILED, (NULL),
1055 ("Got OpenMAX buffer with no free space (%p, %u/%u)", buf,
1056 (guint) buf->omx_buf->nOffset, (guint) buf->omx_buf->nAllocLen));
1057 return GST_FLOW_ERROR;
1061 GST_ELEMENT_ERROR (self, LIBRARY, FAILED, (NULL),
1062 ("OpenMAX component in error state %s (0x%08x)",
1063 gst_omx_component_get_last_error_string (self->enc),
1064 gst_omx_component_get_last_error (self->enc)));
1065 return GST_FLOW_ERROR;
1070 GST_DEBUG_OBJECT (self, "Flushing -- returning FLUSHING");
1071 return GST_FLOW_FLUSHING;
1075 GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS, (NULL),
1076 ("Unable to reconfigure input port"));
1077 return GST_FLOW_ERROR;
1081 GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS, (NULL),
1082 ("Failed to relase input buffer to component: %s (0x%08x)",
1083 gst_omx_error_to_string (err), err));
1084 return GST_FLOW_ERROR;
1088 static GstFlowReturn
1089 gst_omx_audio_enc_drain (GstOMXAudioEnc * self)
1091 GstOMXAudioEncClass *klass;
1093 GstOMXAcquireBufferReturn acq_ret;
1096 GST_DEBUG_OBJECT (self, "Draining component");
1098 klass = GST_OMX_AUDIO_ENC_GET_CLASS (self);
1100 if (!self->started) {
1101 GST_DEBUG_OBJECT (self, "Component not started yet");
1104 self->started = FALSE;
1106 if ((klass->cdata.hacks & GST_OMX_HACK_NO_EMPTY_EOS_BUFFER)) {
1107 GST_WARNING_OBJECT (self, "Component does not support empty EOS buffers");
1111 /* Make sure to release the base class stream lock, otherwise
1112 * _loop() can't call _finish_frame() and we might block forever
1113 * because no input buffers are released */
1114 GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
1116 /* Send an EOS buffer to the component and let the base
1117 * class drop the EOS event. We will send it later when
1118 * the EOS buffer arrives on the output port. */
1119 acq_ret = gst_omx_port_acquire_buffer (self->enc_in_port, &buf);
1120 if (acq_ret != GST_OMX_ACQUIRE_BUFFER_OK) {
1121 GST_AUDIO_ENCODER_STREAM_LOCK (self);
1122 GST_ERROR_OBJECT (self, "Failed to acquire buffer for draining: %d",
1124 return GST_FLOW_ERROR;
1127 g_mutex_lock (&self->drain_lock);
1128 self->draining = TRUE;
1129 buf->omx_buf->nFilledLen = 0;
1130 buf->omx_buf->nTimeStamp =
1131 gst_util_uint64_scale (self->last_upstream_ts, OMX_TICKS_PER_SECOND,
1133 buf->omx_buf->nTickCount = 0;
1134 buf->omx_buf->nFlags |= OMX_BUFFERFLAG_EOS;
1135 err = gst_omx_port_release_buffer (self->enc_in_port, buf);
1136 if (err != OMX_ErrorNone) {
1137 GST_ERROR_OBJECT (self, "Failed to drain component: %s (0x%08x)",
1138 gst_omx_error_to_string (err), err);
1139 GST_AUDIO_ENCODER_STREAM_LOCK (self);
1140 return GST_FLOW_ERROR;
1142 GST_DEBUG_OBJECT (self, "Waiting until component is drained");
1143 g_cond_wait (&self->drain_cond, &self->drain_lock);
1144 GST_DEBUG_OBJECT (self, "Drained component");
1145 g_mutex_unlock (&self->drain_lock);
1146 GST_AUDIO_ENCODER_STREAM_LOCK (self);
1148 self->started = FALSE;