private async Task<ChunkResponse> TestChunkAsync(SmoothStreamChunk chunk) { ChunkRetriever cr = new ChunkRetriever(); var response = await cr.DownloadChunkStatsOnlyAsync(chunk); _responsesCon[chunk] = response; if (ChunkTested != null) { try { this.ChunkTested(this, chunk, response); } catch (Exception ex) { Log("Exception raising chunk tested event: " + ex.Message, false); } } return response; }
private async Task<bool> DownloadHLSChunksAsync() { Log("Start DownloadChunksAsync()", false); ConcurrentBag<HLSChunk> _myChunkBag = new ConcurrentBag<HLSChunk>(); Log("Building chunk list...", false); foreach (var si in _hlsManifest.secondaryManifests) { foreach (var c in si.chunks) { _myChunkBag.Add(c); } } //_myChunkList = _myChunkList.OrderBy(c => c.Bitrate).ThenBy(c => c.FragmentIndex).ToList(); Log(_myChunkBag.Count.ToString() + " possible chunks total", false); int chunksTested = 0; int counter = _myChunkBag.Count; List<Task<ChunkResponse>> runningTasks = new List<Task<ChunkResponse>>(); for (int i = 0; i < counter; i++) { HLSChunk myChunk; _myChunkBag.TryTake(out myChunk); Log("Testing chunk " + myChunk.chunkUrl + (i + 1).ToString() + " of " + counter.ToString(), true); try { try { ChunkRetriever cr = new ChunkRetriever(); runningTasks.Add( cr.DownloadChunkStatsOnlyAsync(myChunk.chunkUrl) ); } catch (Exception ex) { Console.WriteLine(ex.ToString()); throw; } } catch (Exception ex) { //if (!_responsesCon.ContainsKey(_myChunkBag[i])) //{ // _myChunkBag[i] = null; //} Log("Chunk error (" + myChunk.chunkUrl + "): " + ex.Message, false); } chunksTested += 1; } ChunkResponse[] responses = await Task.WhenAll(runningTasks); foreach (var result in responses) { Log(" .. Response: " + (result.Elapsed.HasValue ? System.Math.Round(result.Elapsed.Value.TotalMilliseconds, 0).ToString() + "ms" : "unknown") + ", status code " + result.ReturnStatusCodeNumeric.ToString(), true); } testRunning = false; return true; }