Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 47 additions & 2 deletions engine/core/DynamicAcceptedSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,50 @@ class DynamicAcceptedSession final {
std::move(result),std::move(fx));
}

// WIRE transaction is validated against accepted roster+graph revisions,
// then compiled/published by the SAME native PCM publisher as other edits.
// No GUI state or separate audio graph becomes authoritative on rejection.
Result wireTransaction(std::uint64_t expectedRosterRevision,
std::uint64_t expectedGraphRevision,
std::span<const WireChange> changes,
const DynamicGraphRequest& route,
std::uint64_t expectedDeviceEpoch,
std::uint64_t activeDeviceEpoch,
bool allOutputsDisarmed,
DynamicNativeSessionPublisher::FxPlan fx={}) {
Result result=preflight(expectedRosterRevision,expectedGraphRevision);
if(!result.ok())return result;
auto candidate=accepted_;
result.edit=candidate.wireTransaction(expectedRosterRevision,changes);
if(!result.edit.ok()) {
result.fault=Fault::editRejected;return result;
}
// WIRE-only publication may remain armed ONLY when every accepted
// physical patch and device admission parameter remains unchanged.
// Reject stale/changed physical drafts rather than silently rebasing.
const bool samePhysical=
route.physicalInputs==acceptedRequest_.physicalInputs &&
route.maxFrames==acceptedRequest_.maxFrames &&
route.maxScratchBytes==acceptedRequest_.maxScratchBytes &&
route.mainMonoAfterFx==acceptedRequest_.mainMonoAfterFx &&
route.sources.size()==acceptedRequest_.sources.size() &&
std::equal(route.sources.begin(),route.sources.end(),
acceptedRequest_.sources.begin(),
[](const DynamicSourcePatch& a,const DynamicSourcePatch& b) {
return a.id==b.id && a.inputL==b.inputL &&
a.inputR==b.inputR && a.stereo==b.stereo;
});
if(!allOutputsDisarmed && !samePhysical) {
result.fault=Fault::graphRejected;
result.native.fault=DynamicNativeSessionPublisher::Fault::outputArmed;
result.native.revision=publisher_.acceptedRevision();
return result;
}
return publish(std::move(candidate),route,expectedGraphRevision,
expectedDeviceEpoch,activeDeviceEpoch,allOutputsDisarmed,
std::move(result),std::move(fx),samePhysical);
}

