Skip to content

Commit

Permalink
use poll instead of select in waitForFrame to allow for large fds
Browse files Browse the repository at this point in the history
Fixes #65
  • Loading branch information
edaniels committed Nov 5, 2023
1 parent 87693b3 commit e9ad39d
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/blackjack/webcam

go 1.18

require golang.org/x/sys v0.14.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
14 changes: 2 additions & 12 deletions v4l2.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,19 +557,9 @@ func stopStreaming(fd uintptr) (err error) {

}

func waitForFrame(fd uintptr, timeout uint32) (count int, err error) {

func waitForFrame(pollFds []unix.PollFd, timeout uint32) (count int, err error) {
for {
fds := &unix.FdSet{}
fds.Set(int(fd))

var oneSecInNsec int64 = 1e9
timeoutNsec := int64(timeout) * oneSecInNsec
nativeTimeVal := unix.NsecToTimeval(timeoutNsec)
tv := &nativeTimeVal

count, err = unix.Select(int(fd+1), fds, nil, nil, tv)

count, err = unix.Poll(pollFds, int(timeout*1000))
if count < 0 && err == unix.EINTR {
continue
}
Expand Down
4 changes: 3 additions & 1 deletion webcam.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Webcam struct {
bufcount uint32
buffers [][]byte
streaming bool
pollFds []unix.PollFd
}

type ControlID uint32
Expand Down Expand Up @@ -58,6 +59,7 @@ func Open(path string) (*Webcam, error) {
w := new(Webcam)
w.fd = fd
w.bufcount = 256
w.pollFds = []unix.PollFd{{Fd: int32(fd), Events: unix.POLLIN}}
return w, nil
}

Expand Down Expand Up @@ -294,7 +296,7 @@ func (w *Webcam) ReleaseFrame(index uint32) error {
// Wait until frame could be read
func (w *Webcam) WaitForFrame(timeout uint32) error {

count, err := waitForFrame(w.fd, timeout)
count, err := waitForFrame(w.pollFds, timeout)

if count < 0 || err != nil {
return err
Expand Down

0 comments on commit e9ad39d

Please sign in to comment.