Skip to content

Commit

Permalink
Tidy the libcamera::CameraManager initialisation
Browse files Browse the repository at this point in the history
Make sure we do it only once, even when Picamera2.global_camera_info()
is called before opening a camera.

Signed-off-by: David Plowman <[email protected]>
  • Loading branch information
davidplowman committed Jul 19, 2024
1 parent 39ae89b commit 7a8351e
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions picamera2/picamera2.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,19 @@ def __init__(self):
self.running = False
self.cameras = {}
self._lock = threading.Lock()
self._cms = None

def setup(self):
self.cms = libcamera.CameraManager.singleton()
self.thread = threading.Thread(target=self.listen, daemon=True)
self.running = True
self.thread.start()

@property
def cms(self):
if self._cms is None:
self._cms = libcamera.CameraManager.singleton()
return self._cms

def add(self, index, camera):
with self._lock:
self.cameras[index] = camera
Expand All @@ -78,7 +84,7 @@ def cleanup(self, index):
flag = True
if flag:
self.thread.join()
self.cms = None
self._cms = None

def listen(self):
sel = selectors.DefaultSelector()
Expand All @@ -91,7 +97,7 @@ def listen(self):
callback()

sel.unregister(self.cms.event_fd)
self.cms = None
self._cms = None

def handle_request(self, flushid=None):
"""Handle requests
Expand Down Expand Up @@ -209,7 +215,7 @@ def describe_camera(cam, num):
info["Id"] = cam.id
info["Num"] = num
return info
cameras = [describe_camera(cam, i) for i, cam in enumerate(libcamera.CameraManager.singleton().cameras)]
cameras = [describe_camera(cam, i) for i, cam in enumerate(Picamera2._cm.cms.cameras)]
# Sort alphabetically so they are deterministic, but send USB cams to the back of the class.
return sorted(cameras, key=lambda cam: ("/usb" not in cam['Id'], cam['Id']), reverse=True)

Expand Down

0 comments on commit 7a8351e

Please sign in to comment.