Skip to content

Commit

Permalink
Merge pull request #80 from wtsnz/fix/eager-reconnections
Browse files Browse the repository at this point in the history
Fix issue where switching scenes would cause reconnects
  • Loading branch information
wtsnz committed May 11, 2022
2 parents cdf0423 + 9291ee3 commit 1826d59
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
windows:
name: "Windows 32/64-bit"
environment: production
runs-on: [windows-latest]
runs-on: [windows-2019]
if: contains(github.event.head_commit.message, '[skip ci]') != true
env:
QT_CACHE_VERSION: "2" # Change whenever updating OBS dependencies URL, in order to force a cache reset
Expand Down
2 changes: 1 addition & 1 deletion src/obs-ios-camera-plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
OBS_DECLARE_MODULE()
OBS_MODULE_USE_DEFAULT_LOCALE("obs-ios-camera-plugin", "en-US")

#define IOS_CAMERA_PLUGIN_VERSION "2.9.0"
#define IOS_CAMERA_PLUGIN_VERSION "2.9.1"

extern void RegisterIOSCameraSource();

Expand Down
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 1826d59

Please sign in to comment.