private async Task <Box> GetBox(BoxType boxType) { // get the box size var size = await this.GetBoxSize(this.fileOffset); // gets the box var stream = await WebRequestor.GetStreamRangeAsync(this.fileUri, this.fileOffset, this.fileOffset + (long)size); Box box = null; using (var boxReader = new BoxBinaryReader(stream)) { box = boxReader.ReadNextBox(); this.fileOffset += (long)size; } if (box.Type == boxType) { return(box); } else { return(await this.GetBox(boxType)); } }
private static Stream TrimToBox(Stream stream, BoxType boxType) { long offset = 0; BoxBinaryReader reader = new BoxBinaryReader(stream); Box box = null; do { box = reader.ReadNextBox(); if (box != null && box.Type == boxType) { offset = box.Offset; break; } } while (box != null); if (offset == 0) { return(stream); } else { stream.Seek(offset, SeekOrigin.Begin); var buffer = new byte[stream.Length - offset]; stream.Read(buffer, 0, buffer.Length); return(new MemoryStream(buffer)); } }
/// <summary> /// Opens a local (offline) file and initializes the CFF data that can be used for /// manifest generation. /// </summary> /// <param name="path">The file URI of the resource to be opened. i.e. ms-appx:////Big_Buck_Bunny.uvu </param> public override async Task Parse(Uri path) { if (StorageFile == null) { StorageFile = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(path); } using (var fileStream = await StorageFile.OpenStreamForReadAsync()) { var reader = new BoxBinaryReader(fileStream); Box box = null; do { box = reader.ReadNextBox(); if (box != null) { this.Boxes.Add(box); if (box.Type == BoxType.Moov) { // There may be an mdat after the moov, if so parse it if (reader.PeekNextBoxType() == BoxType.Mdat) { box = reader.ReadNextBox(); this.Boxes.Add(box); } // After parsing the moov and optional mdat after it, skip to the mfra // this will jump past the moof and mdats which we don't need to process reader.GotoMovieFragmentRandomAccess(); } } } while (box != null); } this.InitializeTrackRegistry(); }
private async Task <Box> GetNextBox() { var size = await this.GetBoxSize(this.fileOffset); var boxStream = await WebRequestor.GetStreamRangeAsync(this.fileUri, this.fileOffset, this.fileOffset + (long)size); Box box = null; using (var boxReader = new BoxBinaryReader(boxStream)) { box = boxReader.ReadNextBox(); this.fileOffset += (long)size; } return(box); }
private static Stream HackFragment(Stream stream) { long offset = 0; long versionBitOffset = 0; BoxBinaryReader reader = new BoxBinaryReader(stream); Box box = null; do { box = reader.ReadNextBox(); if (box != null) { if (box.Type == BoxType.Moof) { offset = box.Offset; var traf = box.InnerBoxes.FirstOrDefault(b => b.Type == BoxType.Traf) as TrackFragmentBox; if (traf != null) { var trun = traf.InnerBoxes.First(b => b.Type == BoxType.Trun) as TrackFragmentRunFullBox; if (trun != null && trun.Version != 0) { versionBitOffset = trun.Offset + 8; } } break; } } } while (box != null); if (offset == 0 && versionBitOffset == 0) { return(stream); } else { stream.Seek(offset, SeekOrigin.Begin); var buffer = new byte[stream.Length - offset]; stream.Read(buffer, 0, buffer.Length); if (versionBitOffset != 0) { versionBitOffset -= offset; buffer[versionBitOffset] = 0; } return(new MemoryStream(buffer)); } }
protected void ReadInnerBoxes(BoxBinaryReader reader, params BoxType[] expectedBoxTypes) { InnerBoxOffset = reader.Offset; while (reader.Offset < this.Offset + this.Size) { var box = reader.ReadNextBox(); if (box != null) { if (box.Type != BoxType.Unknown && !expectedBoxTypes.Where(t => t == BoxType.Any || t == box.Type).Any()) { throw new BoxException(string.Format("The child box {0} was not expected inside box {1}", box.Type, this.Type)); } this.InnerBoxes.Add(box); } } }
/// <summary> /// This method of building the mfra will make web requests in order to download the data /// from the online source. /// </summary> /// <param name="callback">The action that should be notified when the process is complete.</param> private async Task ReadMovieFragmentRandomAccess() { #if !RANGESUFFIXSUPPORTED // not all backend services support range suffixes. For example, Azure Blobs. Here is a way around this but it requires an extra request to get the length and therefore does not perform as well. var fileSize = await WebRequestor.GetFileSizeAsync(this.fileUri); #endif // grab the mfra offset #if RANGESUFFIXSUPPORTED var offsetStream = await WebRequestor.GetStreamRangeAsync(this.fileUri, -4); #else var offsetStream = await WebRequestor.GetStreamRangeNoSuffixAsync(this.fileUri, -4, fileSize); #endif uint mfraOffset = 0; using (var reader = new BoxBinaryReader(offsetStream)) { mfraOffset = reader.ReadUInt32(); } // grab the mfra data #if RANGESUFFIXSUPPORTED var mfraStream = await WebRequestor.GetStreamRangeAsync(this.fileUri, -mfraOffset); #else var mfraStream = await WebRequestor.GetStreamRangeNoSuffixAsync(this.fileUri, -mfraOffset, fileSize); #endif // Write the bytes to our TOC file using (var reader = new BoxBinaryReader(mfraStream)) { reader.GotoPosition(0); Box box = null; do { box = reader.ReadNextBox(); if (box != null) { this.Boxes.Add(box); } } while (box != null); } }