-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFastPixBaseCastLabs.kt
More file actions
354 lines (291 loc) · 11.5 KB
/
FastPixBaseCastLabs.kt
File metadata and controls
354 lines (291 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package io.fastpix.castlabs_player_data
import android.content.Context
import android.content.res.Configuration
import android.util.Log
import android.view.View
import com.castlabs.android.player.DisplayInfo
import com.castlabs.android.player.PlayerController
import com.castlabs.android.player.exceptions.CastlabsPlayerException
import com.castlabs.android.player.models.VideoTrackQuality
import com.google.android.exoplayer2.ExoPlayer
import io.fastpix.castlabs_player_data.src.info.CastLabsLibraryInfo
import io.fastpix.castlabs_player_data.src.model.CustomerData
import io.fastpix.castlabs_player_data.src.model.PlayerEvents
import io.fastpix.castlabs_player_data.src.utils.Utils
import io.fastpix.castlabs_player_data.src.utils.Utils.validTransitions
import io.fastpix.data.FastPixDataSDK
import io.fastpix.data.domain.SDKConfiguration
import io.fastpix.data.domain.enums.PlayerEventType
import io.fastpix.data.domain.listeners.PlayerListener
import io.fastpix.data.domain.model.BandwidthModel
import io.fastpix.data.domain.model.ErrorModel
import kotlin.math.ceil
class FastPixBaseCastLabs(
private val context: Context,
private val playerView: View,
private val playerController: PlayerController,
private val enableLogging: Boolean = false,
private val customerData: CustomerData,
) : PlayerListener, com.castlabs.android.player.PlayerListener {
private val TAG = "FastPixBaseCastLabs"
private lateinit var fastPixDataSDK: FastPixDataSDK
private var videoSourceWidth: Int? = null
private var videoSourceHeight: Int? = null
private var errorCode: String? = null
private var errorMessage: String? = null
private var isSeeking = false
// State machine for valid event transitions
private var currentEventState: PlayerEvents? = null
init {
initializeFastPixSDK()
playerController.addPlayerListener(this)
}
private var lastStablePositionUs: Long = -1
private fun dispatchViewBegin() {
if (enableLogging) {
Log.d(TAG, "Dispatching ViewBegin event")
}
fastPixDataSDK.dispatchEvent(PlayerEventType.viewBegin)
}
private fun dispatchPlayerReadyEvent() {
if (enableLogging) {
Log.d(TAG, "Dispatching Play Ready event")
}
fastPixDataSDK.dispatchEvent(PlayerEventType.playerReady)
}
private fun dispatchPlayEvent() {
if (transitionToEvent(PlayerEvents.PLAY)) {
if (enableLogging) {
Log.d(TAG, "Dispatching Play event")
}
fastPixDataSDK.dispatchEvent(PlayerEventType.play)
}
}
private fun dispatchPlayingEvent() {
if (transitionToEvent(PlayerEvents.PLAYING)) {
if (enableLogging) {
Log.d(TAG, "Dispatching Playing event")
}
fastPixDataSDK.dispatchEvent(PlayerEventType.playing)
}
}
private fun dispatchPauseEvent(currentPosition: Int? = null) {
if (transitionToEvent(PlayerEvents.PAUSE)) {
if (enableLogging) {
Log.d(TAG, "Dispatching Pause event")
}
// Note: There's no Pause event type in PlayerEventType enum
// For now, we'll just track the state transition without dispatching
// You may need to add a Pause event type to PlayerEventType enum
if (currentPosition != null) {
fastPixDataSDK.dispatchEvent(PlayerEventType.pause, currentPosition)
} else {
fastPixDataSDK.dispatchEvent(PlayerEventType.pause)
}
}
}
private fun dispatchSeekingEvent(currentPosition: Int? = null) {
if (transitionToEvent(PlayerEvents.SEEKING)) {
if (enableLogging) {
Log.d(TAG, "Dispatching Seeking event")
}
// Temporarily set currentPosition to seeking start position for the seeking event
fastPixDataSDK.dispatchEvent(PlayerEventType.seeking, currentPosition)
}
}
private fun dispatchSeekedEvent(position: Long) {
if (transitionToEvent(PlayerEvents.SEEKED)) {
if (enableLogging) {
Log.d(TAG, "Dispatching Seeked event")
}
fastPixDataSDK.dispatchEvent(PlayerEventType.seeked, position.toInt())
}
}
private fun dispatchBufferingEvent() {
if (transitionToEvent(PlayerEvents.BUFFERING)) {
if (enableLogging) {
Log.d(TAG, "Dispatching Buffering event")
}
fastPixDataSDK.dispatchEvent(PlayerEventType.buffering)
}
}
private fun dispatchBufferedEvent() {
if (transitionToEvent(PlayerEvents.BUFFERED)) {
if (enableLogging) {
Log.d(TAG, "Dispatching Buffered event")
}
fastPixDataSDK.dispatchEvent(PlayerEventType.buffered)
}
}
private fun dispatchEndedEvent(currentPosition: Int? = null) {
if (transitionToEvent(PlayerEvents.ENDED)) {
if (enableLogging) {
Log.d(TAG, "Dispatching Ended event")
}
if (currentPosition != null) {
fastPixDataSDK.dispatchEvent(PlayerEventType.ended, currentPosition)
} else {
fastPixDataSDK.dispatchEvent(PlayerEventType.ended)
}
}
}
private fun dispatchVariantChangeEvent() {
if (transitionToEvent(PlayerEvents.VARIANT_CHANGED)) {
if (enableLogging) {
Log.d(TAG, "Dispatching VariantChange event")
}
fastPixDataSDK.dispatchEvent(PlayerEventType.variantChanged)
}
}
private fun dispatchErrorEvent() {
if (transitionToEvent(PlayerEvents.ERROR)) {
if (enableLogging) {
Log.d(TAG, "Dispatching Error event: $errorMessage")
}
fastPixDataSDK.dispatchEvent(PlayerEventType.error)
}
}
/**
* Validates if the transition from current state to new state is valid
*/
private fun isValidTransition(newEvent: PlayerEvents): Boolean {
val allowedTransitions = validTransitions[currentEventState] ?: emptySet()
return newEvent in allowedTransitions
}
/**
* Safely transitions to a new event state if valid
*/
private fun transitionToEvent(newEvent: PlayerEvents): Boolean {
if (isValidTransition(newEvent)) {
if (newEvent != PlayerEvents.VARIANT_CHANGED) {
currentEventState = newEvent
}
return true
} else {
return false
}
}
private fun initializeFastPixSDK() {
fastPixDataSDK = FastPixDataSDK()
val sdkConfiguration = SDKConfiguration(
playerData = customerData.playerDetails,
workspaceId = customerData.workspaceId,
beaconUrl = customerData.beaconUrl,
videoData = customerData.videoDetails,
playerListener = this,
enableLogging = enableLogging,
customData = customerData.customDataDetails,
)
fastPixDataSDK.initialize(sdkConfiguration, context)
}
override fun playerHeight(): Int? {
val density = context.resources.displayMetrics.density
val rawHeight = playerView.height
val height = ceil(rawHeight / density)
return height.toInt()
}
override fun playerWidth(): Int? {
val density = context.resources.displayMetrics.density
val rawWidth = playerView.width
val width = ceil(rawWidth / density)
return width.toInt()
}
private fun getPlayer(): ExoPlayer? = playerController.player
override fun videoSourceWidth(): Int? = videoSourceWidth
override fun videoSourceHeight(): Int? = videoSourceHeight
override fun playHeadTime(): Int? = getPlayer()?.currentPosition?.toInt() ?: 0
override fun mimeType(): String? =
Utils.getMimeTypeFromUrl(playerController.playerConfig?.contentUrl)
override fun sourceFps(): String? = null
override fun sourceAdvertisedBitrate(): String? = null
override fun sourceAdvertiseFrameRate(): String? = null
override fun sourceDuration(): Int? = playerController.duration.toInt()
override fun isPause(): Boolean? = playerController.isPlaying == false
override fun isAutoPlay(): Boolean? = playerController.isPlayWhenReady
override fun preLoad(): Boolean? = false
override fun isBuffering(): Boolean? = currentEventState == PlayerEvents.BUFFERING
override fun playerCodec(): String? = null
override fun sourceHostName(): String? = null
override fun isLive(): Boolean? = playerController.isLive
override fun sourceUrl(): String? = playerController.playerConfig?.contentUrl
override fun isFullScreen(): Boolean? {
val orientation = context.resources.configuration.orientation
return orientation == Configuration.ORIENTATION_LANDSCAPE
}
override fun getBandWidthData(): BandwidthModel = BandwidthModel()
override fun getPlayerError(): ErrorModel = ErrorModel(errorCode, errorMessage)
override fun getVideoCodec(): String? = null
override fun getSoftwareName(): String? = CastLabsLibraryInfo.SDK_NAME
override fun getSoftwareVersion(): String? = CastLabsLibraryInfo.SDK_VERSION
fun release() {
fastPixDataSDK.release()
errorMessage = null
errorCode = null
playerController.removePlayerListener(this)
playerController.release()
playerController.player?.release()
videoSourceWidth = null
videoSourceHeight = null
currentEventState = null
}
override fun onFatalErrorOccurred(error: CastlabsPlayerException) {
errorMessage = error.cause.toString()
errorCode = error.type.toString()
dispatchErrorEvent()
}
override fun onError(p0: CastlabsPlayerException) {}
override fun onStateChanged(state: PlayerController.State) {
when (state) {
PlayerController.State.Playing -> dispatchPlayingEvent()
PlayerController.State.Idle -> dispatchPauseEvent()
PlayerController.State.Preparing -> {
dispatchViewBegin()
dispatchPlayerReadyEvent()
dispatchPlayEvent()
}
PlayerController.State.Buffering -> dispatchBufferingEvent()
PlayerController.State.Pausing -> dispatchPauseEvent()
PlayerController.State.Finished -> dispatchEndedEvent()
}
}
override fun onSeekTo(p0: Long) {
dispatchSeekingEvent()
}
override fun onSeekCompleted() {
if (!isSeeking) {
isSeeking = true
dispatchPauseEvent()
dispatchSeekingEvent()
}
}
override fun onVideoSizeChanged(width: Int, height: Int, p2: Float) {
videoSourceWidth = width
videoSourceHeight = height
dispatchVariantChangeEvent()
}
override fun onSeekRangeChanged(p0: Long, p1: Long) {}
override fun onPlaybackPositionChanged(position: Long) {
if (!isSeeking) {
lastStablePositionUs = position
return
}
isSeeking = false
dispatchSeekedEvent(position)
}
override fun onDisplayChanged(
p0: DisplayInfo?,
p1: Boolean,
) {
}
override fun onDurationChanged(p0: Long) {
}
override fun onSpeedChanged(p0: Float) {
}
override fun onPlayerModelChanged() {
}
override fun onVideoKeyStatusChanged(p0: List<VideoTrackQuality?>) {
}
override fun onFullyBuffered() {
dispatchBufferedEvent()
}
}