Skip to content

Commit 15064d8

Browse files
committed
test: add cppapi test to give TorchCodecConfig.cmake a try
Signed-off-by: Dmitry Rogozhkin <[email protected]>
1 parent cb82662 commit 15064d8

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

test/cppapi/CMakeLists.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
cmake_minimum_required(VERSION 3.18)
2+
project(CppApiTest)
3+
set(CMAKE_CXX_STANDARD 17)
4+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
5+
6+
find_package(Torch REQUIRED)
7+
find_package(TorchCodec REQUIRED)
8+
9+
function(make_torchcodec_targets torchcodec_variant)
10+
set(binname "torchcodec_cppapitest${torchcodec_variant}")
11+
set(sources CppApiTest.cpp)
12+
13+
# Building executable to check the linkage
14+
add_executable(${binname} ${sources})
15+
# Avoid adding the "lib" prefix which we already add explicitly.
16+
set_target_properties(${binname} PROPERTIES PREFIX "")
17+
set_target_properties(${binname} PROPERTIES CXX_STANDARD 17)
18+
target_link_libraries(${binname}
19+
${TORCH_LIBRARIES}
20+
torchcodec::core${torchcodec_variant}
21+
torchcodec::ffmpeg${torchcodec_variant}
22+
)
23+
endfunction()
24+
25+
foreach(variant IN LISTS TORCHCODEC_VARIANTS)
26+
make_torchcodec_targets(${variant})
27+
endforeach()

test/cppapi/CppApiTest.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include "DeviceInterface.h"
2+
#include "FilterGraph.h"
3+
4+
namespace facebook::torchcodec {
5+
6+
class DummyDeviceInterface : public DeviceInterface {
7+
public:
8+
DummyDeviceInterface(const torch::Device& device) : DeviceInterface(device) {}
9+
10+
virtual ~DummyDeviceInterface() {}
11+
12+
void initialize(
13+
const AVStream* avStream,
14+
const UniqueDecodingAVFormatContext& avFormatCtx,
15+
const SharedAVCodecContext& codecContext) override {}
16+
17+
void convertAVFrameToFrameOutput(
18+
UniqueAVFrame& avFrame,
19+
FrameOutput& frameOutput,
20+
std::optional<torch::Tensor> preAllocatedOutputTensor =
21+
std::nullopt) override {}
22+
23+
private:
24+
std::unique_ptr<FilterGraph> filterGraphContext_;
25+
};
26+
27+
namespace {
28+
static bool g_dummy = registerDeviceInterface(
29+
DeviceInterfaceKey(torch::kPrivateUse1),
30+
[](const torch::Device& device) {
31+
return new DummyDeviceInterface(device);
32+
});
33+
} // namespace
34+
} // namespace facebook::torchcodec
35+
36+
int main(int argc, char* argv[]) {
37+
return 0;
38+
}

test/test_cppapi.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import os
2+
import subprocess
3+
4+
from pathlib import Path
5+
6+
import torch
7+
import torchcodec
8+
9+
10+
def test_cppapi_pkgconfig(tmp_path):
11+
cmake_args = [
12+
"cmake",
13+
"-DCMAKE_BUILD_TYPE=Debug",
14+
"-DCMAKE_VERBOSE_MAKEFILE=ON",
15+
f"-DCMAKE_PREFIX_PATH={torchcodec.cmake_prefix_path};{torch.utils.cmake_prefix_path}",
16+
Path(__file__).parent / "cppapi",
17+
]
18+
result = subprocess.run(cmake_args, cwd=tmp_path)
19+
assert result.returncode == 0
20+
21+
result = subprocess.run(["cmake", "--build", "."], cwd=tmp_path)
22+
assert result.returncode == 0
23+
24+
ver = f"{torchcodec.ffmpeg_major_version}"
25+
result = subprocess.run([f"./torchcodec_cppapitest{ver}"], cwd=tmp_path)
26+
assert result.returncode == 0
27+
28+
29+
def test_cppapi_no_ffmpeg(tmp_path):
30+
cmake_args = [
31+
"cmake",
32+
"-DCMAKE_BUILD_TYPE=Debug",
33+
"-DCMAKE_VERBOSE_MAKEFILE=ON",
34+
f"-DCMAKE_PREFIX_PATH={torchcodec.cmake_prefix_path};{torch.utils.cmake_prefix_path}",
35+
Path(__file__).parent / "cppapi",
36+
]
37+
ver = f"{torchcodec.ffmpeg_major_version}"
38+
my_env = os.environ.copy()
39+
my_env[f"TORCHCODEC_FFMPEG{ver}_INSTALL_PREFIX"] = (
40+
Path(__file__).parent / "no-such-dir"
41+
)
42+
43+
# cmake config should fail as we've set ffmpeg install prefix to the not existing
44+
# directory
45+
result = subprocess.run(cmake_args, cwd=tmp_path, env=my_env)
46+
assert result.returncode != 0
47+
48+
49+
def test_cppapi_with_prefix(tmp_path):
50+
cmake_args = [
51+
"cmake",
52+
"-DCMAKE_BUILD_TYPE=Debug",
53+
"-DCMAKE_VERBOSE_MAKEFILE=ON",
54+
f"-DCMAKE_PREFIX_PATH={torchcodec.cmake_prefix_path};{torch.utils.cmake_prefix_path}",
55+
Path(__file__).parent / "cppapi",
56+
]
57+
58+
# In this test we are calculating the prefix of installed ffmpeg version from the location
59+
# of its libavcodec.pc file. Potentially, on the custom ffmpeg install with custom layout
60+
# our calculation can be wrong and test might fail.
61+
result = subprocess.run(
62+
["pkg-config", "--path", "libavcodec"], capture_output=True, text=True
63+
)
64+
assert result.returncode == 0
65+
66+
ver = f"{torchcodec.ffmpeg_major_version}"
67+
my_env = os.environ.copy()
68+
my_env[f"TORCHCODEC_FFMPEG{ver}_INSTALL_PREFIX"] = Path(
69+
f"{result.stdout}"
70+
).parent.parent.parent
71+
72+
result = subprocess.run(cmake_args, cwd=tmp_path, env=my_env)
73+
assert result.returncode == 0
74+
75+
result = subprocess.run(["cmake", "--build", "."], cwd=tmp_path)
76+
assert result.returncode == 0
77+
78+
ver = f"{torchcodec.ffmpeg_major_version}"
79+
result = subprocess.run([f"./torchcodec_cppapitest{ver}"], cwd=tmp_path)
80+
assert result.returncode == 0

0 commit comments

Comments
 (0)