-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
184 lines (160 loc) · 4.46 KB
/
index.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
import {
isWithCustomOptions,
isWithMediaUrl,
PlayerAnalytics,
PlayerAnalyticsOptions,
} from "@api.video/player-analytics";
import videojs, { VideoJsPlayer } from "video.js";
const Plugin = videojs.getPlugin("plugin");
declare module "video.js" {
// this tells the type system that the VideoJsPlayer object has a method seekButtons
export interface VideoJsPlayer {
apiVideoAnalytics(options?: VideoJsApiVideoAnalyticsOptions): void;
}
}
export class VideoJsApiVideoAnalytics extends Plugin {
private options: VideoJsApiVideoAnalyticsOptions;
private isFirstInit = true;
private lastSegmentBandwidth = 0;
private playerAnalytics!: PlayerAnalytics;
private passthrough: boolean = false;
private lastMediaUrl?: string = undefined;
constructor(player: VideoJsPlayer, options: VideoJsApiVideoAnalyticsOptions) {
super(player);
this.options = options || {};
if (!isWithCustomOptions(this.options) && !isWithMediaUrl(this.options)) {
player.on("loadstart", (_) => {
const src = player.src();
if (this.isApiVideoMediaUrl(src)) {
this.passthrough = false;
if (this.lastMediaUrl !== src) {
(this.options as any).mediaUrl = src;
this.setOptions(this.options);
this.lastMediaUrl = src;
}
} else {
this.passthrough = true;
}
});
} else {
this.setOptions(this.options);
}
}
public setOptions(options: VideoJsApiVideoAnalyticsOptions) {
this.options = options;
this.playerAnalytics = new PlayerAnalytics({
...options,
});
const videoElt = this.player.el().querySelector("video");
if (videoElt) {
this.playerAnalytics.ovbserveMedia(videoElt);
} else {
console.error("No video element found in the player");
}
this.player.ready(() => {
if (this.isFirstInit) {
events.forEach((eventName) =>
this.player.on(eventName, (event: any) =>
this.handleEvent(eventName, event)
)
);
}
this.isFirstInit = false;
});
}
private isApiVideoMediaUrl(mediaUrl: string): boolean {
try {
PlayerAnalytics.parseMediaUrl(mediaUrl);
return true;
} catch (e: any) {
return false;
}
}
private initSegmentsWatcher() {
const tracks = this.player.textTracks();
if (!tracks) {
return;
}
// tslint:disable-next-line
for (let i = 0; i < tracks.length; i++) {
if (tracks[i].label === "segment-metadata") {
const segmentMetadataTrack = tracks[i] as any;
segmentMetadataTrack.on("cuechange", () => {
const activeCue = segmentMetadataTrack.activeCues[0];
if (!activeCue?.value) {
return;
}
this.handleEvent("segmentchange", activeCue.value);
if (this.lastSegmentBandwidth !== activeCue.value.bandwidth) {
this.lastSegmentBandwidth = activeCue.value.bandwidth;
this.handleEvent("qualitychange", activeCue.value.resolution);
}
});
return;
}
}
}
private handleEvent(eventName: string, event: any) {
if (this.passthrough) {
return;
}
if (this.options.onEvent && eventName === "loadedmetadata") {
this.options.onEvent({ type: "ready" });
this.initSegmentsWatcher();
}
if (this.options.onEvent) {
this.options.onEvent({
type: eventName,
...(eventName === "timeupdate"
? { currentTime: this.player.currentTime() }
: {}),
...(eventName === "volumechange"
? { volume: this.player.volume() }
: {}),
...(eventName === "segmentchange" ? { segment: event } : {}),
...(eventName === "qualitychange" ? { resolution: event } : {}),
});
}
}
}
const events = [
"controlsdisabled",
"controlsenabled",
"dispose",
"durationchange",
"ended",
"enterFullWindow",
"enterpictureinpicture",
"error",
"exitFullWindow",
"firstplay",
"fullscreenchange",
"leavepictureinpicture",
"loadedmetadata",
"loadstart",
"pause",
"play",
"playerreset",
"playerresize",
"playing",
"posterchange",
"ratechange",
"ready",
"resize",
"seeked",
"seeking",
"textdata",
"timeupdate",
"useractive",
"userinactive",
"volumechange",
"waiting",
];
export type VideoJsApiVideoAnalyticsOptions = PlayerAnalyticsOptions & {
onEvent?: (event: any) => void;
};
type VideoQuality = {
width: number;
height: number;
bandwidth: number;
};