Atozed Forums

Full Version: duplex audio over udp
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i use Tidtudpserver to send audio to clients i have something i do not understand from long time ago

when one client Send Audiodata to udpserver and udpserver send it back to clients every thing works fine and audio arrived good and clear

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 ?

here is the receiver side code



Code:
procedure TForm2.udpreciverUDPRead(AThread: TIdUDPListenerThread;
 const AData: TIdBytes; ABinding: TIdSocketHandle);
var
 AudioDataSize: Integer;
 AudioData : Pointer;
begin
TThread.Synchronize(nil,
procedure
begin


AudioDataSize := Length(AData);

if (AudioDataSize > 10) then
begin
try
if not Player.Active then
begin
Player.Active := True;
Player.WaitForStart;
end;
except
end;
if BlockAlign > 1 then
begin
Dec(AudioDataSize, AudioDataSize mod BlockAlign);
end;

AudioData := AudioBuffer.BeginUpdate(AudioDataSize);
try
BytesToRaw(AData, AudioData^, AudioDataSize);
finally
AudioBuffer.EndUpdate;
end;
end else
begin
Player.Active := False;
Player.WaitForStop;
end;

end);
end;

and here is the server side i have set multithreaded event to True


Code:
procedure TForm1.broadcatsaudio(ABinding: TIdSocketHandle; ipada : string; porta, serverprta : integer; const AData: TIdBytes);
var
AUDIOLIST : TAUDIOLIST;
c : integer;
List : Tlist;
begin


list := AllAUDIO.LockList;
try



for c := 0 to list.Count - 1 do
begin
AUDIOLIST := list.Items[c];

if (AUDIOLIST.userserverPort = serverprta)
And (AUDIOLIST.userport <> porta) then
begin
Abinding.SendTo(AUDIOLIST.userip, AUDIOLIST.userport, Adata);

end;




end;



finally
 AllAUDIO.UnlockList;
end;



end;


sorry for my bad grammar i am trying to describe what i am trying to do as best as i can
(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;
the queue helped to reduce some of cutting but i cannot mange it to make it work normally like a skype conversation the sound getting terribly bad and not understandable is it from the player it self ?
You likely need to prebuffer more, and turn off nagle and do your own packet management. Streaming is very sensitive to any small delays.
the streaming arrived altogether the sound is not delaying its like playing all together at the same time i think its the player it self that blocking the playing i dont know if iam right but its really hard to get it work