Skip to content

Commit d419729

Browse files
committed
docs: update documentation
1 parent 8c7740e commit d419729

File tree

1 file changed

+104
-29
lines changed

1 file changed

+104
-29
lines changed

docs/pages/other/plugin.md

Lines changed: 104 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,34 @@ First you need to create a new react native package:
2525
npx create-react-native-library@latest react-native-video-custom-analytics
2626
```
2727

28-
Both android and iOS implementation expose an interface `RNVPlugin`.
29-
Your `react-native-video-custom-analytics` shall implement this interface and register itself as a plugin for react native video.
28+
Both Android and iOS implementations expose two interfaces:
29+
- `RNVPlugin`: Base interface for all RNV plugins that doesn't have dependencies nor logic specific to any player
30+
- Player-specific interfaces: `RNVExoplayerPlugin` (Android) and `RNVAVPlayerPlugin` (iOS) for plugins that need direct access to the specific player implementations
31+
32+
Your `react-native-video-custom-analytics` shall implement one of these interfaces and register itself as a plugin for react native video.
3033

3134
## Android
32-
There is no special requierement for gradle file.
33-
You need two mandatory action to be able to receive player handle
35+
36+
There is no special requirement for gradle file.
37+
You need two mandatory actions to be able to receive player handle.
3438

3539
### 1/ Create the plugin
3640

37-
First you should instanciate a class which extends `RNVPlugin`.
41+
You have two options for implementing a plugin on Android:
3842

39-
The proposed integration implement `RNVPlugin` directly inside the Module file (`VideoPluginSampleModule`).
43+
#### Option 1: Basic Plugin (RNVPlugin)
4044

41-
The `RNVPlugin` interface defines following functions, see description here under.
45+
For plugins that don't need Exoplayer-specific functionality, implement the `RNVPlugin` interface:
4246

4347
```kotlin
48+
interface RNVPlugin {
4449
/**
4550
* Function called when a new player is created
4651
* @param id: a random string identifying the player
4752
* @param player: the instantiated player reference
4853
*/
4954
fun onInstanceCreated(id: String, player: Any)
55+
5056
/**
5157
* Function called when a player should be destroyed
5258
* when this callback is called, the plugin shall free all
@@ -55,79 +61,136 @@ The `RNVPlugin` interface defines following functions, see description here unde
5561
* @param player: the player to release
5662
*/
5763
fun onInstanceRemoved(id: String, player: Any)
58-
64+
}
65+
```
66+
67+
#### Option 2: Exoplayer-specific Plugin (RNVExoplayerPlugin)
68+
69+
For plugins that need direct access to Exoplayer functionality, implement the `RNVExoplayerPlugin` interface:
70+
71+
```kotlin
72+
interface RNVExoplayerPlugin : RNVPlugin {
5973
/**
6074
* Optional function that allows plugin to provide custom DRM manager
6175
* Only one plugin can provide DRM manager at a time
6276
* @return DRMManagerSpec implementation if plugin wants to handle DRM, null otherwise
6377
*/
64-
fun getDRMManager(): DRMManagerSpec? {
65-
return null
66-
}
78+
fun getDRMManager(): DRMManagerSpec? = null
79+
80+
/**
81+
* Function called when a new player is created
82+
* @param id: a random string identifying the player
83+
* @param player: the instantiated player reference
84+
* @note: This is helper that ensure that player is non null ExoPlayer
85+
*/
86+
fun onInstanceCreated(id: String, player: ExoPlayer)
87+
88+
/**
89+
* Function called when a player should be destroyed
90+
* when this callback is called, the plugin shall free all
91+
* resources and release all reference to Player object
92+
* @param id: a random string identifying the player
93+
* @param player: the player to release
94+
* @note: This is helper that ensure that player is non null ExoPlayer
95+
*/
96+
fun onInstanceRemoved(id: String, player: ExoPlayer)
97+
}
6798
```
6899

69-
### 2/ register the plugin
100+
The `RNVExoplayerPlugin` interface already implements the base `RNVPlugin` methods by providing type-safe wrappers that ensure you receive an ExoPlayer instance.
70101

71-
To register this allocated class in the main react native video package you should call following function:
102+
### 2/ Register the plugin
103+
104+
To register your plugin with the main react native video package, call the following function:
72105

73106
```kotlin
74107
ReactNativeVideoManager.getInstance().registerPlugin(plugin)
75108
```
76-
The proposed integration register the instanciated class in `createNativeModules` entry point.
109+
110+
The proposed integration registers the instantiated class in the `createNativeModules` entry point.
77111

78112
Your native module can now track Player updates directly from Player reference and report to backend.
79113

80114
## iOS
81115

82116
### 1/ podspec integration
83117

84-
Your new module shall be able to access to react-native-video package, then we must declare it as a dependency of the new module you are creating.
118+
Your new module should be able to access the react-native-video package, so you must declare it as a dependency of the new module you are creating.
85119

86120
```podfile
87121
s.dependency "react-native-video"
88122
```
89123

90124
### 2/ Create the plugin
91125

92-
First you should instanciate a class which extends `RNVPlugin`.
126+
You have two options for implementing a plugin on iOS:
93127

94-
The proposed integration implement `RNVPlugin` directly inside the entry point of the module file (`VideoPluginSample`).
128+
#### Option 1: Basic Plugin (RNVPlugin)
95129

96-
The `RNVPlugin` interface defines following functions, see description here under.
130+
For plugins that don't need AVPlayer-specific functionality, extend the `RNVPlugin` class:
97131

98132
```swift
133+
open class RNVPlugin: NSObject {
99134
/**
100135
* Function called when a new player is created
101136
* @param id: a random string identifying the player
102137
* @param player: the instantiated player reference
103138
*/
104-
func onInstanceCreated(id: String, player: Any)
139+
open func onInstanceCreated(id: String, player: Any) { /* no-op */ }
140+
105141
/**
106142
* Function called when a player should be destroyed
107143
* when this callback is called, the plugin shall free all
108144
* resources and release all reference to Player object
109145
* @param id: a random string identifying the player
110146
* @param player: the player to release
111147
*/
112-
func onInstanceRemoved(id: String, player: Any)
113-
148+
open func onInstanceRemoved(id: String, player: Any) { /* no-op */ }
149+
}
150+
```
151+
152+
#### Option 2: AVPlayer-specific Plugin (RNVAVPlayerPlugin)
153+
154+
For plugins that need direct access to AVPlayer functionality, extend the `RNVAVPlayerPlugin` class:
155+
156+
```swift
157+
open class RNVAVPlayerPlugin: RNVPlugin {
114158
/**
115159
* Optional function that allows plugin to provide custom DRM manager
116160
* Only one plugin can provide DRM manager at a time
117161
* @return: DRMManagerSpec type if plugin wants to handle DRM, nil otherwise
118162
*/
119-
func getDRMManager() -> DRMManagerSpec.Type?
163+
open func getDRMManager() -> DRMManagerSpec? { nil }
164+
165+
/**
166+
* Function called when a new AVPlayer instance is created
167+
* @param id: a random string identifying the player
168+
* @param player: the instantiated AVPlayer
169+
* @note: This is helper that ensure that player is non null AVPlayer
170+
*/
171+
open func onInstanceCreated(id: String, player: AVPlayer) { /* no-op */ }
172+
173+
/**
174+
* Function called when a AVPlayer instance is being removed
175+
* @param id: a random string identifying the player
176+
* @param player: the AVPlayer to release
177+
* @note: This is helper that ensure that player is non null AVPlayer
178+
*/
179+
open func onInstanceRemoved(id: String, player: AVPlayer) { /* no-op */ }
180+
}
120181
```
121182

183+
The `RNVAVPlayerPlugin` class already extends from the base `RNVPlugin` class and provides type-safe wrappers that ensure you receive an AVPlayer instance.
184+
122185
### 3/ Register the plugin
123186

124-
To register this allocated class in the main react native video package you should register it by calling this function:
187+
To register your plugin with the main react native video package, call this function:
125188

126189
```swift
127190
ReactNativeVideoManager.shared.registerPlugin(plugin: plugin)
128191
```
129192

130-
The proposed integration register the instanciated class in file `VideoPluginSample` in the init function:
193+
The proposed integration registers the instantiated class in file `VideoPluginSample` in the init function:
131194

132195
```swift
133196
import react_native_video
@@ -168,14 +231,20 @@ class CustomDRMManager : DRMManagerSpec {
168231
Implement `getDRMManager()` in your plugin to provide the custom DRM manager:
169232

170233
```kotlin
171-
class CustomVideoPlugin : RNVPlugin {
234+
class CustomVideoPlugin : RNVExoplayerPlugin {
172235
private val drmManager = CustomDRMManager()
173236

174237
override fun getDRMManager(): DRMManagerSpec? {
175238
return drmManager
176239
}
177240

178-
// ... other RNVPlugin methods ...
241+
override fun onInstanceCreated(id: String, player: ExoPlayer) {
242+
// Handle player creation
243+
}
244+
245+
override fun onInstanceRemoved(id: String, player: ExoPlayer) {
246+
// Handle player removal
247+
}
179248
}
180249
```
181250

@@ -224,12 +293,18 @@ class CustomDRMManager: NSObject, DRMManagerSpec {
224293
Implement `getDRMManager()` in your plugin to provide the custom DRM manager:
225294

226295
```swift
227-
class CustomVideoPlugin: RNVPlugin {
228-
func getDRMManager() -> DRMManagerSpec.Type? {
296+
class CustomVideoPlugin: RNVAVPlayerPlugin {
297+
override func getDRMManager() -> DRMManagerSpec? {
229298
return CustomDRMManager.self
230299
}
231300

232-
// ... other RNVPlugin methods ...
301+
override func onInstanceCreated(id: String, player: AVPlayer) {
302+
// Handle player creation
303+
}
304+
305+
override func onInstanceRemoved(id: String, player: AVPlayer) {
306+
// Handle player removal
307+
}
233308
}
234309
```
235310

0 commit comments

Comments
 (0)