Skip to content

Commit

Permalink
Add protection from adding/removing device twice
Browse files Browse the repository at this point in the history
If Plugin::AddDevice() or RemoveDevice() is called
twice for same device, the second call is now ignored.
  • Loading branch information
gavv committed May 4, 2024
1 parent 2f2ad68 commit ed8328d
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/Plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,27 @@ void Plugin::AddDevice(std::shared_ptr<Device> device)

GetContext()->Tracer->OperationBegin(op);

if (auto devOwner = device->GetOwnerID(); devOwner != kAudioObjectUnknown) {
if (devOwner == GetID()) {
// Protection from adding same device twice.
GetContext()->Tracer->Message(
"Plugin::AddDevice() device already added"
" devID=%lu pluginID=%lu",
static_cast<unsigned long>(device->GetID()),
static_cast<unsigned long>(GetID()));
} else {
// This is likely a bug in user code. Only Plugin can be
// Device owner, and there is only one plugin.
GetContext()->Tracer->Message(
"Plugin::AddDevice() unexpected device owner"
" devID=%lu devOwnerID=%lu pluginID=%lu",
static_cast<unsigned long>(device->GetID()),
static_cast<unsigned long>(device->GetOwnerID()),
static_cast<unsigned long>(GetID()));
}
goto end;
}

{
auto devices = devices_.Get();

Expand Down Expand Up @@ -117,6 +138,7 @@ void Plugin::AddDevice(std::shared_ptr<Device> device)
NotifyPropertiesChanged(
{kAudioObjectPropertyOwnedObjects, kAudioPlugInPropertyDeviceList});

end:
GetContext()->Tracer->OperationEnd(op, kAudioHardwareNoError);
}

Expand All @@ -130,6 +152,16 @@ void Plugin::RemoveDevice(std::shared_ptr<Device> device)

GetContext()->Tracer->OperationBegin(op);

if (device->GetOwnerID() == kAudioObjectUnknown) {
// Protection from removing same device twice.
GetContext()->Tracer->Message(
"Plugin::RemoveDevice() device already removed"
" devID=%lu pluginID=%lu",
static_cast<unsigned long>(device->GetID()),
static_cast<unsigned long>(GetID()));
goto end;
}

{
auto devices = devices_.Get();

Expand Down Expand Up @@ -163,6 +195,7 @@ void Plugin::RemoveDevice(std::shared_ptr<Device> device)
NotifyPropertiesChanged(
{kAudioObjectPropertyOwnedObjects, kAudioPlugInPropertyDeviceList});

end:
GetContext()->Tracer->OperationEnd(op, kAudioHardwareNoError);
}

Expand Down

0 comments on commit ed8328d

Please sign in to comment.