Solved

indy tcp extract data

Posted on 2014-09-20
4
196 Views
Last Modified: 2014-09-29
iam trying to create chat app thats support voice and text

i don with the codes but i stick on errors

i send audio stream like that

procedure Tmain.recorderData(Sender: TObject; const Buffer: Pointer;
  BufferSize: Cardinal; var FreeIt: Boolean);
  //user send audio how to send and recive ?
var
  Bytes: TIdBytes;
begin
  Freeit := True;
  if Tcli.Connected then
  begin
    if (Buffer <> nil) and (Buffersize > 0) then
    begin
      try
        Bytes := RawToBytes(Buffer^, Buffersize);
      except
        micstop.Caption := 'Invalid !';
        Exit;
      end;
      begin
      Tcli.IOHandler.Write(BufferSize);
      Tcli.IOHandler.Write(Bytes);
    end;
    end
     else
stop.Caption := 'Server error!';
  end else
    stop.Caption := 'Not connected';
end;

Open in new window


and server execute like this

procedure Tmain.TservExecute(AContext: TIdContext);
var
  AudioDataSize: Integer;
  AudioDataBytes: TIdBytes;
  AudioData : Pointer;
begin
try
    EnterCriticalSection(Section);
    try
      AudioDataSize := AContext.Connection.IOHandler.ReadLongInt();
      AContext.Connection.IOHandler.ReadBytes(AudioDataBytes, AudioDataSize);
      if AudioDataSize > 10 then
      begin
        if AudioDataSize <> 0 then
      begin
        try
          if not main.Player.Active then
          begin
            main.Player.Active := True;
            main.Player.WaitForStart;
          end;
        except
        end;
        if BlockAlign > 1 then
          Dec(AudioDataSize, AudioDataSize mod BlockAlign);
        AudioData := AudioBuffer.BeginUpdate(AudioDataSize);
        try
          BytesToRaw(AudioDataBytes, AudioData^, AudioDataSize);
        finally
          AudioBuffer.EndUpdate;
        end;
      end else
      begin
        Player.Active := False;
        Player.WaitForStop;
      end;
      end;
    finally
      LeaveCriticalSection(Section);
    end;
  except
    on e: exception do showmessage(e.Message)
  end;
end;

Open in new window


it works fine but now i cant read what user type in server execute when i use the both code

procedure Tmain.SendClick(Sender: TObject);
//user send message but how to recive ?
var
  strMsg: string;
begin
if usertype.Text = '' then Abort;
strMsg := usertype.Text;
Tcli.IOHandler.WriteLn(strMsg);
usertype.Clear;
end;

Open in new window



any idea on how extract and read audio data and text together ? i search alot with no real help.
0
Comment
Question by:drama22
  • 2
4 Comments
 
LVL 25

Expert Comment

by:Sinisa Vuk
Comment Utility
You should make send buffer header - where you can describe what is following after....
My example of header:
type
  TBuffType = (btAudio, btChat);
  TChatHeader = record
    HeaderMagicCode: Integer;
    BufferType: TBuffType;
    BuffSize: Integer;
  end;

Open in new window


 HeaderMagicCode is a special value (like 999999) - useful when you go out of sync (when stream broke),
BufferType := btAudio when you send audio buffer with size: BuffSize.

When you detect (on server side) using BufferType = btAudio - buffer is audio, and if  BufferType = btChat - then it is chat text.
0
 

Author Comment

by:drama22
Comment Utility
But that will broke text while other client's is talking its multiple user chatroom also I dont understand how to do it with my current code that I post it
0
 
LVL 25

Accepted Solution

by:
Sinisa Vuk earned 500 total points
Comment Utility
That will not broke (chat) text. This header can contain destination address, block type (audio, text), size,... Server and client after that will know what is going on.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now