AS3_Val destroyCamera( void* self, AS3_Val args ) { Camera * camera = AS3_PtrValue(args); camera_destroy(&camera); return 0; }
static void _main_view_destroy(void *data, Evas *e, Evas_Object *obj, void *event_info) { main_view *main_view_data = data; _main_view_stop_camera_preview(main_view_data->camera); camera_destroy(main_view_data->camera); INF("in _main_view_destroy function"); free(data); }
Result camera_create(int32_t const i_deviceID, PixelFormat const i_pixelFormat, uint32_t const i_sizeX, uint32_t const i_sizeY, Camera ** const o_cameraHandle) { struct v4l2_buffer buffer; size_t bufferIndex=0; void *bufferMap=NULL; struct v4l2_requestbuffers bufferRequest; Camera *cameraHandle = NULL; struct v4l2_capability cap; struct v4l2_control currentControl; char devicePathname[PATH_MAX]; uint32_t devicePixelFormat = 0; uint32_t deviceSizeX = 0; uint32_t deviceSizeY = 0; Result result=R_FAILURE; int xioResult=-1; /***** Input Validation *****/ if(i_sizeX == 0 || i_sizeY == 0) { CARL_ERROR("Frame size must be non-0 for both dimensions."); result = R_INPUTBAD; goto end; } /***** Create camera structure *****/ cameraHandle = (Camera*) malloc(sizeof(Camera)); if(cameraHandle == NULL) { CARL_ERROR("Unable to allocate memory."); result = R_MEMORYALLOCATIONERROR; goto end; } /***** Clear fields *****/ cameraHandle->m_buffers = NULL; cameraHandle->m_bufferCount = 0; cameraHandle->m_bufferCountMax = 0; cameraHandle->m_deviceHandle = -1; CLEAR(cameraHandle->m_format); /***** Generate camera path *****/ if(i_deviceID < 0) { CARL_ERROR("Device ID must be > 0."); result = R_INPUTBAD; goto end; } snprintf(devicePathname, sizeof(devicePathname), DEVICE_PATH_PRINTF, i_deviceID); /***** Open camera device *****/ cameraHandle->m_deviceHandle = open(devicePathname, O_RDWR | O_NONBLOCK, 0); if(cameraHandle->m_deviceHandle < 0) { CARL_ERROR("Unable to open device - \"%s\"", strerror(errno)); result = R_DEVICEOPENFAILED; goto end; } /***** Query capabilities *****/ CLEAR(cap); xioResult = xioctl(cameraHandle->m_deviceHandle, VIDIOC_QUERYCAP, &cap); if(xioResult == -1) { CARL_ERROR("Not a video capture device - \"%s\"", strerror(errno)); result = R_DEVICEINVALID; goto end; } if(!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) { CARL_ERROR("Device cannot capture video."); result = R_DEVICENOVIDEOCAP; goto end; } if(!(cap.capabilities & V4L2_CAP_STREAMING)) { CARL_ERROR("Device cannot stream."); result = R_DEVICENOSTREAMCAP; goto end; } /***** Prepare user desires *****/ deviceSizeX = i_sizeX; deviceSizeY = i_sizeY; switch(i_pixelFormat) { case CAMERA_PIXELFORMAT_MJPEG: devicePixelFormat = V4L2_PIX_FMT_MJPEG; break; case CAMERA_PIXELFORMAT_UYVY: devicePixelFormat = V4L2_PIX_FMT_UYVY; break; case CAMERA_PIXELFORMAT_YUYV: devicePixelFormat = V4L2_PIX_FMT_YUYV; break; default: CARL_ERROR("Unsupported pixel format %d specified", i_pixelFormat); result = R_INPUTBAD; goto end; } /***** Stuff user desires into struct *****/ CLEAR(cameraHandle->m_format); cameraHandle->m_format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; cameraHandle->m_format.fmt.pix.width = i_sizeX; cameraHandle->m_format.fmt.pix.height = i_sizeY; cameraHandle->m_format.fmt.pix.pixelformat = devicePixelFormat; cameraHandle->m_format.fmt.pix.field = DEVICE_FIELD; /***** Apply camera attributes *****/ xioResult = xioctl(cameraHandle->m_deviceHandle, VIDIOC_S_FMT, &(cameraHandle->m_format)); if(xioResult == -1) { CARL_ERROR("Attribute application failed - \"%s\"", strerror(errno)); result = R_DEVICEATTRIBUTESETFAILED; goto end; } /***** Check camera attributes *****/ if(cameraHandle->m_format.fmt.pix.pixelformat != devicePixelFormat) { CARL_ERROR("Driver set different pixel format (%u)", cameraHandle->m_format.fmt.pix.pixelformat); result = R_DEVICEPIXELFORMATFAILED; goto end; } if(cameraHandle->m_format.fmt.pix.width != deviceSizeX || cameraHandle->m_format.fmt.pix.height != deviceSizeY) { CARL_ERROR("Driver set different resolution (%u x %u)", cameraHandle->m_format.fmt.pix.width, cameraHandle->m_format.fmt.pix.height); result = R_DEVICERESOLUTIONFAILED; goto end; } /***** Apply camera stream parameters *****/ /* CLEAR(cameraHandle->m_parameters); cameraHandle->m_parameters.capturemode |= V4L2_MODE_HIGHQUALITY; xioResult = xioctl(cameraHandle->m_deviceHandle, VIDIOC_S_PARM, &(cameraHandle->m_parameters)); if(xioResult == -1) { fprintf(stderr, "%s: camera_create() - Parameter application failed - %s\n", g_programName, strerror(errno)); result = R_DEVICEPARAMETERSETFAILED; goto end; } if(!(cameraHandle->m_parameters.capturemode & V4L2_MODE_HIGHQUALITY)) { fprintf(stderr, "%s: camera_create() - Driver did not set high quality mode\n", g_programName); result = R_DEVICEHIGHQUALITYFAILED; goto end; } */ /***** Apply camera priority *****/ /* cameraHandle->m_priority = DEVICE_PRIORITY; xioResult = xioctl(cameraHandle->m_deviceHandle, VIDIOC_S_PRIORITY, &cameraHandle->m_priority); if(xioResult == -1) { if(errno == EBUSY) { fprintf(stderr, "%s: camera_create() - Another file handle alreaddy has priority - %s\n", g_programName, strerror(errno)); } else { fprintf(stderr, "%s: camera_create() - Priority application failed - %s\n", g_programName, strerror(errno)); } result = R_DEVICEPRIORITYSETFAILED; goto end; } if(cameraHandle->m_priority != DEVICE_PRIORITY) { fprintf(stderr, "%s: camera_create() - Driver set priority %d\n", g_programName, cameraHandle->m_priority); result = R_DEVICEPRIORITYFAILED; goto end; } */ /***** Apply camera controls *****/ #if 0 CLEAR(currentControl); currentControl.id = V4L2_CID_EXPOSURE_AUTO; currentControl.value = V4L2_EXPOSURE_MANUAL; xioResult = xioctl(cameraHandle->m_deviceHandle, VIDIOC_S_CTRL, ¤tControl); if(xioResult == -1) { CARL_ERROR("Unable to set shutter priority - \"%s\"", strerror(errno)); result = R_DEVICECONTROLSETFAILED; goto end; } CLEAR(currentControl); currentControl.id = V4L2_CID_EXPOSURE_ABSOLUTE; currentControl.value = 10000; xioResult = xioctl(cameraHandle->m_deviceHandle, VIDIOC_S_CTRL, ¤tControl); if(xioResult == -1) { CARL_ERROR("Unable to set exposure - \"%s\"", strerror(errno)); result = R_DEVICECONTROLSETFAILED; goto end; } xioResult = xioctl(cameraHandle->m_deviceHandle, VIDIOC_G_CTRL, ¤tControl); if(xioResult == -1) { CARL_ERROR("Unable to get exposure - \"%s\"", strerror(errno)); result = R_DEVICECONTROLSETFAILED; goto end; } if(currentControl.value != 9999) { fprintf(stderr, "lolnuts"); result = R_FAILURE; goto end; } #endif /***** Setup buffer request *****/ CLEAR(bufferRequest); bufferRequest.count = DEVICE_BUFFER_COUNT; bufferRequest.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; bufferRequest.memory = V4L2_MEMORY_MMAP; /***** Request buffers *****/ xioResult = xioctl(cameraHandle->m_deviceHandle, VIDIOC_REQBUFS, &bufferRequest); if(xioResult == -1) { CARL_ERROR("Buffer request failed - \"%s\"", strerror(errno)); result = R_BUFFERREQUESTFAILED; goto end; } /***** Create storage for buffer info *****/ cameraHandle->m_buffers = calloc(bufferRequest.count, sizeof(*(cameraHandle->m_buffers))); if(cameraHandle->m_buffers == NULL) { CARL_ERROR("Unable to allocate memory for buffer pointers."); result = R_MEMORYALLOCATIONERROR; goto end; } cameraHandle->m_bufferCountMax = bufferRequest.count; /***** Setup each buffer *****/ for(bufferIndex=0; bufferIndex<bufferRequest.count; ++bufferIndex) { CLEAR(buffer); buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; buffer.memory = V4L2_MEMORY_MMAP; buffer.index = cameraHandle->m_bufferCount; cameraHandle->m_buffers[bufferIndex].m_start = NULL; cameraHandle->m_buffers[bufferIndex].m_sizeBytes = 0; /***** Save buffer info *****/ xioResult = xioctl(cameraHandle->m_deviceHandle, VIDIOC_QUERYBUF, &buffer); if(xioResult == -1) { CARL_ERROR("Buffer query failed - \"%s\"", strerror(errno)); result = R_BUFFERQUERYFAILED; goto end; } /***** Map buffer into application space *****/ bufferMap = mmap(NULL, buffer.length, PROT_READ | PROT_WRITE, MAP_SHARED, cameraHandle->m_deviceHandle, buffer.m.offset); if(MAP_FAILED == bufferMap) { CARL_ERROR("Buffer map failed - \"%s\"", strerror(errno)); result = R_BUFFERMAPFAILED; goto end; } /***** Set data *****/ cameraHandle->m_buffers[bufferIndex].m_start = bufferMap; cameraHandle->m_buffers[bufferIndex].m_sizeBytes = buffer.length; ++cameraHandle->m_bufferCount; } /***** Queue buffers *****/ for(bufferIndex=0; bufferIndex<cameraHandle->m_bufferCount; ++bufferIndex) { CLEAR(buffer); buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; buffer.memory = V4L2_MEMORY_MMAP; buffer.index = bufferIndex; xioResult = xioctl(cameraHandle->m_deviceHandle, VIDIOC_QBUF, &buffer); if(xioResult == -1) { CARL_ERROR("Buffer queue failed - \"%s\"", strerror(errno)); result = R_BUFFERENQUEUEFAILED; goto end; } } /***** Set output *****/ if(o_cameraHandle != NULL) { (*o_cameraHandle) = cameraHandle; } else { camera_destroy(&cameraHandle); } /***** Return *****/ return R_SUCCESS; end: CARL_ERROR("camera_create(%d, %d, %u, %u, %p)", i_deviceID, i_pixelFormat, i_sizeX, i_sizeY, o_cameraHandle); camera_destroy(&cameraHandle); return result; };
static int run_scanner(void) { struct quirc *qr; struct camera cam; struct mjpeg_decoder mj; const struct camera_parms *parms; camera_init(&cam); if (camera_open(&cam, camera_path, video_width, video_height, 25, 1) < 0) { perror("camera_open"); goto fail_qr; } if (camera_map(&cam, 8) < 0) { perror("camera_map"); goto fail_qr; } if (camera_on(&cam) < 0) { perror("camera_on"); goto fail_qr; } if (camera_enqueue_all(&cam) < 0) { perror("camera_enqueue_all"); goto fail_qr; } parms = camera_get_parms(&cam); qr = quirc_new(); if (!qr) { perror("couldn't allocate QR decoder"); goto fail_qr; } if (quirc_resize(qr, parms->width, parms->height) < 0) { perror("couldn't allocate QR buffer"); goto fail_qr_resize; } mjpeg_init(&mj); if (main_loop(&cam, qr, &mj) < 0) goto fail_main_loop; mjpeg_free(&mj); quirc_destroy(qr); camera_destroy(&cam); return 0; fail_main_loop: mjpeg_free(&mj); fail_qr_resize: quirc_destroy(qr); fail_qr: camera_destroy(&cam); return -1; }
int main(int argc, char* argv[]) { // our demo variables World* demoworld = 0; Camera* democamera = 0; Tileset* demotileset = 0; Renderer* demorenderer = 0; // init SDL if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { fprintf(stderr, "SDL Library Initialization Failed!\n\tSDL Error: %s\n", SDL_GetError()); exit(1); } atexit(SDL_Quit); // create the window if (!SDL_SetVideoMode(SCREEN_W, SCREEN_H, SCREEN_BPP, SDL_HWSURFACE | SDL_DOUBLEBUF)) { fprintf(stderr, "SDL Screen Initialization Failed! %dx%d@%dbpp\nSDL Error: %s\n", SCREEN_W, SCREEN_H, SCREEN_BPP, SDL_GetError()); exit(1); } // set the window caption SDL_WM_SetCaption("Basic SDL Map Editor Example Demo -- by Richard Marks <*****@*****.**>", 0); // create our keyboard handler Keyboard* keyboard = keyboard_create(); // load the world demoworld = world_load("example_demo.map"); if (!demoworld) { fprintf(stderr, "unable to load the world file!\n"); exit(1); } // load the tileset demotileset = tileset_load("example_demo.png", 32, 32); if (!demotileset) { fprintf(stderr, "unable to load the tileset file!\n"); exit(1); } // create the camera democamera = camera_create( // anchor position of the camera is at 32,32 on the screen 32, 32, // the camera view is 30 x 22 tiles 30, 22, // need to tell the camera the size of the tiles demotileset->width, demotileset->height); // create the renderer demorenderer = renderer_create(demoworld, demotileset, democamera); // pre-render the scene renderer_pre_render(demorenderer); fprintf(stderr, "\nstarting main loop...\n\n"); // start the main loop SDL_Event event; bool running = true; int startticks = SDL_GetTicks(); int mainlooptimer = SDL_GetTicks(); while(running) { // check for our window being closed while(SDL_PollEvent(&event)) { if (SDL_QUIT == event.type) { running = false; } } // update our keyboard handler keyboard_update(keyboard); // start timing our code startticks = SDL_GetTicks(); // check for our controls // ESC and Q quits if (keyboard->key[SDLK_ESCAPE] || keyboard->key[SDLK_q]) { running = false; } const unsigned int CAMERASPEED = 16; // W and up arrow if (keyboard->key[SDLK_w] || keyboard->key[SDLK_UP]) { if (democamera->inpixels->y > 0) { democamera->inpixels->y -= CAMERASPEED; } } // S and down arrow if (keyboard->key[SDLK_s] || keyboard->key[SDLK_DOWN]) { if (democamera->inpixels->y < demorenderer->scene->h - democamera->inpixels->h) { democamera->inpixels->y += CAMERASPEED; } } // A and left arrow if (keyboard->key[SDLK_a] || keyboard->key[SDLK_LEFT]) { if (democamera->inpixels->x > 0) { democamera->inpixels->x -= CAMERASPEED; } } // D and right arrow if (keyboard->key[SDLK_d] || keyboard->key[SDLK_RIGHT]) { if (democamera->inpixels->x < demorenderer->scene->w - democamera->inpixels->w) { democamera->inpixels->x += CAMERASPEED; } } // render our scene renderer_render(demorenderer); // flip our double buffer SDL_Flip(SDL_GetVideoSurface()); // lock our framerate to roughly 30 FPS int tickdiff = SDL_GetTicks() - startticks; if (tickdiff < 33) { SDL_Delay(33 - tickdiff); } } int tickdiff = SDL_GetTicks() - mainlooptimer; fprintf(stderr, "finished after %d ticks!\n\n", tickdiff); // destroy our pointers tileset_destroy(demotileset); renderer_destroy(demorenderer); world_destroy(demoworld); camera_destroy(democamera); keyboard_destroy(keyboard); return 0; }
static void cleanup(void) { /* end of TC */ camera_destroy(camera); }