Forráskód Böngészése

Z991239-2055 #comment fix: 优化摄像头采集库日志

陈礼鹏80274480 4 éve
szülő
commit
f5db04da26
1 módosított fájl, 128 hozzáadás és 59 törlés
  1. 128 59
      Other/libvideocapture/libvideocapture.cpp

+ 128 - 59
Other/libvideocapture/libvideocapture.cpp

@@ -67,9 +67,6 @@ VideoCaptureImpl::VideoCaptureImpl(ICaptureCallback* pCallback)
 
 	m_iminbrightness = 0;
 	m_imaxbrightness = 0;
-	//m_pFilei420 = NULL;
-	//m_pFileirgb = NULL;
-	//m_pFilei422 = NULL;
 }
 
 VideoCaptureImpl::~VideoCaptureImpl()
@@ -84,12 +81,6 @@ VideoCaptureImpl::~VideoCaptureImpl()
 
 int VideoCaptureImpl::VideoCaptureSetParam(videocap_param_t* param)
 {
-	//m_pFilei420 = fopen("env_640_360_videocapturelib_i420.yuv", "wb+");
-	//m_pFileirgb = fopen("env_640_360_videocapturelib.rgb", "wb+");
-	//m_pFilei422 = fopen("env_640_360_videocapturelib_i422.yuv", "wb+");
-	//if (NULL == m_pFilei420 || NULL == m_pFileirgb || NULL == m_pFilei422) {
-	//	return -1;
-	//}
 	/* check param */
 	if (NULL == param) {
 		return -1;
@@ -103,7 +94,7 @@ int VideoCaptureImpl::VideoCaptureSetParam(videocap_param_t* param)
 		return -1;
 	}
 
-	if (param->fps < 1.0 || param->fps > 100.0) {
+	if (param->fps < 1.0 || param->fps > 50.0) {
 		return -1;
 	}
 
@@ -131,10 +122,15 @@ int VideoCaptureImpl::VideoCaptureSetParam(videocap_param_t* param)
 		}
 	}
 	else {
-		m_callback->Debug("param->option & VIDEOCAP_OPT_EANBLE_RESIZE sucess.");
+		if (m_callback){
+			m_callback->Debug("param->option & VIDEOCAP_OPT_EANBLE_RESIZE success.");
+		}
 	}
 
