@give-tech/ec-player 0.0.1-beta.6 → 0.0.1-beta.7

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 CHANGED
@@ -1460,12 +1460,18 @@ const DEFAULT_HLS_CONFIG = {
1460
1460
  class HLSSegmentPrefetcher extends SegmentPrefetcher {
1461
1461
  constructor(config, callbacks, player) {
1462
1462
  super(config, callbacks);
1463
+ this.currentInitSegmentUri = null;
1463
1464
  this.player = player;
1464
1465
  }
1465
1466
  /**
1466
1467
  * 获取分片数据
1467
1468
  */
1468
1469
  async fetchSegment(segment, index) {
1470
+ if (segment.initSegmentUri && segment.initSegmentUri !== this.currentInitSegmentUri) {
1471
+ console.log("[HLSSegmentPrefetcher] Init segment changed:", segment.initSegmentUri);
1472
+ await this.player.loadNewInitSegment(segment.initSegmentUri);
1473
+ this.currentInitSegmentUri = segment.initSegmentUri;
1474
+ }
1469
1475
  const baseUrl = this.baseUrl;
1470
1476
  const url = segment.uri.startsWith("http") ? segment.uri : baseUrl + segment.uri;
1471
1477
  const headers = {};
@@ -1735,7 +1741,9 @@ class HLSPlayer extends BasePlayer {
1735
1741
  const segmentInfos = this.fmp4Segments.map((seg) => ({
1736
1742
  uri: seg.uri,
1737
1743
  duration: seg.duration,
1738
- byteRange: seg.byteRange
1744
+ byteRange: seg.byteRange,
1745
+ initSegmentUri: seg.initSegmentUri
1746
+ // 传递初始化段 URI
1739
1747
  }));
1740
1748
  this.prefetcher.setSegments(segmentInfos, baseUrl);
1741
1749
  } else {
@@ -1842,6 +1850,45 @@ class HLSPlayer extends BasePlayer {
1842
1850
  throw new Error("Failed to parse fMP4 init segment: no valid codec config found");
1843
1851
  }
1844
1852
  }
1853
+ /**
1854
+ * 加载新的初始化段(用于不连续性流)
1855
+ */
1856
+ async loadNewInitSegment(uri) {
1857
+ console.log("[fMP4] Loading new init segment for discontinuity:", uri);
1858
+ this._sampleQueue.length = 0;
1859
+ this._nalQueue.length = 0;
1860
+ this.frameBuffer = [];
1861
+ const url = uri.startsWith("http") ? uri : this.currentPlaylistUrl.substring(0, this.currentPlaylistUrl.lastIndexOf("/") + 1) + uri;
1862
+ const response = await fetch(url);
1863
+ const data = new Uint8Array(await response.arrayBuffer());
1864
+ console.log("[fMP4] New init segment data size:", data.length, "bytes");
1865
+ const initInfo = this.fmp4Demuxer.parseInitSegment(data);
1866
+ console.log("[fMP4] New init segment parse result:", {
1867
+ isHevc: initInfo?.isHevc,
1868
+ hasAvcC: !!initInfo?.avcC,
1869
+ hasHvcC: !!initInfo?.hvcC
1870
+ });
1871
+ const newIsHevc = initInfo?.isHevc ?? false;
1872
+ if (newIsHevc !== this._isHevc) {
1873
+ console.log("[fMP4] Codec type changed from", this._isHevc ? "HEVC" : "AVC", "to", newIsHevc ? "HEVC" : "AVC");
1874
+ this._isHevc = newIsHevc;
1875
+ this.decoderInitialized = false;
1876
+ if (newIsHevc) {
1877
+ await this.initHevcDecoder();
1878
+ }
1879
+ } else {
1880
+ this.decoderInitialized = false;
1881
+ }
1882
+ if (initInfo?.hvcC && this._isHevc) {
1883
+ this.initDecoderFromHvcC(initInfo.hvcC);
1884
+ console.log("[fMP4] HEVC decoder re-initialized for discontinuity");
1885
+ } else if (initInfo?.avcC) {
1886
+ this.initDecoderFromAvcC(initInfo.avcC);
1887
+ console.log("[fMP4] AVC decoder re-initialized for discontinuity");
1888
+ } else {
1889
+ console.error("[fMP4] Failed to parse new init segment!");
1890
+ }
1891
+ }
1845
1892
  /**
1846
1893
  * 初始化 HEVC 解码器
1847
1894
  */
@@ -1961,25 +2008,31 @@ class HLSPlayer extends BasePlayer {
1961
2008
  }
1962
2009
  const isFMP4 = lines.some((line) => line.includes("#EXT-X-MAP:"));
1963
2010
  console.log("[parsePlaylist] isFMP4:", isFMP4, "url:", url);
2011
+ let currentInitSegment;
1964
2012
  for (let i = 0; i < lines.length; i++) {
1965
2013
  const line = lines[i].trim();
1966
2014
  if (line.startsWith("#EXT-X-MAP:")) {
1967
2015
  const mapInfo = line.substring("#EXT-X-MAP:".length);
1968
2016
  const uriMatch = mapInfo.match(/URI="([^"]+)"/);
1969
2017
  if (uriMatch) {
1970
- initSegment = { uri: uriMatch[1] };
2018
+ currentInitSegment = { uri: uriMatch[1] };
1971
2019
  const byteRangeMatch = mapInfo.match(/BYTERANGE="(\d+)@(\d+)"/);
1972
2020
  if (byteRangeMatch) {
1973
- initSegment.byteRange = {
2021
+ currentInitSegment.byteRange = {
1974
2022
  start: parseInt(byteRangeMatch[2]),
1975
2023
  end: parseInt(byteRangeMatch[2]) + parseInt(byteRangeMatch[1]) - 1
1976
2024
  };
1977
2025
  }
2026
+ if (!initSegment) {
2027
+ initSegment = currentInitSegment;
2028
+ }
1978
2029
  }
2030
+ continue;
2031
+ }
2032
+ if (line === "#EXT-X-DISCONTINUITY") {
2033
+ console.log("[parsePlaylist] Found EXT-X-DISCONTINUITY");
2034
+ continue;
1979
2035
  }
1980
- }
1981
- for (let i = 0; i < lines.length; i++) {
1982
- const line = lines[i].trim();
1983
2036
  if (line.startsWith("#EXTINF:")) {
1984
2037
  const duration = parseFloat(line.split(":")[1].split(",")[0]);
1985
2038
  let byteRange;
@@ -1997,7 +2050,13 @@ class HLSPlayer extends BasePlayer {
1997
2050
  const uri = nextLine;
1998
2051
  if (uri && !uri.startsWith("#")) {
1999
2052
  if (isFMP4) {
2000
- fmp4Segments.push({ uri, duration, byteRange });
2053
+ fmp4Segments.push({
2054
+ uri,
2055
+ duration,
2056
+ byteRange,
2057
+ initSegmentUri: currentInitSegment?.uri
2058
+ // 关联当前初始化段
2059
+ });
2001
2060
  } else {
2002
2061
  segments.push({ uri, duration });
2003
2062
  }