Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Divide box_types.go by specification #139 #140

Merged
merged 1 commit into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions box_types_3gpp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package mp4

var udta3GppMetaBoxTypes = []BoxType{
StrToBoxType("titl"),
StrToBoxType("dscp"),
StrToBoxType("cprt"),
StrToBoxType("perf"),
StrToBoxType("auth"),
StrToBoxType("gnre"),
}

func init() {
for _, bt := range udta3GppMetaBoxTypes {
AddAnyTypeBoxDefEx(&Udta3GppString{}, bt, isUnderUdta, 0)
}
}

type Udta3GppString struct {
AnyTypeBox
FullBox `mp4:"0,extend"`
Pad bool `mp4:"1,size=1,hidden"`
Language [3]byte `mp4:"2,size=5,iso639-2"` // ISO-639-2/T language code
Data []byte `mp4:"3,size=8,string"`
}
75 changes: 75 additions & 0 deletions box_types_3gpp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package mp4

import (
"bytes"
"io"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestBoxTypes3GPP(t *testing.T) {
testCases := []struct {
name string
src IImmutableBox
dst IBox
bin []byte
str string
ctx Context
}{
{
name: "udta 3gpp string",
src: &Udta3GppString{
AnyTypeBox: AnyTypeBox{Type: StrToBoxType("titl")},
Language: [3]byte{0x5, 0xe, 0x7},
Data: []byte("SING"),
},
dst: &Udta3GppString{
AnyTypeBox: AnyTypeBox{Type: StrToBoxType("titl")},
},
bin: []byte{
0, // version
0x00, 0x00, 0x00, // flags
0x15, 0xc7, // language
0x53, 0x49, 0x4e, 0x47, // data
},
str: `Version=0 Flags=0x000000 Language="eng" Data="SING"`,
ctx: Context{UnderUdta: true},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Marshal
buf := bytes.NewBuffer(nil)
n, err := Marshal(buf, tc.src, tc.ctx)
require.NoError(t, err)
assert.Equal(t, uint64(len(tc.bin)), n)
assert.Equal(t, tc.bin, buf.Bytes())

// Unmarshal
r := bytes.NewReader(tc.bin)
n, err = Unmarshal(r, uint64(len(tc.bin)), tc.dst, tc.ctx)
require.NoError(t, err)
assert.Equal(t, uint64(buf.Len()), n)
assert.Equal(t, tc.src, tc.dst)
s, err := r.Seek(0, io.SeekCurrent)
require.NoError(t, err)
assert.Equal(t, int64(buf.Len()), s)

// UnmarshalAny
dst, n, err := UnmarshalAny(bytes.NewReader(tc.bin), tc.src.GetType(), uint64(len(tc.bin)), tc.ctx)
require.NoError(t, err)
assert.Equal(t, uint64(buf.Len()), n)
assert.Equal(t, tc.src, dst)
s, err = r.Seek(0, io.SeekCurrent)
require.NoError(t, err)
assert.Equal(t, int64(buf.Len()), s)

// Stringify
str, err := Stringify(tc.src, tc.ctx)
require.NoError(t, err)
assert.Equal(t, tc.str, str)
})
}
}
Loading