-
Notifications
You must be signed in to change notification settings - Fork 1
/
CurrentMediaItemTracker.kt
190 lines (172 loc) · 6.73 KB
/
CurrentMediaItemTracker.kt
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
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.player.tracker
import androidx.annotation.VisibleForTesting
import androidx.media3.common.MediaItem
import androidx.media3.common.Player
import androidx.media3.common.Timeline
import androidx.media3.exoplayer.ExoPlayer
import ch.srgssr.pillarbox.player.extension.getMediaItemTrackerData
import ch.srgssr.pillarbox.player.extension.getMediaItemTrackerDataOrNull
/**
* Current media item tracker
*
* Track current media item transition or lifecycle.
* Tracking session start when current item changed and have [MediaItemTrackerData] set.
* Tracking session stop when current item changed or when it reached the end of lifecycle.
*
* MediaItem asynchronously call this callback after loaded
* - onTimelineChanged with reason = [Player.TIMELINE_CHANGE_REASON_SOURCE_UPDATE]
*
* @param player The Player for which the current media item must be tracked.
* @param mediaItemTrackerProvider The MediaItemTrackerProvider that provide new instance of [MediaItemTracker].
*/
internal class CurrentMediaItemTracker internal constructor(
private val player: ExoPlayer,
private val mediaItemTrackerProvider: MediaItemTrackerProvider
) : Player.Listener {
/**
* Trackers are null if tracking session is stopped!
*/
private var trackers: MediaItemTrackerList? = null
/**
* Current media item
* Detect mediaId changes or urls if no mediaId
*/
private var currentMediaItem: MediaItem? = null
var enabled: Boolean = true
set(value) {
if (field == value) return
field = value
setMediaItem(player.currentMediaItem)
}
init {
player.addListener(this)
player.currentMediaItem?.let { startNewSession(it) }
}
/**
* Set media item if has not tracking data, set to null
*/
private fun setMediaItem(mediaItem: MediaItem?) {
if (enabled && mediaItem != null && mediaItem.canHaveTrackingSession()) {
if (!areEqual(currentMediaItem, mediaItem)) {
currentItemChange(currentMediaItem, mediaItem)
currentMediaItem = mediaItem
}
} else {
currentMediaItem?.let {
stopSession(MediaItemTracker.StopReason.Stop, player.currentPosition)
}
}
}
private fun currentItemChange(lastMediaItem: MediaItem?, newMediaItem: MediaItem) {
if (lastMediaItem == null) {
startNewSession(newMediaItem)
return
}
if (lastMediaItem.mediaId == newMediaItem.mediaId || lastMediaItem.getMediaItemTrackerData() != newMediaItem.getMediaItemTrackerData()) {
maybeUpdateData(lastMediaItem, newMediaItem)
} else {
stopSession(MediaItemTracker.StopReason.Stop)
startNewSession(newMediaItem)
}
}
/**
* Maybe update data
*
* Don't start or stop if new tracker data is added. Only update existing trackers with new data.
*/
private fun maybeUpdateData(lastMediaItem: MediaItem, newMediaItem: MediaItem) {
trackers?.let {
val lastTrackerData = lastMediaItem.getMediaItemTrackerData()
val newTrackerData = newMediaItem.getMediaItemTrackerData()
for (tracker in it) {
val newData = newTrackerData.getData(tracker) ?: continue
val oldData = lastTrackerData.getData(tracker)
if (newData != oldData) {
tracker.update(newData)
}
}
}
}
private fun stopSession(stopReason: MediaItemTracker.StopReason, positionMs: Long = player.currentPosition) {
trackers?.let {
for (tracker in it) {
tracker.stop(player, stopReason, positionMs)
}
}
trackers = null
currentMediaItem = null
}
private fun startNewSession(mediaItem: MediaItem) {
if (!enabled) return
require(trackers == null)
mediaItem.getMediaItemTrackerData().also { trackerData ->
val trackers = MediaItemTrackerList()
// Create each tracker for this new MediaItem
for (trackerType in trackerData.trackers) {
val tracker = mediaItemTrackerProvider.getMediaItemTrackerFactory(trackerType).create()
trackers.append(tracker)
tracker.start(player, trackerData.getData(tracker))
}
this.trackers = trackers
}
}
override fun onTimelineChanged(timeline: Timeline, reason: Int) {
setMediaItem(player.currentMediaItem)
}
override fun onPlaybackStateChanged(playbackState: Int) {
when (playbackState) {
Player.STATE_ENDED -> stopSession(MediaItemTracker.StopReason.EoF)
Player.STATE_IDLE -> stopSession(MediaItemTracker.StopReason.Stop)
Player.STATE_READY -> if (currentMediaItem == null) setMediaItem(player.currentMediaItem)
else -> {
// Nothing
}
}
}
override fun onPositionDiscontinuity(oldPosition: Player.PositionInfo, newPosition: Player.PositionInfo, reason: Int) {
val oldPositionMs = oldPosition.positionMs
when (reason) {
Player.DISCONTINUITY_REASON_REMOVE -> stopSession(MediaItemTracker.StopReason.Stop, oldPositionMs)
Player.DISCONTINUITY_REASON_AUTO_TRANSITION -> stopSession(MediaItemTracker.StopReason.EoF, oldPositionMs)
else -> {
// Nothing
}
}
}
/**
* On media item transition
*
* @param mediaItem maybe null when playlist become empty
* @param reason
*/
override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) {
setMediaItem(player.currentMediaItem)
}
internal companion object {
/**
* Are equals only checks mediaId and localConfiguration.uri
*
* @param m1
* @param m2
* @return
*/
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
internal fun areEqual(m1: MediaItem?, m2: MediaItem?): Boolean {
return when {
m1 == null && m2 == null -> true
m1 == null || m2 == null -> false
else -> m1.getIdentifier() == m2.getIdentifier() && m1.localConfiguration == m2.localConfiguration
}
}
private fun MediaItem.canHaveTrackingSession(): Boolean {
return this.getMediaItemTrackerDataOrNull() != null
}
private fun MediaItem.getIdentifier(): String? {
return if (mediaId == MediaItem.DEFAULT_MEDIA_ID) localConfiguration?.uri?.toString() else mediaId
}
}
}