2

I am implementing a video streaming pipeline using gst-rtsp-server. I need to know when an RTSP client both connects and disconnects.

From the examples provided with gst-rtsp-server, I can detect a client connecting using the "client-connected" signal of the GstRTSPServer. I'm looking for something similar for when the client disconnects.

I have tried the "closed" and "teardown-request" signals of GstRTSPClient, but those don't do anything when I disconnect the client.

I have also tried calling the following function on a timer, like it is done in several examples. I would expect that to print "Removed 1 sessions" at some point after I've terminated the client, but it never does.

static gboolean
remove_sessions (GstRTSPServer * server)
{
  GstRTSPSessionPool *pool;

  pool = gst_rtsp_server_get_session_pool (server);
  guint removed = gst_rtsp_session_pool_cleanup (pool);
  g_object_unref (pool);
  g_print("Removed %d sessions\n", removed);

  return TRUE;
}

My client is the following gstreamer pipeline:

gst-launch-1.0 -v rtspsrc location=rtsp://$STREAM_IP:8554/test latency=50 ! queue ! rtph264depay ! queue ! avdec_h264 ! autovideosink sync=false

How can I detect client disconnections?

Nicolas
  • 335
  • 3
  • 14
  • What do you mean exactly when you talk about "disconnecting the client"? In RTSP, you close the session by sending a TEARDOWN. Do you mean you close the TCP connection (using the FiN packet) or if the client abruptly closes the socket? – nielsdg Nov 09 '20 at 23:25
  • I'm interested in detecting both when the client loses network connection and when it disconnects intentionally. In my case, that would be `Ctrl+C`-ing the client pipeline. I would have expected that the `last_access` of `GstRTSPSession` would become stale in both situations and I thought that's what the session pool clean up looks at, but apparently not. – Nicolas Nov 10 '20 at 08:29

2 Answers2

1

Call gst_rtsp_server_client_filter() when need to close RTSP server (before server deletion):

    GstRTSPFilterResult clientFilterFunc(GstRTSPServer* server, GstRTSPClient* client, gpointer user)
{
    return GST_RTSP_FILTER_REMOVE;
}
. . . 
{
. . . 
    if( clientCount )
        gst_rtsp_server_client_filter(server, clientFilterFunc, nullptr);

    if (G_IS_OBJECT(server))
    {
        g_object_unref(server);
        server = nullptr;
    }
. . .
}

Code snipped for client connection and close: {

void clientClosed(GstRTSPClient* client, gpointer user)
{
    --clientCount ;
    std::stringstream strm;
    strm << "Client closed ... count: " << ptrTestData->m_clientCount << std::endl;
    g_print("%s", strm.str().c_str());
}

void clientConnected(GstRTSPServer* server, GstRTSPClient* client, gpointer user)
{
    ++clientCount ;
    // hook the client close callback
    g_signal_connect(client, "closed", reinterpret_cast<GCallback>(clientClosed), user);
    std::stringstream strm;
    strm << "Client connected ... count: " << ptrTestData->m_clientCount << std::endl;
    g_print("%s", strm.str().c_str());
}

{ 
 . . . 
     g_signal_connect(server, "client-connected", reinterpret_cast<GCallback>(clientConnected), &(testData));
. . . 
}

}

Pavel
  • 11
  • 2
0

Not sure what problems I had before, but this actually works:

When the client is shut down (Ctrl+C on the gst-launch-1.0 pipeline), the "teardown-request" signal of GstRTSPClient is emitted.

If the client loses connection to the server, the remove_sessions (GstRTSPServer * server) function I posted will report that it removed a session after some time.

Nicolas
  • 335
  • 3
  • 14