rtc

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

commit
973f06ce066dd68da7eaa31c2861be8c9dd4cfb2
parent
ea89ba79a7005a831e13fce2ac246e8345a5bde4
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2020-04-03 19:53
refactor rtc/controls split

Diffstat

R rtc/controls.js -> rtc/media.js 27 +++++++++------------------
M rtc/rtc.js 21 +++++++++++++--------

2 files changed, 22 insertions, 26 deletions


diff --git a/rtc/controls.js b/rtc/media.js

@@ -1,23 +1,14 @@
    1    -1 export var init = function(localVideo, cons) {
    2    -1 	var controls = document.querySelector('.rtc-controls');
   -1     1 var controls = document.querySelector('.rtc-controls');
   -1     2 var localVideo = document.querySelector('.rtc-videos video.local');
   -1     3 localVideo.srcObject = new MediaStream();
    3     4 
    4    -1 	var updateConnections = function() {
    5    -1 		var sender;
    6    -1 		var tracks = localVideo.srcObject.getTracks();
    7    -1 
    8    -1 		for (sender in cons) {
    9    -1 			var con = cons[sender].con;
   10    -1 
   11    -1 			con.getSenders().forEach(s => {
   12    -1 				con.removeTrack(s);
   13    -1 			});
   14    -1 
   15    -1 			tracks.forEach(track => {
   16    -1 				con.addTrack(track, localVideo.srcObject);
   17    -1 			});
   18    -1 		}
   19    -1 	};
   -1     5 export var addLocalTracks = function(con) {
   -1     6 	localVideo.srcObject.getTracks().forEach(track => {
   -1     7 		con.addTrack(track, localVideo.srcObject);
   -1     8 	});
   -1     9 };
   20    10 
   -1    11 export var init = function(updateConnections) {
   21    12 	var updateStreams = async function(event) {
   22    13 		var tracks = localVideo.srcObject.getTracks();
   23    14 		var kind = event.target.name === 'audio' ? 'audio' : 'video';

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

@@ -2,7 +2,7 @@
    2     2 
    3     3 import * as signal from './signal.js';
    4     4 import * as chat from './chat.js';
    5    -1 import * as controls from './controls.js';
   -1     5 import * as media from './media.js';
    6     6 
    7     7 // https://webrtc.github.io/samples/
    8     8 // https://www.html5rocks.com/en/tutorials/webrtc/basics/
@@ -18,9 +18,6 @@ console.log('own queue', queue);
   18    18 var container = document.querySelector('.rtc-videos');
   19    19 var cons = {};
   20    20 
   21    -1 var localVideo = document.querySelector('.rtc-videos video.local');
   22    -1 localVideo.srcObject = new MediaStream();
   23    -1 
   24    21 var closeConnection = function(sender) {
   25    22 	if (sender in cons) {
   26    23 		cons[sender].video.remove();
@@ -57,9 +54,7 @@ var getConnection = function(sender) {
   57    54 		video.srcObject = event.streams[0];
   58    55 	});
   59    56 
   60    -1 	localVideo.srcObject.getTracks().forEach(track => {
   61    -1 		con.addTrack(track, localVideo.srcObject);
   62    -1 	});
   -1    57 	media.addLocalTracks(con);
   63    58 
   64    59 	cons[sender] = {
   65    60 		'con': con,
@@ -134,6 +129,16 @@ window.addEventListener('beforeunload', function() {
  134   129 });
  135   130 
  136   131 chat.init(room, queue);
  137    -1 controls.init(localVideo, cons);
   -1   132 
   -1   133 media.init(function() {
   -1   134 	var sender;
   -1   135 	for (sender in cons) {
   -1   136 		var con = cons[sender].con;
   -1   137 		con.getSenders().forEach(s => {
   -1   138 			con.removeTrack(s);
   -1   139 		});
   -1   140 		media.addLocalTracks(con);
   -1   141 	}
   -1   142 });
  138   143 
  139   144 signal.post(room, {sender: queue, type: 'announce'});