Make Muninn OBS reconnect receiver-owned
This commit is contained in:
parent
da543c420d
commit
0c9e54656b
4 changed files with 153 additions and 12 deletions
|
|
@ -47,7 +47,10 @@ constexpr const char *MuninnObsCatalogType = "muninn.obs_stream_catalog";
|
|||
constexpr const char *MuninnObsCatalogSchema = "muninn.obs_stream_catalog.v1";
|
||||
constexpr const char *MuninnObsCatalogKey = "obs";
|
||||
constexpr uint64_t MuninnCatalogRefreshMs = 2000;
|
||||
constexpr uint64_t MuninnActivationRetryMs = 120000;
|
||||
constexpr uint64_t MuninnActivationRetryMs = 5000;
|
||||
constexpr uint64_t MuninnMediaStartupGraceMs = 3000;
|
||||
constexpr uint64_t MuninnMediaStaleMs = 1500;
|
||||
constexpr uint64_t MuninnMediaRestartCooldownMs = 5000;
|
||||
constexpr uint16_t MuninnCatalogDiscoveryPort = 17874;
|
||||
constexpr uint32_t MuninnCatalogDiscoveryConnectionId = 0x6d750003;
|
||||
|
||||
|
|
@ -89,6 +92,7 @@ struct MuninnSource {
|
|||
std::unique_ptr<class CatalogDiscoveryReceiver> catalog_discovery;
|
||||
std::chrono::steady_clock::time_point next_catalog_refresh = std::chrono::steady_clock::now();
|
||||
std::chrono::steady_clock::time_point last_activation_request = std::chrono::steady_clock::time_point::min();
|
||||
std::chrono::steady_clock::time_point last_media_restart = std::chrono::steady_clock::time_point::min();
|
||||
};
|
||||
|
||||
static void muninn_child_audio(void *param, obs_source_t *source, const struct audio_data *audio_data, bool muted);
|
||||
|
|
@ -125,6 +129,14 @@ static bool starts_with(const std::string &value, const std::string &prefix)
|
|||
return value.rfind(prefix, 0) == 0;
|
||||
}
|
||||
|
||||
static uint64_t steady_millis()
|
||||
{
|
||||
return static_cast<uint64_t>(
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
std::chrono::steady_clock::now().time_since_epoch())
|
||||
.count());
|
||||
}
|
||||
|
||||
static uint32_t read_be32(const uint8_t *data)
|
||||
{
|
||||
return (static_cast<uint32_t>(data[0]) << 24) | (static_cast<uint32_t>(data[1]) << 16) |
|
||||
|
|
@ -473,9 +485,16 @@ public:
|
|||
source_url_ = url;
|
||||
parts_ = *parsed;
|
||||
outputs_.video_url = "udp://127.0.0.1:" + std::to_string(parts_.video_local_port) +
|
||||
"?fifo_size=32768&overrun_nonfatal=1&buffer_size=262144";
|
||||
"?fifo_size=1048576&overrun_nonfatal=1&buffer_size=16777216";
|
||||
outputs_.audio_url = "udp://127.0.0.1:" + std::to_string(parts_.audio_local_port) +
|
||||
"?fifo_size=32768&overrun_nonfatal=1&buffer_size=262144";
|
||||
"?fifo_size=262144&overrun_nonfatal=1&buffer_size=4194304";
|
||||
const uint64_t now = steady_millis();
|
||||
started_ms_.store(now);
|
||||
last_packet_ms_.store(0);
|
||||
last_video_forwarded_ms_.store(0);
|
||||
last_audio_forwarded_ms_.store(0);
|
||||
stale_reported_.store(false);
|
||||
media_connected_ = false;
|
||||
running_ = true;
|
||||
worker_ = std::thread([this] { run(); });
|
||||
blog(LOG_INFO,
|
||||
|
|
@ -500,6 +519,49 @@ public:
|
|||
}
|
||||
source_url_.clear();
|
||||
outputs_ = {};
|
||||
started_ms_.store(0);
|
||||
last_packet_ms_.store(0);
|
||||
last_video_forwarded_ms_.store(0);
|
||||
last_audio_forwarded_ms_.store(0);
|
||||
stale_reported_.store(false);
|
||||
media_connected_ = false;
|
||||
}
|
||||
|
||||
bool has_forwarded_media() const
|
||||
{
|
||||
return last_video_forwarded_ms_.load() != 0 || last_audio_forwarded_ms_.load() != 0;
|
||||
}
|
||||
|
||||
bool needs_activation(uint64_t now_ms) const
|
||||
{
|
||||
const uint64_t started = started_ms_.load();
|
||||
if (started == 0) {
|
||||
return true;
|
||||
}
|
||||
if (now_ms >= started && now_ms - started < MuninnMediaStartupGraceMs) {
|
||||
return false;
|
||||
}
|
||||
if (!running_.load()) {
|
||||
return true;
|
||||
}
|
||||
const uint64_t last_packet = last_packet_ms_.load();
|
||||
if (last_packet == 0) {
|
||||
return true;
|
||||
}
|
||||
const uint64_t last_video = last_video_forwarded_ms_.load();
|
||||
const uint64_t last_audio = last_audio_forwarded_ms_.load();
|
||||
const uint64_t last_media = std::max(last_video, last_audio);
|
||||
return last_media == 0 || (now_ms > last_media && now_ms - last_media >= MuninnMediaStaleMs);
|
||||
}
|
||||
|
||||
bool consume_stale_transition(uint64_t now_ms)
|
||||
{
|
||||
if (!needs_activation(now_ms)) {
|
||||
stale_reported_.store(false);
|
||||
return false;
|
||||
}
|
||||
bool expected = false;
|
||||
return stale_reported_.compare_exchange_strong(expected, true);
|
||||
}
|
||||
|
||||
private:
|
||||
|
|
@ -512,6 +574,7 @@ private:
|
|||
socket_ = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
if (socket_ == INVALID_SOCKET) {
|
||||
blog(LOG_WARNING, "Muninn RUDP bridge could not create socket");
|
||||
running_ = false;
|
||||
WSACleanup();
|
||||
return;
|
||||
}
|
||||
|
|
@ -530,6 +593,7 @@ private:
|
|||
blog(LOG_WARNING, "Muninn RUDP bridge could not bind udp/%u", parts_.port);
|
||||
closesocket(socket_);
|
||||
socket_ = INVALID_SOCKET;
|
||||
running_ = false;
|
||||
WSACleanup();
|
||||
return;
|
||||
}
|
||||
|
|
@ -602,6 +666,7 @@ private:
|
|||
if (version != 0 || connection_id != parts_.connection_id || header_bytes < 36 || size < header_bytes) {
|
||||
return;
|
||||
}
|
||||
last_packet_ms_.store(steady_millis());
|
||||
last_remote_ = remote;
|
||||
const size_t payload_offset = static_cast<size_t>(header_bytes);
|
||||
if (payload_offset + payload_len > size || 36 + channel_len > header_bytes) {
|
||||
|
|
@ -621,6 +686,7 @@ private:
|
|||
waiting_for_video_keyframe_ = true;
|
||||
video_forward_failures_ = 0;
|
||||
audio_forward_failures_ = 0;
|
||||
media_connected_ = true;
|
||||
const auto [ack, ack_mask] = ack_tracker_.state();
|
||||
send_control_packet(2, 0x03, bridge_sequence++, ack, ack_mask, remote);
|
||||
blog(LOG_INFO, "Muninn RUDP bridge accepted media connection sequence %u", sequence);
|
||||
|
|
@ -628,10 +694,18 @@ private:
|
|||
}
|
||||
|
||||
if (packet_type == 3) {
|
||||
if (!media_connected_) {
|
||||
return;
|
||||
}
|
||||
ack_tracker_.remember(sequence);
|
||||
const std::string channel_id(
|
||||
reinterpret_cast<const char *>(packet + 36),
|
||||
reinterpret_cast<const char *>(packet + 36 + channel_len));
|
||||
if (channel_id != "media") {
|
||||
const auto [ack, ack_mask] = ack_tracker_.state();
|
||||
send_control_packet(4, 0x00, bridge_sequence++, ack, ack_mask, remote);
|
||||
return;
|
||||
}
|
||||
std::optional<std::pair<uint32_t, std::vector<uint8_t>>> delivered;
|
||||
if (fragment_count == 0) {
|
||||
delivered = std::make_pair(
|
||||
|
|
@ -728,6 +802,7 @@ private:
|
|||
log_bridge_pressure("failed forwarding video payload to local decoder socket");
|
||||
return;
|
||||
}
|
||||
last_video_forwarded_ms_.store(steady_millis());
|
||||
} else if (media->kind == MuninnMediaPayload::Kind::Audio) {
|
||||
forwarded_bytes = media->payload.size();
|
||||
const int sent = sendto(audio_socket, reinterpret_cast<const char *>(media->payload.data()), static_cast<int>(media->payload.size()), 0,
|
||||
|
|
@ -737,6 +812,7 @@ private:
|
|||
log_bridge_pressure("failed forwarding audio payload to local decoder socket");
|
||||
return;
|
||||
}
|
||||
last_audio_forwarded_ms_.store(steady_millis());
|
||||
} else {
|
||||
++payloads_decoded_dropped_;
|
||||
log_bridge_pressure("dropped unknown media payload");
|
||||
|
|
@ -966,6 +1042,11 @@ private:
|
|||
std::optional<sockaddr_in> last_remote_;
|
||||
#endif
|
||||
std::atomic<bool> running_{false};
|
||||
std::atomic<uint64_t> started_ms_{0};
|
||||
std::atomic<uint64_t> last_packet_ms_{0};
|
||||
std::atomic<uint64_t> last_video_forwarded_ms_{0};
|
||||
std::atomic<uint64_t> last_audio_forwarded_ms_{0};
|
||||
std::atomic<bool> stale_reported_{false};
|
||||
std::thread worker_;
|
||||
muninn_rudp_url::RudpUrlParts parts_;
|
||||
std::map<std::string, MuninnVideoFrameAssembly> video_assemblies_;
|
||||
|
|
@ -979,6 +1060,7 @@ private:
|
|||
uint64_t video_forward_failures_ = 0;
|
||||
uint64_t audio_forward_failures_ = 0;
|
||||
bool waiting_for_video_keyframe_ = true;
|
||||
bool media_connected_ = false;
|
||||
std::string source_url_;
|
||||
RudpBridgeOutputs outputs_;
|
||||
};
|
||||
|
|
@ -1150,14 +1232,15 @@ static std::string endpoint_setting(obs_data_t *settings, const char *key, const
|
|||
return value;
|
||||
}
|
||||
|
||||
static void request_stream_activation(MuninnSource *ctx, const std::string &stream_id)
|
||||
static void request_stream_activation(MuninnSource *ctx, const std::string &stream_id, bool force = false)
|
||||
{
|
||||
const auto canonical_stream_id = canonical_muninn_stream_id(stream_id);
|
||||
if (!ctx || canonical_stream_id.empty() || ctx->command_exe_path.empty() || ctx->command_rudp_target.empty()) {
|
||||
return;
|
||||
}
|
||||
const auto now = std::chrono::steady_clock::now();
|
||||
if (ctx->last_requested_stream_id == canonical_stream_id &&
|
||||
if (!force &&
|
||||
ctx->last_requested_stream_id == canonical_stream_id &&
|
||||
now - ctx->last_activation_request < std::chrono::milliseconds(MuninnActivationRetryMs)) {
|
||||
return;
|
||||
}
|
||||
|
|
@ -1734,7 +1817,7 @@ static obs_source_t *create_ffmpeg_child_source(MuninnSource *ctx,
|
|||
return source;
|
||||
}
|
||||
|
||||
static void create_or_update_media(MuninnSource *ctx, const std::string &url)
|
||||
static void create_or_update_media(MuninnSource *ctx, const std::string &url, bool force_restart = false)
|
||||
{
|
||||
if (url.empty()) {
|
||||
release_media(ctx);
|
||||
|
|
@ -1742,7 +1825,7 @@ static void create_or_update_media(MuninnSource *ctx, const std::string &url)
|
|||
return;
|
||||
}
|
||||
|
||||
if (ctx->media && equivalent_rudp_stream_url(ctx->stream_url, url)) {
|
||||
if (!force_restart && ctx->media && equivalent_rudp_stream_url(ctx->stream_url, url)) {
|
||||
ctx->stream_url = url;
|
||||
return;
|
||||
}
|
||||
|
|
@ -1814,10 +1897,34 @@ static void reconcile_selected_stream(MuninnSource *ctx)
|
|||
ctx->stream_id = selected->stream_id;
|
||||
if (selection_changed) {
|
||||
ctx->last_requested_stream_id.clear();
|
||||
ctx->last_media_restart = std::chrono::steady_clock::time_point::min();
|
||||
}
|
||||
|
||||
request_stream_activation(ctx, selected->stream_id);
|
||||
create_or_update_media(ctx, playable_rudp_stream_url(ctx, *selected));
|
||||
const auto stream_url = playable_rudp_stream_url(ctx, *selected);
|
||||
create_or_update_media(ctx, stream_url);
|
||||
|
||||
bool activation_needed = ctx->last_requested_stream_id != selected_canonical_stream_id;
|
||||
bool force_activation = false;
|
||||
if (ctx->bridge && starts_with(stream_url, "rudp://")) {
|
||||
const uint64_t now_ms = steady_millis();
|
||||
if (ctx->bridge->consume_stale_transition(now_ms)) {
|
||||
blog(LOG_WARNING, "Muninn Stream receiver media is stale; refreshing local media readers and re-requesting activation");
|
||||
const auto now = std::chrono::steady_clock::now();
|
||||
if (now - ctx->last_media_restart >= std::chrono::milliseconds(MuninnMediaRestartCooldownMs)) {
|
||||
create_or_update_media(ctx, stream_url, true);
|
||||
ctx->last_media_restart = now;
|
||||
}
|
||||
ctx->last_requested_stream_id.clear();
|
||||
activation_needed = true;
|
||||
force_activation = true;
|
||||
} else if (ctx->bridge->needs_activation(now_ms)) {
|
||||
activation_needed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (activation_needed) {
|
||||
request_stream_activation(ctx, selected->stream_id, force_activation);
|
||||
}
|
||||
}
|
||||
|
||||
static void populate_stream_list(obs_property_t *property, const std::string &store_path)
|
||||
|
|
|
|||
|
|
@ -11,6 +11,8 @@
|
|||
|
||||
namespace muninn_rudp_fragments {
|
||||
|
||||
constexpr uint32_t CompletedFragmentIdQuarantineMs = 2000;
|
||||
|
||||
struct FragmentResult {
|
||||
uint32_t first_sequence = 0;
|
||||
std::vector<uint8_t> payload;
|
||||
|
|
@ -21,6 +23,7 @@ public:
|
|||
void reset()
|
||||
{
|
||||
assemblies_.clear();
|
||||
recently_closed_.clear();
|
||||
expired_count_ = 0;
|
||||
}
|
||||
|
||||
|
|
@ -38,6 +41,12 @@ public:
|
|||
}
|
||||
|
||||
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;
|
||||
|
|
@ -67,17 +76,20 @@ public:
|
|||
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;
|
||||
}
|
||||
|
|
@ -88,6 +100,17 @@ public:
|
|||
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;
|
||||
|
|
@ -97,6 +120,7 @@ private:
|
|||
};
|
||||
|
||||
std::map<std::string, Assembly> assemblies_;
|
||||
std::map<std::string, std::chrono::steady_clock::time_point> recently_closed_;
|
||||
uint64_t expired_count_ = 0;
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue