From 2e64b713880fc16bb8877eef8efdcc1619345263 Mon Sep 17 00:00:00 2001 From: Oliver Herms Date: Tue, 2 Feb 2021 16:18:10 +0100 Subject: [PATCH 1/2] Express time duration with time.Duration --- v4l2.go | 7 +++---- webcam.go | 6 ++++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/v4l2.go b/v4l2.go index 578c111..59fb90e 100644 --- a/v4l2.go +++ b/v4l2.go @@ -3,6 +3,7 @@ package webcam import ( "bytes" "encoding/binary" + "time" "unsafe" "github.com/blackjack/webcam/ioctl" @@ -431,15 +432,13 @@ func stopStreaming(fd uintptr) (err error) { } -func waitForFrame(fd uintptr, timeout uint32) (count int, err error) { +func waitForFrame(fd uintptr, timeout time.Duration) (count int, err error) { for { fds := &unix.FdSet{} fds.Set(int(fd)) - var oneSecInNsec int64 = 1e9 - timeoutNsec := int64(timeout) * oneSecInNsec - nativeTimeVal := unix.NsecToTimeval(timeoutNsec) + nativeTimeVal := unix.NsecToTimeval(int64(timeout)) tv := &nativeTimeVal count, err = unix.Select(int(fd+1), fds, nil, nil, tv) diff --git a/webcam.go b/webcam.go index 19d3559..4494641 100644 --- a/webcam.go +++ b/webcam.go @@ -5,9 +5,11 @@ package webcam import ( "errors" - "golang.org/x/sys/unix" "reflect" + "time" "unsafe" + + "golang.org/x/sys/unix" ) // Webcam object @@ -233,7 +235,7 @@ func (w *Webcam) ReleaseFrame(index uint32) error { } // Wait until frame could be read -func (w *Webcam) WaitForFrame(timeout uint32) error { +func (w *Webcam) WaitForFrame(timeout time.Duration) error { count, err := waitForFrame(w.fd, timeout) From aee88a9ef32408679e849daf65970eac48c7c147 Mon Sep 17 00:00:00 2001 From: Oliver Herms Date: Tue, 2 Feb 2021 16:19:52 +0100 Subject: [PATCH 2/2] Fix examples --- examples/http_mjpeg_streamer/webcam.go | 2 +- examples/stdout_streamer/stdout_streamer.go | 14 +++++++++----- webcam.go | 2 +- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/examples/http_mjpeg_streamer/webcam.go b/examples/http_mjpeg_streamer/webcam.go index adfa08c..1ce1f8a 100644 --- a/examples/http_mjpeg_streamer/webcam.go +++ b/examples/http_mjpeg_streamer/webcam.go @@ -145,7 +145,7 @@ FMT: go httpVideo(*addr, li) } - timeout := uint32(5) //5 seconds + timeout := time.Second * 5 start := time.Now() var fr time.Duration diff --git a/examples/stdout_streamer/stdout_streamer.go b/examples/stdout_streamer/stdout_streamer.go index af961c1..6efc184 100644 --- a/examples/stdout_streamer/stdout_streamer.go +++ b/examples/stdout_streamer/stdout_streamer.go @@ -6,10 +6,14 @@ // Example usage: go run stdout_streamer.go | vlc - package main -import "github.com/blackjack/webcam" -import "os" -import "fmt" -import "sort" +import ( + "fmt" + "os" + "sort" + "time" + + "github.com/blackjack/webcam" +) func readChoice(s string) int { var i int @@ -89,7 +93,7 @@ func main() { panic(err.Error()) } - timeout := uint32(5) //5 seconds + timeout := time.Second * 5 for { err = cam.WaitForFrame(timeout) diff --git a/webcam.go b/webcam.go index 4494641..e43e285 100644 --- a/webcam.go +++ b/webcam.go @@ -234,7 +234,7 @@ func (w *Webcam) ReleaseFrame(index uint32) error { return mmapEnqueueBuffer(w.fd, index) } -// Wait until frame could be read +// WaitForFrame waits until frame could be read func (w *Webcam) WaitForFrame(timeout time.Duration) error { count, err := waitForFrame(w.fd, timeout)