Skip to content

Instantly share code, notes, and snippets.

@tetkuz
Last active July 13, 2017 10:34
Show Gist options
  • Save tetkuz/2adbb8c87c1119769fbb2bccc297f233 to your computer and use it in GitHub Desktop.
Save tetkuz/2adbb8c87c1119769fbb2bccc297f233 to your computer and use it in GitHub Desktop.
RTSP Server with digest auth
/*
* Kiroru-cast is derived work from gst-rtsp-server examples[1].
* Because of this nature, Kiroru-cast is licensed under LGPL v2
*
* [1]: https://cgit.freedesktop.org/gstreamer/gst-rtsp-server/tree/examples
*/
/*
* Original license
*
* GStreamer
* Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <gst/gst.h>
#include <gst/rtsp-server/rtsp-server.h>
#define DEFAULT_RTSP_PORT "8554"
#define DEFAULT_RTSP_USER "user"
#define DEFAULT_RTSP_PASSWORD "password"
static char *port = (char *) DEFAULT_RTSP_PORT;
static char *user = (char *) DEFAULT_RTSP_USER;
static char *password = (char *) DEFAULT_RTSP_PASSWORD;
static GOptionEntry entries[] = {
{"port", 'p', 0, G_OPTION_ARG_STRING, &port, "Port to listen on (default: " DEFAULT_RTSP_PORT ")", "PORT"},
{"user", 'u', 0, G_OPTION_ARG_STRING, &user, "Username for the digest authorication (default: " DEFAULT_RTSP_USER ")", "USER"},
{"password", 'a', 0, G_OPTION_ARG_STRING, &password, "Password for the digest authorication (default: " DEFAULT_RTSP_PASSWORD ")", "PASSWORD"},
{NULL}
};
/* called when a stream has received an RTCP packet from the client */
static void
on_ssrc_active (GObject * session, GObject * source, GstRTSPMedia * media)
{
GstStructure *stats;
(void)media;
GST_INFO ("source %p in session %p is active", source, session);
g_object_get (source, "stats", &stats, NULL);
if (stats) {
gchar *sstr;
sstr = gst_structure_to_string (stats);
g_print ("structure: %s\n", sstr);
g_free (sstr);
gst_structure_free (stats);
}
}
static void
on_sender_ssrc_active (GObject * session, GObject * source,
GstRTSPMedia * media)
{
GstStructure *stats;
(void)media;
GST_INFO ("source %p in session %p is active", source, session);
g_object_get (source, "stats", &stats, NULL);
if (stats) {
gchar *sstr;
sstr = gst_structure_to_string (stats);
g_print ("Sender stats:\nstructure: %s\n", sstr);
g_free (sstr);
gst_structure_free (stats);
}
}
/* signal callback when the media is prepared for streaming. We can get the
* session manager for each of the streams and connect to some signals. */
static void
media_prepared_cb (GstRTSPMedia * media)
{
guint i, n_streams;
n_streams = gst_rtsp_media_n_streams (media);
GST_INFO ("media %p is prepared and has %u streams", media, n_streams);
for (i = 0; i < n_streams; i++) {
GstRTSPStream *stream;
GObject *session;
stream = gst_rtsp_media_get_stream (media, i);
if (stream == NULL)
continue;
session = gst_rtsp_stream_get_rtpsession (stream);
GST_INFO ("watching session %p on stream %u", session, i);
g_signal_connect (session, "on-ssrc-active",
(GCallback) on_ssrc_active, media);
g_signal_connect (session, "on-sender-ssrc-active",
(GCallback) on_sender_ssrc_active, media);
}
}
static void
media_configure_cb (GstRTSPMediaFactory * factory, GstRTSPMedia * media)
{
/* connect our prepared signal so that we can see when this media is
* prepared for streaming */
g_signal_connect (media, "prepared", (GCallback) media_prepared_cb, factory);
}
static gboolean
remove_func (GstRTSPSessionPool * pool, GstRTSPSession * session,
GstRTSPServer * server)
{
(void)pool;
(void)session;
(void)server;
return GST_RTSP_FILTER_REMOVE;
}
static gboolean
remove_sessions (GstRTSPServer * server)
{
GstRTSPSessionPool *pool;
g_print ("removing all sessions\n");
pool = gst_rtsp_server_get_session_pool (server);
gst_rtsp_session_pool_filter (pool,
(GstRTSPSessionPoolFilterFunc) remove_func, server);
g_object_unref (pool);
return FALSE;
}
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;
GstRTSPMountPoints *mounts;
GstRTSPMediaFactory *factory;
GstRTSPAuth *auth;
GstRTSPToken *token;
GOptionContext *optctx;
GError *error = NULL;
gchar *str;
optctx = g_option_context_new ("<filename.mp4> - Test RTSP Server, MP4");
g_option_context_add_main_entries (optctx, entries, NULL);
g_option_context_add_group (optctx, gst_init_get_option_group ());
if (!g_option_context_parse (optctx, &argc, &argv, &error)) {
g_printerr ("Error parsing options: %s\n", error->message);
g_option_context_free (optctx);
g_clear_error (&error);
return -1;
}
if (argc < 2) {
g_print ("%s\n", g_option_context_get_help (optctx, TRUE, NULL));
return 1;
}
g_option_context_free (optctx);
//gst_init (&argc, &argv);
loop = g_main_loop_new (NULL, FALSE);
/* create a server instance */
server = gst_rtsp_server_new ();
g_object_set (server, "service", port, NULL);
/* get the mounts for this server, every server has a default mapper object
* that be used to map uri mount points to media factories */
mounts = gst_rtsp_server_get_mount_points (server);
str = g_strdup_printf ("( "
"filesrc location=\"%s\" ! qtdemux name=d "
"d. ! queue ! rtph264pay pt=96 name=pay0 "
"d. ! queue ! rtpmp4apay pt=97 name=pay1 " ")", argv[1]);
g_print("-> %s\n", argv[1]);
/* 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, str);
g_signal_connect (factory, "media-configure", (GCallback) media_configure_cb,
factory);
g_free (str);
/* attach the test factory to the /test url */
gst_rtsp_mount_points_add_factory (mounts, "/sample", factory);
/* allow user and admin to access this resource */
gst_rtsp_media_factory_add_role (factory, "user",
GST_RTSP_PERM_MEDIA_FACTORY_ACCESS, G_TYPE_BOOLEAN, TRUE,
GST_RTSP_PERM_MEDIA_FACTORY_CONSTRUCT, G_TYPE_BOOLEAN, TRUE, NULL);
gst_rtsp_media_factory_add_role (factory, "anonymous",
GST_RTSP_PERM_MEDIA_FACTORY_ACCESS, G_TYPE_BOOLEAN, TRUE,
GST_RTSP_PERM_MEDIA_FACTORY_CONSTRUCT, G_TYPE_BOOLEAN, FALSE, NULL);
/* don't need the ref to the mapper anymore */
g_object_unref (mounts);
/* make a new authentication manager */
auth = gst_rtsp_auth_new ();
gst_rtsp_auth_set_supported_methods (auth, GST_RTSP_AUTH_DIGEST);
/* make default token, it has no permissions */
token =
gst_rtsp_token_new (GST_RTSP_TOKEN_MEDIA_FACTORY_ROLE, G_TYPE_STRING,
"anonymous", NULL);
gst_rtsp_auth_set_default_token (auth, token);
gst_rtsp_token_unref (token);
/* make user token */
token =
gst_rtsp_token_new (GST_RTSP_TOKEN_MEDIA_FACTORY_ROLE, G_TYPE_STRING,
"user", NULL);
gst_rtsp_auth_add_digest (auth, user, password, token);
gst_rtsp_token_unref (token);
/* set as the server authentication manager */
gst_rtsp_server_set_auth (server, auth);
g_object_unref (auth);
/* attach the server to the default maincontext */
if (gst_rtsp_server_attach (server, NULL) == 0)
goto failed;
g_timeout_add_seconds (2, (GSourceFunc) timeout, server);
g_timeout_add_seconds (10, (GSourceFunc) remove_sessions, server);
/* start serving */
g_print ("stream with %s:%s ready at rtsp://127.0.0.1:8554/sample\n", user, password);
g_main_loop_run (loop);
return 0;
/* ERRORS */
failed:
{
g_print ("failed to attach the server\n");
return -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment