libvdo
vdo-example-rgb.cc
#include <vdo-error.h>
#include <vdo-stream.h>
#include <vdo-channel.h>
#include <cerrno>
#include <cstdio>
#include <cstdint>
#include <cstdlib>
#include <poll.h>
#include <unistd.h>
#include <print>
#include <chrono>
#include <utility>
int main()
try
{
g_autoptr(GError) error = nullptr;
auto failed = [&error] {
// POSIX error
if (!error) {
std::println(stderr, "<3>error: {}", strerror(errno));
return EXIT_FAILURE;
}
// Maintenance/Installation in progress (e.g. Global-Rotation)
if (vdo_error_is_expected(&error)) {
std::println(stderr, "<6>info: {}", error->message);
return EXIT_SUCCESS;
} else {
std::println(stderr, "<3>error: {}", error->message);
return EXIT_FAILURE;
}
};
g_autoptr(VdoStream) stream = vdo_stream_rgb_new(nullptr, 1u, {640u, 360u}, &error);
if (!stream)
return failed();
int fd = vdo_stream_get_fd(stream, &error);
if (fd < 0)
return failed();
pollfd fds = {.fd = fd, .events = POLL_IN,};
if (!vdo_stream_start(stream, &error))
return failed();
// Fetch 10 frames
for (size_t i = 0zu; i < 10zu;) {
int status = TEMP_FAILURE_RETRY(poll(&fds, 1, -1));
if (status < 0)
return failed();
g_autoptr(VdoBuffer) buffer = vdo_stream_get_buffer(stream, &error);
if (!buffer && g_error_matches(error, VDO_ERROR, VDO_ERROR_NO_DATA))
continue; // Transient Error -> Retry!
if (!buffer)
return failed();
VdoFrame* frame = vdo_buffer_get_frame(buffer);
// Low jitter monotonic capture timestamp. (See g_get_monotonic_time())
auto pts = vdo_frame_get_timestamp(frame);
auto pts_us = std::chrono::microseconds(pts);
std::println("{:%T}", pts_us);
// Allow VDO to reuse the frame/buffer.
if (!vdo_stream_buffer_unref(stream, &buffer, &error))
return failed();
// Only successful frames count!
i += 1;
}
return EXIT_SUCCESS;
}
catch(const std::exception& e)
{
std::ignore = fprintf(stderr, "Exception: %s\n", e.what());
return EXIT_FAILURE;
}
A video frame.
A video stream.
VdoFrame * vdo_buffer_get_frame(VdoBuffer *self)
Return a pointer to the underlying frame.
A class representing a channel.
Definitions related to error reporting, e.g. error codes.
@ VDO_ERROR_NO_DATA
Definition: vdo-error.h:37
#define VDO_ERROR
Vdo error domain.
Definition: vdo-error.h:19
gboolean vdo_error_is_expected(GError **error)
Check if error is expected.
guint64 vdo_frame_get_timestamp(VdoFrame *self)
Return the capture timestamp of this frame.
A class representing a stream session.
VdoBuffer * vdo_stream_get_buffer(VdoStream *self, GError **error)
Fetch a VdoBuffer containing a frame.
gboolean vdo_stream_start(VdoStream *self, GError **error)
Start this video stream.
gboolean vdo_stream_buffer_unref(VdoStream *self, VdoBuffer **buffer, GError **error)
Decrease the reference count for the specified buffer.
gint vdo_stream_get_fd(VdoStream *self, GError **error)
Return a file descriptor representing the underlying socket connection.
VdoStream * vdo_stream_rgb_new(const VdoMap *settings, guint32 input, VdoResolution resolution, GError **error)
Create a new RGB24(RGB888) VdoStream (AXIS OS 12.8+).