2f107db...5202af9
This commit is contained in:
@@ -0,0 +1,208 @@
|
||||
using Lidgren.Network;
|
||||
using OpenTK.Audio.OpenAL;
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
|
||||
namespace Barotrauma.Networking
|
||||
{
|
||||
class VoipCapture : VoipQueue, IDisposable
|
||||
{
|
||||
public static VoipCapture Instance
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
private IntPtr captureDevice;
|
||||
|
||||
private Thread captureThread;
|
||||
|
||||
private bool capturing;
|
||||
|
||||
public double LastdB
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
public DateTime LastEnqueueAudio;
|
||||
|
||||
public override byte QueueID
|
||||
{
|
||||
get
|
||||
{
|
||||
return GameMain.Client?.ID ?? 0;
|
||||
}
|
||||
protected set
|
||||
{
|
||||
//do nothing
|
||||
}
|
||||
}
|
||||
|
||||
public static void Create(string deviceName, UInt16? storedBufferID=null)
|
||||
{
|
||||
if (Instance != null)
|
||||
{
|
||||
throw new Exception("Tried to instance more than one VoipCapture object");
|
||||
}
|
||||
|
||||
Instance = new VoipCapture(deviceName)
|
||||
{
|
||||
LatestBufferID = storedBufferID ?? BUFFER_COUNT - 1
|
||||
};
|
||||
}
|
||||
|
||||
private VoipCapture(string deviceName) : base(GameMain.Client?.ID ?? 0, true, false)
|
||||
{
|
||||
VoipConfig.SetupEncoding();
|
||||
|
||||
//set up capture device
|
||||
captureDevice = Alc.CaptureOpenDevice(deviceName, VoipConfig.FREQUENCY, ALFormat.Mono16, VoipConfig.BUFFER_SIZE * 5);
|
||||
|
||||
ALError alError = AL.GetError();
|
||||
AlcError alcError = Alc.GetError(captureDevice);
|
||||
if (alcError != AlcError.NoError)
|
||||
{
|
||||
throw new Exception("Failed to open capture device: " + alcError.ToString() + " (ALC)");
|
||||
}
|
||||
if (alError != ALError.NoError)
|
||||
{
|
||||
throw new Exception("Failed to open capture device: " + alError.ToString() + " (AL)");
|
||||
}
|
||||
|
||||
Alc.CaptureStart(captureDevice);
|
||||
alcError = Alc.GetError(captureDevice);
|
||||
if (alcError != AlcError.NoError)
|
||||
{
|
||||
throw new Exception("Failed to start capturing: " + alcError.ToString());
|
||||
}
|
||||
|
||||
capturing = true;
|
||||
captureThread = new Thread(UpdateCapture)
|
||||
{
|
||||
IsBackground = true,
|
||||
Name = "VoipCapture"
|
||||
};
|
||||
captureThread.Start();
|
||||
}
|
||||
|
||||
public static void ChangeCaptureDevice(string deviceName)
|
||||
{
|
||||
GameMain.Config.VoiceCaptureDevice = deviceName;
|
||||
|
||||
if (Instance != null)
|
||||
{
|
||||
UInt16 storedBufferID = Instance.LatestBufferID;
|
||||
Instance.Dispose();
|
||||
Create(GameMain.Config.VoiceCaptureDevice, storedBufferID);
|
||||
}
|
||||
}
|
||||
|
||||
void UpdateCapture()
|
||||
{
|
||||
short[] uncompressedBuffer = new short[VoipConfig.BUFFER_SIZE];
|
||||
while (capturing)
|
||||
{
|
||||
AlcError alcError;
|
||||
Alc.GetInteger(captureDevice, AlcGetInteger.CaptureSamples, 1, out int sampleCount);
|
||||
|
||||
alcError = Alc.GetError(captureDevice);
|
||||
if (alcError != AlcError.NoError)
|
||||
{
|
||||
throw new Exception("Failed to determine sample count: " + alcError.ToString());
|
||||
}
|
||||
|
||||
if (sampleCount < VoipConfig.BUFFER_SIZE)
|
||||
{
|
||||
int sleepMs = (VoipConfig.BUFFER_SIZE - sampleCount) * 800 / VoipConfig.FREQUENCY;
|
||||
if (sleepMs < 5) sleepMs = 5;
|
||||
Thread.Sleep(sleepMs);
|
||||
continue;
|
||||
}
|
||||
|
||||
GCHandle handle = GCHandle.Alloc(uncompressedBuffer, GCHandleType.Pinned);
|
||||
try
|
||||
{
|
||||
Alc.CaptureSamples(captureDevice, handle.AddrOfPinnedObject(), VoipConfig.BUFFER_SIZE);
|
||||
}
|
||||
finally
|
||||
{
|
||||
handle.Free();
|
||||
}
|
||||
|
||||
alcError = Alc.GetError(captureDevice);
|
||||
if (alcError != AlcError.NoError)
|
||||
{
|
||||
throw new Exception("Failed to capture samples: " + alcError.ToString());
|
||||
}
|
||||
|
||||
double maxAmplitude = 0.0f;
|
||||
for (int i = 0; i < VoipConfig.BUFFER_SIZE; i++)
|
||||
{
|
||||
double sampleVal = (double)uncompressedBuffer[i] / (double)short.MaxValue;
|
||||
maxAmplitude = Math.Max(maxAmplitude, Math.Abs(sampleVal));
|
||||
}
|
||||
double dB = Math.Min(20 * Math.Log10(maxAmplitude), 0.0);
|
||||
|
||||
LastdB = dB;
|
||||
|
||||
bool allowEnqueue = false;
|
||||
if (GameMain.WindowActive)
|
||||
{
|
||||
if (GameMain.Config.VoiceSetting == GameSettings.VoiceMode.Activity)
|
||||
{
|
||||
if (dB > GameMain.Config.NoiseGateThreshold)
|
||||
{
|
||||
allowEnqueue = true;
|
||||
}
|
||||
}
|
||||
else if (GameMain.Config.VoiceSetting == GameSettings.VoiceMode.PushToTalk)
|
||||
{
|
||||
if (PlayerInput.KeyDown(InputType.Voice))
|
||||
{
|
||||
allowEnqueue = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (allowEnqueue)
|
||||
{
|
||||
LastEnqueueAudio = DateTime.Now;
|
||||
//encode audio and enqueue it
|
||||
lock (buffers)
|
||||
{
|
||||
int compressedCount = VoipConfig.Encoder.Encode(uncompressedBuffer, 0, VoipConfig.BUFFER_SIZE, BufferToQueue, 0, VoipConfig.MAX_COMPRESSED_SIZE);
|
||||
EnqueueBuffer(compressedCount);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//enqueue silence
|
||||
lock (buffers)
|
||||
{
|
||||
EnqueueBuffer(0);
|
||||
}
|
||||
}
|
||||
|
||||
Thread.Sleep(VoipConfig.BUFFER_SIZE * 800 / VoipConfig.FREQUENCY);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Write(NetBuffer msg)
|
||||
{
|
||||
lock (buffers)
|
||||
{
|
||||
base.Write(msg);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Dispose()
|
||||
{
|
||||
Instance = null;
|
||||
capturing = false;
|
||||
captureThread.Join();
|
||||
captureThread = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
using Barotrauma.Sounds;
|
||||
using Lidgren.Network;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Barotrauma.Items.Components;
|
||||
|
||||
namespace Barotrauma.Networking
|
||||
{
|
||||
class VoipClient : IDisposable
|
||||
{
|
||||
private GameClient gameClient;
|
||||
private NetClient netClient;
|
||||
private DateTime lastSendTime;
|
||||
private List<VoipQueue> queues;
|
||||
|
||||
private UInt16 storedBufferID = 0;
|
||||
|
||||
public VoipClient(GameClient gClient,NetClient nClient)
|
||||
{
|
||||
gameClient = gClient;
|
||||
netClient = nClient;
|
||||
|
||||
queues = new List<VoipQueue>();
|
||||
|
||||
lastSendTime = DateTime.Now;
|
||||
}
|
||||
|
||||
public void RegisterQueue(VoipQueue queue)
|
||||
{
|
||||
if (queue == VoipCapture.Instance) return;
|
||||
if (!queues.Contains(queue)) queues.Add(queue);
|
||||
}
|
||||
|
||||
public void UnregisterQueue(VoipQueue queue)
|
||||
{
|
||||
if (queues.Contains(queue)) queues.Remove(queue);
|
||||
}
|
||||
|
||||
public void SendToServer()
|
||||
{
|
||||
if (GameMain.Config.VoiceSetting == GameSettings.VoiceMode.Disabled)
|
||||
{
|
||||
if (VoipCapture.Instance != null)
|
||||
{
|
||||
storedBufferID = VoipCapture.Instance.LatestBufferID;
|
||||
VoipCapture.Instance.Dispose();
|
||||
}
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (VoipCapture.Instance == null) VoipCapture.Create(GameMain.Config.VoiceCaptureDevice, storedBufferID);
|
||||
if (VoipCapture.Instance.EnqueuedTotalLength <= 0) return;
|
||||
}
|
||||
|
||||
if (DateTime.Now >= lastSendTime + VoipConfig.SEND_INTERVAL)
|
||||
{
|
||||
NetOutgoingMessage msg = netClient.CreateMessage();
|
||||
|
||||
msg.Write((byte)ClientPacketHeader.VOICE);
|
||||
msg.Write((byte)VoipCapture.Instance.QueueID);
|
||||
VoipCapture.Instance.Write(msg);
|
||||
|
||||
netClient.SendMessage(msg, NetDeliveryMethod.Unreliable);
|
||||
|
||||
lastSendTime = DateTime.Now;
|
||||
}
|
||||
}
|
||||
|
||||
public void Read(NetBuffer msg)
|
||||
{
|
||||
byte queueId = msg.ReadByte();
|
||||
VoipQueue queue = queues.Find(q => q.QueueID == queueId);
|
||||
|
||||
if (queue == null)
|
||||
{
|
||||
#if DEBUG
|
||||
DebugConsole.NewMessage("Couldn't find VoipQueue with id " + queueId.ToString() + "!", Color.Red);
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
if (queue.Read(msg))
|
||||
{
|
||||
Client client = gameClient.ConnectedClients.Find(c => c.VoipQueue == queue);
|
||||
if (client.Muted || client.MutedLocally) { return; }
|
||||
|
||||
if (client.VoipSound == null)
|
||||
{
|
||||
DebugConsole.Log("Recreating voipsound " + queueId);
|
||||
client.VoipSound = new VoipSound(GameMain.SoundManager, client.VoipQueue);
|
||||
}
|
||||
|
||||
if (client.Character != null && !client.Character.IsDead && !client.Character.IsDead && client.Character.SpeechImpediment <= 100.0f)
|
||||
{
|
||||
var messageType = ChatMessage.CanUseRadio(client.Character, out WifiComponent radio) ? ChatMessageType.Radio : ChatMessageType.Default;
|
||||
client.Character.ShowSpeechBubble(1.25f, ChatMessage.MessageColor[(int)messageType]);
|
||||
|
||||
client.VoipSound.UseRadioFilter = messageType == ChatMessageType.Radio;
|
||||
if (client.VoipSound.UseRadioFilter)
|
||||
{
|
||||
client.VoipSound.SetRange(radio.Range * 0.8f, radio.Range);
|
||||
}
|
||||
else
|
||||
{
|
||||
client.VoipSound.SetRange(ChatMessage.SpeakRange * 0.4f, ChatMessage.SpeakRange);
|
||||
}
|
||||
if (!client.VoipSound.UseRadioFilter && Character.Controlled != null)
|
||||
{
|
||||
client.VoipSound.UseMuffleFilter = SoundPlayer.ShouldMuffleSound(Character.Controlled, client.Character.WorldPosition, ChatMessage.SpeakRange, client.Character.CurrentHull);
|
||||
}
|
||||
}
|
||||
GameMain.NetLobbyScreen.SetPlayerSpeaking(client);
|
||||
GameMain.GameSession?.CrewManager?.SetPlayerSpeaking(client);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
VoipCapture.Instance?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using Concentus.Enums;
|
||||
using Concentus.Structs;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Barotrauma.Networking
|
||||
{
|
||||
static partial class VoipConfig
|
||||
{
|
||||
public static bool Ready = false;
|
||||
|
||||
public const int FREQUENCY = 48000; //not amazing, but not bad audio quality
|
||||
public const int BUFFER_SIZE = 2880; //60ms window, the max Opus seems to support
|
||||
|
||||
public static OpusEncoder Encoder
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
public static OpusDecoder Decoder
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
public static void SetupEncoding()
|
||||
{
|
||||
if (!Ready)
|
||||
{
|
||||
Encoder = new OpusEncoder(FREQUENCY, 1, OpusApplication.OPUS_APPLICATION_VOIP);
|
||||
Encoder.Bandwidth = OpusBandwidth.OPUS_BANDWIDTH_AUTO;
|
||||
Encoder.Bitrate = 8 * MAX_COMPRESSED_SIZE * FREQUENCY / BUFFER_SIZE;
|
||||
Encoder.SignalType = OpusSignal.OPUS_SIGNAL_VOICE;
|
||||
|
||||
Decoder = new OpusDecoder(FREQUENCY, 1);
|
||||
|
||||
Ready = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user