Skip to main content

A Python library wrapped PJSUA2 C++ interface of pjsip/pjproject using pybind11

Project description

PJSUA2 PyBind11

A Python library wrapped PJSUA2 of pjproject with pybind11.

Despite pjproject claiming its high-level API interfaces directly support C++, Java, C#, Python, etc. (through SWIG), we found its SWIG scripts is not such friendly to Python, So we decided to "hand-roll" a pjproject high-level API Python language wrapper. Considering pjproject's high-level API is based on C++ with an object-oriented style and is quite complete, we adopted pybind11 for C++ to Python wrapping. Strictly speaking, this is not a "from-scratch" hand-roll, but rather a "semi-automatic" one. It is sufficient for us to carefully manage memory and avoid dangers.

Preparation

📚 Reference:
https://docs.pjsip.org/en/latest/get-started/posix/build_instructions.html

  • Windows

    This section only records the steps on x86_64 / Windows 10 64bit.

    1. Download and install git for windows, version 2.0 or above.

    2. Download and install VisualStudio 2017 or above (actually developed using VisualStudio 2022), and select "Desktop development with C++" during installation.

    3. Install Python for Windows

      • If you do not need to debug C++ code and only need to generate dynamic library files, you can directly install Python through the Microsoft Store.

        🔖 Tip:
        After this installation, you can set the global commands python, pip, etc., in Windows "Settings" ➡️ "Apps" ➡️ "Apps & features" ➡️ "App execution aliases".

      • If you need to debug C++ code, there are several installation methods:

        Important:
        In the installation interface, select "Custom" installation and check the following options:

        • ☑️ pip
        • ☑️ py launcher
        • ☑️ Download debugging symbols
        • ☑️ Download debug binaries If debugging files are not installed, it will be the same as installing through the Microsoft Store. So when using winget installation, note the --interactive command-line parameter to enter the installer's graphical interface and check the above options.

        🔖 Tip:

        • If "py launcher" is selected during installation, the Python startup command is py, not python.
        • If "Add Python to environment variables" is selected (default unchecked), the Python startup command is python. But this choice may cause conflicts between different versions, so it is not recommended.
        • If neither is selected, there is no global Python startup command, and you need to manually set the PATH environment variable to solve this problem.

      ℹ️ Note:
      The package settings of this project require Python version requires-python = ">=3.8", but the actual development process has always used Python 12. It is recommended to use Python 3.10 or above.

    4. (Optional) Download and install CMake (can be installed via winget)

      CMake is mainly used in conjunction with IDEs to provide code hints and graphical project management. If not used in conjunction with IDEs, it can be ignored.

  • Ubuntu 24.04 LTS

    Install using apt:

    sudo apt install libasound2-dev build-essential cmake pkg-config python3 python3-dev python3-setuptools python3-pip python3-venv python3-pybind11
    

    Among them, libasound2-dev is used to provide ALSA driver support for accessing audio devices.

PJPROJECT

This section only records the steps on x86_64 / Windows 10 64bit.

Download the latest stable version of pjproject (this project currently uses 2.14.1, strongly recommended to keep consistent). Record its directory location and set it to the environment variable PJ_DIR.

Refer to https://docs.pjsip.org/en/latest/get-started/windows/build_instructions.html:

  1. In the directory pjlib/include/pj, create a new header file config_site.h, this file can be empty (default value), refer to https://docs.pjsip.org/en/latest/get-started/guidelines-development.html#config-site-h;
  2. Due to the development environment's Windows SDK and platform toolset version being inconsistent with the project code specified version, you should use VisualStudio to open the solution and follow the prompts to upgrade to the latest VisualStudio project format, and re-target the project to match the current development environment's Windows SDK and platform toolset version, ignore unsupported projects, and then build. Of course, you can also manually modify the project file's XML content if you are familiar with this cold knowledge.
  3. (Optional) In the project list, set pjsua as the startup project;
  4. (Optional) Select the build type and target platform combination, this project is confirmed to support:
    • Release-STATIC | x64
    • Debug-STATIC | x64
    • Win32 and Dynamic (DLL) have not been attempted.
    • ARM64 has not been attempted Generate the pjsua project for the above build type and target platform combination. The generated library files are in the lib directory, named like libpjproject-x86_64-x64-vc14-Release-Static;

If using MSBuild to build (you still need to use VisualStudio to open to upgrade the solution and project files), execute the project build command in the "Developer PowerShell for VS" or "Developer Command Prompt for VS" command line environment, taking Debug-STATIC | x64 as an example:

