Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions test/cppapi/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
38 changes: 38 additions & 0 deletions test/cppapi/CppApiTest.cpp
Original file line number Diff line number Diff line change
@@ -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<torch::Tensor> preAllocatedOutputTensor =
std::nullopt) override {}

private:
std::unique_ptr<FilterGraph> 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;
}
80 changes: 80 additions & 0 deletions test/test_cppapi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
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", "--path", "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}"
).parent.parent.parent

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([f"./torchcodec_cppapitest{ver}"], cwd=tmp_path)
assert result.returncode == 0
Loading