-
Notifications
You must be signed in to change notification settings - Fork 1
/
JpegSource.c
143 lines (113 loc) · 3.43 KB
/
JpegSource.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
/* Provide a single jpeg file repeatedly, for benchmarking. */
#include <stdio.h> /* fprintf */
#include <stdlib.h> /* calloc */
#include <string.h> /* memcpy */
#include "CTI.h"
#include "JpegSource.h"
#include "Images.h"
#include "File.h"
static void Config_handler(Instance *pi, void *msg);
enum { INPUT_CONFIG };
static Input JpegSource_inputs[] = {
[ INPUT_CONFIG ] = { .type_label = "Config_msg", .handler = Config_handler },
};
enum { OUTPUT_JPEG, OUTPUT_CONFIG };
static Output JpegSource_outputs[] = {
[ OUTPUT_JPEG ] = { .type_label = "Jpeg_buffer", .destination = 0L },
[ OUTPUT_CONFIG ] = { .type_label = "Config_msg", .destination = 0L },
};
typedef struct {
Instance i;
Jpeg_buffer * jpeg;
String * label;
} JpegSource_private;
static int set_file(Instance *pi, const char *value)
{
JpegSource_private *priv = (JpegSource_private *)pi;
ArrayU8 *fdata = File_load_data(S((char*)value));
if (fdata) {
priv->jpeg = Jpeg_buffer_from(fdata->data, fdata->len, 0L);
ArrayU8_cleanup(&fdata);
}
else {
fprintf(stderr, "failed to load %s\n", value);
return 1;
}
return 0;
}
static int set_label(Instance *pi, const char *value)
{
JpegSource_private *priv = (JpegSource_private *)pi;
if (priv->label) {
String_free(&priv->label);
}
priv->label = String_new(value);
return 0;
}
static int do_run(Instance *pi, const char *value)
{
/* Clones and posts the jpeg buffer N times. N=atoi(value). */
JpegSource_private *priv = (JpegSource_private *)pi;
int count = atoi(value);
int i;
if (!priv->jpeg) {
fprintf(stderr, "JpegSource has no jpeg data!\n");
return 1;
}
if (!pi->outputs[OUTPUT_JPEG].destination) {
fprintf(stderr, "JpegSource has no destination!\n");
return 1;
}
Image_common c = { .label = priv->label };
for (i=0; i < count; i++) {
Jpeg_buffer *tmp = Jpeg_buffer_from(priv->jpeg->data, priv->jpeg->encoded_length, &c);
dpf("%d/%d (%d)\n", i, count,
pi->outputs[OUTPUT_JPEG].destination->parent->pending_messages);
while (pi->outputs[OUTPUT_JPEG].destination->parent->pending_messages > 5) {
/* Throttle output. 25ms sleep. */
nanosleep(&(struct timespec){.tv_sec = 0, .tv_nsec = 25 * 1000 * 1000}, NULL);
}
PostData(tmp, pi->outputs[OUTPUT_JPEG].destination);
}
if (pi->outputs[OUTPUT_CONFIG].destination) {
PostData(Config_buffer_new("quit", "0"), pi->outputs[OUTPUT_CONFIG].destination);
}
return 0;
}
static Config config_table[] = {
{ "file", set_file, 0L, 0L },
{ "run", do_run, 0L, 0L },
{ "label", set_label, 0L, 0L },
};
static void Config_handler(Instance *pi, void *data)
{
Generic_config_handler(pi, data, config_table, table_size(config_table));
}
static void JpegSource_tick(Instance *pi)
{
Handler_message *hm;
hm = GetData(pi, 1);
if (hm) {
hm->handler(pi, hm->data);
ReleaseMessage(&hm,pi);
}
pi->counter++;
}
static void JpegSource_instance_init(Instance *pi)
{
// JpegSource_private *priv = (JpegSource_private *)pi;
}
static Template JpegSource_template = {
.label = "JpegSource",
.priv_size = sizeof(JpegSource_private),
.inputs = JpegSource_inputs,
.num_inputs = table_size(JpegSource_inputs),
.outputs = JpegSource_outputs,
.num_outputs = table_size(JpegSource_outputs),
.tick = JpegSource_tick,
.instance_init = JpegSource_instance_init,
};
void JpegSource_init(void)
{
Template_register(&JpegSource_template);
}