Skip to content

libobs: Keep Monitor Only out of the outputs when set by type - #2450

Merged
rdoupe merged 3 commits into
obs-rust:masterfrom
rdoupe:fix/ws-monitor-only
Oct 26, 2026
Merged

rdoupe merged 3 commits into
obs-rust:masterfrom
rdoupe:fix/ws-monitor-only

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 26, 2026

Copy link
Copy Markdown
Collaborator

Fixes #2447

What and why

In 33.0, audio monitoring is an on/off switch, and on means monitor and output. The monitor now plays a source even while it is muted, so "Monitor Only" in 33.0 means monitoring on plus muted. #2445 made pre-33 scene collections load that way.

The deprecated obs_source_set_monitoring_type() still turned every type except none into monitoring on and left the mute alone. That setter is what obs-websocket's SetInputAudioMonitorType calls, and what plugins and scripts call. So a source set to Monitor Only through any of them played into the stream and recordings. obs_source_get_monitoring_type(), GetInputAudioMonitorType and the InputAudioMonitorTypeChanged event all reported Monitor and Output for it.

The fix is in libobs only. plugins/obs-websocket is unchanged: it stays the compatibility check (AGENTS.md).

Call Monitoring Mute
set MONITOR_ONLY on muted. The mute is set first, so no audio reaches the outputs in between.
set MONITOR_AND_OUTPUT on unmuted
set NONE off left as it is, except that it undoes the mute MONITOR_ONLY set, if nothing has set the mute since
set an out-of-range value on, as before left as it is
get NONE when off; MONITOR_ONLY when on and muted; MONITOR_AND_OUTPUT when on and unmuted

The "audio_monitoring" signal now carries the same type as the getter. This is the deprecated compatibility signal, and obs-websocket's event is its only user.

The internal monitoring_type field and the saved "monitoring_type"/"monitoring_enabled" values are unchanged. The two audio paths that test that field for monitor-only stay as they were. The scene collection format, the enum and the exported symbols are unchanged.

Undoing the mute. Monitor Only, then None, unmutes the source. Before 33.0 that sequence put the source back into the stream, and a client that sends it expects the same result. Leaving the source muted would silence it everywhere.

A new internal flag, monitor_only_muted (atomic, in struct obs_source), records that the mute came from Monitor Only. Any obs_source_set_muted() call clears it, so:

  • a mute the user set before Monitor Only stays muted after None;
  • a source the user unmuted after Monitor Only stays unmuted;
  • a source the user unmuted and then muted again by hand stays muted.

The flag is not saved: after a restart, None leaves the mute alone.

Rust: the monitoring setters and getters are not ported. No rust/ code mirrors struct obs_source or these functions. The change is C only, and it is the same C on all three libraries.

Known limit: muting or unmuting a monitored source changes what the getter reports, but sends only the "mute" signal, not "audio_monitoring". obs-websocket clients get InputMuteStateChanged. A Monitor Only to Monitor and Output change through the setter is the same case, because monitoring stays on.

  • libobs/obs-source.c.h: the setter, the getter, the signal type, and the flag cleared in obs_source_set_muted().
  • libobs/obs-internal.h: the flag.
  • docs/sphinx/reference-sources.rst: what each type does since 33.0.
  • docs/features.md: the line under Stability.

