bytedeco / javacv

Java interface to OpenCV, FFmpeg, and more
Other
7.57k stars 1.58k forks source link

FFmpegFrameGrabber can't start on a certain RTSP stream #1290

Open slobglob opened 5 years ago

slobglob commented 5 years ago

Hey,

I'm trying to run FFmpegFrameGrabber on a simple Android project. My build.gradle looks like this: implementation group: 'org.bytedeco', name: 'javacv-platform', version: '1.5.1'

And the rest of the code is pretty straight forward as well:

FFmpegFrameGrabber mGrabber = new FFmpegFrameGrabber("rtsp://192.168.1.1");
mGrabber.setVideoCodec(avcodec.AV_CODEC_ID_H264);
mGrabber.setOption("rtsp_transport", "tcp");
mGrabber.setFrameRate(15);
mGrabber.setImageWidth(1280);
mGrabber.setImageHeight(720);
mGrabber.start();

When the .start() command is executed it enters a catch block with the following exception: W/System.err: org.bytedeco.javacv.FrameGrabber$Exception: avformat_open_input() error -113: Could not open input "rtsp://192.168.1.1". (Has setFormat() been called?)

The rtsp camera itself is standalone camera that I have locally here in my LAN, the setup includes:

Watching the RTSP stream from the same phone and setup using VLC works just fine! I dove in and tried to compare the network transport between VLC and and my own code, this are the results from the pcap files:

VLC

14:56:48.000000 IP 192.168.0.10.32763 > 192.168.1.1.rtsp: Flags [S], seq 0, win 4096, length 0
14:56:48.000100 IP 192.168.1.1.rtsp > 192.168.0.10.32763: Flags [S.], seq 0, ack 1, win 4096, length 0
14:56:48.000200 IP 192.168.0.10.32763 > 192.168.1.1.rtsp: Flags [.], ack 1, win 4096, length 0
14:56:49.420000 IP 192.168.0.10.32763 > 192.168.1.1.rtsp: Flags [P.], seq 1:126, ack 1, win 4096, length 125: RTSP: OPTIONS rtsp://192.168.1.1:554/track1 RTSP/1.0
14:56:49.427000 IP 192.168.1.1.rtsp > 192.168.0.10.32763: Flags [P.], seq 1:132, ack 126, win 4096, length 131: RTSP: RTSP/1.0 200 OK
14:56:49.429000 IP 192.168.0.10.32763 > 192.168.1.1.rtsp: Flags [P.], seq 126:277, ack 132, win 4096, length 151: RTSP: DESCRIBE rtsp://192.168.1.1:554/track1 RTSP/1.0
14:56:49.842000 IP 192.168.1.1.rtsp > 192.168.0.10.32763: Flags [P.], seq 132:947, ack 277, win 4096, length 815: RTSP: RTSP/1.0 200 OK
14:56:49.853000 IP 192.168.0.10.32763 > 192.168.1.1.rtsp: Flags [P.], seq 277:455, ack 947, win 4096, length 178: RTSP: SETUP rtsp://192.168.1.1/track1/track1 RTSP/1.0
14:56:49.869000 IP 192.168.1.1.rtsp > 192.168.0.10.32763: Flags [P.], seq 947:1128, ack 455, win 4096, length 181: RTSP: RTSP/1.0 200 OK
14:56:49.879000 IP 192.168.0.10.32763 > 192.168.1.1.rtsp: Flags [P.], seq 455:652, ack 1128, win 4096, length 197: RTSP: SETUP rtsp://192.168.1.1/track1/track2 RTSP/1.0
14:56:49.896000 IP 192.168.1.1.rtsp > 192.168.0.10.32763: Flags [P.], seq 1128:1309, ack 652, win 4096, length 181: RTSP: RTSP/1.0 200 OK
14:56:49.946000 IP 192.168.0.10.32763 > 192.168.1.1.rtsp: Flags [P.], seq 652:809, ack 1309, win 4096, length 157: RTSP: PLAY rtsp://192.168.1.1/track1/ RTSP/1.0
14:56:49.950000 IP 192.168.1.1.rtsp > 192.168.0.10.32763: Flags [P.], seq 1309:1534, ack 809, win 4096, length 225: RTSP: RTSP/1.0 200 OK
14:56:55.374000 IP 192.168.0.10.32763 > 192.168.1.1.rtsp: Flags [P.], seq 809:951, ack 1534, win 4096, length 142: RTSP: TEARDOWN rtsp://192.168.1.1/track1/ RTSP/1.0
14:56:55.560000 IP 192.168.1.1.rtsp > 192.168.0.10.32763: Flags [P.], seq 1534:1578, ack 951, win 4096, length 44: RTSP: RTSP/1.0 200 OK

