-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathcreateVideojsPlayer.ts
200 lines (171 loc) · 5.56 KB
/
createVideojsPlayer.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
import 'video.js/dist/video-js.css';
import videojs, { VideoJsPlayer, VideoJsPlayerOptions } from 'video.js';
import 'videojs-contrib-quality-levels';
import 'videojs-http-source-selector';
import './videojs/qualitySelectorPlugin';
import { appData, getDecodedJwt } from '../data/appData';
import { QualityLevels } from '../types/libs/video.js/extend';
import { Video } from '../types/tracks';
import {
InitializedContextExtensions,
InteractedContextExtensions,
} from '../types/XAPI';
import { report } from '../utils/errors/report';
import { isMSESupported } from '../utils/isMSESupported';
import { Nullable } from '../utils/types';
import { XAPIStatement } from '../XAPI/XAPIStatement';
import { intl } from '../index';
import { Events } from './videojs/qualitySelectorPlugin/types';
export const createVideojsPlayer = (
videoNode: HTMLVideoElement,
dispatchPlayerTimeUpdate: (time: number) => void,
video: Video,
): VideoJsPlayer => {
// add the video-js class name to the video attribute.
videoNode.classList.add('video-js', 'vjs-big-play-centered');
const options: VideoJsPlayerOptions = {
controls: true,
debug: false,
fluid: true,
html5: {
vhs: {
// prevent to have a resolution selected higher than
// the player size. By default this is set to true.
// We set it to true here to remember this undocumented option.
limitRenditionByPlayerDimensions: true,
overrideNative: !videojs.browser.IS_SAFARI,
// take the device pixel ratio into account when doing rendition switching.
// This means that if you have a player with the width of 540px in a high density
// display with a device pixel ratio of 2, a rendition of 1080p will be allowed.
useDevicePixelRatio: true,
},
},
language: intl.locale,
liveui: video.live_state !== null,
playbackRates: [0.5, 0.75, 1, 1.25, 1.5, 1.75, 2, 4],
responsive: true,
};
if (!isMSESupported()) {
options.plugins = {
qualitySelector: {
default: '480',
},
};
}
const player = videojs(videoNode, options);
if (isMSESupported()) {
player.httpSourceSelector();
const qualityLevels = player.qualityLevels();
qualityLevels.on('change', () => interacted(qualityLevels));
} else {
player.on(Events.PLAYER_SOURCES_CHANGED, () => interacted());
}
const tracks = player.remoteTextTracks();
/************************** XAPI **************************/
let xapiStatement: XAPIStatement;
try {
xapiStatement = new XAPIStatement(appData.jwt!, getDecodedJwt().session_id);
} catch (error) {
report(error);
throw error;
}
let currentTime: number = 0;
let seekingAt: number = 0;
let hasSeeked: boolean = false;
let isInitialized: boolean = false;
const getCurrentTrack = (): Nullable<TextTrack> => {
// TextTrackList is not an iterable object
// tslint:disable-next-line:prefer-for-of
for (let i = 0; i < tracks.length; i++) {
if (tracks[i].mode === 'showing') {
return tracks[i];
}
}
return null;
};
let currentTrack = getCurrentTrack();
player.on('canplaythrough', () => {
if (isInitialized) {
return;
}
const contextExtensions: InitializedContextExtensions = {
ccSubtitleEnabled: currentTrack !== null,
fullScreen: player.isFullscreen(),
length: player.duration(),
speed: `${player.playbackRate()}x`,
volume: player.volume(),
};
xapiStatement.initialized(contextExtensions);
isInitialized = true;
});
player.on('playing', () => {
xapiStatement.played({
time: player.currentTime(),
});
});
player.on('pause', () => {
xapiStatement.paused({
time: player.currentTime(),
});
});
/**************** Seeked statement ***********************/
player.on('timeupdate', () => {
if (isInitialized && !player.seeking()) {
currentTime = player.currentTime();
}
dispatchPlayerTimeUpdate(player.currentTime());
});
player.on('seeking', () => {
seekingAt = currentTime;
hasSeeked = true;
});
player.on('seeked', () => {
if (!hasSeeked) {
return;
}
hasSeeked = false;
xapiStatement.seeked({
timeFrom: seekingAt,
timeTo: player.currentTime(),
});
});
/**************** Interacted event *************************/
const interacted = (qualityLevels?: QualityLevels): void => {
if (!isInitialized) {
return;
}
let quality: string | number;
if (qualityLevels) {
quality = qualityLevels[qualityLevels.selectedIndex]?.height;
} else {
quality = player.currentSource().size!;
}
const contextExtensions: InteractedContextExtensions = {
ccSubtitleEnabled: currentTrack !== null,
fullScreen: player.isFullscreen(),
quality,
speed: `${player.playbackRate()}x`,
volume: player.volume(),
};
if (currentTrack !== null) {
contextExtensions.ccSubtitleLanguage = currentTrack.language;
}
xapiStatement.interacted({ time: player.currentTime() }, contextExtensions);
};
player.on('fullscreenchange', () => interacted());
player.on('languagechange', () => interacted());
player.on('ratechange', () => interacted());
player.on('volumechange', () => interacted());
tracks.addEventListener('change', () => {
currentTrack = getCurrentTrack();
interacted();
});
/**************** End interacted event *************************/
window.addEventListener('unload', () => {
if (!isInitialized) {
return;
}
xapiStatement.terminated({ time: player.currentTime() });
});
return player;
};