media-fish / rtmp-server

A Node.js server that receives an RTMP live stream and populates a readable object stream of the published audio, video, and data messages
30 stars 5 forks source link

Usage with OBS? #5

Closed ThisIsMissEm closed 4 years ago

ThisIsMissEm commented 4 years ago

Hi! I'm wanting to use this in conjunction with OBS, the streaming application recommended for Twitch streamers, amongst others.

When I start up the example server, I then start streaming in OBS, pointing it to rtmp://localhost:1935/live, the handshake seems to happen okay, but then suddenly the server crashes with:

    throw new Error(`Insufficient buffer: buff.length=${buffer.length}, offset=${offset}, bytesToRead=${bytesToRead}`);
          ^
Error: Insufficient buffer: buff.length=17617, offset=17617, bytesToRead=3
    at ASSERT (rtmp-server/node_modules/@mediafish/buffer-operator/reader.js:3:11)
    at Object.readNumber (rtmp-server/node_modules/@mediafish/buffer-operator/reader.js:77:3)
    at readMsgType1 (rtmp-server/util/chunk.js:73:36)
    at Object.readMessage (rtmp-server/util/chunk.js:27:31)
    at ConnectionManager.processMessage (rtmp-server/connMgr.js:109:33)
    at Socket.<anonymous> (rtmp-server/connMgr.js:56:32)
    at Socket.emit (events.js:310:20)
    at Socket.EventEmitter.emit (domain.js:482:12)
    at addChunk (_stream_readable.js:286:12)
    at readableAddChunk (_stream_readable.js:268:9)

You can find the full log below:

``` rtmp-server Listening on port: 1935 +0ms rtmp-server [RTMPServer] Incomming connection: ::ffff:127.0.0.1:54764 +13s rtmp-server Handshake done. +0ms rtmp-server { rtmp-server buff: , rtmp-server offset: 1537 rtmp-server } +0ms rtmp-server Protocol Control Message: messageTypeId=1 +3ms rtmp-server [Set Chunk Size] Recieved: 4096 bytes +0ms rtmp-server [Acknowledgement] Sent: sequenceNumber = 3272 +0ms rtmp-server { rtmp-server buff: , rtmp-server offset: 1553 rtmp-server } +3ms rtmp-server Command Message: messageTypeId=20 +1ms rtmp-server [Command] Received: { rtmp-server "name": "connect", rtmp-server "transactionId": 1, rtmp-server "data": { rtmp-server "app": "live", rtmp-server "type": "nonprivate", rtmp-server "flashVer": "FMLE/3.0 (compatible; FMSc/1.0)", rtmp-server "swfUrl": "rtmp://localhost:1935/live", rtmp-server "tcUrl": "rtmp://localhost:1935/live" rtmp-server } rtmp-server } +0ms rtmp-server [Window Acknowledgement Size] Sent: windowSize = 5000000 +2ms rtmp-server [Set Peer Bandwidth] Sent: windowSize = 5000000, limitType=2 +0ms rtmp-server [Set Chunk Size] Sent: chunkSize = 4096 +1ms rtmp-server [Command] Response: { rtmp-server "name": "_result", rtmp-server "transactionId": 1, rtmp-server "params": [ rtmp-server { rtmp-server "fmsVer": "FMS/3,0,1,123", rtmp-server "capabilities": 31 rtmp-server }, rtmp-server { rtmp-server "level": "status", rtmp-server "code": "NetConnection.Connect.Success", rtmp-server "description": "Connection succeeded.", rtmp-server "objectEncoding": 0 rtmp-server } rtmp-server ] rtmp-server } +1ms rtmp-server RTMPConnection object is created. path=/live +1ms rtmp-server Command Message: messageTypeId=20 +3ms rtmp-server [Command] Received: { rtmp-server "name": "releaseStream", rtmp-server "transactionId": 2, rtmp-server "data": "510636083_dkuK2VnvfNSrJ5uF7oWtxjcq1cW0gG" rtmp-server } +0ms rtmp-server Command Message: messageTypeId=20 +0ms rtmp-server [Command] Received: { rtmp-server "name": "FCPublish", rtmp-server "transactionId": 3, rtmp-server "data": "510636083_dkuK2VnvfNSrJ5uF7oWtxjcq1cW0gG" rtmp-server } +1ms rtmp-server Command Message: messageTypeId=20 +1ms rtmp-server [Command] Received: { rtmp-server "name": "createStream", rtmp-server "transactionId": 4, rtmp-server "data": null rtmp-server } +0ms rtmp-server [Command] Response: { rtmp-server "name": "_result", rtmp-server "transactionId": 4, rtmp-server "params": [ rtmp-server null, rtmp-server 1 rtmp-server ] rtmp-server } +0ms rtmp-server { rtmp-server buff: , rtmp-server offset: 1 rtmp-server } +5ms rtmp-server Command Message: messageTypeId=20 +1ms rtmp-server [Command] Received: { rtmp-server "name": "publish", rtmp-server "transactionId": 5, rtmp-server "data": { rtmp-server "publishingName": "live_510636083_dkuK2VnvfNSrJ5uF7oWtxjcq1cW0gG", rtmp-server "publishingType": "live" rtmp-server } rtmp-server } +1ms rtmp-server [Command] Response: { rtmp-server "name": "onStatus", rtmp-server "transactionId": 0, rtmp-server "params": [ rtmp-server null, rtmp-server { rtmp-server "level": "status", rtmp-server "code": "NetStream.Publish.Start", rtmp-server "description": "Start publishing", rtmp-server "audioCodecs": 1024, rtmp-server "videoCodecs": 128 rtmp-server } rtmp-server ] rtmp-server } +0ms rtmp-server RTMPStream object is created. name=*, id=1 +1ms rtmp-server { rtmp-server buff: , rtmp-server offset: 1 rtmp-server } +2ms rtmp-server Data Message: messageTypeId=18, messageStreamId=1 +1ms 0 [Data] [ "@setDataFrame", "onMetaData", [ 0, 0, 1120, 700, "avc1", 3800, 60, "mp4a", 160, 44100, 16, 2, true, false, false, false, false, false, false, "obs-output module (libobs version 25.0.8)" ] ] rtmp-server { rtmp-server buff: , rtmp-server offset: 1 rtmp-server } +443ms rtmp-server Audio Message: messageTypeId=8, messageStreamId=1 +443ms rtmp-server AAC data (len=4) +0ms timestamp: 0 { "AAC": { "format": "AAC", "sampleRate": "44kHz", "size": "16 bits per sample", "isStereo": 1, "data": "", "packetType": "AAC sequence header" } } rtmp-server { rtmp-server buff: , rtmp-server offset: 17 rtmp-server } +1ms rtmp-server Video Message: messageTypeId=9, messageStreamId=1 +1ms rtmp-server AVC data (len=45) +0ms timestamp: 0 { "AVC": { "frameType": "keyframe", "sampleRate": "AVC", "data": "", "packetType": "AVC sequence header", "compositionTimeOffset": 0 } } rtmp-server { rtmp-server buff: , rtmp-server offset: 74 rtmp-server } +1ms rtmp-server Video Message: messageTypeId=9, messageStreamId=1 +1ms rtmp-server AVC data (len=45437) +1ms timestamp: 0 { "AVC": { "frameType": "keyframe", "sampleRate": "AVC", "data": "", "packetType": "AVC NAL unit", "compositionTimeOffset": 33 } } rtmp-server/node_modules/@mediafish/buffer-operator/reader.js:3 throw new Error(`Insufficient buffer: buff.length=${buffer.length}, offset=${offset}, bytesToRead=${bytesToRead}`); ^ Error: Insufficient buffer: buff.length=45533, offset=45531, bytesToRead=3 at ASSERT (rtmp-server/node_modules/@mediafish/buffer-operator/reader.js:3:11) at Object.readNumber (rtmp-server/node_modules/@mediafish/buffer-operator/reader.js:77:3) at readMsgType2 (rtmp-server/util/chunk.js:92:28) at Object.readMessage (rtmp-server/util/chunk.js:29:31) at ConnectionManager.processMessage (rtmp-server/connMgr.js:109:33) at Socket. (rtmp-server/connMgr.js:56:32) at Socket.emit (events.js:310:20) at addChunk (_stream_readable.js:286:12) at readableAddChunk (_stream_readable.js:268:9) at Socket.Readable.push (_stream_readable.js:209:10) ```