-	m_callback->Debug("%s:%d param->option = %d.", __FUNCTION__, __LINE__, param->option);
+	if (m_callback){
+		m_callback->Debug("%s:%d param->option = %d.", __FUNCTION__, __LINE__, param->option);
+	}
+	
 	m_capture = (videocap_t*)malloc(sizeof(videocap_t));
 	if (!m_capture) {
 		return -1;
@@ -146,38 +142,38 @@ int VideoCaptureImpl::VideoCaptureSetParam(videocap_param_t* param)
 	if (param->option & VIDEOCAP_OPT_ENABLE_GRAB) {
 		int width = mode_width[param->cap_mode];
 		int height = mode_height[param->cap_mode];
-		m_callback->Debug("%s:%d, width = %d, height = %d.", __FUNCTION__, __LINE__, width, height);
+		if (m_callback){
+			m_callback->Debug("%s:%d, width = %d, height = %d.", __FUNCTION__, __LINE__, width, height);
+		}
+		
 		if (video_frame_alloc(width, height, param->frame_fmt, &m_capture->cap_frame) != 0) {
 			free(m_capture);
 			return -1;
 		}
 		video_frame_fill_black(&m_capture->cap_frame);
-		//InitializeCriticalSection(&m_capture->cap_cs);
 	}
 	else{
-		m_callback->Debug("param->option & VIDEOCAP_OPT_ENABLE_GRAB is false");
+		if (m_callback){
+			m_callback->Debug("param->option & VIDEOCAP_OPT_ENABLE_GRAB is false");
+		}
 	}
 	if (param->option & VIDEOCAP_OPT_ENABLE_ASYNC_GRAB) {
-		//InitializeCriticalSection(&m_capture->async_cap_cs);
 	}
 	if (param->option & VIDEOCAP_OPT_EANBLE_RESIZE) {
 		int width = mode_width[param->res_mode];
 		int height = mode_height[param->res_mode];
-		m_callback->Debug("%s:%d, width = %d, height = %d.", __FUNCTION__, __LINE__, width, height);
+		if (m_callback){
+			m_callback->Debug("%s:%d, width = %d, height = %d.", __FUNCTION__, __LINE__, width, height);
+		}
+		
 		if (video_frame_alloc(width, height, param->frame_fmt, &m_capture->res_frame) != 0) {
 			if (param->option & VIDEOCAP_OPT_ENABLE_GRAB) {
-				//DeleteCriticalSection(&vcap->cap_cs);
 				video_frame_free(&m_capture->res_frame);
 			}
 			free(m_capture);
 			return -1;
 		}
-		else {
-			m_callback->Debug("video_frame_alloc success");
-		}
 
-		m_callback->Debug("%s:%d linesize_0 = %d, linesize_1 = %d, linesize_2 = %d", __FUNCTION__, __LINE__, m_capture->res_frame.linesize[0], m_capture->res_frame.linesize[1], m_capture->res_frame.linesize[2]);
-		m_callback->Debug("res_frame format is %d", m_capture->res_frame.format);
 		video_frame_fill_black(&m_capture->res_frame);
 		m_capture->sws_context = sws_getContext(mode_width[param->cap_mode],
 			mode_height[param->cap_mode],
@@ -192,13 +188,11 @@ int VideoCaptureImpl::VideoCaptureSetParam(videocap_param_t* param)
 		if (!m_capture->sws_context) {
 			video_frame_free(&m_capture->res_frame);
 			if (param->option & VIDEOCAP_OPT_ENABLE_GRAB) {
-				//DeleteCriticalSection(&vcap->cap_cs);
 				video_frame_free(&m_capture->cap_frame);
 			}
 			free(m_capture);
 			return -1;
 		}
-		//InitializeCriticalSection(&vcap->res_cs);
 	}
 	m_dest_cap_width = mode_width[m_capture->param.cap_mode];
 	m_dest_cap_height = mode_height[m_capture->param.cap_mode];
@@ -295,14 +289,19 @@ int32_t VideoCaptureImpl::IncomingFrame(uint8_t* videoFrame,
 
 	static int icount = 0;
 	if (0 == icount){
-		m_callback->Debug("IncomingFrame capture_time is %d videoFrameLength = %d, width = %d, height = %d, and destination width = %d, height = %d.", captureTime, videoFrameLength, width, height, m_dest_cap_width, m_dest_cap_height);
+		if (m_callback){
+			m_callback->Debug("IncomingFrame capture_time is %d videoFrameLength = %d, width = %d, height = %d, and destination width = %d, height = %d.", captureTime, videoFrameLength, width, height, m_dest_cap_width, m_dest_cap_height);
+		}
 		icount++;
 	}
 	
 	// Not encoded, convert to I420.
 	if (frameInfo.videoType != VideoType::kMJPEG &&
 		CalcBufferSize(frameInfo.videoType, width, abs(height)) != videoFrameLength) {
-		m_callback->Debug("Wrong incoming frame length.");
+		if (m_callback){
+			m_callback->Debug("Wrong incoming frame length.");
+		}
+		
 		return -1;
 	}
 
@@ -476,6 +475,7 @@ static void* VideoCaptureProcess(void *arg)
 	VideoCaptureImpl* pVideoCapture = (VideoCaptureImpl*)arg;
 	int iDeviceFd = pVideoCapture->GetCaptureVideoFd();
 
+	ICaptureCallback* pcallback = pVideoCapture->GetCaptureCallback();
 	while (false == pVideoCapture->GetStopCaptureFlag())
 	{
 		FD_ZERO(&rSet);
@@ -487,17 +487,26 @@ static void* VideoCaptureProcess(void *arg)
 		if (retVal < 0 && errno != EINTR)  // continue if interrupted
 		{
 			// select failed
-			pVideoCapture->GetCaptureCallback()->Debug("exit for select failed.");
+			if (pcallback){
+				pcallback->Debug("exit for select failed.");
+			}
+			
 			return NULL;
 		}
 		else if (retVal == 0) {
 			// select timed out
-			pVideoCapture->GetCaptureCallback()->Debug("exit for select timed out.");
+			if (pcallback){
+				pcallback->Debug("exit for select timed out.");
+			}
+			
 			return NULL;
 		}
 		else if (!FD_ISSET(iDeviceFd, &rSet)) {
 			// not event on camera handle
-			pVideoCapture->GetCaptureCallback()->Debug("exit for not event on camera handle.");
+			if (pcallback){
+				pcallback->Debug("exit for not event on camera handle.");
+			}
+			
 			return NULL;
 		}
 
@@ -509,7 +518,9 @@ static void* VideoCaptureProcess(void *arg)
 			// dequeue a buffer - repeat until dequeued properly!
 			while (ioctl(iDeviceFd, VIDIOC_DQBUF, &buf) < 0) {
 				if (errno != EINTR) {
-					pVideoCapture->GetCaptureCallback()->Debug("could not sync on a buffer on device %s.", strerror(errno));
+					if (pcallback){
+						pcallback->Debug("could not sync on a buffer on device %s.", strerror(errno));
+					}
 					return NULL;
 				}
 			}
@@ -524,7 +535,9 @@ static void* VideoCaptureProcess(void *arg)
 			pVideoCapture->IncomingFrame((unsigned char*)buffer_pool[buf.index].start, buf.length, frameInfo);
 			// enqueue the buffer again
 			if (ioctl(iDeviceFd, VIDIOC_QBUF, &buf) == -1) {
-				pVideoCapture->GetCaptureCallback()->Debug("Failed to enqueue capture buffer");
+				if (pcallback){
+					pcallback->Debug("Failed to enqueue capture buffer");
+				}
 			}
 		}
 	}
@@ -551,7 +564,9 @@ int VideoCaptureImpl::StartVideoCapture()
 	char device[20] = {0};
 	snprintf(device, 20,"/dev/video%d", (int)m_deviceId);
 	if ((m_deviceFd = open(device, O_RDWR | O_NONBLOCK, 0)) < 0) {
-		m_callback->Debug("error in opening %s for %s.", device, strerror(errno));
+		if (m_callback){
+			m_callback->Debug("error in opening %s for %s.", device, strerror(errno));
+		}
 		return -1;
 	}
 
@@ -581,11 +596,15 @@ int VideoCaptureImpl::StartVideoCapture()
 	memset(&fmt, 0, sizeof(fmt));
 	fmt.index = 0;
 	fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-	m_callback->Debug("Video Capture enumerates supported image formats:");
+	if (m_callback){
+		m_callback->Debug("Video Capture enumerates supported image formats:");
+	}
 	while (ioctl(m_deviceFd, VIDIOC_ENUM_FMT, &fmt) == 0) {
 		char strformat[32] = { 0 };
 		GetFourccName(strformat, 32, fmt.pixelformat);
-		m_callback->Debug("pixelformat=%s, description='%s'", strformat, fmt.description);
+		if (m_callback) {
+			m_callback->Debug("pixelformat=%s, description='%s'", strformat, fmt.description);
+		}
 		// Match the preferred order.
 		for (int i = 0; i < nFormats; i++) {
 			if (fmt.pixelformat == fmts[i] && i < fmtsIdx)
@@ -596,13 +615,17 @@ int VideoCaptureImpl::StartVideoCapture()
 	}
 
 	if (fmtsIdx == nFormats) {
-		m_callback->Debug("no supporting video formats found");
+		if (m_callback) {
+			m_callback->Debug("no supporting video formats found");
+		}
 		return -1;
 	}
 	else {
 		char strformat[32] = { 0 };
 		GetFourccName(strformat, 32, fmts[fmtsIdx]);
-		m_callback->Debug("we prefer format %s.",strformat);
+		if (m_callback){
+			m_callback->Debug("we prefer format %s.", strformat);
+		}
 	}
 
 	struct v4l2_format video_fmt;
@@ -613,7 +636,9 @@ int VideoCaptureImpl::StartVideoCapture()
 	video_fmt.fmt.pix.height = mode_height[m_capture->param.cap_mode];
 	video_fmt.fmt.pix.pixelformat = fmts[fmtsIdx];
 
-	m_callback->Debug("video_fmt.fmt.pix.width = %d, video_fmt.fmt.pix.height = %d.", video_fmt.fmt.pix.width, video_fmt.fmt.pix.height);
+	if (m_callback){
+		m_callback->Debug("video_fmt.fmt.pix.width = %d, video_fmt.fmt.pix.height = %d.", video_fmt.fmt.pix.width, video_fmt.fmt.pix.height);
+	}
 
 	if (video_fmt.fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV)
 		m_captureVideoType = VideoType::kYUY2;
@@ -627,13 +652,17 @@ int VideoCaptureImpl::StartVideoCapture()
 
 	// set format and frame size now
 	if (ioctl(m_deviceFd, VIDIOC_S_FMT, &video_fmt) < 0) {
-		m_callback->Debug("error in VIDIOC_S_FMT for %s.", strerror(errno));
+		if (m_callback){
+			m_callback->Debug("error in VIDIOC_S_FMT for %s.", strerror(errno));
+		}
 		return -1;
 	}
 	else
 	{
 		if (ioctl(m_deviceFd, VIDIOC_G_FMT, &video_fmt) < 0){
-			m_callback->Debug("error in VIDIOC_G_FMT for %s.", strerror(errno));
+			if (m_callback){
+				m_callback->Debug("error in VIDIOC_G_FMT for %s.", strerror(errno));
+			}
 			return -1;
 		}
 		else
@@ -641,7 +670,9 @@ int VideoCaptureImpl::StartVideoCapture()
 			// initialize current width and height
 			m_capture_width = video_fmt.fmt.pix.width;
 			m_capture_height = video_fmt.fmt.pix.height;
-			m_callback->Debug("real camera capture m_capture_width = %d, m_capture_height = %d.", m_capture_width, m_capture_height);
+			if (m_callback){
+				m_callback->Debug("real camera capture m_capture_width = %d, m_capture_height = %d.", m_capture_width, m_capture_height);
+			}
 		}
 	}
 
@@ -651,7 +682,9 @@ int VideoCaptureImpl::StartVideoCapture()
 	memset(&streamparms, 0, sizeof(streamparms));
 	streamparms.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
 	if (ioctl(m_deviceFd, VIDIOC_G_PARM, &streamparms) < 0) {
-		m_callback->Debug("error in VIDIOC_G_PARM,and error info is %s.", strerror(errno));
+		if (m_callback){
+			m_callback->Debug("error in VIDIOC_G_PARM,and error info is %s.", strerror(errno));
+		}
 		driver_framerate_support = false;
 		// continue
 	}
@@ -663,16 +696,26 @@ int VideoCaptureImpl::StartVideoCapture()
 			streamparms.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
 			streamparms.parm.capture.timeperframe.numerator = 1;
 			streamparms.parm.capture.timeperframe.denominator = (int32_t)m_capture->param.fps;
-			m_callback->Debug("Set Camera video capture timeperframe numerator is %d, denominator is %d.", streamparms.parm.capture.timeperframe.numerator, streamparms.parm.capture.timeperframe.denominator);
+			if (m_callback){
+				m_callback->Debug("Set Camera video capture timeperframe numerator is %d, denominator is %d.", streamparms.parm.capture.timeperframe.numerator, streamparms.parm.capture.timeperframe.denominator);
+			}
+			
 			if (ioctl(m_deviceFd, VIDIOC_S_PARM, &streamparms) < 0) {
-				m_callback->Debug("Failed to set the framerate. error info is %s.", strerror(errno));
+				if (m_callback){
+					m_callback->Debug("Failed to set the framerate. error info is %s.", strerror(errno));
+				}
 				driver_framerate_support = false;
 			}
 			else {
 				m_currentFrameRate = (int32_t)m_capture->param.fps;
-				m_callback->Debug("Set Camera video capture rate to %d, and numerator is %d, denominator is %d.", m_currentFrameRate, streamparms.parm.capture.timeperframe.numerator, streamparms.parm.capture.timeperframe.denominator);
+				if (m_callback){
+					m_callback->Debug("Set Camera video capture rate to %d, and numerator is %d, denominator is %d.", m_currentFrameRate, streamparms.parm.capture.timeperframe.numerator, streamparms.parm.capture.timeperframe.denominator);
+				}
+				
 				if (ioctl(m_deviceFd, VIDIOC_G_PARM, &streamparms) == 0) {
-					m_callback->Debug("Get video capture numerator is %d, denominator is %d.", streamparms.parm.capture.timeperframe.numerator, streamparms.parm.capture.timeperframe.denominator);
+					if (m_callback){
+						m_callback->Debug("Get video capture numerator is %d, denominator is %d.", streamparms.parm.capture.timeperframe.numerator, streamparms.parm.capture.timeperframe.denominator);
+					}
 				}
 			}
 		}
@@ -686,7 +729,9 @@ int VideoCaptureImpl::StartVideoCapture()
 		}
 		else {
 			m_currentFrameRate = 5;
-			m_callback->Debug("The Camera not support set video capture framerate, set capture rate to %d.", m_currentFrameRate);
+			if (m_callback){
+				m_callback->Debug("The Camera not support set video capture framerate, set capture rate to %d.", m_currentFrameRate);
+			}
 		}
 	}
 
