GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — OpenMaxIL-cpp/samples/camera_threaded.cpp at master · dridri/OpenMaxIL-cpp Expand file tree/ Copy pathcamera_threaded.cppMore file actions 169 lines (137 loc) · 4.91 KB / Copy pathcamera_threaded.cppFile metadata and controls169 lines (137 loc) · 4.91 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 #include <unistd.h> #include <signal.h> #include <pthread.h> #include "../include/Camera.h" #include "../include/VideoEncode.h" #include "../include/VideoDecode.h" #include "../include/VideoRender.h" #include <fstream> #include <iostream> using namespace IL; struct State { Camera* camera; VideoEncode* preview_encode; VideoEncode* record_encode; bool recording; // TODO : set this variable to true from an external thread to activate recording bool running; pthread_t preview_thid; pthread_t record_thid; } state = { nullptr, nullptr, nullptr, false, false }; void terminate( int sig ) { state.running = false; pthread_join( state.record_thid, nullptr ); pthread_join( state.preview_thid, nullptr ); exit(0); } void process_image( uint8_t* data ) { uint8_t* y_plane = data; uint8_t* u_plane = &data[ 1280 * 720 ]; uint8_t* v_plane = &data[ 1280 * 720 + ( 1280 / 2 ) * ( 720 / 2 ) ]; // TODO : process image here (i.e. pass it to OpenCV, export to external program, ...) } void* preview_thread( void* argp ) { #if 0 VideoDecode* decode = new VideoDecode( 1280, 720, 0, VideoDecode::CodingMJPEG, true ); VideoRender* render = new VideoRender(); decode->SetupTunnel( render ); decode->SetState( Component::StateIdle ); render->SetState( Component::StateIdle ); decode->SetState( Component::StateExecuting ); render->SetState( Component::StateExecuting ); #endif bool zero_copy = true; uint8_t* data = nullptr; uint8_t* mjpeg_data = nullptr; if ( not zero_copy ) { // Allocate space for image data = new uint8_t[1280*720*sizeof(uint16_t)]; mjpeg_data = new uint8_t[80000]; } // ATTENTION : Each loop must take less time than it takes to the camera to take one frame // otherwise it will cause underflow which can lead to camera stalling // a good solution is to implement frame skipping (measure time between two loops, if this time // is too big, just skip image processing and MJPEG sendout) while ( state.running ) { if ( zero_copy ) { // Retrieve OMX buffer data = state.camera->outputPorts()[70].buffer->pBuffer; mjpeg_data = state.preview_encode->outputPorts()[201].buffer->pBuffer; } // Get YUV420 image from preview port, this is a blocking call // If zero-copy is activated, we don't pass any buffer int32_t datalen = state.camera->getOutputData( 70, zero_copy ? nullptr : data ); if ( datalen > 0 ) { // Send it to the MJPEG encoder state.preview_encode->fillInput( 200, data, datalen, false, true ); // Process the image process_image( data ); } while ( ( datalen = state.preview_encode->getOutputData( zero_copy ? nullptr : mjpeg_data, false ) ) > 0 ) { // TODO : send MJPEG data to a client, 'data' contains exactly one coded frame which is 'datalen' long #if 0 decode->fillInput( mjpeg_data, datalen ); #endif } } return nullptr; } void* record_thread( void* argp ) { uint8_t* data = new uint8_t[65536]; while ( state.running ) { // Consume h264 data, this is a blocking call int32_t datalen = state.record_encode->getOutputData( data ); if ( datalen > 0 && state.recording ) { // TODO : save data somewhere } } return nullptr; } int main( int ac, char** av ) { // We register signal handler to properly destroy OpenMAX context (by calling exit() in the handler) static int sig_list[] = { 2, 3, 6, 7, 9, 11, 15 }; uint32_t i; for ( i = 0; i <= sizeof(sig_list)/sizeof(int); i++ ) { struct sigaction sa; memset( &sa, 0, sizeof(sa) ); sa.sa_handler = &terminate; sigaction( sig_list[i], &sa, NULL ); } // Initialize hardware bcm_host_init(); // Create components state.camera = new Camera( 1280, 720, 0, false, 0, false ); state.preview_encode = new VideoEncode( 8192, VideoEncode::CodingMJPEG, false, false ); state.record_encode = new VideoEncode( 4096, VideoEncode::CodingAVC, false, false ); // Setup camera state.camera->setFramerate( 30 ); // Copy preview port definition to the encoder to help it handle incoming data Component::CopyPort( &state.camera->outputPorts()[70], &state.preview_encode->inputPorts()[200] ); // Tunnel video port to AVC encoder state.camera->SetupTunnelVideo( state.record_encode ); // Prepare components for next step state.camera->SetState( Component::StateIdle ); state.preview_encode->SetState( Component::StateIdle ); state.record_encode->SetState( Component::StateIdle ); // Allocate buffers that will be processed manually state.camera->AllocateOutputBuffer( 70 ); state.preview_encode->AllocateInputBuffer( 200 ); // Start components state.camera->SetState( Component::StateExecuting ); state.preview_encode->SetState( Component::StateExecuting ); state.record_encode->SetState( Component::StateExecuting ); // Start capturing state.camera->SetCapturing( true ); state.running = true; // Start threads pthread_create( &state.preview_thid, nullptr, &preview_thread, nullptr ); pthread_create( &state.record_thid, nullptr, &record_thread, nullptr ); while ( 1 ) { usleep( 1000 * 1000 ); } return 0; } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |