-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
494 additions
and
946 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using GFF.Component.GAudio.Net; | ||
using GFF.Component.NAudio.Wave; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace GFF.Component.GAudio | ||
{ | ||
public class GAudioClient | ||
{ | ||
AudioClient _audioClient; | ||
|
||
private readonly IWavePlayer waveOut; | ||
private readonly BufferedWaveProvider waveProvider; | ||
private readonly WideBandSpeexCodec _speexCodec; | ||
private readonly WaveIn waveIn; | ||
|
||
public GAudioClient(IPEndPoint endPoint) | ||
{ | ||
_audioClient = new AudioClient(endPoint); | ||
_audioClient.OnReceive += _audioClient_OnReceive; | ||
|
||
_speexCodec = new WideBandSpeexCodec(); | ||
|
||
waveOut = new WaveOut(); | ||
waveProvider = new BufferedWaveProvider(_speexCodec.RecordFormat); | ||
waveOut.Init(waveProvider); | ||
|
||
|
||
waveIn = new WaveIn(); | ||
waveIn.BufferMilliseconds = 50; | ||
waveIn.DeviceNumber = 0; | ||
waveIn.WaveFormat = _speexCodec.RecordFormat; | ||
waveIn.DataAvailable += OnAudioCaptured; | ||
} | ||
|
||
public GAudioClient(string ip, int port) : this(new IPEndPoint(IPAddress.Parse(ip), port)) | ||
{ | ||
|
||
} | ||
|
||
public void Start() | ||
{ | ||
_audioClient.Connect(); | ||
|
||
waveOut.Play(); | ||
|
||
waveIn.StartRecording(); | ||
} | ||
|
||
private void _audioClient_OnReceive(byte[] data) | ||
{ | ||
byte[] decoded = _speexCodec.Decode(data, 0, data.Length); | ||
waveProvider.AddSamples(decoded, 0, decoded.Length); | ||
} | ||
|
||
void OnAudioCaptured(object sender, WaveInEventArgs e) | ||
{ | ||
byte[] encoded = _speexCodec.Encode(e.Buffer, 0, e.BytesRecorded); | ||
_audioClient.Send(encoded); | ||
} | ||
|
||
|
||
public void Stop() | ||
{ | ||
waveIn.StopRecording(); | ||
waveOut.Pause(); | ||
_audioClient.Disconnect(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Stop(); | ||
waveIn.Dispose(); | ||
waveOut.Dispose(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using GFF.Component.GAudio.Net; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace GFF.Component.GAudio | ||
{ | ||
public class GAudioServer | ||
{ | ||
AudioServer _audioServer; | ||
|
||
public GAudioServer(IPEndPoint endPoint) | ||
{ | ||
_audioServer = new AudioServer(endPoint); | ||
} | ||
|
||
public GAudioServer(int port) : this(new IPEndPoint(IPAddress.Any, port)) | ||
{ | ||
|
||
} | ||
|
||
public void Start() | ||
{ | ||
_audioServer.Start(); | ||
} | ||
|
||
public void Stop() | ||
{ | ||
_audioServer.Stop(); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using SAEA.Sockets; | ||
using SAEA.Sockets.Base; | ||
using SAEA.Sockets.Model; | ||
using System; | ||
using System.Net; | ||
|
||
namespace GFF.Component.GAudio.Net | ||
{ | ||
public class AudioClient | ||
{ | ||
IClientSocket _udpClient; | ||
|
||
BaseUnpacker _baseUnpacker; | ||
|
||
public event Action<Byte[]> OnReceive; | ||
|
||
public AudioClient(IPEndPoint endPoint) | ||
{ | ||
var bContext = new BaseContext(); | ||
|
||
_udpClient = SocketFactory.CreateClientSocket(SocketOptionBuilder.Instance.SetSocket(SAEASocketType.Udp) | ||
.SetIPEndPoint(endPoint) | ||
.UseIocp(bContext) | ||
.SetReadBufferSize(SocketOption.UDPMaxLength) | ||
.SetWriteBufferSize(SocketOption.UDPMaxLength) | ||
.Build()); | ||
|
||
_baseUnpacker = (BaseUnpacker)bContext.Unpacker; | ||
|
||
_udpClient.OnReceive += _udpClient_OnReceive; | ||
} | ||
|
||
private void _udpClient_OnReceive(byte[] data) | ||
{ | ||
OnReceive?.Invoke(data); | ||
} | ||
|
||
public void Connect() | ||
{ | ||
_udpClient.Connect(); | ||
} | ||
|
||
public void Send(byte[] data) | ||
{ | ||
_udpClient.SendAsync(data); | ||
} | ||
|
||
public void Disconnect() | ||
{ | ||
_udpClient.Disconnect(); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using SAEA.Sockets; | ||
using SAEA.Sockets.Base; | ||
using SAEA.Sockets.Interface; | ||
using SAEA.Sockets.Model; | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
|
||
namespace GFF.Component.GAudio.Net | ||
{ | ||
public class AudioServer | ||
{ | ||
IServerSocket _udpServer; | ||
|
||
ConcurrentDictionary<string, IUserToken> _cache; | ||
|
||
public AudioServer(IPEndPoint endPoint) | ||
{ | ||
_cache = new ConcurrentDictionary<string, IUserToken>(); | ||
|
||
_udpServer = SocketFactory.CreateServerSocket(SocketOptionBuilder.Instance.SetSocket(SAEASocketType.Udp) | ||
.SetIPEndPoint(endPoint) | ||
.UseIocp<BaseContext>() | ||
.SetReadBufferSize(SocketOption.UDPMaxLength) | ||
.SetWriteBufferSize(SocketOption.UDPMaxLength) | ||
.SetTimeOut(5000) | ||
.Build()); | ||
_udpServer.OnAccepted += _udpServer_OnAccepted; | ||
_udpServer.OnDisconnected += _udpServer_OnDisconnected; | ||
_udpServer.OnReceive += _udpServer_OnReceive; | ||
} | ||
|
||
public void Start() | ||
{ | ||
_udpServer.Start(); | ||
} | ||
|
||
public void Stop() | ||
{ | ||
_udpServer.Stop(); | ||
} | ||
|
||
private void _udpServer_OnReceive(ISession currentSession, byte[] data) | ||
{ | ||
Parallel.ForEach(_cache.Keys, (id) => | ||
{ | ||
try | ||
{ | ||
_udpServer.SendAsync(id, data); | ||
} | ||
catch { } | ||
}); | ||
} | ||
|
||
private void _udpServer_OnAccepted(object obj) | ||
{ | ||
var ut = (IUserToken)obj; | ||
if (ut != null) | ||
{ | ||
_cache.TryAdd(ut.ID, ut); | ||
} | ||
} | ||
|
||
private void _udpServer_OnDisconnected(string ID, Exception ex) | ||
{ | ||
_cache.TryRemove(ID, out IUserToken _); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.