examples: Add command-line parsing and take a 'port' argument

This allows users to run multiple servers on different ports for testing.

Only done for examples that actually take arguments and hence are capable of
outputting different streams for each instance on each port.

https://bugzilla.gnome.org/show_bug.cgi?id=742115
This commit is contained in:
Nirbheek Chauhan 2014-12-30 18:13:49 +05:30 committed by Tim-Philipp Müller
parent 79e41bc2be
commit e0d3807cbb
4 changed files with 81 additions and 14 deletions

View file

@ -21,6 +21,16 @@
#include <gst/rtsp-server/rtsp-server.h> #include <gst/rtsp-server/rtsp-server.h>
#define DEFAULT_RTSP_PORT "8554"
static char *port = (char *) DEFAULT_RTSP_PORT;
static GOptionEntry entries[] = {
{"port", 'p', 0, G_OPTION_ARG_STRING, &port,
"Port to listen on (default: " DEFAULT_RTSP_PORT ")", "PORT"},
{NULL}
};
int int
main (int argc, char *argv[]) main (int argc, char *argv[])
{ {
@ -28,20 +38,26 @@ main (int argc, char *argv[])
GstRTSPServer *server; GstRTSPServer *server;
GstRTSPMountPoints *mounts; GstRTSPMountPoints *mounts;
GstRTSPMediaFactory *factory; GstRTSPMediaFactory *factory;
GOptionContext *optctx;
GError *error = NULL;
gst_init (&argc, &argv); gst_init (&argc, &argv);
if (argc < 2) { optctx = g_option_context_new ("<launch line> - Test RTSP Server, Launch\n\n"
g_print ("usage: %s <launch line> \n" "Example: \"( videotestsrc ! x264enc ! rtph264pay name=pay0 pt=96 )\"");
"example: %s \"( videotestsrc ! x264enc ! rtph264pay name=pay0 pt=96 )\"\n", g_option_context_add_main_entries (optctx, entries, NULL);
argv[0], argv[0]); 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);
return -1; return -1;
} }
g_option_context_free (optctx);
loop = g_main_loop_new (NULL, FALSE); loop = g_main_loop_new (NULL, FALSE);
/* create a server instance */ /* create a server instance */
server = gst_rtsp_server_new (); server = gst_rtsp_server_new ();
g_object_set (server, "service", port, NULL);
/* get the mount points for this server, every server has a default object /* get the mount points for this server, every server has a default object
* that be used to map uri mount points to media factories */ * that be used to map uri mount points to media factories */
@ -64,7 +80,7 @@ main (int argc, char *argv[])
gst_rtsp_server_attach (server, NULL); gst_rtsp_server_attach (server, NULL);
/* start serving */ /* start serving */
g_print ("stream ready at rtsp://127.0.0.1:8554/test\n"); g_print ("stream ready at rtsp://127.0.0.1:%s/test\n", port);
g_main_loop_run (loop); g_main_loop_run (loop);
return 0; return 0;

View file

@ -21,6 +21,16 @@
#include <gst/rtsp-server/rtsp-server.h> #include <gst/rtsp-server/rtsp-server.h>
#define DEFAULT_RTSP_PORT "8554"
static char *port = (char *) DEFAULT_RTSP_PORT;
static GOptionEntry entries[] = {
{"port", 'p', 0, G_OPTION_ARG_STRING, &port,
"Port to listen on (default: " DEFAULT_RTSP_PORT ")", "PORT"},
{NULL}
};
/* called when a stream has received an RTCP packet from the client */ /* called when a stream has received an RTCP packet from the client */
static void static void
on_ssrc_active (GObject * session, GObject * source, GstRTSPMedia * media) on_ssrc_active (GObject * session, GObject * source, GstRTSPMedia * media)
@ -83,19 +93,26 @@ main (int argc, char *argv[])
GstRTSPServer *server; GstRTSPServer *server;
GstRTSPMountPoints *mounts; GstRTSPMountPoints *mounts;
GstRTSPMediaFactory *factory; GstRTSPMediaFactory *factory;
GOptionContext *optctx;
GError *error = NULL;
gchar *str; gchar *str;
gst_init (&argc, &argv); gst_init (&argc, &argv);
if (argc < 2) { optctx = g_option_context_new ("<filename.mp4> - Test RTSP Server, MP4");
g_message ("usage: %s <filename.mp4>", argv[0]); 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);
return -1; return -1;
} }
g_option_context_free (optctx);
loop = g_main_loop_new (NULL, FALSE); loop = g_main_loop_new (NULL, FALSE);
/* create a server instance */ /* create a server instance */
server = gst_rtsp_server_new (); server = gst_rtsp_server_new ();
g_object_set (server, "service", port, NULL);
/* get the mount points for this server, every server has a default object /* get the mount points for this server, every server has a default object
* that be used to map uri mount points to media factories */ * that be used to map uri mount points to media factories */
@ -126,7 +143,7 @@ main (int argc, char *argv[])
gst_rtsp_server_attach (server, NULL); gst_rtsp_server_attach (server, NULL);
/* start serving */ /* start serving */
g_print ("stream ready at rtsp://127.0.0.1:8554/test\n"); g_print ("stream ready at rtsp://127.0.0.1:%s/test\n", port);
g_main_loop_run (loop); g_main_loop_run (loop);
return 0; return 0;

