Exemple #1
0
 public async Task DelayAsync(TimeSpan ts)
 {
     if (await HandlePause() == false)
     {
         await DelayProvider.DelayAsync(ts);
     }
 }
Exemple #2
0
 public async Task YieldAsync()
 {
     if (await HandlePause() == false)
     {
         await DelayProvider.YieldAsync();
     }
 }
Exemple #3
0
 public void WaitOne_TakesCertainTime(DelayProvider.DelayStrategy strategy)
 {
     using (var delay = new DelayProvider(strategy))
     {
         var time = delay.WaitOne();
         var mil  = time.Milliseconds;
         Assert.GreaterOrEqual(mil, 1, $"Strategy {strategy}");
     }
 }
Exemple #4
0
        public async Task <bool> HandlePause()
        {
            var ret = false;

            while (IsPaused != null && IsPaused.Invoke())
            {
                ret = true;
                await DelayProvider.YieldAsync();
            }
            return(ret);
        }
Exemple #5
0
        private bool DelayDecoder(DelayProvider delay, int decodedFrameCount)
        {
            // We don't delay if there was output or there is a command
            // or a stop operation pending
            if (decodedFrameCount > 0 || Commands.IsStopWorkersPending || Commands.HasQueuedCommands)
            {
                return(false);
            }

            // Introduce a delay if the conditions above were not satisfied
            delay.WaitOne();
            return(true);
        }
