NON906 / fftools_lib

Other
3 stars 5 forks source link

FFplay segmentation fault and FfmpegCaputure writeVideo BUG #2

Closed se7enXF closed 1 year ago

se7enXF commented 1 year ago

First of all, these two mistakes are accidental. I run my program in Linux standalone build. I debug for days but not find reason.

1. FFplay segmentation fault

Once the error happens, the program crashed.

image

2. FfmpegCaputure writeVideo Bug

Once the error happens, if I call FfmpegCommand.StopFfmpeg() function the program and editor will stuck forever. I debug in Windows editor and found that program running is stuck at ffmpegThread.Join() at line 159 in FfmpegCommandImpWinLib.cs. In ffmpegThread, running is stuck at line 99 ffmpeg_start() function.
This error is start with NullReferenceException: Object reference not set to an instance of an object, following log is in the picture.

image

NON906 commented 1 year ago

I tried it, but neither happened. How often and when does it occur?

Also, for 2., I attach the code with a try-catch in the place that seems to be the cause. Does this change behavior? FfmpegCaptureImpWinLib.zip

se7enXF commented 1 year ago

Thanks for your replay. Two problems occur about 5%.

For problem 2, I use your new code but problem still exist. Since this exception is not inevitable, I simulated an exception by throw new Exception in your try block. Error can be caught and log out, but if I call FfmpegCommand.csStopFfmpeg() function, the program is also stuck.

I think the stuck problem is that, ffmpeg_stop function do not stop the background ffmpeg process when writeVideo error, so ffmpeg_start is sutck. I have two suggestions:

  1. Find out why NullReferenceException occurs in writeVideo. I find the problem in google and this is most likely caused by multithreading. But I can't optimize the complex code. As it is incidental, maybe the next suggestion is more practical.
  2. We can start from why ffmpeg_stop is not really working. I guess that, if ffmpeg_stop works, ffmpeg_start will returns and all thread exit. I read the code in fftools_lib/ffmpeg.c but it is difficult to understand with my poor programming skills.

Thanks for your replay! I don't understand the logic of running functions in the dll, so I can only turn to you for help.

NON906 commented 1 year ago

I have changed the processing for the part that seems to be the cause. Will this change the behavior? FfmpegUnity_patch_lock.zip

se7enXF commented 1 year ago

Unfortunately, the same problem remains.

NON906 commented 1 year ago

I'm sorry, but I don't have that problem in my environment. So it's hard to fix as it is. Is there any way I can reproduce the problem?

se7enXF commented 1 year ago

You can write a line throw new Exception("test"); at the begining in function FfmpegCaptureImpWinLib.WriteVideo.
Add two button in UI, one for StartFfmpeg() and another for StopFfmpeg().
Play unity and click StartFfmpeg(), you can see the exception. And if click StopFfmpeg(), unity editor will stuck.

NON906 commented 1 year ago

It seems that ffmpeg will not start if an exception occurs at that timing. I changed it so that it doesn't stack even if it occurs, so please check it. FfmpegUnity_patch_lock2.zip

se7enXF commented 1 year ago

Unfortunately, it doesn't work for me. The same problem remains. Maybe WriteVideo thread should be started until variable videoBuffers_ initialization is completed. There is still a risk of multithreading conflicts by using videoBuffers.ContainsKey() in WriteVideo function to determine whether it is initialized or not.

NON906 commented 1 year ago

Maybe WriteVideo thread should be started until variable videoBuffers_ initialization is completed.

I changed it to look like the above. FfmpegUnity_patch_lock3.zip

se7enXF commented 1 year ago

I read the code in FfmpegUnity_patch_lock3.zip, and I will try it.
And I find that there is a try block in WriteAudio when using audioBuffers.ContainsKey(streamId). I will try it in WriteVideo.