首页 > 代码库 > gst-rtsp-server 转发rtsp流

gst-rtsp-server 转发rtsp流

 //以下为rtsp的服务器A
1
#include <gst/gst.h> 2 3 #include <gst/rtsp-server/rtsp-server.h> 4 #include <gst/rtsp-server/rtsp-session-pool.h> 5 6 static gboolean 7 timeout (GstRTSPServer * server) 8 { 9 GstRTSPSessionPool *pool;10 11 pool = gst_rtsp_server_get_session_pool (server);12 13 gst_rtsp_session_pool_cleanup (pool);14 g_object_unref (pool);15 16 return TRUE;17 }18 19 int20 main (int argc, char *argv[])21 {22 GMainLoop *loop;23 GstRTSPServer *server;24 GstRTSPMountPoints *mounts;25 GstRTSPMediaFactory *factory;26 GstRTSPMediaFactory *factory1;27 GstRTSPSessionPool *session;28 29 gst_init (&argc, &argv);30 31 loop = g_main_loop_new (NULL, FALSE);32 33 session = gst_rtsp_session_pool_new();34 gst_rtsp_session_pool_set_max_sessions (session, 255);35 /* 创建服务器实例 */36 server = gst_rtsp_server_new ();37 38 /* 获取服务器的rtsp流的管理器*/39 mounts = gst_rtsp_server_get_mount_points (server);40 41 /* 创建两个rtsp的流管理器,设置流的源*/42 factory = gst_rtsp_media_factory_new ();43 factory1 = gst_rtsp_media_factory_new ();44 gst_rtsp_media_factory_set_launch (factory,45 "( videotestsrc is-live=1 ! x264enc ! rtph264pay name=pay0 pt=96 )");46 47 gst_rtsp_media_factory_set_launch (factory1,48 "( " 49 "filesrc location=/home/kkia/Downloads/My.mp4 ! qtdemux name=d "50 "d. ! queue ! rtph264pay pt=96 name=pay0 "51 "d. ! queue ! rtpmp4apay pt=97 name=pay1 " ")");52 53 gst_rtsp_media_factory_set_shared (factory, TRUE);54 gst_rtsp_media_factory_set_shared (factory1, TRUE);55 56 /*绑定流的地址,并加入流管理器中*/57 gst_rtsp_mount_points_add_factory (mounts, "/test", factory);58 gst_rtsp_mount_points_add_factory (mounts, "/test1", factory1);59 60 g_object_unref (mounts);61 62 gst_rtsp_server_attach (server, NULL); 63 64 g_timeout_add_seconds (2, (GSourceFunc) timeout, server);65 66 g_print ("stream ready at rtsp://127.0.0.1:8554/test\n");67 g_print ("stream ready at rtsp://127.0.0.1:8554/test1\n");68 g_main_loop_run (loop);69 70 return 0;71 }

以上为rtsp的服务器A。

下面将创建rtsp转发服务器B,转发服务器A的rtsp流。

 1 #include <gst/gst.h> 2  3 #include <gst/rtsp-server/rtsp-server.h> 4  5 int 6 main (int argc, char *argv[]) 7 { 8   GMainLoop *loop; 9   GstRTSPServer *server;10   GstRTSPMountPoints *mounts;11   GstRTSPMediaFactory *factory;12 13   gst_init (&argc, &argv);14 15   if (argc < 2) {16     g_print ("usage: %s <launch line> \n"17         "example: %s \"( videotestsrc ! x264enc ! rtph264pay name=pay0 pt=96 )\"\n",18         argv[0], argv[0]);19     return -1;20   }21 22   loop = g_main_loop_new (NULL, FALSE);23 24   /* create a server instance */25   server = gst_rtsp_server_new ();26   gst_rtsp_server_set_service (server, "8555");//配置服务器端口27   mounts = gst_rtsp_server_get_mount_points (server);28 29 30   factory = gst_rtsp_media_factory_new ();31   gst_rtsp_media_factory_set_launch (factory, "( rtspsrc  location=rtsp://192.168.11.36:8554/test1 ! queue ! rtph264depay ! queue ! rtph264pay name=pay0 pt=96 )"32 );//此处服务器的源来自主服务器的rtsp,ip地址改成相应的地址。33   gst_rtsp_media_factory_set_shared (factory, TRUE);34 35   gst_rtsp_mount_points_add_factory (mounts, "/test2", factory);36 37   g_object_unref (mounts);38 39   gst_rtsp_server_attach (server, NULL);40 41   /* start serving */42   g_print ("stream ready at rtsp://127.0.0.1:8554/test\n");43   g_main_loop_run (loop);44 45   return 0;46 }

 

代码可以在gstreamer中 gst-rtsp-server 源代码examples目录下获取到,只是稍加了修改。