private void DownloadInitSegment(Segment segment, bool initReloadRequired) { LogInfo($"Forcing INIT segment reload: {initReloadRequired}"); // Grab a copy (its a struct) of cancellation token so it is not referenced through cancellationTokenSource each time. var cancelToken = cancellationTokenSource.Token; if (initStreamBytes == null || initReloadRequired) { downloadDataTask = CreateDownloadTask(segment, true, null, cancelToken); processDataTask = downloadDataTask.ContinueWith(response => { var shouldContinue = true; if (cancelToken.IsCancellationRequested) { shouldContinue = false; } else if (response.IsFaulted) { HandleFailedInitDownload(GetErrorMessage(response)); shouldContinue = false; } else if (response.IsCanceled) { shouldContinue = false; } else // always continue on successful download { InitDataDownloaded(response.Result); } // throw exception so continuation wont run if (!shouldContinue) { throw new Exception(); } }, TaskScheduler.Default); downloadCompletedTask = processDataTask.ContinueWith( _ => downloadCompletedSubject.OnNext(Unit.Default), TaskContinuationOptions.OnlyOnRanToCompletion); } else { // Already have init segment. Push it down the pipeline & schedule next download var initData = new DownloadResponse { Data = initStreamBytes, SegmentId = null }; LogInfo("Segment: INIT Reusing already downloaded data"); InitDataDownloaded(initData); downloadCompletedSubject.OnNext(Unit.Default); } }
private void InitDataDownloaded(DownloadResponse responseResult) { if (responseResult.Data != null) { chunkReadySubject.OnNext(responseResult.Data); } // Assign initStreamBytes AFTER it has been pushed down the shared buffer. // When issuing EOS, initStreamBytes will be checked for NULLnes. // We do not want to send EOS before init data - will kill demuxer. initStreamBytes = responseResult.Data; initInProgress = false; LogInfo("Segment: INIT enqueued."); }
private DownloadLoopStatus HandleSuccessfulDownload(DownloadResponse responseResult) { if (cancellationTokenSource.IsCancellationRequested) { return(DownloadLoopStatus.GiveUp); } var segment = responseResult.DownloadSegment; lastDownloadSegmentTimeRange = segment.Period.Copy(); bufferTime = segment.Period.Start + segment.Period.Duration; currentSegmentId = currentStreams.NextSegmentId(currentSegmentId); var timeInfo = segment.Period.ToString(); LogInfo($"Segment: {responseResult.SegmentId} enqueued {timeInfo}"); return(DownloadLoopStatus.Continue); }
private bool HandleSuccessfulDownload(DownloadResponse responseResult) { if (cancellationTokenSource.IsCancellationRequested) { return(false); } chunkReadySubject.OnNext(responseResult.Data); var segment = responseResult.DownloadSegment; lastDownloadSegmentTimeRange = segment.Period.Copy(); bufferTime = segment.Period.Start + segment.Period.Duration - (trimOffset ?? TimeSpan.Zero); currentSegmentId = currentStreams.NextSegmentId(currentSegmentId); var timeInfo = segment.Period.ToString(); LogInfo($"Segment: {responseResult.SegmentId} enqueued {timeInfo}"); if (IsBufferingCompleted()) { SendBufferingCompletedEvent(); } return(true); }