[vlc-devel] [PATCH] Support multiple video player instances.

Paulo Vitor Magacho da Silva pvmagacho at gmail.com
Fri Jul 25 04:18:55 CEST 2014


---
 include/vlc/libvlc_media_player.h           | 17 ++++++++++
 lib/media_player.c                          | 32 +++++++++++++++++++
 modules/video_output/android/nativewindow.c | 14 ++++-----
 modules/video_output/android/surface.c      | 48 ++++++++++++++---------------
 modules/video_output/android/utils.h        | 12 ++++++++
 src/libvlc.c                                |  3 ++
 6 files changed, 95 insertions(+), 31 deletions(-)

diff --git a/include/vlc/libvlc_media_player.h b/include/vlc/libvlc_media_player.h
index 4b2f449..5164e4a 100644
--- a/include/vlc/libvlc_media_player.h
+++ b/include/vlc/libvlc_media_player.h
@@ -437,6 +437,23 @@ LIBVLC_API void libvlc_media_player_set_nsobject ( libvlc_media_player_t *p_mi,
 LIBVLC_API void * libvlc_media_player_get_nsobject ( libvlc_media_player_t *p_mi );
 
 /**
+ * Set the android_surface_value_t structure handler holding the Android surface information where 
+ * the media player should render its video output.
+ *
+ * \param p_mi the Media Player
+ * \param object the android_surface_value_t structure to set.
+ */
+LIBVLC_API void libvlc_media_player_set_surfacevalue ( libvlc_media_player_t *p_mi, void * object );
+
+/**
+ * Gets a handler to the android_surface_value_t structure holding Android Surface object information.
+ *
+ * \param p_mi the Media Player
+ * \return the android_surface_value_t pointer or 0 if none where set
+ */
+LIBVLC_API void * libvlc_media_player_get_surfacevalue ( libvlc_media_player_t *p_mi );
+
+/**
  * Set the agl handler where the media player should render its video output.
  *
  * \param p_mi the Media Player
diff --git a/lib/media_player.c b/lib/media_player.c
index b31a832..c556036 100644
--- a/lib/media_player.c
+++ b/lib/media_player.c
@@ -422,6 +422,9 @@ libvlc_media_player_new( libvlc_instance_t *instance )
     var_Create (mp, "drawable-agl", VLC_VAR_INTEGER);
     var_Create (mp, "drawable-nsobject", VLC_VAR_ADDRESS);
 #endif
+#ifdef __ANDROID__
+    var_Create (mp, "drawable-surfacevalue", VLC_VAR_ADDRESS);
+#endif
 
     var_Create (mp, "keyboard-events", VLC_VAR_BOOL);
     var_SetBool (mp, "keyboard-events", true);
@@ -900,6 +903,35 @@ void * libvlc_media_player_get_nsobject( libvlc_media_player_t *p_mi )
 #endif
 }
 
+
+/**************************************************************************
+ * set_surfacevalue
+ **************************************************************************/
+void libvlc_media_player_set_surfacevalue( libvlc_media_player_t *p_mi,
+                                           void * object )
+{
+    assert (p_mi != NULL);
+#ifdef __ANDROID__
+    var_SetAddress (p_mi, "drawable-surfacevalue", object);
+#else
+    (void) p_mi; (void)object;
+#endif
+}
+
+/**************************************************************************
+ * get_surfacevalue
+ **************************************************************************/
+void * libvlc_media_player_get_surfacevalue( libvlc_media_player_t *p_mi )
+{
+    assert (p_mi != NULL);
+#ifdef __ANDROID__
+    return var_GetAddress (p_mi, "drawable-surfacevalue");
+#else
+    return NULL;
+#endif
+}
+
+
 /**************************************************************************
  * set_agl
  **************************************************************************/
diff --git a/modules/video_output/android/nativewindow.c b/modules/video_output/android/nativewindow.c
index 29014b5..5b4097c 100644
--- a/modules/video_output/android/nativewindow.c
+++ b/modules/video_output/android/nativewindow.c
@@ -38,9 +38,9 @@
 #include "utils.h"
 
 extern JavaVM *myVm;
-extern jobject jni_LockAndGetAndroidJavaSurface();
-extern void jni_UnlockAndroidSurface();
-extern void  jni_SetAndroidSurfaceSize(int width, int height, int visible_width, int visible_height, int sar_num, int sar_den);
+extern jobject jni_LockAndGetAndroidJavaSurface(android_surf_value_t *object);
+extern void jni_UnlockAndroidSurface(android_surf_value_t *object);
+extern void  jni_SetAndroidSurfaceSize(android_surf_value_t *object, int width, int height, int visible_width, int visible_height, int sar_num, int sar_den);
 
 static int Open(vout_window_t *, const vout_window_cfg_t *);
 static void Close(vout_window_t *);
@@ -84,7 +84,7 @@ static int Open(vout_window_t *wnd, const vout_window_cfg_t *cfg)
     }
 
     // Create the native window by first getting the Java surface.
