Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LavalinkPlayerHandle improvements #178

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading