/****************************************************************************** Description.: this thread worker grabs a frame and copies it to the global buffer Input Value.: unused Return Value: unused, always NULL ******************************************************************************/ void *cam_thread(void *arg) { context *pcontext = arg; pglobal = pcontext->pglobal; /* set cleanup handler to cleanup allocated ressources */ pthread_cleanup_push(cam_cleanup, pcontext); while(!pglobal->stop) { while(pcontext->videoIn->streamingState == STREAMING_PAUSED) { usleep(1); // maybe not the best way so FIXME } if(stop_camera == 1) { /* check active outputs */ pthread_mutex_lock(&pglobal->in[pcontext->id].out); if(pglobal->in[pcontext->id].num_outs == 0) { /* stop camera */ uvcStopGrab(pcontext->videoIn); /* wait for active outputs */ pthread_cond_wait(&pglobal->in[pcontext->id].out_update, &pglobal->in[pcontext->id].out); } /* allow others to access the global buffer again */ pthread_mutex_unlock(&pglobal->in[pcontext->id].out); } /* grab a frame */ if(uvcGrab(pcontext->videoIn) < 0) { IPRINT("Error grabbing frames\n"); exit(EXIT_FAILURE); } DBG("received frame of size: %d from plugin: %d\n", pcontext->videoIn->buf.bytesused, pcontext->id); /* * Workaround for broken, corrupted frames: * Under low light conditions corrupted frames may get captured. * The good thing is such frames are quite small compared to the regular pictures. * For example a VGA (640x480) webcam picture is normally >= 8kByte large, * corrupted frames are smaller. */ if(pcontext->videoIn->buf.bytesused < minimum_size) { DBG("dropping too small frame, assuming it as broken\n"); continue; } /* copy JPG picture to global buffer */ pthread_mutex_lock(&pglobal->in[pcontext->id].db); /* * If capturing in YUV mode convert to JPEG now. * This compression requires many CPU cycles, so try to avoid YUV format. * Getting JPEGs straight from the webcam, is one of the major advantages of * Linux-UVC compatible devices. */ if(pcontext->videoIn->formatIn == V4L2_PIX_FMT_YUYV) { DBG("compressing frame from input: %d\n", (int)pcontext->id); pglobal->in[pcontext->id].size = compress_yuyv_to_jpeg(pcontext->videoIn, pglobal->in[pcontext->id].buf, pcontext->videoIn->framesizeIn, gquality); } else { DBG("copying frame from input: %d\n", (int)pcontext->id); pglobal->in[pcontext->id].size = memcpy_picture(pglobal->in[pcontext->id].buf, pcontext->videoIn->tmpbuffer, pcontext->videoIn->buf.bytesused); } #if 0 /* motion detection can be done just by comparing the picture size, but it is not very accurate!! */ if((prev_size - global->size)*(prev_size - global->size) > 4 * 1024 * 1024) { DBG("motion detected (delta: %d kB)\n", (prev_size - global->size) / 1024); } prev_size = global->size; #endif /* copy this frame's timestamp to user space */ pglobal->in[pcontext->id].timestamp = pcontext->videoIn->buf.timestamp; /* signal fresh_frame */ pthread_cond_broadcast(&pglobal->in[pcontext->id].db_update); pthread_mutex_unlock(&pglobal->in[pcontext->id].db); /* only use usleep if the fps is below 5, otherwise the overhead is too long */ if(pcontext->videoIn->fps < 5) { DBG("waiting for next frame for %d us\n", 1000 * 1000 / pcontext->videoIn->fps); usleep(1000 * 1000 / pcontext->videoIn->fps); } else { DBG("waiting for next frame\n"); } } DBG("leaving input thread, calling cleanup function now\n"); pthread_cleanup_pop(1); return NULL; }
/****************************************************************************** Description.: this thread worker grabs a frame and copies it to the global buffer Input Value.: unused Return Value: unused, always NULL ******************************************************************************/ void *cam_thread( void *arg ) { /* set cleanup handler to cleanup allocated ressources */ pthread_cleanup_push(cam_cleanup, NULL); while( !pglobal->stop ) { /* grab a frame */ if( uvcGrab(videoIn) < 0 ) { IPRINT("Error grabbing frames\n"); exit(EXIT_FAILURE); } DBG("received frame of size: %d\n", videoIn->buf.bytesused); /* * Workaround for broken, corrupted frames: * Under low light conditions corrupted frames may get captured. * The good thing is such frames are quite small compared to the regular pictures. * For example a VGA (640x480) webcam picture is normally >= 8kByte large, * corrupted frames are smaller. */ if ( videoIn->buf.bytesused < minimum_size ) { DBG("dropping too small frame, assuming it as broken\n"); continue; } /* copy JPG picture to global buffer */ pthread_mutex_lock( &pglobal->db ); /* * If capturing in YUV mode convert to JPEG now. * This compression requires many CPU cycles, so try to avoid YUV format. * Getting JPEGs straight from the webcam, is one of the major advantages of * Linux-UVC compatible devices. */ if (videoIn->formatIn != V4L2_PIX_FMT_MJPEG ) { DBG("compressing frame\n"); pglobal->size = compress_yuyv_to_jpeg(videoIn, pglobal->buf, videoIn->framesizeIn, gquality,videoIn->fmt.fmt.pix.pixelformat); } else { DBG("copying frame\n"); pglobal->size = memcpy_picture(pglobal->buf, videoIn->tmpbuffer, videoIn->buf.bytesused); } #if 0 /* motion detection can be done just by comparing the picture size, but it is not very accurate!! */ if ( (prev_size - global->size)*(prev_size - global->size) > 4*1024*1024 ) { DBG("motion detected (delta: %d kB)\n", (prev_size - global->size) / 1024); } prev_size = global->size; #endif /* signal fresh_frame */ pthread_cond_broadcast(&pglobal->db_update); pthread_mutex_unlock( &pglobal->db ); DBG("waiting for next frame\n"); /* only use usleep if the fps is below 5, otherwise the overhead is too long */ if ( videoIn->fps < 5 ) { usleep(1000*1000/videoIn->fps); } } DBG("leaving input thread, calling cleanup function now\n"); pthread_cleanup_pop(1); return NULL; }
/****************************************************************************** Description.: this thread worker grabs a frame and copies it to the global buffer Input Value.: unused Return Value: unused, always NULL ******************************************************************************/ void *cam_thread(void *arg) { g_settings.init(); setCameraExposure(); CVideoFrame* pFrame = NULL; #ifndef TEST_USE_JPEGS_NOT_CAMERA int width = VIEW_PIXEL_X_WIDTH; int height = VIEW_PIXEL_Y_HEIGHT; IplImage * img = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3); // obraz OpenCV #endif frameGrinder.init(); #ifdef TEST_USE_JPEGS_NOT_CAMERA std::string sBasePath = "/home/"; sBasePath += HOME_NAME; std::string sPath = sBasePath; sPath += "/0243-20150125-22-21-46.jpg"; //sPath += "/0007-20150125-22-36-25.jpg"; cv::Mat frame1 = cv::imread(sPath.c_str(), CV_LOAD_IMAGE_COLOR); if (frame1.empty()) { dbgMsg_s("Failed to read image data from a file1\n"); } sPath = sBasePath; sPath += "/0243-20150125-22-21-46.jpg"; //sPath += "/0007-20150125-22-36-25.jpg"; cv::Mat frame2 = cv::imread(sPath.c_str(), CV_LOAD_IMAGE_COLOR); if (frame2.empty()) { dbgMsg_s("Failed to read image data from a file2\n"); } bool toggle = false; #endif context *pcontext = (context*) arg; pglobal = pcontext->pglobal; /* set cleanup handler to cleanup allocated ressources */ pthread_cleanup_push(cam_cleanup, pcontext); while (!pglobal->stop) { while (pcontext->videoIn->streamingState == STREAMING_PAUSED) { usleep(1); // maybe not the best way so FIXME } #ifdef TEST_USE_JPEGS_NOT_CAMERA if (frameGrinder.safeGetFreeFrame(&pFrame)) { if (toggle) { pFrame->m_frame = frame1; } else { pFrame->m_frame = frame2; } toggle = (!toggle); if (!pFrame->m_frame.empty()) { frameGrinder.safeAddTail(pFrame, CVideoFrame::FRAME_QUEUE_WAIT_FOR_BLOB_DETECT); } else { dbgMsg_s("Frame is empty\n"); frameGrinder.safeAddTail(pFrame, CVideoFrame::FRAME_QUEUE_FREE); } frameGrinder.m_testMonitor.m_nTasksDone[CTestMonitor::TASK_DONE_CAMERA]++; } #else /* grab a frame */ if (uvcGrab(pcontext->videoIn) < 0) { IPRINT("Error grabbing frames\n"); exit(EXIT_FAILURE); } DBG("received frame of size: %d from plugin: %d\n", pcontext->videoIn->buf.bytesused, pcontext->id); /* * Workaround for broken, corrupted frames: * Under low light conditions corrupted frames may get captured. * The good thing is such frames are quite small compared to the regular pictures. * For example a VGA (640x480) webcam picture is normally >= 8kByte large, * corrupted frames are smaller. */ if (pcontext->videoIn->buf.bytesused < minimum_size) { DBG("dropping too small frame, assuming it as broken\n"); continue; } if (g_settings.isDynamicSettingsEnabled()) { g_settings.getValueFromFile(CSetting::SETTING_EXPOSURE); } if(g_settings.isValueChanged(CSetting::SETTING_EXPOSURE)) { setCameraExposure(); } #ifdef NO_CV_JUST_STREAM_THE_CAMERA /* copy JPG picture to global buffer */ pthread_mutex_lock(&pglobal->in[pcontext->id].db); /* * If capturing in YUV mode convert to JPEG now. * This compression requires many CPU cycles, so try to avoid YUV format. * Getting JPEGs straight from the webcam, is one of the major advantages of * Linux-UVC compatible devices. */ if (pcontext->videoIn->formatIn == V4L2_PIX_FMT_YUYV) { DBG("compressing frame from input: %d\n", (int) pcontext->id); pglobal->in[pcontext->id].size = compress_yuyv_to_jpeg(pcontext->videoIn, pglobal->in[pcontext->id].buf, pcontext->videoIn->framesizeIn, gquality); } else { DBG("copying frame from input: %d\n", (int) pcontext->id); pglobal->in[pcontext->id].size = memcpy_picture(pglobal->in[pcontext->id].buf, pcontext->videoIn->tmpbuffer, pcontext->videoIn->buf.bytesused); } /* copy this frame's timestamp to user space */ pglobal->in[pcontext->id].timestamp = pcontext->videoIn->buf.timestamp; /* signal fresh_frame */ pthread_cond_broadcast(&pglobal->in[pcontext->id].db_update); pthread_mutex_unlock(&pglobal->in[pcontext->id].db); #else // #ifndef NO_CV_JUST_STREAM_THE_CAMERA if (frameGrinder.safeGetFreeFrame(&pFrame)) { std::vector<uchar> vectordata(pcontext->videoIn->tmpbuffer, pcontext->videoIn->tmpbuffer + (height * width)); cv::Mat data_mat(vectordata, false); cv::Mat image(cv::imdecode(data_mat, 1)); //put 0 if you want greyscale pFrame->m_frame = image; if (!pFrame->m_frame.empty()) { frameGrinder.safeAddTail(pFrame, CVideoFrame::FRAME_QUEUE_WAIT_FOR_BLOB_DETECT); } else { dbgMsg_s("Frame is empty\n"); frameGrinder.safeAddTail(pFrame, CVideoFrame::FRAME_QUEUE_FREE); } frameGrinder.m_testMonitor.m_nTasksDone[CTestMonitor::TASK_DONE_CAMERA]++; } #endif // #ifndef NO_CV_JUST_STREAM_THE_CAMERA #endif // TEST_USE_JPEGS_NOT_CAMERA } DBG("leaving input thread, calling cleanup function now\n"); pthread_cleanup_pop(1); return NULL; }
/****************************************************************************** Description.: this thread worker grabs a frame and copies it to the global buffer Input Value.: unused Return Value: unused, always NULL ******************************************************************************/ void *cam_thread(void *arg) { int cid = 0, id = (int)arg; context *activecam, *pcontext = &cams[id]; pglobal = pcontext->pglobal; activecam = pcontext; /* set cleanup handler to cleanup allocated ressources */ pthread_cleanup_push(cam_cleanup, pcontext); while(!pglobal->stop) { if(cid != camera) { video_pause(activecam->videoIn); cid = camera; activecam = &cams[id + cid]; IPRINT("Switch to camera..: %s\n", mdev[cid]); video_unpause(activecam->videoIn); } while(activecam->videoIn->streamingState == STREAMING_PAUSED) { usleep(1); // maybe not the best way so FIXME } /* grab a frame */ if(uvcGrab(activecam->videoIn) < 0) { IPRINT("Error grabbing frames\n"); exit(EXIT_FAILURE); } DBG("received frame of size: %d from plugin: %d\n", activecam->videoIn->buf.bytesused, activecam->id); /* * Workaround for broken, corrupted frames: * Under low light conditions corrupted frames may get captured. * The good thing is such frames are quite small compared to the regular pictures. * For example a VGA (640x480) webcam picture is normally >= 8kByte large, * corrupted frames are smaller. */ if(activecam->videoIn->buf.bytesused < minimum_size) { DBG("dropping too small frame, assuming it as broken\n"); continue; } /* copy JPG picture to global buffer */ pthread_mutex_lock(&pglobal->in[pcontext->id].db); /* * If capturing in YUV mode convert to JPEG now. * This compression requires many CPU cycles, so try to avoid YUV format. * Getting JPEGs straight from the webcam, is one of the major advantages of * Linux-UVC compatible devices. */ if(activecam->videoIn->formatIn == V4L2_PIX_FMT_YUYV) { DBG("compressing frame from input: %d\n", (int)activecam->id); pglobal->in[pcontext->id].size = compress_yuyv_to_jpeg(activecam->videoIn, pglobal->in[pcontext->id].buf, activecam->videoIn->framesizeIn, gquality); } else { DBG("copying frame from input: %d\n", (int)activecam->id); pglobal->in[pcontext->id].size = memcpy_picture(pglobal->in[pcontext->id].buf, activecam->videoIn->tmpbuffer, activecam->videoIn->buf.bytesused); } /* copy this frame's timestamp to user space */ pglobal->in[pcontext->id].timestamp = activecam->videoIn->buf.timestamp; /* signal fresh_frame */ pthread_cond_broadcast(&pglobal->in[pcontext->id].db_update); pthread_mutex_unlock(&pglobal->in[pcontext->id].db); /* only use usleep if the fps is below 5, otherwise the overhead is too long */ if(activecam->videoIn->fps < 5) { DBG("waiting for next frame for %d us\n", 1000 * 1000 / activecam->videoIn->fps); usleep(1000 * 1000 / activecam->videoIn->fps); } else { DBG("waiting for next frame\n"); } } DBG("leaving input thread, calling cleanup function now\n"); pthread_cleanup_pop(1); return NULL; }
/****************************************************************************** Description.: this thread worker grabs a frame and copies it to the global buffer Input Value.: unused Return Value: unused, always NULL ******************************************************************************/ void *cam_thread(void *arg) { input *in = (input*)arg; context *pcontext = in->context; unsigned int every_count = 0; DBG("Enter\n"); /* set cleanup handler to cleanup allocated resources */ pthread_cleanup_push(cam_cleanup, in); while(!stop) { while(pcontext->videoIn->streamingState == STREAMING_PAUSED) { usleep(1); // maybe not the best way so FIXME } /* grab a frame */ if(uvcGrab(pcontext->videoIn) < 0) { LOG("Error grabbing frames\n"); exit(EXIT_FAILURE); } if ( every_count < every - 1 ) { DBG("dropping %d frame for every=%d\n", every_count + 1, every); ++every_count; continue; } else { every_count = 0; } /* * Workaround for broken, corrupted frames: * Under low light conditions corrupted frames may get captured. * The good thing is such frames are quite small compared to the regular pictures. * For example a VGA (640x480) webcam picture is normally >= 8kByte large, * corrupted frames are smaller. */ if(pcontext->videoIn->tmpbytesused < minimum_size) { DBG("dropping too small frame, assuming it as broken\n"); continue; } // use software frame dropping on low fps if (pcontext->videoIn->soft_framedrop == 1) { unsigned long last = in->timestamp.tv_sec * 1000 + (in->timestamp.tv_usec/1000); // convert to ms unsigned long current = pcontext->videoIn->buf.timestamp.tv_sec * 1000 + pcontext->videoIn->buf.timestamp.tv_usec/1000; // convert to ms // if the requested time did not esplashed skip the frame if ((current - last) < pcontext->videoIn->frame_period_time) { //DBG("Last frame taken %d ms ago so drop it\n", (current - last)); continue; } DBG("Lagg: %ld\n", (current - last) - pcontext->videoIn->frame_period_time); } /* copy JPG picture to global buffer */ pthread_mutex_lock(&in->db); /* * If capturing in YUV mode convert to JPEG now. * This compression requires many CPU cycles, so try to avoid YUV format. * Getting JPEGs straight from the webcam, is one of the major advantages of * Linux-UVC compatible devices. */ DBG("copying frame from input:\n"); in->size = memcpy_picture(in->buf, pcontext->videoIn->tmpbuffer, pcontext->videoIn->tmpbytesused); /* copy this frame's timestamp to user space */ in->timestamp = pcontext->videoIn->tmptimestamp; /* signal fresh_frame */ pthread_cond_broadcast(&in->db_update); pthread_mutex_unlock(&in->db); } DBG("leaving input thread, calling cleanup function now\n"); pthread_cleanup_pop(1); return NULL; }