vdo.ninja icon indicating copy to clipboard operation
vdo.ninja copied to clipboard

refactor main.js for ability to implement custom session.configuration logic

Open briggeml opened this issue 3 years ago • 7 comments

We need to use own logic to work with session.configuration (stun-turn etc). To avoid make messy inserts and merges in main.js is it possible to put all the connections logic into a separate .js file for customization which overrides main.js?

briggeml avatar Mar 24 '21 13:03 briggeml

I've tried to do just this in index.html file; you can also call your own js file instead. https://github.com/steveseguin/obsninja/blob/055c4652aeccd06f03d92d5e07b3a0781c39a29b/index.html#L1240

steveseguin avatar Mar 24 '21 13:03 steveseguin

I've tried to do just this in index.html file; you can also call your own js file instead. https://github.com/steveseguin/obsninja/blob/055c4652aeccd06f03d92d5e07b3a0781c39a29b/index.html#L1240

Yes, I can see that, but often we need to make long duration async XMLHttpRequest to get addresses, credentials, etc, not just set them statically. This often leads to unpredictable data in session.configuration that mixes with defaults.

briggeml avatar Mar 24 '21 14:03 briggeml

I can refactor as needed; is the goal just to set the TURN/STUN settings via API request?

I did a process-blocking implementation example here:, but I suspect you want it non-blocking. https://github.com/steveseguin/obsninja/blob/cf4959c6db99d4203b7f400ad9a017e37071ed44/main.js#L2153

Can you give me an idea of what you need or how you'd prefer it done, and I'll put together a code example with required changes.

steveseguin avatar Mar 24 '21 14:03 steveseguin

I can refactor as needed; is the goal just to set the TURN/STUN settings via API request?

Yes

I did a process-blocking implementation example here:, but I suspect you want it non-blocking. https://github.com/steveseguin/obsninja/blob/cf4959c6db99d4203b7f400ad9a017e37071ed44/main.js#L2153

Can you give me an idea of what you need or how you'd prefer it done, and I'll put together a code example with required changes.

We want to use non-blocking request.open('GET', 'https://some-address/api', true)

Now we just do sync requests in apicalls.js like this:

index.html:

But sometimes the requests takes few seconds and the browser just freeze. I would appreciate an code example of how to work correctly with these settings.

briggeml avatar Mar 24 '21 15:03 briggeml

Understood. The request makes sense. Off hand, I think I will set this up so that it won't connect until you manually specify it can connect. I may deviate if I find a better way.

steveseguin avatar Mar 24 '21 15:03 steveseguin

Just wanted to share an update that I've been working on this task and will continue to work on it a bit more.

Below is a first pass at a solution, however this particular solution is specific to the next release of OBS.Ninja, which will likely be going into "beta" testing soon.

I might have a solution that works with the existing production version, but it's a bit hacky. If I can get it working well, I'll post it here also.

session.ws = false; // prevents connection
var twillioRequest = new XMLHttpRequest();
twillioRequest.onreadystatechange = function() {
	if (twillioRequest.status === 200) {
		try{
			var res = JSON.parse(twillioRequest.responseText);
		} catch(e){return;}
		session.configuration = {
			iceServers: [{
					"username": res["1"],
					"credential": res["2"],
					"url": "turn:global.turn.twilio.com:3478?transport=tcp",
					"urls": "turn:global.turn.twilio.com:3478?transport=tcp"
				},
				{
					"username": res["1"],
					"credential": res["2"],
					"url": "turn:global.turn.twilio.com:443?transport=tcp",
					"urls": "turn:global.turn.twilio.com:443?transport=tcp"
				}
			],
			sdpSemantics: 'unified-plan' // future-proofing
		};
		if (session.ws===false){
			session.ws=null; // allows connection (clears state)
			session.connect(); // connect if not already connected.
		}
		
	}
};
twillioRequest.open('GET', 'https://api.obs.ninja/twilio', true); // `false` makes the request synchronous
twillioRequest.send();

steveseguin avatar Mar 26 '21 18:03 steveseguin

Thanks. Nevertheless, I will wait solution for the current release version. Is the difference that significant?

briggeml avatar Mar 28 '21 17:03 briggeml