revid: matching up audio packet sizes, chunk sizes and rates throughout revid pipeline

This commit is contained in:
Trek H 2019-04-26 17:03:30 +09:30
parent b1e5b4341f
commit 09db8907a5
6 changed files with 36 additions and 17 deletions

View File

@ -30,14 +30,17 @@ package main
import ( import (
"flag" "flag"
"math"
"os" "os"
"runtime/pprof" "runtime/pprof"
"strconv" "strconv"
"strings" "strings"
"time" "time"
"bitbucket.org/ausocean/av/codec/adpcm"
"bitbucket.org/ausocean/av/container/mts" "bitbucket.org/ausocean/av/container/mts"
"bitbucket.org/ausocean/av/container/mts/meta" "bitbucket.org/ausocean/av/container/mts/meta"
"bitbucket.org/ausocean/av/container/mts/pes"
"bitbucket.org/ausocean/av/revid" "bitbucket.org/ausocean/av/revid"
"bitbucket.org/ausocean/iot/pi/netsender" "bitbucket.org/ausocean/iot/pi/netsender"
"bitbucket.org/ausocean/iot/pi/sds" "bitbucket.org/ausocean/iot/pi/sds"
@ -61,8 +64,6 @@ const (
pkg = "revid-cli:" pkg = "revid-cli:"
defaultLogVerbosity = logger.Info defaultLogVerbosity = logger.Info
defaultSleepTime = 60 // Seconds defaultSleepTime = 60 // Seconds
sampleSize = 2 // Bytes
chunkSize = 16000 // Bytes
) )
// canProfile is set to false with revid-cli is built with "-tags profile". // canProfile is set to false with revid-cli is built with "-tags profile".
@ -193,13 +194,6 @@ func handleFlags() revid.Config {
log.Log(logger.Error, pkg+"bad input argument") log.Log(logger.Error, pkg+"bad input argument")
} }
switch *inputPtr {
case "Audio":
cfg.WriteRate = float64(*sampleRatePtr*sampleSize) / float64(chunkSize)
default:
cfg.WriteRate = float64(*frameRatePtr)
}
switch *inputCodecPtr { switch *inputCodecPtr {
case "H264": case "H264":
cfg.InputCodec = revid.H264 cfg.InputCodec = revid.H264
@ -212,6 +206,26 @@ func handleFlags() revid.Config {
log.Log(logger.Error, pkg+"bad input codec argument") log.Log(logger.Error, pkg+"bad input codec argument")
} }
switch *inputPtr {
case "Audio":
byteDepth := *bitDepthPtr / 8
PCMRate := *sampleRatePtr * byteDepth * *channelsPtr * *recPeriodPtr
var byteRate int
switch cfg.InputCodec {
case revid.PCM:
byteRate = PCMRate
case revid.ADPCM:
byteRate = adpcm.BytesOutput(PCMRate)
}
if byteRate < pes.MaxPesSize {
cfg.WriteRate = 1
} else {
cfg.WriteRate = uint(math.Ceil(float64(byteRate) / float64(pes.MaxPesSize)))
}
default:
cfg.WriteRate = *frameRatePtr
}
if len(outputs) == 0 { if len(outputs) == 0 {
cfg.Outputs = make([]uint8, 1) cfg.Outputs = make([]uint8, 1)
} }

View File

@ -57,6 +57,13 @@ type decoder struct {
step int16 step int16
} }
// BytesOutput will return the number of adpcm bytes that will be generated for the given pcm data
func BytesOutput(pcm int) int {
// for X pcm bytes, 2 bytes are left uncompressed, the rest is compressed by a factor of 4
// and a start index and padding byte are added.
return (pcm-2)/4 + 2 + 1 + 1
}
// PcmBS is the size of the blocks that an encoder uses. // PcmBS is the size of the blocks that an encoder uses.
// 'encodeBlock' will encode PcmBS bytes at a time and the output will be AdpcmBS bytes long. // 'encodeBlock' will encode PcmBS bytes at a time and the output will be AdpcmBS bytes long.
const PcmBS = 1010 const PcmBS = 1010

View File

@ -262,14 +262,14 @@ func PCM(dst io.Writer, src io.Reader, delay time.Duration) error {
tick = ticker.C tick = ticker.C
} }
r := bufio.NewReader(src)
for { for {
buf := make([]byte, 0, audioChunkSize) buf := make([]byte, 0, audioChunkSize)
_, err := r.Read(buf) _, err := src.Read(buf)
if err != nil { if err != nil {
return err return err
} }
<-tick <-tick
fmt.Printf("LEXED AUDIO: %v\n", buf[:64])
_, err = dst.Write(buf) _, err = dst.Write(buf)
if err != nil { if err != nil {
return err return err

View File

@ -249,9 +249,7 @@ func (e *Encoder) Write(data []byte) (int, error) {
pkt.PCR = e.pcr() pkt.PCR = e.pcr()
pusi = false pusi = false
} }
bytes := pkt.Bytes(e.tsSpace[:PacketSize]) _, err := e.dst.Write(pkt.Bytes(e.tsSpace[:PacketSize]))
fmt.Printf("Packet: %v", bytes)
_, err := e.dst.Write(bytes)
if err != nil { if err != nil {
return len(data), err return len(data), err
} }

View File

@ -26,7 +26,7 @@ LICENSE
package pes package pes
const MaxPesSize = 64 * 1 << 10 const MaxPesSize = 64 * 1 << 10 // 65536
/* /*
The below data struct encapsulates the fields of an PES packet. Below is The below data struct encapsulates the fields of an PES packet. Below is

View File

@ -55,7 +55,7 @@ type Config struct {
IntraRefreshPeriod uint IntraRefreshPeriod uint
RtpAddress string RtpAddress string
SendRetry bool SendRetry bool
WriteRate float64 // How many times a second revid encoders will be written to. WriteRate uint // How many times a second revid encoders will be written to.
// Video // Video
Height uint Height uint