/
h264writer.go
96 lines (77 loc) · 1.87 KB
/
h264writer.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
// Package h264writer implements H264 media container writer
package h264writer
import (
"bytes"
"encoding/binary"
"io"
"os"
"github.com/pion/rtp"
"github.com/pion/rtp/codecs"
)
type (
// H264Writer is used to take RTP packets, parse them and
// write the data to an io.Writer.
// Currently it only supports non-interleaved mode
// Therefore, only 1-23, 24 (STAP-A), 28 (FU-A) NAL types are allowed.
// https://tools.ietf.org/html/rfc6184#section-5.2
H264Writer struct {
writer io.Writer
hasKeyFrame bool
cachedPacket *codecs.H264Packet
}
)
// New builds a new H264 writer
func New(filename string) (*H264Writer, error) {
f, err := os.Create(filename)
if err != nil {
return nil, err
}
return NewWith(f), nil
}
// NewWith initializes a new H264 writer with an io.Writer output
func NewWith(w io.Writer) *H264Writer {
return &H264Writer{
writer: w,
}
}
// WriteRTP adds a new packet and writes the appropriate headers for it
func (h *H264Writer) WriteRTP(packet *rtp.Packet) error {
if len(packet.Payload) == 0 {
return nil
}
if !h.hasKeyFrame {
if h.hasKeyFrame = isKeyFrame(packet.Payload); !h.hasKeyFrame {
// key frame not defined yet. discarding packet
return nil
}
}
if h.cachedPacket == nil {
h.cachedPacket = &codecs.H264Packet{}
}
data, err := h.cachedPacket.Unmarshal(packet.Payload)
if err != nil {
return err
}
_, err = h.writer.Write(data)
return err
}
// Close closes the underlying writer
func (h *H264Writer) Close() error {
h.cachedPacket = nil
if h.writer != nil {
if closer, ok := h.writer.(io.Closer); ok {
return closer.Close()
}
}
return nil
}
func isKeyFrame(data []byte) bool {
const typeSTAPA = 24
var word uint32
payload := bytes.NewReader(data)
err := binary.Read(payload, binary.BigEndian, &word)
if err != nil || (word&0x1F000000)>>24 != typeSTAPA {
return false
}
return word&0x1F == 7
}