gpt4 book ai didi

c++ - 关闭在线程上运行的 GStreamer RTSP 服务器

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:48:31 25 4
gpt4 key购买 nike

我编写了一个在线程上创建 RTSP 服务器的程序。我认为通过简单地退出 RTSP 服务器正在运行的循环(即 g_main_loop_quit(loop)),服务器将自行关闭并终止任何现有连接,但事实并非如此.

我使用 VLC 播放器预览 RTSP 流,我注意到当服务器线程退出时(当 main 仍在运行时),VLC 播放器仍然能够接收来自服务器的传输。如果我关闭播放器,我将无法再次连接到服务器。我没有“取消引用”或释放什么来保持服务器的事件?

#include <gst/gst.h>
#include <gst/rtsp-server/rtsp-server.h>
#include <stdlib.h>
#include <string>
#include <pthread.h>
#include <iostream>

using namespace std;

int thread_running = 1;

struct ThreadArgs {
GMainLoop *loop;
};

void *ServerThreadFunction(void *args) {
g_print("Starting server.\n");

ThreadArgs *ta = (ThreadArgs *)args;
GstRTSPServer *server;
GstRTSPMediaFactory *factory;
GstRTSPMountPoints *mounts;

gst_init(NULL, NULL);

server = gst_rtsp_server_new();
gst_rtsp_server_attach(server, NULL);

factory = gst_rtsp_media_factory_new();
gst_rtsp_media_factory_set_launch(factory, "( videotestsrc ! x264enc ! rtph264pay pt=96 name=pay0 )");

mounts = gst_rtsp_server_get_mount_points(server);
gst_rtsp_mount_points_add_factory(mounts, "/test", factory);
g_object_unref(mounts);

g_main_loop_run(ta->loop);

g_object_unref(server);
g_print("Terminating server.\n");
thread_running = 0;
pthread_exit(NULL);
}

void *KillServerFunction(void *args) {
ThreadArgs *ta = (ThreadArgs *)args;
cout << "Is the loop running? " << g_main_loop_is_running(ta->loop) << endl;
g_main_loop_quit(ta->loop);
pthread_exit(NULL);
}

int main (int argc, char *argv[]) {
ThreadArgs *ta = new ThreadArgs;
ta->loop = g_main_loop_new(NULL, FALSE);

pthread_t server_thread;
int rc = pthread_create(&server_thread, NULL, ServerThreadFunction, (void *)ta);

// Start VLC player here e.g. vlc rtsp://127.0.0.1/test

sleep(1);
cout << "Killing server in 3" << endl;
sleep(1);
cout << "Killing server in 2" << endl;
sleep(1);
cout << "Killing server in 1" << endl;
sleep(1);

pthread_t kill_server_thread;
pthread_create(&kill_server_thread, NULL, KillServerFunction, (void *)ta);

while (thread_running == 1) {}
cout << "Program continues to run but stream is still viewable on VLC player..." << endl;
sleep(100);

return 0;
}

最佳答案

请尝试以下操作:

需要调用g_source_remove();

创建服务器:

if ((gst_server_id=gst_rtsp_server_attach (gst_server, NULL)) == 0)
goto failed;

停止服务器:

 g_source_remove (gst_server_id);

此外,来自 gstreamer-rtsp-server docs :

Note that if context is not the default main context as returned by g_main_context_default() (or NULL), g_source_remove() cannot be used to destroy the source. In that case it is recommended to use gst_rtsp_server_create_source() and attach it to context manually.

关于c++ - 关闭在线程上运行的 GStreamer RTSP 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33600762/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com