TProcess gstreamer thread wont killed lazarus / 32-Linux ---------------------------------------------------------------------- af0815 · 2020-01-13 14:48 I want to start gstreamer and make one picture. After this the gstreamer pipeline should be shut down. My problem is, the gstreamer pipeline is still exists and every try there is a new one. Actual the sample works with installed gstreamer and a USB V4L Kamera. How can i shutdown the pipeline too ?! BTW: V4L is not possible because the cameras are only working with gstreamer (GigE, USB3,...). Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TFormVideoTmp.SnapShot();const  BUF_SIZE = 2048; // Buffer size for reading the output in chunks var  AProcess: TProcess;  BytesRead: longint;  Buffer: array[1..BUF_SIZE] of byte;  i,found: Integer;  line: String;  finish: boolean;begin  AProcess := TProcess.Create(nil);   AProcess.Executable := '/bin/sh';  AProcess.Parameters.Add('-c');  AProcess.Parameters.Add('/usr/bin/gst- launch-1.0 -v v4l2src ! identity silent=false ! jpegenc ! filesink location=/tmp/test.jpg');//  AProcess.Parameters.Add('/usr/bin/gst- launch-1.0 -v tcambin ! identity silent=false ! jpegenc ! filesink location=/tmp/test.jpg');  AProcess.Options := [poUsePipes];  AProcess.Execute;  finish := false;  repeat    BytesRead := AProcess.Output.Read(Buffer, BUF_SIZE);    Line := '';    for i := 1 to BytesRead do begin      Line := Line + chr(Buffer[i]);    end;    found := Pos('dts',Line);    if (found > 0) and (BytesRead > (found+4+18)) then begin      finish:= true;    end;    if (BytesRead = 0) then begin      finish:= true;    end;  until finish;  // Stop if no more data is available or dts found  if AProcess.Terminate(0) then    writeln('Teminate ok')  else    writeln('Terminate NOK');  AProcess.Free;end; ---------------------------------------------------------------------- marcov · 2020-01-13 15:03 Probably never block on reads, that leaves you free to terminate. (e.g. when you termine it is complete. Best is to look at  FPC 3.2 or 3.3.1 sources, processbody.inc procedure readinputstream. Note how it continuously checks the bytes to read using P.NumBytesAvailable Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TP rocessnamemacro.ReadInputStream(p:TInputPipeStream;data:TStream;MaxLoo ps:integer=10):boolean;const  BufSize = 4096;var  Buffer: array[0..BufSize - 1] of byte;  ReadBytes: integer;  Available : integer;begin  Available:=P.NumBytesAvailable;  result:=Available>0;  if not result then    Exit;  while (available > 0) and (MaxLoops>0) do  begin    ReadBytes := Output.Read({%H-}Buffer, min(BufSize,Available));    data.Write(Buffer, ReadBytes);    Available:=P.NumBytesAvailable;    dec(MaxLoops);  end;end; ---------------------------------------------------------------------- af0815 · 2020-01-13 15:25 This is not my problem with the block on read. I have changed the line a little to Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   AProcess := TProcess.Create(nil);  AProcess.Executable := '/home/andi/data/start'; and a start with Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i #!/bin/sh/usr/bin/gst-launch-1.0 -v tcambin ! identity silent=false ! jpegenc ! filesink location=/tmp/test.jpg On commandline it work like expected. But i see gst-launch-1.0 itself fork and gstreamer have another processID. Normally one more than the reportedID from TProcess. I think this is my problem, gst-launch is shut down, but gstreamer did not see this signal. ---------------------------------------------------------------------- marcov · 2020-01-13 16:48 Well, then the question is what does gstreamer need to shutdown?  Maybe a bit of stracing helps. ---------------------------------------------------------------------- af0815 · 2020-01-13 17:35 Quote from: marcov on January 13, 2020, 05:48:53 pm Maybe a bit of stracing helps. I found only entries to ST Racing, but nothing about stracing. Can you give me a more desciptive advice ? I should send a cntrl-c (=SIGINT) to the gst-launch before terminate? How can i send it with TProcess ?! ---------------------------------------------------------------------- marcov · 2020-01-13 17:51 Try to type man strace on the cmdline. It is a tool to dump syscalls. ---------------------------------------------------------------------- af0815 · 2020-01-14 06:17 Ok, i have done it, the result: Quote..... futex(0x557d38f59728, FUTEX_WAIT_PRIVATE, 2, NULL) = 0 read(3, "W", 1)                         = 1 futex(0x557d38f59728, FUTEX_WAKE_PRIVATE, 1) = 0 write(14, "\1\0\0\0\0\0\0\0",         = 8 futex(0x557d392a6dd0, FUTEX_WAKE_PRIVATE, 2147483647) = 0 write(14, "\1\0\0\0\0\0\0\0",         = 8 write(14, "\1\0\0\0\0\0\0\0",         = 8 write(1, "/GstPipeline:pipeline0/GstIdenti"..., 248/GstPipeline:pipeline0/GstIdentity:identity0: last-message = chain   ******* (identity0:sink) (25165824 bytes, dts: 0:00:05.304566315, pts: 0:00:05.304566315, duration: none, offset: -1, offset_end: -1, flags: 00000000 , meta: none) 0x7f9dcc03c140 ) = 248 write(14, "\1\0\0\0\0\0\0\0",         = 8 poll([{fd=3, events=POLLIN}, {fd=14, events=POLLIN}], 2, -1) = 1 ([{fd=14, revents=POLLIN}]) read(14, "\5\0\0\0\0\0\0\0", 16)        = 8 poll([{fd=3, events=POLLIN}, {fd=14, events=POLLIN}], 2, -1^C) = ? ERESTART_RESTARTBLOCK (Interrupted by signal) strace: Process 2333 detached andi@DebLap:~$ handling interrupt. Interrupt: Stopping pipeline ... Execution ended after 0:00:06.429844722 Setting pipeline to PAUSED ... Setting pipeline to READY ... Setting pipeline to NULL ... Freeing pipeline ... Ctrl-C should be uses to shut down the pipeline (^C) = ? ERESTART_RESTARTBLOCK (Interrupted by signal)) But a send a sending of SIGINT to TProcess nothing change, the pipeline (and TProcess) is still running Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     repeat    BytesRead := AProcess.Output.Read(Buffer, BUF_SIZE);    Line := '';    for i := 1 to BytesRead do begin      Line := Line + chr(Buffer[i]);    end;    writeln(Line);    found := Pos('dts',Line);    if (found > 0) and (BytesRead > (found+4+18)) then begin      writeln(copy(Line,found+4,18));      writeln('+++++++++++++');      if fpkill(AProcess.Handle,SIGINT)=0 then        writeln('SIGINT ok')      else        writeln('SIGINT NOK');    end;    if (BytesRead = 0) then begin      writeln('-------------');      finish:= true;    end;  until finish;  // Stop if no more data is available or dts found ---------------------------------------------------------------------- trev · 2020-01-14 06:42 Have you tried using  gst-launch  with the -e argument? ---------------------------------------------------------------------- af0815 · 2020-01-14 07:34 Quote from: trev on January 14, 2020, 07:42:02 amHave you tried using  gst-launch  with the -e argument? -e is not a valid argument Quotegst-launch-1.0 accepts the following options: --help Print help synopsis and available FLAGS -v, --verbose Output status information and property notifications -q, --quiet Do not print any progress information -m, --messages Output messages posted on the pipeline's bus -t, --tags Output tags (also known as metadata) -o FILE, --output=FILE Save XML representation of pipeline to FILE and exit -f, --no_fault Do not install a fault handler -T, --trace Print memory allocation traces. The feature must be enabled at compile time to work.Source: https://gstreamer.freedesktop.org/documentation/tools/gst- launch.html?gi-language=c Actual i use a workaround and search for the PID of gst-launch and send the SIGINT direct to the process instead of sending to TProcess. It looks the SIGINT is eaten by the basic process, controlled by TProcess and never send to the childs. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function IsCommandRunning(cmd:String):THandle;var  t:TProcess;  s,s1:TStringList;  erg: integer;  h1,h2,h3:string;begin  Result:=0;  t:=tprocess.create(nil);  t.Executable:='ps';  t.Parameters.Add('-C'+cmd);  t.Options:=[poUsePipes,poWaitonexit];  try    t.Execute;    s:=tstringlist.Create;    try      s.LoadFromStream(t.Output);      if s.Count > 1 then begin        // Zeile 0 = Text        h1:= s[1];        s1:=TStringList.Create;      try          s1.Delimiter:=#32;          s1.StrictDelimiter:=true;        s1.DelimitedText:= trim(s[1]);          h2:= s1[0];          erg := 0;          if s1.Count > 1 then            TryStrToInt(s1[0],erg);          Result:=erg;        finally          s1.free;        end;      end;    finally      s.free;    end;  finally    t.Free;  end;end; if the Handle is 0 the process was not found.