No time to look at your code. Sorry.
Could you tell me why you need a thread for TWSocket ? It is asynchronous
anyway.
If you use a thread, be sure to create TWSocket from the execute method and
to add a message pump in your thread. Also make sure to /not/ call the
message pump from any of the event generated by TWSocket, directly or
indirectly.
--
Contribute to the SSL Effort. Visit http://www.overbyte.be/eng/ssl.html
--
[EMAIL PROTECTED]
http://www.overbyte.be
- Original Message -
From: "Christian Hinske" <[EMAIL PROTECTED]>
To:
Sent: Wednesday, January 10, 2007 10:14 PM
Subject: [twsocket] udp multicast multiple packages -> TWSocket bug?
Hi everyone, I am quite new to this mailing list, but I hope anyone can
answer my question.
The problem I am experiencing is as follows:
I get MPEG2 TS-data from a realtime hardware encoder who is multicasting
into the local network. I am using TWSocket to receive the datapackes. As
some of you might know, MPEG2 Transport Stream (TS) consists of 188 Byte
including a 4Byte header, which I can check to see, if the received package
is OK.
Now, if I am working with only one thread, everything is fine. Once I am
adding a second thread (OnDataAvailable -> RingBuffer; RingBuffer->
Thread2 -> File), it seems that OnDataAvailable isn't executed for every
event and so buffers the data.
The hardware encoder packs 7 MPEG2 packages into one udp-packet, so the
standard-size package I get is 1316 Byte. Now, once I am applying the second
thread, packages get multiples of that (2632), but they all share the same
characteristics: The first header is damaged, 2nd - 7th are fine, 8th -
whatever size are empty or random data.
I was wondering if maybe TWSocket does not copy the second 1316Byte packet
into the buffer-area after the first 1316Byte package if OnDataAvailable
hasn't been called in between, but somewhere into the first bytes, which
would explain why the first header (with 188 Bytes following is broken).
This is how parts of my debugfile look like
1316 Byte udp package received
header 1 - OKAY, pid 256 -> payload video
header 2 - OKAY, pid 256 -> payload video
header 3 - OKAY, pid 256 -> payload video
header 4 - OKAY, pid 259 -> payload audio
header 5 - OKAY, pid 256 -> payload video
header 6 - OKAY, pid 256 -> payload video
header 7 - OKAY, pid 256 -> payload video
2632 Byte udp package received
header 1 - ERROR, pid 19440
header 2 - OKAY, pid 256
header 3 - OKAY, pid 256
header 4 - OKAY, pid 256
header 5 - OKAY, pid 256
header 6 - OKAY, pid 256
header 7 - OKAY, pid 256
header 8 - ERROR, pid 0
header 9 - ERROR, pid 0
header 10 - ERROR, pid 0
header 11 - ERROR, pid 0
header 12 - ERROR, pid 0
header 13 - ERROR, pid 0
header 14 - ERROR, pid 0
The code I am using is in analogy to the TWSocket multithreading example.
Code looks like this:
type
TPos = RECORD
Buffersize, Blocksize : Int64;
CurrentWPos, CurrentRPos, StartPos : Int64;
Diskrepanz : Int64;
Richtung : Byte;
END;
TStreamThread = class(TThread)
private
FAddrDatei : Pointer;
BlockMem : Pointer;
procedure CalculateDiscrepancy;
procedure WriteMemToFile;
procedure ResetPosition;
public
FDatenspeicher : Pointer;
FPos : ^TPos;
FAddrLink : Pointer;
FAusgabe : TLabel;
Ffinalizethread: Boolean;
FExchangeFile : Boolean;
procedure SetLabel;
procedure Execute; override;
end;
TChangeThread = class(TThread)
private
procedure ExchangeFile;
procedure PrepareFile;
public
Mode : Byte;
procedure Execute; override;
end;
TRingBuffer = class(TObject)
private
Speicher : Pointer;
Groesse: Int64;
Start : Pointer;
Writer : Int64;
Reader : Int64;
Diskrepanz : Int64;
Richtung : Integer;
public
fReadyToWrite : TEvent;
fReadyToRead : TEvent;
CS : TCriticalSection;
constructor Create;
procedure GetData(pData : Pointer; pSize : Int64);
procedure PutData(pData : Pointer; pSize : Int64);
end;
var
Datei : ARRAY[0..1] OF TFileStream;
AddrDatei : Pointer;
AddrLink : Pointer;
DoExchange: Byte;
RootName : String;
Socket:TWSocket;
StreamThread : TStreamThread;
ChangeThread : TChangeThread;
ExchangeThread: TChangeThread;
public
{ Public-Deklarationen }
DataRes: Pointer;
Pos: ^TPos;
MyVar : Integer;
CritSec: TCriticalSection;
Wholesize : Int64;
CurrentFileSize: Int64;
WrittenSize: Int64;
Testvariable : Int64;
ReceivedPacks : Int64;
MaxSize: Int64;
PrerunSize : Int64;
FileCount : Integer;
StreamID : Integer;
RecordingID: Integer;
ErrorPacks : Int64;
procedu