Skip to content

Commit

Permalink
Fix issue where switching scenes would cause reconnects
Browse files Browse the repository at this point in the history
This would happen regardless of whether the device or plugin were in the next scene. If there was an iOS Camera input that was duplicated in two scenes, then the plugin would disconnect and reconnect during a scene switch. This occasionally caused issues where the device would fail to reconnect.
  • Loading branch information
wtsnz committed Mar 15, 2022
1 parent cdf0423 commit 26a88b8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 37 deletions.
83 changes: 47 additions & 36 deletions src/obs-ios-camera-source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ void IOSCameraInput::deviceManagerDidUpdateDeviceList(
// User will have to configure the plugin manually when more than one device is plugged in
// due to the fact that multiple instances of the plugin can't subscribe to device events...

connectToDevice();
connectToDevice(false);
}
}

Expand Down Expand Up @@ -184,15 +184,15 @@ void IOSCameraInput::activate()
blog(LOG_INFO, "Activating");
active = true;

connectToDevice();
connectToDevice(false);
}

void IOSCameraInput::deactivate()
{
blog(LOG_INFO, "Deactivating");
active = false;

connectToDevice();
connectToDevice(false);
}

void IOSCameraInput::loadSettings(obs_data_t *settings)
Expand All @@ -218,12 +218,12 @@ void IOSCameraInput::setDeviceUUID(std::string uuid)
state.selectedDeviceUUID = uuid;
}

connectToDevice();
connectToDevice(false);
}

void IOSCameraInput::reconnectToDevice()
{
connectToDevice();
connectToDevice(true);
}

void IOSCameraInput::resetDecoder()
Expand All @@ -238,7 +238,7 @@ void IOSCameraInput::resetDecoder()
obs_source_output_video(source, NULL);
}

void IOSCameraInput::connectToDevice()
void IOSCameraInput::connectToDevice(bool force)
{
blog(LOG_DEBUG, "Connecting to device: %s",
state.selectedDeviceUUID.value_or("none").c_str());
Expand All @@ -259,36 +259,47 @@ void IOSCameraInput::connectToDevice()
// Clear the video frame when a setting changes
resetDecoder();
return;
}

// https://stackoverflow.com/questions/44217316/how-do-i-use-stdoptional-in-c
// Apple compiler hasn't implemented std::optional.value() in < macos 10.14,
// work around this by fetching the value by * method.
std::string selectedUUID = *state.selectedDeviceUUID;

// Disconnect all connection controllers
for (const auto &[uuid, connectionController] : connectionControllers) {
if (connectionController != nullptr) {
connectionController->disconnect();
}
}

if (isConnectingToDifferentDevice) {
resetDecoder();
}

auto shouldConnect = !(disconnectOnInactive == true && active == false);

// Then connect to the selected device if the plugin is active, or inactive and connected on inactive.
for (const auto &[uuid, connectionController] : connectionControllers) {
if (connectionController != nullptr) {
if (uuid == selectedUUID && shouldConnect) {
blog(LOG_DEBUG,
"Starting connection controller");
connectionController->start();
}
}
}
}
// Else there is a selected device
else {

// https://stackoverflow.com/questions/44217316/how-do-i-use-stdoptional-in-c
// Apple compiler hasn't implemented std::optional.value() in < macos 10.14,
// work around this by fetching the value by * method.
std::string selectedUUID = *state.selectedDeviceUUID;

if (isConnectingToDifferentDevice || force) {
// Disconnect to all connection controllers if we're switching the active connection
for (const auto &[uuid, connectionController] : connectionControllers) {
if (connectionController != nullptr) {
connectionController->disconnect();
}
}

resetDecoder();
}

// Connect
auto shouldConnect = !(disconnectOnInactive == true && active == false);

// Then connect to the selected device if the plugin is active, or inactive and connected on inactive.
for (const auto &[uuid, connectionController] : connectionControllers) {
if (connectionController != nullptr) {
if (uuid == selectedUUID) {

if (shouldConnect) {
blog(LOG_DEBUG,
"Starting connection controller");
connectionController->start();

} else {
// Disconnect on Inactive is enabled, which causes the device to disconnect when not in an active scene.
connectionController->disconnect();
}
}
}
}
}
}

#pragma mark - Settings Config
Expand Down
2 changes: 1 addition & 1 deletion src/obs-ios-camera-source.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class IOSCameraInput : public portal::DeviceManager::Delegate,
void setDeviceUUID(std::string uuid);
void reconnectToDevice();
void resetDecoder();
void connectToDevice();
void connectToDevice(bool force);

struct MobileCameraDevice {
std::string uuid;
Expand Down

0 comments on commit 26a88b8

Please sign in to comment.