@nikelroot/video-player 0.1.11 → 0.1.12

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.cjs CHANGED
@@ -854,6 +854,7 @@ var VideoPlayerBase = (props, ref) => {
854
854
  const liveStabilityMode = (0, import_react3.useRef)(false);
855
855
  const liveStallEvents = (0, import_react3.useRef)([]);
856
856
  const controlTime = (0, import_react3.useRef)(null);
857
+ const sourceLoadId = (0, import_react3.useRef)(0);
857
858
  const [playingState, setPlayingState] = (0, import_react3.useState)(!!defaultPlaying);
858
859
  const [mutedState, setMutedState] = (0, import_react3.useState)(!!defaultMuted);
859
860
  const [durationState, setDurationState] = (0, import_react3.useState)(0);
@@ -894,6 +895,18 @@ var VideoPlayerBase = (props, ref) => {
894
895
  controlTime.current = null;
895
896
  }
896
897
  }, []);
898
+ const resetMediaForSourceChange = (0, import_react3.useCallback)(() => {
899
+ clearTimers();
900
+ if (hlsRef.current) {
901
+ hlsRef.current.destroy();
902
+ hlsRef.current = null;
903
+ }
904
+ const video = videoRef.current;
905
+ if (!video) return;
906
+ video.pause();
907
+ video.removeAttribute("src");
908
+ video.load();
909
+ }, [clearTimers]);
897
910
  const setNextPlaying = (0, import_react3.useCallback)(
898
911
  (value) => {
899
912
  if (playingProp === void 0) setPlayingState(value);
@@ -1084,7 +1097,7 @@ var VideoPlayerBase = (props, ref) => {
1084
1097
  );
1085
1098
  }, [hlsConfig, hlsCredentials, live, liveHlsConfig, vodHlsConfig]);
1086
1099
  const loadVideo = (0, import_react3.useCallback)(
1087
- async (startAt = initialTime) => {
1100
+ async (startAt = initialTime, loadId = sourceLoadId.current) => {
1088
1101
  const startVideoRef = videoRef.current;
1089
1102
  if (!startVideoRef) return;
1090
1103
  let HlsCtor;
@@ -1094,13 +1107,16 @@ var VideoPlayerBase = (props, ref) => {
1094
1107
  reportError(error, { type: "hls-loader", src: videoSrc }, messages.streamLoadFailed);
1095
1108
  return;
1096
1109
  }
1110
+ if (loadId !== sourceLoadId.current) return;
1097
1111
  if (!videoRef.current || videoRef.current !== startVideoRef) return;
1098
1112
  const video = videoRef.current;
1099
1113
  if (!video) return;
1100
1114
  const h = new HlsCtor(resolvedHlsConfig());
1101
1115
  hlsRef.current = h;
1116
+ const isStaleLoad = () => loadId !== sourceLoadId.current || hlsRef.current !== h || videoRef.current !== video;
1102
1117
  h.on(HlsCtor.Events.ERROR, (_evt, data) => {
1103
1118
  var _a, _b;
1119
+ if (isStaleLoad()) return;
1104
1120
  const { type: errorType, details, fatal } = data;
1105
1121
  if (details === HlsCtor.ErrorDetails.BUFFER_APPEND_ERROR || details === "bufferAppendingError") return;
1106
1122
  if (!fatal) {
@@ -1121,7 +1137,7 @@ var VideoPlayerBase = (props, ref) => {
1121
1137
  hlsRef.current.destroy();
1122
1138
  hlsRef.current = null;
1123
1139
  }
1124
- void loadVideo(resumeTime2);
1140
+ void loadVideo(resumeTime2, loadId);
1125
1141
  }, 250);
1126
1142
  }
1127
1143
  return;
@@ -1141,7 +1157,7 @@ var VideoPlayerBase = (props, ref) => {
1141
1157
  hlsRef.current.destroy();
1142
1158
  hlsRef.current = null;
1143
1159
  }
1144
- void loadVideo(resumeTime);
1160
+ void loadVideo(resumeTime, loadId);
1145
1161
  }, backoffMs);
1146
1162
  return;
1147
1163
  }
@@ -1185,12 +1201,14 @@ var VideoPlayerBase = (props, ref) => {
1185
1201
  hlsRef.current.destroy();
1186
1202
  hlsRef.current = null;
1187
1203
  }
1188
- void loadVideo(resumeTime);
1204
+ void loadVideo(resumeTime, loadId);
1189
1205
  });
