Skip to content

Commit

Permalink
Added support for libcamera
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin committed Jan 23, 2022
1 parent 7ced790 commit 45960c1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func main() {
HorizontalFlip: true,
VerticalFlip: true,
Rotation: 0,
UseLibcamera: false,
}

router := mux.NewRouter()
Expand Down
34 changes: 21 additions & 13 deletions stream/streaming.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (
const (
readBufferSize = 4096
bufferSizeKB = 256

legacyCommand = "raspivid"
libcameraCommand = "libcamera-vid"
)

var nalSeparator = []byte{0, 0, 0, 1} //NAL break
Expand All @@ -25,6 +28,7 @@ type CameraOptions struct {
HorizontalFlip bool
VerticalFlip bool
Rotation int
UseLibcamera bool // Set to true to enable libcamera, otherwise use legacy raspivid stack
}

// Video streams the video for the Raspberry Pi camera to a websocket
Expand All @@ -51,14 +55,14 @@ func startCamera(options CameraOptions, writer io.Writer, stop chan bool, mutex
defer log.Println("Stopped raspivid")

args := []string{
"-ih",
"-t", "0",
"-o", "-",
"-w", strconv.Itoa(options.Width),
"-h", strconv.Itoa(options.Height),
"-fps", strconv.Itoa(options.Fps),
"-n",
"-pf", "baseline",
"--inline", // H264: Force PPS/SPS header with every I frame
"-t", "0", // Disable timeout
"-o", "-", // Output to stdout
"--width", strconv.Itoa(options.Width),
"--height", strconv.Itoa(options.Height),
"--framerate", strconv.Itoa(options.Fps),
"-n", // Do not show a preview window
"--profile", "baseline", // H264 profile
}

if options.HorizontalFlip {
Expand All @@ -71,9 +75,15 @@ func startCamera(options CameraOptions, writer io.Writer, stop chan bool, mutex
args = append(args, "--rotation")
args = append(args, strconv.Itoa(options.Rotation))
}
var command string
if options.UseLibcamera {
command = libcameraCommand
} else {
command = legacyCommand
}

ctx, cancel := context.WithCancel(context.Background())
cmd := exec.CommandContext(ctx, "raspivid", args...)
cmd := exec.CommandContext(ctx, command, args...)
defer cmd.Wait()
defer cancel()

Expand All @@ -84,7 +94,7 @@ func startCamera(options CameraOptions, writer io.Writer, stop chan bool, mutex
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
log.Println("Started raspivid", cmd.Args)
log.Println("Started "+command, cmd.Args)

p := make([]byte, readBufferSize)
buffer := make([]byte, bufferSizeKB*1024)
Expand All @@ -101,17 +111,15 @@ func startCamera(options CameraOptions, writer io.Writer, stop chan bool, mutex
if err != nil {
if err == io.EOF {
//fmt.Println(string(p[:n])) //should handle any remainding bytes.
log.Println("[Raspivid] EOF")
log.Println("[" + command + "] EOF")
return
}
log.Println(err)
}

//fmt.Println("Received", p[:n])
copied := copy(buffer[currentPos:], p[:n])
startPosSearch := currentPos - NALlen
endPos := currentPos + copied
//fmt.Println("Buffer", buffer[:endPos])

if startPosSearch < 0 {
startPosSearch = 0
Expand Down

0 comments on commit 45960c1

Please sign in to comment.