summaryrefslogtreecommitdiff
path: root/src/lib.c
blob: cc36292fe24f237a7509ee0f91e16d4754b8ad6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 * vim: set sts=2 sw=2 et :
 *
 * License: LGPL-2.1+
 * Copyright (c) 2014 Nirbheek Chauhan <nirbheek@centricular.com>
 *
 * 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.
 */

#include "lib.h"

static void stp_disconnect_cleanup_client (STPClientCtx *ctx);

gboolean
invoke_g_hash_table_remove (STPServerCtx *ctx)
{
  g_hash_table_remove (ctx->parent_ctx_table, ctx->sessionid);
  return G_SOURCE_REMOVE;
}

gboolean
invoke_g_free_client_context (STPClientCtx *ctx)
{
  g_free (ctx);
  return G_SOURCE_REMOVE;
}

guint
stp_get_stream_type_from_string (const char *type)
{
  if (type == NULL)
    return STP_STREAM_TYPE_ALL;

  if (soup_str_case_equal (type, "http"))
    return STP_STREAM_TYPE_HTTP;
  
  if (soup_str_case_equal (type, "rtp-udp"))
    return STP_STREAM_TYPE_RTP_UDP;

  if (soup_str_case_equal (type, "rtp-udp,http") ||
      soup_str_case_equal (type, "http,rtp-udp"))
    return STP_STREAM_TYPE_ALL;

  return STP_STREAM_TYPE_INVALID;
}

const char*
stp_get_stream_type_string (guint stream_type)
{
  switch (stream_type) {
    case STP_STREAM_TYPE_HTTP:
      return "http";
    case STP_STREAM_TYPE_RTP_UDP:
      return "rtp-udp";
    case STP_STREAM_TYPE_ALL:
      return "http,rtp-udp";
    default:
      return "invalid";
  }
}

gboolean
stp_validate_token_server (GInetAddressMask  *mask,
                           SoupClientContext *client)
{
  gboolean ret;
  GInetAddress *addr;

  if (!mask)
    return TRUE;

  addr = g_inet_address_new_from_string (soup_client_context_get_host (client));
  ret = g_inet_address_mask_matches (mask, addr);
  g_object_unref (addr);
  return ret;
}

gboolean
stp_on_gst_bus_message (GstBus       *bus,
                        GstMessage   *msg,
                        STPServerCtx *ctx)
{
  GError *error = NULL;
  char *tmp = NULL;

  switch (GST_MESSAGE_TYPE (msg)) {
    case GST_MESSAGE_ERROR:
      gst_message_parse_error (msg, &error, &tmp);
      g_critical ("ERROR from element %s: %s",
                  GST_OBJECT_NAME (msg->src), error->message);
      g_critical ("Debug info: %s", tmp);
      g_error_free (error);
      g_free (tmp);
      /* Setting the server response will only work if the request
       * hasn't already finished, so we check that */
      if (ctx->status == STP_STATUS_STREAMING &&
          ctx->encoding != SOUP_ENCODING_CONTENT_LENGTH)
        soup_message_set_status (ctx->msg, SOUP_STATUS_INTERNAL_SERVER_ERROR);
      /* Cleanup in the default main context,
       * because GHashTable is not thread-safe */
      g_main_context_invoke_full (NULL, G_PRIORITY_LOW,
                                  (GSourceFunc)invoke_g_hash_table_remove,
                                  ctx, NULL);
      break;
    case GST_MESSAGE_EOS:
      g_debug ("End of file");
      /* Cleanup in the default main context,
       * because GHashTable is not thread-safe */
      g_main_context_invoke_full (NULL, G_PRIORITY_LOW,
                                  (GSourceFunc)invoke_g_hash_table_remove,
                                  ctx, NULL);
      break;
    default:
      //stp_print_status ("%s\n", gst_message_type_get_name (msg->type));
      break;
  }

  return TRUE;
}

/* When the incoming stream reaches EOS, we call this
 * which initiates a shutdown for all clients and then
 * the server itself */
void
stp_server_ctx_cleanup (STPServerCtx *ctx)
{
  g_debug (">>> Doing server cleanup");

  g_source_remove (ctx->timeout_handler_id);
  ctx->timeout_handler_id = 0;

  /* Disconnect and free the context for each client */
  g_list_foreach (ctx->clients, (GFunc)stp_disconnect_cleanup_client, NULL);
  g_list_free (ctx->clients);

  /* Cleanup gstreamer pipeline */
  if (ctx->pipeline) {
    gst_element_set_state (ctx->pipeline, GST_STATE_NULL);
    gst_object_unref (ctx->pipeline);
  }

  g_free (ctx->udp_clients);
  g_free (ctx);
}

