Back to development
[platform/upstream/gstreamer.git] / examples / test-netclock.c
1 /* GStreamer
2  * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
3  * Copyright (C) 2014 Jan Schmidt <jan@centricular.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #include <gst/gst.h>
22
23 #include <gst/net/gstnettimeprovider.h>
24 #include <gst/rtsp-server/rtsp-server.h>
25
26 GstClock *global_clock;
27
28 #define TEST_TYPE_RTSP_MEDIA_FACTORY      (test_rtsp_media_factory_get_type ())
29 #define TEST_TYPE_RTSP_MEDIA              (test_rtsp_media_get_type ())
30
31 GType test_rtsp_media_factory_get_type (void);
32 GType test_rtsp_media_get_type (void);
33
34 static GstRTSPMediaFactory *test_rtsp_media_factory_new (void);
35 static GstElement *create_pipeline (GstRTSPMediaFactory * factory,
36     GstRTSPMedia * media);
37
38 typedef struct TestRTSPMediaFactoryClass TestRTSPMediaFactoryClass;
39 typedef struct TestRTSPMediaFactory TestRTSPMediaFactory;
40
41 struct TestRTSPMediaFactoryClass
42 {
43   GstRTSPMediaFactoryClass parent;
44 };
45
46 struct TestRTSPMediaFactory
47 {
48   GstRTSPMediaFactory parent;
49 };
50
51 typedef struct TestRTSPMediaClass TestRTSPMediaClass;
52 typedef struct TestRTSPMedia TestRTSPMedia;
53
54 struct TestRTSPMediaClass
55 {
56   GstRTSPMediaClass parent;
57 };
58
59 struct TestRTSPMedia
60 {
61   GstRTSPMedia parent;
62 };
63
64 GstRTSPMediaFactory *
65 test_rtsp_media_factory_new (void)
66 {
67   GstRTSPMediaFactory *result;
68
69   result = g_object_new (TEST_TYPE_RTSP_MEDIA_FACTORY, NULL);
70
71   return result;
72 }
73
74 G_DEFINE_TYPE (TestRTSPMediaFactory, test_rtsp_media_factory,
75     GST_TYPE_RTSP_MEDIA_FACTORY);
76
77 static gboolean custom_setup_rtpbin (GstRTSPMedia * media, GstElement * rtpbin);
78
79 static void
80 test_rtsp_media_factory_class_init (TestRTSPMediaFactoryClass * test_klass)
81 {
82   GstRTSPMediaFactoryClass *mf_klass =
83       (GstRTSPMediaFactoryClass *) (test_klass);
84   mf_klass->create_pipeline = create_pipeline;
85 }
86
87 static void
88 test_rtsp_media_factory_init (TestRTSPMediaFactory * factory)
89 {
90 }
91
92 static GstElement *
93 create_pipeline (GstRTSPMediaFactory * factory, GstRTSPMedia * media)
94 {
95   GstElement *pipeline;
96
97   pipeline = gst_pipeline_new ("media-pipeline");
98   gst_pipeline_use_clock (GST_PIPELINE (pipeline), global_clock);
99   gst_rtsp_media_take_pipeline (media, GST_PIPELINE_CAST (pipeline));
100
101   return pipeline;
102 }
103
104 G_DEFINE_TYPE (TestRTSPMedia, test_rtsp_media, GST_TYPE_RTSP_MEDIA);
105
106 static void
107 test_rtsp_media_class_init (TestRTSPMediaClass * test_klass)
108 {
109   GstRTSPMediaClass *klass = (GstRTSPMediaClass *) (test_klass);
110   klass->setup_rtpbin = custom_setup_rtpbin;
111 }
112
113 static void
114 test_rtsp_media_init (TestRTSPMedia * media)
115 {
116 }
117
118 static gboolean
119 custom_setup_rtpbin (GstRTSPMedia * media, GstElement * rtpbin)
120 {
121   g_object_set (rtpbin, "ntp-time-source", 3, NULL);
122   return TRUE;
123 }
124
125 int
126 main (int argc, char *argv[])
127 {
128   GMainLoop *loop;
129   GstRTSPServer *server;
130   GstRTSPMountPoints *mounts;
131   GstRTSPMediaFactory *factory;
132
133   gst_init (&argc, &argv);
134
135   if (argc < 2) {
136     g_print ("usage: %s <launch line> \n"
137         "example: %s \"( videotestsrc is-live=true ! x264enc ! rtph264pay name=pay0 pt=96 )\"\n"
138         "Pipeline must be live for synchronisation to work properly with this method!\n",
139         argv[0], argv[0]);
140     return -1;
141   }
142
143   loop = g_main_loop_new (NULL, FALSE);
144
145   global_clock = gst_system_clock_obtain ();
146   gst_net_time_provider_new (global_clock, "0.0.0.0", 8554);
147
148   /* create a server instance */
149   server = gst_rtsp_server_new ();
150
151   /* get the mount points for this server, every server has a default object
152    * that be used to map uri mount points to media factories */
153   mounts = gst_rtsp_server_get_mount_points (server);
154
155   /* make a media factory for a test stream. The default media factory can use
156    * gst-launch syntax to create pipelines.
157    * any launch line works as long as it contains elements named pay%d. Each
158    * element with pay%d names will be a stream */
159   factory = test_rtsp_media_factory_new ();
160   gst_rtsp_media_factory_set_launch (factory, argv[1]);
161   gst_rtsp_media_factory_set_shared (GST_RTSP_MEDIA_FACTORY (factory), TRUE);
162   gst_rtsp_media_factory_set_media_gtype (GST_RTSP_MEDIA_FACTORY (factory),
163       TEST_TYPE_RTSP_MEDIA);
164
165   /* attach the test factory to the /test url */
166   gst_rtsp_mount_points_add_factory (mounts, "/test", factory);
167
168   /* don't need the ref to the mapper anymore */
169   g_object_unref (mounts);
170
171   /* attach the server to the default maincontext */
172   gst_rtsp_server_attach (server, NULL);
173
174   /* start serving */
175   g_print ("stream ready at rtsp://127.0.0.1:8554/test\n");
176   g_main_loop_run (loop);
177
178   return 0;
179 }