Skip to content

Commit

Permalink
attempt to fix deadlock in stopping stream
Browse files Browse the repository at this point in the history
  • Loading branch information
Galkon committed Sep 11, 2024
1 parent 8b73486 commit 75255e3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ autom4te.cache/*
# Visual Studio 2017/2019 state folder
.vs/

# Visual Studio CMake build output folder
# Visual Studio CMake build output folder
out/

# Visual Studio CMake settings file
Expand All @@ -54,5 +54,8 @@ CMakeSettings.json
# VSCode
.vscode*

# JetBrains
.idea/

# Common build directories of users and VSCode
build*
34 changes: 24 additions & 10 deletions src/hostapi/screencapturekit/pa_mac_screencapturekit.m
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,16 @@ - (void)outputVideoEffectDidStartForStream:(SCStream *)stream {
}

- (void)stream:(SCStream *)stream didStopWithError:(NSError *)error {
fprintf(stderr, "Stream encountered an error\n");
fprintf(stderr, "Stream encountered an error: %s\n", [[error localizedDescription] UTF8String]);

if (self.stream) {
StopStreamInternal((PaStream *)self.stream);
// Mark the stream as having encountered an error to avoid re-entrance issues
self.stream->isStopped = TRUE;

// Dispatch a block to perform the stop operation after returning from this delegate method
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
StopStreamInternal((PaStream *)self.stream);
});
}
}

Expand Down Expand Up @@ -184,21 +191,28 @@ static PaError StopStreamInternal(PaStream *s)
__block PaError result = paNoError;
dispatch_group_t handlerGroup = dispatch_group_create();
dispatch_group_enter(handlerGroup);

// Stop the audio capture session
[stream->audioStream stopCaptureWithCompletionHandler:^(NSError *error) {
if (error)
{
PA_DEBUG(("Failed to stop audio capture: %s\n", [[error localizedDescription] UTF8String]));
result = paInternalError;
}
dispatch_group_leave(handlerGroup);
if (error) {
PA_DEBUG(("Failed to stop audio capture: %s\n", [[error localizedDescription] UTF8String]));
result = paInternalError;
}
dispatch_group_leave(handlerGroup);
}];

stream->isStopped = TRUE;
dispatch_group_wait(handlerGroup, DISPATCH_TIME_FOREVER);

if (dispatch_group_wait(handlerGroup, dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC)) != 0) {
PA_DEBUG(("Timeout occurred while waiting for audio capture to stop.\n"));
result = paInternalError;
}

pthread_mutex_unlock(&stream->stopMutex);
return result;
}


// PortAudio host API stream read function
static PaError ReadStream(PaStream *s, void *buffer, unsigned long frames)
{
Expand Down Expand Up @@ -632,4 +646,4 @@ PaError PaMacScreenCapture_Initialize(PaUtilHostApiRepresentation **hostApi, PaH
Terminate((PaUtilHostApiRepresentation *)paSck);

return result;
}
}

0 comments on commit 75255e3

Please sign in to comment.