-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket.html
186 lines (165 loc) · 5.55 KB
/
websocket.html
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="js/obs-websocket.js"></script>
</head>
<body>
<h1>Inserts M3U</h1>
<div id="inserts">
<input type="file" name="insertsFile" id="insertsFile">
<ul id="insertList"></ul>
</div>
<h1>Main Program M3U</h1>
<div id="mainProgram">
<input type="file" name="mainsFile" id="mainsFile">
<ul id="mainsList"></ul>
</div>
<h1>Scene / Source Settings</h1>
<div id="sceneSourceSettings">
Main Program Scene: <input id="mainProgramScene" placeholder="" value="Scene 2"></br>
Main Program Source: <input id="mainProgramSource" placeholder="" value="Media SRC B"></br>
</hr>
Inserts Program Scene: <input id="insertsProgramScene" placeholder="" value="Scene 1"></br>
Inserts Program Source: <input id="insertsProgramSource" placeholder="" value="Media SRC A"></br>
</div>
<h1>Connect</h1>
<input id="address" placeholder="Address" value="127.0.0.1:4444"></br>
<input id="password" type="password"></br>
<button id="address_button">Connect</button>
<h1>Timer</h1>
<div id="timer"></div>
<script>
const obs = new OBSWebSocket();
const mainScene = document.getElementById('mainProgramScene');
const mainSource = document.getElementById('mainProgramSource');
const insertScene = document.getElementById('insertsProgramScene');
const insertSource = document.getElementById('insertsProgramSource');
var insertsMap = new Map();
var mainsMap = new Map();
document.getElementById('insertsFile').onchange = function(){
const insertsList = document.getElementById('insertList');
while( insertsList.firstChild ){
insertsList.removeChild( insertsList.firstChild );
}
var file = this.files[0];
var reader = new FileReader();
insertsMap = new Map();
reader.onload = function(progressEvent){
var lines = this.result.split('\n');
for(var line = 0; line < lines.length; line++){
var str = lines[line];
var file = '';
var duration = 0;
if (str.startsWith('#EXTINF:')) {
duration = parseInt(str.substr(8, (str.indexOf(',')-8)), 10);
file = lines[line+1];
file = file.replace("\r","");
insertsMap.set(file,duration);
var result = file + ' - ' + duration;
var li = document.createElement("li");
li.appendChild(document.createTextNode(result));
insertsList.appendChild(li);
}
}
};
reader.readAsText(file);
};
document.getElementById('mainsFile').onchange = function(){
const mainsList = document.getElementById('mainsList');
while( mainsList.firstChild ){
mainsList.removeChild( mainsList.firstChild );
}
var file = this.files[0];
var reader = new FileReader();
mainsMap = new Map();
reader.onload = function(progressEvent){
var lines = this.result.split('\n');
for(var line = 0; line < lines.length; line++){
var str = lines[line];
var file = '';
var duration = 0;
if (str.startsWith('#EXTINF:')) {
duration = parseInt(str.substr(8, (str.indexOf(',')-8)), 10);
file = lines[line+1];
file = file.replace("\r","");
mainsMap.set(file,duration);
var result = file + ' - ' + duration;
var li = document.createElement("li");
li.appendChild(document.createTextNode(result));
mainsList.appendChild(li);
}
}
};
reader.readAsText(file);
};
var timerCount = 0;
var isPlaying = false;
var currentItemDuration = 0;
var currentScene = 3;
var currAd = 0;
setInterval(
() => {
const timerDiv = document.getElementById('timer');
if (isPlaying) {
timerCount++;
timerDiv.innerHTML = timerCount;
if (timerCount >= currentItemDuration) {
if (currentScene == 2) {
//Play a PreRoll
currentScene = 1;
currAd++;
for (var insItem = 0; insItem < insertsMap.size; insItem++) {
if (currAd % insertsMap.size == insItem) {
var mapCount = 0;
insertsMap.forEach(function(value, key) {
if (mapCount == insItem) {
obs.send('SetSourceSettings', {'sourceName': insertSource.value,'sourceSettings': {'loop': false,'playlist': [{'value':key}]}});
currentItemDuration = value;
}
mapCount++;
});
}
}
obs.send('SetCurrentScene', { 'scene-name': insertScene.value });
} else {
//Play a File
currentScene = 2;
currAd++;
for (var insItem = 0; insItem < mainsMap.size; insItem++) {
if (currAd % mainsMap.size == insItem) {
var mapCount = 0;
mainsMap.forEach(function(value, key) {
if (mapCount == insItem) {
obs.send('SetSourceSettings', {'sourceName': mainSource.value,'sourceSettings': {'loop': false,'playlist': [{'value':key}]}});
currentItemDuration = value;
}
mapCount++;
});
}
}
obs.send('SetCurrentScene', { 'scene-name': mainScene.value });
}
timerCount = 0;
isPlaying = true;
}
} else {
timerDiv.innerHTML = "Not Active";
}
},
1000
);
document.getElementById('address_button').addEventListener('click', e => {
const address = document.getElementById('address').value;
const password = document.getElementById('password').value;
obs.connect({address: address, password: password});
});
obs.on('ConnectionOpened', () => {
obs.send('SetCurrentScene', { 'scene-name': insertScene.value });
currentScene = 1;
currentItemDuration = 0;
timerCount = 0;
isPlaying = true;
});
</script>
</body>
</html>