diff --git a/flv/FLV.go b/flv/FLV.go index 0b654b9c..b7192c02 100644 --- a/flv/FLV.go +++ b/flv/FLV.go @@ -75,85 +75,3 @@ func (h *Header) ToByteSlice() (output []byte) { fmt.Println(output) 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 -} diff --git a/generator/FLVGenerator.go b/generator/FLVGenerator.go index 70e8bff3..21c34139 100644 --- a/generator/FLVGenerator.go +++ b/generator/FLVGenerator.go @@ -115,9 +115,8 @@ func (g *flvGenerator) ResetTimestamp() { g.currentTimestamp = 0 } -// isKeyFrame checks the nature of the passed frame - returning true if the -// frame is keyframe and false otherwise -func isKeyFrame(frame []byte) bool { +// getNalType returns the naltype byte from the passed frame i.e. nalUnit +func getNalType(frame []byte) byte { byteChannel := make(chan byte, len(frame)) for i := range frame { byteChannel <- frame[i] @@ -128,49 +127,34 @@ func isKeyFrame(frame []byte) bool { aByte = <-byteChannel if (aByte == 0x01 && i == 2) || (aByte == 0x01 && i == 3) { aByte = <-byteChannel - nalType := aByte & 0x1F - switch nalType { - case interFrameCode: - return false - case keyFrameCode: - return true - case 6: - return true - } + return aByte & 0x1F } } } +} + +// 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 } // isSequenceHeader checks the nature of the passed frame and returns true // if it is a sequnce header and false otherwise func isSequenceHeader(frame []byte) bool { - byteChannel := make(chan byte, len(frame)) - for i := range frame { - byteChannel <- frame[i] - } - for len(byteChannel) >= 5 { - aByte := <-byteChannel - 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 - } - } - } + nalType := getnaleType(frame) + switch { + case nalType == 1 || naltype == 5: + return false + case nalType == 6 || nalType == 7 || nalType == 8: + return true } return false } @@ -179,16 +163,16 @@ func isSequenceHeader(frame []byte) bool { // flv tags, which are then passed to the output channel. func (g *flvGenerator) generate() { g.GenHeader() + var frameType byte + var packetType byte for { select { case videoFrame := <-g.inputChan: - var frameType byte if isKeyFrame(videoFrame) { frameType = flv.KeyFrameType } else { frameType = flv.InterFrameType } - var packetType byte if isSequenceHeader(videoFrame) { packetType = flv.SequenceHeader } else {