Ignore v4l2 select() return when stopping capture

With some slightly broken webcams, it's possible that the select()
returns with a timeout or no event. In that case, the v4l2 thread
never returns. To fix this, just check if quit_ is set and exit
unconditionally in that case.

https://bugzilla.mozilla.org/show_bug.cgi?id=1752326

Bug: None
Change-Id: Ic07ce15afd0016ff9f967c2cf64e646c20127457
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/251540
Reviewed-by: Magnus Flodman <mflodman@webrtc.org>
Reviewed-by: Per Kjellander <perkj@webrtc.org>
Commit-Queue: Per Kjellander <perkj@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#36200}
diff --git a/AUTHORS b/AUTHORS
index e4729a5..212c990 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -80,6 +80,7 @@
 Min Wang <mingewang@gmail.com>
 Mo Zanaty <mzanaty@cisco.com>
 Niek van der Maas <mail@niekvandermaas.nl>
+Olivier Crête <olivier.crete@ocrete.ca>
 Pali Rohar
 Paul Kapustin <pkapustin@gmail.com>
 Peng Yu <yupeng323@gmail.com>
diff --git a/modules/video_capture/linux/video_capture_linux.cc b/modules/video_capture/linux/video_capture_linux.cc
index 10f9713..321355f 100644
--- a/modules/video_capture/linux/video_capture_linux.cc
+++ b/modules/video_capture/linux/video_capture_linux.cc
@@ -370,17 +370,6 @@
 
   // _deviceFd written only in StartCapture, when this thread isn't running.
   retVal = select(_deviceFd + 1, &rSet, NULL, NULL, &timeout);
-  if (retVal < 0 && errno != EINTR)  // continue if interrupted
-  {
-    // select failed
-    return false;
-  } else if (retVal == 0) {
-    // select timed out
-    return true;
-  } else if (!FD_ISSET(_deviceFd, &rSet)) {
-    // not event on camera handle
-    return true;
-  }
 
   {
     MutexLock lock(&capture_lock_);
@@ -389,6 +378,18 @@
       return false;
     }
 
+    if (retVal < 0 && errno != EINTR)  // continue if interrupted
+    {
+      // select failed
+      return false;
+    } else if (retVal == 0) {
+      // select timed out
+      return true;
+    } else if (!FD_ISSET(_deviceFd, &rSet)) {
+      // not event on camera handle
+      return true;
+    }
+
     if (_captureStarted) {
       struct v4l2_buffer buf;
       memset(&buf, 0, sizeof(struct v4l2_buffer));