2013-07-28 10:58:43 +00:00
|
|
|
/*
|
2013-07-28 16:24:53 +00:00
|
|
|
* Copyright (C) 2013 Sebastian Dröge <slomo@circular-chaos.org>
|
2013-07-28 10:58:43 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* 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., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2013-07-28 08:42:34 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <gst/gst.h>
|
|
|
|
#include <gio/gio.h>
|
|
|
|
|
2013-07-28 10:58:43 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
gchar *name;
|
|
|
|
GSocketConnection *connection;
|
|
|
|
GSocket *socket;
|
|
|
|
GInputStream *istream;
|
|
|
|
GOutputStream *ostream;
|
2013-07-28 16:35:20 +00:00
|
|
|
GSource *isource, *tosource;
|
2013-07-28 10:58:43 +00:00
|
|
|
GByteArray *current_message;
|
2015-05-10 12:28:15 +00:00
|
|
|
gchar *http_version;
|
2015-05-10 15:53:03 +00:00
|
|
|
gboolean waiting_200_ok;
|
2013-07-28 10:58:43 +00:00
|
|
|
} Client;
|
|
|
|
|
2015-03-18 21:39:26 +00:00
|
|
|
static const char *known_mimetypes[] = {
|
|
|
|
"video/webm",
|
|
|
|
"multipart/x-mixed-replace",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2013-07-28 10:58:43 +00:00
|
|
|
static GMainLoop *loop = NULL;
|
|
|
|
G_LOCK_DEFINE_STATIC (clients);
|
|
|
|
static GList *clients = NULL;
|
|
|
|
static GstElement *pipeline = NULL;
|
|
|
|
static GstElement *multisocketsink = NULL;
|
|
|
|
static gboolean started = FALSE;
|
2015-02-21 13:04:19 +00:00
|
|
|
static gchar *content_type;
|
2015-05-10 15:53:03 +00:00
|
|
|
G_LOCK_DEFINE_STATIC (caps);
|
|
|
|
static gboolean caps_resolved = FALSE;
|
2013-07-28 10:58:43 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
remove_client (Client * client)
|
|
|
|
{
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_print ("Removing connection %s\n", client->name);
|
2013-07-28 10:58:43 +00:00
|
|
|
|
2015-05-05 11:45:49 +00:00
|
|
|
G_LOCK (clients);
|
|
|
|
clients = g_list_remove (clients, client);
|
|
|
|
G_UNLOCK (clients);
|
|
|
|
|
2013-07-28 10:58:43 +00:00
|
|
|
g_free (client->name);
|
2015-05-10 12:28:15 +00:00
|
|
|
g_free (client->http_version);
|
2013-07-28 10:58:43 +00:00
|
|
|
|
|
|
|
if (client->isource) {
|
|
|
|
g_source_destroy (client->isource);
|
|
|
|
g_source_unref (client->isource);
|
|
|
|
}
|
2013-07-28 16:35:20 +00:00
|
|
|
if (client->tosource) {
|
|
|
|
g_source_destroy (client->tosource);
|
|
|
|
g_source_unref (client->tosource);
|
|
|
|
}
|
2013-07-28 10:58:43 +00:00
|
|
|
g_object_unref (client->connection);
|
|
|
|
g_byte_array_unref (client->current_message);
|
|
|
|
|
2023-01-24 20:20:41 +00:00
|
|
|
g_free (client);
|
2013-07-28 10:58:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
write_bytes (Client * client, const gchar * data, guint len)
|
|
|
|
{
|
|
|
|
gssize w;
|
|
|
|
GError *err = NULL;
|
|
|
|
|
|
|
|
/* TODO: We assume this never blocks */
|
|
|
|
do {
|
|
|
|
w = g_output_stream_write (client->ostream, data, len, NULL, &err);
|
2013-07-28 16:39:07 +00:00
|
|
|
if (w > 0) {
|
2013-07-28 10:58:43 +00:00
|
|
|
len -= w;
|
2013-07-28 16:39:07 +00:00
|
|
|
data += w;
|
|
|
|
}
|
2013-07-28 16:32:16 +00:00
|
|
|
} while (w > 0 && len > 0);
|
2013-07-28 10:58:43 +00:00
|
|
|
|
2013-07-28 16:32:16 +00:00
|
|
|
if (w <= 0) {
|
|
|
|
if (err) {
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_print ("Write error %s\n", err->message);
|
2013-07-28 16:32:16 +00:00
|
|
|
g_clear_error (&err);
|
|
|
|
}
|
2013-07-28 10:58:43 +00:00
|
|
|
remove_client (client);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-10 12:28:15 +00:00
|
|
|
static void
|
|
|
|
send_response_200_ok (Client * client)
|
|
|
|
{
|
|
|
|
gchar *response;
|
|
|
|
response = g_strdup_printf ("%s 200 OK\r\n%s\r\n", client->http_version,
|
2019-03-26 05:09:10 +00:00
|
|
|
content_type);
|
2015-05-10 12:28:15 +00:00
|
|
|
write_bytes (client, response, strlen (response));
|
|
|
|
g_free (response);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
send_response_404_not_found (Client * client)
|
|
|
|
{
|
|
|
|
gchar *response;
|
|
|
|
response = g_strdup_printf ("%s 404 Not Found\r\n\r\n", client->http_version);
|
|
|
|
write_bytes (client, response, strlen (response));
|
|
|
|
g_free (response);
|
|
|
|
}
|
|
|
|
|
2013-07-28 10:58:43 +00:00
|
|
|
static void
|
|
|
|
client_message (Client * client, const gchar * data, guint len)
|
|
|
|
{
|
2015-05-10 16:11:16 +00:00
|
|
|
gboolean http_head_request = FALSE;
|
|
|
|
gboolean http_get_request = FALSE;
|
2013-07-28 10:58:43 +00:00
|
|
|
gchar **lines = g_strsplit_set (data, "\r\n", -1);
|
|
|
|
|
2015-05-10 16:11:16 +00:00
|
|
|
if (g_str_has_prefix (lines[0], "HEAD"))
|
|
|
|
http_head_request = TRUE;
|
|
|
|
else if (g_str_has_prefix (lines[0], "GET"))
|
|
|
|
http_get_request = TRUE;
|
2013-07-28 10:58:43 +00:00
|
|
|
|
2015-05-10 16:11:16 +00:00
|
|
|
if (http_head_request || http_get_request) {
|
2013-07-28 10:58:43 +00:00
|
|
|
gchar **parts = g_strsplit (lines[0], " ", -1);
|
|
|
|
gboolean ok = FALSE;
|
|
|
|
|
2015-05-10 12:28:15 +00:00
|
|
|
g_free (client->http_version);
|
|
|
|
|
2013-07-28 16:53:51 +00:00
|
|
|
if (parts[1] && parts[2] && *parts[2] != '\0')
|
2015-05-10 12:28:15 +00:00
|
|
|
client->http_version = g_strdup (parts[2]);
|
2013-07-28 10:58:43 +00:00
|
|
|
else
|
2015-05-10 12:28:15 +00:00
|
|
|
client->http_version = g_strdup ("HTTP/1.0");
|
2013-07-28 10:58:43 +00:00
|
|
|
|
2013-07-28 16:53:51 +00:00
|
|
|
if (parts[1] && strcmp (parts[1], "/") == 0) {
|
2015-05-10 15:53:03 +00:00
|
|
|
G_LOCK (caps);
|
|
|
|
if (caps_resolved)
|
|
|
|
send_response_200_ok (client);
|
|
|
|
else
|
|
|
|
client->waiting_200_ok = TRUE;
|
|
|
|
G_UNLOCK (caps);
|
2013-07-28 10:58:43 +00:00
|
|
|
ok = TRUE;
|
|
|
|
} else {
|
2015-05-10 12:28:15 +00:00
|
|
|
send_response_404_not_found (client);
|
2013-07-28 10:58:43 +00:00
|
|
|
}
|
|
|
|
g_strfreev (parts);
|
|
|
|
|
|
|
|
if (ok) {
|
2015-05-10 16:11:16 +00:00
|
|
|
if (http_get_request) {
|
|
|
|
/* Start streaming to client socket */
|
|
|
|
g_source_destroy (client->isource);
|
|
|
|
g_source_unref (client->isource);
|
|
|
|
client->isource = NULL;
|
|
|
|
g_source_destroy (client->tosource);
|
|
|
|
g_source_unref (client->tosource);
|
|
|
|
client->tosource = NULL;
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_print ("Starting to stream to %s\n", client->name);
|
2015-05-10 16:11:16 +00:00
|
|
|
g_signal_emit_by_name (multisocketsink, "add", client->socket);
|
|
|
|
}
|
2013-07-28 10:58:43 +00:00
|
|
|
|
|
|
|
if (!started) {
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_print ("Starting pipeline\n");
|
2013-07-28 10:58:43 +00:00
|
|
|
if (gst_element_set_state (pipeline,
|
|
|
|
GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_print ("Failed to start pipeline\n");
|
2013-07-28 10:58:43 +00:00
|
|
|
g_main_loop_quit (loop);
|
|
|
|
}
|
|
|
|
started = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gchar **parts = g_strsplit (lines[0], " ", -1);
|
|
|
|
gchar *response;
|
|
|
|
const gchar *http_version;
|
|
|
|
|
2013-07-28 16:53:51 +00:00
|
|
|
if (parts[1] && parts[2] && *parts[2] != '\0')
|
2013-07-28 10:58:43 +00:00
|
|
|
http_version = parts[2];
|
|
|
|
else
|
2013-07-28 16:53:51 +00:00
|
|
|
http_version = "HTTP/1.0";
|
2013-07-28 10:58:43 +00:00
|
|
|
|
|
|
|
response = g_strdup_printf ("%s 400 Bad Request\r\n\r\n", http_version);
|
|
|
|
write_bytes (client, response, strlen (response));
|
|
|
|
g_free (response);
|
|
|
|
g_strfreev (parts);
|
|
|
|
remove_client (client);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_strfreev (lines);
|
|
|
|
}
|
|
|
|
|
2013-07-28 16:35:20 +00:00
|
|
|
static gboolean
|
|
|
|
on_timeout (Client * client)
|
|
|
|
{
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_print ("Timeout\n");
|
2013-07-28 16:35:20 +00:00
|
|
|
remove_client (client);
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2013-07-28 10:58:43 +00:00
|
|
|
static gboolean
|
|
|
|
on_read_bytes (GPollableInputStream * stream, Client * client)
|
|
|
|
{
|
|
|
|
gssize r;
|
|
|
|
gchar data[4096];
|
|
|
|
GError *err = NULL;
|
|
|
|
|
|
|
|
do {
|
|
|
|
r = g_pollable_input_stream_read_nonblocking (G_POLLABLE_INPUT_STREAM
|
|
|
|
(client->istream), data, sizeof (data), NULL, &err);
|
|
|
|
if (r > 0)
|
|
|
|
g_byte_array_append (client->current_message, (guint8 *) data, r);
|
|
|
|
} while (r > 0);
|
|
|
|
|
2013-07-28 16:32:16 +00:00
|
|
|
if (r == 0) {
|
|
|
|
remove_client (client);
|
|
|
|
return FALSE;
|
|
|
|
} else if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK)) {
|
2013-07-28 10:58:43 +00:00
|
|
|
guint8 *tmp = client->current_message->data;
|
2014-10-17 12:43:25 +00:00
|
|
|
guint tmp_len = client->current_message->len;
|
2013-07-28 10:58:43 +00:00
|
|
|
|
|
|
|
g_clear_error (&err);
|
|
|
|
|
2014-10-17 12:43:25 +00:00
|
|
|
while (tmp_len > 3) {
|
2013-07-28 10:58:43 +00:00
|
|
|
if (tmp[0] == 0x0d && tmp[1] == 0x0a && tmp[2] == 0x0d && tmp[3] == 0x0a) {
|
2013-07-28 17:01:34 +00:00
|
|
|
guint len;
|
2013-07-28 10:58:43 +00:00
|
|
|
|
2013-07-28 17:01:34 +00:00
|
|
|
g_byte_array_append (client->current_message, (const guint8 *) "\0", 1);
|
|
|
|
len = tmp - client->current_message->data + 5;
|
2013-07-28 10:58:43 +00:00
|
|
|
client_message (client, (gchar *) client->current_message->data, len);
|
|
|
|
g_byte_array_remove_range (client->current_message, 0, len);
|
|
|
|
tmp = client->current_message->data;
|
2019-03-26 05:09:10 +00:00
|
|
|
tmp_len = client->current_message->len;
|
2013-07-28 10:58:43 +00:00
|
|
|
} else {
|
|
|
|
tmp++;
|
2019-03-26 05:09:10 +00:00
|
|
|
tmp_len--;
|
2013-07-28 10:58:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-28 16:38:01 +00:00
|
|
|
if (client->current_message->len >= 1024 * 1024) {
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_print ("No complete request after 1MB of data\n");
|
2013-07-28 16:38:01 +00:00
|
|
|
remove_client (client);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2013-07-28 10:58:43 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
} else {
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_print ("Read error %s\n", err->message);
|
2013-07-28 16:32:16 +00:00
|
|
|
g_clear_error (&err);
|
|
|
|
remove_client (client);
|
|
|
|
return FALSE;
|
2013-07-28 10:58:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
on_new_connection (GSocketService * service, GSocketConnection * connection,
|
|
|
|
GObject * source_object, gpointer user_data)
|
|
|
|
{
|
2023-01-24 20:20:41 +00:00
|
|
|
Client *client = g_new0 (Client, 1);
|
2013-07-28 10:58:43 +00:00
|
|
|
GSocketAddress *addr;
|
|
|
|
GInetAddress *iaddr;
|
|
|
|
gchar *ip;
|
|
|
|
guint16 port;
|
|
|
|
|
|
|
|
addr = g_socket_connection_get_remote_address (connection, NULL);
|
|
|
|
iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (addr));
|
|
|
|
port = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
|
|
|
|
ip = g_inet_address_to_string (iaddr);
|
|
|
|
client->name = g_strdup_printf ("%s:%u", ip, port);
|
|
|
|
g_free (ip);
|
|
|
|
g_object_unref (addr);
|
|
|
|
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_print ("New connection %s\n", client->name);
|
2013-07-28 10:58:43 +00:00
|
|
|
|
2015-05-10 15:53:03 +00:00
|
|
|
client->waiting_200_ok = FALSE;
|
2015-05-10 12:28:15 +00:00
|
|
|
client->http_version = g_strdup ("");
|
2013-07-28 10:58:43 +00:00
|
|
|
client->connection = g_object_ref (connection);
|
|
|
|
client->socket = g_socket_connection_get_socket (connection);
|
|
|
|
client->istream =
|
|
|
|
g_io_stream_get_input_stream (G_IO_STREAM (client->connection));
|
|
|
|
client->ostream =
|
|
|
|
g_io_stream_get_output_stream (G_IO_STREAM (client->connection));
|
|
|
|
client->current_message = g_byte_array_sized_new (1024);
|
|
|
|
|
2013-07-28 16:35:20 +00:00
|
|
|
client->tosource = g_timeout_source_new_seconds (5);
|
2013-07-28 16:53:51 +00:00
|
|
|
g_source_set_callback (client->tosource, (GSourceFunc) on_timeout, client,
|
2013-07-28 16:35:20 +00:00
|
|
|
NULL);
|
|
|
|
g_source_attach (client->tosource, NULL);
|
|
|
|
|
2013-07-28 10:58:43 +00:00
|
|
|
client->isource =
|
|
|
|
g_pollable_input_stream_create_source (G_POLLABLE_INPUT_STREAM
|
|
|
|
(client->istream), NULL);
|
|
|
|
g_source_set_callback (client->isource, (GSourceFunc) on_read_bytes, client,
|
|
|
|
NULL);
|
|
|
|
g_source_attach (client->isource, NULL);
|
|
|
|
|
|
|
|
G_LOCK (clients);
|
|
|
|
clients = g_list_prepend (clients, client);
|
|
|
|
G_UNLOCK (clients);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
on_message (GstBus * bus, GstMessage * message, gpointer user_data)
|
|
|
|
{
|
|
|
|
switch (GST_MESSAGE_TYPE (message)) {
|
|
|
|
case GST_MESSAGE_ERROR:{
|
|
|
|
gchar *debug;
|
|
|
|
GError *err;
|
|
|
|
|
|
|
|
gst_message_parse_error (message, &err, &debug);
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_print ("Error %s\n", err->message);
|
2013-07-28 10:58:43 +00:00
|
|
|
g_error_free (err);
|
|
|
|
g_free (debug);
|
|
|
|
g_main_loop_quit (loop);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GST_MESSAGE_WARNING:{
|
|
|
|
gchar *debug;
|
|
|
|
GError *err;
|
|
|
|
|
2015-01-27 14:15:10 +00:00
|
|
|
gst_message_parse_warning (message, &err, &debug);
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_print ("Warning %s\n", err->message);
|
2013-07-28 10:58:43 +00:00
|
|
|
g_error_free (err);
|
|
|
|
g_free (debug);
|
2013-07-28 11:54:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GST_MESSAGE_EOS:{
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_print ("EOS\n");
|
2013-07-28 11:54:58 +00:00
|
|
|
g_main_loop_quit (loop);
|
2013-07-28 10:58:43 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_client_socket_removed (GstElement * element, GSocket * socket,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
GList *l;
|
|
|
|
Client *client = NULL;
|
|
|
|
|
|
|
|
G_LOCK (clients);
|
|
|
|
for (l = clients; l; l = l->next) {
|
|
|
|
Client *tmp = l->data;
|
|
|
|
if (socket == tmp->socket) {
|
|
|
|
client = tmp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
G_UNLOCK (clients);
|
|
|
|
|
|
|
|
if (client)
|
|
|
|
remove_client (client);
|
|
|
|
}
|
|
|
|
|
2019-03-26 05:09:10 +00:00
|
|
|
static void
|
|
|
|
on_stream_caps_changed (GObject * obj, GParamSpec * pspec, gpointer user_data)
|
2015-03-18 21:39:26 +00:00
|
|
|
{
|
|
|
|
GstPad *src_pad;
|
|
|
|
GstCaps *src_caps;
|
|
|
|
GstStructure *gstrc;
|
2015-05-10 15:53:03 +00:00
|
|
|
GList *l;
|
2015-03-18 21:39:26 +00:00
|
|
|
|
|
|
|
src_pad = (GstPad *) obj;
|
|
|
|
src_caps = gst_pad_get_current_caps (src_pad);
|
|
|
|
gstrc = gst_caps_get_structure (src_caps, 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Include a Content-type header in the case we know the mime
|
|
|
|
* type is OK in HTTP. Required for MJPEG streams.
|
|
|
|
*/
|
|
|
|
int i = 0;
|
2019-03-26 05:09:10 +00:00
|
|
|
const gchar *mimetype = gst_structure_get_name (gstrc);
|
|
|
|
while (known_mimetypes[i] != NULL) {
|
|
|
|
if (strcmp (mimetype, known_mimetypes[i]) == 0) {
|
2015-03-18 21:39:26 +00:00
|
|
|
if (content_type)
|
2019-03-26 05:09:10 +00:00
|
|
|
g_free (content_type);
|
2015-03-18 21:39:26 +00:00
|
|
|
|
|
|
|
/* Handle the (maybe not so) especial case of multipart to add boundary */
|
2019-03-26 05:09:10 +00:00
|
|
|
if (strcmp (mimetype, "multipart/x-mixed-replace") == 0 &&
|
|
|
|
gst_structure_has_field_typed (gstrc, "boundary", G_TYPE_STRING)) {
|
|
|
|
const gchar *boundary = gst_structure_get_string (gstrc, "boundary");
|
2015-03-18 21:39:26 +00:00
|
|
|
content_type = g_strdup_printf ("Content-Type: "
|
2019-03-26 05:09:10 +00:00
|
|
|
"multipart/x-mixed-replace;boundary=--%s\r\n", boundary);
|
|
|
|
} else {
|
2015-03-18 21:39:26 +00:00
|
|
|
content_type = g_strdup_printf ("Content-Type: %s\r\n", mimetype);
|
|
|
|
}
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_print ("%s", content_type);
|
2015-03-18 21:39:26 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_caps_unref (src_caps);
|
2015-05-10 15:53:03 +00:00
|
|
|
|
|
|
|
/* Send 200 OK to those clients waiting for it */
|
|
|
|
G_LOCK (caps);
|
|
|
|
|
|
|
|
caps_resolved = TRUE;
|
|
|
|
|
|
|
|
G_LOCK (clients);
|
|
|
|
for (l = clients; l; l = l->next) {
|
|
|
|
Client *cl = l->data;
|
|
|
|
if (cl->waiting_200_ok) {
|
|
|
|
send_response_200_ok (cl);
|
|
|
|
cl->waiting_200_ok = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
G_UNLOCK (clients);
|
|
|
|
|
|
|
|
G_UNLOCK (caps);
|
2015-03-18 21:39:26 +00:00
|
|
|
}
|
|
|
|
|
2013-07-28 08:42:34 +00:00
|
|
|
int
|
2013-07-28 10:58:43 +00:00
|
|
|
main (gint argc, gchar ** argv)
|
2013-07-28 08:42:34 +00:00
|
|
|
{
|
2013-07-28 10:58:43 +00:00
|
|
|
GSocketService *service;
|
|
|
|
GstElement *bin, *stream;
|
|
|
|
GstPad *srcpad, *ghostpad, *sinkpad;
|
|
|
|
GError *err = NULL;
|
|
|
|
GstBus *bus;
|
|
|
|
|
|
|
|
gst_init (&argc, &argv);
|
|
|
|
|
2014-10-17 12:39:30 +00:00
|
|
|
if (argc < 4) {
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_print ("usage: %s PORT <launch line>\n"
|
2014-10-17 12:39:30 +00:00
|
|
|
"example: %s 8080 ( videotestsrc ! theoraenc ! oggmux name=stream )\n",
|
2013-07-28 10:58:43 +00:00
|
|
|
argv[0], argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-10-17 12:39:30 +00:00
|
|
|
const gchar *port_str = argv[1];
|
2019-03-26 05:09:10 +00:00
|
|
|
const int port = (int) g_ascii_strtoll (port_str, NULL, 10);
|
2014-10-17 12:39:30 +00:00
|
|
|
|
|
|
|
bin = gst_parse_launchv ((const gchar **) argv + 2, &err);
|
2013-07-28 10:58:43 +00:00
|
|
|
if (!bin) {
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_print ("invalid pipeline: %s\n", err->message);
|
2013-07-28 10:58:43 +00:00
|
|
|
g_clear_error (&err);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream = gst_bin_get_by_name (GST_BIN (bin), "stream");
|
|
|
|
if (!stream) {
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_print ("no element with name \"stream\" found\n");
|
2013-07-28 10:58:43 +00:00
|
|
|
gst_object_unref (bin);
|
|
|
|
return -3;
|
|
|
|
}
|
2015-02-21 13:49:58 +00:00
|
|
|
|
2013-07-28 10:58:43 +00:00
|
|
|
srcpad = gst_element_get_static_pad (stream, "src");
|
|
|
|
if (!srcpad) {
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_print ("no \"src\" pad in element \"stream\" found\n");
|
2013-07-28 10:58:43 +00:00
|
|
|
gst_object_unref (stream);
|
|
|
|
gst_object_unref (bin);
|
|
|
|
return -4;
|
|
|
|
}
|
|
|
|
|
2015-03-18 21:39:26 +00:00
|
|
|
content_type = g_strdup ("");
|
|
|
|
g_signal_connect (srcpad, "notify::caps",
|
2019-03-26 05:09:10 +00:00
|
|
|
G_CALLBACK (on_stream_caps_changed), NULL);
|
2015-03-18 21:39:26 +00:00
|
|
|
|
2013-07-28 10:58:43 +00:00
|
|
|
ghostpad = gst_ghost_pad_new ("src", srcpad);
|
|
|
|
gst_element_add_pad (GST_ELEMENT (bin), ghostpad);
|
|
|
|
gst_object_unref (srcpad);
|
|
|
|
|
|
|
|
pipeline = gst_pipeline_new (NULL);
|
|
|
|
|
|
|
|
multisocketsink = gst_element_factory_make ("multisocketsink", NULL);
|
|
|
|
g_object_set (multisocketsink,
|
|
|
|
"unit-format", GST_FORMAT_TIME,
|
|
|
|
"units-max", (gint64) 7 * GST_SECOND,
|
|
|
|
"units-soft-max", (gint64) 3 * GST_SECOND,
|
|
|
|
"recover-policy", 3 /* keyframe */ ,
|
2013-07-28 11:27:20 +00:00
|
|
|
"timeout", (guint64) 10 * GST_SECOND,
|
2013-07-28 10:58:43 +00:00
|
|
|
"sync-method", 1 /* next-keyframe */ ,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
gst_bin_add_many (GST_BIN (pipeline), bin, multisocketsink, NULL);
|
|
|
|
|
|
|
|
sinkpad = gst_element_get_static_pad (multisocketsink, "sink");
|
|
|
|
gst_pad_link (ghostpad, sinkpad);
|
|
|
|
gst_object_unref (sinkpad);
|
|
|
|
|
|
|
|
bus = gst_element_get_bus (pipeline);
|
|
|
|
gst_bus_add_signal_watch (bus);
|
|
|
|
g_signal_connect (bus, "message", G_CALLBACK (on_message), NULL);
|
|
|
|
gst_object_unref (bus);
|
|
|
|
|
|
|
|
g_signal_connect (multisocketsink, "client-socket-removed",
|
|
|
|
G_CALLBACK (on_client_socket_removed), NULL);
|
|
|
|
|
|
|
|
loop = g_main_loop_new (NULL, FALSE);
|
|
|
|
|
|
|
|
if (gst_element_set_state (pipeline,
|
|
|
|
GST_STATE_READY) == GST_STATE_CHANGE_FAILURE) {
|
|
|
|
gst_object_unref (pipeline);
|
|
|
|
g_main_loop_unref (loop);
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_print ("Failed to set pipeline to ready\n");
|
2013-07-28 10:58:43 +00:00
|
|
|
return -5;
|
|
|
|
}
|
|
|
|
|
|
|
|
service = g_socket_service_new ();
|
2014-10-17 12:39:30 +00:00
|
|
|
g_socket_listener_add_inet_port (G_SOCKET_LISTENER (service), port, NULL,
|
2013-07-28 10:58:43 +00:00
|
|
|
NULL);
|
|
|
|
|
|
|
|
g_signal_connect (service, "incoming", G_CALLBACK (on_new_connection), NULL);
|
|
|
|
|
|
|
|
g_socket_service_start (service);
|
|
|
|
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_print ("Listening on http://127.0.0.1:%d/\n", port);
|
2013-07-28 10:58:43 +00:00
|
|
|
|
|
|
|
g_main_loop_run (loop);
|
|
|
|
|
|
|
|
g_socket_service_stop (service);
|
|
|
|
g_object_unref (service);
|
|
|
|
|
|
|
|
gst_element_set_state (pipeline, GST_STATE_NULL);
|
|
|
|
gst_object_unref (pipeline);
|
|
|
|
|
|
|
|
g_main_loop_unref (loop);
|
2013-07-28 08:42:34 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|