Skip to content

Commit

Permalink
Fixes another compatibility issue with Windows 10 (feature level < 11)
Browse files Browse the repository at this point in the history
+ Minor additional changes


Former-commit-id: ad69a95
  • Loading branch information
SuRGeoNix committed May 7, 2021
1 parent fd70b60 commit a11f383
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 21 deletions.
15 changes: 6 additions & 9 deletions FlyleafLib/MediaFramework/MediaDecoder/AudioDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ namespace FlyleafLib.MediaFramework.MediaDecoder
{
public unsafe class AudioDecoder : DecoderBase
{
public AudioStream AudioStream => (AudioStream) Stream;
public AudioStream AudioStream => (AudioStream) Stream;

public ConcurrentQueue<AudioFrame>
Frames { get; protected set; } = new ConcurrentQueue<AudioFrame>();
Frames { get; protected set; } = new ConcurrentQueue<AudioFrame>();

static AVSampleFormat AOutSampleFormat = AVSampleFormat.AV_SAMPLE_FMT_FLT;
static int AOutChannelLayout = AV_CH_LAYOUT_STEREO;
Expand Down Expand Up @@ -91,7 +91,6 @@ protected override void DecodeInternal()
Status = Status.Decoding;
}


// While Packets Queue Empty (Ended | Quit if Demuxer stopped | Wait until we get packets)
if (demuxer.AudioPackets.Count == 0)
{
Expand Down Expand Up @@ -137,9 +136,10 @@ protected override void DecodeInternal()
{
allowedErrors--;
Log($"[ERROR-2] {Utils.FFmpeg.ErrorCodeToMsg(ret)} ({ret})");
//avcodec_flush_buffers(codecCtx); ??

if (allowedErrors == 0) { Log("[ERROR-0] Too many errors!"); return; }

continue;
}
}
Expand All @@ -150,11 +150,7 @@ protected override void DecodeInternal()
if (ret != 0) { av_frame_unref(frame); break; }

AudioFrame mFrame = ProcessAudioFrame(frame);
if (mFrame != null)
{
//Log(Utils.TicksToTime((long)(mFrame.pts * VideoStream.Timebase)) + " | pts -> " + mFrame.pts);
Frames.Enqueue(mFrame);
}
if (mFrame != null) Frames.Enqueue(mFrame);

av_frame_unref(frame);
}
Expand All @@ -171,6 +167,7 @@ private AudioFrame ProcessAudioFrame(AVFrame* frame)
mFrame.pts = frame->best_effort_timestamp == AV_NOPTS_VALUE ? frame->pts : frame->best_effort_timestamp;
if (mFrame.pts == AV_NOPTS_VALUE) return null;
mFrame.timestamp = ((long)(mFrame.pts * AudioStream.Timebase) - AudioStream.StartTime) + cfg.audio.DelayTicks + (AudioStream.StartTime - decCtx.VideoDecoder.VideoStream.StartTime);
//Log(Utils.TicksToTime(mFrame.timestamp));

try
{
Expand Down
2 changes: 1 addition & 1 deletion FlyleafLib/MediaFramework/MediaDecoder/DecoderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void StartThread()
Status = Status.Opening;
thread = new Thread(() => Decode());
thread.Name = $"[#{decCtx.player.PlayerId}] [Decoder: {Type}]"; thread.IsBackground= true; thread.Start();
while (Status == Status.Opening) Thread.Sleep(5); // Wait for thread to come up
while (Status == Status.Opening) Thread.Sleep(5);
}
public void Start()
{
Expand Down
7 changes: 2 additions & 5 deletions FlyleafLib/MediaFramework/MediaDecoder/VideoDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,7 @@ protected override void DecodeInternal()
}

VideoFrame mFrame = ProcessVideoFrame(frame);
if (mFrame != null)
{
//Log(Utils.TicksToTime((long)(mFrame.pts * VideoStream.Timebase)) + " | pts -> " + mFrame.pts);
Frames.Enqueue(mFrame);
}
if (mFrame != null) Frames.Enqueue(mFrame);

av_frame_unref(frame);
}
Expand All @@ -242,6 +238,7 @@ internal VideoFrame ProcessVideoFrame(AVFrame* frame)
mFrame.pts = frame->best_effort_timestamp == AV_NOPTS_VALUE ? frame->pts : frame->best_effort_timestamp;
if (mFrame.pts == AV_NOPTS_VALUE) return null;
mFrame.timestamp = ((long)(mFrame.pts * VideoStream.Timebase) - VideoStream.StartTime) + cfg.audio.LatencyTicks;
//Log(Utils.TicksToTime(mFrame.timestamp));

// Hardware Frame (NV12|P010) | CopySubresourceRegion FFmpeg Texture Array -> Device Texture[1] (NV12|P010) / SRV (RX_RXGX) -> PixelShader (Y_UV)
if (VideoAccelerated)
Expand Down
6 changes: 6 additions & 0 deletions FlyleafLib/MediaFramework/MediaDemuxer/DemuxerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ public bool FillInfo()
AudioStreams.Add(new AudioStream(fmtCtx->streams[i]) { Demuxer = this });
mapInAVStreamsToStreams.Add(i, AudioStreams[AudioStreams.Count-1]);
break;

