基于gstreamer的支持动态获取多路流的rtsp server示例

一、简介

最近在做rtsp server相关的事情,调研了一些开源的服务器,大部分都是作为独立的进程启动,有点不符合自己的场景。偶然发现gstreamer,名气很大,但是用的人却很少。粗略百度了下相关资料--很少。大部分示例都是提供一路流,或者事先写死几路,没法根据自己播放访问时的url判断流存不存在并动态创建。后面花了点时间稍微了解了下,整理出一份动态创建流的代码,分享出来。

一共有两种方式能达到效果,一种采用main_loop_run运行在线程里,而创建factory等操作可在有需要的时候动态创建。代码基于gst自带的示例test-readme.c改造,编译同gst示例

二、方法一代码

#include <gst/gst.h>

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

void* testFun(void *args)
{
  GMainLoop *loop = (GMainLoop *) args;
  g_main_loop_run (loop);
}

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

  gst_init (&argc, &argv);

  loop = g_main_loop_new (NULL, FALSE);
  pthread_t tTest;
  pthread_create(&tTest, NULL, testFun, loop);

  /* create a server instance */
  server = gst_rtsp_server_new ();
  gst_rtsp_server_set_service (server, "8555");

  /* 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 is-live=1 ! x264enc ! rtph264pay name=pay0 pt=96 )");

  gst_rtsp_media_factory_set_shared (factory, TRUE);

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

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

  /* attach the server to the default maincontext */
  gst_rtsp_server_attach (server, NULL);

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

  return 0;
}

三、方法二的代码

#include <gst/gst.h>
#include <gst/rtsp-server/rtsp-server.h>

const char* port = "10001";

static void
handle_client (GstRTSPClient * client, GstRTSPContext * ctx,
    GstRTSPServer * server, gpointer user_data)
{
  GstRTSPClientClass *klass;
  GstRTSPMountPoints *mounts;
  GstRTSPMediaFactory *factory;
  GstRTSPUrl *uri;
  gchar *path;
  gchar *launch = "( videotestsrc is-live=1 ! x264enc ! rtph264pay name=pay0 pt=96 )";

  uri = ctx->uri;

  if (!uri)
    return;

  klass = GST_RTSP_CLIENT_GET_CLASS (client);
  path = klass->make_path_from_uri (client, uri);

  mounts = gst_rtsp_server_get_mount_points (server);
  factory = gst_rtsp_mount_points_match (mounts, path, NULL);
  if (!factory)
  {
    factory = gst_rtsp_media_factory_new ();
    gst_rtsp_media_factory_set_launch (factory, launch);
    gst_rtsp_media_factory_set_shared (factory, TRUE);

    //g_signal_connect (factory, "media-constructed", (GCallback)
      //                                              media_constructed, NULL);

    gst_rtsp_mount_points_add_factory (mounts, path, factory);
    g_print ("new factory: %s\n", launch);
  }
  else
  {
    g_object_unref (factory);
  }
  g_object_unref (mounts);
  g_free (path);
  //g_free (launch);
}

static void
client_connected (GstRTSPServer * server,
    GstRTSPClient * client, gpointer user_data)
{
  g_signal_connect_object (client, "options-request", (GCallback)
      handle_client, server, G_CONNECT_AFTER);
}

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;
}

int
main (int argc, char *argv[])
{
  GMainLoop *loop;
  GstRTSPServer *server;
  GError *error = NULL;

  gst_init (&argc, &argv);

  loop = g_main_loop_new (NULL, FALSE);

  /* create a server instance */
  server = gst_rtsp_server_new ();
  gst_rtsp_server_set_service (server, port);

  /* attach the server to the default maincontext */
  gst_rtsp_server_attach (server, NULL);

  g_signal_connect (server, "client-connected", (GCallback)
      client_connected, NULL);

  g_timeout_add_seconds (2, (GSourceFunc) timeout, server);

  g_object_unref (server);

  /* start serving */
  g_print ("rtsp://127.0.0.1:%s/\n", port);
  g_main_loop_run (loop);

  return 0;
}

由于偷懒,handle_client函数里直接用的teat-readme里的代码。如果想获取自定义的流,可以将改函数里的内容替换为test-appsrc相关代码

四、下载链接

https://download.csdn.net/download/yingyemin/11016282

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值