forked from bitmovin/bitmovin-player-web-analytics-conviva
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConvivaAnalytics.ts
851 lines (713 loc) · 29 KB
/
ConvivaAnalytics.ts
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
import {
AdBreak, AdBreakEvent, AdEvent, ErrorEvent, PlaybackEvent, PlayerAPI, PlayerEvent, PlayerEventBase,
SeekEvent, SourceConfig, TimeShiftEvent, VideoQualityChangedEvent,
} from 'bitmovin-player';
import { Html5Http } from './Html5Http';
import { Html5Logging } from './Html5Logging';
import { Html5Metadata } from './Html5Metadata';
import { Html5Storage } from './Html5Storage';
import { Html5Time } from './Html5Time';
import { Html5Timer } from './Html5Timer';
import { Timeout } from 'bitmovin-player-ui/dist/js/framework/timeout';
import { ContentMetadataBuilder, Metadata } from './ContentMetadataBuilder';
import { ObjectUtils } from './helper/ObjectUtils';
import { BrowserUtils } from './helper/BrowserUtils';
import { ArrayUtils } from 'bitmovin-player-ui/dist/js/framework/arrayutils';
import { DeviceMetadata } from './Html5Metadata';
import { AdBreakHelper } from './helper/AdBreakHelper';
type Player = PlayerAPI;
export interface ConvivaAnalyticsConfiguration {
/**
* Enables debug logging when set to true (default: false).
*/
debugLoggingEnabled?: boolean;
/**
* The TOUCHSTONE_SERVICE_URL for testing with Touchstone. Only to be used for development, must not be set in
* production or automated testing.
*/
gatewayUrl?: string;
/**
* Option to set the Conviva Device Category, which is used to assist with
* user agent string parsing by the Conviva SDK. (default: WEB)
*/
deviceCategory?: Conviva.Client.DeviceCategory;
}
export interface EventAttributes {
[key: string]: string;
}
export class ConvivaAnalytics {
private static readonly VERSION: string = '{{VERSION}}';
private static STALL_TRACKING_DELAY_MS = 100;
private readonly player: Player;
private events: typeof PlayerEvent;
private readonly handlers: PlayerEventWrapper;
private config: ConvivaAnalyticsConfiguration;
private readonly contentMetadataBuilder: ContentMetadataBuilder;
private readonly systemFactory: Conviva.SystemFactory;
private readonly client: Conviva.Client;
private playerStateManager: Conviva.PlayerStateManager;
private readonly logger: Conviva.LoggingInterface;
private sessionKey: number;
/**
* Tracks the ad playback status and is true between ON_AD_STARTED and ON_AD_FINISHED/SKIPPED/ERROR.
* This flag is required because player.isAd() is unreliable and not always true between the events.
*/
private isAd: boolean;
/**
* Attributes needed to workaround wrong event order in case of a pre-roll ad.
* See {@link onAdBreakStarted} for more info
*/
private adBreakStartedToFire: AdBreakEvent;
/**
* Needed to workaround wrong event order in case of a video-playback-quality-change event.
* See {@link onVideoQualityChanged} for more info
*/
private lastSeenBitrate: number;
// Since there are no stall events during play / playing; seek / seeked; timeShift / timeShifted we need
// to track stalling state between those events. To prevent tracking eg. when seeking in buffer we delay it.
private stallTrackingTimout: Timeout = new Timeout(ConvivaAnalytics.STALL_TRACKING_DELAY_MS, () => {
this.playerStateManager.setPlayerState(Conviva.PlayerStateManager.PlayerState.BUFFERING);
});
/**
* Boolean to track whether a session was ended by an upstream caller instead of within internal session management.
* If this is true, we should avoid initializing a new session internally if a session is not active
*/
private sessionEndedExternally = false;
constructor(player: Player, customerKey: string, config: ConvivaAnalyticsConfiguration = {}) {
if (typeof Conviva === 'undefined') {
console.error('Conviva script missing, cannot init ConvivaAnalytics. '
+ 'Please load the Conviva script (conviva-core-sdk.min.js) before Bitmovin\'s ConvivaAnalytics integration.');
return; // Cancel initialization
}
if (player.getSource()) {
console.error('Bitmovin Conviva integration must be instantiated before calling player.load()');
return; // Cancel initialization
}
this.player = player;
// TODO: Use alternative to deprecated player.exports
this.events = player.exports.PlayerEvent;
this.handlers = new PlayerEventWrapper(player);
this.config = config;
// Set default config values
this.config.debugLoggingEnabled = this.config.debugLoggingEnabled || false;
this.config.deviceCategory = this.config.deviceCategory || Conviva.Client.DeviceCategory.WEB;
this.logger = new Html5Logging();
this.sessionKey = Conviva.Client.NO_SESSION_KEY;
this.isAd = false;
const deviceMetadata: DeviceMetadata = {
deviceCategory: this.config.deviceCategory,
};
const systemInterface = new Conviva.SystemInterface(
new Html5Time(),
new Html5Timer(),
new Html5Http(),
new Html5Storage(),
new Html5Metadata(deviceMetadata),
this.logger,
);
const systemSettings = new Conviva.SystemSettings();
this.systemFactory = new Conviva.SystemFactory(systemInterface, systemSettings);
const clientSettings = new Conviva.ClientSettings(customerKey);
if (config.gatewayUrl) {
clientSettings.gatewayUrl = config.gatewayUrl;
}
this.client = new Conviva.Client(clientSettings, this.systemFactory);
this.contentMetadataBuilder = new ContentMetadataBuilder(this.logger);
this.registerPlayerEvents();
}
/**
* Initializes a new conviva tracking session.
*
* Warning: The integration can only be validated without external session managing. So when using this method we can
* no longer ensure that the session is managed at the correct time. Additional: Since some metadata attributes
* relies on the players source we can't ensure that all metadata attributes are present at session creation.
* Therefore it could be that there will be a 'ContentMetadata created late' issue after conviva validation.
*
* If no source was loaded and no assetName was set via updateContentMetadata this method will throw an error.
*/
public initializeSession(): void {
if (this.isSessionActive()) {
this.logger.consoleLog('There is already a session running.', Conviva.SystemSettings.LogLevel.WARNING);
return;
}
// This could be called before source loaded.
// Without setting the asset name on the content metadata the SDK will throw errors when we initialize the session.
if (!this.player.getSource() && !this.contentMetadataBuilder.assetName) {
throw('AssetName is missing. Load player source first or set assetName via updateContentMetadata');
}
this.internalInitializeSession();
this.sessionEndedExternally = false;
}
/**
* Ends the current conviva tracking session.
* Results in a no-opt if there is no active session.
*
* Warning: Sessions will no longer be created automatically after this method has been called.
*
* The integration can only be validated without external session managing. So when using this method we can
* no longer ensure that the session is managed at the correct time.
*/
public endSession(): void {
if (!this.isSessionActive()) {
return;
}
this.internalEndSession();
this.resetContentMetadata();
this.sessionEndedExternally = true;
}
/**
* Sends a custom application-level event to Conviva's Player Insight. An application-level event can always
* be sent and is not tied to a specific video.
* @param eventName arbitrary event name
* @param eventAttributes a string-to-string dictionary object with arbitrary attribute keys and values
*/
public sendCustomApplicationEvent(eventName: string, eventAttributes: EventAttributes = {}): void {
this.client.sendCustomEvent(Conviva.Client.NO_SESSION_KEY, eventName, eventAttributes);
}
/**
* Sends a custom playback-level event to Conviva's Player Insight. A playback-level event can only be sent
* during an active video session.
* @param eventName arbitrary event name
* @param eventAttributes a string-to-string dictionary object with arbitrary attribute keys and values
*/
public sendCustomPlaybackEvent(eventName: string, eventAttributes: EventAttributes = {}): void {
// Check for active session
if (!this.isSessionActive()) {
this.logger.consoleLog('cannot send playback event, no active monitoring session',
Conviva.SystemSettings.LogLevel.WARNING);
return;
}
this.client.sendCustomEvent(this.sessionKey, eventName, eventAttributes);
}
/**
* Will update the contentMetadata which are tracked with conviva.
*
* If there is an active session only permitted values will be updated and propagated immediately.
* If there is no active session the values will set on session creation.
*
* Attributes set via this method will override automatic tracked once.
* @param metadataOverrides Metadata attributes which will be used to track to conviva.
* @see ContentMetadataBuilder for more information about permitted attributes
*/
public updateContentMetadata(metadataOverrides: Metadata) {
this.internalUpdateContentMetadata(metadataOverrides);
}
/**
* Sends a custom deficiency event during playback to Conviva's Player Insight. If no session is active it will NOT
* create one.
*
* @param message Message which will be send to conviva
* @param severity One of FATAL or WARNING
* @param endSession Boolean flag if session should be closed after reporting the deficiency (Default: true)
*/
public reportPlaybackDeficiency(message: string, severity: Conviva.Client.ErrorSeverity, endSession: boolean = true) {
if (!this.isSessionActive()) {
return;
}
this.client.reportError(this.sessionKey, message, severity);
if (endSession) {
this.internalEndSession();
this.resetContentMetadata();
}
}
/**
* Puts the session state in a notMonitored state.
*/
public pauseTracking(): void {
// AdStart is the right way to pause monitoring according to conviva.
this.client.adStart(
this.sessionKey,
Conviva.Client.AdStream.SEPARATE,
Conviva.Client.AdPlayer.SEPARATE,
Conviva.Client.AdPosition.PREROLL, // Also stops tracking time for VST so PREROLL seems to be sufficient
);
this.client.detachPlayer(this.sessionKey);
this.debugLog('Tracking paused.');
}
/**
* Puts the session state from a notMonitored state into the last one tracked.
*/
public resumeTracking(): void {
// AdEnd is the right way to resume monitoring according to conviva.
this.client.attachPlayer(this.sessionKey, this.playerStateManager);
this.client.adEnd(this.sessionKey);
this.debugLog('Tracking resumed.');
}
public release(): void {
this.destroy();
}
private destroy(event?: PlayerEventBase): void {
this.unregisterPlayerEvents();
this.internalEndSession(event);
this.client.release();
this.systemFactory.release();
}
private debugLog(message?: any, ...optionalParams: any[]): void {
if (this.config.debugLoggingEnabled) {
console.log.apply(console, arguments);
}
}
private getUrlFromSource(source: SourceConfig): string {
switch (this.player.getStreamType()) {
case 'dash':
return source.dash;
case 'hls':
return source.hls;
case 'progressive':
if (Array.isArray(source.progressive)) {
// TODO check if the first stream can be another index (e.g. ordered by bitrate), and select the current
// startup url
return source.progressive[0].url;
} else {
return source.progressive;
}
}
}
private internalUpdateContentMetadata(metadataOverrides: Metadata) {
this.contentMetadataBuilder.setOverrides(metadataOverrides);
if (!this.isSessionActive()) {
this.logger.consoleLog(
'[ ConvivaAnalytics ] no active session. Content metadata will be propagated to Conviva on session initialization.',
Conviva.SystemSettings.LogLevel.DEBUG,
);
return;
}
this.buildContentMetadata();
this.updateSession();
}
/**
* A Conviva Session should only be initialized when there is a source provided in the player because
* Conviva only allows to update different `contentMetadata` only at different times.
*
* The session should be created as soon as there was a play intention from the user.
*
* Set only once:
* - assetName
*
* Update before first video frame:
* - viewerId
* - streamType
* - playerName
* - duration
* - custom
*
* Multiple updates during session:
* - streamUrl
* - defaultResource (unused)
* - encodedFrameRate (unused)
*/
private internalInitializeSession() {
// initialize PlayerStateManager
this.playerStateManager = this.client.getPlayerStateManager();
this.playerStateManager.setPlayerType('Bitmovin Player');
this.playerStateManager.setPlayerVersion(this.player.version);
this.buildContentMetadata();
// Create a Conviva monitoring session.
this.sessionKey = this.client.createSession(this.contentMetadataBuilder.build()); // this will make the initial request
this.debugLog('[ ConvivaAnalytics ] start session', this.sessionKey);
if (!this.isSessionActive()) {
// Something went wrong. With stable system interfaces, this should never happen.
this.logger.consoleLog('Something went wrong, could not obtain session key',
Conviva.SystemSettings.LogLevel.ERROR);
}
this.playerStateManager.setPlayerState(Conviva.PlayerStateManager.PlayerState.STOPPED);
this.client.attachPlayer(this.sessionKey, this.playerStateManager);
if (this.lastSeenBitrate) {
this.playerStateManager.setBitrateKbps(this.lastSeenBitrate);
}
}
/**
* Update contentMetadata which must be present before first video frame
*/
private buildContentMetadata() {
this.contentMetadataBuilder.duration = this.player.getDuration();
this.contentMetadataBuilder.streamType = this.player.isLive() ? Conviva.ContentMetadata.StreamType.LIVE : Conviva.ContentMetadata.StreamType.VOD;
this.contentMetadataBuilder.custom = {
// Autoplay and preload are important options for the Video Startup Time so we track it as custom tags
autoplay: PlayerConfigHelper.getAutoplayConfig(this.player) + '',
preload: PlayerConfigHelper.getPreloadConfig(this.player) + '',
integrationVersion: ConvivaAnalytics.VERSION,
};
const source = this.player.getSource();
// This could be called before we got a source
if (source) {
this.buildSourceRelatedMetadata(source);
}
}
private buildSourceRelatedMetadata(source: SourceConfig) {
this.contentMetadataBuilder.assetName = this.getAssetNameFromSource(source);
this.contentMetadataBuilder.viewerId = this.contentMetadataBuilder.viewerId;
this.contentMetadataBuilder.custom = {
...this.contentMetadataBuilder.custom,
playerType: this.player.getPlayerType(),
streamType: this.player.getStreamType(),
vrContentType: source.vr && source.vr.contentType,
};
this.contentMetadataBuilder.streamUrl = this.getUrlFromSource(source);
}
private updateSession() {
if (!this.isSessionActive()) {
return;
}
this.client.updateContentMetadata(this.sessionKey, this.contentMetadataBuilder.build());
}
private getAssetNameFromSource(source: SourceConfig): string {
let assetName;
const assetTitle = source.title;
if (assetTitle) {
assetName = assetTitle;
} else {
assetName = 'Untitled (no source.title set)';
}
return assetName;
}
private internalEndSession = (event?: PlayerEventBase) => {
if (!this.isSessionActive()) {
return;
}
this.debugLog('[ ConvivaAnalytics ] end session', this.sessionKey, event);
this.client.detachPlayer(this.sessionKey);
this.client.cleanupSession(this.sessionKey);
this.client.releasePlayerStateManager(this.playerStateManager);
this.sessionKey = Conviva.Client.NO_SESSION_KEY;
this.lastSeenBitrate = null;
};
private resetContentMetadata(): void {
this.contentMetadataBuilder.reset();
}
private isSessionActive(): boolean {
return this.sessionKey !== Conviva.Client.NO_SESSION_KEY;
}
private onPlaybackStateChanged = (event: PlayerEventBase) => {
// Do not track playback state changes during ads, (e.g. triggered from IMA)
// or if there is no active session.
if (this.isAd || !this.isSessionActive()) {
return;
}
let playerState;
switch (event.type) {
case this.events.Play:
case this.events.Seek:
case this.events.TimeShift:
this.stallTrackingTimout.start();
break;
case this.events.StallStarted:
this.stallTrackingTimout.clear();
playerState = Conviva.PlayerStateManager.PlayerState.BUFFERING;
break;
case this.events.Playing:
this.stallTrackingTimout.clear();
// In case of a pre-roll ad we need to fire the trackAdBreakStarted right after we got a onPlay
// See onAdBreakStarted for more details.
if (this.adBreakStartedToFire) {
this.trackAdBreakStarted(this.adBreakStartedToFire);
this.adBreakStartedToFire = null;
}
playerState = Conviva.PlayerStateManager.PlayerState.PLAYING;
break;
case this.events.Paused:
this.stallTrackingTimout.clear();
playerState = Conviva.PlayerStateManager.PlayerState.PAUSED;
break;
case this.events.Seeked:
case this.events.TimeShifted:
case this.events.StallEnded:
this.stallTrackingTimout.clear();
if (this.player.isPlaying()) {
playerState = Conviva.PlayerStateManager.PlayerState.PLAYING;
} else {
playerState = Conviva.PlayerStateManager.PlayerState.PAUSED;
}
break;
case this.events.PlaybackFinished:
this.stallTrackingTimout.clear();
playerState = Conviva.PlayerStateManager.PlayerState.STOPPED;
break;
}
if (playerState) {
this.debugLog('[ ConvivaAnalytics ] report playback state', playerState);
this.playerStateManager.setPlayerState(playerState);
}
};
private onSourceLoaded = (event: PlayerEventBase) => {
// In case the session was created external before loading the source
if (!this.isSessionActive()) {
return;
}
this.buildSourceRelatedMetadata(this.player.getSource());
this.updateSession();
};
private onPlay = (event: PlaybackEvent) => {
this.debugLog('[ Player Event ] play', event);
if (this.isAd) {
// Do not track play event during ad (e.g. triggered from IMA)
return;
}
// in case the playback has finished and the user replays the stream create a new session
if (!this.isSessionActive() && !this.sessionEndedExternally) {
this.internalInitializeSession();
}
this.onPlaybackStateChanged(event);
};
private onPlaying = (event: PlaybackEvent) => {
this.contentMetadataBuilder.setPlaybackStarted(true);
this.debugLog('[ Player Event ] playing', event);
this.updateSession();
this.onPlaybackStateChanged(event);
};
private onPlaybackFinished = (event: PlayerEventBase) => {
this.debugLog('[ Player Event ] playback finished', event);
if (!this.isSessionActive()) {
return;
}
this.onPlaybackStateChanged(event);
this.internalEndSession(event);
this.resetContentMetadata();
};
private onVideoQualityChanged = (event: VideoQualityChangedEvent) => {
// We calculate the bitrate with a divisor of 1000 so the values look nicer
// Example: 250000 / 1000 => 250 kbps (250000 / 1024 => 244kbps)
const bitrateKbps = Math.round(event.targetQuality.bitrate / 1000);
if (!this.isSessionActive()) {
// Since the first videoPlaybackQualityChanged event comes before playback ever started we need to store the
// value and use it for tracking when initializing the session.
// TODO: remove this workaround when the player event order is fixed
this.lastSeenBitrate = bitrateKbps;
return;
}
this.lastSeenBitrate = null;
this.playerStateManager.setBitrateKbps(bitrateKbps);
};
private onCustomEvent = (event: PlayerEventBase) => {
if (!this.isSessionActive()) {
this.debugLog('skip custom event, no session existing', event);
return;
}
const eventAttributes = ObjectUtils.flatten(event);
this.sendCustomPlaybackEvent(event.type, eventAttributes);
};
private trackAdBreakStarted = (event: AdBreakEvent) => {
this.debugLog('[ ConvivaAnalytics ] adbreak started', event);
this.isAd = true;
const adPosition = AdBreakHelper.mapAdPosition(event.adBreak, this.player);
if (!this.isSessionActive()) {
// Don't report without a valid session (e.g., in case of a pre-roll, or post-roll ad)
return;
}
this.client.adStart(this.sessionKey, Conviva.Client.AdStream.SEPARATE, Conviva.Client.AdPlayer.CONTENT, adPosition);
};
private onAdBreakFinished = (event: AdBreakEvent | ErrorEvent) => {
this.debugLog('[ ConvivaAnalytics ] adbreak finished', event);
this.isAd = false;
if (!this.isSessionActive()) {
// Don't report without a valid session (e.g., in case of a pre-roll, or post-roll ad)
return;
}
this.client.adEnd(this.sessionKey);
this.playerStateManager.setPlayerState(Conviva.PlayerStateManager.PlayerState.PLAYING);
};
private onAdSkipped = (event: AdEvent) => {
this.onCustomEvent(event);
};
private onAdError = (event: AdEvent) => {
this.onCustomEvent(event);
};
private onSeek = (event: SeekEvent) => {
if (!this.isSessionActive()) {
// Handle the case that the User seeks on the UI before play was triggered.
// This also handles startTime feature. The same applies for onTimeShift.
return;
}
this.trackSeekStart(event.seekTarget);
this.onPlaybackStateChanged(event);
};
private onSeeked = (event: SeekEvent) => {
if (!this.isSessionActive()) {
// See comment in onSeek
return;
}
this.trackSeekEnd();
this.onPlaybackStateChanged(event);
};
private onTimeShift = (event: TimeShiftEvent) => {
if (!this.isSessionActive()) {
// See comment in onSeek
return;
}
// According to conviva it is valid to pass -1 for seeking in live streams
this.trackSeekStart(-1);
this.onPlaybackStateChanged(event);
};
private onTimeShifted = (event: TimeShiftEvent) => {
if (!this.isSessionActive()) {
// See comment in onSeek
return;
}
this.trackSeekEnd();
this.onPlaybackStateChanged(event);
};
private trackSeekStart(target: number) {
this.playerStateManager.setPlayerSeekStart(Math.round(target));
}
private trackSeekEnd() {
this.playerStateManager.setPlayerSeekEnd();
}
private onError = (event: ErrorEvent) => {
if (!this.isSessionActive() && !this.sessionEndedExternally) {
// initialize Session if not yet initialized to capture Video Start Failures
this.internalInitializeSession();
}
this.reportPlaybackDeficiency(String(event.code) + ' ' + event.name, Conviva.Client.ErrorSeverity.FATAL);
};
private onSourceUnloaded = (event: PlayerEventBase) => {
if (this.isAd) {
// Ignore sourceUnloaded events during ads
return;
} else {
this.internalEndSession(event);
this.resetContentMetadata();
}
};
private onDestroy = (event: any) => {
this.destroy(event);
};
private registerPlayerEvents(): void {
const playerEvents = this.handlers;
playerEvents.add(this.events.SourceLoaded, this.onSourceLoaded);
playerEvents.add(this.events.Play, this.onPlay);
playerEvents.add(this.events.Playing, this.onPlaying);
playerEvents.add(this.events.Paused, this.onPlaybackStateChanged);
playerEvents.add(this.events.StallStarted, this.onPlaybackStateChanged);
playerEvents.add(this.events.StallEnded, this.onPlaybackStateChanged);
playerEvents.add(this.events.PlaybackFinished, this.onPlaybackFinished);
playerEvents.add(this.events.VideoPlaybackQualityChanged, this.onVideoQualityChanged);
playerEvents.add(this.events.AudioPlaybackQualityChanged, this.onCustomEvent);
playerEvents.add(this.events.Muted, this.onCustomEvent);
playerEvents.add(this.events.Unmuted, this.onCustomEvent);
playerEvents.add(this.events.ViewModeChanged, this.onCustomEvent);
playerEvents.add(this.events.AdBreakStarted, this.onAdBreakStarted);
playerEvents.add(this.events.AdBreakFinished, this.onAdBreakFinished);
playerEvents.add(this.events.AdSkipped, this.onAdSkipped);
playerEvents.add(this.events.AdError, this.onAdError);
playerEvents.add(this.events.SourceUnloaded, this.onSourceUnloaded);
playerEvents.add(this.events.Error, this.onError);
playerEvents.add(this.events.Destroy, this.onDestroy);
playerEvents.add(this.events.Seek, this.onSeek);
playerEvents.add(this.events.Seeked, this.onSeeked);
playerEvents.add(this.events.TimeShift, this.onTimeShift);
playerEvents.add(this.events.TimeShifted, this.onTimeShifted);
playerEvents.add(this.events.CastStarted, this.onCustomEvent);
playerEvents.add(this.events.CastStopped, this.onCustomEvent);
}
private onAdBreakStarted = (event: AdBreakEvent) => {
// Specific post-roll handling
const isPostRollAdBreak = (adBreak: AdBreak) => {
return adBreak.scheduleTime === Infinity;
};
if (isPostRollAdBreak(event.adBreak)) {
// Fire playbackFinished in case of a post-roll ad to stop session and do not track post roll ad
this.debugLog('[ ConvivaAnalytics ] detected post-roll ad ... Ending session');
this.onPlaybackFinished({
timestamp: Date.now(),
type: this.events.PlaybackFinished,
},
);
return;
}
// Specific pre-roll handling
// TODO: remove this workaround when event order is correct
// Since the event order on initial playback in case of a pre-roll ad is false we need to workaround
// a to early triggered adBreakStarted event. The initial onPlay event is called after the AdBreakStarted
// of the pre-roll ad so we won't initialize a session. Therefore we save the adBreakStarted event and
// trigger it in the initial onPlay event (after initializing the session). (See #onPlaybackStateChanged)
if (!this.isSessionActive()) {
this.adBreakStartedToFire = event;
} else {
this.trackAdBreakStarted(event);
}
};
private unregisterPlayerEvents(): void {
this.handlers.clear();
}
static get version(): string {
return ConvivaAnalytics.VERSION;
}
}
class PlayerConfigHelper {
/**
* The config for autoplay and preload have great impact to the VST (Video Startup Time) we track it.
* Since there is no way to get default config values from the player they are hardcoded.
*/
public static AUTOPLAY_DEFAULT_CONFIG: boolean = false;
/**
* Extract autoplay config form player
*
* @param player: Player
*/
public static getAutoplayConfig(player: Player): boolean {
const playerConfig = player.getConfig();
if (playerConfig.playback && playerConfig.playback.autoplay !== undefined) {
return playerConfig.playback.autoplay;
} else {
return PlayerConfigHelper.AUTOPLAY_DEFAULT_CONFIG;
}
}
/**
* Extract preload config from player
*
* The preload config can be set individual for mobile or desktop as well as on root level for both platforms.
* Default value is true for VOD and false for live streams. If the value is not set for current platform or on root
* level the default value will be used over the value for the other platform.
*
* @param player: Player
*/
public static getPreloadConfig(player: Player): boolean {
const playerConfig = player.getConfig();
if (BrowserUtils.isMobile()) {
if (playerConfig.adaptation
&& playerConfig.adaptation.mobile
&& playerConfig.adaptation.mobile.preload !== undefined) {
return playerConfig.adaptation.mobile.preload;
}
} else {
if (playerConfig.adaptation
&& playerConfig.adaptation.desktop
&& playerConfig.adaptation.desktop.preload !== undefined) {
return playerConfig.adaptation.desktop.preload;
}
}
if (playerConfig.adaptation
&& playerConfig.adaptation.preload !== undefined) {
return playerConfig.adaptation.preload;
}
return !player.isLive();
}
}
class PlayerEventWrapper {
private player: Player;
private readonly eventHandlers: { [eventType: string]: Array<(event?: PlayerEventBase) => void>; };
constructor(player: Player) {
this.player = player;
this.eventHandlers = {};
}
public add(eventType: PlayerEvent, callback: (event?: PlayerEventBase) => void): void {
this.player.on(eventType, callback);
if (!this.eventHandlers[eventType]) {
this.eventHandlers[eventType] = [];
}
this.eventHandlers[eventType].push(callback);
}
public remove(eventType: PlayerEvent, callback: (event?: PlayerEventBase) => void): void {
this.player.off(eventType, callback);
if (this.eventHandlers[eventType]) {
ArrayUtils.remove(this.eventHandlers[eventType], callback);
}
}
public clear(): void {
for (const eventType in this.eventHandlers) {
for (const callback of this.eventHandlers[eventType]) {
this.remove(eventType as PlayerEvent, callback);
}
}
}
}