Browse Source

rpi: pass log level to libcamera (#2617) (#2811)

pull/2814/head
Alessandro Ros 1 year ago committed by GitHub
parent
commit
0c131a2e92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      internal/core/core.go
  2. 4
      internal/core/path.go
  3. 4
      internal/core/path_manager.go
  4. 2
      internal/core/static_source_handler.go
  5. 14
      internal/protocols/rpicamera/exe/camera.cpp
  6. 4
      internal/protocols/rpicamera/exe/parameters.c
  7. 1
      internal/protocols/rpicamera/exe/parameters.h
  8. 1
      internal/protocols/rpicamera/params.go
  9. 32
      internal/protocols/rpicamera/rpicamera.go
  10. 14
      internal/protocols/rpicamera/rpicamera_disabled.go
  11. 22
      internal/staticsources/rpicamera/source.go

2
internal/core/core.go

@ -310,6 +310,7 @@ func (p *Core) createResources(initial bool) error { @@ -310,6 +310,7 @@ func (p *Core) createResources(initial bool) error {
if p.pathManager == nil {
p.pathManager = newPathManager(
p.conf.LogLevel,
p.conf.ExternalAuthenticationURL,
p.conf.RTSPAddress,
p.conf.AuthMethods,
@ -614,6 +615,7 @@ func (p *Core) closeResources(newConf *conf.Conf, calledByAPI bool) { @@ -614,6 +615,7 @@ func (p *Core) closeResources(newConf *conf.Conf, calledByAPI bool) {
closeLogger
closePathManager := newConf == nil ||
newConf.LogLevel != p.conf.LogLevel ||
newConf.ExternalAuthenticationURL != p.conf.ExternalAuthenticationURL ||
newConf.RTSPAddress != p.conf.RTSPAddress ||
!reflect.DeepEqual(newConf.AuthMethods, p.conf.AuthMethods) ||

4
internal/core/path.go

@ -64,6 +64,7 @@ type pathAPIPathsGetReq struct { @@ -64,6 +64,7 @@ type pathAPIPathsGetReq struct {
}
type path struct {
logLevel conf.LogLevel
rtspAddress string
readTimeout conf.StringDuration
writeTimeout conf.StringDuration
@ -116,6 +117,7 @@ type path struct { @@ -116,6 +117,7 @@ type path struct {
func newPath(
parentCtx context.Context,
logLevel conf.LogLevel,
rtspAddress string,
readTimeout conf.StringDuration,
writeTimeout conf.StringDuration,
@ -132,6 +134,7 @@ func newPath( @@ -132,6 +134,7 @@ func newPath(
ctx, ctxCancel := context.WithCancel(parentCtx)
pa := &path{
logLevel: logLevel,
rtspAddress: rtspAddress,
readTimeout: readTimeout,
writeTimeout: writeTimeout,
@ -206,6 +209,7 @@ func (pa *path) run() { @@ -206,6 +209,7 @@ func (pa *path) run() {
pa.source = &staticSourceHandler{
conf: pa.conf,
logLevel: pa.logLevel,
readTimeout: pa.readTimeout,
writeTimeout: pa.writeTimeout,
writeQueueSize: pa.writeQueueSize,

4
internal/core/path_manager.go

@ -79,6 +79,7 @@ type pathManagerParent interface { @@ -79,6 +79,7 @@ type pathManagerParent interface {
}
type pathManager struct {
logLevel conf.LogLevel
externalAuthenticationURL string
rtspAddress string
authMethods conf.AuthMethods
@ -112,6 +113,7 @@ type pathManager struct { @@ -112,6 +113,7 @@ type pathManager struct {
}
func newPathManager(
logLevel conf.LogLevel,
externalAuthenticationURL string,
rtspAddress string,
authMethods conf.AuthMethods,
@ -126,6 +128,7 @@ func newPathManager( @@ -126,6 +128,7 @@ func newPathManager(
ctx, ctxCancel := context.WithCancel(context.Background())
pm := &pathManager{
logLevel: logLevel,
externalAuthenticationURL: externalAuthenticationURL,
rtspAddress: rtspAddress,
authMethods: authMethods,
@ -400,6 +403,7 @@ func (pm *pathManager) createPath( @@ -400,6 +403,7 @@ func (pm *pathManager) createPath(
) {
pa := newPath(
pm.ctx,
pm.logLevel,
pm.rtspAddress,
pm.readTimeout,
pm.writeTimeout,

2
internal/core/static_source_handler.go

@ -31,6 +31,7 @@ type staticSourceHandlerParent interface { @@ -31,6 +31,7 @@ type staticSourceHandlerParent interface {
// staticSourceHandler is a static source handler.
type staticSourceHandler struct {
conf *conf.Path
logLevel conf.LogLevel
readTimeout conf.StringDuration
writeTimeout conf.StringDuration
writeQueueSize int
@ -108,6 +109,7 @@ func (s *staticSourceHandler) initialize() { @@ -108,6 +109,7 @@ func (s *staticSourceHandler) initialize() {
case s.resolvedSource == "rpiCamera":
s.instance = &rpicamerasource.Source{
LogLevel: s.logLevel,
Parent: s,
}
}

14
internal/protocols/rpicamera/exe/camera.cpp

@ -126,13 +126,23 @@ static void set_hdr(bool hdr) { @@ -126,13 +126,23 @@ static void set_hdr(bool hdr) {
}
bool camera_create(const parameters_t *params, camera_frame_cb frame_cb, camera_t **cam) {
std::unique_ptr<CameraPriv> camp = std::make_unique<CameraPriv>();
set_hdr(params->hdr);
if (strcmp(params->log_level, "debug") == 0) {
setenv("LIBCAMERA_LOG_LEVELS", "*:DEBUG", 1);
} else if (strcmp(params->log_level, "info") == 0) {
setenv("LIBCAMERA_LOG_LEVELS", "*:INFO", 1);
} else if (strcmp(params->log_level, "warn") == 0) {
setenv("LIBCAMERA_LOG_LEVELS", "*:WARN", 1);
} else { // error
setenv("LIBCAMERA_LOG_LEVELS", "*:ERROR", 1);
}
// We make sure to set the environment variable before libcamera init
setenv("LIBCAMERA_RPI_TUNING_FILE", params->tuning_file, 1);
std::unique_ptr<CameraPriv> camp = std::make_unique<CameraPriv>();
camp->camera_manager = std::make_unique<CameraManager>();
int ret = camp->camera_manager->start();
if (ret != 0) {

4
internal/protocols/rpicamera/exe/parameters.c

@ -37,7 +37,9 @@ bool parameters_unserialize(parameters_t *params, const uint8_t *buf, size_t buf @@ -37,7 +37,9 @@ bool parameters_unserialize(parameters_t *params, const uint8_t *buf, size_t buf
char *key = strsep(&entry, ":");
char *val = strsep(&entry, ":");
if (strcmp(key, "CameraID") == 0) {
if (strcmp(key, "LogLevel") == 0) {
params->log_level = base64_decode(val);
} else if (strcmp(key, "CameraID") == 0) {
params->camera_id = atoi(val);
} else if (strcmp(key, "Width") == 0) {
params->width = atoi(val);

1
internal/protocols/rpicamera/exe/parameters.h

@ -8,6 +8,7 @@ @@ -8,6 +8,7 @@
#include "sensor_mode.h"
typedef struct {
char *log_level;
unsigned int camera_id;
unsigned int width;
unsigned int height;

1
internal/protocols/rpicamera/params.go

@ -9,6 +9,7 @@ import ( @@ -9,6 +9,7 @@ import (
// Params is a set of camera parameters.
type Params struct {
LogLevel string
CameraID int
Width int
Height int

32
internal/protocols/rpicamera/rpicamera.go

@ -111,8 +111,10 @@ func checkLibraries64Bit() error { @@ -111,8 +111,10 @@ func checkLibraries64Bit() error {
return nil
}
// RPICamera is a RPI Camera reader.
type RPICamera struct {
onData func(time.Duration, [][]byte)
Params Params
OnData func(time.Duration, [][]byte)
cmd *exec.Cmd
pipeConf *pipe
@ -122,31 +124,25 @@ type RPICamera struct { @@ -122,31 +124,25 @@ type RPICamera struct {
readerDone chan error
}
func New(
params Params,
onData func(time.Duration, [][]byte),
) (*RPICamera, error) {
// Initialize initializes a RPICamera.
func (c *RPICamera) Initialize() error {
if runtime.GOARCH == "arm" {
err := checkLibraries64Bit()
if err != nil {
return nil, err
}
return err
}
c := &RPICamera{
onData: onData,
}
var err error
c.pipeConf, err = newPipe()
if err != nil {
return nil, err
return err
}
c.pipeVideo, err = newPipe()
if err != nil {
c.pipeConf.close()
return nil, err
return err
}
env := []string{
@ -158,10 +154,10 @@ func New( @@ -158,10 +154,10 @@ func New(
if err != nil {
c.pipeConf.close()
c.pipeVideo.close()
return nil, err
return err
}
c.pipeConf.write(append([]byte{'c'}, params.serialize()...))
c.pipeConf.write(append([]byte{'c'}, c.Params.serialize()...))
c.waitDone = make(chan error)
go func() {
@ -178,7 +174,7 @@ func New( @@ -178,7 +174,7 @@ func New(
c.pipeConf.close()
c.pipeVideo.close()
<-c.readerDone
return nil, fmt.Errorf("process exited unexpectedly")
return fmt.Errorf("process exited unexpectedly")
case err := <-c.readerDone:
if err != nil {
@ -186,7 +182,7 @@ func New( @@ -186,7 +182,7 @@ func New(
<-c.waitDone
c.pipeConf.close()
c.pipeVideo.close()
return nil, err
return err
}
}
@ -195,7 +191,7 @@ func New( @@ -195,7 +191,7 @@ func New(
c.readerDone <- c.readData()
}()
return c, nil
return nil
}
func (c *RPICamera) Close() {
@ -248,6 +244,6 @@ func (c *RPICamera) readData() error { @@ -248,6 +244,6 @@ func (c *RPICamera) readData() error {
return err
}
c.onData(dts, nalus)
c.OnData(dts, nalus)
}
}

14
internal/protocols/rpicamera/rpicamera_disabled.go

@ -14,14 +14,14 @@ func Cleanup() { @@ -14,14 +14,14 @@ func Cleanup() {
}
// RPICamera is a RPI Camera reader.
type RPICamera struct{}
type RPICamera struct {
Params Params
OnData func(time.Duration, [][]byte)
}
// New allocates a RPICamera.
func New(
_ Params,
_ func(time.Duration, [][]byte),
) (*RPICamera, error) {
return nil, fmt.Errorf("server was compiled without support for the Raspberry Pi Camera")
// Initialize initializes a RPICamera.
func (c *RPICamera) Initialize() error {
return fmt.Errorf("server was compiled without support for the Raspberry Pi Camera")
}
// Close closes a RPICamera.

22
internal/staticsources/rpicamera/source.go

@ -15,8 +15,19 @@ import ( @@ -15,8 +15,19 @@ import (
"github.com/bluenviron/mediamtx/internal/unit"
)
func paramsFromConf(cnf *conf.Path) rpicamera.Params {
func paramsFromConf(logLevel conf.LogLevel, cnf *conf.Path) rpicamera.Params {
return rpicamera.Params{
LogLevel: func() string {
switch logLevel {
case conf.LogLevel(logger.Debug):
return "debug"
case conf.LogLevel(logger.Info):
return "info"
case conf.LogLevel(logger.Warn):
return "warn"
}
return "error"
}(),
CameraID: cnf.RPICameraCamID,
Width: cnf.RPICameraWidth,
Height: cnf.RPICameraHeight,
@ -54,6 +65,7 @@ func paramsFromConf(cnf *conf.Path) rpicamera.Params { @@ -54,6 +65,7 @@ func paramsFromConf(cnf *conf.Path) rpicamera.Params {
// Source is a Raspberry Pi Camera static source.
type Source struct {
LogLevel conf.LogLevel
Parent defs.StaticSourceParent
}
@ -96,7 +108,11 @@ func (s *Source) Run(params defs.StaticSourceRunParams) error { @@ -96,7 +108,11 @@ func (s *Source) Run(params defs.StaticSourceRunParams) error {
})
}
cam, err := rpicamera.New(paramsFromConf(params.Conf), onData)
cam := &rpicamera.RPICamera{
Params: paramsFromConf(s.LogLevel, params.Conf),
OnData: onData,
}
err := cam.Initialize()
if err != nil {
return err
}
@ -111,7 +127,7 @@ func (s *Source) Run(params defs.StaticSourceRunParams) error { @@ -111,7 +127,7 @@ func (s *Source) Run(params defs.StaticSourceRunParams) error {
for {
select {
case cnf := <-params.ReloadConf:
cam.ReloadParams(paramsFromConf(cnf))
cam.ReloadParams(paramsFromConf(s.LogLevel, cnf))
case <-params.Context.Done():
return nil

Loading…
Cancel
Save