question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fallback to addStream when addTrack is not supported

See original GitHub issue

Hi,

Using Simple Peer together with older webrtc API such as https://github.com/oney/react-native-webrtc where addTrack is not supported. Results in stream not being created in the Peer constructor, it also results in a chrash if addStream is used directly.

On line 139 in index.js:

  if ('addTrack' in self._pc) {
    if (self.streams) {
      self.streams.forEach(function (stream) {
        self.addStream(stream)
      })
    }
    self._pc.ontrack = function (event) {
      self._onTrack(event)
    }
  }

This makes it so that the stream is not added and no feedback is given to the user of Simple Peer.

Using the peer.addStream method doesn’t have the same check.

On line 275 in index.js:

var sender = self._pc.addTrack(track, stream)

Here addTrack is used without checking if addTrack exists.

I have made a workaround that enables me to send stream from android app to a chrome browser. I will make a PR shortly.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

2reactions
BabakYaghoobicommented, Mar 21, 2021

Hi guys, If you want to use simple-peer in React Native need some changes:

  1. change somthing in addTrack func:
addTrack(track, stream) {
    this._debug('addTrack()')

    const submap = this._senderMap.get(track) || new Map() // nested Maps map [track, stream] to sender
    let sender = submap.get(stream)
    if (!sender) {

      //Added by Babak <-------- need changes
      if (this._pc.addTrack) {
        sender = this._pc.addTrack(track, stream)
      } else {
        sender = this._pc.addStream(stream)
      }

      submap.set(stream, sender)
      this._senderMap.set(track, submap)
      this._needsNegotiation()
    } else if (sender.removed) {
      throw errCode(new Error('Track has been removed. You should enable/disable tracks that you want to re-add.'), 'ERR_SENDER_REMOVED')
    } else {
      throw errCode(new Error('Track has already been added to that stream.'), 'ERR_SENDER_ALREADY_ADDED')
    }
  }

  1. add this func after: this._pc.ontrack
    // Added by Babak
    this._pc.onaddstream = event => {
      queueMicrotask(() => {
        this._debug('on stream')
        this.emit('stream', event.stream) // ensure all tracks have been added
      })
    }

now the other side for using this stream use like this:

peer.on('stream', (stream) => {
      // console.log('----------------- stream -------------------------------');
      if (stream.currentTarget && stream.currentTarget._remoteStreams) {
        stream = stream.currentTarget._remoteStreams[0];
      }
      //console.log('Got peer stream!!!', peerId, stream);

      peer.stream = stream;
      that.setPeerState(peerId, peer);
    });

tested on version 9.7.2 and work fine on react native, Regrads

0reactions
stanwolverinecommented, Jun 5, 2020

hello @owodunni, how did you end up solving your issue? I am stuck with this same issue, and can’t figure out what to do. please help.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to update from addStream to addTrack correctly?
Try: this.peer.addStream(this.myStream). Note: peer object has a method ...
Read more >
npm:simple-peer-chicho - Skypack.dev
Simple one-to-one WebRTC video/voice and data channels.
Read more >
RTCPeerConnection.addStream() - Web APIs - MDN Web Docs
The obsolete RTCPeerConnection method addStream() adds a MediaStream as a local source of audio or video. Instead of using this obsolete method ...
Read more >
pc/peer_connection.cc - external/webrtc - Git at Google
RTC_CHECK(!IsUnifiedPlan()) << "AddStream is not available with Unified Plan ". "SdpSemantics. Please use AddTrack instead.";.
Read more >
Using DTMF with WebRTC - Web APIs
In order to more fully support audio/video conferencing, WebRTC supports sending ... supports addTrack() ; if it doesn't, we'll fall back to addStream()...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found