我编写了一个在线程上创建RTSP服务器的程序。我认为,只要退出RTSP服务器正在运行的循环(即g_main_loop_quit(loop)),服务器就会自行关闭并终止任何现有的连接,但情况似乎并非如此。
我使用VLC player预览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;
}发布于 2017-09-13 09:12:28
请尝试如下:
需要调用g_source_remove();
创建服务器:
if ((gst_server_id=gst_rtsp_server_attach (gst_server, NULL)) == 0)
goto failed;停止服务器:
g_source_remove (gst_server_id);同时,来自gstreamer服务器文档
注意,如果上下文不是由()(或NULL)返回的默认主上下文,则不能使用g_source_remove()销毁源。在这种情况下,建议使用gst_rtsp_server_create_source()并手动将其附加到上下文中。
https://stackoverflow.com/questions/33600762
复制相似问题