Recent

Author Topic: How can i get the output to my TMemo  (Read 2314 times)

mathias63

  • New Member
  • *
  • Posts: 17
How can i get the output to my TMemo
« on: September 01, 2025, 05:19:02 pm »
Hello again i did some code to open up my rtsp stream with ffplay after a button click, that now works good, but i would allso like to have the output written in a tmemo, to read eventually errors when connecting to it. So this is my code so far in a click button event:

RunProgram := TProcess.Create(nil);
RunProgram.CommandLine := ('/bin/ffplay -rtsp_transport tcp -i rtsp://myuser:mypass@192.168.0.50:554');
RunProgram.Execute;
RunProgram.Free;

I have tried some examples that i found on internet, but i dont really get any output, and the app freezes.
If some one could tell me i would be happy...


/Mathias

jamie

  • Hero Member
  • *****
  • Posts: 7300
Re: How can i get the output to my TMemo
« Reply #1 on: September 01, 2025, 08:09:40 pm »
you need to run the code within a TTHREAD object so you can sit there without locking up the rest of your code.

The only true wisdom is knowing you know nothing

mathias63

  • New Member
  • *
  • Posts: 17
Re: How can i get the output to my TMemo
« Reply #2 on: September 01, 2025, 08:28:56 pm »
you need to run the code within a TTHREAD object so you can sit there without locking up the rest of your code.

Thanks for your answer. I have no clue how or what threads does then, i only made some apps long ago in delphi (maybe 20 years ago). But now try using linux and Lazarus.
But will try google it and see if i can understand how to do this.

jamie

  • Hero Member
  • *****
  • Posts: 7300
Re: How can i get the output to my TMemo
« Reply #3 on: September 01, 2025, 08:34:23 pm »
The only true wisdom is knowing you know nothing

n7800

  • Hero Member
  • *****
  • Posts: 542

mathias63

  • New Member
  • *
  • Posts: 17
Re: How can i get the output to my TMemo
« Reply #5 on: September 01, 2025, 11:18:42 pm »
Thanks for tips but still feels a bit hard to understand what i need to do :D....if i just could download the most simple project with a form, a Tmemo, and button that starts another app and write the output in a TMemo or similar would help so i can just try change some code for my needs.

mathias63

  • New Member
  • *
  • Posts: 17
Re: How can i get the output to my TMemo
« Reply #6 on: September 02, 2025, 12:17:50 am »
See also: https://wiki.freepascal.org/Executing_External_Programs#Reading_large_output

I was trying this on a new project, but now i have no clue how do i get the output to the TMemo?

egsuh

  • Hero Member
  • *****
  • Posts: 1694
Re: How can i get the output to my TMemo
« Reply #7 on: September 02, 2025, 04:33:19 am »
Instead of
     with TStringList.Create do LoadfromStream
in following,

Code: Pascal  [Select][+][-]
  1.   // Or the data can be shown on screen
  2.   with TStringList.Create do
  3.   begin
  4.     OutputStream.Position := 0; // Required to make sure all data is copied from the start
  5.     LoadFromStream(OutputStream);
  6.     writeln(Text);
  7.     writeln('--- Number of lines = ', Count, '----');
  8.     Free
  9.   end;

something like this:

    OutputStream.Position := 0;
    Memo1.Lines.LoadFromStream(OutputStream);
« Last Edit: September 02, 2025, 04:35:39 am by egsuh »

Thausand

  • Sr. Member
  • ****
  • Posts: 389
