p2p-media-loader-hlsjs
Advanced tools
Comparing version
@@ -255,2 +255,3 @@ var __defProp = Object.defineProperty; | ||
} | ||
const MAX_LIVE_SYNC_DURATION = 120; | ||
class HlsJsP2PEngine { | ||
@@ -289,7 +290,4 @@ /** | ||
__publicField(this, "handleLevelUpdated", (event, data) => { | ||
if (this.currentHlsInstance && this.currentHlsInstance.config.liveSyncDurationCount !== data.details.fragments.length - 1 && data.details.live && data.details.fragments[0].type === "main" && !this.currentHlsInstance.userConfig.liveSyncDuration && !this.currentHlsInstance.userConfig.liveSyncDurationCount && data.details.fragments.length > 4) { | ||
this.debug( | ||
`set liveSyncDurationCount ${data.details.fragments.length - 1}` | ||
); | ||
this.currentHlsInstance.config.liveSyncDurationCount = data.details.fragments.length - 1; | ||
if (this.currentHlsInstance && data.details.live && data.details.fragments[0].type === "main" && !this.currentHlsInstance.userConfig.liveSyncDuration && !this.currentHlsInstance.userConfig.liveSyncDurationCount && data.details.fragments.length > 4) { | ||
this.updateLiveSyncDurationCount(data); | ||
} | ||
@@ -471,2 +469,18 @@ this.core.setIsLive(data.details.live); | ||
} | ||
updateLiveSyncDurationCount(data) { | ||
const fragmentDuration = data.details.targetduration; | ||
const maxLiveSyncCount = Math.floor( | ||
MAX_LIVE_SYNC_DURATION / fragmentDuration | ||
); | ||
const newLiveSyncDurationCount = Math.min( | ||
data.details.fragments.length - 1, | ||
maxLiveSyncCount | ||
); | ||
if (this.currentHlsInstance && this.currentHlsInstance.config.liveSyncDurationCount !== newLiveSyncDurationCount) { | ||
this.debug( | ||
`Setting liveSyncDurationCount to ${newLiveSyncDurationCount}` | ||
); | ||
this.currentHlsInstance.config.liveSyncDurationCount = newLiveSyncDurationCount; | ||
} | ||
} | ||
createFragmentLoaderClass() { | ||
@@ -473,0 +487,0 @@ const { core } = this; |
@@ -6,4 +6,4 @@ var j = Object.defineProperty; | ||
var q = (n, t, e) => t in n ? j(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e; | ||
var r = (n, t, e) => q(n, typeof t != "symbol" ? t + "" : t, e), D = (n, t, e) => t.has(n) || R("Cannot " + e); | ||
var i = (n, t, e) => (D(n, t, "read from private field"), e ? e.call(n) : t.get(n)), l = (n, t, e) => t.has(n) ? R("Cannot add the same private member more than once") : t instanceof WeakSet ? t.add(n) : t.set(n, e), u = (n, t, e, s) => (D(n, t, "write to private field"), s ? s.call(n, e) : t.set(n, e), e), x = (n, t, e) => (D(n, t, "access private method"), e); | ||
var a = (n, t, e) => q(n, typeof t != "symbol" ? t + "" : t, e), k = (n, t, e) => t.has(n) || R("Cannot " + e); | ||
var i = (n, t, e) => (k(n, t, "read from private field"), e ? e.call(n) : t.get(n)), l = (n, t, e) => t.has(n) ? R("Cannot add the same private member more than once") : t instanceof WeakSet ? t.add(n) : t.set(n, e), u = (n, t, e, s) => (k(n, t, "write to private field"), s ? s.call(n, e) : t.set(n, e), e), x = (n, t, e) => (k(n, t, "access private method"), e); | ||
import { CoreRequestError as T, debug as N, Core as z } from "p2p-media-loader-core"; | ||
@@ -16,32 +16,32 @@ function F(n, t) { | ||
} | ||
var H, M, g, y, p, m, b, G, A; | ||
var H, M, g, m, p, v, L, $, A; | ||
class B { | ||
constructor(t, e) { | ||
l(this, b); | ||
r(this, "context"); | ||
r(this, "config"); | ||
r(this, "stats"); | ||
l(this, L); | ||
a(this, "context"); | ||
a(this, "config"); | ||
a(this, "stats"); | ||
l(this, H); | ||
l(this, M); | ||
l(this, g); | ||
l(this, y); | ||
l(this, m); | ||
l(this, p); | ||
l(this, m); | ||
u(this, y, e), u(this, M, () => new t.loader(t)), this.stats = { aborted: !1, chunkCount: 0, loading: { start: 0, first: 0, end: 0 }, buffering: { start: 0, first: 0, end: 0 }, parsing: { start: 0, end: 0 }, total: 1, loaded: 1, bwEstimate: 0, retry: 0 }; | ||
l(this, v); | ||
u(this, m, e), u(this, M, () => new t.loader(t)), this.stats = { aborted: !1, chunkCount: 0, loading: { start: 0, first: 0, end: 0 }, buffering: { start: 0, first: 0, end: 0 }, parsing: { start: 0, end: 0 }, total: 1, loaded: 1, bwEstimate: 0, retry: 0 }; | ||
} | ||
load(t, e, s) { | ||
this.context = t, this.config = e, u(this, H, s); | ||
const { stats: o } = this, { rangeStart: d, rangeEnd: a } = t, E = J(d, a !== void 0 ? a - 1 : void 0); | ||
u(this, m, F(t.url, E)); | ||
const h = i(this, y).isSegmentLoadable(i(this, m)); | ||
if (!i(this, y).hasSegment(i(this, m)) || !h) return u(this, g, i(this, M).call(this)), i(this, g).stats = this.stats, void i(this, g).load(t, e, s); | ||
i(this, y).loadSegment(i(this, m), { onSuccess: (f) => { | ||
const { stats: r } = this, { rangeStart: d, rangeEnd: o } = t, E = J(d, o !== void 0 ? o - 1 : void 0); | ||
u(this, v, F(t.url, E)); | ||
const h = i(this, m).isSegmentLoadable(i(this, v)); | ||
if (!i(this, m).hasSegment(i(this, v)) || !h) return u(this, g, i(this, M).call(this)), i(this, g).stats = this.stats, void i(this, g).load(t, e, s); | ||
i(this, m).loadSegment(i(this, v), { onSuccess: (f) => { | ||
u(this, p, f); | ||
const c = i(this, p).data.byteLength; | ||
o.loading = function(L, C, I) { | ||
const w = 8e3 * C / L, S = I - w; | ||
return { start: S - 10, first: S, end: I }; | ||
}(i(this, p).bandwidth, c, performance.now()), o.total = c, o.loaded = c, s.onProgress && s.onProgress(this.stats, t, i(this, p).data, void 0), s.onSuccess({ data: i(this, p).data, url: t.url }, this.stats, t, void 0); | ||
r.loading = function(b, C, S) { | ||
const D = 8e3 * C / b, I = S - D; | ||
return { start: I - 10, first: I, end: S }; | ||
}(i(this, p).bandwidth, c, performance.now()), r.total = c, r.loaded = c, s.onProgress && s.onProgress(this.stats, t, i(this, p).data, void 0), s.onSuccess({ data: i(this, p).data, url: t.url }, this.stats, t, void 0); | ||
}, onError: (f) => { | ||
f instanceof T && f.type === "aborted" && this.stats.aborted || x(this, b, G).call(this, f); | ||
f instanceof T && f.type === "aborted" && this.stats.aborted || x(this, L, $).call(this, f); | ||
} }); | ||
@@ -51,9 +51,9 @@ } | ||
var t, e; | ||
i(this, g) ? i(this, g).abort() : (x(this, b, A).call(this), (e = (t = i(this, H)) == null ? void 0 : t.onAbort) == null || e.call(t, this.stats, this.context, {})); | ||
i(this, g) ? i(this, g).abort() : (x(this, L, A).call(this), (e = (t = i(this, H)) == null ? void 0 : t.onAbort) == null || e.call(t, this.stats, this.context, {})); | ||
} | ||
destroy() { | ||
i(this, g) ? i(this, g).destroy() : (this.stats.aborted || x(this, b, A).call(this), u(this, H, null), this.config = null); | ||
i(this, g) ? i(this, g).destroy() : (this.stats.aborted || x(this, L, A).call(this), u(this, H, null), this.config = null); | ||
} | ||
} | ||
H = new WeakMap(), M = new WeakMap(), g = new WeakMap(), y = new WeakMap(), p = new WeakMap(), m = new WeakMap(), b = new WeakSet(), G = function(t) { | ||
H = new WeakMap(), M = new WeakMap(), g = new WeakMap(), m = new WeakMap(), p = new WeakMap(), v = new WeakMap(), L = new WeakSet(), $ = function(t) { | ||
var s; | ||
@@ -63,26 +63,26 @@ const e = { code: 0, text: "" }; | ||
}, A = function() { | ||
!i(this, p) && i(this, m) && (this.stats.aborted = !0, i(this, y).abortSegmentLoading(i(this, m))); | ||
!i(this, p) && i(this, v) && (this.stats.aborted = !0, i(this, m).abortSegmentLoading(i(this, v))); | ||
}; | ||
var v; | ||
var y; | ||
class X { | ||
constructor(t) { | ||
l(this, v); | ||
r(this, "context"); | ||
r(this, "stats"); | ||
u(this, v, new t.loader(t)), this.stats = i(this, v).stats, this.context = i(this, v).context; | ||
l(this, y); | ||
a(this, "context"); | ||
a(this, "stats"); | ||
u(this, y, new t.loader(t)), this.stats = i(this, y).stats, this.context = i(this, y).context; | ||
} | ||
load(t, e, s) { | ||
i(this, v).load(t, e, s); | ||
i(this, y).load(t, e, s); | ||
} | ||
abort() { | ||
i(this, v).abort(); | ||
i(this, y).abort(); | ||
} | ||
destroy() { | ||
i(this, v).destroy(); | ||
i(this, y).destroy(); | ||
} | ||
} | ||
v = new WeakMap(); | ||
y = new WeakMap(); | ||
class K { | ||
constructor(t) { | ||
r(this, "core"); | ||
a(this, "core"); | ||
this.core = t; | ||
@@ -92,33 +92,33 @@ } | ||
const { levels: e, audioTracks: s } = t; | ||
for (const [o, d] of e.entries()) { | ||
const { url: a } = d; | ||
this.core.addStreamIfNoneExists({ runtimeId: Array.isArray(a) ? a[0] : a, type: "main", index: o }); | ||
for (const [r, d] of e.entries()) { | ||
const { url: o } = d; | ||
this.core.addStreamIfNoneExists({ runtimeId: Array.isArray(o) ? o[0] : o, type: "main", index: r }); | ||
} | ||
for (const [o, d] of s.entries()) { | ||
const { url: a } = d; | ||
this.core.addStreamIfNoneExists({ runtimeId: Array.isArray(a) ? a[0] : a, type: "secondary", index: o }); | ||
for (const [r, d] of s.entries()) { | ||
const { url: o } = d; | ||
this.core.addStreamIfNoneExists({ runtimeId: Array.isArray(o) ? o[0] : o, type: "secondary", index: r }); | ||
} | ||
} | ||
updatePlaylist(t) { | ||
const { details: { url: e, fragments: s, live: o } } = t, d = this.core.getStream(e); | ||
const { details: { url: e, fragments: s, live: r } } = t, d = this.core.getStream(e); | ||
if (!d) return; | ||
const a = new Set(d.segments.keys()), E = []; | ||
const o = new Set(d.segments.keys()), E = []; | ||
s.forEach((h, f) => { | ||
const { url: c, byteRange: L, sn: C, start: I, end: w } = h; | ||
const { url: c, byteRange: b, sn: C, start: S, end: D } = h; | ||
if (C === "initSegment") return; | ||
const [S, P] = L, U = J(S, P !== void 0 ? P - 1 : void 0), k = F(c, U); | ||
a.delete(k), d.segments.has(k) || E.push({ runtimeId: k, url: c, externalId: o ? C : f, byteRange: U, startTime: I, endTime: w }); | ||
}), (E.length || a.size) && this.core.updateStream(e, E, a.values()); | ||
const [I, w] = b, U = J(I, w !== void 0 ? w - 1 : void 0), P = F(c, U); | ||
o.delete(P), d.segments.has(P) || E.push({ runtimeId: P, url: c, externalId: r ? C : f, byteRange: U, startTime: S, endTime: D }); | ||
}), (E.length || o.size) && this.core.updateStream(e, E, o.values()); | ||
} | ||
} | ||
class $ { | ||
class G { | ||
constructor(t) { | ||
r(this, "core"); | ||
r(this, "segmentManager"); | ||
r(this, "hlsInstanceGetter"); | ||
r(this, "currentHlsInstance"); | ||
r(this, "debug", N("p2pml-hlsjs:engine")); | ||
r(this, "updateMediaElementEventHandlers", (t) => { | ||
var o; | ||
const e = (o = this.currentHlsInstance) == null ? void 0 : o.media; | ||
a(this, "core"); | ||
a(this, "segmentManager"); | ||
a(this, "hlsInstanceGetter"); | ||
a(this, "currentHlsInstance"); | ||
a(this, "debug", N("p2pml-hlsjs:engine")); | ||
a(this, "updateMediaElementEventHandlers", (t) => { | ||
var r; | ||
const e = (r = this.currentHlsInstance) == null ? void 0 : r.media; | ||
if (!e) return; | ||
@@ -128,24 +128,24 @@ const s = t === "register" ? "addEventListener" : "removeEventListener"; | ||
}); | ||
r(this, "handleManifestLoaded", (t, e) => { | ||
a(this, "handleManifestLoaded", (t, e) => { | ||
const s = e.networkDetails; | ||
s instanceof XMLHttpRequest ? this.core.setManifestResponseUrl(s.responseURL) : s instanceof Response && this.core.setManifestResponseUrl(s.url), this.segmentManager.processMainManifest(e); | ||
}); | ||
r(this, "handleLevelSwitching", (t, e) => { | ||
a(this, "handleLevelSwitching", (t, e) => { | ||
e.bitrate && this.core.setActiveLevelBitrate(e.bitrate); | ||
}); | ||
r(this, "handleLevelUpdated", (t, e) => { | ||
this.currentHlsInstance && this.currentHlsInstance.config.liveSyncDurationCount !== e.details.fragments.length - 1 && e.details.live && e.details.fragments[0].type === "main" && !this.currentHlsInstance.userConfig.liveSyncDuration && !this.currentHlsInstance.userConfig.liveSyncDurationCount && e.details.fragments.length > 4 && (this.debug("set liveSyncDurationCount " + (e.details.fragments.length - 1)), this.currentHlsInstance.config.liveSyncDurationCount = e.details.fragments.length - 1), this.core.setIsLive(e.details.live), this.segmentManager.updatePlaylist(e); | ||
a(this, "handleLevelUpdated", (t, e) => { | ||
this.currentHlsInstance && e.details.live && e.details.fragments[0].type === "main" && !this.currentHlsInstance.userConfig.liveSyncDuration && !this.currentHlsInstance.userConfig.liveSyncDurationCount && e.details.fragments.length > 4 && this.updateLiveSyncDurationCount(e), this.core.setIsLive(e.details.live), this.segmentManager.updatePlaylist(e); | ||
}); | ||
r(this, "handleMediaAttached", () => { | ||
a(this, "handleMediaAttached", () => { | ||
this.updateMediaElementEventHandlers("register"); | ||
}); | ||
r(this, "handleMediaDetached", () => { | ||
a(this, "handleMediaDetached", () => { | ||
this.updateMediaElementEventHandlers("unregister"); | ||
}); | ||
r(this, "handlePlaybackUpdate", (t) => { | ||
a(this, "handlePlaybackUpdate", (t) => { | ||
const e = t.target; | ||
this.core.updatePlayback(e.currentTime, e.playbackRate); | ||
}); | ||
r(this, "destroyCore", () => this.core.destroy()); | ||
r(this, "destroy", () => { | ||
a(this, "destroyCore", () => this.core.destroy()); | ||
a(this, "destroy", () => { | ||
this.destroyCore(), this.updateHlsEventsHandlers("unregister"), this.updateMediaElementEventHandlers("unregister"), this.currentHlsInstance = void 0; | ||
@@ -156,10 +156,10 @@ }); | ||
static injectMixin(t) { | ||
var s, o; | ||
return e = t, o = class extends e { | ||
constructor(...a) { | ||
var L; | ||
const E = a[0], { p2p: h, ...f } = E ?? {}, c = new $(h); | ||
var s, r; | ||
return e = t, r = class extends e { | ||
constructor(...o) { | ||
var b; | ||
const E = o[0], { p2p: h, ...f } = E ?? {}, c = new G(h); | ||
super({ ...f, ...c.getConfigForHlsJs() }); | ||
l(this, s); | ||
c.bindHls(this), u(this, s, c), (L = h == null ? void 0 : h.onHlsJsCreated) == null || L.call(h, this); | ||
c.bindHls(this), u(this, s, c), (b = h == null ? void 0 : h.onHlsJsCreated) == null || b.call(h, this); | ||
} | ||
@@ -169,3 +169,3 @@ get p2pEngine() { | ||
} | ||
}, s = new WeakMap(), o; | ||
}, s = new WeakMap(), r; | ||
var e; | ||
@@ -202,2 +202,6 @@ } | ||
} | ||
updateLiveSyncDurationCount(t) { | ||
const e = t.details.targetduration, s = Math.floor(120 / e), r = Math.min(t.details.fragments.length - 1, s); | ||
this.currentHlsInstance && this.currentHlsInstance.config.liveSyncDurationCount !== r && (this.debug(`Setting liveSyncDurationCount to ${r}`), this.currentHlsInstance.config.liveSyncDurationCount = r); | ||
} | ||
createFragmentLoaderClass() { | ||
@@ -224,4 +228,4 @@ const { core: t } = this, e = this; | ||
export { | ||
$ as HlsJsP2PEngine | ||
G as HlsJsP2PEngine | ||
}; | ||
//# sourceMappingURL=p2p-media-loader-hlsjs.es.min.js.map |
@@ -180,2 +180,3 @@ import type Hls from "hls.js"; | ||
private handleLevelUpdated; | ||
private updateLiveSyncDurationCount; | ||
private handleMediaAttached; | ||
@@ -182,0 +183,0 @@ private handleMediaDetached; |
@@ -6,2 +6,3 @@ import { FragmentLoaderBase } from "./fragment-loader.js"; | ||
import { injectMixin } from "./engine-static.js"; | ||
const MAX_LIVE_SYNC_DURATION = 120; | ||
/** | ||
@@ -142,4 +143,2 @@ * Represents a P2P (peer-to-peer) engine for HLS (HTTP Live Streaming) to enhance media streaming efficiency. | ||
if (this.currentHlsInstance && | ||
this.currentHlsInstance.config.liveSyncDurationCount !== | ||
data.details.fragments.length - 1 && | ||
data.details.live && | ||
@@ -150,5 +149,3 @@ data.details.fragments[0].type === "main" && | ||
data.details.fragments.length > 4) { | ||
this.debug(`set liveSyncDurationCount ${data.details.fragments.length - 1}`); | ||
this.currentHlsInstance.config.liveSyncDurationCount = | ||
data.details.fragments.length - 1; | ||
this.updateLiveSyncDurationCount(data); | ||
} | ||
@@ -312,2 +309,14 @@ this.core.setIsLive(data.details.live); | ||
} | ||
updateLiveSyncDurationCount(data) { | ||
const fragmentDuration = data.details.targetduration; | ||
const maxLiveSyncCount = Math.floor(MAX_LIVE_SYNC_DURATION / fragmentDuration); | ||
const newLiveSyncDurationCount = Math.min(data.details.fragments.length - 1, maxLiveSyncCount); | ||
if (this.currentHlsInstance && | ||
this.currentHlsInstance.config.liveSyncDurationCount !== | ||
newLiveSyncDurationCount) { | ||
this.debug(`Setting liveSyncDurationCount to ${newLiveSyncDurationCount}`); | ||
this.currentHlsInstance.config.liveSyncDurationCount = | ||
newLiveSyncDurationCount; | ||
} | ||
} | ||
createFragmentLoaderClass() { | ||
@@ -314,0 +323,0 @@ const { core } = this; |
{ | ||
"name": "p2p-media-loader-hlsjs", | ||
"version": "2.1.0", | ||
"version": "2.1.1", | ||
"description": "P2P Media Loader hls.js integration", | ||
@@ -38,7 +38,7 @@ "license": "Apache-2.0", | ||
"dependencies": { | ||
"p2p-media-loader-core": "2.1.0" | ||
"p2p-media-loader-core": "2.1.1" | ||
}, | ||
"devDependencies": { | ||
"@rollup/plugin-terser": "^0.4.4", | ||
"hls.js": "^1.5.17" | ||
"hls.js": "^1.5.19" | ||
}, | ||
@@ -45,0 +45,0 @@ "scripts": { |
@@ -64,2 +64,4 @@ import type Hls from "hls.js"; | ||
const MAX_LIVE_SYNC_DURATION = 120; | ||
/** | ||
@@ -311,4 +313,2 @@ * Represents a P2P (peer-to-peer) engine for HLS (HTTP Live Streaming) to enhance media streaming efficiency. | ||
this.currentHlsInstance && | ||
this.currentHlsInstance.config.liveSyncDurationCount !== | ||
data.details.fragments.length - 1 && | ||
data.details.live && | ||
@@ -320,7 +320,3 @@ data.details.fragments[0].type === ("main" as PlaylistLevelType) && | ||
) { | ||
this.debug( | ||
`set liveSyncDurationCount ${data.details.fragments.length - 1}`, | ||
); | ||
this.currentHlsInstance.config.liveSyncDurationCount = | ||
data.details.fragments.length - 1; | ||
this.updateLiveSyncDurationCount(data); | ||
} | ||
@@ -332,2 +328,28 @@ | ||
private updateLiveSyncDurationCount( | ||
data: LevelUpdatedData | AudioTrackLoadedData, | ||
) { | ||
const fragmentDuration = data.details.targetduration; | ||
const maxLiveSyncCount = Math.floor( | ||
MAX_LIVE_SYNC_DURATION / fragmentDuration, | ||
); | ||
const newLiveSyncDurationCount = Math.min( | ||
data.details.fragments.length - 1, | ||
maxLiveSyncCount, | ||
); | ||
if ( | ||
this.currentHlsInstance && | ||
this.currentHlsInstance.config.liveSyncDurationCount !== | ||
newLiveSyncDurationCount | ||
) { | ||
this.debug( | ||
`Setting liveSyncDurationCount to ${newLiveSyncDurationCount}`, | ||
); | ||
this.currentHlsInstance.config.liveSyncDurationCount = | ||
newLiveSyncDurationCount; | ||
} | ||
} | ||
private handleMediaAttached = () => { | ||
@@ -334,0 +356,0 @@ this.updateMediaElementEventHandlers("register"); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
190082
1.72%2270
1.98%+ Added
- Removed
Updated