|
21 | 21 |
|
22 | 22 | // The oryx FLV package support bytes from/to FLV tags.
|
23 | 23 | package flv
|
| 24 | + |
| 25 | +import ( |
| 26 | + "bytes" |
| 27 | + "errors" |
| 28 | + "io" |
| 29 | +) |
| 30 | + |
| 31 | +// FLV Tag Type is the type of tag, |
| 32 | +// refer to @doc video_file_format_spec_v10.pdf, @page 9, @section FLV tags |
| 33 | +type TagType uint8 |
| 34 | + |
| 35 | +const ( |
| 36 | + TagTypeForbidden TagType = 0 |
| 37 | + TagTypeAudio TagType = 8 |
| 38 | + TagTypeVideo TagType = 9 |
| 39 | + TagTypeScriptData TagType = 18 |
| 40 | +) |
| 41 | + |
| 42 | +func (v TagType) String() string { |
| 43 | + switch v { |
| 44 | + case TagTypeVideo: |
| 45 | + return "Video" |
| 46 | + case TagTypeAudio: |
| 47 | + return "Audio" |
| 48 | + case TagTypeScriptData: |
| 49 | + return "Data" |
| 50 | + default: |
| 51 | + return "Forbidden" |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// FLV Demuxer is used to demux FLV file. |
| 56 | +// Refer to @doc video_file_format_spec_v10.pdf, @page 7, @section The FLV File Format |
| 57 | +// A FLV file must consist the bellow parts: |
| 58 | +// 1. A FLV header, refer to @doc video_file_format_spec_v10.pdf, @page 8, @section The FLV header |
| 59 | +// 2. One or more tags, refer to @doc video_file_format_spec_v10.pdf, @page 9, @section FLV tags |
| 60 | +// @remark We always ignore the previous tag size. |
| 61 | +type Demuxer interface { |
| 62 | + // Read the FLV header, return the version of FLV, whether hasVideo or hasAudio in header. |
| 63 | + ReadHeader() (version uint8, hasVideo, hasAudio bool, err error) |
| 64 | + // Read the FLV tag header, return the tag information, especially the tag size, |
| 65 | + // then user can read the tag payload. |
| 66 | + ReadTagHeader() (tagType TagType, tagSize, timestamp uint32, err error) |
| 67 | + // Read the FLV tag body, drop the next 4 bytes previous tag size. |
| 68 | + ReadTag(tagSize uint32) (tag []byte, err error) |
| 69 | +} |
| 70 | + |
| 71 | +// When FLV signature is not "FLV" |
| 72 | +var ErrSignature = errors.New("FLV signatures are illegal") |
| 73 | + |
| 74 | +// Create a demuxer object. |
| 75 | +func NewDemuxer(r io.Reader) Demuxer { |
| 76 | + return &demuxer{ |
| 77 | + r: r, |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +type demuxer struct { |
| 82 | + r io.Reader |
| 83 | +} |
| 84 | + |
| 85 | +func (v *demuxer) ReadHeader() (version uint8, hasVideo, hasAudio bool, err error) { |
| 86 | + h := &bytes.Buffer{} |
| 87 | + if _, err = io.CopyN(h, v.r, 13); err != nil { |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + p := h.Bytes() |
| 92 | + |
| 93 | + if !bytes.Equal([]byte{byte('F'), byte('L'), byte('V')}, p[:3]) { |
| 94 | + err = ErrSignature |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + version = uint8(p[3]) |
| 99 | + hasVideo = (p[4] & 0x01) == 0x01 |
| 100 | + hasAudio = ((p[4] >> 2) & 0x01) == 0x01 |
| 101 | + |
| 102 | + return |
| 103 | +} |
| 104 | + |
| 105 | +func (v *demuxer) ReadTagHeader() (tagType TagType, tagSize uint32, timestamp uint32, err error) { |
| 106 | + h := &bytes.Buffer{} |
| 107 | + if _, err = io.CopyN(h, v.r, 11); err != nil { |
| 108 | + return |
| 109 | + } |
| 110 | + |
| 111 | + p := h.Bytes() |
| 112 | + |
| 113 | + tagType = TagType(p[0]) |
| 114 | + tagSize = uint32(p[1])<<16 | uint32(p[2])<<8 | uint32(p[3]) |
| 115 | + timestamp = uint32(p[7])<<24 | uint32(p[4])<<16 | uint32(p[5])<<8 | uint32(p[6]) |
| 116 | + |
| 117 | + return |
| 118 | +} |
| 119 | + |
| 120 | +func (v *demuxer) ReadTag(tagSize uint32) (tag []byte, err error) { |
| 121 | + h := &bytes.Buffer{} |
| 122 | + if _, err = io.CopyN(h, v.r, int64(tagSize+4)); err != nil { |
| 123 | + return |
| 124 | + } |
| 125 | + |
| 126 | + p := h.Bytes() |
| 127 | + tag = p[0 : len(p)-4] |
| 128 | + |
| 129 | + return |
| 130 | +} |
0 commit comments