1
1
import { CommonCoreConfig , StreamConfig , StreamType } from "../types.js" ;
2
2
import debug from "debug" ;
3
3
import { SegmentStorage } from "./index.js" ;
4
+ import {
5
+ isAndroid ,
6
+ isIPadOrIPhone ,
7
+ isAndroidWebview ,
8
+ getStorageItemId ,
9
+ } from "./utils.js" ;
4
10
5
11
type SegmentDataItem = {
6
12
segmentId : number ;
7
13
streamId : string ;
8
14
data : ArrayBuffer ;
9
15
startTime : number ;
10
16
endTime : number ;
11
- streamType : string ;
17
+ streamType : StreamType ;
12
18
} ;
13
19
14
20
type Playback = {
@@ -26,16 +32,6 @@ type LastRequestedSegmentInfo = {
26
32
isLiveStream : boolean ;
27
33
} ;
28
34
29
- const getStorageItemId = ( streamId : string , segmentId : number ) =>
30
- `${ streamId } |${ segmentId } ` ;
31
-
32
- const isAndroid = ( ua : string ) => / A n d r o i d / i. test ( ua ) ;
33
-
34
- const isIPadOrIPhone = ( ua : string ) => / i P a d | i P h o n e / i. test ( ua ) ;
35
-
36
- const isAndroidWebview = ( ua : string ) =>
37
- / A n d r o i d / i. test ( ua ) && ! / C h r o m e | F i r e f o x / i. test ( ua ) ;
38
-
39
35
const BYTES_PER_MB = 1048576 ;
40
36
41
37
export class SegmentMemoryStorage implements SegmentStorage {
@@ -117,7 +113,7 @@ export class SegmentMemoryStorage implements SegmentStorage {
117
113
endTime,
118
114
streamType,
119
115
} ) ;
120
- this . updateMemoryStorageSize ( data . byteLength , true ) ;
116
+ this . increaseMemoryStorageSize ( data . byteLength ) ;
121
117
122
118
this . logger ( `add segment: ${ segmentId } to ${ streamId } ` ) ;
123
119
@@ -145,19 +141,18 @@ export class SegmentMemoryStorage implements SegmentStorage {
145
141
memoryUsed : this . currentMemoryStorageSize ,
146
142
} ;
147
143
}
148
- const PlaybackPosition = this . currentPlayback . position ;
144
+ const playbackPosition = this . currentPlayback . position ;
149
145
150
146
let potentialFreeSpace = 0 ;
151
147
for ( const segmentData of this . cache . values ( ) ) {
152
148
const { endTime } = segmentData ;
153
149
154
- if ( PlaybackPosition <= endTime ) continue ;
150
+ if ( playbackPosition <= endTime ) continue ;
155
151
156
152
potentialFreeSpace += segmentData . data . byteLength / BYTES_PER_MB ;
157
153
}
158
154
159
- const usedMemoryInMB =
160
- this . currentMemoryStorageSize - potentialFreeSpace / BYTES_PER_MB ;
155
+ const usedMemoryInMB = this . currentMemoryStorageSize - potentialFreeSpace ;
161
156
162
157
return {
163
158
memoryLimit : this . segmentsMemoryStorageLimit ,
@@ -215,8 +210,8 @@ export class SegmentMemoryStorage implements SegmentStorage {
215
210
216
211
if ( ! shouldRemove ) continue ;
217
212
213
+ this . decreaseMemoryStorageSize ( segmentData . data . byteLength ) ;
218
214
this . cache . delete ( storageId ) ;
219
- this . updateMemoryStorageSize ( segmentData . data . byteLength ) ;
220
215
affectedStreams . add ( streamId ) ;
221
216
this . logger ( `Removed segment ${ segmentId } from stream ${ streamId } ` ) ;
222
217
@@ -249,17 +244,12 @@ export class SegmentMemoryStorage implements SegmentStorage {
249
244
} ) ;
250
245
}
251
246
252
- private updateMemoryStorageSize (
253
- byteLength : number ,
254
- isAddition : boolean = false ,
255
- ) : void {
256
- const changeInMB = byteLength / BYTES_PER_MB ;
247
+ private increaseMemoryStorageSize ( byteLength : number ) {
248
+ this . currentMemoryStorageSize += byteLength / BYTES_PER_MB ;
249
+ }
257
250
258
- if ( isAddition ) {
259
- this . currentMemoryStorageSize += changeInMB ;
260
- } else {
261
- this . currentMemoryStorageSize -= changeInMB ;
262
- }
251
+ private decreaseMemoryStorageSize ( byteLength : number ) {
252
+ this . currentMemoryStorageSize -= byteLength / BYTES_PER_MB ;
263
253
}
264
254
265
255
private shouldRemoveSegment (
@@ -276,10 +266,7 @@ export class SegmentMemoryStorage implements SegmentStorage {
276
266
if ( currentPlaybackPosition <= endTime ) return false ;
277
267
278
268
if ( isLiveStream ) {
279
- if ( currentPlaybackPosition > highDemandTimeWindow + endTime ) {
280
- return true ;
281
- }
282
- return false ;
269
+ return currentPlaybackPosition > highDemandTimeWindow + endTime ;
283
270
}
284
271
285
272
return true ;
@@ -303,15 +290,12 @@ export class SegmentMemoryStorage implements SegmentStorage {
303
290
streamType : string ,
304
291
configKey : "highDemandTimeWindow" | "httpDownloadTimeWindow" ,
305
292
) : number {
306
- if ( ! this . mainStreamConfig || ! this . secondaryStreamConfig ) {
307
- return 0 ;
308
- }
309
-
310
293
const config =
311
294
streamType === "main"
312
295
? this . mainStreamConfig
313
296
: this . secondaryStreamConfig ;
314
- return config [ configKey ] ;
297
+
298
+ return config ?. [ configKey ] ?? 0 ;
315
299
}
316
300
317
301
public destroy ( ) {
0 commit comments