@100mslive/hls-player 0.3.25-alpha.0 → 0.3.25-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +341 -10
  2. package/package.json +11 -3
package/README.md CHANGED
@@ -1,17 +1,348 @@
1
- `@100mslive/hls-player` is currently a wrapper on hls.js with easy to use interface and few add-ons for [100ms's interactive live streaming feature](https://www.100ms.live/docs/javascript/v2/how--to-guides/record-and-live-stream/hls/hls).
1
+ # 100ms HLS Player
2
2
 
3
- Sample usage:
3
+ [![Lint, Test and Build](https://github.com/100mslive/web-sdks/actions/workflows/lint-test-build.yml/badge.svg)](https://github.com/100mslive/web-sdks/actions/workflows/lint-test-build.yml)
4
+ [![Bundle Size](https://badgen.net/bundlephobia/minzip/@100mslive/hls-player)](https://bundlephobia.com/result?p=@100mslive/hls-player)
5
+ [![License](https://img.shields.io/npm/l/@100mslive/hls-player)](https://www.100ms.live/)
6
+ ![Tree shaking](https://badgen.net/bundlephobia/tree-shaking/@100mslive/hls-player)
4
7
 
8
+ The `HMSHLSPlayer` is an HLS player offered by 100ms that can be used to play HLS streams. The player takes a URL and video element to play the stream.
9
+
10
+ ## How to integrate HLS Player SDK
11
+
12
+ You can use Node package manager or yarn to add HMSHLSPlayer sdk to your project.
13
+ Use [@100mslive/hls-player](https://www.npmjs.com/package/@100mslive/hls-player) as the package source.
14
+
15
+ ```bash
16
+ npm i @100mslive/hls-player
17
+ ```
18
+
19
+ ## HMSHLSPlayer methods
20
+
21
+ Below shows all the methods exposed from player
22
+
23
+ ```javascript
24
+ interface IHMSHLSPlayer {
25
+ /**
26
+ * @returns get html video element
27
+ */
28
+ getVideoElement(): HTMLVideoElement;
29
+
30
+ /**
31
+ * set video volumne
32
+ * @param { volume } - in range [0,100]
33
+ */
34
+ setVolume(volume: number): void;
35
+ /**
36
+ *
37
+ * @returns returns HMSHLSLayer which represents current
38
+ * quality.
39
+ */
40
+ getLayer(): HMSHLSLayer | null;
41
+ /**
42
+ *
43
+ * @param { HMSHLSLayer } layer - layer we want to set the stream to.
44
+ * set { height: auto } to set the layer to auto
45
+ */
46
+ setLayer(layer: HMSHLSLayer): void;
47
+ /**
48
+ * move the video to Live
49
+ */
50
+ seekToLivePosition(): Promise<void>;
51
+ /**
52
+ * play stream
53
+ * call this when autoplay error is received
54
+ */
55
+ play(): Promise<void>;
56
+ /**
57
+ * pause stream
58
+ */
59
+ pause(): void;
60
+
61
+ /**
62
+ * It will update the video element current time
63
+ * @param seekValue Pass currentTime in second
64
+ */
65
+ seekTo(seekValue: number): void;
66
+ }
67
+ ```
68
+
69
+ ### How to use Player's HLS Stream
70
+
71
+ You create an instance of HMSHLSPlayer like below:
72
+
73
+ ```javascript
74
+ import { HMSHLSPlayer } from '@100mslive/hls-player';
75
+
76
+ // hls url should be provided which player will run.
77
+ // second parameter is optional, if you had video element then pass to player else we will create one.
78
+ const hlsPlayer = new HMSHLSPlayer(hlsURL, videoEl)
79
+
80
+ // if video element is not present, we will create a new video element which can be attached to your ui.
81
+ const videoEl = hlsPlayer.getVideoElement();
82
+ ```
83
+
84
+ ### How to pause and resume the playback
85
+
86
+ You call play/pause on the hlsPlayer instance like below:
87
+
88
+ ```javascript
89
+ // return Promise<void>
90
+ hmsPlayer.play()
91
+
92
+ hmsPlayer.pause()
93
+ ```
94
+
95
+ ### How to seek forward or backward
96
+
97
+ You use `seekTo` methods on the hlsPlayer to seek to any position in video, below is given example:
98
+
99
+ ```javascript
100
+ // seekValue Pass currentTime in second
101
+ hmsPlayer.seekTo(5)
102
+ ```
103
+
104
+ ### How to seek to live position
105
+
106
+ You use `seekToLivePosition` methods on the hlsPlayer instance to go to the live poition like below:
107
+
108
+ ```javascript
109
+ hmsPlayer.seekToLivePosition()
110
+ ```
111
+
112
+ ### How to change volume of HLS playback
113
+
114
+ Use volume property to change the volume of HLS player. The volume level is between 0-100. Volume is set to 100 by default.
115
+
116
+ ```javascript
117
+ hlsPlayer.setVolume(50);
5
118
  ```
6
- import {
7
- HLSPlaybackState,
8
- } from "@100mslive/hls-player";
9
119
 
10
- // hlsUrl is the url in which the hls stream is ongoing
11
- // videoElement is the video element where you want to play the stream
12
- const player = new HMSHLSPlayer(hlsUrl, videoElement);
13
- player.play()
120
+ ### Set video quality level to hls player
14
121
 
122
+ ```javascript
123
+ /**
124
+ *
125
+ * @returns returns HMSHLSLayer which represents current
126
+ * quality.
127
+ */
128
+ hlsPlayer.getLayer(): HMSHLSLayer | null;
129
+ /**
130
+ *
131
+ * @param { HMSHLSLayer } layer - layer we want to set the stream to.
132
+ * set { height: auto } to set the layer to auto
133
+ */
134
+ hlsPlayer.setLayer(layer: HMSHLSLayer): void;
135
+
136
+ // quality interface
137
+ interface HMSHLSLayer {
138
+ readonly bitrate: number;
139
+ readonly height?: number;
140
+ readonly id?: number;
141
+ readonly width?: number;
142
+ url: string;
143
+ resolution?: string;
144
+ }
15
145
  ```
16
146
 
17
- More details to be added soon.
147
+ ## Events exposed from HMSHLSPlayer
148
+
149
+ We are exposing events from our hls player.
150
+
151
+ ```javascript
152
+ enum HMSHLSPlayerEvents {
153
+ TIMED_METADATA_LOADED = 'timed-metadata',
154
+ SEEK_POS_BEHIND_LIVE_EDGE = 'seek-pos-behind-live-edge',
155
+
156
+ CURRENT_TIME = 'current-time',
157
+ AUTOPLAY_BLOCKED = 'autoplay-blocked',
158
+
159
+ MANIFEST_LOADED = 'manifest-loaded',
160
+ LAYER_UPDATED = 'layer-updated',
161
+
162
+ ERROR = 'error',
163
+ PLAYBACK_STATE = 'playback-state',
164
+ STATS = 'stats',
165
+ }
166
+ ```
167
+
168
+ ### Playback state
169
+
170
+ ```javascript
171
+ enum HLSPlaybackState {
172
+ playing,
173
+ paused,
174
+ }
175
+ interface HMSHLSPlaybackState {
176
+ state: HLSPlaybackState;
177
+ }
178
+ hlsPlayer.on(HMSHLSPlayerEvents.PLAYBACK_STATE, (event: HMSHLSPlayerEvents, data: HMSHLSPlaybackState): void => {});
179
+ ```
180
+
181
+ ### HLS Stats
182
+
183
+ ```javascript
184
+ interface HlsPlayerStats {
185
+ /** Estimated bandwidth in bits/sec. Could be used to show connection speed. */
186
+ bandwidthEstimate?: number;
187
+ /** The bitrate of the current level that is playing. Given in bits/sec */
188
+ bitrate?: number;
189
+ /** the amount of video available in forward buffer. Given in ms */
190
+ bufferedDuration?: number;
191
+ /** how far is the current playback from live edge.*/
192
+ distanceFromLive?: number;
193
+ /** total Frames dropped since started watching the stream. */
194
+ droppedFrames?: number;
195
+ /** the m3u8 url of the current level that is being played */
196
+ url?: string;
197
+ /** the resolution of the level of the video that is being played */
198
+ videoSize?: {
199
+ height: number;
200
+ width: number;
201
+ };
202
+ }
203
+
204
+ hlsPlayer.on(HMSHLSPlayerEvents.STATS, (event: HMSHLSPlayerEvents, data: HlsPlayerStats): void => {});
205
+ ```
206
+
207
+ ### Manifest loaded data
208
+
209
+ Hls player will provide a manifest which will provide a data like different quality layer once url is loaded.
210
+
211
+ ```javascript
212
+ interface HMSHLSManifestLoaded {
213
+ layers: HMSHLSLayer[];
214
+ }
215
+ hlsPlayer.on(HMSHLSPlayerEvents.MANIFEST_LOADED, (event: HMSHLSPlayerEvents, data: HMSHLSManifestLoaded): void => {});
216
+ ```
217
+
218
+ ### Quality changed data
219
+
220
+ ```javascript
221
+ interface HMSHLSLayerUpdated {
222
+ layer: HMSHLSLayer;
223
+ }
224
+ hlsPlayer.on(HMSHLSPlayerEvents.LAYER_UPDATED, (event: HMSHLSPlayerEvents, data: HMSHLSLayerUpdated): void => {});
225
+ ```
226
+
227
+ ### Live Event
228
+
229
+ Player will let you know if player is plaaying video live or not
230
+
231
+ ```javascript
232
+ interface HMSHLSStreamLive {
233
+ isLive: boolean;
234
+ }
235
+ hlsPlayer.on(HMSHLSPlayerEvents.SEEK_POS_BEHIND_LIVE_EDGE, (event: HMSHLSPlayerEvents, data: HMSHLSStreamLive): void => {});
236
+ ```
237
+
238
+ ### HLS timed-metadata
239
+
240
+ HLS player will parse and send the timed-metadata.
241
+
242
+ ```javascript
243
+ interface HMSHLSCue {
244
+ id?: string;
245
+ payload: string;
246
+ duration: number;
247
+ startDate: Date;
248
+ endDate?: Date;
249
+ }
250
+ hlsPlayer.on(HMSHLSPlayerEvents.TIMED_METADATA_LOADED, (event: HMSHLSPlayerEvents, data: HMSHLSCue): void => {});
251
+ ```
252
+
253
+ ### Error handling
254
+
255
+ ```javascript
256
+ interface HMSHLSException {
257
+ name: string,
258
+ message: string,
259
+ description: string,
260
+ isTerminal: boolean, // decide if player error will automatically restart(if false)
261
+ }
262
+ hlsPlayer.on(HMSHLSPlayerEvents.ERROR, (event: HMSHLSPlayerEvents, data: HMSHLSException): void => {});
263
+ hlsPlayer.on(HMSHLSPlayerEvents.AUTOPLAY_BLOCKED, (event: HMSHLSPlayerEvents, data: HMSHLSException): void => {});
264
+ ```
265
+
266
+ ### Video current time
267
+
268
+ ```javascript
269
+ hlsPlayer.on(HMSHLSPlayerEvents.CURRENT_TIME, (event: HMSHLSPlayerEvents, data: number): void => {});
270
+ ```
271
+
272
+ ### Example for events usage
273
+
274
+ Below are the simple example of how to use hls player's event
275
+
276
+ ```javascript
277
+ const isPlaying = false;
278
+ const playbackEventHandler = data => isPlaying = data.state === HLSPlaybackState.paused;
279
+ hlsPlayer.on(HMSHLSPlayerEvents.PLAYBACK_STATE, playbackEventHandler);
280
+ ```
281
+
282
+ ## HLS Player example
283
+
284
+ Below is a simple example in which hls-player will be used in your app.
285
+
286
+ ```javascript
287
+ // Vanilla JavaScript Example
288
+ import { HLSPlaybackState, HMSHLSPlayer, HMSHLSPlayerEvents } from "@100mslive/hls-player";
289
+
290
+ const videoEl; // reference for video element
291
+ const hlsUrl; // reference to hls url
292
+
293
+ // variable to handle ui and take some actions
294
+ let isLive = true, isPaused = false, isAutoBlockedPaused = false;
295
+
296
+ const handleError = data => console.error("[HLSView] error in hls", data);
297
+ const handleNoLongerLive = ({ isLive }) => isLive = isLive;
298
+
299
+ const playbackEventHandler = data => isPaused = (data.state === HLSPlaybackState.paused);
300
+
301
+ const handleAutoplayBlock = data => isAutoBlockedPaused = !!data;
302
+
303
+ const hlsPlayer = new HMSHLSPlayer(hlsUrl, videoEl);
304
+
305
+ hlsPlayer.on(HMSHLSPlayerEvents.SEEK_POS_BEHIND_LIVE_EDGE, handleNoLongerLive);
306
+ hlsPlayer.on(HMSHLSPlayerEvents.ERROR, handleError);
307
+ hlsPlayer.on(HMSHLSPlayerEvents.PLAYBACK_STATE, playbackEventHandler);
308
+ hlsPlayer.on(HMSHLSPlayerEvents.AUTOPLAY_BLOCKED, handleAutoplayBlock);
309
+ ```
310
+
311
+ ```jsx
312
+ // React Example
313
+ import { HLSPlaybackState, HMSHLSPlayer, HMSHLSPlayerEvents } from "@100mslive/hls-player";
314
+ import { useEffect, useState } from 'react';
315
+
316
+ const videoEl; // reference for video element
317
+ const hlsUrl; // reference to hls url
318
+
319
+ // variable to handle ui and take some actions
320
+ const [isVideoLive, setIsVideoLive] = useState(true);
321
+ const [isHlsAutoplayBlocked, setIsHlsAutoplayBlocked] = useState(false);
322
+ const [isPaused, setIsPaused] = useState(false);
323
+
324
+ useEffect(() => {
325
+ const handleError = data => console.error("[HLSView] error in hls", data);
326
+ const handleNoLongerLive = ({ isLive }) => {
327
+ setIsVideoLive(isLive);
328
+ };
329
+
330
+ const playbackEventHandler = data =>
331
+ setIsPaused(data.state === HLSPlaybackState.paused);
332
+
333
+ const handleAutoplayBlock = data => setIsHlsAutoplayBlocked(!!data);
334
+ const hlsPlayer = new HMSHLSPlayer(hlsUrl, videoEl);
335
+
336
+ hlsPlayer.on(HMSHLSPlayerEvents.SEEK_POS_BEHIND_LIVE_EDGE, handleNoLongerLive);
337
+ hlsPlayer.on(HMSHLSPlayerEvents.ERROR, handleError);
338
+ hlsPlayer.on(HMSHLSPlayerEvents.PLAYBACK_STATE, playbackEventHandler);
339
+ hlsPlayer.on(HMSHLSPlayerEvents.AUTOPLAY_BLOCKED, handleAutoplayBlock);
340
+ return () => {
341
+ hlsPlayer.off(HMSHLSPlayerEvents.SEEK_POS_BEHIND_LIVE_EDGE, handleNoLongerLive);
342
+ hlsPlayer.off(HMSHLSPlayerEvents.ERROR, handleError);
343
+ hlsPlayer.off(HMSHLSPlayerEvents.PLAYBACK_STATE, playbackEventHandler);
344
+ hlsPlayer.off(HMSHLSPlayerEvents.AUTOPLAY_BLOCKED, handleAutoplayBlock);
345
+ }
346
+ }, []);
347
+
348
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@100mslive/hls-player",
3
- "version": "0.3.25-alpha.0",
3
+ "version": "0.3.25-alpha.2",
4
4
  "description": "HLS client library which uses HTML5 Video element and Media Source Extension for playback",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.js",
@@ -36,9 +36,17 @@
36
36
  "author": "100ms",
37
37
  "license": "MIT",
38
38
  "dependencies": {
39
- "@100mslive/hls-stats": "0.4.25-alpha.0",
39
+ "@100mslive/hls-stats": "0.4.25-alpha.2",
40
40
  "eventemitter2": "^6.4.9",
41
41
  "hls.js": "1.4.12"
42
42
  },
43
- "gitHead": "b9a15a40498feb37cf34dc6fab790cb9dcfcd775"
43
+ "keywords": [
44
+ "hls",
45
+ "video",
46
+ "player",
47
+ "webrtc",
48
+ "conferencing",
49
+ "100ms"
50
+ ],
51
+ "gitHead": "d4e908bd663f765d36f4b2203584613ab72b2e7f"
44
52
  }