Re: How can i get the output to my TMemo
« Reply #8 on: September 02, 2025, 10:00:00 am »
I have tried some examples that i found on internet, but i dont really get any output, and the app freezes.
Example make wrong use TProcess options (https://www.freepascal.org/docs-html/fcl/process/tprocess.commandline.html read deprecate is for Linux because no work). Make use parameters https://www.freepascal.org/docs-html/fcl/process/tprocess.parameters.html

Quote
If some one could tell me i would be happy...

Thanks for tips but still feels a bit hard to understand what i need to do :D....if i just could download the most simple project with a form, a Tmemo, and button that starts another app and write the output in a TMemo or similar would help so i can just try change some code for my needs.
Is no simple answer, you see when get there but can make very simple example.

something like this:
No work for ffplay output because output have many begin of line rewrite. Is continue output until finish (or error).

Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$h+}
  2. uses
  3.   cthreads, sysutils, sockets, ctypes, process, pipes;
  4.  
  5. // is idea not me
  6. function GetLocalIPString: string;
  7. const
  8.   GDNS_ADDR = '127.0.0.1';
  9.   GDNS_PORT = 53;
  10. var
  11.   sockErr  : cint    = 0;
  12.   fail     : boolean = false;
  13.   socket   : cint;
  14.   IPAddr   : TInetSockAddr;
  15.   HostAddr : TSockAddr;
  16.   namelen  : cuint32;
  17. begin
  18.   result := '';
  19.  
  20.   socket := fpsocket(AF_INET, SOCK_DGRAM, 0);
  21.   fail := (socket = -1);
  22.  
  23.   if not fail then
  24.   begin
  25.     IPAddr.sin_family      := AF_INET;
  26.     IPAddr.sin_port        := htons(GDNS_PORT);
  27.     IPAddr.sin_addr.s_addr := StrToHostAddr(GDNS_ADDR).s_addr;
  28.  
  29.     fail := (fpConnect(socket,@IPAddr,SizeOf(IPAddr)) <> 0);
  30.     if not fail then
  31.     begin
  32.       namelen := SizeOf(HostAddr);
  33.       fail := (fpgetsockname(socket, @HostAddr, @namelen) <> 0);
  34.       if not fail then
  35.       begin
  36.         result := NetAddrToStr(HostAddr.sin_addr);
  37.         exit;
  38.       end;
  39.     end
  40.   end;
  41.  
  42.   if fail then
  43.   begin
  44.     // have error raise
  45.     sockerr := socketError;
  46.     writeln('error local get IP-Address: ', sockerr);
  47.     halt(255);
  48.   end;
  49. end;
  50.  
  51. procedure Log(S: string);
  52. begin
  53.   writeln(S);  // or write memo(.lines)
  54. end;
  55.  
  56. procedure Log(S: string; const A: array of const);
  57. begin
  58.   Log(Format(S, A));
  59. end;
  60.  
  61. procedure RunFFPlay;
  62. var
  63.   LocalIP    : string;
  64.   FFPlayProc : TProcess;
  65.   Answer     : string;
  66.   Count      : LongInt;
  67. begin
  68.   localIP:= GetLocalIPString;
  69.   Log('IP-Address = %s',[LocalIP]);
  70.  
  71.   FFPlayProc:= TProcess.Create(nil);
  72.   try
  73.     FFPlayProc.Executable:= 'ffplay';
  74.     FFPlayProc.Parameters.Add('-i');
  75.     FFPlayProc.Parameters.Add('rtsp://' + localIP + ':8090/live');
  76.  
  77.     FFPlayProc.Options:=[poUsePipes, poStderrToOutPut];
  78.     FFPlayProc.Execute;
  79.  
  80.     // have output FFPlay
  81.     Log('start FFPlay loop');
  82.     while FFPlayProc.Running or (FFPlayProc.Output.NumBytesAvailable>0) do
  83.     begin
  84.       if FFPlayProc.Output.NumBytesAvailable>0 then
  85.       begin
  86.         Count:= FFPlayProc.Output.NumBytesAvailable;
  87.         SetLength(Answer,Count);
  88.  
  89.         Count:= FFPlayProc.Output.Read(Answer[1],Count);
  90.         Log(Answer);
  91.         Sleep(1);
  92.         // can have stop while boolean
  93.       end;
  94.     end;
  95.     Log('stop FFPlay loop');
  96.   finally
  97.     FFPlayProc.Free;
  98.   end;
  99. end;
  100.  
  101. ..
  102. .. RunFFPlay;
  103. ..
  104.  

If want GUI work then make RunFFPlay in TProcess of own.

Thaddy

  • Hero Member
  • *****
  • Posts: 18304
  • Here stood a man who saw the Elbe and jumped it.
Re: How can i get the output to my TMemo
« Reply #9 on: September 02, 2025, 12:32:16 pm »
you need to run the code within a TTHREAD object so you can sit there without locking up the rest of your code.
Or use TProcessAsync.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12523
  • FPC developer.

mathias63

  • New Member
  • *
  • Posts: 17
Re: How can i get the output to my TMemo
« Reply #11 on: September 02, 2025, 09:57:35 pm »
Thanks for answers...takes some time for me to get used to lazarus, will see if i can fix this in the future.

mathias63

  • New Member
  • *
  • Posts: 17
Re: How can i get the output to my TMemo
« Reply #12 on: September 02, 2025, 10:54:03 pm »
Thanks Thausand! i  will try that code tomorrow and see if it works :)

Thausand

  • Sr. Member
  • ****
  • Posts: 389
Re: How can i get the output to my TMemo
« Reply #13 on: September 03, 2025, 03:13:37 am »
Thanks Thausand! i  will try that code tomorrow and see if it works :)
Make note and change parameters for ffplay because example use parameters for work my configuration.

Link marcov also good because example make process output and store in memo.

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1290
Re: How can i get the output to my TMemo
« Reply #14 on: September 03, 2025, 11:33:59 pm »
hello,
have a look here
The example project use TprocessEx  from processutils unit of the fpcupdeluxe project  and displays output in a Tmemo from a command line in RealTime.

Friendly, J.P
« Last Edit: September 03, 2025, 11:37:12 pm by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

 

TinyPortal © 2005-2018