-
Notifications
You must be signed in to change notification settings - Fork 32
/
npm-sdk.ts
125 lines (109 loc) · 4.08 KB
/
npm-sdk.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
import {
create,
ErrorType,
isPlayerSupported,
MediaPlayer,
PlayerError,
PlayerEventType,
PlayerState,
Quality,
TextCue,
TextMetadataCue,
} from 'amazon-ivs-player';
import { setupForm, getFormStream } from '../common/form-control';
/**
* These imports are loaded via the file-loader, and return the path to the asset.
* We use the TypeScript compiler (TSC) to check types; it doesn't know what this WASM module is, so let's ignore the error it throws (TS2307).
*/
// @ts-ignore
import wasmBinaryPath from 'amazon-ivs-player/dist/assets/amazon-ivs-wasmworker.min.wasm';
import wasmWorkerPath from 'amazon-ivs-player/dist/assets/amazon-ivs-wasmworker.min.js';
class PlayerDemo {
private player: MediaPlayer;
private videoElement: HTMLVideoElement =
document.querySelector('#video-player');
constructor(stream: string) {
/**
* The IVS player can only be used in browsers which support WebAssembly.
* You should use `isPlayerSupported` before calling `create`.
* Otherwise, wrap `create` in a `try/catch` block, because an error will be thrown in browsers without WebAssembly support.
*/
if (!isPlayerSupported) {
throw new Error('IVS Player is not supported in this browser');
}
/**
* Web Workers and WASM Workers need to be created via URL. Webpack has created the relative URL for us via file-loader,
* now we just have to create the absolute (fully qualified) URL.
*/
const createAbsolutePath = (assetPath: string) =>
new URL(assetPath, document.URL).toString();
const player = (this.player = create({
wasmWorker: createAbsolutePath(wasmWorkerPath),
wasmBinary: createAbsolutePath(wasmBinaryPath),
}));
player.attachHTMLVideoElement(this.videoElement);
this.attachListeners();
const versionString: HTMLElement = document.querySelector('.version');
versionString.innerText = `Amazon IVS Player version ${player.getVersion()}`;
this.loadAndPlay(stream);
// We're adding the demo here so you can play with it in the console.
// @ts-ignore
window.player = player;
}
loadAndPlay(stream: string) {
const { player } = this;
/**
* With setAutoplay, we don't need to call play() here to try and start the stream. One of three things will happen:
* - Autoplay with sound
* - Autoplay muted
* - Playback blocked
* If autoplay is muted or blocked, the viewer will need to manually interact with the video player in order to unmute or start playback.
* See https://developers.google.com/web/updates/2017/09/autoplay-policy-changes for more info on autoplaying video and best practices.
* */
player.setAutoplay(true);
player.load(stream);
}
private attachListeners() {
const { player } = this;
for (let state of Object.values(PlayerState)) {
player.addEventListener(state, () => {
console.log(state);
});
}
player.addEventListener(PlayerEventType.INITIALIZED, () => {
console.log('INITIALIZED');
});
player.addEventListener(PlayerEventType.ERROR, (error: PlayerError) => {
const statusTooManyRequests = 429;
if (
error.type === ErrorType.NOT_AVAILABLE &&
error.code === statusTooManyRequests
) {
console.error('Concurrent-viewer limit reached', error);
} else {
console.error('ERROR', error);
}
});
player.addEventListener(
PlayerEventType.QUALITY_CHANGED,
(quality: Quality) => {
console.log('QUALITY_CHANGED', quality);
}
);
// This event fires when text cues are encountered, such as captions or subtitles
player.addEventListener(PlayerEventType.TEXT_CUE, (cue: TextCue) => {
console.log('TEXT_CUE', cue.startTime, cue.text);
});
// This event fires when embedded Timed Metadata is encountered
player.addEventListener(
PlayerEventType.TEXT_METADATA_CUE,
(cue: TextMetadataCue) => {
console.log('Timed metadata', cue.text);
}
);
}
}
const demo = new PlayerDemo(getFormStream());
setupForm(function (stream: string) {
demo.loadAndPlay(stream);
});