Skip to content

Commit

Permalink
fix seeking for dash
Browse files Browse the repository at this point in the history
  • Loading branch information
howardchung committed Jul 26, 2024
1 parent 170d51e commit d695dcc
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
7 changes: 2 additions & 5 deletions src/components/App/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type MediasoupClient from 'mediasoup-client';
import type { MediaPlayerClass } from 'dashjs';
import axios from 'axios';
import React from 'react';
import {
Expand Down Expand Up @@ -74,7 +73,6 @@ declare global {
videoRefs: HTMLVideoElementDict;
videoPCs: PCDict;
webtorrent: WebTorrent.Instance | null;
dashPlayer: MediaPlayerClass | undefined;
};
}
}
Expand All @@ -84,7 +82,6 @@ window.watchparty = {
videoRefs: {},
videoPCs: {},
webtorrent: null,
dashPlayer: undefined,
};

interface AppProps {
Expand Down Expand Up @@ -451,7 +448,7 @@ export default class App extends React.Component<AppProps, AppState> {
}
this.YouTubeInterface.stopVideo();

window.watchparty.dashPlayer = undefined;
this.Player().clearDashState();
if (!this.isLocalStreamAFile) {
this.Player().clearState();
}
Expand Down Expand Up @@ -561,7 +558,7 @@ export default class App extends React.Component<AppProps, AppState> {
const dashPlayer = Dash.MediaPlayer().create();
this.setState({ isLiveStream: true });
dashPlayer.initialize(leftVideo, src);
window.watchparty.dashPlayer = dashPlayer;
this.Player().setDashState(dashPlayer);
} else if (isHls(src) && window.MediaSource) {
// Prefer using hls.js if MediaSource Extensions are supported
// otherwise fallback to native HLS support using video tag (i.e. iPhones)
Expand Down
30 changes: 24 additions & 6 deletions src/components/App/HTML.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
import { default as toWebVTT } from 'srt-webvtt';
import { Player } from './Player';
import { MediaPlayerClass } from 'dashjs';

export class HTML implements Player {
elId: string;
dashStartUTC: number;
dashPlayer: MediaPlayerClass | undefined;
constructor(elId: string) {
this.elId = elId;
this.dashStartUTC = Math.floor(Date.now() / 1000);
this.dashPlayer = undefined;
}

clearDashState = () => {
this.dashPlayer = undefined;
};

setDashState = (player: MediaPlayerClass) => {
this.dashPlayer = player;
this.dashStartUTC = Math.floor(Date.now() / 1000);
};

getVideoEl = (): HTMLMediaElement => {
return document.getElementById(this.elId) as HTMLMediaElement;
};

getCurrentTime = () => {
if (this.dashPlayer) {
return this.dashPlayer.time();
}
return this.getVideoEl()?.currentTime ?? 0;
};

getDuration = () => {
return (
window.watchparty.dashPlayer?.duration() ??
this.getVideoEl()?.duration ??
0
);
if (this.dashPlayer) {
return this.dashPlayer.duration();
}
return this.getVideoEl()?.duration ?? 0;
};

isMuted = () => {
Expand Down Expand Up @@ -62,7 +78,9 @@ export class HTML implements Player {
};

seekVideo = (time: number) => {
if (this.getVideoEl()) {
if (this.dashPlayer) {
this.dashPlayer.seek(time);
} else if (this.getVideoEl()) {
this.getVideoEl().currentTime = time;
}
};
Expand Down
4 changes: 4 additions & 0 deletions src/components/App/Player.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { MediaPlayerClass } from 'dashjs';

export abstract class Player {
public abstract getCurrentTime: () => number;
public abstract getDuration: () => number;
Expand All @@ -22,4 +24,6 @@ export abstract class Player {
public abstract getTimeRanges: () => { start: number; end: number }[];
public abstract setLoop: (loop: boolean) => void;
public abstract getVideoEl: () => HTMLMediaElement;
public abstract clearDashState: () => void;
public abstract setDashState: (player: MediaPlayerClass) => void;
}
4 changes: 4 additions & 0 deletions src/components/App/YouTube.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { MediaPlayerClass } from 'dashjs';
import { Player } from './Player';

export class YouTube implements Player {
watchPartyYTPlayer: YT.Player | null;
constructor(watchPartyYTPlayer: YT.Player | null) {
this.watchPartyYTPlayer = watchPartyYTPlayer;
}
clearDashState = () => {};
setDashState = (player: MediaPlayerClass) => {};

getCurrentTime = () => {
return this.watchPartyYTPlayer?.getCurrentTime() ?? 0;
};
Expand Down

0 comments on commit d695dcc

Please sign in to comment.