WebRTC - Detecting if the camera stopped - webrtc

WebRTC Questions
Is there an event for when the users camera stops streaming prematurely?
WebRTC can connect Peer2Peer what about Peer1 having a stream and multiple observers viewing?
Thanks

My experiment with Chrome 24 and 26 show that none of the mediastream/localmediastream or video events fire when a peer disconnects, but the (RTCPeerConnection) icecandidate event does fire several times right after the disconnect: first with the candidate and then without it on the event object.
peer = new RTCPeerConnection({ "iceServers": [{ "url": "stun:stun.l.google.com:19302" }] });
peer.onicecandidate = function(e){
if(!e.candidate) return console.log('Did someone disconnect?',e);
console.log('Someone might have just disconnected. Or maybe not.',e);
}
I've been able to connect 3 peers in chrome successfully as well: as far as I know each peer connection just connects two peers, so to have multiple simultaneous connections the host connects with each one individually.

This is what you wanted:
(await peerConnection.getStats())
.forEach((c)=>{if(c.type =='track')console.log(c)})
That will tell you how many frames have been received. Store that and then ask a second later and compare the two results. if the number of frames is the same it is not getting new data.

Related

onaddstream event not working when 3rd person joined the room in webrtc

I am new here in webrtc, i have strange issue, when i worked with one to one user onaddstream event is working, i am getting its response, but after then 3rd person joined the room onaddstream event is not working, can anyone please help me how to resolve this issue ? here i have added my whole code for it, can anyone please review it and helped me to get event for all the remote users
var pc = new RTCPeerConnection(configuration);
pc.onaddstream = (remoteaddstream) => {
console.log(remoteaddstream);
};
navigator.mediaDevices.getUserMedia({
audio: true,
video: true,
}).then(stream => {
var localstreamid = stream.id;
console.log("stream id :"+localstreamid);
pc.addStream(stream);
}, function(e) {
console.log(e);
});
You need multiple peer connections to connect more than 2 parties, or alternatively you can make all the parties connect to a server that forwards the data.
From https://medium.com/#khan_honney/webrtc-servers-and-multi-party-communication-in-webrtc-6bf3870b15eb
Mesh Topology
Mesh is the simplest topology for a multiparty application. In this topology, every participant sends and receives its media to all other participants. We said it is the simplest because it is the most straightforward method.
Mixing Topology and MCU
Mixing is another topology where each participant sends its media to a central server and receives a media from the central server. This media may contain some or all other participant’s media
Routing Topology and SFU
Routing is a multiparty topology where each participant sends its media to a central server and receives all other’s media from the central server.

Disconnecting a gun peer

What is the correct way to disconnect gun peers?
I can connect peers with:
Gun(['http://127.0.0.1:8080/gun','http://127.0.0.1:8081/gun'])
But cant disconnect again using
Gun(['http://127.0.0.1:8080/gun')
as peers will still both sync. In the last part of the video I couldn't disconnect the peers as both db's were still syncing.
On a different note the first part of the video shows problems with Chrome not updating however firefox works as it should.
Video and
Source Code
#soth this is a good question. There is currently no standard way to disconnect from a peer. This should probably be added.
This is terrible. But here is a hacky solution for now:
var peers = gun.back('opt.peers');
Object.keys(peers).forEach(function(id){
var peer = peers[id];
peer.retry = 0;
gun.on('bye', peer);
});
This will disconnect from all peers. If you only want one, filter by the ID which will probably be the URL.

webrtc onaddstream event handler - differentiate between multiple video streams

In a scenario where an endpoint adds multiple video streams to a peer connection, the onaddstream event handler is invoked multiple times on the peer end.
Is there any means by which an application (on the peer end) can determine between the different video streams (within onaddstream handler)? By identifying the stream, each stream can be associated with a different video element.
RTCPeerConnection has a couple of methods:
getRemoteStreams
getLocalStreams
These methods return an array of MediaStream's associated with the remote/local end of the connection. Every MediaStream has an id, so you will be able to identify them.
There is also getStreamById, but it is deprecated, replaced by the two above.

WebRTC - Reusing connections

Say we have the following:
Peer1 joins, creates RTCPeerConnectionA
Peer2 joins, creates RTCPeerConnectionB
SDP/ICE handshakes occur, connection is established, streaming is happening
4A. Peer2 loses connectivity and rejoins
4B. Peer2 refreshes the browser
What should Peer1 do in 4A and 4B?
With 4A the same RTCPeerConnection object is available for the peer to use - is there any work that needs to be done to completely repair the connection?
In 4B, Peer1 has maintained one end of the connection but Peer2 is starting out from scratch. Can Peer1 re-use the ICE candidates and localDescription to repair the connection with the new RTCPeerConnection on the other end, or does it also need to create a brand new instance of RTCPeerConnection and reinitiate handshakes, onicecandidate, etc?
4a: this is done with an ice restart. https://webrtc.github.io/samples/src/content/peerconnection/restart-ice/ is an example of how to do this.
4b: a new peerconnection will be needed as the refreshed tab will not have the crypto credentials to decode the old stream.

webrtc connection gets disconnected but sound is still on

I have a video chat application using WebRTC. There is a slight problem: Below is my code for oniceconnectionstatechage:
connection.oniceconnectionstatechange = function () {
if (connection.iceConnectionState == 'disconnected' || connection.iceConnectionState == 'closed')
console.log('connection gone.')
};
The problem is that sometimes, when the internet speed is not well, my connection gets disconnected and I see "connection gone" in my console but the sound still stays on. Both sides can hear each other but the video is gone. What can I do to disconnect my connection completely is such a situation?
You see connection gone in your console when the network connection is unstable, as iceConnectionState may have the state disconnected as
a transient state, e. g. on a flaky network, that can recover by itself.
(Mozilla Developer Network)
It might be - this is an assumption - that in some/many of such cases video is dropped as the available bandwidth can't support both audio and video.
If you really want to close the connection completely when a disconnect occurs, you can replace your if-statement (incl. the console.log) in oniceconnectionchange listerner with the following code:
if (connection.iceConnectionState == 'disconnected'){
console.log('Connection gone. Closing connection.');
connection.close();
}
So each disconnect will be followed by closing the connection completely.
However, this is probably bad practice:
Why would you want to close down the whole connection just because there are temporary problems with the network?
I assume that using this code in a real application will lead to bad user experience, as disconnects will appear in many cases the user would not notice otherwise. I suggest it is better to display a warning to the user in case of (frequent) disconnects.