forked from AravisProject/aravis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharvstream.c
386 lines (321 loc) · 10.2 KB
/
arvstream.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
/* Aravis - Digital camera library
*
* Copyright © 2009-2010 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., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* Author: Emmanuel Pacaud <[email protected]>
*/
/**
* SECTION: arvstream
* @short_description: Abstract base class for video stream reception
*
* #ArvStream provides an abstract base class for the implementation of video
* stream reception threads. The interface between the reception thread and the
* main thread is done using asynchronous queues, containing #ArvBuffer
* objects.
*/
#include <arvstream.h>
#include <arvbuffer.h>
#include <arvdebug.h>
enum {
ARV_STREAM_SIGNAL_NEW_BUFFER,
ARV_STREAM_SIGNAL_LAST
} ArvStreamSignals;
static guint arv_stream_signals[ARV_STREAM_SIGNAL_LAST] = {0};
enum {
ARV_STREAM_PROPERTY_0,
ARV_STREAM_PROPERTY_EMIT_SIGNALS,
ARV_STREAM_PROPERTY_LAST
} ArvStreamProperties;
static GObjectClass *parent_class = NULL;
struct _ArvStreamPrivate {
GAsyncQueue *input_queue;
GAsyncQueue *output_queue;
gboolean emit_signals;
};
/**
* arv_stream_push_buffer:
* @stream: a #ArvStream
* @buffer: (transfer full): buffer to push
*
* Pushes a #ArvBuffer to the @stream thread. The @stream takes ownership of @buffer,
* and will free all the buffers still in its queues when destroyed.
*/
void
arv_stream_push_buffer (ArvStream *stream, ArvBuffer *buffer)
{
g_return_if_fail (ARV_IS_STREAM (stream));
g_return_if_fail (ARV_IS_BUFFER (buffer));
g_async_queue_push (stream->priv->input_queue, buffer);
}
/**
* arv_stream_pop_buffer:
* @stream: a #ArvStream
* Returns: (transfer full): a #ArvBuffer
*
* Pops a buffer from the output queue of @stream. The retrieved buffer
* may contain an invalid image. Caller should check the buffer status before using it.
* This function blocks until a buffer is available.
*
* since: 0.1.12
*/
ArvBuffer *
arv_stream_pop_buffer (ArvStream *stream)
{
g_return_val_if_fail (ARV_IS_STREAM (stream), NULL);
return g_async_queue_pop (stream->priv->output_queue);
}
/**
* arv_stream_try_pop_buffer:
* @stream: a #ArvStream
* Returns: (transfer full): a #ArvBuffer, NULL if no buffer is available.
*
* Pops a buffer from the output queue of @stream. The retrieved buffer
* may contain an invalid image. Caller should check the buffer status before using it.
* This is the non blocking version of pop_buffer.
*
* since: 0.1.12
*/
ArvBuffer *
arv_stream_try_pop_buffer (ArvStream *stream)
{
g_return_val_if_fail (ARV_IS_STREAM (stream), NULL);
return g_async_queue_try_pop (stream->priv->output_queue);
}
/**
* arv_stream_timed_pop_buffer:
* @stream: a #ArvStream
* @timeout: timeout, in µs
* Returns: (transfer full): a #ArvBuffer, NULL if no buffer is available until the timeout occurs.
*
* Pops a buffer from the output queue of @stream, waiting no more than @timeout. The retrieved buffer
* may contain an invalid image. Caller should check the buffer status before using it.
*/
ArvBuffer *
arv_stream_timed_pop_buffer (ArvStream *stream, guint64 timeout)
{
GTimeVal end_time;
g_return_val_if_fail (ARV_IS_STREAM (stream), NULL);
g_get_current_time (&end_time);
g_time_val_add (&end_time, timeout);
return g_async_queue_timed_pop (stream->priv->output_queue, &end_time);
}
/**
* arv_stream_pop_input_buffer: (skip)
* @stream: (transfer full): a #ArvStream
*
* Pos a buffer from the input queue of @stream.
*/
ArvBuffer *
arv_stream_pop_input_buffer (ArvStream *stream)
{
g_return_val_if_fail (ARV_IS_STREAM (stream), NULL);
return g_async_queue_try_pop (stream->priv->input_queue);
}
void
arv_stream_push_output_buffer (ArvStream *stream, ArvBuffer *buffer)
{
g_return_if_fail (ARV_IS_STREAM (stream));
g_return_if_fail (ARV_IS_BUFFER (buffer));
g_async_queue_push (stream->priv->output_queue, buffer);
if (stream->priv->emit_signals)
g_signal_emit (stream, arv_stream_signals[ARV_STREAM_SIGNAL_NEW_BUFFER], 0);
}
/**
* arv_stream_get_n_buffers:
* @stream: a #ArvStream
* @n_input_buffers: (out) (allow-none): input queue length
* @n_output_buffers: (out) (allow-none): output queue length
*
* An accessor to the stream buffer queue lengths.
*/
void
arv_stream_get_n_buffers (ArvStream *stream, gint *n_input_buffers, gint *n_output_buffers)
{
if (!ARV_IS_STREAM (stream)) {
if (n_input_buffers != NULL)
*n_input_buffers = 0;
if (n_output_buffers != NULL)
*n_output_buffers = 0;
return;
}
if (n_input_buffers != NULL)
*n_input_buffers = g_async_queue_length (stream->priv->input_queue);
if (n_output_buffers != NULL)
*n_output_buffers = g_async_queue_length (stream->priv->output_queue);
}
/**
* arv_stream_get_statistics:
* @stream: a #ArvStream
* @n_completed_buffers: (out) (allow-none): number of complete received buffers
* @n_failures: (out) (allow-none): number of reception failures
* @n_underruns: (out) (allow-none): number of input buffer underruns
*
* An accessor to the stream statistics.
*/
void
arv_stream_get_statistics (ArvStream *stream,
guint64 *n_completed_buffers,
guint64 *n_failures,
guint64 *n_underruns)
{
ArvStreamClass *stream_class;
guint64 dummy;
if (n_completed_buffers == NULL)
n_completed_buffers = &dummy;
if (n_failures == NULL)
n_failures = &dummy;
if (n_underruns == NULL)
n_underruns = &dummy;
*n_completed_buffers = 0;
*n_failures = 0;
*n_underruns = 0;
g_return_if_fail (ARV_IS_STREAM (stream));
stream_class = ARV_STREAM_GET_CLASS (stream);
if (stream_class->get_statistics != NULL)
stream_class->get_statistics (stream, n_completed_buffers, n_failures, n_underruns);
}
/**
* arv_stream_set_emit_signals:
* @stream: a #ArvStream
* @emit_signals: the new state
*
* Make stream emit signals. This option is
* by default disabled because signal emission is expensive and unneeded when
* the application prefers to operate in pull mode.
*
* Since: 0.1.3
*/
void
arv_stream_set_emit_signals (ArvStream *stream, gboolean emit_signals)
{
g_return_if_fail (ARV_IS_STREAM (stream));
stream->priv->emit_signals = emit_signals;
}
/**
* arv_stream_get_emit_signals:
* @stream: a #ArvStream
*
* Check if stream will emit its signals.
*
* Returns: %TRUE if @appsink is emiting its signals.
*
* Since: 0.1.3
*/
gboolean
arv_stream_get_emit_signals (ArvStream *stream)
{
g_return_val_if_fail (ARV_IS_STREAM (stream), FALSE);
return stream->priv->emit_signals;
}
static void
arv_stream_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
ArvStream *stream = ARV_STREAM (object);
switch (prop_id) {
case ARV_STREAM_PROPERTY_EMIT_SIGNALS:
arv_stream_set_emit_signals (stream, g_value_get_boolean (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
arv_stream_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec)
{
ArvStream *stream = ARV_STREAM (object);
switch (prop_id) {
case ARV_STREAM_PROPERTY_EMIT_SIGNALS:
g_value_set_boolean (value, arv_stream_get_emit_signals (stream));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
arv_stream_init (ArvStream *stream)
{
stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream, ARV_TYPE_STREAM, ArvStreamPrivate);
stream->priv->input_queue = g_async_queue_new ();
stream->priv->output_queue = g_async_queue_new ();
stream->priv->emit_signals = FALSE;
}
static void
arv_stream_finalize (GObject *object)
{
ArvStream *stream = ARV_STREAM (object);
ArvBuffer *buffer;
arv_debug_stream ("[Stream::finalize] Flush %d buffer[s] in input queue",
g_async_queue_length (stream->priv->input_queue));
arv_debug_stream ("[Stream::finalize] Flush %d buffer[s] in output queue",
g_async_queue_length (stream->priv->output_queue));
do {
buffer = g_async_queue_try_pop (stream->priv->output_queue);
if (buffer != NULL)
g_object_unref (buffer);
} while (buffer != NULL);
do {
buffer = g_async_queue_try_pop (stream->priv->input_queue);
if (buffer != NULL)
g_object_unref (buffer);
} while (buffer != NULL);
g_async_queue_unref (stream->priv->input_queue);
g_async_queue_unref (stream->priv->output_queue);
parent_class->finalize (object);
}
static void
arv_stream_class_init (ArvStreamClass *node_class)
{
GObjectClass *object_class = G_OBJECT_CLASS (node_class);
g_type_class_add_private (node_class, sizeof (ArvStreamPrivate));
parent_class = g_type_class_peek_parent (node_class);
object_class->finalize = arv_stream_finalize;
object_class->set_property = arv_stream_set_property;
object_class->get_property = arv_stream_get_property;
/**
* ArvStream::new-buffer:
* @stream: the stream that emited the signal
*
* Signal that a new buffer is available.
*
* This signal is emited from the stream receive thread and only when the
* "emit-signals" property is %TRUE.
*
* The new buffer can be retrieved with arv_stream_pop_buffer().
*
* Note that this signal is only emited when the "emit-signals" property is
* set to %TRUE, which it is not by default for performance reasons.
*/
arv_stream_signals[ARV_STREAM_SIGNAL_NEW_BUFFER] =
g_signal_new ("new-buffer",
G_TYPE_FROM_CLASS (node_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (ArvStreamClass, new_buffer),
NULL, NULL,
g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
g_object_class_install_property (
object_class, ARV_STREAM_PROPERTY_EMIT_SIGNALS,
g_param_spec_boolean ("emit-signals", "Emit signals",
"Emit signals", FALSE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
);
}
G_DEFINE_ABSTRACT_TYPE (ArvStream, arv_stream, G_TYPE_OBJECT)