File: gstcudaipcserver_unix.cpp

package info (click to toggle)
gst-plugins-bad1.0 1.28.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 72,252 kB
  • sloc: ansic: 744,658; cpp: 300,297; objc: 3,559; xml: 3,351; sh: 1,095; python: 565; makefile: 181; java: 75
file content (406 lines) | stat: -rw-r--r-- 12,285 bytes parent folder | download | duplicates (4)
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/* GStreamer
 * Copyright (C) 2023 Seungha Yang <seungha@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.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "gstcudaipcserver_unix.h"
#include <string.h>
#include <string>

#include <gio/gunixsocketaddress.h>
#include <gio/gunixconnection.h>
#include <glib/gstdio.h>

GST_DEBUG_CATEGORY_EXTERN (cuda_ipc_server_debug);
#define GST_CAT_DEFAULT cuda_ipc_server_debug

/* *INDENT-OFF* */
struct GstCudaIpcServerConnUnix : public GstCudaIpcServerConn
{
  GstCudaIpcServerConnUnix (GSocketConnection * conn)
  {
    socket_conn = (GSocketConnection *) g_object_ref (conn);
    istream = g_io_stream_get_input_stream (G_IO_STREAM (socket_conn));
    ostream = g_io_stream_get_output_stream (G_IO_STREAM (socket_conn));
  }

  ~GstCudaIpcServerConnUnix ()
  {
    g_clear_object (&socket_conn);
  }

  void CloseConn ()
  {
    if (socket_conn)
      g_io_stream_close (G_IO_STREAM (socket_conn), nullptr, nullptr);
  }

  /* Holds ref */
  GSocketConnection *socket_conn = nullptr;

  /* Owned by socket_conn */
  GInputStream *istream;
  GOutputStream *ostream;
};
struct GstCudaIpcServerUnixPrivate
{
  GstCudaIpcServerUnixPrivate ()
  {
    main_context = g_main_context_new ();
    main_loop = g_main_loop_new (main_context, FALSE);
    cancellable = g_cancellable_new ();
  }

  ~GstCudaIpcServerUnixPrivate ()
  {
    g_main_loop_unref (main_loop);
    g_main_context_unref (main_context);
    g_object_unref (cancellable);
  }

  std::string address;
  GMainLoop *main_loop;
  GMainContext *main_context;
  GCancellable *cancellable;
};
/* *INDENT-ON* */

struct _GstCudaIpcServerUnix
{
  GstCudaIpcServer parent;

  GstCudaIpcServerUnixPrivate *priv;
};

static void gst_cuda_ipc_server_unix_finalize (GObject * object);
static void gst_cuda_ipc_server_unix_loop (GstCudaIpcServer * server);
static void gst_cuda_ipc_server_unix_terminate (GstCudaIpcServer * server);
static void gst_cuda_ipc_server_unix_invoke (GstCudaIpcServer * server);
static bool gst_cuda_ipc_server_unix_wait_msg (GstCudaIpcServer * server,
    GstCudaIpcServerConn * conn);
static bool gst_cuda_ipc_server_unix_send_msg (GstCudaIpcServer * server,
    GstCudaIpcServerConn * conn);
static bool gst_cuda_ipc_server_unix_send_mmap_msg (GstCudaIpcServer * server,
    GstCudaIpcServerConn * conn, GstCudaSharableHandle handle);

#define gst_cuda_ipc_server_unix_parent_class parent_class
G_DEFINE_TYPE (GstCudaIpcServerUnix,
    gst_cuda_ipc_server_unix, GST_TYPE_CUDA_IPC_SERVER);

static void
gst_cuda_ipc_server_unix_class_init (GstCudaIpcServerUnixClass * klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GstCudaIpcServerClass *server_class = GST_CUDA_IPC_SERVER_CLASS (klass);

  object_class->finalize = gst_cuda_ipc_server_unix_finalize;

  server_class->loop = GST_DEBUG_FUNCPTR (gst_cuda_ipc_server_unix_loop);
  server_class->terminate =
      GST_DEBUG_FUNCPTR (gst_cuda_ipc_server_unix_terminate);
  server_class->invoke = GST_DEBUG_FUNCPTR (gst_cuda_ipc_server_unix_invoke);
  server_class->wait_msg =
      GST_DEBUG_FUNCPTR (gst_cuda_ipc_server_unix_wait_msg);
  server_class->send_msg =
      GST_DEBUG_FUNCPTR (gst_cuda_ipc_server_unix_send_msg);
  server_class->send_mmap_msg =
      GST_DEBUG_FUNCPTR (gst_cuda_ipc_server_unix_send_mmap_msg);
}

static void
gst_cuda_ipc_server_unix_init (GstCudaIpcServerUnix * self)
{
  self->priv = new GstCudaIpcServerUnixPrivate ();
}

