peerjs icon indicating copy to clipboard operation
peerjs copied to clipboard

The stream I get from iosrtc and pass to peerJs gives error

Open uhotspot4 opened this issue 6 years ago • 11 comments

I have my voice chat working perfectly using PeerJs but since WKWebView on iOS doesn't provide navigator.getUserMedia, had to use something like cordova-plugin-iosrtc just to get the userMedia, but still I use PeerJs for the rest of the process. And now I get following error:

TypeError: Argument 1 ('track') to RTCPeerConnection.addTrack must be an instance of MediaStreamTrack

And following is the piece of code I am using:

 cordova.plugins.iosrtc.getUserMedia({ audio: true, video: false },
    ).then(function (stream) {
    try{
       //Folowing is the line causing error
      var call = peer.call('A', stream);

      }catch(error) {console.log("This is the error I get:"+error)}

    call.on('stream', function(remoteStream) {  
       document.getElementById('audio').srcObject = remoteStream;
    });
}).catch(function (error) {
  console.error('getUserMedia failed: ', error);
}

I would appreciate if someone tells me how I can combine those two libraries and what I have done wrong!

Here is the stack trace:

capacitor://localhost/js/peerjs.js:8173:30 forEach@[native code] capacitor://localhost/js/peerjs.js:8172:31 capacitor://localhost/js/peerjs.js:7728:34 MediaConnection@capacitor://localhost/js/peerjs.js:8348:40 capacitor://localhost/js/peerjs.js:9865:64

And also I added a log right before line #8173 in peerjs.js file to check the track type using track.constructor.name and it says it is MediaStreamTrack!

uhotspot4 avatar Jul 17 '20 21:07 uhotspot4

Would you try as below:

navigator.mediaDevices.getUserMedia({ audio: true, video: true })

mozhn avatar Jul 19 '20 11:07 mozhn

@mozhn Thanks for the reply. I did and it gives the error saying mediaDevices is undefined.

I found same error being reported here https://github.com/webrtcHacks/adapter/issues/989 and the fixes @ https://github.com/webrtcHacks/adapter/pull/990/files/588953a348f7be9088f49899cb028a5a89316ec1 and https://github.com/webrtcHacks/adapter/pull/992/files/f18c41e39c2c65186954341b0581e8a0e31c9033.

I printed out the window.RTCPeerConnection.prototype.addTrack right before calling peer.call('A', stream); and it looks exactly as what they have in that patch!

function addTrack(track) {
   var stream = arguments[1];

  if (stream) {
    if (!this._localStreams) {
      this._localStreams = [stream];
    } else if (!this._localStreams.includes(stream)) {
      this._localStreams.push(stream);
    }
  }

  return _addTrack.apply(this, arguments);
}

Also have checked the argument that is passed to peerConnection.addTrack(track, stream); in peerjs.js @8173 and they are of types MediaStreamTrack and MediaStream!

And again for your information this thing works in Safari without using iosrtc. It just throws the error when I package the app using capacitor and use iosrtc to get access to browsers media streams.

uhotspot4 avatar Jul 19 '20 16:07 uhotspot4

Could there be something missing. My project Ionic 4 and Cordova. I'm not getting error. I'm not sure but did you import the library below?

https://webrtc.github.io/adapter/adapter-latest.js

My index.html source: https://hastebin.com/nadiqarave.xml

I hope it helps.

mozhn avatar Jul 19 '20 16:07 mozhn

@mozhn Didn't even know about adapter.js. Just came across it when searching for the error I get on Internet. So do you think I can drop iosrtc and instead user adapter.js?

Since you are using cordova I assume yours is an app using WKWebView too and not just a webpage running in safari, am I right? Didn't you ran into this problem when accessing mediaStream? Apparently it is a bug in webkit implementation in IOS and they have reported a bug for it and it hasn't been resolved yet -> https://bugs.webkit.org/show_bug.cgi?id=208667 I started using iosrtc just to overcome the mentioned bug!

uhotspot4 avatar Jul 19 '20 16:07 uhotspot4

Yes i used WKWebView. I have never experienced the error you mentioned. I followed the steps at this address and succeeded. The bottom of the page.

https://github.com/cordova-rtc/cordova-plugin-iosrtc

Example: https://hastebin.com/puqevumohe.js Now I realized it was just for cordova.

mozhn avatar Jul 19 '20 17:07 mozhn

@mozhn Thanks a lot, I'll check it out. And one more thing, do you use navigator.mediaDevices.getUserMedia in your code or something else?

uhotspot4 avatar Jul 19 '20 17:07 uhotspot4

Yes it is

mozhn avatar Jul 19 '20 18:07 mozhn

It is working now. Thanks for all the help.

uhotspot4 avatar Jul 20 '20 20:07 uhotspot4

It will be useful for everyone if you explain exactly what you are doing.

mozhn avatar Jul 21 '20 07:07 mozhn

@uhotspot4 I had the same problem, when trying to use this plugin with Peerjs on an Ionic app. Any insight on how you solved this problem?

dyegonery avatar Oct 27 '20 21:10 dyegonery

See this comment: https://github.com/peers/peerjs/issues/705#issuecomment-660676552

I recommand to close this issue, for future issue please create issue on cordova-plugin-iosrtc repository itself https://github.com/cordova-rtc/cordova-plugin-iosrtc

Notice that I'm the maintainer of iosRTC.

hthetiot avatar Mar 03 '21 21:03 hthetiot