feross / simple-peer

📡 Simple WebRTC video, voice, and data channels
MIT License
7.41k stars 970 forks source link

Stream buffer full? #130

Closed SourceBoy closed 7 years ago

SourceBoy commented 7 years ago

I'm observing a weird behavior under Node 6.9.4:

const peer1 = new SimplePeer(/* configured */)

peer1.on('connect', () => {
  const stream = fs.createReadStream('Some large file (1GB would do)')
  //stream.pipe(peer1) // Only sends about 3 to 4MB of file
  stream.on('data', (chunk) => {
    peer1.write(chunk) // Returns false after about 3MB
    // Adding if false stream.pause() and stream.resume() on peer1.on('drain') doesn't work either
  })
})
const peer2 = new SimplePeer(/* configured */)
let size = 0

peer2.on('data', (chunk) => {
  // Only receives about 2 to 3MB of file no matter what
  console.log(size += chunk.length) // Does NOT receive the whole file regardless using peer1.write(chunk) or stream.pipe(peer1) from peer1
})

It'd seem that self._channel.bufferedAmount is > self._maxBufferedAmount; doesn't the channel buffer get flushed upon send()?

SourceBoy commented 7 years ago

Test result indicates that the drain event from peer1 never fired:

peer1.on('connect', () => {
  stream.on('data', (chunk) => {
    if (peer1.write(chunk) === false) {
      stream.pause()
    }
  })

  // Doesn't fire
  peer1.on('drain', () => {
    stream.resume()
  })
})

output

feross commented 7 years ago

Which webrtc implementation are you using in Node.js? wrtc?

SourceBoy commented 7 years ago

@feross electron-webrtc@0.3.0 src/RTCDataChannel.js#L164

SourceBoy commented 7 years ago

Looks like this RTCDataChannel.send() may have a bug and is somehow causing this.

Switching to wrtc with this binary on Linux worked.

The memory usage is however, rather high. Looking at this makes me wonder if the RAM ate the whole 1GB file since both sender & recipient's memory spiked up 1GB Memory usage (Closing the data channel or destroying the peer instance doesn't free the memory either)

feross commented 7 years ago

@SourceBoy glad you got your issue figured out. thanks for opening an issue on the electron-webrtc repo.