Added a method for sending silent audio frames on OpusEncodeStream (#2668)

* Implemented ClientDisconnect event for audio client.

* Added a method for sending silent frames.

* moved comment

* Added method summary.
+ removed changes to project file.

* Removed residual stuff remaining from previous edits.

* bunch of things

* Revert "bunch of things"

This reverts commit 292f23f4e1aabb26d2a3e5b9a2bdff8b5554635e.

* Update src/Discord.Net.WebSocket/Audio/Streams/OpusEncodeStream.cs

* Update src/Discord.Net.WebSocket/Audio/Streams/OpusEncodeStream.cs

* Update src/Discord.Net.WebSocket/Audio/Streams/OpusEncodeStream.cs

---------

Co-authored-by: Quin Lynch <49576606+quinchs@users.noreply.github.com>
This commit is contained in:
Frederik P
2023-08-10 15:15:56 +02:00
committed by GitHub
parent 2b8584d07d
commit 59094d2e1f

View File

@@ -1,4 +1,5 @@
using System; using System;
using System.Runtime.CompilerServices;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -23,6 +24,30 @@ namespace Discord.Audio.Streams
_buffer = new byte[OpusConverter.FrameBytes]; _buffer = new byte[OpusConverter.FrameBytes];
} }
/// <summary>
/// Sends silent frames to avoid interpolation errors after breaks in data transmission.
/// </summary>
/// <returns>A task representing the asynchronous operation of sending a silent frame.</returns>
public async Task WriteSilentFramesAsync()
{
// https://discord.com/developers/docs/topics/voice-connections#voice-data-interpolation
byte[] frameBytes = new byte[OpusConverter.FrameBytes];
// Magic silence numbers.
frameBytes[0] = 0xF8;
frameBytes[1] = 0xFF;
frameBytes[2] = 0xFE;
// The rest of the array is already zeroes, so no need to fill the rest.
const int frameCount = 5;
for (int i = 0; i < frameCount; i += 1)
{
await WriteAsync(frameBytes, 0, frameBytes.Length).ConfigureAwait(false);
}
}
public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancelToken) public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancelToken)
{ {
//Assume thread-safe //Assume thread-safe