MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
mxl.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025 Contributors to the Media eXchange Layer project.
2// SPDX-License-Identifier: Apache-2.0
3
4#include "mxl/mxl.h"
5#include <exception>
6#include <memory>
7#include <string>
8#include <mxl/version.h>
13
14#ifdef __linux__
15# include <sys/vfs.h>
16# include <linux/magic.h>
17#elif defined(__APPLE__)
18# include <cstring>
19# include <sys/mount.h>
20# include <sys/param.h>
21#endif
22
23static char const g_mxl_version_string[] = MXL_VERSION_FULL;
24
25extern "C"
28{
29 if (out_version != nullptr)
30 {
31 out_version->major = MXL_VERSION_MAJOR;
32 out_version->minor = MXL_VERSION_MINOR;
33 out_version->bugfix = MXL_VERSION_PATCH;
34 out_version->build = MXL_VERSION_BUILD;
35 out_version->full = g_mxl_version_string;
36 return MXL_STATUS_OK;
37 }
38 else
39 {
41 }
42}
43
44extern "C" MXL_EXPORT
45mxlInstance mxlCreateInstance(char const* in_mxlDomain, char const* in_options)
46{
47 if (in_mxlDomain == nullptr)
48 {
49 MXL_ERROR("Failed to create instance : the domain is NULL");
50 return nullptr;
51 }
52
53 try
54 {
55 auto const opts = (in_options != nullptr) ? in_options : "";
56 auto domainWatcher = std::make_shared<mxl::lib::DomainWatcher>(in_mxlDomain);
57 auto flowIoFactory = std::make_unique<mxl::lib::PosixFlowIoFactory>(domainWatcher);
58 return reinterpret_cast<mxlInstance>(new mxl::lib::Instance{in_mxlDomain, opts, std::move(flowIoFactory), std::move(domainWatcher)});
59 }
60 catch (std::exception& e)
61 {
62 MXL_ERROR("Failed to create instance : {}", e.what());
63 return nullptr;
64 }
65 catch (...)
66 {
67 MXL_ERROR("Failed to create instance");
68 return nullptr;
69 }
70}
71
72extern "C" MXL_EXPORT
73mxlStatus mxlIsTmpFs(char const* in_path, bool* out_isTmpFs)
74{
75 if ((in_path == nullptr) || (out_isTmpFs == nullptr))
76 {
78 }
79
80#ifdef __linux__
81 using statfs_t = struct statfs;
82 auto buf = statfs_t{};
83 if (::statfs(in_path, &buf) != 0)
84 {
85 return MXL_ERR_UNKNOWN;
86 }
87 *out_isTmpFs = (buf.f_type == TMPFS_MAGIC) || (buf.f_type == RAMFS_MAGIC);
88 return MXL_STATUS_OK;
89#elif defined(__APPLE__)
90 using statfs_t = struct statfs;
91 auto buf = statfs_t{};
92 if (::statfs(in_path, &buf) != 0)
93 {
94 return MXL_ERR_UNKNOWN;
95 }
96 *out_isTmpFs = std::strcmp(buf.f_fstypename, "tmpfs") == 0;
97 return MXL_STATUS_OK;
98#else
99 *out_isTmpFs = false;
100 return MXL_STATUS_OK;
101#endif
102}
103
104extern "C"
107{
108 try
109 {
110 delete mxl::lib::to_Instance(in_instance);
111 return (in_instance != nullptr) ? MXL_STATUS_OK : MXL_ERR_INVALID_ARG;
112 }
113 catch (...)
114 {
115 return MXL_ERR_UNKNOWN;
116 }
117}
118
119extern "C"
122{
123 try
124 {
125 if (auto const instance = mxl::lib::to_Instance(in_instance); instance != nullptr)
126 {
127 MXL_DEBUG("Starting garbage collection of flows");
128 [[maybe_unused]]
129 auto count = instance->garbageCollect();
130 MXL_DEBUG("Garbage collected {} flows", count);
131 return MXL_STATUS_OK;
132 }
133 else
134 {
135 return MXL_ERR_INVALID_ARG;
136 }
137 }
138 catch (...)
139 {
140 return MXL_ERR_UNKNOWN;
141 }
142}
#define MXL_ERROR(...)
Definition Logging.hpp:67
#define MXL_DEBUG(...)
Definition Logging.hpp:34
MXL_EXPORT mxlStatus mxlDestroyInstance(mxlInstance in_instance)
Definition mxl.cpp:106
MXL_EXPORT mxlStatus mxlGetVersion(mxlVersionType *out_version)
Definition mxl.cpp:27
MXL_EXPORT mxlInstance mxlCreateInstance(char const *in_mxlDomain, char const *in_options)
Definition mxl.cpp:45
MXL_EXPORT mxlStatus mxlIsTmpFs(char const *in_path, bool *out_isTmpFs)
Definition mxl.cpp:73
MXL_EXPORT mxlStatus mxlGarbageCollectFlows(mxlInstance in_instance)
Definition mxl.cpp:121
static char const g_mxl_version_string[]
Definition mxl.cpp:23
mxlStatus
MXL SDK Status codes.
Definition mxl.h:22
@ MXL_ERR_UNKNOWN
Definition mxl.h:27
@ MXL_ERR_INVALID_ARG
Definition mxl.h:35
@ MXL_STATUS_OK
Definition mxl.h:23
struct mxlInstance_t * mxlInstance
Definition mxl.h:74
Instance * to_Instance(mxlInstance instance) noexcept
Utility function to convert from a C mxlInstance handle to a C++ Instance class.
Definition Instance.hpp:247
#define MXL_EXPORT
Definition platform.h:9
MXL SDK Semantic versionning structure.
Definition mxl.h:57
uint16_t minor
Definition mxl.h:59
char const * full
Definition mxl.h:62
uint16_t build
Definition mxl.h:61
uint16_t bugfix
Definition mxl.h:60
uint16_t major
Definition mxl.h:58