|
| 1 | +"""Reference-counted lazy start/stop wrappers for picamera2. |
| 2 | +
|
| 3 | +CameraSession wraps Picamera2.start()/stop(): the camera only runs while |
| 4 | +at least one consumer (encoder) holds a reference. |
| 5 | +
|
| 6 | +LazyEncoder wraps Picamera2.start_encoder()/stop_encoder(): the encoder |
| 7 | +only runs while at least one consumer (HTTP stream / snapshot / WebRTC |
| 8 | +peer connection) holds a reference. Each LazyEncoder also holds a |
| 9 | +reference on the CameraSession while running, so the camera itself |
| 10 | +turns off when no encoders are active. |
| 11 | +
|
| 12 | +LazyEncoder supports a ``linger_seconds`` parameter: |
| 13 | +
|
| 14 | +* ``< 0`` keeps the encoder running once started; subsequent releases that |
| 15 | + drive the ref-count to zero do not stop it. Useful for the MJPEG path |
| 16 | + when paired with a startup pre-warm so e.g. timelapse snapshots stay on |
| 17 | + the warm path. |
| 18 | +* ``0`` stops the encoder immediately when the last consumer releases. |
| 19 | +* ``> 0`` schedules a delayed stop; a fresh acquire within the window |
| 20 | + cancels the pending stop. Useful to bridge brief reconnects without |
| 21 | + paying the cold-start cost on every reconnect. |
| 22 | +""" |
| 23 | + |
| 24 | +import threading |
| 25 | + |
| 26 | + |
| 27 | +class CameraSession: |
| 28 | + def __init__(self, picam2): |
| 29 | + self._picam2 = picam2 |
| 30 | + self._refs = 0 |
| 31 | + self._lock = threading.Lock() |
| 32 | + |
| 33 | + def acquire(self): |
| 34 | + with self._lock: |
| 35 | + self._refs += 1 |
| 36 | + if self._refs > 1: |
| 37 | + return |
| 38 | + try: |
| 39 | + self._picam2.start() |
| 40 | + except Exception: |
| 41 | + self._refs -= 1 |
| 42 | + raise |
| 43 | + |
| 44 | + def release(self): |
| 45 | + with self._lock: |
| 46 | + if self._refs == 0: |
| 47 | + return |
| 48 | + self._refs -= 1 |
| 49 | + if self._refs == 0: |
| 50 | + self._picam2.stop() |
| 51 | + |
| 52 | + |
| 53 | +class LazyEncoder: |
| 54 | + def __init__( |
| 55 | + self, |
| 56 | + picam2, |
| 57 | + encoder_factory, |
| 58 | + output, |
| 59 | + session=None, |
| 60 | + linger_seconds=0, |
| 61 | + ): |
| 62 | + """ |
| 63 | + :param picam2: the Picamera2 instance to start/stop the encoder on. |
| 64 | + :param encoder_factory: zero-arg callable returning a fresh Encoder. |
| 65 | + :param output: the picamera2 Output to attach to the encoder. |
| 66 | + :param session: optional CameraSession. If provided, the camera is |
| 67 | + started/stopped together with the encoder so the camera only runs |
| 68 | + when at least one encoder is active. |
| 69 | + :param linger_seconds: behavior when the last consumer releases. ``0`` |
| 70 | + stops immediately; ``>0`` schedules a stop that is cancelled if a |
| 71 | + new consumer acquires within the window; ``<0`` keeps the encoder |
| 72 | + running forever after the first start. |
| 73 | + """ |
| 74 | + self._picam2 = picam2 |
| 75 | + self._encoder_factory = encoder_factory |
| 76 | + self._output = output |
| 77 | + self._session = session |
| 78 | + self._linger_seconds = linger_seconds |
| 79 | + self._encoder = None |
| 80 | + self._refs = 0 |
| 81 | + self._lock = threading.Lock() |
| 82 | + self._stop_timer = None |
| 83 | + self._stop_token = 0 |
| 84 | + |
| 85 | + def acquire(self): |
| 86 | + with self._lock: |
| 87 | + self._cancel_linger_locked() |
| 88 | + self._refs += 1 |
| 89 | + if self._encoder is not None: |
| 90 | + return |
| 91 | + session_acquired = False |
| 92 | + try: |
| 93 | + if self._session is not None: |
| 94 | + self._session.acquire() |
| 95 | + session_acquired = True |
| 96 | + self._encoder = self._encoder_factory() |
| 97 | + self._picam2.start_encoder(self._encoder, self._output) |
| 98 | + except Exception: |
| 99 | + self._refs -= 1 |
| 100 | + self._encoder = None |
| 101 | + if session_acquired and self._session is not None: |
| 102 | + self._session.release() |
| 103 | + raise |
| 104 | + |
| 105 | + def release(self): |
| 106 | + with self._lock: |
| 107 | + if self._refs == 0: |
| 108 | + return |
| 109 | + self._refs -= 1 |
| 110 | + if self._refs > 0 or self._linger_seconds < 0: |
| 111 | + return |
| 112 | + if self._linger_seconds == 0: |
| 113 | + self._stop_now_locked() |
| 114 | + else: |
| 115 | + self._schedule_linger_locked() |
| 116 | + |
| 117 | + def _stop_now_locked(self): |
| 118 | + encoder = self._encoder |
| 119 | + self._encoder = None |
| 120 | + try: |
| 121 | + self._picam2.stop_encoder(encoder) |
| 122 | + finally: |
| 123 | + if self._session is not None: |
| 124 | + self._session.release() |
| 125 | + |
| 126 | + def _cancel_linger_locked(self): |
| 127 | + if self._stop_timer is None: |
| 128 | + return |
| 129 | + self._stop_timer.cancel() |
| 130 | + self._stop_timer = None |
| 131 | + self._stop_token += 1 |
| 132 | + |
| 133 | + def _schedule_linger_locked(self): |
| 134 | + self._stop_token += 1 |
| 135 | + token = self._stop_token |
| 136 | + timer = threading.Timer( |
| 137 | + self._linger_seconds, self._linger_callback, args=(token,) |
| 138 | + ) |
| 139 | + timer.daemon = True |
| 140 | + self._stop_timer = timer |
| 141 | + timer.start() |
| 142 | + |
| 143 | + def _linger_callback(self, token): |
| 144 | + with self._lock: |
| 145 | + if self._stop_token != token: |
| 146 | + return |
| 147 | + self._stop_timer = None |
| 148 | + if self._refs == 0 and self._encoder is not None: |
| 149 | + self._stop_now_locked() |
| 150 | + |
| 151 | + def __enter__(self): |
| 152 | + self.acquire() |
| 153 | + return self |
| 154 | + |
| 155 | + def __exit__(self, *exc): |
| 156 | + self.release() |
0 commit comments