Skip to content

Commit

Permalink
Fixes #38
Browse files Browse the repository at this point in the history
  • Loading branch information
SuRGeoNix committed May 16, 2021
1 parent 42a5065 commit 1cb1ba1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
52 changes: 36 additions & 16 deletions FlyleafLib/MediaFramework/MediaDemuxer/DemuxerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,20 +117,32 @@ public int Open(string url, Stream stream, Dictionary<string, string> opt)

// Open Format Context
AVFormatContext* fmtCtxPtr = fmtCtx;
ret = avformat_open_input(&fmtCtxPtr, stream == null ? url : null, null, &avopt);
if (ret < 0) { Log($"[Format] [ERROR-1] {Utils.FFmpeg.ErrorCodeToMsg(ret)} ({ret})"); return ret; }
lock (lockFmtCtx)
ret = avformat_open_input(&fmtCtxPtr, stream == null ? url : null, null, &avopt);
if (ret < 0) { Log($"[Format] [ERROR-1] {Utils.FFmpeg.ErrorCodeToMsg(ret)} ({ret})"); fmtCtx = null; return ret; }
if (Status != Status.Opening) return -1;

// Find Streams Info
ret = avformat_find_stream_info(fmtCtx, null);
lock (lockFmtCtx)
{
if (Status != Status.Opening) return -1;
ret = avformat_find_stream_info(fmtCtx, null);
}
if (ret < 0) { Log($"[Format] [ERROR-2] {Utils.FFmpeg.ErrorCodeToMsg(ret)} ({ret})"); avformat_close_input(&fmtCtxPtr); return ret; }

bool hasVideo = FillInfo();
if (Type == MediaType.Video && !hasVideo)
{ Log($"[Format] [ERROR-3] No video stream found"); avformat_close_input(&fmtCtxPtr); return -3; }
else if (Type == MediaType.Audio && AudioStreams.Count == 0)
{ Log($"[Format] [ERROR-4] No audio stream found"); avformat_close_input(&fmtCtxPtr); return -4; }
else if (Type == MediaType.Subs && SubtitlesStreams.Count == 0)
{ Log($"[Format] [ERROR-5] No subtitles stream found"); avformat_close_input(&fmtCtxPtr); return -5; }
if (Status != Status.Opening) return -1;

lock (lockFmtCtx)
{
if (Status != Status.Opening) return -1;
bool hasVideo = FillInfo();

if (Type == MediaType.Video && !hasVideo)
{ Log($"[Format] [ERROR-3] No video stream found"); avformat_close_input(&fmtCtxPtr); return -3; }
else if (Type == MediaType.Audio && AudioStreams.Count == 0)
{ Log($"[Format] [ERROR-4] No audio stream found"); avformat_close_input(&fmtCtxPtr); return -4; }
else if (Type == MediaType.Subs && SubtitlesStreams.Count == 0)
{ Log($"[Format] [ERROR-5] No subtitles stream found"); avformat_close_input(&fmtCtxPtr); return -5; }
}

StartThread();

Expand Down Expand Up @@ -243,10 +255,13 @@ public void Pause()
Status = Status.Pausing;
while (Status == Status.Pausing) Thread.Sleep(5);
}

//[System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptions]
//[System.Security.SecurityCritical]
public void Stop()
{
if (Status == Status.Stopped) return;

StopThread();

// Free Streams
Expand All @@ -259,13 +274,18 @@ public void Stop()
DisposePackets(AudioPackets);
DisposePackets(VideoPackets);
DisposePackets(SubtitlesPackets);
if (packet != null) fixed (AVPacket** ptr = &packet) av_packet_free(ptr);

// Close Format / Custom Contexts
if (fmtCtx != null) fixed (AVFormatContext** ptr = &fmtCtx) { avformat_close_input(ptr); fmtCtx = null; }
CustomIOContext.Dispose();
lock (lockFmtCtx)
{
if (fmtCtx != null)
fixed (AVFormatContext** ptr = &fmtCtx) { avformat_close_input(ptr); fmtCtx = null; }

Status = Status.Stopped;
if (packet != null) fixed (AVPacket** ptr = &packet) av_packet_free(ptr);
CustomIOContext.Dispose();

Status = Status.Stopped;
}
}
public void StopThread()
{
Expand Down
1 change: 1 addition & 0 deletions FlyleafLib/MediaPlayer/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@ public void Seek(int ms, bool foreward = false)
/// </summary>
public void Stop()
{
decoder.Stop();
lock (lockSeek)
lock (lockOpen)
Initialize();
Expand Down

0 comments on commit 1cb1ba1

Please sign in to comment.