case AVMEDIA_TYPE_VIDEO:
// Might excludes valid video streams for rtsp/web cams? Better way to ensure that they are actually image streams? (fps maybe?)
if (avcodec_get_name(fmtCtx->streams[i]->codecpar->codec_id) == "mjpeg") { Log($"Excluding image stream #{i}"); continue; }
Expand All @@ -172,6 +173,7 @@ public bool FillInfo()
mapInAVStreamsToStreams.Add(i, VideoStreams[VideoStreams.Count-1]);
if (VideoStreams[VideoStreams.Count-1].PixelFormat != AVPixelFormat.AV_PIX_FMT_NONE) hasVideo = true;
break;

case AVMEDIA_TYPE_SUBTITLE:
SubtitlesStreams.Add(new SubtitlesStream(fmtCtx->streams[i]) { Demuxer = this, Converted = true, Downloaded = true });
mapInAVStreamsToStreams.Add(i, SubtitlesStreams[SubtitlesStreams.Count-1]);
Expand Down Expand Up @@ -563,6 +565,10 @@ private void Remux()
CurTime = (long) curTime;
}

// Fixes the StartTime/Duration (eg. for live streams) but changes the codec_tag / codec_id etc...
//packet->pts = av_rescale_q_rnd(((long)(packet->pts * mapInAVStreamsToStreams[in_stream->index].Timebase) - StartTime) / 10, av_get_time_base_q(), out_stream->time_base, AVRounding.AV_ROUND_NEAR_INF | AVRounding.AV_ROUND_PASS_MINMAX);
//packet->dts = av_rescale_q_rnd(((long)(packet->dts * mapInAVStreamsToStreams[in_stream->index].Timebase) - StartTime) / 10, av_get_time_base_q(), out_stream->time_base, AVRounding.AV_ROUND_NEAR_INF | AVRounding.AV_ROUND_PASS_MINMAX);

packet->pts = av_rescale_q_rnd(packet->pts, in_stream->time_base, out_stream->time_base, AVRounding.AV_ROUND_NEAR_INF | AVRounding.AV_ROUND_PASS_MINMAX);
packet->dts = av_rescale_q_rnd(packet->dts, in_stream->time_base, out_stream->time_base, AVRounding.AV_ROUND_NEAR_INF | AVRounding.AV_ROUND_PASS_MINMAX);
packet->duration = av_rescale_q(packet->duration,in_stream->time_base, out_stream->time_base);
Expand Down
5 changes: 1 addition & 4 deletions FlyleafLib/MediaPlayer/AudioPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,7 @@ public string Device
buffer.BufferLength = 1000 * 1024;
volumeSampleProvider= new VolumeSampleProvider(buffer.ToSampleProvider());

if (Device == Master.AudioMaster.DefaultDeviceName)
player = new DirectSoundOut(-30 + (int)(cfg.audio.LatencyTicks / 10000));
else
player = new DirectSoundOut(_Device == null ? Master.AudioMaster.DeviceIdNaudio : DeviceIdNaudio, -30 + (int)(cfg.audio.LatencyTicks / 10000));
player = new DirectSoundOut(_Device == null ? Master.AudioMaster.DeviceIdNaudio : DeviceIdNaudio, -30 + (int)(cfg.audio.LatencyTicks / 10000));

player.Init(volumeSampleProvider);
player.Play();
Expand Down
3 changes: 2 additions & 1 deletion FlyleafLib/MediaPlayer/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,8 @@ private bool MediaBuffer()
else
{
if (!decoder.VideoDecoder.IsRunning && !isVideoSwitch) { Log("[SCREAMER] Video Exhausted"); shouldStop= true; }
if (!decoder.AudioDecoder.IsRunning || Session.CurAudioStream.Demuxer.Status == MediaFramework.MediaDemuxer.Status.QueueFull) { Log("[SCREAMER] Audio Exhausted"); gotAudio = true; }
if (vFrame != null && !gotAudio && (!decoder.AudioDecoder.IsRunning || Session.CurAudioStream.Demuxer.Status == MediaFramework.MediaDemuxer.Status.QueueFull)) {
Log("[SCREAMER] Audio Exhausted"); gotAudio = true; }
}

Thread.Sleep(10);
Expand Down
2 changes: 1 addition & 1 deletion FlyleafLib/MediaRenderer/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public Renderer(Player player)
// Swap Chain (TODO: Backwards compatibility)
var desc1 = new SwapChainDescription1()
{
BufferCount = device.FeatureLevel >= SharpDX.Direct3D.FeatureLevel.Level_11_0 ? 6 : 1, // Should be 1 for Win < 8 | HDR 60 fps requires 6 for non drops
BufferCount = Utils.IsWin10 ? 6 : 1, // Should be 1 for Win < 8 | HDR 60 fps requires 6 for non drops
SwapEffect = Utils.IsWin10 ? SwapEffect.FlipSequential : SwapEffect.Discard,

//Format = HDREnabled ? Format.R10G10B10A2_UNorm : Format.B8G8R8A8_UNorm, // Create always 10 bit and fallback to 8?
Expand Down

0 comments on commit a11f383

Please sign in to comment.