watson / rtsp-stream

A transport agnostic RTSP serial multiplexer module for Node
MIT License
91 stars 28 forks source link

Usage example for RTSP client #1

Open mofux opened 8 years ago

mofux commented 8 years ago

Hello, thanks for this module.

I am trying to build a RTSP client from it, which is able to connect to a RTSP server, collect the streaming data (video, teletext, epg information) and save it to disk.

Now, as far as I can see, I would create a net client connection to the RTSP server, and then I would have to go through a setup process (probably this can be found somewhere in the RFC) using the encoder and then wait for the stream and decode it... but I am a little lost how to do it, especially how to compose the messages.

Would you be able to give me a short code example on how the server connection process would look like and how I can decode the messages from the server, so that i finally can access the underlying media stream?

Thanks a lot!

watson commented 8 years ago

@mofux I've never tried this, but off the top of my head I think you'd do something like this:

var net = require('net')
var rtsp = require('rtsp-stream')

var encoder = new rtsp.Encoder()
var decoder = new rtsp.Decoder()

// connect to RTSP server
var socket = net.connect({ host: 'example.com', port: 5000 }, function () {
  // make a request to the RTSP server
  var req = encoder.request({ method: 'OPTIONS', uri: '*' })
  req.setHeader('CSeq', 1)
  req.end()
})

// handle response from server
decoder.on('response', function (res) {
  console.log('--> received response from server:', res.statusCode)
  console.log('--> headers:', res.headers)
  res.pipe(process.stdout)
})

// connect the plumbing
encoder.pipe(socket).pipe(decoder)
mofux commented 8 years ago

Perfect, worked, that pushes me forward! If you want, I can submit a pull request to your repository, providing an example script on how to connect to an RTSP server and getting the server to start streaming media data to us. Lets say in an /examples folder?

watson commented 8 years ago

@mofux that would be awesome thanks :smiley: - glad it worked btw! I never tested using this module in a client setup, so I just wrote the above example by heart without testing it :wink:

kumarprimer commented 8 years ago

i am facing a issue of using rtsp-stream.

decoder = new rtsp.Decoder();
decoder.on('request', function (req) {
       var data = ""
       req.on('data', function (chunk) {
            data += chunk.toString();
       });
       req.on('end', function () {
            console.log('end of message',data);
       });
}

if request have large body content like 10K, i am receiving only one data signal call.

What i mean to say partial data is received with the above code. How to catch the complete packet ?

kumarprimer commented 8 years ago

how to enable debug logs

watson commented 8 years ago

@kumarprimer In the future, please open a new issue instead of adding your question to an existing and unrelated issue.

To enable debug mode, run the node app with the environment variabel DEBUG=rtsp-stream.

In regards to the issue you describe of not receiving more than one data event, the IncomingMessage object that is emitted by the request event is a simpel pass-through stream, so it should work just fine. I've just added an extra test to make sure I test for that and it seems to work just fine.

If you're still experiencing the issue, please open a new issue and provide a code-example that I can use to reproduce the error.