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
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* JPEG still image video source
*
* Copyright (C) 2018 Paul Elder
*
* Contact: Paul Elder <paul.elder@ideasonboard.com>
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <linux/videodev2.h>
#include "events.h"
#include "timer.h"
#include "tools.h"
#include "v4l2.h"
#include "jpg-source.h"
#include "video-buffers.h"
struct jpg_source {
struct video_source src;
unsigned int imgsize;
void *imgdata;
struct timer *timer;
bool streaming;
};
#define to_jpg_source(s) container_of(s, struct jpg_source, src)
static void jpg_source_destroy(struct video_source *s)
{
struct jpg_source *src = to_jpg_source(s);
if (src->imgdata)
free(src->imgdata);
timer_destroy(src->timer);
free(src);
}
static int jpg_source_set_format(struct video_source *s __attribute__((unused)),
struct v4l2_pix_format *fmt)
{
if (fmt->pixelformat != v4l2_fourcc('M', 'J', 'P', 'G')) {
printf("jpg-source: unsupported fourcc\n");
return -EINVAL;
}
return 0;
}
static int jpg_source_set_frame_rate(struct video_source *s, unsigned int fps)
{
struct jpg_source *src = to_jpg_source(s);
timer_set_fps(src->timer, fps);
return 0;
}
static int jpg_source_free_buffers(struct video_source *s __attribute__((unused)))
{
return 0;
}
static int jpg_source_stream_on(struct video_source *s)
{
struct jpg_source *src = to_jpg_source(s);
int ret;
ret = timer_arm(src->timer);
if (ret)
return ret;
src->streaming = true;
return 0;
}
static int jpg_source_stream_off(struct video_source *s)
{
struct jpg_source *src = to_jpg_source(s);
int ret;
/*
* No error check here, because we want to flag that streaming is over
* even if the timer is still running due to the failure.
*/
ret = timer_disarm(src->timer);
src->streaming = false;
return ret;
}
static void jpg_source_fill_buffer(struct video_source *s,
struct video_buffer *buf)
{
struct jpg_source *src = to_jpg_source(s);
memcpy(buf->mem, src->imgdata, src->imgsize);
buf->bytesused = src->imgsize;
/*
* Wait for the timer to elapse to ensure that our configured frame rate
* is adhered to.
*/
if (src->streaming)
timer_wait(src->timer);
}
static const struct video_source_ops jpg_source_ops = {
.destroy = jpg_source_destroy,
.set_format = jpg_source_set_format,
.set_frame_rate = jpg_source_set_frame_rate,
.alloc_buffers = NULL,
.export_buffers = NULL,
.free_buffers = jpg_source_free_buffers,
.stream_on = jpg_source_stream_on,
.stream_off = jpg_source_stream_off,
.queue_buffer = NULL,
.fill_buffer = jpg_source_fill_buffer,
};
struct video_source *jpg_video_source_create(const char *img_path)
{
struct jpg_source *src;
int fd = -1;
int ret;
printf("using jpg video source\n");
if (img_path == NULL)
return NULL;
src = malloc(sizeof *src);
if (!src)
return NULL;
memset(src, 0, sizeof *src);
src->src.ops = &jpg_source_ops;
fd = open(img_path, O_RDONLY);
if (fd == -1) {
printf("Unable to open MJPEG image '%s'\n", img_path);
goto err_free_src;
}
src->imgsize = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
src->imgdata = malloc(src->imgsize);
if (src->imgdata == NULL) {
printf("Unable to allocate memory for MJPEG image\n");
goto err_close_fd;
}
ret = read(fd, src->imgdata, src->imgsize);
if (ret < 0) {
fprintf(stderr, "error reading data from %s: %d\n", img_path, errno);
goto err_free_imgdata;
}
src->timer = timer_new();
if (!src->timer)
goto err_free_imgdata;
close(fd);
return &src->src;
err_free_imgdata:
free(src->imgdata);
err_close_fd:
close(fd);
err_free_src:
free(src);
return NULL;
}
void jpg_video_source_init(struct video_source *s, struct events *events)
{
struct jpg_source *src = to_jpg_source(s);
src->src.events = events;
}
|