[vlc-commits] mediacodec: separate configure behaviour from start

Alexandre Janniaux git at videolan.org
Wed Jul 11 15:29:37 CEST 2018


vlc | branch: master | Alexandre Janniaux <alexandre.janniaux at gmail.com> | Mon Jul  9 18:30:09 2018 +0200| [71a0e80e40a2c6e4a5d910315cfbba4ecdb15b62] | committer: Jean-Baptiste Kempf

mediacodec: separate configure behaviour from start

start was creating the codec, configuring it and eventually starting it
in the end. Splitting this behaviour into configure + start allows code
sharing with mediacodec encoder.

Signed-off-by: Jean-Baptiste Kempf <jb at videolan.org>

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=71a0e80e40a2c6e4a5d910315cfbba4ecdb15b62
---

 modules/codec/omxil/mediacodec.c     |   7 +-
 modules/codec/omxil/mediacodec.h     |   3 +-
 modules/codec/omxil/mediacodec_jni.c | 146 ++++++++++++++++++++---------------
 modules/codec/omxil/mediacodec_ndk.c |  85 +++++++++++---------
 4 files changed, 141 insertions(+), 100 deletions(-)

diff --git a/modules/codec/omxil/mediacodec.c b/modules/codec/omxil/mediacodec.c
index 284bd5854b..2a87dbe5c0 100644
--- a/modules/codec/omxil/mediacodec.c
+++ b/modules/codec/omxil/mediacodec.c
@@ -515,7 +515,12 @@ static int StartMediaCodec(decoder_t *p_dec)
         args.audio.i_channel_count  = p_sys->audio.i_channels;
     }
 
-    return p_sys->api.start(&p_sys->api, &args);
+    if (p_sys->api.configure_decoder(&p_sys->api, &args) != 0)
+    {
+        return MC_API_ERROR;
+    }
+
+    return p_sys->api.start(&p_sys->api);
 }
 
 /*****************************************************************************
diff --git a/modules/codec/omxil/mediacodec.h b/modules/codec/omxil/mediacodec.h
index fe8804cf8e..bcc7d665d4 100644
--- a/modules/codec/omxil/mediacodec.h
+++ b/modules/codec/omxil/mediacodec.h
@@ -126,7 +126,8 @@ struct mc_api
 
     void (*clean)(mc_api *);
     int (*prepare)(mc_api *, int i_profile);
-    int (*start)(mc_api *, union mc_api_args *p_args);
+    int (*configure_decoder)(mc_api *, union mc_api_args* p_args);
+    int (*start)(mc_api *);
     int (*stop)(mc_api *);
     int (*flush)(mc_api *);
 
diff --git a/modules/codec/omxil/mediacodec_jni.c b/modules/codec/omxil/mediacodec_jni.c
index 0868086fbb..fa0f66e7ae 100644
--- a/modules/codec/omxil/mediacodec_jni.c
+++ b/modules/codec/omxil/mediacodec_jni.c
@@ -479,56 +479,9 @@ loopclean:
 }
 
 /*****************************************************************************
- * Stop
+ * ConfigureDecoder
  *****************************************************************************/
-static int Stop(mc_api *api)
-{
-    mc_api_sys *p_sys = api->p_sys;
-    JNIEnv *env;
-
-    api->b_direct_rendering = false;
-
-    GET_ENV();
-
-    if (p_sys->input_buffers)
-    {
-        (*env)->DeleteGlobalRef(env, p_sys->input_buffers);
-        p_sys->input_buffers = NULL;
-    }
-    if (p_sys->output_buffers)
-    {
-        (*env)->DeleteGlobalRef(env, p_sys->output_buffers);
-        p_sys->output_buffers = NULL;
-    }
-    if (p_sys->codec)
-    {
-        if (api->b_started)
-        {
-            (*env)->CallVoidMethod(env, p_sys->codec, jfields.stop);
-            if (CHECK_EXCEPTION())
-                msg_Err(api->p_obj, "Exception in MediaCodec.stop");
-            api->b_started = false;
-        }
-
-        (*env)->CallVoidMethod(env, p_sys->codec, jfields.release);
-        if (CHECK_EXCEPTION())
-            msg_Err(api->p_obj, "Exception in MediaCodec.release");
-        (*env)->DeleteGlobalRef(env, p_sys->codec);
-        p_sys->codec = NULL;
-    }
-    if (p_sys->buffer_info)
-    {
-        (*env)->DeleteGlobalRef(env, p_sys->buffer_info);
-        p_sys->buffer_info = NULL;
-    }
-    msg_Dbg(api->p_obj, "MediaCodec via JNI closed");
-    return 0;
-}
-
-/*****************************************************************************
- * Start
- *****************************************************************************/
-static int Start(mc_api *api, union mc_api_args *p_args)
+static int ConfigureDecoder(mc_api *api, union mc_api_args* p_args)
 {
     mc_api_sys *p_sys = api->p_sys;
     JNIEnv* env = NULL;
@@ -538,9 +491,6 @@ static int Start(mc_api *api, union mc_api_args *p_args)
     jstring jcodec_name = NULL;
     jobject jcodec = NULL;
     jobject jformat = NULL;
-    jobject jinput_buffers = NULL;
-    jobject joutput_buffers = NULL;
-    jobject jbuffer_info = NULL;
     jobject jsurface = NULL;
 
     assert(api->psz_mime && api->psz_name);
@@ -599,6 +549,7 @@ static int Start(mc_api *api, union mc_api_args *p_args)
                                                  p_args->audio.i_sample_rate,
                                                  p_args->audio.i_channel_count);
     }
