Skip to content

Commit 5b8bbed

Browse files
authored
Merge pull request #626 from stream-labs/startup-audio-crash
Fixed startup crash and improved synchronization
2 parents 0ceb0df + b2d788c commit 5b8bbed

File tree

1 file changed

+31
-13
lines changed

1 file changed

+31
-13
lines changed

libobs/obs.c

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,16 @@ static int obs_init_video()
764764
obs_free_graphics();
765765
return errorcode;
766766
}
767+
768+
// As graphics is initialized only once, here is the perfect place to init mutexes
769+
if (pthread_mutex_init(&video->task_mutex, NULL) < 0)
770+
return OBS_VIDEO_FAIL;
771+
if (pthread_mutex_init(&video->encoder_group_mutex, NULL) < 0)
772+
return OBS_VIDEO_FAIL;
773+
if (pthread_mutex_init(&video->mixes_mutex, NULL) < 0)
774+
return OBS_VIDEO_FAIL;
775+
if (pthread_mutex_init(&video->canvases_mutex, NULL) < 0)
776+
return OBS_VIDEO_FAIL;
767777
}
768778

769779
blog(LOG_INFO, "[VIDEO_CANVAS] wait obs_graphics_thread to stop");
@@ -788,12 +798,6 @@ static int obs_init_video()
788798
video->video_half_frame_interval_ns =
789799
util_mul_div64(500000000ULL, max_fps_den, max_fps_num);
790800

791-
if (pthread_mutex_init(&video->task_mutex, NULL) < 0)
792-
return OBS_VIDEO_FAIL;
793-
if (pthread_mutex_init(&video->encoder_group_mutex, NULL) < 0)
794-
return OBS_VIDEO_FAIL;
795-
if (pthread_mutex_init(&video->mixes_mutex, NULL) < 0)
796-
return OBS_VIDEO_FAIL;
797801
blog(LOG_INFO, "[VIDEO_CANVAS] init with canvases %zu",
798802
obs->video.canvases.num);
799803
for (size_t i = 0, num = obs->video.canvases.num; i < num; i++) {
@@ -924,6 +928,12 @@ void obs_free_video_mix(struct obs_core_video_mix *video)
924928

925929
static void obs_free_video(bool full_clean)
926930
{
931+
if (!obs->video.graphics) {
932+
blog(LOG_INFO,
933+
"obs_free_video - video is not initialized yet, skipping");
934+
return;
935+
}
936+
927937
pthread_mutex_lock(&obs->video.mixes_mutex);
928938
size_t num_views = 0;
929939
for (size_t i = 0; i < obs->video.mixes.num; i++) {
@@ -940,19 +950,22 @@ static void obs_free_video(bool full_clean)
940950
obs->video.main_mix = NULL;
941951
pthread_mutex_unlock(&obs->video.mixes_mutex);
942952

943-
pthread_mutex_destroy(&obs->video.mixes_mutex);
944-
pthread_mutex_init_value(&obs->video.mixes_mutex);
953+
if (full_clean) {
954+
pthread_mutex_destroy(&obs->video.mixes_mutex);
955+
pthread_mutex_init_value(&obs->video.mixes_mutex);
956+
}
945957

946958
for (size_t i = 0; i < obs->video.ready_encoder_groups.num; i++) {
947959
obs_weak_encoder_release(
948960
obs->video.ready_encoder_groups.array[i]);
949961
}
950962
da_free(obs->video.ready_encoder_groups);
951963

952-
pthread_mutex_destroy(&obs->video.encoder_group_mutex);
953-
pthread_mutex_init_value(&obs->video.encoder_group_mutex);
954-
955964
if (full_clean) {
965+
pthread_mutex_destroy(&obs->video.encoder_group_mutex);
966+
pthread_mutex_init_value(&obs->video.encoder_group_mutex);
967+
968+
pthread_mutex_lock(&obs->video.canvases_mutex);
956969
size_t num = obs->video.canvases.num;
957970
for (size_t i = 0; i < num; i++) {
958971
bfree(obs->video.canvases.array[i]);
@@ -964,9 +977,14 @@ static void obs_free_video(bool full_clean)
964977
pthread_mutex_init_value(&obs->video.canvases_mutex);
965978
}
966979

967-
pthread_mutex_destroy(&obs->video.task_mutex);
968-
pthread_mutex_init_value(&obs->video.task_mutex);
980+
pthread_mutex_lock(&obs->video.task_mutex);
969981
deque_free(&obs->video.tasks);
982+
pthread_mutex_unlock(&obs->video.task_mutex);
983+
984+
if (full_clean) {
985+
pthread_mutex_destroy(&obs->video.task_mutex);
986+
pthread_mutex_init_value(&obs->video.task_mutex);
987+
}
970988
}
971989

972990
static void obs_free_graphics(void)

0 commit comments

Comments
 (0)