revid: improved logging key value pairs in revid.go

This commit is contained in:
saxon 2018-09-14 12:17:29 +09:30
parent 852e714d13
commit b3a8f2bca3
3 changed files with 54 additions and 42 deletions

View File

@ -43,7 +43,7 @@ type Config struct {
RtmpMethod uint8 RtmpMethod uint8
Packetization uint8 Packetization uint8
QuantizationMode uint8 QuantizationMode uint8
Verbosity uint8 Verbosity int8
HorizontalFlip uint8 HorizontalFlip uint8
VerticalFlip uint8 VerticalFlip uint8
FramesPerClip int FramesPerClip int
@ -280,7 +280,7 @@ func (c *Config) Validate(r *Revid) error {
} }
if c.Quantization == "" { if c.Quantization == "" {
c.Logger.Log(smartlogger.Warning, "No quantization defined, defaulting to 35!") c.Logger.Log(smartlogger.Warning, "No quantization defined")
c.Quantization = defaultQuantization c.Quantization = defaultQuantization
} else { } else {
if integer, err := strconv.Atoi(c.Quantization); integer < 0 || integer > 51 || err != nil { if integer, err := strconv.Atoi(c.Quantization); integer < 0 || integer > 51 || err != nil {

View File

@ -31,7 +31,7 @@ package revid
import ( import (
"errors" "errors"
"fmt" _ "fmt"
"io" "io"
"os" "os"
"os/exec" "os/exec"
@ -145,14 +145,14 @@ func (r *Revid) reset(config Config) error {
r.config.Logger = config.Logger r.config.Logger = config.Logger
err := config.Validate(r) err := config.Validate(r)
if err != nil { if err != nil {
return errors.New("Config struct is bad!: " + err.Error()) return errors.New("Config struct is bad: " + err.Error())
} }
r.config = config r.config = config
if r.destination != nil { if r.destination != nil {
err = r.destination.close() err = r.destination.close()
if err != nil { if err != nil {
r.config.Logger.Log(smartlogger.Error, err.Error()) r.config.Logger.Log(smartlogger.Error, "Could not close destination", "error", err.Error())
} }
} }
switch r.config.Output { switch r.config.Output {
@ -186,10 +186,10 @@ func (r *Revid) reset(config Config) error {
} }
switch r.config.InputCodec { switch r.config.InputCodec {
case H264: case H264:
r.config.Logger.Log(smartlogger.Info, "using H264 lexer") r.config.Logger.Log(smartlogger.Info, "Using H264 lexer")
r.lexTo = lex.H264 r.lexTo = lex.H264
case Mjpeg: case Mjpeg:
r.config.Logger.Log(smartlogger.Info, "using MJPEG lexer") r.config.Logger.Log(smartlogger.Info, "Using MJPEG lexer")
r.lexTo = lex.MJPEG r.lexTo = lex.MJPEG
} }
@ -297,16 +297,17 @@ func (r *Revid) packClips() {
case frame := <-r.encoder.Stream(): case frame := <-r.encoder.Stream():
lenOfFrame := len(frame) lenOfFrame := len(frame)
if lenOfFrame > ringBufferElementSize { if lenOfFrame > ringBufferElementSize {
r.config.Logger.Log(smartlogger.Warning, fmt.Sprintf("Frame was too big: %v bytes, getting another one!", lenOfFrame)) r.config.Logger.Log(smartlogger.Warning, "Frame was too big", "frame size", lenOfFrame)
frame = r.getFrame() frame = r.getFrame()
lenOfFrame = len(frame) lenOfFrame = len(frame)
} }
_, err := r.ringBuffer.Write(frame) _, err := r.ringBuffer.Write(frame)
if err != nil { if err != nil {
if err == ring.ErrDropped { if err == ring.ErrDropped {
r.config.Logger.Log(smartlogger.Warning, fmt.Sprintf("dropped %d byte frame", len(frame))) r.config.Logger.Log(smartlogger.Warning, "dropped frame", "frame size", len(frame))
} else { } else {
r.config.Logger.Log(smartlogger.Error, err.Error()) r.config.Logger.Log(smartlogger.Error, "Unexpected ringbuffer write error",
"error", err.Error())
} }
} }
packetCount++ packetCount++
@ -352,31 +353,31 @@ func (r *Revid) outputClips() {
r.config.Logger.Log(smartlogger.Debug, "About to send") r.config.Logger.Log(smartlogger.Debug, "About to send")
err = r.destination.load(chunk) err = r.destination.load(chunk)
if err != nil { if err != nil {
r.config.Logger.Log(smartlogger.Error, "failed to load clip") r.config.Logger.Log(smartlogger.Error, "Failed to load clip")
} }
err = r.destination.send() err = r.destination.send()
if err == nil { if err == nil {
r.config.Logger.Log(smartlogger.Debug, "sent clip") r.config.Logger.Log(smartlogger.Debug, "Sent clip")
} }
if r.isRunning && err != nil && chunk.Len() > 11 { if r.isRunning && err != nil && chunk.Len() > 11 {
r.config.Logger.Log(smartlogger.Debug, "Send failed! Trying again") r.config.Logger.Log(smartlogger.Debug, "Send failed! Trying again")
// Try and send again // Try and send again
err = r.destination.send() err = r.destination.send()
r.config.Logger.Log(smartlogger.Error, err.Error()) r.config.Logger.Log(smartlogger.Error, "Destination send error", "error", err.Error())
// if there's still an error we try and reconnect, unless we're stopping // if there's still an error we try and reconnect, unless we're stopping
for r.isRunning && err != nil { for r.isRunning && err != nil {
r.config.Logger.Log(smartlogger.Debug, "Send failed a again! Trying to reconnect...") r.config.Logger.Log(smartlogger.Debug, "Send failed a again! Trying to reconnect...")
time.Sleep(time.Duration(sendFailedDelay) * time.Millisecond) time.Sleep(time.Duration(sendFailedDelay) * time.Millisecond)
r.config.Logger.Log(smartlogger.Error, err.Error()) r.config.Logger.Log(smartlogger.Error, "Send failed with error", "error", err.Error())
if rs, ok := r.destination.(restarter); ok { if rs, ok := r.destination.(restarter); ok {
r.config.Logger.Log(smartlogger.Debug, fmt.Sprintf("restarting %T session", rs)) r.config.Logger.Log(smartlogger.Debug, "restarting session", "session", rs)
err = rs.restart() err = rs.restart()
if err != nil { if err != nil {
// TODO(kortschak): Make this "Fatal" when that exists. // TODO(kortschak): Make this "Fatal" when that exists.
r.config.Logger.Log(smartlogger.Error, "failed to restart rtmp session") r.config.Logger.Log(smartlogger.Error, "failed to restart rtmp session", "error", err.Error())
r.isRunning = false r.isRunning = false
return return
} }
@ -386,7 +387,7 @@ func (r *Revid) outputClips() {
r.config.Logger.Log(smartlogger.Debug, "Trying to send again with new connection...") r.config.Logger.Log(smartlogger.Debug, "Trying to send again with new connection...")
err = r.destination.send() err = r.destination.send()
if err != nil { if err != nil {
r.config.Logger.Log(smartlogger.Error, err.Error()) r.config.Logger.Log(smartlogger.Error, "Send failed with error", "error", err.Error())
} }
} }
} }
@ -401,16 +402,16 @@ func (r *Revid) outputClips() {
if deltaTime > bitrateTime { if deltaTime > bitrateTime {
// FIXME(kortschak): For subsecond deltaTime, this will give infinite bitrate. // FIXME(kortschak): For subsecond deltaTime, this will give infinite bitrate.
r.bitrate = int(float64(bytes*8) / float64(deltaTime/time.Second)) r.bitrate = int(float64(bytes*8) / float64(deltaTime/time.Second))
r.config.Logger.Log(smartlogger.Debug, fmt.Sprintf("Bitrate: %v bits/s\n", r.bitrate)) r.config.Logger.Log(smartlogger.Debug, "Bitrate (bits/s)", "bitrate", r.bitrate)
r.config.Logger.Log(smartlogger.Debug, fmt.Sprintf("Ring buffer size: %v\n", r.ringBuffer.Len())) r.config.Logger.Log(smartlogger.Debug, "Ring buffer size", "value", r.ringBuffer.Len())
prevTime = now prevTime = now
bytes = 0 bytes = 0
} }
} }
r.config.Logger.Log(smartlogger.Info, "Not outputting clips anymore!") r.config.Logger.Log(smartlogger.Info, "Not outputting clips anymore")
err := r.destination.close() err := r.destination.close()
if err != nil { if err != nil {
r.config.Logger.Log(smartlogger.Error, "failed to close destination") r.config.Logger.Log(smartlogger.Error, "Failed to close destination", "error", err.Error())
} }
} }
@ -420,32 +421,42 @@ func (r *Revid) startRaspivid() error {
r.config.Logger.Log(smartlogger.Info, "Starting raspivid!") r.config.Logger.Log(smartlogger.Info, "Starting raspivid!")
switch r.config.InputCodec { switch r.config.InputCodec {
case H264: case H264:
args := []string{ // FIXME(saxon): consider using a map right from the start to store
"-cd", "H264", // the raspivid args
"-o", "-", raspiArgs := make(map[string]string)
"-n", raspiArgs["-cd"] = "H264"
"-t", runContinuously, raspiArgs["-o"] = "-"
"-b", r.config.Bitrate, raspiArgs["-n"] = ""
"-w", r.config.Width, raspiArgs["-t"] = runContinuously
"-h", r.config.Height, raspiArgs["-b"] = r.config.Bitrate
"-fps", r.config.FrameRate, raspiArgs["-w"] = r.config.Width
"-ih", raspiArgs["-h"] = r.config.Height
"-g", r.config.IntraRefreshPeriod, raspiArgs["-fps"] = r.config.FrameRate
} raspiArgs["-ih"] = ""
raspiArgs["-g"] = r.config.IntraRefreshPeriod
if r.config.QuantizationMode == QuantizationOn { if r.config.QuantizationMode == QuantizationOn {
args = append(args, "-qp", r.config.Quantization) raspiArgs["-qp"] = r.config.Quantization
} }
if r.config.HorizontalFlip == Yes { if r.config.HorizontalFlip == Yes {
args = append(args, "-hf") raspiArgs["-hf"] = strconv.Itoa(int(r.config.HorizontalFlip))
} }
if r.config.VerticalFlip == Yes { if r.config.VerticalFlip == Yes {
args = append(args, "-vf") raspiArgs["-vf"] = strconv.Itoa(int(r.config.VerticalFlip))
}
// Log all the args and create []string
args := make([]string, 0)
for i := range raspiArgs {
r.config.Logger.Log(smartlogger.Info, "Raspivid arg", i, raspiArgs[i])
// First append the flag
args = append(args, i)
// and now append any values for the flag
args = append(args, raspiArgs[i])
} }
r.config.Logger.Log(smartlogger.Info, fmt.Sprintf("Starting raspivid with args: %v", args))
r.cmd = exec.Command("raspivid", args...) r.cmd = exec.Command("raspivid", args...)
case Mjpeg: case Mjpeg:
// FIXME(saxon): do above in this case too
r.cmd = exec.Command("raspivid", r.cmd = exec.Command("raspivid",
"-cd", "MJPEG", "-cd", "MJPEG",
"-o", "-", "-o", "-",

View File

@ -28,8 +28,8 @@ LICENSE
package revid package revid
import ( import (
"testing" _ "testing"
"time" _ "time"
) )
/* /*
@ -143,7 +143,7 @@ func TestRtmpOutputUsingLibRtmp(t *testing.T){
revidInst.Stop() revidInst.Stop()
} }
*/ */
/*
// Test revidInst with a Raspivid h264 input // Test revidInst with a Raspivid h264 input
func TestRaspividToRtmp(t *testing.T) { func TestRaspividToRtmp(t *testing.T) {
config := Config{ config := Config{
@ -157,7 +157,7 @@ func TestRaspividToRtmp(t *testing.T) {
Packetization: Flv, Packetization: Flv,
FrameRate: "25", FrameRate: "25",
} }
revidInst, err := New(config) revidInst, err := New(config, nil)
if err != nil { if err != nil {
t.Errorf("Should not have got an error!") t.Errorf("Should not have got an error!")
return return
@ -166,3 +166,4 @@ func TestRaspividToRtmp(t *testing.T) {
time.Sleep(43200 * time.Second) time.Sleep(43200 * time.Second)
revidInst.Stop() revidInst.Stop()
} }
*/