public class AudioCapture
{
public enum CaptureMode
{
Capture,
LoopbackCapture
}
private const CaptureMode captureMode = CaptureMode.LoopbackCapture;
private MMDevice _selectedDevice;
private WasapiCapture _soundIn;
private IWaveSource _finalSource;
private bool bCapturedStop = false;
private const int AUDIOBLOCKSIZE = 3528;
public MMDevice SelectedDevice
{
get { return _selectedDevice; }
set
{
_selectedDevice = value;
}
}
private AudioSender audioSender = new AudioSender();
public bool AudioDeviceInit()
{
bool bRtn = false;
try
{
using (var deviceEnumerator = new MMDeviceEnumerator())
using (var deviceCollection = deviceEnumerator.EnumAudioEndpoints(
captureMode == CaptureMode.Capture ? DataFlow.Capture : DataFlow.Render, DeviceState.Active))
{
foreach (var device in deviceCollection)
{
var deviceFormat = WaveFormatFromBlob(device.PropertyStore[
new PropertyKey(new Guid(0xf19f064d, 0x82c, 0x4e27, 0xbc, 0x73, 0x68, 0x82, 0xa1, 0xbb, 0x8e, 0x4c), 0)].BlobValue);
_selectedDevice = device;
bRtn = true;
}
}
}
catch (Exception e)
{
K_BoxLog.LogDebug(e);
}
return bRtn;
}
public bool AudioCaptureStart(string serverip)
{
bool bRtn = false;
try
{
if (SelectedDevice == null)
{
return false;
}
if (AudioSender.connected == false)
audioSender.AudioSendConnect(serverip);
if (captureMode == CaptureMode.Capture)
_soundIn = new WasapiCapture();
else
_soundIn = new WasapiLoopbackCapture();
_soundIn.Device = SelectedDevice;
_soundIn.Initialize();
var soundInSource = new SoundInSource(_soundIn, AUDIOBLOCKSIZE * 4);
var singleBlockNotificationStream = new SimpleNotificationSource(soundInSource);
_finalSource = singleBlockNotificationStream
.ToWaveSource(16);
byte[] buffer = new byte[AUDIOBLOCKSIZE * 2];
soundInSource.DataAvailable += (s, e) =>
{
int read;
while ((read = _finalSource.Read(buffer, 0, buffer.Length)) > 0)
{
audioSender.AudioSend(buffer, read);
}
};
_soundIn.Start();
bRtn = true;
}
catch { }
return bRtn;
}
private void SingleBlockNotificationStreamOnSingleBlockRead(object sender, SingleBlockReadEventArgs e)
{
}
public bool AudioCaptureStop()
{
bool bRtn = false;
try
{
if (AudioSender.connected == true)
audioSender.AudioSendDisconnect();
_soundIn.Stop();
bRtn = false;
}
catch { }
return bRtn;
}
private static WaveFormat WaveFormatFromBlob(Blob blob)
{
if (blob.Length == 40)
return (WaveFormat)Marshal.PtrToStructure(blob.Data, typeof(WaveFormatExtensible));
return (WaveFormat)Marshal.PtrToStructure(blob.Data, typeof(WaveFormat));
}
}
댓글 없음:
댓글 쓰기