2018-02-13 11:39:26 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
RtpToTsConverter.go - provides utilities for the conversion of Rtp packets
|
|
|
|
to equivalent MpegTs packets.
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
See Readme.md
|
|
|
|
|
|
|
|
AUTHOR
|
|
|
|
Saxon Nelson-Milton <saxon.milton@gmail.com>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
RtpToTsConverter.go is Copyright (C) 2017 the Australian Ocean Lab (AusOcean)
|
|
|
|
|
|
|
|
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-15 10:02:04 +03:00
|
|
|
_"fmt"
|
|
|
|
"time"
|
2018-02-12 10:58:29 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
inputChanLength = 1000
|
|
|
|
outputChanLength = 1000
|
|
|
|
)
|
|
|
|
|
2018-02-10 16:25:55 +03:00
|
|
|
type flvGenerator struct {
|
|
|
|
fps uint
|
|
|
|
inputChan chan []byte
|
|
|
|
outputChan chan []byte
|
2018-02-12 10:58:29 +03:00
|
|
|
audioFlag bool
|
|
|
|
videoFlag bool
|
|
|
|
lastTagSize int
|
|
|
|
currentTimestamp uint32
|
|
|
|
header flv.Header
|
2018-02-10 16:25:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *flvGenerator)GetInputChan() chan []byte {
|
|
|
|
return g.inputChan
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *flvGenerator)GetOutputChan() chan []byte {
|
|
|
|
return g.outputChan
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *flvGenerator) Start(){
|
|
|
|
go g.generate()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *flvGenerator) GenHeader(){
|
2018-02-11 09:06:59 +03:00
|
|
|
header := flv.Header{
|
2018-02-12 10:58:29 +03:00
|
|
|
AudioFlag: g.audioFlag,
|
|
|
|
VideoFlag: g.videoFlag,
|
2018-02-10 16:25:55 +03:00
|
|
|
}
|
2018-02-12 10:58:29 +03:00
|
|
|
g.outputChan <- header.ToByteSlice()
|
2018-02-10 16:25:55 +03:00
|
|
|
}
|
|
|
|
|
2018-02-11 09:06:59 +03:00
|
|
|
func (g *flvGenerator) getNextTimestamp() (timestamp uint32){
|
2018-02-10 16:25:55 +03:00
|
|
|
timestamp = g.currentTimestamp
|
2018-02-12 10:58:29 +03:00
|
|
|
g.currentTimestamp += uint32(1000) / uint32(g.fps)
|
2018-02-10 16:25:55 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *flvGenerator) ResetTimestamp() {
|
2018-02-12 10:58:29 +03:00
|
|
|
g.currentTimestamp = 0
|
2018-02-10 16:25:55 +03:00
|
|
|
}
|
|
|
|
|
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]
|
|
|
|
}
|
|
|
|
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 true
|
|
|
|
case 6:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-02-12 10:58:29 +03:00
|
|
|
func (g *flvGenerator) generate() {
|
2018-02-11 09:06:59 +03:00
|
|
|
g.GenHeader()
|
2018-02-10 16:25:55 +03:00
|
|
|
for {
|
|
|
|
select {
|
2018-02-12 10:58:29 +03:00
|
|
|
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
|
|
|
|
if isSequenceHeader(videoFrame){
|
|
|
|
packetType = flv.SequenceHeader
|
|
|
|
} else {
|
|
|
|
packetType = flv.AVCNALU
|
|
|
|
|
|
|
|
}
|
2018-02-15 10:02:04 +03:00
|
|
|
timeStamp := g.getNextTimestamp()
|
|
|
|
videoTag := flv.VideoTag{
|
2018-02-12 10:58:29 +03:00
|
|
|
PrevTagSize: uint32(g.lastTagSize),
|
|
|
|
TagType: uint8(flv.VideoTagType),
|
|
|
|
DataSize: uint32(len(videoFrame)) + flv.DataHeaderLength,
|
2018-02-15 10:02:04 +03:00
|
|
|
Timestamp: timeStamp,
|
2018-02-12 16:31:19 +03:00
|
|
|
TimestampExtended: flv.NoTimestampExtension,
|
2018-02-16 08:46:24 +03:00
|
|
|
FrameType: frameType,
|
2018-02-12 10:58:29 +03:00
|
|
|
Codec: flv.H264,
|
2018-02-16 08:46:24 +03:00
|
|
|
PacketType: packetType,
|
2018-02-12 10:58:29 +03:00
|
|
|
CompositionTime: 0,
|
|
|
|
Data: videoFrame,
|
2018-02-11 09:06:59 +03:00
|
|
|
}
|
2018-02-15 10:02:04 +03:00
|
|
|
videoTagAsByteSlice := videoTag.ToByteSlice()
|
|
|
|
g.lastTagSize = len(videoTagAsByteSlice)
|
|
|
|
g.outputChan<-videoTagAsByteSlice
|
|
|
|
|
|
|
|
soundData := make([]byte, 10)
|
|
|
|
for i := range soundData {
|
|
|
|
if i == 0 {
|
|
|
|
soundData[i] = 1
|
|
|
|
} else {
|
|
|
|
soundData[i] = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
audioTag := flv.AudioTag{
|
|
|
|
PrevTagSize: uint32(g.lastTagSize),
|
|
|
|
TagType: uint8(flv.AudioTagType),
|
2018-02-16 08:46:24 +03:00
|
|
|
DataSize: 7,
|
2018-02-15 10:02:04 +03:00
|
|
|
Timestamp: timeStamp,
|
|
|
|
TimestampExtended: flv.NoTimestampExtension,
|
|
|
|
SoundFormat: flv.AACAudioFormat,
|
2018-02-16 08:46:24 +03:00
|
|
|
SoundRate: 3,
|
|
|
|
SoundSize: true,
|
|
|
|
SoundType: true,
|
|
|
|
Data: []byte{0x00,0x12,0x08,0x56,0xe5,0x00},
|
2018-02-15 10:02:04 +03:00
|
|
|
}
|
|
|
|
audioTagAsByteSlice := audioTag.ToByteSlice()
|
|
|
|
g.lastTagSize = len(audioTagAsByteSlice)
|
|
|
|
g.outputChan<-audioTagAsByteSlice
|
|
|
|
|
2018-02-16 08:46:24 +03:00
|
|
|
time.Sleep(60*time.Millisecond)
|
2018-02-10 16:25:55 +03:00
|
|
|
}
|
|
|
|
}
|
2018-02-10 09:59:56 +03:00
|
|
|
}
|