Skip to content

Commit

Permalink
Return the right type and error when opening VideoCapture fails
Browse files Browse the repository at this point in the history
When opening a video capture OpenCV signals an error when open() fails,
however we are ignoring this error and not setting it when returning
from Go bindings functions.

Furthermore open() function returns bool. We are returning the open()
error as integeer, which is ok in C/C++ world as it's implicitly cast
into particular bool value - we want to make this explicit in GoCV so we
are returning bool now as per OpenCV.
  • Loading branch information
milosgajdos committed Nov 13, 2018
1 parent ee0f33d commit 218c4ae
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
4 changes: 2 additions & 2 deletions videoio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ void VideoCapture_Close(VideoCapture v) {
delete v;
}

int VideoCapture_Open(VideoCapture v, const char* uri) {
bool VideoCapture_Open(VideoCapture v, const char* uri) {
return v->open(uri);
}

int VideoCapture_OpenDevice(VideoCapture v, int device) {
bool VideoCapture_OpenDevice(VideoCapture v, int device) {
return v->open(device);
}

Expand Down
11 changes: 9 additions & 2 deletions videoio.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,22 @@ func VideoCaptureFile(uri string) (vc *VideoCapture, err error) {
cURI := C.CString(uri)
defer C.free(unsafe.Pointer(cURI))

C.VideoCapture_Open(vc.p, cURI)
if !C.VideoCapture_Open(vc.p, cURI) {
err = fmt.Errorf("Error opening file: %s", uri)
}

return
}

// VideoCaptureDevice opens a VideoCapture from a device and prepares
// to start capturing.
func VideoCaptureDevice(device int) (vc *VideoCapture, err error) {
vc = &VideoCapture{p: C.VideoCapture_New()}
C.VideoCapture_OpenDevice(vc.p, C.int(device))

if !C.VideoCapture_OpenDevice(vc.p, C.int(device)) {
err = fmt.Errorf("Error opening device: %d", device)
}

return
}

Expand Down
4 changes: 2 additions & 2 deletions videoio.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ typedef void* VideoWriter;
// VideoCapture
VideoCapture VideoCapture_New();
void VideoCapture_Close(VideoCapture v);
int VideoCapture_Open(VideoCapture v, const char* uri);
int VideoCapture_OpenDevice(VideoCapture v, int device);
bool VideoCapture_Open(VideoCapture v, const char* uri);
bool VideoCapture_OpenDevice(VideoCapture v, int device);
void VideoCapture_Set(VideoCapture v, int prop, double param);
double VideoCapture_Get(VideoCapture v, int prop);
int VideoCapture_IsOpened(VideoCapture v);
Expand Down
13 changes: 12 additions & 1 deletion videoio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ func TestVideoCaptureInvalid(t *testing.T) {
}

func TestVideoCaptureFile(t *testing.T) {
vc, _ := VideoCaptureFile("images/small.mp4")
vc, err := VideoCaptureFile("images/small.mp4")
defer vc.Close()

if err != nil {
t.Errorf("%s", err)
}

if !vc.IsOpened() {
t.Error("Unable to open VideoCaptureFile")
}
Expand All @@ -62,6 +66,13 @@ func TestVideoCaptureFile(t *testing.T) {
if img.Empty() {
t.Error("Unable to read VideoCaptureFile")
}

vc2, err := VideoCaptureFile("nonexistent.mp4")
defer vc2.Close()

if err == nil {
t.Errorf("Expected error when opening invalid file")
}
}

func TestVideoWriterFile(t *testing.T) {
Expand Down

0 comments on commit 218c4ae

Please sign in to comment.