Skip to content

Commit

Permalink
handle event to get subitem, parse() has timing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
autoandshare committed May 11, 2021
1 parent b775fc2 commit f12cc83
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 36 deletions.
10 changes: 6 additions & 4 deletions src/main/java/autoandshare/headvr/lib/BasicUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import autoandshare.headvr.lib.controller.headcontrol.HeadControl;
import autoandshare.headvr.lib.controller.headcontrol.HeadMotion.Motion;
import autoandshare.headvr.lib.rendering.VRSurface;

public class BasicUI extends VRSurface {
Expand Down Expand Up @@ -130,10 +127,15 @@ public void glDraw(Eye eye) {

}

private String getFileNameOrTitle() {
return state.isFileProtocol ?
state.fileName : state.title;
}

private void drawFileNameOrMessage(Canvas canvas) {
String fullTxt = (state.message != null) ?
state.message :
"" + state.currentIndex + "/" + state.count + " " + state.fileName;
"" + state.currentIndex + "/" + state.count + " " + getFileNameOrTitle();

drawString(canvas,
TextUtils.ellipsize(fullTxt, leftAlignTextPaint, (endX - beginX) * 3 / 4, TextUtils.TruncateAt.END).toString(),
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/autoandshare/headvr/lib/State.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package autoandshare.headvr.lib;

import android.net.Uri;
import android.util.Log;

import com.google.vr.sdk.base.Eye;
Expand Down Expand Up @@ -27,6 +28,8 @@ public class State {
// error info
public String errorMessage;

public boolean isFileProtocol;

// basic video info
public String fileName;

Expand Down Expand Up @@ -122,6 +125,11 @@ public void reset() {
this.errorMessage = null;

this.fileName = "";

this.title = "";
this.audioTracks = null;
this.subtitleTracks = null;

this.videoType = null;
this.force2D = false;

Expand Down
105 changes: 73 additions & 32 deletions src/main/java/autoandshare/headvr/lib/VideoRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import android.app.Activity;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.util.Log;

import com.google.vr.sdk.base.Eye;

import org.jetbrains.annotations.Nullable;
import org.videolan.libvlc.Media;
import org.videolan.libvlc.MediaPlayer;
import org.videolan.libvlc.interfaces.ILibVLC;
import org.videolan.libvlc.interfaces.IMedia;
import org.videolan.libvlc.interfaces.IMediaList;
import org.videolan.libvlc.interfaces.IVLCVout;
import org.videolan.medialibrary.interfaces.media.MediaWrapper;
import org.videolan.resources.VLCInstance;
Expand All @@ -23,7 +26,7 @@

import static java.lang.Math.min;

public class VideoRenderer {
public class VideoRenderer implements IMedia.EventListener {
private static final String TAG = "VideoRenderer";

public void stop() {
Expand Down Expand Up @@ -264,7 +267,11 @@ public VideoRenderer(Activity activity, State state) {
}

private void onVideoLoaded() {
state.title = mPlayer.getMedia().getMeta(IMedia.Meta.Title);

IMedia m = mPlayer.getMedia();
state.title = m.getMeta(IMedia.Meta.Title);
m.release();

state.audioTracks = mPlayer.getAudioTracks();
state.subtitleTracks = mPlayer.getSpuTracks();
state.loadValues();
Expand All @@ -282,29 +289,18 @@ private void onVideoLoaded() {

private ParcelFileDescriptor fd;

private boolean isLocalHost(Uri uri) {
String host = uri.getHost().toLowerCase();
return host.equals("localhost") || host.equals("127.0.0.1");
}

private boolean isProbablyStreamingService(Uri uri) {
return uri.getScheme().toLowerCase().startsWith("http") && (!isLocalHost(uri));
}

public void playUri(MediaWrapper mw) {
if (this.mw != null) {
savePosition();
}
switchingVideo = true;
ended = false;
framesCount = 0;
updatePositionRequested = false;
retry = 0;
vtrack = null;
state.reset();

resetAll();

this.mw = mw;
state.fileName = mw.getTitle();
state.isFileProtocol = PathUtil.isFileAccessProtocol(mw.getUri().getScheme());
state.fileName = mw.getFileName();
state.title = mw.getTitle();

state.propertyKey = PathUtil.getKey(mw.getUri());

if (mPlayer != null) {
Expand All @@ -329,6 +325,26 @@ public void playUri(MediaWrapper mw) {
vlcVout.setVideoSurface(videoScreen.getSurfaceTexture());
vlcVout.attachViews();

IMedia m = getiMedia(mw);
if (m == null) {
return;
}
playMedia(m);
m.release();
}

private void resetAll() {
switchingVideo = true;
ended = false;
framesCount = 0;
updatePositionRequested = false;
retry = 0;
vtrack = null;
state.reset();
}

@Nullable
private IMedia getiMedia(MediaWrapper mw) {
IMedia m;
Uri uri = mw.getUri();
if (uri.getScheme().equals("content")) {
Expand All @@ -338,27 +354,20 @@ public void playUri(MediaWrapper mw) {
m = new Media(mILibVLC, fd.getFileDescriptor());
} catch (Exception e) {
state.errorMessage = e.getMessage();
return;
return null;
}
} else {
m = new Media(mILibVLC, uri);
if (isProbablyStreamingService(uri)) {
// youtube needs this
m.parse(Media.Parse.ParseNetwork);
if ((m.subItems() != null) && m.subItems().getCount() != 0) {
IMedia subMedia = m.subItems().getMediaAt(0);
m.release();
m = subMedia;
}
}
}
return m;
}

private void playMedia(IMedia m) {
// disable subtitle
m.addOption(MessageFormat.format(":sub-track-id={0}", String.valueOf(Integer.MAX_VALUE)));

m.setEventListener(this);
mPlayer.setMedia(m);
m.release();

playAndSeek();

switchingVideo = false;
Expand Down Expand Up @@ -518,7 +527,7 @@ public void onEvent(MediaPlayer.Event event) {
case MediaPlayer.Event.Stopped:
state.playerState = "Stopped";
if ((!state.videoLoaded) && (mPlayer.getLength() == 0)) {
if (retry < 3) {
if (retry < 1) {
mPlayer.stop();
playAndSeek();
retry += 1;
Expand All @@ -542,4 +551,36 @@ public void onEvent(MediaPlayer.Event event) {
break;
}
}

@Override
public void onEvent(IMedia.Event event) {
Log.d(TAG, "got imedia event " + event.type);
if (event.type != IMedia.Event.SubItemAdded) {
return;
}

playSubItem();
}

private void playSubItem() {
IMedia m = mPlayer.getMedia();

if (m != null) {

IMediaList subItems = m.subItems();
if (subItems != null && subItems.getCount() > 0) {

IMedia subItem = subItems.getMediaAt(0);
if (subItem != null) {
Log.d(TAG, "try to play sub item " + subItem.getUri());
playMedia(subItem);
subItem.release();
}

subItems.release();
}

m.release();
}
}
}

0 comments on commit f12cc83

Please sign in to comment.