Skip to content

Commit

Permalink
Add Parser/Deserializer for fmp4 boxes
Browse files Browse the repository at this point in the history
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
weibz committed Aug 7, 2023
1 parent ab7ff1d commit 2f305e1
Show file tree
Hide file tree
Showing 31 changed files with 8,451 additions and 0 deletions.
973 changes: 973 additions & 0 deletions fmp4/Box.cs

Large diffs are not rendered by default.

69 changes: 69 additions & 0 deletions fmp4/BoxExtensions.cs
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;
}
}
}
}
Loading

0 comments on commit 2f305e1

Please sign in to comment.