4c3b11445c
Phase 0 of design/pyrowave-codec-plan.md — the opt-in wired-LAN ultra-low- latency codec. Vendored at upstream 509e4f88 (API 0.4.0, Granite 44362775, volk + vulkan-headers pins in PUNKTFUNK-VENDOR.txt), pruned to the 6.6 MB the standalone no-renderer build needs; scripts/vendor-pyrowave.sh reproduces the tree (a pin bump is protocol-affecting, plan §4.2). build.rs drives the wrapper CMakeLists (static archives incl. a static C-API lib upstream only ships shared) + bindgen over pyrowave.h; Linux and Windows only, empty stub elsewhere (Apple gets a native Metal port, §4.7). Offline-safe by construction: no network, no system lib, vendored Vulkan headers — same model as the opus dep (flatpak builder has no network). Phase-0 validation on .21 (RTX 5070 Ti, driver 610.43.03): - upstream pyrowave-c-test + interop test (incl. dmabuf/DRM-modifier Vulkan<->Vulkan) pass, from the pristine AND the pruned tree - GPU kernel times at ~1.6 bpp noise: encode/decode 0.090/0.042 ms @800p, 0.146/0.067 @1080p, 0.226/0.103 @1440p, 0.477/0.201 @4K — order of magnitude under NVENC's 1-2 ms retrieve, CBR lands within ~100 B of target - cargo test -p pyrowave-sys green (static link + API-version pin check) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
183 lines
4.9 KiB
C++
183 lines
4.9 KiB
C++
/* Copyright (c) 2017-2026 Hans-Kristian Arntzen
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
* a copy of this software and associated documentation files (the
|
|
* "Software"), to deal in the Software without restriction, including
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
* the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be
|
|
* included in all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
*/
|
|
|
|
#include "message_queue.hpp"
|
|
#include "aligned_alloc.hpp"
|
|
#include "logging.hpp"
|
|
#include <algorithm>
|
|
#include <stdlib.h>
|
|
|
|
namespace Util
|
|
{
|
|
void MessageQueuePayloadDeleter::operator()(void *ptr)
|
|
{
|
|
memalign_free(ptr);
|
|
}
|
|
|
|
LockFreeMessageQueue::LockFreeMessageQueue()
|
|
{
|
|
for (unsigned i = 0; i < 8; i++)
|
|
payload_capacity[i] = 256u << i;
|
|
for (unsigned i = 0; i < 8; i++)
|
|
write_ring[i].reset((16u * 1024u) >> i);
|
|
read_ring.reset(32 * 1024);
|
|
|
|
// Pre-fill the rings.
|
|
for (unsigned i = 0; i < 8; i++)
|
|
{
|
|
unsigned count = 512u >> i;
|
|
for (unsigned j = 0; j < count; j++)
|
|
{
|
|
MessageQueuePayload payload;
|
|
payload.set_payload_data(memalign_calloc(64, payload_capacity[i]), payload_capacity[i]);
|
|
recycle_payload(std::move(payload));
|
|
}
|
|
}
|
|
}
|
|
|
|
size_t LockFreeMessageQueue::available_read_messages() const noexcept
|
|
{
|
|
return read_ring.read_avail();
|
|
}
|
|
|
|
MessageQueuePayload LockFreeMessageQueue::read_message() noexcept
|
|
{
|
|
MessageQueuePayload payload;
|
|
read_ring.read_and_move(payload);
|
|
return payload;
|
|
}
|
|
|
|
bool LockFreeMessageQueue::push_written_payload(MessageQueuePayload payload) noexcept
|
|
{
|
|
return read_ring.write_and_move(std::move(payload));
|
|
}
|
|
|
|
void LockFreeMessageQueue::recycle_payload(MessageQueuePayload payload) noexcept
|
|
{
|
|
for (unsigned i = 0; i < 8; i++)
|
|
{
|
|
if (payload.get_capacity() == payload_capacity[i])
|
|
{
|
|
write_ring[i].write_and_move(std::move(payload));
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
MessageQueuePayload LockFreeMessageQueue::allocate_write_payload(size_t size) noexcept
|
|
{
|
|
MessageQueuePayload payload;
|
|
for (unsigned i = 0; i < 8; i++)
|
|
{
|
|
if (size <= payload_capacity[i])
|
|
{
|
|
if (!write_ring[i].read_and_move(payload))
|
|
payload.set_payload_data(memalign_calloc(64, payload_capacity[i]), payload_capacity[i]);
|
|
return payload;
|
|
}
|
|
}
|
|
|
|
payload.set_payload_data(memalign_calloc(64, size), size);
|
|
return payload;
|
|
}
|
|
|
|
MessageQueue::MessageQueue()
|
|
{
|
|
corked.store(true);
|
|
}
|
|
|
|
void MessageQueue::cork()
|
|
{
|
|
corked.store(true, std::memory_order_relaxed);
|
|
}
|
|
|
|
void MessageQueue::uncork()
|
|
{
|
|
corked.store(false, std::memory_order_relaxed);
|
|
}
|
|
|
|
bool MessageQueue::is_uncorked() const
|
|
{
|
|
return !corked.load(std::memory_order_relaxed);
|
|
}
|
|
|
|
MessageQueuePayload MessageQueue::allocate_write_payload(size_t size) noexcept
|
|
{
|
|
if (corked.load(std::memory_order_relaxed))
|
|
return {};
|
|
std::lock_guard<std::mutex> holder{lock};
|
|
return LockFreeMessageQueue::allocate_write_payload(size);
|
|
}
|
|
|
|
bool MessageQueue::push_written_payload(MessageQueuePayload payload) noexcept
|
|
{
|
|
std::lock_guard<std::mutex> holder{lock};
|
|
return LockFreeMessageQueue::push_written_payload(std::move(payload));
|
|
}
|
|
|
|
size_t MessageQueue::available_read_messages() const noexcept
|
|
{
|
|
std::lock_guard<std::mutex> holder{lock};
|
|
return LockFreeMessageQueue::available_read_messages();
|
|
}
|
|
|
|
MessageQueuePayload MessageQueue::read_message() noexcept
|
|
{
|
|
std::lock_guard<std::mutex> holder{lock};
|
|
return LockFreeMessageQueue::read_message();
|
|
}
|
|
|
|
void MessageQueue::recycle_payload(MessageQueuePayload payload) noexcept
|
|
{
|
|
std::lock_guard<std::mutex> holder{lock};
|
|
return LockFreeMessageQueue::recycle_payload(std::move(payload));
|
|
}
|
|
|
|
bool MessageQueue::log(const char *tag, const char *fmt, va_list va)
|
|
{
|
|
if (!is_uncorked())
|
|
return false;
|
|
char message_buffer[16 * 1024];
|
|
memcpy(message_buffer, tag, strlen(tag));
|
|
|
|
vsnprintf(message_buffer + strlen(tag), sizeof(message_buffer) - strlen(tag), fmt, va);
|
|
va_end(va);
|
|
|
|
size_t message_size = strlen(message_buffer) + 1;
|
|
|
|
while (message_size >= 2 && message_buffer[message_size - 2] == '\n')
|
|
{
|
|
message_buffer[message_size - 2] = '\0';
|
|
message_size--;
|
|
}
|
|
|
|
auto message_payload = allocate_write_payload(message_size);
|
|
if (message_payload)
|
|
{
|
|
memcpy(static_cast<char *>(message_payload.get_payload_data()), message_buffer, message_size);
|
|
push_written_payload(std::move(message_payload));
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|