Started adding audio receive
This commit is contained in:
@@ -17,6 +17,18 @@ namespace Discord.Audio
|
||||
//TODO: Add audio reconnecting
|
||||
internal class AudioClient : IAudioClient, IDisposable
|
||||
{
|
||||
internal struct StreamPair
|
||||
{
|
||||
public AudioInStream Reader;
|
||||
public AudioOutStream Writer;
|
||||
|
||||
public StreamPair(AudioInStream reader, AudioOutStream writer)
|
||||
{
|
||||
Reader = reader;
|
||||
Writer = writer;
|
||||
}
|
||||
}
|
||||
|
||||
public event Func<Task> Connected
|
||||
{
|
||||
add { _connectedEvent.Add(value); }
|
||||
@@ -41,6 +53,8 @@ namespace Discord.Audio
|
||||
private readonly ConnectionManager _connection;
|
||||
private readonly SemaphoreSlim _stateLock;
|
||||
private readonly ConcurrentQueue<long> _heartbeatTimes;
|
||||
private readonly ConcurrentDictionary<uint, ulong> _ssrcMap;
|
||||
private readonly ConcurrentDictionary<ulong, StreamPair> _streams;
|
||||
|
||||
private Task _heartbeatTask;
|
||||
private long _lastMessageTime;
|
||||
@@ -75,6 +89,8 @@ namespace Discord.Audio
|
||||
_connection.Connected += () => _connectedEvent.InvokeAsync();
|
||||
_connection.Disconnected += (ex, recon) => _disconnectedEvent.InvokeAsync(ex);
|
||||
_heartbeatTimes = new ConcurrentQueue<long>();
|
||||
_ssrcMap = new ConcurrentDictionary<uint, ulong>();
|
||||
_streams = new ConcurrentDictionary<ulong, StreamPair>();
|
||||
|
||||
_serializer = new JsonSerializer { ContractResolver = new DiscordContractResolver() };
|
||||
_serializer.Error += (s, e) =>
|
||||
@@ -166,6 +182,35 @@ namespace Discord.Audio
|
||||
throw new ArgumentException("Value must be 120, 240, 480, 960, 1920 or 2880", nameof(samplesPerFrame));
|
||||
}
|
||||
|
||||
internal void CreateInputStream(ulong userId)
|
||||
{
|
||||
//Assume Thread-safe
|
||||
if (!_streams.ContainsKey(userId))
|
||||
{
|
||||
var readerStream = new InputStream();
|
||||
var writerStream = new OpusDecodeStream(new RTPReadStream(readerStream, _secretKey));
|
||||
_streams.TryAdd(userId, new StreamPair(readerStream, writerStream));
|
||||
}
|
||||
}
|
||||
internal AudioInStream GetInputStream(ulong id)
|
||||
{
|
||||
StreamPair streamPair;
|
||||
if (_streams.TryGetValue(id, out streamPair))
|
||||
return streamPair.Reader;
|
||||
return null;
|
||||
}
|
||||
internal void RemoveInputStream(ulong userId)
|
||||
{
|
||||
_streams.TryRemove(userId, out var ignored);
|
||||
}
|
||||
internal void ClearInputStreams()
|
||||
{
|
||||
foreach (var pair in _streams.Values)
|
||||
pair.Reader.Dispose();
|
||||
_ssrcMap.Clear();
|
||||
_streams.Clear();
|
||||
}
|
||||
|
||||
private async Task ProcessMessageAsync(VoiceOpCode opCode, object payload)
|
||||
{
|
||||
_lastMessageTime = Environment.TickCount;
|
||||
@@ -219,6 +264,14 @@ namespace Discord.Audio
|
||||
}
|
||||
}
|
||||
break;
|
||||
case VoiceOpCode.Speaking:
|
||||
{
|
||||
await _audioLogger.DebugAsync("Received Speaking").ConfigureAwait(false);
|
||||
|
||||
var data = (payload as JToken).ToObject<SpeakingEvent>(_serializer);
|
||||
_ssrcMap[data.Ssrc] = data.UserId; //TODO: Memory Leak: SSRCs are never cleaned up
|
||||
}
|
||||
break;
|
||||
default:
|
||||
await _audioLogger.WarningAsync($"Unknown OpCode ({opCode})").ConfigureAwait(false);
|
||||
return;
|
||||
@@ -234,19 +287,56 @@ namespace Discord.Audio
|
||||
{
|
||||
if (!_connection.IsCompleted)
|
||||
{
|
||||
if (packet.Length == 70)
|
||||
if (packet.Length != 70)
|
||||
{
|
||||
string ip;
|
||||
int port;
|
||||
try
|
||||
{
|
||||
ip = Encoding.UTF8.GetString(packet, 4, 70 - 6).TrimEnd('\0');
|
||||
port = packet[69] | (packet[68] << 8);
|
||||
}
|
||||
catch { return; }
|
||||
|
||||
await _audioLogger.DebugAsync("Received Discovery").ConfigureAwait(false);
|
||||
await ApiClient.SendSelectProtocol(ip, port).ConfigureAwait(false);
|
||||
await _audioLogger.DebugAsync($"Malformed Packet").ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
string ip;
|
||||
int port;
|
||||
try
|
||||
{
|
||||
ip = Encoding.UTF8.GetString(packet, 4, 70 - 6).TrimEnd('\0');
|
||||
port = packet[69] | (packet[68] << 8);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await _audioLogger.DebugAsync($"Malformed Packet", ex).ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
|
||||
await _audioLogger.DebugAsync("Received Discovery").ConfigureAwait(false);
|
||||
await ApiClient.SendSelectProtocol(ip, port).ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
uint ssrc;
|
||||
ulong userId;
|
||||
StreamPair pair;
|
||||
|
||||
if (!RTPReadStream.TryReadSsrc(packet, 0, out ssrc))
|
||||
{
|
||||
await _audioLogger.DebugAsync($"Malformed Frame").ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
if (!_ssrcMap.TryGetValue(ssrc, out userId))
|
||||
{
|
||||
await _audioLogger.DebugAsync($"Unknown SSRC {ssrc}").ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
if (!_streams.TryGetValue(userId, out pair))
|
||||
{
|
||||
await _audioLogger.DebugAsync($"Unknown User {userId}").ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
await pair.Writer.WriteAsync(packet, 0, packet.Length).ConfigureAwait(false);
|
||||
await _audioLogger.DebugAsync($"Received {packet.Length} bytes from user {userId}").ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await _audioLogger.DebugAsync($"Malformed Frame", ex).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace Discord.Audio
|
||||
{
|
||||
[Flags]
|
||||
public enum AudioMode : byte
|
||||
{
|
||||
Disabled = 0,
|
||||
Outgoing = 1,
|
||||
Incoming = 2,
|
||||
Both = Outgoing | Incoming
|
||||
}
|
||||
}
|
||||
@@ -59,9 +59,6 @@ namespace Discord.Audio.Streams
|
||||
{
|
||||
return Task.Run(async () =>
|
||||
{
|
||||
#if DEBUG
|
||||
uint num = 0;
|
||||
#endif
|
||||
try
|
||||
{
|
||||
while (!_isPreloaded && !_cancelToken.IsCancellationRequested)
|
||||
@@ -82,7 +79,7 @@ namespace Discord.Audio.Streams
|
||||
_queueLock.Release();
|
||||
nextTick += _ticksPerFrame;
|
||||
#if DEBUG
|
||||
var _ = _logger.DebugAsync($"{num++}: Sent {frame.Bytes} bytes ({_queuedFrames.Count} frames buffered)");
|
||||
var _ = _logger.DebugAsync($"Sent {frame.Bytes} bytes ({_queuedFrames.Count} frames buffered)");
|
||||
#endif
|
||||
}
|
||||
else
|
||||
@@ -93,7 +90,7 @@ namespace Discord.Audio.Streams
|
||||
nextTick += _ticksPerFrame;
|
||||
}
|
||||
#if DEBUG
|
||||
var _ = _logger.DebugAsync($"{num++}: Buffer underrun");
|
||||
var _ = _logger.DebugAsync($"Buffer underrun");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,12 +12,13 @@ namespace Discord.Audio.Streams
|
||||
private ushort _nextSeq;
|
||||
private uint _nextTimestamp;
|
||||
private bool _hasHeader;
|
||||
private bool _isDisposed;
|
||||
|
||||
public override bool CanRead => true;
|
||||
public override bool CanRead => !_isDisposed;
|
||||
public override bool CanSeek => false;
|
||||
public override bool CanWrite => true;
|
||||
public override bool CanWrite => false;
|
||||
|
||||
public InputStream(byte[] secretKey)
|
||||
public InputStream()
|
||||
{
|
||||
_frames = new ConcurrentQueue<RTPFrame>();
|
||||
}
|
||||
@@ -54,10 +55,13 @@ namespace Discord.Audio.Streams
|
||||
{
|
||||
cancelToken.ThrowIfCancellationRequested();
|
||||
|
||||
if (_frames.Count > 1000)
|
||||
if (_frames.Count > 100) //1-2 seconds
|
||||
{
|
||||
_hasHeader = false;
|
||||
return Task.Delay(0); //Buffer overloaded
|
||||
if (_hasHeader)
|
||||
throw new InvalidOperationException("Received payload with an RTP header");
|
||||
}
|
||||
if (!_hasHeader)
|
||||
throw new InvalidOperationException("Received payload without an RTP header");
|
||||
byte[] payload = new byte[count];
|
||||
Buffer.BlockCopy(buffer, offset, payload, 0, count);
|
||||
|
||||
@@ -69,5 +73,10 @@ namespace Discord.Audio.Streams
|
||||
_hasHeader = false;
|
||||
return Task.Delay(0);
|
||||
}
|
||||
|
||||
protected override void Dispose(bool isDisposing)
|
||||
{
|
||||
_isDisposed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,15 +6,17 @@ namespace Discord.Audio.Streams
|
||||
///<summary> Converts Opus to PCM </summary>
|
||||
public class OpusDecodeStream : AudioOutStream
|
||||
{
|
||||
public const int SampleRate = OpusEncodeStream.SampleRate;
|
||||
|
||||
private readonly AudioOutStream _next;
|
||||
private readonly byte[] _buffer;
|
||||
private readonly OpusDecoder _decoder;
|
||||
|
||||
public OpusDecodeStream(AudioOutStream next, int samplingRate, int channels = OpusConverter.MaxChannels, int bufferSize = 4000)
|
||||
public OpusDecodeStream(AudioOutStream next, int channels = OpusConverter.MaxChannels, int bufferSize = 4000)
|
||||
{
|
||||
_next = next;
|
||||
_buffer = new byte[bufferSize];
|
||||
_decoder = new OpusDecoder(samplingRate, channels);
|
||||
_decoder = new OpusDecoder(SampleRate, channels);
|
||||
}
|
||||
|
||||
public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
|
||||
|
||||
@@ -31,11 +31,14 @@ namespace Discord.Audio.Streams
|
||||
{
|
||||
cancelToken.ThrowIfCancellationRequested();
|
||||
|
||||
if (buffer[offset + 0] != 0x80 || buffer[offset + 1] != 0x78)
|
||||
return;
|
||||
|
||||
var payload = new byte[count - 12];
|
||||
Buffer.BlockCopy(buffer, offset + 12, payload, 0, count - 12);
|
||||
|
||||
ushort seq = (ushort)((buffer[offset + 3] << 8) |
|
||||
(buffer[offset + 2] << 0));
|
||||
ushort seq = (ushort)((buffer[offset + 2] << 8) |
|
||||
(buffer[offset + 3] << 0));
|
||||
|
||||
uint timestamp = (uint)((buffer[offset + 4] << 24) |
|
||||
(buffer[offset + 5] << 16) |
|
||||
@@ -45,5 +48,20 @@ namespace Discord.Audio.Streams
|
||||
_queue.WriteHeader(seq, timestamp);
|
||||
await (_next ?? _queue as Stream).WriteAsync(buffer, offset, count, cancelToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public static bool TryReadSsrc(byte[] buffer, int offset, out uint ssrc)
|
||||
{
|
||||
if (buffer.Length - offset < 12)
|
||||
{
|
||||
ssrc = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
ssrc = (uint)((buffer[offset + 8] << 24) |
|
||||
(buffer[offset + 9] << 16) |
|
||||
(buffer[offset + 10] << 16) |
|
||||
(buffer[offset + 11] << 0));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user