Finding server port In RTSP server base on RTP with UDP transport

Hi Guys,
I want to find out Server port is RTSP server with Gstreamer when UDP is selected as Transport. This my source code it just shows Client port not server port(server port is shown = -1).

#include <gst/gst.h>
#include <gst/rtsp-server/rtsp-server.h>
#include <stdio.h>
static gboolean timeout(GstRTSPServer *server) {
    GstRTSPSessionPool *pool;

    pool = gst_rtsp_server_get_session_pool(server);
    gst_rtsp_session_pool_cleanup(pool);
    g_object_unref(pool);

    return TRUE;
}
GstRTSPMediaFactory *Factory;
void static media_configure_handler(GstRTSPMediaFactory *factory,
                                    GstRTSPMedia *media, void *This) {

    printf("\n media_configure_handler \n");
    GstRTSPContext *ctx;
    ctx = gst_rtsp_context_get_current();
    if (ctx == NULL) {
        printf("\n No current RTSP context\n");
        return;
    }
    const GstRTSPTransport *rtsp_transport;
    if(ctx->trans)
    {
        rtsp_transport = gst_rtsp_stream_transport_get_transport (ctx->trans);
        if (!rtsp_transport) {
            g_print ("No RTSP transport found\n");

        }
        else
        {
            if (rtsp_transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP) {
                g_print ("Server RTP min port: %d\n", rtsp_transport->server_port.min);
                g_print ("Server RTP max port: %d\n", rtsp_transport->server_port.max);
                g_print ("client RTP min port: %d\n", rtsp_transport->client_port.min);
                g_print ("client RTP max port: %d\n", rtsp_transport->client_port.max);
            } else {
                g_print ("Not using UDP transport\n");
            }
        }
    }

}
static GstRTSPStatusCode *
setup_request_callback (GstRTSPClient * self,
                        GstRTSPContext * ctx,
                        gpointer user_data)
{

    printf("\n setup_request_callback\n");
    const GstRTSPTransport *rtsp_transport;
    if(ctx->trans)
    {
        rtsp_transport = gst_rtsp_stream_transport_get_transport (ctx->trans);
        if (!rtsp_transport) {
            g_print ("No RTSP transport found\n");

        }
        else
        {
            if (rtsp_transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP) {
                g_print ("Server RTP min port: %d\n", rtsp_transport->server_port.min);
                g_print ("Server RTP max port: %d\n", rtsp_transport->server_port.max);
                g_print ("client RTP min port: %d\n", rtsp_transport->client_port.min);
                g_print ("client RTP max port: %d\n", rtsp_transport->client_port.max);
            } else {
                g_print ("Not using UDP transport\n");
            }
        }
    }
}
static GstRTSPStatusCode *
play_request_callback (GstRTSPClient * self,
                       GstRTSPContext * ctx,
                       gpointer user_data)
{

    printf("\n pre_play_request_callback\n");
    g_signal_connect(Factory, "media_configure",
                     G_CALLBACK(media_configure_handler), nullptr);
    GstRTSPStreamTransport *transport;
    const GstRTSPTransport *rtsp_transport;

    if(ctx->trans)
    {
        rtsp_transport = gst_rtsp_stream_transport_get_transport (ctx->trans);
        if (!rtsp_transport) {
            g_print ("No RTSP transport found\n");

        }
        else
        {
            if (rtsp_transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP) {
                g_print ("Server RTP min port: %d\n", rtsp_transport->server_port.min);
                g_print ("Server RTP max port: %d\n", rtsp_transport->server_port.max);
                g_print ("client RTP min port: %d\n", rtsp_transport->client_port.min);
                g_print ("client RTP max port: %d\n", rtsp_transport->client_port.max);
            } else {
                g_print ("Not using UDP transport\n");
            }
        }
    }

    //        // Check if it's UDP

}
static void DescribeRequest(GstRTSPClient  *gstrtspclient, GstRTSPContext *arg1, gpointer user_data)
{
    gchar *urlString = gst_rtsp_url_get_request_uri(arg1->uri);



    printf(" DescribeRequest - urlString =  %s",urlString);



}


