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 g_mutex_lock (&self->drain_lock);
495 if (self->draining) {
496 self->draining = FALSE;
497 g_cond_broadcast (&self->drain_cond);
499 gst_pad_pause_task (GST_AUDIO_ENCODER_SRC_PAD (self));
500 self->downstream_flow_ret = GST_FLOW_FLUSHING;
501 self->started = FALSE;
502 g_mutex_unlock (&self->drain_lock);
507 g_mutex_lock (&self->drain_lock);
508 if (self->draining) {
509 GST_DEBUG_OBJECT (self, "Drained");
510 self->draining = FALSE;
511 g_cond_broadcast (&self->drain_cond);
512 flow_ret = GST_FLOW_OK;
513 gst_pad_pause_task (GST_AUDIO_ENCODER_SRC_PAD (self));
515 GST_DEBUG_OBJECT (self, "Component signalled EOS");
516 flow_ret = GST_FLOW_EOS;
518 g_mutex_unlock (&self->drain_lock);
520 GST_AUDIO_ENCODER_STREAM_LOCK (self);
521 self->downstream_flow_ret = flow_ret;
523 /* Here we fallback and pause the task for the EOS case */
524 if (flow_ret != GST_FLOW_OK)
527 GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
533 if (flow_ret == GST_FLOW_EOS) {
534 GST_DEBUG_OBJECT (self, "EOS");
536 gst_pad_push_event (GST_AUDIO_ENCODER_SRC_PAD (self),
537 gst_event_new_eos ());
538 gst_pad_pause_task (GST_AUDIO_ENCODER_SRC_PAD (self));
539 self->started = FALSE;
540 } else if (flow_ret < GST_FLOW_EOS) {
541 GST_ELEMENT_ERROR (self, STREAM, FAILED, ("Internal data stream error."),
542 ("stream stopped, reason %s", gst_flow_get_name (flow_ret)));
544 gst_pad_push_event (GST_AUDIO_ENCODER_SRC_PAD (self),
545 gst_event_new_eos ());
546 gst_pad_pause_task (GST_AUDIO_ENCODER_SRC_PAD (self));
547 self->started = FALSE;
548 } else if (flow_ret == GST_FLOW_FLUSHING) {
549 GST_DEBUG_OBJECT (self, "Flushing -- stopping task");
550 g_mutex_lock (&self->drain_lock);
551 if (self->draining) {
552 self->draining = FALSE;
553 g_cond_broadcast (&self->drain_cond);
555 gst_pad_pause_task (GST_AUDIO_ENCODER_SRC_PAD (self));
556 self->started = FALSE;
557 g_mutex_unlock (&self->drain_lock);
559 GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
564 GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS, (NULL),
565 ("Unable to reconfigure output port"));
566 gst_pad_push_event (GST_AUDIO_ENCODER_SRC_PAD (self), gst_event_new_eos ());
567 gst_pad_pause_task (GST_AUDIO_ENCODER_SRC_PAD (self));
568 self->downstream_flow_ret = GST_FLOW_NOT_NEGOTIATED;
569 self->started = FALSE;
574 GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS, (NULL), ("Failed to set caps"));
575 gst_pad_push_event (GST_AUDIO_ENCODER_SRC_PAD (self), gst_event_new_eos ());
576 gst_pad_pause_task (GST_AUDIO_ENCODER_SRC_PAD (self));
577 self->downstream_flow_ret = GST_FLOW_NOT_NEGOTIATED;
578 self->started = FALSE;
583 GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS, (NULL),
584 ("Failed to relase output buffer to component: %s (0x%08x)",
585 gst_omx_error_to_string (err), err));
586 gst_pad_push_event (GST_AUDIO_ENCODER_SRC_PAD (self), gst_event_new_eos ());
587 gst_pad_pause_task (GST_AUDIO_ENCODER_SRC_PAD (self));
588 self->downstream_flow_ret = GST_FLOW_ERROR;
589 self->started = FALSE;
590 GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
596 gst_omx_audio_enc_start (GstAudioEncoder * encoder)
598 GstOMXAudioEnc *self;
600 self = GST_OMX_AUDIO_ENC (encoder);
602 self->last_upstream_ts = 0;
603 self->downstream_flow_ret = GST_FLOW_OK;
609 gst_omx_audio_enc_stop (GstAudioEncoder * encoder)
611 GstOMXAudioEnc *self;
613 self = GST_OMX_AUDIO_ENC (encoder);
615 GST_DEBUG_OBJECT (self, "Stopping encoder");
617 gst_omx_port_set_flushing (self->enc_in_port, 5 * GST_SECOND, TRUE);
618 gst_omx_port_set_flushing (self->enc_out_port, 5 * GST_SECOND, TRUE);
620 gst_pad_stop_task (GST_AUDIO_ENCODER_SRC_PAD (encoder));
622 if (gst_omx_component_get_state (self->enc, 0) > OMX_StateIdle)
623 gst_omx_component_set_state (self->enc, OMX_StateIdle);
625 self->downstream_flow_ret = GST_FLOW_FLUSHING;
626 self->started = FALSE;
628 g_mutex_lock (&self->drain_lock);
629 self->draining = FALSE;
630 g_cond_broadcast (&self->drain_cond);
631 g_mutex_unlock (&self->drain_lock);
633 gst_omx_component_get_state (self->enc, 5 * GST_SECOND);
639 gst_omx_audio_enc_set_format (GstAudioEncoder * encoder, GstAudioInfo * info)
641 GstOMXAudioEnc *self;
642 GstOMXAudioEncClass *klass;
643 gboolean needs_disable = FALSE;
644 OMX_PARAM_PORTDEFINITIONTYPE port_def;
645 OMX_AUDIO_PARAM_PCMMODETYPE pcm_param;
649 self = GST_OMX_AUDIO_ENC (encoder);
650 klass = GST_OMX_AUDIO_ENC_GET_CLASS (encoder);
652 GST_DEBUG_OBJECT (self, "Setting new caps");
654 /* Set audio encoder base class properties */
655 gst_audio_encoder_set_frame_samples_min (encoder,
656 gst_util_uint64_scale_ceil (OMX_MIN_PCMPAYLOAD_MSEC,
657 GST_MSECOND * info->rate, GST_SECOND));
658 gst_audio_encoder_set_frame_samples_max (encoder, 0);
660 gst_omx_port_get_port_definition (self->enc_in_port, &port_def);
663 gst_omx_component_get_state (self->enc,
664 GST_CLOCK_TIME_NONE) != OMX_StateLoaded;
665 /* If the component is not in Loaded state and a real format change happens
666 * we have to disable the port and re-allocate all buffers. If no real
667 * format change happened we can just exit here.
670 GST_DEBUG_OBJECT (self, "Need to disable and drain encoder");
671 gst_omx_audio_enc_drain (self);
672 gst_omx_port_set_flushing (self->enc_out_port, 5 * GST_SECOND, TRUE);
674 /* Wait until the srcpad loop is finished,
675 * unlock GST_AUDIO_ENCODER_STREAM_LOCK to prevent deadlocks
676 * caused by using this lock from inside the loop function */
677 GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
678 gst_pad_stop_task (GST_AUDIO_ENCODER_SRC_PAD (encoder));
679 GST_AUDIO_ENCODER_STREAM_LOCK (self);
681 if (klass->cdata.hacks & GST_OMX_HACK_NO_COMPONENT_RECONFIGURE) {
682 GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
683 gst_omx_audio_enc_stop (GST_AUDIO_ENCODER (self));
684 gst_omx_audio_enc_close (GST_AUDIO_ENCODER (self));
685 GST_AUDIO_ENCODER_STREAM_LOCK (self);
687 if (!gst_omx_audio_enc_open (GST_AUDIO_ENCODER (self)))
689 needs_disable = FALSE;
691 if (gst_omx_port_set_enabled (self->enc_in_port, FALSE) != OMX_ErrorNone)
693 if (gst_omx_port_set_enabled (self->enc_out_port, FALSE) != OMX_ErrorNone)
695 if (gst_omx_port_wait_buffers_released (self->enc_in_port,
696 5 * GST_SECOND) != OMX_ErrorNone)
698 if (gst_omx_port_wait_buffers_released (self->enc_out_port,
699 1 * GST_SECOND) != OMX_ErrorNone)
701 if (gst_omx_port_deallocate_buffers (self->enc_in_port) != OMX_ErrorNone)
703 if (gst_omx_port_deallocate_buffers (self->enc_out_port) != OMX_ErrorNone)
705 if (gst_omx_port_wait_enabled (self->enc_in_port,
706 1 * GST_SECOND) != OMX_ErrorNone)
708 if (gst_omx_port_wait_enabled (self->enc_out_port,
709 1 * GST_SECOND) != OMX_ErrorNone)
713 GST_DEBUG_OBJECT (self, "Encoder drained and disabled");
716 port_def.format.audio.eEncoding = OMX_AUDIO_CodingPCM;
717 GST_DEBUG_OBJECT (self, "Setting inport port definition");
718 if (gst_omx_port_update_port_definition (self->enc_in_port,
719 &port_def) != OMX_ErrorNone)
722 GST_OMX_INIT_STRUCT (&pcm_param);
723 pcm_param.nPortIndex = self->enc_in_port->index;
724 pcm_param.nChannels = info->channels;
726 ((info->finfo->flags & GST_AUDIO_FORMAT_FLAG_SIGNED) ?
727 OMX_NumericalDataSigned : OMX_NumericalDataUnsigned);
729 ((info->finfo->endianness == G_LITTLE_ENDIAN) ?
730 OMX_EndianLittle : OMX_EndianBig);
731 pcm_param.bInterleaved = OMX_TRUE;
732 pcm_param.nBitPerSample = info->finfo->width;
733 pcm_param.nSamplingRate = info->rate;
734 pcm_param.ePCMMode = OMX_AUDIO_PCMModeLinear;
736 for (i = 0; i < pcm_param.nChannels; i++) {
737 OMX_AUDIO_CHANNELTYPE pos;
739 switch (info->position[i]) {
740 case GST_AUDIO_CHANNEL_POSITION_MONO:
741 case GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER:
742 pos = OMX_AUDIO_ChannelCF;
744 case GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT:
745 pos = OMX_AUDIO_ChannelLF;
747 case GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT:
748 pos = OMX_AUDIO_ChannelRF;
750 case GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT:
751 pos = OMX_AUDIO_ChannelLS;
753 case GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT:
754 pos = OMX_AUDIO_ChannelRS;
756 case GST_AUDIO_CHANNEL_POSITION_LFE1:
757 pos = OMX_AUDIO_ChannelLFE;
759 case GST_AUDIO_CHANNEL_POSITION_REAR_CENTER:
760 pos = OMX_AUDIO_ChannelCS;
762 case GST_AUDIO_CHANNEL_POSITION_REAR_LEFT:
763 pos = OMX_AUDIO_ChannelLR;
765 case GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT:
766 pos = OMX_AUDIO_ChannelRR;
769 pos = OMX_AUDIO_ChannelNone;
772 pcm_param.eChannelMapping[i] = pos;
775 GST_DEBUG_OBJECT (self, "Setting PCM parameters");
777 gst_omx_component_set_parameter (self->enc, OMX_IndexParamAudioPcm,
779 if (err != OMX_ErrorNone) {
780 GST_ERROR_OBJECT (self, "Failed to set PCM parameters: %s (0x%08x)",
781 gst_omx_error_to_string (err), err);
785 if (klass->set_format) {
786 if (!klass->set_format (self, self->enc_in_port, info)) {
787 GST_ERROR_OBJECT (self, "Subclass failed to set the new format");
792 GST_DEBUG_OBJECT (self, "Updating outport port definition");
793 if (gst_omx_port_update_port_definition (self->enc_out_port,
794 NULL) != OMX_ErrorNone)
797 GST_DEBUG_OBJECT (self, "Enabling component");
799 if (gst_omx_port_set_enabled (self->enc_in_port, TRUE) != OMX_ErrorNone)
801 if (gst_omx_port_allocate_buffers (self->enc_in_port) != OMX_ErrorNone)
804 if ((klass->cdata.hacks & GST_OMX_HACK_NO_DISABLE_OUTPORT)) {
805 if (gst_omx_port_set_enabled (self->enc_out_port, TRUE) != OMX_ErrorNone)
807 if (gst_omx_port_allocate_buffers (self->enc_out_port) != OMX_ErrorNone)
810 if (gst_omx_port_wait_enabled (self->enc_out_port,
811 5 * GST_SECOND) != OMX_ErrorNone)
815 if (gst_omx_port_wait_enabled (self->enc_in_port,
816 5 * GST_SECOND) != OMX_ErrorNone)
818 if (gst_omx_port_mark_reconfigured (self->enc_in_port) != OMX_ErrorNone)
821 if (!(klass->cdata.hacks & GST_OMX_HACK_NO_DISABLE_OUTPORT)) {
822 /* Disable output port */
823 if (gst_omx_port_set_enabled (self->enc_out_port, FALSE) != OMX_ErrorNone)
826 if (gst_omx_port_wait_enabled (self->enc_out_port,
827 1 * GST_SECOND) != OMX_ErrorNone)
830 if (gst_omx_component_set_state (self->enc,
831 OMX_StateIdle) != OMX_ErrorNone)
834 /* Need to allocate buffers to reach Idle state */
835 if (gst_omx_port_allocate_buffers (self->enc_in_port) != OMX_ErrorNone)
838 if (gst_omx_component_set_state (self->enc,
839 OMX_StateIdle) != OMX_ErrorNone)
842 /* Need to allocate buffers to reach Idle state */
843 if (gst_omx_port_allocate_buffers (self->enc_in_port) != OMX_ErrorNone)
845 if (gst_omx_port_allocate_buffers (self->enc_out_port) != OMX_ErrorNone)
849 if (gst_omx_component_get_state (self->enc,
850 GST_CLOCK_TIME_NONE) != OMX_StateIdle)
853 if (gst_omx_component_set_state (self->enc,
854 OMX_StateExecuting) != OMX_ErrorNone)
857 if (gst_omx_component_get_state (self->enc,
858 GST_CLOCK_TIME_NONE) != OMX_StateExecuting)
862 /* Unset flushing to allow ports to accept data again */
863 gst_omx_port_set_flushing (self->enc_in_port, 5 * GST_SECOND, FALSE);
864 gst_omx_port_set_flushing (self->enc_out_port, 5 * GST_SECOND, FALSE);
866 if (gst_omx_component_get_last_error (self->enc) != OMX_ErrorNone) {
867 GST_ERROR_OBJECT (self, "Component in error state: %s (0x%08x)",
868 gst_omx_component_get_last_error_string (self->enc),
869 gst_omx_component_get_last_error (self->enc));
873 /* Start the srcpad loop again */
874 GST_DEBUG_OBJECT (self, "Starting task again");
875 self->downstream_flow_ret = GST_FLOW_OK;
876 gst_pad_start_task (GST_AUDIO_ENCODER_SRC_PAD (self),
877 (GstTaskFunction) gst_omx_audio_enc_loop, encoder, NULL);
883 gst_omx_audio_enc_flush (GstAudioEncoder * encoder)
885 GstOMXAudioEnc *self;
887 self = GST_OMX_AUDIO_ENC (encoder);
889 GST_DEBUG_OBJECT (self, "Resetting encoder");
891 gst_omx_port_set_flushing (self->enc_in_port, 5 * GST_SECOND, TRUE);
892 gst_omx_port_set_flushing (self->enc_out_port, 5 * GST_SECOND, TRUE);
894 /* Wait until the srcpad loop is finished */
895 GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
896 GST_PAD_STREAM_LOCK (GST_AUDIO_ENCODER_SRC_PAD (self));
897 GST_PAD_STREAM_UNLOCK (GST_AUDIO_ENCODER_SRC_PAD (self));
898 GST_AUDIO_ENCODER_STREAM_LOCK (self);
900 gst_omx_port_set_flushing (self->enc_in_port, 5 * GST_SECOND, FALSE);
901 gst_omx_port_set_flushing (self->enc_out_port, 5 * GST_SECOND, FALSE);
902 gst_omx_port_populate (self->enc_out_port);
904 /* Start the srcpad loop again */
905 self->last_upstream_ts = 0;
906 self->downstream_flow_ret = GST_FLOW_OK;
907 self->started = FALSE;
908 gst_pad_start_task (GST_AUDIO_ENCODER_SRC_PAD (self),
909 (GstTaskFunction) gst_omx_audio_enc_loop, encoder, NULL);
913 gst_omx_audio_enc_handle_frame (GstAudioEncoder * encoder, GstBuffer * inbuf)
915 GstOMXAcquireBufferReturn acq_ret = GST_OMX_ACQUIRE_BUFFER_ERROR;
916 GstOMXAudioEnc *self;
921 GstClockTime timestamp, duration, timestamp_offset = 0;
924 self = GST_OMX_AUDIO_ENC (encoder);
926 if (self->downstream_flow_ret != GST_FLOW_OK) {
927 return self->downstream_flow_ret;
931 return gst_omx_audio_enc_drain (self);
933 GST_DEBUG_OBJECT (self, "Handling frame");
935 timestamp = GST_BUFFER_TIMESTAMP (inbuf);
936 duration = GST_BUFFER_DURATION (inbuf);
938 port = self->enc_in_port;
940 size = gst_buffer_get_size (inbuf);
941 while (offset < size) {
942 /* Make sure to release the base class stream lock, otherwise
943 * _loop() can't call _finish_frame() and we might block forever
944 * because no input buffers are released */
945 GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
946 acq_ret = gst_omx_port_acquire_buffer (port, &buf);
948 if (acq_ret == GST_OMX_ACQUIRE_BUFFER_ERROR) {
949 GST_AUDIO_ENCODER_STREAM_LOCK (self);
950 goto component_error;
951 } else if (acq_ret == GST_OMX_ACQUIRE_BUFFER_FLUSHING) {
952 GST_AUDIO_ENCODER_STREAM_LOCK (self);
954 } else if (acq_ret == GST_OMX_ACQUIRE_BUFFER_RECONFIGURE) {
955 /* Reallocate all buffers */
956 err = gst_omx_port_set_enabled (port, FALSE);
957 if (err != OMX_ErrorNone) {
958 GST_AUDIO_ENCODER_STREAM_LOCK (self);
959 goto reconfigure_error;
962 err = gst_omx_port_wait_buffers_released (port, 5 * GST_SECOND);
963 if (err != OMX_ErrorNone) {
964 GST_AUDIO_ENCODER_STREAM_LOCK (self);
965 goto reconfigure_error;
968 err = gst_omx_port_deallocate_buffers (port);
969 if (err != OMX_ErrorNone) {
970 GST_AUDIO_ENCODER_STREAM_LOCK (self);
971 goto reconfigure_error;
974 err = gst_omx_port_wait_enabled (port, 1 * GST_SECOND);
975 if (err != OMX_ErrorNone) {
976 GST_AUDIO_ENCODER_STREAM_LOCK (self);
977 goto reconfigure_error;
980 err = gst_omx_port_set_enabled (port, TRUE);
981 if (err != OMX_ErrorNone) {
982 GST_AUDIO_ENCODER_STREAM_LOCK (self);
983 goto reconfigure_error;
986 err = gst_omx_port_allocate_buffers (port);
987 if (err != OMX_ErrorNone) {
988 GST_AUDIO_ENCODER_STREAM_LOCK (self);
989 goto reconfigure_error;
992 err = gst_omx_port_wait_enabled (port, 5 * GST_SECOND);
993 if (err != OMX_ErrorNone) {
994 GST_AUDIO_ENCODER_STREAM_LOCK (self);
995 goto reconfigure_error;
998 err = gst_omx_port_mark_reconfigured (port);
999 if (err != OMX_ErrorNone) {
1000 GST_AUDIO_ENCODER_STREAM_LOCK (self);
1001 goto reconfigure_error;
1004 /* Now get a new buffer and fill it */
1005 GST_AUDIO_ENCODER_STREAM_LOCK (self);
1008 GST_AUDIO_ENCODER_STREAM_LOCK (self);
1010 g_assert (acq_ret == GST_OMX_ACQUIRE_BUFFER_OK && buf != NULL);
1012 if (self->downstream_flow_ret != GST_FLOW_OK) {
1013 gst_omx_port_release_buffer (port, buf);
1014 return self->downstream_flow_ret;
1017 if (buf->omx_buf->nAllocLen - buf->omx_buf->nOffset <= 0) {
1018 gst_omx_port_release_buffer (port, buf);
1022 GST_DEBUG_OBJECT (self, "Handling frame at offset %d", offset);
1024 /* Copy the buffer content in chunks of size as requested
1026 buf->omx_buf->nFilledLen =
1027 MIN (size - offset, buf->omx_buf->nAllocLen - buf->omx_buf->nOffset);
1028 gst_buffer_extract (inbuf, offset,
1029 buf->omx_buf->pBuffer + buf->omx_buf->nOffset,
1030 buf->omx_buf->nFilledLen);
1032 /* Interpolate timestamps if we're passing the buffer
1033 * in multiple chunks */
1034 if (offset != 0 && duration != GST_CLOCK_TIME_NONE) {
1035 timestamp_offset = gst_util_uint64_scale (offset, duration, size);
1038 if (timestamp != GST_CLOCK_TIME_NONE) {
1039 buf->omx_buf->nTimeStamp =
1040 gst_util_uint64_scale (timestamp + timestamp_offset,
1041 OMX_TICKS_PER_SECOND, GST_SECOND);
1042 self->last_upstream_ts = timestamp + timestamp_offset;
1044 if (duration != GST_CLOCK_TIME_NONE) {
1045 buf->omx_buf->nTickCount =
1046 gst_util_uint64_scale (buf->omx_buf->nFilledLen, duration, size);
1047 buf->omx_buf->nTickCount =
1048 gst_util_uint64_scale (buf->omx_buf->nTickCount,
1049 OMX_TICKS_PER_SECOND, GST_SECOND);
1050 self->last_upstream_ts += duration;
1053 offset += buf->omx_buf->nFilledLen;
1054 self->started = TRUE;
1055 err = gst_omx_port_release_buffer (port, buf);
1056 if (err != OMX_ErrorNone)
1060 GST_DEBUG_OBJECT (self, "Passed frame to component");
1062 return self->downstream_flow_ret;
1066 GST_ELEMENT_ERROR (self, LIBRARY, FAILED, (NULL),
1067 ("Got OpenMAX buffer with no free space (%p, %u/%u)", buf,
1068 (guint) buf->omx_buf->nOffset, (guint) buf->omx_buf->nAllocLen));
1069 return GST_FLOW_ERROR;
1073 GST_ELEMENT_ERROR (self, LIBRARY, FAILED, (NULL),
1074 ("OpenMAX component in error state %s (0x%08x)",
1075 gst_omx_component_get_last_error_string (self->enc),
1076 gst_omx_component_get_last_error (self->enc)));
1077 return GST_FLOW_ERROR;
1082 GST_DEBUG_OBJECT (self, "Flushing -- returning FLUSHING");
1083 return GST_FLOW_FLUSHING;
1087 GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS, (NULL),
1088 ("Unable to reconfigure input port"));
1089 return GST_FLOW_ERROR;
1093 GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS, (NULL),
1094 ("Failed to relase input buffer to component: %s (0x%08x)",
1095 gst_omx_error_to_string (err), err));
1096 return GST_FLOW_ERROR;
1100 static GstFlowReturn
1101 gst_omx_audio_enc_drain (GstOMXAudioEnc * self)
1103 GstOMXAudioEncClass *klass;
1105 GstOMXAcquireBufferReturn acq_ret;
1108 GST_DEBUG_OBJECT (self, "Draining component");
1110 klass = GST_OMX_AUDIO_ENC_GET_CLASS (self);
1112 if (!self->started) {
1113 GST_DEBUG_OBJECT (self, "Component not started yet");
1116 self->started = FALSE;
1118 if ((klass->cdata.hacks & GST_OMX_HACK_NO_EMPTY_EOS_BUFFER)) {
1119 GST_WARNING_OBJECT (self, "Component does not support empty EOS buffers");
1123 /* Make sure to release the base class stream lock, otherwise
1124 * _loop() can't call _finish_frame() and we might block forever
1125 * because no input buffers are released */
1126 GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
1128 /* Send an EOS buffer to the component and let the base
1129 * class drop the EOS event. We will send it later when
1130 * the EOS buffer arrives on the output port. */
1131 acq_ret = gst_omx_port_acquire_buffer (self->enc_in_port, &buf);
1132 if (acq_ret != GST_OMX_ACQUIRE_BUFFER_OK) {
1133 GST_AUDIO_ENCODER_STREAM_LOCK (self);
1134 GST_ERROR_OBJECT (self, "Failed to acquire buffer for draining: %d",
1136 return GST_FLOW_ERROR;
1139 g_mutex_lock (&self->drain_lock);
1140 self->draining = TRUE;
1141 buf->omx_buf->nFilledLen = 0;
1142 buf->omx_buf->nTimeStamp =
1143 gst_util_uint64_scale (self->last_upstream_ts, OMX_TICKS_PER_SECOND,
1145 buf->omx_buf->nTickCount = 0;
1146 buf->omx_buf->nFlags |= OMX_BUFFERFLAG_EOS;
1147 err = gst_omx_port_release_buffer (self->enc_in_port, buf);
1148 if (err != OMX_ErrorNone) {
1149 GST_ERROR_OBJECT (self, "Failed to drain component: %s (0x%08x)",
1150 gst_omx_error_to_string (err), err);
1151 GST_AUDIO_ENCODER_STREAM_LOCK (self);
1152 return GST_FLOW_ERROR;
1154 GST_DEBUG_OBJECT (self, "Waiting until component is drained");
1155 g_cond_wait (&self->drain_cond, &self->drain_lock);
1156 GST_DEBUG_OBJECT (self, "Drained component");
1157 g_mutex_unlock (&self->drain_lock);
1158 GST_AUDIO_ENCODER_STREAM_LOCK (self);
1160 self->started = FALSE;