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
153
154
155
156
157
158
|
/*****************************************************************************
# #
# KVMD - The main PiKVM daemon. #
# #
# Copyright (C) 2018-2024 Maxim Devaev <[email protected]> #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
# #
*****************************************************************************/
"use strict";
import {tools, $} from "../tools.js";
export function MjpegStreamer(__setActive, __setInactive, __setInfo) {
var self = this;
/************************************************************************/
var __key = tools.makeId();
var __id = "";
var __fps = -1;
var __state = null;
var __timer = null;
var __timer_retries = 0;
/************************************************************************/
self.getName = () => "HTTP MJPEG";
self.getMode = () => "mjpeg";
self.getResolution = function() {
let el = $("stream-image");
return {
"real_width": el.naturalWidth,
"real_height": el.naturalHeight,
"view_width": el.offsetWidth,
"view_height": el.offsetHeight,
};
};
self.ensureStream = function(state) {
if (state) {
__state = state;
__findId();
if (__id.length > 0 && __id in __state.stream.clients_stat) {
__setStreamActive();
__stopChecking();
} else {
__ensureChecking();
}
} else {
__stopChecking();
__setStreamInactive();
}
};
self.stopStream = function() {
self.ensureStream(null);
let blank = "/share/png/blank-stream.png";
if (!String.prototype.endsWith.call($("stream-image").src, blank)) {
$("stream-image").src = blank;
}
};
var __setStreamActive = function() {
let old_fps = __fps;
__fps = __state.stream.clients_stat[__id].fps;
if (old_fps < 0) {
__logInfo("Active");
__setActive();
}
__setInfo(true, __state.source.online, `${__fps} fps dynamic`);
};
var __setStreamInactive = function() {
let old_fps = __fps;
__key = tools.makeId();
__id = "";
__fps = -1;
__state = null;
if (old_fps >= 0) {
__logInfo("Inactive");
__setInactive();
__setInfo(false, false, "");
}
};
var __ensureChecking = function() {
if (!__timer) {
__timer_retries = 10;
__timer = setInterval(__checkStream, 100);
}
};
var __stopChecking = function() {
if (__timer) {
clearInterval(__timer);
}
__timer = null;
__timer_retries = 0;
};
var __findId = function() {
let sc = tools.cookies.get("stream_client");
if (__id.length === 0 && sc && sc.startsWith(__key + "/")) {
__logInfo("Found acceptable stream_client cookie:", sc);
__id = sc.slice(sc.indexOf("/") + 1);
}
};
var __checkStream = function() {
__findId();
if (__id.legnth > 0 && __id in __state.stream.clients_stat) {
__setStreamActive();
__stopChecking();
} else if (__id.length > 0 && __timer_retries >= 0) {
__timer_retries -= 1;
} else {
__setStreamInactive();
__stopChecking();
let path = `/streamer/stream?key=${__key}`;
if (tools.browser.is_safari || tools.browser.is_ios) {
// uStreamer fix for WebKit
__logInfo("Using dual_final_frames=1 to fix WebKit bugs");
path += "&dual_final_frames=1";
} else if (tools.browser.is_chrome || tools.browser.is_blink) {
// uStreamer fix for Blink https://bugs.chromium.org/p/chromium/issues/detail?id=527446
__logInfo("Using advance_headers=1 to fix Blink bugs");
path += "&advance_headers=1";
}
__logInfo("Refreshing ...");
$("stream-image").src = path;
}
};
var __logInfo = (...args) => tools.info("Stream [MJPEG]:", ...args);
}
|