static GstRTSPStatusCode
pre_setup_request (GstRTSPClient * client,
                   GstRTSPContext * ctx)
{
    printf("\n pre- setup_request_callback\n");
    GstRTSPStreamTransport *transport;
    const GstRTSPTransport *rtsp_transport;
    if(ctx->trans)
    {
        rtsp_transport = gst_rtsp_stream_transport_get_transport (ctx->trans);
        if (!rtsp_transport) {
            g_print ("No RTSP transport found\n");

        }
        else
        {
            if (rtsp_transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP) {
                g_print ("Server RTP min port: %d\n", rtsp_transport->server_port.min);
                g_print ("Server RTP max port: %d\n", rtsp_transport->server_port.max);
                g_print ("client RTP min port: %d\n", rtsp_transport->client_port.min);
                g_print ("client RTP max port: %d\n", rtsp_transport->client_port.max);
            } else {
                g_print ("Not using UDP transport\n");
            }
        }
    }
}


static void handle_response_callback (GstRTSPClient * self,
                                      GstRTSPContext * ctx,
                                      gpointer user_data)
{
    printf("\n handle_response_callback \n");
    GstRTSPStreamTransport *transport;
    const GstRTSPTransport *rtsp_transport;
    if(ctx->trans)
    {
        rtsp_transport = gst_rtsp_stream_transport_get_transport (ctx->trans);
        if (!rtsp_transport) {
            g_print ("No RTSP transport found\n");

        }
        else
        {
            if (rtsp_transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP) {
                g_print ("Server RTP min port: %d\n", rtsp_transport->server_port.min);
                g_print ("Server RTP max port: %d\n", rtsp_transport->server_port.max);
                g_print ("client RTP min port: %d\n", rtsp_transport->client_port.min);
                g_print ("client RTP max port: %d\n", rtsp_transport->client_port.max);
            } else {
                g_print ("Not using UDP transport\n");
            }
        }
    }
}
void static closed_callback (GstRTSPClient * self,
                 gpointer user_data)
{
    printf("\n closed_callback \n");
    GstRTSPStreamTransport *transport;
    const GstRTSPTransport *rtsp_transport;
    GstRTSPContext *ctx;


    // Get the current RTSP context
    ctx = gst_rtsp_context_get_current();
    if (ctx == NULL) {
        printf("\n No current RTSP context\n");
        return;
    }
    if(ctx->trans)
    {
        rtsp_transport = gst_rtsp_stream_transport_get_transport (ctx->trans);
        if (!rtsp_transport) {
            g_print ("No RTSP transport found\n");

        }
        else
        {
            if (rtsp_transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP) {
                g_print ("Server RTP min port: %d\n", rtsp_transport->server_port.min);
                g_print ("Server RTP max port: %d\n", rtsp_transport->server_port.max);
                g_print ("client RTP min port: %d\n", rtsp_transport->client_port.min);
                g_print ("client RTP max port: %d\n", rtsp_transport->client_port.max);
            } else {
                g_print ("Not using UDP transport\n");
            }
        }
    }
}
void static set_parameter_request_callback (GstRTSPClient * self,
                                            GstRTSPContext * ctx,
                                            gpointer user_data)
{
    printf("\n set_parameter_request_callback \n");
    GstRTSPStreamTransport *transport;
    const GstRTSPTransport *rtsp_transport;
    if(ctx->trans)
    {
        rtsp_transport = gst_rtsp_stream_transport_get_transport (ctx->trans);
        if (!rtsp_transport) {
            g_print ("No RTSP transport found\n");

        }
        else
        {
            if (rtsp_transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP) {
                g_print ("Server RTP min port: %d\n", rtsp_transport->server_port.min);
                g_print ("Server RTP max port: %d\n", rtsp_transport->server_port.max);
                g_print ("client RTP min port: %d\n", rtsp_transport->client_port.min);
                g_print ("client RTP max port: %d\n", rtsp_transport->client_port.max);
            } else {
                g_print ("Not using UDP transport\n");
            }
        }
    }
}
void static get_parameter_request_callback (GstRTSPClient * self,
                                            GstRTSPContext * ctx,
                                            gpointer user_data)
{
    printf("\n get_parameter_request_callback \n");
    GstRTSPStreamTransport *transport;
    const GstRTSPTransport *rtsp_transport;
    if(ctx->trans)
    {
        rtsp_transport = gst_rtsp_stream_transport_get_transport (ctx->trans);

        if (!rtsp_transport) {
            g_print ("No RTSP transport found\n");

        }
        else
        {
            if (rtsp_transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP) {
                g_print ("Server RTP min port: %d\n", rtsp_transport->server_port.min);
                g_print ("Server RTP max port: %d\n", rtsp_transport->server_port.max);
                g_print ("client RTP min port: %d\n", rtsp_transport->client_port.min);
                g_print ("client RTP max port: %d\n", rtsp_transport->client_port.max);
            } else {
                g_print ("Not using UDP transport\n");
            }
        }
    }
}





