Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(container): add ca-certificates package #49

Merged
merged 10 commits into from
Mar 10, 2024
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ FROM debian:bookworm-slim

# Install ALSA library and SOX
RUN apt-get update && apt-get install -y \
ca-certificates \
libasound2 \
sox \
&& rm -rf /var/lib/apt/lists/*
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ ifeq ($(TARGETPLATFORM),linux/arm64)
endif

# Common flags
CGO_FLAGS := CGO_ENABLED=1 CGO_CFLAGS="-I$(HOME)/src/tensorflow -DMA_NO_PULSEAUDIO"
#CGO_FLAGS := CGO_ENABLED=1 CGO_CFLAGS="-I$(HOME)/src/tensorflow -DMA_NO_PULSEAUDIO"
CGO_FLAGS := CGO_ENABLED=1 CGO_CFLAGS="-I$(HOME)/src/tensorflow"
LDFLAGS := -ldflags "-s -w"

# Default build for local development
Expand Down
2 changes: 1 addition & 1 deletion internal/analysis/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (p *Processor) startDetectionProcessor() {
// processDetections examines each detection from the queue, updating held detections
// with new or higher-confidence instances and setting an appropriate flush deadline.
func (p *Processor) processDetections(item *queue.Results) {
const delay = 9 * time.Second // Delay before a detection is considered final and is flushed.
const delay = 12 * time.Second // Delay before a detection is considered final and is flushed.

for _, detection := range p.processResults(item) {
commonName := strings.ToLower(detection.Note.CommonName)
Expand Down
38 changes: 38 additions & 0 deletions internal/conf/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
package conf

import (
"bufio"
"fmt"
"log"
"os"
"os/user"
"path/filepath"
"runtime"
"strings"
)

// getDefaultConfigPaths returns a list of default configuration paths for the current operating system.
Expand Down Expand Up @@ -104,3 +106,39 @@ func PrintUserInfo() {
}
}
}

// RunningInContainer checks if the program is running inside a container.
func RunningInContainer() bool {
// Check for the existence of the /.dockerenv file (Docker-specific).
if _, err := os.Stat("/.dockerenv"); err == nil {
return true
}

// Check for the existence of the /run/.containerenv file (Podman-specific).
if _, err := os.Stat("/run/.containerenv"); err == nil {
return true
}

// Check the container environment variable.
if containerEnv, exists := os.LookupEnv("container"); exists && containerEnv != "" {
return true
}

// Check cgroup for hints of container runtime.
file, err := os.Open("/proc/self/cgroup")
if err != nil {
fmt.Println("Error opening /proc/self/cgroup:", err)
return false
}
defer file.Close()

scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "docker") || strings.Contains(line, "podman") {
return true
}
}

return false
}
1 change: 1 addition & 0 deletions internal/myaudio/audiobuffer.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// this file defines ring buffer which is used for capturing audio clips
package myaudio

import (
Expand Down
35 changes: 34 additions & 1 deletion internal/myaudio/capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"os"
"runtime"
"sync"
"time"

Expand All @@ -19,7 +20,18 @@ func CaptureAudio(settings *conf.Settings, wg *sync.WaitGroup, quitChan chan str
if settings.Debug {
fmt.Println("Initializing context")
}
malgoCtx, err := malgo.InitContext(nil, malgo.ContextConfig{}, func(message string) {

// if Linux set malgo.BackendAlsa, else set nil for auto select
var backend malgo.Backend
if runtime.GOOS == "linux" {
backend = malgo.BackendAlsa
} else if runtime.GOOS == "windows" {
backend = malgo.BackendWasapi
} else if runtime.GOOS == "darwin" {
backend = malgo.BackendCoreaudio
}

malgoCtx, err := malgo.InitContext([]malgo.Backend{backend}, malgo.ContextConfig{}, func(message string) {
if settings.Debug {
fmt.Print(message)
}
Expand All @@ -35,6 +47,27 @@ func CaptureAudio(settings *conf.Settings, wg *sync.WaitGroup, quitChan chan str
deviceConfig.SampleRate = conf.SampleRate
deviceConfig.Alsa.NoMMap = 1

var infos []malgo.DeviceInfo

// Get list of capture devices
infos, err = malgoCtx.Devices(malgo.Capture)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

fmt.Println("Capture Devices")
for i, info := range infos {
e := "ok"
_, err := malgoCtx.DeviceInfo(malgo.Capture, info.ID, malgo.Shared)
if err != nil {
e = err.Error()
}
fmt.Printf(" %d: %s, %s, [%s]\n", i, info.Name(), info.ID.String(), e)
}
//selectedDeviceInfo := infos[2]
//deviceConfig.Capture.DeviceID = selectedDeviceInfo.ID.Pointer()

// Write to ringbuffer when audio data is received
// BufferMonitor() will poll this buffer and read data from it
onReceiveFrames := func(pSample2, pSamples []byte, framecount uint32) {
Expand Down
6 changes: 5 additions & 1 deletion internal/myaudio/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ func ProcessData(data []byte, bn *birdnet.BirdNET) error {

// Create a Results message to be sent through queue to processor
resultsMessage := queue.Results{
StartTime: startTime.Add(-4000 * time.Millisecond),
// Start time is the time from which point capture audio is started, because of delay
// caused by BirdNET analysis etc. we need to go back in time for some amount to start
// capture before bird call begins (at least that is the idea, not sure if it is always
// working as intended)
StartTime: startTime.Add(-5000 * time.Millisecond),
ElapsedTime: elapsedTime,
PCMdata: data,
Results: results,
Expand Down
Loading