def _create_buffer(self, buffer_size=30):
        req = v4l2.v4l2_requestbuffers()
        req.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
        req.memory = v4l2.V4L2_MEMORY_MMAP
        req.count = buffer_size  # nr of buffer frames
        fcntl.ioctl(self.fd, v4l2.VIDIOC_REQBUFS,
                    req)  # tell the driver that we want some buffers

        self.buffers = []
        for ind in range(req.count):
            # setup a buffer
            buf = v4l2.v4l2_buffer()
            buf.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
            buf.memory = v4l2.V4L2_MEMORY_MMAP
            buf.index = ind
            fcntl.ioctl(self.fd, v4l2.VIDIOC_QUERYBUF, buf)

            mm = mmap.mmap(self.fd.fileno(),
                           buf.length,
                           mmap.MAP_SHARED,
                           mmap.PROT_READ | mmap.PROT_WRITE,
                           offset=buf.m.offset)
            self.buffers.append(mm)

            # queue the buffer for capture
            fcntl.ioctl(self.fd, v4l2.VIDIOC_QBUF, buf)
Exemple #2
0
    def create_buffers(self, count):
        # Create buffers
        rbuf = v4l2.v4l2_requestbuffers()
        rbuf.count = count
        rbuf.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
        rbuf.memory = v4l2.V4L2_MEMORY_MMAP

        fcntl.ioctl(self, v4l2.VIDIOC_REQBUFS, rbuf)

        for i in range(rbuf.count):
            # Get buffer
            buf = v4l2.v4l2_buffer()
            buf.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
            buf.memory = v4l2.V4L2_MEMORY_MMAP
            buf.index = i
            fcntl.ioctl(self, v4l2.VIDIOC_QUERYBUF, buf)

            # Mem map buffer
            mm = mmap.mmap(self.fileno(),
                           buf.length,
                           mmap.MAP_SHARED,
                           mmap.PROT_READ | mmap.PROT_WRITE,
                           offset=buf.m.offset)
            self.buffers.append(mm)

            # Queue the buffer for capture
            fcntl.ioctl(self, v4l2.VIDIOC_QBUF, buf)
Exemple #3
0
    def _alloc_buffers(self):
        req = v4l2.v4l2_requestbuffers()
        req.count = config.V4l2ReqBuffersCount
        req.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
        req.memory = v4l2.V4L2_MEMORY_MMAP
        if fcntl.ioctl(self.camera, v4l2.VIDIOC_REQBUFS, req):
            print 'Failed to request buffers...'

        self.bufferscount = req.count
        print 'request buffers:%d' % self.bufferscount

        self._buffers = []
        self._mmaps = []
        for cnt in range(0, req.count):
            buf = v4l2.v4l2_buffer()
            self._buffers.append(buf)
            buf.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
            buf.memory = v4l2.V4L2_MEMORY_MMAP
            buf.index = cnt
            if fcntl.ioctl(self.camera, v4l2.VIDIOC_QUERYBUF, buf):
                print 'failed to query buffers...'
            else:
                print 'index:%d, length:%d' % (buf.index, buf.length)

            self._mmaps.append(
                mmap.mmap(self.camera,
                          buf.length,
                          mmap.MAP_SHARED,
                          mmap.PROT_READ | mmap.PROT_WRITE,
                          offset=buf.m.offset))
            fcntl.ioctl(self.camera, v4l2.VIDIOC_QBUF, buf)
Exemple #4
0
    def init_mmap(self):
        req = v4l2.v4l2_requestbuffers()

        req.count = self.NUM_BUFFERS
        req.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
        req.memory = v4l2.V4L2_MEMORY_MMAP

        try:
            ioctl(self.fd, v4l2.VIDIOC_REQBUFS, req)
        except Exception:
            raise Exception("video buffer request failed")

        if req.count < 2:
            raise Exception("Insufficient buffer memory on {}".format(
                self.device_name))

        self.buffers = []
        for x in range(req.count):
            buf = v4l2.v4l2_buffer()
            buf.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
            buf.memory = v4l2.V4L2_MEMORY_MMAP
            buf.index = x

            ioctl(self.fd, v4l2.VIDIOC_QUERYBUF, buf)

            buf.buffer = mmap.mmap(self.fd,
                                   buf.length,
                                   mmap.PROT_READ,
                                   mmap.MAP_SHARED,
                                   offset=buf.m.offset)
            self.buffers.append(buf)
    def init_mmap(self):
        req = v4l2.v4l2_requestbuffers()
        
        req.count = NUM_BUFFERS
        req.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
        req.memory = v4l2.V4L2_MEMORY_MMAP
        
        try:
            ioctl(self.fd, v4l2.VIDIOC_REQBUFS, req)
        except Exception:
            raise Exception("video buffer request failed")
        
        if req.count < 2:
            raise Exception("Insufficient buffer memory on {}".format(self.device_name))

        self.buffers = []
        for x in range(req.count):
            buf = v4l2.v4l2_buffer()
            buf.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
            buf.memory = v4l2.V4L2_MEMORY_MMAP
            buf.index = x
            
            ioctl(self.fd, v4l2.VIDIOC_QUERYBUF, buf)

            buf.buffer =  mmap.mmap(self.fd, buf.length, mmap.PROT_READ, mmap.MAP_SHARED, offset=buf.m.offset)
            self.buffers.append(buf)
