@give-tech/ec-player 0.0.1-beta.0 → 0.0.1-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +45 -28
- package/dist/index.js.map +1 -1
- package/dist/player/BasePlayer.d.ts.map +1 -1
- package/dist/player/EcPlayerCore.d.ts.map +1 -1
- package/dist/player/FLVPlayer.d.ts.map +1 -1
- package/dist/player/HLSPlayer.d.ts +2 -1
- package/dist/player/HLSPlayer.d.ts.map +1 -1
- package/dist/prefetch/SegmentPrefetcher.d.ts +6 -2
- package/dist/prefetch/SegmentPrefetcher.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -147,6 +147,7 @@ class BasePlayer {
|
|
|
147
147
|
droppedFrames: 0,
|
|
148
148
|
isPrefetching: false,
|
|
149
149
|
segmentIndex: 0,
|
|
150
|
+
fetchedSegmentCount: 0,
|
|
150
151
|
totalSegments: 0,
|
|
151
152
|
downloadSpeed: 0,
|
|
152
153
|
currentTime: 0,
|
|
@@ -227,6 +228,7 @@ class BasePlayer {
|
|
|
227
228
|
droppedFrames: 0,
|
|
228
229
|
isPrefetching: false,
|
|
229
230
|
segmentIndex: 0,
|
|
231
|
+
fetchedSegmentCount: 0,
|
|
230
232
|
totalSegments: 0,
|
|
231
233
|
downloadSpeed: 0,
|
|
232
234
|
currentTime: 0,
|
|
@@ -973,6 +975,7 @@ class SegmentPrefetcher extends BasePrefetcher {
|
|
|
973
975
|
this.prefetchQueue = [];
|
|
974
976
|
this.segments = [];
|
|
975
977
|
this.currentSegmentIndex = 0;
|
|
978
|
+
this.fetchedSegmentCount = 0;
|
|
976
979
|
this.baseUrl = "";
|
|
977
980
|
this.isPrefetchingSegment = false;
|
|
978
981
|
}
|
|
@@ -984,7 +987,8 @@ class SegmentPrefetcher extends BasePrefetcher {
|
|
|
984
987
|
...super.createInitialState(),
|
|
985
988
|
currentSegmentIndex: 0,
|
|
986
989
|
totalSegments: 0,
|
|
987
|
-
prefetchQueueSize: 0
|
|
990
|
+
prefetchQueueSize: 0,
|
|
991
|
+
fetchedSegmentCount: 0
|
|
988
992
|
};
|
|
989
993
|
}
|
|
990
994
|
/**
|
|
@@ -998,7 +1002,8 @@ class SegmentPrefetcher extends BasePrefetcher {
|
|
|
998
1002
|
this.updateStatus({
|
|
999
1003
|
totalSegments: segments.length,
|
|
1000
1004
|
currentSegmentIndex: 0,
|
|
1001
|
-
prefetchQueueSize: 0
|
|
1005
|
+
prefetchQueueSize: 0,
|
|
1006
|
+
fetchedSegmentCount: 0
|
|
1002
1007
|
});
|
|
1003
1008
|
console.log(`[SegmentPrefetcher] Set ${segments.length} segments, baseUrl: ${baseUrl}`);
|
|
1004
1009
|
}
|
|
@@ -1010,7 +1015,8 @@ class SegmentPrefetcher extends BasePrefetcher {
|
|
|
1010
1015
|
this.prefetchQueue = [];
|
|
1011
1016
|
this.updateStatus({
|
|
1012
1017
|
currentSegmentIndex: index,
|
|
1013
|
-
prefetchQueueSize: 0
|
|
1018
|
+
prefetchQueueSize: 0,
|
|
1019
|
+
fetchedSegmentCount: 0
|
|
1014
1020
|
});
|
|
1015
1021
|
console.log(`[SegmentPrefetcher] Current segment index set to ${index}`);
|
|
1016
1022
|
}
|
|
@@ -1052,7 +1058,11 @@ class SegmentPrefetcher extends BasePrefetcher {
|
|
|
1052
1058
|
segmentIndex: nextIndex,
|
|
1053
1059
|
fetchTime
|
|
1054
1060
|
});
|
|
1055
|
-
this.
|
|
1061
|
+
this.fetchedSegmentCount++;
|
|
1062
|
+
this.updateStatus({
|
|
1063
|
+
prefetchQueueSize: this.prefetchQueue.length,
|
|
1064
|
+
fetchedSegmentCount: this.fetchedSegmentCount
|
|
1065
|
+
});
|
|
1056
1066
|
const callbacks = this.callbacks;
|
|
1057
1067
|
callbacks.onSegmentFetched?.(nextIndex, data);
|
|
1058
1068
|
console.log(`[SegmentPrefetcher] Fetched segment #${nextIndex}: ${data.length} bytes, ${fetchTime.toFixed(0)}ms`);
|
|
@@ -1378,16 +1388,12 @@ class HLSPlayer extends BasePlayer {
|
|
|
1378
1388
|
this._nalQueue = [];
|
|
1379
1389
|
this._sampleQueue = [];
|
|
1380
1390
|
this.prefetcher = null;
|
|
1381
|
-
this.
|
|
1391
|
+
this.lastRenderTime = 0;
|
|
1392
|
+
this.renderLoop = (timestamp = 0) => {
|
|
1382
1393
|
if (!this.isPlaying) return;
|
|
1383
1394
|
const downloaded = this.isFMP4 ? this.sampleQueue.length : this.nalQueue.length;
|
|
1384
1395
|
const segments = this.isFMP4 ? this.fmp4Segments : this.segments;
|
|
1385
1396
|
const totalSegments = segments.length;
|
|
1386
|
-
let currentTime = 0;
|
|
1387
|
-
for (let i = 0; i < this.currentSegmentIndex && i < segments.length; i++) {
|
|
1388
|
-
currentTime += segments[i].duration * 1e3;
|
|
1389
|
-
}
|
|
1390
|
-
this.setCurrentTime(currentTime);
|
|
1391
1397
|
this.updateState({
|
|
1392
1398
|
decoded: this.frameBuffer.length,
|
|
1393
1399
|
downloaded,
|
|
@@ -1398,10 +1404,17 @@ class HLSPlayer extends BasePlayer {
|
|
|
1398
1404
|
if (this.frameBuffer.length > 0 && this.renderer) {
|
|
1399
1405
|
const frame = this.frameBuffer.shift();
|
|
1400
1406
|
this.renderer.render(frame);
|
|
1407
|
+
if (this.lastRenderTime > 0) {
|
|
1408
|
+
const elapsed = timestamp - this.lastRenderTime;
|
|
1409
|
+
this.setCurrentTime(this._currentTime + elapsed);
|
|
1410
|
+
}
|
|
1411
|
+
this.lastRenderTime = timestamp;
|
|
1401
1412
|
this.updateState({ resolution: `${frame.width}x${frame.height}` });
|
|
1402
1413
|
const fps = this.renderer.updateFps();
|
|
1403
1414
|
this.updateState({ fps });
|
|
1404
1415
|
this.callbacks.onFrameRender?.(frame);
|
|
1416
|
+
} else {
|
|
1417
|
+
this.lastRenderTime = 0;
|
|
1405
1418
|
}
|
|
1406
1419
|
this.frameTimer = requestAnimationFrame(this.renderLoop);
|
|
1407
1420
|
};
|
|
@@ -1475,8 +1488,11 @@ class HLSPlayer extends BasePlayer {
|
|
|
1475
1488
|
async play() {
|
|
1476
1489
|
this.isPlaying = true;
|
|
1477
1490
|
this.decodeLoopAbort = false;
|
|
1491
|
+
this.lastRenderTime = 0;
|
|
1478
1492
|
this.updateState({ isPlaying: true });
|
|
1479
|
-
this.
|
|
1493
|
+
if (!this.prefetcher) {
|
|
1494
|
+
this.initPrefetcher();
|
|
1495
|
+
}
|
|
1480
1496
|
this.prefetcher?.start();
|
|
1481
1497
|
this.decodeLoop();
|
|
1482
1498
|
this.frameTimer = requestAnimationFrame(this.renderLoop);
|
|
@@ -1569,9 +1585,11 @@ class HLSPlayer extends BasePlayer {
|
|
|
1569
1585
|
HLS_PREFETCHER_CONFIG,
|
|
1570
1586
|
{
|
|
1571
1587
|
onStatusChange: (status) => {
|
|
1588
|
+
const segStatus = status;
|
|
1572
1589
|
this.updateState({
|
|
1573
1590
|
isPrefetching: status.isPrefetching,
|
|
1574
|
-
downloadSpeed: status.downloadSpeed
|
|
1591
|
+
downloadSpeed: status.downloadSpeed,
|
|
1592
|
+
fetchedSegmentCount: segStatus.fetchedSegmentCount ?? 0
|
|
1575
1593
|
});
|
|
1576
1594
|
},
|
|
1577
1595
|
onSegmentParsed: (segmentIndex, itemCount) => {
|
|
@@ -2537,7 +2555,7 @@ class FLVPlayer extends BasePlayer {
|
|
|
2537
2555
|
const isLive = this.config.isLive;
|
|
2538
2556
|
if (this._timedFrameBuffer.length > 0 && this.renderer) {
|
|
2539
2557
|
this.consecutiveEmptyBuffer = 0;
|
|
2540
|
-
if (this.
|
|
2558
|
+
if (this.playStartTime <= 0) {
|
|
2541
2559
|
this.firstFrameDts = this._timedFrameBuffer[0].dts;
|
|
2542
2560
|
this.playStartTime = now;
|
|
2543
2561
|
this.playStartTimeOffset = 0;
|
|
@@ -2792,24 +2810,22 @@ class FLVPlayer extends BasePlayer {
|
|
|
2792
2810
|
}
|
|
2793
2811
|
let newCount = 0;
|
|
2794
2812
|
for (const tag of result.videoTags) {
|
|
2795
|
-
|
|
2796
|
-
|
|
2797
|
-
|
|
2798
|
-
|
|
2799
|
-
|
|
2800
|
-
|
|
2801
|
-
|
|
2802
|
-
|
|
2803
|
-
break;
|
|
2804
|
-
}
|
|
2813
|
+
let annexBData = this.flvDemuxer.videoTagToAnnexB(tag, lengthSize);
|
|
2814
|
+
if (!annexBData) {
|
|
2815
|
+
for (const trySize of [4, 2, 1]) {
|
|
2816
|
+
if (trySize !== lengthSize) {
|
|
2817
|
+
annexBData = this.flvDemuxer.videoTagToAnnexB(tag, trySize);
|
|
2818
|
+
if (annexBData) {
|
|
2819
|
+
lengthSize = trySize;
|
|
2820
|
+
break;
|
|
2805
2821
|
}
|
|
2806
2822
|
}
|
|
2807
2823
|
}
|
|
2808
|
-
|
|
2809
|
-
|
|
2810
|
-
|
|
2811
|
-
|
|
2812
|
-
|
|
2824
|
+
}
|
|
2825
|
+
if (annexBData) {
|
|
2826
|
+
this._videoTagQueue.push({ tag, annexBData });
|
|
2827
|
+
this._lastQueuedTimestamp = tag.timestamp;
|
|
2828
|
+
newCount++;
|
|
2813
2829
|
}
|
|
2814
2830
|
}
|
|
2815
2831
|
if (newCount > 0) {
|
|
@@ -3476,6 +3492,7 @@ class EcPlayerCore {
|
|
|
3476
3492
|
droppedFrames: 0,
|
|
3477
3493
|
isPrefetching: false,
|
|
3478
3494
|
segmentIndex: 0,
|
|
3495
|
+
fetchedSegmentCount: 0,
|
|
3479
3496
|
totalSegments: 0,
|
|
3480
3497
|
downloadSpeed: 0,
|
|
3481
3498
|
currentTime: 0,
|