Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: How to cancel a session completely #33

Closed
nooitaf opened this issue Apr 26, 2021 · 17 comments
Closed

Question: How to cancel a session completely #33

nooitaf opened this issue Apr 26, 2021 · 17 comments
Assignees
Labels
android Affect Android platform bug Something isn't working fixed-in-v4.5 Fixed in release v4.5 library Affects the library v4.4 Affects v4.4 release

Comments

@nooitaf
Copy link

nooitaf commented Apr 26, 2021

I'm trying to cancel a running rtmp session on android started with FFprobeKit.executeAsync(), but for some reason it doesn't completely close the connection even tho the session says it's completed. I get a callback and it seems like it ended, but it doesn't. Also canceled sessions seem to stick around after they get canceled. Is there a way to remove them?

This is what i do to cancel all running sessions...

List<Session> sessions = FFmpegKitConfig.getSessions();
for (int i = 0; i < sessions.size(); i++) {
  Session session = sessions.get(i);
  FFmpegKit.cancel(session.getSessionId());
}

Also tried FFmpegKit.cancel() but i get the same result

From the first session (that doesn't stop) i get this when i cancel it which looks like its done..

Exiting normally, received signal 2.
, state=COMPLETED, returnCode=255, failStackTrace='null'}

But my rtmp server still thinks there is a connection and when i try to start a new one i get this instantly..

[rtmp @ 0x7e2d0fcf80] Server error: Stream already publishing
rtmp://10.0.0.200/live/android: Operation not permitted
, state=COMPLETED, returnCode=1, failStackTrace='null'}

When i start multiple sessions i get all tries listed as sessions from getSessions() with completed state and they never seem to go away.

I'm very new to android and java so maybe i'm just missing something simple, or this is just normal, but it doesn't seem right.
Is there maybe another way to just force kill all ffmpeg processes?

@tanersener tanersener added android Affect Android platform needs-analysis We don't know that this is. It must be investigated further labels Apr 26, 2021
@tanersener
Copy link
Collaborator

Generally logs (console output) explains what ffmpeg is doing. Have you checked them?

@tanersener tanersener self-assigned this Apr 26, 2021
@nooitaf
Copy link
Author

nooitaf commented Apr 26, 2021

Hmm, idk what other logs you mean, but the 2 snippets above are the last 2 or 3 lintes of the logs? I shortened them as the rest seems just normal working ffmpeg stdout. I can't see live whats going on but as soon as the session ends it gives me the FFmpegKitConfig.getLastCompletedSession()string which is very similar to what happens on linux stdout. Would Log.d in the apply give me live updates? I assumed it would also just call apply once.

@tanersener
Copy link
Collaborator

I shortened them as the rest seems just normal working ffmpeg stdout.

Would you mind sharing them? I'm sure they are normal. But I need to understand what ffmpeg is doing in order to make a comment about your issue.

Would Log.d in the apply give me live updates?

You mean the apply method of one of the callbacks?

@nooitaf
Copy link
Author

nooitaf commented Apr 26, 2021

This is my fork which i'm trying to move from mobile-ffmpeg to ffmpeg-kit..https://github.com/nooitaf/cordova-plugin-ffmpeg

Excuse the chaotic state of the java file but my first priority was getting rtmp working. So essentially only exec and cancel work right now. Maybe the code helps to shed some light on things.

I test it on my Android 9 device. with full-gpl 4.4 because i need the camera. But in that test i was just streaming a file, which works but never closes connection for some reason.

