SafeNet/.svn/pristine/06/06c7f7dbc53a5fa0cf3d6dce783...

264 lines
7.1 KiB
Plaintext

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Drawing;
using libZPlay;
using System.Runtime.InteropServices;
using System.IO;
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using LumiSoft.Media.Wave;
namespace ConsoleApplication1
{
class Test
{
[DllImport("libzplay.dll", CharSet = CharSet.Unicode)]
public static extern int ZPlay();
static ConfigurablePlayer player1;
static void Main(string[] args)
{
player1 = new ConfigurablePlayer(0, 8000, 16, 1);
player1.PlayInQueue(new byte[] {0, 9});
UDPClient client = new UDPClient(player1);
//Thread t = new Thread(NewThread);
//t.Start();
Console.WriteLine("blabla");
System.Threading.Thread.Sleep(400000);
}
static void NewThread()
{
UDPServer server = new UDPServer(player1);
server.Listen();
}
}
public class UDPClient
{
private IPAddress addr;
private IPEndPoint ipep;
public static Socket newsock;
private ConfigurablePlayer player;
public UDPClient(ConfigurablePlayer player)
{
this.player = player;
byte[] data = new byte[] { 1, 2, 3, 4, 5, 6, 7 };
addr = IPAddress.Parse("10.120.1.111");
ipep = new IPEndPoint(addr, 9050);
Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
Console.WriteLine("intra");
player.GetMicData += new ConfigurablePlayer.GetMicDataHandler(Send);
player.TurnOnMic(0, 8000, 16, 1, 400);
//newsock.Send(data, data.Length, ipep);
//newsock.SendTo(data, data.Length, SocketFlags.None, ipep);
}
public void Send(byte[] data)
{
//Console.WriteLine("kk");
IPAddress addr = IPAddress.Parse("10.120.1.111");
IPEndPoint ipep = new IPEndPoint(addr, 9050);
Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//Console.WriteLine("se trimite din microphone" + newsock.ToString());
newsock.SendTo(data, data.Length, SocketFlags.None, ipep);
}
}
public class UDPServer
{
byte[] data;
private UdpClient newsock;
private IPEndPoint sender;
private ConfigurablePlayer player;
public UDPServer(ConfigurablePlayer player)
{
this.player = player;
//data = new byte[1024];
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9051);
newsock = new UdpClient(ipep);
sender = new IPEndPoint(IPAddress.Any, 0);
}
public void Listen()
{
Console.WriteLine("waiting for client...");
//ConfigurablePlayer player2 = new ConfigurablePlayer(ConfigurablePlayer.ChannelType.Right, ConfigurablePlayer.FormatType.Wav);
while (true)
{
data = newsock.Receive(ref sender);
Console.WriteLine("recived");
player.PlayInQueue(data);
//Console.WriteLine(Encoding.ASCII.GetString(data, 0, data.Length));
//newsock.Send(data, data.Length, sender);
}
}
}
public class ConfigurablePlayer
{
private WaveOut m_pSoundSender = null;
private WaveIn m_pSoundReceiver = null;
public event GetMicDataHandler GetMicData;
public delegate void GetMicDataHandler(byte[] micData);
/// <summary>
/// Constructor
/// </summary>
/// <param name="deviceNumber"></param>
/// <param name="channel"></param>
/// <param name="format"></param>
public ConfigurablePlayer(int outputDevice, int samplesPerSec, int bitsPerSample, int channels)
{
m_pSoundSender = new WaveOut(WaveOut.Devices[outputDevice], samplesPerSec, bitsPerSample, channels);
}
/// <summary>
/// Plays the data. If the player already plays someting, the data is queued.
/// ATENTION!!! if you call this function for more than one time
/// make sure that you don't pass the same reference of bytes
/// user like this: byte[] bytes = new byte[size];
/// </summary>
/// <param name="bytes"></param>
public void PlayInQueue(byte[] bytes)
{
m_pSoundSender.Play(bytes, 0, bytes.Length);
}
/// <summary>
/// Stops the current player. Also resets the queue.
/// </summary>
public void Stop()
{
m_pSoundSender.Dispose();
}
/// <summary>
/// Turn on the microphone.
/// </summary>
/// <param name="micDeviceNumber"></param>
/// <param name="samplesPerSec"></param>
/// <param name="bitsPerSample"></param>
/// <param name="channels"></param>
/// <param name="bufferSize"></param>
public void TurnOnMic(int micDeviceNumber, int samplesPerSec, int bitsPerSample, int channels, int bufferSize)
{
// G711 needs 8KHZ 16 bit 1 channel audio,
// 400kb buffer gives us 25ms audio frame.
//m_pSoundReceiver = new WaveIn(WaveIn.Devices[0],8000,16,1,400);
m_pSoundReceiver = new WaveIn(WaveIn.Devices[micDeviceNumber], samplesPerSec, bitsPerSample, channels, bufferSize);
m_pSoundReceiver.BufferFull += new BufferFullHandler
(m_pSoundReceiver_BufferFull);
m_pSoundReceiver.Start();
}
public void TurnOffMic()
{
if (m_pSoundReceiver != null)
{
m_pSoundReceiver.Stop();
m_pSoundReceiver = null;
}
}
/// <summary>
/// This method is called when recording buffer is full
/// and we need to process it.
/// </summary>
/// <param name="buffer">Recorded data.</param>
private void m_pSoundReceiver_BufferFull(byte[] buffer)
{
//Console.WriteLine("mic");
if (GetMicData != null)
{
GetMicData(buffer);
//Console.WriteLine("mare");
}
//Console.WriteLine("crazy");
}
public bool setLeftRightPercentage(int left, int right)
{
if (!(0 <= left && left <= 100))
{
return false;
}
if (!(0 <= right && right <= 100))
{
return false;
}
m_pSoundSender.SetVolume((ushort)left, (ushort)right);
return true;
}
}
}