I write Windows service witch decodig video stream from camera. I write it on c# with FFMPEG.Autogen wrapper.
My problem is "AccessViolationException" when i run it as service. If i run application as Console Application i have no exceptions.
In Stacktrace i see this:
в FFmpeg.AutoGen.ffmpeg+<>c.<.cctor>b__5_572(FFmpeg.AutoGen.AVFrame*, FFmpeg.AutoGen.AVFrame*, Int32)
в FFmpeg.AutoGen.ffmpeg.av_hwframe_transfer_data(FFmpeg.AutoGen.AVFrame*, FFmpeg.AutoGen.AVFrame*,Int32)
в VideoProviderService.VideoSources.RTSPVideoSource.TryDecodeNextFrame(Boolean ByRef)
Code of TryDecodeNextFrame method:
public IntPtr TryDecodeNextFrame(out bool state)
{
try
{
ffmpeg.av_frame_unref(pFrame);
ffmpeg.av_frame_unref(cpuFrame);
int error;
do
{
try
{
do
{
timeout = DateTime.Now.AddSeconds(2);
error = ffmpeg.av_read_frame(_pFormatContext, pPacket);
if (error == ffmpeg.AVERROR_EOF)
{
state = false;
return IntPtr.Zero;
}
error.ThrowExceptionIfError();
} while (pPacket->stream_index != _streamIndex);
ffmpeg.avcodec_send_packet(pCodecContext, pPacket).ThrowExceptionIfError();
}
finally
{
ffmpeg.av_packet_unref(pPacket);
}
error = ffmpeg.avcodec_receive_frame(pCodecContext, pFrame);
} while (error == ffmpeg.AVERROR(ffmpeg.EAGAIN));
error.ThrowExceptionIfError();
ffmpeg.av_hwframe_transfer_data(cpuFrame, pFrame, 0).ThrowExceptionIfError();
ptrToFrame = (IntPtr)vfc.Convert(*cpuFrame).data[0];
}
catch
{
state = false;
return IntPtr.Zero;
}
state = true;
return ptrToFrame;
}
What i tried to do:
- I checked arguments of
av_hwframe_transfer_data
. - I changed the user for the service.
- I tried compile as x86 or x64 configuration.
I have no idea how to solve this. Does anyone have any thoughts?
It seems that you are not handling properly your frames (pFrame/cpuFrame). Especially, for the cpuFrame you should alloc it in every run and free it at the end of every run. Additionally for the pFrame you should unref it directly after av_hw_frame_transfer_data. Eg:-