@@ -61,6 +61,8 @@ export type HlsWithP2PConfig<HlsType extends abstract new () => unknown> =
61
61
} ;
62
62
} ;
63
63
64
+ const MAX_LIVE_SYNC_DURATION = 120 ;
65
+
64
66
/**
65
67
* Represents a P2P (peer-to-peer) engine for HLS (HTTP Live Streaming) to enhance media streaming efficiency.
66
68
* This class integrates P2P technologies into HLS.js, enabling the distribution of media segments via a peer network
@@ -307,25 +309,45 @@ export class HlsJsP2PEngine {
307
309
) => {
308
310
if (
309
311
this . currentHlsInstance &&
310
- this . currentHlsInstance . config . liveSyncDurationCount !==
311
- data . details . fragments . length - 1 &&
312
312
data . details . live &&
313
313
data . details . fragments [ 0 ] . type === ( "main" as PlaylistLevelType ) &&
314
314
! this . currentHlsInstance . userConfig . liveSyncDuration &&
315
315
! this . currentHlsInstance . userConfig . liveSyncDurationCount &&
316
316
data . details . fragments . length > 4
317
317
) {
318
- this . debug (
319
- `set liveSyncDurationCount ${ data . details . fragments . length - 1 } ` ,
320
- ) ;
321
- this . currentHlsInstance . config . liveSyncDurationCount =
322
- data . details . fragments . length - 1 ;
318
+ this . updateLiveSyncDurationCount ( data ) ;
323
319
}
324
320
325
321
this . core . setIsLive ( data . details . live ) ;
326
322
this . segmentManager . updatePlaylist ( data ) ;
327
323
} ;
328
324
325
+ private updateLiveSyncDurationCount (
326
+ data : LevelUpdatedData | AudioTrackLoadedData ,
327
+ ) {
328
+ const fragmentDuration = data . details . targetduration ;
329
+
330
+ const maxLiveSyncCount = Math . floor (
331
+ MAX_LIVE_SYNC_DURATION / fragmentDuration ,
332
+ ) ;
333
+ const newLiveSyncDurationCount = Math . min (
334
+ data . details . fragments . length - 1 ,
335
+ maxLiveSyncCount ,
336
+ ) ;
337
+
338
+ if (
339
+ this . currentHlsInstance &&
340
+ this . currentHlsInstance . config . liveSyncDurationCount !==
341
+ newLiveSyncDurationCount
342
+ ) {
343
+ this . debug (
344
+ `Setting liveSyncDurationCount to ${ newLiveSyncDurationCount } ` ,
345
+ ) ;
346
+ this . currentHlsInstance . config . liveSyncDurationCount =
347
+ newLiveSyncDurationCount ;
348
+ }
349
+ }
350
+
329
351
private handleMediaAttached = ( ) => {
330
352
this . updateMediaElementEventHandlers ( "register" ) ;
331
353
} ;
0 commit comments