static void
gst_cuda_ipc_server_unix_finalize (GObject * object)
{
  GstCudaIpcServerUnix *self = GST_CUDA_IPC_SERVER_UNIX (object);

  GST_DEBUG_OBJECT (self, "finalize");

  delete self->priv;

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
gst_cuda_ipc_server_unix_terminate (GstCudaIpcServer * server)
{
  GstCudaIpcServerUnix *self = GST_CUDA_IPC_SERVER_UNIX (server);
  GstCudaIpcServerUnixPrivate *priv = self->priv;

  GST_DEBUG_OBJECT (self, "terminate");

  g_main_loop_quit (priv->main_loop);
}

static gboolean
gst_cuda_ipc_server_invoke_func (GstCudaIpcServer * server)
{
  gst_cuda_ipc_server_on_idle (server);

  return G_SOURCE_REMOVE;
}

static void
gst_cuda_ipc_server_unix_invoke (GstCudaIpcServer * server)
{
  GstCudaIpcServerUnix *self = GST_CUDA_IPC_SERVER_UNIX (server);
  GstCudaIpcServerUnixPrivate *priv = self->priv;

  g_main_context_invoke (priv->main_context,
      (GSourceFunc) gst_cuda_ipc_server_invoke_func, server);
}

static void
gst_cuda_ipc_server_unix_payload_finish (GObject * source,
    GAsyncResult * result, GstCudaIpcServerConnUnix * conn)
{
  GstCudaIpcServer *server = conn->server;
  gsize size;
  GError *err = nullptr;
  bool ret = true;

  if (!g_input_stream_read_all_finish (conn->istream, result, &size, &err)) {
    GST_WARNING_OBJECT (server, "Read failed with %s, conn-id: %u",
        err->message, conn->id);
    g_clear_error (&err);
    ret = false;
  }

  gst_cuda_ipc_server_wait_msg_finish (server, conn, ret);
}

static void
gst_cuda_ipc_server_unix_wait_msg_finish (GObject * source,
    GAsyncResult * result, GstCudaIpcServerConnUnix * conn)
{
  GstCudaIpcServerUnix *self = GST_CUDA_IPC_SERVER_UNIX (conn->server);
  GstCudaIpcServerUnixPrivate *priv = self->priv;
  gsize size;
  GError *err = nullptr;
  GstCudaIpcPacketHeader header;

  if (!g_input_stream_read_all_finish (conn->istream, result, &size, &err)) {
    GST_WARNING_OBJECT (self, "Read failed with %s, conn-id: %u",
        err->message, conn->id);
    g_clear_error (&err);
    gst_cuda_ipc_server_wait_msg_finish (conn->server, conn, false);
    return;
  }

  if (!gst_cuda_ipc_pkt_identify (conn->client_msg, header)) {
    GST_ERROR_OBJECT (self, "Broken header, conn-id: %u", conn->id);
    gst_cuda_ipc_server_wait_msg_finish (conn->server, conn, false);
    return;
  }

  if (header.payload_size == 0) {
    gst_cuda_ipc_server_wait_msg_finish (conn->server, conn, true);
    return;
  }

  GST_LOG_OBJECT (self, "Reading payload");

  g_input_stream_read_all_async (conn->istream, &conn->client_msg[0] +
      GST_CUDA_IPC_PKT_HEADER_SIZE, header.payload_size, G_PRIORITY_DEFAULT,
      priv->cancellable,
      (GAsyncReadyCallback) gst_cuda_ipc_server_unix_payload_finish, conn);
}

static bool
gst_cuda_ipc_server_unix_wait_msg (GstCudaIpcServer * server,
    GstCudaIpcServerConn * conn)
{
  GstCudaIpcServerUnix *self = GST_CUDA_IPC_SERVER_UNIX (conn->server);
  GstCudaIpcServerUnixPrivate *priv = self->priv;
  GstCudaIpcServerConnUnix *unix_conn =
      static_cast < GstCudaIpcServerConnUnix * >(conn);

  GST_LOG_OBJECT (self, "Waiting for client message");

  g_input_stream_read_all_async (unix_conn->istream, &conn->client_msg[0],
      GST_CUDA_IPC_PKT_HEADER_SIZE, G_PRIORITY_DEFAULT, priv->cancellable,
      (GAsyncReadyCallback) gst_cuda_ipc_server_unix_wait_msg_finish,
      unix_conn);

  return true;
}

static void
gst_cuda_ipc_server_unix_send_msg_finish (GObject * source,
    GAsyncResult * result, GstCudaIpcServerConnUnix * conn)
{
  GstCudaIpcServerUnix *self = GST_CUDA_IPC_SERVER_UNIX (conn->server);
  gsize size;
  GError *err = nullptr;

  if (!g_output_stream_write_all_finish (conn->ostream, result, &size, &err)) {
    GST_WARNING_OBJECT (self, "Write failed with %s, conn-id: %u",
        err->message, conn->id);
    g_clear_error (&err);
    gst_cuda_ipc_server_send_msg_finish (conn->server, conn, false);
    return;
  }

  GST_LOG_OBJECT (self, "Sent message");

  gst_cuda_ipc_server_send_msg_finish (conn->server, conn, true);
}

static bool
gst_cuda_ipc_server_unix_send_msg (GstCudaIpcServer * server,
    GstCudaIpcServerConn * conn)
{
  GstCudaIpcServerUnix *self = GST_CUDA_IPC_SERVER_UNIX (conn->server);
  GstCudaIpcServerUnixPrivate *priv = self->priv;
  GstCudaIpcServerConnUnix *unix_conn =
      static_cast < GstCudaIpcServerConnUnix * >(conn);

  GST_LOG_OBJECT (self, "Sending message");

  g_output_stream_write_all_async (unix_conn->ostream, &conn->server_msg[0],
      conn->server_msg.size (), G_PRIORITY_DEFAULT, priv->cancellable,
      (GAsyncReadyCallback) gst_cuda_ipc_server_unix_send_msg_finish,
      unix_conn);

  return true;
}

static bool
gst_cuda_ipc_server_unix_send_mmap_msg (GstCudaIpcServer * server,
    GstCudaIpcServerConn * conn, GstCudaSharableHandle handle)
{
  GstCudaIpcServerUnix *self = GST_CUDA_IPC_SERVER_UNIX (conn->server);
  GstCudaIpcServerUnixPrivate *priv = self->priv;
  GstCudaIpcServerConnUnix *unix_conn =
      static_cast < GstCudaIpcServerConnUnix * >(conn);
  GError *err = nullptr;

  GST_LOG_OBJECT (self, "Sending mmap message");

  if (!g_output_stream_write_all (unix_conn->ostream, &conn->server_msg[0],
          conn->server_msg.size (), nullptr, priv->cancellable, &err)) {
    GST_WARNING_OBJECT (self, "Couldn't write mmap data, %s", err->message);
    g_clear_error (&err);
    return false;
  }

  if (!g_unix_connection_send_fd (G_UNIX_CONNECTION (unix_conn->socket_conn),
          handle, priv->cancellable, &err)) {
    GST_WARNING ("Couldn't send fd, %s", err->message);
    g_clear_error (&err);
    return false;
  }

  gst_cuda_ipc_server_send_msg_finish (server, conn, true);

  return true;
}

static gboolean
gst_cuda_ipc_server_unix_on_incoming (GSocketService * service,
    GSocketConnection * socket_conn, GObject * source_obj,
    GstCudaIpcServer * self)
{
  GST_DEBUG_OBJECT (self, "Got new connection");

  auto conn = std::make_shared < GstCudaIpcServerConnUnix > (socket_conn);
  gst_cuda_ipc_server_on_incoming_connection (self, conn);

  return TRUE;
}

static void
gst_cuda_ipc_server_unix_loop (GstCudaIpcServer * server)
{
  GstCudaIpcServerUnix *self = GST_CUDA_IPC_SERVER_UNIX (server);
  GstCudaIpcServerUnixPrivate *priv = self->priv;
  GError *err = nullptr;
  GSocketAddress *addr;
  GSocketService *service;
  gboolean ret;

  g_main_context_push_thread_default (priv->main_context);

  service = g_socket_service_new ();
  addr = g_unix_socket_address_new (priv->address.c_str ());

  GST_DEBUG_OBJECT (self, "Creating service with address \"%s\"",
      priv->address.c_str ());

  ret = g_socket_listener_add_address (G_SOCKET_LISTENER (service),
      addr, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, nullptr,
      nullptr, &err);
  g_object_unref (addr);

  if (!ret) {
    GST_ERROR_OBJECT (self, "Setup failed, error: %s", err->message);
    g_clear_error (&err);
    g_clear_object (&service);
    gst_cuda_ipc_server_abort (server);
  } else {
    g_signal_connect (service,
        "incoming", G_CALLBACK (gst_cuda_ipc_server_unix_on_incoming), self);
    g_socket_service_start (service);
  }

  GST_DEBUG_OBJECT (self, "Starting loop");

  g_main_loop_run (priv->main_loop);

  GST_DEBUG_OBJECT (self, "Loop stopped");

  if (service) {
    g_cancellable_cancel (priv->cancellable);
    g_unlink (priv->address.c_str ());
    g_clear_object (&service);
  }

  g_main_context_pop_thread_default (priv->main_context);
}

GstCudaIpcServer *
gst_cuda_ipc_server_new (const gchar * address, GstCudaContext * context,
    GstCudaIpcMode ipc_mode)
{
  GstCudaIpcServerUnix *self;
  GstCudaIpcServer *server;

  g_return_val_if_fail (address, nullptr);
  g_return_val_if_fail (GST_IS_CUDA_CONTEXT (context), nullptr);

  self = (GstCudaIpcServerUnix *)
      g_object_new (GST_TYPE_CUDA_IPC_SERVER_UNIX, nullptr);
  gst_object_ref_sink (self);

  self->priv->address = address;
  server = GST_CUDA_IPC_SERVER (self);
  server->context = (GstCudaContext *) gst_object_ref (context);
  server->ipc_mode = ipc_mode;
  server->pid = getpid ();

  gst_cuda_ipc_server_run (server);

  return server;
}