Commit 3c8d956c authored by Geoffrey Métais's avatar Geoffrey Métais

Refactor video size into ScaleType enum

parent e54bdc7a
......@@ -42,6 +42,8 @@ import org.videolan.libvlc.util.VLCVideoLayout;
import java.io.File;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
@SuppressWarnings("unused, JniMissingFunction")
......@@ -363,13 +365,15 @@ public class MediaPlayer extends VLCObject<MediaPlayer.Event> {
}
//Video size constants
public static final int SURFACE_BEST_FIT = 0;
public static final int SURFACE_FIT_SCREEN = 1;
public static final int SURFACE_FILL = 2;
public static final int SURFACE_16_9 = 3;
public static final int SURFACE_4_3 = 4;
public static final int SURFACE_ORIGINAL = 5;
public static final int SURFACE_SIZE_COUNT = 6;
public enum ScaleType {
SURFACE_BEST_FIT,
SURFACE_FIT_SCREEN,
SURFACE_FILL,
SURFACE_16_9,
SURFACE_4_3,
SURFACE_ORIGINAL
}
public static final int SURFACE_SCALES_COUNT = ScaleType.values().length;
private Media mMedia = null;
private boolean mPlaying = false;
......@@ -572,7 +576,7 @@ public class MediaPlayer extends VLCObject<MediaPlayer.Event> {
*
* @param media a valid Media object
*/
public MediaPlayer(Media media) {
public MediaPlayer(@NonNull Media media) {
super(media);
if (media == null || media.isReleased())
throw new IllegalArgumentException("Media is null or released");
......@@ -588,7 +592,7 @@ public class MediaPlayer extends VLCObject<MediaPlayer.Event> {
return mWindow;
}
public void attachViews(VLCVideoLayout surfaceFrame, DisplayManager dm, boolean subtitles, boolean textureView) {
public void attachViews(@NonNull VLCVideoLayout surfaceFrame, @Nullable DisplayManager dm, boolean subtitles, boolean textureView) {
mVideoHelper = new VideoHelper(this, surfaceFrame, dm, subtitles, textureView);
mVideoHelper.attachViews();
}
......@@ -604,12 +608,13 @@ public class MediaPlayer extends VLCObject<MediaPlayer.Event> {
if (mVideoHelper != null) mVideoHelper.updateVideoSurfaces();
}
public void setVideoSurfacesize(int size) {
if (mVideoHelper != null) mVideoHelper.setVideoSurfacesize(size);
public void setVideoScale(@NonNull ScaleType type) {
if (mVideoHelper != null) mVideoHelper.setVideoScale(type);
}
public int getVideoSurfacesize() {
return mVideoHelper != null ? mVideoHelper.getVideoSurfacesize() : SURFACE_BEST_FIT;
@NonNull
public ScaleType getVideoScale() {
return mVideoHelper != null ? mVideoHelper.getVideoScale() : ScaleType.SURFACE_BEST_FIT;
}
/**
......
......@@ -22,7 +22,7 @@ import org.videolan.libvlc.util.VLCVideoLayout;
class VideoHelper implements IVLCVout.OnNewVideoLayoutListener {
private static final String TAG = "LibVLC/VideoHelper";
private int mCurrentSize = MediaPlayer.SURFACE_BEST_FIT;
private MediaPlayer.ScaleType mCurrentScaleType = MediaPlayer.ScaleType.SURFACE_BEST_FIT;
private int mVideoHeight = 0;
private int mVideoWidth = 0;
......@@ -127,19 +127,19 @@ class VideoHelper implements IVLCVout.OnNewVideoLayoutListener {
private void changeMediaPlayerLayout(int displayW, int displayH) {
/* Change the video placement using the MediaPlayer API */
switch (mCurrentSize) {
case MediaPlayer.SURFACE_BEST_FIT:
switch (mCurrentScaleType) {
case SURFACE_BEST_FIT:
mMediaPlayer.setAspectRatio(null);
mMediaPlayer.setScale(0);
break;
case MediaPlayer.SURFACE_FIT_SCREEN:
case MediaPlayer.SURFACE_FILL: {
case SURFACE_FIT_SCREEN:
case SURFACE_FILL: {
Media.VideoTrack vtrack = mMediaPlayer.getCurrentVideoTrack();
if (vtrack == null)
return;
final boolean videoSwapped = vtrack.orientation == Media.VideoTrack.Orientation.LeftBottom
|| vtrack.orientation == Media.VideoTrack.Orientation.RightTop;
if (mCurrentSize == MediaPlayer.SURFACE_FIT_SCREEN) {
if (mCurrentScaleType == MediaPlayer.ScaleType.SURFACE_FIT_SCREEN) {
int videoW = vtrack.width;
int videoH = vtrack.height;
......@@ -168,15 +168,15 @@ class VideoHelper implements IVLCVout.OnNewVideoLayoutListener {
}
break;
}
case MediaPlayer.SURFACE_16_9:
case SURFACE_16_9:
mMediaPlayer.setAspectRatio("16:9");
mMediaPlayer.setScale(0);
break;
case MediaPlayer.SURFACE_4_3:
case SURFACE_4_3:
mMediaPlayer.setAspectRatio("4:3");
mMediaPlayer.setScale(0);
break;
case MediaPlayer.SURFACE_ORIGINAL:
case SURFACE_ORIGINAL:
mMediaPlayer.setAspectRatio(null);
mMediaPlayer.setScale(1);
break;
......@@ -251,36 +251,36 @@ class VideoHelper implements IVLCVout.OnNewVideoLayoutListener {
// compute the display aspect ratio
double dar = dw / dh;
switch (mCurrentSize) {
case MediaPlayer.SURFACE_BEST_FIT:
switch (mCurrentScaleType) {
case SURFACE_BEST_FIT:
if (dar < ar)
dh = dw / ar;
else
dw = dh * ar;
break;
case MediaPlayer.SURFACE_FIT_SCREEN:
case SURFACE_FIT_SCREEN:
if (dar >= ar)
dh = dw / ar; /* horizontal */
else
dw = dh * ar; /* vertical */
break;
case MediaPlayer.SURFACE_FILL:
case SURFACE_FILL:
break;
case MediaPlayer.SURFACE_16_9:
case SURFACE_16_9:
ar = 16.0 / 9.0;
if (dar < ar)
dh = dw / ar;
else
dw = dh * ar;
break;
case MediaPlayer.SURFACE_4_3:
case SURFACE_4_3:
ar = 4.0 / 3.0;
if (dar < ar)
dh = dw / ar;
else
dw = dh * ar;
break;
case MediaPlayer.SURFACE_ORIGINAL:
case SURFACE_ORIGINAL:
dh = mVideoVisibleHeight;
dw = vw;
break;
......@@ -314,12 +314,12 @@ class VideoHelper implements IVLCVout.OnNewVideoLayoutListener {
updateVideoSurfaces();
}
void setVideoSurfacesize(int size) {
mCurrentSize = size;
void setVideoScale(MediaPlayer.ScaleType type) {
mCurrentScaleType = type;
updateVideoSurfaces();
}
int getVideoSurfacesize() {
return mCurrentSize;
MediaPlayer.ScaleType getVideoScale() {
return mCurrentScaleType;
}
}
......@@ -647,8 +647,8 @@ public class VideoPlayerActivity extends AppCompatActivity implements IPlaybackS
}
final MediaPlayer mediaPlayer = mService.getMediaplayer();
mediaPlayer.attachViews(mVideoLayout, mDisplayManager, true, false);
final int size = mIsBenchmark ? MediaPlayer.SURFACE_FILL : mSettings.getInt(PreferencesActivity.VIDEO_RATIO, MediaPlayer.SURFACE_BEST_FIT);
mediaPlayer.setVideoSurfacesize(size);
final MediaPlayer.ScaleType size = mIsBenchmark ? MediaPlayer.ScaleType.SURFACE_FILL : MediaPlayer.ScaleType.values()[mSettings.getInt(PreferencesActivity.VIDEO_RATIO, MediaPlayer.ScaleType.SURFACE_BEST_FIT.ordinal())];
mediaPlayer.setVideoScale(size);
initUI();
......@@ -1875,8 +1875,8 @@ public class VideoPlayerActivity extends AppCompatActivity implements IPlaybackS
return mDisplayManager.isPrimary();
}
public int getCurrentSize() {
return mService == null ? 0 : mService.getMediaplayer().getVideoSurfacesize();
public MediaPlayer.ScaleType getCurrentScaleType() {
return mService == null ? MediaPlayer.ScaleType.SURFACE_BEST_FIT : mService.getMediaplayer().getVideoScale();
}
public boolean toggleLoop(View v) {
......@@ -2085,35 +2085,36 @@ public class VideoPlayerActivity extends AppCompatActivity implements IPlaybackS
}
public void resizeVideo() {
final int size = mService.getMediaplayer().getVideoSurfacesize();
setVideoSurfacesize((size+1)%MediaPlayer.SURFACE_SIZE_COUNT);
final int next = (mService.getMediaplayer().getVideoScale().ordinal()+1)%MediaPlayer.SURFACE_SCALES_COUNT;
final MediaPlayer.ScaleType scale = MediaPlayer.ScaleType.values()[next];
setVideoScale(scale);
}
void setVideoSurfacesize(int size) {
mService.getMediaplayer().setVideoSurfacesize(size);
final int newSize = mService.getMediaplayer().getVideoSurfacesize();
void setVideoScale(MediaPlayer.ScaleType scale) {
mService.getMediaplayer().setVideoScale(scale);
final MediaPlayer.ScaleType newSize = mService.getMediaplayer().getVideoScale();
switch (newSize) {
case MediaPlayer.SURFACE_BEST_FIT:
case SURFACE_BEST_FIT:
showInfo(R.string.surface_best_fit, 1000);
break;
case MediaPlayer.SURFACE_FIT_SCREEN:
case SURFACE_FIT_SCREEN:
showInfo(R.string.surface_fit_screen, 1000);
break;
case MediaPlayer.SURFACE_FILL:
case SURFACE_FILL:
showInfo(R.string.surface_fill, 1000);
break;
case MediaPlayer.SURFACE_16_9:
case SURFACE_16_9:
showInfo("16:9", 1000);
break;
case MediaPlayer.SURFACE_4_3:
case SURFACE_4_3:
showInfo("4:3", 1000);
break;
case MediaPlayer.SURFACE_ORIGINAL:
case SURFACE_ORIGINAL:
showInfo(R.string.surface_original, 1000);
break;
}
mSettings.edit()
.putInt(PreferencesActivity.VIDEO_RATIO, newSize)
.putInt(PreferencesActivity.VIDEO_RATIO, newSize.ordinal())
.apply();
}
......
......@@ -276,7 +276,7 @@ class VideoTouchDelegate(private val player: VideoPlayerActivity,
private val mScaleListener = object : ScaleGestureDetector.SimpleOnScaleGestureListener() {
private var savedSize = -1
private var savedScale : MediaPlayer.ScaleType? = null
override fun onScaleBegin(detector: ScaleGestureDetector): Boolean {
return screenConfig.xRange != 0 || player.fov == 0f
}
......@@ -295,14 +295,14 @@ class VideoTouchDelegate(private val player: VideoPlayerActivity,
override fun onScaleEnd(detector: ScaleGestureDetector) {
if (player.fov == 0f && !player.isLocked) {
val grow = detector.scaleFactor > 1.0f
if (grow && player.currentSize != MediaPlayer.SURFACE_FIT_SCREEN) {
savedSize = player.currentSize
player.setVideoSurfacesize(MediaPlayer.SURFACE_FIT_SCREEN)
} else if (!grow && savedSize != -1) {
player.setVideoSurfacesize(savedSize)
savedSize = -1
} else if (!grow && player.currentSize == MediaPlayer.SURFACE_FIT_SCREEN) {
player.setVideoSurfacesize(MediaPlayer.SURFACE_BEST_FIT)
if (grow && player.currentScaleType != MediaPlayer.ScaleType.SURFACE_FIT_SCREEN) {
savedScale = player.currentScaleType
player.setVideoScale(MediaPlayer.ScaleType.SURFACE_FIT_SCREEN)
} else if (!grow && savedScale != null) {
player.setVideoScale(savedScale)
savedScale = null
} else if (!grow && player.currentScaleType == MediaPlayer.ScaleType.SURFACE_FIT_SCREEN) {
player.setVideoScale(MediaPlayer.ScaleType.SURFACE_BEST_FIT)
}
}
}
......
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