Skip to content

Commit

Permalink
Changed how the linux v4l2 file is opened (#66)
Browse files Browse the repository at this point in the history
open as read only. ioctl does not require files to be open for writting, and capturing a frame should be done read only.
This should prevent blocking other processes including the camera produce in cases where the v4l2 device is actually a loopback device.
Additionally fixed error handling around file opening to check for negative value prior to casting to unsigned.

Co-authored-by: Brian Stark <[email protected]>
  • Loading branch information
bpstark and Brian Stark authored Mar 29, 2024
1 parent e9ad39d commit f8ddb31
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion v4l2.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ func mmapQueryBuffer(fd uintptr, index uint32, length *uint32) (buffer []byte, e

*length = req.length

buffer, err = unix.Mmap(int(fd), int64(offset), int(req.length), unix.PROT_READ|unix.PROT_WRITE, unix.MAP_SHARED)
buffer, err = unix.Mmap(int(fd), int64(offset), int(req.length), unix.PROT_READ, unix.MAP_SHARED)
return
}

Expand Down
11 changes: 7 additions & 4 deletions webcam.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package webcam

import (
"errors"
"fmt"
"reflect"
"unsafe"

Expand Down Expand Up @@ -35,12 +36,14 @@ type Control struct {
// capable to stream video
func Open(path string) (*Webcam, error) {

handle, err := unix.Open(path, unix.O_RDWR|unix.O_NONBLOCK, 0666)
fd := uintptr(handle)

if fd < 0 || err != nil {
handle, err := unix.Open(path, unix.O_RDONLY|unix.O_NONBLOCK, 0666)
if err != nil {
return nil, err
}
if handle < 0 {
return nil, fmt.Errorf("failed to open %v", path)
}
fd := uintptr(handle)

supportsVideoCapture, supportsVideoStreaming, err := checkCapabilities(fd)

Expand Down

0 comments on commit f8ddb31

Please sign in to comment.