BogdanovKirill / RtspClientSharp

Pure C# RTSP client for .NET Standard without external dependencies and with true async nature. I welcome contributions.
MIT License
706 stars 284 forks source link

Changing from ElasticBuffer to MemoryStream seems to have broken MJPEG streaming #55

Closed athibault42 closed 4 years ago

athibault42 commented 4 years ago

Hi,

While trying to use the client in a project, I noticed that while H264 streams worked beautifully, I had systematic receive timeouts for MJPEG streams, both in my code and with SimpleRtspPlayer. Looked into it and turns out the FrameReceived event was never raised while trying to get a MJPEG stream. Part of the problem seems to be in MJPEGVideoPayloadParser.Parse() line 145:

if (_previousTimeOffset != timeOffset && _frameStream.Position != 0)
                _frameStream.Position = 0;

This reset Position to 0 before

if (fragmentOffset == 0)
            {
                if (_frameStream.Position != 0)
                    GenerateFrame(timeOffset);
                ...

is called, so frames are never generated. Commenting lines 145/146 make it so frames are now generated, but then SimpleRtspPlayer still doesn't works because FFmpegVideoDecoder.TryDecode() returns null. Looks like FFmpegVideoPInvoke.DecodeFrame() return -3. Reverting changes to MJPEGVideoPayloadParser.cs, ElasticBuffer.cs and BufferOverflowException.cs like they were before the commit 3b1b21967217ec30d1ae3c62fdbd7ce409106646 make it so SimpleRtspPlayer works again with MJPEG streams.

Thank you. Great work, love the client.

BogdanovKirill commented 4 years ago

Hello !

Thank you very much for report. Please could you make some changes and test? You need to replace GenerateFrame method in MJPEGVideoPayloadParser: https://gist.github.com/BogdanovKirill/8fb7d7b4f6fd858ffe67d6f0b03c0082

athibault42 commented 4 years ago

Thanks for the quick response. Removing the lines

if (_previousTimeOffset != timeOffset && _frameStream.Position != 0)
                _frameStream.Position = 0;

and changing GenerateFrame to your updated version did the trick, I'm now getting images in both my code and SimpleRtspPlayer.

BogdanovKirill commented 4 years ago

What if these lines will stay? if (_previousTimeOffset != timeOffset && _frameStream.Position != 0) _frameStream.Position = 0;

athibault42 commented 4 years ago

With these lines in the code, FrameReceived is never raised, so I get a Receive Timeout exception. These lines reset Position to 0 just before the frame is due to be generated.

BogdanovKirill commented 4 years ago

Ok, thank you. Then it should be removed.

athibault42 commented 4 years ago

Thank you. Do you know when a new NuGet package will be available with the fix?

BogdanovKirill commented 4 years ago

Hello,

I've published 1.3.2 version. Please check and reopen if it would required.