Exemple #6
0
    def __init__(self, width, height, dev):
        self.fd = os.open(dev, os.O_RDWR)

        cap = v4l2.v4l2_capability()
        fcntl.ioctl(self.fd, v4l2.VIDIOC_QUERYCAP, cap)

        fmt = v4l2.v4l2_format()
        fmt.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
        fmt.fmt.pix.pixelformat = v4l2.V4L2_PIX_FMT_MJPEG
        fmt.fmt.pix.width = width
        fmt.fmt.pix.height = height
        fcntl.ioctl(self.fd, v4l2.VIDIOC_S_FMT, fmt)

        bufreq = v4l2.v4l2_requestbuffers()
        bufreq.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
        bufreq.memory = v4l2.V4L2_MEMORY_MMAP
        bufreq.count = 1
        fcntl.ioctl(self.fd, v4l2.VIDIOC_REQBUFS, bufreq)

        self.info = v4l2.v4l2_buffer()
        self.info.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
        self.info.memory = v4l2.V4L2_MEMORY_MMAP
        self.info.index = 0
        fcntl.ioctl(self.fd, v4l2.VIDIOC_QUERYBUF, self.info)

        self.buf = mmap.mmap(
                self.fd,
                self.info.length,
                mmap.MAP_SHARED,
                mmap.PROT_READ | mmap.PROT_WRITE,
                self.info.m.offset)

        self.type = ctypes.c_uint32(self.info.type)
Exemple #7
0
	def setUpBuffer(self):
		if not self.available: return
		if not self.fmt: raise SystemError("Image Format not set!")
		
		self.reqbuf = v4l2.v4l2_requestbuffers()
		self.reqbuf.count = 1
		self.reqbuf.type = self.fmt.type
		self.reqbuf.memory = v4l2.V4L2_MEMORY_MMAP
		
		success = fcntl.ioctl(self.device, v4l2.VIDIOC_REQBUFS, self.reqbuf)
		if success == -1:
			raise SystemError("Could not set up camera buffer")
			
		for index in range(self.reqbuf.count):
			buf = v4l2.v4l2_buffer()
			buf.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE 
			buf.memory = v4l2.V4L2_MEMORY_MMAP 
			buf.index = index 
			fcntl.ioctl(self.device, v4l2.VIDIOC_QUERYBUF, buf) 
			image = mmap.mmap(self.device.fileno(), buf.length, offset=buf.m.offset)
			self.imagebuffers.append(image)
			
		for index in range(self.reqbuf.count): 
			buf = v4l2.v4l2_buffer() 
			buf.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE 
			buf.memory = v4l2.V4L2_MEMORY_MMAP
			buf.index = index
			fcntl.ioctl(self.device, v4l2.VIDIOC_QBUF, buf)
Exemple #8
0
    def Start(self, buff_num):
        if self.verbose:
            print '==== START ===='
        req = v4l2.v4l2_requestbuffers()
        req.count = buff_num
        req.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
        req.memory = v4l2.V4L2_MEMORY_MMAP
        xioctl(self.vd, v4l2.VIDIOC_REQBUFS, req)

        if self.verbose:
            print '--requesetbuffers success'

        n_buffers = req.count
        self.buffers = list()
        for i in range(n_buffers):
            self.buffers.append(buffer_struct())

        for i in range(n_buffers):
            buf = v4l2.v4l2_buffer()
            buf.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
            buf.memory = v4l2.V4L2_MEMORY_MMAP
            buf.index = i
            xioctl(self.vd, v4l2.VIDIOC_QUERYBUF, buf)
            self.buffers[i].length = buf.length
            self.buffers[i].start = mmap.mmap(self.vd.fileno(),
                                              buf.length,
                                              flags=mmap.MAP_SHARED,
                                              prot=mmap.PROT_READ
                                              | mmap.PROT_WRITE,
                                              offset=buf.m.offset)
            if self.verbose:
                print '--Memory MAP'
                print '  +MMAP:', i, buf.m.offset, buf.length

        for i in range(n_buffers):
            buf = v4l2.v4l2_buffer()
            buf.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
            buf.memory = v4l2.V4L2_MEMORY_MMAP
            buf.index = i
            xioctl(self.vd, v4l2.VIDIOC_QBUF, buf)

        if self.verbose:
            print '--requesetbuffers success'

        buftype = v4l2.v4l2_buf_type(v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE)
        xioctl(self.vd, v4l2.VIDIOC_STREAMON, buftype)

        time.sleep(1)
        buftype = v4l2.v4l2_buf_type(v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE)
        xioctl(self.vd, v4l2.VIDIOC_STREAMON, buftype)
        if self.verbose:
            print '**** START ****'
