|
1 | 1 | import { connectChat } from "./chat.js"
|
2 | 2 |
|
| 3 | +const chatToggler = document.getElementById("chat-toggler"); |
| 4 | +const chat = document.getElementById("chat"); |
| 5 | +const settingsToggler = document.getElementById("settings-toggler"); |
| 6 | +const settings = document.getElementById("settings"); |
| 7 | +const videoQuality = document.getElementById("video-quality"); |
| 8 | + |
3 | 9 | const pcConfig = { iceServers: [{ urls: "stun:stun.l.google.com:19302" }] };
|
4 | 10 | const whepEndpoint = `${window.location.origin}/api/whep`
|
5 | 11 | const videoPlayer = document.getElementById("videoplayer");
|
6 | 12 | const candidates = [];
|
7 | 13 | let patchEndpoint;
|
| 14 | +let layers = null; |
8 | 15 |
|
9 | 16 | async function sendCandidate(candidate) {
|
10 | 17 | const response = await fetch(patchEndpoint, {
|
@@ -58,26 +65,137 @@ async function connectMedia() {
|
58 | 65 | body: pc.localDescription.sdp
|
59 | 66 | });
|
60 | 67 |
|
61 |
| - if (response.status === 201) { |
62 |
| - patchEndpoint = response.headers.get("location"); |
63 |
| - console.log("Sucessfully initialized WHEP connection") |
64 |
| - |
65 |
| - } else { |
| 68 | + if (response.status !== 201) { |
66 | 69 | console.error(`Failed to initialize WHEP connection, status: ${response.status}`);
|
67 | 70 | return;
|
68 | 71 | }
|
69 | 72 |
|
| 73 | + patchEndpoint = response.headers.get("location"); |
| 74 | + console.log("Sucessfully initialized WHEP connection") |
| 75 | + |
70 | 76 | for (const candidate of candidates) {
|
71 | 77 | sendCandidate(candidate);
|
72 | 78 | }
|
73 | 79 |
|
74 | 80 | let sdp = await response.text();
|
75 | 81 | await pc.setRemoteDescription({ type: "answer", sdp: sdp });
|
| 82 | + |
| 83 | + connectServerEvents(); |
| 84 | +} |
| 85 | + |
| 86 | +async function connectServerEvents() { |
| 87 | + const response = await fetch(`${patchEndpoint}/sse`, { |
| 88 | + method: "POST", |
| 89 | + cache: "no-cache", |
| 90 | + headers: { "Content-Type": "application/json" }, |
| 91 | + body: JSON.stringify(["layers"]) |
| 92 | + }); |
| 93 | + |
| 94 | + if (response.status !== 201) { |
| 95 | + console.error(`Failed to fetch SSE endpoint, status: ${response.status}`); |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + const eventStream = response.headers.get("location"); |
| 100 | + const eventSource = new EventSource(eventStream); |
| 101 | + eventSource.onopen = (ev) => { |
| 102 | + console.log("EventStream opened", ev); |
| 103 | + } |
| 104 | + |
| 105 | + eventSource.onmessage = (ev) => { |
| 106 | + const data = JSON.parse(ev.data); |
| 107 | + updateLayers(data.layers) |
| 108 | + }; |
| 109 | + |
| 110 | + eventSource.onerror = (ev) => { |
| 111 | + console.log("EventStream closed", ev); |
| 112 | + eventSource.close(); |
| 113 | + }; |
| 114 | +} |
| 115 | + |
| 116 | +function updateLayers(new_layers) { |
| 117 | + // check if layers changed, if not, just return |
| 118 | + if (new_layers === null && layers === null) return; |
| 119 | + if ( |
| 120 | + layers !== null && |
| 121 | + new_layers !== null && |
| 122 | + new_layers.length === layers.length && |
| 123 | + new_layers.every((layer, i) => layer === layers[i]) |
| 124 | + ) return; |
| 125 | + |
| 126 | + if (new_layers === null) { |
| 127 | + videoQuality.appendChild(new Option("Disabled", null, true, true)); |
| 128 | + videoQuality.disabled = true; |
| 129 | + layers = null; |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + while (videoQuality.firstChild) { |
| 134 | + videoQuality.removeChild(videoQuality.firstChild); |
| 135 | + } |
| 136 | + |
| 137 | + if (new_layers === null) { |
| 138 | + videoQuality.appendChild(new Option("Disabled", null, true, true)); |
| 139 | + videoQuality.disabled = true; |
| 140 | + } else { |
| 141 | + videoQuality.disabled = false; |
| 142 | + new_layers |
| 143 | + .map((layer, i) => { |
| 144 | + var text = layer; |
| 145 | + if (layer == "h") text = "High"; |
| 146 | + if (layer == "m") text = "Medium"; |
| 147 | + if (layer == "l") text = "Low"; |
| 148 | + return new Option(text, layer, i == 0, layer == 0); |
| 149 | + }) |
| 150 | + .forEach(option => videoQuality.appendChild(option)) |
| 151 | + } |
| 152 | + |
| 153 | + layers = new_layers; |
| 154 | +} |
| 155 | + |
| 156 | +async function changeLayer(layer) { |
| 157 | + if (patchEndpoint) { |
| 158 | + const response = await fetch(`${patchEndpoint}/layer`, { |
| 159 | + method: "POST", |
| 160 | + cache: "no-cache", |
| 161 | + headers: { "Content-Type": "application/json" }, |
| 162 | + body: JSON.stringify({encodingId: layer}) |
| 163 | + }); |
| 164 | + |
| 165 | + if (response.status != 200) { |
| 166 | + console.warn("Changing layer failed", response) |
| 167 | + updateLayers(null); |
| 168 | + } |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +function toggleBox(element, other) { |
| 173 | + if (window.getComputedStyle(element).display === "none") { |
| 174 | + element.style.display = "flex"; |
| 175 | + other.style.display = "none"; |
| 176 | + |
| 177 | + // For screen's width lower than 1024, |
| 178 | + // eiter show video player or chat at the same time. |
| 179 | + if (window.innerWidth < 1024) { |
| 180 | + document.getElementById("videoplayer-wrapper").style.display = "none"; |
| 181 | + } |
| 182 | + } else { |
| 183 | + element.style.display = "none"; |
| 184 | + |
| 185 | + if (window.innerWidth < 1024) { |
| 186 | + document.getElementById("videoplayer-wrapper").style.display = "block"; |
| 187 | + } |
| 188 | + } |
76 | 189 | }
|
77 | 190 |
|
78 | 191 | export const Home = {
|
79 | 192 | mounted() {
|
80 | 193 | connectMedia()
|
81 | 194 | connectChat()
|
| 195 | + |
| 196 | + videoQuality.onchange = () => changeLayer(videoQuality.value) |
| 197 | + |
| 198 | + chatToggler.onclick = () => toggleBox(chat, settings); |
| 199 | + settingsToggler.onclick = () => toggleBox(settings, chat); |
82 | 200 | }
|
83 | 201 | }
|
0 commit comments