av/exp/rvcl/main.go

199 lines
6.0 KiB
Go
Raw Permalink Normal View History

/*
DESCRIPTION
rvcl is command line interface for revid. The user can provide configuration
by passing a JSON string directly, or by specifying a file containing the JSON.
AUTHORS
Saxon A. Nelson-Milton <saxon@ausocean.org>
Dan Kortschak <dan@ausocean.org>
Scott Barnard <scott@ausocean.org>
LICENSE
Copyright (C) 2020 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 http://www.gnu.org/licenses.
USAGE
User may providied path to a config JSON file using the 'config-file' flag.
For example, to stream a H.264 file to youtube we will need to create a file
containing configuration JSON:
{
"Input":"file",
"InputPath":"<path to h264 file>",
"Output":"rtmp",
"RtmpUrl":"<rtmp url>",
"FileFPS":"25"
}
Then, using rvcl we can stream:
./rvcl -config-file="config.json"
We can also use the 'config' flag to directly pass JSON as a string.
NB: Quotations must have backslashes, '\', proceeding them for rvcl to consider
the string valid JSON. For example, to play a file to youtube using the JSON
config described above, we would do the following:
./rvcl -config="{\"Input\":\"file\",\"InputPath\":\"<file path>\",
\"Output\":\"rtmp\",\"RtmpUrl\":\"<rtmp url>\",\"FileFPS\":\"25\"}"
rvcl passes given variables to revid i.e. all variables defined by revid are
valid. See revid config package i.e. bitbucket.org/ausocean/av/revid/config for
revid specific variables, and see AVDevice implementations i.e.
bitbucket.org/ausocean/av/device for input device specific variables.
*/
// rvcl is a command line interface for revid.
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"runtime/pprof"
"gopkg.in/natefinch/lumberjack.v2"
"bitbucket.org/ausocean/av/container/mts"
"bitbucket.org/ausocean/av/container/mts/meta"
"bitbucket.org/ausocean/av/revid"
"bitbucket.org/ausocean/av/revid/config"
"bitbucket.org/ausocean/iot/pi/netsender"
"bitbucket.org/ausocean/utils/logger"
)
// Copyright information prefixed to all metadata.
const (
metaPreambleKey = "copyright"
metaPreambleData = "ausocean.org/license/content2019"
)
// Logging configuration.
const (
logMaxSize = 500 // MB
logMaxBackups = 10
logMaxAge = 28 // days
logLevel = logger.Debug
logPath = "/var/log/netsender/netsender.log"
logSuppress = false
)
// Misc consts.
const (
pkg = "rvcl: "
profilePath = "rvcl.prof"
)
// canProfile is set to false with revid-cli is built with "-tags profile".
var canProfile = false
// The logger that will be used throughout.
var log *logger.Logger
func main() {
mts.Meta = meta.NewWith([][2]string{{metaPreambleKey, metaPreambleData}})
// If built with profile tag, we will start CPU profiling.
if canProfile {
profile()
defer pprof.StopCPUProfile()
}
// User can provide config through single JSON string flag, or through JSON file.
var (
configPtr = flag.String("config", "", "Provide configuration JSON to revid (see readme for further information).")
configFilePtr = flag.String("config-file", "", "Location of revid configuration file (see readme for further information).")
)
flag.Parse()
// Create config map according to flags or file, and panic if both are defined.
var (
cfg map[string]string
err error
)
switch {
case *configPtr != "" && *configFilePtr != "": // This doesn't make sense so panic.
panic("cannot define both command-line config and file config")
case *configPtr != "": // Decode JSON file to map.
err = json.Unmarshal([]byte(*configPtr), &cfg)
if err != nil {
panic(fmt.Sprintf("could not decode JSON config: %w", err))
}
case *configFilePtr != "": // Decode JSON string to map from command line flag.
f, err := os.Open(*configFilePtr)
if err != nil {
panic(fmt.Sprintf("could not open config file: %w", err))
}
err = json.NewDecoder(f).Decode(&cfg)
if err != nil {
panic(fmt.Sprintf("could not decode JSON config: %w", err))
}
default: // No config information has been provided; give empty map to force defaults.
cfg = map[string]string{}
}
// Create logger that methods will be called on by the netsender client and
// revid to log messages. Logs will go the lumberjack logger to handle file
// writing of messages.
log = logger.New(
logLevel,
&lumberjack.Logger{
Filename: logPath,
MaxSize: logMaxSize, // MB
MaxBackups: logMaxBackups,
MaxAge: logMaxAge, // days
},
logSuppress,
)
// Create a netsender client. This is used only for HTTP sending of media
// in this binary.
ns, err := netsender.New(log, nil, nil, nil, nil)
if err != nil {
log.Log(logger.Fatal, pkg+"could not initialise netsender client: "+err.Error())
}
rv, err := revid.New(config.Config{Logger: log}, ns)
if err != nil {
panic(fmt.Sprintf("could not create revid: %w", err))
}
// Configure revid with configuration map obtained through flags or file.
// If config is empty, defaults will be adopted by revid.
err = rv.Update(cfg)
if err != nil {
panic(fmt.Sprintf("could not update revid config: %w", err))
}
err = rv.Start()
if err != nil {
panic(fmt.Sprintf("could not start revid: %w", err))
}
// Run indefinitely.
select {}
}
// profile creates a file to hold CPU profile metrics and begins CPU profiling.
func profile() {
f, err := os.Create(profilePath)
if err != nil {
log.Log(logger.Fatal, pkg+"could not create CPU profile", "error", err.Error())
}
if err := pprof.StartCPUProfile(f); err != nil {
log.Log(logger.Fatal, pkg+"could not start CPU profile", "error", err.Error())
}
}