MSBuild pjproject-vs14.sln -target:pjsua -m -p:Configuration=Debug-Static -p:Platform=x64

Build

This section only documents the steps on x86_64 / Windows 10 64bit.

During development, you can use either CMake or setuptools for building. The former is mainly used for development and debugging, while the latter is primarily used for packaging and distribution.

🔖 Tip:
Many steps require using pip for installation — this requires internet access! If the official pip repository is too slow, you can use free mirrors from Alibaba Cloud or Tencent Cloud. Example setup:

pip config set --user global.index-url https://mirrors.aliyun.com/pypi/simple
  • Building with setuptools

    This method is suitable for scenarios where: C++ code is not modified, only Python Native modules are generated; obtaining artifacts for packaging and distribution; minor modifications to C++ code for simple debugging;

    Caution:

    • The path to the pjproject directory should be set in the environment variable PJ_DIR;
    • If the PJ_REBUILD switch is enabled, commands must be executed in Developer PowerShell for VS or Developer Command Prompt for VS;
    • This build method directly calls the setuptools script (setup.py), which requires pre-installing the project's development dependencies (defined in "pjsua2pybind11/requirements.txt") using pip in the current Python development environment. Installation command is provided in the next step;
    1. Install the necessary dependencies for building and packaging this sub-project using pip in your current Python development environment (strongly recommended to use a separate venv as the Python development environment):

      pip install -r pjsua2pybind11/requirements.txt
      
    2. Build

      The setuptools script of this sub-project provides several build methods (must be executed in the pjsua2pybind11 directory):

      • In-place build (recommended method)

        Suitable for quick builds after minor modifications to C++ code and rapid generation of native modules required for development.

        cd pjsua2pybind11
        python setup.py build_ext --inplace
        

        After execution, shared/dynamic library files will be output to the pjsua2pybind11/python directory. On Windows, filenames resemble: pjsua2.cp312-win_amd64.pyd.

      • Complete build of native parts:

        cd pjsua2pybind11
        python setup.py build_ext
        

        After execution, only shared/dynamic libraries (this project actually only has shared/dynamic libraries) are generated. Output directories look like pjsua2pybind11/build/lib.win-amd64-cpython-312

      • Complete build:

        cd pjsua2pybind11
        python setup.py build
        

        Since this project only has shared/dynamic libraries, executing python setup.py build is essentially the same as executing python setup.py build_ext.

        Output directories look like pjsua2pybind11/build/lib.win-amd64-cpython-312

      🔖 Tip:

      • If pjproject has not been built before building this sub-project, set the environment variable PJ_REBUILD

        This way, when using the setuptools script, it will first build pjproject. Note that you need to open and upgrade the pjproject solution and project files using VisualStudio beforehand.

      • In this case, python setup.py ... must be executed in Developer PowerShell / CommandPrompt for VS.

        For example:

        **********************************************************************
        ** Visual Studio 2022 Developer PowerShell v17.10.1
        ** Copyright (c) 2022 Microsoft Corporation
        **********************************************************************
        PS C:\Program Files\Microsoft Visual Studio\2022\Community> cd C:\Repos\softphone\pjsua2pybind11
        PS C:\Repos\softphone\pjsua2pybind11> $Env:PJ_DIR="C:\Repos\pjproject"
        PS C:\Repos\softphone\pjsua2pybind11> $Env:PJ_REBUILD="1"
        PS C:\Repos\softphone\pjsua2pybind11> cd pjsua2pybind11
        PS C:\Repos\softphone\pjsua2pybind11> python setup.py build_ext --inplace
        
  • Building with CMake

    This method is suitable for developing and debugging with an C/C++ IDE that supports CMake

    1. Install pybind11 and other development dependencies using pip in your current Python development environment (strongly recommended to use a separate venv as the Python development environment):

      pip install -r pjsua2pybind11/requirements.txt
      
    2. Modify the configuration file CMakePresets.json.in to CMakePresets.json according to your local development environment, then call CMake to execute the workflow

      For example, modify CMakePresets.json as follows:

      /// ...
      "configurePresets": [{
           "name": "win-vs2022",
           "inherits": "default",
           "generator": "Visual Studio 17 2022",
           "condition": {
             "type": "equals",
             "lhs": "${hostSystemName}",
             "rhs": "Windows"
           },
           "cacheVariables": {
             "PJ_DIR": "D:\\opensource\\pjproject",
             "pybind11_DIR": "${sourceDir}/../.venv/lib/site-packages/pybind11/share/cmake/pybind11"
           }
      }],
      /// ...
      

      Modify the "cacheVariables" according to your actual development environment, such as:

      • "PJ_DIR": Set to the directory of the pjproject source code project

      • "pybind11_DIR": Set to the CMake script directory of pybind11

        After installing dependencies using pip, you can check the CMake script directory of pybind11 with the following command:

        pybind11-config --cmakedir
        

      After setting up, execute the CMake workflow, for example:

      cmake --workflow --preset win
      

      Or execute step by step, for example:

      # configure:
      cmake --preset win-vs2022
      
      # build:
      cmake --build --preset win-vs2022-release
      

    The generated Python native module target files will be output to the directory pjsua2pybind11/python, with filenames resembling: pjsua2.cp312-win_amd64.pyd

    FAQ

    • Q: How do I specify if I have multiple Python environments on my development machine?

    • A: This situation is more complex and requires manually filling in CMake variables, which can be specified in the preset configuration file or command line. The variable Python3_ROOT_DIR defines the root directory of the Python 3 installation to be used, for example:

      cd build
      cmake -DPJ_REBUILD=yes -DCMAKE_BUILD_TYPE=Debug -Dpybind11_DIR="$(pybind11-config --cmakedir)" -DPython3_ROOT_DIR="C:\Program Files\Python 3.10" ..
      

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

