Commit b8b000df authored by Geoffrey Métais's avatar Geoffrey Métais Committed by Geoffrey Métais

Set LibVLC instance in a proper singleton

parent e05a281b
Pipeline #17428 passed with stage
in 4 minutes and 51 seconds
......@@ -91,7 +91,7 @@ class AppSetupDelegate : AppDelegate,
AppScope.launch(Dispatchers.IO) {
if (!VLCInstance.testCompatibleCPU(AppContextProvider.appContext)) return@launch
Dialog.setCallbacks(VLCInstance[this@backgroundInit], DialogDelegate)
Dialog.setCallbacks(VLCInstance.getInstance(this@backgroundInit), DialogDelegate)
}
packageManager.setComponentEnabledSetting(ComponentName(this, SendCrashActivity::class.java),
if (BuildConfig.BETA) PackageManager.COMPONENT_ENABLED_STATE_ENABLED else PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP)
......
......@@ -30,52 +30,46 @@ import org.videolan.libvlc.FactoryManager
import org.videolan.libvlc.interfaces.ILibVLC
import org.videolan.libvlc.interfaces.ILibVLCFactory
import org.videolan.libvlc.util.VLCUtil
import org.videolan.resources.VLCInstance.init
import org.videolan.resources.util.VLCCrashHandler
import org.videolan.tools.SingletonHolder
@ObsoleteCoroutinesApi
object VLCInstance {
object VLCInstance : SingletonHolder<ILibVLC, Context>({ init(it.applicationContext) }) {
const val TAG = "VLC/UiTools/VLCInstance"
@SuppressLint("StaticFieldLeak")
private var sLibVLC: ILibVLC? = null
private lateinit var sLibVLC: ILibVLC
private val libVLCFactory by lazy { FactoryManager.getFactory(ILibVLCFactory.factoryId) as ILibVLCFactory }
private val libVLCFactory= FactoryManager.getFactory(ILibVLCFactory.factoryId) as ILibVLCFactory
/** A set of utility functions for the VLC application */
@Synchronized
@Throws(IllegalStateException::class)
operator fun get(ctx: Context): ILibVLC {
if (sLibVLC == null) {
Thread.setDefaultUncaughtExceptionHandler(VLCCrashHandler())
fun init(ctx: Context) : ILibVLC {
Thread.setDefaultUncaughtExceptionHandler(VLCCrashHandler())
val context = ctx.applicationContext
if (!VLCUtil.hasCompatibleCPU(context)) {
Log.e(TAG, VLCUtil.getErrorMsg())
throw IllegalStateException("LibVLC initialisation failed: " + VLCUtil.getErrorMsg())
}
// TODO change LibVLC signature to accept a List instead of an ArrayList
sLibVLC = libVLCFactory.getFromOptions(context, VLCOptions.libOptions)
if (!VLCUtil.hasCompatibleCPU(ctx)) {
Log.e(TAG, VLCUtil.getErrorMsg())
throw IllegalStateException("LibVLC initialisation failed: " + VLCUtil.getErrorMsg())
}
return sLibVLC!!
// TODO change LibVLC signature to accept a List instead of an ArrayList
sLibVLC = libVLCFactory.getFromOptions(ctx, VLCOptions.libOptions)
return sLibVLC
}
@Synchronized
@Throws(IllegalStateException::class)
fun restart() {
sLibVLC?.release()
sLibVLC.release()
sLibVLC = libVLCFactory.getFromOptions(AppContextProvider.appContext, VLCOptions.libOptions)
}
@Synchronized
fun testCompatibleCPU(context: Context): Boolean {
return if (sLibVLC == null && !VLCUtil.hasCompatibleCPU(context)) {
return if (!VLCUtil.hasCompatibleCPU(context)) {
if (context is Activity) {
val i = Intent(Intent.ACTION_VIEW).setClassName(context.applicationContext, COMPATERROR_ACTIVITY)
context.startActivity(i)
}
false
} else
true
} else true
}
}
......@@ -64,7 +64,7 @@ class PreviewVideoInputService : TvInputService(), CoroutineScope by MainScope()
return@launch
}
try {
val media = mFactory.getFromUri(VLCInstance.get(this@PreviewVideoInputService), mw.uri)
val media = mFactory.getFromUri(VLCInstance.getInstance(this@PreviewVideoInputService), mw.uri)
val start = if (mw.length <= 0L) 0L else mw.length.random()
media.addOption(":start-time=${start/1000L}")
awaitSurface()
......
......@@ -49,7 +49,7 @@ object RendererDelegate : RendererDiscoverer.EventListener {
suspend fun start() {
if (started) return
val libVlc = withContext(Dispatchers.IO) { VLCInstance.get(AppContextProvider.appContext) }
val libVlc = withContext(Dispatchers.IO) { VLCInstance.getInstance(AppContextProvider.appContext) }
started = true
for (discoverer in RendererDiscoverer.list(libVlc)) {
val rd = RendererDiscoverer(libVlc, discoverer.name)
......
......@@ -245,7 +245,7 @@ class InfoModel : ViewModel() {
internal fun parseTracks(context: Context, mw: MediaWrapper) = viewModelScope.launch {
val media = withContext(Dispatchers.IO) {
val libVlc = VLCInstance[context]
val libVlc = VLCInstance.getInstance(context)
mMediaFactory.getFromUri(libVlc, mw.uri).apply { parse() }
}
if (!isActive) return@launch
......
......@@ -140,7 +140,7 @@ class EqualizerFragment : VLCBottomSheetDialogFragment() {
if (context == null || presets == null) return@launch
if (bandCount == -1) bandCount = withContext(Dispatchers.IO) {
VLCInstance.get(requireContext())
VLCInstance.getInstance(requireContext())
MediaPlayer.Equalizer.getBandCount()
}
if (!isStarted()) return@launch
......@@ -466,7 +466,7 @@ class EqualizerFragment : VLCBottomSheetDialogFragment() {
}
}
if (bandCount == -1) bandCount = withContext(Dispatchers.IO) {
VLCInstance.get(requireContext())
VLCInstance.getInstance(requireContext())
MediaPlayer.Equalizer.getBandCount()
}
if (!isStarted()) return@launch
......
......@@ -210,7 +210,7 @@ class PlayerController(val context: Context) : IVLCVout.Callback, MediaPlayer.Ev
}
private fun newMediaPlayer() : MediaPlayer {
return MediaPlayer(VLCInstance.get(context)).apply {
return MediaPlayer(VLCInstance.getInstance(context)).apply {
setAudioDigitalOutputEnabled(VLCOptions.isAudioDigitalOutputEnabled(settings))
VLCOptions.getAout(settings)?.let { setAudioOutput(it) }
setRenderer(PlaybackService.renderer.value)
......
......@@ -314,7 +314,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
val title = mw.getMetaLong(MediaWrapper.META_TITLE)
if (title > 0) uri = Uri.parse("$uri#$title")
val start = getStartTime(mw)
val media = mediaFactory.getFromUri(VLCInstance.get(service), uri)
val media = mediaFactory.getFromUri(VLCInstance.getInstance(service), uri)
media.addOption(":start-time=${start/1000L}")
VLCOptions.setMediaOptions(media, ctx, flags or mw.flags, PlaybackService.hasRenderer())
/* keeping only video during benchmark */
......
......@@ -114,7 +114,7 @@ abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<Me
protected open fun initBrowser() {
if (mediabrowser == null) {
registerCreator { MediaBrowser(VLCInstance[context], null, browserHandler) }
registerCreator { MediaBrowser(VLCInstance.getInstance(context), null, browserHandler) }
mediabrowser = get(this)
}
}
......
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