think I'm done for the night

This commit is contained in:
Saxon Milton 2018-03-01 01:20:14 +10:30
parent fdb44de57e
commit 1d27e80bdc
2 changed files with 23 additions and 121 deletions

View File

@ -75,85 +75,3 @@ func (h *Header) ToByteSlice() (output []byte) {
fmt.Println(output) fmt.Println(output)
return return
} }
type VideoTag struct {
TagType uint8
DataSize uint32
Timestamp uint32
TimestampExtended uint32
FrameType byte
Codec byte
PacketType byte
CompositionTime uint32
Data []byte
PrevTagSize uint32
}
func (t *VideoTag) ToByteSlice() (output []byte) {
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,
0x00 | byte(t.FrameType<<4) | byte(t.Codec),
t.PacketType,
byte(t.CompositionTime >> 16),
byte(t.CompositionTime >> 8),
byte(t.CompositionTime),
}...)
output = append(output, t.Data...)
output = append(output, []byte{
byte(t.PrevTagSize >> 24),
byte(t.PrevTagSize >> 16),
byte(t.PrevTagSize >> 8),
byte(t.PrevTagSize),
}...)
return
}
type AudioTag struct {
TagType uint8
DataSize uint32
Timestamp uint32
TimestampExtended uint32
SoundFormat uint8
SoundRate uint8
SoundSize bool
SoundType bool
Data []byte
PrevTagSize uint32
}
func (t *AudioTag) ToByteSlice() (output []byte) {
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) | btb(t.SoundSize)<<1 | btb(t.SoundType),
}...)
output = append(output, t.Data...)
output = append(output, []byte{
byte(t.PrevTagSize >> 24),
byte(t.PrevTagSize >> 16),
byte(t.PrevTagSize >> 8),
byte(t.PrevTagSize),
}...)
return
}

View File

@ -115,9 +115,8 @@ func (g *flvGenerator) ResetTimestamp() {
g.currentTimestamp = 0 g.currentTimestamp = 0
} }
// isKeyFrame checks the nature of the passed frame - returning true if the // getNalType returns the naltype byte from the passed frame i.e. nalUnit
// frame is keyframe and false otherwise func getNalType(frame []byte) byte {
func isKeyFrame(frame []byte) bool {
byteChannel := make(chan byte, len(frame)) byteChannel := make(chan byte, len(frame))
for i := range frame { for i := range frame {
byteChannel <- frame[i] byteChannel <- frame[i]
@ -128,49 +127,34 @@ func isKeyFrame(frame []byte) bool {
aByte = <-byteChannel aByte = <-byteChannel
if (aByte == 0x01 && i == 2) || (aByte == 0x01 && i == 3) { if (aByte == 0x01 && i == 2) || (aByte == 0x01 && i == 3) {
aByte = <-byteChannel aByte = <-byteChannel
nalType := aByte & 0x1F return aByte & 0x1F
switch nalType {
case interFrameCode:
return false
case keyFrameCode:
return true
case 6:
return true
}
} }
} }
} }
}
// isKeyFrame checks the nature of the passed frame - returning true if the
// frame is keyframe and false otherwise
func isKeyFrame(frame []byte) bool {
nalType := getNaleType(frame)
switch {
case nalType == interFrameCode:
return false
case nalType == keyFramecode || nalType == 6:
return true
}
return false return false
} }
// isSequenceHeader checks the nature of the passed frame and returns true // isSequenceHeader checks the nature of the passed frame and returns true
// if it is a sequnce header and false otherwise // if it is a sequnce header and false otherwise
func isSequenceHeader(frame []byte) bool { func isSequenceHeader(frame []byte) bool {
byteChannel := make(chan byte, len(frame)) nalType := getnaleType(frame)
for i := range frame { switch {
byteChannel <- frame[i] case nalType == 1 || naltype == 5:
} return false
for len(byteChannel) >= 5 { case nalType == 6 || nalType == 7 || nalType == 8:
aByte := <-byteChannel return true
for i := 1; aByte == 0x00 && i != 4; i++ {
aByte = <-byteChannel
if (aByte == 0x01 && i == 2) || (aByte == 0x01 && i == 3) {
aByte = <-byteChannel
nalType := aByte & 0x1F
switch nalType {
case 1:
return false
case 5:
return false
case 6:
return true
case 7:
return true
case 8:
return true
}
}
}
} }
return false return false
} }
@ -179,16 +163,16 @@ func isSequenceHeader(frame []byte) bool {
// flv tags, which are then passed to the output channel. // flv tags, which are then passed to the output channel.
func (g *flvGenerator) generate() { func (g *flvGenerator) generate() {
g.GenHeader() g.GenHeader()
var frameType byte
var packetType byte
for { for {
select { select {
case videoFrame := <-g.inputChan: case videoFrame := <-g.inputChan:
var frameType byte
if isKeyFrame(videoFrame) { if isKeyFrame(videoFrame) {
frameType = flv.KeyFrameType frameType = flv.KeyFrameType
} else { } else {
frameType = flv.InterFrameType frameType = flv.InterFrameType
} }
var packetType byte
if isSequenceHeader(videoFrame) { if isSequenceHeader(videoFrame) {
packetType = flv.SequenceHeader packetType = flv.SequenceHeader
} else { } else {