-
Notifications
You must be signed in to change notification settings - Fork 0
/
cuda_rtsp.c
376 lines (329 loc) · 10.6 KB
/
cuda_rtsp.c
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
#include "cuda_rtsp.h"
#include <assert.h>
#include <stdio.h>
#define GST_USE_UNSTABLE_API 1
#include <gst/gst.h>
#include <gst/app/gstappsrc.h>
#include <gst/cuda/cuda-gst.h>
#include <gst/cuda/gstcudabufferpool.h>
#include <gst/cuda/gstcudamemory.h>
#include <gst/rtsp-server/rtsp-server.h>
const char *FORMATS[] = {
"NV12",
"YV12",
"I420",
"BGRA",
"RGBA",
"Y444",
"VUYA",
"ARGB",
"ABGR",
"BGR",
"RGB",
};
typedef struct CUrtsp_server_st
{
GMainContext *context;
GMainLoop *loop;
GstRTSPServer *gst_rtsp_server;
int server_id;
} CUrtsp_server_st;
typedef struct CUrtsp_session_st
{
CUDA_RTSP_SESSION session_info;
GstRTSPMediaFactory *gst_rtsp_media_factory;
GstContext *gst_context;
GstCudaContext *gst_cuda_context;
GstBufferPool *cu_buffer_pool;
GstClockTime timestamp;
} CUrtsp_session_st;
// error buffer
char current_error[256];
// set contents of error buffer
static void cuRTSPSetError(const char *format, ...);
// check if video format requires cudaconvert element
static bool cuRTSPSessionNeedsConvert(CUrtsp_format format);
static void cuRTSPSessionConfigure(
GstRTSPMediaFactory *factory,
GstRTSPMedia *media,
CUrtsp_session hSession);
static void cuRTSPSessionPushBuffer(
GstElement *appsrc,
guint unused,
CUrtsp_session hSession);
static void cuRTSPSessionDestroy(
CUrtsp_session hSession);
void cuRTSPInit()
{
assert(cuInit(0) == CUDA_SUCCESS);
gst_init(NULL, NULL);
assert(gst_cuda_load_library() == TRUE);
gst_cuda_memory_init_once();
}
void cuRTSPDeinit()
{
gst_deinit();
}
const char *cuRTSPGetError()
{
const char *result;
result = NULL;
if (strlen(current_error) > 0)
{
result = ¤t_error[0];
}
return result;
}
CUresult cuRTSPServerCreate(CUrtsp_server *pServer, const CUDA_RTSP_SERVER *pCreateServer)
{
CUresult result;
char service[7];
result = CUDA_SUCCESS;
if (pServer == NULL)
{
result = CUDA_ERROR_INVALID_VALUE;
cuRTSPSetError("cuRTSPServerCreate: pServer cannot be NULL");
goto error;
}
(*pServer) = calloc(1, sizeof(struct CUrtsp_server_st));
(*pServer)->loop = NULL;
(*pServer)->gst_rtsp_server = gst_rtsp_server_new();
if (pCreateServer != NULL && pCreateServer->port != CU_RTSP_DEFAULT_PORT)
{
if (pCreateServer->port < 1 || pCreateServer->port > 65535)
{
result = CUDA_ERROR_INVALID_VALUE;
cuRTSPSetError("cuRTSPServerCreate: port value must be [1,65535]");
goto error;
}
snprintf(service, sizeof(service) / sizeof(service[0]), "%d", (int)pCreateServer->port);
gst_rtsp_server_set_service((*pServer)->gst_rtsp_server, service);
}
goto done;
error:
if ((*pServer) != NULL)
{
cuRTSPServerDestroy(*pServer);
}
done:
return result;
}
void cuRTSPServerDestroy(CUrtsp_server hServer)
{
if (hServer != NULL)
{
if (hServer->gst_rtsp_server != NULL)
{
gst_object_unref(hServer->gst_rtsp_server);
}
free(hServer);
}
}
CUresult cuRTSPServerAttachGMain(CUrtsp_server hServer, GMainContext *context)
{
CUresult result;
result = CUDA_SUCCESS;
if (hServer != NULL)
{
hServer->context = context;
hServer->server_id = gst_rtsp_server_attach(hServer->gst_rtsp_server, context);
}
else
{
result = CUDA_ERROR_INVALID_VALUE;
cuRTSPSetError("cuRTSPServerAttachGMain: hServer cannot be NULL");
}
return result;
}
CUresult cuRTSPServerAttach(CUrtsp_server hServer)
{
return cuRTSPServerAttachGMain(hServer, NULL);
}
void cuRTSPServerDispatch(CUrtsp_server hServer)
{
hServer->loop = g_main_loop_new(hServer->context, FALSE);
g_main_loop_run(hServer->loop);
}
void cuRTSPServerShutdown(CUrtsp_server hServer)
{
g_main_loop_quit(hServer->loop);
}
CUresult cuRTSPSessionCreate(CUrtsp_session *pSession, const CUDA_RTSP_SESSION *pCreateSession)
{
const char *launch_string_no_convert = "( appsrc name=source ! nvh264enc ! rtph264pay name=pay0 pt=96 )";
const char *launch_string_with_convert = "( appsrc name=source ! cudaconvert ! nvh264enc ! rtph264pay name=pay0 pt=96 )";
CUresult result;
GstStructure *s;
gint device_id;
const char *launch_string;
result = CUDA_SUCCESS;
if (pSession == NULL)
{
cuRTSPSetError("cuRTSPSessionCreate: pSession cannot be NULL");
goto error;
}
if (pCreateSession == NULL)
{
cuRTSPSetError("cuRTSPSessionCreate: pCreateSession cannot be NULL");
goto error;
}
if (cuRTSPSessionNeedsConvert(pCreateSession->format))
{
launch_string = launch_string_with_convert;
}
else
{
launch_string = launch_string_no_convert;
}
*pSession = calloc(1, sizeof(CUrtsp_session_st));
(*pSession)->session_info.device = pCreateSession->device;
(*pSession)->session_info.context = pCreateSession->context;
(*pSession)->session_info.width = pCreateSession->width;
(*pSession)->session_info.height = pCreateSession->height;
(*pSession)->session_info.format = pCreateSession->format;
(*pSession)->session_info.fpsNum = pCreateSession->fpsNum;
(*pSession)->session_info.fpsDen = pCreateSession->fpsDen;
(*pSession)->session_info.writeCallback = pCreateSession->writeCallback;
(*pSession)->session_info.userData = pCreateSession->userData;
(*pSession)->gst_rtsp_media_factory = gst_rtsp_media_factory_new();
(*pSession)->gst_cuda_context = gst_cuda_context_new_wrapped((*pSession)->session_info.context, (*pSession)->session_info.device);
(*pSession)->gst_context = gst_context_new(GST_CUDA_CONTEXT_TYPE, TRUE);
g_object_get(G_OBJECT((*pSession)->gst_cuda_context), "cuda-device-id", &device_id, NULL);
s = gst_context_writable_structure((*pSession)->gst_context);
gst_structure_set(s, GST_CUDA_CONTEXT_TYPE, GST_TYPE_CUDA_CONTEXT,
(*pSession)->gst_cuda_context, "cuda-device-id", G_TYPE_INT, device_id, NULL);
(*pSession)->cu_buffer_pool = gst_cuda_buffer_pool_new((*pSession)->gst_cuda_context);
gst_rtsp_media_factory_set_launch((*pSession)->gst_rtsp_media_factory, launch_string);
gst_rtsp_media_factory_set_enable_rtcp((*pSession)->gst_rtsp_media_factory, (pCreateSession->live) ? FALSE : TRUE);
g_signal_connect((*pSession)->gst_rtsp_media_factory, "media-configure", (GCallback)cuRTSPSessionConfigure, *pSession);
goto done;
error:
result = CUDA_ERROR_INVALID_VALUE;
done:
return result;
}
CUresult cuRTSPSessionMount(CUrtsp_session hSession, CUrtsp_server hServer, const char *path)
{
CUresult result;
GstRTSPMountPoints *mount_points;
result = CUDA_SUCCESS;
mount_points = NULL;
if (hSession == NULL)
{
cuRTSPSetError("cuRTSPSessionMount: hSession cannot be NULL");
goto error;
}
if (hServer == NULL)
{
cuRTSPSetError("cuRTSPSessionMount: hServer cannot be NULL");
goto error;
}
mount_points = gst_rtsp_server_get_mount_points(hServer->gst_rtsp_server);
gst_rtsp_mount_points_add_factory(mount_points, path, hSession->gst_rtsp_media_factory);
goto done;
error:
result = CUDA_ERROR_INVALID_VALUE;
done:
if (mount_points != NULL)
{
g_object_unref(mount_points);
}
return result;
}
static void cuRTSPSetError(const char *format, ...)
{
va_list args;
va_start(args, format);
vsnprintf(current_error, sizeof(current_error) / sizeof(current_error[0]), format, args);
va_end(args);
}
static void cuRTSPSessionConfigure(
GstRTSPMediaFactory *factory,
GstRTSPMedia *media,
CUrtsp_session hSession)
{
const char *caps_format = "video/x-raw(memory:CUDAMemory),format=%s,width=%d,height=%d,framerate=%d/%d";
char caps_string[256];
GstElement *pipeline;
GstElement *appsrc;
GstCaps *caps;
GstVideoInfo video_info;
GstStructure *config;
snprintf(
&caps_string[0],
sizeof(caps_string) / sizeof(caps_string[0]),
caps_format,
FORMATS[hSession->session_info.format],
(int)hSession->session_info.width,
(int)hSession->session_info.height,
(int)hSession->session_info.fpsNum,
(int)hSession->session_info.fpsDen);
caps = gst_caps_from_string(&caps_string[0]);
pipeline = gst_rtsp_media_get_element(media);
gst_element_set_context(pipeline, GST_CONTEXT(hSession->gst_context));
appsrc = gst_bin_get_by_name_recurse_up(GST_BIN(pipeline), "source");
gst_util_set_object_arg(G_OBJECT(appsrc), "format", "time");
g_object_set(G_OBJECT(appsrc), "caps",
caps,
NULL);
gst_video_info_from_caps(&video_info, caps);
config = gst_buffer_pool_get_config(hSession->cu_buffer_pool);
gst_buffer_pool_config_set_params(config, caps, video_info.size, 2, 0);
gst_buffer_pool_set_config(hSession->cu_buffer_pool, config);
gst_buffer_pool_set_active(hSession->cu_buffer_pool, TRUE);
g_signal_connect(appsrc, "need-data", (GCallback)cuRTSPSessionPushBuffer, hSession);
gst_caps_unref(caps);
gst_object_unref(appsrc);
gst_object_unref(pipeline);
}
static void cuRTSPSessionPushBuffer(
GstElement *appsrc,
guint unused,
CUrtsp_session hSession)
{
GstBuffer *buffer;
GstMapInfo map_info;
GstFlowReturn ret;
assert(hSession != NULL);
assert(cuCtxPushCurrent(hSession->session_info.context) == CUDA_SUCCESS);
gst_buffer_pool_acquire_buffer(hSession->cu_buffer_pool, &buffer, NULL);
gst_buffer_map(buffer, &map_info, GST_MAP_WRITE);
hSession->session_info.writeCallback(
(CUdeviceptr)map_info.data,
map_info.size,
hSession->session_info.userData);
gst_buffer_unmap(buffer, &map_info);
GST_BUFFER_PTS(buffer) = hSession->timestamp;
GST_BUFFER_DURATION(buffer) = gst_util_uint64_scale_int(
hSession->session_info.fpsDen,
GST_SECOND,
hSession->session_info.fpsNum);
hSession->timestamp += GST_BUFFER_DURATION(buffer);
g_signal_emit_by_name(appsrc, "push-buffer", buffer, &ret);
gst_buffer_unref(buffer);
assert(cuCtxPopCurrent(&hSession->session_info.context) == CUDA_SUCCESS);
}
static void cuRTSPSessionDestroy(
CUrtsp_session hSession)
{
}
bool cuRTSPSessionNeedsConvert(CUrtsp_format format)
{
bool result;
switch (format)
{
case CU_RTSP_FORMAT_NV12:
case CU_RTSP_FORMAT_YV12:
case CU_RTSP_FORMAT_I420:
case CU_RTSP_FORMAT_BGRA:
case CU_RTSP_FORMAT_RGBA:
case CU_RTSP_FORMAT_Y444:
case CU_RTSP_FORMAT_VUYA:
result = false;
break;
default:
result = true;
break;
}
return result;
}