234 lines
6.7 KiB
C#
234 lines
6.7 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using SafeMobileLib;
|
|||
|
using System.Net.Sockets;
|
|||
|
using System.Net;
|
|||
|
using System.Threading;
|
|||
|
using System.IO;
|
|||
|
|
|||
|
namespace AppServerMobile
|
|||
|
{
|
|||
|
class RecordingHandle
|
|||
|
{
|
|||
|
private TcpClass tcpRecAudio;
|
|||
|
private TCPServer tcpRecAndroid;
|
|||
|
private string ip;
|
|||
|
private int portAudio;
|
|||
|
private int portAndro;
|
|||
|
private NetworkStream nsAndro;
|
|||
|
|
|||
|
public RecordingHandle(string _ip, int _portAudio, int _portAndro)
|
|||
|
{
|
|||
|
ip = _ip;
|
|||
|
portAudio = _portAudio;
|
|||
|
portAndro = _portAndro;
|
|||
|
|
|||
|
tcpRecAndroid = new TCPServer();
|
|||
|
tcpRecAndroid.Start(_portAndro);
|
|||
|
tcpRecAndroid.OnConnectionRecv += new TCPServer.ConnectionRecv(tcpRecAndroid_OnConnectionRecv);
|
|||
|
tcpRecAndroid.OnConnectionEnded += new TCPServer.ConnectionEnded(tcpRecAndroid_OnConnectionEnded);
|
|||
|
|
|||
|
//init andro decode encode helper
|
|||
|
memStream = new MemoryStream();
|
|||
|
}
|
|||
|
|
|||
|
void tcpRecAndroid_OnConnectionEnded(string IP)
|
|||
|
{
|
|||
|
nsAndro = null;
|
|||
|
}
|
|||
|
|
|||
|
void tcpRecAndroid_OnConnectionRecv(NetworkStream ns, string IP)
|
|||
|
{
|
|||
|
nsAndro = ns;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
MemoryStream memStream;
|
|||
|
public void ConnectAudio()
|
|||
|
{
|
|||
|
DisconnectAudio();
|
|||
|
|
|||
|
tcpRecAudio = new TcpClass();
|
|||
|
tcpRecAudio.Start(ip, portAudio);
|
|||
|
tcpRecAudio.OnMessageRecv+=new TcpClass.MessageRecv(tcpRecAudio_OnMessageRecv);
|
|||
|
tcpRecAudio.OnConnectionEnded += new TcpClass.ConnectionEnded(tcpRecAudio_OnConnectionEnded);
|
|||
|
}
|
|||
|
void tcpRecAudio_OnMessageRecv(byte[] data, int len)
|
|||
|
{
|
|||
|
memStream.Write(data, 0, len);
|
|||
|
Console.WriteLine("received rec len:" + len);
|
|||
|
/*
|
|||
|
nAndro.AddAudio4Decode(data);
|
|||
|
|
|||
|
while (nAndro.Length > 0)
|
|||
|
{
|
|||
|
byte[] dataout = new byte[len * 2];
|
|||
|
int recv = nAndro.GetDecodedAudio(dataout, 0, len * 2);
|
|||
|
SendAndroid(dataout, recv);
|
|||
|
}
|
|||
|
*/
|
|||
|
}
|
|||
|
|
|||
|
void tcpRecAudio_OnConnectionEnded()
|
|||
|
{
|
|||
|
Console.WriteLine("received all file: total len:"+memStream.Length);
|
|||
|
byte[] buff = new byte[memStream.Length];
|
|||
|
buff = memStream.ToArray();
|
|||
|
try
|
|||
|
{
|
|||
|
|
|||
|
SendAndroid(buff, buff.Length);
|
|||
|
/*
|
|||
|
nAndro.AddAudio4Decode(buff);
|
|||
|
while (nAndro.Length > 0)
|
|||
|
{
|
|||
|
byte[] dataout = new byte[1024];
|
|||
|
int recv = nAndro.GetDecodedAudio(dataout, 0, 1024);
|
|||
|
SendAndroid(dataout, recv);
|
|||
|
Console.WriteLine("Sending to andro <nAndro.Length:" + nAndro.Length +"> <recv:"+recv + ">");
|
|||
|
}*/
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
Console.WriteLine("Error send Recording to Android:" + ex.ToString());
|
|||
|
}
|
|||
|
memStream.Flush();
|
|||
|
memStream = new MemoryStream();
|
|||
|
}
|
|||
|
|
|||
|
public void DisconnectAudio()
|
|||
|
{
|
|||
|
if (tcpRecAudio != null)
|
|||
|
{
|
|||
|
tcpRecAudio.Stop();
|
|||
|
tcpRecAudio = null;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public delegate void MessageRecv(byte[] data, int recv);
|
|||
|
public event MessageRecv OnMessageRecv;
|
|||
|
|
|||
|
public void SendAPP(byte[] data, int len)
|
|||
|
{
|
|||
|
tcpRecAudio.Send(data);
|
|||
|
}
|
|||
|
|
|||
|
public void SendAndroid(byte[] data, int len)
|
|||
|
{
|
|||
|
if (nsAndro != null)
|
|||
|
{
|
|||
|
tcpRecAndroid.Send(data, len, nsAndro);
|
|||
|
//Console.WriteLine("sending rec len:"+len);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
public class TcpClass
|
|||
|
{
|
|||
|
private TcpClient client;
|
|||
|
private IPEndPoint serverEndPoint;
|
|||
|
private NetworkStream clientStream;
|
|||
|
private string serverIp;
|
|||
|
private int serverPort;
|
|||
|
private bool connected = false;
|
|||
|
private Thread thr = null;
|
|||
|
|
|||
|
public TcpClass()
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
public bool Start(string ip, int port)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
serverIp = ip;
|
|||
|
serverPort = port;
|
|||
|
client = new TcpClient();
|
|||
|
serverEndPoint = new IPEndPoint(IPAddress.Parse(serverIp), serverPort);
|
|||
|
client.Connect(serverEndPoint);
|
|||
|
clientStream = client.GetStream();
|
|||
|
|
|||
|
thr = new Thread(new ParameterizedThreadStart(HandleData));
|
|||
|
thr.Start(clientStream);
|
|||
|
connected = true;
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
SM.Debug("Ex:" + ex.ToString());
|
|||
|
connected = false;
|
|||
|
}
|
|||
|
return connected;
|
|||
|
}
|
|||
|
|
|||
|
public void Stop()
|
|||
|
{
|
|||
|
if (clientStream != null)
|
|||
|
{
|
|||
|
clientStream.Close();
|
|||
|
}
|
|||
|
if (client != null)
|
|||
|
{
|
|||
|
client.Close();
|
|||
|
}
|
|||
|
connected = false;
|
|||
|
}
|
|||
|
|
|||
|
public void Send(byte[] buffer)
|
|||
|
{
|
|||
|
if (connected)
|
|||
|
{
|
|||
|
clientStream.Write(buffer, 0, buffer.Length);
|
|||
|
clientStream.Flush();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void HandleData(object ns)
|
|||
|
{
|
|||
|
NetworkStream nStream = (NetworkStream)ns;
|
|||
|
byte[] message = new byte[128];
|
|||
|
int bytesRead;
|
|||
|
while (true)
|
|||
|
{
|
|||
|
bytesRead = 0;
|
|||
|
try
|
|||
|
{
|
|||
|
//blocks until a message is recevied
|
|||
|
bytesRead = nStream.Read(message, 0, 128);
|
|||
|
}
|
|||
|
catch
|
|||
|
{
|
|||
|
//a socket error has occured
|
|||
|
break;
|
|||
|
}
|
|||
|
if (bytesRead == 0)
|
|||
|
{
|
|||
|
//the client has disconnected from the server
|
|||
|
break;
|
|||
|
}
|
|||
|
//message has successfully been received
|
|||
|
//ASCIIEncoding encoder = new ASCIIEncoding();
|
|||
|
if (this.OnMessageRecv != null)
|
|||
|
{
|
|||
|
this.OnMessageRecv(message, bytesRead);
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
nStream.Close();
|
|||
|
if (this.OnConnectionEnded != null)
|
|||
|
{
|
|||
|
this.OnConnectionEnded();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public delegate void MessageRecv(byte[] data, int recv);
|
|||
|
public event MessageRecv OnMessageRecv;
|
|||
|
|
|||
|
public delegate void ConnectionEnded();
|
|||
|
public event ConnectionEnded OnConnectionEnded;
|
|||
|
}
|
|||
|
}
|