pjsua2_pybind11-0.1a3-cp312-cp312-manylinux_2_28_x86_64.whl (4.7 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.28+ x86-64

pjsua2_pybind11-0.1a3-cp311-cp311-manylinux_2_28_x86_64.whl (4.7 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.28+ x86-64

pjsua2_pybind11-0.1a3-cp310-cp310-manylinux_2_28_x86_64.whl (4.7 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.28+ x86-64

pjsua2_pybind11-0.1a3-cp39-cp39-manylinux_2_28_x86_64.whl (4.7 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.28+ x86-64

pjsua2_pybind11-0.1a3-cp38-cp38-manylinux_2_28_x86_64.whl (4.7 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.28+ x86-64

File details

Details for the file pjsua2_pybind11-0.1a3-cp312-cp312-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pjsua2_pybind11-0.1a3-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 cf39c0878395d470f942769129d80f90cf0b12bb3f1a811120984c1a42fecc0a
MD5 1838779839fd7cd351182aa150c0926f
BLAKE2b-256 0169342b795ba1e0a7b51c3cdfb1dce4361f6271330706d3343f028c7accc61f

See more details on using hashes here.

File details

Details for the file pjsua2_pybind11-0.1a3-cp311-cp311-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pjsua2_pybind11-0.1a3-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 fea98773ac36f7aa1f59c5cb25a474acf1336a2d41546373aeae93666283c28f
MD5 4aa924b2cc6945c8741e122de29551e0
BLAKE2b-256 99a43e094f70bba0942636ffc8f586918f0beb560ee01c3dae470d287a0699c6

See more details on using hashes here.

File details

Details for the file pjsua2_pybind11-0.1a3-cp310-cp310-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pjsua2_pybind11-0.1a3-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 02911ca242f6e7bdba5747336c7cd2024cd91e1858bc2cc853be5253432ac5e8
MD5 6e3f0cb24619fc6295259236a695e46a
BLAKE2b-256 fffb0c26c898e099993b9086adf21da07d3245f4e83587562e7f5e1c24fe2980

See more details on using hashes here.

File details

Details for the file pjsua2_pybind11-0.1a3-cp39-cp39-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pjsua2_pybind11-0.1a3-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0e4d6f5574c770e5e20351235e9198e6955d09f8b1c3637897e98452bf986960
MD5 bcaac4068d24b9ac2d6e20509a9a74ae
BLAKE2b-256 4fb8a79212396c2b605a0f1d990ead7c4c9d6d6029f776c91b528c5996b2e0e0

See more details on using hashes here.

File details

Details for the file pjsua2_pybind11-0.1a3-cp38-cp38-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pjsua2_pybind11-0.1a3-cp38-cp38-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 be57762dbc3fb3cf4c31bb98658f403ad41a18400aa1191c2fc101f623e5266f
MD5 8d49ccb5cf84b8751994fc5b4ab24104
BLAKE2b-256 f90024acc0d151ec4cb7fa25ef2e3c21ae883b2be269b8432a5d2e9b31401458

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page