dot-i wrote:OK, thanks, so if I understand correctly this is all purely streaming, there is no functional file recording yet in what you tried?
i work on it
Code: Select all
namespace Sat2Ip.RtspSample
{
public class DumpToFile
{
static int TS_PACKET_SIZE = 188;
private string _address;
int _port;
string _file;
Socket _sock;
Thread _th;
int _timeout = 5000;
volatile bool _stopCondition = false;
public void start(string address,int port, string file)
{
_stopCondition = false;
_address = address;
_port = port;
_file = file;
_th = new Thread(receive) {IsBackground = true};
_th.Start();
}
public void stop()
{
_stopCondition = true;
_th.Abort();
}
private void receive()
{
int RECV_SIZE = 8192;
byte[] data = new byte[RECV_SIZE];
int recv = 0;
var rtpEndPoint = new IPEndPoint(IPAddress.Parse(_address), _port);
_sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
_sock.Bind(rtpEndPoint);
var sender = new IPEndPoint(IPAddress.Parse(_address), _port);
var remotEndPoint = (EndPoint)sender;
recv = _sock.ReceiveFrom(data, data.Length, SocketFlags.None, ref remotEndPoint);
// open file stream
FileStream fs = null; ;
try
{
fs = new FileStream(_file, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite, 4096, true);
}
catch
{
if (_sock != null)
{
_sock.Close();
_sock = null;
}
if (fs != null)
{
fs.Close();
}
}
byte[] syncbyte = BitConverter.GetBytes(0x47); //0x47
byte[] buffer = new byte[RECV_SIZE];
while (recv > 0)
{
//ts packet is 188 bytes long
//each udp packet should start with with syncbyte 0X47
//and is 188 x 7 =1316 bytes long
WriteObject wo = new WriteObject();
wo.FStream = fs;
int j = 0;
for (int i = 0; i < recv; i = i + 188)
{
if (data[i] == syncbyte[0])
{
Array.Copy(data, i, buffer, j, 188);
j = j + 188;
}
else
{
Console.Write("Syncbyte not found");
}
}
IAsyncResult asyncResult;
fs.BeginWrite(buffer, 0, j, EndWriteCallBack, wo);
try
{
recv = _sock.ReceiveFrom(data, data.Length, SocketFlags.None, ref remotEndPoint);
}
catch
{
break;
}
if (_stopCondition)
break;
}
// clean
if (_sock != null)
{
_sock.Close();
_sock = null;
}
if (fs != null)
{
fs.Close();
}
}
private void EndWriteCallBack(IAsyncResult res)
{
var wo = (WriteObject)res.AsyncState;
var ftemp = wo.FStream;
ftemp.EndWrite(res);
}
}
internal class WriteObject
{
FileStream fStream;
public FileStream FStream
{
get { return fStream; }
set { fStream = value; }
}
}
}
the streams.ts is createt and data Comes in
in this hang i at time "No Sync Byte found"
Code: Select all
for (int i = 0; i < recv; i = i + 188)
{
if (data[i] == syncbyte[0])
{
Array.Copy(data, i, buffer, j, 188);
j = j + 188;
}
else
{
Console.Write("Syncbyte not found");
}
}