[libcamera-devel] [PATCH v9 4/7] Add Python bindings

Laurent Pinchart laurent.pinchart at ideasonboard.com
Sat May 7 15:00:36 CEST 2022


Hi Tomi,

Thank you for the patch.

On Sat, May 07, 2022 at 01:11:49PM +0300, Tomi Valkeinen wrote:
> Add libcamera Python bindings. pybind11 is used to generate the C++ <->
> Python layer.
> 
> We use pybind11 'smart_holder' version to avoid issues with private
> destructors and shared_ptr. There is also an alternative solution here:
> 
> https://github.com/pybind/pybind11/pull/2067
> 
> Only a subset of libcamera classes are exposed. Implementing and testing
> the wrapper classes is challenging, and as such only classes that I have
> needed have been added so far.
> 
> Signed-off-by: Tomi Valkeinen <tomi.valkeinen at ideasonboard.com>
> Reviewed-by: Laurent Pinchart <laurent.pinchart at ideasonboard.com>
> ---
>  meson.build                                   |   1 +
>  meson_options.txt                             |   5 +
>  src/meson.build                               |   1 +
>  src/py/libcamera/__init__.py                  |  84 +++
>  src/py/libcamera/meson.build                  |  51 ++
>  src/py/libcamera/pyenums.cpp                  |  34 +
>  src/py/libcamera/pymain.cpp                   | 642 ++++++++++++++++++
>  src/py/meson.build                            |   1 +
>  subprojects/.gitignore                        |   3 +-
>  subprojects/packagefiles/pybind11/meson.build |   7 +
>  subprojects/pybind11.wrap                     |   9 +
>  11 files changed, 837 insertions(+), 1 deletion(-)
>  create mode 100644 src/py/libcamera/__init__.py
>  create mode 100644 src/py/libcamera/meson.build
>  create mode 100644 src/py/libcamera/pyenums.cpp
>  create mode 100644 src/py/libcamera/pymain.cpp
>  create mode 100644 src/py/meson.build
>  create mode 100644 subprojects/packagefiles/pybind11/meson.build
>  create mode 100644 subprojects/pybind11.wrap

[snip]

> diff --git a/src/py/libcamera/meson.build b/src/py/libcamera/meson.build
> new file mode 100644
> index 00000000..fce9fd5e
> --- /dev/null
> +++ b/src/py/libcamera/meson.build
> @@ -0,0 +1,51 @@
> +# SPDX-License-Identifier: CC0-1.0
> +
> +py3_dep = dependency('python3', required : get_option('pycamera'))
> +
> +if not py3_dep.found()
> +    pycamera_enabled = false
> +    subdir_done()
> +endif
> +
> +pycamera_enabled = true
> +
> +pybind11_proj = subproject('pybind11')
> +pybind11_dep = pybind11_proj.get_variable('pybind11_dep')
> +
> +pycamera_sources = files([
> +    'pyenums.cpp',
> +    'pymain.cpp',
> +])
> +
> +pycamera_deps = [
> +    libcamera_public,
> +    py3_dep,
> +    pybind11_dep,
> +]
> +
> +pycamera_args = [
> +    '-fvisibility=hidden',
> +    '-Wno-shadow',
> +    '-DPYBIND11_USE_SMART_HOLDER_AS_DEFAULT',
> +]
> +
> +destdir = get_option('libdir') / ('python' + py3_dep.version()) / 'site-packages' / 'libcamera'
> +
> +pycamera = shared_module('_libcamera',
> +                         pycamera_sources,
> +                         install : true,
> +                         install_dir : destdir,
> +                         name_prefix : '',
> +                         dependencies : pycamera_deps,
> +                         cpp_args : pycamera_args)
> +
> +run_command('ln', '-fsT', '../../../../src/py/libcamera/__init__.py',
> +            meson.current_build_dir() / '__init__.py',
> +            check: true)
> +
> +install_data(['__init__.py'], install_dir : destdir)
> +
> +# \todo Generate stubs when building. Depends on pybind11-stubgen. Sometimes
> +# this works, sometimes doesn't... To generate pylibcamera stubs.
> +# $ PYTHONPATH=build/src/py pybind11-stubgen --no-setup-py -o build/src/py libcamera
> +# $ mv build/src/py/libcamera-stubs/* build/src/py/libcamera/

Could you add the link you shared in the review of the previous version
to explain what stubs are ?

[snip]

> diff --git a/src/py/libcamera/pymain.cpp b/src/py/libcamera/pymain.cpp
> new file mode 100644
> index 00000000..ee306186
> --- /dev/null
> +++ b/src/py/libcamera/pymain.cpp
> @@ -0,0 +1,642 @@

[snip]

> +static std::weak_ptr<CameraManager> gCameraManager;
> +static int gEventfd;
> +static std::mutex gReqlistMutex;
> +static std::vector<Request *> gReqList;
> +
> +static void handleRequestCompleted(Request *req)
> +{
> +	{
> +		std::lock_guard guard(gReqlistMutex);
> +		gReqList.push_back(req);
> +	}
> +
> +	uint64_t v = 1;
> +	size_t s = write(gEventfd, &v, 8);
> +	if (s != 8)
> +		throw std::runtime_error("Unable to write to eventfd");

libcamera doesn't handle exceptions thrown to it (exceptions thrown
towards Python are fine with me, assuming pybind11 handles them fine). A
LOG(FATAL) would be better here.

> +}

[snip]

-- 
Regards,

Laurent Pinchart


More information about the libcamera-devel mailing list