-    jobject javaSurface = jni_LockAndGetAndroidJavaSurface();
+    jobject javaSurface = jni_LockAndGetAndroidJavaSurface(NULL);
     if (javaSurface == NULL)
         goto error;
 
@@ -93,7 +93,7 @@ static int Open(vout_window_t *wnd, const vout_window_cfg_t *cfg)
     p_sys->window = p_sys->native_window.winFromSurface(p_env, javaSurface); // ANativeWindow_fromSurface call.
     (*myVm)->DetachCurrentThread(myVm);
 
-    jni_UnlockAndroidSurface();
+    jni_UnlockAndroidSurface(NULL);
 
     if (p_sys->window == NULL)
         goto error;
@@ -103,7 +103,7 @@ static int Open(vout_window_t *wnd, const vout_window_cfg_t *cfg)
     wnd->sys = p_sys;
 
     // Set the Java surface size.
-    jni_SetAndroidSurfaceSize(cfg->width, cfg->height, cfg->width, cfg->height, 1, 1);
+    jni_SetAndroidSurfaceSize(NULL, cfg->width, cfg->height, cfg->width, cfg->height, 1, 1);
 
     return VLC_SUCCESS;
 
@@ -137,7 +137,7 @@ static int Control(vout_window_t *wnd, int cmd, va_list ap)
         {
             unsigned width = va_arg(ap, unsigned);
             unsigned height = va_arg(ap, unsigned);
-            jni_SetAndroidSurfaceSize(width, height, width, height, 1, 1);
+            jni_SetAndroidSurfaceSize(NULL, width, height, width, height, 1, 1);
             break;
         }
         case VOUT_WINDOW_SET_STATE:
diff --git a/modules/video_output/android/surface.c b/modules/video_output/android/surface.c
index f6a183e..fe119e0 100644
--- a/modules/video_output/android/surface.c
+++ b/modules/video_output/android/surface.c
@@ -74,10 +74,10 @@ vlc_module_end()
  *****************************************************************************/
 
 extern JavaVM *myVm;
-extern void *jni_LockAndGetAndroidSurface();
-extern jobject jni_LockAndGetAndroidJavaSurface();
-extern void  jni_UnlockAndroidSurface();
-extern void  jni_SetAndroidSurfaceSize(int width, int height, int visible_width, int visible_height, int sar_num, int sar_den);
+extern void *jni_LockAndGetAndroidSurface(android_surf_value_t *object);
+extern jobject jni_LockAndGetAndroidJavaSurface(android_surf_value_t *object);
+extern void  jni_UnlockAndroidSurface(android_surf_value_t *object);
+extern void  jni_SetAndroidSurfaceSize(android_surf_value_t *object, int width, int height, int visible_width, int visible_height, int sar_num, int sar_den);
 
 // _ZN7android7Surface4lockEPNS0_11SurfaceInfoEb
 typedef void (*Surface_lock)(void *, void *, int);
