From eaa73bc52f43881b4f4dd28c8c4d4d0abe68d410 Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Mon, 30 May 2022 23:59:42 -0700 Subject: [PATCH] [Threading] Queue expensive actions outside audio Use delayed dispatching on expensive operations which occur every time the "heard" track end happens, and when audio metadata is changed by a stream, so that they are inserted into the main thread queue after the invoking callback returns control execution back to the audio thread. Signed-off-by: Christopher Snowhill --- Application/PlaybackController.m | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/Application/PlaybackController.m b/Application/PlaybackController.m index ceb693e09..1e0d3073b 100644 --- a/Application/PlaybackController.m +++ b/Application/PlaybackController.m @@ -580,16 +580,19 @@ NSDictionary *makeRGInfo(PlaylistEntry *pe) { - (void)audioPlayer:(AudioPlayer *)player didBeginStream:(id)userInfo { PlaylistEntry *pe = (PlaylistEntry *)userInfo; - [playlistController setCurrentEntry:pe]; + // Delay the action until this function has returned to the audio thread + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_MSEC), dispatch_get_main_queue(), ^{ + [self->playlistController setCurrentEntry:pe]; - if(_eq) - equalizerApplyGenre(_eq, [pe genre]); + if(self->_eq) + equalizerApplyGenre(self->_eq, [pe genre]); - lastPosition = -10; + self->lastPosition = -10; - [self setPosition:0]; + [self setPosition:0]; - [self removeHDCD:nil]; + [self removeHDCD:nil]; + }); [[NSNotificationCenter defaultCenter] postNotificationName:CogPlaybackDidBeginNotficiation object:pe]; } @@ -677,7 +680,10 @@ NSDictionary *makeRGInfo(PlaylistEntry *pe) { if (!pe) pe = [playlistController currentEntry]; [pe setMetadata:info]; [playlistView refreshCurrentTrack:self]; - [self sendMetaData]; + // Delay the action until this function has returned to the audio thread + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_MSEC), dispatch_get_main_queue(), ^{ + [self sendMetaData]; + }); [[NSNotificationCenter defaultCenter] postNotificationName:CogPlaybackDidBeginNotficiation object:pe]; }