- Timestamp:
- Oct 2, 2019, 4:06:31 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/sanscv/lib/FlairSensorActuator/src/V4LCamera.cpp
r328 r335 62 62 Thread::Err("device is unable to capture video memory.\n"); 63 63 } 64 /* 65 * unnecessary? 66 struct video_capability capability; 67 memset(&capability, 0, sizeof (video_capability)); 68 capability.type = cap.capabilities; 69 70 // Query channels number 71 if (xioctl(device, VIDIOC_G_INPUT, &capability.channels)==-1) { 72 Thread::Err("VIDIOC_G_INPUT xioctl\n"); 73 } 74 */ 64 75 65 //get v4l2_format 76 66 struct v4l2_format form; … … 105 95 QueueBuffer(bufferIndex); 106 96 } 107 108 //alloc img buf109 Printf("TODO: alloc bufs in cmem and uses it for v4l\n");110 output->buffer=AllocFunction(width*height*2);111 97 112 98 // enable the streaming … … 138 124 139 125 V4LCamera::~V4LCamera() { 140 FreeFunction(output->buffer); 126 for (int n_buffers = 0; n_buffers < nbBuffers; n_buffers++) { 127 FreeFunction((char*)buffers[n_buffers].start); 128 } 141 129 SafeStop(); 142 130 Join(); … … 220 208 hasProblems=true; 221 209 } 222 //Printf("todo allocate 1 frame and copy here\n"); 210 223 211 output->GetMutex(); 224 memcpy(output->buffer,buffers[bufferIndex].start,buffers[bufferIndex].length);212 output->buffer=(char*)buffers[bufferIndex].start; 225 213 output->ReleaseMutex(); 226 214 … … 239 227 memset(&buf, 0, sizeof (v4l2_buffer)); 240 228 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 241 buf.memory = V4L2_MEMORY_ MMAP;229 buf.memory = V4L2_MEMORY_USERPTR;//V4L2_MEMORY_MMAP; 242 230 buf.index = (unsigned long)index; 243 244 if (xioctl (device, VIDIOC_QBUF, &buf)==-1) { 245 Thread::Err("VIDIOC_QBUF xioctl\n"); 231 buf.m.userptr=(unsigned long)(buffers[index].start); 232 buf.length=buffers[index].length; 233 234 int ret=xioctl (device, VIDIOC_QBUF, &buf); 235 if (ret==-1) { 236 Thread::Err("VIDIOC_QBUF xioctl %s\n",strerror(-ret)); 246 237 return -1; 247 238 } … … 261 252 tv.tv_sec = 0; 262 253 tv.tv_usec = 100000; 263 //Printf("%lld\n",GetTimeMS()); 254 264 255 int r = select (device+1, &fds, NULL, NULL, &tv); 265 //Printf("%lld select ok\n",GetTimeMS()); 256 266 257 if (-1 == r) { 267 258 char errorMsg[256]; … … 278 269 memset(&buf, 0, sizeof (v4l2_buffer)); 279 270 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 280 buf.memory = V4L2_MEMORY_ MMAP;271 buf.memory = V4L2_MEMORY_USERPTR;//V4L2_MEMORY_MMAP; 281 272 282 273 //get last captured image 283 274 int prevDQbuf=-1; 284 275 for(int i=0;i<4;i++) { 285 //Printf("%lld ioctl\n",GetTimeMS());286 276 if (xioctl (device, VIDIOC_DQBUF, &buf)==-1) { 287 //Printf("iter %i err %i\n",i,errno);288 277 if (errno==EAGAIN) { 289 //Printf("exit buf %i\n",buf.index);290 278 break; 291 279 } else { … … 294 282 } 295 283 } else { 296 //Printf("buf %i\n",buf.index);297 284 if(prevDQbuf!=-1) { 298 285 QueueBuffer(prevDQbuf); 299 //Printf("queued %i\n",prevDQbuf);300 286 } 301 prevDQbuf=buf.index; 287 for (int n_buffers = 0; n_buffers < nbBuffers; n_buffers++) { 288 if((void*)(buf.m.userptr)==buffers[n_buffers].start) { 289 prevDQbuf=n_buffers; 290 bufferIndex=n_buffers; 291 break; 292 } 293 } 302 294 } 303 //Printf("%lld ioctl ok\n",GetTimeMS()); 304 } 305 306 if(buf.index >= nbBuffers) { 307 Thread::Err("buf.index >= nbBuffers\n"); 308 return -1; 309 } 295 } 310 296 311 bufferIndex=buf.index;312 313 297 return 1; 314 298 } … … 324 308 requestbuffers.count = buffer_number; 325 309 requestbuffers.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 326 requestbuffers.memory = V4L2_MEMORY_ MMAP;310 requestbuffers.memory = V4L2_MEMORY_USERPTR;//V4L2_MEMORY_MMAP; 327 311 328 312 if (xioctl (device, VIDIOC_REQBUFS, &requestbuffers)==-1) { 329 if ( EINVAL == errno) {330 Thread::Err(" memory mappingnot supported\n");313 if (errno==EINVAL) { 314 Thread::Err("VIDIOC_REQBUFS user memory not supported\n"); 331 315 } else { 332 316 Thread::Err ("VIDIOC_REQBUFS xioctl\n"); … … 334 318 return -1; 335 319 } 336 337 if (requestbuffers.count < buffer_number) { 338 if (buffer_number == 1) { 339 Thread::Err("Insufficient buffer memory\n"); 340 return -1; 341 } else { 342 buffer_number--; 343 Thread::Warn ("Insufficient buffer memory, decreasing buffers\n"); 344 goto try_again; 345 } 320 321 nbBuffers=DEFAULT_V4L_BUFFERS; 322 for (int n_buffers = 0; n_buffers < nbBuffers; n_buffers++) { 323 buffers[n_buffers].length = output->GetDataType().GetSize(); 324 buffers[n_buffers].start =AllocFunction(output->GetDataType().GetSize()); 346 325 } 347 326 348 for (int n_buffers = 0; n_buffers < requestbuffers.count; ++n_buffers) {349 struct v4l2_buffer buf;350 351 memset(&buf, 0, sizeof (v4l2_buffer));352 353 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;354 buf.memory = V4L2_MEMORY_MMAP;355 buf.index = n_buffers;356 357 if (xioctl (device, VIDIOC_QUERYBUF, &buf)==-1) {358 Thread::Err("VIDIOC_QUERYBUF xioctl\n");359 return -1;360 }361 362 buffers[n_buffers].length = buf.length;363 buffers[n_buffers].start =364 mmap (NULL /* start anywhere */,365 buf.length,366 PROT_READ | PROT_WRITE /* required */,367 MAP_SHARED /* recommended */,368 device, buf.m.offset);369 //Printf("buffer %i start %x length %i\n",n_buffers,buffers[n_buffers].start,buffers[n_buffers].length);370 if (MAP_FAILED == buffers[n_buffers].start) {371 Thread::Err("mmap\n");372 return -1;373 }374 }375 nbBuffers=requestbuffers.count;376 //Printf("allocated %i buffers\n",nbBuffers);377 327 return 1; 378 328 };
Note:
See TracChangeset
for help on using the changeset viewer.