diff --git a/cmd/revid-cli/main.go b/cmd/revid-cli/main.go index b4abf0ef..68e7c07f 100644 --- a/cmd/revid-cli/main.go +++ b/cmd/revid-cli/main.go @@ -30,14 +30,17 @@ package main import ( "flag" + "math" "os" "runtime/pprof" "strconv" "strings" "time" + "bitbucket.org/ausocean/av/codec/adpcm" "bitbucket.org/ausocean/av/container/mts" "bitbucket.org/ausocean/av/container/mts/meta" + "bitbucket.org/ausocean/av/container/mts/pes" "bitbucket.org/ausocean/av/revid" "bitbucket.org/ausocean/iot/pi/netsender" "bitbucket.org/ausocean/iot/pi/sds" @@ -60,9 +63,7 @@ const ( defaultLogPath = "/var/log/netsender" pkg = "revid-cli:" defaultLogVerbosity = logger.Info - defaultSleepTime = 60 // Seconds - sampleSize = 2 // Bytes - chunkSize = 16000 // Bytes + defaultSleepTime = 60 // Seconds ) // 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") } - switch *inputPtr { - case "Audio": - cfg.WriteRate = float64(*sampleRatePtr*sampleSize) / float64(chunkSize) - default: - cfg.WriteRate = float64(*frameRatePtr) - } - switch *inputCodecPtr { case "H264": cfg.InputCodec = revid.H264 @@ -212,6 +206,26 @@ func handleFlags() revid.Config { 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 { cfg.Outputs = make([]uint8, 1) } diff --git a/codec/adpcm/adpcm.go b/codec/adpcm/adpcm.go index 595728a2..f90af10b 100644 --- a/codec/adpcm/adpcm.go +++ b/codec/adpcm/adpcm.go @@ -57,6 +57,13 @@ type decoder struct { 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. // 'encodeBlock' will encode PcmBS bytes at a time and the output will be AdpcmBS bytes long. const PcmBS = 1010 diff --git a/codec/lex/lex.go b/codec/lex/lex.go index ff1d4e4d..9f5dc55a 100644 --- a/codec/lex/lex.go +++ b/codec/lex/lex.go @@ -262,14 +262,14 @@ func PCM(dst io.Writer, src io.Reader, delay time.Duration) error { tick = ticker.C } - r := bufio.NewReader(src) for { buf := make([]byte, 0, audioChunkSize) - _, err := r.Read(buf) + _, err := src.Read(buf) if err != nil { return err } <-tick + fmt.Printf("LEXED AUDIO: %v\n", buf[:64]) _, err = dst.Write(buf) if err != nil { return err diff --git a/container/mts/encoder.go b/container/mts/encoder.go index b27a100d..a4e4931b 100644 --- a/container/mts/encoder.go +++ b/container/mts/encoder.go @@ -249,9 +249,7 @@ func (e *Encoder) Write(data []byte) (int, error) { pkt.PCR = e.pcr() pusi = false } - bytes := pkt.Bytes(e.tsSpace[:PacketSize]) - fmt.Printf("Packet: %v", bytes) - _, err := e.dst.Write(bytes) + _, err := e.dst.Write(pkt.Bytes(e.tsSpace[:PacketSize])) if err != nil { return len(data), err } diff --git a/container/mts/pes/pes.go b/container/mts/pes/pes.go index b0e40f86..58a143cc 100644 --- a/container/mts/pes/pes.go +++ b/container/mts/pes/pes.go @@ -26,7 +26,7 @@ LICENSE 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 diff --git a/revid/config.go b/revid/config.go index ab686e43..83d8339b 100644 --- a/revid/config.go +++ b/revid/config.go @@ -55,7 +55,7 @@ type Config struct { IntraRefreshPeriod uint RtpAddress string 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 Height uint