restore compatibility with non-raspberry arm systems (#1587)

This commit is contained in:
Alessandro Ros 2023-03-20 23:01:47 +01:00 committed by GitHub
parent 7e684f157d
commit 7a4928643d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 91 additions and 90 deletions

View File

@ -181,8 +181,6 @@ outer:
p.ctxCancel()
p.closeResources(nil, false)
rpicamera.LibcameraCleanup()
}
func (p *Core) createResources(initial bool) error {
@ -210,11 +208,6 @@ func (p *Core) createResources(initial bool) error {
// do not check for errors
rlimit.Raise()
err := rpicamera.LibcameraSetup()
if err != nil {
return err
}
gin.SetMode(gin.ReleaseMode)
p.externalCmdPool = externalcmd.NewPool()
@ -679,6 +672,10 @@ func (p *Core) closeResources(newConf *conf.Conf, calledByAPI bool) {
p.externalCmdPool.Close()
}
if newConf == nil {
rpicamera.Cleanup()
}
if closeLogger {
p.logger.Close()
p.logger = nil

View File

@ -1,51 +0,0 @@
//go:build rpicamera
// +build rpicamera
package rpicamera
import (
"fmt"
"os"
"os/exec"
"strings"
)
func findLibrary(name string) (string, error) {
byts, err := exec.Command("ldconfig", "-p").Output()
if err == nil {
for _, line := range strings.Split(string(byts), "\n") {
f := strings.Split(line, " => ")
if len(f) == 2 && strings.Contains(f[1], name+".so") {
return f[1], nil
}
}
}
return "", fmt.Errorf("library '%s' not found", name)
}
func setupSymlink(name string) error {
lib, err := findLibrary(name)
if err != nil {
return err
}
os.Remove("/dev/shm/" + name + ".so.x.x.x")
return os.Symlink(lib, "/dev/shm/"+name+".so.x.x.x")
}
// LibcameraSetup creates libcamera simlinks that are version agnostic.
func LibcameraSetup() error {
err := setupSymlink("libcamera")
if err != nil {
return err
}
return setupSymlink("libcamera-base")
}
// LibcameraCleanup removes files created by LibcameraSetup.
func LibcameraCleanup() {
os.Remove("/dev/shm/libcamera-base.so.x.x.x")
os.Remove("/dev/shm/libcamera.so.x.x.x")
}

View File

@ -1,13 +0,0 @@
//go:build !rpicamera
// +build !rpicamera
package rpicamera
// LibcameraSetup creates libcamera simlinks that are version agnostic.
func LibcameraSetup() error {
return nil
}
// LibcameraCleanup removes files created by LibcameraSetup.
func LibcameraCleanup() {
}

View File

@ -12,6 +12,7 @@ import (
"runtime"
"strconv"
"strings"
"sync"
"time"
"github.com/aler9/gortsplib/v2/pkg/codecs/h264"
@ -35,24 +36,6 @@ func getKernelArch() (string, error) {
return string(byts[:len(byts)-1]), nil
}
// 32-bit embedded executables can't run on 64-bit.
func checkArch() error {
if runtime.GOARCH != "arm" {
return nil
}
arch, err := getKernelArch()
if err != nil {
return err
}
if arch == "aarch64" {
return fmt.Errorf("OS is 64-bit, you need the arm64 server version")
}
return nil
}
func startEmbeddedExe(content []byte, env []string) (*exec.Cmd, error) {
tempPath := tempPathPrefix + strconv.FormatInt(time.Now().UnixNano(), 10)
@ -113,6 +96,87 @@ func serializeParams(p Params) []byte {
return []byte(strings.Join(ret, " "))
}
func findLibrary(name string) (string, error) {
byts, err := exec.Command("ldconfig", "-p").Output()
if err == nil {
for _, line := range strings.Split(string(byts), "\n") {
f := strings.Split(line, " => ")
if len(f) == 2 && strings.Contains(f[1], name+".so") {
return f[1], nil
}
}
}
return "", fmt.Errorf("library '%s' not found", name)
}
func setupSymlink(name string) error {
lib, err := findLibrary(name)
if err != nil {
return err
}
os.Remove("/dev/shm/" + name + ".so.x.x.x")
return os.Symlink(lib, "/dev/shm/"+name+".so.x.x.x")
}
// 32-bit embedded executables can't run on 64-bit.
func checkArch() error {
if runtime.GOARCH != "arm" {
return nil
}
arch, err := getKernelArch()
if err != nil {
return err
}
if arch == "aarch64" {
return fmt.Errorf("OS is 64-bit, you need the arm64 server version")
}
return nil
}
var (
mutex sync.Mutex
setupped bool
)
func setupLibcameraOnce() error {
mutex.Lock()
defer mutex.Unlock()
if !setupped {
err := checkArch()
if err != nil {
return err
}
err = setupSymlink("libcamera")
if err != nil {
return err
}
err = setupSymlink("libcamera-base")
if err != nil {
return err
}
setupped = true
}
return nil
}
// Cleanup cleanups files created by the camera implementation.
func Cleanup() {
if setupped {
os.Remove("/dev/shm/libcamera-base.so.x.x.x")
os.Remove("/dev/shm/libcamera.so.x.x.x")
}
}
type RPICamera struct {
onData func(time.Duration, [][]byte)
@ -128,7 +192,7 @@ func New(
params Params,
onData func(time.Duration, [][]byte),
) (*RPICamera, error) {
err := checkArch()
err := setupLibcameraOnce()
if err != nil {
return nil, err
}

View File

@ -9,6 +9,10 @@ import (
"time"
)
// Cleanup cleanups files created by the camera implementation.
func Cleanup() {
}
// RPICamera is a RPI Camera reader.
type RPICamera struct{}