(08-03-2018, 04:08 PM)Madammar Wrote: if 2 or 3 clients send audio at the same time the audio arrived un understandable and have too many cutting i wanted to make it a live conversation with duplex audio but i dont understand where is the problem is is it from idudpserver or from receiver side ?
Well, for one thing, your receiver is synchronizing the entire OnUDPRead event handler, so you may as well have set udprecive.ThreadedEvent=False instead. But more importantly, not everything the event handler is doing needs to be synchronized at all. Do as much processing as you can inside the event handler itself, and synchronize only what is actually needed, like accessing the audio Player.
You might also consider using TThread.Queue() instead of TThread.Synchronize(), so that your event handler is not waiting on the Player on all, so the receiving of UDP packets and the playing of those packets occur in parallel, not in serial.
Try this:
Code:
procedure TForm2.udpreciverUDPRead(AThread: TIdUDPListenerThread;
const AData: TIdBytes; ABinding: TIdSocketHandle);
var
AudioDataSize: Integer;
begin
AudioDataSize := Length(AData);
if (AudioDataSize > 10) then
begin
if BlockAlign > 1 then
Dec(AudioDataSize, AudioDataSize mod BlockAlign);
TThread.Queue(nil,
procedure
var
AudioData : Pointer;
begin
try
if not Player.Active then
begin
Player.Active := True;
Player.WaitForStart;
end;
except
end;
AudioData := AudioBuffer.BeginUpdate(AudioDataSize);
try
BytesToRaw(AData, AudioData^, AudioDataSize);
finally
AudioBuffer.EndUpdate;
end;
end
);
end else
begin
TThread.Queue(nil,
procedure
begin
Player.Active := False;
Player.WaitForStop;
end
);
end;
end;