Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gst-rtsp-server: detect client disconnect

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?

like image 711
Nicolas Avatar asked Dec 06 '25 18:12

Nicolas


1 Answers

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));
. . . 
}

}

like image 169
Pavel Avatar answered Dec 11 '25 03:12

Pavel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!