private void UpdateBuffering(TimeSpan timestampDifference, int bufferSize) { if (this._bufferingPolicy.IsDoneBuffering(timestampDifference, bufferSize, this._totalBufferedStart, this._isStarting)) { this._isBuffering = false; Debug.WriteLine("BufferingManager.UpdateBuffering done buffering: duration {0} size {1} starting {2} memory {3:F} MiB", (object)timestampDifference, (object)bufferSize, (object)(bool)(this._isStarting), (object)ByteConversion.BytesToMiB(GC.GetTotalMemory(false))); this.DumpQueues(); this._isStarting = false; this.ReportBuffering(1f); } else { DateTime utcNow = DateTime.UtcNow; if (utcNow - this._bufferStatusTimeUtc >= BufferingManager.BufferStatusUpdatePeriod) { this._bufferStatusTimeUtc = utcNow; float progress = this._bufferingPolicy.GetProgress(timestampDifference, bufferSize, this._totalBufferedStart, this._isStarting); Debug.WriteLine("BufferingManager.UpdateBuffering: {0:F2}% duration {1} size {2} starting {3} memory {4:F} MiB", (object)(float)((double)progress * 100.0), (object)timestampDifference, (object)bufferSize, (object)(bool)(this._isStarting), (object)ByteConversion.BytesToMiB(GC.GetTotalMemory(false))); this.ReportBuffering(progress); } } }
protected virtual async Task ReadSegmentsAsync(CancellationToken cancellationToken) { bool lockTaken = false; TaskCompletionSource <long> readResultTask; object obj; try { Monitor.Enter(obj = this._readerLock, ref lockTaken); readResultTask = this._readResultTask; } finally { if (lockTaken) { Monitor.Exit(obj); } } this._total = 0L; try { using (IAsyncEnumerator <ISegmentReader> enumerator = this._segmentReaders.GetEnumerator()) { while (true) { if (await enumerator.MoveNextAsync().ConfigureAwait(false)) { ISegmentReader segmentReader = enumerator.Current; DateTimeOffset start = DateTimeOffset.Now; Debug.WriteLine("++++ Starting {0} at {1}. Total memory: {2:F} MiB", (object)segmentReader, (object)start, (object)ByteConversion.BytesToMiB(GC.GetTotalMemory(false))); await this.ReadSegmentAsync(segmentReader, cancellationToken).ConfigureAwait(false); DateTimeOffset complete = DateTimeOffset.Now; Debug.WriteLine("---- Completed {0} at {1} ({2}). Total memory: {3:F} MiB", (object)segmentReader, (object)complete, (object)(complete - start), (object)ByteConversion.BytesToMiB(GC.GetTotalMemory(false))); } else { break; } } } this._enqueue((WorkBuffer)null); } catch (OperationCanceledException ex) { readResultTask.TrySetCanceled(); } catch (Exception ex) { Debug.WriteLine("CallbackReader.ReadAsync() failed: " + ExceptionExtensions.ExtendedMessage(ex)); readResultTask.TrySetException(ex); } finally { if (!readResultTask.Task.IsCompleted) { readResultTask.TrySetResult(this._total); } } }
private bool UpdateState() { if (this._statuses.Count <= 0) { return(false); } TimeSpan timeSpan1 = TimeSpan.MinValue; TimeSpan timeSpan2 = TimeSpan.MaxValue; TimeSpan timeSpan3 = TimeSpan.MaxValue; int num1 = int.MaxValue; int num2 = int.MinValue; int num3 = 0; bool flag1 = false; bool flag2 = this._isEof; bool isExhausted = false; bool isAllExhausted = true; foreach (BufferStatus bufferStatus in this._statuses) { if (!bufferStatus.IsDone) { flag2 = false; } if (bufferStatus.IsMedia) { if (0 == bufferStatus.PacketCount) { isExhausted = true; } else { isAllExhausted = false; } if (!bufferStatus.IsValid) { if (timeSpan3 > TimeSpan.Zero) { timeSpan3 = TimeSpan.Zero; } } else { num3 += bufferStatus.Size; flag1 = true; int packetCount = bufferStatus.PacketCount; if (packetCount < num1) { num1 = packetCount; } if (packetCount > num2) { num2 = packetCount; } TimeSpan?newest = bufferStatus.Newest; TimeSpan?nullable1 = newest; TimeSpan timeSpan4 = timeSpan1; if ((nullable1.HasValue ? (nullable1.GetValueOrDefault() > timeSpan4 ? 1 : 0) : 0) != 0) { timeSpan1 = newest.Value; } TimeSpan?oldest = bufferStatus.Oldest; nullable1 = oldest; TimeSpan timeSpan5 = timeSpan2; if ((nullable1.HasValue ? (nullable1.GetValueOrDefault() < timeSpan5 ? 1 : 0) : 0) != 0) { timeSpan2 = oldest.Value; } nullable1 = newest; TimeSpan?nullable2 = oldest; nullable1 = nullable1.HasValue & nullable2.HasValue ? new TimeSpan?(nullable1.GetValueOrDefault() - nullable2.GetValueOrDefault()) : new TimeSpan?(); TimeSpan timeSpan6 = nullable1 ?? TimeSpan.Zero; if (timeSpan6 < TimeSpan.Zero) { timeSpan6 = TimeSpan.Zero; } if (timeSpan6 < timeSpan3) { timeSpan3 = timeSpan6; } } } } if (flag2) { this._isEof = true; } TimeSpan timeSpan7 = flag1 ? timeSpan3 : TimeSpan.MaxValue; Debug.Assert((timeSpan7 == TimeSpan.MaxValue ? 1 : (timeSpan7 + TimeSpan.FromMilliseconds(500.0) >= TimeSpan.Zero ? 1 : 0)) != 0, string.Format("Invalid timestamp difference: {0} (newest {1} oldest {2} low count {3} high count {4} valid data {5})", (object)timeSpan7, (object)timeSpan1, (object)timeSpan2, (object)num1, (object)num2, (object)(bool)(flag1))); if (timeSpan7 <= TimeSpan.Zero) { timeSpan7 = TimeSpan.Zero; flag1 = false; } if (this._isBuffering) { if (flag2) { this._isBuffering = false; Debug.WriteLine("BufferingManager.UpdateState done buffering (eof): duration {0} size {1} starting {2} memory {3:F} MiB", flag1 ? (object)timeSpan7.ToString() : (object)"none", flag1 ? (object)num3.ToString() : (object)"none", (object)(bool)(this._isStarting), (object)ByteConversion.BytesToMiB(GC.GetTotalMemory(false))); this.DumpQueues(); this.ReportBuffering(1f); } else if (flag1) { this.UpdateBuffering(timeSpan7, num3); } if (!this._isBuffering) { return(false); } } else if (!flag2 && isExhausted) { Debug.WriteLine("BufferingManager.UpdateState start buffering: duration {0} size {1} starting {2} memory {3:F} MiB", (object)timeSpan7, (object)num3, (object)(bool)(this._isStarting), (object)ByteConversion.BytesToMiB(GC.GetTotalMemory(false))); this.DumpQueues(); this._totalBufferedStart = num3; this.UnlockedStartBuffering(); return(false); } if (!flag1) { return(false); } bool flag3 = this._bufferingPolicy.ShouldBlockReads(this._blockReads, timeSpan7, num3, isExhausted, isAllExhausted); if (flag3 != this._blockReads) { Debug.WriteLine("BufferingManager.UpdateState read blocking -> {0} duration {1} size {2} memory {3:F} MiB", (object)(bool)(flag3), flag1 ? (object)timeSpan7.ToString() : (object)"none", flag1 ? (object)num3.ToString() : (object)"none", (object)ByteConversion.BytesToMiB(GC.GetTotalMemory(false))); this.DumpQueues(); } return(flag3); }
protected virtual async Task ReadSegmentsAsync(CancellationToken cancellationToken) { TaskCompletionSource <long> readResultTask; lock (_readerLock) { readResultTask = _readResultTask; } _total = 0L; try { using (IAsyncEnumerator <ISegmentReader> enumerator = _segmentReaders.GetEnumerator()) { while (true) { if (await enumerator.MoveNextAsync().ConfigureAwait(false)) { ISegmentReader segmentReader = enumerator.Current; DateTimeOffset start = DateTimeOffset.Now; Debug.WriteLine("++++ Starting {0} at {1}. Total memory: {2:F} MiB", segmentReader, start, ByteConversion.BytesToMiB(GC.GetTotalMemory(false))); await ReadSegmentAsync(segmentReader, cancellationToken).ConfigureAwait(false); DateTimeOffset complete = DateTimeOffset.Now; Debug.WriteLine("---- Completed {0} at {1} ({2}). Total memory: {3:F} MiB", segmentReader, complete, (complete - start), ByteConversion.BytesToMiB(GC.GetTotalMemory(false))); } else { break; } } } _enqueue(null); } catch (OperationCanceledException ex) { readResultTask.TrySetCanceled(); } catch (Exception ex) { Debug.WriteLine("CallbackReader.ReadAsync() failed: " + ExceptionExtensions.ExtendedMessage(ex)); readResultTask.TrySetException(ex); } finally { if (!readResultTask.Task.IsCompleted) { readResultTask.TrySetResult(_total); } } }