@@ -695,15 +740,21 @@ int VideoCaptureImpl::StartVideoCapture()
 	}
 
 	if (!AllocateVideoBuffers()) {
-		m_callback->Debug("failed to allocate video capture buffers");
+		if (m_callback){
+			m_callback->Debug("failed to allocate video capture buffers");
+		}
 		return -1;
 	}
 	else{
-		m_callback->Debug("allocate video capture buffers success!");
+		if (m_callback){
+			m_callback->Debug("allocate video capture buffers success!");
+		}
 	}
 
 	if (-1 == pthread_create(&m_CaptureThreadId, NULL, VideoCaptureProcess, this)) {
-		m_callback->Debug("Create Video Capture Thread Failed!");
+		if (m_callback){
+			m_callback->Debug("Create Video Capture Thread Failed!");
+		}
 		return -1;
 	}
 
@@ -711,7 +762,9 @@ int VideoCaptureImpl::StartVideoCapture()
 	enum v4l2_buf_type type;
 	type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
 	if (ioctl(m_deviceFd, VIDIOC_STREAMON, &type) == -1) {
-		m_callback->Debug("failed to turn on stream for %s.", strerror(errno));
+		if (m_callback){
+			m_callback->Debug("failed to turn on stream for %s.", strerror(errno));
+		}
 		return -1;
 	}
 
