Hi,
Please check if you can run like this sample:
Gstreamer decode live video stream with the delay difference between gst-launch-1.0 command and appsink callback - #6 by DaneLLL
Register a EoS callback to appsink and once you receive the EoS, you can terminate the gstreamer pipeline.