Skip to content

Commit

Permalink
Replace os.OpenFile with unix.Open to avoid blocking write operations (
Browse files Browse the repository at this point in the history
…#15)

Replace os.OpenFile with unix.Open to avoid blocking write operations on the camera.
  • Loading branch information
colin-davis authored and Oleksandr Senkovych committed Mar 7, 2018
1 parent c667811 commit e904897
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions webcam.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ package webcam
import (
"errors"
"golang.org/x/sys/unix"
"os"
"reflect"
"unsafe"
)

// Webcam object
type Webcam struct {
file *os.File
fd uintptr
buffers [][]byte
}
Expand All @@ -23,8 +21,8 @@ type Webcam struct {
// capable to stream video
func Open(path string) (*Webcam, error) {

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

if fd < 0 || err != nil {
return nil, err
Expand All @@ -45,8 +43,7 @@ func Open(path string) (*Webcam, error) {
}

w := new(Webcam)
w.fd = fd
w.file = file
w.fd = uintptr(fd)
return w, nil
}

Expand Down Expand Up @@ -218,7 +215,7 @@ func (w *Webcam) Close() error {
}
}

err := w.file.Close()
err := unix.Close(int(w.fd))

return err
}
Expand Down

0 comments on commit e904897

Please sign in to comment.