1.4.5 migration and bug fixes
[platform/upstream/gst-rtsp-server.git] / gst / rtsp-server / rtsp-server-wfd.c
1 /* GStreamer
2  * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 /**
20  * SECTION:rtsp-server
21  * @short_description: The main server object
22  * @see_also: #GstRTSPClient, #GstRTSPThreadPool
23  *
24  * The server object is the object listening for connections on a port and
25  * creating #GstRTSPClient objects to handle those connections.
26  *
27  * The server will listen on the address set with gst_rtsp_server_set_address()
28  * and the port or service configured with gst_rtsp_server_set_service().
29  * Use gst_rtsp_server_set_backlog() to configure the amount of pending requests
30  * that the server will keep. By default the server listens on the current
31  * network (0.0.0.0) and port 8554.
32  *
33  * The server will require an SSL connection when a TLS certificate has been
34  * set in the auth object with gst_rtsp_auth_set_tls_certificate().
35  *
36  * To start the server, use gst_rtsp_server_attach() to attach it to a
37  * #GMainContext. For more control, gst_rtsp_server_create_source() and
38  * gst_rtsp_server_create_socket() can be used to get a #GSource and #GSocket
39  * respectively.
40  *
41  * gst_rtsp_server_transfer_connection() can be used to transfer an existing
42  * socket to the RTSP server, for example from an HTTP server.
43  *
44  * Once the server socket is attached to a mainloop, it will start accepting
45  * connections. When a new connection is received, a new #GstRTSPClient object
46  * is created to handle the connection. The new client will be configured with
47  * the server #GstRTSPAuth, #GstRTSPMountPoints, #GstRTSPSessionPool and
48  * #GstRTSPThreadPool.
49  *
50  * The server uses the configured #GstRTSPThreadPool object to handle the
51  * remainder of the communication with this client.
52  *
53  * Last reviewed on 2013-07-11 (1.0.0)
54  */
55 #include <stdlib.h>
56 #include <string.h>
57
58 #include "rtsp-server-wfd.h"
59 #include "rtsp-client-wfd.h"
60
61 #define GST_RTSP_WFD_SERVER_GET_PRIVATE(obj)  \
62        (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_WFD_SERVER, GstRTSPWFDServerPrivate))
63
64 #define GST_RTSP_WFD_SERVER_GET_LOCK(server)  (&(GST_RTSP_WFD_SERVER_CAST(server)->priv->lock))
65 #define GST_RTSP_WFD_SERVER_LOCK(server)      (g_mutex_lock(GST_RTSP_WFD_SERVER_GET_LOCK(server)))
66 #define GST_RTSP_WFD_SERVER_UNLOCK(server)    (g_mutex_unlock(GST_RTSP_WFD_SERVER_GET_LOCK(server)))
67
68 struct _GstRTSPWFDServerPrivate
69 {
70   GMutex lock;                  /* protects everything in this struct */
71
72   /* the clients that are connected */
73   GList *clients;
74   guint64 native_resolution;
75   guint64 supported_resolution;
76 };
77
78 G_DEFINE_TYPE (GstRTSPWFDServer, gst_rtsp_wfd_server, GST_TYPE_RTSP_SERVER);
79
80 GST_DEBUG_CATEGORY_STATIC (rtsp_wfd_server_debug);
81 #define GST_CAT_DEFAULT rtsp_wfd_server_debug
82
83 static void gst_rtsp_wfd_server_get_property (GObject * object, guint propid,
84     GValue * value, GParamSpec * pspec);
85 static void gst_rtsp_wfd_server_set_property (GObject * object, guint propid,
86     const GValue * value, GParamSpec * pspec);
87 static void gst_rtsp_wfd_server_finalize (GObject * object);
88
89 static GstRTSPClient *create_client_wfd (GstRTSPServer * server);
90 static void client_connected_wfd (GstRTSPServer * server,
91     GstRTSPClient * client);
92
93 static void
94 gst_rtsp_wfd_server_class_init (GstRTSPWFDServerClass * klass)
95 {
96   GObjectClass *gobject_class;
97   GstRTSPServerClass *rtsp_server_class;
98
99   g_type_class_add_private (klass, sizeof (GstRTSPWFDServerPrivate));
100
101   gobject_class = G_OBJECT_CLASS (klass);
102   rtsp_server_class = GST_RTSP_SERVER_CLASS (klass);
103
104   gobject_class->get_property = gst_rtsp_wfd_server_get_property;
105   gobject_class->set_property = gst_rtsp_wfd_server_set_property;
106   gobject_class->finalize = gst_rtsp_wfd_server_finalize;
107
108   rtsp_server_class->create_client = create_client_wfd;
109   rtsp_server_class->client_connected = client_connected_wfd;
110
111
112   GST_DEBUG_CATEGORY_INIT (rtsp_wfd_server_debug, "rtspwfdserver", 0,
113       "GstRTSPWFDServer");
114 }
115
116 static void
117 gst_rtsp_wfd_server_init (GstRTSPWFDServer * server)
118 {
119   GstRTSPWFDServerPrivate *priv = GST_RTSP_WFD_SERVER_GET_PRIVATE (server);
120
121   server->priv = priv;
122   server->priv->native_resolution = 0;
123   server->priv->supported_resolution = 1;
124   GST_INFO_OBJECT (server, "New server is initialized");
125 }
126
127 static void
128 gst_rtsp_wfd_server_finalize (GObject * object)
129 {
130   GstRTSPWFDServer *server = GST_RTSP_WFD_SERVER (object);
131   //GstRTSPWFDServerPrivate *priv = server->priv;
132
133   GST_DEBUG_OBJECT (server, "finalize server");
134
135   G_OBJECT_CLASS (gst_rtsp_wfd_server_parent_class)->finalize (object);
136 }
137
138 /**
139  * gst_rtsp_server_new:
140  *
141  * Create a new #GstRTSPWFDServer instance.
142  */
143 GstRTSPWFDServer *
144 gst_rtsp_wfd_server_new (void)
145 {
146   GstRTSPWFDServer *result;
147
148   result = g_object_new (GST_TYPE_RTSP_WFD_SERVER, NULL);
149
150   return result;
151 }
152
153 static void
154 gst_rtsp_wfd_server_get_property (GObject * object, guint propid,
155     GValue * value, GParamSpec * pspec)
156 {
157   //GstRTSPWFDServer *server = GST_RTSP_WFD_SERVER (object);
158
159   switch (propid) {
160     default:
161       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
162   }
163 }
164
165 static void
166 gst_rtsp_wfd_server_set_property (GObject * object, guint propid,
167     const GValue * value, GParamSpec * pspec)
168 {
169   //GstRTSPWFDServer *server = GST_RTSP_WFD_SERVER (object);
170
171   switch (propid) {
172     default:
173       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
174   }
175 }
176
177 static gboolean
178 _start_wfd (gpointer data)
179 {
180   GstRTSPWFDClient *client = (GstRTSPWFDClient *) data;
181
182   GST_INFO_OBJECT (client, "WFD client is STARTing");
183
184   gst_rtsp_wfd_client_start_wfd (client);
185   return FALSE;
186 }
187
188 static void
189 client_connected_wfd (GstRTSPServer * server, GstRTSPClient * client)
190 {
191   GST_INFO_OBJECT (server, "Client is connected");
192
193   g_idle_add (_start_wfd, client);
194   return;
195 }
196
197 static GstRTSPClient *
198 create_client_wfd (GstRTSPServer * server)
199 {
200   GstRTSPWFDClient *client;
201   GstRTSPThreadPool *thread_pool = NULL;
202   GstRTSPSessionPool *session_pool = NULL;
203   GstRTSPMountPoints *mount_points = NULL;
204   GstRTSPAuth *auth = NULL;
205   GstRTSPWFDServerPrivate *priv = GST_RTSP_WFD_SERVER_GET_PRIVATE(server);
206
207   GST_INFO_OBJECT (server, "New Client is being created");
208
209   /* a new client connected, create a session to handle the client. */
210   client = gst_rtsp_wfd_client_new ();
211
212   thread_pool = gst_rtsp_server_get_thread_pool (server);
213   session_pool = gst_rtsp_server_get_session_pool (server);
214   mount_points = gst_rtsp_server_get_mount_points (server);
215   auth = gst_rtsp_server_get_auth (server);
216
217   /* set the session pool that this client should use */
218   GST_RTSP_WFD_SERVER_LOCK (server);
219   gst_rtsp_client_set_session_pool (GST_RTSP_CLIENT_CAST (client),
220       session_pool);
221   /* set the mount points that this client should use */
222   gst_rtsp_client_set_mount_points (GST_RTSP_CLIENT_CAST (client),
223       mount_points);
224   /* set authentication manager */
225   gst_rtsp_client_set_auth (GST_RTSP_CLIENT_CAST (client), auth);
226   /* set threadpool */
227   gst_rtsp_client_set_thread_pool (GST_RTSP_CLIENT_CAST (client), thread_pool);
228
229   gst_rtsp_wfd_client_set_video_supported_resolution (client,
230         priv->supported_resolution);
231
232   gst_rtsp_wfd_client_set_video_native_resolution (client,
233         priv->native_resolution);
234
235   GST_RTSP_WFD_SERVER_UNLOCK (server);
236
237   return GST_RTSP_CLIENT (client);
238 }
239
240 GstRTSPResult
241 gst_rtsp_wfd_server_trigger_request (GstRTSPServer * server,
242     GstWFDTriggerType type)
243 {
244   GstRTSPResult res = GST_RTSP_OK;
245   GList *clients, *walk, *next;
246
247   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), GST_RTSP_ERROR);
248
249   clients = gst_rtsp_server_client_filter (server, NULL, NULL);
250   if (clients == NULL) {
251     GST_ERROR_OBJECT (server, "There is no client in this server");
252   }
253
254   for (walk = clients; walk; walk = next) {
255     GstRTSPClient *client = walk->data;
256
257     next = g_list_next (walk);
258
259     res =
260         gst_rtsp_wfd_client_trigger_request (GST_RTSP_WFD_CLIENT (client),
261         type);
262     if (res != GST_RTSP_OK) {
263       GST_ERROR_OBJECT (server, "Failed to send trigger request %d", type);
264     }
265     g_object_unref (client);
266   }
267
268   return res;
269
270 }
271
272 GstRTSPResult
273 gst_rtsp_wfd_server_set_supported_reso(GstRTSPWFDServer *server, guint64 supported_reso)
274 {
275   GstRTSPResult res = GST_RTSP_OK;
276   GstRTSPWFDServerPrivate *priv = GST_RTSP_WFD_SERVER_GET_PRIVATE(server);
277
278   g_return_val_if_fail (GST_IS_RTSP_WFD_SERVER (server), GST_RTSP_ERROR);
279
280   GST_RTSP_WFD_SERVER_LOCK (server);
281
282   priv->supported_resolution = supported_reso;
283
284   GST_RTSP_WFD_SERVER_UNLOCK (server);
285   return res;
286 }
287 GstRTSPResult
288 gst_rtsp_wfd_server_set_video_native_reso (GstRTSPWFDServer *server, guint64 native_reso)
289 {
290           GstRTSPResult res = GST_RTSP_OK;
291           GstRTSPWFDServerPrivate *priv = GST_RTSP_WFD_SERVER_GET_PRIVATE(server);
292
293           g_return_val_if_fail (GST_IS_RTSP_WFD_SERVER (server), GST_RTSP_ERROR);
294
295           GST_RTSP_WFD_SERVER_LOCK (server);
296
297           priv->native_resolution = native_reso;
298
299           GST_RTSP_WFD_SERVER_UNLOCK (server);
300           return res;
301 }