void ImperxStream::Stream(unsigned char *frame, Semaphore &frame_semaphore, Flag &stream_flag) { // The pipeline is already "armed", we just have to tell the device // to start sending us images printf( "Sending StartAcquisition command to device\n" ); lDeviceParams->ExecuteCommand( "AcquisitionStart" ); char lDoodle[] = "|\\-|-/"; int lDoodleIndex = 0; PvInt64 lImageCountVal = 0; double lFrameRateVal = 0.0; double lBandwidthVal = 0.0; // Acquire images until the user instructs us to stop printf( "\n<press a key to stop streaming>\n" ); while ( stream_flag.check() ) { std::cout << "here\n"; // Retrieve next buffer PvBuffer *lBuffer = NULL; PvResult lOperationResult; PvResult lResult = lPipeline.RetrieveNextBuffer( &lBuffer, 1000, &lOperationResult ); if ( lResult.IsOK() ) { if ( lOperationResult.IsOK() ) { // Process Buffer lStreamParams->GetIntegerValue( "ImagesCount", lImageCountVal ); lStreamParams->GetFloatValue( "AcquisitionRateAverage", lFrameRateVal ); lStreamParams->GetFloatValue( "BandwidthAverage", lBandwidthVal ); // If the buffer contains an image, display width and height PvUInt32 lWidth = 0, lHeight = 0; if ( lBuffer->GetPayloadType() == PvPayloadTypeImage ) { // Get image specific buffer interface PvImage *lImage = lBuffer->GetImage(); // Read width, height lWidth = lBuffer->GetImage()->GetWidth(); lHeight = lBuffer->GetImage()->GetHeight(); stream_flag.raise(); } std::cout << lWidth << " " << lHeight << "\n"; } // We have an image - do some processing (...) and VERY IMPORTANT, // release the buffer back to the pipeline //semaphore thing //get all in there. //a semaphore thing lPipeline.ReleaseBuffer( lBuffer ); } else { // Timeout printf( "%c Timeout\r", lDoodle[ lDoodleIndex ] ); } ++lDoodleIndex %= 6; } }
void *CameraStreamThread( void * threadargs) { long tid = (long)((struct Thread_data *)threadargs)->thread_id; printf("CameraStream thread #%ld!\n", tid); ImperxStream camera; cv::Mat localFrame; timespec preExposure, postExposure, timeElapsed, duration; int width, height; int failcount = 0; uint16_t localExposure = exposure; int16_t localPreampGain = preampGain; uint16_t localAnalogGain = analogGain; cameraReady = 0; staleFrame = true; while(1) { if (stop_message[tid] == 1) { printf("CameraStream thread #%ld exiting\n", tid); camera.Stop(); camera.Disconnect(); started[tid] = false; pthread_exit( NULL ); } else if (cameraReady == false) { if (camera.Connect() != 0) { std::cout << "Error connecting to camera!\n"; sleep(SLEEP_CAMERA_CONNECT); continue; } else { camera.ConfigureSnap(); camera.SetROISize(CAMERA_XSIZE,CAMERA_YSIZE); camera.SetROIOffset(CAMERA_XOFFSET,CAMERA_YOFFSET); camera.SetExposure(localExposure); camera.SetAnalogGain(localAnalogGain); camera.SetPreAmpGain(localPreampGain); width = camera.GetROIWidth(); height = camera.GetROIHeight(); localFrame.create(height, width, CV_8UC1); if(camera.Initialize() != 0) { std::cout << "Error initializing camera!\n"; //may need disconnect here sleep(SLEEP_CAMERA_CONNECT); continue; } cameraReady = 1; frameCount = 0; } } else { if (localExposure != exposure) { localExposure = exposure; camera.SetExposure(localExposure); } if (localPreampGain != preampGain) { localPreampGain = preampGain; camera.SetPreAmpGain(localPreampGain); } if (localAnalogGain != analogGain) { localAnalogGain = analogGain; camera.SetAnalogGain(analogGain); } clock_gettime(CLOCK_REALTIME, &preExposure); if(!camera.Snap(localFrame)) { failcount = 0; procReady.raise(); saveReady.raise(); //printf("CameraStreamThread: trying to lock\n"); pthread_mutex_lock(&mutexImage); //printf("CameraStreamThread: got lock, copying over\n"); localFrame.copyTo(frame); frameTime = preExposure; //printf("%d\n", frame.at<uint8_t>(0,0)); frameCount++; pthread_mutex_unlock(&mutexImage); staleFrame = false; //printf("camera temp is %lld\n", camera.getTemperature()); camera_temperature = camera.getTemperature(); } else { failcount++; std::cout << "Frame failure count = " << failcount << std::endl; if (failcount >= 10) { camera.Stop(); camera.Disconnect(); cameraReady = false; staleFrame = true; std::cout << "*********************** RESETTING CAMERA ***********************************" << std::endl; continue; } } clock_gettime(CLOCK_REALTIME, &postExposure); timeElapsed = TimespecDiff(preExposure, postExposure); duration.tv_sec = frameRate.tv_sec - timeElapsed.tv_sec; duration.tv_nsec = frameRate.tv_nsec - timeElapsed.tv_nsec; // std::cout << timeElapsed.tv_sec << " " << timeElapsed.tv_nsec << "\n"; nanosleep(&duration, NULL); } } }