Exemple #9
0
    def _setup_buffers(self, buf_count: int) -> None:
        """roughly equivalent to v4l2capture's create_buffers"""
        assert not self.started
        assert len(self.bufmaps) == 0

        reqbuf = v4l2.v4l2_requestbuffers(
            type=v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE,
            count=buf_count,
            memory=v4l2.V4L2_MEMORY_MMAP)
        self._v4l2_ioctl(v4l2.VIDIOC_REQBUFS, reqbuf)
        assert reqbuf.count > 0

        for idx in range(reqbuf.count):
            buf = v4l2.v4l2_buffer(type=v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE,
                                   index=idx,
                                   memory=v4l2.V4L2_MEMORY_MMAP)
            self._v4l2_ioctl(v4l2.VIDIOC_QUERYBUF, buf)
            self.bufmaps += [
                mmap.mmap(self.dev_fd,
                          buf.length,
                          access=mmap.ACCESS_WRITE,
                          offset=buf.m.offset)
            ]
Exemple #10
0
    def __init__(self):
        self.W = 1920
        self.H = 1080
        self.controls = {}
        #open
        l.debug("open")
        self.vd = open('/dev/video0', 'rb+', buffering=0)

        #queryctrl/g_ctrl
        qctrl = v4l2.v4l2_queryctrl()
        ctrl = v4l2.v4l2_control()
        #brightness
        qctrl.id = v4l2.V4L2_CID_BRIGHTNESS
        try:
            fcntl.ioctl(self.vd, v4l2.VIDIOC_QUERYCTRL, qctrl)
            ctrl.id = qctrl.id
            fcntl.ioctl(self.vd, v4l2.VIDIOC_G_CTRL, ctrl)
        except:
            l.error("QUERYCTRL/G_CTRL failed")
        self.controls["brightness"] = (ctrl.id, ctrl.value, qctrl.minimum,
                                       qctrl.maximum)
        #exposure
        qctrl.id = v4l2.V4L2_CID_EXPOSURE_ABSOLUTE
        try:
            fcntl.ioctl(self.vd, v4l2.VIDIOC_QUERYCTRL, qctrl)
            ctrl.id = qctrl.id
            fcntl.ioctl(self.vd, v4l2.VIDIOC_G_CTRL, ctrl)
        except:
            l.error("QUERYCTRL/G_CTRL failed")
        self.controls["exposure"] = (ctrl.id, ctrl.value, qctrl.minimum,
                                     qctrl.maximum)

        #querycap
        l.debug("querycap")
        cp = v4l2.v4l2_capability()
        fcntl.ioctl(self.vd, v4l2.VIDIOC_QUERYCAP, cp)

        #s_fmt
        l.debug("s_fmt")
        fmt = v4l2.v4l2_format()
        fmt.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
        fmt.fmt.pix.width = self.W
        fmt.fmt.pix.height = self.H
        fmt.fmt.pix.pixelformat = v4l2.V4L2_PIX_FMT_SBGGR10
        fmt.fmt.pix.field = v4l2.V4L2_FIELD_NONE
        fcntl.ioctl(self.vd, v4l2.VIDIOC_S_FMT, fmt)

        #g_fmt
        fmt = v4l2.v4l2_format()
        fmt.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
        fcntl.ioctl(self.vd, v4l2.VIDIOC_G_FMT, fmt)
        self.framesize = fmt.fmt.pix.sizeimage

        #reqbufs
        l.debug("reqbufs")
        req = v4l2.v4l2_requestbuffers()
        req.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
        req.memory = v4l2.V4L2_MEMORY_MMAP
        req.count = 4
        fcntl.ioctl(self.vd, v4l2.VIDIOC_REQBUFS, req)

        self.buffers = []
        for ind in range(req.count):
            #querybufs
            buf = v4l2.v4l2_buffer()
            buf.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
            buf.memory = v4l2.V4L2_MEMORY_MMAP
            buf.index = ind
            fcntl.ioctl(self.vd, v4l2.VIDIOC_QUERYBUF, buf)

            #mmap
            mm = mmap.mmap(self.vd.fileno(),
                           buf.length,
                           mmap.MAP_SHARED,
                           mmap.PROT_READ | mmap.PROT_WRITE,
                           offset=buf.m.offset)
            self.buffers.append(mm)

            #qbuf
            fcntl.ioctl(self.vd, v4l2.VIDIOC_QBUF, buf)

        #streamon
        l.debug("streamon")
        buf_type = v4l2.v4l2_buf_type(v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE)
        fcntl.ioctl(self.vd, v4l2.VIDIOC_STREAMON, buf_type)

        #select
        l.debug("select")
        t0 = time.time()
        max_t = 1
        ready_to_read, ready_to_write, in_error = ([], [], [])
        while len(ready_to_read) == 0 and time.time() - t0 < max_t:
            ready_to_read, ready_to_write, in_error = select.select([self.vd],
                                                                    [], [],
                                                                    max_t)
