(61d00a474) v0.9.7.1
This commit is contained in:
@@ -0,0 +1,260 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using OpenAL;
|
||||
using System;
|
||||
using System.Linq;
|
||||
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 double LastAmplitude
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
public float Gain
|
||||
{
|
||||
get { return GameMain.Config?.MicrophoneVolume ?? 1.0f; }
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
var capture = new VoipCapture(deviceName)
|
||||
{
|
||||
LatestBufferID = storedBufferID ?? BUFFER_COUNT - 1
|
||||
};
|
||||
if (capture.captureDevice != IntPtr.Zero)
|
||||
{
|
||||
Instance = capture;
|
||||
}
|
||||
}
|
||||
|
||||
private VoipCapture(string deviceName) : base(GameMain.Client?.ID ?? 0, true, false)
|
||||
{
|
||||
VoipConfig.SetupEncoding();
|
||||
|
||||
//set up capture device
|
||||
captureDevice = Alc.CaptureOpenDevice(deviceName, VoipConfig.FREQUENCY, Al.FormatMono16, VoipConfig.BUFFER_SIZE * 5);
|
||||
|
||||
if (captureDevice == IntPtr.Zero)
|
||||
{
|
||||
DebugConsole.NewMessage("Alc.CaptureOpenDevice attempt 1 failed: error code " + Alc.GetError(IntPtr.Zero).ToString(),Color.Orange);
|
||||
//attempt using a smaller buffer size
|
||||
captureDevice = Alc.CaptureOpenDevice(deviceName, VoipConfig.FREQUENCY, Al.FormatMono16, VoipConfig.BUFFER_SIZE * 2);
|
||||
}
|
||||
|
||||
if (captureDevice == IntPtr.Zero)
|
||||
{
|
||||
DebugConsole.NewMessage("Alc.CaptureOpenDevice attempt 2 failed: error code " + Alc.GetError(IntPtr.Zero).ToString(), Color.Orange);
|
||||
//attempt using the default device
|
||||
captureDevice = Alc.CaptureOpenDevice("", VoipConfig.FREQUENCY, Al.FormatMono16, VoipConfig.BUFFER_SIZE * 2);
|
||||
}
|
||||
|
||||
if (captureDevice == IntPtr.Zero)
|
||||
{
|
||||
string errorCode = Alc.GetError(IntPtr.Zero).ToString();
|
||||
if (!GUIMessageBox.MessageBoxes.Any(mb => mb.UserData as string == "capturedevicenotfound"))
|
||||
{
|
||||
GUI.SettingsMenuOpen = false;
|
||||
new GUIMessageBox(TextManager.Get("Error"),
|
||||
(TextManager.Get("VoipCaptureDeviceNotFound", returnNull: true) ?? "Could not start voice capture, suitable capture device not found.") + " (" + errorCode + ")")
|
||||
{
|
||||
UserData = "capturedevicenotfound"
|
||||
};
|
||||
}
|
||||
GameAnalyticsManager.AddErrorEventOnce("Alc.CaptureDeviceOpenFailed", GameAnalyticsSDK.Net.EGAErrorSeverity.Error,
|
||||
"Alc.CaptureDeviceOpen(" + deviceName + ") failed. Error code: " + errorCode);
|
||||
GameMain.Config.VoiceSetting = GameSettings.VoiceMode.Disabled;
|
||||
Instance?.Dispose();
|
||||
Instance = null;
|
||||
return;
|
||||
}
|
||||
|
||||
int alError = Al.GetError();
|
||||
int alcError = Alc.GetError(captureDevice);
|
||||
if (alcError != Alc.NoError)
|
||||
{
|
||||
throw new Exception("Failed to open capture device: " + alcError.ToString() + " (ALC)");
|
||||
}
|
||||
if (alError != Al.NoError)
|
||||
{
|
||||
throw new Exception("Failed to open capture device: " + alError.ToString() + " (AL)");
|
||||
}
|
||||
|
||||
Alc.CaptureStart(captureDevice);
|
||||
alcError = Alc.GetError(captureDevice);
|
||||
if (alcError != Alc.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)
|
||||
{
|
||||
int alcError;
|
||||
Alc.GetInteger(captureDevice, Alc.EnumCaptureSamples, out int sampleCount);
|
||||
|
||||
alcError = Alc.GetError(captureDevice);
|
||||
if (alcError != Alc.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 != Alc.NoError)
|
||||
{
|
||||
throw new Exception("Failed to capture samples: " + alcError.ToString());
|
||||
}
|
||||
|
||||
double maxAmplitude = 0.0f;
|
||||
for (int i = 0; i < VoipConfig.BUFFER_SIZE; i++)
|
||||
{
|
||||
uncompressedBuffer[i] = (short)MathHelper.Clamp((uncompressedBuffer[i] * Gain), -short.MaxValue, short.MaxValue);
|
||||
double sampleVal = uncompressedBuffer[i] / (double)short.MaxValue;
|
||||
maxAmplitude = Math.Max(maxAmplitude, Math.Abs(sampleVal));
|
||||
}
|
||||
double dB = Math.Min(20 * Math.Log10(maxAmplitude), 0.0);
|
||||
|
||||
LastdB = dB;
|
||||
LastAmplitude = maxAmplitude;
|
||||
|
||||
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) && GUI.KeyboardDispatcher.Subscriber == null)
|
||||
{
|
||||
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(10);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Write(IWriteMessage msg)
|
||||
{
|
||||
lock (buffers)
|
||||
{
|
||||
base.Write(msg);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Dispose()
|
||||
{
|
||||
Instance = null;
|
||||
capturing = false;
|
||||
captureThread?.Join();
|
||||
captureThread = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
using Barotrauma.Sounds;
|
||||
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 ClientPeer netClient;
|
||||
private DateTime lastSendTime;
|
||||
private List<VoipQueue> queues;
|
||||
|
||||
private UInt16 storedBufferID = 0;
|
||||
|
||||
private static Rectangle[] voiceIconSheetRects;
|
||||
|
||||
public VoipClient(GameClient gClient,ClientPeer 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 == null || VoipCapture.Instance.EnqueuedTotalLength <= 0) return;
|
||||
}
|
||||
|
||||
if (DateTime.Now >= lastSendTime + VoipConfig.SEND_INTERVAL)
|
||||
{
|
||||
IWriteMessage msg = new WriteOnlyMessage();
|
||||
|
||||
msg.Write((byte)ClientPacketHeader.VOICE);
|
||||
msg.Write((byte)VoipCapture.Instance.QueueID);
|
||||
VoipCapture.Instance.Write(msg);
|
||||
|
||||
netClient.Send(msg, DeliveryMethod.Unreliable);
|
||||
|
||||
lastSendTime = DateTime.Now;
|
||||
}
|
||||
}
|
||||
|
||||
public void Read(IReadMessage 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() + "!", GUI.Style.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(client.Name, GameMain.SoundManager, client.VoipQueue);
|
||||
}
|
||||
|
||||
if (client.Character != null && !client.Character.IsDead && !client.Character.Removed && 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?.SetClientSpeaking(client);
|
||||
|
||||
if ((client.VoipSound.CurrentAmplitude * client.VoipSound.Gain * GameMain.SoundManager.GetCategoryGainMultiplier("voip")) > 0.1f) //TODO: might need to tweak
|
||||
{
|
||||
if (client.Character != null && !client.Character.Removed)
|
||||
{
|
||||
Vector3 clientPos = new Vector3(client.Character.WorldPosition.X, client.Character.WorldPosition.Y, 0.0f);
|
||||
Vector3 listenerPos = GameMain.SoundManager.ListenerPosition;
|
||||
float attenuationDist = client.VoipSound.Near * 1.125f;
|
||||
if (Vector3.DistanceSquared(clientPos, listenerPos) < attenuationDist * attenuationDist)
|
||||
{
|
||||
GameMain.SoundManager.VoipAttenuatedGain = 0.5f;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
GameMain.SoundManager.VoipAttenuatedGain = 0.5f;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void UpdateVoiceIndicator(GUIImage soundIcon, float voipAmplitude, float deltaTime)
|
||||
{
|
||||
if (voiceIconSheetRects == null)
|
||||
{
|
||||
var soundIconStyle = GUI.Style.GetComponentStyle("GUISoundIcon");
|
||||
Rectangle sourceRect = soundIconStyle.Sprites.First().Value.First().Sprite.SourceRect;
|
||||
var indexPieces = soundIconStyle.Element.Attribute("sheetindices").Value.Split(';');
|
||||
voiceIconSheetRects = new Rectangle[indexPieces.Length];
|
||||
for (int i = 0; i < indexPieces.Length; i++)
|
||||
{
|
||||
Point location = sourceRect.Location + XMLExtensions.ParsePoint(indexPieces[i].Trim()) * sourceRect.Size;
|
||||
voiceIconSheetRects[i] = new Rectangle(location, sourceRect.Size);
|
||||
}
|
||||
}
|
||||
|
||||
Pair<string, float> userdata = soundIcon.UserData as Pair<string, float>;
|
||||
userdata.Second = Math.Max(voipAmplitude, userdata.Second - deltaTime);
|
||||
|
||||
if (userdata.Second <= 0.0f)
|
||||
{
|
||||
soundIcon.Visible = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
soundIcon.Visible = true;
|
||||
int sheetIndex = (int)Math.Floor(userdata.Second * voiceIconSheetRects.Length);
|
||||
sheetIndex = MathHelper.Clamp(sheetIndex, 0, voiceIconSheetRects.Length - 1);
|
||||
soundIcon.SourceRect = voiceIconSheetRects[sheetIndex];
|
||||
soundIcon.OverrideState = GUIComponent.ComponentState.None;
|
||||
soundIcon.HoverColor = Color.White;
|
||||
}
|
||||
}
|
||||
|
||||
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