+
     /* No limits for input size */
     SET_INTEGER(jformat, "max-input-size", 0);
 
@@ -613,6 +564,8 @@ static int Start(mc_api *api, union mc_api_args *p_args)
                                  "with an output surface.");
             goto error;
         }
+
+        api->b_direct_rendering = b_direct_rendering;
     }
     else
     {
@@ -623,7 +576,85 @@ static int Start(mc_api *api, union mc_api_args *p_args)
             msg_Warn(api->p_obj, "Exception occurred in MediaCodec.configure");
             goto error;
         }
+        api->b_direct_rendering = false;
+    }
+
+    i_ret = 0;
+
+error:
+    if (jmime)
+        (*env)->DeleteLocalRef(env, jmime);
+    if (jcodec_name)
+        (*env)->DeleteLocalRef(env, jcodec_name);
+    if (jcodec)
+        (*env)->DeleteLocalRef(env, jcodec);
+    if (jformat)
+        (*env)->DeleteLocalRef(env, jformat);
+
+    return i_ret;
+}
+
+/*****************************************************************************
+ * Stop
+ *****************************************************************************/
+static int Stop(mc_api *api)
+{
+    mc_api_sys *p_sys = api->p_sys;
+    JNIEnv *env;
+
+    api->b_direct_rendering = false;
+
+    GET_ENV();
+
+    if (p_sys->input_buffers)
+    {
+        (*env)->DeleteGlobalRef(env, p_sys->input_buffers);
+        p_sys->input_buffers = NULL;
+    }
+    if (p_sys->output_buffers)
+    {
+        (*env)->DeleteGlobalRef(env, p_sys->output_buffers);
+        p_sys->output_buffers = NULL;
+    }
+    if (p_sys->codec)
+    {
+        if (api->b_started)
+        {
+            (*env)->CallVoidMethod(env, p_sys->codec, jfields.stop);
+            if (CHECK_EXCEPTION())
+                msg_Err(api->p_obj, "Exception in MediaCodec.stop");
+            api->b_started = false;
+        }
+
+        (*env)->CallVoidMethod(env, p_sys->codec, jfields.release);
+        if (CHECK_EXCEPTION())
+            msg_Err(api->p_obj, "Exception in MediaCodec.release");
+        (*env)->DeleteGlobalRef(env, p_sys->codec);
+        p_sys->codec = NULL;
     }
+    if (p_sys->buffer_info)
+    {
+        (*env)->DeleteGlobalRef(env, p_sys->buffer_info);
+        p_sys->buffer_info = NULL;
+    }
+    msg_Dbg(api->p_obj, "MediaCodec via JNI closed");
+    return 0;
+}
+
+/*****************************************************************************
+ * Start
+ *****************************************************************************/
+static int Start(mc_api *api)
+{
+    mc_api_sys *p_sys = api->p_sys;
+    JNIEnv* env = NULL;
+    jobject jinput_buffers = NULL;
+    jobject joutput_buffers = NULL;
+    jobject jbuffer_info = NULL;
+
+    GET_ENV();
+
+    int i_ret = MC_API_ERROR;
 
     (*env)->CallVoidMethod(env, p_sys->codec, jfields.start);
     if (CHECK_EXCEPTION())
@@ -635,7 +666,6 @@ static int Start(mc_api *api, union mc_api_args *p_args)
 
     if (jfields.get_input_buffers && jfields.get_output_buffers)
     {
-
         jinput_buffers = (*env)->CallObjectMethod(env, p_sys->codec,
                                                   jfields.get_input_buffers);
         if (CHECK_EXCEPTION())
@@ -658,19 +688,10 @@ static int Start(mc_api *api, union mc_api_args *p_args)
                                      jfields.buffer_info_ctor);
     p_sys->buffer_info = (*env)->NewGlobalRef(env, jbuffer_info);
 
-    api->b_direct_rendering = b_direct_rendering;
     i_ret = 0;
     msg_Dbg(api->p_obj, "MediaCodec via JNI opened");
 
 error:
-    if (jmime)
-        (*env)->DeleteLocalRef(env, jmime);
-    if (jcodec_name)
-        (*env)->DeleteLocalRef(env, jcodec_name);
-    if (jcodec)
-        (*env)->DeleteLocalRef(env, jcodec);
-    if (jformat)
-        (*env)->DeleteLocalRef(env, jformat);
     if (jinput_buffers)
         (*env)->DeleteLocalRef(env, jinput_buffers);
     if (joutput_buffers)
@@ -1022,6 +1043,7 @@ int MediaCodecJni_Init(mc_api *api)
 
     api->clean = Clean;
     api->prepare = Prepare;
+    api->configure_decoder = ConfigureDecoder;
     api->start = Start;
     api->stop = Stop;
     api->flush = Flush;
diff --git a/modules/codec/omxil/mediacodec_ndk.c b/modules/codec/omxil/mediacodec_ndk.c
index bccd380681..c8a8680772 100644
--- a/modules/codec/omxil/mediacodec_ndk.c
+++ b/modules/codec/omxil/mediacodec_ndk.c
@@ -279,41 +279,11 @@ struct mc_api_sys
 };
 
 /*****************************************************************************
- * Stop
- *****************************************************************************/
-static int Stop(mc_api *api)
-{
-    mc_api_sys *p_sys = api->p_sys;
-
-    api->b_direct_rendering = false;
-
-    if (p_sys->p_codec)
-    {
-        if (api->b_started)
-        {
-            syms.AMediaCodec.stop(p_sys->p_codec);
-            api->b_started = false;
-        }
-        syms.AMediaCodec.delete(p_sys->p_codec);
-        p_sys->p_codec = NULL;
-    }
-    if (p_sys->p_format)
-    {
-        syms.AMediaFormat.delete(p_sys->p_format);
-        p_sys->p_format = NULL;
-    }
-
-    msg_Dbg(api->p_obj, "MediaCodec via NDK closed");
-    return 0;
-}
-
-/*****************************************************************************
- * Start
+ * ConfigureDecoder
  *****************************************************************************/
