Skip to content

Commit

Permalink
Fix race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
xBaank committed Aug 18, 2024
1 parent dccffe8 commit 1064ae3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
58 changes: 49 additions & 9 deletions Console/Audio/PlayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ internal class PlayerController(YoutubeClient youtubeClient, SettingsRepository
: IAsyncDisposable
{
private readonly AsyncLock _lock = new();
private readonly object _listLock = new();
private readonly YoutubeClient _youtubeClient = youtubeClient;
private readonly SettingsRepository _settingsRepository = settingsRepository;
private float _volume = -1f;
private PlayState _state = PlayState.Stopped;
private List<IVideo> _queue = [];
private readonly List<IVideo> _queue = [];
private int _currentSongIndex = 0;
private Matroska? _matroskaPlayerBuffer = null;
private AudioSender? _audioSender = null;
Expand Down Expand Up @@ -86,8 +87,27 @@ await _settingsRepository.SaveSettingsAsync(

public TimeSpan? Time => _audioSender?.CurrentTime;
public TimeSpan? TotalTime => _matroskaPlayerBuffer?.TotalTime ?? Song?.Duration;
public IVideo? Song => _queue.ElementAtOrDefault(_currentSongIndex);
public IReadOnlyCollection<IVideo> Songs => _queue;
public IVideo? Song
{
get
{
lock (_listLock)
{
return _queue.ElementAtOrDefault(_currentSongIndex);
}
}
}

public IReadOnlyCollection<IVideo> Songs
{
get
{
lock (_listLock)
{
return _queue.ToList().AsReadOnly(); // Avoid direct access to _queue
}
}
}
public LoopState LoopState { get; set; }

public async ValueTask DisposeAsync()
Expand Down Expand Up @@ -162,7 +182,11 @@ public async Task SetAsync(
ResetState();
_currentSongIndex = 0;

_queue = [.. videos];
lock (_listLock)
{
_queue.Clear();
_queue.AddRange(videos);
}
QueueChanged?.Invoke(_queue);
}

Expand All @@ -185,8 +209,12 @@ public async Task SetAsync(
.Take(200)
.ToListAsync(cancellationToken: cancellationToken);

_queue = [firstVideo, .. playlist];
_queue = _queue.WhereNotNull().DistinctBy(i => i.Id).ToList(); //Remove duplicate videos
lock (_listLock)
{
_queue.Clear();
IVideo?[] allVideos = [firstVideo, .. playlist];
_queue.AddRange(allVideos.WhereNotNull().DistinctBy(i => i.Id));
}
QueueChanged?.Invoke(_queue);
}

Expand All @@ -199,7 +227,11 @@ public async Task SetAsync(ISearchResult item, CancellationToken cancellationTok

if (item is VideoSearchResult videoSearchResult)
{
_queue = [videoSearchResult];
lock (_listLock)
{
_queue.Clear();
_queue.Add(videoSearchResult);
}
}

if (item is PlaylistSearchResult playlistSearchResult)
Expand All @@ -208,7 +240,11 @@ public async Task SetAsync(ISearchResult item, CancellationToken cancellationTok
.Playlists.GetVideosAsync(playlistSearchResult.Id, cancellationToken)
.ToListAsync<IVideo>(cancellationToken: cancellationToken);

_queue = videos;
lock (_listLock)
{
_queue.Clear();
_queue.AddRange(videos);
}
}

if (item is ChannelSearchResult channelSearchResult)
Expand All @@ -217,7 +253,11 @@ public async Task SetAsync(ISearchResult item, CancellationToken cancellationTok
.Channels.GetUploadsAsync(channelSearchResult.Id, cancellationToken)
.ToListAsync<IVideo>(cancellationToken: cancellationToken);

_queue = videos;
lock (_listLock)
{
_queue.Clear();
_queue.AddRange(videos);
}
}

QueueChanged?.Invoke(_queue);
Expand Down
2 changes: 1 addition & 1 deletion Console/Views/QueueView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public override void HideLoading()

public async Task SavePlaylist()
{
var songs = playerController.Songs.ToList();
var songs = playerController.Songs;

if (songs.Count == 0)
return;
Expand Down

0 comments on commit 1064ae3

Please sign in to comment.