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

@@ -81,6 +81,8 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
}
}
private let instanceId = UUID().uuidString
private var _isBuffering = false {
didSet {
onVideoBuffer?(["isBuffering": _isBuffering, "target": reactTag as Any])
@@ -184,6 +186,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
init(eventDispatcher: RCTEventDispatcher!) {
super.init(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
ReactNativeVideoManager.shared.registerView(newInstance: self)
#if USE_GOOGLE_IMA
_imaAdsManager = RCTIMAAdsManager(video: self, pipEnabled: isPipEnabled)
#endif
@@ -263,6 +266,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
#if os(iOS)
_pip = nil
#endif
ReactNativeVideoManager.shared.unregisterView(newInstance: self)
}
// MARK: - App lifecycle handlers
@@ -462,6 +466,8 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
if _player == nil {
_player = AVPlayer()
ReactNativeVideoManager.shared.onInstanceCreated(id: instanceId, player: _player)
_player!.replaceCurrentItem(with: playerItem)
if _showNotificationControls {
@@ -1261,6 +1267,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
_selectedAudioTrackCriteria = nil
_presentingViewController = nil
ReactNativeVideoManager.shared.onInstanceRemoved(id: instanceId, player: _player)
_player = nil
_resouceLoaderDelegate = nil
_playerObserver.clearPlayer()

23
ios/Video/RNVPlugin.swift Normal file
View File

@@ -0,0 +1,23 @@
//
// RNVPlugin.swift
// react-native-video
//
import Foundation
public protocol RNVPlugin {
/**
* Function called when a new player is created
* @param id: a random string identifying the player
* @param player: the instantiated player reference
*/
func 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
*/
func onInstanceRemoved(id: String, player: Any)
}

View File

@@ -0,0 +1,53 @@
//
// ReactNativeVideoManager.swift
// react-native-video
//
import Foundation
public class ReactNativeVideoManager: RNVPlugin {
private let expectedMaxVideoCount = 10
// create a private initializer
private init() {}
public static let shared: ReactNativeVideoManager = .init()
var instanceList: [RCTVideo] = Array()
var pluginList: [RNVPlugin] = Array()
/**
* register a new ReactExoplayerViewManager in the managed list
*/
func registerView(newInstance: RCTVideo) {
if instanceList.count > expectedMaxVideoCount {
DebugLog("multiple Video displayed ?")
}
instanceList.append(newInstance)
}
/**
* unregister existing ReactExoplayerViewManager in the managed list
*/
func unregisterView(newInstance: RCTVideo) {
if let i = instanceList.firstIndex(of: newInstance) {
instanceList.remove(at: i)
}
}
/**
* register a new plugin in the managed list
*/
public func registerPlugin(plugin: RNVPlugin) {
pluginList.append(plugin)
return
}
public func onInstanceCreated(id: String, player: Any) {
pluginList.forEach { it in it.onInstanceCreated(id: id, player: player) }
}
public func onInstanceRemoved(id: String, player: Any) {
pluginList.forEach { it in it.onInstanceRemoved(id: id, player: player) }
}
}