Exemple #6
0
        protected override async Task <IBankResponse> CallBank(PayingAttempt payingAttempt, CancellationToken cancellationToken)
        {
            // Simulate bank response delay
            var delay = DelayProvider.Delays();
            await Task.Delay(delay, cancellationToken);

            // Call bank's service
            var         request  = new BNPPaymentRequest(payingAttempt.GatewayPaymentId, payingAttempt.Amount, payingAttempt.Currency, payingAttempt.CardCvv, payingAttempt.CardExpiry, payingAttempt.CardNumber);
            BNPResponse response = await _bnp.RespondToPayment(request);

            _paymentIdsMapper.RememberMapping(new AcquiringBankPaymentId(response.BankPaymentId), new GatewayPaymentId(response.GatewayPaymentId));
            return(AdaptToBankResponse(response));
        }
        /// <summary>
        /// Continually decodes the available packet buffer to have as
        /// many frames as possible in each frame queue and
        /// up to the MaxFrames on each component
        /// </summary>
        internal void RunFrameDecodingWorker()
        {
            #region Worker State Setup

            // The delay provider prevents 100% core usage
            var delay = new DelayProvider();

            // State variables
            var decodedFrameCount = 0;
            var wallClock         = TimeSpan.Zero;
            var rangePercent      = 0d;
            var isInRange         = false;
            var playAfterSeek     = false;

            // Holds the main media type
            var main = Container.Components.Main.MediaType;

            // Holds the auxiliary media types
            var auxs = Container.Components.MediaTypes.ExcludeMediaType(main);

            // Holds all components
            var all = Container.Components.MediaTypes.DeepCopy();

            var isBuffering     = false;
            var resumeClock     = false;
            var hasPendingSeeks = false;

            MediaComponent   comp   = null;
            MediaBlockBuffer blocks = null;

            #endregion

            #region Worker Loop

            try
            {
                while (IsTaskCancellationPending == false)
                {
                    #region 1. Setup the Decoding Cycle

                    // Singal a Seek starting operation
                    hasPendingSeeks = Commands.PendingCountOf(MediaCommandType.Seek) > 0;
                    if (State.IsSeeking == false && hasPendingSeeks)
                    {
                        playAfterSeek   = State.IsPlaying;
                        State.IsSeeking = true;
                        SendOnSeekingStarted();
                    }

                    // Execute the following command at the beginning of the cycle
                    Commands.ProcessNext();

                    // Wait for a seek operation to complete (if any)
                    // and initiate a frame decoding cycle.
                    SeekingDone.Wait();

                    // Set initial state
                    wallClock         = WallClock;
                    decodedFrameCount = 0;

                    // Signal a Seek ending operation
                    // TOD: Maybe this should go on the block rendering worker?
                    hasPendingSeeks = Commands.PendingCountOf(MediaCommandType.Seek) > 0;
                    if (State.IsSeeking && hasPendingSeeks == false)
                    {
                        // Detect a end of seek cycle and update to the final position
                        wallClock = SnapToFramePosition(WallClock);
                        Clock.Update(wallClock);
                        State.UpdatePosition(wallClock);

                        // Call the seek method on all renderers
                        foreach (var kvp in Renderers)
                        {
                            LastRenderTime[kvp.Key] = TimeSpan.MinValue;
                            kvp.Value.Seek();
                        }

                        SendOnSeekingEnded();
                        State.IsSeeking = false;
                        if (playAfterSeek)
                        {
                            Clock.Play();
                            State.UpdateMediaState(PlaybackStatus.Play);
                        }
                        else
                        {
                            State.UpdateMediaState(PlaybackStatus.Pause);
                        }
                    }
                    else if (State.IsSeeking == false)
                    {
                        // Notify position changes
                        State.UpdatePosition(wallClock);
                    }

                    // Initiate the frame docding cycle
                    FrameDecodingCycle.Begin();

                    #endregion

                    #region 2. Main Component Decoding

                    // Capture component and blocks for easier readability
                    comp   = Container.Components[main];
                    blocks = Blocks[main];

                    // Handle the main component decoding; Start by checking we have some packets
                    while (comp.PacketBufferCount <= 0 && CanReadMorePackets && ShouldReadMorePackets)
                    {
                        PacketReadingCycle.Wait(Constants.Interval.LowPriority);
                    }

                    if (comp.PacketBufferCount > 0)
                    {
                        // Detect if we are in range for the main component
                        isInRange = blocks.IsInRange(wallClock);

                        if (isInRange == false)
                        {
                            // Signal the start of a sync-buffering scenario
                            HasDecoderSeeked = true;
                            isBuffering      = true;
                            resumeClock      = Clock.IsRunning;
                            Clock.Pause();
                            Log(MediaLogMessageType.Debug, $"SYNC-BUFFER: Started.");

                            // Read some frames and try to get a valid range
                            do
                            {
                                // Try to get more packets by waiting for read cycles.
                                if (CanReadMorePackets && comp.PacketBufferCount <= 0)
                                {
                                    PacketReadingCycle.Wait();
                                }

                                // Decode some frames and check if we are in reange now
                                decodedFrameCount += AddBlocks(main);
                                isInRange          = blocks.IsInRange(wallClock);

                                // Break the cycle if we are in range
                                if (isInRange || CanReadMorePackets == false)
                                {
                                    break;
                                }
                            }while (decodedFrameCount <= 0 && blocks.IsFull == false);

                            // Unfortunately at this point we will need to adjust the clock after creating the frames.
                            // to ensure tha mian component is within the clock range if the decoded
                            // frames are not with range. This is normal while buffering though.
                            if (isInRange == false)
                            {
                                // Update the wall clock to the most appropriate available block.
                                if (blocks.Count > 0)
                                {
                                    wallClock = blocks[wallClock].StartTime;
                                }
                                else
                                {
                                    resumeClock = false; // Hard stop the clock.
                                }
                                // Update the clock to what the main component range mandates
                                Clock.Update(wallClock);

                                // Call seek to invalidate renderer
                                LastRenderTime[main] = TimeSpan.MinValue;
                                Renderers[main].Seek();

                                // Try to recover the regular loop
                                isInRange = true;
                                while (CanReadMorePackets && comp.PacketBufferCount <= 0)
                                {
                                    PacketReadingCycle.Wait();
                                }
                            }
                        }

                        if (isInRange)
                        {
                            // Check if we need more blocks for the current components
                            rangePercent = blocks.GetRangePercent(wallClock);

                            // Read as much as we can for this cycle.
                            while (comp.PacketBufferCount > 0)
                            {
                                rangePercent = blocks.GetRangePercent(wallClock);

                                if (blocks.IsFull == false || (blocks.IsFull && rangePercent > 0.75d && rangePercent < 1d))
                                {
                                    decodedFrameCount += AddBlocks(main);
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                    }

                    #endregion

                    #region 3. Auxiliary Component Decoding

                    foreach (var t in auxs)
                    {
                        if (State.IsSeeking)
                        {
                            continue;
                        }

                        // Capture the current block buffer and component
                        // for easier readability
                        comp      = Container.Components[t];
                        blocks    = Blocks[t];
                        isInRange = blocks.IsInRange(wallClock);

                        // Invalidate the renderer if we don't have the block.
                        if (isInRange == false)
                        {
                            LastRenderTime[t] = TimeSpan.MinValue;
                            Renderers[t].Seek();
                        }

                        // wait for component to get there if we only have furutre blocks
                        // in auxiliary component.
                        if (blocks.Count > 0 && blocks.RangeStartTime > wallClock)
                        {
                            continue;
                        }

                        // Try to catch up with the wall clock
                        while (blocks.Count == 0 || blocks.RangeEndTime <= wallClock)
                        {
                            // Wait for packets if we don't have enough packets
                            if (CanReadMorePackets && comp.PacketBufferCount <= 0)
                            {
                                PacketReadingCycle.Wait();
                            }

                            if (comp.PacketBufferCount <= 0)
                            {
                                break;
                            }
                            else
                            {
                                decodedFrameCount += AddBlocks(t);
                            }
                        }

                        isInRange = blocks.IsInRange(wallClock);

                        // Move to the next component if we don't meet a regular conditions
                        if (isInRange == false || isBuffering || comp.PacketBufferCount <= 0)
                        {
                            continue;
                        }

                        // Read as much as we can for this cycle.
                        while (comp.PacketBufferCount > 0)
                        {
                            rangePercent = blocks.GetRangePercent(wallClock);

                            if (blocks.IsFull == false || (blocks.IsFull && rangePercent > 0.75d && rangePercent < 1d))
                            {
                                decodedFrameCount += AddBlocks(t);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }

                    #endregion

                    #region 4. Detect End of Media

                    // Detect end of block rendering
                    // TODO: Maybe this detection should be performed on the BlockRendering worker?
                    if (isBuffering == false &&
                        State.IsSeeking == false &&
                        CanReadMoreFramesOf(main) == false &&
                        Blocks[main].IndexOf(wallClock) == Blocks[main].Count - 1)
                    {
                        if (State.HasMediaEnded == false)
                        {
                            // Rendered all and nothing else to read
                            Clock.Pause();

                            if (State.NaturalDuration != null && State.NaturalDuration != TimeSpan.MinValue)
                            {
                                wallClock = State.NaturalDuration.Value;
                            }
                            else
                            {
                                wallClock = Blocks[main].RangeEndTime;
                            }

                            Clock.Update(wallClock);
                            State.HasMediaEnded = true;
                            State.UpdateMediaState(PlaybackStatus.Stop, wallClock);
                            SendOnMediaEnded();
                        }
                    }
                    else
                    {
                        State.HasMediaEnded = false;
                    }

                    #endregion

                    #region 6. Finish the Cycle

                    // complete buffering notifications
                    if (isBuffering)
                    {
                        // Reset the buffering flag
                        isBuffering = false;

                        // Resume the clock if it was playing
                        if (resumeClock)
                        {
                            Clock.Play();
                        }

                        // log some message
                        Log(
                            MediaLogMessageType.Debug,
                            $"SYNC-BUFFER: Finished. Clock set to {wallClock.Format()}");
                    }

                    // Complete the frame decoding cycle
                    FrameDecodingCycle.Complete();

                    // After a seek operation, always reset the has seeked flag.
                    HasDecoderSeeked = false;

                    // If not already set, guess the 1-second buffer length
                    State.GuessBufferingProperties();

                    // Give it a break if there was nothing to decode.
                    // We probably need to wait for some more input
                    if (decodedFrameCount <= 0 && Commands.PendingCount <= 0)
                    {
                        delay.WaitOne();
                    }

                    #endregion
                }
            }
            catch (ThreadAbortException) { /* swallow */ }
            catch { if (!IsDisposed)
                    {
                        throw;
                    }
            }
            finally
            {
                // Always exit notifying the cycle is done.
                FrameDecodingCycle.Complete();
                delay.Dispose();
            }

            #endregion
        }
Exemple #8
0
        /// <summary>
        /// Runs the read task which keeps a packet buffer as full as possible.
        /// It reports on DownloadProgress by enqueueing an update to the property
        /// in order to avoid any kind of disruption to this thread caused by the UI thread.
        /// </summary>
        internal void RunPacketReadingWorker()
        {
            #region Worker State Setup

            // The delay provider prevents 100% core usage
            var delay = new DelayProvider();

            // Holds the packet count for each read cycle
            var packetsRead = new MediaTypeDictionary <int>();

            // State variables for media types
            var t = MediaType.None;

            // Store Container in local variable to prevent NullReferenceException
            // when dispose occurs sametime with read cycle
            var mediaContainer = Container;

            #endregion

            #region Worker Loop

            try
            {
                // Worker logic begins here
                while (IsTaskCancellationPending == false)
                {
                    // Wait for seeking or changing to be done.
                    MediaChangingDone.Wait();
                    SeekingDone.Wait();

                    // Enter a packet reading cycle
                    PacketReadingCycle.Begin();

                    // Initialize Packets read to 0 for each component and state variables
                    foreach (var k in mediaContainer.Components.MediaTypes)
                    {
                        packetsRead[k] = 0;
                    }

                    // Start to perform the read loop
                    // NOTE: Disrupting the packet reader causes errors in UPD streams. Disrupt as little as possible
                    while (CanReadMorePackets && ShouldReadMorePackets && IsTaskCancellationPending == false)
                    {
                        // Perform a packet read. t will hold the packet type.
                        try
                        {
                            t = mediaContainer.Read();
                        }
                        catch (MediaContainerException)
                        {
                            continue;
                        }

                        // Discard packets that we don't need (i.e. MediaType == None)
                        if (mediaContainer.Components.MediaTypes.HasMediaType(t) == false)
                        {
                            continue;
                        }

                        // Update the packet count for the components
                        packetsRead[t] += 1;

                        // Ensure we have read at least some packets from main and auxiliary streams.
                        if (packetsRead.FundamentalsGreaterThan(0))
                        {
                            break;
                        }
                    }

                    // finish the reading cycle.
                    PacketReadingCycle.Complete();

                    // Don't evaluate a pause condition if we are seeking
                    if (SeekingDone.IsInProgress)
                    {
                        continue;
                    }

                    // Wait some if we have a full packet buffer or we are unable to read more packets (i.e. EOF).
                    if (ShouldReadMorePackets == false ||
                        CanReadMorePackets == false ||
                        packetsRead.GetSum() <= 0)
                    {
                        delay.WaitOne();
                    }
                }
            }
            catch (ThreadAbortException) { /* swallow */ }
            catch { if (!IsDisposed)
                    {
                        throw;
                    }
            }
            finally
            {
                // Always exit notifying the reading cycle is done.
                PacketReadingCycle.Complete();
                delay.Dispose();
            }

            #endregion
        }
Exemple #9
0
        /// <summary>
        /// Continually decodes the available packet buffer to have as
        /// many frames as possible in each frame queue and
        /// up to the MaxFrames on each component
        /// </summary>
        internal void RunFrameDecodingWorker()
        {
            // TODO: Don't use State properties in workers as they are only for
            // TODO: Check the use of wall clock. Maybe it's be more consistent
            // to use a single atomic wallclock value per cycle. Check other workers as well.
            // state notification purposes.
            // State variables
            var wasSyncBuffering  = false;
            var delay             = new DelayProvider(); // The delay provider prevents 100% core usage
            var decodedFrameCount = 0;
            var rangePercent      = 0d;
            var main = Container.Components.MainMediaType; // Holds the main media type
            var resumeSyncBufferingClock = false;
            MediaBlockBuffer blocks      = null;

            try
            {
                while (Commands.IsStopWorkersPending == false)
                {
                    #region Setup the Decoding Cycle

                    // Determine what to do on a priority command
                    if (Commands.IsExecutingDirectCommand)
                    {
                        if (Commands.IsClosing)
                        {
                            break;
                        }
                        if (Commands.IsChanging)
                        {
                            Commands.WaitForDirectCommand();
                        }
                    }

                    // Execute the following command at the beginning of the cycle
                    if (IsSyncBuffering == false)
                    {
                        Commands.ExecuteNextQueuedCommand();
                    }

                    // Signal a Seek starting operation and set the initial state
                    FrameDecodingCycle.Begin();

                    // Update state properties -- this must be after processing commanmds as
                    // a direct command might have changed the components
                    main = Container.Components.MainMediaType;
                    decodedFrameCount = 0;

                    #endregion

                    // The 2-part logic blocks detect a sync-buffering scenario
                    // and then decodes the necessary frames.
                    if (State.HasMediaEnded == false && IsWorkerInterruptRequested == false)
                    {
                        #region Sync-Buffering

                        // Capture the blocks for easier readability
                        blocks = Blocks[main];

                        // If we are not then we need to begin sync-buffering
                        if (wasSyncBuffering == false && blocks.IsInRange(WallClock) == false)
                        {
                            // Signal the start of a sync-buffering scenario
                            IsSyncBuffering          = true;
                            wasSyncBuffering         = true;
                            resumeSyncBufferingClock = Clock.IsRunning;
                            Clock.Pause();
                            State.UpdateMediaState(PlaybackStatus.Manual);
                            Log(MediaLogMessageType.Debug, $"SYNC-BUFFER: Started.");
                        }

                        #endregion

                        #region Component Decoding

                        // We need to add blocks if the wall clock is over 75%
                        // for each of the components so that we have some buffer.
                        foreach (var t in Container.Components.MediaTypes)
                        {
                            if (IsWorkerInterruptRequested)
                            {
                                break;
                            }

                            // Capture a reference to the blocks and the current Range Percent
                            const double rangePercentThreshold = 0.75d;
                            blocks       = Blocks[t];
                            rangePercent = blocks.GetRangePercent(WallClock);

                            // Read as much as we can for this cycle but always within range.
                            while (blocks.IsFull == false || rangePercent > rangePercentThreshold)
                            {
                                // Stop decoding under sync-buffering conditions
                                if (IsSyncBuffering && blocks.IsFull)
                                {
                                    break;
                                }

                                if (IsWorkerInterruptRequested || AddNextBlock(t) == false)
                                {
                                    break;
                                }

                                decodedFrameCount += 1;
                                rangePercent       = blocks.GetRangePercent(WallClock);

                                // Determine break conditions to save CPU time
                                if (IsSyncBuffering == false &&
                                    rangePercent > 0 &&
                                    rangePercent <= rangePercentThreshold &&
                                    blocks.IsFull == false &&
                                    blocks.CapacityPercent >= 0.25d &&
                                    blocks.IsInRange(WallClock))
                                {
                                    break;
                                }
                            }
                        }

                        // Give it a break if we are still buffering packets
                        if (IsSyncBuffering)
                        {
                            delay.WaitOne();
                            FrameDecodingCycle.Complete();
                            continue;
                        }

                        #endregion
                    }

                    #region Finish the Cycle

                    // Detect End of Media Scenarios
                    DetectEndOfMedia(decodedFrameCount, main);

                    // Resume sync-buffering clock
                    if (wasSyncBuffering && IsSyncBuffering == false)
                    {
                        // Sync-buffering blocks
                        blocks = Blocks[main];

                        // Unfortunately at this point we will need to adjust the clock after creating the frames.
                        // to ensure tha mian component is within the clock range if the decoded
                        // frames are not with range. This is normal while buffering though.
                        if (blocks.IsInRange(WallClock) == false)
                        {
                            // Update the wall clock to the most appropriate available block.
                            if (blocks.Count > 0)
                            {
                                ChangePosition(blocks[WallClock].StartTime);
                            }
                            else
                            {
                                resumeSyncBufferingClock = false; // Hard stop the clock.
                            }
                        }

                        // log some message and resume the clock if it was playing
                        Log(MediaLogMessageType.Debug, $"SYNC-BUFFER: Finished. Clock set to {WallClock.Format()}");

                        if (resumeSyncBufferingClock && State.HasMediaEnded == false)
                        {
                            ResumePlayback();
                        }

                        wasSyncBuffering         = false;
                        resumeSyncBufferingClock = false;
                    }

                    // Provide updates to decoding stats
                    State.UpdateDecodingBitrate(
                        Blocks.Values.Sum(b => b.IsInRange(WallClock) ? b.RangeBitrate : 0));

                    // Complete the frame decoding cycle
                    FrameDecodingCycle.Complete();

                    // Give it a break if there was nothing to decode.
                    DelayDecoder(delay, decodedFrameCount);

                    #endregion
                }
            }
            catch { throw; }
            finally
            {
                // Reset decoding stats
                State.UpdateDecodingBitrate(0);

                // Always exit notifying the cycle is done.
                FrameDecodingCycle.Complete();
                delay.Dispose();
            }
        }
        /// <summary>
        /// Continually decodes the available packet buffer to have as
        /// many frames as possible in each frame queue and
        /// up to the MaxFrames on each component
        /// </summary>
        internal void RunFrameDecodingWorker()
        {
            #region Worker State Setup

            // The delay provider prevents 100% core usage
            var delay = new DelayProvider();

            // State variables
            var decodedFrameCount = 0;
            var wallClock         = TimeSpan.Zero;
            var rangePercent      = 0d;
            var isInRange         = false;

            // Holds the main media type
            var main = Container.Components.Main.MediaType;

            // Holds the auxiliary media types
            var auxs = Container.Components.MediaTypes.Except(main);

            // State properties
            var isBuffering = false;
            var resumeClock = false;

            MediaComponent   comp   = null;
            MediaBlockBuffer blocks = null;

            #endregion

            #region Worker Loop

            try
            {
                while (Commands.IsStopWorkersPending == false)
                {
                    #region 1. Setup the Decoding Cycle

                    // Determine what to do on a priority command
                    if (Commands.IsExecutingDirectCommand)
                    {
                        if (Commands.IsClosing)
                        {
                            break;
                        }
                        if (Commands.IsChanging)
                        {
                            Commands.WaitForDirectCommand();
                        }
                    }

                    // Update state properties -- this must be after processing commanmds as
                    // a direct command might have changed the components
                    main = Container.Components.Main.MediaType;
                    auxs = Container.Components.MediaTypes.Except(main);

                    // Execute the following command at the beginning of the cycle
                    Commands.ExecuteNextQueuedCommand();

                    // Signal a Seek starting operation
                    FrameDecodingCycle.Begin();

                    // Set initial state
                    wallClock         = WallClock;
                    decodedFrameCount = 0;

                    #endregion

                    if (State.HasMediaEnded == false)
                    {
                        #region 2. Main Component Decoding

                        // Capture component and blocks for easier readability
                        // comp is current component, blocks is the block collection for the component
                        comp   = Container.Components[main];
                        blocks = Blocks[main];

                        // Detect if we are in range for the main component
                        isInRange = blocks.IsInRange(wallClock);

                        if (isInRange == false)
                        {
                            // Signal the start of a sync-buffering scenario
                            isBuffering = true;
                            State.SignalBufferingStarted();
                            resumeClock = Clock.IsRunning;
                            Clock.Pause();
                            Log(MediaLogMessageType.Debug, $"SYNC-BUFFER: Started.");

                            // Read some frames and try to get a valid range
                            do
                            {
                                // Try to get more packets by waiting for read cycles.
                                WaitForPackets(comp, 1);

                                // Decode some frames and check if we are in reange now
                                if (AddNextBlock(main) == false)
                                {
                                    break;
                                }

                                decodedFrameCount += 1;
                                isInRange          = blocks.IsInRange(wallClock);

                                // Break the cycle if we are in range
                                if (isInRange || CanReadMorePackets == false || ShouldReadMorePackets == false)
                                {
                                    break;
                                }
                            }while (blocks.IsFull == false);

                            // Unfortunately at this point we will need to adjust the clock after creating the frames.
                            // to ensure tha mian component is within the clock range if the decoded
                            // frames are not with range. This is normal while buffering though.
                            if (isInRange == false)
                            {
                                // Update the wall clock to the most appropriate available block.
                                if (blocks.Count > 0)
                                {
                                    wallClock = blocks[wallClock].StartTime;
                                }
                                else
                                {
                                    resumeClock = false; // Hard stop the clock.
                                }
                                // Update the clock to what the main component range mandates
                                Clock.Update(wallClock);

                                // Force renderer invalidation
                                InvalidateRenderer(main);

                                // Try to recover the regular loop
                                isInRange = true;
                            }
                        }

                        if (isInRange)
                        {
                            // Check if we need more blocks for the current components
                            rangePercent = blocks.GetRangePercent(wallClock);

                            // Read as much as we can for this cycle but always within range.
                            while (blocks.IsFull == false || (blocks.IsFull && rangePercent > 0.75d && rangePercent < 1d))
                            {
                                if (AddNextBlock(main) == false)
                                {
                                    break;
                                }

                                decodedFrameCount += 1;
                                rangePercent       = blocks.GetRangePercent(wallClock);
                                continue;
                            }
                        }

                        #endregion

                        #region 3. Auxiliary Component Decoding

                        foreach (var t in auxs)
                        {
                            if (State.IsSeeking)
                            {
                                continue;
                            }

                            // Capture the current block buffer and component
                            // for easier readability
                            comp      = Container.Components[t];
                            blocks    = Blocks[t];
                            isInRange = blocks.IsInRange(wallClock);

                            // wait for component to get there if we only have furutre blocks
                            // in auxiliary component.
                            if (blocks.Count > 0 && blocks.RangeStartTime > wallClock)
                            {
                                continue;
                            }

                            // We need the other components to catch up with the main
                            while (blocks.Count == 0 || blocks.RangeEndTime <= wallClock ||
                                   (Blocks[main].Count > 0 && blocks.RangeEndTime < Blocks[main].RangeEndTime))
                            {
                                // give up if we never received frames for the expected component
                                if (AddNextBlock(t) == false)
                                {
                                    break;
                                }
                            }

                            // Check if we are finally within range
                            isInRange = blocks.IsInRange(wallClock);

                            // Invalidate the renderer if we don't have the block.
                            if (isInRange == false)
                            {
                                InvalidateRenderer(t);
                            }

                            // Move to the next component if we don't meet a regular conditions
                            if (isInRange == false || isBuffering)
                            {
                                continue;
                            }

                            // Decode as much as we can off the packet buffer for this cycle.
                            rangePercent = blocks.GetRangePercent(wallClock);
                            while (blocks.IsFull == false || (blocks.IsFull && rangePercent > 0.75d && rangePercent < 1d))
                            {
                                if (AddNextBlock(t) == false)
                                {
                                    break;
                                }

                                rangePercent = blocks.GetRangePercent(wallClock);
                            }
                        }

                        #endregion
                    }

                    #region 4. Detect End of Media

                    // Detect end of block rendering
                    // TODO: Maybe this detection should be performed on the BlockRendering worker?
                    if (isBuffering == false &&
                        decodedFrameCount <= 0 &&
                        State.IsSeeking == false &&
                        CanReadMoreFramesOf(main) == false &&
                        Blocks[main].IndexOf(wallClock) == Blocks[main].Count - 1)
                    {
                        if (State.HasMediaEnded == false)
                        {
                            // Rendered all and nothing else to read
                            Clock.Pause();
                            wallClock = Blocks[main].RangeEndTime;
                            Clock.Update(wallClock);

                            if (State.NaturalDuration != null &&
                                State.NaturalDuration != TimeSpan.MinValue &&
                                State.NaturalDuration < wallClock)
                            {
                                Log(MediaLogMessageType.Warning,
                                    $"{nameof(State.HasMediaEnded)} conditions met at {wallClock.Format()} but " +
                                    $"{nameof(State.NaturalDuration)} reports {State.NaturalDuration.Value.Format()}");
                            }

                            State.UpdateMediaEnded(true);
                            State.UpdateMediaState(PlaybackStatus.Stop, wallClock);
                            SendOnMediaEnded();
                        }
                    }
                    else
                    {
                        State.UpdateMediaEnded(false);
                    }

                    #endregion

                    #region 6. Finish the Cycle

                    // complete buffering notifications
                    if (isBuffering)
                    {
                        // Always reset the buffering flag
                        isBuffering = false;

                        // Resume the clock if it was playing
                        if (resumeClock)
                        {
                            Clock.Play();
                        }

                        // log some message
                        Log(MediaLogMessageType.Debug, $"SYNC-BUFFER: Finished. Clock set to {wallClock.Format()}");
                    }

                    // If not already set, guess the 1-second buffer length
                    State.GuessBufferingProperties();

                    // Complete the frame decoding cycle
                    FrameDecodingCycle.Complete();

                    // Give it a break if there was nothing to decode.
                    // We probably need to wait for some more input
                    if (Commands.IsStopWorkersPending == false &&
                        decodedFrameCount <= 0 &&
                        Commands.HasQueuedCommands == false)
                    {
                        delay.WaitOne();
                    }

                    #endregion
                }
            }
            catch { throw; }
            finally
            {
                // Always exit notifying the cycle is done.
                FrameDecodingCycle.Complete();
                delay.Dispose();
            }

            #endregion
        }
        /// <summary>
        /// Runs the read task which keeps a packet buffer as full as possible.
        /// It reports on DownloadProgress by enqueueing an update to the property
        /// in order to avoid any kind of disruption to this thread caused by the UI thread.
        /// </summary>
        internal void RunPacketReadingWorker()
        {
            #region Worker State Setup

            // The delay provider prevents 100% core usage
            var delay = new DelayProvider();

            // Holds the packet count for each read cycle
            var packetsRead = new MediaTypeDictionary <int>();

            // State variables for media types
            var t = MediaType.None;

            // Signal the start of a buffering operation
            State.SignalBufferingStarted();

            #endregion

            #region Worker Loop

            try
            {
                // Worker logic begins here
                while (Commands.IsStopWorkersPending == false)
                {
                    // Determine what to do on a priority command
                    if (Commands.IsExecutingDirectCommand)
                    {
                        if (Commands.IsClosing)
                        {
                            break;
                        }
                        if (Commands.IsChanging)
                        {
                            Commands.WaitForDirectCommand();
                        }
                    }

                    // Wait for seeking or changing to be done.
                    Commands.WaitForActiveSeekCommand();

                    // Enter a packet reading cycle
                    PacketReadingCycle.Begin();

                    // Initialize Packets read to 0 for each component and state variables
                    foreach (var k in Container.Components.MediaTypes)
                    {
                        packetsRead[k] = 0;
                    }

                    // Start to perform the read loop
                    // NOTE: Disrupting the packet reader causes errors in UPD streams. Disrupt as little as possible
                    while (ShouldReadMorePackets &&
                           CanReadMorePackets &&
                           Commands.IsActivelySeeking == false)
                    {
                        // Perform a packet read. t will hold the packet type.
                        try { t = Container.Read(); }
                        catch (MediaContainerException) { continue; }

                        // Discard packets that we don't need (i.e. MediaType == None)
                        if (Container.Components.MediaTypes.HasMediaType(t) == false)
                        {
                            continue;
                        }

                        // Update the packet count for the components
                        packetsRead[t] += 1;

                        // Ensure we have read at least some packets from main and auxiliary streams.
                        if (packetsRead.ContainsMoreThan(0))
                        {
                            break;
                        }
                    }

                    // finish the reading cycle.
                    PacketReadingCycle.Complete();

                    // Don't evaluate a pause/delay condition if we are seeking
                    if (Commands.IsActivelySeeking)
                    {
                        continue;
                    }

                    // Wait some if we have a full packet buffer or we are unable to read more packets (i.e. EOF).
                    if (ShouldReadMorePackets == false ||
                        CanReadMorePackets == false ||
                        packetsRead.GetSum() <= 0)
                    {
                        delay.WaitOne();
                    }
                }
            }
            catch { throw; }
            finally
            {
                // Always exit notifying the reading cycle is done.
                PacketReadingCycle.Complete();
                delay.Dispose();
            }

            #endregion
        }