private async void Playlist_SegmentSwitched(object sender, IHLSSegmentSwitchEventArgs args)
{
await this.RefreshStateAsync();
try
{
// only process if app selected 608 captions
if (ClosedCaptionType.CC608Instream == this._CaptionType)
{
var toSegment = args.ToSegment;
if (TrackType.AUDIO == toSegment.MediaType)
{
// no need to process audio track, as it will not contain 608 caption data
return;
}
var payloads = toSegment.GetInbandCCUnits();
// no data--nothing to process
if (null == payloads)
return;
// raise the event to let listeners know that we have 608 data available
if (null != Instream608CaptionsAvailable)
this.Instream608CaptionsAvailable(this, EventArgs.Empty);
// copy the data to a WinRT-compatible data structure
var map = new Dictionary<ulong, IList<Byte>>();
foreach (var payload in payloads)
{
//Debug.WriteLine("Found 608 payload data for timestamp: " + payload.Timestamp.ToString() + " ticks (" + (payload.Timestamp / 10000000.0).ToString() + " sec)" );
var rawBytes = payload.GetPayload();
if (null != rawBytes)
{
var bytes = new List<byte>(rawBytes);
ulong key = (ulong)payload.Timestamp.Ticks;
if (map.ContainsKey(key))
map[key] = bytes;
else
map.Add(key, bytes);
}
}
await this._MediaPlayer.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
try
{
if (this._MediaPlayer.SelectedCaption != null)
{
// call this method on the selected caption object (if there is one)--this will in turn raise an
// event that the 608 caption plugin should be listening for...
this._MediaPlayer.SelectedCaption.AugmentPayload(map, TimeSpan.Zero, TimeSpan.Zero);
}
// add the payload data to each caption object, in case it gets selected later
foreach (var c in this._MediaPlayer.AvailableCaptions)
{
// make sure we have the queue ready--if not, create it
var queue = c.Payload as Queue<Dictionary<ulong, IList<Byte>>>;
if (null == queue)
{
queue = new Queue<Dictionary<ulong, IList<Byte>>>();
c.Payload = queue;
}
// store the most recent data on the end of the queue
queue.Enqueue(map);
// but only keep the most recent three sets of data (as we could be as much as 30 seconds ahead)
while (queue.Count > 3)
queue.Dequeue();
}
}
catch { }
});
}
else if (this._CaptionType == ClosedCaptionType.WebVTTSidecar && _WebVTTCaptions != null && _WebVTTCaptions.CurrentSubtitleId != null)
{
await this._MediaPlayer.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
{
try
{
await _WebVTTCaptions.DownloadAllSegmentsAsync(_WebVTTCaptions.CurrentSubtitleId, this._MediaPlayer.Position);
}
catch { }
});
}
}
catch { }
}