3

I am trying to attach the stream captured using getusermedia() on startPeerConnection(stream). I have tried to add the stream in the following way.

   function startPeerConnection(stream) {
            var configuration = {
                // Uncomment this code to add custom iceServers    //
                "iceServers": [{ "url": "stun:stun.1.google.com:19302" }]
            };

            yourConnection = new RTCPeerConnection(configuration);
            theirConnection = new RTCPeerConnection(configuration);


            // Setup stream listening 
            yourConnection.addStream(stream);//***getting the error on this line***
            theirConnection.onaddstream = function (e) {
                theirVideo.srcObject = e.stream;
                theirVideo.play();
            };


            // Setup ice handling
            yourConnection.onicecandidate = function (event) {
                if (event.candidate) {
                    theirConnection.addIceCandidate(new RTCIceCandidate(event.candidate));
                }
            };

            theirConnection.onicecandidate = function (event) {
                if (event.candidate) {
                    yourConnection.addIceCandidate(new RTCIceCandidate(event.candidate));
                }
            };



            // Begin the offer 
            yourConnection.createOffer(function (offer) {
                yourConnection.setLocalDescription(offer);
                theirConnection.setRemoteDescription(offer);
                theirConnection.createAnswer(function (offer) {
                    theirConnection.setLocalDescription(offer);
                    yourConnection.setRemoteDescription(offer);
                });
            });
        };

The RTCpeerconnection goes like this:

var RTCPeerConnection = function(options) {

    var iceServers = options.iceServers || defaults.iceServers;
    var constraints = options.constraints || defaults.constraints;

    var peerConnection = new PeerConnection(iceServers);

    peerConnection.onicecandidate = onicecandidate;
    peerConnection.onaddstream = onaddstream;
    peerConnection.addStream(options.stream);//***getting error on here ***

    function onicecandidate(event) {
        if (!event.candidate || !peerConnection) return;
        if (options.getice) options.getice(event.candidate);
    }

    function onaddstream(event) {
        options.gotstream && options.gotstream(event);
    }


DHARMENDRA SINGH
  • 607
  • 5
  • 21
marceloo1223
  • 95
  • 1
  • 10
  • Where does `PeerConnection` come from? It is not in the browser standards. – mahalde Jul 19 '19 at 07:24
  • i have used the js file from here:https://github.com/muaz-khan/WebRTC-Experiment/tree/master/RTCPeerConnection – marceloo1223 Jul 19 '19 at 07:39
  • uhm... you are overriding the RTCPeerConnection object in the "goes like this" with a function that has the same name.That is a bad idea... – Philipp Hancke Jul 19 '19 at 09:00
  • Relying on a github repo labeled "experiment" that hasn't been touched in 6 years, is not a good idea for WebRTC. It suffers from several problems, which I enumerate in the issue I duped it too. – jib Jul 20 '19 at 14:04
  • hye @jib thanks for the advice i have tried your demo here :https://jsfiddle.net/aynr0k5q/ and it looks pretty cool. I just want to know how do we setup a remote connection for this i wil be thankful if u can help – marceloo1223 Jul 24 '19 at 04:05
  • Updated that jsfiddle to use `addTrack` instead of `addStream` https://jsfiddle.net/p6d49haj/ – SSpoke Mar 11 '20 at 10:20

1 Answers1

7

addStream is a method that has been removed from the standard and Safari doesn't implement it. Either switch to the addTrack method by replacing

peerConnection.addStream(options.stream);

with

options.stream.getTracks().forEach(track => peerConnection.addTrack(track, options.stream))

or include adapter.js in your project which polyfills addStream

Philipp Hancke
  • 15,855
  • 2
  • 23
  • 31