forked from felipec/gst-dsp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gstdspdummy.c
456 lines (356 loc) · 9.59 KB
/
gstdspdummy.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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*
* Copyright (C) 2009 Nokia Corporation.
*
* Author: Felipe Contreras <felipe.contreras@nokia.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation
* version 2.1 of the License.
*
* 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "gstdspdummy.h"
#include "plugin.h"
#include "util.h"
#include "dsp_bridge.h"
#include <string.h> /* for memcpy */
#define GST_CAT_DEFAULT gstdsp_debug
static GstElementClass *parent_class;
static GstCaps *
generate_src_template(void)
{
GstCaps *caps;
caps = gst_caps_new_any();
return caps;
}
static GstCaps *
generate_sink_template(void)
{
GstCaps *caps;
caps = gst_caps_new_any();
return caps;
}
static inline void *
create_node(int dsp_handle,
void *proc)
{
dsp_node_t *node;
const dsp_uuid_t dummy_uuid = { 0x3dac26d0, 0x6d4b, 0x11dd, 0xad, 0x8b,
{ 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66 } };
if (!gstdsp_register(dsp_handle, &dummy_uuid, DSP_DCD_NODETYPE, "dummy.dll64P")) {
GST_ERROR("dsp node register failed");
return NULL;
}
if (!gstdsp_register(dsp_handle, &dummy_uuid, DSP_DCD_LIBRARYTYPE, "dummy.dll64P")) {
GST_ERROR("dsp node register failed");
return NULL;
}
if (!dsp_node_allocate(dsp_handle, proc, &dummy_uuid, NULL, NULL, &node)) {
GST_ERROR("dsp node allocate failed");
return NULL;
}
if (!dsp_node_create(dsp_handle, node)) {
GST_ERROR("dsp node create failed");
return NULL;
}
GST_INFO("dsp node created");
return node;
}
static inline bool
destroy_node(int dsp_handle,
dsp_node_t *node)
{
if (node) {
if (!dsp_node_free(dsp_handle, node)) {
GST_ERROR("dsp node free failed");
return false;
}
GST_INFO("dsp node deleted");
}
return true;
}
static gboolean
dsp_init(GstDspDummy *self)
{
int dsp_handle;
self->dsp_handle = dsp_handle = dsp_open();
if (dsp_handle < 0) {
GST_ERROR("dsp open failed");
return FALSE;
}
if (!dsp_attach(dsp_handle, 0, NULL, &self->proc)) {
GST_ERROR("dsp attach failed");
goto fail;
}
self->node = create_node(dsp_handle, self->proc);
if (!self->node) {
GST_ERROR("dsp node creation failed");
goto fail;
}
return TRUE;
fail:
if (self->proc) {
if (!dsp_detach(dsp_handle, self->proc))
GST_ERROR("dsp detach failed");
self->proc = NULL;
}
if (self->dsp_handle >= 0) {
if (dsp_close(dsp_handle) < 0)
GST_ERROR("dsp close failed");
self->dsp_handle = -1;
}
return FALSE;
}
static gboolean
dsp_deinit(GstDspDummy *self)
{
gboolean ret = TRUE;
if (self->node) {
if (!destroy_node(self->dsp_handle, self->node)) {
GST_ERROR("dsp node destroy failed");
ret = FALSE;
}
self->node = NULL;
}
if (self->proc) {
if (!dsp_detach(self->dsp_handle, self->proc)) {
GST_ERROR("dsp detach failed");
ret = FALSE;
}
self->proc = NULL;
}
if (self->dsp_handle >= 0) {
if (dsp_close(self->dsp_handle) < 0) {
GST_ERROR("dsp close failed");
ret = FALSE;
}
self->dsp_handle = -1;
}
return ret;
}
static inline void
configure_dsp_node(int dsp_handle,
dsp_node_t *node,
dmm_buffer_t *input_buffer,
dmm_buffer_t *output_buffer)
{
dsp_send_message(dsp_handle, node, 0,
(uint32_t) input_buffer->map,
(uint32_t) output_buffer->map);
}
static gboolean
dsp_start(GstDspDummy *self)
{
if (!dsp_node_run(self->dsp_handle, self->node)) {
GST_ERROR("dsp node run failed");
return FALSE;
}
GST_INFO("dsp node running");
self->in_buffer = dmm_buffer_new(self->dsp_handle, self->proc);
self->out_buffer = dmm_buffer_new(self->dsp_handle, self->proc);
self->in_buffer->alignment = 0;
return TRUE;
}
static gboolean
dsp_stop(GstDspDummy *self)
{
unsigned long exit_status;
dmm_buffer_free(self->out_buffer);
dmm_buffer_free(self->in_buffer);
if (!dsp_node_terminate(self->dsp_handle, self->node, &exit_status)) {
GST_ERROR("dsp node terminate failed: %lx", exit_status);
return FALSE;
}
GST_INFO("dsp node terminated");
return TRUE;
}
static GstStateChangeReturn
change_state(GstElement *element,
GstStateChange transition)
{
GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
GstDspDummy *self;
self = GST_DSP_DUMMY(element);
switch (transition) {
case GST_STATE_CHANGE_NULL_TO_READY:
if (!dsp_init(self)) {
GST_ERROR("dsp init failed");
return GST_STATE_CHANGE_FAILURE;
}
break;
case GST_STATE_CHANGE_READY_TO_PAUSED:
if (!dsp_start(self)) {
GST_ERROR("dsp start failed");
return GST_STATE_CHANGE_FAILURE;
}
break;
default:
break;
}
ret = parent_class->change_state(element, transition);
if (ret == GST_STATE_CHANGE_FAILURE)
return ret;
switch (transition) {
case GST_STATE_CHANGE_PAUSED_TO_READY:
if (!dsp_stop(self)) {
GST_ERROR("dsp stop failed");
return GST_STATE_CHANGE_FAILURE;
}
break;
case GST_STATE_CHANGE_READY_TO_NULL:
if (!dsp_deinit(self)) {
GST_ERROR("dsp deinit failed");
return GST_STATE_CHANGE_FAILURE;
}
break;
default:
break;
}
return ret;
}
static inline void
map_buffer(GstDspDummy *self,
GstBuffer *g_buf,
dmm_buffer_t *d_buf)
{
#if 0
if (d_buf->alignment == 0 ||
(unsigned long) GST_BUFFER_DATA(g_buf) % d_buf->alignment == 0)
{
if (d_buf->data != GST_BUFFER_DATA(g_buf))
dmm_buffer_use(d_buf, GST_BUFFER_DATA(g_buf), GST_BUFFER_SIZE(g_buf));
d_buf->user_data = g_buf;
return;
}
if (d_buf->alignment != 0) {
GST_WARNING("buffer not aligned: %p, %lu",
GST_BUFFER_DATA(g_buf),
(unsigned long) GST_BUFFER_DATA(g_buf) % d_buf->alignment);
}
#endif
/* reallocate? */
if (!d_buf->allocated_data ||
d_buf->size > GST_BUFFER_SIZE(g_buf)) {
dmm_buffer_allocate(d_buf, GST_BUFFER_SIZE(g_buf));
}
d_buf->need_copy = true;
}
static GstFlowReturn
pad_chain(GstPad *pad,
GstBuffer *buf)
{
GstDspDummy *self;
GstFlowReturn ret;
GstBuffer *out_buf;
self = GST_DSP_DUMMY(GST_OBJECT_PARENT(pad));
ret = gst_pad_alloc_buffer_and_set_caps(self->srcpad,
GST_BUFFER_OFFSET_NONE,
GST_BUFFER_SIZE(buf),
GST_BUFFER_CAPS(buf),
&out_buf);
if (G_UNLIKELY(ret != GST_FLOW_OK)) {
GST_ERROR_OBJECT(self, "couldn't allocate buffer");
ret = GST_FLOW_ERROR;
goto leave;
}
/* map dsp to gpp address */
map_buffer(self, buf, self->in_buffer);
map_buffer(self, out_buf, self->out_buffer);
configure_dsp_node(self->dsp_handle, self->node, self->in_buffer, self->out_buffer);
if (self->in_buffer->need_copy) {
memcpy(self->in_buffer->data, GST_BUFFER_DATA(buf), GST_BUFFER_SIZE(buf));
self->in_buffer->need_copy = false;
}
{
dsp_msg_t msg;
dmm_buffer_clean(self->in_buffer, self->in_buffer->size);
msg.cmd = 1;
msg.arg_1 = self->in_buffer->size;
dsp_node_put_message(self->dsp_handle, self->node, &msg, -1);
dsp_node_get_message(self->dsp_handle, self->node, &msg, -1);
dmm_buffer_invalidate(self->out_buffer, self->out_buffer->size);
}
if (self->out_buffer->need_copy) {
memcpy(GST_BUFFER_DATA(out_buf), self->out_buffer->data, GST_BUFFER_SIZE(out_buf));
self->out_buffer->need_copy = false;
}
GST_BUFFER_TIMESTAMP(out_buf) = GST_BUFFER_TIMESTAMP(buf);
ret = gst_pad_push(self->srcpad, out_buf);
leave:
gst_buffer_unref(buf);
return ret;
}
static void
instance_init(GTypeInstance *instance,
gpointer g_class)
{
GstDspDummy *self;
GstElementClass *element_class;
element_class = GST_ELEMENT_CLASS(g_class);
self = GST_DSP_DUMMY(instance);
self->sinkpad =
gst_pad_new_from_template(gst_element_class_get_pad_template(element_class, "sink"), "sink");
gst_pad_set_chain_function(self->sinkpad, pad_chain);
self->srcpad =
gst_pad_new_from_template(gst_element_class_get_pad_template(element_class, "src"), "src");
gst_pad_use_fixed_caps(self->srcpad);
gst_element_add_pad(GST_ELEMENT(self), self->sinkpad);
gst_element_add_pad(GST_ELEMENT(self), self->srcpad);
}
static void
base_init(gpointer g_class)
{
GstElementClass *element_class;
GstPadTemplate *template;
GstElementDetails details;
element_class = GST_ELEMENT_CLASS(g_class);
details.longname = "DSP dummy element";
details.klass = "None";
details.description = "Copies the input to the output";
details.author = "Felipe Contreras";
gst_element_class_set_details(element_class, &details);
template = gst_pad_template_new("src", GST_PAD_SRC,
GST_PAD_ALWAYS,
generate_src_template());
gst_element_class_add_pad_template(element_class, template);
gst_object_unref(template);
template = gst_pad_template_new("sink", GST_PAD_SINK,
GST_PAD_ALWAYS,
generate_sink_template());
gst_element_class_add_pad_template(element_class, template);
gst_object_unref(template);
}
static void
class_init(gpointer g_class,
gpointer class_data)
{
GstElementClass *gstelement_class;
parent_class = g_type_class_peek_parent(g_class);
gstelement_class = GST_ELEMENT_CLASS(g_class);
gstelement_class->change_state = change_state;
}
GType
gst_dsp_dummy_get_type(void)
{
static GType type;
if (G_UNLIKELY(type == 0)) {
GTypeInfo type_info = {
.class_size = sizeof(GstDspDummyClass),
.class_init = class_init,
.base_init = base_init,
.instance_size = sizeof(GstDspDummy),
.instance_init = instance_init,
};
type = g_type_register_static(GST_TYPE_ELEMENT, "GstDspDummy", &type_info, 0);
}
return type;
}