summaryrefslogtreecommitdiff
path: root/kvmd/web/js/stream.js
blob: f1c5d950af4c97f85c458fc838aa5298ef8ac7e1 (plain)
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
function Stream() {
	// var self = this;

	/********************************************************************************/

	var __prev_state = false;
	var __normal_size = {width: 640, height: 480};
	var __size_factor = 1;

	var __init__ = function() {
		$("stream-led").title = "Stream inactive";

		$("stream-reset-button").onclick = __clickResetButton;
		$("stream-size-slider").oninput = __resize;
		$("stream-size-slider").onchange = __resize;

		__startPoller();
	};

	/********************************************************************************/

	var __startPoller = function() {
		var http = tools.makeRequest("GET", "/streamer/?action=snapshot", function() {
			if (http.readyState === 2 || http.readyState === 4) {
				var status = http.status;
				http.onreadystatechange = null;
				http.abort();
				if (status !== 200) {
					tools.info("Refreshing stream ...");
					__prev_state = false;
					$("stream-image").className = "stream-image-inactive";
					$("stream-box").classList.add("stream-box-inactive");
					$("stream-led").className = "led-off";
					$("stream-led").title = "Stream inactive";
					$("stream-reset-button").disabled = true;
				} else if (!__prev_state) {
					__refreshImage();
					__prev_state = true;
					$("stream-image").className = "stream-image-active";
					$("stream-box").classList.remove("stream-box-inactive");
					$("stream-led").className = "led-on";
					$("stream-led").title = "Stream is active";
					$("stream-reset-button").disabled = false;
				}
			}
		});
		setTimeout(__startPoller, 2000);
	};

	var __clickResetButton = function() {
		$("stream-reset-button").disabled = true;
		var http = tools.makeRequest("POST", "/kvmd/streamer/reset", function() {
			if (http.readyState === 4) {
				if (http.status !== 200) {
					alert("Can't reset stream:", http.responseText);
				}
			}
		});
	};

	var __resize = function() {
		var percent = $("stream-size-slider").value;
		$("stream-size-counter").innerHTML = percent + "%";
		__size_factor = percent / 100;
		__applySizeFactor();
	};

	var __applySizeFactor = function() {
		var el_stream_image = $("stream-image");
		el_stream_image.style.width = __normal_size.width * __size_factor + "px";
		el_stream_image.style.height = __normal_size.height * __size_factor + "px";
	};

	var __refreshImage = function() {
		var http = tools.makeRequest("GET", "/kvmd/streamer", function() {
			if (http.readyState === 4 && http.status === 200) {
				__normal_size = JSON.parse(http.responseText).result.size;
				__applySizeFactor();
				$("stream-image").src = "/streamer/?action=stream&time=" + new Date().getTime();
			}
		});
	};

	__init__();
}