-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBasicEventConvivaReporter.swift
More file actions
170 lines (142 loc) · 6.41 KB
/
BasicEventConvivaReporter.swift
File metadata and controls
170 lines (142 loc) · 6.41 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
//
// BasicEventConvivaReporter.swift
//
import ConvivaSDK
import THEOplayerSDK
#if canImport(THEOplayerTHEOadsIntegration)
import THEOplayerTHEOadsIntegration
#endif
class BasicEventConvivaReporter {
struct Session {
struct Source {
let description: SourceDescription
let url: String?
}
var started = false
var source: Source?
}
/// The endpoint to which all the events are sent
private let videoAnalytics: CISVideoAnalytics
private let storage: ConvivaConnectorStorage
var currentSession = Session()
init(videoAnalytics: CISVideoAnalytics, storage: ConvivaConnectorStorage) {
self.videoAnalytics = videoAnalytics
self.storage = storage
}
func play(event: PlayEvent) {
guard !self.currentSession.started else { return }
let initialContentInfo = Utilities.extendedContentInfo(contentInfo: [:], storage: self.storage)
self.videoAnalytics.reportPlaybackRequested(initialContentInfo)
self.currentSession.started = true
}
func playing(event: PlayingEvent) {
self.videoAnalytics.reportPlaybackMetric(CIS_SSDK_PLAYBACK_METRIC_PLAYER_STATE, value: PlayerState.CONVIVA_PLAYING.rawValue)
}
func timeUpdate(event: TimeUpdateEvent) {
self.videoAnalytics.reportPlaybackMetric(CIS_SSDK_PLAYBACK_METRIC_PLAY_HEAD_TIME, value: event.currentTimeInMilliseconds)
}
func pause(event: PauseEvent) {
self.videoAnalytics.reportPlaybackMetric(CIS_SSDK_PLAYBACK_METRIC_PLAYER_STATE, value: PlayerState.CONVIVA_PAUSED.rawValue)
}
func waiting(event: WaitingEvent) {
self.videoAnalytics.reportPlaybackMetric(CIS_SSDK_PLAYBACK_METRIC_PLAYER_STATE, value: PlayerState.CONVIVA_BUFFERING.rawValue)
}
func seeking(event: SeekingEvent) {
self.videoAnalytics.reportPlaybackMetric(CIS_SSDK_PLAYBACK_METRIC_SEEK_STARTED, value: event.currentTimeInMilliseconds)
}
func seeked(event: SeekedEvent) {
self.videoAnalytics.reportPlaybackMetric(CIS_SSDK_PLAYBACK_METRIC_SEEK_ENDED, value: event.currentTimeInMilliseconds)
}
func error(event: ErrorEvent) {
if self.currentSession.started {
self.videoAnalytics.reportPlaybackFailed(event.error, contentInfo: nil)
// the reportPlaybackFailed will close the session on the Conviva backend.
self.currentSession.started = false
}
}
func networkError(event: NetworkErrorEvent) {
self.videoAnalytics.reportPlaybackError(event.error?.message ?? Utilities.defaultStringValue, errorSeverity: .ERROR_WARNING)
}
func sourceChange(event: SourceChangeEvent, selectedSource: String?) {
if event.source != self.currentSession.source?.description, self.currentSession.source != nil {
self.reportEndedIfPlayed()
}
// clear all session specific stored values for the previous source
self.storage.clearValueForKey(CIS_SSDK_METADATA_ASSET_NAME) // asset name from the previous source
self.storage.clearValueForKey(CIS_SSDK_PLAYBACK_METRIC_BITRATE) // last reported bitrate for previous source
let newSource: Session.Source?
if let source = event.source, let url = selectedSource {
newSource = .init(description: source, url: url)
let assetName = source.metadata?.title ?? Utilities.defaultStringValue;
var contentInfo = [
CIS_SSDK_METADATA_PLAYER_NAME: Utilities.playerName,
CIS_SSDK_METADATA_STREAM_URL: url,
CIS_SSDK_METADATA_ASSET_NAME: assetName,
CIS_SSDK_METADATA_IS_LIVE: NSNumber(value: false),
CIS_SSDK_METADATA_DURATION: NSNumber(value: -1)
] as [String: Any]
// THEOads adTagParameters
if let adTagParams = self.theoAdsAdTagParams(source: newSource?.description) {
adTagParams.forEach { (key, value) in
contentInfo["theoAdsTag_\(key)"] = value
}
}
self.videoAnalytics.setContentInfo(contentInfo)
self.storage.storeKeyValuePair(key: CIS_SSDK_METADATA_ASSET_NAME, value: assetName)
self.storage.storeKeyValuePair(key: CIS_SSDK_METADATA_STREAM_URL, value: url)
} else {
newSource = nil
#if DEBUG
print("[THEOplayerConnectorConviva] setting unknown source")
#endif
}
self.currentSession.source = newSource
}
private func theoAdsAdTagParams(source: SourceDescription?) -> [String:String]? {
#if canImport(THEOplayerTHEOadsIntegration)
if let currentSource = source,
let ads = currentSource.ads,
let theoAdsDescription = ads.first(where: {
$0.integration == .theoAds && $0 is THEOAdDescription
}) as? THEOAdDescription,
let foundParams = theoAdsDescription.adTagParameters {
return foundParams
}
#endif
return [:]
}
func renderedFramerateUpdate(framerate: Float) {
self.videoAnalytics.reportPlaybackMetric(CIS_SSDK_PLAYBACK_METRIC_RENDERED_FRAMERATE, value: NSNumber(value: Int(framerate.rounded())))
}
func ended(event: EndedEvent) {
self.videoAnalytics.reportPlaybackMetric(CIS_SSDK_PLAYBACK_METRIC_PLAYER_STATE, value: PlayerState.CONVIVA_STOPPED.rawValue)
self.reportEndedIfPlayed()
}
func reportEndedIfPlayed() {
if self.currentSession.started {
self.videoAnalytics.reportPlaybackEnded()
self.videoAnalytics.cleanup()
self.currentSession = Session()
}
}
func durationChange(event: DurationChangeEvent) {
if let duration = event.duration, self.currentSession.source?.url != nil {
if duration.isInfinite {
self.videoAnalytics.setContentInfo([
CIS_SSDK_METADATA_IS_LIVE: NSNumber(value: true)
])
} else {
self.videoAnalytics.setContentInfo([
CIS_SSDK_METADATA_IS_LIVE: NSNumber(value: false),
CIS_SSDK_METADATA_DURATION: NSNumber(value: duration)
])
}
}
}
func destroy(event: DestroyEvent) {
self.reportEndedIfPlayed()
}
deinit {
self.reportEndedIfPlayed()
}
}