I can post the stdout after i slept (didn't save it, sry).
This is the command i use in chrome console to start on the phone.

ffmpeg.exec("-re -i file:///storage/emulated/0/DCIM/test.mp4 -c:v copy -c:a aac -ar 44100 -f flv rtmp://10.0.0.237/live/android", (success) => console.log(success), (failure) => console.log(failure));

@nooitaf
Copy link
Author

nooitaf commented Apr 26, 2021

Oh wait i still had the terminal output which is a little verbose with date and time but i think the same as the console...
https://pastebin.com/PMPrvGtr

@tanersener
Copy link
Collaborator

Also canceled sessions seem to stick around after they get canceled.

As Javadoc says, getSessions returns sessions in the history. If you don't need them you can decrease the size of session history using setSessionHistorySize.

@nooitaf
Copy link
Author

nooitaf commented Apr 27, 2021

As Javadoc says, getSessions returns sessions in the history. If you don't need them you can decrease the size of session history using setSessionHistorySize.

Ah, good to know,.. but i assume it won't force-kill still running connections i guess? Would it be possible to get the process-id of the ffmpeg process and sig-kill it that way? I remember having a similar issue with ffmpeg in nodejs, where i had to pause the piped stdin and sig-kill it, as it didn't want to listen to a normal term sometimes.

@tanersener
Copy link
Collaborator

Ah, good to know,.. but i assume it won't force-kill still running connections i guess? Would it be possible to get the process-id of the ffmpeg process and sig-kill it that way? I remember having a similar issue with ffmpeg in nodejs, where i had to pause the piped stdin and sig-kill it, as it didn't want to listen to a normal term sometimes.

I don't get exactly what you want to do there but I need to remind that ffmpeg-kit or ffmpeg doesn't have their own process id. There is only process there, the process that runs your application. Killing that process means killing your application.

And, the method that changes the history size does not know whether those sessions are still running or not. So, it won't take actions about them.

@nooitaf
Copy link
Author

nooitaf commented Apr 28, 2021

I was wondering why the rtmp timeout on the server never triggers and looked at the tcp packages on the server. After i cancel the ffmpeg session the phone still responses with ack on the port it used for the connection before. If i disable Wifi and re-enable it, the phone responses properly with reset and the server stops immediatly. At this point i can start another session with ffmpeg and it works again.


Server TCP dump

  • Starting streaming...
19:22:22.993690 IP 10.0.0.125.41234 > rtmpserver.local.1935: Flags [S], seq 3994281749, win 65535, options [mss 1460,sackOK,TS val 138135443 ecr 0,nop,wscale 8], length 0
19:22:22.993718 IP rtmpserver.local.1935 > 10.0.0.125.41234: Flags [S.], seq 4024949280, ack 3994281750, win 28960, options [mss 1460,sackOK,TS val 1490061515 ecr 138135443,nop,wscale 7], length 0
19:22:22.996801 IP 10.0.0.125.41234 > rtmpserver.local.1935: Flags [.], ack 1, win 343, options [nop,nop,TS val 138135446 ecr 1490061515], length 0
19:22:22.997345 IP 10.0.0.125.41234 > rtmpserver.local.1935: Flags [P.], seq 1:1538, ack 1, win 343, options [nop,nop,TS val 138135446 ecr 1490061515], length 1537
19:22:22.997351 IP rtmpserver.local.1935 > 10.0.0.125.41234: Flags [.], ack 1538, win 251, options [nop,nop,TS val 1490061516 ecr 138135446], length 0
19:22:22.997605 IP rtmpserver.local.1935 > 10.0.0.125.41234: Flags [.], seq 1:1449, ack 1538, win 251, options [nop,nop,TS val 1490061516 ecr 138135446], length 1448
19:22:22.997614 IP rtmpserver.local.1935 > 10.0.0.125.41234: Flags [.], seq 1449:2897, ack 1538, win 251, options [nop,nop,TS val 1490061516 ecr 138135446], length 1448
19:22:22.997772 IP rtmpserver.local.1935 > 10.0.0.125.41234: Flags [P.], seq 2897:3074, ack 1538, win 251, options [nop,nop,TS val 1490061516 ecr 138135446], length 177
19:22:22.999131 IP 10.0.0.125.41234 > rtmpserver.local.1935: Flags [.], ack 1449, win 354, options [nop,nop,TS val 138135447 ecr 1490061516], length 0
19:22:22.999680 IP 10.0.0.125.41234 > rtmpserver.local.1935: Flags [.], ack 2897, win 365, options [nop,nop,TS val 138135447 ecr 1490061516], length 0
19:22:22.999882 IP 10.0.0.125.41234 > rtmpserver.local.1935: Flags [.], ack 3074, win 365, options [nop,nop,TS val 138135447 ecr 1490061516], length 0
19:22:23.000670 IP 10.0.0.125.41234 > rtmpserver.local.1935: Flags [P.], seq 1538:3086, ack 3074, win 365, options [nop,nop,TS val 138135447 ecr 1490061516], length 1548
19:22:23.000688 IP rtmpserver.local.1935 > 10.0.0.125.41234: Flags [.], ack 3086, win 275, options [nop,nop,TS val 1490061517 ecr 138135447], length 0
19:22:23.002556 IP 10.0.0.125.41234 > rtmpserver.local.1935: Flags [P.], seq 3086:3227, ack 3074, win 365, options [nop,nop,TS val 138135447 ecr 1490061517], length 141
19:22:23.003011 IP rtmpserver.local.1935 > 10.0.0.125.41234: Flags [P.], seq 3074:3090, ack 3227, win 298, options [nop,nop,TS val 1490061517 ecr 138135447], length 16
19:22:23.042481 IP 10.0.0.125.41234 > rtmpserver.local.1935: Flags [.], ack 3090, win 365, options [nop,nop,TS val 138135458 ecr 1490061517], length 0
19:22:23.042495 IP rtmpserver.local.1935 > 10.0.0.125.41234: Flags [P.], seq 3090:3325, ack 3227, win 298, options [nop,nop,TS val 1490061527 ecr 138135458], length 235
19:22:23.043689 IP 10.0.0.125.41234 > rtmpserver.local.1935: Flags [.], ack 3325, win 377, options [nop,nop,TS val 138135458 ecr 1490061527], length 0
19:22:23.043823 IP 10.0.0.125.41234 > rtmpserver.local.1935: Flags [P.], seq 3227:3239, ack 3325, win 377, options [nop,nop,TS val 138135458 ecr 1490061527], length 12
19:22:23.083046 IP rtmpserver.local.1935 > 10.0.0.125.41234: Flags [.], ack 3239, win 298, options [nop,nop,TS val 1490061538 ecr 138135458], length 0
19:22:23.084608 IP 10.0.0.125.41234 > rtmpserver.local.1935: Flags [P.], seq 3239:3360, ack 3325, win 377, options [nop,nop,TS val 138135468 ecr 1490061538], length 121
19:22:23.084638 IP rtmpserver.local.1935 > 10.0.0.125.41234: Flags [.], ack 3360, win 298, options [nop,nop,TS val 1490061538 ecr 138135468], length 0
19:22:23.084887 IP rtmpserver.local.1935 > 10.0.0.125.41234: Flags [P.], seq 3325:3366, ack 3360, win 298, options [nop,nop,TS val 1490061538 ecr 138135468], length 41
19:22:23.087125 IP 10.0.0.125.41234 > rtmpserver.local.1935: Flags [P.], seq 3360:3372, ack 3366, win 377, options [nop,nop,TS val 138135469 ecr 1490061538], length 12
19:22:23.127042 IP rtmpserver.local.1935 > 10.0.0.125.41234: Flags [.], ack 3372, win 298, options [nop,nop,TS val 1490061549 ecr 138135469], length 0
19:22:23.128763 IP 10.0.0.125.41234 > rtmpserver.local.1935: Flags [P.], seq 3372:3409, ack 3366, win 377, options [nop,nop,TS val 138135479 ecr 1490061549], length 37
19:22:23.128768 IP rtmpserver.local.1935 > 10.0.0.125.41234: Flags [.], ack 3409, win 298, options [nop,nop,TS val 1490061549 ecr 138135479], length 0
19:22:23.130958 IP rtmpserver.local.1935 > 10.0.0.125.41234: Flags [P.], seq 3366:3498, ack 3409, win 298, options [nop,nop,TS val 1490061549 ecr 138135479], length 132
19:22:23.149940 IP 10.0.0.125.41234 > rtmpserver.local.1935: Flags [P.], seq 3409:3421, ack 3498, win 377, options [nop,nop,TS val 138135484 ecr 1490061549], length 12
19:22:23.187042 IP rtmpserver.local.1935 > 10.0.0.125.41234: Flags [.], ack 3421, win 298, options [nop,nop,TS val 1490061564 ecr 138135484], length 0
19:22:23.188612 IP 10.0.0.125.41234 > rtmpserver.local.1935: Flags [P.], seq 3421:4132, ack 3498, win 377, options [nop,nop,TS val 138135494 ecr 1490061564], length 711
19:22:23.188617 IP rtmpserver.local.1935 > 10.0.0.125.41234: Flags [.], ack 4132, win 320, options [nop,nop,TS val 1490061564 ecr 138135494], length 0
19:22:23.209419 IP 10.0.0.125.41234 > rtmpserver.local.1935: Flags [P.], seq 4132:4140, ack 3498, win 377, options [nop,nop,TS val 138135499 ecr 1490061564], length 8
19:22:23.209473 IP rtmpserver.local.1935 > 10.0.0.125.41234: Flags [.], ack 4140, win 320, options [nop,nop,TS val 1490061569 ecr 138135499], length 0
19:22:23.210010 IP 10.0.0.125.41234 > rtmpserver.local.1935: Flags [.], seq 4140:5588, ack 3498, win 377, options [nop,nop,TS val 138135499 ecr 1490061564], length 1448
19:22:23.210012 IP rtmpserver.local.1935 > 10.0.0.125.41234: Flags [.], ack 5588, win 343, options [nop,nop,TS val 1490061569 ecr 138135499], length 0
19:22:23.210243 IP 10.0.0.125.41234 > rtmpserver.local.1935: Flags [.], seq 5588:7036, ack 3498, win 377, options [nop,nop,TS val 138135499 ecr 1490061564], length 1448
19:22:23.210244 IP rtmpserver.local.1935 > 10.0.0.125.41234: Flags [.], ack 7036, win 365, options [nop,nop,TS val 1490061569 ecr 138135499], length 0
19:22:23.210505 IP 10.0.0.125.41234 > rtmpserver.local.1935: Flags [.], seq 7036:8484, ack 3498, win 377, options [nop,nop,TS val 138135499 ecr 1490061564], length 1448
19:22:23.210506 IP rtmpserver.local.1935 > 10.0.0.125.41234: Flags [.], ack 8484, win 388, options [nop,nop,TS val 1490061569 ecr 138135499], length 0
19:22:23.210723 IP 10.0.0.125.41234 > rtmpserver.local.1935: Flags [.], seq 8484:9932, ack 3498, win 377, options [nop,nop,TS val 138135499 ecr 1490061564], length 1448
19:22:23.210736 IP rtmpserver.local.1935 > 10.0.0.125.41234: Flags [.], ack 9932, win 411, options [nop,nop,TS val 1490061569 ecr 138135499], length 0
...
[ repeating stream packages ]
...
19:22:49.737053 IP 10.0.0.125.41234 > rtmpserver.local.1935: Flags [.], seq 145181819:145184715, ack 3498, win 377, options [nop,nop,TS val 138142131 ecr 1490068186], length 2896
19:22:49.737055 IP rtmpserver.local.1935 > 10.0.0.125.41234: Flags [.], ack 145184715, win 8178, options [nop,nop,TS val 1490068201 ecr 138142131], length 0
19:22:49.737286 IP 10.0.0.125.41234 > rtmpserver.local.1935: Flags [.], seq 145184715:145186163, ack 3498, win 377, options [nop,nop,TS val 138142131 ecr 1490068186], length 1448
19:22:49.737580 IP 10.0.0.125.41234 > rtmpserver.local.1935: Flags [.], seq 145186163:145189059, ack 3498, win 377, options [nop,nop,TS val 138142131 ecr 1490068186], length 2896
19:22:49.737582 IP rtmpserver.local.1935 > 10.0.0.125.41234: Flags [.], ack 145189059, win 8178, options [nop,nop,TS val 1490068201 ecr 138142131], length 0
19:22:49.737769 IP 10.0.0.125.41234 > rtmpserver.local.1935: Flags [.], seq 145189059:145190507, ack 3498, win 377, options [nop,nop,TS val 138142131 ecr 1490068186], length 1448
19:22:49.738064 IP 10.0.0.125.41234 > rtmpserver.local.1935: Flags [.], seq 145190507:145191955, ack 3498, win 377, options [nop,nop,TS val 138142131 ecr 1490068186], length 1448
19:22:49.738065 IP rtmpserver.local.1935 > 10.0.0.125.41234: Flags [.], ack 145191955, win 8178, options [nop,nop,TS val 1490068201 ecr 138142131], length 0
19:22:49.738322 IP 10.0.0.125.41234 > rtmpserver.local.1935: Flags [.], seq 145191955:145193403, ack 3498, win 377, options [nop,nop,TS val 138142131 ecr 1490068186], length 1448
19:22:49.743725 IP 10.0.0.125.41234 > rtmpserver.local.1935: Flags [P.], seq 145193403:145194010, ack 3498, win 377, options [nop,nop,TS val 138142132 ecr 1490068198], length 607
19:22:49.743737 IP rtmpserver.local.1935 > 10.0.0.125.41234: Flags [.], ack 145194010, win 8178, options [nop,nop,TS val 1490068203 ecr 138142131], length 0
19:22:53.003095 IP rtmpserver.local.1935 > 10.0.0.125.41234: Flags [P.], seq 3498:3516, ack 145194010, win 8178, options [nop,nop,TS val 1490069018 ecr 138142131], length 18
19:22:53.206681 IP 10.0.0.125.41234 > rtmpserver.local.1935: Flags [.], ack 3516, win 377, options [nop,nop,TS val 138142999 ecr 1490069018], length 0
  • FFmpeg on phone canceled, or video ended

  • Server pings phone:

19:23:23.002814 IP rtmpserver.local.1935 > 10.0.0.125.41234: Flags [P.], seq 3516:3534, ack 145194010, win 8178, options [nop,nop,TS val 1490076517 ecr 138142999], length 18
19:23:23.251041 IP rtmpserver.local.1935 > 10.0.0.125.41234: Flags [P.], seq 3516:3534, ack 145194010, win 8178, options [nop,nop,TS val 1490076580 ecr 138142999], length 18
19:23:23.274320 IP 10.0.0.125.41234 > rtmpserver.local.1935: Flags [.], ack 3534, win 377, options [nop,nop,TS val 138150515 ecr 1490076517], length 0
19:23:23.274465 IP 10.0.0.125.41234 > rtmpserver.local.1935: Flags [.], ack 3534, win 377, options [nop,nop,TS val 138150515 ecr 1490076580,nop,nop,sack 1 {3516:3534}], length 0
19:23:53.002750 IP rtmpserver.local.1935 > 10.0.0.125.41234: Flags [P.], seq 3534:3552, ack 145194010, win 8178, options [nop,nop,TS val 1490084017 ecr 138150515], length 18
...
[ repeating ]
...
19:28:23.004715 IP 10.0.0.125.41234 > rtmpserver.local.1935: Flags [.], ack 3714, win 377, options [nop,nop,TS val 138225448 ecr 1490151518], length 0
19:28:53.003341 IP rtmpserver.local.1935 > 10.0.0.125.41234: Flags [P.], seq 3714:3732, ack 145194010, win 8178, options [nop,nop,TS val 1490159018 ecr 138225448], length 18
19:28:53.211337 IP 10.0.0.125.41234 > rtmpserver.local.1935: Flags [.], ack 3732, win 377, options [nop,nop,TS val 138233000 ecr 1490159018], length 0
19:29:23.003671 IP rtmpserver.local.1935 > 10.0.0.125.41234: Flags [P.], seq 3732:3750, ack 145194010, win 8178, options [nop,nop,TS val 1490166518 ecr 138233000], length 18
19:29:23.112164 IP 10.0.0.125.41234 > rtmpserver.local.1935: Flags [.], ack 3750, win 377, options [nop,nop,TS val 138240475 ecr 1490166518], length 0
  • Wifi diabled and re-enabled
19:29:53.004028 IP rtmpserver.local.1935 > 10.0.0.125.41234: Flags [P.], seq 3750:3768, ack 145194010, win 8178, options [nop,nop,TS val 1490174018 ecr 138240475], length 18
19:29:53.124211 IP 10.0.0.125.41234 > rtmpserver.local.1935: Flags [R], seq 4139475759, win 0, length 0
  • Server stops it's rtmp session

@tanersener
Copy link
Collaborator

My tests confirm that cancel method doesn't close rtmp connections properly. I'm not sure whether it must send an rtmp termination message or tcp connection must be closed to stop it. Details must be analysed.

But if you try to stream data to the same endpoint again it will fail. My rtmp server has a auto-clean feature, it cleans these hanging rtmp connections in 15 seconds. If I start another session on 16th second it doesn't receive any errors. That can be used as a temporary workaround.

Most probably this is a bug in ffmpeg-kits cancel mechanism. But a more detailed investigation is still necessary to understand where exactly this comes from.

@tanersener tanersener added bug Something isn't working v4.4 Affects v4.4 release LTS Affects LTS releases and removed needs-analysis We don't know that this is. It must be investigated further labels Apr 29, 2021
@tanersener
Copy link
Collaborator

Fixed in development.

@nooitaf
Copy link
Author

nooitaf commented May 1, 2021

Awesome! \o/

@tanersener tanersener added fixed-on-development Fixed on the development branch. Not released yet. library Affects the library labels May 1, 2021
@tanersener tanersener removed the LTS Affects LTS releases label May 1, 2021
@remymatheus
Copy link

This is my fork which i'm trying to move from mobile-ffmpeg to ffmpeg-kit..https://github.com/nooitaf/cordova-plugin-ffmpeg

Excuse the chaotic state of the java file but my first priority was getting rtmp working. So essentially only exec and cancel work right now. Maybe the code helps to shed some light on things.

I test it on my Android 9 device. with full-gpl 4.4 because i need the camera. But in that test i was just streaming a file, which works but never closes connection for some reason.

I can post the stdout after i slept (didn't save it, sry).
This is the command i use in chrome console to start on the phone.

ffmpeg.exec("-re -i file:///storage/emulated/0/DCIM/test.mp4 -c:v copy -c:a aac -ar 44100 -f flv rtmp://10.0.0.237/live/android", (success) => console.log(success), (failure) => console.log(failure));

Hi friend, did you continue working on the implementation of ffmpeg-kit in Cordova?

@nooitaf
Copy link
Author

nooitaf commented May 16, 2021

Did you continue working on the implementation of ffmpeg-kit in Cordova?

I had to put it on hold for a while so i removed the repo, but i can ping you when i recreate it.

@DuyIoT
Copy link

DuyIoT commented Jul 2, 2021

@tanersener Same issue.
I using ffmpeg kit merge 2 videos, and when i cancel(use ffmpegkit.cancel), the file in progress still generate the ts file and return state failed.
It's a feature or bug?

@tanersener
Copy link
Collaborator

@DuyIoT I don't know the details of your case. Hard to say if it the expected behaviour or another symptom of the bug fixed in this thread.

@arthenica arthenica deleted a comment from github-actions bot Sep 1, 2021
@tanersener tanersener added fixed-in-v4.5 Fixed in release v4.5 and removed fixed-on-development Fixed on the development branch. Not released yet. labels Sep 20, 2021
sagarpshah added a commit to sagarpshah/ffmpeg-kit that referenced this issue Jan 9, 2022
@sagarpshah
Copy link

I would recommend to below steps to cancel a session

  1. use your own executor service like below
    ExecutorService executorService = Executors.newFixedThreadPool(10);

  2. Instead of using session created by FFmpegKit.executeAsync create your own FFmpegSession using the required constructor.

  3. Create your own execution task by extending AsyncFFmpegExecuteTask. Override the run method as below
    public void run() { try { if (Thread.currentThread().isInterrupted()) { return; } else { super.run(); } } catch (Exception ex) {} }

  4. Create new object of your execution task and submit it in your executorService. This will create a new object of Future. Preserve this object.

  5. Now calling cancel() method on your Future object will cancel your session completely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
android Affect Android platform bug Something isn't working fixed-in-v4.5 Fixed in release v4.5 library Affects the library v4.4 Affects v4.4 release
Projects
None yet
Development

No branches or pull requests

5 participants