codecutil, revid: make codec a type and make const strings for metadata keys

This commit is contained in:
Trek H 2021-02-22 14:16:41 +10:30
parent 875cc196f5
commit 9211ae4338
7 changed files with 68 additions and 40 deletions

View File

@ -24,10 +24,13 @@ LICENSE
package codecutil
// Codec represents a media codec.
type Codec uint8
// A global list containing all available codecs for reference in any application.
// When adding or removing a codec from this list, the numCodecs const must be updated.
// When adding or removing a codec from this list, the corresponding codec string const must be updated below.
const (
Undef = iota
Undef Codec = iota
PCM
ADPCM
H264
@ -37,7 +40,35 @@ const (
maxCodec
)
// IsValid recieves an int representing a codec and checks if it is valid.
func IsValid(codec uint8) bool {
return 0 < codec && codec < maxCodec
const (
pcmStr = "pcm"
adpcmStr = "adpcm"
h264Str = "h264"
h265Str = "h265"
mjpegStr = "mjpeg"
jpegStr = "jpeg"
)
// IsValid checks if a codec is a known and valid codec.
func (c Codec) IsValid() bool {
return 0 < c && c < maxCodec
}
func (c Codec) String() string {
switch c {
case PCM:
return pcmStr
case ADPCM:
return adpcmStr
case H264:
return h264Str
case H265:
return h265Str
case MJPEG:
return mjpegStr
case JPEG:
return jpegStr
default:
return ""
}
}

View File

@ -101,6 +101,13 @@ const (
defaultMediaPID = PIDVideo
)
// Used to consistently read and write MTS metadata entries.
const (
WriteRateStr = "writeRate"
TSStr = "ts"
LocStr = "loc"
)
// Meta allows addition of metadata to encoded mts from outside of this pkg.
// See meta pkg for usage.
//
@ -164,7 +171,7 @@ func NewEncoder(dst io.WriteCloser, log logger.LoggerIF, options ...func(*Encode
}
log.Debug("encoder options applied")
Meta.Add("writeRate", fmt.Sprintf("%f", 1/float64(e.writePeriod.Seconds())))
Meta.Add(WriteRateStr, fmt.Sprintf("%f", 1/float64(e.writePeriod.Seconds())))
e.pmt.SyntaxSection.SpecificData.(*psi.PMT).StreamSpecificData.StreamType = e.streamID
e.pmt.SyntaxSection.SpecificData.(*psi.PMT).StreamSpecificData.PID = e.mediaPID
@ -332,7 +339,7 @@ func updateMeta(b []byte, log logger.LoggerIF) ([]byte, error) {
p := psi.PSIBytes(b)
if RealTime.IsSet() {
t := strconv.Itoa(int(RealTime.Get().Unix()))
Meta.Add("ts", t)
Meta.Add(TSStr, t)
log.Debug("latest time added to meta", "time", t)
}
err := p.AddDescriptor(psi.MetadataTag, Meta.Encode())

View File

@ -276,7 +276,7 @@ func TestMetaEncode1(t *testing.T) {
t.Fatalf("could not create encoder, failed with error: %v", err)
}
Meta.Add("ts", "12345678")
Meta.Add(TSStr, "12345678")
if err := e.writePSI(); err != nil {
t.Errorf("unexpected error: %v\n", err.Error())
}
@ -313,8 +313,8 @@ func TestMetaEncode2(t *testing.T) {
t.Fatalf("could not create MTS encoder, failed with error: %v", err)
}
Meta.Add("ts", "12345678")
Meta.Add("loc", "1234,4321,1234")
Meta.Add(TSStr, "12345678")
Meta.Add(LocStr, "1234,4321,1234")
if err := e.writePSI(); err != nil {
t.Errorf("did not expect error: %v from writePSI", err.Error())
}
@ -350,8 +350,8 @@ func TestExtractMeta(t *testing.T) {
t.Fatalf("could not create MTS encoder, failed with error: %v", err)
}
Meta.Add("ts", "12345678")
Meta.Add("loc", "1234,4321,1234")
Meta.Add(TSStr, "12345678")
Meta.Add(LocStr, "1234,4321,1234")
if err := e.writePSI(); err != nil {
t.Errorf("did not expect error: %v", err.Error())
}

View File

@ -36,6 +36,15 @@ import (
"bitbucket.org/ausocean/utils/logger"
)
// Used to reliably read, write, and test audio metadata entry keys.
const (
SampleRateStr = "sampleRate"
ChannelsStr = "channels"
PeriodStr = "period"
BitDepthStr = "bitDepth"
CodecStr = "codec"
)
func (r *Revid) setupAudio() error {
// Create new ALSA device.
d := alsa.New(r.cfg.Logger)
@ -59,19 +68,11 @@ func (r *Revid) setupAudio() error {
r.lexTo = l.Lex
// Add metadata.
mts.Meta.Add("sampleRate", strconv.Itoa(int(r.cfg.SampleRate)))
mts.Meta.Add("channels", strconv.Itoa(int(r.cfg.Channels)))
mts.Meta.Add("period", fmt.Sprintf("%.6f", r.cfg.RecPeriod))
mts.Meta.Add("bitDepth", strconv.Itoa(int(r.cfg.BitDepth)))
switch r.cfg.InputCodec {
case codecutil.PCM:
mts.Meta.Add("codec", "pcm")
case codecutil.ADPCM:
mts.Meta.Add("codec", "adpcm")
default:
r.cfg.Logger.Log(logger.Fatal, "no audio codec set in config")
}
mts.Meta.Add(SampleRateStr, strconv.Itoa(int(r.cfg.SampleRate)))
mts.Meta.Add(ChannelsStr, strconv.Itoa(int(r.cfg.Channels)))
mts.Meta.Add(PeriodStr, fmt.Sprintf("%.6f", r.cfg.RecPeriod))
mts.Meta.Add(BitDepthStr, strconv.Itoa(int(r.cfg.BitDepth)))
mts.Meta.Add(CodecStr, r.cfg.InputCodec.String())
return nil
}

View File

@ -29,6 +29,7 @@ package config
import (
"time"
"bitbucket.org/ausocean/av/codec/codecutil"
"bitbucket.org/ausocean/utils/logger"
)
@ -168,7 +169,7 @@ type Config struct {
// InputCodec defines the input codec we wish to use, and therefore defines the
// lexer for use in the pipeline. This defaults to H264, but H265 is also a
// valid option if we expect this from the input.
InputCodec uint8
InputCodec codecutil.Codec
// InputPath defines the input file location for File Input. This must be
// defined if File input is to be used.

View File

@ -299,19 +299,7 @@ var Variables = []struct {
Name: KeyInputCodec,
Type_: "enum:H264,H265,MJPEG,JPEG,PCM,ADPCM",
Update: func(c *Config, v string) {
c.InputCodec = parseEnum(
KeyInputCodec,
v,
map[string]uint8{
"h264": codecutil.H264,
"h265": codecutil.H265,
"mjpeg": codecutil.MJPEG,
"jpeg": codecutil.JPEG,
"pcm": codecutil.PCM,
"adpcm": codecutil.ADPCM,
},
c,
)
c.InputCodec = codecutil.FromString(v)
},
Validate: func(c *Config) {
switch c.InputCodec {

View File

@ -153,7 +153,7 @@ func extractMeta(r string, log func(lvl int8, msg string, args ...interface{}))
log(logger.Debug, "No location in reply")
} else {
log(logger.Debug, fmt.Sprintf("got location: %v", g))
mts.Meta.Add("loc", g)
mts.Meta.Add(LocStr, g)
}
return nil