Skip to content

Commit

Permalink
make the fix more specific to safari to avoid edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
Florent-Bouisset committed Sep 18, 2024
1 parent e3c17c8 commit 43d01f5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
10 changes: 5 additions & 5 deletions src/main_thread/init/directfile_content_initializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ export default class DirectFileContentInitializer extends ContentInitializer {
function getDirectFileInitialTime(
mediaElement: IMediaElement,
startAt?: IInitialTimeOptions,
): number {
): number | undefined {
if (isNullOrUndefined(startAt)) {
return 0;
}
Expand All @@ -275,15 +275,15 @@ function getDirectFileInitialTime(

if (mediaElement.seekable.length > 0) {
const lastSegmentEnd = mediaElement.seekable.end(mediaElement.seekable.length - 1);
if(isFinite(lastSegmentEnd)) {
if (isFinite(lastSegmentEnd)) {
return Math.max(0, lastSegmentEnd + startAt.fromLastPosition);
}
}
log.warn(
"startAt.fromLastPosition set but no known duration, " + "beginning at 0.",
"startAt.fromLastPosition set but no known duration, " +
"it may be too soon to seek",
);
return 0;

return undefined;
} else if (typeof startAt.fromLivePosition === "number") {
const livePosition =
mediaElement.seekable.length > 0 ? mediaElement.seekable.end(0) : duration;
Expand Down
23 changes: 17 additions & 6 deletions src/main_thread/init/utils/initial_seek_and_play.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import type { IMediaElement } from "../../../compat/browser_compatibility_types";
import { isSafariDesktop, isSafariMobile } from "../../../compat/browser_detection";
import canSeekDirectlyAfterLoadedMetadata from "../../../compat/can_seek_directly_after_loaded_metadata";
import shouldValidateMetadata from "../../../compat/should_validate_metadata";
import { MediaError } from "../../../errors";
Expand Down Expand Up @@ -71,7 +72,7 @@ export default function performInitialSeekAndPlay(
}: {
mediaElement: IMediaElement;
playbackObserver: IMediaElementPlaybackObserver;
startTime: number | (() => number);
startTime: number | (() => number | undefined);
mustAutoPlay: boolean;
isDirectfile: boolean;
onWarning: (err: IPlayerError) => void;
Expand Down Expand Up @@ -107,18 +108,28 @@ export default function performInitialSeekAndPlay(
if (!isDirectfile || typeof startTime === "number") {
const initiallySeekedTime =
typeof startTime === "number" ? startTime : startTime();
if (initiallySeekedTime !== 0) {
if (initiallySeekedTime !== 0 && initiallySeekedTime !== undefined) {
performInitialSeek(initiallySeekedTime);
}
waitForSeekable();
} else {
playbackObserver.listen(
(obs, stopListening) => {
if (obs.readyState > 1) {
const initiallySeekedTime =
typeof startTime === "number" ? startTime : startTime();
const isSafari = isSafariDesktop || isSafariMobile;

// For most browsers, readyState 1 is sufficient for seeking.
// However, on Safari, readyState 1 may occur too early, leading to cases where
// mediaElement.duration or mediaElement.seekable.end(0) are not yet available
// and could return `Infinity`, causing issues with seeking.
// To handle this, we require readyState 2 on Safari before attempting to seek.
const minReadyStateForSeeking =
isSafari && initiallySeekedTime === undefined ? 2 : 1;
if (obs.readyState >= minReadyStateForSeeking) {
stopListening();
const initiallySeekedTime =
typeof startTime === "number" ? startTime : startTime();
if (initiallySeekedTime !== 0) {

if (initiallySeekedTime !== 0 && initiallySeekedTime !== undefined) {
if (canSeekDirectlyAfterLoadedMetadata) {
performInitialSeek(initiallySeekedTime);
} else {
Expand Down

0 comments on commit 43d01f5

Please sign in to comment.