-static int Start(mc_api *api, union mc_api_args *p_args)
+static int ConfigureDecoder(mc_api *api, union mc_api_args *p_args)
 {
     mc_api_sys *p_sys = api->p_sys;
-    int i_ret = MC_API_ERROR;
     ANativeWindow *p_anw = NULL;
 
     assert(api->psz_mime && api->psz_name);
@@ -323,14 +293,14 @@ static int Start(mc_api *api, union mc_api_args *p_args)
     {
         msg_Err(api->p_obj, "AMediaCodec.createCodecByName for %s failed",
                 api->psz_name);
-        goto error;
+        return MC_API_ERROR;
     }
 
     p_sys->p_format = syms.AMediaFormat.new();
     if (!p_sys->p_format)
     {
         msg_Err(api->p_obj, "AMediaFormat.new failed");
-        goto error;
+        return MC_API_ERROR;
     }
 
     syms.AMediaFormat.setInt32(p_sys->p_format, "encoder", 0);
@@ -363,8 +333,51 @@ static int Start(mc_api *api, union mc_api_args *p_args)
                                    p_anw, NULL, 0) != AMEDIA_OK)
     {
         msg_Err(api->p_obj, "AMediaCodec.configure failed");
-        goto error;
+        return MC_API_ERROR;
+    }
+
+    api->b_direct_rendering = !!p_anw;
+
+    return 0;
+}
+
+/*****************************************************************************
+ * Stop
+ *****************************************************************************/
+static int Stop(mc_api *api)
+{
+    mc_api_sys *p_sys = api->p_sys;
+
+    api->b_direct_rendering = false;
+
+    if (p_sys->p_codec)
+    {
+        if (api->b_started)
+        {
+            syms.AMediaCodec.stop(p_sys->p_codec);
+            api->b_started = false;
+        }
+        syms.AMediaCodec.delete(p_sys->p_codec);
+        p_sys->p_codec = NULL;
+    }
+    if (p_sys->p_format)
+    {
+        syms.AMediaFormat.delete(p_sys->p_format);
+        p_sys->p_format = NULL;
     }
+
+    msg_Dbg(api->p_obj, "MediaCodec via NDK closed");
+    return 0;
+}
+
+/*****************************************************************************
+ * Start
+ *****************************************************************************/
+static int Start(mc_api *api)
+{
+    mc_api_sys *p_sys = api->p_sys;
+    int i_ret = MC_API_ERROR;
+
     if (syms.AMediaCodec.start(p_sys->p_codec) != AMEDIA_OK)
     {
         msg_Err(api->p_obj, "AMediaCodec.start failed");
@@ -372,7 +385,6 @@ static int Start(mc_api *api, union mc_api_args *p_args)
     }
 
     api->b_started = true;
-    api->b_direct_rendering = !!p_anw;
     i_ret = 0;
 
     msg_Dbg(api->p_obj, "MediaCodec via NDK opened");
@@ -634,6 +646,7 @@ int MediaCodecNdk_Init(mc_api *api)
 
     api->clean = Clean;
     api->prepare = Prepare;
+    api->configure_decoder = ConfigureDecoder;
     api->start = Start;
     api->stop = Stop;
     api->flush = Flush;



More information about the vlc-commits mailing list