Skip to content
83 changes: 56 additions & 27 deletions cpp/obs/test/moq-source-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
#include <algorithm>
#include <atomic>
#include <chrono>
#include <cstdarg>
#include <cmath>
#include <condition_variable>
#include <cstdio>
#include <cstdlib>
#include <cstring>
Expand Down Expand Up @@ -111,26 +111,40 @@ uint32_t g_obs_sample_rate = 48000;
// as a leak.
std::atomic<long> g_live_allocs{0};

// Candidate resolve and subscribe failures, logged under the source lock as the candidate
// is released, so taking that lock afterwards observes the release.
// Watch one attempt, not whichever replacement a later catalog snapshot may start.
// The logger runs under the source lock, before the failed attempt is released.
struct CandidateFailure {
std::weak_ptr<Track> track;
bool failed = false;
};
std::mutex g_candidate_mutex;
std::condition_variable g_candidate_failed;
int g_candidate_failures = 0;
CandidateFailure g_candidate;
} // namespace

extern "C" {

// Deliberately silent: stdio locking would create happens-before edges between
// the test thread and the source's worker and mask the races we're after. Only a
// candidate's resolve or subscribe failure synchronizes, so a scenario can wait on it.
// the test thread and the source's worker and mask the races we're after. Match
// the watched role while its attempt is alive; playback identity checks below
// distinguish an active-track error from the candidate's failure.
void blog(int, const char *format, ...)
{
if (!strstr(format, "Failed to resolve") && !strstr(format, "Failed to subscribe") &&
!strstr(format, "Audio track error"))
const char *kind = nullptr;
if (strstr(format, "Audio track error") || strstr(format, "Failed to subscribe to Audio track")) {
kind = "Audio";
} else if (strstr(format, "Failed to subscribe to %s track")) {
va_list args;
va_start(args, format);
kind = va_arg(args, const char *);
va_end(args);
}
if (!kind)
return;
std::lock_guard<std::mutex> lock(g_candidate_mutex);
g_candidate_failures++;
g_candidate_failed.notify_all();
auto candidate = g_candidate.track.lock();
if (!candidate || strcmp(candidate->kind, kind) != 0)
return;
g_candidate.failed = true;
}

void *bmalloc(size_t size)
Expand Down Expand Up @@ -1065,10 +1079,11 @@ int main()
Publisher publisher(relay, "obs/test");
void *source = createSource();
CHECK(publisher.WriteUntil([] { return g_output_audio > 0; }));
int failures = 0;
auto *ctx = static_cast<moq_source *>(source);
std::weak_ptr<Track> active;
{
std::lock_guard<std::mutex> lock(g_candidate_mutex);
failures = g_candidate_failures;
std::lock_guard<std::mutex> lock(ctx->mutex);
active = ctx->connection->audio;
}
moq::AudioInit init{};
init.format = moq::AudioFormat::kOpus;
Expand All @@ -1077,16 +1092,27 @@ int main()
moq::MediaTrackProducer::audio(publisher.broadcast,
moq::MediaTarget::kNamed{std::string("!replacement")}, init),
"replacement audio");
std::weak_ptr<Track> candidate;
CHECK(publisher.WriteUntil([&] {
std::lock_guard<std::mutex> lock(ctx->mutex);
const auto &pending = ctx->connection->pending_audio;
if (!pending || !pending->audio_consumer)
return false;
candidate = pending;
std::lock_guard<std::mutex> failure_lock(g_candidate_mutex);
g_candidate = {candidate, false};
return true;
}));
CHECK(publisher.WriteUntil([&] {
TestOk(replacement->write_frame(moq::Frame{BAD_OPUS, 1'000'000'000}), "replacement frame");
TestOk(replacement->cut(), "replacement audio cut");
std::lock_guard<std::mutex> lock(g_candidate_mutex);
return g_candidate_failures > failures;
return g_candidate.failed && candidate.expired();
}));
auto *ctx = static_cast<moq_source *>(source);
{
std::lock_guard<std::mutex> lock(ctx->mutex);
CHECK(ctx->connection->audio && !ctx->connection->pending_audio);
auto playing = active.lock();
CHECK(playing && ctx->connection->audio == playing);
}
const int audio = g_output_audio;
CHECK(publisher.WriteUntil([&] { return g_output_audio >= audio + 3; }));
Expand Down Expand Up @@ -1187,15 +1213,16 @@ int main()
snapshot->video.clear();
snapshot->video.emplace("!unavailable", config);
auto *ctx = static_cast<moq_source *>(source);
int failures = 0;
{
std::lock_guard<std::mutex> lock(g_candidate_mutex);
failures = g_candidate_failures;
}
std::weak_ptr<Track> candidate;
std::weak_ptr<Track> active;
{
std::lock_guard<std::mutex> lock(ctx->mutex);
active = ctx->connection->video;
moq_source_subscribe_video(ctx, ctx->connection, *snapshot);
CHECK(ctx->connection->pending_video != nullptr);
candidate = ctx->connection->pending_video;
std::lock_guard<std::mutex> failure_lock(g_candidate_mutex);
g_candidate = {candidate, false};
}
if (hidden) {
// An unannounced sibling stays pending, without interrupting current playback.
Expand All @@ -1206,13 +1233,15 @@ int main()
route.epoch = "01900000-0000-7000-8000-000000000002";
TestOk(hidden->announce(route), "announce mismatched sibling");
}
{
std::unique_lock<std::mutex> lock(g_candidate_mutex);
g_candidate_failed.wait(lock, [&] { return g_candidate_failures > failures; });
}
CHECK(WaitFor([] {
std::lock_guard<std::mutex> lock(g_candidate_mutex);
return g_candidate.failed;
}));
{
std::lock_guard<std::mutex> lock(ctx->mutex);
CHECK(ctx->connection && !ctx->connection->pending_video);
auto playing = active.lock();
CHECK(ctx->connection && playing && ctx->connection->video == playing);
CHECK(candidate.expired());
}
const int frames = g_output_frames;
CHECK(publisher.WriteUntil([&] { return g_output_frames >= frames + 3; }));
Expand Down
1 change: 0 additions & 1 deletion quest/m0/nightly/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,5 @@ Public API: none. Wire: none.
## Required

- [moq-net compiles in Fuzz and send-depth](/quest/m0/nightly/send-overflow.md) - the ietf publisher future stops overflowing the Send check
- [OBS audio-candidate test](/quest/m0/nightly/obs-audio-candidate.md) - the failed audio candidate check passes under TSan and load, fixed at its reproduced cause
- [qmux builds clean for iOS](/quest/m0/nightly/qmux-ios-dead-code.md) - `TcpStats` is gated to the platforms that implement it, so the iOS simulator build has no dead code
- [Probe against Cubic](/quest/m0/nightly/competing-cubic.md) - the `competing_cubic` probe test stops resending a stream after its reset
27 changes: 0 additions & 27 deletions quest/m0/nightly/obs-audio-candidate.md

This file was deleted.

Loading