private TimeSpan CalculateDownloadTimeout(Segment segment) { if (!IsDynamic) { return(TimeSpan.MaxValue); } var timeout = timeBufferDepthDefault; var averageThroughput = throughputHistory.GetAverageThroughput(); if (averageThroughput > 0 && currentRepresentation.Bandwidth.HasValue && segment.Period != null) { var bandwidth = currentRepresentation.Bandwidth.Value; var duration = segment.Period.Duration.TotalSeconds; var segmentSize = bandwidth * duration; var calculatedTimeNeeded = TimeSpan.FromSeconds(segmentSize / averageThroughput * 1.5); var docParams = currentStreams.GetDocumentParameters(); if (docParams == null) { throw new ArgumentNullException("currentStreams.GetDocumentParameters() returns null"); } var manifestMinBufferDepth = docParams.Document.MinBufferTime ?? TimeSpan.Zero; timeout = calculatedTimeNeeded > manifestMinBufferDepth ? calculatedTimeNeeded : manifestMinBufferDepth; } return(timeout); }
public void AdaptToNetConditions() { // Treat adaptive streaming as "non critical". If cannot be processed now due to lock, attempt // will be done at next iteration. if (!Monitor.TryEnter(switchStreamLock)) { return; } try { if (DisableAdaptiveStreaming) { return; } if (currentStream == null && pendingStream == null) { return; } var streamToAdapt = pendingStream ?? currentStream; if (streamToAdapt.Representation.Bandwidth.HasValue == false) { return; } var currentThroughput = throughputHistory.GetAverageThroughput(); if (Math.Abs(currentThroughput) < 0.1) { return; } Logger.Debug("Adaptation values:"); Logger.Debug(" current throughput: " + currentThroughput); Logger.Debug(" current stream bandwidth: " + streamToAdapt.Representation.Bandwidth.Value); // availableStreams is sorted array by descending bandwidth var stream = availableStreams.FirstOrDefault(o => o.Representation.Bandwidth <= currentThroughput) ?? availableStreams.Last(); if (stream.Representation.Bandwidth == streamToAdapt.Representation.Bandwidth) { return; } Logger.Info("Changing stream to bandwidth: " + stream.Representation.Bandwidth); pendingStream = stream; } finally { Monitor.Exit(switchStreamLock); } }
public void AdaptToNetConditions() { if (DisableAdaptiveStreaming) { return; } if (currentStream == null && pendingStream == null) { return; } var streamToAdapt = pendingStream ?? currentStream; if (streamToAdapt.Representation.Bandwidth.HasValue == false) { return; } var currentThroughput = throughputHistory.GetAverageThroughput(); if (Math.Abs(currentThroughput) < 0.1) { return; } Logger.Debug("Adaptation values:"); Logger.Debug(" current throughput: " + currentThroughput); Logger.Debug(" current stream bandwidth: " + streamToAdapt.Representation.Bandwidth.Value); // availableStreams is sorted array by descending bandwidth var stream = availableStreams.FirstOrDefault(o => o.Representation.Bandwidth <= currentThroughput) ?? availableStreams.Last(); if (stream.Representation.Bandwidth == streamToAdapt.Representation.Bandwidth) { return; } Logger.Info("Changing stream to bandwidth: " + stream.Representation.Bandwidth); pendingStream = stream; }