forked from pion/webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ivfwriter.go
230 lines (191 loc) · 4.92 KB
/
ivfwriter.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
// Package ivfwriter implements IVF media container writer
package ivfwriter
import (
"encoding/binary"
"errors"
"io"
"os"
"github.com/pion/rtp"
"github.com/pion/rtp/codecs"
"github.com/pion/rtp/pkg/frame"
)
var (
errFileNotOpened = errors.New("file not opened")
errInvalidNilPacket = errors.New("invalid nil packet")
errCodecAlreadySet = errors.New("codec is already set")
errNoSuchCodec = errors.New("no codec for this MimeType")
)
const (
mimeTypeVP8 = "video/VP8"
mimeTypeAV1 = "video/AV1"
ivfFileHeaderSignature = "DKIF"
)
// IVFWriter is used to take RTP packets and write them to an IVF on disk
type IVFWriter struct {
ioWriter io.Writer
count uint64
seenKeyFrame bool
isVP8, isAV1 bool
// VP8
currentFrame []byte
// AV1
av1Frame frame.AV1
}
// New builds a new IVF writer
func New(fileName string, opts ...Option) (*IVFWriter, error) {
f, err := os.Create(fileName) //nolint:gosec
if err != nil {
return nil, err
}
writer, err := NewWith(f, opts...)
if err != nil {
return nil, err
}
writer.ioWriter = f
return writer, nil
}
// NewWith initialize a new IVF writer with an io.Writer output
func NewWith(out io.Writer, opts ...Option) (*IVFWriter, error) {
if out == nil {
return nil, errFileNotOpened
}
writer := &IVFWriter{
ioWriter: out,
seenKeyFrame: false,
}
for _, o := range opts {
if err := o(writer); err != nil {
return nil, err
}
}
if !writer.isAV1 && !writer.isVP8 {
writer.isVP8 = true
}
if err := writer.writeHeader(); err != nil {
return nil, err
}
return writer, nil
}
func (i *IVFWriter) writeHeader() error {
header := make([]byte, 32)
copy(header[0:], ivfFileHeaderSignature) // DKIF
binary.LittleEndian.PutUint16(header[4:], 0) // Version
binary.LittleEndian.PutUint16(header[6:], 32) // Header size
// FOURCC
if i.isVP8 {
copy(header[8:], "VP80")
} else if i.isAV1 {
copy(header[8:], "AV01")
}
binary.LittleEndian.PutUint16(header[12:], 640) // Width in pixels
binary.LittleEndian.PutUint16(header[14:], 480) // Height in pixels
binary.LittleEndian.PutUint32(header[16:], 30) // Framerate denominator
binary.LittleEndian.PutUint32(header[20:], 1) // Framerate numerator
binary.LittleEndian.PutUint32(header[24:], 900) // Frame count, will be updated on first Close() call
binary.LittleEndian.PutUint32(header[28:], 0) // Unused
_, err := i.ioWriter.Write(header)
return err
}
func (i *IVFWriter) writeFrame(frame []byte) error {
frameHeader := make([]byte, 12)
binary.LittleEndian.PutUint32(frameHeader[0:], uint32(len(frame))) // Frame length
binary.LittleEndian.PutUint64(frameHeader[4:], i.count) // PTS
i.count++
if _, err := i.ioWriter.Write(frameHeader); err != nil {
return err
}
_, err := i.ioWriter.Write(frame)
return err
}
// WriteRTP adds a new packet and writes the appropriate headers for it
func (i *IVFWriter) WriteRTP(packet *rtp.Packet) error {
if i.ioWriter == nil {
return errFileNotOpened
} else if len(packet.Payload) == 0 {
return nil
}
if i.isVP8 {
vp8Packet := codecs.VP8Packet{}
if _, err := vp8Packet.Unmarshal(packet.Payload); err != nil {
return err
}
isKeyFrame := vp8Packet.Payload[0] & 0x01
switch {
case !i.seenKeyFrame && isKeyFrame == 1:
return nil
case i.currentFrame == nil && vp8Packet.S != 1:
return nil
}
i.seenKeyFrame = true
i.currentFrame = append(i.currentFrame, vp8Packet.Payload[0:]...)
if !packet.Marker {
return nil
} else if len(i.currentFrame) == 0 {
return nil
}
if err := i.writeFrame(i.currentFrame); err != nil {
return err
}
i.currentFrame = nil
} else if i.isAV1 {
av1Packet := &codecs.AV1Packet{}
if _, err := av1Packet.Unmarshal(packet.Payload); err != nil {
return err
}
obus, err := i.av1Frame.ReadFrames(av1Packet)
if err != nil {
return err
}
for j := range obus {
if err := i.writeFrame(obus[j]); err != nil {
return err
}
}
}
return nil
}
// Close stops the recording
func (i *IVFWriter) Close() error {
if i.ioWriter == nil {
// Returns no error as it may be convenient to call
// Close() multiple times
return nil
}
defer func() {
i.ioWriter = nil
}()
if ws, ok := i.ioWriter.(io.WriteSeeker); ok {
// Update the framecount
if _, err := ws.Seek(24, 0); err != nil {
return err
}
buff := make([]byte, 4)
binary.LittleEndian.PutUint32(buff, uint32(i.count))
if _, err := ws.Write(buff); err != nil {
return err
}
}
if closer, ok := i.ioWriter.(io.Closer); ok {
return closer.Close()
}
return nil
}
// An Option configures a SampleBuilder.
type Option func(i *IVFWriter) error
// WithCodec configures if IVFWriter is writing AV1 or VP8 packets to disk
func WithCodec(mimeType string) Option {
return func(i *IVFWriter) error {
if i.isVP8 || i.isAV1 {
return errCodecAlreadySet
}
switch mimeType {
case mimeTypeVP8:
i.isVP8 = true
case mimeTypeAV1:
i.isAV1 = true
default:
return errNoSuchCodec
}
return nil
}
}