@@ -123,6 +123,8 @@ struct vout_display_sys_t {
 
     video_format_t fmt;
     bool b_changed_crop;
+
+    android_surf_value_t *object;
 };
 
 struct picture_sys_t {
@@ -134,8 +136,6 @@ struct picture_sys_t {
 static int  AndroidLockSurface(picture_t *);
 static void AndroidUnlockSurface(picture_t *);
 
-static vlc_mutex_t single_instance = VLC_STATIC_MUTEX;
-
 static inline void *LoadSurface(const char *psz_lib, vout_display_sys_t *sys)
 {
     void *p_library = dlopen(psz_lib, RTLD_NOW);
@@ -178,19 +178,19 @@ static int Open(vlc_object_t *p_this)
     if (fmt.i_chroma == VLC_CODEC_ANDROID_OPAQUE)
         return VLC_EGENERIC;
 
-    /* */
-    if (vlc_mutex_trylock(&single_instance) != 0) {
-        msg_Err(vd, "Can't start more than one instance at a time");
-        return VLC_EGENERIC;
-    }
-
     /* Allocate structure */
     vout_display_sys_t *sys = (struct vout_display_sys_t*) calloc(1, sizeof(*sys));
     if (!sys) {
-        vlc_mutex_unlock(&single_instance);
         return VLC_ENOMEM;
     }
 
+    sys->object = var_CreateGetAddress (vd, "drawable-surfacevalue");
+    if (!sys->object) {
+        free(sys);
+        msg_Err(vd, "No android_surf_value_t set.");
+        return VLC_EGENERIC;
+    }
+
     /* */
     sys->p_library = LoadNativeWindowAPI(&sys->native_window);
     sys->s_unlockAndPost = (Surface_unlockAndPost)sys->native_window.unlockAndPost;
@@ -199,7 +199,6 @@ static int Open(vlc_object_t *p_this)
     if (!sys->p_library) {
         free(sys);
         msg_Err(vd, "Could not initialize libandroid.so/libui.so/libgui.so/libsurfaceflinger_client.so!");
-        vlc_mutex_unlock(&single_instance);
         return VLC_EGENERIC;
     }
 
@@ -286,7 +285,6 @@ static int Open(vlc_object_t *p_this)
 enomem:
     dlclose(sys->p_library);
     free(sys);
-    vlc_mutex_unlock(&single_instance);
     return VLC_ENOMEM;
 }
 
@@ -298,9 +296,11 @@ static void Close(vlc_object_t *p_this)
     picture_pool_Delete(sys->pool);
     if (sys->window)
         sys->native_window.winRelease(sys->window);
+
+    var_Destroy (vd, "drawable-surfacevalue");
+
     dlclose(sys->p_library);
     free(sys);
-    vlc_mutex_unlock(&single_instance);
 }
 
 static picture_pool_t *Pool(vout_display_t *vd, unsigned count)
@@ -357,9 +357,9 @@ static int  AndroidLockSurface(picture_t *picture)
     sh = sys->fmt.i_height;
 
     if (sys->native_window.winFromSurface) {
-        jobject jsurf = jni_LockAndGetAndroidJavaSurface();
+        jobject jsurf = jni_LockAndGetAndroidJavaSurface(sys->object);
         if (unlikely(!jsurf)) {
-            jni_UnlockAndroidSurface();
+            jni_UnlockAndroidSurface(sys->object);
             return VLC_EGENERIC;
         }
         if (sys->window && jsurf != sys->jsurf) {
@@ -377,9 +377,9 @@ static int  AndroidLockSurface(picture_t *picture)
         // as parameter to the unlock function
         picsys->surf = surf = sys->window;
     } else {
-        picsys->surf = surf = jni_LockAndGetAndroidSurface();
+        picsys->surf = surf = jni_LockAndGetAndroidSurface(sys->object);
         if (unlikely(!surf)) {
-            jni_UnlockAndroidSurface();
+            jni_UnlockAndroidSurface(sys->object);
             return VLC_EGENERIC;
         }
     }
@@ -389,7 +389,7 @@ static int  AndroidLockSurface(picture_t *picture)
         ANativeWindow_Buffer buf = { 0 };
         int32_t err = sys->native_window.winLock(sys->window, &buf, NULL);
         if (err) {
-            jni_UnlockAndroidSurface();
+            jni_UnlockAndroidSurface(sys->object);
             return VLC_EGENERIC;
         }
         info->w      = buf.width;
@@ -408,12 +408,12 @@ static int  AndroidLockSurface(picture_t *picture)
 
     if (info->w != aligned_width || info->h != sh || sys->b_changed_crop) {
         // input size doesn't match the surface size -> request a resize
-        jni_SetAndroidSurfaceSize(aligned_width, sh, sys->fmt.i_visible_width, sys->fmt.i_visible_height, sys->i_sar_num, sys->i_sar_den);
+        jni_SetAndroidSurfaceSize(sys->object, aligned_width, sh, sys->fmt.i_visible_width, sys->fmt.i_visible_height, sys->i_sar_num, sys->i_sar_den);
         // When using ANativeWindow, one should use ANativeWindow_setBuffersGeometry
         // to set the size and format. In our case, these are set via the SurfaceHolder
         // in Java, so we seem to manage without calling this ANativeWindow function.
         sys->s_unlockAndPost(surf);
-        jni_UnlockAndroidSurface();
+        jni_UnlockAndroidSurface(sys->object);
         sys->b_changed_crop = false;
         return VLC_EGENERIC;
     }
@@ -435,7 +435,7 @@ static void AndroidUnlockSurface(picture_t *picture)
 
     if (likely(picsys->surf))
         sys->s_unlockAndPost(picsys->surf);
-    jni_UnlockAndroidSurface();
+    jni_UnlockAndroidSurface(sys->object);
 }
 
 static void Display(vout_display_t *vd, picture_t *picture, subpicture_t *subpicture)
diff --git a/modules/video_output/android/utils.h b/modules/video_output/android/utils.h
index 9a72df6..d77b9cf 100644
--- a/modules/video_output/android/utils.h
+++ b/modules/video_output/android/utils.h
@@ -24,6 +24,8 @@
 # include "config.h"
 #endif
 
+#include <pthread.h>
+
 #include <android/native_window.h>
 #include <jni.h>
 #include <android/native_window_jni.h>
@@ -44,6 +46,16 @@ typedef struct
     ptr_ANativeWindow_unlockAndPost unlockAndPost;
 } native_window_api_t;
 
