@gcorevideo/player 0.0.1 → 0.0.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.
package/dist/index.js CHANGED
@@ -1,467 +1,4 @@
1
- import { $, Log, Loader, Player as Player$1, Browser } from '@clappr/core';
2
- import assert from 'assert';
3
- import HLSJS from 'hls.js';
4
- import EventLite from 'event-lite';
5
-
6
- const tracer = {
7
- trace: () => { },
8
- reportError: () => { },
9
- };
10
- /**
11
- * @public
12
- * Sets a tracer implementation, e.g., LogTracer or SentryTracer
13
- */
14
- function setTracer(t) {
15
- tracer.trace = t.trace.bind(t);
16
- tracer.reportError = t.reportError.bind(t);
17
- }
18
- /**
19
- * @public
20
- * @param e
21
- */
22
- function reportError(e) {
23
- tracer.reportError(e);
24
- }
25
- /**
26
- * @public
27
- * @param msg
28
- * @param data
29
- */
30
- function trace(msg, data = {}) {
31
- tracer.trace(msg, data);
32
- }
33
-
34
- var PlayerEvent;
35
- (function (PlayerEvent) {
36
- PlayerEvent["Ready"] = "ready";
37
- PlayerEvent["Play"] = "play";
38
- PlayerEvent["Pause"] = "pause";
39
- PlayerEvent["Stop"] = "stop";
40
- PlayerEvent["Ended"] = "ended";
41
- })(PlayerEvent || (PlayerEvent = {}));
42
- const T = "Player";
43
- const DEFAULT_OPTIONS = {
44
- autoPlay: false,
45
- mute: false,
46
- loop: false,
47
- };
48
- /**
49
- * @beta
50
- */
51
- class Player {
52
- emitter = new EventLite();
53
- player = null;
54
- pluginLoaders = [];
55
- clapprReady = false;
56
- timer = null;
57
- tuneInEntered = false;
58
- supportedMediaTransports = [];
59
- config;
60
- get playing() {
61
- return this.player ? this.player.isPlaying() : false;
62
- }
63
- get ready() {
64
- return this.clapprReady;
65
- }
66
- constructor(config) {
67
- this.config = $.extend(true, {}, DEFAULT_OPTIONS, config);
68
- }
69
- on(event, handler) {
70
- this.emitter.on(event, handler);
71
- }
72
- off(event, handler) {
73
- this.emitter.off(event, handler);
74
- }
75
- async init(playerElement) {
76
- assert.ok(!this.player, 'Player already initialized');
77
- assert.ok(playerElement, 'Player element is required');
78
- if (this.config.debug === 'all' ||
79
- this.config.debug === 'clappr') {
80
- Log.setLevel(0);
81
- }
82
- Log.debug('Config', this.config);
83
- this.configurePlugins();
84
- return this.loadPlugins().then(async () => {
85
- const coreOpts = this.buildCoreOptions(playerElement);
86
- const { core, container, } = Loader.registeredPlugins;
87
- coreOpts.plugins = {
88
- core: Object.values(core),
89
- container: Object.values(container),
90
- playback: Loader.registeredPlaybacks,
91
- };
92
- console.log('plugins', coreOpts.plugins);
93
- return this.initPlayer(coreOpts);
94
- });
95
- }
96
- destroy() {
97
- trace(`${T} destroy`, { player: !!this.player });
98
- if (this.player) {
99
- this.player.destroy();
100
- this.player = null;
101
- }
102
- }
103
- pause() {
104
- assert.ok(this.player, 'Player not initialized');
105
- this.player.pause();
106
- }
107
- play() {
108
- assert.ok(this.player, 'Player not initialized');
109
- this.player.play();
110
- }
111
- seekTo(time) {
112
- assert.ok(this.player, 'Player not initialized');
113
- this.player.seek(time);
114
- }
115
- stop() {
116
- assert.ok(this.player, 'Player not initialized');
117
- this.player.stop();
118
- }
119
- static registerPlugin(plugin) {
120
- Loader.registerPlugin(plugin);
121
- }
122
- loadPlugins() {
123
- return Promise.all(this.pluginLoaders.map((loader) => loader())).then(() => { });
124
- }
125
- initPlayer(coreOptions) {
126
- assert.ok(!this.player, 'Player already initialized');
127
- const player = new Player$1(coreOptions);
128
- this.player = player;
129
- this.timer = globalThis.setTimeout(() => {
130
- try {
131
- if (!this.clapprReady &&
132
- player.core &&
133
- player.core
134
- .activePlayback) {
135
- this.tuneIn();
136
- // player.onReady = null; // TODO ?
137
- }
138
- }
139
- catch (e) {
140
- reportError(e);
141
- }
142
- }, 4000);
143
- }
144
- // TODO sort this out
145
- async tuneIn() {
146
- assert.ok(this.player);
147
- trace(`${T} tuneIn enter`, {
148
- ready: this.clapprReady,
149
- tuneInEntered: this.tuneInEntered,
150
- });
151
- if (this.tuneInEntered) {
152
- return;
153
- }
154
- this.tuneInEntered = true;
155
- const player = this.player;
156
- if (Browser.isiOS &&
157
- player.core.activePlayback) {
158
- player.core.activePlayback.$el.on('webkitendfullscreen', () => {
159
- try {
160
- player.core.handleFullscreenChange();
161
- }
162
- catch (e) {
163
- reportError(e);
164
- }
165
- });
166
- }
167
- }
168
- configurePlugins() {
169
- if (!Browser.isiOS && this.config.multisources.some((el) => el.sourceDash)) {
170
- this.scheduleLoad(async () => {
171
- const module = await import('./DashPlayback-BeZz7mN9.js');
172
- Loader.registerPlayback(module.default);
173
- });
174
- }
175
- // TODO remove !isiOS?
176
- // if (!Browser.isiOS && this.config.multisources.some((el) => el.hls_mpegts_url)) {
177
- if (this.config.multisources.some((el) => el.hlsMpegtsUrl || el.hlsCmafUrl || el.source.endsWith('.m3u8'))) {
178
- this.scheduleLoad(async () => {
179
- const module = await import('./HlsPlayback-Avwy8-0O.js');
180
- Loader.registerPlayback(module.default);
181
- });
182
- }
183
- }
184
- events = {
185
- onReady: () => {
186
- if (this.clapprReady) {
187
- return;
188
- }
189
- this.clapprReady = true;
190
- if (this.timer) {
191
- clearTimeout(this.timer);
192
- this.timer = null;
193
- }
194
- trace(`${T} onReady`);
195
- setTimeout(() => this.tuneIn(), 0);
196
- try {
197
- this.emitter.emit(PlayerEvent.Ready);
198
- }
199
- catch (e) {
200
- reportError(e);
201
- }
202
- },
203
- onPlay: () => {
204
- try {
205
- this.emitter.emit(PlayerEvent.Play);
206
- }
207
- catch (e) {
208
- reportError(e);
209
- }
210
- },
211
- onPause: () => {
212
- try {
213
- this.emitter.emit(PlayerEvent.Pause);
214
- }
215
- catch (e) {
216
- reportError(e);
217
- }
218
- },
219
- onEnded: () => {
220
- try {
221
- this.emitter.emit(PlayerEvent.Ended);
222
- }
223
- catch (e) {
224
- reportError(e);
225
- }
226
- },
227
- onStop: () => {
228
- try {
229
- this.emitter.emit(PlayerEvent.Stop);
230
- }
231
- catch (e) {
232
- reportError(e);
233
- }
234
- },
235
- };
236
- buildCoreOptions(playerElement) {
237
- this.checkMediaTransportsSupport();
238
- const multisources = this.processMultisources(this.config.priorityTransport);
239
- const mediaSources = multisources.map(ms => ms.source);
240
- const mainSource = this.findMainSource();
241
- const mainSourceUrl = unwrapSource(mainSource ? this.selectMediaTransport(mainSource, this.config.priorityTransport) : undefined);
242
- const poster = mainSource?.poster ?? this.config.poster;
243
- const coreOptions = {
244
- autoPlay: this.config.autoPlay,
245
- debug: this.config.debug || 'none',
246
- events: this.events,
247
- multisources,
248
- mute: this.config.mute,
249
- ...this.config.pluginSettings,
250
- playback: {
251
- controls: false,
252
- preload: Browser.isiOS ? 'metadata' : 'none',
253
- playInline: true,
254
- crossOrigin: 'anonymous', // TODO
255
- hlsjsConfig: {
256
- debug: this.config.debug === 'all' || this.config.debug === 'hls',
257
- },
258
- },
259
- parent: playerElement,
260
- playbackType: this.config.playbackType,
261
- poster,
262
- width: playerElement.clientWidth,
263
- height: playerElement.clientHeight,
264
- loop: this.config.loop,
265
- strings: this.config.strings,
266
- source: mainSourceUrl,
267
- sources: mediaSources,
268
- };
269
- trace(`${T} buildCoreOptions`, coreOptions);
270
- return coreOptions;
271
- }
272
- findMainSource() {
273
- return this.config.multisources.find(ms => ms.live !== false);
274
- }
275
- scheduleLoad(cb) {
276
- this.pluginLoaders.push(cb);
277
- }
278
- selectMediaTransport(ms, priorityTransport = ms.priorityTransport) {
279
- const cmafUrl = ms.hlsCmafUrl || ms.source; // source is default url for hls
280
- const mpegtsUrl = ms.hlsMpegtsUrl; // no-low-latency HLS
281
- const dashUrl = ms.sourceDash;
282
- const masterSource = ms.source;
283
- const mts = this.getAvailableTransportsPreference(priorityTransport);
284
- for (const mt of mts) {
285
- switch (mt) {
286
- case 'dash':
287
- if (dashUrl) {
288
- return dashUrl;
289
- }
290
- break;
291
- case 'hls':
292
- if (cmafUrl) {
293
- return cmafUrl;
294
- }
295
- break;
296
- default:
297
- return mpegtsUrl || masterSource;
298
- }
299
- }
300
- // no supported transport found
301
- return '';
302
- }
303
- getAvailableTransportsPreference(priorityTransport) {
304
- const mtp = [];
305
- if (priorityTransport !== 'auto' && this.supportedMediaTransports.includes(priorityTransport)) {
306
- mtp.push(priorityTransport);
307
- }
308
- for (const mt of this.supportedMediaTransports) {
309
- if (mt !== priorityTransport) {
310
- mtp.push(mt);
311
- }
312
- }
313
- return mtp;
314
- }
315
- checkMediaTransportsSupport() {
316
- const isDashSupported = typeof (globalThis.MediaSource || globalThis.WebKitMediaSource) === 'function';
317
- if (isDashSupported) {
318
- this.supportedMediaTransports.push('dash');
319
- }
320
- if (HLSJS.isSupported()) {
321
- this.supportedMediaTransports.push('hls');
322
- }
323
- this.supportedMediaTransports.push('mpegts');
324
- }
325
- processMultisources(transport) {
326
- return this.config.multisources.map((ms) => ({
327
- ...ms,
328
- source: this.selectMediaTransport(ms, transport),
329
- })).filter((el) => !!el.source);
330
- }
331
- }
332
- function unwrapSource(s) {
333
- if (!s) {
334
- return;
335
- }
336
- return typeof s === "string" ? s : s.source;
337
- }
338
-
339
- function fromStreamMediaSourceDto(s) {
340
- return ({
341
- ...s,
342
- hlsCmafUrl: s.hls_cmaf_url,
343
- hlsMpegtsUrl: s.hls_mpegts_url,
344
- priorityTransport: s.priority_transport,
345
- sourceDash: s.source_dash,
346
- vtt: s.vtt,
347
- });
348
- }
349
-
350
- const APP_NAME = "gplayer";
351
- class DebuggerWrapper {
352
- writer;
353
- namespace;
354
- currentWriter;
355
- constructor(writer, namespace, enabled = true) {
356
- this.writer = writer;
357
- this.namespace = namespace;
358
- this.currentWriter = enabled ? writer : nullWriter;
359
- }
360
- enable() {
361
- this.currentWriter = this.writer;
362
- }
363
- disable() {
364
- this.currentWriter = nullWriter;
365
- }
366
- write = (m, ...args) => {
367
- const tokens = args.map((_) => "%s");
368
- if (typeof m === "string" || args.length > 0) {
369
- tokens.unshift("%s");
370
- }
371
- this.currentWriter(`${this.namespace}: ${tokens.join(' ')}`, m, ...args.map(a => JSON.stringify(a)));
372
- };
373
- }
374
- const currentPatterns = [];
375
- function parsePattern(pattern) {
376
- if (pattern === "*") {
377
- return /.?/;
378
- }
379
- return new RegExp("^" + pattern.replace(/\*/g, "[@\\w-]+"), "i");
380
- }
381
- function pass(namespace) {
382
- return currentPatterns.some((p) => p.test(namespace));
383
- }
384
- function nullWriter() { }
385
- /**
386
- * Logging utility with [debug](https://www.npmjs.com/package/debug)-like API
387
- * @beta
388
- */
389
- class Logger {
390
- info;
391
- warn;
392
- error;
393
- debug;
394
- static items = [];
395
- constructor(namespace, appName = APP_NAME) {
396
- const ns = namespace ? `:${namespace}` : "";
397
- const info = new DebuggerWrapper(console.info.bind(console), `${appName}:INFO${ns}`, pass(namespace));
398
- this.info = info.write;
399
- const warn = new DebuggerWrapper(console.warn.bind(console), `${appName}:WARN${ns}`, pass(namespace));
400
- this.warn = warn.write;
401
- const error = new DebuggerWrapper(console.error.bind(console), `${appName}:ERROR${ns}`, pass(namespace));
402
- this.error = error.write;
403
- const debug = new DebuggerWrapper(console.debug.bind(console), `${appName}:DEBUG${ns}`, pass(namespace));
404
- this.debug = debug.write;
405
- Logger.items.push(warn);
406
- Logger.items.push(info);
407
- Logger.items.push(error);
408
- Logger.items.push(debug);
409
- }
410
- /**
411
- * @param patterns - comma-separated list of patterns, can contain '*' as a wildcard
412
- */
413
- static enable(patterns) {
414
- currentPatterns.splice(0, currentPatterns.length, ...patterns.split(",").filter(Boolean).map(parsePattern));
415
- Logger.toggleItems();
416
- }
417
- static disable() {
418
- currentPatterns.splice(0, currentPatterns.length);
419
- }
420
- static toggleItems() {
421
- for (const w of Logger.items) {
422
- if (pass(w.namespace)) {
423
- w.enable();
424
- }
425
- else {
426
- w.disable();
427
- }
428
- }
429
- }
430
- }
431
-
432
- /**
433
- * A tracer that logs to the console
434
- * @public
435
- */
436
- class LogTracer {
437
- logger;
438
- constructor(ns = "") {
439
- this.logger = new Logger(ns);
440
- }
441
- reportError(e) {
442
- this.logger.error(e);
443
- }
444
- trace(msg, data) {
445
- this.logger.debug(msg, data);
446
- }
447
- }
448
-
449
- /**
450
- * @beta
451
- */
452
- class SentryTracer {
453
- client;
454
- constructor(client) {
455
- this.client = client;
456
- }
457
- reportError(e) {
458
- this.client.captureException(e);
459
- }
460
- trace(msg, data) {
461
- this.client.captureMessage(msg, "info", {
462
- data,
463
- });
464
- }
465
- }
466
-
467
- export { LogTracer, Logger, Player, PlayerEvent, SentryTracer, fromStreamMediaSourceDto, reportError, setTracer, trace };
1
+ export { L as LogTracer, c as Logger, b as Player, P as PlayerEvent, S as SentryTracer, f as fromStreamMediaSourceDto, r as reportError, s as setTracer, t as trace } from './index-DTOY9tbl.js';
2
+ import '@clappr/core';
3
+ import 'hls.js';
4
+ import 'event-lite';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gcorevideo/player",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "Gcore JavaScript video player",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -37,6 +37,7 @@
37
37
  "@types/ua-parser-js": "^0.7.39",
38
38
  "assert": "^2.1.0",
39
39
  "rollup": "^4.27.4",
40
+ "rollup-plugin-polyfill-node": "^0.13.0",
40
41
  "rollup-plugin-sass": "^1.14.0",
41
42
  "rollup-plugin-scss": "^4.0.1",
42
43
  "rollup-plugin-string": "^3.0.0",
package/rollup.config.js CHANGED
@@ -3,6 +3,7 @@ import resolve from '@rollup/plugin-node-resolve';
3
3
  import commonjs from '@rollup/plugin-commonjs';
4
4
  import sass from 'rollup-plugin-sass';
5
5
  import { string } from 'rollup-plugin-string';
6
+ import polyfillNode from 'rollup-plugin-polyfill-node';
6
7
 
7
8
  export default [
8
9
  {
@@ -22,6 +23,7 @@ export default [
22
23
  '**/*.svg',
23
24
  ],
24
25
  }),
26
+ polyfillNode(),
25
27
  ],
26
28
  output: [
27
29
  {