From 94f55d5922ca4cf3bb0d30ca2b09181d7106c44a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torbjo=CC=88rn=20Einarsson?= Date: Sun, 10 Nov 2024 08:23:12 +0100 Subject: [PATCH] feat: extension of CreateHdlr --- CHANGELOG.md | 2 ++ mp4/hdlr.go | 11 ++++++----- mp4/hdlr_test.go | 48 ++++++++++++++++++++++++++++++++++++++---------- 3 files changed, 46 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 401db8cb..c911c7a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - CreatePrftBox now takes flags parameter - PrftBox Info output - Removed ReplaceChild method of StsdBox +- CreateHdlr name for timed metadata ### Added @@ -19,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Constants for PrftBox flags - Unittest to all commands and examples + ### Fixed - Allow missing optional DecoderSpecificInfo diff --git a/mp4/hdlr.go b/mp4/hdlr.go index 54110a87..1e6047bb 100644 --- a/mp4/hdlr.go +++ b/mp4/hdlr.go @@ -12,9 +12,8 @@ import ( // Contained in: Media Box (mdia) or Meta Box (meta) // // This box describes the type of data contained in the trak. -// HandlerType can be : "vide" (video track), "soun" (audio track), "subt" (subtitle track) -// Other types are: "hint" (hint track), "meta" (timed Metadata track), "auxv" (auxiliary video track). -// clcp (Closed Captions (QuickTime)) +// Most common hnadler types are: "vide" (video track), "soun" (audio track), "subt" (subtitle track), +// "text" (text track). "meta" (timed Metadata track), clcp (Closed Captions (QuickTime)) type HdlrBox struct { Version byte Flags uint32 @@ -40,16 +39,18 @@ func CreateHdlr(mediaOrHdlrType string) (*HdlrBox, error) { case "text", "wvtt": hdlr.HandlerType = "text" hdlr.Name = "mp4ff text handler" + case "meta": + hdlr.HandlerType = "meta" + hdlr.Name = "mp4ff timed metadata handler" case "clcp": hdlr.HandlerType = "subt" hdlr.Name = "mp4ff closed captions handler" default: if len(mediaOrHdlrType) != 4 { - return nil, fmt.Errorf("unknown media or hdlr type %s", mediaOrHdlrType) + return nil, fmt.Errorf("handler type is not four characters: %s", mediaOrHdlrType) } hdlr.HandlerType = mediaOrHdlrType hdlr.Name = fmt.Sprintf("mp4ff %s handler", mediaOrHdlrType) - } return hdlr, nil } diff --git a/mp4/hdlr_test.go b/mp4/hdlr_test.go index af06d1e6..2b514381 100644 --- a/mp4/hdlr_test.go +++ b/mp4/hdlr_test.go @@ -7,19 +7,47 @@ import ( ) func TestHdlr(t *testing.T) { - mediaTypes := []string{"video", "audio", "subtitle"} - for _, m := range mediaTypes { - hdlr, err := CreateHdlr(m) - assertNoError(t, err) - boxDiffAfterEncodeAndDecode(t, hdlr) + cases := []struct { + mediaType string + handlerType string + handlerName string + expectedError string + }{ + {"video", "vide", "mp4ff video handler", ""}, + {"vide", "vide", "mp4ff video handler", ""}, + {"audio", "soun", "mp4ff audio handler", ""}, + {"soun", "soun", "mp4ff audio handler", ""}, + {"subtitle", "subt", "mp4ff subtitle handler", ""}, + {"text", "text", "mp4ff text handler", ""}, + {"wvtt", "text", "mp4ff text handler", ""}, + {"meta", "meta", "mp4ff timed metadata handler", ""}, + {"clcp", "subt", "mp4ff closed captions handler", ""}, + {"roses", "", "", "handler type is not four characters: roses"}, + {"auxv", "auxv", "mp4ff auxv handler", ""}, } - for _, m := range mediaTypes { - hdlr, err := CreateHdlr(m) - hdlr.LacksNullTermination = true - assertNoError(t, err) - boxDiffAfterEncodeAndDecode(t, hdlr) + for _, c := range cases { + t.Run(c.mediaType, func(t *testing.T) { + hdlr, err := CreateHdlr(c.mediaType) + if c.expectedError != "" { + if err == nil { + t.Errorf("Expected error %s, but got nil", c.expectedError) + } else if err.Error() != c.expectedError { + t.Errorf("Expected error %s, but got %s", c.expectedError, err.Error()) + } + return + } + if hdlr.HandlerType != c.handlerType { + t.Errorf("Expected handler type %s, but got %s", c.handlerType, hdlr.HandlerType) + } + if hdlr.Name != c.handlerName { + t.Errorf("Expected handler name %s, but got %s", c.handlerName, hdlr.Name) + } + boxDiffAfterEncodeAndDecode(t, hdlr) + hdlr.LacksNullTermination = true + boxDiffAfterEncodeAndDecode(t, hdlr) + }) } }