-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Parser/Deserializer for fmp4 boxes
Description Add fmp4 boxes parser/deserializer for common boxes which are used by Live, LiveToVOD, smooth or cmaf files. moov, mdat, moof and its children boxes etc. mfra as smooth specific box is not included. tfdt box as cmaf specific box is included. new boxes can be added later when needed. All the unknown boxes are treated as basic Box with its own four-letter type. The code is ported over from our source tree with matching namespace and compatible with new c# compiler. Internal tests have passed for different types of boxes.
- Loading branch information
Showing
31 changed files
with
8,451 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
| ||
using System.Text; | ||
|
||
namespace AMSMigrate.Fmp4 | ||
{ | ||
/// <summary> | ||
/// This class contains Box extension methods. | ||
/// </summary> | ||
public static class BoxExtensions | ||
{ | ||
/// <summary> | ||
/// Clone (deep copy) the given box type by serializing and deserializing. | ||
/// This is the non-generic method which is used when you don't know ahead of time | ||
/// what the box is going to be. | ||
/// Note that this method has side effects on the given box.Dirty flag (will go false). | ||
/// </summary> | ||
/// <typeparam name="BoxT">The box type.</typeparam> | ||
/// <param name="box">The box instance to round-trip. Note that as a result of serialization, | ||
/// the box.Dirty flag will become false.</param> | ||
/// <returns>The cloned box.</returns> | ||
public static Box CloneBySerialization(this Box box) | ||
{ | ||
return CloneBySerialization<Box>(box, parseBox: (reader) => MP4BoxFactory.ParseSingleBox(reader)); | ||
} | ||
|
||
/// <summary> | ||
/// Clone (deep copy) the given box type by serializing and deserializing. | ||
/// This is the generic method which is used when you know ahead of time what the | ||
/// box is going to be. If your prediction is wrong, an MP4DeserializeException will be thrown. | ||
/// Note that this method has side effects on the given box.Dirty flag (will go false). | ||
/// </summary> | ||
/// <typeparam name="BoxT">The box type.</typeparam> | ||
/// <param name="box">The box instance to round-trip. Note that as a result of serialization, | ||
/// the box.Dirty flag will become false.</param> | ||
/// <returns>The cloned box.</returns> | ||
public static BoxT CloneBySerialization<BoxT>(this BoxT box) where BoxT : Box | ||
{ | ||
return CloneBySerialization<BoxT>(box, parseBox: (reader) => MP4BoxFactory.ParseSingleBox<BoxT>(reader)); | ||
} | ||
|
||
/// <summary> | ||
/// Clone (deep copy) the given box type by serializing and deserializing. | ||
/// This is the generic method which is used when you know ahead of time what the | ||
/// box is going to be. If your prediction is wrong, an MP4DeserializeException will be thrown. | ||
/// Note that this method has side effects on the given box.Dirty flag (will go false). | ||
/// </summary> | ||
/// <typeparam name="BoxT">The box type.</typeparam> | ||
/// <param name="box">The box instance to round-trip. Note that as a result of serialization, | ||
/// the box.Dirty flag will become false.</param> | ||
/// <param name="parseBox">The function which will parse and return a BoxT using the given reader.</param> | ||
/// <returns>The cloned box.</returns> | ||
private static BoxT CloneBySerialization<BoxT>(this BoxT box, Func<MP4Reader, BoxT> parseBox) where BoxT : Box | ||
{ | ||
using (var stream = new MemoryStream()) | ||
using (var writer = new MP4Writer(stream, Encoding.Default, leaveOpen: true)) | ||
using (var reader = new MP4Reader(stream, Encoding.Default, leaveOpen: true)) | ||
{ | ||
box.WriteTo(writer); | ||
stream.Position = 0; | ||
BoxT result = parseBox(reader); | ||
|
||
// We get to choose our policy for trunCopy.Dirty: always true/false, or copy from source. | ||
// At the moment, we choose always dirty, to reflect that it has never been serialized. | ||
result.Dirty = true; | ||
return result; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.