-
Notifications
You must be signed in to change notification settings - Fork 9
/
decoder.go
110 lines (90 loc) · 2.17 KB
/
decoder.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
//
// Copyright (c) 2018- yutopp ([email protected])
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
//
package flv
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"github.com/yutopp/go-flv/tag"
)
type Decoder struct {
r io.Reader
header *Header
decodedOnce bool
}
func NewDecoder(r io.Reader) (*Decoder, error) {
header, err := DecodeFlvHeader(r)
if err != nil {
return nil, err
}
if header.DataOffset > HeaderLength {
offset := header.DataOffset - HeaderLength
if _, err := io.CopyN(io.Discard, r, int64(offset)); err != nil {
return nil, err
}
}
return &Decoder{
r: r,
header: header,
}, nil
}
func (dec *Decoder) Header() *Header {
return dec.header
}
func (dec *Decoder) Decode(flvTag *tag.FlvTag) error {
// read previous tag size
previousTagSize, err := dec.decodeTagSize()
if err != nil {
return fmt.Errorf("failed to decode tag size: %w", err)
}
// first size must be 0
if !dec.decodedOnce {
if previousTagSize != 0 {
return fmt.Errorf("initial tag size should be 0: Actual = %d", previousTagSize)
}
dec.decodedOnce = true
}
// decode tag
if err := tag.DecodeFlvTag(dec.r, flvTag); err != nil {
return err
}
return nil
}
func (dec *Decoder) decodeTagSize() (uint32, error) {
buf := make([]byte, 4)
if _, err := io.ReadAtLeast(dec.r, buf, len(buf)); err != nil {
return 0, err
}
return binary.BigEndian.Uint32(buf), nil
}
func DecodeFlvHeader(r io.Reader) (*Header, error) {
buf := make([]byte, HeaderLength)
if _, err := io.ReadAtLeast(r, buf, len(buf)); err != nil {
return nil, err
}
signature := buf[0:3]
if !bytes.Equal(signature, HeaderSignature) {
return nil, fmt.Errorf("signature is not matched (FLV): %+v", signature)
}
version := buf[3]
flags := buf[4]
flagsAudio := (flags & 0b00000100) >> 2
flagsVideo := (flags & 0b00000001)
dataOffset := binary.BigEndian.Uint32(buf[5:9])
header := &Header{
Version: version,
DataOffset: dataOffset,
}
if flagsAudio != 0 {
header.Flags |= FlagsAudio
}
if flagsVideo != 0 {
header.Flags |= FlagsVideo
}
return header, nil
}