Strip superseded Muninn OBS streaming receiver from native/obs_stem_source
This commit is contained in:
parent
c6f4541eea
commit
ca9f1298e4
16 changed files with 2 additions and 6028 deletions
|
|
@ -3,10 +3,8 @@ cmake_minimum_required(VERSION 3.20)
|
|||
project(mimir_obs_stem_source LANGUAGES CXX)
|
||||
|
||||
find_package(libobs REQUIRED CONFIG)
|
||||
include(CTest)
|
||||
|
||||
add_library(mimir_obs_stem_source MODULE
|
||||
mimir_obs_muninn_source.cpp
|
||||
mimir_obs_program_texture_source.cpp
|
||||
mimir_obs_stem_source.cpp)
|
||||
|
||||
|
|
@ -15,52 +13,4 @@ target_compile_definitions(mimir_obs_stem_source PRIVATE
|
|||
WIN32_LEAN_AND_MEAN
|
||||
NOMINMAX
|
||||
_WIN32_WINNT=0x0A00)
|
||||
target_link_libraries(mimir_obs_stem_source PRIVATE OBS::libobs d3d11 d3d12 dxgi ws2_32)
|
||||
|
||||
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../../tools/muninn_media_decoder_probe.cpp")
|
||||
add_executable(muninn_media_decoder_probe
|
||||
../../tools/muninn_media_decoder_probe.cpp)
|
||||
target_compile_features(muninn_media_decoder_probe PRIVATE cxx_std_20)
|
||||
target_include_directories(muninn_media_decoder_probe PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR})
|
||||
endif()
|
||||
|
||||
add_executable(muninn_rudp_url_tests
|
||||
tests/muninn_rudp_url_tests.cpp)
|
||||
target_compile_features(muninn_rudp_url_tests PRIVATE cxx_std_20)
|
||||
target_include_directories(muninn_rudp_url_tests PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR})
|
||||
add_test(NAME muninn_rudp_url_tests COMMAND muninn_rudp_url_tests)
|
||||
|
||||
add_executable(muninn_media_wire_tests
|
||||
tests/muninn_media_wire_tests.cpp)
|
||||
target_compile_features(muninn_media_wire_tests PRIVATE cxx_std_20)
|
||||
target_include_directories(muninn_media_wire_tests PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR})
|
||||
add_test(NAME muninn_media_wire_tests COMMAND muninn_media_wire_tests)
|
||||
|
||||
add_executable(muninn_audio_fec_tests
|
||||
tests/muninn_audio_fec_tests.cpp)
|
||||
target_compile_features(muninn_audio_fec_tests PRIVATE cxx_std_20)
|
||||
target_include_directories(muninn_audio_fec_tests PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR})
|
||||
add_test(NAME muninn_audio_fec_tests COMMAND muninn_audio_fec_tests)
|
||||
|
||||
add_executable(muninn_video_fec_tests
|
||||
tests/muninn_video_fec_tests.cpp)
|
||||
target_compile_features(muninn_video_fec_tests PRIVATE cxx_std_20)
|
||||
target_include_directories(muninn_video_fec_tests PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR})
|
||||
add_test(NAME muninn_video_fec_tests COMMAND muninn_video_fec_tests)
|
||||
|
||||
add_executable(muninn_udp_socket_tests
|
||||
tests/muninn_udp_socket_tests.cpp)
|
||||
target_compile_features(muninn_udp_socket_tests PRIVATE cxx_std_20)
|
||||
target_compile_definitions(muninn_udp_socket_tests PRIVATE
|
||||
WIN32_LEAN_AND_MEAN
|
||||
NOMINMAX
|
||||
_WIN32_WINNT=0x0A00)
|
||||
target_include_directories(muninn_udp_socket_tests PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR})
|
||||
target_link_libraries(muninn_udp_socket_tests PRIVATE ws2_32)
|
||||
add_test(NAME muninn_udp_socket_tests COMMAND muninn_udp_socket_tests)
|
||||
target_link_libraries(mimir_obs_stem_source PRIVATE OBS::libobs d3d11 d3d12 dxgi)
|
||||
|
|
|
|||
|
|
@ -2,13 +2,3 @@ MimirAudioStem="Mimir Audio Stem"
|
|||
MimirAudioStem.MapName="Shared memory map"
|
||||
MimirAudioStem.StemId="Stem id"
|
||||
MimirProgramTexture="Mimir Program Texture"
|
||||
MuninnStream="Muninn Stream"
|
||||
MuninnVideoSource="Muninn Video"
|
||||
MuninnAudioSource="Muninn Audio"
|
||||
MuninnStream.Stream="Muninn stream"
|
||||
MuninnStream.Video="Video device"
|
||||
MuninnStream.Audio="Audio device"
|
||||
MuninnStream.VideoBitrateKbps="Video bitrate (kbps)"
|
||||
MuninnStream.VideoLatencyMs="Video/transport latency (ms)"
|
||||
MuninnStream.AudioReorderMs="Audio reorder latency (ms)"
|
||||
MuninnStream.Refresh="Refresh Muninn streams"
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -16,7 +16,6 @@ OBS_DECLARE_MODULE()
|
|||
OBS_MODULE_USE_DEFAULT_LOCALE("mimir-obs-stem-source", "en-US")
|
||||
|
||||
void mimir_register_program_texture_source();
|
||||
void mimir_register_muninn_source();
|
||||
|
||||
struct MimirStemSource {
|
||||
obs_source_t *source = nullptr;
|
||||
|
|
@ -210,11 +209,8 @@ bool obs_module_load(void)
|
|||
{
|
||||
obs_register_source(&mimir_stem_source_info);
|
||||
mimir_register_program_texture_source();
|
||||
mimir_register_muninn_source();
|
||||
blog(
|
||||
LOG_INFO,
|
||||
"Mimir OBS sources loaded: mimir_audio_stem_source, mimir_program_texture_source, "
|
||||
"muninn_stream_source, muninn_video_source, muninn_audio_source; "
|
||||
"Muninn Odin discovery build 20260705.2");
|
||||
"Mimir OBS sources loaded: mimir_audio_stem_source, mimir_program_texture_source");
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,179 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <iterator>
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace muninn_audio_fec {
|
||||
|
||||
inline constexpr size_t DataShards = 4;
|
||||
inline constexpr size_t ParityShards = 2;
|
||||
inline constexpr size_t TotalShards = DataShards + ParityShards;
|
||||
using Bytes = std::vector<uint8_t>;
|
||||
using Shards = std::array<std::optional<Bytes>, TotalShards>;
|
||||
|
||||
inline uint8_t multiply(uint8_t left, uint8_t right)
|
||||
{
|
||||
uint16_t product = 0;
|
||||
uint16_t a = left;
|
||||
uint16_t b = right;
|
||||
while (b != 0) {
|
||||
if ((b & 1u) != 0) product ^= a;
|
||||
b >>= 1u;
|
||||
a <<= 1u;
|
||||
if ((a & 0x100u) != 0) a ^= 0x11du;
|
||||
}
|
||||
return static_cast<uint8_t>(product);
|
||||
}
|
||||
|
||||
inline uint8_t power(uint8_t value, uint16_t exponent)
|
||||
{
|
||||
uint8_t result = 1;
|
||||
while (exponent != 0) {
|
||||
if ((exponent & 1u) != 0) result = multiply(result, value);
|
||||
value = multiply(value, value);
|
||||
exponent >>= 1u;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
inline uint8_t inverse(uint8_t value)
|
||||
{
|
||||
return value == 0 ? 0 : power(value, 254);
|
||||
}
|
||||
|
||||
inline constexpr std::array<std::array<uint8_t, DataShards>, TotalShards> Generator{{
|
||||
{{1, 0, 0, 0}}, {{0, 1, 0, 0}}, {{0, 0, 1, 0}}, {{0, 0, 0, 1}},
|
||||
{{1, 1, 1, 1}}, {{1, 2, 4, 8}}
|
||||
}};
|
||||
|
||||
inline std::array<Bytes, ParityShards> encode(const std::array<Bytes, DataShards> &data)
|
||||
{
|
||||
const size_t bytes = data[0].size();
|
||||
for (const auto &shard : data) if (shard.size() != bytes) return {};
|
||||
std::array<Bytes, ParityShards> parity{Bytes(bytes), Bytes(bytes)};
|
||||
for (size_t p = 0; p < ParityShards; ++p)
|
||||
for (size_t d = 0; d < DataShards; ++d)
|
||||
for (size_t i = 0; i < bytes; ++i)
|
||||
parity[p][i] ^= multiply(Generator[DataShards + p][d], data[d][i]);
|
||||
return parity;
|
||||
}
|
||||
|
||||
inline bool recover(Shards &shards)
|
||||
{
|
||||
size_t shard_bytes = 0;
|
||||
std::array<size_t, DataShards> available{};
|
||||
size_t count = 0;
|
||||
for (size_t i = 0; i < TotalShards; ++i) {
|
||||
if (!shards[i]) continue;
|
||||
if (shard_bytes == 0) shard_bytes = shards[i]->size();
|
||||
if (shards[i]->size() != shard_bytes) return false;
|
||||
if (count < DataShards) available[count++] = i;
|
||||
}
|
||||
if (count < DataShards || shard_bytes == 0) return false;
|
||||
|
||||
std::array<std::array<uint8_t, DataShards * 2>, DataShards> matrix{};
|
||||
for (size_t row = 0; row < DataShards; ++row) {
|
||||
for (size_t col = 0; col < DataShards; ++col) matrix[row][col] = Generator[available[row]][col];
|
||||
matrix[row][DataShards + row] = 1;
|
||||
}
|
||||
for (size_t col = 0; col < DataShards; ++col) {
|
||||
size_t pivot = col;
|
||||
while (pivot < DataShards && matrix[pivot][col] == 0) ++pivot;
|
||||
if (pivot == DataShards) return false;
|
||||
if (pivot != col) std::swap(matrix[pivot], matrix[col]);
|
||||
const uint8_t scale = inverse(matrix[col][col]);
|
||||
for (size_t j = 0; j < DataShards * 2; ++j) matrix[col][j] = multiply(matrix[col][j], scale);
|
||||
for (size_t row = 0; row < DataShards; ++row) {
|
||||
if (row == col) continue;
|
||||
const uint8_t factor = matrix[row][col];
|
||||
for (size_t j = 0; j < DataShards * 2; ++j)
|
||||
matrix[row][j] ^= multiply(factor, matrix[col][j]);
|
||||
}
|
||||
}
|
||||
for (size_t missing = 0; missing < DataShards; ++missing) {
|
||||
if (shards[missing]) continue;
|
||||
Bytes decoded(shard_bytes);
|
||||
for (size_t row = 0; row < DataShards; ++row) {
|
||||
const uint8_t coefficient = matrix[missing][DataShards + row];
|
||||
for (size_t i = 0; i < shard_bytes; ++i)
|
||||
decoded[i] ^= multiply(coefficient, (*shards[available[row]])[i]);
|
||||
}
|
||||
shards[missing] = std::move(decoded);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
struct BlockKey {
|
||||
std::string stream_id;
|
||||
std::string session_id;
|
||||
uint64_t base_packet_id = 0;
|
||||
bool operator<(const BlockKey &other) const
|
||||
{
|
||||
return std::tie(stream_id, session_id, base_packet_id) <
|
||||
std::tie(other.stream_id, other.session_id, other.base_packet_id);
|
||||
}
|
||||
};
|
||||
|
||||
struct RecoveredPacket { uint64_t packet_id; Bytes payload; };
|
||||
|
||||
class BlockReceiver {
|
||||
public:
|
||||
using Clock = std::chrono::steady_clock;
|
||||
explicit BlockReceiver(std::chrono::milliseconds deadline, size_t max_blocks = 128)
|
||||
: deadline_(deadline), max_blocks_(max_blocks) {}
|
||||
|
||||
std::vector<RecoveredPacket> insert(const BlockKey &key, size_t shard_index, Bytes payload,
|
||||
Clock::time_point now)
|
||||
{
|
||||
expire(now);
|
||||
if (shard_index >= TotalShards || payload.empty() || completed_.count(key) != 0) return {};
|
||||
if (blocks_.size() >= max_blocks_ && blocks_.count(key) == 0) blocks_.erase(blocks_.begin());
|
||||
auto [it, created] = blocks_.try_emplace(key, Block{now, payload.size(), {}});
|
||||
Block &block = it->second;
|
||||
if (payload.size() != block.shard_bytes || block.shards[shard_index]) return {};
|
||||
block.shards[shard_index] = std::move(payload);
|
||||
size_t present = 0;
|
||||
for (const auto &shard : block.shards) if (shard) ++present;
|
||||
if (present < DataShards) return {};
|
||||
Shards before = block.shards;
|
||||
if (!recover(block.shards)) return {};
|
||||
std::vector<RecoveredPacket> result;
|
||||
for (size_t i = 0; i < DataShards; ++i)
|
||||
if (!before[i] && block.shards[i]) result.push_back({key.base_packet_id + i, *block.shards[i]});
|
||||
blocks_.erase(it);
|
||||
completed_[key] = now;
|
||||
return result;
|
||||
}
|
||||
|
||||
void expire(Clock::time_point now)
|
||||
{
|
||||
for (auto it = blocks_.begin(); it != blocks_.end();)
|
||||
it = now - it->second.created_at >= deadline_ ? blocks_.erase(it) : std::next(it);
|
||||
for (auto it = completed_.begin(); it != completed_.end();)
|
||||
it = now - it->second >= deadline_ ? completed_.erase(it) : std::next(it);
|
||||
}
|
||||
size_t pending_blocks() const { return blocks_.size(); }
|
||||
void clear()
|
||||
{
|
||||
blocks_.clear();
|
||||
completed_.clear();
|
||||
}
|
||||
|
||||
private:
|
||||
struct Block { Clock::time_point created_at; size_t shard_bytes; Shards shards; };
|
||||
std::chrono::milliseconds deadline_;
|
||||
size_t max_blocks_;
|
||||
std::map<BlockKey, Block> blocks_;
|
||||
std::map<BlockKey, Clock::time_point> completed_;
|
||||
};
|
||||
|
||||
} // namespace muninn_audio_fec
|
||||
|
|
@ -1,274 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <climits>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace muninn_media_wire {
|
||||
|
||||
inline constexpr const char *ReceiverFeedbackSchema = "muninn.media_receiver_feedback.v1";
|
||||
|
||||
class MsgpackWriter {
|
||||
public:
|
||||
const std::vector<uint8_t> &bytes() const { return bytes_; }
|
||||
|
||||
void write_raw(const std::vector<uint8_t> &value)
|
||||
{
|
||||
bytes_.insert(bytes_.end(), value.begin(), value.end());
|
||||
}
|
||||
|
||||
void write_nil() { bytes_.push_back(0xc0); }
|
||||
|
||||
void write_bool(bool value) { bytes_.push_back(value ? 0xc3 : 0xc2); }
|
||||
|
||||
void write_u64(uint64_t value)
|
||||
{
|
||||
if (value <= 0x7f) {
|
||||
bytes_.push_back(static_cast<uint8_t>(value));
|
||||
} else if (value <= 0xff) {
|
||||
bytes_.push_back(0xcc);
|
||||
bytes_.push_back(static_cast<uint8_t>(value));
|
||||
} else if (value <= 0xffff) {
|
||||
bytes_.push_back(0xcd);
|
||||
write_be16_value(static_cast<uint16_t>(value));
|
||||
} else if (value <= 0xffffffffull) {
|
||||
bytes_.push_back(0xce);
|
||||
write_be32_value(static_cast<uint32_t>(value));
|
||||
} else {
|
||||
bytes_.push_back(0xcf);
|
||||
write_be64_value(value);
|
||||
}
|
||||
}
|
||||
|
||||
void write_i64(int64_t value)
|
||||
{
|
||||
if (value >= 0) {
|
||||
write_u64(static_cast<uint64_t>(value));
|
||||
return;
|
||||
}
|
||||
if (value >= -32) {
|
||||
bytes_.push_back(static_cast<uint8_t>(0xe0 | (value + 32)));
|
||||
} else if (value >= INT8_MIN) {
|
||||
bytes_.push_back(0xd0);
|
||||
bytes_.push_back(static_cast<uint8_t>(value));
|
||||
} else if (value >= INT16_MIN) {
|
||||
bytes_.push_back(0xd1);
|
||||
write_be16_value(static_cast<uint16_t>(value));
|
||||
} else if (value >= INT32_MIN) {
|
||||
bytes_.push_back(0xd2);
|
||||
write_be32_value(static_cast<uint32_t>(value));
|
||||
} else {
|
||||
bytes_.push_back(0xd3);
|
||||
write_be64_value(static_cast<uint64_t>(value));
|
||||
}
|
||||
}
|
||||
|
||||
void write_string(const std::string &value)
|
||||
{
|
||||
const auto length = value.size();
|
||||
if (length <= 31) {
|
||||
bytes_.push_back(static_cast<uint8_t>(0xa0 | length));
|
||||
} else if (length <= 0xff) {
|
||||
bytes_.push_back(0xd9);
|
||||
bytes_.push_back(static_cast<uint8_t>(length));
|
||||
} else if (length <= 0xffff) {
|
||||
bytes_.push_back(0xda);
|
||||
write_be16_value(static_cast<uint16_t>(length));
|
||||
} else {
|
||||
bytes_.push_back(0xdb);
|
||||
write_be32_value(static_cast<uint32_t>(length));
|
||||
}
|
||||
bytes_.insert(bytes_.end(), value.begin(), value.end());
|
||||
}
|
||||
|
||||
void write_binary(const std::vector<uint8_t> &value)
|
||||
{
|
||||
const auto length = value.size();
|
||||
if (length <= 0xff) {
|
||||
bytes_.push_back(0xc4);
|
||||
bytes_.push_back(static_cast<uint8_t>(length));
|
||||
} else if (length <= 0xffff) {
|
||||
bytes_.push_back(0xc5);
|
||||
write_be16_value(static_cast<uint16_t>(length));
|
||||
} else {
|
||||
bytes_.push_back(0xc6);
|
||||
write_be32_value(static_cast<uint32_t>(length));
|
||||
}
|
||||
bytes_.insert(bytes_.end(), value.begin(), value.end());
|
||||
}
|
||||
|
||||
void write_array_len(uint32_t length)
|
||||
{
|
||||
if (length <= 15) {
|
||||
bytes_.push_back(static_cast<uint8_t>(0x90 | length));
|
||||
} else if (length <= 0xffff) {
|
||||
bytes_.push_back(0xdc);
|
||||
write_be16_value(static_cast<uint16_t>(length));
|
||||
} else {
|
||||
bytes_.push_back(0xdd);
|
||||
write_be32_value(length);
|
||||
}
|
||||
}
|
||||
|
||||
void write_map_len(uint32_t length)
|
||||
{
|
||||
if (length <= 15) {
|
||||
bytes_.push_back(static_cast<uint8_t>(0x80 | length));
|
||||
} else if (length <= 0xffff) {
|
||||
bytes_.push_back(0xde);
|
||||
write_be16_value(static_cast<uint16_t>(length));
|
||||
} else {
|
||||
bytes_.push_back(0xdf);
|
||||
write_be32_value(length);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<uint8_t> bytes_;
|
||||
|
||||
void write_be16_value(uint16_t value)
|
||||
{
|
||||
bytes_.push_back(static_cast<uint8_t>((value >> 8) & 0xff));
|
||||
bytes_.push_back(static_cast<uint8_t>(value & 0xff));
|
||||
}
|
||||
|
||||
void write_be32_value(uint32_t value)
|
||||
{
|
||||
bytes_.push_back(static_cast<uint8_t>((value >> 24) & 0xff));
|
||||
bytes_.push_back(static_cast<uint8_t>((value >> 16) & 0xff));
|
||||
bytes_.push_back(static_cast<uint8_t>((value >> 8) & 0xff));
|
||||
bytes_.push_back(static_cast<uint8_t>(value & 0xff));
|
||||
}
|
||||
|
||||
void write_be64_value(uint64_t value)
|
||||
{
|
||||
write_be32_value(static_cast<uint32_t>((value >> 32) & 0xffffffffull));
|
||||
write_be32_value(static_cast<uint32_t>(value & 0xffffffffull));
|
||||
}
|
||||
};
|
||||
|
||||
inline std::string feedback_observed_at()
|
||||
{
|
||||
const auto now = std::chrono::system_clock::now().time_since_epoch();
|
||||
const auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(now).count();
|
||||
return "unix-ms:" + std::to_string(millis);
|
||||
}
|
||||
|
||||
inline void write_string_array(MsgpackWriter &writer, const std::vector<std::string> &values)
|
||||
{
|
||||
writer.write_array_len(static_cast<uint32_t>(values.size()));
|
||||
for (const auto &value : values) {
|
||||
writer.write_string(value);
|
||||
}
|
||||
}
|
||||
|
||||
inline void write_u64_array(MsgpackWriter &writer, const std::vector<uint64_t> &values)
|
||||
{
|
||||
writer.write_array_len(static_cast<uint32_t>(values.size()));
|
||||
for (const auto value : values) {
|
||||
writer.write_u64(value);
|
||||
}
|
||||
}
|
||||
|
||||
inline std::vector<uint64_t> receiver_feedback_late_frame_ids(uint64_t frame_id, bool late_frame)
|
||||
{
|
||||
return late_frame ? std::vector<uint64_t>{frame_id} : std::vector<uint64_t>{};
|
||||
}
|
||||
|
||||
inline std::vector<uint8_t> encode_receiver_feedback_payload(
|
||||
const std::string &stream_id,
|
||||
const std::string &session_id,
|
||||
uint64_t frame_id,
|
||||
const std::vector<std::string> &missing_chunk_keys,
|
||||
bool requested_keyframe,
|
||||
bool late_frame,
|
||||
const std::string &observed_at)
|
||||
{
|
||||
MsgpackWriter record;
|
||||
record.write_array_len(11);
|
||||
record.write_string(stream_id);
|
||||
record.write_string(session_id);
|
||||
record.write_string("starfire.obs");
|
||||
if (frame_id > 0) {
|
||||
record.write_u64(frame_id - 1);
|
||||
} else {
|
||||
record.write_nil();
|
||||
}
|
||||
write_u64_array(record, {});
|
||||
write_u64_array(record, receiver_feedback_late_frame_ids(frame_id, late_frame));
|
||||
record.write_bool(requested_keyframe);
|
||||
record.write_i64(0);
|
||||
record.write_i64(0);
|
||||
record.write_string(observed_at);
|
||||
write_string_array(record, missing_chunk_keys);
|
||||
|
||||
const std::string record_key = stream_id + ":" + session_id + ":feedback:starfire.obs";
|
||||
const std::string message_id =
|
||||
"muninn-media:" + std::string(ReceiverFeedbackSchema) + ":" + record_key;
|
||||
|
||||
MsgpackWriter document;
|
||||
document.write_map_len(9);
|
||||
document.write_string("schemaId");
|
||||
document.write_string(ReceiverFeedbackSchema);
|
||||
document.write_string("recordKey");
|
||||
document.write_string(record_key);
|
||||
document.write_string("storedAt");
|
||||
document.write_string(observed_at);
|
||||
document.write_string("payloadEncoding");
|
||||
document.write_string("messagepack");
|
||||
document.write_string("payload");
|
||||
document.write_binary(record.bytes());
|
||||
document.write_string("sourceRuntimeId");
|
||||
document.write_string("starfire");
|
||||
document.write_string("sourceAgentId");
|
||||
document.write_nil();
|
||||
document.write_string("sourceRole");
|
||||
document.write_string("mimir.obs");
|
||||
document.write_string("tags");
|
||||
write_string_array(document, {"muninn.media"});
|
||||
|
||||
MsgpackWriter wire;
|
||||
wire.write_map_len(3);
|
||||
wire.write_string("schemaVersion");
|
||||
wire.write_string("cultnet.document_put_raw.v0");
|
||||
wire.write_string("messageId");
|
||||
wire.write_string(message_id);
|
||||
wire.write_string("document");
|
||||
wire.write_raw(document.bytes());
|
||||
return wire.bytes();
|
||||
}
|
||||
|
||||
inline std::vector<uint8_t> encode_receiver_feedback_payload(
|
||||
const std::string &stream_id,
|
||||
const std::string &session_id,
|
||||
uint64_t frame_id,
|
||||
const std::vector<std::string> &missing_chunk_keys,
|
||||
bool requested_keyframe)
|
||||
{
|
||||
return encode_receiver_feedback_payload(
|
||||
stream_id,
|
||||
session_id,
|
||||
frame_id,
|
||||
missing_chunk_keys,
|
||||
requested_keyframe,
|
||||
requested_keyframe,
|
||||
feedback_observed_at());
|
||||
}
|
||||
|
||||
inline std::vector<uint8_t> encode_receiver_feedback_payload(
|
||||
const std::string &stream_id,
|
||||
const std::string &session_id,
|
||||
uint64_t frame_id,
|
||||
const std::vector<std::string> &missing_chunk_keys)
|
||||
{
|
||||
return encode_receiver_feedback_payload(
|
||||
stream_id,
|
||||
session_id,
|
||||
frame_id,
|
||||
missing_chunk_keys,
|
||||
true);
|
||||
}
|
||||
|
||||
} // namespace muninn_media_wire
|
||||
|
|
@ -1,54 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <set>
|
||||
#include <utility>
|
||||
|
||||
namespace muninn_rudp_ack {
|
||||
|
||||
class AckTracker {
|
||||
public:
|
||||
void reset()
|
||||
{
|
||||
received_sequences_.clear();
|
||||
highest_received_sequence_.reset();
|
||||
}
|
||||
|
||||
void remember(uint32_t sequence)
|
||||
{
|
||||
received_sequences_.insert(sequence);
|
||||
if (!highest_received_sequence_ || sequence > *highest_received_sequence_) {
|
||||
highest_received_sequence_ = sequence;
|
||||
}
|
||||
if (!highest_received_sequence_) {
|
||||
return;
|
||||
}
|
||||
const uint32_t highest = *highest_received_sequence_;
|
||||
for (auto iterator = received_sequences_.begin(); iterator != received_sequences_.end();) {
|
||||
if (*iterator + 32 < highest) {
|
||||
iterator = received_sequences_.erase(iterator);
|
||||
} else {
|
||||
++iterator;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::pair<uint32_t, uint32_t> state() const
|
||||
{
|
||||
const uint32_t ack = highest_received_sequence_.value_or(0);
|
||||
uint32_t ack_mask = 0;
|
||||
for (uint32_t bit = 0; bit < 32; ++bit) {
|
||||
if (ack > bit && received_sequences_.find(ack - bit - 1) != received_sequences_.end()) {
|
||||
ack_mask |= 1u << bit;
|
||||
}
|
||||
}
|
||||
return {ack, ack_mask};
|
||||
}
|
||||
|
||||
private:
|
||||
std::set<uint32_t> received_sequences_;
|
||||
std::optional<uint32_t> highest_received_sequence_;
|
||||
};
|
||||
|
||||
} // namespace muninn_rudp_ack
|
||||
|
|
@ -1,127 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace muninn_rudp_fragments {
|
||||
|
||||
constexpr uint32_t CompletedFragmentIdQuarantineMs = 2000;
|
||||
|
||||
struct FragmentResult {
|
||||
uint32_t first_sequence = 0;
|
||||
std::vector<uint8_t> payload;
|
||||
};
|
||||
|
||||
class FragmentAssembler {
|
||||
public:
|
||||
void reset()
|
||||
{
|
||||
assemblies_.clear();
|
||||
recently_closed_.clear();
|
||||
expired_count_ = 0;
|
||||
}
|
||||
|
||||
std::optional<FragmentResult> insert(const std::string &channel_id,
|
||||
uint16_t fragment_id,
|
||||
uint16_t fragment_index,
|
||||
uint16_t fragment_count,
|
||||
uint32_t sequence,
|
||||
const uint8_t *payload,
|
||||
uint32_t payload_len,
|
||||
std::chrono::steady_clock::time_point now)
|
||||
{
|
||||
if (fragment_id == 0 || fragment_index >= fragment_count) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
const std::string key = channel_id + ":" + std::to_string(fragment_id);
|
||||
expire_recently_closed(now);
|
||||
const auto existing = assemblies_.find(key);
|
||||
if (existing == assemblies_.end() && recently_closed_.find(key) != recently_closed_.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
auto &assembly = assemblies_[key];
|
||||
if (assembly.payloads.empty()) {
|
||||
assembly.channel_id = channel_id;
|
||||
assembly.fragment_count = fragment_count;
|
||||
assembly.started_at = now;
|
||||
}
|
||||
if (assembly.channel_id != channel_id || assembly.fragment_count != fragment_count) {
|
||||
assemblies_.erase(key);
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
assembly.payloads.emplace(fragment_index, std::vector<uint8_t>(payload, payload + payload_len));
|
||||
assembly.sequences.emplace(fragment_index, sequence);
|
||||
if (assembly.payloads.size() < assembly.fragment_count) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
FragmentResult result;
|
||||
result.first_sequence = UINT32_MAX;
|
||||
for (uint16_t index = 0; index < assembly.fragment_count; ++index) {
|
||||
const auto payload_found = assembly.payloads.find(index);
|
||||
const auto sequence_found = assembly.sequences.find(index);
|
||||
if (payload_found == assembly.payloads.end() || sequence_found == assembly.sequences.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
result.payload.insert(result.payload.end(), payload_found->second.begin(), payload_found->second.end());
|
||||
result.first_sequence = std::min(result.first_sequence, sequence_found->second);
|
||||
}
|
||||
assemblies_.erase(key);
|
||||
recently_closed_[key] = now;
|
||||
return result;
|
||||
}
|
||||
|
||||
uint64_t expire(std::chrono::steady_clock::time_point now, uint32_t expire_after_ms)
|
||||
{
|
||||
expire_recently_closed(now);
|
||||
uint64_t expired = 0;
|
||||
for (auto iterator = assemblies_.begin(); iterator != assemblies_.end();) {
|
||||
if (now - iterator->second.started_at < std::chrono::milliseconds(expire_after_ms)) {
|
||||
++iterator;
|
||||
continue;
|
||||
}
|
||||
recently_closed_[iterator->first] = now;
|
||||
iterator = assemblies_.erase(iterator);
|
||||
++expired;
|
||||
}
|
||||
expired_count_ += expired;
|
||||
return expired;
|
||||
}
|
||||
|
||||
uint64_t expired_count() const { return expired_count_; }
|
||||
|
||||
private:
|
||||
void expire_recently_closed(std::chrono::steady_clock::time_point now)
|
||||
{
|
||||
for (auto iterator = recently_closed_.begin(); iterator != recently_closed_.end();) {
|
||||
if (now - iterator->second < std::chrono::milliseconds(CompletedFragmentIdQuarantineMs)) {
|
||||
++iterator;
|
||||
continue;
|
||||
}
|
||||
iterator = recently_closed_.erase(iterator);
|
||||
}
|
||||
}
|
||||
|
||||
struct Assembly {
|
||||
std::string channel_id;
|
||||
uint16_t fragment_count = 0;
|
||||
std::map<uint16_t, std::vector<uint8_t>> payloads;
|
||||
std::map<uint16_t, uint32_t> sequences;
|
||||
std::chrono::steady_clock::time_point started_at;
|
||||
};
|
||||
|
||||
std::map<std::string, Assembly> assemblies_;
|
||||
std::map<std::string, std::chrono::steady_clock::time_point> recently_closed_;
|
||||
uint64_t expired_count_ = 0;
|
||||
};
|
||||
|
||||
} // namespace muninn_rudp_fragments
|
||||
|
|
@ -1,126 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
namespace muninn_rudp_url {
|
||||
|
||||
inline constexpr uint32_t DefaultMediaRudpConnectionId = 0x6d750001;
|
||||
inline constexpr uint32_t DefaultAudioRudpConnectionId = 0x6d750004;
|
||||
inline constexpr uint32_t DefaultReliableExpireAfterMs = 75;
|
||||
inline constexpr uint32_t DefaultVideoAssemblyDeadlineMs = 75;
|
||||
inline constexpr uint32_t DefaultGapWaitMs = 8;
|
||||
inline constexpr uint32_t DefaultAudioReorderMs = 120;
|
||||
inline constexpr uint32_t DefaultSenderResendDelayMs = 5;
|
||||
inline constexpr uint32_t MaxLatencyBudgetMs = 500;
|
||||
|
||||
struct RudpUrlParts {
|
||||
std::string channel_id = "media";
|
||||
uint16_t port = 0;
|
||||
uint16_t video_local_port = 0;
|
||||
uint16_t audio_local_port = 0;
|
||||
uint32_t connection_id = DefaultMediaRudpConnectionId;
|
||||
uint32_t audio_connection_id = DefaultAudioRudpConnectionId;
|
||||
uint32_t sender_resend_delay_ms = DefaultSenderResendDelayMs;
|
||||
uint32_t reliable_expire_after_ms = DefaultReliableExpireAfterMs;
|
||||
uint32_t video_assembly_deadline_ms = DefaultVideoAssemblyDeadlineMs;
|
||||
uint32_t gap_wait_ms = DefaultGapWaitMs;
|
||||
uint32_t audio_reorder_ms = DefaultAudioReorderMs;
|
||||
};
|
||||
|
||||
inline bool starts_with(const std::string &value, const std::string &prefix)
|
||||
{
|
||||
return value.rfind(prefix, 0) == 0;
|
||||
}
|
||||
|
||||
inline std::optional<uint32_t> parse_u32_hex_or_decimal(const std::string &value)
|
||||
{
|
||||
try {
|
||||
size_t consumed = 0;
|
||||
const int base = starts_with(value, "0x") || starts_with(value, "0X") ? 16 : 10;
|
||||
const auto parsed = std::stoul(value, &consumed, base);
|
||||
if (consumed == value.size()) {
|
||||
return static_cast<uint32_t>(parsed);
|
||||
}
|
||||
} catch (...) {
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
inline std::optional<RudpUrlParts> parse(const std::string &url)
|
||||
{
|
||||
if (!starts_with(url, "rudp://")) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
const auto authority_start = std::string("rudp://").size();
|
||||
const auto path_start = url.find('/', authority_start);
|
||||
const auto query_start = url.find('?', authority_start);
|
||||
const auto authority_end = std::min(path_start == std::string::npos ? url.size() : path_start,
|
||||
query_start == std::string::npos ? url.size() : query_start);
|
||||
const auto authority = url.substr(authority_start, authority_end - authority_start);
|
||||
const auto colon = authority.rfind(':');
|
||||
if (colon == std::string::npos || colon + 1 >= authority.size()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
RudpUrlParts parts;
|
||||
parts.port = static_cast<uint16_t>(std::stoul(authority.substr(colon + 1)));
|
||||
parts.video_local_port = static_cast<uint16_t>(parts.port + 10000);
|
||||
parts.audio_local_port = static_cast<uint16_t>(parts.port + 10001);
|
||||
|
||||
if (query_start != std::string::npos) {
|
||||
std::stringstream query(url.substr(query_start + 1));
|
||||
std::string item;
|
||||
while (std::getline(query, item, '&')) {
|
||||
const auto equals = item.find('=');
|
||||
if (equals == std::string::npos) {
|
||||
continue;
|
||||
}
|
||||
const auto key = item.substr(0, equals);
|
||||
const auto value = item.substr(equals + 1);
|
||||
if (key == "channel" && !value.empty()) {
|
||||
parts.channel_id = value;
|
||||
} else if (key == "connection") {
|
||||
if (const auto parsed = parse_u32_hex_or_decimal(value)) {
|
||||
parts.connection_id = *parsed;
|
||||
}
|
||||
} else if (key == "audio_connection" || key == "audio_connection_id") {
|
||||
if (const auto parsed = parse_u32_hex_or_decimal(value)) {
|
||||
parts.audio_connection_id = *parsed;
|
||||
}
|
||||
} else if (key == "local_port" || key == "video_local_port") {
|
||||
parts.video_local_port = static_cast<uint16_t>(std::stoul(value));
|
||||
} else if (key == "audio_local_port") {
|
||||
parts.audio_local_port = static_cast<uint16_t>(std::stoul(value));
|
||||
} else if (key == "reliable_expire_after_ms") {
|
||||
if (const auto parsed = parse_u32_hex_or_decimal(value)) {
|
||||
parts.reliable_expire_after_ms = std::clamp(*parsed, 1u, MaxLatencyBudgetMs);
|
||||
}
|
||||
} else if (key == "sender_resend_delay_ms") {
|
||||
if (const auto parsed = parse_u32_hex_or_decimal(value)) {
|
||||
parts.sender_resend_delay_ms = std::clamp(*parsed, 1u, 1000u);
|
||||
}
|
||||
} else if (key == "assembly_deadline_ms") {
|
||||
if (const auto parsed = parse_u32_hex_or_decimal(value)) {
|
||||
parts.video_assembly_deadline_ms = std::clamp(*parsed, 1u, MaxLatencyBudgetMs);
|
||||
}
|
||||
} else if (key == "gap_wait_ms") {
|
||||
if (const auto parsed = parse_u32_hex_or_decimal(value)) {
|
||||
parts.gap_wait_ms = std::clamp(*parsed, 0u, 1000u);
|
||||
}
|
||||
} else if (key == "audio_reorder_ms") {
|
||||
if (const auto parsed = parse_u32_hex_or_decimal(value)) {
|
||||
parts.audio_reorder_ms = std::clamp(*parsed, 0u, MaxLatencyBudgetMs);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return parts;
|
||||
}
|
||||
|
||||
} // namespace muninn_rudp_url
|
||||
|
|
@ -1,32 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
|
||||
namespace muninn_udp_socket {
|
||||
|
||||
inline SOCKET create(bool exclusive_listener = false)
|
||||
{
|
||||
const SOCKET socket = WSASocketW(
|
||||
AF_INET,
|
||||
SOCK_DGRAM,
|
||||
IPPROTO_UDP,
|
||||
nullptr,
|
||||
0,
|
||||
WSA_FLAG_NO_HANDLE_INHERIT);
|
||||
if (socket == INVALID_SOCKET) {
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
if (exclusive_listener) {
|
||||
const BOOL exclusive = TRUE;
|
||||
if (setsockopt(socket, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
|
||||
reinterpret_cast<const char *>(&exclusive), sizeof(exclusive)) != 0) {
|
||||
closesocket(socket);
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
}
|
||||
return socket;
|
||||
}
|
||||
|
||||
} // namespace muninn_udp_socket
|
||||
#endif
|
||||
|
|
@ -1,181 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace muninn_video_fec {
|
||||
|
||||
inline uint8_t raw_multiply(uint8_t a, uint8_t b)
|
||||
{
|
||||
uint8_t product = 0;
|
||||
while (b != 0) {
|
||||
if ((b & 1) != 0)
|
||||
product ^= a;
|
||||
const bool carry = (a & 0x80) != 0;
|
||||
a <<= 1;
|
||||
if (carry)
|
||||
a ^= 0x1d;
|
||||
b >>= 1;
|
||||
}
|
||||
return product;
|
||||
}
|
||||
|
||||
struct Tables {
|
||||
uint8_t products[256][256] = {};
|
||||
uint8_t inverses[256] = {};
|
||||
|
||||
Tables()
|
||||
{
|
||||
for (size_t a = 0; a < 256; ++a) {
|
||||
for (size_t b = 0; b < 256; ++b)
|
||||
products[a][b] = raw_multiply(static_cast<uint8_t>(a), static_cast<uint8_t>(b));
|
||||
}
|
||||
for (size_t value = 1; value < 256; ++value) {
|
||||
uint8_t result = 1;
|
||||
for (size_t index = 0; index < 254; ++index)
|
||||
result = raw_multiply(result, static_cast<uint8_t>(value));
|
||||
inverses[value] = result;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
inline const Tables &tables()
|
||||
{
|
||||
static const Tables value;
|
||||
return value;
|
||||
}
|
||||
|
||||
inline uint8_t multiply(uint8_t a, uint8_t b)
|
||||
{
|
||||
return tables().products[a][b];
|
||||
}
|
||||
|
||||
inline uint8_t inverse(uint8_t value)
|
||||
{
|
||||
return tables().inverses[value];
|
||||
}
|
||||
|
||||
inline uint8_t coefficient(uint16_t parity_index, uint16_t parity_count, uint16_t data_index)
|
||||
{
|
||||
return inverse(
|
||||
static_cast<uint8_t>(parity_index) ^ static_cast<uint8_t>(parity_count + data_index));
|
||||
}
|
||||
|
||||
inline bool recover(std::vector<std::vector<uint8_t>> &chunks,
|
||||
const std::vector<uint32_t> &chunk_lengths,
|
||||
uint16_t parity_count,
|
||||
const std::map<uint16_t, std::vector<uint8_t>> &parity)
|
||||
{
|
||||
if (chunks.size() != chunk_lengths.size() || chunks.empty() ||
|
||||
chunks.size() + parity_count > 256)
|
||||
return false;
|
||||
std::vector<uint16_t> missing;
|
||||
for (uint16_t index = 0; index < chunks.size(); ++index) {
|
||||
if (chunks[index].empty())
|
||||
missing.push_back(index);
|
||||
}
|
||||
if (missing.empty())
|
||||
return true;
|
||||
if (missing.size() > parity.size())
|
||||
return false;
|
||||
|
||||
std::vector<std::pair<uint16_t, const std::vector<uint8_t> *>> rows;
|
||||
for (const auto &shard : parity) {
|
||||
if (shard.first < parity_count && !shard.second.empty())
|
||||
rows.emplace_back(shard.first, &shard.second);
|
||||
if (rows.size() == missing.size())
|
||||
break;
|
||||
}
|
||||
if (rows.size() != missing.size())
|
||||
return false;
|
||||
|
||||
size_t shard_bytes = 0;
|
||||
for (uint32_t length : chunk_lengths)
|
||||
shard_bytes = std::max(shard_bytes, static_cast<size_t>(length));
|
||||
if (shard_bytes == 0 || std::any_of(rows.begin(), rows.end(),
|
||||
[shard_bytes](const auto &row) { return row.second->size() < shard_bytes; }))
|
||||
return false;
|
||||
|
||||
std::vector<std::vector<uint8_t>> recovered;
|
||||
recovered.reserve(missing.size());
|
||||
for (uint16_t index : missing) {
|
||||
if (chunk_lengths[index] == 0 || chunk_lengths[index] > shard_bytes)
|
||||
return false;
|
||||
recovered.emplace_back(chunk_lengths[index], 0);
|
||||
}
|
||||
|
||||
const size_t count = missing.size();
|
||||
std::vector<std::vector<uint8_t>> inverse_matrix(count, std::vector<uint8_t>(count * 2, 0));
|
||||
for (size_t row = 0; row < count; ++row) {
|
||||
for (size_t column = 0; column < count; ++column)
|
||||
inverse_matrix[row][column] = coefficient(rows[row].first, parity_count, missing[column]);
|
||||
inverse_matrix[row][count + row] = 1;
|
||||
}
|
||||
for (size_t column = 0; column < count; ++column) {
|
||||
size_t pivot = column;
|
||||
while (pivot < count && inverse_matrix[pivot][column] == 0)
|
||||
++pivot;
|
||||
if (pivot == count)
|
||||
return false;
|
||||
std::swap(inverse_matrix[column], inverse_matrix[pivot]);
|
||||
const uint8_t scale = inverse(inverse_matrix[column][column]);
|
||||
for (size_t cell = 0; cell < count * 2; ++cell)
|
||||
inverse_matrix[column][cell] = multiply(inverse_matrix[column][cell], scale);
|
||||
for (size_t row = 0; row < count; ++row) {
|
||||
if (row == column)
|
||||
continue;
|
||||
const uint8_t factor = inverse_matrix[row][column];
|
||||
for (size_t cell = 0; cell < count * 2; ++cell)
|
||||
inverse_matrix[row][cell] ^= multiply(factor, inverse_matrix[column][cell]);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<uint8_t> rhs(count, 0);
|
||||
for (size_t offset = 0; offset < shard_bytes; ++offset) {
|
||||
for (size_t row = 0; row < count; ++row) {
|
||||
const uint16_t parity_index = rows[row].first;
|
||||
rhs[row] = (*rows[row].second)[offset];
|
||||
for (uint16_t data_index = 0; data_index < chunks.size(); ++data_index) {
|
||||
if (!chunks[data_index].empty() && offset < chunks[data_index].size())
|
||||
rhs[row] ^= multiply(coefficient(parity_index, parity_count, data_index),
|
||||
chunks[data_index][offset]);
|
||||
}
|
||||
}
|
||||
for (size_t index = 0; index < count; ++index) {
|
||||
uint8_t value = 0;
|
||||
for (size_t row = 0; row < count; ++row)
|
||||
value ^= multiply(inverse_matrix[index][count + row], rhs[row]);
|
||||
if (offset < recovered[index].size())
|
||||
recovered[index][offset] = value;
|
||||
}
|
||||
}
|
||||
for (size_t index = 0; index < count; ++index)
|
||||
chunks[missing[index]] = std::move(recovered[index]);
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool recover_block(std::vector<std::vector<uint8_t>> &frame_chunks,
|
||||
uint16_t data_start,
|
||||
const std::vector<uint32_t> &block_lengths,
|
||||
uint16_t parity_count,
|
||||
const std::map<uint16_t, std::vector<uint8_t>> &parity)
|
||||
{
|
||||
if (block_lengths.empty() ||
|
||||
static_cast<size_t>(data_start) + block_lengths.size() > frame_chunks.size())
|
||||
return false;
|
||||
std::vector<std::vector<uint8_t>> block_chunks;
|
||||
block_chunks.reserve(block_lengths.size());
|
||||
for (size_t index = 0; index < block_lengths.size(); ++index)
|
||||
block_chunks.push_back(frame_chunks[data_start + index]);
|
||||
if (!recover(block_chunks, block_lengths, parity_count, parity))
|
||||
return false;
|
||||
for (size_t index = 0; index < block_lengths.size(); ++index)
|
||||
frame_chunks[data_start + index] = std::move(block_chunks[index]);
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace muninn_video_fec
|
||||
|
|
@ -1,79 +0,0 @@
|
|||
#include "muninn_audio_fec.h"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
|
||||
static void require(bool condition, const char *message) { if (!condition) throw std::runtime_error(message); }
|
||||
|
||||
static std::array<muninn_audio_fec::Bytes, 4> fixture()
|
||||
{
|
||||
std::array<muninn_audio_fec::Bytes, 4> data;
|
||||
for (size_t shard = 0; shard < data.size(); ++shard) {
|
||||
data[shard].resize(257);
|
||||
for (size_t i = 0; i < data[shard].size(); ++i) data[shard][i] = uint8_t((shard * 71 + i * 29) & 0xff);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
static muninn_audio_fec::Shards all_shards(const std::array<muninn_audio_fec::Bytes, 4> &data)
|
||||
{
|
||||
auto parity = muninn_audio_fec::encode(data);
|
||||
return {data[0], data[1], data[2], data[3], parity[0], parity[1]};
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
try {
|
||||
const auto data = fixture();
|
||||
for (size_t erased = 0; erased < 6; ++erased) {
|
||||
auto shards = all_shards(data); shards[erased].reset();
|
||||
require(muninn_audio_fec::recover(shards), "one-erasure recovery failed");
|
||||
for (size_t i = 0; i < 4; ++i) require(shards[i] == data[i], "one-erasure data mismatch");
|
||||
}
|
||||
for (size_t first = 0; first < 6; ++first) for (size_t second = first + 1; second < 6; ++second) {
|
||||
auto shards = all_shards(data); shards[first].reset(); shards[second].reset();
|
||||
require(muninn_audio_fec::recover(shards), "two-erasure recovery failed");
|
||||
for (size_t i = 0; i < 4; ++i) require(shards[i] == data[i], "two-erasure data mismatch");
|
||||
}
|
||||
{
|
||||
auto shards = all_shards(data);
|
||||
shards[0].reset(); shards[1].reset(); shards[2].reset();
|
||||
require(!muninn_audio_fec::recover(shards), "three erasures were incorrectly recoverable");
|
||||
}
|
||||
{
|
||||
auto shards = all_shards(data);
|
||||
shards[5]->pop_back();
|
||||
require(!muninn_audio_fec::recover(shards), "unequal shard sizes were accepted");
|
||||
}
|
||||
|
||||
using namespace muninn_audio_fec;
|
||||
BlockReceiver receiver(std::chrono::milliseconds(10), 2);
|
||||
BlockKey key{"stream", "session", 100};
|
||||
auto shards = all_shards(data);
|
||||
auto t0 = BlockReceiver::Clock::time_point{};
|
||||
require(receiver.insert(key, 5, *shards[5], t0).empty(), "early parity emitted data");
|
||||
require(receiver.insert(key, 2, *shards[2], t0).empty(), "partial reorder emitted data");
|
||||
require(receiver.insert(key, 2, *shards[2], t0).empty(), "duplicate emitted data");
|
||||
require(receiver.insert(key, 1, *shards[1], t0).empty(), "partial block emitted data");
|
||||
auto recovered = receiver.insert(key, 4, *shards[4], t0);
|
||||
require(recovered.size() == 2 && recovered[0].packet_id == 100 && recovered[1].packet_id == 103,
|
||||
"reordered block did not recover missing packet ids");
|
||||
require(recovered[0].payload == data[0] && recovered[1].payload == data[3], "receiver recovery mismatch");
|
||||
require(receiver.insert(key, 0, data[0], t0).empty(), "completed-block duplicate was admitted");
|
||||
|
||||
BlockKey expired{"stream", "session", 200};
|
||||
receiver.insert(expired, 0, data[0], t0);
|
||||
receiver.expire(t0 + std::chrono::milliseconds(10));
|
||||
require(receiver.pending_blocks() == 0, "expired block survived deadline");
|
||||
|
||||
BlockReceiver bounded(std::chrono::seconds(1), 2);
|
||||
bounded.insert({"s", "x", 0}, 0, data[0], t0);
|
||||
bounded.insert({"s", "x", 4}, 0, data[0], t0);
|
||||
bounded.insert({"s", "x", 8}, 0, data[0], t0);
|
||||
require(bounded.pending_blocks() == 2, "block bound was not enforced");
|
||||
} catch (const std::exception &error) {
|
||||
std::cerr << error.what() << '\n'; return EXIT_FAILURE;
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
@ -1,54 +0,0 @@
|
|||
#include "muninn_media_wire.h"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
|
||||
namespace {
|
||||
|
||||
void require(bool condition, const char *message)
|
||||
{
|
||||
if (!condition) {
|
||||
std::cerr << message << '\n';
|
||||
std::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
const auto early = muninn_media_wire::receiver_feedback_late_frame_ids(42, false);
|
||||
require(early.empty(), "early repair feedback marked a live frame late");
|
||||
|
||||
const auto expired = muninn_media_wire::receiver_feedback_late_frame_ids(42, true);
|
||||
require(expired.size() == 1 && expired.front() == 42,
|
||||
"expired feedback did not name the late frame");
|
||||
|
||||
const auto early_wire = muninn_media_wire::encode_receiver_feedback_payload(
|
||||
"muninn.raven.av.rudp",
|
||||
"raven:session:video",
|
||||
42,
|
||||
{"42:3"},
|
||||
false,
|
||||
false,
|
||||
"unix-ms:1000");
|
||||
const auto expired_wire = muninn_media_wire::encode_receiver_feedback_payload(
|
||||
"muninn.raven.av.rudp",
|
||||
"raven:session:video",
|
||||
42,
|
||||
{"42:3"},
|
||||
true,
|
||||
true,
|
||||
"unix-ms:1000");
|
||||
require(!early_wire.empty() && !expired_wire.empty(), "feedback wire encoding was empty");
|
||||
require(early_wire != expired_wire, "early and expired feedback encoded identically");
|
||||
if (argc == 2 && std::string(argv[1]) == "--emit-early-hex") {
|
||||
for (const auto byte : early_wire) {
|
||||
std::cout << std::hex << std::setw(2) << std::setfill('0')
|
||||
<< static_cast<unsigned int>(byte);
|
||||
}
|
||||
std::cout << '\n';
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,60 +0,0 @@
|
|||
#include "muninn_rudp_url.h"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
namespace {
|
||||
|
||||
void require(bool condition, const std::string &message)
|
||||
{
|
||||
if (!condition) {
|
||||
std::cerr << message << '\n';
|
||||
std::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
const auto configured = muninn_rudp_url::parse(
|
||||
"rudp://192.168.1.66:5204/muninn.raven.av.rudp?channel=realtime&format=muninn-typed-media&connection=0x6d750001&audio_connection=0x6d750004&sender_resend_delay_ms=5&reliable_expire_after_ms=120&assembly_deadline_ms=120&gap_wait_ms=16&audio_reorder_ms=80");
|
||||
require(configured.has_value(), "configured RUDP URL did not parse");
|
||||
require(configured->port == 5204, "configured RUDP URL port was not parsed");
|
||||
require(configured->channel_id == "realtime", "configured RUDP realtime channel was not parsed");
|
||||
require(configured->video_local_port == 15204, "configured RUDP video local port was not derived");
|
||||
require(configured->audio_local_port == 15205, "configured RUDP audio local port was not derived");
|
||||
require(configured->connection_id == 0x6d750001, "configured RUDP connection id was not parsed");
|
||||
require(configured->audio_connection_id == 0x6d750004, "configured RUDP audio connection id was not parsed");
|
||||
require(configured->reliable_expire_after_ms == 120, "configured reliable expiry did not survive parse");
|
||||
require(configured->video_assembly_deadline_ms == 120, "configured assembly deadline did not survive parse");
|
||||
require(configured->gap_wait_ms == 16, "configured gap wait did not survive parse");
|
||||
require(configured->audio_reorder_ms == 80, "configured audio reorder wait did not survive parse");
|
||||
|
||||
const auto clamped = muninn_rudp_url::parse(
|
||||
"rudp://192.168.1.66:5204/muninn.raven.av.rudp?reliable_expire_after_ms=9000&assembly_deadline_ms=9000");
|
||||
require(clamped.has_value(), "oversized latency RUDP URL did not parse");
|
||||
require(clamped->reliable_expire_after_ms == muninn_rudp_url::MaxLatencyBudgetMs,
|
||||
"oversized reliable expiry did not clamp to the max latency budget");
|
||||
require(clamped->video_assembly_deadline_ms == muninn_rudp_url::MaxLatencyBudgetMs,
|
||||
"oversized assembly deadline did not clamp to the max latency budget");
|
||||
|
||||
const auto fallback = muninn_rudp_url::parse("rudp://192.168.1.66:5204/muninn.raven.av.rudp");
|
||||
require(fallback.has_value(), "minimal RUDP URL did not parse");
|
||||
require(fallback->channel_id == "media", "minimal RUDP URL did not use bounded reliable media delivery");
|
||||
require(fallback->connection_id == muninn_rudp_url::DefaultMediaRudpConnectionId,
|
||||
"minimal RUDP URL did not keep the default connection id");
|
||||
require(fallback->audio_connection_id == muninn_rudp_url::DefaultAudioRudpConnectionId,
|
||||
"minimal RUDP URL did not keep the default audio connection id");
|
||||
require(fallback->reliable_expire_after_ms == muninn_rudp_url::DefaultReliableExpireAfterMs,
|
||||
"minimal RUDP URL did not keep the default reliable expiry");
|
||||
require(fallback->video_assembly_deadline_ms == muninn_rudp_url::DefaultVideoAssemblyDeadlineMs,
|
||||
"minimal RUDP URL did not keep the default assembly deadline");
|
||||
require(fallback->audio_reorder_ms == muninn_rudp_url::DefaultAudioReorderMs,
|
||||
"minimal RUDP URL did not keep the default audio reorder wait");
|
||||
require(fallback->audio_reorder_ms == 120,
|
||||
"minimal RUDP URL did not use the bounded realtime audio reorder budget");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,34 +0,0 @@
|
|||
#include "muninn_udp_socket.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <windows.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
WSADATA data{};
|
||||
assert(WSAStartup(MAKEWORD(2, 2), &data) == 0);
|
||||
|
||||
const SOCKET owner = muninn_udp_socket::create(true);
|
||||
assert(owner != INVALID_SOCKET);
|
||||
DWORD flags = HANDLE_FLAG_INHERIT;
|
||||
assert(GetHandleInformation(reinterpret_cast<HANDLE>(owner), &flags));
|
||||
assert((flags & HANDLE_FLAG_INHERIT) == 0);
|
||||
|
||||
sockaddr_in address{};
|
||||
address.sin_family = AF_INET;
|
||||
address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
||||
address.sin_port = 0;
|
||||
assert(bind(owner, reinterpret_cast<sockaddr *>(&address), sizeof(address)) == 0);
|
||||
|
||||
int address_size = sizeof(address);
|
||||
assert(getsockname(owner, reinterpret_cast<sockaddr *>(&address), &address_size) == 0);
|
||||
const SOCKET contender = muninn_udp_socket::create(true);
|
||||
assert(contender != INVALID_SOCKET);
|
||||
assert(bind(contender, reinterpret_cast<sockaddr *>(&address), sizeof(address)) != 0);
|
||||
assert(WSAGetLastError() == WSAEADDRINUSE || WSAGetLastError() == WSAEACCES);
|
||||
|
||||
closesocket(contender);
|
||||
closesocket(owner);
|
||||
WSACleanup();
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,111 +0,0 @@
|
|||
#include "muninn_video_fec.h"
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdlib>
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
void require(bool condition)
|
||||
{
|
||||
if (!condition)
|
||||
std::abort();
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
constexpr uint16_t data_count = 64;
|
||||
constexpr uint16_t parity_count = 16;
|
||||
constexpr size_t shard_bytes = 1400;
|
||||
std::vector<std::vector<uint8_t>> original(data_count, std::vector<uint8_t>(shard_bytes));
|
||||
for (uint16_t data = 0; data < data_count; ++data) {
|
||||
for (size_t offset = 0; offset < shard_bytes; ++offset)
|
||||
original[data][offset] = static_cast<uint8_t>((data * 29 + offset * 17) & 0xff);
|
||||
}
|
||||
std::map<uint16_t, std::vector<uint8_t>> parity;
|
||||
for (uint16_t row = 0; row < parity_count; ++row) {
|
||||
auto &shard = parity[row];
|
||||
shard.assign(shard_bytes, 0);
|
||||
for (uint16_t data = 0; data < data_count; ++data) {
|
||||
const uint8_t coefficient = muninn_video_fec::coefficient(row, parity_count, data);
|
||||
for (size_t offset = 0; offset < shard_bytes; ++offset)
|
||||
shard[offset] ^= muninn_video_fec::multiply(original[data][offset], coefficient);
|
||||
}
|
||||
}
|
||||
const std::vector<uint32_t> lengths(data_count, shard_bytes);
|
||||
for (uint16_t burst = 1; burst <= parity_count; ++burst) {
|
||||
auto damaged = original;
|
||||
for (uint16_t index = 0; index < burst; ++index)
|
||||
damaged[20 + index].clear();
|
||||
const auto started = std::chrono::steady_clock::now();
|
||||
require(muninn_video_fec::recover(damaged, lengths, parity_count, parity));
|
||||
require(std::chrono::steady_clock::now() - started < std::chrono::milliseconds(50));
|
||||
require(damaged == original);
|
||||
}
|
||||
auto excessive = original;
|
||||
for (uint16_t index = 0; index <= parity_count; ++index)
|
||||
excessive[index].clear();
|
||||
require(!muninn_video_fec::recover(excessive, lengths, parity_count, parity));
|
||||
|
||||
constexpr uint16_t block_data = 8;
|
||||
constexpr uint16_t frame_data = 80;
|
||||
constexpr uint16_t block_count = frame_data / block_data;
|
||||
constexpr size_t block_shard_bytes = 96;
|
||||
std::vector<std::vector<uint8_t>> frame(frame_data, std::vector<uint8_t>(block_shard_bytes));
|
||||
for (uint16_t data = 0; data < frame_data; ++data) {
|
||||
for (size_t offset = 0; offset < block_shard_bytes; ++offset)
|
||||
frame[data][offset] = static_cast<uint8_t>((data * 31 + offset * 13) & 0xff);
|
||||
}
|
||||
std::vector<std::map<uint16_t, std::vector<uint8_t>>> block_parity(block_count);
|
||||
for (uint16_t block = 0; block < block_count; ++block) {
|
||||
for (uint16_t row = 0; row < block_data; ++row) {
|
||||
auto &shard = block_parity[block][row];
|
||||
shard.assign(block_shard_bytes, 0);
|
||||
for (uint16_t local = 0; local < block_data; ++local) {
|
||||
const auto &data = frame[block * block_data + local];
|
||||
const uint8_t coefficient = muninn_video_fec::coefficient(row, block_data, local);
|
||||
for (size_t offset = 0; offset < block_shard_bytes; ++offset)
|
||||
shard[offset] ^= muninn_video_fec::multiply(data[offset], coefficient);
|
||||
}
|
||||
}
|
||||
}
|
||||
struct WireShard {
|
||||
bool parity;
|
||||
uint16_t block;
|
||||
uint16_t local;
|
||||
};
|
||||
std::vector<WireShard> wire;
|
||||
for (uint16_t local = 0; local < block_data; ++local) {
|
||||
for (uint16_t block = 0; block < block_count; ++block)
|
||||
wire.push_back({false, block, local});
|
||||
for (uint16_t block = 0; block < block_count; ++block)
|
||||
wire.push_back({true, block, local});
|
||||
}
|
||||
require(wire.size() == frame_data * 2);
|
||||
const std::vector<uint32_t> block_lengths(block_data, block_shard_bytes);
|
||||
for (size_t burst = 1; burst <= 54; ++burst) {
|
||||
for (size_t start = 0; start + burst <= wire.size(); ++start) {
|
||||
auto damaged = frame;
|
||||
auto available_parity = block_parity;
|
||||
for (size_t index = start; index < start + burst; ++index) {
|
||||
const auto shard = wire[index];
|
||||
if (shard.parity)
|
||||
available_parity[shard.block].erase(shard.local);
|
||||
else
|
||||
damaged[shard.block * block_data + shard.local].clear();
|
||||
}
|
||||
const auto started = std::chrono::steady_clock::now();
|
||||
for (uint16_t block = 0; block < block_count; ++block) {
|
||||
require(muninn_video_fec::recover_block(
|
||||
damaged,
|
||||
block * block_data,
|
||||
block_lengths,
|
||||
block_data,
|
||||
available_parity[block]));
|
||||
}
|
||||
require(std::chrono::steady_clock::now() - started < std::chrono::milliseconds(50));
|
||||
require(damaged == frame);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in a new issue