Skip to content

Commit

Permalink
Video item fixes
Browse files Browse the repository at this point in the history
- Fix youtube fallback, `videoItem.url` is now immutable
- JsApi.setVideoSrc now updates to supported player based on `url` arg
- Fix auto-pause when video is ended (with `requestLeaderOnPause`)
  • Loading branch information
RblSb committed Apr 29, 2024
1 parent 9d844bb commit 5d2375c
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 63 deletions.
33 changes: 28 additions & 5 deletions build/server.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 45 additions & 23 deletions res/client.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 23 additions & 7 deletions src/Types.hx
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,30 @@ typedef Message = {
time:String
}

@:using(Types.VideoItemTools)
typedef VideoItem = {
url:String,
title:String,
author:String,
duration:Float,
?subs:String,
isTemp:Bool,
isIframe:Bool
/** Immutable, used as identifier for skipping / removing items **/
final url:String;
var title:String;
var author:String;
var duration:Float;
var ?subs:String;
var isTemp:Bool;
var isIframe:Bool;
}

private class VideoItemTools {
public static function withUrl(item:VideoItem, url:String):VideoItem {
return {
url: url,
title: item.title,
author: item.author,
duration: item.duration,
subs: item.subs,
isTemp: item.isTemp,
isIframe: item.isIframe
};
}
}

typedef FlashbackItem = {
Expand Down
4 changes: 2 additions & 2 deletions src/VideoList.hx
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ class VideoList {
return items.length;
}

public var currentItem(get, never):VideoItem;
public var currentItem(get, never):Null<VideoItem>;

inline function get_currentItem():Null<VideoItem> {
return items[pos];
}

public inline function getItem(i:Int):VideoItem {
public inline function getItem(i:Int):Null<VideoItem> {
return items[i];
}

Expand Down
4 changes: 2 additions & 2 deletions src/client/JsApi.hx
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ class JsApi {
}

@:expose
public static function setVideoSrc(src:String):Void {
player.changeVideoSrc(src);
public static function setVideoSrc(url:String):Void {
player.changeVideoSrc(url);
}

/** Returns current page hostname (domain without protocol) **/
Expand Down
37 changes: 20 additions & 17 deletions src/client/Player.hx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class Player {
setItemElementType(el, videoList.getItem(pos).isTemp);
}

public function getCurrentItem():VideoItem {
public function getCurrentItem():Null<VideoItem> {
return videoList.currentItem;
}

Expand Down Expand Up @@ -121,10 +121,7 @@ class Player {
public function setVideo(i:Int):Void {
if (!main.isSyncActive) return;
final item = videoList.getItem(i);
var currentPlayer = players.find(p -> p.isSupportedLink(item.url));
if (currentPlayer != null) setPlayer(currentPlayer);
else if (item.isIframe) setPlayer(iframePlayer);
else setPlayer(rawPlayer);
setSupportedPlayer(item.url, item.isIframe);

removeActiveLabel(videoList.pos);
videoList.setPos(i);
Expand All @@ -140,19 +137,18 @@ class Player {
ge("#currenttitle").textContent = item.title;
}

public function changeVideoSrc(src:String):Void {
function setSupportedPlayer(url:String, isIframe:Bool):Void {
final currentPlayer = players.find(p -> p.isSupportedLink(url));
if (currentPlayer != null) setPlayer(currentPlayer);
else if (isIframe) setPlayer(iframePlayer);
else setPlayer(rawPlayer);
}

public function changeVideoSrc(url:String):Void {
if (!main.isVideoEnabled) return;
if (player == null) return;
final item = videoList.currentItem ?? return;
player.loadVideo({
url: src,
title: item.title,
author: item.author,
duration: item.duration,
subs: item.subs,
isTemp: item.isTemp,
isIframe: item.isIframe
});
final item:VideoItem = videoList.currentItem ?? return;
setSupportedPlayer(url, item.isIframe);
player.loadVideo(item.withUrl(url));
}

public function removeVideo():Void {
Expand Down Expand Up @@ -191,6 +187,13 @@ class Player {
}

public function onPause():Void {
final item = videoList.currentItem ?? return;
// do not send pause if video is ended
if (getTime() >= item.duration - 0.01) return;
// youtube raw fallback has around one second difference from rounded youtube duration
if (player == rawPlayer && youtube.isSupportedLink(item.url)) {
if (getTime() >= item.duration - 1) return;
}
final hasAutoPause = main.hasLeaderOnPauseRequest() && videoList.length > 0
&& getTime() > 1;
if (hasAutoPause && !main.hasLeader()) {
Expand Down
5 changes: 2 additions & 3 deletions src/client/players/Youtube.hx
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ class Youtube implements IPlayer {
onError: e -> {
// TODO message error codes
trace('Error ${e.data}');
final item = player.getCurrentItem() ?? return;
rawSourceFallback(item.url);
}
}
Expand All @@ -258,9 +259,7 @@ class Youtube implements IPlayer {
trace(info);
return;
};
final item = player.getCurrentItem();
item.url = format.url;
player.refresh();
player.changeVideoSrc(format.url);
});
main.send({
type: GetYoutubeVideoInfo,
Expand Down
10 changes: 6 additions & 4 deletions src/server/Main.hx
Original file line number Diff line number Diff line change
Expand Up @@ -612,16 +612,18 @@ class Main {
if (!data.addVideo.atEnd && !checkPermission(client, ChangeOrderPerm)) {
data.addVideo.atEnd = true;
}
final item = data.addVideo.item;
var item = data.addVideo.item;
item.author = client.name;
final local = '$localIp:$port';
if (item.url.contains(local)) {
item.url = item.url.replace(local, '$globalIp:$port');
final localIpPort = '$localIp:$port';
if (item.url.contains(localIpPort)) {
final newUrl = item.url.replace(localIpPort, '$globalIp:$port');
item = item.withUrl(newUrl);
}
if (videoList.exists(i -> i.url == item.url)) {
serverMessage(client, "videoAlreadyExistsError");
return;
}
data.addVideo.item = item;
videoList.addItem(item, data.addVideo.atEnd);
broadcast(data);
// Initial timer start if VideoLoaded is not happen
Expand Down

0 comments on commit 5d2375c

Please sign in to comment.