Index: /branches/sanscv/lib/FlairSensorActuator/src/V4LCamera.cpp
===================================================================
--- /branches/sanscv/lib/FlairSensorActuator/src/V4LCamera.cpp	(revision 334)
+++ /branches/sanscv/lib/FlairSensorActuator/src/V4LCamera.cpp	(revision 335)
@@ -62,15 +62,5 @@
         Thread::Err("device is unable to capture video memory.\n");
     }
-  /*  
-   * unnecessary?
-    struct video_capability capability;
-    memset(&capability, 0, sizeof (video_capability));
-    capability.type = cap.capabilities;
-     
-    // Query channels number
-    if (xioctl(device, VIDIOC_G_INPUT, &capability.channels)==-1) {
-        Thread::Err("VIDIOC_G_INPUT xioctl\n");
-    }
-    */
+
     //get v4l2_format
     struct v4l2_format form;
@@ -105,8 +95,4 @@
         QueueBuffer(bufferIndex);
     }
-    
-    //alloc img buf
-    Printf("TODO: alloc bufs in cmem and uses it for v4l\n");
-    output->buffer=AllocFunction(width*height*2);
     
     // enable the streaming
@@ -138,5 +124,7 @@
 
 V4LCamera::~V4LCamera() {
-    FreeFunction(output->buffer);
+    for (int n_buffers = 0; n_buffers < nbBuffers; n_buffers++) {
+       FreeFunction((char*)buffers[n_buffers].start);
+   }
     SafeStop();
     Join();
@@ -220,7 +208,7 @@
       hasProblems=true;
     }
-//Printf("todo allocate 1 frame and copy here\n");
+
     output->GetMutex();
-    memcpy(output->buffer,buffers[bufferIndex].start,buffers[bufferIndex].length);
+    output->buffer=(char*)buffers[bufferIndex].start;
     output->ReleaseMutex();
 
@@ -239,9 +227,12 @@
         memset(&buf, 0, sizeof (v4l2_buffer));
         buf.type        = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-        buf.memory      = V4L2_MEMORY_MMAP;
+        buf.memory      = V4L2_MEMORY_USERPTR;//V4L2_MEMORY_MMAP;
         buf.index       = (unsigned long)index;
-
-        if (xioctl (device, VIDIOC_QBUF, &buf)==-1) {
-            Thread::Err("VIDIOC_QBUF xioctl\n");
+        buf.m.userptr=(unsigned long)(buffers[index].start);
+        buf.length=buffers[index].length;
+        
+        int ret=xioctl (device, VIDIOC_QBUF, &buf);
+        if (ret==-1) {
+            Thread::Err("VIDIOC_QBUF xioctl %s\n",strerror(-ret));
             return -1;
         }
@@ -261,7 +252,7 @@
     tv.tv_sec = 0;
     tv.tv_usec = 100000;
-//Printf("%lld\n",GetTimeMS());
+
     int r = select (device+1, &fds, NULL, NULL, &tv);
-//Printf("%lld select ok\n",GetTimeMS());
+
     if (-1 == r) {
         char errorMsg[256];
@@ -278,14 +269,11 @@
     memset(&buf, 0, sizeof (v4l2_buffer));
     buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-    buf.memory = V4L2_MEMORY_MMAP;
+    buf.memory = V4L2_MEMORY_USERPTR;//V4L2_MEMORY_MMAP;
 
     //get last captured image
     int prevDQbuf=-1;
     for(int i=0;i<4;i++) {
-        //Printf("%lld ioctl\n",GetTimeMS());
         if (xioctl (device, VIDIOC_DQBUF, &buf)==-1) {
-            //Printf("iter %i err %i\n",i,errno);
             if (errno==EAGAIN) {
-                //Printf("exit buf %i\n",buf.index);
                 break;
             } else {
@@ -294,21 +282,17 @@
             }
        } else {
-           //Printf("buf %i\n",buf.index);
            if(prevDQbuf!=-1) {
                QueueBuffer(prevDQbuf);
-               //Printf("queued %i\n",prevDQbuf);
            }
-           prevDQbuf=buf.index;
+           for (int n_buffers = 0; n_buffers < nbBuffers; n_buffers++) {
+               if((void*)(buf.m.userptr)==buffers[n_buffers].start) {
+                   prevDQbuf=n_buffers;
+                   bufferIndex=n_buffers;
+                   break;
+               }
+           }
        }
-       //Printf("%lld ioctl ok\n",GetTimeMS());
-    }
-
-   if(buf.index >= nbBuffers) {
-     Thread::Err("buf.index >= nbBuffers\n");
-     return -1;
-   }
+    }
    
-    bufferIndex=buf.index;
-  
    return 1;
 }
@@ -324,9 +308,9 @@
    requestbuffers.count = buffer_number;
    requestbuffers.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-   requestbuffers.memory = V4L2_MEMORY_MMAP;
+   requestbuffers.memory = V4L2_MEMORY_USERPTR;//V4L2_MEMORY_MMAP;
 
    if (xioctl (device, VIDIOC_REQBUFS, &requestbuffers)==-1) {
-       if (EINVAL == errno) {
-         Thread::Err("memory mapping not supported\n");
+       if (errno==EINVAL) {
+         Thread::Err("VIDIOC_REQBUFS user memory not supported\n");
        } else {
          Thread::Err ("VIDIOC_REQBUFS xioctl\n");
@@ -334,45 +318,11 @@
        return -1;
    }
-
-   if (requestbuffers.count < buffer_number) {
-       if (buffer_number == 1) {
-        Thread::Err("Insufficient buffer memory\n");
-        return -1;
-       } else {
-        buffer_number--;
-        Thread::Warn ("Insufficient buffer memory, decreasing buffers\n");
-        goto try_again;
-       }
+   
+   nbBuffers=DEFAULT_V4L_BUFFERS;
+   for (int n_buffers = 0; n_buffers < nbBuffers; n_buffers++) {
+       buffers[n_buffers].length = output->GetDataType().GetSize();
+       buffers[n_buffers].start =AllocFunction(output->GetDataType().GetSize());
    }
 
-   for (int n_buffers = 0; n_buffers < requestbuffers.count; ++n_buffers) {
-       struct v4l2_buffer buf;
-
-       memset(&buf, 0, sizeof (v4l2_buffer));
-
-       buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-       buf.memory = V4L2_MEMORY_MMAP;
-       buf.index = n_buffers;
-
-       if (xioctl (device, VIDIOC_QUERYBUF, &buf)==-1) {
-           Thread::Err("VIDIOC_QUERYBUF xioctl\n");
-           return -1;
-       }
-
-       buffers[n_buffers].length = buf.length;
-       buffers[n_buffers].start =
-         mmap (NULL /* start anywhere */,
-               buf.length,
-               PROT_READ | PROT_WRITE /* required */,
-               MAP_SHARED /* recommended */,
-               device, buf.m.offset);
-//Printf("buffer %i start %x length %i\n",n_buffers,buffers[n_buffers].start,buffers[n_buffers].length);
-       if (MAP_FAILED == buffers[n_buffers].start) {
-           Thread::Err("mmap\n");
-           return -1;
-       }
-   }
-   nbBuffers=requestbuffers.count;
-//Printf("allocated %i buffers\n",nbBuffers);
    return 1;
 };
