av/generator/FLVGenerator.go

64 lines
1.1 KiB
Go

package generator
type flvGenerator struct {
fps uint
inputChan chan []byte
outputChan chan []byte
header Header
}
func (g *flvGenerator)GetInputChan() chan []byte {
return g.inputChan
}
func (g *flvGenerator)GetOutputChan() chan []byte {
return g.outputChan
}
func NewFlvGenerator() (g *flvGenerator) {
g = new(flvGenerator)
g.timestamp = 0
g.lastTagSize = 0
return
}
func (g *flvGenerator) Start(){
go g.generate()
}
func (g *flvGenerator) GenHeader(){
header := flv.Header{
AudioFlag: true,
VideoFlag: true,
}
g.outputChan <- header.toByteSlice()
}
func (g *flvGenerator) getNextTimestamp() (timestamp uint32){
timestamp = g.currentTimestamp
g.currentTimeStamp += 100*time.Millisecond() / g.fps
return
}
func (g *flvGenerator) ResetTimestamp() {
g.timestamp = 0
}
func (g *tsGenerator) generate() {
g.GenHeader()
for {
select {
case videoFrame := <-g.inputChan
tag := VideoTage{
PrevTagSize: g.lastTagSize,
TagType: flv.videoTagType,
DataSize: len(videoFrame),
Timestamp: g.getNextTimestamp(),
TimestampExtended: 0,
Data: videoFrame
}
g.outputChan<-tag.toByteSlice()
}
}
}