static GstPadProbeReturn
pad_blocked_cleanup_cb (GstPad             *srcpad,
                        GstPadProbeInfo    *info,
                        STPClientCtx *ctx)
{
  GstElement *tee;
  GstElement *sinkbin = GST_ELEMENT (gst_element_get_parent (ctx->appsink));

  stp_print_status (".");
  /* Remove the probe */
  gst_pad_remove_probe (srcpad, GST_PAD_PROBE_INFO_ID (info));

  tee = gst_pad_get_parent_element (srcpad);
  gst_pad_unlink (srcpad, ctx->ghostsinkpad);
  gst_element_release_request_pad (tee, srcpad);
  gst_object_unref (srcpad);
  gst_object_unref (tee);

  gst_element_set_state (sinkbin, GST_STATE_NULL);
  gst_bin_remove (GST_BIN (ctx->server_ctx->pipeline), sinkbin);
  gst_object_unref (sinkbin);

  stp_print_status (" Client cleanup done!\n");
  return GST_PAD_PROBE_OK;
}

/* When a client leaves or is kicked, we set the response status
 * and then call cleanup here. This involves removing the appsink
 * branch for the client from the pipeline, and then freeing the
 * context. */
void
stp_client_ctx_cleanup (STPClientCtx *ctx)
{
  GstPad *srcpad;
  STPServerCtx *server_ctx = ctx->server_ctx;
  GstElement *sinkbin = GST_ELEMENT (gst_element_get_parent (ctx->appsink));

  stp_print_status (">>> Doing client cleanup.");

  g_source_remove (ctx->timeout_handler_id);
  ctx->timeout_handler_id = 0;

  server_ctx->clients = g_list_remove (server_ctx->clients, ctx);

  /* Block sinkpad and srcpad, then unlink and remove */
  srcpad = gst_pad_get_peer (ctx->ghostsinkpad);

  gst_pad_add_probe (srcpad, GST_PAD_PROBE_TYPE_BLOCK,
                     (GstPadProbeCallback) pad_blocked_cleanup_cb,
                     ctx, (GDestroyNotify)g_free);

  gst_object_unref (sinkbin);
  stp_print_status (".");
}

void
stp_stream_token_free (STPStreamToken *token)
{
  g_free (token->udp_clients);
  g_free (token);
}

/* When shutting down a client due to a server shutdown,
 * we just need to remove the client timeout handler and
 * free the context. The rest is owned by the pipeline. */
static void
stp_disconnect_cleanup_client (STPClientCtx *ctx)
{
  stp_print_status (">>> Disconnecting client on server shutdown\n");

  /* FIXME: This isn't actually setting the status for client 
   * connections, and clients are just left hanging */
  soup_message_set_status (ctx->msg, SOUP_STATUS_GONE);
  soup_message_body_complete (ctx->msg->response_body);

  g_source_remove (ctx->timeout_handler_id);
  ctx->timeout_handler_id = 0;
  /* Don't call the "finished" handler and do a double-free */
  g_signal_handler_disconnect (ctx->msg, ctx->finished_handler_id);

  /* There might still be functions which use the ctx waiting 
   * to be invoked on the next main context dispatch, so we 
   * free this at the end of all those dispatches. */
  g_main_context_invoke_full (NULL, G_PRIORITY_LOW,
                              (GSourceFunc)invoke_g_free_client_context,
                              ctx, NULL);
}

/* Returns a copy of the streamheader GstBuffer */
GstBuffer*
stp_get_streamheader_from_caps (GstCaps *caps)
{
  GstStructure *s;
  const GValue *array, *value;

  s = gst_caps_get_structure (caps, 0);
  array = gst_structure_get_value (s, "streamheader");
  g_return_val_if_fail (GST_VALUE_HOLDS_ARRAY (array), NULL);

  value = gst_value_array_get_value (array, 0);
  g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_BUFFER, NULL);

  return gst_buffer_copy (g_value_peek_pointer (value));
}

/* Returns a GstBuffer which has consumed the passed-in data */
GstBuffer*
stp_get_gst_buffer (SoupBuffer *chunk)
{
  gsize len;
  const guint8 *d;
  SoupBuffer *copy;
  GstBuffer *buffer;
  GstMemory *memory;

  copy = soup_buffer_copy (chunk);
  soup_buffer_get_data (copy, &d, &len);
  buffer = gst_buffer_new ();
  memory = gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY,
                                  (guint8*)d, len, 0, len, copy,
                                  (GDestroyNotify)soup_buffer_free);
  gst_buffer_append_memory (buffer, memory);
  return buffer;
}