feat: add plugins management (#3909)

This commit is contained in:
Olivier Bouillet
2024-06-25 08:55:32 +02:00
committed by GitHub
parent 3cfb96adb9
commit 91d27a6009
28 changed files with 828 additions and 140 deletions

View File

@@ -123,6 +123,7 @@ import com.brentvatne.common.react.VideoEventEmitter;
import com.brentvatne.common.toolbox.DebugLog;
import com.brentvatne.react.BuildConfig;
import com.brentvatne.react.R;
import com.brentvatne.react.ReactNativeVideoManager;
import com.brentvatne.receiver.AudioBecomingNoisyReceiver;
import com.brentvatne.receiver.BecomingNoisyListener;
import com.facebook.react.bridge.LifecycleEventListener;
@@ -258,6 +259,9 @@ public class ReactExoplayerView extends FrameLayout implements
private long lastDuration = -1;
private boolean viewHasDropped = false;
private String instanceId = String.valueOf(UUID.randomUUID());
private void updateProgress() {
if (player != null) {
if (playerControlView != null && isPlayingAd() && controls) {
@@ -756,6 +760,7 @@ public class ReactExoplayerView extends FrameLayout implements
.setLoadControl(loadControl)
.setMediaSourceFactory(mediaSourceFactory)
.build();
ReactNativeVideoManager.Companion.getInstance().onInstanceCreated(instanceId, player);
refreshDebugState();
player.addListener(self);
player.setVolume(muted ? 0.f : audioVolume * 1);
@@ -1150,6 +1155,7 @@ public class ReactExoplayerView extends FrameLayout implements
player.removeListener(this);
trackSelector = null;
ReactNativeVideoManager.Companion.getInstance().onInstanceRemoved(instanceId, player);
player = null;
}

View File

@@ -20,6 +20,7 @@ import com.brentvatne.common.api.SubtitleStyle;
import com.brentvatne.common.react.VideoEventEmitter;
import com.brentvatne.common.toolbox.DebugLog;
import com.brentvatne.common.toolbox.ReactBridgeUtils;
import com.brentvatne.react.ReactNativeVideoManager;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.common.MapBuilder;
@@ -97,12 +98,14 @@ public class ReactExoplayerViewManager extends ViewGroupManager<ReactExoplayerVi
@NonNull
@Override
protected ReactExoplayerView createViewInstance(@NonNull ThemedReactContext themedReactContext) {
ReactNativeVideoManager.Companion.getInstance().registerView(this);
return new ReactExoplayerView(themedReactContext, config);
}
@Override
public void onDropViewInstance(ReactExoplayerView view) {
view.cleanUpResources();
ReactNativeVideoManager.Companion.getInstance().unregisterView(this);
}
@Override

View File

@@ -0,0 +1,22 @@
package com.brentvatne.react
/**
* Plugin interface definition
*/
interface RNVPlugin {
/**
* Function called when a new player is created
* @param id: a random string identifying the player
* @param player: the instantiated player reference
*/
fun onInstanceCreated(id: String, player: Any)
/**
* Function called when a player should be destroyed
* when this callback is called, the plugin shall free all
* resources and release all reference to Player object
* @param id: a random string identifying the player
* @param player: the player to release
*/
fun onInstanceRemoved(id: String, player: Any)
}

View File

@@ -0,0 +1,71 @@
package com.brentvatne.react
import com.brentvatne.common.toolbox.DebugLog
import com.brentvatne.exoplayer.ReactExoplayerViewManager
/**
* ReactNativeVideoManager is a singleton class which allows to manipulate / the global state of the app
* It handles the list of <Video view instanced and registration of plugins
*/
class ReactNativeVideoManager : RNVPlugin {
companion object {
private const val TAG = "ReactNativeVideoManager"
@Volatile
private var instance: ReactNativeVideoManager? = null
/**
* Singleton accessor
*/
fun getInstance(): ReactNativeVideoManager =
instance ?: synchronized(this) {
instance ?: ReactNativeVideoManager().also { instance = it }
}
}
private var instanceList: ArrayList<ReactExoplayerViewManager> = ArrayList()
private var pluginList: ArrayList<RNVPlugin> = ArrayList()
/**
* register a new ReactExoplayerViewManager in the managed list
*/
fun registerView(newInstance: ReactExoplayerViewManager): () -> Boolean =
{
if (instanceList.size > 2) {
DebugLog.d(TAG, "multiple Video displayed ?")
}
instanceList.add(newInstance)
}
/**
* unregister existing ReactExoplayerViewManager in the managed list
*/
fun unregisterView(newInstance: ReactExoplayerViewManager): () -> Boolean =
{
instanceList.remove(newInstance)
}
/**
* register a new plugin in the managed list
*/
fun registerPlugin(plugin: RNVPlugin) {
pluginList.add(plugin)
return
}
/**
* unregister a plugin from the managed list
*/
fun unregisterPlugin(plugin: RNVPlugin) {
pluginList.remove(plugin)
return
}
override fun onInstanceCreated(id: String, player: Any) {
pluginList.forEach { it.onInstanceCreated(id, player) }
}
override fun onInstanceRemoved(id: String, player: Any) {
pluginList.forEach { it.onInstanceRemoved(id, player) }
}
}