Allow external VoiceChannel client API implementation (#1057)

* ConnectAsync(bool external) API implemented

This allows developers to handle the AudioClient externally (for example with Lavalink)

* Modify ConnectAsync API and add DisconnectAsync to IAudioChannel

* Update summary message on IAudioChannel.DisconnectAsync()

* Review changes:
- Fix `if (!external)` styling
- Remove unecessary ConnectAsync overload

* SocketVoiceChannel overload update

* Update IAudioChannel.ConnectAsync()
- Remove ConfigAction from parameters
- Add SelfDeafen/SelfMute to parameters

* Remove SocketVoiceChannel.ConnectAsync() default params (Inherit)
This commit is contained in:
NovusTheory
2018-05-31 16:21:57 -05:00
committed by Finite Reality
parent 3acf2a9a6b
commit 890904f32c
6 changed files with 32 additions and 15 deletions

View File

@@ -1,4 +1,4 @@
using Discord.Audio; using Discord.Audio;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -7,6 +7,9 @@ namespace Discord
public interface IAudioChannel : IChannel public interface IAudioChannel : IChannel
{ {
/// <summary> Connects to this audio channel. </summary> /// <summary> Connects to this audio channel. </summary>
Task<IAudioClient> ConnectAsync(Action<IAudioClient> configAction = null); Task<IAudioClient> ConnectAsync(bool selfDeaf = false, bool selfMute = false, bool external = false);
/// <summary> Disconnects from this audio channel. </summary>
Task DisconnectAsync();
} }
} }

View File

@@ -147,7 +147,8 @@ namespace Discord.Rest
=> EnterTypingState(options); => EnterTypingState(options);
//IAudioChannel //IAudioChannel
Task<IAudioClient> IAudioChannel.ConnectAsync(Action<IAudioClient> configAction) { throw new NotSupportedException(); } Task<IAudioClient> IAudioChannel.ConnectAsync(bool selfDeaf, bool selfMute, bool external) { throw new NotSupportedException(); }
Task IAudioChannel.DisconnectAsync() { throw new NotSupportedException(); }
//IChannel //IChannel
Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options) Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)

View File

@@ -45,7 +45,8 @@ namespace Discord.Rest
private string DebuggerDisplay => $"{Name} ({Id}, Voice)"; private string DebuggerDisplay => $"{Name} ({Id}, Voice)";
//IAudioChannel //IAudioChannel
Task<IAudioClient> IAudioChannel.ConnectAsync(Action<IAudioClient> configAction) { throw new NotSupportedException(); } Task<IAudioClient> IAudioChannel.ConnectAsync(bool selfDeaf, bool selfMute, bool external) { throw new NotSupportedException(); }
Task IAudioChannel.DisconnectAsync() { throw new NotSupportedException(); }
//IGuildChannel //IGuildChannel
Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options) Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)

View File

@@ -211,7 +211,8 @@ namespace Discord.WebSocket
=> EnterTypingState(options); => EnterTypingState(options);
//IAudioChannel //IAudioChannel
Task<IAudioClient> IAudioChannel.ConnectAsync(Action<IAudioClient> configAction) { throw new NotSupportedException(); } Task<IAudioClient> IAudioChannel.ConnectAsync(bool selfDeaf, bool selfMute, bool external) { throw new NotSupportedException(); }
Task IAudioChannel.DisconnectAsync() { throw new NotSupportedException(); }
//IChannel //IChannel
Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options) Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)

View File

@@ -43,11 +43,14 @@ namespace Discord.WebSocket
public Task ModifyAsync(Action<VoiceChannelProperties> func, RequestOptions options = null) public Task ModifyAsync(Action<VoiceChannelProperties> func, RequestOptions options = null)
=> ChannelHelper.ModifyAsync(this, Discord, func, options); => ChannelHelper.ModifyAsync(this, Discord, func, options);
public async Task<IAudioClient> ConnectAsync(Action<IAudioClient> configAction = null) public async Task<IAudioClient> ConnectAsync(bool selfDeaf, bool selfMute, bool external)
{ {
return await Guild.ConnectAudioAsync(Id, false, false, configAction).ConfigureAwait(false); return await Guild.ConnectAudioAsync(Id, selfDeaf, selfMute, external).ConfigureAwait(false);
} }
public async Task DisconnectAsync()
=> await Guild.DisconnectAudioAsync();
public override SocketGuildUser GetUser(ulong id) public override SocketGuildUser GetUser(ulong id)
{ {
var user = Guild.GetUser(id); var user = Guild.GetUser(id);

View File

@@ -508,11 +508,8 @@ namespace Discord.WebSocket
{ {
return _audioClient?.GetInputStream(userId); return _audioClient?.GetInputStream(userId);
} }
internal async Task<IAudioClient> ConnectAudioAsync(ulong channelId, bool selfDeaf, bool selfMute, Action<IAudioClient> configAction) internal async Task<IAudioClient> ConnectAudioAsync(ulong channelId, bool selfDeaf, bool selfMute, bool external)
{ {
selfDeaf = false;
selfMute = false;
TaskCompletionSource<AudioClient> promise; TaskCompletionSource<AudioClient> promise;
await _audioLock.WaitAsync().ConfigureAwait(false); await _audioLock.WaitAsync().ConfigureAwait(false);
@@ -522,6 +519,13 @@ namespace Discord.WebSocket
promise = new TaskCompletionSource<AudioClient>(); promise = new TaskCompletionSource<AudioClient>();
_audioConnectPromise = promise; _audioConnectPromise = promise;
if (external)
{
var _ = promise.TrySetResultAsync(null);
await Discord.ApiClient.SendVoiceStateUpdateAsync(Id, channelId, selfDeaf, selfMute).ConfigureAwait(false);
return null;
}
if (_audioClient == null) if (_audioClient == null)
{ {
var audioClient = new AudioClient(this, Discord.GetAudioId(), channelId); var audioClient = new AudioClient(this, Discord.GetAudioId(), channelId);
@@ -529,7 +533,9 @@ namespace Discord.WebSocket
{ {
if (!promise.Task.IsCompleted) if (!promise.Task.IsCompleted)
{ {
try { audioClient.Dispose(); } catch { } try
{ audioClient.Dispose(); }
catch { }
_audioClient = null; _audioClient = null;
if (ex != null) if (ex != null)
await promise.TrySetExceptionAsync(ex); await promise.TrySetExceptionAsync(ex);
@@ -543,7 +549,6 @@ namespace Discord.WebSocket
var _ = promise.TrySetResultAsync(_audioClient); var _ = promise.TrySetResultAsync(_audioClient);
return Task.Delay(0); return Task.Delay(0);
}; };
configAction?.Invoke(audioClient);
_audioClient = audioClient; _audioClient = audioClient;
} }
@@ -601,10 +606,13 @@ namespace Discord.WebSocket
await _audioLock.WaitAsync().ConfigureAwait(false); await _audioLock.WaitAsync().ConfigureAwait(false);
try try
{
if (_audioClient != null)
{ {
await RepopulateAudioStreamsAsync().ConfigureAwait(false); await RepopulateAudioStreamsAsync().ConfigureAwait(false);
await _audioClient.StartAsync(url, Discord.CurrentUser.Id, voiceState.VoiceSessionId, token).ConfigureAwait(false); await _audioClient.StartAsync(url, Discord.CurrentUser.Id, voiceState.VoiceSessionId, token).ConfigureAwait(false);
} }
}
catch (OperationCanceledException) catch (OperationCanceledException)
{ {
await DisconnectAudioInternalAsync().ConfigureAwait(false); await DisconnectAudioInternalAsync().ConfigureAwait(false);