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
|
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2022, Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
from collections import defaultdict
import errno
import gc
import libcamera as libcam
import os
import selectors
import time
import unittest
import weakref
class BaseTestCase(unittest.TestCase):
def assertZero(self, a, msg=None):
self.assertEqual(a, 0, msg)
class SimpleTestMethods(BaseTestCase):
def test_get_ref(self):
cm = libcam.CameraManager.singleton()
wr_cm = weakref.ref(cm)
cam = cm.get('platform/vimc.0 Sensor B')
self.assertIsNotNone(cam)
wr_cam = weakref.ref(cam)
cm = None
gc.collect()
self.assertIsNotNone(wr_cm())
cam = None
gc.collect()
self.assertIsNone(wr_cm())
self.assertIsNone(wr_cam())
def test_acquire_release(self):
cm = libcam.CameraManager.singleton()
cam = cm.get('platform/vimc.0 Sensor B')
self.assertIsNotNone(cam)
ret = cam.acquire()
self.assertZero(ret)
ret = cam.release()
self.assertZero(ret)
def test_double_acquire(self):
cm = libcam.CameraManager.singleton()
cam = cm.get('platform/vimc.0 Sensor B')
self.assertIsNotNone(cam)
ret = cam.acquire()
self.assertZero(ret)
libcam.log_set_level('Camera', 'FATAL')
ret = cam.acquire()
self.assertEqual(ret, -errno.EBUSY)
libcam.log_set_level('Camera', 'ERROR')
ret = cam.release()
self.assertZero(ret)
ret = cam.release()
# I expected EBUSY, but looks like double release works fine
self.assertZero(ret)
class CameraTesterBase(BaseTestCase):
def setUp(self):
self.cm = libcam.CameraManager.singleton()
self.cam = next((cam for cam in self.cm.cameras if 'platform/vimc' in cam.id), None)
if self.cam is None:
self.cm = None
self.skipTest('No vimc found')
ret = self.cam.acquire()
if ret != 0:
self.cam = None
self.cm = None
raise Exception('Failed to acquire camera')
def tearDown(self):
# If a test fails, the camera may be in running state. So always stop.
self.cam.stop()
ret = self.cam.release()
if ret != 0:
raise Exception('Failed to release camera')
self.cam = None
self.cm = None
class AllocatorTestMethods(CameraTesterBase):
def test_allocator(self):
cam = self.cam
camconfig = cam.generate_configuration([libcam.StreamRole.StillCapture])
self.assertTrue(camconfig.size == 1)
wr_camconfig = weakref.ref(camconfig)
streamconfig = camconfig.at(0)
wr_streamconfig = weakref.ref(streamconfig)
ret = cam.configure(camconfig)
self.assertZero(ret)
stream = streamconfig.stream
wr_stream = weakref.ref(stream)
# stream should keep streamconfig and camconfig alive
streamconfig = None
camconfig = None
gc.collect()
self.assertIsNotNone(wr_camconfig())
self.assertIsNotNone(wr_streamconfig())
allocator = libcam.FrameBufferAllocator(cam)
ret = allocator.allocate(stream)
self.assertTrue(ret > 0)
wr_allocator = weakref.ref(allocator)
buffers = allocator.buffers(stream)
buffers = None
buffer = allocator.buffers(stream)[0]
self.assertIsNotNone(buffer)
wr_buffer = weakref.ref(buffer)
allocator = None
gc.collect()
self.assertIsNotNone(wr_buffer())
self.assertIsNotNone(wr_allocator())
self.assertIsNotNone(wr_stream())
buffer = None
gc.collect()
self.assertIsNone(wr_buffer())
self.assertIsNone(wr_allocator())
self.assertIsNotNone(wr_stream())
stream = None
gc.collect()
self.assertIsNone(wr_stream())
self.assertIsNone(wr_camconfig())
self.assertIsNone(wr_streamconfig())
class SimpleCaptureMethods(CameraTesterBase):
def test_sleep(self):
cm = self.cm
cam = self.cam
camconfig = cam.generate_configuration([libcam.StreamRole.StillCapture])
self.assertTrue(camconfig.size == 1)
streamconfig = camconfig.at(0)
fmts = streamconfig.formats
ret = cam.configure(camconfig)
self.assertZero(ret)
stream = streamconfig.stream
allocator = libcam.FrameBufferAllocator(cam)
ret = allocator.allocate(stream)
self.assertTrue(ret > 0)
num_bufs = len(allocator.buffers(stream))
reqs = []
for i in range(num_bufs):
req = cam.create_request(i)
self.assertIsNotNone(req)
buffer = allocator.buffers(stream)[i]
ret = req.add_buffer(stream, buffer)
self.assertZero(ret)
reqs.append(req)
buffer = None
ret = cam.start()
self.assertZero(ret)
for req in reqs:
ret = cam.queue_request(req)
self.assertZero(ret)
reqs = None
gc.collect()
time.sleep(0.5)
reqs = cm.get_ready_requests()
self.assertTrue(len(reqs) == num_bufs)
for i, req in enumerate(reqs):
self.assertTrue(i == req.cookie)
reqs = None
gc.collect()
ret = cam.stop()
self.assertZero(ret)
def test_select(self):
cm = self.cm
cam = self.cam
camconfig = cam.generate_configuration([libcam.StreamRole.StillCapture])
self.assertTrue(camconfig.size == 1)
streamconfig = camconfig.at(0)
fmts = streamconfig.formats
ret = cam.configure(camconfig)
self.assertZero(ret)
stream = streamconfig.stream
allocator = libcam.FrameBufferAllocator(cam)
ret = allocator.allocate(stream)
self.assertTrue(ret > 0)
num_bufs = len(allocator.buffers(stream))
reqs = []
for i in range(num_bufs):
req = cam.create_request(i)
self.assertIsNotNone(req)
buffer = allocator.buffers(stream)[i]
ret = req.add_buffer(stream, buffer)
self.assertZero(ret)
reqs.append(req)
buffer = None
ret = cam.start()
self.assertZero(ret)
for req in reqs:
ret = cam.queue_request(req)
self.assertZero(ret)
reqs = None
gc.collect()
sel = selectors.DefaultSelector()
sel.register(cm.efd, selectors.EVENT_READ)
reqs = []
running = True
while running:
events = sel.select()
for key, _ in events:
os.read(key.fd, 8)
ready_reqs = cm.get_ready_requests()
self.assertTrue(len(ready_reqs) > 0)
reqs += ready_reqs
if len(reqs) == num_bufs:
running = False
self.assertTrue(len(reqs) == num_bufs)
for i, req in enumerate(reqs):
self.assertTrue(i == req.cookie)
reqs = None
gc.collect()
ret = cam.stop()
self.assertZero(ret)
# Recursively expand slist's objects into olist, using seen to track already
# processed objects.
def _getr(slist, olist, seen):
for e in slist:
if id(e) in seen:
continue
seen.add(id(e))
olist.append(e)
tl = gc.get_referents(e)
if tl:
_getr(tl, olist, seen)
def get_all_objects(ignored=[]):
gcl = gc.get_objects()
olist = []
seen = set()
seen.add(id(gcl))
seen.add(id(olist))
seen.add(id(seen))
seen.update(set([id(o) for o in ignored]))
_getr(gcl, olist, seen)
return olist
def create_type_count_map(olist):
map = defaultdict(int)
for o in olist:
map[type(o)] += 1
return map
def diff_type_count_maps(before, after):
return [(k, after[k] - before[k]) for k in after if after[k] != before[k]]
if __name__ == '__main__':
# \todo This is an attempt to see the Python objects that are not collected,
# but this doesn't work very well, as things always leak a bit.
test_leaks = False
if test_leaks:
gc.unfreeze()
gc.collect()
obs_before = get_all_objects()
unittest.main(exit=False)
if test_leaks:
gc.unfreeze()
gc.collect()
obs_after = get_all_objects([obs_before])
before = create_type_count_map(obs_before)
after = create_type_count_map(obs_after)
leaks = diff_type_count_maps(before, after)
if len(leaks) > 0:
print(leaks)
|