agsh / rtsp-ffmpeg

Node.js FFMpeg wrapper for streaming RTSP into MotionJPEG
MIT License
249 stars 84 forks source link

Event stream.on is never fired #5

Open rodrigosalinas opened 8 years ago

rodrigosalinas commented 8 years ago

Hi, I'm trying to run your server example. Whether I left the URLs unchanged or if I change it to my cameras streams, the socket events fire up as expected, but not the On event of the Stream object. Can you help me pls? My cameras URL are like this: rtsp://192.168.1.48:554/profile1. Thank you.

agsh commented 8 years ago

Hello! Can you send me full demo example? And what did you mean when you said about 'on' event of the stream object?

rodrigosalinas commented 8 years ago

Thanks for your response. I'm using your server example as is. No change but the urls for the streams.

agsh commented 8 years ago

Do you mean that when you change sources to your ip-cams nothing works?

rodrigosalinas commented 8 years ago

Whether I change it or not. Is the same. The event that never get fired is the data instead of the on I wrote.

agsh commented 8 years ago

Oh, now I understand. Are you sure that this is wright rstp stream? Try to check it with vlc and ffmpeg with corresponding params

rodrigosalinas commented 8 years ago

This url I open it in VLC with no problem: rtsp://192.168.1.48:554/profile1

rodrigosalinas commented 8 years ago

This is my code:

const app    = require('express')(),
      server = require('http').Server(app),
      io     = require('socket.io')(server),
      rtsp   = require('rtsp-ffmpeg');

server.listen(6147);

var uri    = 'rtsp://192.168.1.48:554/profile1';
var stream = new rtsp.FFMpeg({input: uri});

io.on('connection', function(socket) {
  console.log("Connected.");
  var pipeStream = function(data) {
    console.log("Stream arrived.");
    socket.emit('data', data.toString('base64'));
  };
  stream.on('data', pipeStream);
  socket.on('disconnect', function() {
    console.log("Disconnected.");
    stream.removeListener('data', pipeStream);
  });
});

app.get('/', function (req, res) {
  res.sendFile(__dirname + '/public/index.html');
});
agsh commented 8 years ago

Are you're sure that rtsp://192.168.1.48:554/profile1 is pure rtsp stream that can be processed with ffmpeg?

rodrigosalinas commented 8 years ago

How do you advise me to check that? That URL I open in VLC with no problem.

rodrigosalinas commented 8 years ago

In fact, if I just open rtsp://192.168.1.48 in VLC, it also works. But not with the code.

agsh commented 8 years ago

And what is the result of ffmpeg -i rtsp://192.168.1.48 -f image2 -updatefirst 1?

rodrigosalinas commented 8 years ago

This was the result:

ffmpeg version N-69438-gb47ab04 Copyright (c) 2000-2015 the FFmpeg developers
  built on Jan 31 2015 10:37:29 with gcc 4.9.1 (Ubuntu 4.9.1-16ubuntu6)
  configuration: --enable-shared --disable-static
  libavutil      54. 18.100 / 54. 18.100
  libavcodec     56. 21.101 / 56. 21.101
  libavformat    56. 19.100 / 56. 19.100
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5.  9.103 /  5.  9.103
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  1.100 /  1.  1.100
Trailing options were found on the commandline.
[rtsp @ 0xb1f3a0] UDP timeout, retrying with TCP
[rtsp @ 0xb1f3a0] method PAUSE failed: 405 Method Not Allowed
[rtsp @ 0xb1f3a0] Could not find codec parameters for stream 0 (Video: h264, none): unspecified size
Consider increasing the value for the 'analyzeduration' and 'probesize' options
Guessed Channel Layout for  Input Stream #0.1 : mono
Guessed Channel Layout for  Input Stream #0.2 : mono
Input #0, rtsp, from 'rtsp://192.168.1.48':
  Metadata:
    title           : \profile1
  Duration: N/A, start: 0.000000, bitrate: 128 kb/s
    Stream #0:0: Video: h264, none, 90k tbr, 90k tbn, 180k tbc
    Stream #0:1: Audio: pcm_alaw, 8000 Hz, 1 channels, s16, 64 kb/s
    Stream #0:2: Audio: pcm_alaw, 8000 Hz, 1 channels, s16, 64 kb/s
At least one output file must be specified
agsh commented 8 years ago

Seems that it is failure with your rtsp stream. Just try to fix them with different ffmpeg arguments that you can later pass to the consturtuctor of rtsr.FFMpeg as an argument

