diff --git a/videoio.cpp b/videoio.cpp index 40988436..2090fd59 100644 --- a/videoio.cpp +++ b/videoio.cpp @@ -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); } diff --git a/videoio.go b/videoio.go index 4891b20e..1a8ab8d9 100644 --- a/videoio.go +++ b/videoio.go @@ -169,7 +169,10 @@ 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 } @@ -177,7 +180,11 @@ func VideoCaptureFile(uri string) (vc *VideoCapture, err error) { // 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 } diff --git a/videoio.h b/videoio.h index 78944a18..b779fd9e 100644 --- a/videoio.h +++ b/videoio.h @@ -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); diff --git a/videoio_test.go b/videoio_test.go index 2783a608..f482c3f4 100644 --- a/videoio_test.go +++ b/videoio_test.go @@ -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") } @@ -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) {