private void MakeTimelineInfo(IXmlNode segmentTimeline, uint bitrate, ulong timescale, string repID, uint startNumber, ulong presentationTimeOffset, string mediaFormat, ref MediaStream stream) { var timeline = segmentTimeline.ChildNodes.Where(x => x.NodeName == "S"); uint segmentNumber = startNumber; ulong time = 0; //Set time equal to the @t attribute of first element, otherwise it is 0. //This is to replace the value of $Time$ in the media segment url var tAttribute = timeline.First().Attributes.GetNamedItem("t"); if (tAttribute != null) { time = ulong.Parse(tAttribute.InnerText); } foreach (var t in timeline) { ulong duration = ulong.Parse(t.Attributes.GetNamedItem("d").InnerText); var repeatAttribute = t.Attributes.GetNamedItem("r"); long repeats = 0; if (repeatAttribute != null) { repeats = long.Parse(repeatAttribute.InnerText); } for (int i = 0; i < repeats + 1; i++) { MediaSegment segment = new MediaSegment(); segment.Duration = duration; segment.Timestamp = time - presentationTimeOffset; segment.Number = segmentNumber; //Construct the mediaURL string mediaSegmentUrl; ExpandDASHUrlSegmentTemplate(mediaFormat, manifest.BaseUrl, bitrate, repID, segmentNumber, time, out mediaSegmentUrl); segment.SegmentUrl = mediaSegmentUrl; stream.PushBackSegment(segment); time += duration; segmentNumber++; } } double msStreamDuration = (double)time * (1000) / timescale; #if DEBUG Logger.Log("Found " + stream.Segments.Count() + " segments - Duration: " + msStreamDuration.ToString() + " miliseconds"); #endif if (msStreamDuration > manifest.MediaPresentationDuration.TotalMilliseconds) { manifest.MediaPresentationDuration = TimeSpan.FromMilliseconds(msStreamDuration); } }
private void MakeNumberBaseSegmentInfo(uint bitrate, UInt64 timescale, UInt64 segmentDuration, string repID, uint startNumber, string mediaFormat, ref MediaStream stream) { var hnsPresentationDuration = (Convert.ToUInt64(manifest.MediaPresentationDuration.TotalMilliseconds) * Convert.ToUInt64(10000)); var hnsFragmentDuration = ((segmentDuration * 10000000) / timescale); UInt64 count = (uint)(hnsPresentationDuration / hnsFragmentDuration); UInt64 remain = (uint)(hnsPresentationDuration - (hnsFragmentDuration * count)); if (manifest.IsLive && count == 0) { if (manifest.TimeShiftBufferDepth.TotalSeconds > 0 && hnsFragmentDuration > 0) { var hnsTimeShiftBufferDepth = (ulong)(manifest.TimeShiftBufferDepth.TotalSeconds * 10000000); count = hnsTimeShiftBufferDepth / hnsFragmentDuration; } else { count = LiveSegmentCount; } } UInt64 time = 0; UInt64 segmentNumber = startNumber; if (manifest.IsLive) { CalculateNumberIdentifierForLive(hnsFragmentDuration, ref segmentNumber); } //Since we calculated the latest segment number, we will add to the front of the list for (UInt64 i = 0; i < count; i++) { MediaSegment segment = new MediaSegment(); segment.Timestamp = time; segment.Duration = segmentDuration; segment.Number = segmentNumber; //Construct the mediaURL string mediaSegmentUrl; ExpandDASHUrlSegmentTemplate(mediaFormat, manifest.BaseUrl, bitrate, repID, segmentNumber, time, out mediaSegmentUrl); segment.SegmentUrl = mediaSegmentUrl; stream.PushBackSegment(segment); segmentNumber++; time += segmentDuration; } if (remain > 0) { MediaSegment segment = new MediaSegment(); segment.Timestamp = time; segment.Duration = remain / timescale; segment.Number = segmentNumber; string mediaSegmentUrl; ExpandDASHUrlSegmentTemplate(mediaFormat, manifest.BaseUrl, bitrate, repID, segmentNumber, time, out mediaSegmentUrl); segment.SegmentUrl = mediaSegmentUrl; stream.PushBackSegment(segment); #if DEBUG Logger.Log("Adding final segment to stream with MimeType:" + stream.MimeType + ". Segment{ Timestamp: " + time + "Duration: " + segmentDuration + "Number: " + startNumber + "}" + "\nURL: " + segment.SegmentUrl); #endif time += segmentDuration; } }