RED → GREEN

  • RED commit: 4638cd099, two new tests, both under xvfb. No existing test is changed, and none tested the old set/get behaviour.
    • test_source_monitoring_type (graphics suite, test-graph.json) calls the libobs setter and getter directly. On master, 4 of 5 cases fail. For example, Monitor Only gives "signal carried monitor and output; getter reports monitor and output; muted no; the tone reached the program mix (1024 frames)". The round trip for the other two types passes.
    • test_ws_monitor_only (tests.d, beside the other test_ws_* tests) runs the unchanged obs-websocket SetInputAudioMonitorType/GetInputAudioMonitorType handlers against libobs. On master, 4 of 5 cases fail: Monitor Only is "GetInputAudioMonitorType reports OBS_MONITORING_TYPE_MONITOR_AND_OUTPUT, muted no, the tone reached the program mix (1024 frames)".
    • Both tests play a tone from a source on the program, and check the mixed audio libobs hands to outputs (obs_add_raw_audio_callback) and the monitor feed. The RED runs were on the C lane.
  • GREEN: 45cbcec3a.
    • Both new tests pass 5/5 on the C lane in 3 runs out of 3, and 5/5 on hybrid and on libobs-c-reference. On every lane, Monitor Only shows "monitoring on, muted yes, 0 program frames carry the tone".
    • test_source_monitor_only_load (libobs: Mute 32.x Monitor Only sources on load #2445's test) passes 3/3 on all three lanes.
    • Full ctest (headless: private D-Bus, fresh HOME, no audio device): C 521/521, hybrid 548/548. Two tests that need a display failed at setup in the first pass, then passed under a private Xvfb. Three tests skip because they need a vivid V4L2 device or a PulseAudio daemon.
    • All 13 test_ws_* tests pass.
    • Exported symbols: 1766 each, the same set on C, hybrid and libobs-c-reference.
    • Local gate: --lane lint ok, --test-diff origin/master 4167/4167 (6 skipped), --lane cross ok. ci/submodule_direction.py origin/master ok: no submodule moves.
  • Real app: not run. The websocket path is covered end to end by test_ws_monitor_only, through the real request handlers and libobs.

Checklist

  • Base is master
  • I claimed the issue, or no one else has an open PR for it
  • Characterization test (if the unit had none), then RED, then GREEN commits
  • User-visible change: its line in docs/features.md is added or updated
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite (gate passed locally, head not stamped)
  • Warning or flag changes (-W..., ratchets): not applicable
  • Intentional differences from C listed in the parity test header and rust/README.md (ports): not applicable (not ported)

@rdoupe
rdoupe requested a review from zackees as a code owner October 26, 2026 06:29
@zackees

zackees commented Oct 26, 2026

Copy link
Copy Markdown
Member

Coordinator review: approve in substance. Muting before turning monitoring on, deriving the getter from monitoring plus mute, and tracking the mute with the monitor_only_muted flag (cleared by any set_muted, so a user's own mute survives None) are all right. The websocket plugin is untouched, and a new Tier 2 test covers it through the real plugin.
One non-blocking note for the ledger or docs: setting MONITOR_AND_OUTPUT on a source the user had muted now unmutes it. Before 33.0 those two were independent, so a muted source set to Monitor and Output stayed silent. Here a plugin or websocket client that sets Monitor and Output on a muted mic makes it audible on stream. That follows from the getter mapping 'on + muted' to Monitor Only, so I see no clean alternative. Please say it explicitly in reference-sources.rst so plugin authors know. Queuing when CI is green.

@zackees
zackees enabled auto-merge October 26, 2026 06:32
@rdoupe

rdoupe commented Oct 26, 2026

Copy link
Copy Markdown
Collaborator Author

Done in 3f5e8d8. reference-sources.rst now says that Monitor and Output unmutes the source, that this differs from before 33.0, and that plugins which must not unmute should check obs_source_muted() first.

@rdoupe

rdoupe commented Oct 26, 2026

Copy link
Copy Markdown
Collaborator Author

The Windows Tier 2 failure here comes from master, not this PR. test_source_video_tick_steps.c:237 (from #2448) trips MSVC warning C5287 (comparing two different enum types) under -WX. #2451 fixes it with a one-line change. Once it lands, this PR's Windows job should pass on a re-run.

@zackees

zackees commented Oct 26, 2026

Copy link
Copy Markdown
Member

The Windows Tier 2 failure here is the MSVC C5287 error in test_source_video_tick_steps.c from #2448, not this change. #2451 fixes it and is queued; this will need a rebase or rerun after it lands.

…2447)

Since 33.0, monitoring is on or off, on means monitor and output, and
the monitor ignores mute, so Monitor Only is monitoring on plus muted
(obs-rust#2445 loads old scene collections that way). The deprecated
obs_source_set_monitoring_type() turns every type but none into
monitoring on and leaves the mute alone, so a source set to Monitor Only
by a plugin, a script or obs-websocket's SetInputAudioMonitorType plays
into the stream and recordings. obs_source_get_monitoring_type(),
GetInputAudioMonitorType and the "audio_monitoring" signal report
Monitor and Output for it.

test_source_monitoring_type calls the libobs setter and getter;
test_ws_monitor_only goes through the unchanged obs-websocket input
requests, compiled against libobs. Both play a tone from a source on the
program and check the mixed audio libobs hands to outputs and the
monitor feed after each step: Monitor Only must keep the tone out of the
mix and read back as Monitor Only; the other types must round-trip;
leaving Monitor Only must undo its mute but keep any other mute; a
monitored, muted source must read back as Monitor Only.

Refs obs-rust#2447
Since 33.0, monitoring is on or off, on means monitor and output, and
the monitor plays a source even when it is muted. The deprecated
obs_source_set_monitoring_type() turned Monitor Only into monitoring on
and left the mute alone, so a source that a plugin, a script or
obs-websocket's SetInputAudioMonitorType set to Monitor Only played into
the stream and recordings.

Monitor Only now mutes the source, then turns monitoring on, as a
pre-33.0 Monitor Only source loads (obs-rust#2444). Monitor and Output unmutes
it and turns monitoring on. None turns monitoring off and keeps the
mute, except that it undoes the mute Monitor Only set when nothing has
set the mute since, so the source plays into the outputs as None did
before 33.0. obs_source_set_muted() clears that mark. The getter and
the "audio_monitoring" signal report a monitored, muted source as
Monitor Only; the stored type and the saved scene collection are
unchanged.

Fixes obs-rust#2447
@rdoupe
rdoupe force-pushed the fix/ws-monitor-only branch from 3f5e8d8 to de05845 Compare October 26, 2026 07:09
@zackees
zackees added this pull request to the merge queue Oct 26, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 26, 2026
@zackees
zackees added this pull request to the merge queue Oct 26, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 26, 2026
@zackees
zackees added this pull request to the merge queue Oct 26, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 26, 2026
@rdoupe
rdoupe added this pull request to the merge queue Oct 26, 2026
Merged via the queue into obs-rust:master with commit c1329ac Oct 26, 2026
38 checks passed
@rdoupe
rdoupe deleted the fix/ws-monitor-only branch October 26, 2026 09:37
@rdoupe rdoupe mentioned this pull request Oct 27, 2026
6 of 7 tasks
rdoupe added a commit that referenced this pull request Oct 27, 2026
Behaviour-neutral extraction before the Rust port. The decisions of
obs_source_set_flags(), obs_source_set_audio_mixers(),
obs_source_get_audio_mixers(), obs_source_enable_push_to_mute(),
obs_source_enable_push_to_talk(), obs_source_set_monitoring_enabled(),
obs_source_set_monitoring_type(), obs_source_get_monitoring_type(),
clamp_balance() and obs_source_set_audio_active() in obs-source.c.h move
to libobs/obs-source-monitoring.c.h, as thirteen functions on plain
values declared in the new libobs/obs-source-monitoring.h without
EXPORT (hidden, as -fvisibility=hidden keeps them):

- _changes(): a set does something only when the value changes (the
  flags, the mixers, monitoring on or off, and the push-to enables'
  signal);
- _has_audio(): the mixers' gate for a source that does not own its
  info id (OBS_SOURCE_AUDIO in the output flags);
- _mixers_store(): the low 32 bits of what an audio_mixers handler
  leaves;
- _push_to_logs(): the push-to enables log a change on an audio source;
- _on_type(): monitoring on and muted is Monitor Only, else Monitor and
  Output (#2447), for the compatibility signal and the getter;
- _stored_type() and _monitor() -> KEEP | CREATE | DESTROY: the type
  obs_source_set_monitoring_enabled() stores and the monitor route;
- _route() -> OFF | MONITOR_ONLY | MONITOR_AND_OUTPUT | ON: the steps of
  obs_source_set_monitoring_type() for a type (#2444, #2447);
- _off_reads_mute() and _off_unmutes(): none looks at the mute only when
  Monitor Only set it, and undoes it if it is still on;
- _only_mutes(): Monitor Only mutes first if not muted;
- _balance(): clamp_balance(), NaN centred, else clamped to [0, 1]
  (#2051);
- _audio_active() -> NONE | ACTIVATE | DEACTIVATE: the signal of a
  change.

Split per step, as #2153 did, so every call and read stays where it was
and under its condition: obs_source_valid(), audio_mutex,
os_atomic_load_bool() of monitor_only_muted once before the steps and
os_atomic_set_bool() of it and of audio_active, the reads of
owns_info_id (and of the output flags only when it is false),
obs_source_get_output_flags() before the changed test, user_muted read
only when monitoring turns on, monitoring_enabled read again after the
signals, obs_source_muted() only where the C's && reached it (after
monitoring is off, for none), obs_source_set_muted() and
obs_source_set_monitoring_enabled() in their order (the mute before
monitoring turns on), every calldata_*(), signal and log, the (float)
of the handler's balance, audio_monitor_create() and _destroy(), and
the getters' answers for an invalid source (0, false and 0.5).
clamp_balance() stays as an inline wrapper for its two calls. The
monitoring type crosses as int; the header copies OBS_SOURCE_AUDIO and
the three monitoring types, and obs-source.c.h checks them and that
enum obs_monitoring_type is int-sized with _Static_assert.

Checked in a scratch harness (not committed). It compiles
origin/master's fifteen functions (the ten above with
obs_source_get_flags(), obs_source_get_monitoring_enabled(),
obs_source_get_balance_value(), obs_source_set_balance_value() and
obs_source_audio_active()) verbatim next to this branch's and the moved
file, as C++, over a stand-in source whose fields log every read and
write with its value. Every call is logged in order with its arguments:
obs_source_valid(), the lock and unlock, the calldata sets and gets,
each signal, blog(), obs_source_get_output_flags(), the atomics,
obs_source_muted(), obs_source_set_muted(), obs_source_dosignal(), the
push-to signal and audio_monitor_create() and _destroy(). A signal
handler may leave any mixers (above UINT32_MAX, negative, the ends of
int64_t) or any balance, and another thread may write any field at any
event. Inputs: an invalid source or any start state, flags and mixers
at the edges and random, types 0 to 4, -1, the ends of int and random,
balances at NaN (both signs), the signed zeros, the smallest
subnormals, 1.0 and its neighbours, the ends of float, the infinities
and random bit patterns; one to four operations each.

5,800,000 runs: 2,000,000 each with GCC and Clang at -O2, 500,000 each
at -O0, 300,000 each under ASan and UBSan with no recovery (GCC -O0,
Clang -O2, without C++'s enum range check, which C does not have) and
200,000 with gcov. All 82,503,972 logged events match, as does the
final state. Every branch of origin/master's functions is taken both
ways.

30 deliberately broken moves were planted, and all 30 fail it: each
function's rule inverted, widened or made constant (changes as == and
>, the audio bit as bit 0 or any flag, the mixers shifted or saturated,
the log rule without the flag or without the change, the on type
constant or inverted, the stored type Monitor Only, the monitor never
kept or created on the wrong edge, each route moved, the default OFF,
the three mute steps constant, NaN as 0.0, the clamp at <= 0 and below
1.0, the active signal never NONE or on prev), and at the call sites
the mute read before the mark, user_muted read without its log, the
output flags read only after a change, the mixers' gate reading the
output flags first, and the type taken mod 4.

git blame on the moved lines shows the upstream commits d2934ec and
646836a, and the fork's #2058 (the balance clamp) and #2450 (the
Monitor Only steps); they move as they are.

The new file joins libobs.core-source with libobs.pch.h, which includes
the new header; source-sets.json lists it. docs/rust-port/naming.md
already maps libobs/obs-source*.c.h to rust/obs-source. The exported
symbol set is unchanged (1766 on the C backend); the thirteen functions
are local.

Refs #2621.
MCPVOT pushed a commit to MCPVOT/obs-studio that referenced this pull request Oct 30, 2026
* libobs: Move the source monitoring decisions

Behaviour-neutral extraction before the Rust port. The decisions of
obs_source_set_flags(), obs_source_set_audio_mixers(),
obs_source_get_audio_mixers(), obs_source_enable_push_to_mute(),
obs_source_enable_push_to_talk(), obs_source_set_monitoring_enabled(),
obs_source_set_monitoring_type(), obs_source_get_monitoring_type(),
clamp_balance() and obs_source_set_audio_active() in obs-source.c.h move
to libobs/obs-source-monitoring.c.h, as thirteen functions on plain
values declared in the new libobs/obs-source-monitoring.h without
EXPORT (hidden, as -fvisibility=hidden keeps them):

- _changes(): a set does something only when the value changes (the
  flags, the mixers, monitoring on or off, and the push-to enables'
  signal);
- _has_audio(): the mixers' gate for a source that does not own its
  info id (OBS_SOURCE_AUDIO in the output flags);
- _mixers_store(): the low 32 bits of what an audio_mixers handler
  leaves;
- _push_to_logs(): the push-to enables log a change on an audio source;
- _on_type(): monitoring on and muted is Monitor Only, else Monitor and
  Output (obs-rust#2447), for the compatibility signal and the getter;
- _stored_type() and _monitor() -> KEEP | CREATE | DESTROY: the type
  obs_source_set_monitoring_enabled() stores and the monitor route;
- _route() -> OFF | MONITOR_ONLY | MONITOR_AND_OUTPUT | ON: the steps of
  obs_source_set_monitoring_type() for a type (obs-rust#2444, obs-rust#2447);
- _off_reads_mute() and _off_unmutes(): none looks at the mute only when
  Monitor Only set it, and undoes it if it is still on;
- _only_mutes(): Monitor Only mutes first if not muted;
- _balance(): clamp_balance(), NaN centred, else clamped to [0, 1]
  (obs-rust#2051);
- _audio_active() -> NONE | ACTIVATE | DEACTIVATE: the signal of a
  change.

Split per step, as obs-rust#2153 did, so every call and read stays where it was
and under its condition: obs_source_valid(), audio_mutex,
os_atomic_load_bool() of monitor_only_muted once before the steps and
os_atomic_set_bool() of it and of audio_active, the reads of
owns_info_id (and of the output flags only when it is false),
obs_source_get_output_flags() before the changed test, user_muted read
only when monitoring turns on, monitoring_enabled read again after the
signals, obs_source_muted() only where the C's && reached it (after
monitoring is off, for none), obs_source_set_muted() and
obs_source_set_monitoring_enabled() in their order (the mute before
monitoring turns on), every calldata_*(), signal and log, the (float)
of the handler's balance, audio_monitor_create() and _destroy(), and
the getters' answers for an invalid source (0, false and 0.5).
clamp_balance() stays as an inline wrapper for its two calls. The
monitoring type crosses as int; the header copies OBS_SOURCE_AUDIO and
the three monitoring types, and obs-source.c.h checks them and that
enum obs_monitoring_type is int-sized with _Static_assert.

Checked in a scratch harness (not committed). It compiles
origin/master's fifteen functions (the ten above with
obs_source_get_flags(), obs_source_get_monitoring_enabled(),
obs_source_get_balance_value(), obs_source_set_balance_value() and
obs_source_audio_active()) verbatim next to this branch's and the moved
file, as C++, over a stand-in source whose fields log every read and
write with its value. Every call is logged in order with its arguments:
obs_source_valid(), the lock and unlock, the calldata sets and gets,
each signal, blog(), obs_source_get_output_flags(), the atomics,
obs_source_muted(), obs_source_set_muted(), obs_source_dosignal(), the
push-to signal and audio_monitor_create() and _destroy(). A signal
handler may leave any mixers (above UINT32_MAX, negative, the ends of
int64_t) or any balance, and another thread may write any field at any
event. Inputs: an invalid source or any start state, flags and mixers
at the edges and random, types 0 to 4, -1, the ends of int and random,
balances at NaN (both signs), the signed zeros, the smallest
subnormals, 1.0 and its neighbours, the ends of float, the infinities
and random bit patterns; one to four operations each.

5,800,000 runs: 2,000,000 each with GCC and Clang at -O2, 500,000 each
at -O0, 300,000 each under ASan and UBSan with no recovery (GCC -O0,
Clang -O2, without C++'s enum range check, which C does not have) and
200,000 with gcov. All 82,503,972 logged events match, as does the
final state. Every branch of origin/master's functions is taken both
ways.

30 deliberately broken moves were planted, and all 30 fail it: each
function's rule inverted, widened or made constant (changes as == and
>, the audio bit as bit 0 or any flag, the mixers shifted or saturated,
the log rule without the flag or without the change, the on type
constant or inverted, the stored type Monitor Only, the monitor never
kept or created on the wrong edge, each route moved, the default OFF,
the three mute steps constant, NaN as 0.0, the clamp at <= 0 and below
1.0, the active signal never NONE or on prev), and at the call sites
the mute read before the mark, user_muted read without its log, the
output flags read only after a change, the mixers' gate reading the
output flags first, and the type taken mod 4.

git blame on the moved lines shows the upstream commits d2934ec and
646836a, and the fork's obs-rust#2058 (the balance clamp) and obs-rust#2450 (the
Monitor Only steps); they move as they are.

The new file joins libobs.core-source with libobs.pch.h, which includes
the new header; source-sets.json lists it. docs/rust-port/naming.md
already maps libobs/obs-source*.c.h to rust/obs-source. The exported
symbol set is unchanged (1766 on the C backend); the thirteen functions
are local.

Refs obs-rust#2621.

* test: Characterize the source monitoring rules

test_source_monitoring_steps compiles libobs/obs-source-monitoring.c.h
(ASan and UBSan) and calls only its functions through the header. 16
cases:

- the header's copies of OBS_SOURCE_AUDIO and the three monitoring
  types, the enum's size and the monitor, route and active values;
- the same-value rule over pairs of 32-bit edges and the four bools;
- the audio gate over every bit and the mixers' stored value;
- the push-to log rule over flags and changes;
- the on and stored types and the monitor route, every combination;
- the route over -1000..1000 and the ends of int, and the three mute
  steps;
- the balance at NaN (quiet, negative and signalling), the signed
  zeros, the smallest subnormals, 1.0 and its neighbours, the ends of
  float and the infinities;
- the audio active signal, every combination.

obs_source_set_monitoring_type(), _set_monitoring_enabled(),
_get_monitoring_type(), _set_balance_value(), _set_audio_mixers(),
_get_audio_mixers(), _set_flags(), the push-to enables and
_set_audio_active() are also copied onto a stand-in source that records
its signals, logs, mute calls and monitor, to pin end to end: every
type (none, Monitor Only, Monitor and Output, 3 and -1) from every
start state (on or off, muted or not, Monitor Only's mark set or not),
with the steps in order (the mute before monitoring turns on), the
compatibility signal's type and the getter after each; Monitor Only
then none (the mute goes) and with a mute set in between (it stays);
the same value of monitoring on or off doing nothing; the balance sent
and the one a handler leaves (NaN, 2.0, -1.0, 0.75, -0.0); the mixers
of an audio source, a video-only source with and without its own info
id and an invalid source, and a handler's value; the flags; the
push-to enables with the same value (no signal, no log), a change
(log, then signal) and on a video-only source (signal, no log); and
audio active set twice (one signal).

Characterized, not endorsed:
- the mixers a handler leaves are truncated to their low 32 bits:
  2^32 + 3 is 3, -1 is UINT32_MAX;
- a balance of -0.0 stays -0.0;
- Monitor and Output unmutes a mute the user set, while none keeps it.

It is in the native test graph, in name order, with the header as a
watch input and libm off MSVC. No assert_in_range or fail_msg, so it
builds with cmocka 1.x and 2.x, and no two enum types are compared.
The real-libobs tests test_source_monitoring_type,
test_source_monitor_only_load and test_source_audio_balance_mono cover
the callers.

Refs obs-rust#2621.

* obs-source: Test the monitoring port

RED for the port of libobs/obs-source-monitoring.c.h to a new
monitoring module of rust/obs-source, next to ptm_gate and
volume_apply. The safe core is a stub (todo!()): changes(),
has_audio(), mixers_store(), push_to_logs(), on_type(), stored_type(),
monitor() -> Monitor (Keep, Create, Destroy), route() -> Route (Off,
MonitorOnly, MonitorAndOutput, On), off_reads_mute(), off_unmutes(),
only_mutes(), balance() and audio_active() -> Active (None, Activate,
Deactivate), with AUDIO and the three monitoring types.

The plan sketched wider steps: monitoring_enable(cur, enabled, muted)
returning the compatibility type, the stored type and the monitor at
once, monitoring_type_first(type, muted) and
monitoring_type_after_disable(muted_for_monitor_only, muted_now), and
type_of(valid, enabled, muted). Each would take a read the C makes only
under a condition (user_muted only when monitoring turns on,
monitoring_enabled again after the signals, obs_source_muted() only
where its && reaches it) before the C knows it makes it; so the C keeps
those conditions and asks one rule per read. The validity checks and
the getters' literals stay C.

tests/source/monitoring.rs ports test_source_monitoring_steps 1:1: its
16 cases under the same names (steps_*), in the same order, with the
same values and expectations, the characterized-not-endorsed cases and
the stand-in source's copies of the callers included.

Edge cases (edge_*) check the core against models of origin/master's
expressions:
- every decision over every combination of its flags;
- the route over -100,000..100,000, the ends of i32 and every power of
  two with its neighbours;
- the mixers at every power of two of i64 with its neighbours and
  negations, and the audio gate at every bit;
- the balance at every special value (both NaNs, signalling NaNs, the
  signed zeros, the subnormals, 1.0 and its neighbours, the ends and
  the infinities), every 1/4096 from -2.0 to 2.0 and a stride over
  every bit pattern;
- proptests (4,096 cases each): every decision over full-range values,
  and sequences of up to eleven operations (flags, mixers, push-to,
  monitoring on or off, any type, the getters, any balance, audio
  active, a user mute, a handler that leaves any mixers or balance,
  the output flags and the info id) through the stand-in against a
  copy of the callers with origin/master's expressions inline; both
  end in the same state after every step.

soldr cargo test -p obs-data --test obs-tests-core --
source::monitoring passes 1 of 22 (the constants) and fails 21 with
"not yet implemented".

Refs obs-rust#2621.

* obs-source: Port the source monitoring rules

Ports libobs/obs-source-monitoring.c.h, the monitoring, monitor-only
mute, balance, mixer, flag, push-to and audio active rules of
obs-source.c, to the monitoring module of rust/obs-source.
obs-source.c.h stays C around it: obs_source_valid(), audio_mutex, the
atomics on monitor_only_muted and audio_active, every read of the
source's fields at its point (the output flags only when the source does
not own its info id, user_muted only when monitoring turns on,
monitoring_enabled again after the signals),
obs_source_get_output_flags(), obs_source_muted() and
obs_source_set_muted() under the C's own &&, every calldata_*(), signal
and log, the (float) of the handler's balance, audio_monitor_create()
and _destroy(), and the getters' literals. The hybrid libobs drops the C
file and links the Rust shims in its place.

Safe core (src/monitoring.rs), thirteen functions:
- changes(): cur != value;
- has_audio(): AUDIO (1 << 1) in the output flags;
- mixers_store(): the low 32 bits of the i64, by its bytes;
- push_to_logs(): has_audio() && changed;
- on_type(), stored_type(): Monitor Only when muted, else Monitor and
  Output; Monitor and Output when on, else none;
- monitor() -> Monitor: Create on false -> true, Destroy on true ->
  false, else Keep;
- route() -> Route: 0, 1 and 2 to Off, MonitorOnly and
  MonitorAndOutput, any other i32 On;
- off_reads_mute(), off_unmutes(), only_mutes(): the mark, the mute,
  and not muted;
- balance(): NaN is 0.5, then the C's two compares in its order (not
  f32::clamp()), so -0.0 stays -0.0;
- audio_active() -> Active: Activate on false -> true, Deactivate on
  true -> false, else None.
The module denies clippy's arithmetic, indexing, as, float_cmp,
unwrap, expect, panic, todo, unimplemented and unreachable lints,
keeps no state and takes no lock.

Shims (src/ffi/source_monitoring.rs): the thirteen C signatures by
value; the monitoring types as int, the handler's mixers as int64_t,
the balance as float, the monitor, route and active outcomes as the
C's enums (u32). No pointer crosses and no struct is mirrored. No
intentional difference from the C.

C side: the file leaves libobs.core-source for its own group,
libobs.ported-source-monitoring (LIBOBS_PORTED_SOURCES), whose PCH is
only math.h and obs-source-monitoring.h; libobs.pch.h keeps the header
for obs-source.c. The functions are not EXPORT, so the shims are
hidden: rust-hidden.txt, the local: list of rust-exports.map and
rust-unexports-macos.txt. libobs-rust already links the crate's ffi.
ci/test_ported_sources.py checks the group on every host.

test_source_monitoring_steps still compiles the C file into itself,
unchanged. The new test_source_monitoring_steps_rust builds the same
test source against libobs_rust-static instead, with libm off MSVC
for nextafterf(), as test_transition_load_steps_rust does, and links:
the test includes the header and obs.h and calls only hidden
functions. It passes 16/16 on the C backend, the hybrid and
libobs-c-reference, as does test_source_monitoring_steps; so do
test_source_monitoring_type, test_source_monitor_only_load,
test_source_audio_balance_mono and the other 80 source, audio,
monitor, mute, push-to and volume tests (three failed once each under
a parallel run and pass alone on both). The exported symbol sets are
unchanged and equal: 1766 on the C backend, the hybrid and
libobs-c-reference; the thirteen functions are local in all three, and
the group's object is built only for libobs-c-reference.

soldr cargo test -p obs-data --test obs-tests-core --
source::monitoring:: passes 22 of 22.

No user-visible change, so no docs/features.md line.

Refs obs-rust#2621.

* obs-source: Check monitoring against the C

Tier 3 for the monitoring port. oracle/source_monitoring.c compiles the
unmodified libobs/obs-source-monitoring.c.h with its thirteen functions
renamed to oracle_*, and exports the header's values and sizes by
index (the renames also rename the monitor and route enum tags, which
share their functions' names, so the enums are sized by what the
functions return). tests/source/monitoring_parity.rs gives each input
to the oracle, the shim (the extern "C" function the hybrid links) and
the core, and compares each bool as the C's byte (0 or 1, the same on
both sides), each outcome as the C's enum value, each integer exactly
and the balance by its bits:

- the header's values and the sizes of the three enums, bool, int and
  float;
- every function of flags over every combination of them, and the
  push-to log rule with each flag bit;
- the same-value rule and the audio gate over every pair of 32-bit
  edges and every bit;
- the mixers at every power of two of int64_t with its neighbours and
  negations, the 65,536 values either side of 2^32 and -2^32 and at
  each end, and a stride over int64_t;
- the route over every int from -1,000,000 to 1,000,000, every 65,521st
  int and the ends;
- the balance over every bit pattern whose top byte is 0x00, 0x3f,
  0x7f, 0x80, 0xbf or 0xff (the zeros, the subnormals, 0.5 to 2.0, the
  infinities and every NaN, of both signs: 100,663,296 patterns) and
  every 251st pattern of the rest;
- proptests (20,000 cases) over full-range values.

Nothing is excluded: the C has no undefined or host-dependent result
here (isnan() and the compares are IEEE on every host, and the
(uint32_t) of an int64_t is modular), and the port has no intentional
difference. Every one of the 2^32 balance patterns was also checked
once (131 s in a debug build, too slow to keep).

Mutation check: 34 mutants, all killed. 22 in the core (changes as ==,
the audio bit as bit 0, the mixers saturated or byte-swapped, the log
rule without the flag or without the change, the on type inverted, the
stored type Monitor Only, the monitor never created or created on the
wrong edge, each route moved and the default Off, the three mute steps
constant or inverted, NaN as 0.0, the clamp at <= 0.0 and above 0.999,
the active signal on the wrong edge or never None): Tier 1 and Tier 3
each kill all 22. 12 in the shims (changes inverted, the mixers
shifted, the log rule always changed, the on type inverted, the
monitor's and the active signal's arguments swapped, the type taken
mod 4, the balance's absolute value, Create as Destroy, On as Off,
Activate as None, and off_unmutes() answered by the wrong rule): Tier 3
kills all 12 (Tier 1 calls only the core).

Refs obs-rust#2621.

* docs: Record the source monitoring port

rust/README.md gets the row for libobs/obs-source-monitoring.c.h
(obs-source::monitoring), in sorted place: what moved, what stays C
and why (every read and call at its point and under its condition, the
C's own && before each mute read, the atomics, the signals, the
monitor and the getters' literals), the characterized-not-endorsed
behaviours, the tests that see the swap and what Tier 3 covers. The
obs-source crate row, its Cargo description and the lib.rs crate docs
name the new module. docs/rust-port/naming.md already maps
libobs/obs-source*.c.h to rust/obs-source.

rust/obs-c-oracle/benches/port_comparison/source_monitoring.rs runs
1024 updates over every combination of the flags the C reads, with
monitoring types 0, 1, 2, 3 and -1, handler mixers of 0x3f, -1 and
past 32 bits, and balances at NaN, -0.0, 0.25, 0.5, 1.0, 2.0 and -1.0,
each through the same-value rule, the audio gate, the stored mixers,
the push-to log rule, the monitoring types and monitor route, the
type's route and its three mute steps, the balance twice and the
active signal, on the shims and on the oracle, and checks both
checksums match. Release, x86_64 Linux, GCC, a loaded host, median per
1024 updates: Rust 17.74 us, C 18.05 us (0.98x), about 17 ns an update.

The port changes nothing a user can see, so docs/features.md has no
line.

Fixes obs-rust#2621.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

bug: obs-websocket: SetInputAudioMonitorType "Monitor Only" plays into the stream on 33.0 (follow-up of #2444)

2 participants