void vc_hostfs_init(void) { // This hostfs module is not thread safe - it allocaes a block // of memory and uses it without any kind of locking. // // It offers no advantage of stdio, and so most clients should // not use it. Arguably FILESYS should use it in order to get // the FIFO support. const char *thread_name = vcos_thread_get_name(vcos_thread_current()); if (strcmp(thread_name, "FILESYS") != 0 && strcmp(thread_name, "HFilesys") != 0) { fprintf(stderr,"%s: vc_hostfs is deprecated. Please use stdio\n", vcos_thread_get_name(vcos_thread_current())); } vcos_log_register("hostfs", &hostfs_log_cat); DEBUG_MINOR("init"); // Allocate memory for the file info table p_file_info_table = (file_info_t *)calloc( FILE_INFO_TABLE_CHUNK_LEN, sizeof( file_info_t ) ); assert( p_file_info_table != NULL ); if (p_file_info_table) { file_info_table_len = FILE_INFO_TABLE_CHUNK_LEN; } }
/** * main */ int main(int argc, const char **argv) { // Our main data storage vessel.. RASPISTILL_STATE state; MMAL_STATUS_T status = MMAL_SUCCESS; MMAL_PORT_T *camera_preview_port = NULL; MMAL_PORT_T *camera_video_port = NULL; MMAL_PORT_T *camera_still_port = NULL; MMAL_PORT_T *preview_input_port = NULL; MMAL_PORT_T *encoder_input_port = NULL; MMAL_PORT_T *encoder_output_port = NULL; bcm_host_init(); // Register our application with the logging system vcos_log_register("RaspiStill", VCOS_LOG_CATEGORY); signal(SIGINT, signal_handler); default_status(&state); // Do we have any parameters if (argc == 1) { fprintf(stderr, "\%s Camera App %s\n\n", basename(argv[0]), VERSION_STRING); display_valid_parameters(basename(argv[0])); exit(0); }
int32_t vc_gpuserv_init( void ) { VCHIQ_SERVICE_PARAMS_T vchiq_params; VCOS_STATUS_T status = VCOS_ENXIO; VCHIQ_STATUS_T vchiq_status; vcos_once(&gpuserv_client_once, init_once); vcos_mutex_lock(&gpuserv_client.lock); if (gpuserv_client.refcount++ > 0) { /* Already initialised so nothing to do */ vcos_mutex_unlock(&gpuserv_client.lock); return VCOS_SUCCESS; } vcos_log_set_level(VCOS_LOG_CATEGORY, VCOS_LOG_TRACE); vcos_log_register("gpuserv", VCOS_LOG_CATEGORY); vcos_log_trace("%s: starting initialisation", VCOS_FUNCTION); /* Initialise a VCHIQ instance */ vchiq_status = vchiq_initialise(&gpuserv_client_vchiq_instance); if (vchiq_status != VCHIQ_SUCCESS) { vcos_log_error("%s: failed to initialise vchiq: %d", VCOS_FUNCTION, vchiq_status); goto error; } vchiq_status = vchiq_connect(gpuserv_client_vchiq_instance); if (vchiq_status != VCHIQ_SUCCESS) { vcos_log_error("%s: failed to connect to vchiq: %d", VCOS_FUNCTION, vchiq_status); goto error; } memset(&vchiq_params, 0, sizeof(vchiq_params)); vchiq_params.fourcc = VCHIQ_MAKE_FOURCC('G','P','U','S'); vchiq_params.callback = gpuserv_callback; vchiq_params.userdata = NULL; vchiq_params.version = 1; vchiq_params.version_min = 1; vchiq_status = vchiq_open_service(gpuserv_client_vchiq_instance, &vchiq_params, &gpuserv_client.service); if (vchiq_status != VCHIQ_SUCCESS) { vcos_log_error("%s: could not open vchiq service: %d", VCOS_FUNCTION, vchiq_status); goto error; } vcos_mutex_unlock(&gpuserv_client.lock); return 0; error: vcos_mutex_unlock(&gpuserv_client.lock); return -1; }
/* Initialises GL preview state and creates the dispmanx native window. * @param state Pointer to the GL preview state. * @return Zero if successful. */ int raspitex_init(RASPITEX_STATE *state) { VCOS_STATUS_T status; int rc; vcos_init(); vcos_log_register("RaspiTex", VCOS_LOG_CATEGORY); vcos_log_set_level(VCOS_LOG_CATEGORY, state->verbose ? VCOS_LOG_INFO : VCOS_LOG_WARN); vcos_log_trace("%s", VCOS_FUNCTION); status = vcos_semaphore_create(&state->capture.start_sem, "glcap_start_sem", 1); if (status != VCOS_SUCCESS) goto error; status = vcos_semaphore_create(&state->capture.completed_sem, "glcap_completed_sem", 0); if (status != VCOS_SUCCESS) goto error; switch (state->scene_id) { case RASPITEX_SCENE_SQUARE: rc = square_open(state); break; case RASPITEX_SCENE_MIRROR: rc = mirror_open(state); break; case RASPITEX_SCENE_TEAPOT: rc = teapot_open(state); break; case RASPITEX_SCENE_YUV: rc = yuv_open(state); break; case RASPITEX_SCENE_SOBEL: rc = sobel_open(state); break; case RASPITEXT_SCENE_BGS_SIMPLE: rc = gl_simple_open(state); break; default: rc = -1; break; } if (rc != 0) goto error; return 0; error: vcos_log_error("%s: failed", VCOS_FUNCTION); return -1; }
void vcos_logging_init(void) { if (inited) { // FIXME: should print a warning or something here return; } vcos_mutex_create(&lock, "vcos_log"); vcos_log_register("default", &dflt_log_category); dflt_log_category.flags.want_prefix = 0; vcos_assert(!inited); inited = 1; }
void vcos_logging_init(void) { if (inited) { /* FIXME: should print a warning or something here */ return; } vcos_mutex_create(&lock, "vcos_log"); vcos_log_platform_init(); vcos_log_register("default", &dflt_log_category); #if VCOS_WANT_LOG_CMD vcos_cmd_register( &cmd_log ); #endif vcos_assert(!inited); inited = 1; }
/** * main */ int main(int argc, const char **argv) { // Our main data storage vessel.. RASPISTILLYUV_STATE state; MMAL_STATUS_T status; MMAL_PORT_T *camera_preview_port = NULL; MMAL_PORT_T *camera_video_port = NULL; MMAL_PORT_T *camera_still_port = NULL; MMAL_PORT_T *preview_input_port = NULL; FILE *output_file = NULL; // Register our application with the logging system vcos_log_register("RaspiStill", VCOS_LOG_CATEGORY); printf("\nRaspiStillYUV Camera App\n"); printf( "========================\n\n"); signal(SIGINT, signal_handler); // Do we have any parameters if (argc == 1) { display_valid_parameters(); exit(0); } default_status(&state); // Parse the command line and put options in to our status structure if (parse_cmdline(argc, argv, &state)) { status = -1; exit(0); } if (state.verbose) dump_status(&state); // OK, we have a nice set of parameters. Now set up our components // We have two components. Camera and Preview // Camera is different in stills/video, but preview // is the same so handed off to a separate module if (!create_camera_component(&state)) { vcos_log_error("%s: Failed to create camera component", __func__); } else if ( !raspipreview_create(&state.preview_parameters)) { vcos_log_error("%s: Failed to create preview component", __func__); destroy_camera_component(&state); } else { PORT_USERDATA callback_data; if (state.verbose) printf("Starting component connection stage\n"); camera_preview_port = state.camera_component->output[MMAL_CAMERA_PREVIEW_PORT]; camera_video_port = state.camera_component->output[MMAL_CAMERA_VIDEO_PORT]; camera_still_port = state.camera_component->output[MMAL_CAMERA_CAPTURE_PORT]; preview_input_port = state.preview_parameters.preview_component->input[0]; if (state.preview_parameters.wantPreview ) { if (state.verbose) { printf("Connecting camera preview port to preview input port\n"); printf("Starting video preview\n"); } // Connect camera to preview status = connect_ports(camera_preview_port, preview_input_port, &state.preview_connection); } if (status == MMAL_SUCCESS) { VCOS_STATUS_T vcos_status; if (state.filename) { if (state.verbose) printf("Opening output file %s\n", state.filename); output_file = fopen(state.filename, "wb"); if (!output_file) { // Notify user, carry on but discarding encoded output buffers vcos_log_error("%s: Error opening output file: %s\nNo output file will be generated\n", __func__, state.filename); } } // Set up our userdata - this is passed though to the callback where we need the information. callback_data.file_handle = output_file; callback_data.pstate = &state; vcos_status = vcos_semaphore_create(&callback_data.complete_semaphore, "RaspiStill-sem", 0); vcos_assert(vcos_status == VCOS_SUCCESS); camera_still_port->userdata = (struct MMAL_PORT_USERDATA_T *)&callback_data; if (state.verbose) printf("Enabling camera still output port\n"); // Enable the camera still output port and tell it its callback function status = mmal_port_enable(camera_still_port, camera_buffer_callback); if (status != MMAL_SUCCESS) { vcos_log_error("Failed to setup camera output"); goto error; } if (state.verbose) printf("Starting video preview\n"); // Send all the buffers to the encoder output port { int num = mmal_queue_length(state.camera_pool->queue); int q; for (q=0;q<num;q++) { MMAL_BUFFER_HEADER_T *buffer = mmal_queue_get(state.camera_pool->queue); if (!buffer) vcos_log_error("Unable to get a required buffer %d from pool queue", q); if (mmal_port_send_buffer(camera_still_port, buffer)!= MMAL_SUCCESS) vcos_log_error("Unable to send a buffer to encoder output port (%d)", q); } } // Now wait until we need to do the capture vcos_sleep(state.timeout); // And only do the capture if we have specified a filename and its opened OK if (output_file) { if (state.verbose) printf("Starting capture\n"); // Fire the capture if (mmal_port_parameter_set_boolean(camera_still_port, MMAL_PARAMETER_CAPTURE, 1) != MMAL_SUCCESS) { vcos_log_error("%s: Failed to start capture", __func__); } else { // Wait for capture to complete // For some reason using vcos_semaphore_wait_timeout sometimes returns immediately with bad parameter error // even though it appears to be all correct, so reverting to untimed one until figure out why its erratic vcos_semaphore_wait(&callback_data.complete_semaphore); if (state.verbose) printf("Finished capture\n"); } } vcos_semaphore_delete(&callback_data.complete_semaphore); } else { mmal_status_to_int(status); vcos_log_error("%s: Failed to connect camera to preview", __func__); } error: mmal_status_to_int(status); if (state.verbose) printf("Closing down\n"); if (output_file) fclose(output_file); // Disable all our ports that are not handled by connections check_disable_port(camera_video_port); if (state.preview_parameters.wantPreview ) mmal_connection_destroy(state.preview_connection); /* Disable components */ if (state.preview_parameters.preview_component) mmal_component_disable(state.preview_parameters.preview_component); if (state.camera_component) mmal_component_disable(state.camera_component); raspipreview_destroy(&state.preview_parameters); destroy_camera_component(&state); if (state.verbose) printf("Close down completed, all components disconnected, disabled and destroyed\n\n"); } return 0; }
/** * main */ int main(int argc, const char **argv) { // Our main data storage vessel.. RASPIVID_STATE state; MMAL_STATUS_T status = MMAL_SUCCESS; MMAL_PORT_T *camera_preview_port = NULL; MMAL_PORT_T *camera_video_port = NULL; MMAL_PORT_T *camera_still_port = NULL; MMAL_PORT_T *preview_input_port = NULL; MMAL_PORT_T *encoder_input_port = NULL; MMAL_PORT_T *encoder_output_port = NULL; FILE *output_file = NULL; bcm_host_init(); // Register our application with the logging system vcos_log_register("RaspiVid", VCOS_LOG_CATEGORY); signal(SIGINT, signal_handler); default_status(&state); // Do we have any parameters if (argc == 1) { fprintf(stderr, "\n%s Camera App %s\n\n", basename(argv[0]), VERSION_STRING); display_valid_parameters(basename(argv[0])); exit(0); } // Parse the command line and put options in to our status structure if (parse_cmdline(argc, argv, &state)) { status = -1; exit(0); } if (state.verbose) { fprintf(stderr, "\n%s Camera App %s\n\n", basename(argv[0]), VERSION_STRING); dump_status(&state); } // OK, we have a nice set of parameters. Now set up our components // We have three components. Camera, Preview and encoder. if ((status = create_camera_component(&state)) != MMAL_SUCCESS) { vcos_log_error("%s: Failed to create camera component", __func__); } else if ((status = raspipreview_create(&state.preview_parameters)) != MMAL_SUCCESS) { vcos_log_error("%s: Failed to create preview component", __func__); destroy_camera_component(&state); } else if ((status = create_encoder_component(&state)) != MMAL_SUCCESS) { vcos_log_error("%s: Failed to create encode component", __func__); raspipreview_destroy(&state.preview_parameters); destroy_camera_component(&state); } else { PORT_USERDATA callback_data; if (state.verbose) fprintf(stderr, "Starting component connection stage\n"); camera_preview_port = state.camera_component->output[MMAL_CAMERA_PREVIEW_PORT]; camera_video_port = state.camera_component->output[MMAL_CAMERA_VIDEO_PORT]; camera_still_port = state.camera_component->output[MMAL_CAMERA_CAPTURE_PORT]; preview_input_port = state.preview_parameters.preview_component->input[0]; encoder_input_port = state.encoder_component->input[0]; encoder_output_port = state.encoder_component->output[0]; if (state.preview_parameters.wantPreview ) { if (state.verbose) { fprintf(stderr, "Connecting camera preview port to preview input port\n"); fprintf(stderr, "Starting video preview\n"); } // Connect camera to preview status = connect_ports(camera_preview_port, preview_input_port, &state.preview_connection); } else { status = MMAL_SUCCESS; } if (status == MMAL_SUCCESS) { if (state.verbose) fprintf(stderr, "Connecting camera stills port to encoder input port\n"); // Now connect the camera to the encoder status = connect_ports(camera_video_port, encoder_input_port, &state.encoder_connection); if (status != MMAL_SUCCESS) { vcos_log_error("%s: Failed to connect camera video port to encoder input", __func__); goto error; } if (state.filename) { if (state.filename[0] == '-') { output_file = stdout; // Ensure we don't upset the output stream with diagnostics/info state.verbose = 0; } else { if (state.verbose) fprintf(stderr, "Opening output file \"%s\"\n", state.filename); output_file = fopen(state.filename, "wb"); } if (!output_file) { // Notify user, carry on but discarding encoded output buffers vcos_log_error("%s: Error opening output file: %s\nNo output file will be generated\n", __func__, state.filename); } } // Set up our userdata - this is passed though to the callback where we need the information. callback_data.file_handle = output_file; callback_data.image = Mat(Size(state.width, state.height), CV_8UC1); callback_data.pstate = &state; callback_data.abort = 0; encoder_output_port->userdata = (struct MMAL_PORT_USERDATA_T *)&callback_data; if (state.verbose) fprintf(stderr, "Enabling encoder output port\n"); // Enable the encoder output port and tell it its callback function status = mmal_port_enable(encoder_output_port, encoder_buffer_callback); if (status != MMAL_SUCCESS) { vcos_log_error("Failed to setup encoder output"); goto error; } if (state.demoMode) { // Run for the user specific time.. int num_iterations = state.timeout / state.demoInterval; int i; if (state.verbose) fprintf(stderr, "Running in demo mode\n"); for (i=0;state.timeout == 0 || i<num_iterations;i++) { raspicamcontrol_cycle_test(state.camera_component); vcos_sleep(state.demoInterval); } } else { // Only encode stuff if we have a filename and it opened if (output_file) { int wait; if (state.verbose) fprintf(stderr, "Starting video capture\n"); if (mmal_port_parameter_set_boolean(camera_video_port, MMAL_PARAMETER_CAPTURE, 1) != MMAL_SUCCESS) { goto error; } // Send all the buffers to the encoder output port { int num = mmal_queue_length(state.encoder_pool->queue); int q; for (q=0;q<num;q++) { MMAL_BUFFER_HEADER_T *buffer = mmal_queue_get(state.encoder_pool->queue); if (!buffer) vcos_log_error("Unable to get a required buffer %d from pool queue", q); if (mmal_port_send_buffer(encoder_output_port, buffer)!= MMAL_SUCCESS) vcos_log_error("Unable to send a buffer to encoder output port (%d)", q); } } // Now wait until we need to stop. Whilst waiting we do need to check to see if we have aborted (for example // out of storage space) // Going to check every ABORT_INTERVAL milliseconds for (wait = 0; state.timeout == 0 || wait < state.timeout; wait+= ABORT_INTERVAL) { vcos_sleep(ABORT_INTERVAL); if (callback_data.abort) break; } if (state.verbose) fprintf(stderr, "Finished capture\n"); } else { if (state.timeout) vcos_sleep(state.timeout); else for (;;) vcos_sleep(ABORT_INTERVAL); } } } else { mmal_status_to_int(status); vcos_log_error("%s: Failed to connect camera to preview", __func__); } error: mmal_status_to_int(status); if (state.verbose) fprintf(stderr, "Closing down\n"); // Disable all our ports that are not handled by connections check_disable_port(camera_still_port); check_disable_port(encoder_output_port); if (state.preview_parameters.wantPreview ) mmal_connection_destroy(state.preview_connection); mmal_connection_destroy(state.encoder_connection); // Can now close our file. Note disabling ports may flush buffers which causes // problems if we have already closed the file! if (output_file && output_file != stdout) fclose(output_file); /* Disable components */ if (state.encoder_component) mmal_component_disable(state.encoder_component); if (state.preview_parameters.preview_component) mmal_component_disable(state.preview_parameters.preview_component); if (state.camera_component) mmal_component_disable(state.camera_component); destroy_encoder_component(&state); raspipreview_destroy(&state.preview_parameters); destroy_camera_component(&state); if (state.verbose) fprintf(stderr, "Close down completed, all components disconnected, disabled and destroyed\n\n"); } if (status != MMAL_SUCCESS) raspicamcontrol_check_configuration(128); return 0; }
int OpenVideoCoreMemoryFileWithOffset( const char *filename, VC_MEM_ACCESS_HANDLE_T *vcHandlePtr, size_t loadOffset ) { int rc = 0; VC_MEM_ACCESS_HANDLE_T newHandle; VC_DEBUG_SYMBOL_T debug_sym; VC_MEM_ADDR_T symAddr; size_t symTableSize; unsigned symIdx; struct { VC_DEBUG_HEADER_T header; VC_DEBUG_PARAMS_T params; } vc_dbg; vcos_log_register( "debug_sym", &debug_sym_log_category ); if (( newHandle = calloc( 1, sizeof( *newHandle ))) == NULL ) { return -ENOMEM; } if ( filename == NULL ) { newHandle->use_vc_mem = 1; filename = "/dev/vc-mem"; } else { newHandle->use_vc_mem = 0; } if (( newHandle->memFd = open( filename, ( newHandle->use_vc_mem ? O_RDWR : O_RDONLY ) | O_SYNC )) < 0 ) { ERR( "Unable to open '%s': %s(%d)\n", filename, strerror( errno ), errno ); free(newHandle); return -errno; } DBG( "Opened %s memFd = %d", filename, newHandle->memFd ); if ( newHandle->use_vc_mem ) { newHandle->memFdBase = 0; #if defined(WIN32) || defined(__CYGWIN__) #define VC_MEM_SIZE (128 * 1024 * 1024) newHandle->vcMemSize = VC_MEM_SIZE; newHandle->vcMemBase = 0; newHandle->vcMemLoad = 0; newHandle->vcMemPhys = 0; #else if ( ioctl( newHandle->memFd, VC_MEM_IOC_MEM_SIZE, &newHandle->vcMemSize ) != 0 ) { ERR( "Failed to get memory size via ioctl: %s(%d)\n", strerror( errno ), errno ); free(newHandle); return -errno; } if ( ioctl( newHandle->memFd, VC_MEM_IOC_MEM_BASE, &newHandle->vcMemBase ) != 0 ) { ERR( "Failed to get memory base via ioctl: %s(%d)\n", strerror( errno ), errno ); free(newHandle); return -errno; } if ( ioctl( newHandle->memFd, VC_MEM_IOC_MEM_LOAD, &newHandle->vcMemLoad ) != 0 ) { ERR( "Failed to get memory load via ioctl: %s(%d)\n", strerror( errno ), errno ); /* Backward compatibility. */ newHandle->vcMemLoad = newHandle->vcMemBase; } if ( ioctl( newHandle->memFd, VC_MEM_IOC_MEM_PHYS_ADDR, &newHandle->vcMemPhys ) != 0 ) { ERR( "Failed to get memory physical address via ioctl: %s(%d)\n", strerror( errno ), errno ); free(newHandle); return -errno; } #endif } else { off_t len = lseek( newHandle->memFd, 0, SEEK_END ); if ( len < 0 ) { ERR( "Failed to seek to end of file: %s(%d)\n", strerror( errno ), errno ); free(newHandle); return -errno; } newHandle->vcMemPhys = 0; newHandle->vcMemSize = len; newHandle->vcMemBase = 0; newHandle->vcMemLoad = loadOffset; newHandle->memFdBase = 0; } DBG( "vcMemSize = %08x", newHandle->vcMemSize ); DBG( "vcMemBase = %08x", newHandle->vcMemBase ); DBG( "vcMemLoad = %08x", newHandle->vcMemLoad ); DBG( "vcMemPhys = %08x", newHandle->vcMemPhys ); newHandle->vcMemEnd = newHandle->vcMemBase + newHandle->vcMemSize - 1; // See if we can detect the symbol table if ( !ReadVideoCoreMemory( newHandle, &newHandle->vcSymbolTableOffset, newHandle->vcMemLoad + VC_SYMBOL_BASE_OFFSET, sizeof( newHandle->vcSymbolTableOffset ))) { ERR( "ReadVideoCoreMemory @VC_SYMBOL_BASE_OFFSET (0x%08x) failed\n", VC_SYMBOL_BASE_OFFSET ); rc = -EIO; goto err_exit; } // When reading from a file, the VC binary is read into a buffer and the effective base is 0. // But that may not be the actual base address the binary is intended to be loaded to. The // following reads the debug header to find out what the actual base address is. if( !newHandle->use_vc_mem ) { // Read the complete debug header if ( !ReadVideoCoreMemory( newHandle, &vc_dbg, newHandle->vcMemLoad + VC_SYMBOL_BASE_OFFSET, sizeof( vc_dbg ))) { ERR( "ReadVideoCoreMemory @VC_SYMBOL_BASE_OFFSET (0x%08x) failed\n", VC_SYMBOL_BASE_OFFSET ); rc = -EIO; goto err_exit; } // The vc_dbg header gives the "base" address of the VC binary, // which debug_sym calls the "load" address, so we need to adjust // it by loadOffset to find the base of the whole memory dump file newHandle->memFdBase = vc_dbg.params.vcMemBase - loadOffset; newHandle->vcMemBase = vc_dbg.params.vcMemBase - loadOffset; newHandle->vcMemLoad = vc_dbg.params.vcMemBase; newHandle->vcMemEnd = newHandle->memFdBase + newHandle->vcMemSize - 1; DBG( "Updated from debug header:" ); DBG( "vcMemSize = %08x", newHandle->vcMemSize ); DBG( "vcMemBase = %08x", newHandle->vcMemBase ); DBG( "vcMemLoad = %08x", newHandle->vcMemLoad ); DBG( "vcMemPhys = %08x", newHandle->vcMemPhys ); } DBG( "vcSymbolTableOffset = 0x%08x", newHandle->vcSymbolTableOffset ); // Make sure that the pointer points into the first few megabytes of // the memory space. if ( (newHandle->vcSymbolTableOffset - newHandle->vcMemLoad) > ( MAX_VC_SIZE * 1024 * 1024 )) { ERR( "newHandle->vcSymbolTableOffset (0x%x - 0x%x) > %dMB\n", newHandle->vcSymbolTableOffset, newHandle->vcMemLoad, MAX_VC_SIZE ); rc = -EIO; goto err_exit; } // Make a pass to count how many symbols there are. symAddr = newHandle->vcSymbolTableOffset; newHandle->numSymbols = 0; do { if ( !ReadVideoCoreMemory( newHandle, &debug_sym, symAddr, sizeof( debug_sym ))) { ERR( "ReadVideoCoreMemory @ symAddr(0x%08x) failed\n", symAddr ); rc = -EIO; goto err_exit; } newHandle->numSymbols++; DBG( "Symbol %d: label: 0x%p addr: 0x%08x size: %zu", newHandle->numSymbols, debug_sym.label, debug_sym.addr, debug_sym.size ); if ( newHandle->numSymbols > 1024 ) { // Something isn't sane. ERR( "numSymbols (%d) > 1024 - looks wrong\n", newHandle->numSymbols ); rc = -EIO; goto err_exit; } symAddr += sizeof( debug_sym ); } while ( debug_sym.label != 0 ); newHandle->numSymbols--; DBG( "Detected %d symbols", newHandle->numSymbols ); // Allocate some memory to hold the symbols, and read them in. symTableSize = newHandle->numSymbols * sizeof( debug_sym ); if (( newHandle->symbol = malloc( symTableSize )) == NULL ) { rc = -ENOMEM; goto err_exit; } if ( !ReadVideoCoreMemory( newHandle, newHandle->symbol, newHandle->vcSymbolTableOffset, symTableSize )) { ERR( "ReadVideoCoreMemory @ newHandle->vcSymbolTableOffset(0x%08x) failed\n", newHandle->vcSymbolTableOffset ); rc = -EIO; goto err_exit; } // The names of the symbols are pointers in videocore space. We want // to have them available locally, so we make copies and fixup // the pointer. for ( symIdx = 0; symIdx < newHandle->numSymbols; symIdx++ ) { VC_DEBUG_SYMBOL_T *sym; char symName[ 256 ]; sym = &newHandle->symbol[ symIdx ]; DBG( "Symbol %d: label: 0x%p addr: 0x%08x size: %zu", symIdx, sym->label, sym->addr, sym->size ); if ( !ReadVideoCoreMemory( newHandle, symName, TO_VC_MEM_ADDR(sym->label), sizeof( symName ))) { ERR( "ReadVideoCoreMemory @ sym->label(0x%08x) failed\n", TO_VC_MEM_ADDR(sym->label) ); rc = -EIO; goto err_exit; } symName[ sizeof( symName ) - 1 ] = '\0'; sym->label = vcos_strdup( symName ); DBG( "Symbol %d (@0x%p): label: '%s' addr: 0x%08x size: %zu", symIdx, sym, sym->label, sym->addr, sym->size ); } *vcHandlePtr = newHandle; return 0; err_exit: close( newHandle->memFd ); free( newHandle ); return rc; }
/** * main */ int main(int argc, const char **argv) { // *** MODIFICATION: OpenCV modifications. // Load previous image. IplImage* prevImage = cvLoadImage("motion1.jpg", CV_LOAD_IMAGE_COLOR); // Create two arrays with the same number of channels than the original one. avg1 = cvCreateMat(prevImage->height,prevImage->width,CV_32FC3); avg2 = cvCreateMat(prevImage->height,prevImage->width,CV_32FC3); // Create image of 32 bits. IplImage* image32 = cvCreateImage(cvSize(prevImage->width,prevImage->height), 32,3); // Convert image to 32 bits. cvConvertScale(prevImage,image32,1/255,0); // Set data from previous image into arrays. cvSetData(avg1,image32->imageData,image32->widthStep); cvSetData(avg2,image32->imageData,image32->widthStep); // *** MODIFICATION end // Our main data storage vessel.. RASPISTILL_STATE state; MMAL_STATUS_T status = MMAL_SUCCESS; MMAL_PORT_T *camera_preview_port = NULL; MMAL_PORT_T *camera_video_port = NULL; MMAL_PORT_T *camera_still_port = NULL; MMAL_PORT_T *preview_input_port = NULL; MMAL_PORT_T *encoder_input_port = NULL; MMAL_PORT_T *encoder_output_port = NULL; bcm_host_init(); // Register our application with the logging system vcos_log_register("fast", VCOS_LOG_CATEGORY); signal(SIGINT, signal_handler); default_status(&state); if (state.verbose) { fprintf(stderr, "\n%s Camera App %s\n\n", basename(argv[0]), VERSION_STRING); } // OK, we have a nice set of parameters. Now set up our components // We have three components. Camera, Preview and encoder. // Camera and encoder are different in stills/video, but preview // is the same so handed off to a separate module if ((status = create_camera_component(&state)) != MMAL_SUCCESS) { vcos_log_error("%s: Failed to create camera component", __func__); } else if ((status = raspipreview_create(&state.preview_parameters)) != MMAL_SUCCESS) { vcos_log_error("%s: Failed to create preview component", __func__); destroy_camera_component(&state); } else if ((status = create_encoder_component(&state)) != MMAL_SUCCESS) { vcos_log_error("%s: Failed to create encode component", __func__); raspipreview_destroy(&state.preview_parameters); destroy_camera_component(&state); } else { PORT_USERDATA callback_data; if (state.verbose) fprintf(stderr, "Starting component connection stage\n"); camera_preview_port = state.camera_component->output[MMAL_CAMERA_PREVIEW_PORT]; camera_video_port = state.camera_component->output[MMAL_CAMERA_VIDEO_PORT]; camera_still_port = state.camera_component->output[MMAL_CAMERA_CAPTURE_PORT]; preview_input_port = state.preview_parameters.preview_component->input[0]; encoder_input_port = state.encoder_component->input[0]; encoder_output_port = state.encoder_component->output[0]; if (state.preview_parameters.wantPreview ) { if (state.verbose) { fprintf(stderr, "Connecting camera preview port to preview input port\n"); fprintf(stderr, "Starting video preview\n"); } // *** USER: remove preview // Connect camera to preview //status = connect_ports(camera_preview_port, preview_input_port, &state.preview_connection); } else { status = MMAL_SUCCESS; } if (status == MMAL_SUCCESS) { VCOS_STATUS_T vcos_status; if (state.verbose) fprintf(stderr, "Connecting camera stills port to encoder input port\n"); // Now connect the camera to the encoder status = connect_ports(camera_still_port, encoder_input_port, &state.encoder_connection); if (status != MMAL_SUCCESS) { vcos_log_error("%s: Failed to connect camera video port to encoder input", __func__); goto error; } // Set up our userdata - this is passed though to the callback where we need the information. // Null until we open our filename callback_data.file_handle = NULL; callback_data.pstate = &state; vcos_status = vcos_semaphore_create(&callback_data.complete_semaphore, "RaspiStill-sem", 0); vcos_assert(vcos_status == VCOS_SUCCESS); if (status != MMAL_SUCCESS) { vcos_log_error("Failed to setup encoder output"); goto error; } FILE *output_file = NULL; int frame = 1; // Enable the encoder output port encoder_output_port->userdata = (struct MMAL_PORT_USERDATA_T *)&callback_data; if (state.verbose) fprintf(stderr, "Enabling encoder output port\n"); // Enable the encoder output port and tell it its callback function status = mmal_port_enable(encoder_output_port, encoder_buffer_callback); // Create an empty matrix with the size of the buffer. CvMat* buf = cvCreateMat(1,60000,CV_8UC1); // Keep buffer that gets frames from queue. MMAL_BUFFER_HEADER_T *buffer; // Image to be displayed. IplImage* image; // Keep number of buffers and index for the loop. int num, q; while(1) { // Send all the buffers to the encoder output port num = mmal_queue_length(state.encoder_pool->queue); for (q=0;q<num;q++) { buffer = mmal_queue_get(state.encoder_pool->queue); if (!buffer) vcos_log_error("Unable to get a required buffer %d from pool queue", q); if (mmal_port_send_buffer(encoder_output_port, buffer)!= MMAL_SUCCESS) vcos_log_error("Unable to send a buffer to encoder output port (%d)", q); } // for if (mmal_port_parameter_set_boolean(camera_still_port, MMAL_PARAMETER_CAPTURE, 1) != MMAL_SUCCESS) vcos_log_error("%s: Failed to start capture", __func__); else { // Wait for capture to complete // For some reason using vcos_semaphore_wait_timeout sometimes returns immediately with bad parameter error // even though it appears to be all correct, so reverting to untimed one until figure out why its erratic vcos_semaphore_wait(&callback_data.complete_semaphore); if (state.verbose) fprintf(stderr, "Finished capture %d\n", frame); } // else // Copy buffer from camera to matrix. buf->data.ptr = buffer->data; // This workaround is needed for the code to work // *** TODO: investigate why. printf("Until here works\n"); // Decode the image and display it. image = cvDecodeImage(buf, CV_LOAD_IMAGE_COLOR); // Destinations CvMat* res1 = cvCreateMat(image->height,image->width,CV_8UC3); CvMat* res2 = cvCreateMat(image->height,image->width,CV_8UC3); // Update running averages and then scale, calculate absolute values // and convert the result 8-bit. // *** USER:change the value of the weight. cvRunningAvg(image,avg2,0.0001, NULL); cvConvertScaleAbs(avg2, res2, 1,0); cvRunningAvg(image,avg1,0.1, NULL); cvConvertScaleAbs(avg1, res1, 1,0); // Show images cvShowImage("img",image); cvShowImage("avg1",res1); cvShowImage("avg2",res2); cvWaitKey(20); // Update previous image. cvSaveImage("motion1.jpg", image, 0); } // end while vcos_semaphore_delete(&callback_data.complete_semaphore); } else { mmal_status_to_int(status); vcos_log_error("%s: Failed to connect camera to preview", __func__); } error: mmal_status_to_int(status); if (state.verbose) fprintf(stderr, "Closing down\n"); // Disable all our ports that are not handled by connections check_disable_port(camera_video_port); check_disable_port(encoder_output_port); if (state.preview_parameters.wantPreview ) mmal_connection_destroy(state.preview_connection); mmal_connection_destroy(state.encoder_connection); /* Disable components */ if (state.encoder_component) mmal_component_disable(state.encoder_component); if (state.preview_parameters.preview_component) mmal_component_disable(state.preview_parameters.preview_component); if (state.camera_component) mmal_component_disable(state.camera_component); destroy_encoder_component(&state); raspipreview_destroy(&state.preview_parameters); destroy_camera_component(&state); if (state.verbose) fprintf(stderr, "Close down completed, all components disconnected, disabled and destroyed\n\n"); } if (status != MMAL_SUCCESS) raspicamcontrol_check_configuration(128); return 0; }
int main(int argc, const char **argv) { VCOS_THREAD_ATTR_T attrs; int i; vcos_init(); signal(SIGINT, test_signal_handler); /* coverity[tainted_data] Ignore unnecessary warning about an attacker * being able to pass an arbitrarily long "-vvvvv..." argument */ if (test_parse_cmdline(argc, argv)) return -1; if (verbosity--) { static char value[512]; const char *levels[] = {"warn", "info", "trace"}; char *env = getenv("VC_LOGLEVEL"); if (verbosity >= MMAL_COUNTOF(levels)) verbosity = MMAL_COUNTOF(levels) - 1; snprintf(value, sizeof(value)-1, "mmalplay:%s,mmal:%s,%s", levels[verbosity], levels[verbosity], env ? env : ""); #ifdef WIN32 _putenv("VC_LOGLEVEL", value, 1); #else setenv("VC_LOGLEVEL", value, 1); #endif } vcos_log_register("mmalplay", VCOS_LOG_CATEGORY); LOG_INFO("MMAL Video Playback Test App"); vcos_thread_attr_init(&attrs); for (i = 0; i < play_info_count; i++) { const char *uri = play_info[i].uri; memcpy(play_info[i].name, THREAD_PREFIX, sizeof(THREAD_PREFIX)); if (strlen(uri) >= URI_FOR_THREAD_NAME_MAX) uri += strlen(uri) - URI_FOR_THREAD_NAME_MAX; strncat(play_info[i].name, uri, URI_FOR_THREAD_NAME_MAX); vcos_mutex_create(&play_info[i].lock, "mmalplay"); play_info[i].options.render_layer = i; if (vcos_thread_create(&play_info[i].thread, play_info[i].name, &attrs, mmal_playback, &play_info[i]) != VCOS_SUCCESS) { LOG_ERROR("Thread creation failure for URI %s", play_info[i].uri); return -2; } } if (sleepy_time != 0) { #ifdef WIN32 Sleep(sleepy_time); #else sleep(sleepy_time); #endif for (i = 0; i < play_info_count; i++) { vcos_mutex_lock(&play_info[i].lock); if (play_info[i].ctx) mmalplay_stop(play_info[i].ctx); vcos_mutex_unlock(&play_info[i].lock); } } LOG_TRACE("Waiting for threads to terminate"); for (i = 0; i < play_info_count; i++) { vcos_thread_join(&play_info[i].thread, NULL); LOG_TRACE("Joined thread %d (%i)", i, play_info[i].status); } LOG_TRACE("Completed"); /* Check for errors */ for (i = 0; i < play_info_count; i++) { if (!play_info[i].status) continue; LOG_ERROR("Playback of %s failed (%i, %s)", play_info[i].uri, play_info[i].status, mmal_status_to_string(play_info[i].status)); fprintf(stderr, "playback of %s failed (%i, %s)\n", play_info[i].uri, play_info[i].status, mmal_status_to_string(play_info[i].status)); return play_info[i].status; } return 0; }
MMAL_STATUS_T mmal_vc_init(void) { VCHIQ_SERVICE_PARAMS_T vchiq_params; MMAL_BOOL_T vchiq_initialised = 0, waitpool_initialised = 0; MMAL_BOOL_T service_initialised = 0; MMAL_STATUS_T status = MMAL_EIO; VCHIQ_STATUS_T vchiq_status; int count; vcos_once(&once, init_once); vcos_mutex_lock(&client.lock); count = client.refcount++; if (count > 0) { /* Already initialised so nothing to do */ vcos_mutex_unlock(&client.lock); return MMAL_SUCCESS; } vcos_log_register("mmalipc", VCOS_LOG_CATEGORY); /* Initialise a VCHIQ instance */ vchiq_status = vchiq_initialise(&mmal_vchiq_instance); if (vchiq_status != VCHIQ_SUCCESS) { LOG_ERROR("failed to initialise vchiq"); status = MMAL_EIO; goto error; } vchiq_initialised = 1; vchiq_status = vchiq_connect(mmal_vchiq_instance); if (vchiq_status != VCHIQ_SUCCESS) { LOG_ERROR("failed to connect to vchiq"); status = MMAL_EIO; goto error; } memset(&vchiq_params,0,sizeof(vchiq_params)); vchiq_params.fourcc = MMAL_CONTROL_FOURCC(); vchiq_params.callback = mmal_vc_vchiq_callback; vchiq_params.userdata = &client; vchiq_params.version = WORKER_VER_MAJOR; vchiq_params.version_min = WORKER_VER_MINIMUM; vchiq_status = vchiq_open_service(mmal_vchiq_instance, &vchiq_params, &client.service); if (vchiq_status != VCHIQ_SUCCESS) { LOG_ERROR("could not open vchiq service"); status = MMAL_EIO; goto error; } client.usecount = 1; /* usecount set to 1 by the open call. */ service_initialised = 1; status = create_waitpool(&client.waitpool); if (status != MMAL_SUCCESS) { LOG_ERROR("could not create wait pool"); goto error; } waitpool_initialised = 1; if (vcos_mutex_create(&client.bulk_lock, "mmal client bulk lock") != VCOS_SUCCESS) { LOG_ERROR("could not create bulk lock"); status = MMAL_ENOSPC; goto error; } client.inited = 1; vcos_mutex_unlock(&client.lock); /* assume we're not using VC immediately. Do this outside the lock */ mmal_vc_release(); return MMAL_SUCCESS; error: if (waitpool_initialised) destroy_waitpool(&client.waitpool); if (service_initialised) { client.usecount = 0; vchiq_close_service(client.service); } if (vchiq_initialised) vchiq_shutdown(mmal_vchiq_instance); vcos_log_unregister(VCOS_LOG_CATEGORY); client.refcount--; vcos_mutex_unlock(&client.lock); return status; }
/** * main */ int main(int argc, const char **argv) { // Our main data storage vessel.. RASPIVID_STATE state; MMAL_STATUS_T status; MMAL_PORT_T *camera_preview_port = NULL; MMAL_PORT_T *camera_video_port = NULL; MMAL_PORT_T *camera_still_port = NULL; MMAL_PORT_T *preview_input_port = NULL; MMAL_PORT_T *encoder_input_port = NULL; MMAL_PORT_T *encoder_output_port = NULL; FILE *output_file = NULL; // Register our application with the logging system vcos_log_register("RaspiVid", VCOS_LOG_CATEGORY); printf("\nRaspiVid Camera App\n"); printf("===================\n\n"); signal(SIGINT, signal_handler); default_status(&state); // Do we have any parameters if (argc == 1) { display_valid_parameters(); exit(0); } // Parse the command line and put options in to our status structure if (parse_cmdline(argc, argv, &state)) { status = -1; exit(0); } if (state.verbose) dump_status(&state); // OK, we have a nice set of parameters. Now set up our components // We have three components. Camera, Preview and encoder. if (!create_camera_component(&state)) { vcos_log_error("%s: Failed to create camera component", __func__); } else if (!raspipreview_create(&state.preview_parameters)) { vcos_log_error("%s: Failed to create preview component", __func__); destroy_camera_component(&state); } else if (!create_encoder_component(&state)) { vcos_log_error("%s: Failed to create encode component", __func__); raspipreview_destroy(&state.preview_parameters); destroy_camera_component(&state); } else { PORT_USERDATA callback_data; if (state.verbose) printf("Starting component connection stage\n"); camera_preview_port = state.camera_component->output[MMAL_CAMERA_PREVIEW_PORT]; camera_video_port = state.camera_component->output[MMAL_CAMERA_VIDEO_PORT]; camera_still_port = state.camera_component->output[MMAL_CAMERA_CAPTURE_PORT]; preview_input_port = state.preview_parameters.preview_component->input[0]; encoder_input_port = state.encoder_component->input[0]; encoder_output_port = state.encoder_component->output[0]; if (state.preview_parameters.wantPreview ) { if (state.verbose) { printf("Connecting camera preview port to preview input port\n"); printf("Starting video preview\n"); } // Connect camera to preview status = connect_ports(camera_preview_port, preview_input_port, &state.preview_connection); } if (status == MMAL_SUCCESS) { VCOS_STATUS_T vcos_status; if (state.verbose) printf("Connecting camera stills port to encoder input port\n"); // Now connect the camera to the encoder status = connect_ports(camera_video_port, encoder_input_port, &state.encoder_connection); if (status != MMAL_SUCCESS) { vcos_log_error("%s: Failed to connect camera video port to encoder input", __func__); goto error; } if (state.filename) { if (state.verbose) printf("Opening output file %s\n", state.filename); output_file = fopen(state.filename, "wb"); if (!output_file) { // Notify user, carry on but discarding encoded output buffers vcos_log_error("%s: Error opening output file: %s\nNo output file will be generated\n", __func__, state.filename); } } // Set up our userdata - this is passed though to the callback where we need the information. callback_data.file_handle = output_file; callback_data.pstate = &state; vcos_status = vcos_semaphore_create(&callback_data.complete_semaphore, "RaspiStill-sem", 0); vcos_assert(vcos_status == VCOS_SUCCESS); encoder_output_port->userdata = (struct MMAL_PORT_USERDATA_T *)&callback_data; if (state.verbose) printf("Enabling encoder output port\n"); // Enable the encoder output port and tell it its callback function status = mmal_port_enable(encoder_output_port, encoder_buffer_callback); if (status != MMAL_SUCCESS) { vcos_log_error("Failed to setup encoder output"); goto error; } if (state.demoMode) { // Run for the user specific time.. int num_iterations = state.timeout / state.demoInterval; int i; printf("Running in demo mode\n"); for (i=0;i<num_iterations;i++) { raspicamcontrol_cycle_test(state.camera_component); vcos_sleep(state.demoInterval); } } else { // Only encode stuff if we have a filename and it opened if (output_file) { if (state.verbose) printf("Starting video capture\n"); if (mmal_port_parameter_set_boolean(camera_video_port, MMAL_PARAMETER_CAPTURE, 1) != MMAL_SUCCESS) { goto error; } // Send all the buffers to the encoder output port { int num = mmal_queue_length(state.encoder_pool->queue); int q; for (q=0;q<num;q++) { MMAL_BUFFER_HEADER_T *buffer = mmal_queue_get(state.encoder_pool->queue); if (!buffer) vcos_log_error("Unable to get a required buffer %d from pool queue", q); if (mmal_port_send_buffer(encoder_output_port, buffer)!= MMAL_SUCCESS) vcos_log_error("Unable to send a buffer to encoder output port (%d)", q); } } // Now wait until we need to stop vcos_sleep(state.timeout); printf("Finished capture\n"); } else vcos_sleep(state.timeout); } } else { mmal_status_to_int(status); vcos_log_error("%s: Failed to connect camera to preview", __func__); } error: mmal_status_to_int(status); if (state.verbose) printf("Closing down\n"); // Disable all our ports that are not handled by connections check_disable_port(camera_still_port); check_disable_port(encoder_output_port); if (state.preview_parameters.wantPreview ) mmal_connection_destroy(state.preview_connection); mmal_connection_destroy(state.encoder_connection); // Can now close our file. Note disabling ports may flush buffers which causes // problems if we have already closed the file! if (output_file) fclose(output_file); /* Disable components */ if (state.encoder_component) mmal_component_disable(state.encoder_component); if (state.preview_parameters.preview_component) mmal_component_disable(state.preview_parameters.preview_component); if (state.camera_component) mmal_component_disable(state.camera_component); destroy_encoder_component(&state); raspipreview_destroy(&state.preview_parameters); destroy_camera_component(&state); if (state.verbose) printf("Close down completed, all components disconnected, disabled and destroyed\n\n"); } return 0; }
/** * init_cam */ int init_cam(RASPIVID_STATE *state) { // Our main data storage vessel.. MMAL_STATUS_T status; MMAL_PORT_T *camera_video_port = NULL; MMAL_PORT_T *camera_still_port = NULL; MMAL_PORT_T *preview_input_port = NULL; //MMAL_PORT_T *encoder_input_port = NULL; // MMAL_PORT_T *encoder_output_port = NULL; bcm_host_init(); get_status(state); // Register our application with the logging system vcos_log_register("RaspiVid", VCOS_LOG_CATEGORY); signal(SIGINT, signal_handler); // OK, we have a nice set of parameters. Now set up our components // We have three components. Camera, Preview and encoder. if (!create_camera_component(state)) { ROS_INFO("%s: Failed to create camera component", __func__); /*else if ((status = create_encoder_component(state)) != MMAL_SUCCESS) { ROS_INFO("%s: Failed to create encode component", __func__); destroy_camera_component(state); }*/ } else { PORT_USERDATA * callback_data = (PORT_USERDATA *) malloc (sizeof(PORT_USERDATA)); camera_video_port = state->camera_component->output[MMAL_CAMERA_VIDEO_PORT]; camera_still_port = state->camera_component->output[MMAL_CAMERA_CAPTURE_PORT]; //encoder_input_port = state->encoder_component->input[0]; //encoder_output_port = state->encoder_component->output[0]; /*status = connect_ports(camera_video_port, encoder_input_port, &state->encoder_connection); if (status != MMAL_SUCCESS) { ROS_INFO("%s: Failed to connect camera video port to encoder input", __func__); return 1; }*/ callback_data->buffer[0] = (unsigned char *) malloc(state->width * state->height * 8); callback_data->buffer[1] = (unsigned char *) malloc(state->width * state->height * 8); // Set up our userdata - this is passed though to the callback where we need the information. callback_data->pstate = state; callback_data->abort = 0; callback_data->id = 0; callback_data->frame = 0; //encoder_output_port->userdata = (struct MMAL_PORT_USERDATA_T *) callback_data_enc; camera_video_port->userdata = (struct MMAL_PORT_USERDATA_T *) callback_data; //PORT_USERDATA *pData = (PORT_USERDATA *)encoder_output_port->userdata; PORT_USERDATA *pData = (PORT_USERDATA *)camera_video_port->userdata; // Enable the encoder output port and tell it its callback function //status = mmal_port_enable(encoder_output_port, encoder_buffer_callback); status = mmal_port_enable(camera_video_port, camera_buffer_callback); if (status != MMAL_SUCCESS) { ROS_INFO("Failed to setup encoder output"); return 1; } state->isInit = 1; } return 0; }
int main(int argc, char **argv) { // Parse command line options/arguments. VIDTEX_PARAMS_T params = {0}; int rc; int status = 0; opterr = 0; while ((rc = getopt(argc, argv, "d:iuvy")) != -1) { switch (rc) { case 'd': params.duration_ms = atoi(optarg); break; case 'i': params.opts |= VIDTEX_OPT_IMG_PER_FRAME; break; case 'y': params.opts |= VIDTEX_OPT_Y_TEXTURE; break; case 'u': params.opts |= VIDTEX_OPT_U_TEXTURE; break; case 'v': params.opts |= VIDTEX_OPT_V_TEXTURE; break; default: usage(argv[0]); return 2; } } if (optind < argc - 1) { usage(argv[0]); return 2; } if (optind < argc) { strncpy(params.uri, argv[optind], sizeof(params.uri) - 1); params.uri[sizeof(params.uri) - 1] = '\0'; } // Init vcos logging. vcos_log_set_level(VCOS_LOG_CATEGORY, VCOS_LOG_INFO); vcos_log_register("vidtex", VCOS_LOG_CATEGORY); // Run video-on-texture application in a separate thread. #ifdef __ANDROID__ status = Launcher::runApp("vidtex", launch_vidtex, ¶ms, sizeof(params)); #else status = runApp("vidtex", launch_vidtex, ¶ms, sizeof(params)); #endif return (status == 0) ? EXIT_SUCCESS : EXIT_FAILURE; }
int init_cam() { bcm_host_init(); // Register our application with the logging system vcos_log_register("RaspiStill", VCOS_LOG_CATEGORY); signal(SIGINT, signal_handler); default_status(&gState); if ((gStatus = create_camera_component(&gState)) != MMAL_SUCCESS) { vcos_log_error("%s: Failed to create camera component", __func__); gStatus = ! MMAL_SUCCESS; } else if ((gStatus = nullsink_preview(&gState.preview_parameters)) != MMAL_SUCCESS) { vcos_log_error("%s: Failed to create preview component", __func__); destroy_camera_component(&gState); gStatus = ! MMAL_SUCCESS; } else { gCamera_preview_port = gState.camera_component->output[MMAL_CAMERA_PREVIEW_PORT]; gCamera_video_port = gState.camera_component->output[MMAL_CAMERA_VIDEO_PORT]; gCamera_still_port = gState.camera_component->output[MMAL_CAMERA_CAPTURE_PORT]; // Note we are lucky that the preview and null sink components use the same input port // so we can simple do this without conditionals gPreview_input_port = gState.preview_parameters.preview_component->input[0]; // Connect camera to preview (which might be a null_sink if no preview required) gStatus = connect_ports(gCamera_preview_port, gPreview_input_port, &gState.preview_connection); if (gStatus == MMAL_SUCCESS) { VCOS_STATUS_T vcos_status; // Set up our userdata - this is passed though to the callback where we need the information. gCallback_data.pstate = &gState; vcos_status = vcos_semaphore_create(&gCallback_data.complete_semaphore, "RaspiStill-sem", 0); vcos_assert(vcos_status == VCOS_SUCCESS); gCamera_still_port->userdata = (struct MMAL_PORT_USERDATA_T *)&gCallback_data; // Enable the camera still output port and tell it its callback function gStatus = mmal_port_enable(gCamera_still_port, camera_buffer_callback); if (gStatus != MMAL_SUCCESS) { vcos_log_error("Failed to setup camera output"); error_cam(); } } else { mmal_status_to_int(gStatus); vcos_log_error("%s: Failed to connect camera to preview", __func__); } } if (gStatus != MMAL_SUCCESS) raspicamcontrol_check_configuration(128); return gStatus != MMAL_SUCCESS; }
/** * main */ int main(int argc, const char **argv) { // Our main data storage vessel.. RASPISTILLYUV_STATE state; int exit_code = EX_OK; MMAL_STATUS_T status = MMAL_SUCCESS; MMAL_PORT_T *camera_preview_port = NULL; MMAL_PORT_T *camera_video_port = NULL; MMAL_PORT_T *camera_still_port = NULL; MMAL_PORT_T *preview_input_port = NULL; FILE *output_file = NULL; bcm_host_init(); // Register our application with the logging system vcos_log_register("RaspiStill", VCOS_LOG_CATEGORY); signal(SIGINT, signal_handler); // Disable USR1 for the moment - may be reenabled if go in to signal capture mode signal(SIGUSR1, SIG_IGN); default_status(&state); // Do we have any parameters if (argc == 1) { fprintf(stdout, "\n%s Camera App %s\n\n", basename(argv[0]), VERSION_STRING); display_valid_parameters(basename(argv[0])); exit(EX_USAGE); } default_status(&state); // Parse the command line and put options in to our status structure if (parse_cmdline(argc, argv, &state)) { status = -1; exit(EX_USAGE); } if (state.verbose) { fprintf(stderr, "\n%s Camera App %s\n\n", basename(argv[0]), VERSION_STRING); dump_status(&state); } // OK, we have a nice set of parameters. Now set up our components // We have two components. Camera and Preview // Camera is different in stills/video, but preview // is the same so handed off to a separate module if ((status = create_camera_component(&state)) != MMAL_SUCCESS) { vcos_log_error("%s: Failed to create camera component", __func__); exit_code = EX_SOFTWARE; } else if ((status = raspipreview_create(&state.preview_parameters)) != MMAL_SUCCESS) { vcos_log_error("%s: Failed to create preview component", __func__); destroy_camera_component(&state); exit_code = EX_SOFTWARE; } else { PORT_USERDATA callback_data; if (state.verbose) fprintf(stderr, "Starting component connection stage\n"); camera_preview_port = state.camera_component->output[MMAL_CAMERA_PREVIEW_PORT]; camera_video_port = state.camera_component->output[MMAL_CAMERA_VIDEO_PORT]; camera_still_port = state.camera_component->output[MMAL_CAMERA_CAPTURE_PORT]; // Note we are lucky that the preview and null sink components use the same input port // so we can simple do this without conditionals preview_input_port = state.preview_parameters.preview_component->input[0]; // Connect camera to preview (which might be a null_sink if no preview required) status = connect_ports(camera_preview_port, preview_input_port, &state.preview_connection); if (status == MMAL_SUCCESS) { VCOS_STATUS_T vcos_status; // Set up our userdata - this is passed though to the callback where we need the information. // Null until we open our filename callback_data.file_handle = NULL; callback_data.pstate = &state; vcos_status = vcos_semaphore_create(&callback_data.complete_semaphore, "RaspiStill-sem", 0); vcos_assert(vcos_status == VCOS_SUCCESS); camera_still_port->userdata = (struct MMAL_PORT_USERDATA_T *)&callback_data; if (state.verbose) fprintf(stderr, "Enabling camera still output port\n"); // Enable the camera still output port and tell it its callback function status = mmal_port_enable(camera_still_port, camera_buffer_callback); if (status != MMAL_SUCCESS) { vcos_log_error("Failed to setup camera output"); goto error; } if (state.verbose) fprintf(stderr, "Starting video preview\n"); int frame, keep_looping = 1; FILE *output_file = NULL; char *use_filename = NULL; // Temporary filename while image being written char *final_filename = NULL; // Name that file gets once writing complete frame = 0; while (keep_looping) { keep_looping = wait_for_next_frame(&state, &frame); // Open the file if (state.filename) { if (state.filename[0] == '-') { output_file = stdout; // Ensure we don't upset the output stream with diagnostics/info state.verbose = 0; } else { vcos_assert(use_filename == NULL && final_filename == NULL); status = create_filenames(&final_filename, &use_filename, state.filename, frame); if (status != MMAL_SUCCESS) { vcos_log_error("Unable to create filenames"); goto error; } if (state.verbose) fprintf(stderr, "Opening output file %s\n", final_filename); // Technically it is opening the temp~ filename which will be ranamed to the final filename output_file = fopen(use_filename, "wb"); if (!output_file) { // Notify user, carry on but discarding encoded output buffers vcos_log_error("%s: Error opening output file: %s\nNo output file will be generated\n", __func__, use_filename); } } callback_data.file_handle = output_file; } if (output_file) { int num, q; // There is a possibility that shutter needs to be set each loop. if (mmal_status_to_int(mmal_port_parameter_set_uint32(state.camera_component->control, MMAL_PARAMETER_SHUTTER_SPEED, state.camera_parameters.shutter_speed) != MMAL_SUCCESS)) vcos_log_error("Unable to set shutter speed"); // Send all the buffers to the camera output port num = mmal_queue_length(state.camera_pool->queue); for (q=0;q<num;q++) { MMAL_BUFFER_HEADER_T *buffer = mmal_queue_get(state.camera_pool->queue); if (!buffer) vcos_log_error("Unable to get a required buffer %d from pool queue", q); if (mmal_port_send_buffer(camera_still_port, buffer)!= MMAL_SUCCESS) vcos_log_error("Unable to send a buffer to camera output port (%d)", q); } if (state.burstCaptureMode && frame==1) { mmal_port_parameter_set_boolean(state.camera_component->control, MMAL_PARAMETER_CAMERA_BURST_CAPTURE, 1); } if (state.verbose) fprintf(stderr, "Starting capture %d\n", frame); if (mmal_port_parameter_set_boolean(camera_still_port, MMAL_PARAMETER_CAPTURE, 1) != MMAL_SUCCESS) { vcos_log_error("%s: Failed to start capture", __func__); } else { // Wait for capture to complete // For some reason using vcos_semaphore_wait_timeout sometimes returns immediately with bad parameter error // even though it appears to be all correct, so reverting to untimed one until figure out why its erratic vcos_semaphore_wait(&callback_data.complete_semaphore); if (state.verbose) fprintf(stderr, "Finished capture %d\n", frame); } // Ensure we don't die if get callback with no open file callback_data.file_handle = NULL; if (output_file != stdout) { rename_file(&state, output_file, final_filename, use_filename, frame); } } if (use_filename) { free(use_filename); use_filename = NULL; } if (final_filename) { free(final_filename); final_filename = NULL; } } // end for (frame) vcos_semaphore_delete(&callback_data.complete_semaphore); } else { mmal_status_to_int(status); vcos_log_error("%s: Failed to connect camera to preview", __func__); } error: mmal_status_to_int(status); if (state.verbose) fprintf(stderr, "Closing down\n"); if (output_file) fclose(output_file); // Disable all our ports that are not handled by connections check_disable_port(camera_video_port); if (state.preview_connection) mmal_connection_destroy(state.preview_connection); /* Disable components */ if (state.preview_parameters.preview_component) mmal_component_disable(state.preview_parameters.preview_component); if (state.camera_component) mmal_component_disable(state.camera_component); raspipreview_destroy(&state.preview_parameters); destroy_camera_component(&state); if (state.verbose) fprintf(stderr, "Close down completed, all components disconnected, disabled and destroyed\n\n"); } if (status != MMAL_SUCCESS) raspicamcontrol_check_configuration(128); return exit_code; }
int main( int argc, char **argv ) { int32_t ret; char optstring[OPTSTRING_LEN]; int opt; int opt_alloc = 0; int opt_status = 0; uint32_t alloc_size = 0; int opt_pid = -1; VCSM_STATUS_T status_mode = VCSM_STATUS_NONE; void *usr_ptr_1; unsigned int usr_hdl_1; #if defined(DOUBLE_ALLOC) || defined(RESIZE_ALLOC) void *usr_ptr_2; unsigned int usr_hdl_2; #endif // Initialize VCOS vcos_init(); vcos_log_set_level(&smem_log_category, VCOS_LOG_INFO); smem_log_category.flags.want_prefix = 0; vcos_log_register( "smem", &smem_log_category ); // Create the option string that we will be using to parse the arguments create_optstring( optstring ); // Parse the command line arguments while (( opt = getopt_long_only( argc, argv, optstring, long_opts, NULL )) != -1 ) { switch ( opt ) { case 0: { // getopt_long returns 0 for entries where flag is non-NULL break; } case OPT_ALLOC: { char *end; alloc_size = (uint32_t)strtoul( optarg, &end, 10 ); if (end == optarg) { vcos_log_info( "Invalid arguments '%s'", optarg ); goto err_out; } opt_alloc = 1; break; } case OPT_PID: { char *end; opt_pid = (int)strtol( optarg, &end, 10 ); if (end == optarg) { vcos_log_info( "Invalid arguments '%s'", optarg ); goto err_out; } break; } case OPT_STATUS: { char status_str[32]; /* coverity[secure_coding] String length specified, so can't overflow */ if ( sscanf( optarg, "%31s", status_str ) != 1 ) { vcos_log_info( "Invalid arguments '%s'", optarg ); goto err_out; } if ( vcos_strcasecmp( status_str, "all" ) == 0 ) { status_mode = VCSM_STATUS_VC_MAP_ALL; } else if ( vcos_strcasecmp( status_str, "vc" ) == 0 ) { status_mode = VCSM_STATUS_VC_WALK_ALLOC; } else if ( vcos_strcasecmp( status_str, "map" ) == 0 ) { status_mode = VCSM_STATUS_HOST_WALK_MAP; } else if ( vcos_strcasecmp( status_str, "host" ) == 0 ) { status_mode = VCSM_STATUS_HOST_WALK_PID_ALLOC; } else { goto err_out; } opt_status = 1; break; } default: { vcos_log_info( "Unrecognized option '%d'", opt ); goto err_usage; } case '?': case OPT_HELP: { goto err_usage; } } // end switch } // end while argc -= optind; argv += optind; if (( optind == 1 ) || ( argc > 0 )) { if ( argc > 0 ) { vcos_log_info( "Unrecognized argument -- '%s'", *argv ); } goto err_usage; } // Start the shared memory support. if ( vcsm_init() == -1 ) { vcos_log_info( "Cannot initialize smem device" ); goto err_out; } if ( opt_alloc == 1 ) { vcos_log_info( "Allocating 2 times %u-bytes in shared memory", alloc_size ); usr_hdl_1 = vcsm_malloc( alloc_size, "smem-test-alloc" ); vcos_log_info( "Allocation 1 result: user %x, vc-hdl %x", usr_hdl_1, vcsm_vc_hdl_from_hdl( usr_hdl_1 ) ); #if defined(DOUBLE_ALLOC) || defined(RESIZE_ALLOC) usr_hdl_2 = vcsm_malloc( alloc_size, NULL ); vcos_log_info( "Allocation 2 result: user %x", usr_hdl_2 ); usr_ptr_2 = vcsm_lock( usr_hdl_2 ); vcos_log_info( "Allocation 2 : lock %p", usr_ptr_2 ); vcos_log_info( "Allocation 2 : unlock %d", vcsm_unlock_hdl( usr_hdl_2 ) ); #endif // Do a simple write/read test. if ( usr_hdl_1 != 0 ) { usr_ptr_1 = vcsm_lock( usr_hdl_1 ); vcos_log_info( "Allocation 1 : lock %p", usr_ptr_1 ); if ( usr_ptr_1 ) { memset ( usr_ptr_1, 0, alloc_size ); memcpy ( usr_ptr_1, blah_blah, 32 ); vcos_log_info( "Allocation 1 contains: \"%s\"", (char *)usr_ptr_1 ); vcos_log_info( "Allocation 1: vc-hdl %x", vcsm_vc_hdl_from_ptr ( usr_ptr_1 ) ); vcos_log_info( "Allocation 1: usr-hdl %x", vcsm_usr_handle ( usr_ptr_1 ) ); vcos_log_info( "Allocation 1 : unlock %d", vcsm_unlock_ptr( usr_ptr_1 ) ); } usr_ptr_1 = vcsm_lock( usr_hdl_1 ); vcos_log_info( "Allocation 1 (relock) : lock %p", usr_ptr_1 ); if ( usr_ptr_1 ) { vcos_log_info( "Allocation 1 (relock) : unlock %d", vcsm_unlock_hdl( usr_hdl_1 ) ); } } #if defined(RESIZE_ALLOC) ret = vcsm_resize( usr_hdl_1, 2 * alloc_size ); vcos_log_info( "Allocation 1 : resize %d", ret ); if ( ret == 0 ) { usr_ptr_1 = vcsm_lock( usr_hdl_1 ); vcos_log_info( "Allocation 1 (resize) : lock %p", usr_ptr_1 ); if ( usr_ptr_1 ) { memset ( usr_ptr_1, 0, 2 * alloc_size ); memcpy ( usr_ptr_1, blah_blah, 32 ); vcos_log_info( "Allocation 1 (resized) contains: \"%s\"", (char *)usr_ptr_1 ); vcos_log_info( "Allocation 1 (resized) : unlock %d", vcsm_unlock_ptr( usr_ptr_1 ) ); } } // This checks that the memory can be remapped properly // because the Block 1 expanded beyond Block 2 boundary. // usr_ptr_2 = vcsm_lock( usr_hdl_2 ); vcos_log_info( "Allocation 2 : lock %p", usr_ptr_2 ); vcos_log_info( "Allocation 2 : unlock %d", vcsm_unlock_hdl( usr_hdl_2 ) ); // This checks that we can free a memory block even if it // is locked, which could be the case if the application // dies. // usr_ptr_2 = vcsm_lock( usr_hdl_2 ); vcos_log_info( "Allocation 2 : lock %p", usr_ptr_2 ); vcsm_free ( usr_hdl_2 ); #endif #if defined(DOUBLE_ALLOC) #endif } if ( opt_status == 1 ) { get_status( status_mode, opt_pid ); } // If we allocated something, wait for the signal to exit to give chance for the // user to poke around the allocation test. // if ( opt_alloc == 1 ) { start_monitor(); vcos_event_wait( &quit_event ); vcos_event_delete( &quit_event ); } // Terminate the shared memory support. vcsm_exit (); goto err_out; err_usage: show_usage(); err_out: exit( 1 ); }