- commit
- 5395944228d0d0012bc258a37b4547aa93f854d7
- parent
- 59abee892ace482053660c49fe69758faf6d19e8
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2020-04-01 07:05
only create new Stream when tracks are added/removed
Diffstat
M | www/rtc/rtc.js | 83 | ++++++++++++++++++++++++++++--------------------------------- |
1 files changed, 38 insertions, 45 deletions
diff --git a/www/rtc/rtc.js b/www/rtc/rtc.js
@@ -141,67 +141,60 @@ 141 141 var controls = document.querySelector('.rtc-controls'); 142 142 143 143 var updateStreams = async function(event) {144 -1 var newStream = new MediaStream();145 -1 var kind = event.target.name === 'screen' ? 'video' : event.target.name;146 -1 var stream;-1 144 var tracks = localVideo.srcObject.getTracks(); -1 145 var kind = event.target.name === 'audio' ? 'audio' : 'video'; 147 146148 -1 if (controls.video.checked && controls.screen.checked) {-1 147 if (!event.target.checked) { -1 148 tracks.forEach(track => { -1 149 if (track.kind === kind) { -1 150 track.enabled = false; -1 151 } -1 152 }); -1 153 } else if ( -1 154 tracks.filter(t => t.kind === kind).length && -1 155 !(controls.video.checked && controls.screen.checked) -1 156 ) { -1 157 tracks.forEach(track => { -1 158 if (track.kind === kind) { -1 159 track.enabled = true; -1 160 } -1 161 }); -1 162 } else { -1 163 var newStream = new MediaStream(); -1 164 var stream; 149 165 try { 150 166 if (event.target.name === 'screen') { 151 167 stream = await navigator.mediaDevices.getDisplayMedia();152 -1 controls.video.checked = false;153 168 } else {154 -1 stream = await navigator.mediaDevices.getUserMedia({video: true});155 -1 controls.screen.checked = false;-1 169 stream = await navigator.mediaDevices.getUserMedia({[kind]: true}); 156 170 } 157 171 } catch (err) { 158 172 event.target.checked = false; 159 173 return; 160 174 } 161 175162 -1 localVideo.srcObject.getVideoTracks().forEach(track => {163 -1 track.stop();164 -1 });165 -1 localVideo.srcObject.getAudioTracks().forEach(track => {166 -1 newStream.addTrack(track);167 -1 });168 -1 stream.getVideoTracks().forEach(track => {169 -1 newStream.addTrack(track);170 -1 });171 -1 } else {172 -1 var exists = false;173 -1 localVideo.srcObject.getTracks().forEach(track => {174 -1 newStream.addTrack(track);-1 176 if (event.target.name === 'video') { -1 177 controls.screen.checked = false; -1 178 localVideo.classList.add('mirrored'); -1 179 } else if (event.target.name === 'screen') { -1 180 controls.video.checked = false; -1 181 localVideo.classList.remove('mirrored'); -1 182 } -1 183 -1 184 tracks.forEach(track => { 175 185 if (track.kind === kind) {176 -1 track.enabled = event.target.checked;177 -1 exists = true;-1 186 track.stop(); -1 187 } else { -1 188 newStream.addTrack(track); 178 189 } 179 190 });180 -1 if (!exists && event.target.checked) {181 -1 try {182 -1 if (event.target.name === 'screen') {183 -1 stream = await navigator.mediaDevices.getDisplayMedia();184 -1 } else {185 -1 stream = await navigator.mediaDevices.getUserMedia({[kind]: true});186 -1 }187 -1 } catch (err) {188 -1 event.target.checked = false;189 -1 return;190 -1 }191 -1 stream.getTracks().forEach(track => {192 -1 newStream.addTrack(track);193 -1 });194 -1 }195 -1 }-1 191 stream.getTracks().forEach(track => { -1 192 newStream.addTrack(track); -1 193 }); 196 194197 -1 if (event.target.name === 'video') {198 -1 localVideo.classList.add('mirrored');199 -1 } else if (event.target.name === 'screen') {200 -1 localVideo.classList.remove('mirrored');-1 195 localVideo.srcObject = newStream; -1 196 updateConnections(); 201 197 }202 -1203 -1 localVideo.srcObject = newStream;204 -1 updateConnections();205 198 }; 206 199 207 200 document.querySelector('.rtc-controls').addEventListener('change', updateStreams);