Repository navigation
Expand file tree
/
Copy pathcapture_filter.c
More file actions
230 lines (203 loc) · 9.47 KB
/
Copy pathcapture_filter.c
File metadata and controls
230 lines (203 loc) · 9.47 KB
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
/**
* @file capture_filter.c
* @author Martin Pulec <pulec@cesnet.cz>
*/
/*
* Copyright (c) 2014-2026 CESNET, zájmové sdružení právnických osob
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, is permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of CESNET nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "capture_filter.h"
#include <assert.h> // for assert
#include <stdio.h> // for printf, fprintf, stderr
#include <stdlib.h> // for free, NULL, atoi, calloc, malloc
#include <string.h> // for strcasecmp, strchr, strcmp, strdup...
#include "lib_common.h" // for get_libraries_for_class, library_class
#include "messaging.h" // for msg_universal, new_response, RESPONSE_I...
#include "module.h" // for module, module_done, module_init_default
#include "utils/color_out.h" // for color_printf, TERM_BOLD, TERM_RESET
#include "utils/list.h" // for simple_linked_list_pop, simple_linked_l...
struct capture_filter {
struct module mod;
struct simple_linked_list *filters;
};
struct capture_filter_instance {
const struct capture_filter_info *functions;
void *state;
};
static int create_filter(struct capture_filter *s, char *cfg)
{
bool found = false;
const char *options = "";
char *filter_name = cfg;
if(strchr(filter_name, ':')) {
options = strchr(filter_name, ':') + 1;
*strchr(filter_name, ':') = '\0';
}
struct class_modules capture_filters = get_libraries_for_class(
LIBRARY_CLASS_CAPTURE_FILTER, CAPTURE_FILTER_ABI_VERSION);
for (unsigned i = 0; i< capture_filters.count; i++) {
const struct capture_filter_info *capture_filter_info =
capture_filters.item[i].info;
if (strcasecmp(capture_filters.item[i].name, filter_name) ==
0) {
struct capture_filter_instance *instance = (struct capture_filter_instance *)
malloc(sizeof(struct capture_filter_instance));
instance->functions = capture_filter_info;
int ret = capture_filter_info->init(&s->mod, options, &instance->state);
if(ret < 0) {
fprintf(stderr, "Unable to initialize capture filter: %s\n",
filter_name);
}
if(ret != 0) {
free(instance);
return ret;
}
simple_linked_list_append(s->filters, instance);
found = true;
break;
}
}
if (!found) {
fprintf(stderr, "Unable to find capture filter: %s\n",
filter_name);
return -1;
}
return 0;
}
int capture_filter_init(struct module *parent, const char *cfg, struct capture_filter **state)
{
if (cfg && (strcasecmp(cfg, "help") == 0 || strcasecmp(cfg, "fullhelp") == 0)) {
printf("Usage:\n");
color_printf(TERM_BOLD "\t--capture-filter <filter1>[:opts][,<filter2>[:opts][,<filter3>[:<opts>]]]" TERM_RESET " -t <capture>\n\n");
printf("Available capture filters:\n");
list_modules(LIBRARY_CLASS_CAPTURE_FILTER, CAPTURE_FILTER_ABI_VERSION, strcasecmp(cfg, "fullhelp") == 0);
if (strcasecmp(cfg, "fullhelp") != 0) {
printf("\n(use \"fullhelp\" to show hidden filters)\n");
}
printf("\nSee "
"<https://github.com/CESNET/UltraGrid/wiki/"
"Video-Filters> for overview.\n");
return 1;
}
struct capture_filter *s = (struct capture_filter *) calloc(1, sizeof(struct capture_filter));
char *item, *save_ptr;
assert(s);
char *filter_list_str = NULL,
*tmp = NULL;
s->filters = simple_linked_list_init();
module_init_default(&s->mod);
s->mod.cls = MODULE_CLASS_FILTER;
module_register(&s->mod, parent);
if(cfg) {
filter_list_str = tmp = strdup(cfg);
while((item = strtok_r(filter_list_str, ",", &save_ptr))) {
char filter_name[128];
strncpy(filter_name, item, sizeof filter_name - 1);
int ret = create_filter(s, filter_name);
if (ret != 0) {
free(tmp);
capture_filter_destroy(s);
return ret;
}
filter_list_str = NULL;
}
}
free(tmp);
*state = s;
return 0;
}
void capture_filter_destroy(struct capture_filter *state)
{
struct capture_filter *s = state;
while(simple_linked_list_size(s->filters) > 0) {
struct capture_filter_instance *inst = (struct capture_filter_instance *) simple_linked_list_pop(s->filters);
inst->functions->done(inst->state);
free(inst);
}
simple_linked_list_destroy(s->filters);
module_done(&s->mod);
free(state);
}
static struct response *process_message(struct capture_filter *s, struct msg_universal *msg)
{
if (strncmp("delete ", msg->text, strlen("delete ")) == 0) {
int index = atoi(msg->text + strlen("delete "));
struct capture_filter_instance *inst = (struct capture_filter_instance *)
simple_linked_list_remove_index(s->filters, index);
if (!inst) {
fprintf(stderr, "Unable to remove capture filter index %d.\n",
index);
return new_response(RESPONSE_INT_SERV_ERR, NULL);
} else {
printf("Capture filter #%d removed successfully.\n", index);
inst->functions->done(inst->state);
free(inst);
}
} else if (strcmp("flush", msg->text) == 0) {
while(simple_linked_list_size(s->filters) > 0) {
struct capture_filter_instance *inst = (struct capture_filter_instance *) simple_linked_list_pop(s->filters);
inst->functions->done(inst->state);
free(inst);
}
} else if (strcmp("help", msg->text) == 0) {
printf("Capture filter control:\n"
"\tflush - remove all filters\n"
"\tdelete <x> - delete x-th filter\n"
"\t<filter> - append a filter named <filter>\n");
} else {
char *fmt = strdup(msg->text);
if (create_filter(s, fmt) != 0) {
fprintf(stderr, "Cannot create capture filter: %s.\n",
msg->text);
free(fmt);
return new_response(RESPONSE_INT_SERV_ERR, NULL);
} else {
printf("Capture filter \"%s\" created successfully.\n",
msg->text);
}
free(fmt);
}
return new_response(RESPONSE_OK, NULL);
}
struct video_frame *capture_filter(struct capture_filter *state, struct video_frame *frame) {
struct capture_filter *s = state;
struct message *msg;
while ((msg = check_message(&s->mod))) {
struct response *r = process_message(s, (struct msg_universal *) msg);
free_message(msg, r);
}
for (list_it it = simple_linked_list_it_init(s->filters);
it != LIST_IT_END;) {
struct capture_filter_instance *inst = (struct capture_filter_instance *) simple_linked_list_it_next(&it);
frame = inst->functions->filter(inst->state, frame);
}
return frame;
}