Skip to content

Commit

Permalink
Merge pull request #178 from SKProCH/lavalinkPlayerHandleImprovements
Browse files Browse the repository at this point in the history
LavalinkPlayerHandle improvements
  • Loading branch information
angelobreuer authored Sep 1, 2024
2 parents ea71c3c + 7a489bc commit f86256f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
33 changes: 26 additions & 7 deletions src/Lavalink4NET/Players/LavalinkPlayerHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,25 @@ private async ValueTask CompleteAsync(bool isVoiceServerUpdated, CancellationTok
if (_value is TaskCompletionSource<ILavalinkPlayer> taskCompletionSource)
{
// _value is automatically set by CreatePlayerAsync
var player = await CreatePlayerAsync(cancellationToken).ConfigureAwait(false);

taskCompletionSource.TrySetResult(player);

Interlocked.Decrement(ref Diagnostics.PendingHandles);
Interlocked.Increment(ref Diagnostics.ActivePlayers);
try
{
// CreatePlayerAsync can throw if request to lavalink fails
// We should handle this to avoid never completed lavalink player handle
var player = await CreatePlayerAsync(cancellationToken).ConfigureAwait(false);

taskCompletionSource.TrySetResult(player);

Interlocked.Decrement(ref Diagnostics.PendingHandles);
Interlocked.Increment(ref Diagnostics.ActivePlayers);
}
catch (Exception e)
{
// Here we're passing CancellationToken.None to ensure what the player disposing event will properly
// clean up this handle from cache
await _playerContext.LifecycleNotifier!.NotifyDisposeAsync(_guildId, CancellationToken.None);
taskCompletionSource.TrySetException(e);
await DisposeAsync();
}
}
else
{
Expand Down Expand Up @@ -198,7 +211,8 @@ private async ValueTask<TPlayer> CreatePlayerAsync(CancellationToken cancellatio

if (initialTrack.Reference.IsPresent)
{
playerProperties = playerProperties with { TrackData = initialTrack.Track!.ToString(), };
var playableTrack = await initialTrack.Reference.Track.GetPlayableTrackAsync(cancellationToken);
playerProperties = playerProperties with { TrackData = playableTrack.ToString()};
}
else
{
Expand All @@ -210,6 +224,11 @@ private async ValueTask<TPlayer> CreatePlayerAsync(CancellationToken cancellatio
}
}

if (_options.Value.InitialPosition is not null)
{
playerProperties = playerProperties with { Position = _options.Value.InitialPosition.Value };
}

if (_options.Value.InitialVolume is not null)
{
playerProperties = playerProperties with { Volume = _options.Value.InitialVolume.Value, };
Expand Down
8 changes: 6 additions & 2 deletions src/Lavalink4NET/Players/LavalinkPlayerOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Lavalink4NET.Players;
using System;

namespace Lavalink4NET.Players;

using Lavalink4NET.Rest.Entities.Tracks;

Expand All @@ -12,11 +14,13 @@ public record class LavalinkPlayerOptions

public ITrackQueueItem? InitialTrack { get; set; }

public TimeSpan? InitialPosition { get; set; }

public TrackLoadOptions InitialLoadOptions { get; set; }

public float? InitialVolume { get; set; }

public bool SelfDeaf { get; set; }

public bool SelfMute { get; set; }
}
}
5 changes: 2 additions & 3 deletions src/Lavalink4NET/Players/PlayerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,10 @@ async ValueTask IPlayerLifecycleNotifier.NotifyDisposeAsync(ulong guildId, Cance

await using var _ = playerHandle.ConfigureAwait(false);

Debug.Assert(playerHandle.Player is not null);
Debug.Assert(playerHandle.Player is { State: PlayerState.Destroyed, });

// Player can be null if lavalink node will throw while creating player
if (playerHandle.Player is not null)
{
Debug.Assert(playerHandle.Player is { State: PlayerState.Destroyed, });
var eventArgs = new PlayerDestroyedEventArgs(playerHandle.Player);

await PlayerDestroyed
Expand Down

0 comments on commit f86256f

Please sign in to comment.