@clappr/hlsjs-playback 1.2.0 → 1.4.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clappr/hlsjs-playback",
3
- "version": "1.2.0",
3
+ "version": "1.4.0",
4
4
  "description": "HLS Playback based on hls.js",
5
5
  "main": "./dist/hlsjs-playback.js",
6
6
  "module": "./dist/hlsjs-playback.esm.js",
@@ -36,13 +36,13 @@
36
36
  },
37
37
  "homepage": "https://github.com/clappr/hlsjs-playback",
38
38
  "peerDependencies": {
39
- "@clappr/core": "^0.4.27",
39
+ "@clappr/core": "*",
40
40
  "hls.js": "^1.2.4"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@babel/core": "^7.14.2",
44
44
  "@babel/preset-env": "^7.14.2",
45
- "@clappr/core": "^0.4.27",
45
+ "@clappr/core": "^0.8.0",
46
46
  "@rollup/plugin-babel": "^5.3.0",
47
47
  "@rollup/plugin-commonjs": "^19.0.0",
48
48
  "@rollup/plugin-node-resolve": "^13.0.0",
@@ -65,5 +65,14 @@
65
65
  "commitizen": {
66
66
  "path": "./node_modules/cz-conventional-changelog"
67
67
  }
68
+ },
69
+ "nx": {
70
+ "targets": {
71
+ "test": {
72
+ "dependsOn": [
73
+ "^release"
74
+ ]
75
+ }
76
+ }
68
77
  }
69
78
  }
package/src/hls.js CHANGED
@@ -127,6 +127,15 @@ export default class HlsjsPlayback extends HTML5Video {
127
127
  return this.options.src
128
128
  }
129
129
 
130
+ get currentTimestamp() {
131
+ if (!this._currentFragment) return null
132
+ const startTime = this._currentFragment.programDateTime
133
+ const playbackTime = this.el.currentTime
134
+ const playTimeOffSet = playbackTime - this._currentFragment.start
135
+ const currentTimestampInMs = startTime + playTimeOffSet * 1000
136
+ return currentTimestampInMs / 1000
137
+ }
138
+
130
139
  static get HLSJS() {
131
140
  return HLSJS
132
141
  }