@@ -734,7 +787,9 @@ bool VideoCaptureImpl::AllocateVideoBuffers()
 
 	//向设备申请缓冲区
 	if (ioctl(m_deviceFd, VIDIOC_REQBUFS, &rbuffer) < 0){
-		m_callback->Debug("Could not get buffers from device for %s.", strerror(errno));
+		if (m_callback){
+			m_callback->Debug("Could not get buffers from device for %s.", strerror(errno));
+		}
 		return false;
 	}
 
@@ -790,7 +845,9 @@ bool VideoCaptureImpl::DeAllocateVideoBuffers()
 	enum v4l2_buf_type type;
 	type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
 	if (ioctl(m_deviceFd, VIDIOC_STREAMOFF, &type) < 0){
-		m_callback->Debug("VIDIOC_STREAMOFF error. error no: %d", errno);
+		if (m_callback){
+			m_callback->Debug("VIDIOC_STREAMOFF error. error no: %d", errno);
+		}
 	}
 
 	return true;
@@ -839,10 +896,14 @@ int VideoCaptureImpl::StopVideoCapture()
 		m_bStopCapture = true;
 
 		if (0 == pthread_join(m_CaptureThreadId, NULL))		{
-			m_callback->Debug("thread join video capture thread success.");
+			if (m_callback){
+				m_callback->Debug("thread join video capture thread success.");
+			}
 		}
 		else {
-			m_callback->Debug("thread join video capture thread failed for %s.", strerror(errno));
+			if (m_callback){
+				m_callback->Debug("thread join video capture thread failed for %s.", strerror(errno));
+			}
 		}
 
 		DeAllocateVideoBuffers();