+typedef struct android_surf_value_t {
+    pthread_mutex_t vout_android_lock;
+    pthread_cond_t vout_android_surf_attached;
+    void *vout_android_surf;
+    void *vout_android_gui;
+    jobject vout_android_java_surf;
+    jobject vout_android_subtitles_surf;
+    bool vout_video_player_activity_created;
+} android_surf_value_t;
+
 /* Fill the structure passed as parameter and return a library handle
    that should be destroyed with dlclose. */
 void *LoadNativeWindowAPI(native_window_api_t *native);
diff --git a/src/libvlc.c b/src/libvlc.c
index 946ce2e..9d1bcd8 100644
--- a/src/libvlc.c
+++ b/src/libvlc.c
@@ -485,6 +485,9 @@ dbus_out:
     var_Create( p_libvlc, "drawable-clip-right", VLC_VAR_INTEGER );
     var_Create( p_libvlc, "drawable-nsobject", VLC_VAR_ADDRESS );
 #endif
+#ifdef __ANDROID__
+    var_Create( p_libvlc, "drawable-vlcobject", VLC_VAR_ADDRESS);
+#endif
 #if defined (_WIN32) || defined (__OS2__)
     var_Create( p_libvlc, "drawable-hwnd", VLC_VAR_INTEGER );
 #endif
-- 
1.8.5.2 (Apple Git-48)




More information about the vlc-devel mailing list