aboutsummaryrefslogtreecommitdiffstats
path: root/src/client/players/Raw.hx
blob: cd01a42698a89ab776bd7fe7027cd95898b7ab79 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package client.players;

import haxe.Timer;
import js.html.Element;
import js.html.VideoElement;
import js.Browser.document;
import client.Main.ge;
import Types.VideoData;
import Types.VideoItem;

class Raw implements IPlayer {

	static var controlsHider:Timer;
	final main:Main;
	final player:Player;
	final playerEl:Element = ge("#ytapiplayer");
	var playAllowed = true;
	var video:VideoElement;

	public function new(main:Main, player:Player) {
		this.main = main;
		this.player = player;
	}

	public function getVideoData(url:String, callback:(data:VideoData)->Void):Void {
		var title = url.substr(url.lastIndexOf('/') + 1);
		final matchName = ~/^(.+)\./;
		if (matchName.match(title)) title = matchName.matched(1);
		else title = Lang.get("rawVideo");

		final video = document.createVideoElement();
		video.src = url;
		video.onerror = e -> {
			if (playerEl.contains(video)) playerEl.removeChild(video);
			callback({duration: 0});
		}
		video.onloadedmetadata = () -> {
			if (playerEl.contains(video)) playerEl.removeChild(video);
			callback({
				duration: video.duration,
				title: title
			});
		}
		Utils.prepend(playerEl, video);
	}

	public function loadVideo(item:VideoItem):Void {
		video = document.createVideoElement();
		video.id = "videoplayer";
		final url = main.tryLocalIp(item.url);
		video.src = url;
		video.controls = true;
		final isTouch = untyped __js__("'ontouchstart' in window");
		if (controlsHider != null) controlsHider.stop();
		if (!isTouch) controlsHider = Timer.delay(() -> {
			video.controls = false;
		}, 3000);
		video.onmousemove = e -> {
			controlsHider.stop();
			video.controls = true;
			video.onmousemove = null;
		}
		video.oncanplaythrough = player.onCanBePlayed;
		video.onseeking = player.onSetTime;
		video.onplay = e -> {
			playAllowed = true;
			player.onPlay();
		}
		video.onpause = player.onPause;
		video.onratechange = player.onRateChange;
		playerEl.appendChild(video);
	}

	public function removeVideo():Void {
		if (video == null) return;
		playerEl.removeChild(video);
		video = null;
	}

	public function play():Void {
		if (video == null) return;
		if (!playAllowed) return;
		final promise = video.play();
		if (promise == null) return;
		promise.catchError(error -> {
			// Do not try to play video anymore or Chromium will hide play button
			playAllowed = false;
		});
	}

	public function pause():Void {
		if (video == null) return;
		video.pause();
	}

	public function getTime():Float {
		if (video == null) return 0;
		return video.currentTime;
	}

	public function setTime(time:Float):Void {
		if (video == null) return;
		video.currentTime = time;
	}

	public function getPlaybackRate():Float {
		return video.playbackRate;
	}

	public function setPlaybackRate(rate:Float):Void {
		video.playbackRate = rate;
	}

}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage