Browse Source

#IQRV #comment [mediacontroller] 解决多次打开和关闭摄像头后存在失败问题

陈礼鹏80274480 4 years ago
parent
commit
6a42f241ca

+ 2 - 29
Module/mod_mediacontroller/capture.cpp

@@ -1249,35 +1249,7 @@ static int video_capture_start_linux(video_capture_t* video_cap)
 		dev_id = rvc_videocap_get_video_device_id(conf->strVideoOpt.GetData());
 		if (-1 == dev_id){
 			Dbg("No operation camera[%s], please check config file or device!", conf->strVideoOpt.GetData());
-			{
-				const char* dev_name = conf->strVideoOpt.GetData();
-				if (NULL == dev_name) {
-					Dbg("conf->strVideoOpt.GetData() is null.");
-					return -1;
-				}
-
-				int icount = rvc_videocap_get_device_count();
-				Dbg("rvc_videocap_get_device_count is %d.", icount);
-				int ifound = 0;
-				for (int i = 0; i < 64 && ifound < icount; ++i) {
-					char strfullname[2 * MAX_PATH] = { 0 };
-					if (0 == rvc_videocap_get_device_fullpathname(i, strfullname, 2 * MAX_PATH)) {
-						ifound++;
-						Dbg("[%d] camera name is: %s", i, strfullname);
-						Dbg("      dev name is: %s", dev_name);
-						if (0 == strcasecmp(strfullname, dev_name))
-						{
-							Dbg("second time matched video device id is %d.", i);
-							dev_id = i;
-							break;
-						}
-					}
-				}
-			}
-
-			if (-1 == dev_id){
-				return iret;
-			}
+			return iret;
 		}
 	}
 	Dbg("video dev_id is %d", dev_id);
@@ -1336,6 +1308,7 @@ static int video_capture_start_linux(video_capture_t* video_cap)
 }
 #endif // RVC_OS_WIN
 
+
 static int video_capture_start(video_capture_t *video_cap)
 {
 #ifdef RVC_OS_WIN

+ 8 - 0
Other/libmediadeviceinfo/linux/videodevice_info_linux.cpp

@@ -38,6 +38,7 @@ int  rvc_videocap_get_device_count()
 			}
 
 			if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)){
+				close(fd);
 				continue;
 			}
 
@@ -66,6 +67,7 @@ int  rvc_videocap_get_device_count()
 
 				tFmtDesc.index++;
 			}
+			close(fd);
 		}
 	}
 	return icount;
@@ -90,6 +92,7 @@ int  rvc_videocap_get_device_name(int device_id, char* buf, int len)
 		}
 
 		if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)){
+			close(fd);
 			return iret;
 		}
 
@@ -121,6 +124,7 @@ int  rvc_videocap_get_device_name(int device_id, char* buf, int len)
 			snprintf(buf, len, "%s%s%s", cap.card, ";", cap.bus_info);
 			iret = 0;
 		}
+		close(fd);
 	}
 	return iret;
 }
@@ -152,6 +156,7 @@ int  rvc_videocap_get_device_info(int device_id, char* namebuf, int namelen, cha
 
 		if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
 		{
+			close(fd);
 			return iret;
 		}
 
@@ -185,6 +190,7 @@ int  rvc_videocap_get_device_info(int device_id, char* namebuf, int namelen, cha
 			snprintf(pathbuf, pathlen, "%s", cap.bus_info);
 			iret = 0;
 		}
+		close(fd);
 	}
 	return iret;
 }
@@ -226,6 +232,7 @@ int  rvc_videocap_get_device_fullpathname(int device_id, char* fullnamebuf, int
 
 		if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
 		{
+			close(fd);
 			return iret;
 		}
 
@@ -269,6 +276,7 @@ int  rvc_videocap_get_device_fullpathname(int device_id, char* fullnamebuf, int
 				iret = 0;
 			}
 		}
+		close(fd);
 	}
 	return iret;
 }

+ 11 - 0
Other/libvideocapture/linux/videocapture_linux.cpp

@@ -671,6 +671,7 @@ int VideoCaptureImpl::StartVideoCapture()
 		if (m_callback) {
 			m_callback->Debug("no supporting video formats found");
 		}
+		close(m_deviceFd);
 		return -1;
 	}
 	else {
@@ -708,6 +709,7 @@ int VideoCaptureImpl::StartVideoCapture()
 		if (m_callback){
 			m_callback->Debug("error in VIDIOC_S_FMT for %s.", strerror(errno));
 		}
+		close(m_deviceFd);
 		return -1;
 	}
 	else
@@ -716,6 +718,7 @@ int VideoCaptureImpl::StartVideoCapture()
 			if (m_callback){
 				m_callback->Debug("error in VIDIOC_G_FMT for %s.", strerror(errno));
 			}
+			close(m_deviceFd);
 			return -1;
 		}
 		else
@@ -789,6 +792,7 @@ int VideoCaptureImpl::StartVideoCapture()
 	}
 
 	if (false == GetCamBrightnessInfo()) {
+		close(m_deviceFd);
 		return -1;
 	}
 
@@ -796,6 +800,7 @@ int VideoCaptureImpl::StartVideoCapture()
 		if (m_callback){
 			m_callback->Debug("failed to allocate video capture buffers");
 		}
+		close(m_deviceFd);
 		return -1;
 	}
 	else{
@@ -808,6 +813,7 @@ int VideoCaptureImpl::StartVideoCapture()
 		if (m_callback){
 			m_callback->Debug("Create Video Capture Thread Failed!");
 		}
+		close(m_deviceFd);
 		return -1;
 	}
 
@@ -818,6 +824,7 @@ int VideoCaptureImpl::StartVideoCapture()
 		if (m_callback){
 			m_callback->Debug("failed to turn on stream for %s.", strerror(errno));
 		}
+		close(m_deviceFd);
 		return -1;
 	}
 
@@ -1012,6 +1019,10 @@ int VideoCaptureImpl::StopVideoCapture()
 		FreeAlignedMallocVideoBuffer();
 		close(m_deviceFd);
 		m_deviceFd = -1;
+
+		if (m_callback) {
+			m_callback->Debug("video capture has stopped!");
+		}
 	}
 
 	return 0;