This repository has been archived by the owner on Nov 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
160 lines (144 loc) · 3.58 KB
/
parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"fmt"
"io"
)
type mediaInfo struct {
r *mp4Reader
movie *MovieInfo
moof *movieFragment // current
dataPos int64
// for internal usage
currentState parsingState
leftAtomSize uint64
}
func newMediaInfo(r io.ReadSeeker) *mediaInfo {
return &mediaInfo{r: newMp4Reader(r),
currentState: stateParsingIDLE,
}
}
type parsingState uint32
const (
stateParsingIDLE parsingState = iota // idle
stateParsingFTYP // parsing "ftyp"/"styp"
stateParsingMOOV // parsing "moov"
stateParsingMOOF // parsing "moof"
stateParsingSIDX // parsing "sidx"
stateParsingSSIX // parsing "ssix"
stateParsingMDAT // parsing "mdat"
stateParsingEnd // parsing finished
)
func (p *mediaInfo) parseInternal() (err error) {
for p.currentState != stateParsingEnd {
if p.currentState == stateParsingIDLE {
_, err = p.checkStatus() // get the state and atomReader
if err != nil {
logD.Println(err)
return err
}
}
switch p.currentState {
case stateParsingFTYP:
ftypReader, e := p.r.GetAtom()
if e != nil {
logE.Println(e)
return e
}
_ = parseFtyp(p.movie, ftypReader)
p.currentState = stateParsingIDLE
break
case stateParsingMOOV:
movieReader, e := p.r.GetAtom()
if e != nil {
logE.Println(e)
return e
}
e = parseMoov(p.movie, movieReader)
if e != nil {
return e
}
p.currentState = stateParsingIDLE
break
case stateParsingMOOF:
moofReader, e := p.r.GetAtom()
if e != nil {
return e
}
p.moof = newMovieFragment(p.movie)
e = parseMoof(p.moof, moofReader)
if e != nil {
return e
}
p.currentState = stateParsingIDLE
break
case stateParsingSIDX:
sidxReader, e := p.r.GetAtom()
if e != nil {
return e
}
parseSidx(p.movie, sidxReader)
p.currentState = stateParsingIDLE
break
case stateParsingSSIX:
ssixReader, e := p.r.GetAtom()
if e != nil {
return e
}
parseSsix(p.movie, ssixReader)
p.currentState = stateParsingIDLE
break
case stateParsingMDAT:
p.dataPos = p.r.GetAtomPosition()
if p.movie == nil || !p.movie.parsedProfile {
err = p.r.SkipCurrentAtom()
if err != nil {
return err
}
}
p.currentState = stateParsingIDLE
break
}
}
return nil
}
func (p *mediaInfo) checkStatus() (*atom, error) {
for {
a, e := p.r.PeekAtomHeader()
if e != nil {
if e == io.EOF {
p.currentState = stateParsingEnd
}
return nil, e
}
fmt.Println(a.Type())
if fourCCftyp == a.atomType || fourCCstyp == a.atomType || fourCCmoov == a.atomType || fourCCmoof == a.atomType || fourCCsidx == a.atomType || fourCCmdat == a.atomType || fourCCssix == a.atomType {
if p.movie == nil {
// when meeting the FourCC above, the MovieInfo will be created.
p.movie = new(MovieInfo)
}
switch a.atomType {
case fourCCftyp:
fallthrough
case fourCCstyp:
p.currentState = stateParsingFTYP
case fourCCmoov:
p.currentState = stateParsingMOOV
case fourCCmoof:
p.currentState = stateParsingMOOF
case fourCCsidx:
p.currentState = stateParsingSIDX
case fourCCssix:
p.currentState = stateParsingSSIX
case fourCCmdat:
p.currentState = stateParsingMDAT
}
return a, nil
} else if fourCCskip == a.atomType || fourCCfree == a.atomType || fourCCpdin == a.atomType || fourCCprft == a.atomType || fourCCmeta == a.atomType {
_ = p.r.SkipCurrentAtom()
continue
} else {
_ = p.r.SkipCurrentAtom()
}
}
//return nil, ErrUnsupportedAtomType
}