rodrigosalinas commented 8 years ago

Thank you very much anyway. Can I ask your ffmpeg version? Apparently there are some issues with the latest versions.

rodrigosalinas commented 8 years ago

Hi. Finally this command worked for me to get a snapshot:

ffmpeg -rtsp_transport tcp -i rtsp://192.168.1.48:554/profile1 -map 0:0 -y -updatefirst 1 -q 31 -frames 1 -f image2 camera.jpg

How can I translate that to rtsp-ffmpeg? Thank you very very much.

agsh commented 8 years ago

Oh, this is simple, just look at https://github.com/agsh/rtsp-ffmpeg/blob/master/lib/rtsp-ffmpeg.js#L74 add all missing arguments in array in style of spawn node.js method https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options

rodrigosalinas commented 8 years ago

Hi. One last time. ¿Like this?

var uri    = 'rtsp://192.168.1.48:554/profile1';
var stream = new rtsp.FFMpeg({input: uri}, ['-rtsp_transport', 'tcp', '-map', '0:0', '-frames', '1', '-q', '1']);
rodrigosalinas commented 8 years ago

And one more thing. I need the -rtsp_transport argument to be first in the list of arguments, otherwise it doesn't work. It has to be in front of the -i argument.

agsh commented 8 years ago

Ok, I'll fix it in the next few days

agsh commented 8 years ago

Moved additional options to be first in the list of arguments. You can try either npm version or github master branch. But you should use constructor like this:

var uri    = 'rtsp://192.168.1.48:554/profile1';
var stream = new rtsp.FFMpeg({input: uri, arguments: ['-rtsp_transport', 'tcp', '-map', '0:0', '-frames', '1']});

Please try this and comment what happens.

rodrigosalinas commented 8 years ago

Hi agsh, thank you for your interest. I still can't get the snapshot, or the streaming for my camera, with your updated code. Let me show you the log when I invoke ffmpeg in the console. Perhaps that can help:

ffmpeg -rtsp_transport tcp -i rtsp://192.168.1.48:554/profile1 -map 0:0 -y -updatefirst 1 -q 0 -frames 1 -f image2 camera.jpg
ffmpeg version 2.7.1 Copyright (c) 2000-2015 the FFmpeg developers
  built with Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/2.7.1 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-libx264 --enable-libmp3lame --enable-libvo-aacenc --enable-libxvid --enable-vda
  libavutil      54. 27.100 / 54. 27.100
  libavcodec     56. 41.100 / 56. 41.100
  libavformat    56. 36.100 / 56. 36.100
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 16.101 /  5. 16.101
  libavresample   2.  1.  0 /  2.  1.  0
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  2.100 /  1.  2.100
  libpostproc    53.  3.100 / 53.  3.100
Guessed Channel Layout for  Input Stream #0.1 : mono
Guessed Channel Layout for  Input Stream #0.2 : mono
Input #0, rtsp, from 'rtsp://192.168.1.48:554/profile1':
  Metadata:
    title           : \profile1
  Duration: N/A, start: 0.000000, bitrate: N/A
    Stream #0:0: Video: h264 (Baseline), yuv420p, 1920x1080, 30 fps, 30 tbr, 90k tbn, 180k tbc
    Stream #0:1: Audio: pcm_alaw, 8000 Hz, 1 channels, s16, 64 kb/s
    Stream #0:2: Audio: pcm_alaw, 8000 Hz, 1 channels, s16, 64 kb/s
[swscaler @ 0x7fc9ac000000] deprecated pixel format used, make sure you did set range correctly
Output #0, image2, to 'camera.jpg':
  Metadata:
    title           : \profile1
    encoder         : Lavf56.36.100
    Stream #0:0: Video: mjpeg, yuvj420p(pc), 1920x1080, q=2-31, 200 kb/s, 30 fps, 30 tbn, 30 tbc
    Metadata:
      encoder         : Lavc56.41.100 mjpeg
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> mjpeg (native))
Press [q] to stop, [?] for help
frame=    1 fps=0.0 q=0.0 Lsize=N/A time=00:00:00.03 bitrate=N/A    
video:137kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
rodrigosalinas commented 8 years ago

I just tried something. Apparently not all optional parameters belongs at the beginning. For what I have read, the order in what the parameters appear instructs ffmpeg to apply them to the input or to the output. So, in my case, -rtsp_transport tcp belongs to the beginning, but not the map or frames options.