Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace os.OpenFile with unix.Open to avoid blocking write operations #15

Merged
merged 2 commits into from
Mar 7, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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