View file

@ -21,6 +21,16 @@
#include <gst/rtsp-server/rtsp-server.h> #include <gst/rtsp-server/rtsp-server.h>
#define DEFAULT_RTSP_PORT "8554"
static char *port = (char *) DEFAULT_RTSP_PORT;
static GOptionEntry entries[] = {
{"port", 'p', 0, G_OPTION_ARG_STRING, &port,
"Port to listen on (default: " DEFAULT_RTSP_PORT ")", "PORT"},
{NULL}
};
int int
main (int argc, char *argv[]) main (int argc, char *argv[])
{ {
@ -28,19 +38,26 @@ main (int argc, char *argv[])
GstRTSPServer *server; GstRTSPServer *server;
GstRTSPMountPoints *mounts; GstRTSPMountPoints *mounts;
GstRTSPMediaFactory *factory; GstRTSPMediaFactory *factory;
GOptionContext *optctx;
GError *error = NULL;
gchar *str; gchar *str;
gst_init (&argc, &argv); gst_init (&argc, &argv);
if (argc < 2) { optctx = g_option_context_new ("<filename.ogg> - Test RTSP Server, OGG");
g_message ("usage: %s <filename.ogg>", argv[0]); 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);
return -1; return -1;
} }
g_option_context_free (optctx);
loop = g_main_loop_new (NULL, FALSE); loop = g_main_loop_new (NULL, FALSE);
/* create a server instance */ /* create a server instance */
server = gst_rtsp_server_new (); server = gst_rtsp_server_new ();
g_object_set (server, "service", port, NULL);
/* get the mount points for this server, every server has a default object /* get the mount points for this server, every server has a default object
* that be used to map uri mount points to media factories */ * that be used to map uri mount points to media factories */
@ -69,7 +86,7 @@ main (int argc, char *argv[])
gst_rtsp_server_attach (server, NULL); gst_rtsp_server_attach (server, NULL);
/* start serving */ /* start serving */
g_print ("stream ready at rtsp://127.0.0.1:8554/test\n"); g_print ("stream ready at rtsp://127.0.0.1:%s/test\n", port);
g_main_loop_run (loop); g_main_loop_run (loop);
return 0; return 0;

View file

@ -22,6 +22,16 @@
#include <gst/rtsp-server/rtsp-server.h> #include <gst/rtsp-server/rtsp-server.h>
#include <gst/rtsp-server/rtsp-media-factory-uri.h> #include <gst/rtsp-server/rtsp-media-factory-uri.h>
#define DEFAULT_RTSP_PORT "8554"
static char *port = (char *) DEFAULT_RTSP_PORT;
static GOptionEntry entries[] = {
{"port", 'p', 0, G_OPTION_ARG_STRING, &port,
"Port to listen on (default: " DEFAULT_RTSP_PORT ")", "PORT"},
{NULL}
};
static gboolean static gboolean
timeout (GstRTSPServer * server) timeout (GstRTSPServer * server)
@ -55,18 +65,25 @@ main (int argc, char *argv[])
GstRTSPServer *server; GstRTSPServer *server;
GstRTSPMountPoints *mounts; GstRTSPMountPoints *mounts;
GstRTSPMediaFactoryURI *factory; GstRTSPMediaFactoryURI *factory;
GOptionContext *optctx;
GError *error = NULL;
gst_init (&argc, &argv); gst_init (&argc, &argv);
if (argc < 2) { optctx = g_option_context_new ("<uri> - Test RTSP Server, URI");
g_message ("usage: %s <uri>", argv[0]); 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);
return -1; return -1;
} }
g_option_context_free (optctx);
loop = g_main_loop_new (NULL, FALSE); loop = g_main_loop_new (NULL, FALSE);
/* create a server instance */ /* create a server instance */
server = gst_rtsp_server_new (); server = gst_rtsp_server_new ();
g_object_set (server, "service", port, NULL);
/* get the mount points for this server, every server has a default object /* get the mount points for this server, every server has a default object
* that be used to map uri mount points to media factories */ * that be used to map uri mount points to media factories */
@ -100,7 +117,7 @@ main (int argc, char *argv[])
g_timeout_add_seconds (10, (GSourceFunc) remove_map, server); g_timeout_add_seconds (10, (GSourceFunc) remove_map, server);
/* start serving */ /* start serving */
g_print ("stream ready at rtsp://127.0.0.1:8554/test\n"); g_print ("stream ready at rtsp://127.0.0.1:%s/test\n", port);
g_main_loop_run (loop); g_main_loop_run (loop);
return 0; return 0;