The startup time of gst-rtsp-server is too long

I called gst-rtsp-server to push stream through udp client and server communication. I encapsulated the push stream module into a function, and then called it according to the instructions sent by the client in the main function. But I found that it took 10 seconds to complete every switch of push stream instruction to push stream successfully. What can I do to reduce the time it takes to switch push commands

I’m not sure I understand what you’re doing exactly.

Please provide more information, or some minimal example code.

Specifically, I enable udp server listening on the main thread with the following code,For communication and reuse between threads, GST-related variables are all defined as global variables.

int main(void)
//define two threads
pthread_t stream_thread1, stream_thread2;
        // Receive data to store in buf
        udpServer_waitDataConnect_timeout(&server, &connect, buf, 128, 5, 0);
        //if true execute the following code
        if(strcmp(cleaned_buf, "udpClient test5") == 0 )
                    int inst_num1 = 1;
                    //use thread1 to execute the instruction_switch function based on the argument inst_num1,which represents a push flow instruction
                    pthread_create(&stream_thread1, NULL, instruction_switch, &inst_num1);
            //if true execute the following code
        if(strcmp(cleaned_buf, "udpClient test20") == 0 )
                    int inst_num2 = 2;
                    //g_source_remove (id);
                    //g_main_loop_quit (loop);
                    //cancel thread 1, but it does not work, and the stream is still pushed
                    //use thread2 to execute the instruction_switch function based on the argument inst_num2,which represents another push flow instruction
                    pthread_create(&stream_thread2, NULL, instruction_switch, &inst_num2);

I wrapped the push flow module into a function, as follows:

int instruction_switch(int *instNum)
  printf("%s", "into instruction_switch\n");
  char *instruction4 = "( v4l2src device=/dev/video11 ! video/x-raw,format=NV12,width=1920,height=1080 !  mpph264enc bps-max=1000 ! rtph264pay name=pay0 pt=96 )";
  char *instruction5 = "(videomixer name=mix sink_0::alpha=1 sink_0::xpos=0 sink_0::ypos=0 sink_1::alpha=1 sink_1::xpos=960 sink_1::ypos=0 ! videoconvert ! mpph264enc bps-max=1000 ! h264parse ! rtph264pay name=pay0 pt=96 v4l2src device=/dev/video11 ! video/x-raw,format=NV12,width=1920,height=1080 ! videoscale ! video/x-raw,width=960,height=540 ! videorate ! video/x-raw,framerate=25/1 ! mix.sink_0 v4l2src device=/dev/video12 ! video/x-raw,format=NV12,width=1920,height=1080 ! videoscale ! video/x-raw,width=960,height=540 ! videorate ! video/x-raw,framerate=25/1 ! mix.sink_1)";
  //Execute different push flow instructions according to the received instruction
  if(*instNum == 1){
    g_print ("instNum is 1\n");
    gst_rtsp_media_factory_set_launch (factory, instruction4);
    g_print ("--------instNum is 1 end--------\n");
  else if(*instNum == 2){
    g_print ("instNum is 2\n");
    gst_rtsp_media_factory_set_launch (factory, instruction5);
    g_print ("--------instNum is 2 end--------\n");
  gst_rtsp_mount_points_add_factory (mounts, "/test", factory);
  g_print ("--------gst_rtsp_mount_points_add_factory--------\n");
  id = gst_rtsp_server_attach (gst_server, NULL);
  g_print ("--------gst_rtsp_server_attach--------\n");
  g_main_loop_run (loop);
  //g_main_loop_quit (loop);
  g_print ("--------g_main_loop_run_end--------\n");
  return 0;

I run into the following problem:

  1. When I run the program, from triggering the first judgment to execute the push stream instruction 1, to receiving the data in the pull stream side and displaying the screen, it takes about 10 seconds. What is the reason for this? I urgently need to reduce the time.
  2. When the second judgment is triggered to execute push 2, I need to close push 1, because I need to use the same url path, I try to use the thread cancellation function pthread_cancel(stream_thread1), but it fails, it has no effect, push 1 is still going on. I tried to use GST-related functions to update directives, such as g_source_remove (id), g_main_loop_quit (loop), gst_rtsp_mount_points_remove_factory (mounts, “/test”), but I’m not sure if these functions should go on the main thread or child thread. I tested but nothing worked. In particular, use the function gst_rtsp_mount_points_remove_factory (mounts, “/test”), I got an error: GLib-GObject-CRITICAL: 06:14:25.979: g_object_unref: assertion ‘G_IS_OBJECT (object)’ failed.
    I am very broken now, I don’t know how to solve my problem, hope for guidance.

I also tested that when I turned the server off and on or closed the loop and started the loop again, and the pull terminal disconnected the pull stream and pulled the stream again, I found that the instruction switch was successful. But it still takes a long time, it takes 4s, and it needs to manually restart the pull stream, which is not what I want. I want to control it only through the server (push stream), not through the client (pull stream)

Can you help me analyze this problem

Hello, do you understand this problem