@@ -865,7 +926,9 @@ int VideoCaptureImpl::GetCamBrightness(int* ibright)
 	struct v4l2_control ctrl;
 	ctrl.id = V4L2_CID_BRIGHTNESS;
 	if (ioctl(m_deviceFd,VIDIOC_G_CTRL,&ctrl) == -1){
-		m_callback->Debug("VIDIOC_S_CTRL get V4L2_CID_BRIGHTNESS error for %s", strerror(errno));
+		if (m_callback){
+			m_callback->Debug("VIDIOC_S_CTRL get V4L2_CID_BRIGHTNESS error for %s", strerror(errno));
+		}
 	}
 	else {
 		//m_callback->Debug("VIDIOC_S_CTRL get V4L2_CID_BRIGHTNESS success and brightness is %d", ctrl.value);
@@ -883,7 +946,9 @@ int VideoCaptureImpl::SetCamBrightness(int ibright)
 	ctrl.id = V4L2_CID_BRIGHTNESS;
 	ctrl.value = TransToRealBrightnessValue(ibright);
 	if (ioctl(m_deviceFd, VIDIOC_S_CTRL, &ctrl) == -1){
-		m_callback->Debug("VIDIOC_S_CTRL set V4L2_CID_BRIGHTNESS error for %s", strerror(errno));
+		if (m_callback){
+			m_callback->Debug("VIDIOC_S_CTRL set V4L2_CID_BRIGHTNESS error for %s", strerror(errno));
+		}
 	}
 	else{
 		//m_callback->Debug("VIDIOC_S_CTRL set V4L2_CID_BRIGHTNESS success %d", ctrl.value);
@@ -920,10 +985,14 @@ bool VideoCaptureImpl::GetCamBrightnessInfo()
 	struct v4l2_queryctrl qctrl;
 	qctrl.id = V4L2_CID_BRIGHTNESS;
 	if (ioctl(m_deviceFd, VIDIOC_QUERYCTRL, &qctrl) == -1) {
-		m_callback->Debug("VIDIOC_QUERYCTRL get V4L2_CID_BRIGHTNESS error for %s", strerror(errno));
+		if (m_callback){
+			m_callback->Debug("VIDIOC_QUERYCTRL get V4L2_CID_BRIGHTNESS error for %s", strerror(errno));
+		}
 	}
 	else {
-		m_callback->Debug("VIDIOC_QUERYCTRL get V4L2_CID_BRIGHTNESS success {min(%d) - max(%d)},default is %d", qctrl.minimum, qctrl.maximum, qctrl.default_value);
+		if (m_callback){
+			m_callback->Debug("VIDIOC_QUERYCTRL get V4L2_CID_BRIGHTNESS success {min(%d) - max(%d)},default is %d", qctrl.minimum, qctrl.maximum, qctrl.default_value);
+		}
 		m_idefaultbrightness = qctrl.default_value;
 		m_iminbrightness = qctrl.minimum;
 		m_imaxbrightness = qctrl.maximum;