static void new_session_callback (GstRTSPClient * self,
                                  GstRTSPSession * object,
                                  gpointer user_data)
{

    GstRTSPContext *ctx;


    // Get the current RTSP context
    ctx = gst_rtsp_context_get_current();
    if (ctx == NULL) {
        printf("\n No current RTSP context\n");
        return;
    }
    printf("\n new_session_callback \n");
    GstRTSPStreamTransport *transport;
    const GstRTSPTransport *rtsp_transport;
    if(ctx->trans)
    {
        rtsp_transport = gst_rtsp_stream_transport_get_transport (ctx->trans);
        if (!rtsp_transport) {
            g_print ("No RTSP transport found\n");

        }
        else
        {
            if (rtsp_transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP) {
                g_print ("Server RTP min port: %d\n", rtsp_transport->server_port.min);
                g_print ("Server RTP max port: %d\n", rtsp_transport->server_port.max);
                g_print ("client RTP min port: %d\n", rtsp_transport->client_port.min);
                g_print ("client RTP max port: %d\n", rtsp_transport->client_port.max);
            } else {
                g_print ("Not using UDP transport\n");
            }
        }
    }

}

static void options_request_callback (GstRTSPClient * self,
                                      GstRTSPContext * ctx,
                                      gpointer user_data)
{
    printf("\n options_request_callback \n");
    GstRTSPStreamTransport *transport;
    const GstRTSPTransport *rtsp_transport;
    if(ctx->trans)
    {
        rtsp_transport = gst_rtsp_stream_transport_get_transport (ctx->trans);
        if (!rtsp_transport) {
            g_print ("No RTSP transport found\n");

        }
        else
        {
            if (rtsp_transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP) {
                g_print ("Server RTP min port: %d\n", rtsp_transport->server_port.min);
                g_print ("Server RTP max port: %d\n", rtsp_transport->server_port.max);
                g_print ("client RTP min port: %d\n", rtsp_transport->client_port.min);
                g_print ("client RTP max port: %d\n", rtsp_transport->client_port.max);
            } else {
                g_print ("Not using UDP transport\n");
            }
        }
    }
}

