@@ -4,14 +4,12 @@ import { SegmentsMemoryStorage } from "./segments-storage";
4
4
import { Settings , CoreEventHandlers } from "./types" ;
5
5
import { BandwidthApproximator } from "./bandwidth-approximator" ;
6
6
import { Playback , QueueItem } from "./internal-types" ;
7
- import {
8
- RequestsContainer ,
9
- EngineCallbacks ,
10
- HybridLoaderRequest ,
11
- Request ,
12
- } from "./request-container" ;
7
+ import { RequestsContainer } from "./request-container" ;
8
+ import { Request , EngineCallbacks , HybridLoaderRequest } from "./request" ;
13
9
import * as QueueUtils from "./utils/queue" ;
14
10
import * as LoggerUtils from "./utils/logger" ;
11
+ import * as StreamUtils from "./utils/stream" ;
12
+ import * as Utils from "./utils/utils" ;
15
13
import { P2PLoadersContainer } from "./p2p/loaders-container" ;
16
14
import { PeerRequestError } from "./p2p/peer" ;
17
15
import debug from "debug" ;
@@ -40,7 +38,7 @@ export class HybridLoader {
40
38
this . lastRequestedSegment = requestedSegment ;
41
39
const activeStream = requestedSegment . stream ;
42
40
this . playback = { position : requestedSegment . startTime , rate : 1 } ;
43
- this . segmentAvgDuration = getSegmentAvgDuration ( activeStream ) ;
41
+ this . segmentAvgDuration = StreamUtils . getSegmentAvgDuration ( activeStream ) ;
44
42
this . requests = new RequestsContainer (
45
43
requestedSegment . stream . type ,
46
44
this . bandwidthApproximator
@@ -159,23 +157,6 @@ export class HybridLoader {
159
157
160
158
if ( statuses . isHighDemand ) {
161
159
if ( request ?. type === "http" ) continue ;
162
-
163
- if ( request ?. type === "p2p" ) {
164
- const timeToPlayback = getTimeToSegmentPlayback (
165
- segment ,
166
- this . playback
167
- ) ;
168
- const remainingDownloadTime =
169
- getPredictedRemainingDownloadTime ( request ) ;
170
- if (
171
- remainingDownloadTime === undefined ||
172
- remainingDownloadTime > timeToPlayback
173
- ) {
174
- request . abort ( ) ;
175
- } else {
176
- continue ;
177
- }
178
- }
179
160
if ( this . requests . executingHttpCount < simultaneousHttpDownloads ) {
180
161
void this . loadThroughHttp ( item ) ;
181
162
continue ;
@@ -218,8 +199,11 @@ export class HybridLoader {
218
199
219
200
// api method for engines
220
201
abortSegment ( segment : Segment ) {
202
+ const request = this . requests . get ( segment ) ;
203
+ if ( ! request ) return ;
204
+ request . abort ( ) ;
205
+ this . createProcessQueueMicrotask ( ) ;
221
206
this . logger . engine ( "abort: " , LoggerUtils . getSegmentString ( segment ) ) ;
222
- this . requests . abortEngineRequest ( segment ) ;
223
207
}
224
208
225
209
private async loadThroughHttp ( item : QueueItem , isRandom = false ) {
@@ -286,7 +270,7 @@ export class HybridLoader {
286
270
const shouldLoad = Math . random ( ) < probability ;
287
271
288
272
if ( ! shouldLoad ) return ;
289
- const item = queue [ Math . floor ( Math . random ( ) * queue . length ) ] ;
273
+ const item = Utils . getRandomItem ( queue ) ;
290
274
void this . loadThroughHttp ( item , true ) ;
291
275
292
276
this . logger . loader (
@@ -387,36 +371,3 @@ function* arrayBackwards<T>(arr: T[]) {
387
371
yield arr [ i ] ;
388
372
}
389
373
}
390
-
391
- function getTimeToSegmentPlayback ( segment : Segment , playback : Playback ) {
392
- return Math . max ( segment . startTime - playback . position , 0 ) / playback . rate ;
393
- }
394
-
395
- function getPredictedRemainingDownloadTime ( request : HybridLoaderRequest ) {
396
- const { progress } = request ;
397
- if ( ! progress || progress . lastLoadedChunkTimestamp === undefined ) {
398
- return undefined ;
399
- }
400
-
401
- const now = performance . now ( ) ;
402
- const bandwidth =
403
- progress . percent /
404
- ( progress . lastLoadedChunkTimestamp - progress . startTimestamp ) ;
405
- const remainingDownloadPercent = 100 - progress . percent ;
406
- const predictedRemainingTimeFromLastDownload =
407
- remainingDownloadPercent / bandwidth ;
408
- const timeFromLastDownload = now - progress . lastLoadedChunkTimestamp ;
409
- return ( predictedRemainingTimeFromLastDownload - timeFromLastDownload ) / 1000 ;
410
- }
411
-
412
- function getSegmentAvgDuration ( stream : StreamWithSegments ) {
413
- const { segments } = stream ;
414
- let sumDuration = 0 ;
415
- const size = segments . size ;
416
- for ( const segment of segments . values ( ) ) {
417
- const duration = segment . endTime - segment . startTime ;
418
- sumDuration += duration ;
419
- }
420
-
421
- return sumDuration / size ;
422
- }
0 commit comments