rtsp-server: Use single-include rtsp header to make sure we get all definitions
[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_element_set_base_time (pipeline, 0);
100   gst_element_set_start_time (pipeline, GST_CLOCK_TIME_NONE);
101   gst_rtsp_media_take_pipeline (media, GST_PIPELINE_CAST (pipeline));
102
103   return pipeline;
104 }
105
106 G_DEFINE_TYPE (TestRTSPMedia, test_rtsp_media, GST_TYPE_RTSP_MEDIA);
107
108 static void
109 test_rtsp_media_class_init (TestRTSPMediaClass * test_klass)
110 {
111   GstRTSPMediaClass *klass = (GstRTSPMediaClass *) (test_klass);
112   klass->setup_rtpbin = custom_setup_rtpbin;
113 }
114
115 static void
116 test_rtsp_media_init (TestRTSPMedia * media)
117 {
118 }
119
120 static gboolean
121 custom_setup_rtpbin (GstRTSPMedia * media, GstElement * rtpbin)
122 {
123   g_object_set (rtpbin, "use-pipeline-clock", TRUE, NULL);
124   return TRUE;
125 }
126
127 int
128 main (int argc, char *argv[])
129 {
130   GMainLoop *loop;
131   GstRTSPServer *server;
132   GstRTSPMountPoints *mounts;
133   GstRTSPMediaFactory *factory;
134
135   gst_init (&argc, &argv);
136
137   if (argc < 2) {
138     g_print ("usage: %s <launch line> \n"
139         "example: %s \"( videotestsrc is-live=true ! x264enc ! rtph264pay name=pay0 pt=96 )\"\n"
140         "Pipeline must be live for synchronisation to work properly with this method!\n",
141         argv[0], argv[0]);
142     return -1;
143   }
144
145   loop = g_main_loop_new (NULL, FALSE);
146
147   global_clock = gst_system_clock_obtain ();
148   gst_net_time_provider_new (global_clock, "0.0.0.0", 8554);
149
150   /* create a server instance */
151   server = gst_rtsp_server_new ();
152
153   /* get the mount points for this server, every server has a default object
154    * that be used to map uri mount points to media factories */
155   mounts = gst_rtsp_server_get_mount_points (server);
156
157   /* make a media factory for a test stream. The default media factory can use
158    * gst-launch syntax to create pipelines.
159    * any launch line works as long as it contains elements named pay%d. Each
160    * element with pay%d names will be a stream */
161   factory = test_rtsp_media_factory_new ();
162   gst_rtsp_media_factory_set_launch (factory, argv[1]);
163   gst_rtsp_media_factory_set_shared (GST_RTSP_MEDIA_FACTORY (factory), TRUE);
164   gst_rtsp_media_factory_set_media_gtype (GST_RTSP_MEDIA_FACTORY (factory),
165       TEST_TYPE_RTSP_MEDIA);
166
167   /* attach the test factory to the /test url */
168   gst_rtsp_mount_points_add_factory (mounts, "/test", factory);
169
170   /* don't need the ref to the mapper anymore */
171   g_object_unref (mounts);
172
173   /* attach the server to the default maincontext */
174   gst_rtsp_server_attach (server, NULL);
175
176   /* start serving */
177   g_print ("stream ready at rtsp://127.0.0.1:8554/test\n");
178   g_main_loop_run (loop);
179
180   return 0;
181 }