2011-08-15 13:10:04 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011, Hewlett-Packard Development Company, L.P.
|
|
|
|
* Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>, Collabora Ltd.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation
|
|
|
|
* version 2.1 of the License.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <gst/gst.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "gstomxaudioenc.h"
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (gst_omx_audio_enc_debug_category);
|
|
|
|
#define GST_CAT_DEFAULT gst_omx_audio_enc_debug_category
|
|
|
|
|
|
|
|
/* prototypes */
|
|
|
|
static void gst_omx_audio_enc_finalize (GObject * object);
|
|
|
|
|
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_omx_audio_enc_change_state (GstElement * element,
|
|
|
|
GstStateChange transition);
|
|
|
|
|
2011-11-25 10:31:58 +00:00
|
|
|
static gboolean gst_omx_audio_enc_start (GstAudioEncoder * encoder);
|
|
|
|
static gboolean gst_omx_audio_enc_stop (GstAudioEncoder * encoder);
|
|
|
|
static gboolean gst_omx_audio_enc_set_format (GstAudioEncoder * encoder,
|
|
|
|
GstAudioInfo * info);
|
|
|
|
static gboolean gst_omx_audio_enc_event (GstAudioEncoder * encoder,
|
2011-08-15 13:10:04 +00:00
|
|
|
GstEvent * event);
|
2011-11-25 10:31:58 +00:00
|
|
|
static GstFlowReturn gst_omx_audio_enc_handle_frame (GstAudioEncoder *
|
2011-08-15 13:10:04 +00:00
|
|
|
encoder, GstBuffer * buffer);
|
2011-11-25 10:31:58 +00:00
|
|
|
static void gst_omx_audio_enc_flush (GstAudioEncoder * encoder);
|
2011-08-15 13:10:04 +00:00
|
|
|
|
2011-11-01 15:46:09 +00:00
|
|
|
static GstFlowReturn gst_omx_audio_enc_drain (GstOMXAudioEnc * self);
|
|
|
|
|
2011-08-15 13:10:04 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0
|
|
|
|
};
|
|
|
|
|
|
|
|
/* class initialization */
|
|
|
|
|
|
|
|
#define DEBUG_INIT(bla) \
|
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_omx_audio_enc_debug_category, "omxaudioenc", 0, \
|
|
|
|
"debug category for gst-omx audio encoder base class");
|
|
|
|
|
2011-11-25 10:31:58 +00:00
|
|
|
GST_BOILERPLATE_FULL (GstOMXAudioEnc, gst_omx_audio_enc, GstAudioEncoder,
|
|
|
|
GST_TYPE_AUDIO_ENCODER, DEBUG_INIT);
|
2011-08-15 13:10:04 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gst_omx_audio_enc_base_init (gpointer g_class)
|
|
|
|
{
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
|
|
|
|
GstOMXAudioEncClass *audioenc_class = GST_OMX_AUDIO_ENC_CLASS (g_class);
|
|
|
|
GKeyFile *config;
|
|
|
|
const gchar *element_name;
|
|
|
|
GError *err;
|
|
|
|
gchar *core_name, *component_name, *component_role;
|
|
|
|
gint in_port_index, out_port_index;
|
|
|
|
gchar *template_caps;
|
|
|
|
GstPadTemplate *templ;
|
|
|
|
GstCaps *caps;
|
|
|
|
gchar **hacks;
|
|
|
|
|
|
|
|
element_name =
|
|
|
|
g_type_get_qdata (G_TYPE_FROM_CLASS (g_class),
|
|
|
|
gst_omx_element_name_quark);
|
|
|
|
/* This happens for the base class and abstract subclasses */
|
|
|
|
if (!element_name)
|
|
|
|
return;
|
|
|
|
|
|
|
|
config = gst_omx_get_configuration ();
|
|
|
|
|
|
|
|
/* This will always succeed, see check in plugin_init */
|
|
|
|
core_name = g_key_file_get_string (config, element_name, "core-name", NULL);
|
|
|
|
g_assert (core_name != NULL);
|
|
|
|
audioenc_class->core_name = core_name;
|
|
|
|
component_name =
|
|
|
|
g_key_file_get_string (config, element_name, "component-name", NULL);
|
|
|
|
g_assert (component_name != NULL);
|
|
|
|
audioenc_class->component_name = component_name;
|
|
|
|
|
|
|
|
/* If this fails we simply don't set a role */
|
|
|
|
if ((component_role =
|
|
|
|
g_key_file_get_string (config, element_name, "component-role",
|
|
|
|
NULL))) {
|
|
|
|
GST_DEBUG ("Using component-role '%s' for element '%s'", component_role,
|
|
|
|
element_name);
|
|
|
|
audioenc_class->component_role = component_role;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Now set the inport/outport indizes and assume sane defaults */
|
|
|
|
err = NULL;
|
|
|
|
in_port_index =
|
|
|
|
g_key_file_get_integer (config, element_name, "in-port-index", &err);
|
|
|
|
if (err != NULL) {
|
|
|
|
GST_DEBUG ("No 'in-port-index' set for element '%s', assuming 0: %s",
|
|
|
|
element_name, err->message);
|
|
|
|
in_port_index = 0;
|
|
|
|
g_error_free (err);
|
|
|
|
}
|
|
|
|
audioenc_class->in_port_index = in_port_index;
|
|
|
|
|
|
|
|
err = NULL;
|
|
|
|
out_port_index =
|
|
|
|
g_key_file_get_integer (config, element_name, "out-port-index", &err);
|
|
|
|
if (err != NULL) {
|
|
|
|
GST_DEBUG ("No 'out-port-index' set for element '%s', assuming 1: %s",
|
|
|
|
element_name, err->message);
|
|
|
|
out_port_index = 1;
|
|
|
|
g_error_free (err);
|
|
|
|
}
|
|
|
|
audioenc_class->out_port_index = out_port_index;
|
|
|
|
|
|
|
|
/* Add pad templates */
|
|
|
|
err = NULL;
|
|
|
|
if (!(template_caps =
|
|
|
|
g_key_file_get_string (config, element_name, "sink-template-caps",
|
|
|
|
&err))) {
|
|
|
|
GST_DEBUG
|
|
|
|
("No sink template caps specified for element '%s', using default '%s'",
|
|
|
|
element_name, audioenc_class->default_sink_template_caps);
|
|
|
|
caps = gst_caps_from_string (audioenc_class->default_sink_template_caps);
|
|
|
|
g_assert (caps != NULL);
|
|
|
|
g_error_free (err);
|
|
|
|
} else {
|
|
|
|
caps = gst_caps_from_string (template_caps);
|
|
|
|
if (!caps) {
|
|
|
|
GST_DEBUG
|
|
|
|
("Could not parse sink template caps '%s' for element '%s', using default '%s'",
|
|
|
|
template_caps, element_name,
|
|
|
|
audioenc_class->default_sink_template_caps);
|
|
|
|
caps = gst_caps_from_string (audioenc_class->default_sink_template_caps);
|
|
|
|
g_assert (caps != NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
templ = gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, caps);
|
|
|
|
g_free (template_caps);
|
|
|
|
gst_element_class_add_pad_template (element_class, templ);
|
|
|
|
gst_object_unref (templ);
|
|
|
|
|
|
|
|
err = NULL;
|
|
|
|
if (!(template_caps =
|
|
|
|
g_key_file_get_string (config, element_name, "src-template-caps",
|
|
|
|
&err))) {
|
|
|
|
GST_DEBUG
|
|
|
|
("No src template caps specified for element '%s', using default '%s'",
|
|
|
|
element_name, audioenc_class->default_src_template_caps);
|
|
|
|
caps = gst_caps_from_string (audioenc_class->default_src_template_caps);
|
|
|
|
g_assert (caps != NULL);
|
|
|
|
g_error_free (err);
|
|
|
|
} else {
|
|
|
|
caps = gst_caps_from_string (template_caps);
|
|
|
|
if (!caps) {
|
|
|
|
GST_DEBUG
|
|
|
|
("Could not parse src template caps '%s' for element '%s', using default '%s'",
|
|
|
|
template_caps, element_name,
|
|
|
|
audioenc_class->default_src_template_caps);
|
|
|
|
caps = gst_caps_from_string (audioenc_class->default_src_template_caps);
|
|
|
|
g_assert (caps != NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
templ = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, caps);
|
|
|
|
g_free (template_caps);
|
|
|
|
gst_element_class_add_pad_template (element_class, templ);
|
|
|
|
gst_object_unref (templ);
|
|
|
|
|
|
|
|
if ((hacks =
|
|
|
|
g_key_file_get_string_list (config, element_name, "hacks", NULL,
|
|
|
|
NULL))) {
|
|
|
|
#ifndef GST_DISABLE_GST_DEBUG
|
|
|
|
gchar **walk = hacks;
|
|
|
|
|
|
|
|
while (*walk) {
|
|
|
|
GST_DEBUG ("Using hack: %s", *walk);
|
|
|
|
walk++;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
audioenc_class->hacks = gst_omx_parse_hacks (hacks);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_omx_audio_enc_class_init (GstOMXAudioEncClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
|
2011-11-25 10:31:58 +00:00
|
|
|
GstAudioEncoderClass *audio_encoder_class = GST_AUDIO_ENCODER_CLASS (klass);
|
2011-08-15 13:10:04 +00:00
|
|
|
|
|
|
|
gobject_class->finalize = gst_omx_audio_enc_finalize;
|
|
|
|
|
|
|
|
element_class->change_state =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_omx_audio_enc_change_state);
|
|
|
|
|
2011-11-25 10:31:58 +00:00
|
|
|
audio_encoder_class->start = GST_DEBUG_FUNCPTR (gst_omx_audio_enc_start);
|
|
|
|
audio_encoder_class->stop = GST_DEBUG_FUNCPTR (gst_omx_audio_enc_stop);
|
|
|
|
audio_encoder_class->flush = GST_DEBUG_FUNCPTR (gst_omx_audio_enc_flush);
|
|
|
|
audio_encoder_class->set_format =
|
2011-08-15 13:10:04 +00:00
|
|
|
GST_DEBUG_FUNCPTR (gst_omx_audio_enc_set_format);
|
2011-11-25 10:31:58 +00:00
|
|
|
audio_encoder_class->handle_frame =
|
2011-08-15 13:10:04 +00:00
|
|
|
GST_DEBUG_FUNCPTR (gst_omx_audio_enc_handle_frame);
|
2011-11-25 10:31:58 +00:00
|
|
|
audio_encoder_class->event = GST_DEBUG_FUNCPTR (gst_omx_audio_enc_event);
|
2011-08-15 13:10:04 +00:00
|
|
|
|
|
|
|
klass->default_sink_template_caps = "audio/x-raw-int, "
|
|
|
|
"rate = (int) [ 1, MAX ], "
|
|
|
|
"channels = (int) [ 1, " G_STRINGIFY (OMX_AUDIO_MAXCHANNELS) " ], "
|
|
|
|
"endianness = (int) { LITTLE_ENDIAN, BIG_ENDIAN }, "
|
|
|
|
"width = (int) 8, "
|
|
|
|
"depth = (int) 8, "
|
|
|
|
"signed = (boolean) { true, false }; "
|
|
|
|
"audio/x-raw-int, "
|
|
|
|
"rate = (int) [ 1, MAX ], "
|
|
|
|
"channels = (int) [ 1, " G_STRINGIFY (OMX_AUDIO_MAXCHANNELS) " ], "
|
|
|
|
"endianness = (int) { LITTLE_ENDIAN, BIG_ENDIAN }, "
|
|
|
|
"width = (int) 16, "
|
|
|
|
"depth = (int) 16, "
|
|
|
|
"signed = (boolean) { true, false }; "
|
|
|
|
"audio/x-raw-int, "
|
|
|
|
"rate = (int) [ 1, MAX ], "
|
|
|
|
"channels = (int) [ 1, " G_STRINGIFY (OMX_AUDIO_MAXCHANNELS) " ], "
|
|
|
|
"endianness = (int) { LITTLE_ENDIAN, BIG_ENDIAN }, "
|
|
|
|
"width = (int) 24, "
|
|
|
|
"depth = (int) 24, "
|
|
|
|
"signed = (boolean) { true, false }; "
|
|
|
|
"audio/x-raw-int, "
|
|
|
|
"rate = (int) [ 1, MAX ], "
|
|
|
|
"channels = (int) [ 1, " G_STRINGIFY (OMX_AUDIO_MAXCHANNELS) " ], "
|
|
|
|
"endianness = (int) { LITTLE_ENDIAN, BIG_ENDIAN }, "
|
|
|
|
"width = (int) 32, "
|
|
|
|
"depth = (int) 32, " "signed = (boolean) { true, false }";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_omx_audio_enc_init (GstOMXAudioEnc * self, GstOMXAudioEncClass * klass)
|
|
|
|
{
|
2011-11-01 15:46:09 +00:00
|
|
|
self->drain_lock = g_mutex_new ();
|
|
|
|
self->drain_cond = g_cond_new ();
|
2011-08-15 13:10:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_omx_audio_enc_open (GstOMXAudioEnc * self)
|
|
|
|
{
|
|
|
|
GstOMXAudioEncClass *klass = GST_OMX_AUDIO_ENC_GET_CLASS (self);
|
|
|
|
|
|
|
|
self->component =
|
|
|
|
gst_omx_component_new (GST_OBJECT_CAST (self), klass->core_name,
|
|
|
|
klass->component_name, klass->component_role, klass->hacks);
|
|
|
|
self->started = FALSE;
|
|
|
|
|
|
|
|
if (!self->component)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (gst_omx_component_get_state (self->component,
|
|
|
|
GST_CLOCK_TIME_NONE) != OMX_StateLoaded)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
self->in_port =
|
|
|
|
gst_omx_component_add_port (self->component, klass->in_port_index);
|
|
|
|
self->out_port =
|
|
|
|
gst_omx_component_add_port (self->component, klass->out_port_index);
|
|
|
|
|
|
|
|
if (!self->in_port || !self->out_port)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-11-17 18:19:35 +00:00
|
|
|
|
2011-08-15 13:10:04 +00:00
|
|
|
static gboolean
|
2011-11-17 18:19:35 +00:00
|
|
|
gst_omx_audio_enc_shutdown (GstOMXAudioEnc * self)
|
2011-08-15 13:10:04 +00:00
|
|
|
{
|
|
|
|
OMX_STATETYPE state;
|
|
|
|
|
2011-11-17 18:19:35 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "Shutting down encoder");
|
|
|
|
|
2011-08-15 13:10:04 +00:00
|
|
|
state = gst_omx_component_get_state (self->component, 0);
|
|
|
|
if (state > OMX_StateLoaded || state == OMX_StateInvalid) {
|
2011-09-27 13:08:54 +00:00
|
|
|
if (state > OMX_StateIdle) {
|
|
|
|
gst_omx_component_set_state (self->component, OMX_StateIdle);
|
|
|
|
gst_omx_component_get_state (self->component, 5 * GST_SECOND);
|
|
|
|
}
|
2011-08-15 13:10:04 +00:00
|
|
|
gst_omx_component_set_state (self->component, OMX_StateLoaded);
|
|
|
|
gst_omx_port_deallocate_buffers (self->in_port);
|
|
|
|
gst_omx_port_deallocate_buffers (self->out_port);
|
|
|
|
if (state > OMX_StateLoaded)
|
|
|
|
gst_omx_component_get_state (self->component, 5 * GST_SECOND);
|
|
|
|
}
|
|
|
|
|
2011-11-17 18:19:35 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_omx_audio_enc_close (GstOMXAudioEnc * self)
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (self, "Closing encoder");
|
|
|
|
|
|
|
|
if (!gst_omx_audio_enc_shutdown (self))
|
|
|
|
return FALSE;
|
|
|
|
|
2011-08-15 13:10:04 +00:00
|
|
|
self->in_port = NULL;
|
|
|
|
self->out_port = NULL;
|
|
|
|
if (self->component)
|
|
|
|
gst_omx_component_free (self->component);
|
|
|
|
self->component = NULL;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_omx_audio_enc_finalize (GObject * object)
|
|
|
|
{
|
2011-11-01 15:46:09 +00:00
|
|
|
GstOMXAudioEnc *self = GST_OMX_AUDIO_ENC (object);
|
|
|
|
|
|
|
|
g_mutex_free (self->drain_lock);
|
|
|
|
g_cond_free (self->drain_cond);
|
2011-08-15 13:10:04 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_omx_audio_enc_change_state (GstElement * element, GstStateChange transition)
|
|
|
|
{
|
|
|
|
GstOMXAudioEnc *self;
|
|
|
|
GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GST_IS_OMX_AUDIO_ENC (element),
|
|
|
|
GST_STATE_CHANGE_FAILURE);
|
|
|
|
self = GST_OMX_AUDIO_ENC (element);
|
|
|
|
|
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_NULL_TO_READY:
|
|
|
|
if (!gst_omx_audio_enc_open (self))
|
|
|
|
ret = GST_STATE_CHANGE_FAILURE;
|
|
|
|
break;
|
|
|
|
case GST_STATE_CHANGE_READY_TO_PAUSED:
|
|
|
|
if (self->in_port)
|
|
|
|
gst_omx_port_set_flushing (self->in_port, FALSE);
|
|
|
|
if (self->out_port)
|
|
|
|
gst_omx_port_set_flushing (self->out_port, FALSE);
|
2011-11-01 14:10:12 +00:00
|
|
|
self->downstream_flow_ret = GST_FLOW_OK;
|
2011-11-01 15:46:09 +00:00
|
|
|
|
|
|
|
self->draining = FALSE;
|
2011-11-10 13:39:40 +00:00
|
|
|
self->started = FALSE;
|
2011-08-15 13:10:04 +00:00
|
|
|
break;
|
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
|
|
|
|
break;
|
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_READY:
|
|
|
|
if (self->in_port)
|
|
|
|
gst_omx_port_set_flushing (self->in_port, TRUE);
|
|
|
|
if (self->out_port)
|
|
|
|
gst_omx_port_set_flushing (self->out_port, TRUE);
|
2011-11-01 15:46:09 +00:00
|
|
|
|
|
|
|
g_mutex_lock (self->drain_lock);
|
|
|
|
self->draining = FALSE;
|
|
|
|
g_cond_broadcast (self->drain_cond);
|
|
|
|
g_mutex_unlock (self->drain_lock);
|
2011-08-15 13:10:04 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == GST_STATE_CHANGE_FAILURE)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
|
|
|
|
|
|
|
|
if (ret == GST_STATE_CHANGE_FAILURE)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
|
|
|
|
break;
|
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_READY:
|
2011-11-01 14:10:12 +00:00
|
|
|
self->downstream_flow_ret = GST_FLOW_WRONG_STATE;
|
2011-11-10 13:39:40 +00:00
|
|
|
self->started = FALSE;
|
2011-11-17 18:19:35 +00:00
|
|
|
|
|
|
|
if (!gst_omx_audio_enc_shutdown (self))
|
|
|
|
ret = GST_STATE_CHANGE_FAILURE;
|
2011-08-15 13:10:04 +00:00
|
|
|
break;
|
|
|
|
case GST_STATE_CHANGE_READY_TO_NULL:
|
|
|
|
if (!gst_omx_audio_enc_close (self))
|
|
|
|
ret = GST_STATE_CHANGE_FAILURE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_omx_audio_enc_loop (GstOMXAudioEnc * self)
|
|
|
|
{
|
|
|
|
GstOMXAudioEncClass *klass;
|
|
|
|
GstOMXPort *port = self->out_port;
|
|
|
|
GstOMXBuffer *buf = NULL;
|
|
|
|
GstFlowReturn flow_ret = GST_FLOW_OK;
|
|
|
|
GstOMXAcquireBufferReturn acq_return;
|
2011-11-17 19:26:33 +00:00
|
|
|
gboolean is_eos;
|
2011-08-15 13:10:04 +00:00
|
|
|
|
|
|
|
klass = GST_OMX_AUDIO_ENC_GET_CLASS (self);
|
|
|
|
|
|
|
|
acq_return = gst_omx_port_acquire_buffer (port, &buf);
|
|
|
|
if (acq_return == GST_OMX_ACQUIRE_BUFFER_ERROR) {
|
|
|
|
goto component_error;
|
|
|
|
} else if (acq_return == GST_OMX_ACQUIRE_BUFFER_FLUSHING) {
|
|
|
|
goto flushing;
|
|
|
|
} else if (acq_return == GST_OMX_ACQUIRE_BUFFER_RECONFIGURE) {
|
|
|
|
if (gst_omx_port_reconfigure (self->out_port) != OMX_ErrorNone)
|
|
|
|
goto reconfigure_error;
|
|
|
|
/* And restart the loop */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-11-25 10:31:58 +00:00
|
|
|
if (!GST_PAD_CAPS (GST_AUDIO_ENCODER_SRC_PAD (self))
|
2011-08-15 13:10:04 +00:00
|
|
|
|| acq_return == GST_OMX_ACQUIRE_BUFFER_RECONFIGURED) {
|
2011-11-25 10:31:58 +00:00
|
|
|
GstAudioInfo *info =
|
|
|
|
gst_audio_encoder_get_audio_info (GST_AUDIO_ENCODER (self));
|
2011-08-15 13:10:04 +00:00
|
|
|
GstCaps *caps;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Port settings have changed, updating caps");
|
|
|
|
|
2011-11-25 10:31:58 +00:00
|
|
|
GST_AUDIO_ENCODER_STREAM_LOCK (self);
|
|
|
|
caps = klass->get_caps (self, self->out_port, info);
|
2011-08-15 13:10:04 +00:00
|
|
|
if (!caps) {
|
|
|
|
if (buf)
|
|
|
|
gst_omx_port_release_buffer (self->out_port, buf);
|
2011-11-25 10:31:58 +00:00
|
|
|
GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
|
2011-08-15 13:10:04 +00:00
|
|
|
goto caps_failed;
|
|
|
|
}
|
|
|
|
|
2011-11-25 10:31:58 +00:00
|
|
|
if (!gst_pad_set_caps (GST_AUDIO_ENCODER_SRC_PAD (self), caps)) {
|
2011-08-15 13:10:04 +00:00
|
|
|
gst_caps_unref (caps);
|
|
|
|
if (buf)
|
|
|
|
gst_omx_port_release_buffer (self->out_port, buf);
|
2011-11-25 10:31:58 +00:00
|
|
|
GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
|
2011-08-15 13:10:04 +00:00
|
|
|
goto caps_failed;
|
|
|
|
}
|
|
|
|
gst_caps_unref (caps);
|
2011-11-25 10:31:58 +00:00
|
|
|
GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
|
2011-08-15 13:10:04 +00:00
|
|
|
|
|
|
|
/* Now get a buffer */
|
|
|
|
if (acq_return != GST_OMX_ACQUIRE_BUFFER_OK)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_assert (acq_return == GST_OMX_ACQUIRE_BUFFER_OK && buf != NULL);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Handling buffer: 0x%08x %lu", buf->omx_buf->nFlags,
|
|
|
|
buf->omx_buf->nTimeStamp);
|
|
|
|
|
2011-11-25 10:31:58 +00:00
|
|
|
GST_AUDIO_ENCODER_STREAM_LOCK (self);
|
2011-11-17 19:26:33 +00:00
|
|
|
is_eos = ! !(buf->omx_buf->nFlags & OMX_BUFFERFLAG_EOS);
|
|
|
|
|
2011-08-15 13:10:04 +00:00
|
|
|
if ((buf->omx_buf->nFlags & OMX_BUFFERFLAG_CODECCONFIG)
|
|
|
|
&& buf->omx_buf->nFilledLen > 0) {
|
|
|
|
GstCaps *caps;
|
|
|
|
GstBuffer *codec_data;
|
|
|
|
|
2011-11-25 10:31:58 +00:00
|
|
|
caps = gst_caps_copy (GST_PAD_CAPS (GST_AUDIO_ENCODER_SRC_PAD (self)));
|
2011-08-15 13:10:04 +00:00
|
|
|
codec_data = gst_buffer_new_and_alloc (buf->omx_buf->nFilledLen);
|
|
|
|
memcpy (GST_BUFFER_DATA (codec_data),
|
|
|
|
buf->omx_buf->pBuffer + buf->omx_buf->nOffset,
|
|
|
|
buf->omx_buf->nFilledLen);
|
|
|
|
|
|
|
|
gst_caps_set_simple (caps, "codec_data", GST_TYPE_BUFFER, codec_data, NULL);
|
2011-11-25 10:31:58 +00:00
|
|
|
if (!gst_pad_set_caps (GST_AUDIO_ENCODER_SRC_PAD (self), caps)) {
|
2011-08-15 13:10:04 +00:00
|
|
|
gst_caps_unref (caps);
|
|
|
|
if (buf)
|
|
|
|
gst_omx_port_release_buffer (self->out_port, buf);
|
2011-11-25 10:31:58 +00:00
|
|
|
GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
|
2011-08-15 13:10:04 +00:00
|
|
|
goto caps_failed;
|
|
|
|
}
|
|
|
|
gst_caps_unref (caps);
|
|
|
|
flow_ret = GST_FLOW_OK;
|
|
|
|
} else if (buf->omx_buf->nFilledLen > 0) {
|
|
|
|
GstBuffer *outbuf;
|
2011-08-17 11:03:50 +00:00
|
|
|
guint n_samples;
|
|
|
|
|
|
|
|
n_samples =
|
|
|
|
klass->get_num_samples (self, self->out_port,
|
2011-11-25 10:31:58 +00:00
|
|
|
gst_audio_encoder_get_audio_info (GST_AUDIO_ENCODER (self)), buf);
|
2011-08-15 13:10:04 +00:00
|
|
|
|
|
|
|
if (buf->omx_buf->nFilledLen > 0) {
|
|
|
|
outbuf = gst_buffer_new_and_alloc (buf->omx_buf->nFilledLen);
|
|
|
|
|
|
|
|
memcpy (GST_BUFFER_DATA (outbuf),
|
|
|
|
buf->omx_buf->pBuffer + buf->omx_buf->nOffset,
|
|
|
|
buf->omx_buf->nFilledLen);
|
|
|
|
} else {
|
|
|
|
outbuf = gst_buffer_new ();
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_buffer_set_caps (outbuf,
|
2011-11-25 10:31:58 +00:00
|
|
|
GST_PAD_CAPS (GST_AUDIO_ENCODER_SRC_PAD (self)));
|
2011-08-15 13:10:04 +00:00
|
|
|
|
|
|
|
GST_BUFFER_TIMESTAMP (outbuf) =
|
|
|
|
gst_util_uint64_scale (buf->omx_buf->nTimeStamp, GST_SECOND,
|
|
|
|
OMX_TICKS_PER_SECOND);
|
|
|
|
if (buf->omx_buf->nTickCount != 0)
|
|
|
|
GST_BUFFER_DURATION (outbuf) =
|
|
|
|
gst_util_uint64_scale (buf->omx_buf->nTickCount, GST_SECOND,
|
|
|
|
OMX_TICKS_PER_SECOND);
|
|
|
|
|
|
|
|
flow_ret =
|
2011-11-25 10:31:58 +00:00
|
|
|
gst_audio_encoder_finish_frame (GST_AUDIO_ENCODER (self),
|
2011-08-17 11:03:50 +00:00
|
|
|
outbuf, n_samples);
|
2011-08-15 13:10:04 +00:00
|
|
|
}
|
|
|
|
|
2011-11-17 19:26:33 +00:00
|
|
|
if (is_eos || flow_ret == GST_FLOW_UNEXPECTED) {
|
2011-11-01 15:46:09 +00:00
|
|
|
g_mutex_lock (self->drain_lock);
|
|
|
|
if (self->draining) {
|
2011-11-10 13:56:19 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "Drained");
|
2011-11-01 15:46:09 +00:00
|
|
|
self->draining = FALSE;
|
|
|
|
g_cond_broadcast (self->drain_cond);
|
2011-11-10 13:56:19 +00:00
|
|
|
} else if (flow_ret == GST_FLOW_OK) {
|
|
|
|
GST_DEBUG_OBJECT (self, "Component signalled EOS");
|
2011-11-01 15:46:09 +00:00
|
|
|
flow_ret = GST_FLOW_UNEXPECTED;
|
|
|
|
}
|
|
|
|
g_mutex_unlock (self->drain_lock);
|
2011-11-10 13:56:19 +00:00
|
|
|
} else {
|
|
|
|
GST_DEBUG_OBJECT (self, "Finished frame: %s", gst_flow_get_name (flow_ret));
|
2011-11-01 15:46:09 +00:00
|
|
|
}
|
2011-08-15 13:10:04 +00:00
|
|
|
|
|
|
|
gst_omx_port_release_buffer (port, buf);
|
|
|
|
|
2011-11-01 14:10:12 +00:00
|
|
|
self->downstream_flow_ret = flow_ret;
|
|
|
|
|
2011-08-15 13:10:04 +00:00
|
|
|
if (flow_ret != GST_FLOW_OK)
|
|
|
|
goto flow_error;
|
|
|
|
|
2011-11-25 10:31:58 +00:00
|
|
|
GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
|
2011-11-10 14:10:14 +00:00
|
|
|
|
2011-08-15 13:10:04 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
component_error:
|
|
|
|
{
|
|
|
|
GST_ELEMENT_ERROR (self, LIBRARY, FAILED, (NULL),
|
|
|
|
("OpenMAX component in error state %s (0x%08x)",
|
|
|
|
gst_omx_component_get_last_error_string (self->component),
|
|
|
|
gst_omx_component_get_last_error (self->component)));
|
2011-11-25 10:31:58 +00:00
|
|
|
gst_pad_push_event (GST_AUDIO_ENCODER_SRC_PAD (self), gst_event_new_eos ());
|
|
|
|
gst_pad_pause_task (GST_AUDIO_ENCODER_SRC_PAD (self));
|
2011-11-01 14:10:12 +00:00
|
|
|
self->downstream_flow_ret = GST_FLOW_ERROR;
|
2011-11-08 10:07:01 +00:00
|
|
|
self->started = FALSE;
|
2011-08-15 13:10:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
flushing:
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (self, "Flushing -- stopping task");
|
2011-11-25 10:31:58 +00:00
|
|
|
gst_pad_pause_task (GST_AUDIO_ENCODER_SRC_PAD (self));
|
2011-11-01 14:10:12 +00:00
|
|
|
self->downstream_flow_ret = GST_FLOW_WRONG_STATE;
|
2011-11-08 10:07:01 +00:00
|
|
|
self->started = FALSE;
|
2011-08-15 13:10:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
flow_error:
|
|
|
|
{
|
|
|
|
if (flow_ret == GST_FLOW_UNEXPECTED) {
|
|
|
|
GST_DEBUG_OBJECT (self, "EOS");
|
|
|
|
|
2011-11-25 10:31:58 +00:00
|
|
|
gst_pad_push_event (GST_AUDIO_ENCODER_SRC_PAD (self),
|
2011-08-15 13:10:04 +00:00
|
|
|
gst_event_new_eos ());
|
2011-11-25 10:31:58 +00:00
|
|
|
gst_pad_pause_task (GST_AUDIO_ENCODER_SRC_PAD (self));
|
2011-08-15 13:10:04 +00:00
|
|
|
} else if (flow_ret == GST_FLOW_NOT_LINKED
|
|
|
|
|| flow_ret < GST_FLOW_UNEXPECTED) {
|
|
|
|
GST_ELEMENT_ERROR (self, STREAM, FAILED, ("Internal data stream error."),
|
|
|
|
("stream stopped, reason %s", gst_flow_get_name (flow_ret)));
|
|
|
|
|
2011-11-25 10:31:58 +00:00
|
|
|
gst_pad_push_event (GST_AUDIO_ENCODER_SRC_PAD (self),
|
2011-08-15 13:10:04 +00:00
|
|
|
gst_event_new_eos ());
|
2011-11-25 10:31:58 +00:00
|
|
|
gst_pad_pause_task (GST_AUDIO_ENCODER_SRC_PAD (self));
|
2011-08-15 13:10:04 +00:00
|
|
|
}
|
2011-11-08 10:07:01 +00:00
|
|
|
self->started = FALSE;
|
2011-11-25 10:31:58 +00:00
|
|
|
GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
|
2011-08-15 13:10:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
reconfigure_error:
|
|
|
|
{
|
|
|
|
GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS, (NULL),
|
|
|
|
("Unable to reconfigure output port"));
|
2011-11-25 10:31:58 +00:00
|
|
|
gst_pad_push_event (GST_AUDIO_ENCODER_SRC_PAD (self), gst_event_new_eos ());
|
|
|
|
gst_pad_pause_task (GST_AUDIO_ENCODER_SRC_PAD (self));
|
2011-11-01 14:10:12 +00:00
|
|
|
self->downstream_flow_ret = GST_FLOW_NOT_NEGOTIATED;
|
2011-11-08 10:07:01 +00:00
|
|
|
self->started = FALSE;
|
2011-08-15 13:10:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
caps_failed:
|
|
|
|
{
|
|
|
|
GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS, (NULL), ("Failed to set caps"));
|
2011-11-25 10:31:58 +00:00
|
|
|
gst_pad_push_event (GST_AUDIO_ENCODER_SRC_PAD (self), gst_event_new_eos ());
|
|
|
|
gst_pad_pause_task (GST_AUDIO_ENCODER_SRC_PAD (self));
|
2011-11-01 14:10:12 +00:00
|
|
|
self->downstream_flow_ret = GST_FLOW_NOT_NEGOTIATED;
|
2011-11-08 10:07:01 +00:00
|
|
|
self->started = FALSE;
|
2011-08-15 13:10:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2011-11-25 10:31:58 +00:00
|
|
|
gst_omx_audio_enc_start (GstAudioEncoder * encoder)
|
2011-08-15 13:10:04 +00:00
|
|
|
{
|
|
|
|
GstOMXAudioEnc *self;
|
|
|
|
gboolean ret;
|
|
|
|
|
|
|
|
self = GST_OMX_AUDIO_ENC (encoder);
|
|
|
|
|
2011-11-08 10:07:01 +00:00
|
|
|
self->eos = FALSE;
|
2011-11-01 14:10:12 +00:00
|
|
|
self->downstream_flow_ret = GST_FLOW_OK;
|
2011-08-15 13:10:04 +00:00
|
|
|
ret =
|
2011-11-25 10:31:58 +00:00
|
|
|
gst_pad_start_task (GST_AUDIO_ENCODER_SRC_PAD (self),
|
2011-08-15 13:10:04 +00:00
|
|
|
(GstTaskFunction) gst_omx_audio_enc_loop, self);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2011-11-25 10:31:58 +00:00
|
|
|
gst_omx_audio_enc_stop (GstAudioEncoder * encoder)
|
2011-08-15 13:10:04 +00:00
|
|
|
{
|
|
|
|
GstOMXAudioEnc *self;
|
|
|
|
|
|
|
|
self = GST_OMX_AUDIO_ENC (encoder);
|
|
|
|
|
2011-11-18 18:00:31 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "Stopping encoder");
|
|
|
|
|
|
|
|
gst_omx_port_set_flushing (self->in_port, TRUE);
|
|
|
|
gst_omx_port_set_flushing (self->out_port, TRUE);
|
|
|
|
|
2011-11-25 10:31:58 +00:00
|
|
|
gst_pad_stop_task (GST_AUDIO_ENCODER_SRC_PAD (encoder));
|
2011-08-15 13:10:04 +00:00
|
|
|
|
|
|
|
if (gst_omx_component_get_state (self->component, 0) > OMX_StateIdle)
|
|
|
|
gst_omx_component_set_state (self->component, OMX_StateIdle);
|
|
|
|
|
2011-11-01 14:10:12 +00:00
|
|
|
self->downstream_flow_ret = GST_FLOW_WRONG_STATE;
|
2011-11-08 10:07:01 +00:00
|
|
|
self->started = FALSE;
|
|
|
|
self->eos = FALSE;
|
2011-11-01 15:46:09 +00:00
|
|
|
|
|
|
|
g_mutex_lock (self->drain_lock);
|
|
|
|
self->draining = FALSE;
|
|
|
|
g_cond_broadcast (self->drain_cond);
|
|
|
|
g_mutex_unlock (self->drain_lock);
|
|
|
|
|
2011-08-15 13:10:04 +00:00
|
|
|
gst_omx_component_get_state (self->component, 5 * GST_SECOND);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2011-11-25 10:31:58 +00:00
|
|
|
gst_omx_audio_enc_set_format (GstAudioEncoder * encoder, GstAudioInfo * info)
|
2011-08-15 13:10:04 +00:00
|
|
|
{
|
|
|
|
GstOMXAudioEnc *self;
|
|
|
|
GstOMXAudioEncClass *klass;
|
|
|
|
gboolean needs_disable = FALSE;
|
|
|
|
OMX_PARAM_PORTDEFINITIONTYPE port_def;
|
|
|
|
OMX_AUDIO_PARAM_PCMMODETYPE pcm_param;
|
|
|
|
gint i;
|
|
|
|
OMX_ERRORTYPE err;
|
|
|
|
|
|
|
|
self = GST_OMX_AUDIO_ENC (encoder);
|
|
|
|
klass = GST_OMX_AUDIO_ENC_GET_CLASS (encoder);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Setting new caps");
|
|
|
|
|
2011-08-17 12:33:31 +00:00
|
|
|
/* Set audio encoder base class properties */
|
2011-11-25 10:31:58 +00:00
|
|
|
gst_audio_encoder_set_frame_samples_min (encoder,
|
2011-08-17 12:33:31 +00:00
|
|
|
gst_util_uint64_scale_ceil (OMX_MIN_PCMPAYLOAD_MSEC,
|
2011-11-25 10:31:58 +00:00
|
|
|
GST_MSECOND * info->rate, GST_SECOND));
|
|
|
|
gst_audio_encoder_set_frame_samples_max (encoder, 0);
|
2011-08-17 12:33:31 +00:00
|
|
|
|
2011-08-15 13:10:04 +00:00
|
|
|
gst_omx_port_get_port_definition (self->in_port, &port_def);
|
|
|
|
|
|
|
|
needs_disable =
|
|
|
|
gst_omx_component_get_state (self->component,
|
|
|
|
GST_CLOCK_TIME_NONE) != OMX_StateLoaded;
|
|
|
|
/* If the component is not in Loaded state and a real format change happens
|
|
|
|
* we have to disable the port and re-allocate all buffers. If no real
|
|
|
|
* format change happened we can just exit here.
|
|
|
|
*/
|
|
|
|
if (needs_disable) {
|
2011-11-08 10:07:01 +00:00
|
|
|
gst_omx_audio_enc_drain (self);
|
2011-11-04 08:43:48 +00:00
|
|
|
|
2011-08-15 13:10:04 +00:00
|
|
|
if (gst_omx_port_manual_reconfigure (self->in_port, TRUE) != OMX_ErrorNone)
|
|
|
|
return FALSE;
|
|
|
|
if (gst_omx_port_set_enabled (self->in_port, FALSE) != OMX_ErrorNone)
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
port_def.format.audio.eEncoding = OMX_AUDIO_CodingPCM;
|
|
|
|
if (!gst_omx_port_update_port_definition (self->in_port, &port_def))
|
|
|
|
return FALSE;
|
|
|
|
if (!gst_omx_port_update_port_definition (self->out_port, NULL))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
GST_OMX_INIT_STRUCT (&pcm_param);
|
|
|
|
pcm_param.nPortIndex = self->in_port->index;
|
2011-11-25 10:31:58 +00:00
|
|
|
pcm_param.nChannels = info->channels;
|
2011-08-15 13:10:04 +00:00
|
|
|
pcm_param.eNumData =
|
2011-11-25 10:31:58 +00:00
|
|
|
((info->finfo->flags & GST_AUDIO_FORMAT_FLAG_SIGNED) ?
|
|
|
|
OMX_NumericalDataSigned : OMX_NumericalDataUnsigned);
|
2011-08-15 13:10:04 +00:00
|
|
|
pcm_param.eEndian =
|
2011-11-25 10:31:58 +00:00
|
|
|
((info->finfo->endianness == G_LITTLE_ENDIAN) ?
|
|
|
|
OMX_EndianLittle : OMX_EndianBig);
|
2011-08-15 13:10:04 +00:00
|
|
|
pcm_param.bInterleaved = OMX_TRUE;
|
2011-11-25 10:31:58 +00:00
|
|
|
pcm_param.nBitPerSample = info->finfo->width;
|
|
|
|
pcm_param.nSamplingRate = info->rate;
|
2011-08-15 13:10:04 +00:00
|
|
|
pcm_param.ePCMMode = OMX_AUDIO_PCMModeLinear;
|
|
|
|
|
|
|
|
for (i = 0; i < pcm_param.nChannels; i++) {
|
|
|
|
OMX_AUDIO_CHANNELTYPE pos;
|
|
|
|
|
2011-11-25 10:31:58 +00:00
|
|
|
switch (info->position[i]) {
|
2011-08-15 13:10:04 +00:00
|
|
|
case GST_AUDIO_CHANNEL_POSITION_FRONT_MONO:
|
|
|
|
case GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER:
|
|
|
|
pos = OMX_AUDIO_ChannelCF;
|
|
|
|
break;
|
|
|
|
case GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT:
|
|
|
|
pos = OMX_AUDIO_ChannelLF;
|
|
|
|
break;
|
|
|
|
case GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT:
|
|
|
|
pos = OMX_AUDIO_ChannelRF;
|
|
|
|
break;
|
|
|
|
case GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT:
|
|
|
|
pos = OMX_AUDIO_ChannelLS;
|
|
|
|
break;
|
|
|
|
case GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT:
|
|
|
|
pos = OMX_AUDIO_ChannelRS;
|
|
|
|
break;
|
|
|
|
case GST_AUDIO_CHANNEL_POSITION_LFE:
|
|
|
|
pos = OMX_AUDIO_ChannelLFE;
|
|
|
|
break;
|
|
|
|
case GST_AUDIO_CHANNEL_POSITION_REAR_CENTER:
|
|
|
|
pos = OMX_AUDIO_ChannelCS;
|
|
|
|
break;
|
|
|
|
case GST_AUDIO_CHANNEL_POSITION_REAR_LEFT:
|
|
|
|
pos = OMX_AUDIO_ChannelLR;
|
|
|
|
break;
|
|
|
|
case GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT:
|
|
|
|
pos = OMX_AUDIO_ChannelRR;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
pos = OMX_AUDIO_ChannelNone;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pcm_param.eChannelMapping[i] = pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
err =
|
|
|
|
gst_omx_component_set_parameter (self->component, OMX_IndexParamAudioPcm,
|
|
|
|
&pcm_param);
|
|
|
|
if (err != OMX_ErrorNone) {
|
|
|
|
GST_ERROR_OBJECT (self, "Failed to set PCM parameters: %s (0x%08x)",
|
|
|
|
gst_omx_error_to_string (err), err);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (klass->set_format) {
|
2011-11-25 10:31:58 +00:00
|
|
|
if (!klass->set_format (self, self->in_port, info)) {
|
2011-08-15 13:10:04 +00:00
|
|
|
GST_ERROR_OBJECT (self, "Subclass failed to set the new format");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (needs_disable) {
|
|
|
|
if (gst_omx_port_set_enabled (self->in_port, TRUE) != OMX_ErrorNone)
|
|
|
|
return FALSE;
|
|
|
|
if (gst_omx_port_manual_reconfigure (self->in_port, FALSE) != OMX_ErrorNone)
|
|
|
|
return FALSE;
|
|
|
|
} else {
|
|
|
|
if (gst_omx_component_set_state (self->component,
|
|
|
|
OMX_StateIdle) != OMX_ErrorNone)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* Need to allocate buffers to reach Idle state */
|
|
|
|
if (gst_omx_port_allocate_buffers (self->in_port) != OMX_ErrorNone)
|
|
|
|
return FALSE;
|
|
|
|
if (gst_omx_port_allocate_buffers (self->out_port) != OMX_ErrorNone)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (gst_omx_component_get_state (self->component,
|
|
|
|
GST_CLOCK_TIME_NONE) != OMX_StateIdle)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (gst_omx_component_set_state (self->component,
|
|
|
|
OMX_StateExecuting) != OMX_ErrorNone)
|
|
|
|
return FALSE;
|
2011-09-14 08:15:38 +00:00
|
|
|
|
|
|
|
if (gst_omx_component_get_state (self->component,
|
|
|
|
GST_CLOCK_TIME_NONE) != OMX_StateExecuting)
|
|
|
|
return FALSE;
|
2011-08-15 13:10:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Unset flushing to allow ports to accept data again */
|
|
|
|
gst_omx_port_set_flushing (self->in_port, FALSE);
|
|
|
|
gst_omx_port_set_flushing (self->out_port, FALSE);
|
|
|
|
|
|
|
|
if (gst_omx_component_get_last_error (self->component) != OMX_ErrorNone) {
|
|
|
|
GST_ERROR_OBJECT (self, "Component in error state: %s (0x%08x)",
|
|
|
|
gst_omx_component_get_last_error_string (self->component),
|
|
|
|
gst_omx_component_get_last_error (self->component));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Start the srcpad loop again */
|
2011-11-01 14:10:12 +00:00
|
|
|
self->downstream_flow_ret = GST_FLOW_OK;
|
2011-11-25 10:31:58 +00:00
|
|
|
gst_pad_start_task (GST_AUDIO_ENCODER_SRC_PAD (self),
|
2011-08-15 13:10:04 +00:00
|
|
|
(GstTaskFunction) gst_omx_audio_enc_loop, encoder);
|
|
|
|
|
2011-09-14 08:15:38 +00:00
|
|
|
return TRUE;
|
2011-08-15 13:10:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-11-25 10:31:58 +00:00
|
|
|
gst_omx_audio_enc_flush (GstAudioEncoder * encoder)
|
2011-08-15 13:10:04 +00:00
|
|
|
{
|
|
|
|
GstOMXAudioEnc *self;
|
|
|
|
|
|
|
|
self = GST_OMX_AUDIO_ENC (encoder);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Resetting encoder");
|
|
|
|
|
2011-11-10 14:03:05 +00:00
|
|
|
gst_omx_audio_enc_drain (self);
|
2011-11-01 15:46:09 +00:00
|
|
|
|
2011-11-10 14:03:05 +00:00
|
|
|
gst_omx_port_set_flushing (self->in_port, TRUE);
|
|
|
|
gst_omx_port_set_flushing (self->out_port, TRUE);
|
2011-08-15 13:10:04 +00:00
|
|
|
|
2011-11-10 14:03:05 +00:00
|
|
|
/* Wait until the srcpad loop is finished */
|
2011-11-25 10:31:58 +00:00
|
|
|
GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
|
|
|
|
GST_PAD_STREAM_LOCK (GST_AUDIO_ENCODER_SRC_PAD (self));
|
|
|
|
GST_PAD_STREAM_UNLOCK (GST_AUDIO_ENCODER_SRC_PAD (self));
|
|
|
|
GST_AUDIO_ENCODER_STREAM_LOCK (self);
|
2011-08-15 13:10:04 +00:00
|
|
|
|
2011-11-10 14:03:05 +00:00
|
|
|
gst_omx_port_set_flushing (self->in_port, FALSE);
|
|
|
|
gst_omx_port_set_flushing (self->out_port, FALSE);
|
2011-08-15 13:10:04 +00:00
|
|
|
|
|
|
|
/* Start the srcpad loop again */
|
2011-11-01 14:10:12 +00:00
|
|
|
self->downstream_flow_ret = GST_FLOW_OK;
|
2011-11-08 10:07:01 +00:00
|
|
|
self->eos = FALSE;
|
2011-11-25 10:31:58 +00:00
|
|
|
gst_pad_start_task (GST_AUDIO_ENCODER_SRC_PAD (self),
|
2011-08-15 13:10:04 +00:00
|
|
|
(GstTaskFunction) gst_omx_audio_enc_loop, encoder);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
2011-11-25 10:31:58 +00:00
|
|
|
gst_omx_audio_enc_handle_frame (GstAudioEncoder * encoder, GstBuffer * inbuf)
|
2011-08-15 13:10:04 +00:00
|
|
|
{
|
|
|
|
GstOMXAcquireBufferReturn acq_ret = GST_OMX_ACQUIRE_BUFFER_ERROR;
|
|
|
|
GstOMXAudioEnc *self;
|
|
|
|
GstOMXBuffer *buf;
|
|
|
|
guint offset = 0;
|
|
|
|
GstClockTime timestamp, duration, timestamp_offset = 0;
|
|
|
|
|
|
|
|
self = GST_OMX_AUDIO_ENC (encoder);
|
|
|
|
|
2011-11-08 10:07:01 +00:00
|
|
|
if (self->eos) {
|
|
|
|
GST_WARNING_OBJECT (self, "Got frame after EOS");
|
|
|
|
return GST_FLOW_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2011-11-01 14:10:12 +00:00
|
|
|
if (self->downstream_flow_ret != GST_FLOW_OK) {
|
|
|
|
GST_ERROR_OBJECT (self, "Downstream returned %s",
|
|
|
|
gst_flow_get_name (self->downstream_flow_ret));
|
|
|
|
|
|
|
|
return self->downstream_flow_ret;
|
|
|
|
}
|
|
|
|
|
2011-08-17 12:17:18 +00:00
|
|
|
if (inbuf == NULL)
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
|
2011-08-15 13:10:04 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "Handling frame");
|
|
|
|
|
|
|
|
timestamp = GST_BUFFER_TIMESTAMP (inbuf);
|
|
|
|
duration = GST_BUFFER_DURATION (inbuf);
|
|
|
|
|
|
|
|
while (offset < GST_BUFFER_SIZE (inbuf)) {
|
2011-08-17 09:34:31 +00:00
|
|
|
/* Make sure to release the base class stream lock, otherwise
|
|
|
|
* _loop() can't call _finish_frame() and we might block forever
|
|
|
|
* because no input buffers are released */
|
2011-11-25 10:31:58 +00:00
|
|
|
GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
|
2011-08-15 13:10:04 +00:00
|
|
|
acq_ret = gst_omx_port_acquire_buffer (self->in_port, &buf);
|
2011-11-25 10:31:58 +00:00
|
|
|
GST_AUDIO_ENCODER_STREAM_LOCK (self);
|
2011-08-15 13:10:04 +00:00
|
|
|
|
|
|
|
if (acq_ret == GST_OMX_ACQUIRE_BUFFER_ERROR) {
|
|
|
|
goto component_error;
|
|
|
|
} else if (acq_ret == GST_OMX_ACQUIRE_BUFFER_FLUSHING) {
|
|
|
|
goto flushing;
|
|
|
|
} else if (acq_ret == GST_OMX_ACQUIRE_BUFFER_RECONFIGURE) {
|
|
|
|
if (gst_omx_port_reconfigure (self->in_port) != OMX_ErrorNone)
|
|
|
|
goto reconfigure_error;
|
|
|
|
/* Now get a new buffer and fill it */
|
|
|
|
continue;
|
|
|
|
} else if (acq_ret == GST_OMX_ACQUIRE_BUFFER_RECONFIGURED) {
|
|
|
|
/* TODO: Anything to do here? Don't think so */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_assert (acq_ret == GST_OMX_ACQUIRE_BUFFER_OK && buf != NULL);
|
|
|
|
|
2011-11-01 14:10:12 +00:00
|
|
|
if (self->downstream_flow_ret != GST_FLOW_OK) {
|
|
|
|
GST_ERROR_OBJECT (self, "Downstream returned %s",
|
|
|
|
gst_flow_get_name (self->downstream_flow_ret));
|
|
|
|
|
|
|
|
gst_omx_port_release_buffer (self->in_port, buf);
|
|
|
|
return self->downstream_flow_ret;
|
|
|
|
}
|
|
|
|
|
2011-11-08 07:31:58 +00:00
|
|
|
if (buf->omx_buf->nAllocLen - buf->omx_buf->nOffset <= 0) {
|
|
|
|
gst_omx_port_release_buffer (self->in_port, buf);
|
2011-09-23 15:02:49 +00:00
|
|
|
goto full_buffer;
|
2011-11-08 07:31:58 +00:00
|
|
|
}
|
2011-09-23 15:02:49 +00:00
|
|
|
|
2011-08-15 13:10:04 +00:00
|
|
|
/* Copy the buffer content in chunks of size as requested
|
|
|
|
* by the port */
|
|
|
|
buf->omx_buf->nFilledLen =
|
|
|
|
MIN (GST_BUFFER_SIZE (inbuf) - offset,
|
|
|
|
buf->omx_buf->nAllocLen - buf->omx_buf->nOffset);
|
|
|
|
memcpy (buf->omx_buf->pBuffer + buf->omx_buf->nOffset,
|
|
|
|
GST_BUFFER_DATA (inbuf) + offset, buf->omx_buf->nFilledLen);
|
|
|
|
|
|
|
|
/* Interpolate timestamps if we're passing the buffer
|
|
|
|
* in multiple chunks */
|
|
|
|
if (offset != 0 && duration != GST_CLOCK_TIME_NONE) {
|
|
|
|
timestamp_offset =
|
|
|
|
gst_util_uint64_scale (offset, duration, GST_BUFFER_SIZE (inbuf));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timestamp != GST_CLOCK_TIME_NONE) {
|
|
|
|
buf->omx_buf->nTimeStamp =
|
|
|
|
gst_util_uint64_scale (timestamp + timestamp_offset,
|
|
|
|
OMX_TICKS_PER_SECOND, GST_SECOND);
|
|
|
|
}
|
|
|
|
if (duration != GST_CLOCK_TIME_NONE) {
|
|
|
|
buf->omx_buf->nTickCount =
|
|
|
|
gst_util_uint64_scale (buf->omx_buf->nFilledLen, duration,
|
|
|
|
GST_BUFFER_SIZE (inbuf));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
offset += buf->omx_buf->nFilledLen;
|
|
|
|
self->started = TRUE;
|
|
|
|
gst_omx_port_release_buffer (self->in_port, buf);
|
|
|
|
}
|
|
|
|
|
2011-11-01 14:10:12 +00:00
|
|
|
return self->downstream_flow_ret;
|
2011-08-15 13:10:04 +00:00
|
|
|
|
2011-09-23 15:02:49 +00:00
|
|
|
full_buffer:
|
|
|
|
{
|
|
|
|
GST_ELEMENT_ERROR (self, LIBRARY, FAILED, (NULL),
|
|
|
|
("Got OpenMAX buffer with no free space (%p, %u/%u)", buf,
|
|
|
|
buf->omx_buf->nOffset, buf->omx_buf->nAllocLen));
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
2011-08-15 13:10:04 +00:00
|
|
|
component_error:
|
|
|
|
{
|
|
|
|
GST_ELEMENT_ERROR (self, LIBRARY, FAILED, (NULL),
|
|
|
|
("OpenMAX component in error state %s (0x%08x)",
|
|
|
|
gst_omx_component_get_last_error_string (self->component),
|
|
|
|
gst_omx_component_get_last_error (self->component)));
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
flushing:
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (self, "Flushing -- returning WRONG_STATE");
|
|
|
|
return GST_FLOW_WRONG_STATE;
|
|
|
|
}
|
|
|
|
reconfigure_error:
|
|
|
|
{
|
|
|
|
GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS, (NULL),
|
|
|
|
("Unable to reconfigure input port"));
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2011-11-25 10:31:58 +00:00
|
|
|
gst_omx_audio_enc_event (GstAudioEncoder * encoder, GstEvent * event)
|
2011-08-15 13:10:04 +00:00
|
|
|
{
|
|
|
|
GstOMXAudioEnc *self;
|
|
|
|
|
|
|
|
self = GST_OMX_AUDIO_ENC (encoder);
|
|
|
|
|
|
|
|
if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
|
|
|
|
GstOMXBuffer *buf;
|
|
|
|
GstOMXAcquireBufferReturn acq_ret;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Sending EOS to the component");
|
|
|
|
|
2011-11-08 10:07:01 +00:00
|
|
|
/* Don't send EOS buffer twice, this doesn't work */
|
2011-11-10 13:54:33 +00:00
|
|
|
if (self->eos) {
|
|
|
|
GST_DEBUG_OBJECT (self, "Component is already EOS");
|
2011-11-08 10:07:01 +00:00
|
|
|
return FALSE;
|
2011-11-10 13:54:33 +00:00
|
|
|
}
|
2011-11-08 10:07:01 +00:00
|
|
|
self->eos = TRUE;
|
|
|
|
|
2011-11-08 11:45:16 +00:00
|
|
|
/* Make sure to release the base class stream lock, otherwise
|
|
|
|
* _loop() can't call _finish_frame() and we might block forever
|
|
|
|
* because no input buffers are released */
|
2011-11-25 10:31:58 +00:00
|
|
|
GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
|
2011-11-08 11:45:16 +00:00
|
|
|
|
2011-08-15 13:10:04 +00:00
|
|
|
/* Send an EOS buffer to the component and let the base
|
|
|
|
* class drop the EOS event. We will send it later when
|
|
|
|
* the EOS buffer arrives on the output port. */
|
|
|
|
acq_ret = gst_omx_port_acquire_buffer (self->in_port, &buf);
|
|
|
|
if (acq_ret == GST_OMX_ACQUIRE_BUFFER_OK) {
|
|
|
|
buf->omx_buf->nFlags |= OMX_BUFFERFLAG_EOS;
|
|
|
|
gst_omx_port_release_buffer (self->in_port, buf);
|
2011-11-10 13:54:33 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "Sent EOS to the component");
|
|
|
|
} else {
|
|
|
|
GST_ERROR_OBJECT (self, "Failed to acquire buffer for EOS: %d", acq_ret);
|
2011-08-15 13:10:04 +00:00
|
|
|
}
|
2011-11-08 11:45:16 +00:00
|
|
|
|
2011-11-25 10:31:58 +00:00
|
|
|
GST_AUDIO_ENCODER_STREAM_LOCK (self);
|
2011-11-08 11:45:16 +00:00
|
|
|
|
2011-08-15 13:10:04 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2011-11-01 15:46:09 +00:00
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_omx_audio_enc_drain (GstOMXAudioEnc * self)
|
|
|
|
{
|
|
|
|
GstOMXBuffer *buf;
|
|
|
|
GstOMXAcquireBufferReturn acq_ret;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Draining component");
|
|
|
|
|
2011-11-10 13:54:33 +00:00
|
|
|
if (!self->started) {
|
|
|
|
GST_DEBUG_OBJECT (self, "Component not started yet");
|
2011-11-08 10:07:01 +00:00
|
|
|
return GST_FLOW_OK;
|
2011-11-10 13:54:33 +00:00
|
|
|
}
|
2011-11-08 10:07:01 +00:00
|
|
|
self->started = FALSE;
|
|
|
|
|
|
|
|
/* Don't send EOS buffer twice, this doesn't work */
|
2011-11-10 13:54:33 +00:00
|
|
|
if (self->eos) {
|
|
|
|
GST_DEBUG_OBJECT (self, "Component is EOS already");
|
2011-11-08 10:07:01 +00:00
|
|
|
return GST_FLOW_OK;
|
2011-11-10 13:54:33 +00:00
|
|
|
}
|
2011-11-04 08:43:48 +00:00
|
|
|
|
2011-11-08 11:45:16 +00:00
|
|
|
/* Make sure to release the base class stream lock, otherwise
|
|
|
|
* _loop() can't call _finish_frame() and we might block forever
|
|
|
|
* because no input buffers are released */
|
2011-11-25 10:31:58 +00:00
|
|
|
GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
|
2011-11-08 11:45:16 +00:00
|
|
|
|
2011-11-01 15:46:09 +00:00
|
|
|
/* Send an EOS buffer to the component and let the base
|
|
|
|
* class drop the EOS event. We will send it later when
|
|
|
|
* the EOS buffer arrives on the output port. */
|
|
|
|
acq_ret = gst_omx_port_acquire_buffer (self->in_port, &buf);
|
2011-11-08 11:45:16 +00:00
|
|
|
if (acq_ret != GST_OMX_ACQUIRE_BUFFER_OK) {
|
2011-11-25 10:31:58 +00:00
|
|
|
GST_AUDIO_ENCODER_STREAM_LOCK (self);
|
2011-11-10 13:54:33 +00:00
|
|
|
GST_ERROR_OBJECT (self, "Failed to acquire buffer for draining: %d",
|
|
|
|
acq_ret);
|
2011-11-01 15:46:09 +00:00
|
|
|
return GST_FLOW_ERROR;
|
2011-11-08 11:45:16 +00:00
|
|
|
}
|
2011-11-01 15:46:09 +00:00
|
|
|
|
|
|
|
g_mutex_lock (self->drain_lock);
|
|
|
|
self->draining = TRUE;
|
|
|
|
buf->omx_buf->nFlags |= OMX_BUFFERFLAG_EOS;
|
|
|
|
gst_omx_port_release_buffer (self->in_port, buf);
|
|
|
|
GST_DEBUG_OBJECT (self, "Waiting until component is drained");
|
|
|
|
g_cond_wait (self->drain_cond, self->drain_lock);
|
|
|
|
GST_DEBUG_OBJECT (self, "Drained component");
|
|
|
|
g_mutex_unlock (self->drain_lock);
|
2011-11-25 10:31:58 +00:00
|
|
|
GST_AUDIO_ENCODER_STREAM_LOCK (self);
|
2011-11-01 15:46:09 +00:00
|
|
|
|
2011-11-08 10:07:01 +00:00
|
|
|
self->started = FALSE;
|
|
|
|
|
2011-11-01 15:46:09 +00:00
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|