diff --git a/packages/p2p-media-loader-core/src/hybrid-loader.ts b/packages/p2p-media-loader-core/src/hybrid-loader.ts index 16d24880..00078acb 100644 --- a/packages/p2p-media-loader-core/src/hybrid-loader.ts +++ b/packages/p2p-media-loader-core/src/hybrid-loader.ts @@ -19,7 +19,7 @@ export class HybridLoader { private readonly playback: Playback; private lastQueueProcessingTimeStamp?: number; private readonly segmentAvgDuration: number; - private readonly randomHttpDownloadInterval: number; + private randomHttpDownloadInterval!: number; private readonly logger: { engine: debug.Debugger; loader: debug.Debugger }; constructor( @@ -54,15 +54,25 @@ export class HybridLoader { this.settings ); - this.logger = { - loader: debug(`core:hybrid-loader-${activeStream.type}`), - engine: debug(`core:hybrid-loader-${activeStream.type}-engine`), - }; + const loader = debug(`core:hybrid-loader-${activeStream.type}`); + const engine = debug(`core:hybrid-loader-${activeStream.type}-engine`); + loader.color = "coral"; + engine.color = "orange"; + this.logger = { loader, engine }; - this.randomHttpDownloadInterval = window.setInterval( - this.loadRandomThroughHttp.bind(this), - 1500 - ); + // this.randomHttpDownloadInterval = window.setInterval( + // this.loadRandomThroughHttp.bind(this), + // 1500 + // ); + this.setIntervalLoading(); + } + + private setIntervalLoading() { + const randomTimeout = (Math.random() * 2 + 1) * 1000; + this.randomHttpDownloadInterval = window.setTimeout(() => { + this.loadRandomThroughHttp(); + this.setIntervalLoading(); + }, randomTimeout); } // api method for engines @@ -254,7 +264,7 @@ export class HybridLoader { }); if (!queue.length) return; const peersAmount = connectedPeersAmount + 1; - const probability = Math.min(queue.length / peersAmount, 1) / 2; + const probability = Math.min(queue.length / peersAmount, 1); const shouldLoad = Math.random() < probability; if (!shouldLoad) return; diff --git a/packages/p2p-media-loader-core/src/p2p-loader.ts b/packages/p2p-media-loader-core/src/p2p-loader.ts index c510fa16..5951d8a1 100644 --- a/packages/p2p-media-loader-core/src/p2p-loader.ts +++ b/packages/p2p-media-loader-core/src/p2p-loader.ts @@ -89,6 +89,7 @@ export class P2PLoader { candidate, { onPeerConnected: this.onPeerConnected.bind(this), + onPeerClosed: this.onPeerClosed.bind(this), onSegmentRequested: this.onSegmentRequested.bind(this), }, this.settings @@ -156,6 +157,11 @@ export class P2PLoader { peer.sendSegmentsAnnouncement(this.announcement); } + private onPeerClosed(peer: Peer) { + this.logger(`peer closed: ${peer.localId}`); + this.peers.delete(peer.id); + } + private updateAndBroadcastAnnouncement = () => { this.updateSegmentAnnouncement(); this.broadcastSegmentAnnouncement(); diff --git a/packages/p2p-media-loader-core/src/peer.ts b/packages/p2p-media-loader-core/src/peer.ts index aa81eb09..22f456aa 100644 --- a/packages/p2p-media-loader-core/src/peer.ts +++ b/packages/p2p-media-loader-core/src/peer.ts @@ -15,6 +15,7 @@ import { PeerRequestError } from "./errors"; type PeerEventHandlers = { onPeerConnected: (peer: Peer) => void; + onPeerClosed: (peer: Peer) => void; onSegmentRequested: (peer: Peer, segmentId: string) => void; }; @@ -63,6 +64,7 @@ export class Peer { if (this.connection === candidate) { this.connection = undefined; this.cancelSegmentRequest("peer-closed"); + this.eventHandlers.onPeerClosed(this); } }); // eslint-disable-next-line @typescript-eslint/no-empty-function