Any ideas as to what's going wrong? Is it that OBS is sending video data to rtmp-server in an unsupported encoding?

kuu commented 4 years ago

Thanks for the message. It looks like the rtmp-server crashed by attempting to read the incoming buffer a little bit too much, beyond its end. The logs indicate that there were some extra bytes after the first video packet, which the server tried to read and died for. OBS was sending audio/video in supported format (AAC + AVC) so no worries. Maybe there's a bug on the server's side.

Though I'm interested in why the extra bytes were there, an easy workaround would be just to ignore such the extra bytes instead of crashing. Will implement the workaround so please hang tight for maybe a few days.

ThisIsMissEm commented 4 years ago

Okay, thanks! Hopefully you're able to reproduce it. I honestly don't know much about rtmp/video streaming, so was a little lost.

Could the extra bytes be metadata of some kind?

On 3. Jul 2020, at 06:03, Kuu Miyazaki notifications@github.com wrote:

Thanks for the message. It looks like the rtmp-server crashed by attempting to read the incoming buffer a little bit too much, beyond its end. The logs indicate that there were some extra bytes after the first video packet, which the server tried to read and died for. OBS was sending audio/video in supported format (AAC + AVC) so no worries. Maybe there's a bug on the server's side.

Though I'm interested in why the extra bytes were there, an easy workaround would be just to ignore such the extra bytes instead of crashing. Will implement the workaround so please hang tight for maybe a few days.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

kuu commented 4 years ago

I was able to reproduce the issue with OBS Studio 25.0.8 on macOS. I just modified the code to ignore the extra bytes. The change has already be published to npm as @mediafish/rtmp-server@0.1.1

Could the extra bytes be metadata of some kind?

I don't think so because those extra bytes are very short, 1~3 bytes. I think it's safe to ignore but you can check the logs if any skipping was happening: 2020-07-05T04:59:16.012Z rtmp-server Extra bytes found. Skipping trailing 4 bytes.

Feel free to open a new issue if the issue is persistent. Thanks.

kuu commented 4 years ago

https://github.com/media-fish/rtmp-server/pull/7