Last active
July 20, 2017 22:12
-
-
Save tmm1/dd0235551fe8a9a291163ccd7c031b36 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/app/src/main/java/is/xyz/mpv/MPVLib.java b/app/src/main/java/is/xyz/mpv/MPVLib.java | |
index 686b6a6..47bd989 100644 | |
--- a/app/src/main/java/is/xyz/mpv/MPVLib.java | |
+++ b/app/src/main/java/is/xyz/mpv/MPVLib.java | |
@@ -6,6 +6,7 @@ import java.util.ArrayList; | |
import java.util.List; | |
import android.content.Context; | |
import android.opengl.GLSurfaceView; | |
+import android.view.Surface; | |
public class MPVLib { | |
@@ -21,6 +22,8 @@ public class MPVLib { | |
public static native void destroy(); | |
public static native void initGL(GLSurfaceView view); | |
public static native void destroyGL(); | |
+ public static native void attachSurface(Surface surface); | |
+ public static native void detachSurface(); | |
public static native void resize(int width, int height); | |
public static native void draw(); | |
diff --git a/app/src/main/java/is/xyz/mpv/MPVView.kt b/app/src/main/java/is/xyz/mpv/MPVView.kt | |
index db90db3..17e38ca 100644 | |
--- a/app/src/main/java/is/xyz/mpv/MPVView.kt | |
+++ b/app/src/main/java/is/xyz/mpv/MPVView.kt | |
@@ -3,6 +3,8 @@ package `is`.xyz.mpv | |
import android.content.Context | |
import android.media.AudioManager | |
import android.opengl.GLSurfaceView | |
+import android.view.SurfaceView | |
+import android.view.SurfaceHolder | |
import android.util.AttributeSet | |
import android.util.Log | |
import android.view.WindowManager | |
@@ -15,9 +17,10 @@ import android.os.Build | |
import android.preference.PreferenceManager | |
import kotlin.reflect.KProperty | |
-internal class MPVView(context: Context, attrs: AttributeSet) : GLSurfaceView(context, attrs) { | |
+internal class MPVView(context: Context, attrs: AttributeSet) : SurfaceView(context, attrs), SurfaceHolder.Callback { | |
fun initialize(configDir: String) { | |
+ holder.addCallback(this) | |
MPVLib.create(this.context) | |
MPVLib.setOptionString("config", "yes") | |
MPVLib.setOptionString("config-dir", configDir) | |
@@ -33,30 +36,6 @@ internal class MPVView(context: Context, attrs: AttributeSet) : GLSurfaceView(co | |
// initial options | |
data class Property(val preference_name: String, val mpv_option: String) | |
- // hwdec | |
- val hwdec = if (sharedPreferences.getBoolean("hardware_decoding", true)) | |
- "mediacodec" | |
- else | |
- "no" | |
- | |
- // vo: set display fps as reported by android | |
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
- val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager | |
- val disp = wm.defaultDisplay | |
- val refreshRate = disp.mode.refreshRate | |
- | |
- Log.v(TAG, "Display ${disp.displayId} reports FPS of $refreshRate") | |
- | |
- if (sharedPreferences.getBoolean("video_refreshrate", true)) | |
- MPVLib.setOptionString("display-fps", refreshRate.toString()) | |
- else | |
- Log.v(TAG, "...however we are ignoring that as requested by the user") | |
- | |
- } else { | |
- Log.v(TAG, "Android version too old, disabling refresh rate functionality " + | |
- "(${Build.VERSION.SDK_INT} < ${Build.VERSION_CODES.M})") | |
- } | |
- | |
// ao: set optimal buffer size and sample rate for opensles, to get better audio playback | |
val am = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager | |
val framesPerBuffer = am.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER) | |
@@ -70,13 +49,7 @@ internal class MPVView(context: Context, attrs: AttributeSet) : GLSurfaceView(co | |
val opts = arrayOf( | |
Property("default_audio_language", "alang"), | |
- Property("default_subtitle_language", "slang"), | |
- | |
- // vo-related | |
- Property("video_upscale", "scale"), | |
- Property("video_downscale", "dscale"), | |
- Property("video_scale_param1", "scale-param1"), | |
- Property("video_scale_param2", "scale-param2") | |
+ Property("default_subtitle_language", "slang") | |
) | |
for ((preference_name, mpv_option) in opts) { | |
@@ -85,40 +58,26 @@ internal class MPVView(context: Context, attrs: AttributeSet) : GLSurfaceView(co | |
MPVLib.setOptionString(mpv_option, preference) | |
} | |
- if (sharedPreferences.getBoolean("video_deband", false)) { | |
- // use gradfun as --deband=yes did not work on my device's mobile GPUs | |
- // also lower the default radius to improve perf | |
- MPVLib.setOptionString("vf", "gradfun=radius=12") | |
- } | |
- | |
// set options | |
- MPVLib.setOptionString("vo", "opengl-cb") | |
- MPVLib.setOptionString("hwdec", hwdec) | |
+ MPVLib.setOptionString("vo", "android") | |
+ MPVLib.setOptionString("hwdec", "mediacodec") | |
MPVLib.setOptionString("hwdec-codecs", "h264,hevc,mpeg4,mpeg2video,vp8,vp9") | |
MPVLib.setOptionString("ao", "opensles") | |
MPVLib.setOptionString("tls-verify", "yes") | |
MPVLib.setOptionString("tls-ca-file", "${this.context.filesDir.path}/cacert.pem") | |
} | |
- fun playFile(filePath: String) { | |
- // Pick an EGLConfig with RGB8 color, 16-bit depth, no stencil, | |
- // supporting OpenGL ES 3.0 or later backwards-compatible versions. | |
- setEGLContextClientVersion(2) | |
- setEGLConfigChooser(8, 8, 8, 0, 16, 0) | |
- val renderer = Renderer(this) | |
- renderer.setFilePath(filePath) | |
- setRenderer(renderer) | |
- renderMode = RENDERMODE_WHEN_DIRTY | |
+ var filePath: String? = null | |
+ fun playFile(path: String) { | |
+ filePath = path | |
} | |
- override fun onPause() { | |
- queueEvent { | |
- MPVLib.setPropertyString("vid", "no") | |
- MPVLib.destroyGL() | |
- } | |
+ fun onPause() { | |
paused = true | |
- super.onPause() | |
+ } | |
+ | |
+ fun onResume() { | |
} | |
// Called when back button is pressed, or app is shutting down | |
@@ -250,33 +209,19 @@ internal class MPVView(context: Context, attrs: AttributeSet) : GLSurfaceView(co | |
fun cycleSub() = MPVLib.command(arrayOf("cycle", "sub")) | |
fun cycleHwdec() = MPVLib.setPropertyString("hwdec", if (hwdecActive!!) "no" else "mediacodec") | |
- private class Renderer(val glView: MPVView) : GLSurfaceView.Renderer { | |
- private var filePath: String? = null | |
- | |
- override fun onDrawFrame(gl: GL10) { | |
- MPVLib.draw() | |
- } | |
- | |
- override fun onSurfaceChanged(gl: GL10, width: Int, height: Int) { | |
- MPVLib.resize(width, height) | |
- } | |
+ override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) { | |
+ } | |
- override fun onSurfaceCreated(gl: GL10, config: EGLConfig) { | |
- Log.w(TAG, "Creating libmpv GL surface") | |
- MPVLib.initGL(glView) | |
- if (filePath != null) { | |
- MPVLib.command(arrayOf("loadfile", filePath as String)) | |
- filePath = null | |
- } else { | |
- // Get here when user goes to home screen and then returns to the app | |
- // mpv disables video output when opengl context is destroyed, enable it back | |
- MPVLib.setPropertyInt("vid", 1) | |
- } | |
+ override fun surfaceCreated(holder: SurfaceHolder) { | |
+ MPVLib.attachSurface(holder.surface) | |
+ if (filePath != null) { | |
+ MPVLib.command(arrayOf("loadfile", filePath as String)) | |
+ filePath = null | |
} | |
+ } | |
- fun setFilePath(file_path: String) { | |
- filePath = file_path | |
- } | |
+ override fun surfaceDestroyed(holder: SurfaceHolder) { | |
+ MPVLib.detachSurface() | |
} | |
companion object { | |
diff --git a/app/src/main/jni/globals.h b/app/src/main/jni/globals.h | |
index e2427b8..50d9f67 100644 | |
--- a/app/src/main/jni/globals.h | |
+++ b/app/src/main/jni/globals.h | |
@@ -5,3 +5,4 @@ | |
extern JavaVM *g_vm; | |
extern mpv_handle *g_mpv; | |
extern std::atomic<bool> g_event_thread_request_exit; | |
+extern jobject g_surface; | |
diff --git a/app/src/main/jni/render.cpp b/app/src/main/jni/render.cpp | |
index f657d5e..643c291 100644 | |
--- a/app/src/main/jni/render.cpp | |
+++ b/app/src/main/jni/render.cpp | |
@@ -16,6 +16,9 @@ extern "C" { | |
jni_func(void, resize, jint width, jint height); | |
jni_func(void, draw); | |
+ | |
+ jni_func(void, attachSurface, jobject surface); | |
+ jni_func(void, detachSurface); | |
}; | |
static mpv_opengl_cb_context *mpv_gl; | |
@@ -96,3 +99,18 @@ jni_func(void, destroyGL) { | |
mpv_opengl_cb_uninit_gl(mpv_gl); | |
mpv_gl = NULL; | |
} | |
+ | |
+jobject g_surface; | |
+ | |
+jni_func(void, attachSurface, jobject surface) { | |
+ g_surface = env->NewGlobalRef(surface); | |
+ int64_t wid = (int64_t)(intptr_t)g_surface; | |
+ mpv_set_option(g_mpv, "wid", MPV_FORMAT_INT64, (void *)&wid); | |
+} | |
+ | |
+jni_func(void, detachSurface) { | |
+ env->DeleteGlobalRef(g_surface); | |
+ g_surface = NULL; | |
+ int64_t wid = 0; | |
+ mpv_set_option(g_mpv, "wid", MPV_FORMAT_INT64, (void *)&wid); | |
+} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment