2018-02-10 16:25:55 +03:00
|
|
|
package flv
|
|
|
|
|
|
|
|
import (
|
2018-02-11 09:06:59 +03:00
|
|
|
"../tools"
|
2018-02-12 10:58:29 +03:00
|
|
|
"fmt"
|
2018-02-10 16:25:55 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-02-11 09:06:59 +03:00
|
|
|
headerLength = 72
|
|
|
|
version = 0x01
|
|
|
|
maxVideoTagSize = 10000
|
|
|
|
maxAudioTagSize = 10000
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-02-12 10:58:29 +03:00
|
|
|
VideoTagType = 9
|
2018-02-15 10:02:04 +03:00
|
|
|
AudioTagType = 8
|
2018-02-12 10:58:29 +03:00
|
|
|
KeyFrameType = 1
|
2018-02-15 11:13:45 +03:00
|
|
|
InterFrameType = 2
|
2018-02-12 10:58:29 +03:00
|
|
|
H264 = 7
|
|
|
|
AVCNALU = 1
|
2018-02-16 08:46:24 +03:00
|
|
|
SequenceHeader = 0
|
2018-02-12 10:58:29 +03:00
|
|
|
DataHeaderLength = 5
|
2018-02-12 16:31:19 +03:00
|
|
|
NoTimestampExtension = 0
|
2018-02-15 10:02:04 +03:00
|
|
|
AACAudioFormat = 10
|
2018-02-19 08:06:13 +03:00
|
|
|
PCMAudioFormat = 0
|
2018-02-10 16:25:55 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Header struct {
|
2018-02-11 09:06:59 +03:00
|
|
|
AudioFlag bool
|
|
|
|
VideoFlag bool
|
2018-02-10 16:25:55 +03:00
|
|
|
}
|
|
|
|
|
2018-02-12 10:58:29 +03:00
|
|
|
func (h *Header) ToByteSlice() (output []byte) {
|
2018-02-11 09:06:59 +03:00
|
|
|
output = make([]byte, 0, headerLength)
|
|
|
|
output = append(output, []byte{0x46, 0x4C, 0x56,
|
|
|
|
version,
|
2018-02-12 10:58:29 +03:00
|
|
|
0x00 | tools.BoolToByte(h.AudioFlag)<<2 | tools.BoolToByte(h.VideoFlag),
|
|
|
|
0x00, 0x00, 0x00, byte(9),
|
2018-02-11 09:06:59 +03:00
|
|
|
}...)
|
2018-02-12 10:58:29 +03:00
|
|
|
fmt.Println(output)
|
2018-02-11 09:06:59 +03:00
|
|
|
return
|
2018-02-10 16:25:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type VideoTag struct {
|
2018-02-11 09:06:59 +03:00
|
|
|
TagType uint8
|
|
|
|
DataSize uint32
|
|
|
|
Timestamp uint32
|
|
|
|
TimestampExtended uint32
|
2018-02-12 10:58:29 +03:00
|
|
|
FrameType byte
|
|
|
|
Codec byte
|
|
|
|
PacketType byte
|
|
|
|
CompositionTime uint32
|
2018-02-11 09:06:59 +03:00
|
|
|
Data []byte
|
2018-02-19 08:06:13 +03:00
|
|
|
PrevTagSize uint32
|
2018-02-10 16:25:55 +03:00
|
|
|
}
|
|
|
|
|
2018-02-12 10:58:29 +03:00
|
|
|
func (t *VideoTag) ToByteSlice() (output []byte) {
|
2018-02-11 09:06:59 +03:00
|
|
|
output = make([]byte, 0, maxVideoTagSize)
|
|
|
|
output = append(output, []byte{
|
2018-02-12 10:58:29 +03:00
|
|
|
byte(t.TagType),
|
|
|
|
byte(t.DataSize >> 16),
|
|
|
|
byte(t.DataSize >> 8),
|
|
|
|
byte(t.DataSize),
|
|
|
|
byte(t.Timestamp >> 16),
|
|
|
|
byte(t.Timestamp >> 8),
|
|
|
|
byte(t.Timestamp),
|
|
|
|
byte(t.TimestampExtended),
|
2018-02-11 09:06:59 +03:00
|
|
|
0x00, 0x00, 0x00,
|
2018-02-15 11:13:45 +03:00
|
|
|
0x00 | byte(t.FrameType << 4) | byte(t.Codec),
|
2018-02-12 10:58:29 +03:00
|
|
|
t.PacketType,
|
|
|
|
byte(t.CompositionTime >> 16),byte(t.CompositionTime >> 8),byte(t.CompositionTime),
|
2018-02-11 09:06:59 +03:00
|
|
|
}...)
|
2018-02-12 10:58:29 +03:00
|
|
|
output = append(output, t.Data...)
|
2018-02-19 08:06:13 +03:00
|
|
|
output = append(output, []byte{
|
|
|
|
byte(t.PrevTagSize >> 24),
|
|
|
|
byte(t.PrevTagSize >> 16),
|
|
|
|
byte(t.PrevTagSize >> 8),
|
|
|
|
byte(t.PrevTagSize),
|
|
|
|
}...)
|
2018-02-11 09:06:59 +03:00
|
|
|
return
|
2018-02-10 16:25:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type AudioTag struct {
|
2018-02-15 10:02:04 +03:00
|
|
|
TagType uint8
|
|
|
|
DataSize uint32
|
|
|
|
Timestamp uint32
|
|
|
|
TimestampExtended uint32
|
2018-02-12 10:58:29 +03:00
|
|
|
SoundFormat uint8
|
2018-02-15 10:02:04 +03:00
|
|
|
SoundRate uint8
|
|
|
|
SoundSize bool
|
|
|
|
SoundType bool
|
|
|
|
Data []byte
|
2018-02-19 08:06:13 +03:00
|
|
|
PrevTagSize uint32
|
2018-02-10 16:25:55 +03:00
|
|
|
}
|
|
|
|
|
2018-02-12 10:58:29 +03:00
|
|
|
func (t *AudioTag) ToByteSlice() (output []byte) {
|
2018-02-15 10:02:04 +03:00
|
|
|
output = make([]byte, 0, maxVideoTagSize)
|
|
|
|
output = append(output, []byte{
|
|
|
|
byte(t.TagType),
|
|
|
|
byte(t.DataSize >> 16),
|
|
|
|
byte(t.DataSize >> 8),
|
|
|
|
byte(t.DataSize),
|
|
|
|
byte(t.Timestamp >> 16),
|
|
|
|
byte(t.Timestamp >> 8),
|
|
|
|
byte(t.Timestamp),
|
|
|
|
byte(t.TimestampExtended),
|
|
|
|
0x00, 0x00, 0x00,
|
|
|
|
byte(t.SoundFormat << 4) | byte(t.SoundRate<<2) | byte(tools.BoolToByte(t.SoundSize)<<1) | byte(tools.BoolToByte(t.SoundType)),
|
|
|
|
}...)
|
2018-02-12 10:58:29 +03:00
|
|
|
output = append(output, t.Data...)
|
2018-02-19 08:06:13 +03:00
|
|
|
output = append(output, []byte{
|
|
|
|
byte(t.PrevTagSize >> 24),
|
|
|
|
byte(t.PrevTagSize >> 16),
|
|
|
|
byte(t.PrevTagSize >> 8),
|
|
|
|
byte(t.PrevTagSize),
|
|
|
|
}...)
|
2018-02-11 09:06:59 +03:00
|
|
|
return
|
2018-02-10 16:25:55 +03:00
|
|
|
}
|