From 7bf0a98909914cb1f6a6714dcebe8e2e456191fb Mon Sep 17 00:00:00 2001 From: Stefan Seyfried Date: Sat, 25 Feb 2012 21:18:07 +0100 Subject: [PATCH] spark: make cVideo::openDevice() more robust we sometimes seem to get EBUSY when opening the video device directly after close() - retry for half a second to get it opened --- libspark/video.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/libspark/video.cpp b/libspark/video.cpp index 43f9110..dcbe987 100644 --- a/libspark/video.cpp +++ b/libspark/video.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -156,13 +157,25 @@ cVideo::~cVideo(void) void cVideo::openDevice(void) { + int n = 0; lt_debug("%s\n", __func__); /* todo: this fd checking is racy, should be protected by a lock */ if (fd != -1) /* already open */ return; +retry: if ((fd = open(VIDEO_DEVICE, O_RDWR)) < 0) - lt_info("%s cannot open %s: %m\n", __FUNCTION__, VIDEO_DEVICE); - fcntl(fd, F_SETFD, FD_CLOEXEC); + { + if (errno == EBUSY) + { + /* sometimes we get busy quickly after close() */ + usleep(50000); + if (++n < 10) + goto retry; + } + lt_info("%s cannot open %s: %m, retries %d\n", __func__, VIDEO_DEVICE, n); + } + else + fcntl(fd, F_SETFD, FD_CLOEXEC); playstate = VIDEO_STOPPED; }