diff --git a/.github/workflows/linux_cuda_aarch64_wheel.yaml b/.github/workflows/linux_cuda_aarch64_wheel.yaml deleted file mode 100644 index bfb55fdca..000000000 --- a/.github/workflows/linux_cuda_aarch64_wheel.yaml +++ /dev/null @@ -1,135 +0,0 @@ -name: Build and test Linux CUDA aarch64 wheels - -on: - pull_request: - push: - branches: - - nightly - - main - - release/* - tags: - - v[0-9]+.[0-9]+.[0-9]+-rc[0-9]+ - workflow_dispatch: - -concurrency: - group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref_name }}-${{ github.ref_type == 'branch' && github.sha }}-${{ github.event_name == 'workflow_dispatch' }} - cancel-in-progress: true - -permissions: - id-token: write - contents: write - -defaults: - run: - shell: bash -l -eo pipefail {0} - -jobs: - generate-matrix: - uses: pytorch/test-infra/.github/workflows/generate_binary_build_matrix.yml@main - with: - package-type: wheel - os: linux-aarch64 - test-infra-repository: pytorch/test-infra - test-infra-ref: main - with-cpu: disable - with-xpu: disable - with-rocm: disable - with-cuda: enable - build-python-only: "disable" - build: - needs: generate-matrix - strategy: - fail-fast: false - name: Build and Upload wheel - uses: pytorch/test-infra/.github/workflows/build_wheels_linux.yml@main - with: - repository: meta-pytorch/torchcodec - ref: "" - test-infra-repository: pytorch/test-infra - test-infra-ref: main - build-matrix: ${{ needs.generate-matrix.outputs.matrix }} - pre-script: packaging/pre_build_script.sh - post-script: packaging/post_build_script.sh - smoke-test-script: packaging/fake_smoke_test.py - package-name: torchcodec - trigger-event: ${{ github.event_name }} - architecture: aarch64 - build-platform: "python-build-package" - build-command: "BUILD_AGAINST_ALL_FFMPEG_FROM_S3=1 ENABLE_CUDA=1 python -m build --wheel -vvv --no-isolation" - - install-and-test: - runs-on: linux.arm64.2xlarge - container: - image: pytorch/manylinuxaarch64-builder:cuda12.6 - env: - cuda_version_without_periods: "126" - strategy: - fail-fast: false - matrix: - python-version: ['3.10'] - ffmpeg-version-for-tests: ['4.4.2', '5.1.2', '6.1.1', '7.0.1', '8.0'] - needs: build - steps: - - uses: actions/download-artifact@v4 - with: - name: meta-pytorch_torchcodec__${{ matrix.python-version }}_cu${{ env.cuda_version_without_periods }}_aarch64 - path: pytorch/torchcodec/dist/ - - name: Setup conda env - uses: conda-incubator/setup-miniconda@v3 - with: - auto-update-conda: true - # Using miniforge instead of miniconda ensures that the default - # conda channel is conda-forge instead of main/default. This ensures - # ABI consistency between dependencies: - # https://conda-forge.org/docs/user/transitioning_from_defaults/ - miniforge-version: latest - activate-environment: test - python-version: ${{ matrix.python-version }} - - name: Update pip - run: python -m pip install --upgrade pip - - name: Install PyTorch - run: | - ${CONDA_RUN} python -m pip install --pre torch torchvision --index-url https://download.pytorch.org/whl/nightly/cu${{ env.cuda_version_without_periods }} - - name: Install torchcodec from the wheel - run: | - wheel_path=`find pytorch/torchcodec/dist -type f -name "*.whl"` - echo Installing $wheel_path - python -m pip install $wheel_path -vvv - - - name: Check out repo - uses: actions/checkout@v3 - - name: Install ffmpeg, post build - run: | - # Ideally we would have checked for that before installing the wheel, - # but we need to checkout the repo to access this file, and we don't - # want to checkout the repo before installing the wheel to avoid any - # side-effect. It's OK. - source packaging/helpers.sh - assert_ffmpeg_not_installed - - conda install "ffmpeg=${{ matrix.ffmpeg-version-for-tests }}" -c conda-forge - ffmpeg -version - echo LD_LIBRARY_PATH=$CONDA_PREFIX/lib:/usr/local/cuda/lib64/:${LD_LIBRARY_PATH} >> $GITHUB_ENV - - - name: Install test dependencies - run: | - # Ideally we would find a way to get those dependencies from pyproject.toml - python -m pip install numpy pytest pillow - - - name: Delete the src/ folder just for fun - run: | - # The only reason we checked-out the repo is to get access to the - # tests. We don't care about the rest. Out of precaution, we delete - # the src/ folder to be extra sure that we're running the code from - # the installed wheel rather than from the source. - # This is just to be extra cautious and very overkill because a) - # there's no way the `torchcodec` package from src/ can be found from - # the PythonPath: the main point of `src/` is precisely to protect - # against that and b) if we ever were to execute code from - # `src/torchcodec`, it would fail loudly because the built .so files - # aren't present there. - rm -r src/ - ls - - name: Run Python tests - run: | - pytest --override-ini="addopts=-v" test diff --git a/.github/workflows/linux_cuda_wheel.yaml b/.github/workflows/linux_cuda_wheel.yaml deleted file mode 100644 index 4e711b8c3..000000000 --- a/.github/workflows/linux_cuda_wheel.yaml +++ /dev/null @@ -1,147 +0,0 @@ -name: Build and test Linux CUDA wheels - -on: - pull_request: - push: - branches: - - nightly - - main - - release/* - tags: - - v[0-9]+.[0-9]+.[0-9]+-rc[0-9]+ - workflow_dispatch: - -concurrency: - group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref_name }}-${{ github.ref_type == 'branch' && github.sha }}-${{ github.event_name == 'workflow_dispatch' }} - cancel-in-progress: true - -permissions: - id-token: write - contents: write - -defaults: - run: - shell: bash -l -eo pipefail {0} - -jobs: - generate-matrix: - uses: pytorch/test-infra/.github/workflows/generate_binary_build_matrix.yml@main - with: - package-type: wheel - os: linux - test-infra-repository: pytorch/test-infra - test-infra-ref: main - with-cpu: disable - with-xpu: disable - with-rocm: disable - with-cuda: enable - build-python-only: "disable" - build: - needs: generate-matrix - strategy: - fail-fast: false - name: Build and Upload wheel - uses: pytorch/test-infra/.github/workflows/build_wheels_linux.yml@main - with: - repository: meta-pytorch/torchcodec - ref: "" - test-infra-repository: pytorch/test-infra - test-infra-ref: main - build-matrix: ${{ needs.generate-matrix.outputs.matrix }} - pre-script: packaging/pre_build_script.sh - post-script: packaging/post_build_script.sh - smoke-test-script: packaging/fake_smoke_test.py - package-name: torchcodec - trigger-event: ${{ github.event_name }} - build-platform: "python-build-package" - build-command: "BUILD_AGAINST_ALL_FFMPEG_FROM_S3=1 ENABLE_CUDA=1 python -m build --wheel -vvv --no-isolation" - - install-and-test: - runs-on: linux.g5.4xlarge.nvidia.gpu - strategy: - fail-fast: false - matrix: - # 3.10 corresponds to the minimum python version for which we build - # the wheel unless the label cliflow/binaries/all is present in the - # PR. - # For the actual release we should add that label and change this to - # include more python versions. - python-version: ['3.10'] - # We test against 12.6 and 13.0 to avoid having too big of a CI matrix, - # but for releases we should add 12.8. - cuda-version: ['12.6', '13.0'] - # TODO: put back ffmpeg 5 https://github.com/pytorch/torchcodec/issues/325 - ffmpeg-version-for-tests: ['4.4.2', '6', '7', '8.0'] - - container: - image: "pytorch/manylinux2_28-builder:cuda${{ matrix.cuda-version }}" - options: "--gpus all -e NVIDIA_DRIVER_CAPABILITIES=video,compute,utility" - needs: build - steps: - - name: Setup env vars - run: | - cuda_version_without_periods=$(echo "${{ matrix.cuda-version }}" | sed 's/\.//g') - echo cuda_version_without_periods=${cuda_version_without_periods} >> $GITHUB_ENV - python_version_without_periods=$(echo "${{ matrix.python-version }}" | sed 's/\.//g') - echo python_version_without_periods=${python_version_without_periods} >> $GITHUB_ENV - - uses: actions/download-artifact@v4 - with: - name: meta-pytorch_torchcodec__${{ matrix.python-version }}_cu${{ env.cuda_version_without_periods }}_x86_64 - path: pytorch/torchcodec/dist/ - - name: Setup miniconda using test-infra - uses: pytorch/test-infra/.github/actions/setup-miniconda@main - with: - python-version: ${{ matrix.python-version }} - # We install conda packages at the start because otherwise conda may have conflicts with dependencies. - # Note: xorg-libxau was addded to fix a problem with ffmpeg 4. We should consider removing it. - default-packages: "nvidia/label/cuda-${{ matrix.cuda-version }}.0::libnpp nvidia::cuda-nvrtc=${{ matrix.cuda-version }} nvidia::cuda-toolkit=${{ matrix.cuda-version }} nvidia::cuda-cudart=${{ matrix.cuda-version }} nvidia::cuda-driver-dev=${{ matrix.cuda-version }} conda-forge::ffmpeg=${{ matrix.ffmpeg-version-for-tests }} conda-forge::xorg-libxau" - - name: Check env, set LD_LIBRARY_PATH - run: | - ${CONDA_RUN} env - ${CONDA_RUN} conda info - ${CONDA_RUN} nvidia-smi - ${CONDA_RUN} conda list - echo LD_LIBRARY_PATH=$CONDA_PREFIX/lib:/usr/local/cuda/lib64/:${LD_LIBRARY_PATH} >> $GITHUB_ENV - - name: Assert ffmpeg exists - run: | - ${CONDA_RUN} ffmpeg -buildconf - - name: Update pip - run: ${CONDA_RUN} python -m pip install --upgrade pip - - name: Install PyTorch - run: | - ${CONDA_RUN} python -m pip install --pre torch torchvision --index-url https://download.pytorch.org/whl/nightly/cu${{ env.cuda_version_without_periods }} - ${CONDA_RUN} python -c 'import torch; print(f"{torch.__version__}"); print(f"{torch.__file__}"); print(f"{torch.cuda.is_available()=}")' - - name: Install torchcodec from the wheel - run: | - wheel_path=`find pytorch/torchcodec/dist -type f -name "*cu${{ env.cuda_version_without_periods }}-cp${{ env.python_version_without_periods }}*.whl"` - echo Installing $wheel_path - ${CONDA_RUN} python -m pip install $wheel_path -vvv - - - name: Check out repo - uses: actions/checkout@v3 - - - name: Install test dependencies - run: | - # Ideally we would find a way to get those dependencies from pyproject.toml - ${CONDA_RUN} python -m pip install numpy pytest pillow - - - name: Delete the src/ folder just for fun - run: | - # The only reason we checked-out the repo is to get access to the - # tests. We don't care about the rest. Out of precaution, we delete - # the src/ folder to be extra sure that we're running the code from - # the installed wheel rather than from the source. - # This is just to be extra cautious and very overkill because a) - # there's no way the `torchcodec` package from src/ can be found from - # the PythonPath: the main point of `src/` is precisely to protect - # against that and b) if we ever were to execute code from - # `src/torchcodec`, it would fail loudly because the built .so files - # aren't present there. - rm -r src/ - ls - - name: Run Python tests - run: | - ${CONDA_RUN} FAIL_WITHOUT_CUDA=1 pytest --override-ini="addopts=-v" test --tb=short - - name: Run Python benchmark - run: | - ${CONDA_RUN} time python benchmarks/decoders/gpu_benchmark.py --devices=cuda:0,cpu --resize_devices=none diff --git a/.github/workflows/linux_wheel.yaml b/.github/workflows/linux_wheel.yaml index cccbedc25..17073330a 100644 --- a/.github/workflows/linux_wheel.yaml +++ b/.github/workflows/linux_wheel.yaml @@ -63,7 +63,7 @@ jobs: fail-fast: false matrix: python-version: ['3.10'] - ffmpeg-version-for-tests: ['4.4.2', '5.1.2', '6.1.1', '7.0.1', '8.0'] + ffmpeg-version-for-tests: ['8.0'] needs: build steps: - uses: actions/download-artifact@v4 @@ -103,8 +103,9 @@ jobs: source packaging/helpers.sh assert_ffmpeg_not_installed - conda install "ffmpeg=${{ matrix.ffmpeg-version-for-tests }}" -c conda-forge + conda install "ffmpeg=${{ matrix.ffmpeg-version-for-tests }}" pkg-config -c conda-forge ffmpeg -version + ldd $(which ffmpeg) - name: Install test dependencies run: | @@ -127,4 +128,5 @@ jobs: ls - name: Run Python tests run: | + echo "LD_LIBRARY_PATH: $LD_LIBRARY_PATH" pytest --override-ini="addopts=-v" test diff --git a/.github/workflows/macos_wheel.yaml b/.github/workflows/macos_wheel.yaml deleted file mode 100644 index ead45784d..000000000 --- a/.github/workflows/macos_wheel.yaml +++ /dev/null @@ -1,126 +0,0 @@ -name: Build and test MacOS wheel - -on: - pull_request: - push: - branches: - - nightly - - main - - release/* - tags: - - v[0-9]+.[0-9]+.[0-9]+-rc[0-9]+ - workflow_dispatch: - -concurrency: - group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref_name }}-${{ github.ref_type == 'branch' && github.sha }}-${{ github.event_name == 'workflow_dispatch' }} - cancel-in-progress: true - -permissions: - id-token: write - contents: write - -defaults: - run: - shell: bash -l -eo pipefail {0} - -jobs: - - generate-matrix: - uses: pytorch/test-infra/.github/workflows/generate_binary_build_matrix.yml@main - with: - package-type: wheel - os: macos-arm64 - test-infra-repository: pytorch/test-infra - test-infra-ref: main - with-xpu: disable - with-rocm: disable - with-cuda: disable - build-python-only: "disable" - - build: - needs: generate-matrix - strategy: - fail-fast: false - name: Build and Upload Mac wheel - uses: pytorch/test-infra/.github/workflows/build_wheels_macos.yml@main - with: - repository: meta-pytorch/torchcodec - ref: "" - test-infra-repository: pytorch/test-infra - test-infra-ref: main - build-matrix: ${{ needs.generate-matrix.outputs.matrix }} - pre-script: packaging/pre_build_script.sh - post-script: packaging/post_build_script.sh - smoke-test-script: packaging/fake_smoke_test.py - runner-type: macos-14 - setup-miniconda: true - package-name: torchcodec - trigger-event: ${{ github.event_name }} - build-platform: "python-build-package" - build-command: "BUILD_AGAINST_ALL_FFMPEG_FROM_S3=1 python -m build --wheel -vvv --no-isolation" - - install-and-test: - runs-on: macos-14-xlarge - strategy: - fail-fast: false - matrix: - python-version: ['3.10'] - ffmpeg-version-for-tests: ['4.4.2', '5.1.2', '6.1.1', '7.0.1', '8.0'] - needs: build - steps: - - name: Download wheel - uses: actions/download-artifact@v4 - with: - name: meta-pytorch_torchcodec__${{ matrix.python-version }}_cpu_ - path: pytorch/torchcodec/dist/ - - - name: Setup conda env - uses: conda-incubator/setup-miniconda@v3 - with: - auto-update-conda: true - miniconda-version: "latest" - activate-environment: test - python-version: ${{ matrix.python-version }} - - name: Update pip - run: python -m pip install --upgrade pip - - - name: Install PyTorch - run: | - python -m pip install --pre torch torchvision --index-url https://download.pytorch.org/whl/nightly/cpu - - - name: Install torchcodec from the wheel - run: | - wheel_path=`find pytorch/torchcodec/dist -type f -name "*.whl"` - echo Installing $wheel_path - python -m pip install $wheel_path -vvv - - - name: Check out torchcodec repo - uses: actions/checkout@v3 - - - name: Install ffmpeg - run: | - conda install "ffmpeg=${{ matrix.ffmpeg-version-for-tests }}" -c conda-forge - ffmpeg -version - - - name: Install test dependencies - run: | - python -m pip install numpy pytest pillow - - - name: Delete the src/ folder just for fun - run: | - # The only reason we checked-out the repo is to get access to the - # tests. We don't care about the rest. Out of precaution, we delete - # the src/ folder to be extra sure that we're running the code from - # the installed wheel rather than from the source. - # This is just to be extra cautious and very overkill because a) - # there's no way the `torchcodec` package from src/ can be found from - # the PythonPath: the main point of `src/` is precisely to protect - # against that and b) if we ever were to execute code from - # `src/torchcodec`, it would fail loudly because the built .so files - # aren't present there. - rm -r src/ - ls -lh - - - name: Run Python tests - run: | - pytest --override-ini="addopts=-v" test diff --git a/.github/workflows/windows_wheel.yaml b/.github/workflows/windows_wheel.yaml deleted file mode 100644 index 8a9b5b740..000000000 --- a/.github/workflows/windows_wheel.yaml +++ /dev/null @@ -1,138 +0,0 @@ -name: Build and test Windows wheel - -on: - pull_request: - push: - branches: - - nightly - - main - - release/* - tags: - - v[0-9]+.[0-9]+.[0-9]+-rc[0-9]+ - workflow_dispatch: - -concurrency: - group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref_name }}-${{ github.ref_type == 'branch' && github.sha }}-${{ github.event_name == 'workflow_dispatch' }} - cancel-in-progress: true - -permissions: - id-token: write - contents: write - -defaults: - run: - shell: bash -l -eo pipefail {0} - -jobs: - - generate-matrix: - uses: pytorch/test-infra/.github/workflows/generate_binary_build_matrix.yml@main - with: - package-type: wheel - os: windows - test-infra-repository: pytorch/test-infra - test-infra-ref: main - with-xpu: disable - with-rocm: disable - with-cuda: disable - build-python-only: "disable" - python-versions: '["3.10", "3.11", "3.12", "3.13"]' - - - build: - needs: generate-matrix - strategy: - fail-fast: false - name: Build and Upload Windows wheel - uses: pytorch/test-infra/.github/workflows/build_wheels_windows.yml@main - with: - repository: meta-pytorch/torchcodec - ref: "" - test-infra-repository: pytorch/test-infra - test-infra-ref: main - build-matrix: ${{ needs.generate-matrix.outputs.matrix }} - pre-script: packaging/pre_build_script.sh - # post-script: packaging/post_build_script.sh TODO: consider enabling post-build checks for Windows - env-script: packaging/vc_env_helper.bat - smoke-test-script: packaging/fake_smoke_test.py - package-name: torchcodec - trigger-event: ${{ github.event_name }} - build-platform: "python-build-package" - # The BUILD_AGAINST_ALL_FFMPEG_FROM_S3 var, needed to build the wheel, is - # set in vc_env_helper.bat Couldn't find a way to set it from here. - build-command: "python -m build --wheel -vvv --no-isolation" - - install-and-test: - runs-on: windows-latest - strategy: - fail-fast: false - matrix: - python-version: ['3.10'] - # TODO: FFmpeg 5 on Windows segfaults in avcodec_open2() when passing - # bad parameters. - # See https://github.com/pytorch/torchcodec/pull/806 - ffmpeg-version-for-tests: ['4.4.2', '6.1.1', '7.0.1', '8.0'] - needs: build - steps: - - uses: actions/download-artifact@v4 - with: - name: meta-pytorch_torchcodec__${{ matrix.python-version }}_cpu_x64 - path: pytorch/torchcodec/dist/ - - name: Setup conda env - uses: conda-incubator/setup-miniconda@v2 - with: - auto-update-conda: true - # Using miniforge instead of miniconda ensures that the default - # conda channel is conda-forge instead of main/default. This ensures - # ABI consistency between dependencies: - # https://conda-forge.org/docs/user/transitioning_from_defaults/ - miniforge-version: latest - activate-environment: test - python-version: ${{ matrix.python-version }} - - name: Update pip - run: python -m pip install --upgrade pip - - name: Install PyTorch - run: | - python -m pip install --pre torch torchvision --index-url https://download.pytorch.org/whl/nightly/cpu - - name: Install torchcodec from the wheel - run: | - wheel_path=`find pytorch/torchcodec/dist -type f -name "*.whl"` - echo Installing $wheel_path - python -m pip install $wheel_path -vvv - - name: Check out repo - uses: actions/checkout@v3 - - name: Install ffmpeg, post build - run: | - # Ideally we would have checked for that before installing the wheel, - # but we need to checkout the repo to access this file, and we don't - # want to checkout the repo before installing the wheel to avoid any - # side-effect. It's OK. - source packaging/helpers.sh - assert_ffmpeg_not_installed - conda install "ffmpeg=${{ matrix.ffmpeg-version-for-tests }}" -c conda-forge - ffmpeg -version - - name: Test torchcodec import after FFmpeg installation - run: | - echo "Testing torchcodec import after FFmpeg is installed and PATH is updated..." - python -c "import torchcodec; print('TorchCodec import successful!')" - - name: Install test dependencies - run: | - # Ideally we would find a way to get those dependencies from pyproject.toml - python -m pip install numpy pytest pillow - - name: Delete the src/ folder just for fun - run: | - # The only reason we checked-out the repo is to get access to the - # tests. We don't care about the rest. Out of precaution, we delete - # the src/ folder to be extra sure that we're running the code from - # the installed wheel rather than from the source. - # This is just to be extra cautious and very overkill because a) - # there's no way the `torchcodec` package from src/ can be found from - # the PythonPath: the main point of `src/` is precisely to protect - # against that and b) if we ever were to execute code from - # `src/torchcodec`, it would fail loudly because the built .so files - # aren't present there. - rm -r src/ - ls - - name: Run Python tests - run: | - pytest test -vvv diff --git a/test/cppapi/CMakeLists.txt b/test/cppapi/CMakeLists.txt new file mode 100644 index 000000000..cc7b5c941 --- /dev/null +++ b/test/cppapi/CMakeLists.txt @@ -0,0 +1,27 @@ +cmake_minimum_required(VERSION 3.18) +project(CppApiTest) +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +find_package(Torch REQUIRED) +find_package(TorchCodec REQUIRED) + +function(make_torchcodec_targets torchcodec_variant) + set(binname "torchcodec_cppapitest${torchcodec_variant}") + set(sources CppApiTest.cpp) + + # Building executable to check the linkage + add_executable(${binname} ${sources}) + # Avoid adding the "lib" prefix which we already add explicitly. + set_target_properties(${binname} PROPERTIES PREFIX "") + set_target_properties(${binname} PROPERTIES CXX_STANDARD 17) + target_link_libraries(${binname} + ${TORCH_LIBRARIES} + torchcodec::core${torchcodec_variant} + torchcodec::ffmpeg${torchcodec_variant} + ) +endfunction() + +foreach(variant IN LISTS TORCHCODEC_VARIANTS) + make_torchcodec_targets(${variant}) +endforeach() diff --git a/test/cppapi/CppApiTest.cpp b/test/cppapi/CppApiTest.cpp new file mode 100644 index 000000000..00459207d --- /dev/null +++ b/test/cppapi/CppApiTest.cpp @@ -0,0 +1,38 @@ +#include "DeviceInterface.h" +#include "FilterGraph.h" + +namespace facebook::torchcodec { + +class DummyDeviceInterface : public DeviceInterface { + public: + DummyDeviceInterface(const torch::Device& device) : DeviceInterface(device) {} + + virtual ~DummyDeviceInterface() {} + + void initialize( + const AVStream* avStream, + const UniqueDecodingAVFormatContext& avFormatCtx, + const SharedAVCodecContext& codecContext) override {} + + void convertAVFrameToFrameOutput( + UniqueAVFrame& avFrame, + FrameOutput& frameOutput, + std::optional preAllocatedOutputTensor = + std::nullopt) override {} + + private: + std::unique_ptr filterGraphContext_; +}; + +namespace { +static bool g_dummy = registerDeviceInterface( + DeviceInterfaceKey(torch::kPrivateUse1), + [](const torch::Device& device) { + return new DummyDeviceInterface(device); + }); +} // namespace +} // namespace facebook::torchcodec + +int main(int argc, char* argv[]) { + return 0; +} diff --git a/test/test_cppapi.py b/test/test_cppapi.py new file mode 100644 index 000000000..738b6277e --- /dev/null +++ b/test/test_cppapi.py @@ -0,0 +1,84 @@ +import os +import subprocess + +from pathlib import Path + +import torch +import torchcodec + + +def test_cppapi_pkgconfig(tmp_path): + cmake_args = [ + "cmake", + "-DCMAKE_BUILD_TYPE=Debug", + "-DCMAKE_VERBOSE_MAKEFILE=ON", + f"-DCMAKE_PREFIX_PATH={torchcodec.cmake_prefix_path};{torch.utils.cmake_prefix_path}", + Path(__file__).parent / "cppapi", + ] + result = subprocess.run(cmake_args, cwd=tmp_path) + assert result.returncode == 0 + + result = subprocess.run(["cmake", "--build", "."], cwd=tmp_path) + assert result.returncode == 0 + + ver = f"{torchcodec.ffmpeg_major_version}" + result = subprocess.run([f"./torchcodec_cppapitest{ver}"], cwd=tmp_path) + assert result.returncode == 0 + + +def test_cppapi_no_ffmpeg(tmp_path): + cmake_args = [ + "cmake", + "-DCMAKE_BUILD_TYPE=Debug", + "-DCMAKE_VERBOSE_MAKEFILE=ON", + f"-DCMAKE_PREFIX_PATH={torchcodec.cmake_prefix_path};{torch.utils.cmake_prefix_path}", + Path(__file__).parent / "cppapi", + ] + ver = f"{torchcodec.ffmpeg_major_version}" + my_env = os.environ.copy() + my_env[f"TORCHCODEC_FFMPEG{ver}_INSTALL_PREFIX"] = ( + Path(__file__).parent / "no-such-dir" + ) + + # cmake config should fail as we've set ffmpeg install prefix to the not existing + # directory + result = subprocess.run(cmake_args, cwd=tmp_path, env=my_env) + assert result.returncode != 0 + + +def test_cppapi_with_prefix(tmp_path): + cmake_args = [ + "cmake", + "-DCMAKE_BUILD_TYPE=Debug", + "-DCMAKE_VERBOSE_MAKEFILE=ON", + f"-DCMAKE_PREFIX_PATH={torchcodec.cmake_prefix_path};{torch.utils.cmake_prefix_path}", + Path(__file__).parent / "cppapi", + ] + + # In this test we are calculating the prefix of installed ffmpeg version from the location + # of its libavcodec.pc file. Potentially, on the custom ffmpeg install with custom layout + # our calculation can be wrong and test might fail. + result = subprocess.run( + ["pkg-config", "--variable=prefix", "libavcodec"], + capture_output=True, + text=True, + ) + assert result.returncode == 0 + + ver = f"{torchcodec.ffmpeg_major_version}" + my_env = os.environ.copy() + my_env[f"TORCHCODEC_FFMPEG{ver}_INSTALL_PREFIX"] = Path(f"{result.stdout.strip()}") + + result = subprocess.run(cmake_args, cwd=tmp_path, env=my_env) + assert result.returncode == 0 + + result = subprocess.run(["cmake", "--build", "."], cwd=tmp_path) + assert result.returncode == 0 + + ver = f"{torchcodec.ffmpeg_major_version}" + + result = subprocess.run(["ldd", f"./torchcodec_cppapitest{ver}"], cwd=tmp_path) + assert result.returncode == 0 + + result = subprocess.run([f"./torchcodec_cppapitest{ver}"], cwd=tmp_path) + assert result.returncode == 0