Add AL-FEC feature
[platform/upstream/gst-rtsp-server.git] / gst / rtsp-server / rtsp-server-wfd.c
1 /* GStreamer
2  * Copyright (C) 2015 Samsung Electronics Hyunjun Ko <zzoon.ko@samsung.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 #include "rtsp-client-ext.h"
61
62 #define GST_RTSP_WFD_SERVER_GET_PRIVATE(obj)  \
63        (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_WFD_SERVER, GstRTSPWFDServerPrivate))
64
65 #define GST_RTSP_WFD_SERVER_GET_LOCK(server)  (&(GST_RTSP_WFD_SERVER_CAST(server)->priv->lock))
66 #define GST_RTSP_WFD_SERVER_LOCK(server)      (g_mutex_lock(GST_RTSP_WFD_SERVER_GET_LOCK(server)))
67 #define GST_RTSP_WFD_SERVER_UNLOCK(server)    (g_mutex_unlock(GST_RTSP_WFD_SERVER_GET_LOCK(server)))
68
69 struct _GstRTSPWFDServerPrivate
70 {
71   GMutex lock;                  /* protects everything in this struct */
72
73   /* the clients that are connected */
74   GList *clients;
75   guint64 native_resolution;
76   guint64 supported_resolution;
77   guint8 audio_codec;
78 };
79
80 G_DEFINE_TYPE (GstRTSPWFDServer, gst_rtsp_wfd_server, GST_TYPE_RTSP_SERVER);
81
82 GST_DEBUG_CATEGORY_STATIC (rtsp_wfd_server_debug);
83 #define GST_CAT_DEFAULT rtsp_wfd_server_debug
84
85 static void gst_rtsp_wfd_server_get_property (GObject * object, guint propid,
86     GValue * value, GParamSpec * pspec);
87 static void gst_rtsp_wfd_server_set_property (GObject * object, guint propid,
88     const GValue * value, GParamSpec * pspec);
89 static void gst_rtsp_wfd_server_finalize (GObject * object);
90
91 static GstRTSPClient *create_client_wfd (GstRTSPServer * server);
92 static void client_connected_wfd (GstRTSPServer * server,
93     GstRTSPClient * client);
94
95 static void
96 gst_rtsp_wfd_server_class_init (GstRTSPWFDServerClass * klass)
97 {
98   GObjectClass *gobject_class;
99   GstRTSPServerClass *rtsp_server_class;
100
101   g_type_class_add_private (klass, sizeof (GstRTSPWFDServerPrivate));
102
103   gobject_class = G_OBJECT_CLASS (klass);
104   rtsp_server_class = GST_RTSP_SERVER_CLASS (klass);
105
106   gobject_class->get_property = gst_rtsp_wfd_server_get_property;
107   gobject_class->set_property = gst_rtsp_wfd_server_set_property;
108   gobject_class->finalize = gst_rtsp_wfd_server_finalize;
109
110   rtsp_server_class->create_client = create_client_wfd;
111   rtsp_server_class->client_connected = client_connected_wfd;
112
113
114   GST_DEBUG_CATEGORY_INIT (rtsp_wfd_server_debug, "rtspwfdserver", 0,
115       "GstRTSPWFDServer");
116 }
117
118 static void
119 gst_rtsp_wfd_server_init (GstRTSPWFDServer * server)
120 {
121   GstRTSPWFDServerPrivate *priv = GST_RTSP_WFD_SERVER_GET_PRIVATE (server);
122
123   g_return_if_fail (priv != NULL);
124
125   server->priv = priv;
126   server->priv->native_resolution = 0;
127   server->priv->supported_resolution = 1;
128   server->priv->audio_codec = 2;
129   GST_INFO_OBJECT (server, "New server is initialized");
130 }
131
132 static void
133 gst_rtsp_wfd_server_finalize (GObject * object)
134 {
135   GstRTSPWFDServer *server = GST_RTSP_WFD_SERVER (object);
136   //GstRTSPWFDServerPrivate *priv = server->priv;
137
138   GST_DEBUG_OBJECT (server, "finalize server");
139
140   G_OBJECT_CLASS (gst_rtsp_wfd_server_parent_class)->finalize (object);
141 }
142
143 /**
144  * gst_rtsp_server_new:
145  *
146  * Create a new #GstRTSPWFDServer instance.
147  */
148 GstRTSPWFDServer *
149 gst_rtsp_wfd_server_new (void)
150 {
151   GstRTSPWFDServer *result;
152
153   result = g_object_new (GST_TYPE_RTSP_WFD_SERVER, NULL);
154
155   return result;
156 }
157
158 static void
159 gst_rtsp_wfd_server_get_property (GObject * object, guint propid,
160     GValue * value, GParamSpec * pspec)
161 {
162   //GstRTSPWFDServer *server = GST_RTSP_WFD_SERVER (object);
163
164   switch (propid) {
165     default:
166       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
167   }
168 }
169
170 static void
171 gst_rtsp_wfd_server_set_property (GObject * object, guint propid,
172     const GValue * value, GParamSpec * pspec)
173 {
174   //GstRTSPWFDServer *server = GST_RTSP_WFD_SERVER (object);
175
176   switch (propid) {
177     default:
178       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
179   }
180 }
181
182 static gboolean
183 _start_wfd (gpointer data)
184 {
185   GstRTSPWFDClient *client = (GstRTSPWFDClient *) data;
186
187   GST_INFO_OBJECT (client, "WFD client is STARTing");
188
189   gst_rtsp_wfd_client_start_wfd (client);
190   return FALSE;
191 }
192
193 static void
194 client_connected_wfd (GstRTSPServer * server, GstRTSPClient * client)
195 {
196   GST_INFO_OBJECT (server, "Client is connected");
197
198   gst_rtsp_wfd_client_set_host_address (GST_RTSP_WFD_CLIENT_CAST (client),
199       gst_rtsp_server_get_address (server));
200   g_idle_add (_start_wfd, client);
201   return;
202 }
203
204 static GstRTSPClient *
205 create_client_wfd (GstRTSPServer * server)
206 {
207   GstRTSPWFDClient *client;
208   GstRTSPThreadPool *thread_pool = NULL;
209   GstRTSPSessionPool *session_pool = NULL;
210   GstRTSPMountPoints *mount_points = NULL;
211   GstRTSPAuth *auth = NULL;
212   GstRTSPWFDServerPrivate *priv = GST_RTSP_WFD_SERVER_GET_PRIVATE (server);
213
214   g_return_val_if_fail (priv != NULL, NULL);
215
216   GST_INFO_OBJECT (server, "New Client is being created");
217
218   /* a new client connected, create a session to handle the client. */
219   //client = gst_rtsp_wfd_client_new();
220   client = (GstRTSPWFDClient *) gst_rtsp_ext_client_new ();
221
222   thread_pool = gst_rtsp_server_get_thread_pool (server);
223   session_pool = gst_rtsp_server_get_session_pool (server);
224   mount_points = gst_rtsp_server_get_mount_points (server);
225   auth = gst_rtsp_server_get_auth (server);
226
227   /* set the session pool that this client should use */
228   GST_RTSP_WFD_SERVER_LOCK (server);
229   gst_rtsp_client_set_session_pool (GST_RTSP_CLIENT_CAST (client),
230       session_pool);
231   /* set the mount points that this client should use */
232   gst_rtsp_client_set_mount_points (GST_RTSP_CLIENT_CAST (client),
233       mount_points);
234   /* set authentication manager */
235   gst_rtsp_client_set_auth (GST_RTSP_CLIENT_CAST (client), auth);
236   /* set threadpool */
237   gst_rtsp_client_set_thread_pool (GST_RTSP_CLIENT_CAST (client), thread_pool);
238
239   gst_rtsp_wfd_client_set_video_supported_resolution (client,
240       priv->supported_resolution);
241
242   gst_rtsp_wfd_client_set_video_native_resolution (client,
243       priv->native_resolution);
244
245   gst_rtsp_wfd_client_set_audio_codec (client, priv->audio_codec);
246
247   GST_RTSP_WFD_SERVER_UNLOCK (server);
248
249   return GST_RTSP_CLIENT (client);
250 }
251
252 GstRTSPResult
253 gst_rtsp_wfd_server_trigger_request (GstRTSPServer * server,
254     GstWFDTriggerType type)
255 {
256   GstRTSPResult res = GST_RTSP_OK;
257   GList *clients, *walk, *next;
258
259   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), GST_RTSP_ERROR);
260
261   clients = gst_rtsp_server_client_filter (server, NULL, NULL);
262   if (clients == NULL) {
263     GST_ERROR_OBJECT (server, "There is no client in this server");
264   }
265
266   for (walk = clients; walk; walk = next) {
267     GstRTSPClient *client = walk->data;
268
269     next = g_list_next (walk);
270
271     res =
272         gst_rtsp_wfd_client_trigger_request (GST_RTSP_WFD_CLIENT (client),
273         type);
274     if (res != GST_RTSP_OK) {
275       GST_ERROR_OBJECT (server, "Failed to send trigger request %d", type);
276     }
277     g_object_unref (client);
278   }
279
280   return res;
281
282 }
283
284 GstRTSPResult
285 gst_rtsp_wfd_server_set_supported_reso (GstRTSPWFDServer * server,
286     guint64 supported_reso)
287 {
288   GstRTSPResult res = GST_RTSP_OK;
289   GstRTSPWFDServerPrivate *priv = GST_RTSP_WFD_SERVER_GET_PRIVATE (server);
290
291   g_return_val_if_fail (GST_IS_RTSP_WFD_SERVER (server), GST_RTSP_ERROR);
292   g_return_val_if_fail (priv != NULL, GST_RTSP_ERROR);
293
294   GST_RTSP_WFD_SERVER_LOCK (server);
295
296   priv->supported_resolution = supported_reso;
297
298   GST_RTSP_WFD_SERVER_UNLOCK (server);
299   return res;
300 }
301
302 GstRTSPResult
303 gst_rtsp_wfd_server_set_video_native_reso (GstRTSPWFDServer * server,
304     guint64 native_reso)
305 {
306   GstRTSPResult res = GST_RTSP_OK;
307   GstRTSPWFDServerPrivate *priv = GST_RTSP_WFD_SERVER_GET_PRIVATE (server);
308
309   g_return_val_if_fail (GST_IS_RTSP_WFD_SERVER (server), GST_RTSP_ERROR);
310   g_return_val_if_fail (priv != NULL, GST_RTSP_ERROR);
311
312   GST_RTSP_WFD_SERVER_LOCK (server);
313
314   priv->native_resolution = native_reso;
315
316   GST_RTSP_WFD_SERVER_UNLOCK (server);
317   return res;
318 }
319
320 GstRTSPResult
321 gst_rtsp_wfd_server_set_audio_codec (GstRTSPWFDServer * server,
322     guint8 audio_codec)
323 {
324   GstRTSPResult res = GST_RTSP_OK;
325   GstRTSPWFDServerPrivate *priv = GST_RTSP_WFD_SERVER_GET_PRIVATE (server);
326
327   g_return_val_if_fail (GST_IS_RTSP_WFD_SERVER (server), GST_RTSP_ERROR);
328   g_return_val_if_fail (priv != NULL, GST_RTSP_ERROR);
329
330   GST_RTSP_WFD_SERVER_LOCK (server);
331
332   priv->audio_codec = audio_codec;
333
334   GST_RTSP_WFD_SERVER_UNLOCK (server);
335   return res;
336 }