rtc

minimal webrtc client
git clone https://git.ce9e.org/rtc.git

commit
4e7b6ab150f8f9b24ec7059120ac302586d9da36
parent
8d25536fb1a853974eea8b6af014cd706b8473e9
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2020-03-24 06:12
rtc: create new Stream instead of modifying existing one

Diffstat

M www/rtc/rtc.js 26 ++++++++++++++++----------

1 files changed, 16 insertions, 10 deletions


diff --git a/www/rtc/rtc.js b/www/rtc/rtc.js

@@ -133,41 +133,47 @@
  133   133 	}
  134   134 
  135   135 	var updateStreams = async function(event) {
  136    -1 		var oldTracks, getStream, other;
   -1   136 		var newStream = new MediaStream();
   -1   137 		var kind, getStream, other;
  137   138 
  138   139 		if (event.target.name === 'audio') {
  139    -1 			oldTracks = localVideo.srcObject.getAudioTracks();
   -1   140 			kind = 'audio';
  140   141 			getStream = () => navigator.mediaDevices.getUserMedia({audio: true});
  141   142 		} else if (event.target.name === 'video') {
  142    -1 			oldTracks = localVideo.srcObject.getVideoTracks();
   -1   143 			kind = 'video';
  143   144 			getStream = () => navigator.mediaDevices.getUserMedia({video: true});
  144   145 			other = document.querySelector('[name="screen"]');
  145   146 		} else {
  146    -1 			oldTracks = localVideo.srcObject.getVideoTracks();
   -1   147 			kind = 'video';
  147   148 			getStream = () => navigator.mediaDevices.getDisplayMedia();
  148   149 			other = document.querySelector('[name="video"]');
  149   150 		}
  150   151 
  151    -1 		oldTracks.forEach(track => {
  152    -1 			track.stop()
  153    -1 			localVideo.srcObject.removeTrack(track);
   -1   152 		localVideo.srcObject.getTracks().forEach(track => {
   -1   153 			if (track.kind === kind) {
   -1   154 				track.stop();
   -1   155 			} else {
   -1   156 				newStream.addTrack(track);
   -1   157 			}
  154   158 		});
   -1   159 
  155   160 		if (event.target.checked) {
  156   161 			try {
  157   162 				var stream = await getStream();
  158    -1 				stream.getTracks().forEach(track => {
  159    -1 					localVideo.srcObject.addTrack(track);
  160    -1 				});
  161   163 			} catch {
  162   164 				event.target.checked = false;
  163   165 				return;
  164   166 			}
   -1   167 			stream.getTracks().forEach(track => {
   -1   168 				newStream.addTrack(track);
   -1   169 			});
  165   170 		}
  166   171 
  167   172 		if (other) {
  168   173 			other.checked = false;
  169   174 		}
  170   175 
   -1   176 		localVideo.srcObject = newStream;
  171   177 		updateConnections();
  172   178 	};
  173   179