[Android] Use threading as to not block the UI thread when checking for video tracks

Edward Wang git at videolan.org
Wed Jun 20 22:53:28 CEST 2012


android | branch: master | Edward Wang <edward.c.wang at compdigitec.com> | Wed Jun 20 16:52:06 2012 -0400| [66212ed2882beb0f7946b7315d3d31e22aa268b2] | committer: Edward Wang

Use threading as to not block the UI thread when checking for video tracks

> http://git.videolan.org/gitweb.cgi/android.git/?a=commit;h=66212ed2882beb0f7946b7315d3d31e22aa268b2
---

 vlc-android/jni/libvlcjni.c                        |   19 +++++-
 vlc-android/src/org/videolan/vlc/LibVLC.java       |    2 +-
 .../src/org/videolan/vlc/gui/MainActivity.java     |   61 +++++++++++++++-----
 3 files changed, 63 insertions(+), 19 deletions(-)

diff --git a/vlc-android/jni/libvlcjni.c b/vlc-android/jni/libvlcjni.c
index 8ebf843..f9fa601 100644
--- a/vlc-android/jni/libvlcjni.c
+++ b/vlc-android/jni/libvlcjni.c
@@ -574,11 +574,22 @@ jboolean Java_org_videolan_vlc_LibVLC_hasVideoTrack(JNIEnv *env, jobject thiz,
     libvlc_media_player_play( p_mp );
 
     pthread_mutex_lock(&monitor->doneMutex);
-    while (!monitor->length_changed)
-        pthread_cond_wait(&monitor->doneCondVar, &monitor->doneMutex);
+
+    struct timespec deadline;
+    clock_gettime(CLOCK_REALTIME, &deadline);
+    deadline.tv_sec += 2; /* If "VLC can't open the file", return */
+    int mp_alive = 1;
+    while( !monitor->length_changed && mp_alive ) {
+        pthread_cond_timedwait(&monitor->doneCondVar, &monitor->doneMutex, &deadline);
+        mp_alive = libvlc_media_player_will_play(p_mp);
+    }
     pthread_mutex_unlock(&monitor->doneMutex);
 
-    int i_nbTracks = libvlc_video_get_track_count(p_mp);
+    int i_nbTracks;
+    if( mp_alive )
+        i_nbTracks = libvlc_video_get_track_count(p_mp);
+    else
+        i_nbTracks = -1;
     LOGI("Number of video tracks: %d",i_nbTracks);
 
     libvlc_event_detach(ev, libvlc_MediaPlayerLengthChanged, length_changed_callback, monitor);
@@ -592,6 +603,8 @@ jboolean Java_org_videolan_vlc_LibVLC_hasVideoTrack(JNIEnv *env, jobject thiz,
 
     if(i_nbTracks > 0)
         return JNI_TRUE;
+    else if(i_nbTracks < 0)
+        (*env)->ThrowNew(env, (*env)->FindClass(env, "java/io/IOException"), "VLC can't open the file");
     else
         return JNI_FALSE;
 }
diff --git a/vlc-android/src/org/videolan/vlc/LibVLC.java b/vlc-android/src/org/videolan/vlc/LibVLC.java
index 6ce6ddf..62b39a6 100644
--- a/vlc-android/src/org/videolan/vlc/LibVLC.java
+++ b/vlc-android/src/org/videolan/vlc/LibVLC.java
@@ -243,7 +243,7 @@ public class LibVLC {
     /**
      * Return true if there is a video track in the file
      */
-    public boolean hasVideoTrack(String mrl) {
+    public boolean hasVideoTrack(String mrl) throws java.io.IOException {
         return hasVideoTrack(mLibVlcInstance, mrl);
     }
 
diff --git a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
index bbbca13..8c1e245 100644
--- a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
@@ -20,12 +20,14 @@
 
 package org.videolan.vlc.gui;
 
+import java.io.IOException;
 import java.util.ArrayList;
 
 import org.videolan.vlc.AudioServiceController;
 import org.videolan.vlc.LibVLC;
 import org.videolan.vlc.MediaLibrary;
 import org.videolan.vlc.R;
+import org.videolan.vlc.VLCCallbackTask;
 import org.videolan.vlc.gui.audio.AudioActivityGroup;
 import org.videolan.vlc.gui.audio.AudioPlayerActivity;
 import org.videolan.vlc.gui.video.VideoActivityGroup;
@@ -38,6 +40,7 @@ import android.app.Activity;
 import android.app.ActivityGroup;
 import android.app.AlertDialog;
 import android.app.Dialog;
+import android.app.ProgressDialog;
 import android.app.TabActivity;
 import android.content.BroadcastReceiver;
 import android.content.Context;
@@ -246,22 +249,50 @@ public class MainActivity extends TabActivity {
                 b.setView(input);
                 b.setPositiveButton("Open", new DialogInterface.OnClickListener() {
                     public void onClick(DialogInterface dialog, int button) {
-                            AudioServiceController c = AudioServiceController.getInstance();
-                            String s = input.getText().toString();
-
-                            if(!LibVLC.getExistingInstance().hasVideoTrack(s)) {
-                                Log.d(TAG, "Auto-detected audio for " + s);
-                                ArrayList<String> media = new ArrayList<String>();
-                                media.add(input.getText().toString());
-                                c.append(media);
-                            } else {
-                                Log.d(TAG, "Auto-detected Video for " + s);
-                                Intent intent = new Intent(getApplicationContext(), VideoPlayerActivity.class);
-                                intent.putExtra("itemLocation", s);
-                                startActivity(intent);
-                            }
-                        }
+                        ProgressDialog pd = ProgressDialog.show(
+                                MainActivity.this,
+                                getApplicationContext().getString(R.string.loading),
+                                "Please wait...", true);
+                        pd.setCancelable(true);
+
+                        VLCCallbackTask t = new VLCCallbackTask(
+                            /* Task to run */
+                            new VLCCallbackTask.CallbackListener() {
+                                @Override
+                                public void callback() {
+                                    AudioServiceController c = AudioServiceController.getInstance();
+                                    String s = input.getText().toString();
+
+                                    try {
+                                        if(!LibVLC.getExistingInstance().hasVideoTrack(s)) {
+                                            Log.d(TAG, "Auto-detected audio for " + s);
+                                            ArrayList<String> media = new ArrayList<String>();
+                                            media.add(input.getText().toString());
+                                            c.append(media);
+                                        } else {
+                                            Log.d(TAG, "Auto-detected Video for " + s);
+                                            Intent intent = new Intent(getApplicationContext(),
+                                                                       VideoPlayerActivity.class);
+                                            intent.putExtra("itemLocation", s);
+                                            startActivity(intent);
+                                        }
+                                    } catch(IOException e) {
+                                        /* VLC is unable to open the MRL */
+                                        return;
+                                    }
+                                }
+
+                                @Override
+                                public void callback_object(Object o) {
+                                    ProgressDialog pd = (ProgressDialog)o;
+                                    pd.dismiss();
+                                }
+                            }, pd);
+
+                        /* Start this in a new friend as to not block the UI thread */
+                        new Thread(t).start();
                     }
+                }
                 );
                 b.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                     @Override



More information about the Android mailing list