Skip to content

Commit

Permalink
Fixes #38
Browse files Browse the repository at this point in the history
  • Loading branch information
SuRGeoNix committed May 17, 2021
1 parent 2fa0eaf commit 5f5c2ec
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 41 deletions.
52 changes: 27 additions & 25 deletions FlyleafLib/MediaFramework/MediaDecoder/DecoderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,40 +50,42 @@ public DecoderBase(MediaContext.DecoderContext decCtx)

public int Open(StreamBase stream)
{
int ret;

Stop();
lock (stream.Demuxer.lockFmtCtx)
{
int ret;

Status = Status.Opening;
Stream = stream;
demuxer = stream.Demuxer;
Stop();
Status = Status.Opening;
Stream = stream;
demuxer = stream.Demuxer;

AVCodec* codec = avcodec_find_decoder(stream.AVStream->codecpar->codec_id);
if (codec == null)
{ Log($"[CodecOpen {Type}] [ERROR-1] No suitable codec found"); return -1; }
AVCodec* codec = avcodec_find_decoder(stream.AVStream->codecpar->codec_id);
if (codec == null)
{ Log($"[CodecOpen {Type}] [ERROR-1] No suitable codec found"); return -1; }

codecCtx = avcodec_alloc_context3(null);
if (codecCtx == null)
{ Log($"[CodecOpen {Type}] [ERROR-2] Failed to allocate context3"); return -1; }
codecCtx = avcodec_alloc_context3(null);
if (codecCtx == null)
{ Log($"[CodecOpen {Type}] [ERROR-2] Failed to allocate context3"); return -1; }

ret = avcodec_parameters_to_context(codecCtx, stream.AVStream->codecpar);
if (ret < 0)
{ Log($"[CodecOpen {Type}] [ERROR-3] {Utils.FFmpeg.ErrorCodeToMsg(ret)} ({ret})"); return ret; }
ret = avcodec_parameters_to_context(codecCtx, stream.AVStream->codecpar);
if (ret < 0)
{ Log($"[CodecOpen {Type}] [ERROR-3] {Utils.FFmpeg.ErrorCodeToMsg(ret)} ({ret})"); return ret; }

codecCtx->pkt_timebase = stream.AVStream->time_base;
codecCtx->codec_id = codec->id;
codecCtx->pkt_timebase = stream.AVStream->time_base;
codecCtx->codec_id = codec->id;

ret = Setup(codec);
if (ret < 0) return ret;
ret = Setup(codec);
if (ret < 0) return ret;

ret = avcodec_open2(codecCtx, codec, null);
if (ret < 0) return ret;
ret = avcodec_open2(codecCtx, codec, null);
if (ret < 0) return ret;

frame = av_frame_alloc();
demuxer.EnableStream(Stream);
StartThread();
frame = av_frame_alloc();
demuxer.EnableStream(Stream);
StartThread();

return ret; // 0 for success
return ret; // 0 for success
}
}

public virtual void Stop()
Expand Down
32 changes: 16 additions & 16 deletions FlyleafLib/MediaFramework/MediaDemuxer/DemuxerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public ConcurrentQueue<IntPtr> GetPacketsPtr(MediaType type)

AVFormatContext*fmtCtx;
AVPacket* packet;
object lockFmtCtx = new object();
internal object lockFmtCtx = new object();

Config cfg;

Expand Down Expand Up @@ -142,11 +142,11 @@ public int Open(string url, Stream stream, Dictionary<string, string> opt)
{ 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();
StartThread();

if (ret > 0) ret = 0;
if (ret > 0) ret = 0;
}
}
finally { if (Status == Status.Opening) Status = Status.Stopped; }

Expand Down Expand Up @@ -264,20 +264,20 @@ public void Stop()

StopThread();

// Free Streams
AudioStreams.Clear();
VideoStreams.Clear();
SubtitlesStreams.Clear();
EnabledStreams.Clear();
lock (lockFmtCtx)
{
// Free Streams
AudioStreams.Clear();
VideoStreams.Clear();
SubtitlesStreams.Clear();
EnabledStreams.Clear();

// Free Packets
DisposePackets(AudioPackets);
DisposePackets(VideoPackets);
DisposePackets(SubtitlesPackets);
// Free Packets
DisposePackets(AudioPackets);
DisposePackets(VideoPackets);
DisposePackets(SubtitlesPackets);

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

Expand Down

0 comments on commit 5f5c2ec

Please sign in to comment.