Commit 66212ed2 authored by Edward Wang's avatar Edward Wang

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

parent 9357ae33
......@@ -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;
}
......
......@@ -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);
}
......
......@@ -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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment