iizukanao / node-rtsp-rtmp-server

RTSP/RTMP/HTTP hybrid server
MIT License
1.09k stars 282 forks source link

fix overwriting buffer while Socket.send() in progress #122

Closed Elektri4ker closed 5 years ago

Elektri4ker commented 5 years ago

Faced the problem while deploying own restreaming service.

The problem is: when several clients wants the same source, some clients can receive corrupt data. The reason is: when rtsp server sends buffer via socket.send(buffer), which is async, it then changes data in the buffer while the buffer is yet utilized by send operation. It is sometimes hard to reproduce this problem because of it's asyncronous nature. To solve this: for each client make own copy of message buffer. The fix is only for rtsp/NAL units, maybe this problem is actual somewhere else?

iizukanao commented 5 years ago

Good catch! I didn't know socket.send() has such behavior. Found this note on Node.js API doc.

An optional callback function may be specified to as a way of reporting DNS errors or for determining when it is safe to reuse the buf object.

Thank you @Elektri4ker !