1190
1206
  h.on(HlsCtor.Events.MEDIA_ATTACHED, () => {
1207
+ if (isStaleLoad()) return;
1191
1208
  h.loadSource(videoSrc);
1192
1209
  });
1193
1210
  h.on(HlsCtor.Events.MANIFEST_PARSED, () => {
1211
+ if (isStaleLoad()) return;
1194
1212
  fatalReloadCount.current = 0;
1195
1213
  netRetryCount.current = 0;
1196
1214
  mediaRecoverCount.current = 0;
@@ -1220,14 +1238,15 @@ var VideoPlayerBase = (props, ref) => {
1220
1238
  videoSrc
1221
1239
  ]
1222
1240
  );
1223
- const loadVideoNative = (0, import_react3.useCallback)(() => {
1241
+ const loadVideoNative = (0, import_react3.useCallback)((loadId = sourceLoadId.current) => {
1242
+ if (loadId !== sourceLoadId.current) return;
1224
1243
  const video = videoRef.current;
1225
1244
  if (!video) return;
1226
1245
  video.src = videoSrc;
1227
1246
  video.currentTime = initialTime;
1228
1247
  if (autoPlay) void playAction();
1229
1248
  }, [autoPlay, initialTime, playAction, videoSrc]);
1230
- const loadVideoHls = (0, import_react3.useCallback)(async () => {
1249
+ const loadVideoHls = (0, import_react3.useCallback)(async (loadId = sourceLoadId.current) => {
1231
1250
  const video = videoRef.current;
1232
1251
  const canNativeHls = !!(video == null ? void 0 : video.canPlayType) && video.canPlayType("application/vnd.apple.mpegurl") !== "";
1233
1252
  let HlsCtor;
@@ -1237,13 +1256,14 @@ var VideoPlayerBase = (props, ref) => {
1237
1256
  reportError(error, { type: "hls-loader", src: videoSrc }, messages.streamLoadFailed);
1238
1257
  return;
1239
1258
  }
1259
+ if (loadId !== sourceLoadId.current) return;
1240
1260
  if (!videoRef.current || videoRef.current !== video) return;
1241
1261
  if (HlsCtor.isSupported()) {
1242
- void loadVideo(initialTime);
1262
+ void loadVideo(initialTime, loadId);
1243
1263
  return;
1244
1264
  }
1245
1265
  if (canNativeHls) {
1246
- loadVideoNative();
1266
+ loadVideoNative(loadId);
1247
1267
  return;
1248
1268
  }
1249
1269
  reportError(null, { type: "hls", src: videoSrc, reason: "HLS is not supported in this browser" }, messages.unsupported);
@@ -1368,34 +1388,30 @@ var VideoPlayerBase = (props, ref) => {
1368
1388
  }, [currentTimeProp, duration]);
1369
1389
  (0, import_react3.useEffect)(() => {
1370
1390
  if (!videoRef.current) return;
1391
+ const loadId = sourceLoadId.current + 1;
1392
+ sourceLoadId.current = loadId;
1371
1393
  clearErrorState();
1372
1394
  fatalReloadCount.current = 0;
1373
1395
  netRetryCount.current = 0;
1374
1396
  mediaRecoverCount.current = 0;
1375
1397
  liveStabilityMode.current = false;
1376
1398
  liveStallEvents.current = [];
1377
- if (hlsRef.current) {
1378
- hlsRef.current.destroy();
1379
- hlsRef.current = null;
1380
- }
1381
- if (sourceType === "native" || type === "video") loadVideoNative();
1382
- else if (sourceType === "hls") void loadVideo(initialTime);
1383
- else if (isLikelyHlsSource(videoSrc)) void loadVideoHls();
1384
- else loadVideoNative();
1399
+ resetMediaForSourceChange();
1400
+ if (sourceType === "native" || type === "video") loadVideoNative(loadId);
1401
+ else if (sourceType === "hls") void loadVideo(initialTime, loadId);
1402
+ else if (isLikelyHlsSource(videoSrc)) void loadVideoHls(loadId);
1403
+ else loadVideoNative(loadId);
1385
1404
  return () => {
1386
- clearTimers();
1387
- if (hlsRef.current) {
1388
- hlsRef.current.destroy();
1389
- hlsRef.current = null;
1390
- }
1405
+ sourceLoadId.current += 1;
1406
+ resetMediaForSourceChange();
1391
1407
  };
1392
1408
  }, [
1393
1409
  clearErrorState,
1394
- clearTimers,
1395
1410
  loadVideo,
1396
1411
  loadVideoHls,
1397
1412
  loadVideoNative,
1398
1413
  initialTime,
1414
+ resetMediaForSourceChange,
1399
1415
  reloadKey,
1400
1416
  reloadToken,
1401
1417
  sourceType,
package/dist/index.js CHANGED
@@ -825,6 +825,7 @@ var VideoPlayerBase = (props, ref) => {
825
825
  const liveStabilityMode = useRef2(false);
826
826
  const liveStallEvents = useRef2([]);
827
827
  const controlTime = useRef2(null);
828
+ const sourceLoadId = useRef2(0);
828
829
  const [playingState, setPlayingState] = useState(!!defaultPlaying);
829
830
  const [mutedState, setMutedState] = useState(!!defaultMuted);
830
831
  const [durationState, setDurationState] = useState(0);
@@ -865,6 +866,18 @@ var VideoPlayerBase = (props, ref) => {
865
866
  controlTime.current = null;
866
867
  }
867
868
  }, []);
869
+ const resetMediaForSourceChange = useCallback(() => {
870
+ clearTimers();
871
+ if (hlsRef.current) {
872
+ hlsRef.current.destroy();
873
+ hlsRef.current = null;
874
+ }
875
+ const video = videoRef.current;
876
+ if (!video) return;
877
+ video.pause();
878
+ video.removeAttribute("src");
879
+ video.load();
880
+ }, [clearTimers]);
868
881
  const setNextPlaying = useCallback(
869
882
  (value) => {
870
883
  if (playingProp === void 0) setPlayingState(value);
@@ -1055,7 +1068,7 @@ var VideoPlayerBase = (props, ref) => {
1055
1068
  );
1056
1069
  }, [hlsConfig, hlsCredentials, live, liveHlsConfig, vodHlsConfig]);
1057
1070
  const loadVideo = useCallback(
1058
- async (startAt = initialTime) => {
1071
+ async (startAt = initialTime, loadId = sourceLoadId.current) => {
1059
1072
  const startVideoRef = videoRef.current;
1060
1073
  if (!startVideoRef) return;
1061
1074
  let HlsCtor;
@@ -1065,13 +1078,16 @@ var VideoPlayerBase = (props, ref) => {
1065
1078
  reportError(error, { type: "hls-loader", src: videoSrc }, messages.streamLoadFailed);
1066
1079
  return;
1067
1080
  }
1081
+ if (loadId !== sourceLoadId.current) return;
1068
1082
  if (!videoRef.current || videoRef.current !== startVideoRef) return;
1069
1083
  const video = videoRef.current;
1070
1084
  if (!video) return;
1071
1085
  const h = new HlsCtor(resolvedHlsConfig());
1072
1086
  hlsRef.current = h;
1087
+ const isStaleLoad = () => loadId !== sourceLoadId.current || hlsRef.current !== h || videoRef.current !== video;
1073
1088
  h.on(HlsCtor.Events.ERROR, (_evt, data) => {
1074
1089
  var _a, _b;
1090
+ if (isStaleLoad()) return;
1075
1091
  const { type: errorType, details, fatal } = data;
1076
1092
  if (details === HlsCtor.ErrorDetails.BUFFER_APPEND_ERROR || details === "bufferAppendingError") return;
1077
1093
  if (!fatal) {
@@ -1092,7 +1108,7 @@ var VideoPlayerBase = (props, ref) => {
1092
1108
  hlsRef.current.destroy();
1093
1109
  hlsRef.current = null;
1094
1110
  }
1095
- void loadVideo(resumeTime2);
1111
+ void loadVideo(resumeTime2, loadId);
1096
1112
  }, 250);
1097
1113
  }
1098
1114
  return;
@@ -1112,7 +1128,7 @@ var VideoPlayerBase = (props, ref) => {
1112
1128
  hlsRef.current.destroy();
1113
1129
  hlsRef.current = null;
1114
1130
  }
1115
- void loadVideo(resumeTime);
1131
+ void loadVideo(resumeTime, loadId);
1116
1132
  }, backoffMs);
1117
1133
  return;
1118
1134
  }
@@ -1156,12 +1172,14 @@ var VideoPlayerBase = (props, ref) => {
1156
1172
  hlsRef.current.destroy();
1157
1173
  hlsRef.current = null;
1158
1174
  }
1159
- void loadVideo(resumeTime);
1175
+ void loadVideo(resumeTime, loadId);
1160
1176
  });
1161
1177
  h.on(HlsCtor.Events.MEDIA_ATTACHED, () => {
1178
+ if (isStaleLoad()) return;
1162
1179
  h.loadSource(videoSrc);
1163
1180
  });
1164
1181
  h.on(HlsCtor.Events.MANIFEST_PARSED, () => {
1182
+ if (isStaleLoad()) return;
1165
1183
  fatalReloadCount.current = 0;
1166
1184
  netRetryCount.current = 0;
1167
1185
  mediaRecoverCount.current = 0;
@@ -1191,14 +1209,15 @@ var VideoPlayerBase = (props, ref) => {
1191
1209
  videoSrc
1192
1210
  ]
1193
1211
  );
1194
- const loadVideoNative = useCallback(() => {
1212
+ const loadVideoNative = useCallback((loadId = sourceLoadId.current) => {
1213
+ if (loadId !== sourceLoadId.current) return;
1195
1214
  const video = videoRef.current;
1196
1215
  if (!video) return;
1197
1216
  video.src = videoSrc;
1198
1217
  video.currentTime = initialTime;
1199
1218
  if (autoPlay) void playAction();
1200
1219
  }, [autoPlay, initialTime, playAction, videoSrc]);
1201
- const loadVideoHls = useCallback(async () => {
1220
+ const loadVideoHls = useCallback(async (loadId = sourceLoadId.current) => {
1202
1221
  const video = videoRef.current;
1203
1222
  const canNativeHls = !!(video == null ? void 0 : video.canPlayType) && video.canPlayType("application/vnd.apple.mpegurl") !== "";
1204
1223
  let HlsCtor;
@@ -1208,13 +1227,14 @@ var VideoPlayerBase = (props, ref) => {
1208
1227
  reportError(error, { type: "hls-loader", src: videoSrc }, messages.streamLoadFailed);
1209
1228
  return;
1210
1229
  }
1230
+ if (loadId !== sourceLoadId.current) return;
1211
1231
  if (!videoRef.current || videoRef.current !== video) return;
1212
1232
  if (HlsCtor.isSupported()) {
1213
- void loadVideo(initialTime);
1233
+ void loadVideo(initialTime, loadId);
1214
1234
  return;
1215
1235
  }
1216
1236
  if (canNativeHls) {
1217
- loadVideoNative();
1237
+ loadVideoNative(loadId);
1218
1238
  return;
1219
1239
  }
1220
1240
  reportError(null, { type: "hls", src: videoSrc, reason: "HLS is not supported in this browser" }, messages.unsupported);
@@ -1339,34 +1359,30 @@ var VideoPlayerBase = (props, ref) => {
1339
1359
  }, [currentTimeProp, duration]);
1340
1360
  useEffect(() => {
1341
1361
  if (!videoRef.current) return;
1362
+ const loadId = sourceLoadId.current + 1;
1363
+ sourceLoadId.current = loadId;
1342
1364
  clearErrorState();
1343
1365
  fatalReloadCount.current = 0;
1344
1366
  netRetryCount.current = 0;
1345
1367
  mediaRecoverCount.current = 0;
1346
1368
  liveStabilityMode.current = false;
1347
1369
  liveStallEvents.current = [];
1348
- if (hlsRef.current) {
1349
- hlsRef.current.destroy();
1350
- hlsRef.current = null;
1351
- }
1352
- if (sourceType === "native" || type === "video") loadVideoNative();
1353
- else if (sourceType === "hls") void loadVideo(initialTime);
1354
- else if (isLikelyHlsSource(videoSrc)) void loadVideoHls();
1355
- else loadVideoNative();
1370
+ resetMediaForSourceChange();
1371
+ if (sourceType === "native" || type === "video") loadVideoNative(loadId);
1372
+ else if (sourceType === "hls") void loadVideo(initialTime, loadId);
1373
+ else if (isLikelyHlsSource(videoSrc)) void loadVideoHls(loadId);
1374
+ else loadVideoNative(loadId);
1356
1375
  return () => {
1357
- clearTimers();
1358
- if (hlsRef.current) {
1359
- hlsRef.current.destroy();
1360
- hlsRef.current = null;
1361
- }
1376
+ sourceLoadId.current += 1;
1377
+ resetMediaForSourceChange();
1362
1378
  };
1363
1379
  }, [
1364
1380
  clearErrorState,
1365
- clearTimers,
1366
1381
  loadVideo,
1367
1382
  loadVideoHls,
1368
1383
  loadVideoNative,
1369
1384
  initialTime,
1385
+ resetMediaForSourceChange,
1370
1386
  reloadKey,
1371
1387
  reloadToken,
1372
1388
  sourceType,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nikelroot/video-player",
3
- "version": "0.1.11",
3
+ "version": "0.1.12",
4
4
  "description": "Standalone React video player with HLS support.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",