Skip to content

Commit

Permalink
Add original element names of WebM aliases (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
at-wat authored Dec 15, 2019
1 parent 6250d16 commit 637c037
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 18 deletions.
33 changes: 23 additions & 10 deletions elementtype.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ const (
ElementSeekPosition

ElementInfo
ElementTimecodeScale
ElementTimestampScale
ElementDuration
ElementDateUTC
ElementTitle
ElementMuxingApp
ElementWritingApp

ElementCluster
ElementTimecode
ElementTimestamp
ElementPrevSize
ElementSimpleBlock
ElementBlockGroup
Expand Down Expand Up @@ -125,6 +125,12 @@ const (
elementMax
)

// WebM aliases
const (
ElementTimecodeScale = ElementTimestampScale
ElementTimecode = ElementTimestamp
)

func (i ElementType) String() string {
switch i {
case ElementEBML:
Expand Down Expand Up @@ -157,8 +163,8 @@ func (i ElementType) String() string {
return "SeekPosition"
case ElementInfo:
return "Info"
case ElementTimecodeScale:
return "TimecodeScale"
case ElementTimestampScale:
return "TimestampScale"
case ElementDuration:
return "Duration"
case ElementDateUTC:
Expand All @@ -171,8 +177,8 @@ func (i ElementType) String() string {
return "WritingApp"
case ElementCluster:
return "Cluster"
case ElementTimecode:
return "Timecode"
case ElementTimestamp:
return "Timestamp"
case ElementPrevSize:
return "PrevSize"
case ElementSimpleBlock:
Expand Down Expand Up @@ -342,8 +348,8 @@ func ElementTypeFromString(s string) (ElementType, error) {
return ElementSeekPosition, nil
case "Info":
return ElementInfo, nil
case "TimecodeScale":
return ElementTimecodeScale, nil
case "TimestampScale":
return ElementTimestampScale, nil
case "Duration":
return ElementDuration, nil
case "DateUTC":
Expand All @@ -356,8 +362,8 @@ func ElementTypeFromString(s string) (ElementType, error) {
return ElementWritingApp, nil
case "Cluster":
return ElementCluster, nil
case "Timecode":
return ElementTimecode, nil
case "Timestamp":
return ElementTimestamp, nil
case "PrevSize":
return ElementPrevSize, nil
case "SimpleBlock":
Expand Down Expand Up @@ -484,6 +490,13 @@ func ElementTypeFromString(s string) (ElementType, error) {
return ElementCueClusterPosition, nil
case "CueBlockNumber":
return ElementCueBlockNumber, nil

// WebM aliases
case "TimecodeScale":
return ElementTimecodeScale, nil
case "Timecode":
return ElementTimecode, nil

default:
return 0, errUnknownElementType
}
Expand Down
16 changes: 8 additions & 8 deletions unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,7 @@ func readElement(r0 io.Reader, n int64, vo reflect.Value, depth int, pos uint64,
r = r0
}

type field struct {
v reflect.Value
t reflect.Type
}
fieldMap := make(map[string]field)
fieldMap := make(map[ElementType]reflect.Value)
if vo.IsValid() {
for i := 0; i < vo.NumField(); i++ {
var nn []string
Expand All @@ -77,7 +73,11 @@ func readElement(r0 io.Reader, n int64, vo reflect.Value, depth int, pos uint64,
} else {
name = vo.Type().Field(i).Name
}
fieldMap[name] = field{vo.Field(i), vo.Type().Field(i).Type}
t, err := ElementTypeFromString(name)
if err != nil {
return nil, err
}
fieldMap[t] = vo.Field(i)
}
}

Expand All @@ -102,8 +102,8 @@ func readElement(r0 io.Reader, n int64, vo reflect.Value, depth int, pos uint64,
return nil, err
}
var vnext reflect.Value
if fm, ok := fieldMap[v.e.String()]; ok {
vnext = fm.v
if vn, ok := fieldMap[v.e]; ok {
vnext = vn
}

var elem *Element
Expand Down
9 changes: 9 additions & 0 deletions unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,15 @@ func TestUnmarshal_Error(t *testing.T) {
t.Errorf("Unexpected error, %v, got %v\n", errIndefiniteType, err)
}
})
t.Run("UnknownElement", func(t *testing.T) {
input := &struct {
Header struct {
} `ebml:"Unknown"`
}{}
if err := Unmarshal(bytes.NewBuffer([]byte{}), input); err != errUnknownElementType {
t.Errorf("Unexpected error, %v, got %v\n", errUnknownElementType, err)
}
})
t.Run("Short", func(t *testing.T) {
TestBinaries := map[string][]byte{
"ElementID": {0x1a, 0x45, 0xdf},
Expand Down

0 comments on commit 637c037

Please sign in to comment.