Started adding audio receive

This commit is contained in:
RogueException
2017-03-14 03:14:57 -03:00
parent 1d5b7a2b01
commit 254c83ecff
12 changed files with 216 additions and 76 deletions

View File

@@ -42,13 +42,7 @@ namespace Discord.WebSocket
public async Task<IAudioClient> ConnectAsync()
{
var audioMode = Discord.AudioMode;
if (audioMode == AudioMode.Disabled)
throw new InvalidOperationException($"Audio is not enabled on this client, {nameof(DiscordSocketConfig.AudioMode)} in {nameof(DiscordSocketConfig)} must be set.");
return await Guild.ConnectAudioAsync(Id,
(audioMode & AudioMode.Incoming) == 0,
(audioMode & AudioMode.Outgoing) == 0).ConfigureAwait(false);
return await Guild.ConnectAudioAsync(Id, false, false).ConfigureAwait(false);
}
public override SocketGuildUser GetUser(ulong id)

View File

@@ -426,9 +426,23 @@ namespace Discord.WebSocket
internal SocketVoiceState AddOrUpdateVoiceState(ClientState state, VoiceStateModel model)
{
var voiceChannel = state.GetChannel(model.ChannelId.Value) as SocketVoiceChannel;
var voiceState = SocketVoiceState.Create(voiceChannel, model);
_voiceStates[model.UserId] = voiceState;
return voiceState;
var before = GetVoiceState(model.UserId) ?? SocketVoiceState.Default;
var after = SocketVoiceState.Create(voiceChannel, model);
_voiceStates[model.UserId] = after;
if (before.VoiceChannel?.Id != after.VoiceChannel?.Id)
{
if (model.UserId == CurrentUser.Id)
RepopulateAudioStreams();
else
{
_audioClient?.RemoveInputStream(model.UserId); //User changed channels, end their stream
if (CurrentUser.VoiceChannel != null && after.VoiceChannel?.Id == CurrentUser.VoiceChannel?.Id)
_audioClient.CreateInputStream(model.UserId);
}
}
return after;
}
internal SocketVoiceState? GetVoiceState(ulong id)
{
@@ -446,6 +460,10 @@ namespace Discord.WebSocket
}
//Audio
internal AudioInStream GetAudioStream(ulong userId)
{
return _audioClient?.GetInputStream(userId);
}
internal async Task<IAudioClient> ConnectAudioAsync(ulong channelId, bool selfDeaf, bool selfMute)
{
selfDeaf = false;
@@ -531,6 +549,7 @@ namespace Discord.WebSocket
}
};
_audioClient = audioClient;
RepopulateAudioStreams();
}
_audioClient.Connected += () =>
{
@@ -554,6 +573,22 @@ namespace Discord.WebSocket
}
}
internal void RepopulateAudioStreams()
{
if (_audioClient != null)
{
_audioClient.ClearInputStreams(); //We changed channels, end all current streams
if (CurrentUser.VoiceChannel != null)
{
foreach (var pair in _voiceStates)
{
if (pair.Value.VoiceChannel?.Id == CurrentUser.VoiceChannel?.Id)
_audioClient.CreateInputStream(pair.Key);
}
}
}
}
public override string ToString() => Name;
private string DebuggerDisplay => $"{Name} ({Id})";
internal SocketGuild Clone() => MemberwiseClone() as SocketGuild;

View File

@@ -1,4 +1,5 @@
using Discord.Rest;
using Discord.Audio;
using Discord.Rest;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
@@ -37,6 +38,7 @@ namespace Discord.WebSocket
public SocketVoiceChannel VoiceChannel => VoiceState?.VoiceChannel;
public string VoiceSessionId => VoiceState?.VoiceSessionId ?? "";
public SocketVoiceState? VoiceState => Guild.GetVoiceState(Id);
public AudioInStream AudioStream => Guild.GetAudioStream(Id);
/// <summary> The position of the user within the role hirearchy. </summary>
/// <remarks> The returned value equal to the position of the highest role the user has,