FFmpegFrameGrabber

15:14:13.000000 IP 192.168.0.10.32763 > 192.168.1.1.rtsp: Flags [S], seq 0, win 4096, length 0
15:14:13.000100 IP 192.168.1.1.rtsp > 192.168.0.10.32763: Flags [S.], seq 0, ack 1, win 4096, length 0
15:14:13.000200 IP 192.168.0.10.32763 > 192.168.1.1.rtsp: Flags [.], ack 1, win 4096, length 0
15:14:14.316000 IP 192.168.0.10.32763 > 192.168.1.1.rtsp: Flags [P.], seq 1:81, ack 1, win 4096, length 80: RTSP: OPTIONS rtsp://192.168.1.1:554/ RTSP/1.0
15:14:14.324000 IP 192.168.1.1.rtsp > 192.168.0.10.32763: Flags [P.], seq 1:132, ack 81, win 4096, length 131: RTSP: RTSP/1.0 200 OK
15:14:14.327000 IP 192.168.0.10.32763 > 192.168.1.1.rtsp: Flags [P.], seq 81:187, ack 132, win 4096, length 106: RTSP: DESCRIBE rtsp://192.168.1.1:554/ RTSP/1.0
15:14:14.929000 IP 192.168.1.1.rtsp > 192.168.0.10.32763: Flags [P.], seq 132:941, ack 187, win 4096, length 809: RTSP: RTSP/1.0 200 OK
15:14:14.938000 IP 192.168.0.10.32763 > 192.168.1.1.rtsp: Flags [P.], seq 187:323, ack 941, win 4096, length 136: RTSP: SETUP rtsp://192.168.1.1//track1/track2 RTSP/1.0
15:14:15.109000 IP 192.168.1.1.rtsp > 192.168.0.10.32763: Flags [P.], seq 941:1096, ack 323, win 4096, length 155: RTSP: RTSP/1.0 200 OK
15:14:15.117000 IP 192.168.0.10.32763 > 192.168.1.1.rtsp: Flags [P.], seq 323:435, ack 1096, win 4096, length 112: RTSP: PLAY rtsp://192.168.1.1// RTSP/1.0
15:14:15.122000 IP 192.168.1.1.rtsp > 192.168.0.10.32763: Flags [P.], seq 1096:1154, ack 435, win 4096, length 58: RTSP: RTSP/1.0 404 Stream Not Found

As you can see in the FFmpegFrameGrabber pcap, for some reason it tries to setup track1/track2 fails and doesn't try any other combination (unlike the VLC) instead of separating the tracks, and then shows 404 not found for the stream. I'm not sure why this happens or what I'm doing wrong, running the exact same code with a public rtsp (rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov) stream over the internet worked perfect.

Do you have any idea why does this happen? Any options I can set to fix this? Any help is much appreciated! Thanks.

saudet commented 5 years ago

Does it do the same thing with the ffplay or the ffmpeg program?

saudet commented 5 years ago

BTW, we can use the ffmpeg program very easily even on Android: http://bytedeco.org/javacpp-presets/ffmpeg/apidocs/org/bytedeco/ffmpeg/ffmpeg.html

slobglob commented 5 years ago

@saudet Tried to run ffmpeg -i rtsp://192.168.1.1 -r 15 example.mp4 from my mac while connected to that camera, also nogo. What do you think are the possible issues here? Can any ffmpeg flag fix this problem? Or it's something more complicated?

saudet commented 5 years ago

There's probably a way to get it working with some options, yes.

slobglob commented 5 years ago

I'm looking at this https://ffmpeg.org/ffmpeg-protocols.html#rtsp, do you have any idea what might be the key for this?

saudet commented 5 years ago

setOption() should work.

slobglob commented 5 years ago

Ok, I'm trying to attack this from ffmpeg flags perspective, I guess this isn't related directly to this lib code. You can leave this open and I'll update this once I'll have a solution / dead end, if it's not relevant you can close this. Thanks.

slobglob commented 5 years ago

It looks like ffmpeg has some issues running RTSP with the specific model of camera that I'm using. I have to recompile it in order for it to work. I guess this can be closed as it has nothing to do with this project eventually. Thanks anyways!

saudet commented 5 years ago

We can apply the same fix for JavaCV. Could you send a pull request with the patch?

slobglob commented 5 years ago

@saudet tbh, I'm looking for a different solution then recompiling ffmpeg, if I won't find anything i might do it. If so, I'll open a PR fixing it. Thanks!