如何找到导致哪个mmap参数[Errno 22]参数异常无效?

时间:2017-03-29 13:57:37

标签: python linux ubuntu-16.04 mmap v4l2

我试图为V4L相机分配一个小缓冲区,但我收到了

mmap.error: [Errno 22] Invalid argument

您能否建议如何调试和查找无效参数,以便mmap成功?


MCVE代码:

from __future__ import print_function

import fcntl
import mmap
import os
import psutil
import v4l2


def alocate_buf(buf):
    print("Trying to allocate a buffer of size {}".format(buf.length))
    print("vd.fileno():", vd.fileno(), type(vd.fileno()))
    print("buf.length:", buf.length, type(buf.length))
    print("buf.m.offset:", buf.m.offset, type(buf.m.offset))
    mm = mmap.mmap(vd.fileno(), buf.length, mmap.MAP_SHARED, mmap.PROT_READ | mmap.PROT_WRITE, offset=buf.m.offset)


if __name__ == "__main__":
    mem = psutil.virtual_memory()
    print("available memory:", mem.available)

    vd = open("/dev/video3", 'rb+', buffering=0)

    buf = v4l2.v4l2_buffer()
    buf.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
    buf.memory = v4l2.V4L2_MEMORY_MMAP
    buf.index = 0
    buf.length = 1024

    alocate_buf(buf)

代码运行:

$ python find_max_mmap.py
available memory: 6439845888
Trying to allocate a buffer of size 1024
vd.fileno(): 3 <type 'int'>
buf.length: 1024 <type 'long'>
buf.m.offset: 0 <type 'long'>
Traceback (most recent call last):
  File "find_max_mmap.py", line 30, in <module>
    alocate_buf(buf)
  File "find_max_mmap.py", line 15, in alocate_buf
    mm = mmap.mmap(vd.fileno(), buf.length, mmap.MAP_SHARED, mmap.PROT_READ | mmap.PROT_WRITE, offset=buf.m.offset)
mmap.error: [Errno 22] Invalid argument

环境:

  • 操作系统:Ubuntu 16.04,Linux 4.4.0-64-generic,#85-Ubuntu SMP x86_64 GNU / Linux
  • UVC设备:英特尔实感410


编辑1:

请注意,将相关行更改为:

mm = mmap.mmap(vd.fileno(), buf.length)

仍然给出:

$ python mmap_SO.with_0.py
available memory: 6418022400
Trying to allocate a buffer of size 0
vd.fileno(): 3 <type 'int'>
buf.length: 0 <type 'long'>
buf.m.offset: 0 <type 'long'>
Traceback (most recent call last):
  File "mmap_SO.with_0.py", line 30, in <module>
    alocate_buf(buf)
  File "mmap_SO.with_0.py", line 15, in alocate_buf
    mm = mmap.mmap(vd.fileno(), buf.length)
mmap.error: [Errno 22] Invalid argument

2 个答案:

答案 0 :(得分:1)

我自己没有使用过Python v4l2模块,但是在测试MM​​AP与USERPTR分配方法时,我确实首先点击了EINVAL。我的问题原来是我在该文件描述符上调用mmap()之前没有设置图像格式。

鉴于我在代码中看不到v4l2_format()fnctl.ioctl(vd, VIDIOC_S_FORMAT, ...)的任何构造,这似乎也是您的问题。如果您尝试设置格式,您仍然可以获得EINVAL吗?

为确保您指定与/ dev / video3兼容的分辨率和像素格式,您可以通过guvcview或其他网络摄像头工具进行检查。

答案 1 :(得分:0)

使用open("/dev/video3", 'rb+', buffering=0)代替os.open("/dev/video3",os.O_RDWR|os.O_NONBLOCK)