@@ -187,7 +196,7 @@ export default class HlsjsPlayback extends HTML5Video {
187
196
 
188
197
  _destroyHLSInstance() {
189
198
  if (!this._hls) return
190
- this._manifestParsed = false
199
+ this._manifestLoading = false
191
200
  this._ccIsSetup = false
192
201
  this._ccTracksUpdated = false
193
202
  this._setInitialState()
@@ -208,7 +217,7 @@ export default class HlsjsPlayback extends HTML5Video {
208
217
  _listenHLSEvents() {
209
218
  if (!this._hls) return
210
219
  this._hls.once(HLSJS.Events.MEDIA_ATTACHED, () => { this.options.hlsPlayback.preload && this._hls.loadSource(this.options.src) })
211
- this._hls.on(HLSJS.Events.MANIFEST_PARSED, () => this._manifestParsed = true)
220
+ this._hls.on(HLSJS.Events.MANIFEST_LOADING, () => this._manifestLoading = true)
212
221
  this._hls.on(HLSJS.Events.LEVEL_LOADED, (evt, data) => this._updatePlaybackType(evt, data))
213
222
  this._hls.on(HLSJS.Events.LEVEL_UPDATED, (evt, data) => this._onLevelUpdated(evt, data))
214
223
  this._hls.on(HLSJS.Events.LEVEL_SWITCHING, (evt,data) => this._onLevelSwitch(evt, data))
@@ -477,7 +486,7 @@ export default class HlsjsPlayback extends HTML5Video {
477
486
 
478
487
  play() {
479
488
  !this._hls && this._setup()
480
- !this._manifestParsed && !this.options.hlsPlayback.preload && this._hls.loadSource(this.options.src)
489
+ !this._manifestLoading && !this.options.hlsPlayback.preload && this._hls.loadSource(this.options.src)
481
490
  super.play()
482
491
  this._startTimeUpdateTimer()
483
492
  }
@@ -635,6 +644,7 @@ export default class HlsjsPlayback extends HTML5Video {
635
644
  }
636
645
 
637
646
  _onFragmentChanged(evt, data) {
647
+ this._currentFragment = data.frag
638
648
  this.trigger(Events.Custom.PLAYBACK_FRAGMENT_CHANGED, data)
639
649
  }
640
650
 
package/src/hls.test.js CHANGED
@@ -151,13 +151,13 @@ describe('HlsjsPlayback', () => {
151
151
  })
152
152
 
153
153
  describe('_setup method', () => {
154
- test('sets _manifestParsed flag to false', () => {
154
+ test('sets _manifestLoading flag to false', () => {
155
155
  const playback = new HlsjsPlayback({ src: 'http://clappr.io/foo.m3u8' })
156
- expect(playback._manifestParsed).toBeUndefined()
156
+ expect(playback._manifestLoading).toBeUndefined()
157
157
 
158
158
  playback._setup()
159
159
 
160
- expect(playback._manifestParsed).toBeFalsy()
160
+ expect(playback._manifestLoading).toBeFalsy()
161
161
  })
162
162
 
163
163
  test('calls this._hls.loadSource when MEDIA_ATTACHED event is triggered and hlsPlayback.preload is true', () => {
@@ -176,15 +176,16 @@ describe('HlsjsPlayback', () => {
176
176
  expect(playback._hls.loadSource).toHaveBeenCalledTimes(1)
177
177
  })
178
178
 
179
- test('updates _manifestParsed flag value to true if MANIFEST_PARSED event is triggered', () => {
180
- const playback = new HlsjsPlayback({ src: 'http://clappr.io/foo.m3u8' })
179
+ test('updates _manifestLoading flag value to true if MANIFEST_LOADING event is triggered', () => {
180
+ const src = 'http://clappr.io/foo.m3u8'
181
+ const playback = new HlsjsPlayback({ src })
181
182
 
182
- expect(playback._manifestParsed).toBeUndefined()
183
+ expect(playback._manifestLoading).toBeUndefined()
183
184
 
184
185
  playback._setup()
185
- playback._hls.trigger(HLSJS.Events.MANIFEST_PARSED, { levels: [] })
186
+ playback._hls.trigger(HLSJS.Events.MANIFEST_LOADING, { url: src })
186
187
 
187
- expect(playback._manifestParsed).toBeTruthy()
188
+ expect(playback._manifestLoading).toBeTruthy()
188
189
  })
189
190
 
190
191
  test('calls bindCustomListeners method', () => {
@@ -241,24 +242,29 @@ describe('HlsjsPlayback', () => {
241
242
  })
242
243
 
243
244
  describe('play method', () => {
244
- test('calls this._hls.loadSource if _manifestParsed flag and options.hlsPlayback.preload are falsy', () => {
245
- const playback = new HlsjsPlayback({ src: 'http://clappr.io/foo.m3u8', hlsPlayback: { preload: true } })
245
+ test('calls this._hls.loadSource once if _manifestLoading flag and options.hlsPlayback.preload are falsy', () => {
246
+ jest.spyOn(HLSJS.prototype, 'loadSource').mockImplementation()
247
+ const src = 'http://clappr.io/foo.m3u8'
248
+ const playback = new HlsjsPlayback({ src, hlsPlayback: { preload: false } })
246
249
  playback._setup()
247
- jest.spyOn(playback._hls, 'loadSource')
248
- playback.play()
250
+ playback._hls.trigger(HLSJS.Events.MEDIA_ATTACHED, { media: playback.el })
249
251
 
250
252
  expect(playback._hls.loadSource).not.toHaveBeenCalled()
251
253
 
252
- playback.options.hlsPlayback.preload = false
253
- playback._manifestParsed = true
254
254
  playback.play()
255
+ playback._hls.trigger(HLSJS.Events.MANIFEST_LOADING, { url: src })
255
256
 
256
- expect(playback._hls.loadSource).not.toHaveBeenCalled()
257
+ expect(playback._hls.loadSource).toHaveBeenCalledTimes(1)
257
258
 
258
- playback._manifestParsed = false
259
+ // do not call loadSource again while loading a manifest
259
260
  playback.play()
260
-
261
261
  expect(playback._hls.loadSource).toHaveBeenCalledTimes(1)
262
+
263
+ playback._manifestLoading = false
264
+ playback.play()
265
+
266
+ expect(playback._hls.loadSource).toHaveBeenCalledTimes(2)
267
+ HLSJS.prototype.loadSource.mockRestore()
262
268
  })
263
269
  })
264
270
 
@@ -346,4 +352,27 @@ describe('HlsjsPlayback', () => {
346
352
  expect(cb).not.toHaveBeenCalled()
347
353
  })
348
354
  })
355
+
356
+ describe('currentTimestamp', () => {
357
+ it('returns the fragment time plus the current playback time', () => {
358
+ const fragmentMock = {
359
+ frag: {
360
+ programDateTime: 1556663040000, // 'Tue Apr 30 2019 19:24:00'
361
+ start: 0,
362
+ }
363
+ }
364
+ const playback = new HlsjsPlayback({ src: 'http://clappr.io/foo.m3u8' })
365
+ playback.el.currentTime = 5
366
+ playback._setup()
367
+ playback.unbindCustomListeners()
368
+ playback._hls.trigger(HLSJS.Events.FRAG_CHANGED, fragmentMock)
369
+ expect(playback.currentTimestamp).toBe(1556663045) // 'Tue Apr 30 2019 19:24:05'
370
+ })
371
+
372
+ it('returns null if the playback does not have a fragment', () => {
373
+ const playback = new HlsjsPlayback({ src: 'http://clappr.io/foo.m3u8' })
374
+ playback._setup()
375
+ expect(playback.currentTimestamp).toBe(null)
376
+ })
377
+ })
349
378
  })