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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
function Stream() {
// var self = this;
/********************************************************************************/
var __prev_state = false;
var __quality = 10;
var __normal_size = {width: 640, height: 480};
var __size_factor = 1;
var __init__ = function() {
$("stream-led").title = "Stream inactive";
var quality = 10;
$("stream-quality-select").innerHTML = "";
for (; quality <= 100; quality += 10) {
$("stream-quality-select").innerHTML += "<option value=\"" + quality + "\">" + quality + "%</option>";
}
tools.setOnClick($("stream-screenshot-button"), __clickScreenshotButton);
$("stream-quality-select").onchange = __changeQuality;
$("stream-size-slider").oninput = __resize;
$("stream-size-slider").onchange = __resize;
tools.setOnClick($("stream-reset-button"), __clickResetButton);
__startPoller();
};
/********************************************************************************/
// XXX: In current implementation we don't need this event because Stream() has own state poller
var __startPoller = function() {
var http = tools.makeRequest("GET", "/streamer/ping", function() {
if (http.readyState === 4) {
var response = (http.status === 200 ? JSON.parse(http.responseText) : null);
if (http.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-screenshot-button").disabled = true;
$("stream-quality-select").disabled = true;
$("stream-reset-button").disabled = true;
__updateStreamHeader(false);
} else if (http.status === 200) {
if (__prev_state) {
if (__normal_size != response.stream.resolution) {
__normal_size = response.stream.resolution;
__applySizeFactor();
}
} else {
__normal_size = response.stream.resolution;
__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-screenshot-button").disabled = false;
$("stream-quality-select").disabled = false;
$("stream-reset-button").disabled = false;
}
__updateStreamHeader(true);
}
}
});
setTimeout(__startPoller, 1000);
};
var __updateStreamHeader = function(online) {
var el_grab = document.querySelector("#stream-window-header .window-grab");
if (online) {
el_grab.innerHTML = "Stream – " + __normal_size.width + "x" + __normal_size.height;
} else {
el_grab.innerHTML = "Stream – offline";
}
};
var __clickScreenshotButton = function() {
var el_a = document.createElement("a");
el_a.href = "/streamer/snapshot";
el_a.download = "screenshot.jpg";
document.body.appendChild(el_a);
el_a.click();
setTimeout(() => document.body.removeChild(el_a), 0);
};
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) {
ui.error("Can't reset stream:<br>", http.responseText);
}
}
});
};
var __changeQuality = function() {
var quality = parseInt($("stream-quality-select").value);
if (__quality != quality) {
$("stream-quality-select").disabled = true;
var http = tools.makeRequest("POST", "/kvmd/streamer/set_params?quality=" + quality, function() {
if (http.readyState === 4) {
if (http.status !== 200) {
ui.error("Can't configure stream:<br>", http.responseText);
}
}
});
}
};
var __resize = function() {
var percent = $("stream-size-slider").value;
$("stream-size-value").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";
ui.showWindow($("stream-window"), false);
};
var __refreshImage = function() {
var http = tools.makeRequest("GET", "/kvmd/streamer", function() {
if (http.readyState === 4 && http.status === 200) {
var result = JSON.parse(http.responseText).result;
if (__quality != result.quality) {
tools.info("Quality changed:", result.quality);
document.querySelector("#stream-quality-select [value=\"" + result.quality + "\"]").selected = true;
__quality = result.quality;
}
__applySizeFactor();
$("stream-image").src = "/streamer/stream?t=" + new Date().getTime();
}
});
};
__init__();
}
|