Skip to content

Commit

Permalink
add audio
Browse files Browse the repository at this point in the history
  • Loading branch information
yswenli committed Jan 31, 2021
1 parent 9fba70d commit 7886667
Show file tree
Hide file tree
Showing 23 changed files with 494 additions and 946 deletions.
81 changes: 81 additions & 0 deletions GFF.Component/GAudio/GAudioClient.cs
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();
}
}
}
35 changes: 35 additions & 0 deletions GFF.Component/GAudio/GAudioServer.cs
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();
}
}
}
9 changes: 0 additions & 9 deletions GFF.Component/GAudio/IAudioReceiver.cs

This file was deleted.

9 changes: 0 additions & 9 deletions GFF.Component/GAudio/IAudioSender.cs

This file was deleted.

54 changes: 54 additions & 0 deletions GFF.Component/GAudio/Net/AudioClient.cs
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();
}

}
}
70 changes: 70 additions & 0 deletions GFF.Component/GAudio/Net/AudioServer.cs
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 _);
}
}
}
37 changes: 0 additions & 37 deletions GFF.Component/GAudio/NetworkAudioPlayer.cs

This file was deleted.

39 changes: 0 additions & 39 deletions GFF.Component/GAudio/NetworkAudioSender.cs

This file was deleted.

Loading

0 comments on commit 7886667

Please sign in to comment.