-
Notifications
You must be signed in to change notification settings - Fork 47
/
astiav_test.go
245 lines (205 loc) · 5.29 KB
/
astiav_test.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package astiav
import (
"errors"
"fmt"
"os"
"sync"
"testing"
"github.com/asticode/go-astikit"
)
var globalHelper = newHelper()
func TestMain(m *testing.M) {
// Make sure to exit with the proper code
var code int
defer func(code *int) {
os.Exit(*code)
}(&code)
// Make sure to close global helper
defer globalHelper.close()
// Run
code = m.Run()
}
type helper struct {
closer *astikit.Closer
inputs map[string]*helperInput
m *sync.Mutex // Locks inputs
}
func newHelper() *helper {
return &helper{
closer: astikit.NewCloser(),
inputs: make(map[string]*helperInput),
m: &sync.Mutex{},
}
}
func (h *helper) close() {
h.closer.Close()
}
type helperInput struct {
firstPkt *Packet
formatContext *FormatContext
lastFrames map[MediaType]*Frame
}
func newHelperInput() *helperInput {
return &helperInput{lastFrames: make(map[MediaType]*Frame)}
}
func (h *helper) inputFormatContext(name string, ifmt *InputFormat) (fc *FormatContext, err error) {
h.m.Lock()
i, ok := h.inputs[name]
if ok && i.formatContext != nil {
h.m.Unlock()
return i.formatContext, nil
}
h.m.Unlock()
if fc = AllocFormatContext(); fc == nil {
err = errors.New("astiav_test: allocated format context is nil")
return
}
h.closer.Add(fc.Free)
if err = fc.OpenInput("testdata/"+name, ifmt, nil); err != nil {
err = fmt.Errorf("astiav_test: opening input failed: %w", err)
return
}
h.closer.Add(fc.CloseInput)
if err = fc.FindStreamInfo(nil); err != nil {
err = fmt.Errorf("astiav_test: finding stream info failed: %w", err)
return
}
h.m.Lock()
if _, ok := h.inputs[name]; !ok {
h.inputs[name] = newHelperInput()
}
h.inputs[name].formatContext = fc
h.m.Unlock()
return
}
func (h *helper) inputFirstPacket(name string) (pkt *Packet, err error) {
h.m.Lock()
i, ok := h.inputs[name]
if ok && i.firstPkt != nil {
h.m.Unlock()
return i.firstPkt, nil
}
h.m.Unlock()
var fc *FormatContext
if fc, err = h.inputFormatContext(name, nil); err != nil {
err = fmt.Errorf("astiav_test: getting input format context failed")
return
}
pkt = AllocPacket()
if pkt == nil {
err = errors.New("astiav_test: pkt is nil")
return
}
h.closer.Add(pkt.Free)
if err = fc.ReadFrame(pkt); err != nil {
err = fmt.Errorf("astiav_test: reading frame failed: %w", err)
return
}
h.m.Lock()
h.inputs[name].firstPkt = pkt
h.m.Unlock()
return
}
func (h *helper) inputLastFrame(name string, mediaType MediaType, ifmt *InputFormat) (*Frame, error) {
h.m.Lock()
if i, ok := h.inputs[name]; ok {
if len(i.lastFrames) > 0 {
f, ok := i.lastFrames[mediaType]
h.m.Unlock()
if ok {
return f, nil
}
return nil, fmt.Errorf("astiav_test: no last frame for media type %s", mediaType)
}
}
h.m.Unlock()
fc, err := h.inputFormatContext(name, ifmt)
if err != nil {
return nil, fmt.Errorf("astiav_test: getting input format context failed: %w", err)
}
type stream struct {
cc *CodecContext
s *Stream
}
streams := make(map[int]*stream)
mediaTypeFound := false
for _, v := range fc.Streams() {
s := &stream{s: v}
streams[v.Index()] = s
c := FindDecoder(v.CodecParameters().CodecID())
if c == nil {
return nil, errors.New("astiav_test: no codec")
}
s.cc = AllocCodecContext(c)
if s.cc == nil {
return nil, errors.New("astiav_test: no codec context")
}
h.closer.Add(s.cc.Free)
if err = s.s.CodecParameters().ToCodecContext(s.cc); err != nil {
return nil, fmt.Errorf("astiav_test: updating codec context failed: %w", err)
}
if err = s.cc.Open(c, nil); err != nil {
return nil, fmt.Errorf("astiav_test: opening codec context failed: %w", err)
}
if _, ok := h.inputs[name].lastFrames[s.cc.MediaType()]; !ok {
h.inputs[name].lastFrames[s.cc.MediaType()] = AllocFrame()
h.closer.Add(h.inputs[name].lastFrames[s.cc.MediaType()].Free)
}
if s.cc.MediaType() == mediaType {
mediaTypeFound = true
}
}
if !mediaTypeFound {
return nil, fmt.Errorf("astiav_test: no stream for media type %s", mediaType)
}
var pkt1 *Packet
if pkt1, err = h.inputFirstPacket(name); err != nil {
return nil, fmt.Errorf("astiav_test: getting input first packet failed: %w", err)
}
pkt2 := AllocPacket()
h.closer.Add(pkt2.Free)
f := AllocFrame()
h.closer.Add(f.Free)
pkts := []*Packet{pkt1}
for {
if err = fc.ReadFrame(pkt2); err != nil {
if errors.Is(err, ErrEof) || errors.Is(err, ErrEagain) {
if len(pkts) == 0 {
err = nil
break
}
} else {
return nil, fmt.Errorf("astiav_test: reading frame failed: %w", err)
}
} else {
pkts = append(pkts, pkt2)
}
for _, pkt := range pkts {
s, ok := streams[pkt.StreamIndex()]
if !ok {
continue
}
if err = s.cc.SendPacket(pkt); err != nil {
return nil, fmt.Errorf("astiav_test: sending packet failed: %w", err)
}
for {
if err = s.cc.ReceiveFrame(f); err != nil {
if errors.Is(err, ErrEof) || errors.Is(err, ErrEagain) {
err = nil
break
}
return nil, fmt.Errorf("astiav_test: receiving frame failed: %w", err)
}
h.m.Lock()
h.inputs[name].lastFrames[s.cc.MediaType()].Unref()
err = h.inputs[name].lastFrames[s.cc.MediaType()].Ref(f)
h.m.Unlock()
if err != nil {
return nil, fmt.Errorf("astiav_test: refing frame failed: %w", err)
}
}
}
pkts = []*Packet{}
}
return h.inputs[name].lastFrames[mediaType], nil
}