*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
*/
#include <stdlib.h>
/* #define DEFAULT_ADDRESS "::0" */
#define DEFAULT_SERVICE "8554"
#define DEFAULT_BACKLOG 5
+#define DEFAULT_MAX_THREADS 0
/* Define to use the SO_LINGER option so that the server sockets can be resused
* sooner. Disabled for now because it is not very well implemented by various
PROP_SESSION_POOL,
PROP_MEDIA_MAPPING,
+ PROP_MAX_THREADS,
PROP_LAST
};
GST_DEBUG_CATEGORY_STATIC (rtsp_server_debug);
#define GST_CAT_DEFAULT rtsp_server_debug
+typedef struct _ClientContext ClientContext;
+
static guint gst_rtsp_server_signals[SIGNAL_LAST] = { 0 };
static void gst_rtsp_server_get_property (GObject * object, guint propid,
const GValue * value, GParamSpec * pspec);
static void gst_rtsp_server_finalize (GObject * object);
+static gpointer do_loop (ClientContext * ctx);
static GstRTSPClient *default_create_client (GstRTSPServer * server);
static gboolean default_accept_client (GstRTSPServer * server,
GstRTSPClient * client, GSocket * socket, GError ** error);
"The media mapping to use for client session",
GST_TYPE_RTSP_MEDIA_MAPPING,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+ /**
+ * GstRTSPServer::max-threads:
+ *
+ * The maximum amount of threads to use for client connections. A value of
+ * 0 means to use only the mainloop, -1 means an unlimited amount of
+ * threads.
+ */
+ g_object_class_install_property (gobject_class, PROP_MAX_THREADS,
+ g_param_spec_int ("max-threads", "Max Threads",
+ "The maximum amount of threads to use for client connections "
+ "(0 = only mainloop, -1 = unlimited)", -1, G_MAXINT,
+ DEFAULT_MAX_THREADS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED] =
g_signal_new ("client-connected", G_TYPE_FROM_CLASS (gobject_class),
klass->create_client = default_create_client;
klass->accept_client = default_accept_client;
+ klass->pool = g_thread_pool_new ((GFunc) do_loop, klass, -1, FALSE, NULL);
+
GST_DEBUG_CATEGORY_INIT (rtsp_server_debug, "rtspserver", 0, "GstRTSPServer");
}
return result;
}
+/**
+ * gst_rtsp_server_get_bound_port:
+ * @server: a #GstRTSPServer
+ *
+ * Get the port number where the server was bound to.
+ *
+ * Returns: the port number
+ */
int
gst_rtsp_server_get_bound_port (GstRTSPServer * server)
{
return result;
}
+/**
+ * gst_rtsp_server_set_max_threads:
+ * @server: a #GstRTSPServer
+ * @max_threads: maximum threads
+ *
+ * Set the maximum threads used by the server to handle client requests.
+ * A value of 0 will use the server mainloop, a value of -1 will use an
+ * unlimited number of threads.
+ */
+void
+gst_rtsp_server_set_max_threads (GstRTSPServer * server, gint max_threads)
+{
+ g_return_if_fail (GST_IS_RTSP_SERVER (server));
+
+ GST_RTSP_SERVER_LOCK (server);
+ server->max_threads = max_threads;
+ GST_RTSP_SERVER_UNLOCK (server);
+}
+
+/**
+ * gst_rtsp_server_get_max_threads:
+ * @server: a #GstRTSPServer
+ *
+ * Get the maximum number of threads used for client connections.
+ * See gst_rtsp_server_set_max_threads().
+ *
+ * Returns: the maximum number of threads.
+ */
+gint
+gst_rtsp_server_get_max_threads (GstRTSPServer * server)
+{
+ gint res;
+
+ g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
+
+ GST_RTSP_SERVER_LOCK (server);
+ res = server->max_threads;
+ GST_RTSP_SERVER_UNLOCK (server);
+
+ return res;
+}
+
+
static void
gst_rtsp_server_get_property (GObject * object, guint propid,
GValue * value, GParamSpec * pspec)
case PROP_MEDIA_MAPPING:
g_value_take_object (value, gst_rtsp_server_get_media_mapping (server));
break;
+ case PROP_MAX_THREADS:
+ g_value_set_int (value, gst_rtsp_server_get_max_threads (server));
+ break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
}
case PROP_MEDIA_MAPPING:
gst_rtsp_server_set_media_mapping (server, g_value_get_object (value));
break;
+ case PROP_MAX_THREADS:
+ gst_rtsp_server_set_max_threads (server, g_value_get_int (value));
+ break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
}
}
}
+struct _ClientContext
+{
+ GstRTSPServer *server;
+ GMainLoop *loop;
+ GMainContext *context;
+ GstRTSPClient *client;
+};
+
+static void
+free_client_context (ClientContext * ctx)
+{
+ g_main_context_unref (ctx->context);
+ if (ctx->loop)
+ g_main_loop_unref (ctx->loop);
+ g_object_unref (ctx->client);
+ g_slice_free (ClientContext, ctx);
+}
+
+static gpointer
+do_loop (ClientContext * ctx)
+{
+ GST_INFO ("enter mainloop");
+ g_main_loop_run (ctx->loop);
+ GST_INFO ("exit mainloop");
+
+ free_client_context (ctx);
+
+ return NULL;
+}
+
static void
-unmanage_client (GstRTSPClient * client, GstRTSPServer * server)
+unmanage_client (GstRTSPClient * client, ClientContext * ctx)
{
+ GstRTSPServer *server = ctx->server;
+
GST_DEBUG_OBJECT (server, "unmanage client %p", client);
g_object_ref (server);
gst_rtsp_client_set_server (client, NULL);
GST_RTSP_SERVER_LOCK (server);
- server->clients = g_list_remove (server->clients, client);
+ server->clients = g_list_remove (server->clients, ctx);
GST_RTSP_SERVER_UNLOCK (server);
- g_object_unref (server);
- g_object_unref (client);
+ if (ctx->loop)
+ g_main_loop_quit (ctx->loop);
+ else
+ free_client_context (ctx);
+
+ g_object_unref (server);
}
-/* add the client to the active list of clients, takes ownership of
- * the client */
+/* add the client context to the active list of clients, takes ownership
+ * of client */
static void
manage_client (GstRTSPServer * server, GstRTSPClient * client)
{
+ ClientContext *ctx;
+
GST_DEBUG_OBJECT (server, "manage client %p", client);
gst_rtsp_client_set_server (client, server);
+ ctx = g_slice_new0 (ClientContext);
+ ctx->server = server;
+ ctx->client = client;
+ if (server->max_threads == 0) {
+ GSource *source;
+
+ /* find the context to add the watch */
+ if ((source = g_main_current_source ()))
+ ctx->context = g_main_context_ref (g_source_get_context (source));
+ else
+ ctx->context = NULL;
+ } else {
+ ctx->context = g_main_context_new ();
+ ctx->loop = g_main_loop_new (ctx->context, TRUE);
+ }
+ gst_rtsp_client_attach (client, ctx->context);
+
GST_RTSP_SERVER_LOCK (server);
- g_signal_connect (client, "closed", (GCallback) unmanage_client, server);
- server->clients = g_list_prepend (server->clients, client);
+ g_signal_connect (client, "closed", (GCallback) unmanage_client, ctx);
+ server->clients = g_list_prepend (server->clients, ctx);
GST_RTSP_SERVER_UNLOCK (server);
+
+ if (ctx->loop) {
+ GstRTSPServerClass *klass = GST_RTSP_SERVER_GET_CLASS (server);
+
+ g_thread_pool_push (klass->pool, ctx, NULL);
+ }
}
static GstRTSPClient *
goto client_failed;
/* a new client connected, create a client object to handle the client. */
- if (!gst_rtsp_client_create_from_socket (client, socket, ip, port,
- initial_buffer, &error)) {
+ if (!gst_rtsp_client_use_socket (client, socket, ip,
+ port, initial_buffer, &error)) {
goto transfer_failed;
}
{
GST_ERROR_OBJECT (server, "failed to accept client: %s", error->message);
g_error_free (error);
- gst_object_unref (client);
+ g_object_unref (client);
return FALSE;
}
}
* gst_rtsp_server_io_func:
* @socket: a #GSocket
* @condition: the condition on @source
+ * @server: a #GstRTSPServer
*
* A default #GSocketSourceFunc that creates a new #GstRTSPClient to accept and handle a
* new connection on @socket or @server.
{
GST_ERROR_OBJECT (server, "failed to accept client: %s", error->message);
g_error_free (error);
- gst_object_unref (client);
+ g_object_unref (client);
return FALSE;
}
}
gst_rtsp_server_create_source (GstRTSPServer * server,
GCancellable * cancellable, GError ** error)
{
- GSocket *socket;
+ GSocket *socket, *old;
GSource *source;
g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
socket = gst_rtsp_server_create_socket (server, NULL, error);
if (socket == NULL)
goto no_socket;
+
+ GST_RTSP_SERVER_LOCK (server);
+ old = server->socket;
server->socket = g_object_ref (socket);
+ GST_RTSP_SERVER_UNLOCK (server);
+
+ if (old)
+ g_object_unref (old);
/* create a watch for reads (new connections) and possible errors */
source = g_socket_create_source (socket, G_IO_IN |