static void teardown_request_callback (GstRTSPClient * self,
                                       GstRTSPContext * ctx,
                                       gpointer user_data)
{
    printf("\n teardown_request_callback \n");
    GstRTSPStreamTransport *transport;
    const GstRTSPTransport *rtsp_transport;
    if(ctx->trans)
    {
        rtsp_transport = gst_rtsp_stream_transport_get_transport (ctx->trans);
        if (!rtsp_transport) {
            g_print ("No RTSP transport found\n");

        }
        else
        {
            if (rtsp_transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP) {
                g_print ("Server RTP min port: %d\n", rtsp_transport->server_port.min);
                g_print ("Server RTP max port: %d\n", rtsp_transport->server_port.max);
                g_print ("client RTP min port: %d\n", rtsp_transport->client_port.min);
                g_print ("client RTP max port: %d\n", rtsp_transport->client_port.max);
            } else {
                g_print ("Not using UDP transport\n");
            }
        }
    }


}
static void send_message_callback (GstRTSPClient * self,
                                   GstRTSPSession * session,
                                   gpointer message,
                                   gpointer user_data)
{
    GstRTSPContext *ctx;

    printf("\n send_message_callback %s \n",(char * )message);
    // Get the current RTSP context
    ctx = gst_rtsp_context_get_current();
    if (ctx == NULL) {
        printf("\n No current RTSP context\n");
        return;
    }

    GstRTSPStreamTransport *transport;
    const GstRTSPTransport *rtsp_transport;
    if(ctx->trans)
    {
        rtsp_transport = gst_rtsp_stream_transport_get_transport (ctx->trans);
        if (!rtsp_transport) {
            g_print ("No RTSP transport found\n");

        }
        else
        {
            if (rtsp_transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP) {
                g_print ("Server RTP min port: %d\n", rtsp_transport->server_port.min);
                g_print ("Server RTP max port: %d\n", rtsp_transport->server_port.max);
                g_print ("client RTP min port: %d\n", rtsp_transport->client_port.min);
                g_print ("client RTP max port: %d\n", rtsp_transport->client_port.max);
            } else {
                g_print ("Not using UDP transport\n");
            }
        }
    }
}
 static  GstRTSPStatusCode *
pre_setup_request_callback (GstRTSPClient * self,
                            GstRTSPContext * ctx,
                            gpointer user_data)
{
       printf("\n pre_setup_request_callback \n");
       const GstRTSPTransport *rtsp_transport;
       if(ctx->trans)
       {
           rtsp_transport = gst_rtsp_stream_transport_get_transport (ctx->trans);
           if (!rtsp_transport) {
               g_print ("No RTSP transport found\n");

           }
           else
           {
               if (rtsp_transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP) {
                   g_print ("Server RTP min port: %d\n", rtsp_transport->server_port.min);
                   g_print ("Server RTP max port: %d\n", rtsp_transport->server_port.max);
                   g_print ("client RTP min port: %d\n", rtsp_transport->client_port.min);
                   g_print ("client RTP max port: %d\n", rtsp_transport->client_port.max);
               } else {
                   g_print ("Not using UDP transport\n");
               }
           }
       }

}



