Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thread safety fixes to address potential crashes and freezes #52

Merged
merged 9 commits into from
Feb 8, 2023
Next Next commit
Fix threading issues in spout_output
  • Loading branch information
y2kcyborg committed Feb 4, 2023
commit 1a15009a27228959f1ed4dd95ec070e202189de3
20 changes: 16 additions & 4 deletions source/win-spout-output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ struct spout_output
bool init_spout(void* data)
{
spout_output* context = (spout_output*)data;

spoututils::SetSpoutLogLevel(spoututils::SPOUT_LOG_VERBOSE);
spoututils::EnableSpoutLog();
context->sender->SetMaxSenders(255);

if (!context->sender->OpenDirectX11()) {
Expand Down Expand Up @@ -100,6 +101,8 @@ bool win_spout_output_start(void* data)
return false;
}

pthread_mutex_lock(&context->mutex);

context->sender->SetSenderName(context->senderName);

int32_t width = (int32_t)obs_output_get_width(context->output);
Expand All @@ -109,12 +112,14 @@ bool win_spout_output_start(void* data)
if (!video)
{
blog(LOG_ERROR, "Trying to start with no video!");
pthread_mutex_unlock(&context->mutex);
return false;
}

if (!obs_output_can_begin_data_capture(context->output, 0))
{
blog(LOG_ERROR, "Unable to begin data capture!");
pthread_mutex_unlock(&context->mutex);
return false;
}

Expand All @@ -135,8 +140,9 @@ bool win_spout_output_start(void* data)
else
blog(LOG_INFO, "Creating capture with name: %s, width: %i, height: %i", context->senderName, width, height);


return context->output_started;
bool started = context->output_started;
pthread_mutex_unlock(&context->mutex);
return started;
}

void win_spout_output_stop(void* data, uint64_t ts)
Expand All @@ -145,28 +151,34 @@ void win_spout_output_stop(void* data, uint64_t ts)

spout_output* context = (spout_output*)data;

pthread_mutex_lock(&context->mutex);

if (context->output_started)
{
context->output_started = false;

obs_output_end_data_capture(context->output);
context->sender->ReleaseSender();
}

pthread_mutex_unlock(&context->mutex);
}

void win_spout_output_rawvideo(void* data, struct video_data* frame)
{
spout_output* context = (spout_output*)data;

pthread_mutex_lock(&context->mutex);

if (!context->output_started)
{
pthread_mutex_unlock(&context->mutex);
return;
}

int32_t width = (int32_t)obs_output_get_width(context->output);
int32_t height = (int32_t)obs_output_get_height(context->output);

pthread_mutex_lock(&context->mutex);
context->sender->SendImage(frame->data[0], width, height);
pthread_mutex_unlock(&context->mutex);
}
Expand Down