Commit 204ff391 authored by Thomas Guillem's avatar Thomas Guillem

native_sample: use only gles2 vout

Change surface layout in native and don't implement OnNewVideoLayoutListener
(therefore, the "android-display" module won't work).
parent 5c9c1ccd
......@@ -16,6 +16,16 @@
#define SAMPLE_URL "http://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_640x360.m4v"
enum surface_layout
{
SURFACE_BEST_FIT,
SURFACE_16_9,
SURFACE_4_3,
SURFACE_ORIGINAL,
};
static const enum surface_layout surface_layout = SURFACE_BEST_FIT;
static struct
{
struct {
......@@ -89,6 +99,30 @@ Java_org_videolan_nativesample_NativeActivity_nativeDestroy(
free(p_ctx);
}
static void
UpdateSurfaceLayout(struct context *p_ctx, enum surface_layout layout)
{
switch (layout)
{
case SURFACE_BEST_FIT:
libvlc_video_set_aspect_ratio(p_ctx->p_mp, NULL);
libvlc_video_set_scale(p_ctx->p_mp, 0);
break;
case SURFACE_16_9:
libvlc_video_set_aspect_ratio(p_ctx->p_mp, "16:9");
libvlc_video_set_scale(p_ctx->p_mp, 0);
break;
case SURFACE_4_3:
libvlc_video_set_aspect_ratio(p_ctx->p_mp, "4:3");
libvlc_video_set_scale(p_ctx->p_mp, 0);
break;
case SURFACE_ORIGINAL:
libvlc_video_set_aspect_ratio(p_ctx->p_mp, NULL);
libvlc_video_set_scale(p_ctx->p_mp, 1);
break;
}
}
static bool
PlayUrl(struct context *p_ctx, const char *psz_url)
{
......@@ -106,6 +140,7 @@ PlayUrl(struct context *p_ctx, const char *psz_url)
libvlc_media_add_option(p_m, ":codec=mediacodec_ndk,mediacodec_jni,all");
libvlc_media_player_set_media(p_ctx->p_mp, p_m);
UpdateSurfaceLayout(p_ctx, surface_layout);
int i_ret = libvlc_media_player_play(p_ctx->p_mp);
/* Instance is now owned by the media player */
......
......@@ -10,18 +10,14 @@
package org.videolan.nativesample;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.FrameLayout;
import android.widget.Toast;
......@@ -30,36 +26,17 @@ import org.videolan.libvlc.AWindow;
import org.videolan.libvlc.IVLCVout;
@SuppressWarnings("JniMissingFunction")
public class NativeActivity extends AppCompatActivity implements IVLCVout.Callback,
IVLCVout.OnNewVideoLayoutListener {
private static final boolean ENABLE_SUBTITLES = true;
public class NativeActivity extends AppCompatActivity implements IVLCVout.Callback {
private static final String TAG = "NativeActivity";
private static final int SURFACE_BEST_FIT = 0;
private static final int SURFACE_FIT_HORIZONTAL = 1;
private static final int SURFACE_FIT_VERTICAL = 2;
private static final int SURFACE_FILL = 3;
private static final int SURFACE_16_9 = 4;
private static final int SURFACE_4_3 = 5;
private static final int SURFACE_ORIGINAL = 6;
private static int CURRENT_SIZE = SURFACE_BEST_FIT;
private boolean mNativeStarted = false;
private AWindow mAWindow = null;
private SurfaceView mUiSurface = null;
private FrameLayout mVideoSurfaceFrame = null;
private SurfaceView mVideoSurface = null;
private SurfaceView mSubtitlesSurface = null;
private final Handler mHandler = new Handler();
private View.OnLayoutChangeListener mOnLayoutChangeListener = null;
private int mVideoHeight = 0;
private int mVideoWidth = 0;
private int mVideoVisibleHeight = 0;
private int mVideoVisibleWidth = 0;
private int mVideoSarNum = 0;
private int mVideoSarDen = 0;
private static int sInit = -1;
static synchronized boolean loadLibraries(Context context) {
if (sInit != -1)
......@@ -113,14 +90,8 @@ public class NativeActivity extends AppCompatActivity implements IVLCVout.Callba
mAWindow.addCallback(this);
mUiSurface = (SurfaceView) findViewById(R.id.ui_surface);
mVideoSurfaceFrame = (FrameLayout) findViewById(R.id.video_surface_frame);
mVideoSurface = (SurfaceView) findViewById(R.id.video_surface);
if (ENABLE_SUBTITLES) {
final ViewStub stub = (ViewStub) findViewById(R.id.subtitles_stub);
mSubtitlesSurface = (SurfaceView) stub.inflate();
mSubtitlesSurface.setZOrderMediaOverlay(true);
mSubtitlesSurface.getHolder().setFormat(PixelFormat.TRANSLUCENT);
}
if (!nativeCreate()) {
Toast.makeText(this, "Couldn't create LibVLC", Toast.LENGTH_LONG).show();
finish();
......@@ -140,27 +111,20 @@ public class NativeActivity extends AppCompatActivity implements IVLCVout.Callba
mAWindow.setVideoView(mVideoSurface);
if (mSubtitlesSurface != null)
mAWindow.setSubtitlesView(mSubtitlesSurface);
mAWindow.attachViews(this);
mAWindow.attachViews();
if (mOnLayoutChangeListener == null) {
mOnLayoutChangeListener = new View.OnLayoutChangeListener() {
private final Runnable mRunnable = new Runnable() {
@Override
public void run() {
updateVideoSurfaces();
}
};
@Override
public void onLayoutChange(View v, int left, int top, int right,
int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
if (left != oldLeft || top != oldTop || right != oldRight || bottom != oldBottom) {
mHandler.removeCallbacks(mRunnable);
mHandler.post(mRunnable);
mAWindow.setWindowSize(right - left, bottom - top);
}
}
};
}
mVideoSurfaceFrame.addOnLayoutChangeListener(mOnLayoutChangeListener);
mVideoSurface.addOnLayoutChangeListener(mOnLayoutChangeListener);
}
@Override
......@@ -168,7 +132,7 @@ public class NativeActivity extends AppCompatActivity implements IVLCVout.Callba
super.onStop();
if (mOnLayoutChangeListener != null) {
mVideoSurfaceFrame.removeOnLayoutChangeListener(mOnLayoutChangeListener);
mVideoSurface.removeOnLayoutChangeListener(mOnLayoutChangeListener);
mOnLayoutChangeListener = null;
}
......@@ -177,107 +141,6 @@ public class NativeActivity extends AppCompatActivity implements IVLCVout.Callba
mAWindow.detachViews();
}
private void updateVideoSurfaces() {
if (mVideoWidth * mVideoHeight == 0)
return;
int sw = getWindow().getDecorView().getWidth();
int sh = getWindow().getDecorView().getHeight();
mAWindow.setWindowSize(sw, sh);
double dw = sw, dh = sh;
final boolean isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
if (sw > sh && isPortrait || sw < sh && !isPortrait) {
dw = sh;
dh = sw;
}
// sanity check
if (dw * dh == 0) {
Log.e(TAG, "Invalid surface size");
return;
}
// compute the aspect ratio
double ar, vw;
if (mVideoSarDen == mVideoSarNum) {
/* No indication about the density, assuming 1:1 */
vw = mVideoVisibleWidth;
ar = (double)mVideoVisibleWidth / (double)mVideoVisibleHeight;
} else {
/* Use the specified aspect ratio */
vw = mVideoVisibleWidth * (double)mVideoSarNum / mVideoSarDen;
ar = vw / mVideoVisibleHeight;
}
// compute the display aspect ratio
double dar = dw / dh;
switch (CURRENT_SIZE) {
case SURFACE_BEST_FIT:
if (dar < ar)
dh = dw / ar;
else
dw = dh * ar;
break;
case SURFACE_FIT_HORIZONTAL:
dh = dw / ar;
break;
case SURFACE_FIT_VERTICAL:
dw = dh * ar;
break;
case SURFACE_FILL:
break;
case SURFACE_16_9:
ar = 16.0 / 9.0;
if (dar < ar)
dh = dw / ar;
else
dw = dh * ar;
break;
case SURFACE_4_3:
ar = 4.0 / 3.0;
if (dar < ar)
dh = dw / ar;
else
dw = dh * ar;
break;
case SURFACE_ORIGINAL:
dh = mVideoVisibleHeight;
dw = vw;
break;
}
// set display size
ViewGroup.LayoutParams lp = mVideoSurface.getLayoutParams();
lp.width = (int) Math.ceil(dw * mVideoWidth / mVideoVisibleWidth);
lp.height = (int) Math.ceil(dh * mVideoHeight / mVideoVisibleHeight);
mVideoSurface.setLayoutParams(lp);
if (mSubtitlesSurface != null)
mSubtitlesSurface.setLayoutParams(lp);
// set frame size (crop if necessary)
lp = mVideoSurfaceFrame.getLayoutParams();
lp.width = (int) Math.floor(dw);
lp.height = (int) Math.floor(dh);
mVideoSurfaceFrame.setLayoutParams(lp);
mVideoSurface.invalidate();
if (mSubtitlesSurface != null)
mSubtitlesSurface.invalidate();
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public void onNewVideoLayout(IVLCVout vlcVout, int width, int height, int visibleWidth, int visibleHeight, int sarNum, int sarDen) {
mVideoWidth = width;
mVideoHeight = height;
mVideoVisibleWidth = visibleWidth;
mVideoVisibleHeight = visibleHeight;
mVideoSarNum = sarNum;
mVideoSarDen = sarDen;
updateVideoSurfaces();
}
@Override
public void onSurfacesCreated(IVLCVout vlcVout) {
......
......@@ -6,28 +6,13 @@
android:background="@android:color/background_dark"
tools:context=".NativeActivity">
<SurfaceView
android:id="@+id/video_surface"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<SurfaceView
android:id="@+id/ui_surface"
android:layout_width="1dp"
android:layout_height="1dp" />
<FrameLayout
android:id="@+id/video_surface_frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:foregroundGravity="clip_horizontal|clip_vertical"
tools:ignore="true">
<SurfaceView
android:id="@+id/video_surface"
android:layout_width="1dp"
android:layout_height="1dp" />
<ViewStub
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="@layout/subtitles_surface"
android:id="@+id/subtitles_stub" />
</FrameLayout>
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<SurfaceView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="1dp"
android:layout_height="1dp">
</SurfaceView>
\ No newline at end of file
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