static void ClientConnected(GstRTSPServer *gstrtspserver, GstRTSPClient *arg1, gpointer user_data)
{
    g_signal_connect(arg1, "setup-request", (GCallback)setup_request_callback, user_data);
    g_signal_connect(arg1, "closed", (GCallback)closed_callback, user_data);
    g_signal_connect(arg1, "handle-response", (GCallback)handle_response_callback, user_data);
    g_signal_connect(arg1, "get-parameter-request", (GCallback)get_parameter_request_callback, user_data);
    g_signal_connect(arg1, "set-parameter-request", (GCallback)set_parameter_request_callback, user_data);
    g_signal_connect(arg1, "play-request", (GCallback)play_request_callback, user_data);
    g_signal_connect(arg1, "teardown-request", (GCallback)teardown_request_callback, user_data);
    g_signal_connect(arg1, "new-session", (GCallback)new_session_callback, user_data);
    g_signal_connect(arg1, "options-request", (GCallback)options_request_callback, user_data);
    g_signal_connect(arg1, "send-message", (GCallback)send_message_callback, user_data);
    g_signal_connect(arg1, "describe-request", (GCallback)DescribeRequest, user_data);

}
int main(int argc, char *argv[]) {
    GMainLoop *loop;
    GstRTSPServer *server;
    GstRTSPMountPoints *mounts;
    GstRTSPMediaFactory *factory;
    GstRTSPAddressPool *pool;

    gst_init(&argc, &argv);

    loop = g_main_loop_new(NULL, FALSE);

    /* create a server instance */
    server = gst_rtsp_server_new();
    g_object_set(server, "service", "8554", NULL);

    /* get the mount points for this server, every server has a default object
   * that be used to map uri mount points to media factories */
    mounts = gst_rtsp_server_get_mount_points(server);

    /* make a media factory for a test stream. The default media factory can use
   * gst-launch syntax to create pipelines.
   * any launch line works as long as it contains elements named pay%d. Each
   * element with pay%d names will be a stream */
    factory = gst_rtsp_media_factory_new();
    gst_rtsp_media_factory_set_launch(
                factory,
                "videotestsrc ! video/x-raw,width=352,height=288,framerate=15/1 !  "
      "x264enc ! rtph264pay name=pay0 pt=96 ");

    gst_rtsp_media_factory_set_shared(factory, TRUE);
    /* only allow multicast */
    gst_rtsp_media_factory_set_protocols(
                factory, GstRTSPLowerTrans(GST_RTSP_LOWER_TRANS_UDP));

    /* attach the test factory to the /test url */
    gst_rtsp_mount_points_add_factory(mounts, "/test", factory);
    Factory = factory;

    /* don't need the ref to the mapper anymore */
    g_object_unref(mounts);

    /* attach the server to the default maincontext */
    if (gst_rtsp_server_attach(server, NULL) == 0) goto failed;

    g_timeout_add_seconds(300, (GSourceFunc)timeout, server);
    g_signal_connect(server, "client-connected", (GCallback)ClientConnected, nullptr);

    /* start serving */
    g_print("stream ready at rtsp://127.0.0.1:8554/test\n");
    g_main_loop_run(loop);

    return 0;

    /* ERRORS */
failed : {
        g_print("failed to attach the server\n");
        return -1;
    }
}

So which signal has information about the server port? A server port is selected in RTSP protocol when a setup request from client to server is established and server responses to client (here server port is selected).
“play-request” does not have transport information “play-request”
“pre-setup request” makes the server stop working “pre-setup-request”
“setup request” shows server port -1 “pre-setup-request”
“get-parameter-request” does not have information about the transport layer.
“set-parameter-request” does not have information about the transport layer.
“handle-response” signal hasn’t been called .why?
“send-message” signal is called multiple times just one time it shows server port -1
“options-request” signal does not have information about the transport layer.
The “new-session” signal does not have information about the transport layer.
Even a “teardown-request” signal does not have information about the transport layer.
Even a “closed” signal does not have information about the transport layer.(even doesn’t have RTSP context)
This is my client pipeline.

gst-launch-1.0 rtspsrc   location=rtsp://30.42.0.2:8554/test  ! rtph264depay  ! h264parse ! decodebin ! videoconvert  !  autovideosink  -v -e

Thanks so much.

Hi,
We would suggest run RTSP server through test-launch. Please refer to

Jetson AGX Orin FAQ
Q: Is there any example of running RTSP streaming?

The default port is 8554 and there is an option to configure it in test-launch.

And the port is set in RTSP URL, such as rtsp://30.42.0.2:8554/test

If you launch the server with different port number, please modify the URL accordingly.

Thanks for your attention.

No RTSP server port that default value is 8554.

I mean when RTSP server transport layer is set to udp in this situation RTSP doesn’t work with tcp and rtp is used. At first the client selects an udp port and in response the server selects a udp port in the setup phase of the RTSP server. I need to find out the server selected udp port for RTP when rtsp server is in udp transport config. I even found an udp client port but the server port is -1. I want to know about udp server port.

Thanks so much.

Hi,
We don’t know about this. Would see if other users can share experience.