forked from pixeltris/TwitchAdSolutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
168 lines (168 loc) · 5.78 KB
/
utils.js
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
159
160
161
162
163
164
165
166
167
168
var defaultChannel = '';
var defaultName = 'test1';
var inputWidth = '100px';
var playerWidth = '800px';
var defaultVolume = 1;
//
const STATE_STOPPED = 1;
const STATE_RECORDING = 2;
const STATE_REPLAYING = 3;
const STATE_WAITING = 4;
var state = STATE_STOPPED;
//
var channelElement = null;
var nameElement = null;
var stateInfoElement = null;
var streamNormal = null;
var streamMini = null;
var streamOutput = null;
var streamAlt = null;
var streams = [];
//
function updateStateInfo(newState) {
if (newState) {
state = newState;
}
if (!streamNormal || !streamMini || !streamOutput || !streamAlt) {
return;
}
var stateStr = '';
switch (state) {
case STATE_STOPPED: stateStr = 'stopped'; break;
case STATE_RECORDING: stateStr = 'recording'; break;
case STATE_REPLAYING: stateStr = 'replaying'; break;
case STATE_WAITING: stateStr = 'waiting'; break;
}
stateInfoElement.textContent = ' state: ' + stateStr;
}
function updateStreamInfo(stream) {
stream.InfoElement.textContent = '[' + stream.StreamName + ']';
}
function setStreamSrc(stream, src) {
stream.hls.loadSource('/' + src + '/' + channelElement.value + '|' + nameElement.value);
stream.hls.attachMedia(stream);
}
function recordOrReplayStream(newState, type) {
if (!channelElement.value) {
alert('Channel name textbox is empty');
return;
}
stopStream();
updateStateInfo(STATE_WAITING);
fetch('/' + type + '-begin/' + channelElement.value + '|' + nameElement.value).then(async function(response) {
if (response.status == 200) {
var str = await response.text();
if (str) {
updateStateInfo(newState);
setStreamSrc(streamNormal, 'm3u8_normal');
setStreamSrc(streamMini, 'm3u8_mini');
setStreamSrc(streamOutput, 'm3u8_output');
setStreamSrc(streamAlt, 'm3u8_alt');
} else {
stopStream();
}
} else {
stopStream();
}
});
}
function recordStream() {
recordOrReplayStream(STATE_RECORDING, 'record');
}
function replayStream() {
alert('TODO');
//recordOrReplayStream(STATE_REPLAYING, 'replay');
}
function stopStream() {
for (var i = 0; i < streams.length; i++) {
streams[i].hls.stopLoad();
streams[i].pause();
}
updateStateInfo(STATE_STOPPED);
}
function createStreamElement(name) {
/////////////////////////////////////////
var containerElement = document.createElement('div');
containerElement.style.display = 'inline-block';
containerElement.style.width = 'auto';
document.body.appendChild(containerElement);
/////////////////////////////////////////
var infoElement = document.createElement('span');
containerElement.appendChild(infoElement);
containerElement.appendChild(document.createElement('br'));
/////////////////////////////////////////
var stream = document.createElement('video');
stream.style.maxWidth = playerWidth;
stream.style.width = playerWidth;
stream.InfoElement = infoElement;
stream.StreamName = name;
stream.autoplay = true;
stream.volume = defaultVolume;
stream.hls = new Hls();
containerElement.appendChild(stream);
/////////////////////////////////////////
streams.push(stream);
updateStreamInfo(stream);
return stream;
}
function onHlsLoaded() {
/////////////////////////////////////////
var label1 = document.createElement('span');
label1.textContent = 'channel:';
document.body.appendChild(label1);
/////////////////////////////////////////
channelElement = document.createElement('input');
channelElement.value = defaultChannel;
channelElement.style.width = inputWidth;
document.body.appendChild(channelElement);
/////////////////////////////////////////
var label2 = document.createElement('span');
label2.textContent = 'name:';
document.body.appendChild(label2);
/////////////////////////////////////////
nameElement = document.createElement('input');
nameElement.value = defaultName;
nameElement.style.width = inputWidth;
document.body.appendChild(nameElement);
/////////////////////////////////////////
var recordBtn = document.createElement('button');
recordBtn.textContent = 'record';
recordBtn.onclick = recordStream;
document.body.appendChild(recordBtn);
/////////////////////////////////////////
var replayBtn = document.createElement('button');
replayBtn.textContent = 'replay';
replayBtn.onclick = replayStream;
document.body.appendChild(replayBtn);
/////////////////////////////////////////
var stopBtn = document.createElement('button');
stopBtn.textContent = 'stop';
stopBtn.onclick = stopStream;
document.body.appendChild(stopBtn);
/////////////////////////////////////////
stateInfoElement = document.createElement('span');
document.body.appendChild(stateInfoElement);
/////////////////////////////////////////
document.body.appendChild(document.createElement('br'));
/////////////////////////////////////////
streamNormal = createStreamElement('normal');
streamMini = createStreamElement('mini');
streamOutput = createStreamElement('output');
streamAlt = createStreamElement('alt');
updateStateInfo();
}
function onContentLoaded() {
var script = document.createElement('script');
script.src = 'https://cdn.jsdelivr.net/npm/hls.js@latest';
script.onload = function() {
onHlsLoaded();
};
document.head.appendChild(script);
}
if (document.readyState === 'complete' || document.readyState === 'loaded' || document.readyState === 'interactive') {
onContentLoaded();
} else {
window.addEventListener('DOMContentLoaded', function() {
onContentLoaded();
});
}