Good day,
What missed in the documentation is:
I have not tried it yet, but what I am imagining is:
I hope that would be the correct way to connect two peers with streams in both directions.
What I can suggest is a convenience function that could be run after the peers are connected with a data channel:
peer.connectTwoWays({audio:true, video:true})
Which would create the streams and trigger the onstream events on both ends. I don't know if that is possible, but it would be very nice.
To play a stream with an audio track, just attach it as the srcObject to an HTML5 audio element.
peer.on('stream', (stream) => {
const audioElement = document.createElement('audio')
audioElement.srcObject = stream
audioElement.play()
})
If you want two-way audio (like a voice call), just attach a stream with an audio track to both ends of the peer?
const peerA = new Peer({ initiator: true, stream: myMediaStream })
const peerB = new Peer({ initiator: false, stream: myMediaStream })
Thank you. A naive question; how do I know if the incoming stream is an audio or video or audio and video stream, so I know what type of element to attach it to?
PS I am adding the stream dynamically, after the data channel is created. Can I start attaching the streams simultaneously? I worry, that the renegotiation might then fail. That is why I first attach one stream, wait for it to start and only then send another stream in the other direction. Is that the correct way to do it, or can I just send a signal to the other peer at once to start sending his stream simultaneously?
You can attach streams whenever you want. Why do you think the renegotiation would fail?
@measwel To answer your question about knowing what type of stream it is, you can test the stream to see if it has video tracks (as well as test for audio tracks).
var isVideo = stream.getVideoTracks().length > 0;
To @t-mullen's point, you can add and remove tracks at-will. I have an app that adds video w/ audio, video w/o audio, and audio-only dynamically throughout the course of a call. SimplePeer handles any necessary renegotiation behind the scenes. There is no specific need to ease into it with a data-channel-only connection.
Most helpful comment
@measwel To answer your question about knowing what type of stream it is, you can test the stream to see if it has video tracks (as well as test for audio tracks).
var isVideo = stream.getVideoTracks().length > 0;To @t-mullen's point, you can add and remove tracks at-will. I have an app that adds video w/ audio, video w/o audio, and audio-only dynamically throughout the course of a call. SimplePeer handles any necessary renegotiation behind the scenes. There is no specific need to ease into it with a data-channel-only connection.