2018-02-13 11:39:26 +03:00
|
|
|
/*
|
|
|
|
NAME
|
2018-02-28 16:46:59 +03:00
|
|
|
FLVGenerator.go
|
2018-02-13 11:39:26 +03:00
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
See Readme.md
|
|
|
|
|
|
|
|
AUTHOR
|
2018-02-28 16:46:59 +03:00
|
|
|
Saxon Nelson-Milton <saxon@ausocean.org>
|
2018-02-13 11:39:26 +03:00
|
|
|
|
|
|
|
LICENSE
|
2018-02-28 16:46:59 +03:00
|
|
|
FLVGenerator.go is Copyright (C) 2017 the Australian Ocean Lab (AusOcean)
|
2018-02-13 11:39:26 +03:00
|
|
|
|
|
|
|
It is free software: you can redistribute it and/or modify them
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with revid in gpl.txt. If not, see [GNU licenses](http://www.gnu.org/licenses).
|
|
|
|
*/
|
2018-02-10 16:25:55 +03:00
|
|
|
package generator
|
2018-02-10 09:59:56 +03:00
|
|
|
|
2018-02-12 10:58:29 +03:00
|
|
|
import (
|
|
|
|
"../flv"
|
2018-02-28 16:46:59 +03:00
|
|
|
_ "fmt"
|
|
|
|
_ "time"
|
2018-02-12 10:58:29 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-02-28 16:46:59 +03:00
|
|
|
inputChanLength = 1000
|
2018-02-12 10:58:29 +03:00
|
|
|
outputChanLength = 1000
|
2018-02-28 16:46:59 +03:00
|
|
|
audioSize = 18
|
|
|
|
videoHeaderSize = 16
|
|
|
|
interFrameCode = 1
|
|
|
|
keyFrameCode = 5
|
|
|
|
sequenceCode = 6
|
2018-02-12 10:58:29 +03:00
|
|
|
)
|
|
|
|
|
2018-02-28 16:46:59 +03:00
|
|
|
// Data representing silent audio (required for youtube)
|
|
|
|
var dummyAudioTag1Data = []byte{0x00, 0x12, 0x08, 0x56, 0xe5, 0x00}
|
|
|
|
var dummyAudioTag2Data = []byte{0x01, 0xdc, 0x00, 0x4c, 0x61, 0x76, 0x63, 0x35,
|
|
|
|
0x38, 0x2e, 0x36, 0x2e, 0x31, 0x30, 0x32, 0x00, 0x02, 0x30, 0x40, 0x0e}
|
|
|
|
|
|
|
|
// flvGenerator provides properties required for the generation of flv video
|
|
|
|
// from raw video data
|
2018-02-10 16:25:55 +03:00
|
|
|
type flvGenerator struct {
|
2018-02-28 16:46:59 +03:00
|
|
|
fps uint
|
|
|
|
inputChan chan []byte
|
|
|
|
outputChan chan []byte
|
|
|
|
audioFlag bool
|
|
|
|
videoFlag bool
|
|
|
|
lastTagSize int
|
2018-02-12 10:58:29 +03:00
|
|
|
currentTimestamp uint32
|
2018-02-28 16:46:59 +03:00
|
|
|
header flv.Header
|
2018-02-10 16:25:55 +03:00
|
|
|
}
|
|
|
|
|
2018-02-28 16:46:59 +03:00
|
|
|
// GetInputChan returns the input channel to the generator. This is where the
|
|
|
|
// raw data frames are entered into the generator
|
|
|
|
func (g *flvGenerator) GetInputChan() chan []byte {
|
2018-02-10 16:25:55 +03:00
|
|
|
return g.inputChan
|
|
|
|
}
|
|
|
|
|
2018-02-28 16:46:59 +03:00
|
|
|
// GetOutputChan retuns the output chan of the generator - this is where the
|
|
|
|
// flv packets (more specifically tags) are outputted.
|
|
|
|
func (g *flvGenerator) GetOutputChan() chan []byte {
|
2018-02-10 16:25:55 +03:00
|
|
|
return g.outputChan
|
|
|
|
}
|
|
|
|
|
2018-02-28 16:46:59 +03:00
|
|
|
// NewFlvGenerator retuns an instance of the flvGenerator struct
|
2018-02-12 10:58:29 +03:00
|
|
|
func NewFlvGenerator(audio bool, video bool, fps uint) (g *flvGenerator) {
|
2018-02-10 16:25:55 +03:00
|
|
|
g = new(flvGenerator)
|
2018-02-12 10:58:29 +03:00
|
|
|
g.fps = fps
|
|
|
|
g.audioFlag = audio
|
|
|
|
g.videoFlag = video
|
2018-02-28 16:46:59 +03:00
|
|
|
g.currentTimestamp = 0
|
2018-02-11 09:06:59 +03:00
|
|
|
g.lastTagSize = 0
|
2018-02-12 10:58:29 +03:00
|
|
|
g.inputChan = make(chan []byte, inputChanLength)
|
|
|
|
g.outputChan = make(chan []byte, outputChanLength)
|
2018-02-10 16:25:55 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-28 16:46:59 +03:00
|
|
|
// Start beings the generation routine - i.e. if raw data is given to the input
|
|
|
|
// channel flv tags will be produced and available from the output channel.
|
|
|
|
func (g *flvGenerator) Start() {
|
2018-02-10 16:25:55 +03:00
|
|
|
go g.generate()
|
|
|
|
}
|
|
|
|
|
2018-02-28 16:46:59 +03:00
|
|
|
// GenHeader generates the flv header and sends it down the output chan for use
|
|
|
|
// This will generally be called once at the start of file writing/transmission.
|
|
|
|
func (g *flvGenerator) GenHeader() {
|
|
|
|
header := flv.Header{
|
2018-02-12 10:58:29 +03:00
|
|
|
AudioFlag: g.audioFlag,
|
|
|
|
VideoFlag: g.videoFlag,
|
2018-02-28 16:46:59 +03:00
|
|
|
}
|
|
|
|
g.outputChan <- header.ToByteSlice()
|
2018-02-10 16:25:55 +03:00
|
|
|
}
|
|
|
|
|
2018-02-28 16:46:59 +03:00
|
|
|
// getNextTimestamp generates and returns the next timestamp based on the given
|
|
|
|
// fps rate
|
|
|
|
func (g *flvGenerator) getNextTimestamp() (timestamp uint32) {
|
|
|
|
timestamp = g.currentTimestamp
|
|
|
|
g.currentTimestamp += uint32(1000) / uint32(g.fps)
|
|
|
|
return
|
2018-02-10 16:25:55 +03:00
|
|
|
}
|
|
|
|
|
2018-02-28 16:46:59 +03:00
|
|
|
// ResetTimestamp resets the current timestamp to 0 i.e. equivalent to start of
|
|
|
|
// transmission
|
2018-02-10 16:25:55 +03:00
|
|
|
func (g *flvGenerator) ResetTimestamp() {
|
2018-02-28 16:46:59 +03:00
|
|
|
g.currentTimestamp = 0
|
2018-02-10 16:25:55 +03:00
|
|
|
}
|
|
|
|
|
2018-02-28 16:46:59 +03:00
|
|
|
// isKeyFrame checks the nature of the passed frame - returning true if the
|
|
|
|
// frame is keyframe and false otherwise
|
2018-02-16 08:46:24 +03:00
|
|
|
func isKeyFrame(frame []byte) bool {
|
|
|
|
byteChannel := make(chan byte, len(frame))
|
|
|
|
for i := range frame {
|
|
|
|
byteChannel <- frame[i]
|
|
|
|
}
|
2018-02-28 16:46:59 +03:00
|
|
|
for len(byteChannel) >= 5 {
|
|
|
|
aByte := <-byteChannel
|
|
|
|
for i := 1; aByte == 0x00 && i != 4; i++ {
|
2018-02-16 08:46:24 +03:00
|
|
|
aByte = <-byteChannel
|
2018-02-28 16:46:59 +03:00
|
|
|
if (aByte == 0x01 && i == 2) || (aByte == 0x01 && i == 3) {
|
2018-02-16 08:46:24 +03:00
|
|
|
aByte = <-byteChannel
|
|
|
|
nalType := aByte & 0x1F
|
|
|
|
switch nalType {
|
2018-02-19 08:06:13 +03:00
|
|
|
case interFrameCode:
|
2018-02-16 08:46:24 +03:00
|
|
|
return false
|
2018-02-19 08:06:13 +03:00
|
|
|
case keyFrameCode:
|
2018-02-16 08:46:24 +03:00
|
|
|
return true
|
|
|
|
case 6:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-02-28 16:46:59 +03:00
|
|
|
// isSequenceHeader checks the nature of the passed frame and returns true
|
|
|
|
// if it is a sequnce header and false otherwise
|
2018-02-16 08:46:24 +03:00
|
|
|
func isSequenceHeader(frame []byte) bool {
|
|
|
|
byteChannel := make(chan byte, len(frame))
|
|
|
|
for i := range frame {
|
|
|
|
byteChannel <- frame[i]
|
|
|
|
}
|
2018-02-28 16:46:59 +03:00
|
|
|
for len(byteChannel) >= 5 {
|
|
|
|
aByte := <-byteChannel
|
|
|
|
for i := 1; aByte == 0x00 && i != 4; i++ {
|
2018-02-16 08:46:24 +03:00
|
|
|
aByte = <-byteChannel
|
2018-02-28 16:46:59 +03:00
|
|
|
if (aByte == 0x01 && i == 2) || (aByte == 0x01 && i == 3) {
|
2018-02-16 08:46:24 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-02-28 16:46:59 +03:00
|
|
|
// generate takes in raw video data from the input chan and packetises it into
|
|
|
|
// flv tags, which are then passed to the output channel.
|
2018-02-12 10:58:29 +03:00
|
|
|
func (g *flvGenerator) generate() {
|
2018-02-28 16:46:59 +03:00
|
|
|
g.GenHeader()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case videoFrame := <-g.inputChan:
|
2018-02-16 08:46:24 +03:00
|
|
|
var frameType byte
|
|
|
|
if isKeyFrame(videoFrame) {
|
|
|
|
frameType = flv.KeyFrameType
|
|
|
|
} else {
|
|
|
|
frameType = flv.InterFrameType
|
|
|
|
}
|
|
|
|
var packetType byte
|
2018-02-28 16:46:59 +03:00
|
|
|
if isSequenceHeader(videoFrame) {
|
2018-02-16 08:46:24 +03:00
|
|
|
packetType = flv.SequenceHeader
|
|
|
|
} else {
|
|
|
|
packetType = flv.AVCNALU
|
|
|
|
}
|
2018-02-15 10:02:04 +03:00
|
|
|
timeStamp := g.getNextTimestamp()
|
2018-02-28 16:46:59 +03:00
|
|
|
// Do we have video to send off ?
|
2018-02-19 08:06:13 +03:00
|
|
|
if g.videoFlag {
|
2018-02-28 16:46:59 +03:00
|
|
|
g.outputChan <- flv.VideoTag{
|
|
|
|
TagType: uint8(flv.VideoTagType),
|
|
|
|
DataSize: uint32(len(videoFrame)) + flv.DataHeaderLength,
|
|
|
|
Timestamp: timeStamp,
|
2018-02-19 08:06:13 +03:00
|
|
|
TimestampExtended: flv.NoTimestampExtension,
|
2018-02-28 16:46:59 +03:00
|
|
|
FrameType: frameType,
|
|
|
|
Codec: flv.H264,
|
|
|
|
PacketType: packetType,
|
|
|
|
CompositionTime: 0,
|
|
|
|
Data: videoFrame,
|
|
|
|
PrevTagSize: uint32(videoHeaderSize + len(videoFrame)),
|
|
|
|
}.ToByteSlice()
|
2018-02-15 10:02:04 +03:00
|
|
|
}
|
2018-02-28 16:46:59 +03:00
|
|
|
// Do we even have some audio to send off ?
|
2018-02-19 08:06:13 +03:00
|
|
|
if g.audioFlag {
|
2018-02-28 16:46:59 +03:00
|
|
|
// Not sure why but we need two audio tags for dummy silent audio
|
|
|
|
g.outputChan <- flv.AudioTag{
|
|
|
|
TagType: uint8(flv.AudioTagType),
|
|
|
|
DataSize: 7,
|
|
|
|
Timestamp: timeStamp,
|
2018-02-19 08:06:13 +03:00
|
|
|
TimestampExtended: flv.NoTimestampExtension,
|
2018-02-28 16:46:59 +03:00
|
|
|
SoundFormat: flv.AACAudioFormat,
|
|
|
|
SoundRate: 3,
|
|
|
|
SoundSize: true,
|
|
|
|
SoundType: true,
|
|
|
|
Data: dummyAudioTag1Data,
|
|
|
|
PrevTagSize: uint32(audioSize),
|
|
|
|
}.ToByteSlice()
|
|
|
|
|
|
|
|
g.outputChan <- flv.AudioTag{
|
|
|
|
TagType: uint8(flv.AudioTagType),
|
|
|
|
DataSize: 21,
|
|
|
|
Timestamp: timeStamp,
|
2018-02-19 08:06:13 +03:00
|
|
|
TimestampExtended: flv.NoTimestampExtension,
|
2018-02-28 16:46:59 +03:00
|
|
|
SoundFormat: flv.AACAudioFormat,
|
|
|
|
SoundRate: 3,
|
|
|
|
SoundSize: true,
|
|
|
|
SoundType: true,
|
|
|
|
Data: dummyAudioTag2Data,
|
|
|
|
PrevTagSize: uint32(22),
|
|
|
|
}.ToByteSlice()
|
2018-02-19 08:06:13 +03:00
|
|
|
}
|
2018-02-28 16:46:59 +03:00
|
|
|
}
|
|
|
|
}
|
2018-02-10 09:59:56 +03:00
|
|
|
}
|