Skip to content

Commit

Permalink
rpi: pass log level to libcamera (#2617)
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Dec 15, 2023
1 parent 22414bb commit 6c41f5c
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 33 deletions.
2 changes: 2 additions & 0 deletions internal/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) ||
Expand Down
4 changes: 4 additions & 0 deletions internal/core/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type pathAPIPathsGetReq struct {
}

type path struct {
logLevel conf.LogLevel
rtspAddress string
readTimeout conf.StringDuration
writeTimeout conf.StringDuration
Expand Down Expand Up @@ -116,6 +117,7 @@ type path struct {

func newPath(
parentCtx context.Context,
logLevel conf.LogLevel,
rtspAddress string,
readTimeout conf.StringDuration,
writeTimeout conf.StringDuration,
Expand All @@ -132,6 +134,7 @@ func newPath(
ctx, ctxCancel := context.WithCancel(parentCtx)

pa := &path{
logLevel: logLevel,
rtspAddress: rtspAddress,
readTimeout: readTimeout,
writeTimeout: writeTimeout,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions internal/core/path_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ type pathManagerParent interface {
}

type pathManager struct {
logLevel conf.LogLevel
externalAuthenticationURL string
rtspAddress string
authMethods conf.AuthMethods
Expand Down Expand Up @@ -112,6 +113,7 @@ type pathManager struct {
}

func newPathManager(
logLevel conf.LogLevel,
externalAuthenticationURL string,
rtspAddress string,
authMethods conf.AuthMethods,
Expand All @@ -126,6 +128,7 @@ func newPathManager(
ctx, ctxCancel := context.WithCancel(context.Background())

pm := &pathManager{
logLevel: logLevel,
externalAuthenticationURL: externalAuthenticationURL,
rtspAddress: rtspAddress,
authMethods: authMethods,
Expand Down Expand Up @@ -400,6 +403,7 @@ func (pm *pathManager) createPath(
) {
pa := newPath(
pm.ctx,
pm.logLevel,
pm.rtspAddress,
pm.readTimeout,
pm.writeTimeout,
Expand Down
4 changes: 3 additions & 1 deletion internal/core/static_source_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -108,7 +109,8 @@ func (s *staticSourceHandler) initialize() {

case s.resolvedSource == "rpiCamera":
s.instance = &rpicamerasource.Source{
Parent: s,
LogLevel: s.logLevel,
Parent: s,

Check warning on line 113 in internal/core/static_source_handler.go

View check run for this annotation

Codecov / codecov/patch

internal/core/static_source_handler.go#L112-L113

Added lines #L112 - L113 were not covered by tests
}
}
}
Expand Down
14 changes: 12 additions & 2 deletions internal/protocols/rpicamera/exe/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion internal/protocols/rpicamera/exe/parameters.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions internal/protocols/rpicamera/exe/parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "sensor_mode.h"

typedef struct {
char *log_level;
unsigned int camera_id;
unsigned int width;
unsigned int height;
Expand Down
1 change: 1 addition & 0 deletions internal/protocols/rpicamera/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

// Params is a set of camera parameters.
type Params struct {
LogLevel string
CameraID int
Width int
Height int
Expand Down
32 changes: 14 additions & 18 deletions internal/protocols/rpicamera/rpicamera.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{
Expand All @@ -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() {
Expand All @@ -178,15 +174,15 @@ 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 {
c.pipeConf.write([]byte{'e'})
<-c.waitDone
c.pipeConf.close()
c.pipeVideo.close()
return nil, err
return err
}
}

Expand All @@ -195,7 +191,7 @@ func New(
c.readerDone <- c.readData()
}()

return c, nil
return nil
}

func (c *RPICamera) Close() {
Expand Down Expand Up @@ -248,6 +244,6 @@ func (c *RPICamera) readData() error {
return err
}

c.onData(dts, nalus)
c.OnData(dts, nalus)
}
}
14 changes: 7 additions & 7 deletions internal/protocols/rpicamera/rpicamera_disabled.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
24 changes: 20 additions & 4 deletions internal/staticsources/rpicamera/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -54,7 +65,8 @@ func paramsFromConf(cnf *conf.Path) rpicamera.Params {

// Source is a Raspberry Pi Camera static source.
type Source struct {
Parent defs.StaticSourceParent
LogLevel conf.LogLevel
Parent defs.StaticSourceParent
}

// Log implements logger.Writer.
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down

0 comments on commit 6c41f5c

Please sign in to comment.