/// <summary> /// Download all segments for the closed caption starting in specified <paramref name="position"/>. /// </summary> /// <param name="subtitle">Subtitle rendition of the closed caption to download.</param> /// <param name="position">Starting position for the download.</param> /// <returns>Async task.</returns> private async Task DownloadAllSegmentsAsync(IHLSSubtitleRendition subtitle, TimeSpan position) { if (null == this._Controller || !this._Controller.IsValid || null == this._Controller.Playlist) { return; } uint segmentCount = await subtitle.RefreshAsync(); if (0 == segmentCount) { return; } var locators = subtitle.GetSubtitleLocators(); List <Tuple <uint, string> > toFetch = null; if (this._Controller.Playlist.IsLive && !(null == this._WebVTTLocators || 0 == this._WebVTTLocators.Count)) { var locdata = locators.Where(l => l.StartPosition >= position.TotalSeconds).Select(l => new Tuple <uint, string>(l.Index, l.Location)).ToList(); toFetch = locdata.Except(_WebVTTLocators, new LocatorEqualityComparer()).ToList(); this._WebVTTLocators = locdata; } else { this._WebVTTLocators = locators.Where(l => l.StartPosition >= position.TotalSeconds).Select(l => new Tuple <uint, string>(l.Index, l.Location)).ToList(); toFetch = this._WebVTTLocators; } await Task.Run(async() => { using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient()) { foreach (var loc in toFetch) { if (this._Cancel) { break; } Uri uri; if (Uri.TryCreate(loc.Item2, UriKind.RelativeOrAbsolute, out uri)) { await this.DownloadSegmentAsync(client, loc.Item1, uri); } } } }); }
/// <summary> /// Download all segments for the closed caption starting in specified <paramref name="position"/>. /// </summary> /// <param name="id">ID of the closed caption to download.</param> /// <param name="position">Starting position for the download.</param> public async Task DownloadAllSegmentsAsync(string id, TimeSpan position) { try { IHLSSubtitleRendition subtitle = this.FindRendition(id); if (null != subtitle) { this.CurrentSubtitleId = id; await this.DownloadAllSegmentsAsync(subtitle, position); } } catch (NullReferenceException) { // Swallow exceptions here as 404s and other errors can cause the SDK code being accessed above to throw } }