// Studio Pro: one independent Bus ChannelId and automatic assignment of
// selected primary OUTs; FX Return leaves those OUTs intact and adds sends.
Result createBusForSelected(std::uint64_t expectedRosterRevision,
Expand Down Expand Up @@ -221,7 +265,8 @@ class DynamicAcceptedSession final {
std::uint64_t previousGraphRevision,
std::uint64_t requestedEpoch,std::uint64_t currentEpoch,
bool outputsDisarmed,Result&& result,
DynamicNativeSessionPublisher::FxPlan fx) {
DynamicNativeSessionPublisher::FxPlan fx,
bool verifiedWireOnly=false) {
// Existing SOURCE targets belong to the accepted native session.
// For already-patched ChannelIds with the same input format, ignore
// stale physical pin ordinals supplied by an old GUI draft during
Expand Down Expand Up @@ -267,7 +312,7 @@ class DynamicAcceptedSession final {
const bool nextHasFx=static_cast<bool>(fx.owner);
result.native=publisher_.commit(candidate,effective,previousGraphRevision,
requestedEpoch,currentEpoch,outputsDisarmed,
std::move(fx));
std::move(fx),verifiedWireOnly);
if(!result.native.ok()) {
result.fault=Fault::graphRejected;
result.edit.createdIds.clear(); // NEVER advertise phantom Bus
Expand Down
155 changes: 154 additions & 1 deletion engine/core/DynamicChannelRoster.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ struct ChannelSendRef final {
bool preFader=false;
float pan=0.f; // send-only balance: PRE mono uses equal-power coefficients
bool primaryOutput=false; // OUT selector edge, not a parallel SEND
std::uint64_t edgeId=0; // control-owned stable identity, never a PCM buffer
};
struct ChannelStripDefinition final {
ChannelId id{};
Expand Down Expand Up @@ -78,6 +79,15 @@ struct ChannelEditResult final {
bool ok() const noexcept {return fault==ChannelEditFault::none;}
};

struct WireChange final {
enum class Kind : std::uint8_t { connect, remove, retarget, routeOnly };
Kind kind=Kind::connect;
std::uint64_t edgeId=0; // REMOVE/RETARGET must address exact accepted edge
ChannelId source{},destination{};
float gain=1.f;
bool preFader=false;
};

class DynamicChannelRoster final {
public:
explicit DynamicChannelRoster(ChannelRosterBudget budget={}) : budget_(budget) {}
Expand Down Expand Up @@ -276,6 +286,10 @@ class DynamicChannelRoster final {
// Structural edits are infrequent and prepared entirely on CONTROL.
// Rebuild before acceptance: rejected/failed edits cannot leave the
// active stable-ID map pointing into an old reordered vector.
if(!assignMissingEdges(candidate,nextEdgeId_)) {
result.fault=ChannelEditFault::idExhausted;
result.createdIds.clear();return result;
}
auto stagedIndex=buildIndex(candidate);
channels_.swap(candidate);
index_.swap(stagedIndex);
Expand All @@ -284,6 +298,122 @@ class DynamicChannelRoster final {
return result;
}

// CONTROL only: one atomic WIRE batch against the canonical roster.
// Native PCM compile/publish is performed by DynamicAcceptedSession.
// Edge identity survives RETARGET and unrelated graph edits.
ChannelEditResult wireTransaction(std::uint64_t expectedRevision,
std::span<const WireChange> changes) {
ChannelEditResult result;result.revision=revision_;
if(expectedRevision!=revision_) {
result.fault=ChannelEditFault::staleRevision;return result;
}
if(changes.empty() || revision_==std::numeric_limits<std::uint64_t>::max()) {
result.fault=ChannelEditFault::invalidAction;return result;
}
auto candidate=channels_;
auto edgeCounter=nextEdgeId_;
if(!assignMissingEdges(candidate,edgeCounter)) {
result.fault=ChannelEditFault::idExhausted;return result;
}
const auto findRow=[&](ChannelId id) {
return std::find_if(candidate.begin(),candidate.end(),
[id](const auto& row){return row.id==id;});
};
const auto findEdge=[&](std::uint64_t id) {
for(auto& row:candidate)
for(auto it=row.sends.begin();it!=row.sends.end();++it)
if(it->edgeId==id)
return std::pair{&row,it};
return std::pair<ChannelStripDefinition*,
std::vector<ChannelSendRef>::iterator>{nullptr,{}};
};
const auto legal=[&](ChannelId from,ChannelId to,float gain) {
const auto a=findRow(from),b=findRow(to);
return from.value && to.value && from!=to &&
a!=candidate.end() && b!=candidate.end() &&
a->role!=ChannelRole::main && a->role!=ChannelRole::monitor &&
b->role!=ChannelRole::monitor &&
std::isfinite(gain) && gain>=0.f && gain<=4.f;
};
const auto duplicate=[&](ChannelId from,ChannelId to) {
const auto row=findRow(from);
return row!=candidate.end() &&
std::any_of(row->sends.begin(),row->sends.end(),
[to](const auto& e){return e.destination==to;});
};
const auto connect=[&](ChannelId from,ChannelId to,float gain,
bool pre) {
auto row=findRow(from);
if(!legal(from,to,gain) || duplicate(from,to) ||
row->sends.size()>=budget_.maximumSendsPerChannel ||
edgeCounter==0 || edgeCounter==std::numeric_limits<std::uint64_t>::max())
return false;
row->sends.push_back({to,gain,pre,0.f,false,edgeCounter++});
return true;
};
for(std::size_t i=0;i<changes.size();++i) {
const auto& change=changes[i];
result.actionIndex=i;
bool ok=false;
switch(change.kind) {
case WireChange::Kind::connect:
ok=connect(change.source,change.destination,change.gain,
change.preFader);
break;
case WireChange::Kind::remove: {
if(!change.edgeId)break;
auto [row,it]=findEdge(change.edgeId);
if(row) {row->sends.erase(it);ok=true;}
break;
}
case WireChange::Kind::retarget: {
if(!change.edgeId)break;
auto [row,it]=findEdge(change.edgeId);
if(!row || !legal(row->id,change.destination,it->gain))break;
if(it->destination!=change.destination &&
duplicate(row->id,change.destination))break;
it->destination=change.destination;
ok=true;break;
}
case WireChange::Kind::routeOnly: {
const auto src=findRow(change.source);
if(src==candidate.end())break;
const auto master=std::find_if(candidate.begin(),candidate.end(),
[](const auto& row){return row.role==ChannelRole::main;});
if(master==candidate.end() ||
!legal(change.source,change.destination,change.gain) ||
change.destination==master->id)break;
const auto mainId=master->id;
// Normal SEND retained its MAIN route. ROUTE_ONLY removes
// precisely MAIN and reuses an existing target edge instead
// of double summing or rejecting an already-connected target.
const bool alreadyConnected=duplicate(change.source,
change.destination);
auto& sends=src->sends;
sends.erase(std::remove_if(sends.begin(),sends.end(),
[mainId](const auto& e){return e.destination==mainId;}),
sends.end());
ok=alreadyConnected ||
connect(change.source,change.destination,change.gain,
change.preFader);
break;
}
}
if(!ok) {result.fault=ChannelEditFault::invalidValue;return result;}
}
if(!withinBudget(candidate)) {
result.fault=ChannelEditFault::budgetExceeded;return result;
}
if(!assignMissingEdges(candidate,nextEdgeId_)) {
result.fault=ChannelEditFault::idExhausted;
result.createdIds.clear();return result;
}
auto stagedIndex=buildIndex(candidate);
channels_.swap(candidate);index_.swap(stagedIndex);
nextEdgeId_=edgeCounter;
result.revision=++revision_;return result;
}

// Studio Console Add Bus / Add FX Channel: allocate a fresh independent
// stereo ChannelId, OUT -> MAIN, without borrowing an input or requiring
// a selection. CONTROL only; native graph publication is separate.
Expand Down Expand Up @@ -322,6 +452,10 @@ class DynamicChannelRoster final {
// Structural edits are infrequent and prepared entirely on CONTROL.
// Rebuild before acceptance: rejected/failed edits cannot leave the
// active stable-ID map pointing into an old reordered vector.
if(!assignMissingEdges(candidate,nextEdgeId_)) {
result.fault=ChannelEditFault::idExhausted;
result.createdIds.clear();return result;
}
auto stagedIndex=buildIndex(candidate);
channels_.swap(candidate);
index_.swap(stagedIndex);result.createdIds.push_back({nextId_++});
Expand Down Expand Up @@ -377,6 +511,10 @@ class DynamicChannelRoster final {
// Structural edits are infrequent and prepared entirely on CONTROL.
// Rebuild before acceptance: rejected/failed edits cannot leave the
// active stable-ID map pointing into an old reordered vector.
if(!assignMissingEdges(candidate,nextEdgeId_)) {
result.fault=ChannelEditFault::idExhausted;
result.createdIds.clear();return result;
}
auto stagedIndex=buildIndex(candidate);
channels_.swap(candidate);
index_.swap(stagedIndex);
Expand Down Expand Up @@ -457,6 +595,10 @@ class DynamicChannelRoster final {
// Structural edits are infrequent and prepared entirely on CONTROL.
// Rebuild before acceptance: rejected/failed edits cannot leave the
// active stable-ID map pointing into an old reordered vector.
if(!assignMissingEdges(candidate,nextEdgeId_)) {
result.fault=ChannelEditFault::idExhausted;
result.createdIds.clear();return result;
}
auto stagedIndex=buildIndex(candidate);
channels_.swap(candidate);
index_.swap(stagedIndex);
Expand All @@ -466,6 +608,17 @@ class DynamicChannelRoster final {
}

private:
static bool assignMissingEdges(std::vector<ChannelStripDefinition>& rows,
std::uint64_t& next) noexcept {
for(auto& row:rows)
for(auto& edge:row.sends)
if(!edge.edgeId) {
if(!next || next==std::numeric_limits<std::uint64_t>::max())
return false;
edge.edgeId=next++;
}
return true;
}
static std::unordered_map<std::uint64_t,std::size_t> buildIndex(
const std::vector<ChannelStripDefinition>& rows) {
std::unordered_map<std::uint64_t,std::size_t> index;
Expand Down Expand Up @@ -506,6 +659,6 @@ class DynamicChannelRoster final {
ChannelRosterBudget budget_;
std::vector<ChannelStripDefinition> channels_;
std::unordered_map<std::uint64_t,std::size_t> index_; // CONTROL ONLY
std::uint64_t revision_=0, nextId_=1;
std::uint64_t revision_=0, nextId_=1, nextEdgeId_=1;
};
} // namespace armix::mixer
13 changes: 13 additions & 0 deletions engine/core/DynamicConsoleRoutingView.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <cstdint>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

Expand All @@ -19,6 +20,12 @@ struct ConsoleSendView final {
float gain=1.f,pan=0.f;
bool preFader=false;
};
struct ConsoleWireView final {
std::uint64_t edgeId=0;
ChannelId source{},destination{};
float gain=1.f;
bool preFader=false,primaryOutput=false;
};
struct ConsoleChannelView final {
ChannelId id{};
ChannelRole role=ChannelRole::input;
Expand All @@ -40,6 +47,7 @@ class DynamicConsoleRoutingView final {
public:
std::uint64_t rosterRevision=0;
std::vector<ConsoleChannelView> channels;
std::vector<ConsoleWireView> edges; // same accepted graph, not GUI-owned routes
ChannelId main{},monitor{};
const ConsoleChannelView* find(ChannelId id) const noexcept {
const auto it=std::find_if(channels.begin(),channels.end(),
Expand All @@ -55,6 +63,7 @@ class DynamicConsoleRoutingView final {
const auto& rows=roster.channels();
next.channels.reserve(rows.size());
std::unordered_map<std::uint64_t,std::size_t> index;
std::unordered_set<std::uint64_t> edgeIds;
index.reserve(rows.size());
for(const auto& row:rows) {
if(!row.id.value || !index.emplace(row.id.value,next.channels.size()).second)
Expand All @@ -76,6 +85,10 @@ class DynamicConsoleRoutingView final {
for(std::size_t i=0;i<rows.size();++i) {
auto& sender=next.channels[i];
for(const auto& link:rows[i].sends) {
if(!link.edgeId || !edgeIds.insert(link.edgeId).second)
return false; // an ambiguous physical plug is never LIVE
next.edges.push_back({link.edgeId,sender.id,link.destination,
link.gain,link.preFader,link.primaryOutput});
const auto it=index.find(link.destination.value);
if(it==index.end())return false;
auto& destination=next.channels[it->second];
Expand Down
7 changes: 5 additions & 2 deletions engine/core/DynamicNativeSessionPublisher.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class DynamicNativeSessionPublisher final {
std::uint64_t proposedDeviceEpoch,
std::uint64_t activeDeviceEpoch,
bool allOutputsDisarmed,
FxPlan fx={}) {
FxPlan fx={},bool verifiedWireOnly=false) {
CommitResult report;report.revision=publisher_.lastRevision();
if(!fx.valid() ||
(fx.owner && (fx.deviceEpoch!=proposedDeviceEpoch ||
Expand All @@ -83,7 +83,10 @@ class DynamicNativeSessionPublisher final {
expectedRevision==std::numeric_limits<std::uint64_t>::max()) {
report.fault=Fault::staleRevision;return report;
}
if(!allOutputsDisarmed) {
// Only DynamicAcceptedSession may authorize a WIRE-only change
// while armed after checking the physical device patch is unchanged.
// All source/device/roster edits still require output disarm.
if(!allOutputsDisarmed && !verifiedWireOnly) {
report.fault=Fault::outputArmed;return report;
}
auto next=std::make_unique<Publisher::Snapshot>();
Expand Down
19 changes: 19 additions & 0 deletions src/LiveAudioEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -1387,6 +1387,25 @@ class LiveAudioEngine final : private juce::AudioIODeviceCallback
return dryRoutingLab_.setNativePrimaryOutput(
ep,pcmRev,rosterRev,src,dst,true);
}
// Native WIRE mutation on the already installed single ChannelId PCM
// path. No device restart, audio-thread heap work or output disarm;
// fail closed for legacy M4 sources until they are migrated.
LiveDryRoutingLab::NativeBusResult applyNativeWireTransaction(
std::uint64_t ep,std::uint64_t pcmRevision,
std::uint64_t rosterRevision,
std::span<const armix::mixer::WireChange> changes) {
LiveDryRoutingLab::NativeBusResult fail;
fail.error="NATIVE_WIRE_DEVICE_NOT_READY";
if(independent_.active()||!attached_||!directFence_.permitted()||
routeReconfiguring_.load(std::memory_order_acquire)||
diagnosticRoute_.load(std::memory_order_acquire)!=
armix::diagnostics::Route::normal||
handoff_.busy()||
fxMovePhase_.load(std::memory_order_acquire)!=0)
return fail;
return dryRoutingLab_.applyNativeWireTransaction(
ep,pcmRevision,rosterRevision,changes);
}
bool setNativeInputMix(std::uint64_t epoch,std::uint64_t rev,
armix::mixer::ChannelId id,float gain,float pan) noexcept {
if(independent_.active()||!directFence_.permitted()||
Expand Down
Loading
Loading