Readded outgoing audio
This commit is contained in:
@@ -39,7 +39,7 @@ namespace Discord.Audio
|
||||
private readonly JsonSerializer _serializer;
|
||||
|
||||
private TaskCompletionSource<bool> _connectTask;
|
||||
private CancellationTokenSource _cancelToken;
|
||||
private CancellationTokenSource _cancelTokenSource;
|
||||
private Task _heartbeatTask;
|
||||
private long _heartbeatTime;
|
||||
private string _url;
|
||||
@@ -110,7 +110,7 @@ namespace Discord.Audio
|
||||
{
|
||||
_url = url;
|
||||
_connectTask = new TaskCompletionSource<bool>();
|
||||
_cancelToken = new CancellationTokenSource();
|
||||
_cancelTokenSource = new CancellationTokenSource();
|
||||
|
||||
await ApiClient.ConnectAsync("wss://" + url).ConfigureAwait(false);
|
||||
await ApiClient.SendIdentityAsync(userId, sessionId, token).ConfigureAwait(false);
|
||||
@@ -152,7 +152,7 @@ namespace Discord.Audio
|
||||
await _audioLogger.InfoAsync("Disconnecting").ConfigureAwait(false);
|
||||
|
||||
//Signal tasks to complete
|
||||
try { _cancelToken.Cancel(); } catch { }
|
||||
try { _cancelTokenSource.Cancel(); } catch { }
|
||||
|
||||
//Disconnect from server
|
||||
await ApiClient.DisconnectAsync().ConfigureAwait(false);
|
||||
@@ -169,19 +169,35 @@ namespace Discord.Audio
|
||||
await _disconnectedEvent.InvokeAsync(ex).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public void Send(byte[] data, int count)
|
||||
{
|
||||
//TODO: Queue these?
|
||||
ApiClient.SendAsync(data, count).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public Stream CreateOpusStream(int samplesPerFrame, int bufferSize = 4000)
|
||||
{
|
||||
return new RTPWriteStream(this, _secretKey, samplesPerFrame, _ssrc, bufferSize = 4000);
|
||||
CheckSamplesPerFrame(samplesPerFrame);
|
||||
var target = new BufferedAudioTarget(ApiClient, samplesPerFrame, _cancelTokenSource.Token);
|
||||
return new RTPWriteStream(target, _secretKey, samplesPerFrame, _ssrc, bufferSize = 4000);
|
||||
}
|
||||
public Stream CreateDirectOpusStream(int samplesPerFrame, int bufferSize = 4000)
|
||||
{
|
||||
CheckSamplesPerFrame(samplesPerFrame);
|
||||
var target = new DirectAudioTarget(ApiClient);
|
||||
return new RTPWriteStream(target, _secretKey, samplesPerFrame, _ssrc, bufferSize = 4000);
|
||||
}
|
||||
public Stream CreatePCMStream(int samplesPerFrame, int? bitrate = null, int bufferSize = 4000)
|
||||
{
|
||||
return new OpusEncodeStream(this, _secretKey, samplesPerFrame, _ssrc, bitrate, bufferSize);
|
||||
CheckSamplesPerFrame(samplesPerFrame);
|
||||
var target = new BufferedAudioTarget(ApiClient, samplesPerFrame, _cancelTokenSource.Token);
|
||||
return new OpusEncodeStream(target, _secretKey, samplesPerFrame, _ssrc, bitrate, bufferSize);
|
||||
}
|
||||
public Stream CreateDirectPCMStream(int samplesPerFrame, int? bitrate = null, int bufferSize = 4000)
|
||||
{
|
||||
CheckSamplesPerFrame(samplesPerFrame);
|
||||
var target = new DirectAudioTarget(ApiClient);
|
||||
return new OpusEncodeStream(target, _secretKey, samplesPerFrame, _ssrc, bitrate, bufferSize);
|
||||
}
|
||||
private void CheckSamplesPerFrame(int samplesPerFrame)
|
||||
{
|
||||
if (samplesPerFrame != 120 && samplesPerFrame != 240 && samplesPerFrame != 480 &&
|
||||
samplesPerFrame != 960 && samplesPerFrame != 1920 && samplesPerFrame != 2880)
|
||||
throw new ArgumentException("Value must be 120, 240, 480, 960, 1920 or 2880", nameof(samplesPerFrame));
|
||||
}
|
||||
|
||||
private async Task ProcessMessageAsync(VoiceOpCode opCode, object payload)
|
||||
@@ -201,7 +217,7 @@ namespace Discord.Audio
|
||||
throw new InvalidOperationException($"Discord does not support {DiscordVoiceAPIClient.Mode}");
|
||||
|
||||
_heartbeatTime = 0;
|
||||
_heartbeatTask = RunHeartbeatAsync(data.HeartbeatInterval, _cancelToken.Token);
|
||||
_heartbeatTask = RunHeartbeatAsync(data.HeartbeatInterval, _cancelTokenSource.Token);
|
||||
|
||||
ApiClient.SetUdpEndpoint(_url, data.Port);
|
||||
await ApiClient.SendDiscoveryAsync(_ssrc).ConfigureAwait(false);
|
||||
|
||||
@@ -52,7 +52,7 @@ namespace Discord.Audio
|
||||
throw new Exception($"Opus Error: {(OpusError)result}");
|
||||
}
|
||||
|
||||
/// <summary> Gets or sets whether Forward Error Correction is enabled. </summary>
|
||||
/// <summary> Gets or sets the encoder's bitrate. </summary>
|
||||
public void SetBitrate(int value)
|
||||
{
|
||||
if (value < 1 || value > DiscordVoiceAPIClient.MaxBitrate)
|
||||
|
||||
@@ -7,8 +7,8 @@
|
||||
|
||||
private readonly OpusEncoder _encoder;
|
||||
|
||||
internal OpusEncodeStream(AudioClient audioClient, byte[] secretKey, int samplesPerFrame, uint ssrc, int? bitrate = null, int bufferSize = 4000)
|
||||
: base(audioClient, secretKey, samplesPerFrame, ssrc, bufferSize)
|
||||
internal OpusEncodeStream(IAudioTarget target, byte[] secretKey, int samplesPerFrame, uint ssrc, int? bitrate = null, int bufferSize = 4000)
|
||||
: base(target, secretKey, samplesPerFrame, ssrc, bufferSize)
|
||||
{
|
||||
_encoder = new OpusEncoder(SampleRate, Channels);
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace Discord.Audio
|
||||
{
|
||||
internal class RTPWriteStream : Stream
|
||||
{
|
||||
private readonly AudioClient _audioClient;
|
||||
private readonly IAudioTarget _target;
|
||||
private readonly byte[] _nonce, _secretKey;
|
||||
private int _samplesPerFrame;
|
||||
private uint _ssrc, _timestamp = 0;
|
||||
@@ -16,9 +16,9 @@ namespace Discord.Audio
|
||||
public override bool CanSeek => false;
|
||||
public override bool CanWrite => true;
|
||||
|
||||
internal RTPWriteStream(AudioClient audioClient, byte[] secretKey, int samplesPerFrame, uint ssrc, int bufferSize = 4000)
|
||||
internal RTPWriteStream(IAudioTarget target, byte[] secretKey, int samplesPerFrame, uint ssrc, int bufferSize = 4000)
|
||||
{
|
||||
_audioClient = audioClient;
|
||||
_target = target;
|
||||
_secretKey = secretKey;
|
||||
_samplesPerFrame = samplesPerFrame;
|
||||
_ssrc = ssrc;
|
||||
@@ -48,7 +48,7 @@ namespace Discord.Audio
|
||||
|
||||
count = SecretBox.Encrypt(buffer, offset, count, _buffer, 12, _nonce, _secretKey);
|
||||
Buffer.BlockCopy(_nonce, 0, _buffer, 0, 12); //Copy the RTP header from nonce to buffer
|
||||
_audioClient.Send(_buffer, count + 12);
|
||||
_target.SendAsync(_buffer, count + 12).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
public override void Flush() { }
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Diagnostics;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Discord.Audio
|
||||
{
|
||||
internal class BufferedAudioTarget : IAudioTarget, IDisposable
|
||||
{
|
||||
private static readonly byte[] _silencePacket = new byte[] { 0xF8, 0xFF, 0xFE };
|
||||
|
||||
private double _ticksPerFrame;
|
||||
private Task _task;
|
||||
private DiscordVoiceAPIClient _client;
|
||||
private CancellationTokenSource _cancelTokenSource;
|
||||
private ConcurrentQueue<byte[]> _queue;
|
||||
|
||||
internal BufferedAudioTarget(DiscordVoiceAPIClient client, int samplesPerFrame, CancellationToken cancelToken)
|
||||
{
|
||||
_client = client;
|
||||
double milliseconds = samplesPerFrame / 48.0;
|
||||
double ticksPerFrame = Stopwatch.Frequency / 1000.0 * milliseconds;
|
||||
|
||||
_cancelTokenSource = new CancellationTokenSource();
|
||||
cancelToken = CancellationTokenSource.CreateLinkedTokenSource(_cancelTokenSource.Token, cancelToken).Token;
|
||||
_queue = new ConcurrentQueue<byte[]>(); //TODO: We need a better queue
|
||||
|
||||
_task = Run(ticksPerFrame, cancelToken);
|
||||
}
|
||||
|
||||
private Task Run(double ticksPerFrame, CancellationToken cancelToken)
|
||||
{
|
||||
return Task.Run(async () =>
|
||||
{
|
||||
var stopwatch = Stopwatch.StartNew();
|
||||
long lastTick = stopwatch.ElapsedTicks;
|
||||
double ticksPerMilli = Stopwatch.Frequency / 1000.0;
|
||||
while (!cancelToken.IsCancellationRequested)
|
||||
{
|
||||
long thisTick = stopwatch.ElapsedTicks;
|
||||
double remaining = ticksPerFrame - (thisTick - lastTick);
|
||||
if (remaining <= 0)
|
||||
{
|
||||
byte[] buffer;
|
||||
if (_queue.TryDequeue(out buffer))
|
||||
await _client.SendAsync(buffer, buffer.Length).ConfigureAwait(false);
|
||||
else
|
||||
await _client.SendAsync(_silencePacket, _silencePacket.Length).ConfigureAwait(false);
|
||||
lastTick = thisTick;
|
||||
}
|
||||
else if (remaining > 1)
|
||||
{
|
||||
int millis = (int)Math.Floor(remaining / ticksPerMilli);
|
||||
await Task.Delay(millis).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Task SendAsync(byte[] buffer, int count)
|
||||
{
|
||||
byte[] newBuffer = new byte[count];
|
||||
Buffer.BlockCopy(buffer, 0, newBuffer, 0, count);
|
||||
_queue.Enqueue(newBuffer);
|
||||
return Task.Delay(0);
|
||||
}
|
||||
|
||||
protected void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
_cancelTokenSource.Cancel();
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
16
src/Discord.Net.WebSocket/Audio/Targets/DirectAudioTarget.cs
Normal file
16
src/Discord.Net.WebSocket/Audio/Targets/DirectAudioTarget.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Discord.Audio
|
||||
{
|
||||
internal class DirectAudioTarget : IAudioTarget
|
||||
{
|
||||
private readonly DiscordVoiceAPIClient _client;
|
||||
public DirectAudioTarget(DiscordVoiceAPIClient client)
|
||||
{
|
||||
_client = client;
|
||||
}
|
||||
|
||||
public Task SendAsync(byte[] buffer, int count)
|
||||
=> _client.SendAsync(buffer, count);
|
||||
}
|
||||
}
|
||||
9
src/Discord.Net.WebSocket/Audio/Targets/IAudioTarget.cs
Normal file
9
src/Discord.Net.WebSocket/Audio/Targets/IAudioTarget.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Discord.Audio
|
||||
{
|
||||
internal interface IAudioTarget
|
||||
{
|
||||
Task SendAsync(byte[] buffer, int count);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user