forked from AravisProject/aravis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharvfakestream.c
250 lines (194 loc) · 7.4 KB
/
arvfakestream.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
/* Aravis - Digital camera library
*
* Copyright © 2009-2022 Emmanuel Pacaud
*
* 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; either
* version 2 of the License, or (at your option) any later version.
*
* 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.
*
* Author: Emmanuel Pacaud <[email protected]>
*/
/**
* SECTION: arvfakestream
* @short_description: Fake stream
*/
#include <arvfakestreamprivate.h>
#include <arvfakecamera.h>
#include <arvfakedevice.h>
#include <arvstreamprivate.h>
#include <arvbufferprivate.h>
#include <arvdebug.h>
#include <arvmisc.h>
typedef struct {
ArvStream *stream;
ArvFakeCamera *fake_camera;
ArvStreamCallback callback;
void *callback_data;
gboolean cancel;
/* Statistics */
guint64 n_completed_buffers;
guint64 n_failures;
guint64 n_underruns;
guint64 n_transferred_bytes;
guint64 n_ignored_bytes;
} ArvFakeStreamThreadData;
typedef struct {
GThread *thread;
ArvFakeStreamThreadData *thread_data;
} ArvFakeStreamPrivate;
struct _ArvFakeStream {
ArvStream stream;
};
struct _ArvFakeStreamClass {
ArvStreamClass parent_class;
};
G_DEFINE_TYPE_WITH_CODE (ArvFakeStream, arv_fake_stream, ARV_TYPE_STREAM, G_ADD_PRIVATE (ArvFakeStream))
/* Acquisition thread */
static void *
arv_fake_stream_thread (void *data)
{
ArvFakeStreamThreadData *thread_data = data;
ArvBuffer *buffer;
arv_debug_stream_thread ("[FakeStream::thread] Start");
if (thread_data->callback != NULL)
thread_data->callback (thread_data->callback_data, ARV_STREAM_CALLBACK_TYPE_INIT, NULL);
while (!g_atomic_int_get (&thread_data->cancel)) {
arv_fake_camera_wait_for_next_frame (thread_data->fake_camera);
buffer = arv_stream_pop_input_buffer (thread_data->stream);
if (buffer != NULL) {
buffer->priv->received_size = 0;
if (thread_data->callback != NULL)
thread_data->callback (thread_data->callback_data, ARV_STREAM_CALLBACK_TYPE_START_BUFFER,
NULL);
arv_fake_camera_fill_buffer (thread_data->fake_camera, buffer, NULL);
thread_data->n_transferred_bytes += buffer->priv->allocated_size;
if (buffer->priv->status == ARV_BUFFER_STATUS_SUCCESS)
thread_data->n_completed_buffers++;
else
thread_data->n_failures++;
arv_stream_push_output_buffer (thread_data->stream, buffer);
if (thread_data->callback != NULL)
thread_data->callback (thread_data->callback_data, ARV_STREAM_CALLBACK_TYPE_BUFFER_DONE,
buffer);
} else
thread_data->n_underruns++;
}
if (thread_data->callback != NULL)
thread_data->callback (thread_data->callback_data, ARV_STREAM_CALLBACK_TYPE_EXIT, NULL);
arv_debug_stream_thread ("[FakeStream::thread] Stop");
return NULL;
}
/* ArvFakeStream implemenation */
static void
arv_fake_stream_start_thread (ArvStream *stream)
{
ArvFakeStream *fake_stream = ARV_FAKE_STREAM (stream);
ArvFakeStreamPrivate *priv = arv_fake_stream_get_instance_private (fake_stream);
ArvFakeStreamThreadData *thread_data;
g_return_if_fail (priv->thread == NULL);
g_return_if_fail (priv->thread_data != NULL);
thread_data = priv->thread_data;
thread_data->cancel = FALSE;
priv->thread = g_thread_new ("arv_fake_stream", arv_fake_stream_thread, priv->thread_data);
}
static void
arv_fake_stream_stop_thread (ArvStream *stream)
{
ArvFakeStream *fake_stream = ARV_FAKE_STREAM (stream);
ArvFakeStreamPrivate *priv = arv_fake_stream_get_instance_private (fake_stream);
ArvFakeStreamThreadData *thread_data;
g_return_if_fail (priv->thread != NULL);
g_return_if_fail (priv->thread_data != NULL);
thread_data = priv->thread_data;
g_atomic_int_set (&thread_data->cancel, TRUE);
g_thread_join (priv->thread);
priv->thread = NULL;
}
/**
* arv_fake_stream_new: (skip)
* @camera: a #ArvFakeDevice
* @callback: (scope call): image processing callback
* @callback_data: (closure): user data for @callback
* @error: a #GError placeholder, %NULL to ignore
*
* Return Value: (transfer full): a new #ArvStream.
*/
ArvStream *
arv_fake_stream_new (ArvFakeDevice *device, ArvStreamCallback callback, void *callback_data, GDestroyNotify destroy, GError **error)
{
return g_initable_new (ARV_TYPE_FAKE_STREAM, NULL, error,
"device", device,
"callback", callback,
"callback-data", callback_data,
"destroy-notify", destroy,
NULL);
}
static void
arv_fake_stream_constructed (GObject *object)
{
ArvStream *stream = ARV_STREAM (object);
ArvFakeStream *fake_stream = ARV_FAKE_STREAM (object);
ArvFakeStreamPrivate *priv = arv_fake_stream_get_instance_private (fake_stream);
ArvFakeStreamThreadData *thread_data;
ArvFakeDevice *fake_device = NULL;
thread_data = g_new0 (ArvFakeStreamThreadData, 1);
thread_data->stream = stream;
g_object_get (object,
"device", &fake_device,
"callback", &thread_data->callback,
"callback-data", &thread_data->callback_data,
NULL);
thread_data->fake_camera = arv_fake_device_get_fake_camera (fake_device);
thread_data->cancel = FALSE;
arv_stream_declare_info (ARV_STREAM (fake_stream), "n_completed_buffers",
G_TYPE_UINT64, &thread_data->n_completed_buffers);
arv_stream_declare_info (ARV_STREAM (fake_stream), "n_failures",
G_TYPE_UINT64, &thread_data->n_failures);
arv_stream_declare_info (ARV_STREAM (fake_stream), "n_underruns",
G_TYPE_UINT64, &thread_data->n_underruns);
arv_stream_declare_info (ARV_STREAM (fake_stream), "n_transferred_bytes",
G_TYPE_UINT64, &thread_data->n_transferred_bytes);
arv_stream_declare_info (ARV_STREAM (fake_stream), "n_ignored_bytes",
G_TYPE_UINT64, &thread_data->n_ignored_bytes);
priv->thread_data = thread_data;
arv_fake_stream_start_thread (ARV_STREAM (fake_stream));
G_OBJECT_CLASS (arv_fake_stream_parent_class)->constructed (object);
g_clear_object (&fake_device);
}
/* ArvStream implementation */
static void
arv_fake_stream_init (ArvFakeStream *fake_stream)
{
}
static void
arv_fake_stream_finalize (GObject *object)
{
ArvFakeStream *fake_stream = ARV_FAKE_STREAM (object);
ArvFakeStreamPrivate *priv = arv_fake_stream_get_instance_private (fake_stream);
arv_fake_stream_stop_thread (ARV_STREAM (fake_stream));
if (priv->thread_data != NULL) {
g_clear_pointer (&priv->thread_data, g_free);
}
G_OBJECT_CLASS (arv_fake_stream_parent_class)->finalize (object);
}
static void
arv_fake_stream_class_init (ArvFakeStreamClass *fake_stream_class)
{
GObjectClass *object_class = G_OBJECT_CLASS (fake_stream_class);
ArvStreamClass *stream_class = ARV_STREAM_CLASS (fake_stream_class);
object_class->constructed = arv_fake_stream_constructed;
object_class->finalize = arv_fake_stream_finalize;
stream_class->start_thread = arv_fake_stream_start_thread;
stream_class->stop_thread = arv_fake_stream_stop_thread;
}