Exemple #11
0
    def __init__(self):
        self.W = 1920
        self.H = 1080
        self.controls = {}
        #open
        l.debug("open")
        self.vd = open('/dev/video0', 'rb+', buffering=0)

        #queryctrl/g_ctrl
        qctrl = v4l2.v4l2_queryctrl()
        ctrl = v4l2.v4l2_control()
        #brightness
        qctrl.id = v4l2.V4L2_CID_BRIGHTNESS
        try:
            fcntl.ioctl(self.vd, v4l2.VIDIOC_QUERYCTRL, qctrl)
            ctrl.id = qctrl.id
            fcntl.ioctl(self.vd, v4l2.VIDIOC_G_CTRL, ctrl)
        except:
            l.error("QUERYCTRL/G_CTRL failed")
        self.controls["brightness"] = (ctrl.id, ctrl.value, qctrl.minimum, qctrl.maximum)
        #exposure
        qctrl.id = v4l2.V4L2_CID_EXPOSURE_ABSOLUTE
        try:
            fcntl.ioctl(self.vd, v4l2.VIDIOC_QUERYCTRL, qctrl)
            ctrl.id = qctrl.id
            fcntl.ioctl(self.vd, v4l2.VIDIOC_G_CTRL, ctrl)
        except:
            l.error("QUERYCTRL/G_CTRL failed")
        self.controls["exposure"] = (ctrl.id, ctrl.value, qctrl.minimum, qctrl.maximum)

        #querycap
        l.debug("querycap")
        cp = v4l2.v4l2_capability()
        fcntl.ioctl(self.vd, v4l2.VIDIOC_QUERYCAP, cp)

        #s_fmt
        l.debug("s_fmt")
        fmt = v4l2.v4l2_format()
        fmt.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
        fmt.fmt.pix.width = self.W
        fmt.fmt.pix.height = self.H
        fmt.fmt.pix.pixelformat = v4l2.V4L2_PIX_FMT_SBGGR10
        fmt.fmt.pix.field = v4l2.V4L2_FIELD_NONE
        fcntl.ioctl(self.vd, v4l2.VIDIOC_S_FMT, fmt)

        #g_fmt
        fmt = v4l2.v4l2_format()
        fmt.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
        fcntl.ioctl(self.vd, v4l2.VIDIOC_G_FMT, fmt)
        self.framesize = fmt.fmt.pix.sizeimage

        #reqbufs
        l.debug("reqbufs")
        req = v4l2.v4l2_requestbuffers()
        req.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
        req.memory = v4l2.V4L2_MEMORY_MMAP
        req.count = 4
        fcntl.ioctl(self.vd, v4l2.VIDIOC_REQBUFS, req)

        self.buffers = []
        for ind in range(req.count):
            #querybufs
            buf = v4l2.v4l2_buffer()
            buf.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
            buf.memory = v4l2.V4L2_MEMORY_MMAP
            buf.index = ind
            fcntl.ioctl(self.vd, v4l2.VIDIOC_QUERYBUF, buf)

            #mmap
            mm = mmap.mmap(self.vd.fileno(), buf.length, mmap.MAP_SHARED, mmap.PROT_READ | mmap.PROT_WRITE, offset=buf.m.offset)
            self.buffers.append(mm)

            #qbuf
            fcntl.ioctl(self.vd, v4l2.VIDIOC_QBUF, buf)

        #streamon
        l.debug("streamon")
        buf_type = v4l2.v4l2_buf_type(v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE)
        fcntl.ioctl(self.vd, v4l2.VIDIOC_STREAMON, buf_type)

        #select
        l.debug("select")
        t0 = time.time()
        max_t = 1
        ready_to_read, ready_to_write, in_error = ([], [], [])
        while len(ready_to_read) == 0 and time.time() - t0 < max_t:
            ready_to_read, ready_to_write, in_error = select.select([self.vd], [], [], max_t)