From f0122b159d84df94cf9a909a72b75f1faa7d43cd Mon Sep 17 00:00:00 2001 From: mryel00 Date: Sun, 29 Jan 2023 21:34:11 +0100 Subject: [PATCH 01/55] feat: Add basic USB-Camera support --- spyglass/camera.py | 6 ++- spyglass/cli.py | 50 ++++++++++++++------- spyglass/server.py | 102 +++++++++++++++++++++++------------------- spyglass/usbCamera.py | 28 ++++++++++++ spyglass/usbServer.py | 22 +++++++++ 5 files changed, 144 insertions(+), 64 deletions(-) create mode 100644 spyglass/usbCamera.py create mode 100644 spyglass/usbServer.py diff --git a/spyglass/camera.py b/spyglass/camera.py index 5b5733b..2040480 100644 --- a/spyglass/camera.py +++ b/spyglass/camera.py @@ -8,8 +8,10 @@ def init_camera( fps: int, autofocus: str, lens_position: float, - autofocus_speed: str): - picam2 = Picamera2() + autofocus_speed: str, + camera_num: int): + + picam2 = Picamera2(camera_num) controls = {'FrameRate': fps} diff --git a/spyglass/cli.py b/spyglass/cli.py index 5c9b9cf..3b7c49b 100644 --- a/spyglass/cli.py +++ b/spyglass/cli.py @@ -7,8 +7,10 @@ import libcamera import re from spyglass.camera import init_camera +from spyglass.usbCamera import init_usb_camera from spyglass.server import StreamingOutput from spyglass.server import run_server +from spyglass.usbServer import run_usb_server from picamera2.encoders import MJPEGEncoder from picamera2.outputs import FileOutput @@ -30,21 +32,37 @@ def main(args=None): width, height = split_resolution(parsed_args.resolution) stream_url = parsed_args.stream_url snapshot_url = parsed_args.snapshot_url - picam2 = init_camera( - width, - height, - parsed_args.fps, - parse_autofocus(parsed_args.autofocus), - parsed_args.lensposition, - parse_autofocus_speed(parsed_args.autofocusspeed)) - - output = StreamingOutput() - picam2.start_recording(MJPEGEncoder(), FileOutput(output)) - - try: - run_server(bind_address, port, output, stream_url, snapshot_url) - finally: - picam2.stop_recording() + is_usbcam = parsed_args.is_usbcam + + if is_usbcam: + picam2 = init_usb_camera( + width, + height, + parsed_args.fps, + parse_autofocus(parsed_args.autofocus), + parsed_args.lensposition, + parse_autofocus_speed(parsed_args.autofocusspeed), + parsed_args.camera_num) + try: + picam2.start() + run_usb_server(bind_address, port, picam2, stream_url, snapshot_url) + finally: + picam2.stop() + else: + picam2 = init_camera( + width, + height, + parsed_args.fps, + parse_autofocus(parsed_args.autofocus), + parsed_args.lensposition, + parse_autofocus_speed(parsed_args.autofocusspeed), + parsed_args.camera_num) + try: + output = StreamingOutput() + picam2.start_recording(MJPEGEncoder(), FileOutput(output)) + run_server(bind_address, port, output, stream_url, snapshot_url) + finally: + picam2.stop_recording() # region args parsers @@ -114,6 +132,8 @@ def get_parser(): 'Only used with Autofocus manual') parser.add_argument('-s', '--autofocusspeed', type=str, default='normal', choices=['normal', 'fast'], help='Autofocus speed. Only used with Autofocus continuous') + parser.add_argument('-n', '--camera_num', type=int, default=0, help='Camera number to be used') + parser.add_argument('-u', '--is_usbcam', action='store_true', help='Make MJPEG USB-Cams usable. Only use if normal method doesn\'t work') return parser # endregion cli args diff --git a/spyglass/server.py b/spyglass/server.py index 0e37a04..0459026 100755 --- a/spyglass/server.py +++ b/spyglass/server.py @@ -23,63 +23,71 @@ class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer): allow_reuse_address = True daemon_threads = True +class StreamingHandler(server.BaseHTTPRequestHandler): + output = None + stream_url = '' + snapshot_url = '' -def run_server(bind_address, port, output, stream_url='/stream', snapshot_url='/snapshot'): - class StreamingHandler(server.BaseHTTPRequestHandler): - def do_GET(self): - if self.path == stream_url: - self.start_streaming() - elif self.path == snapshot_url: - self.send_snapshot() - else: - self.send_error(404) - self.end_headers() - - def start_streaming(self): - try: - self.send_response(200) - self.send_default_headers() - self.send_header('Content-Type', 'multipart/x-mixed-replace; boundary=FRAME') - self.end_headers() - while True: - with output.condition: - output.condition.wait() - frame = output.frame - self.wfile.write(b'--FRAME\r\n') - self.send_jpeg_content_headers(frame) - self.end_headers() - self.wfile.write(frame) - self.wfile.write(b'\r\n') - except Exception as e: - logging.warning('Removed streaming client %s: %s', self.client_address, str(e)) + def do_GET(self): + if self.path == self.stream_url: + self.start_streaming() + elif self.path == self.snapshot_url: + self.send_snapshot() + else: + self.send_error(404) + self.end_headers() - def send_snapshot(self): - try: - self.send_response(200) - self.send_default_headers() - with output.condition: - output.condition.wait() - frame = output.frame + def start_streaming(self): + try: + self.send_response(200) + self.send_default_headers() + self.send_header('Content-Type', 'multipart/x-mixed-replace; boundary=FRAME') + self.end_headers() + while True: + frame = self.get_frame() + self.wfile.write(b'--FRAME\r\n') self.send_jpeg_content_headers(frame) self.end_headers() self.wfile.write(frame) - except Exception as e: - logging.warning( - 'Removed client %s: %s', - self.client_address, str(e)) + self.wfile.write(b'\r\n') + except Exception as e: + logging.warning('Removed streaming client %s: %s', self.client_address, str(e)) - def send_default_headers(self): - self.send_header('Age', 0) - self.send_header('Cache-Control', 'no-cache, private') - self.send_header('Pragma', 'no-cache') + def send_snapshot(self): + try: + self.send_response(200) + self.send_default_headers() + frame = self.get_frame() + self.send_jpeg_content_headers(frame) + self.end_headers() + self.wfile.write(frame) + except Exception as e: + logging.warning( + 'Removed client %s: %s', + self.client_address, str(e)) - def send_jpeg_content_headers(self, frame): - self.send_header('Content-Type', 'image/jpeg') - self.send_header('Content-Length', len(frame)) + def send_default_headers(self): + self.send_header('Age', 0) + self.send_header('Cache-Control', 'no-cache, private') + self.send_header('Pragma', 'no-cache') + def send_jpeg_content_headers(self, frame): + self.send_header('Content-Type', 'image/jpeg') + self.send_header('Content-Length', len(frame)) + + def get_frame(self): + with self.output.condition: + self.output.condition.wait() + return self.output.frame + +def run_server(bind_address, port, output, stream_url='/stream', snapshot_url='/snapshot'): logger.info('Server listening on %s:%d', bind_address, port) logger.info('Streaming endpoint: %s', stream_url) logger.info('Snapshot endpoint: %s', snapshot_url) address = (bind_address, port) - current_server = StreamingServer(address, StreamingHandler) + streaming_handler = StreamingHandler + streaming_handler.output = output + streaming_handler.stream_url = stream_url + streaming_handler.snapshot_url = snapshot_url + current_server = StreamingServer(address, streaming_handler) current_server.serve_forever() diff --git a/spyglass/usbCamera.py b/spyglass/usbCamera.py new file mode 100644 index 0000000..4ecad1c --- /dev/null +++ b/spyglass/usbCamera.py @@ -0,0 +1,28 @@ +import libcamera +from picamera2 import Picamera2 + + +def init_usb_camera( + width: int, + height: int, + fps: int, + autofocus: str, + lens_position: float, + autofocus_speed: str, + camera_num: int): + + picam2 = Picamera2(camera_num) + + controls = {} + + if 'AfMode' in picam2.camera_controls: + controls['AfMode'] = autofocus + controls['AfSpeed'] = autofocus_speed + if autofocus == libcamera.controls.AfModeEnum.Manual: + controls['LensPosition'] = lens_position + else: + print('Attached camera does not support autofocus') + + picam2.configure(picam2.create_preview_configuration(main={'size': (width, height), 'format': 'MJPEG'}, controls=controls)) + + return picam2 diff --git a/spyglass/usbServer.py b/spyglass/usbServer.py new file mode 100644 index 0000000..05733af --- /dev/null +++ b/spyglass/usbServer.py @@ -0,0 +1,22 @@ +#!/usr/bin/python3 + +from spyglass.server import StreamingHandler, StreamingServer +from . import logger + +class StreamingHandlerUSB(StreamingHandler): + def get_frame(self): + #TODO: Cuts framerate in 1/n with n streams open, add some kind of buffer + return self.output.capture_buffer() + + +def run_usb_server(bind_address, port, cam, stream_url='/stream', snapshot_url='/snapshot'): + logger.info('Server listening on %s:%d', bind_address, port) + logger.info('Streaming endpoint: %s', stream_url) + logger.info('Snapshot endpoint: %s', snapshot_url) + address = (bind_address, port) + streaming_handler = StreamingHandlerUSB + streaming_handler.output = cam + streaming_handler.stream_url = stream_url + streaming_handler.snapshot_url = snapshot_url + current_server = StreamingServer(address, streaming_handler) + current_server.serve_forever() \ No newline at end of file From d7cedac5f800b93ec5de74a59bd25a99db37e513 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Wed, 3 Apr 2024 21:33:55 +0200 Subject: [PATCH 02/55] refactor: refactor camera code Signed-off-by: Patrick Gehrsitz --- spyglass/camera.py | 28 --------------- spyglass/camera/__init__.py | 22 ++++++++++++ spyglass/camera/camera.py | 68 +++++++++++++++++++++++++++++++++++++ spyglass/camera/csi.py | 48 ++++++++++++++++++++++++++ spyglass/camera/usb.py | 37 ++++++++++++++++++++ spyglass/cli.py | 52 +++++++--------------------- spyglass/server.py | 14 -------- spyglass/usbCamera.py | 28 --------------- spyglass/usbServer.py | 22 ------------ 9 files changed, 187 insertions(+), 132 deletions(-) delete mode 100644 spyglass/camera.py create mode 100644 spyglass/camera/__init__.py create mode 100644 spyglass/camera/camera.py create mode 100644 spyglass/camera/csi.py create mode 100644 spyglass/camera/usb.py delete mode 100644 spyglass/usbCamera.py delete mode 100644 spyglass/usbServer.py diff --git a/spyglass/camera.py b/spyglass/camera.py deleted file mode 100644 index 2040480..0000000 --- a/spyglass/camera.py +++ /dev/null @@ -1,28 +0,0 @@ -import libcamera -from picamera2 import Picamera2 - - -def init_camera( - width: int, - height: int, - fps: int, - autofocus: str, - lens_position: float, - autofocus_speed: str, - camera_num: int): - - picam2 = Picamera2(camera_num) - - controls = {'FrameRate': fps} - - if 'AfMode' in picam2.camera_controls: - controls['AfMode'] = autofocus - controls['AfSpeed'] = autofocus_speed - if autofocus == libcamera.controls.AfModeEnum.Manual: - controls['LensPosition'] = lens_position - else: - print('Attached camera does not support autofocus') - - picam2.configure(picam2.create_video_configuration(main={'size': (width, height)}, controls=controls)) - - return picam2 diff --git a/spyglass/camera/__init__.py b/spyglass/camera/__init__.py new file mode 100644 index 0000000..cf877d4 --- /dev/null +++ b/spyglass/camera/__init__.py @@ -0,0 +1,22 @@ +from .camera import Camera +from .csi import CSI +from .usb import USB + +from picamera2 import Picamera2 + +def init_camera( + width: int, + height: int, + fps: int, + autofocus: str, + lens_position: float, + autofocus_speed: str, + camera_num: int) -> Camera: + + picam2 = Picamera2(camera_num) + if picam2._is_rpi_camera(): + cam = CSI(width, height, fps, autofocus, lens_position, autofocus_speed) + else: + cam = USB(width, height, fps, autofocus, lens_position, autofocus_speed) + cam.configure() + return cam diff --git a/spyglass/camera/camera.py b/spyglass/camera/camera.py new file mode 100644 index 0000000..e0cbad3 --- /dev/null +++ b/spyglass/camera/camera.py @@ -0,0 +1,68 @@ +from abc import ABC, abstractmethod +from picamera2 import Picamera2 +from .. import logger +from .. server import StreamingServer, StreamingHandler + +class Camera(ABC): + def __init__(self, + picam2: Picamera2, + width: int, + height: int, + fps: int, + autofocus: str, + lens_position: float, + autofocus_speed: str): + + self.picam2 = picam2 + self.width = width + self.height = height + self.fps = fps + self.autofocus = autofocus + self.lens_position = lens_position + self.autofocus_speed = autofocus_speed + + def create_controls(self): + controls = {'FrameRate': self.fps} + + if 'AfMode' in self.picam2.camera_controls: + controls['AfMode'] = self.autofocus + controls['AfSpeed'] = self.autofocus_speed + if self.autofocus == self.libcamera.controls.AfModeEnum.Manual: + controls['LensPosition'] = self.lens_position + else: + print('Attached camera does not support autofocus') + + return controls + + def _run_server(self, + bind_address, + port, + output, + streaming_handler: StreamingHandler, + stream_url='/stream', + snapshot_url='/snapshot'): + logger.info('Server listening on %s:%d', bind_address, port) + logger.info('Streaming endpoint: %s', stream_url) + logger.info('Snapshot endpoint: %s', snapshot_url) + address = (bind_address, port) + streaming_handler.output = output + streaming_handler.stream_url = stream_url + streaming_handler.snapshot_url = snapshot_url + current_server = StreamingServer(address, streaming_handler) + current_server.serve_forever() + + @abstractmethod + def configure(self): + pass + + @abstractmethod + def stop(self): + pass + + @abstractmethod + def start_and_run_server(self, + bind_address, + port, + stream_url='/stream', + snapshot_url='/snapshot'): + pass diff --git a/spyglass/camera/csi.py b/spyglass/camera/csi.py new file mode 100644 index 0000000..a584300 --- /dev/null +++ b/spyglass/camera/csi.py @@ -0,0 +1,48 @@ +import io + +from picamera2.encoders import MJPEGEncoder +from picamera2.outputs import FileOutput +from threading import Condition + +from . import camera +from .. server import StreamingHandler + +class CSI(camera.Camera): + def configure(self): + controls = self.create_controls() + + self.picam2.configure( + self.picam2.create_video_configuration( + main={'size': (self.width, self.height)}, + controls=controls + ) + ) + + def start_and_run_server(self, + bind_address, + port, + stream_url='/stream', + snapshot_url='/snapshot'): + + class StreamingOutput(io.BufferedIOBase): + def __init__(self): + self.frame = None + self.condition = Condition() + + def write(self, buf): + with self.condition: + self.frame = buf + self.condition.notify_all() + output = StreamingOutput() + self.picam2.start_recording(MJPEGEncoder(), FileOutput(output)) + self._run_server( + bind_address, + port, + output, + StreamingHandler(), + stream_url=stream_url, + snapshot_url=snapshot_url + ) + + def stop(self): + self.picam2.stop_recording() diff --git a/spyglass/camera/usb.py b/spyglass/camera/usb.py new file mode 100644 index 0000000..b7a07d9 --- /dev/null +++ b/spyglass/camera/usb.py @@ -0,0 +1,37 @@ +from . import camera + +from spyglass.server import StreamingHandler, StreamingServer +from .. import logger + +class USB(camera.Camera): + def configure(self): + controls = self.create_controls() + + self.picam2.configure( + self.picam2.create_preview_configuration( + main={'size': (self.width, self.height), 'format': 'MJPEG'}, + controls=controls + ) + ) + + def start_and_run_server(self, + bind_address, + port, + stream_url='/stream', + snapshot_url='/snapshot'): + class StreamingHandlerUSB(StreamingHandler): + def get_frame(self): + #TODO: Cuts framerate in 1/n with n streams open, add some kind of buffer + return self.output.capture_buffer() + self.picam2.start() + self._run_server( + bind_address, + port, + self.picam2, + StreamingHandlerUSB(), + stream_url=stream_url, + snapshot_url=snapshot_url + ) + + def stop(self): + self.picam2.stop() diff --git a/spyglass/cli.py b/spyglass/cli.py index 3b7c49b..c8f7973 100644 --- a/spyglass/cli.py +++ b/spyglass/cli.py @@ -6,14 +6,7 @@ import sys import libcamera import re -from spyglass.camera import init_camera -from spyglass.usbCamera import init_usb_camera -from spyglass.server import StreamingOutput -from spyglass.server import run_server -from spyglass.usbServer import run_usb_server -from picamera2.encoders import MJPEGEncoder -from picamera2.outputs import FileOutput - +from .camera import init_camera def main(args=None): """Entry point for hello cli. @@ -32,38 +25,18 @@ def main(args=None): width, height = split_resolution(parsed_args.resolution) stream_url = parsed_args.stream_url snapshot_url = parsed_args.snapshot_url - is_usbcam = parsed_args.is_usbcam - - if is_usbcam: - picam2 = init_usb_camera( - width, - height, - parsed_args.fps, - parse_autofocus(parsed_args.autofocus), - parsed_args.lensposition, - parse_autofocus_speed(parsed_args.autofocusspeed), - parsed_args.camera_num) - try: - picam2.start() - run_usb_server(bind_address, port, picam2, stream_url, snapshot_url) - finally: - picam2.stop() - else: - picam2 = init_camera( - width, - height, - parsed_args.fps, - parse_autofocus(parsed_args.autofocus), - parsed_args.lensposition, - parse_autofocus_speed(parsed_args.autofocusspeed), - parsed_args.camera_num) - try: - output = StreamingOutput() - picam2.start_recording(MJPEGEncoder(), FileOutput(output)) - run_server(bind_address, port, output, stream_url, snapshot_url) - finally: - picam2.stop_recording() + cam = init_camera(width, + height, + parsed_args.fps, + parse_autofocus(parsed_args.autofocus), + parsed_args.lensposition, + parse_autofocus_speed(parsed_args.autofocusspeed), + parsed_args.camera_num) + try: + cam.start_and_run_server(bind_address, port, stream_url, snapshot_url) + finally: + cam.stop() # region args parsers @@ -133,7 +106,6 @@ def get_parser(): parser.add_argument('-s', '--autofocusspeed', type=str, default='normal', choices=['normal', 'fast'], help='Autofocus speed. Only used with Autofocus continuous') parser.add_argument('-n', '--camera_num', type=int, default=0, help='Camera number to be used') - parser.add_argument('-u', '--is_usbcam', action='store_true', help='Make MJPEG USB-Cams usable. Only use if normal method doesn\'t work') return parser # endregion cli args diff --git a/spyglass/server.py b/spyglass/server.py index 0459026..64619a1 100755 --- a/spyglass/server.py +++ b/spyglass/server.py @@ -1,24 +1,10 @@ #!/usr/bin/python3 -import io import logging import socketserver from http import server -from threading import Condition from . import logger - -class StreamingOutput(io.BufferedIOBase): - def __init__(self): - self.frame = None - self.condition = Condition() - - def write(self, buf): - with self.condition: - self.frame = buf - self.condition.notify_all() - - class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer): allow_reuse_address = True daemon_threads = True diff --git a/spyglass/usbCamera.py b/spyglass/usbCamera.py deleted file mode 100644 index 4ecad1c..0000000 --- a/spyglass/usbCamera.py +++ /dev/null @@ -1,28 +0,0 @@ -import libcamera -from picamera2 import Picamera2 - - -def init_usb_camera( - width: int, - height: int, - fps: int, - autofocus: str, - lens_position: float, - autofocus_speed: str, - camera_num: int): - - picam2 = Picamera2(camera_num) - - controls = {} - - if 'AfMode' in picam2.camera_controls: - controls['AfMode'] = autofocus - controls['AfSpeed'] = autofocus_speed - if autofocus == libcamera.controls.AfModeEnum.Manual: - controls['LensPosition'] = lens_position - else: - print('Attached camera does not support autofocus') - - picam2.configure(picam2.create_preview_configuration(main={'size': (width, height), 'format': 'MJPEG'}, controls=controls)) - - return picam2 diff --git a/spyglass/usbServer.py b/spyglass/usbServer.py deleted file mode 100644 index 05733af..0000000 --- a/spyglass/usbServer.py +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/python3 - -from spyglass.server import StreamingHandler, StreamingServer -from . import logger - -class StreamingHandlerUSB(StreamingHandler): - def get_frame(self): - #TODO: Cuts framerate in 1/n with n streams open, add some kind of buffer - return self.output.capture_buffer() - - -def run_usb_server(bind_address, port, cam, stream_url='/stream', snapshot_url='/snapshot'): - logger.info('Server listening on %s:%d', bind_address, port) - logger.info('Streaming endpoint: %s', stream_url) - logger.info('Snapshot endpoint: %s', snapshot_url) - address = (bind_address, port) - streaming_handler = StreamingHandlerUSB - streaming_handler.output = cam - streaming_handler.stream_url = stream_url - streaming_handler.snapshot_url = snapshot_url - current_server = StreamingServer(address, streaming_handler) - current_server.serve_forever() \ No newline at end of file From b136f9556b0c9169e8802864aa78cd4c16484e21 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Thu, 4 Apr 2024 17:30:00 +0200 Subject: [PATCH 03/55] Merge branch 'expand_camera_control_ability' into add_usbcam_support Signed-off-by: Patrick Gehrsitz --- .editorconfig | 19 + .github/CODEOWNERS | 2 +- .../pull_request_template.md | 7 + .github/workflows/build-and-package.yml | 29 - .github/workflows/check-commit-message.yml | 32 + .github/workflows/pull-request.yml | 49 +- .github/workflows/release.yml | 53 ++ .gitignore | 2 + .pre-commit-config.yaml | 8 + CHANGELOG.md | 71 ++ CONTRIBUTING.md | 20 - Contributors.md | 3 - LICENSE | 875 ++++++++++++++---- Makefile | 63 ++ README.md | 204 +++- docs/CONTRIBUTING.md | 107 +++ docs/contributors.md | 9 + docs/developer-certificate-of-origin.md | 34 + pyproject.toml | 20 + requirements-dev.txt | 2 + requirements-test.txt | 4 + requirements.txt | 2 +- resources/controls_style.css | 69 ++ resources/spyglass.conf | 73 ++ resources/spyglass.service | 26 + run.py | 2 +- scripts/spyglass | 145 +++ setup.cfg | 4 +- spyglass/__version__.py | 1 + spyglass/camera.py | 0 spyglass/camera/__init__.py | 20 +- spyglass/camera/camera.py | 17 +- spyglass/camera/csi.py | 26 +- spyglass/camera/usb.py | 28 +- spyglass/camera_options.py | 105 +++ spyglass/cli.py | 105 ++- spyglass/exif.py | 47 + spyglass/server.py | 121 ++- spyglass/url_parsing.py | 49 + tests/test_cli.py | 474 ++++++++++ tests/test_exif.py | 17 + tests/test_url_parsing.py | 137 +++ 42 files changed, 2709 insertions(+), 372 deletions(-) create mode 100644 .editorconfig create mode 100644 .github/PULL_REQUEST_TEMPLATE/pull_request_template.md delete mode 100644 .github/workflows/build-and-package.yml create mode 100644 .github/workflows/check-commit-message.yml create mode 100644 .github/workflows/release.yml create mode 100644 .pre-commit-config.yaml create mode 100644 CHANGELOG.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Contributors.md create mode 100644 Makefile create mode 100644 docs/CONTRIBUTING.md create mode 100644 docs/contributors.md create mode 100644 docs/developer-certificate-of-origin.md create mode 100644 requirements-dev.txt create mode 100644 requirements-test.txt create mode 100644 resources/controls_style.css create mode 100644 resources/spyglass.conf create mode 100644 resources/spyglass.service create mode 100755 scripts/spyglass create mode 100644 spyglass/__version__.py create mode 100644 spyglass/camera.py create mode 100644 spyglass/camera_options.py create mode 100644 spyglass/exif.py create mode 100644 spyglass/url_parsing.py create mode 100644 tests/test_cli.py create mode 100644 tests/test_exif.py create mode 100644 tests/test_url_parsing.py diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..cfc425a --- /dev/null +++ b/.editorconfig @@ -0,0 +1,19 @@ +# EditorConfig is awesome: https://EditorConfig.org +root = true + +[*] +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[{Makefile,**.mk}] +indent_style = tab + +[*.py] +indent_size = 4 +indent_style = space + +[{**.yaml,**.yml}] +indent_style = space +indent_size = 2 diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 305d07f..e6ded5c 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -1 +1 @@ -* @roamingthings @mryel00 +* @roamingthings @mryel00 @KwadFan diff --git a/.github/PULL_REQUEST_TEMPLATE/pull_request_template.md b/.github/PULL_REQUEST_TEMPLATE/pull_request_template.md new file mode 100644 index 0000000..5786bda --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE/pull_request_template.md @@ -0,0 +1,7 @@ +Describe what the submission changes. + +What is its intention? + +What benefit does it bring to the project? + +Signed-off-by: My Name diff --git a/.github/workflows/build-and-package.yml b/.github/workflows/build-and-package.yml deleted file mode 100644 index e9471d3..0000000 --- a/.github/workflows/build-and-package.yml +++ /dev/null @@ -1,29 +0,0 @@ -name: Build and Package - -on: - push: - branches: [ main ] - -jobs: - build: - name: build and package - runs-on: ubuntu-latest - - steps: - - name: Checkout code - uses: actions/checkout@v3 - - - name: Set up Python - uses: actions/setup-python@v4 - with: - python-version: '3.10' - - - name: Install dependencies - run: | - pip install --upgrade pip - pip install build - if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - - - name: Build and package - run: | - python -m build diff --git a/.github/workflows/check-commit-message.yml b/.github/workflows/check-commit-message.yml new file mode 100644 index 0000000..9c38cfa --- /dev/null +++ b/.github/workflows/check-commit-message.yml @@ -0,0 +1,32 @@ +name: Enforce conventional commits + +on: + pull_request_review: + types: [submitted] + +jobs: + validate: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v3 + with: + fetch-depth: 0 + ref: ${{ github.head_ref }} + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.10' + cache: 'pip' + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install commitizen + + - name: Validate pull request title and commit messages + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + echo "${{ github.event.pull_request.title }}" | cz check diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index 3dc41f8..4be90e0 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -2,7 +2,7 @@ name: Pull Request on: pull_request: - branches: [ master ] + branches: [ main ] jobs: build: @@ -10,27 +10,30 @@ jobs: runs-on: ubuntu-latest steps: - - name: Checkout code - uses: actions/checkout@v3 + - name: Checkout code + uses: actions/checkout@v3 - - name: Set up Python - uses: actions/setup-python@v4 - with: - python-version: '3.10' + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.10' + cache: 'pip' - - name: Install dependencies - run: | - pip install --upgrade pip - pip install flake8 - pip install build - if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - - name: Lint with flake8 - run: | - # stop the build if there are Python syntax errors or undefined names - flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics - # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide - flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics - - name: Build and test - run: | - python -m build - python -m pytest + - name: Install dependencies + run: | + pip install --upgrade pip + pip install flake8 + pip install build + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + if [ -f requirements-test.txt ]; then pip install -r requirements-test.txt; fi + + - name: Lint with flake8 + run: | + # stop the build if there are Python syntax errors or undefined names + flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics + # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide + flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics + + - name: Test with pytest + run: | + pytest diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..aef0699 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,53 @@ +name: Release version + +on: + push: + branches: [ main ] + +permissions: + contents: write + +jobs: + release-version: + if: "!startsWith(github.event.head_commit.message, 'bump:')" + runs-on: ubuntu-latest + name: "Bump version, create changelog and release" + + steps: + - name: Check out + uses: actions/checkout@v3 + with: + token: "${{ secrets.PERSONAL_ACCESS_TOKEN }}" + fetch-depth: 0 + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.10' + cache: 'pip' + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install build + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + + - name: Create bump and changelog + uses: commitizen-tools/commitizen-action@master + id: bump + with: + github_token: ${{ secrets.PERSONAL_ACCESS_TOKEN }} + changelog_increment_filename: release_body.md + + - name: Build package + run: | + python -m build + + - name: Release + uses: softprops/action-gh-release@v1 + with: + body_path: release_body.md + tag_name: ${{ env.REVISION }} + files: dist/* + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore index 259d74e..5408877 100644 --- a/.gitignore +++ b/.gitignore @@ -53,3 +53,5 @@ cover/ # JetBrains .idea/ + +venv diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..efe4887 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,8 @@ +repos: +- hooks: + - id: commitizen + - id: commitizen-branch + stages: + - push + repo: https://github.com/commitizen-tools/commitizen + rev: v2.42.0 diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..13f31c3 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,71 @@ +## v0.13.0 (2023-07-22) + +### Feat + +- add picamera2 tuning filters option (#55) + +## v0.12.0 (2023-07-06) + +### Feat + +- Add compatibility with moonraker update manager (#54) + +## v0.11.2 (2023-07-02) + +### Fix + +- fix url parsing (#53) + +## v0.11.1 (2023-06-10) + +### Fix + +- URL routing to accept requests that contain unused parameters (#42) + +## v0.11.0 (2023-03-19) + +### Feat + +- add exif based image rotation (#37) + +## v0.10.3 (2023-03-05) + +### Fix + +- fix issue with python2 (#40) + +## v0.10.2 (2023-02-19) + +### Fix + +- fix version project configuration + +## v0.10.1 (2023-02-19) + +### Fix + +- build packages after version bump + +## v0.10.0 (2023-02-19) + +### Feat + +- Restrict resolution to 1920x1920 (#31) +- Add testing to project (#7) +- add basic service and installer (#17) + +## v0.0.0 (2023-02-02) + +### Feat + +- Improve project structure and setup (#4) + +### Fix + +- Fix vertical flip (#16) +- Make run.py executable +- Fix python version in GitHub actions + +### Refactor + +- Rename GitHub Action jobs (#6) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md deleted file mode 100644 index 6bf37d6..0000000 --- a/CONTRIBUTING.md +++ /dev/null @@ -1,20 +0,0 @@ -# How to Contribute - -:tada: Fist off, thank you very much that you want to contribute! :tada: - -Please help to keep the project maintainable, easy to contribute to, and more secure by following this guide. - -## Pull Requests - -If you want to contribute to the project, making a fork and pull request: - -> 1. Create your own fork -> 2. Clone the fork locally -> 3. Make changes in your local clone -> 4. Push the changes from local to your fork -> 5. Create a pull request to pull the changes from your fork back into the -> upstream repository - -Please use clear commit messages, so we can understand what each commit does. -We'll review every PR and might offer feedback or request changes before -merging. diff --git a/Contributors.md b/Contributors.md deleted file mode 100644 index b2a751c..0000000 --- a/Contributors.md +++ /dev/null @@ -1,3 +0,0 @@ -# Contributors - -* [mryel00](https://github.com/mryel00) diff --git a/LICENSE b/LICENSE index 67dcfaa..2485f00 100644 --- a/LICENSE +++ b/LICENSE @@ -1,201 +1,674 @@ - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright 2023 Alexander Sparkowsky - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. \ No newline at end of file + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Spyglass Copyright (C) 2023 Alexander Sparkowsky + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..de8ac99 --- /dev/null +++ b/Makefile @@ -0,0 +1,63 @@ +## Spyglass Installer +## +## Selfdocumenting Makefile +## Based on https://www.freecodecamp.org/news/self-documenting-makefile/ + + +.PHONY: help install uninstall update + + +#### Install Paths +USER = $(shell whoami) +SYSTEMD = /etc/systemd/system +BIN_PATH = /usr/local/bin +PRINTER_DATA_PATH = /home/$(USER)/printer_data +CONF_PATH = $(PRINTER_DATA_PATH)/config + +all: + $(MAKE) help + +install: ## Install Spyglass as service + @printf "\nCopying systemd service file ...\n" + @sudo cp -f "${PWD}/resources/spyglass.service" $(SYSTEMD) + @sudo sed -i "s/%USER%/$(USER)/g" $(SYSTEMD)/spyglass.service + @printf "\nCopying Spyglass launch script ...\n" + @sudo ln -sf "${PWD}/scripts/spyglass" $(BIN_PATH) + @printf "\nCopying basic configuration file ...\n" + @cp -f "${PWD}/resources/spyglass.conf" $(CONF_PATH) + @printf "\nPopulate new service file ... \n" + @sudo systemctl daemon-reload + @sudo echo "spyglass" >> $(PRINTER_DATA_PATH)/moonraker.asvc + @printf "\nEnable Spyglass service ... \n" + @sudo systemctl enable spyglass + @printf "\nTo be sure, everything is setup please reboot ...\n" + @printf "Thanks for choosing Spyglass ...\n" + +uninstall: ## Uninstall Spyglass + @printf "\nDisable Spyglass service ... \n" + @sudo systemctl disable spyglass + @printf "\nRemove systemd service file ...\n" + @sudo rm -f $(SYSTEMD)/spyglass.service + @printf "\nRemoving Spyglass launch script ...\n" + @sudo rm -f $(BIN_PATH)/spyglass + @sudo sed '/spyglass/d' $(PRINTER_DATA_PATH)/moonraker.asvc > $(PRINTER_DATA_PATH)/moonraker.asvc + +update: ## Update Spyglass (via git Repository) + @git fetch && git pull + +upgrade-moonraker: ## In case of old version of Spyglass being upgraded to newer version with Moonraker update manager compatibility + @printf "Upgrading systemctl ...\n" + @sudo cp -f "${PWD}/resources/spyglass.service" $(SYSTEMD) + @sudo sed -i "s/%USER%/$(USER)/g" $(SYSTEMD)/spyglass.service + @printf "Saving backup of moonraker.asvc file as %s ...\n" $(PRINTER_DATA_PATH)/moonraker.asvc.bak + @sudo cp -f $(PRINTER_DATA_PATH)/moonraker.asvc $(PRINTER_DATA_PATH)/moonraker.asvc.bak + @printf "Upgrading Moonraker update manager authorization ...\n" + @sudo sed -i '/spyglass/d' $(PRINTER_DATA_PATH)/moonraker.asvc + @sudo echo "spyglass" >> $(PRINTER_DATA_PATH)/moonraker.asvc + @printf "You can now include the configuration in moonraker.conf to manage Spyglass updates ...\n" + @printf "Upgrade completed ...\n" + @printf "Thanks for choosing Spyglass ...\n" + +help: ## Show this help + @printf "\nSpyglass Install Helper:\n" + @grep -E -h '\s##\s' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}' diff --git a/README.md b/README.md index 628500b..1f94e62 100644 --- a/README.md +++ b/README.md @@ -1,68 +1,210 @@ # Spyglass ---- -**Please note that this project is in a very early stage. Use at your own risk. Think about contributing to the project -if you find that something is not working, and you are able to fix it. Every contribution is appreciated.** - ---- +> **Please note that this project is in a very early stage. Use at your own risk. Think about contributing to the project +if you find that something is not working, and you are able to fix it. Every contribution is appreciated.** A simple mjpeg server for Picamera2. With Spyglass you are able to stream videos from a camera that is supported by [libcamera](http://libcamera.org) like the [Raspberry Pi Camera Module 3](https://www.raspberrypi.com/products/camera-module-3/). +Current version: 0.13.0 + ## Prerequisites -* Raspberry Pi OS Bullseye -* [Picamera2](https://github.com/raspberrypi/picamera2) - Already installed on Raspberry Pi OS Bullseye -* Python 3 -* A camera supported by libcamera and connected to the Raspberry Pi +- Raspberry Pi OS Bullseye +- [Picamera2](https://github.com/raspberrypi/picamera2) - Already installed on Raspberry Pi OS Bullseye +- Python 3.8+ +- A camera supported by libcamera and connected to the Raspberry Pi ## Quick Start The server can be started with + ```shell ./run.py ``` This will start the server with the following default configuration: -* Address the server binds to: 0.0.0.0 -* Port: 8080 -* Resolution: 640x480 -* Framerate: 15fps -* Stream URL: /stream -* Snapshot URL: /snapshot + +- Address the server binds to: 0.0.0.0 +- Port: 8080 +- Resolution: 640x480 +- Framerate: 15fps +- Stream URL: /stream +- Snapshot URL: /snapshot ## Configuration On startup the following arguments are supported: -| Argument | Description | Default | -|--------------------------|-----------------------------------------------------------------------------------------------------|--------------| -| `-b`, `--bindaddress` | Address where the server will listen for new incoming connections. | `0.0.0.0` | -| `-p`, `--port` | Port where the server will listen for new incoming connections. | `8080` | -| `-r`, `--resolution` | Resolution of the captured frames. This argument expects the format x | `640x480` | -| `-f`, `--fps` | Framerate in frames per second (fps). | `15` | -| `-st`, `--stream_url` | Sets the URL for the mjpeg stream. | `/stream` | -| `-sn`, `--snapshot_url` | Sets the URL for snapshots (single frame of stream). | `/snapshot` | -| `-af`, `--autofocus` | Autofocus mode. Supported modes: `manual`, `continous` | `continuous` | -| `-l`, `--lensposition` | Set focal distance. 0 for infinite focus, 0.5 for approximate 50cm. Only used with Autofocus manual | `0.0` | -| `-s`, `--autofocusspeed` | Autofocus speed. Supported values: `normal`, `fast`. Only used with Autofocus continuous | `normal` | +| Argument | Description | Default | +|-------------------------------|------------------------------------------------------------------------------------------------------------------------------------|--------------| +| `-b`, `--bindaddress` | Address where the server will listen for new incoming connections. | `0.0.0.0` | +| `-p`, `--port` | Port where the server will listen for new incoming connections. | `8080` | +| `-r`, `--resolution` | Resolution of the captured frames. This argument expects the format \x\ | `640x480` | +| `-f`, `--fps` | Framerate in frames per second (fps). | `15` | +| `-st`, `--stream_url` | Sets the URL for the mjpeg stream. | `/stream` | +| `-sn`, `--snapshot_url` | Sets the URL for snapshots (single frame of stream). | `/snapshot` | +| `-af`, `--autofocus` | Autofocus mode. Supported modes: `manual`, `continuous`. | `continuous` | +| `-l`, `--lensposition` | Set focal distance. 0 for infinite focus, 0.5 for approximate 50cm. Only used with Autofocus manual. | `0.0` | +| `-s`, `--autofocusspeed` | Autofocus speed. Supported values: `normal`, `fast`. Only used with Autofocus continuous | `normal` | +| `-ud`, `--upsidedown` | Rotate the image by 180° (see below) | | +| `-fh`, `--flip_horizontal` | Mirror the image horizontally (see below) | | +| `-fv`, `--flip_vertical` | Mirror the image vertically (see below) | | +| `-or`, `--orientation_exif` | Set the image orientation using an EXIF header (see below) | | +| `-c`, `--controls` | Define camera controls to start spyglass with. Can be used multiple times. This argument expects the format \=\. | | +| `--list-controls` | List all available libcamera controls onto the console. Those can be used with `--controls` | | +| `-tf`, `--tuning_filter` | Set a tuning filter file name. | | +| `-tfd`, `--tuning_filter_dir` | Set the directory to look for tuning filters. | | Starting the server without any argument is the same as + ```shell -./run.py -b 0.0.0.0 -p 8080 -r 640x480 -f 15 -st '/stream' -sn '/snapshot' -af continous -l 0.0 -s normal +./run.py -b 0.0.0.0 -p 8080 -r 640x480 -f 15 -st '/stream' -sn '/snapshot' -af continuous -l 0.0 -s normal ``` The stream can then be accessed at `http://:8080/stream` +### Maximum resolution + +Please note that the maximum recommended resolution is 1920x1080 (16:9). + +The absolute maximum resolution is 1920x1920. If you choose a higher resolution spyglass may crash. + +### Image Orientation + +There are two ways to change the image orientation. + +To use the ability of picamera2 to transform the image you can use the following options when starting spyglass: + * `-ud` or `--upsidedown` - Rotate the image by 180° + * `-fh` or `--flip_horizontal` - Mirror the image horizontally + * `-fv` or `--flip_vertical` - Mirror the image vertically + +Alternatively you can create an EXIF header to modify the image orientation. Most modern browsers should respect the +this header. + +Use the `-or` or `--orientation_exif` option and choose from one of the following orientations + * `h` - Horizontal (normal) + * `mh` - Mirror horizontal + * `r180` - Rotate 180 + * `mv` - Mirror vertical + * `mhr270` - Mirror horizontal and rotate 270 CW + * `r90` - Rotate 90 CW + * `mhr90` - Mirror horizontal and rotate 90 CW + * `r270` - Rotate 270 CW + +For example to rotate the image 90 degree clockwise you would start spyglass the following way: +```shell +./run.py -or r90 +``` + +### Tuning filter +Tuning filters are used to normalize or modify the camera image output, for example, using an NoIR camera can lead to a pink color, whether applying a filter to it you could remove its tone pink. More information here: https://github.com/raspberrypi/picamera2/blob/main/examples/tuning_file.py + + +Predefined filters can be found at one of the picamera2 directories: +- ~/libcamera/src/ipa/rpi/vc4/data +- /usr/local/share/libcamera/ipa/rpi/vc4 +- /usr/share/libcamera/ipa/rpi/vc4 +- /usr/share/libcamera/ipa/raspberrypi + +You can also define your own directory of filter by using parameter `tuning_filter_dir`. + +list the files present there and you can use it in our config. eg.: `ov5647_noir.json` + + ## Using Spyglass with Mainsail -If you ant to use Spyglass as a webcam source for [Mainsail]() add a webcam with the following configuration: -* URL Stream: `/webcam/stream` -* URL Snapshot: `/webcam/snapshot` -* Service: `V4L-MJPEG` +If you want to use Spyglass as a webcam source for [Mainsail]() add a webcam with the following configuration: + +- URL Stream: `/webcam/stream` +- URL Snapshot: `/webcam/snapshot` +- Service: `V4L-MJPEG` ## Install as application If you want to install Spyglass globally on your machine you can use `python -m pip install .` to do so. + +## Install and run as a service + +### Install + +Quite simple: + +```bash +cd ~/spyglass +make install +``` + +This will ask you for your `sudo` password.\ +After install is done, please reboot to ensure service starts properly + +To uninstall the service simply use + +```bash +cd ~/spyglass +make uninstall +``` + +#### Using Moonraker Update Manager + +To be able to use Moonraker update manager, add the following lines to moonraker.conf: + +```conf +[update_manager spyglass] +type: git_repo +channel: beta +path: ~/spyglass +origin: https://github.com/roamingthings/spyglass.git +managed_services: spyglass +``` +> Make sure moonraker.asvc contains `spyglass` in the list: `cat ~/printer_data/moonraker.asvc | grep spyglass`. +> +> If not there execute: `make upgrade-moonraker` + +### Configuration + +You should find a configuration file in `~/printer_data/config/spyglass.conf`. + +Please see [spyglass.conf](resources/spyglass.conf) for an example + +### Restart the service + +To restart the service use `systemctl`: + +```shell +sudo systemctl restart spyglass +``` + +## Start Developing + +If you want to setup your environment for development perform the following steps: + +Setup your Python virtual environment: +```shell +python -m venv .venv # Create a new virtual environment +. .venv/bin/activate # Activate virtual environment +python -m pip install --upgrade pip # Upgrade PIP to the current version +pip install -r requirements.txt # Install application dependencies +pip install -r requirements-test.txt # Install test dependencies +pip install -r requirements-dev.txt # Install development dependencies +``` + +The project uses [commitizen](https://github.com/commitizen-tools/commitizen) to check commit messages to comply with +[Conventional Commits](http://conventionalcommits.org). When installing the development dependencies git-hooks will be +set up to check commit messages pre commit. + +### Problems when Committing to your Branch + + You may get the following error message when you try to push to your branch: + ``` + fatal: ambiguous argument 'origin/HEAD..HEAD': unknown revision or path not in the working tree. + Use '--' to separate paths from revisions, like this: + 'git [...] -- [...]' + ``` + This error occurs when your HEAD branch is not properly set ([Stack Overflow](https://stackoverflow.com/a/8841024)) + To fix this run the following command: + ```shell + git remote set-head origin -a + ``` diff --git a/docs/CONTRIBUTING.md b/docs/CONTRIBUTING.md new file mode 100644 index 0000000..b427718 --- /dev/null +++ b/docs/CONTRIBUTING.md @@ -0,0 +1,107 @@ +# How to Contribute + +:tada: Fist off, thank you very much that you want to contribute! :tada: + +Please help to keep the project maintainable, easy to contribute to, and more secure by following this guide. + +## The Contribution Process + +If you want to contribute to the project, making a fork and pull request: + +1. Create your own fork. +2. Clone the fork locally. +3. Make changes in your local clone. +4. Push the changes from local to your fork. +5. Create a [GitHub Pull Request](https://github.com/roamingthings/spyglass/pulls) when your submission is ready to + to be deployed into the project. +6. A [reviewer](contributors.md) that is ready to review your submission will assign themselves to the Pull Request on + GitHub. The review process aims at checking the submission for defects, completeness and overall fit into the + general architecture + of the project. +7. After a successful review the Pull Request will be 'approved' on GitHub and will be committed to the main branch by + a [contributor](contributors.md). + +## About the Review Process + +Every contribution to Spyglass will be reviewed before it is merged into the main branch. The review aims to check for +defects and to ensure that the submission follows the general style and architecture of the project. + +It is understood that there is not a single 'best way' to accomplish a task. Therefore, it is not intended to discuss +if the submission is the 'best' implementation. + +Most of the time you will receive feedback from the review. Please be prepared to provide more information or details +and update your submission, if required. + +Common aspects that a review looks for: + +1. Are there any defects and is the submission ready to be widely distributed? +2. Does the submission provide real additional value to the project that will improve what users will get out of the + software? +3. Does the submission include automated tests (e.g. unit test) when applicable that will ensure that the implementation + does what it + is intended to do? +4. Is the copyright of the submission clear, compatible with the project and non-gratuitous? +5. Commits well formatted, cover a single topic and independent? +6. Is the documentation updated to reflect the changes? +7. Does the implementation follow the general style of the project? + +## Format of Commit Messages + +The header of the commit should be conformal with [conventional commits](https://www.conventionalcommits.org) and the +description should be contained in the commit message body. + +``` +: lowercase, present form, short summary (the 'what' of the change) + +Optional, more detailed explanation of what the commit does (the 'why' and 'how'). + +Signed-off-by: My Name +``` + +The `` may be one of the following list: + +* `feat` - A new feature +* `fix` - A bug fix +* `test` - Adding a new test or improve an existing one +* `docs` - Changes or additions to the documentation +* `refactor` - Refactoring of the code or other project elements +* `chore` - Other modifications that do not modify implementation, test or documentations + +It is important to have a "Signed-off-by" line on each commit to certify that you agree to the +[developer certificate of origin](developer-certificate-of-origin.md). Depending on your IDE or editor you can +automatically add this submission line with each commit. +It has to contain your real name (please don't use pseudonyms) and contain a current email address. Unfortunately, we +cannot accept anonymous submissions. + +You can use `git commit -s` to sign off a commit. + +## Format of the Pull Request + +The pull request title and description will help the contributors to describe the change that is finally merged into +the main branch. Each submission will be squashed into a single commit before the merge. + +This project follows the [Conventional Commits specification](https://www.conventionalcommits.org) to help to easily +understand the intention and change of a commit. + +When crating the pull request we ask you to use the following guideline: + +The title of the pull request has the following format: + +``` +: lowercase, present form, short summary (the 'what' of the change) +``` + +If your submission does introduce a breaking change please add `BREAKING CHANGE` to the beginning of the description. + +Similar to a commit the description describes the overall change of the submission and include a `Signed-off-by` line +at the end: + +``` +Describe what the submission changes. + +What is its intention? + +What benefit does it bring to the project? + +Signed-off-by: My Name +``` diff --git a/docs/contributors.md b/docs/contributors.md new file mode 100644 index 0000000..66850c2 --- /dev/null +++ b/docs/contributors.md @@ -0,0 +1,9 @@ +# Main Contributors + +In alphabetical order of GitHub handles: + +| GitHub Handle | Name | +|---------------------------------------------------|----------------------| +| [KwadFan](https://github.com/KwadFan) | Stephan Wendel | +| [mryel00](https://github.com/mryel00) | Patrick Gehrsitz | +| [roamingthings](https://github.com/roamingthings) | Alexander Sparkowsky | diff --git a/docs/developer-certificate-of-origin.md b/docs/developer-certificate-of-origin.md new file mode 100644 index 0000000..49b8cb0 --- /dev/null +++ b/docs/developer-certificate-of-origin.md @@ -0,0 +1,34 @@ +Developer Certificate of Origin +Version 1.1 + +Copyright (C) 2004, 2006 The Linux Foundation and its contributors. + +Everyone is permitted to copy and distribute verbatim copies of this +license document, but changing it is not allowed. + + +Developer's Certificate of Origin 1.1 + +By making a contribution to this project, I certify that: + +(a) The contribution was created in whole or in part by me and I + have the right to submit it under the open source license + indicated in the file; or + +(b) The contribution is based upon previous work that, to the best + of my knowledge, is covered under an appropriate open source + license and I have the right under that license to submit that + work with modifications, whether created in whole or in part + by me, under the same open source license (unless I am + permitted to submit under a different license), as indicated + in the file; or + +(c) The contribution was provided directly to me by some other + person who certified (a), (b) or (c) and I have not modified + it. + +(d) I understand and agree that this project and the contribution + are public and that a record of the contribution (including all + personal information I submit with it, including my sign-off) is + maintained indefinitely and may be redistributed consistent with + this project or the open source license(s) involved. diff --git a/pyproject.toml b/pyproject.toml index bf91296..1d11a0f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,3 +1,23 @@ [build-system] build-backend = "setuptools.build_meta" requires = ["setuptools", "wheel"] + +[tool.pytest.ini_options] +addopts = [ + "--import-mode=importlib", +] +pythonpath = [ + "." +] +mock_use_standalone_module = true + +[tool.commitizen] +name = "cz_conventional_commits" +version = "0.13.0" +tag_format = "v$version" +version_files = [ + "spyglass/__version__.py", + "setup.cfg:version", + "pyproject.toml:version", + "README.md:Current version" +] diff --git a/requirements-dev.txt b/requirements-dev.txt new file mode 100644 index 0000000..817f696 --- /dev/null +++ b/requirements-dev.txt @@ -0,0 +1,2 @@ +commitizen +pre-commit diff --git a/requirements-test.txt b/requirements-test.txt new file mode 100644 index 0000000..36ce862 --- /dev/null +++ b/requirements-test.txt @@ -0,0 +1,4 @@ +pylint +pytest +mock +pytest-mock diff --git a/requirements.txt b/requirements.txt index 8e7d3dc..0612eb5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -setuptools~=66.1.1 +setuptools~=69.0.3 diff --git a/resources/controls_style.css b/resources/controls_style.css new file mode 100644 index 0000000..e88c32e --- /dev/null +++ b/resources/controls_style.css @@ -0,0 +1,69 @@ +body { + font-family: Arial, sans-serif; + background-color: #f6f6f6; + margin: 40px; + display: flex; + flex-direction: column; + align-items: center; +} + +.card-container { + display: flex; + justify-content: center; + align-items: center; + width: 100%; + max-width: 1100px; +} + +.card-container:nth-child(odd) .card { + background-color: white; +} + +.card-container:nth-child(even) .card { + background-color: #f0f0f0; +} + +.card { + border-radius: 5px; + box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); + padding: 0px 20px 20px 20px; /* top right bottom left */ + width: 80%; + box-sizing: border-box; + transition: 0.3s; +} + +.card:hover { + box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2); +} + +.card h2 { + color: #2C3E50; + border-bottom: 1px solid #2C3E50; + padding-bottom: 10px; + margin-bottom: 10px; +} + +.card-content { + display: flex; + margin-bottom: 1px; +} + +.setting { + flex: 1; + display: flex; + margin: 0 5px; +} + +.label, +.value { + font-size: 14px; +} + +.label { + font-weight: bold; + color: #7F8C8D; +} + +.value { + margin-left: 4px; +} diff --git a/resources/spyglass.conf b/resources/spyglass.conf new file mode 100644 index 0000000..1072b56 --- /dev/null +++ b/resources/spyglass.conf @@ -0,0 +1,73 @@ +#### spyglass - Picamera2 MJPG Streamer +#### +#### https://github.com/roamingthings/spyglass +#### +#### This File is distributed under GPLv3 +#### + +#### NOTE: Please ensure parameters are in capital letters and values in lowercase! +#### NOTE: Values has to be surrounded by double quotes! ("value") +#### NOTE: If commented out or includes typos it will use hardcoded defaults! + +#### Running Spyglass with proxy or Standalone (BOOL)[default: true] +NO_PROXY="true" + +#### HTTP Port to listen on (INTEGER)[default: 8080] +HTTP_PORT="8080" + +#### Resolution (INTEGERxINTEGER)[default: 640x480] +RESOLUTION="640x480" +#### NOTE: the maximum supported resolution is 1920x1920 (recommended maximum 1920x1080) + +#### Frames per second (INTEGER)[default: 15] +FPS="15" + +#### Stream URL (STRING)[default: /stream] +STREAM_URL="/stream" +#### NOTE: use format as shown below to stay MJPG-Streamer URL compatible +## STREAM_URL="/?action=stream" + +#### Snapshot URL (STRING)[default: /snapshot] +SNAPSHOT_URL="/snapshot" +#### NOTE: use format as shown below to stay MJPG-Streamer URL compatible +## SNAPSHOT_URL="/?action=snapshot" + +#### Autofocus behavior (STRING:manual,continuous)[default: continuous] +AUTO_FOCUS="continuous" + +#### Focal Distance (float:0.0)[default: 0.0] +#### NOTE: Set focal distance. 0 for infinite focus, 0.5 for approximate 50cm. +#### Only used with Autofocus manual +FOCAL_DIST="0.0" + +#### Auto Focus Speed (STRING:normal,fast)[default: normal] +#### NOTE: Autofocus speed. Supported values: normal, fast. +#### Only used with Autofocus continuous +AF_SPEED="normal" + +#### EXIF Orientation (STRING:h,mh,r180,mv,mhr270,r90,mhr90,r270)[default: h] +#### NOTE: Set the image orientation using an EXIF header. +#### h - Horizontal (normal) +#### mh - Mirror horizontal +#### r180 - Rotate 180 +#### mv - Mirror vertical +#### mhr270 - Mirror horizontal and rotate 270 CW +#### r90 - Rotate 90 CW +#### mhr90 - Mirror horizontal and rotate 90 CW +#### r270 - Rotate 270 CW +ORIENTATION_EXIF="h" + +#### Camera Controls +#### NOTE: Set v4l2 controls your camera supports at startup +#### EXAMPLE: CONTROLS="brightness=0,awbenable=false" +CONTROLS="" + +#### Tuning Filter Directory (STRING)[default: none] +#### NOTE: Directory where to search for tuning filters(if defined). +#### Directory only used if TUNING_FILTER is defined +# TUNING_FILTER_DIR="/usr/share/libcamera/ipa/raspberrypi" + +#### Tuning Filter (STRING)[default: none] +#### NOTE: Name of the file to be used to apply tuning filter. +#### If dir not defined, default pycamera2 directories will be used. +# TUNING_FILTER="ov5647_noir.json" diff --git a/resources/spyglass.service b/resources/spyglass.service new file mode 100644 index 0000000..6843152 --- /dev/null +++ b/resources/spyglass.service @@ -0,0 +1,26 @@ +#### spyglass - Picamera2 MJPG Streamer +#### +#### https://github.com/roamingthings/spyglass +#### +#### This File is distributed under GPLv3 +#### + +[Unit] +Description=spyglass - Picamera2 MJPG Streamer +Documentation=https://github.com/roamingthings/spyglass +After=udev.service network-online.target nss-lookup.target +Wants=udev.service network-online.target +StartLimitBurst=10 +StartLimitIntervalSec=180 + +[Install] +WantedBy=multi-user.target + +[Service] +Type=simple +User=%USER% +RemainAfterExit=Yes +WorkingDirectory=/home/%USER%/spyglass +ExecStart= /usr/local/bin/spyglass +Restart=on-failure +RestartSec=5 diff --git a/run.py b/run.py index f5965e0..4e9d235 100755 --- a/run.py +++ b/run.py @@ -1,4 +1,4 @@ -#! /usr/bin/env python +#! /usr/bin/env python3 from spyglass.cli import main diff --git a/scripts/spyglass b/scripts/spyglass new file mode 100755 index 0000000..46b6261 --- /dev/null +++ b/scripts/spyglass @@ -0,0 +1,145 @@ +#!/usr/bin/env bash +#### spyglass - Picamera2 MJPG Streamer +#### +#### https://github.com/roamingthings/spyglass +#### +#### This File is distributed under GPLv3 +#### + +# shellcheck enable=require-variable-braces + +### Error Handling +set -Eeou pipefail + + +### Global Variables +BASE_SPY_PATH="$(dirname "$(readlink -f "${0}")")" +PY_BIN="$(command -v python)" +SPYGLASS_CFG="${HOME}/printer_data/config/spyglass.conf" + +### Helper Messages +debug_msg() { + printf "DEBUG: %s\n" "${1}" +} + +help_msg() { + echo -e "spyglass - Picamera2 MJPG Streamer\nUsage:" + echo -e "\t spyglass [Options]" + echo -e "\n\t\t-h Prints this help." + echo -e "\n\t\t-c \n\t\t\tPath to your spyglass.conf" + echo -e "\n\t\t-v Show spyglass version\n" +} + +wrong_args_msg() { + echo -e "spyglass: Wrong Arguments!" + echo -e "\n\tTry: spyglass -h\n" +} + +### Helper Funcs + +## Version of spyglass +self_version() { + pushd "${BASE_SPY_PATH}" &> /dev/null + git describe --always --tags + popd &> /dev/null +} + +check_py_version() { + local version + if [[ -n "${PY_BIN}" ]]; then + version=$("${PY_BIN}" -V | cut -d" " -f2 | cut -d"." -f1) + else + printf "ERROR: Python interpreter is not installed! [EXITING]\n" + exit 1 + fi + if [[ -n "${version}" ]] && [[ "${version}" = "3" ]]; then + printf "INFO: Python interpreter Version %s found ... [OK]\n" "$("${PY_BIN}" -V)" + elif [[ -n "${version}" ]] && [[ "${version}" = "2" ]]; then + printf "ERROR: Python interpreter Version 3 is required! [EXITING]\n" + exit 1 + fi +} + +get_config() { + if [[ -n "${SPYGLASS_CFG}" ]] && [[ -f "${SPYGLASS_CFG}" ]]; then + printf "INFO: Configuration file found in %s\n" "${SPYGLASS_CFG}" + print_config + # shellcheck disable=SC1090 + . "${SPYGLASS_CFG}" + else + printf "ERROR: No configuration file found in %s! [EXITING]\n" "${SPYGLASS_CFG}" + exit 1 + fi +} + +print_config() { + local prefix + prefix="\t\t" + printf "INFO: Print Configfile: '%s'\n" "${SPYGLASS_CFG}" + (sed '/^#.*/d;/./,$!d;/^$/d' | cut -d'#' -f1) < "${SPYGLASS_CFG}" | \ + while read -r line; do + printf "%b%s\n" "${prefix}" "${line}" + done +} + +run_spyglass() { + local bind_adress + # ensure default for NO_PROXY + [[ -n "${NO_PROXY}" ]] || NO_PROXY="true" + + if [[ "${NO_PROXY}" != "true" ]]; then + bind_adress="127.0.0.1" + else + bind_adress="0.0.0.0" + fi + + "${PY_BIN}" "$(dirname "${BASE_SPY_PATH}")/run.py" \ + --bindaddress "${bind_adress}" \ + --port "${HTTP_PORT:-8080}" \ + --resolution "${RESOLUTION:-640x480}" \ + --fps "${FPS:-15}" \ + --stream_url "${STREAM_URL:-\/stream}" \ + --snapshot_url "${SNAPSHOT_URL:-\/snapshot}" \ + --autofocus "${AUTO_FOCUS:-continuous}" \ + --lensposition "${FOCAL_DIST:-0.0}" \ + --autofocusspeed "${AF_SPEED:-normal}" \ + --orientation_exif "${ORIENTATION_EXIF:-h}" \ + --tuning_filter "${TUNING_FILTER:-}"\ + --tuning_filter_dir "${TUNING_FILTER_DIR:-}" \ + --controls-string "${CONTROLS:-0=0}" # 0=0 to prevent error on empty string +} + +#### MAIN +## Parse Args +while getopts ":vhc:d" arg; do + case "${arg}" in + v ) + echo -e "\nspyglass Version: $(self_version)\n" + exit 0 + ;; + h ) + help_msg + exit 0 + ;; + c ) + SPYGLASS_CFG="${OPTARG}" + ;; + d ) + set -x + ;; + \?) + wrong_args_msg + exit 1 + ;; + esac +done + + +check_py_version +get_config +run_spyglass + +### Loop to keep running +while true; do + sleep 1 +done diff --git a/setup.cfg b/setup.cfg index 057cd83..1abba7d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,9 +1,9 @@ [metadata] name = spyglass -version = 0.0.1 +version = 0.13.0 description = A simple mjpeg server for Picamera2 url = https://github.com/roamingthings/spyglass -license = Apache License 2.0 +license = GPL-3.0-only license_files = LICENSE NOTICE diff --git a/spyglass/__version__.py b/spyglass/__version__.py new file mode 100644 index 0000000..f23a6b3 --- /dev/null +++ b/spyglass/__version__.py @@ -0,0 +1 @@ +__version__ = "0.13.0" diff --git a/spyglass/camera.py b/spyglass/camera.py new file mode 100644 index 0000000..e69de29 diff --git a/spyglass/camera/__init__.py b/spyglass/camera/__init__.py index cf877d4..a277d81 100644 --- a/spyglass/camera/__init__.py +++ b/spyglass/camera/__init__.py @@ -5,18 +5,32 @@ from picamera2 import Picamera2 def init_camera( + camera_num: int, width: int, height: int, fps: int, autofocus: str, lens_position: float, autofocus_speed: str, - camera_num: int) -> Camera: + upsidedown=False, + flip_horizontal=False, + flip_vertical=False, + control_list: list[list[str]]=[], + tuning_filter=None, + tuning_filter_dir=None + ) -> Camera: + tuning = None - picam2 = Picamera2(camera_num) + if tuning_filter: + params = {'tuning_file': tuning_filter} + if tuning_filter_dir: + params['dir'] = tuning_filter_dir + tuning = Picamera2.load_tuning_file(**params) + + picam2 = Picamera2(camera_num, tuning=tuning) if picam2._is_rpi_camera(): cam = CSI(width, height, fps, autofocus, lens_position, autofocus_speed) else: cam = USB(width, height, fps, autofocus, lens_position, autofocus_speed) - cam.configure() + cam.configure(control_list, upsidedown, flip_horizontal, flip_vertical) return cam diff --git a/spyglass/camera/camera.py b/spyglass/camera/camera.py index e0cbad3..71ba375 100644 --- a/spyglass/camera/camera.py +++ b/spyglass/camera/camera.py @@ -19,7 +19,7 @@ def __init__(self, self.fps = fps self.autofocus = autofocus self.lens_position = lens_position - self.autofocus_speed = autofocus_speed + self.autofocus_speed = autofocus_speed def create_controls(self): controls = {'FrameRate': self.fps} @@ -40,19 +40,27 @@ def _run_server(self, output, streaming_handler: StreamingHandler, stream_url='/stream', - snapshot_url='/snapshot'): + snapshot_url='/snapshot', + orientation_exif=0): logger.info('Server listening on %s:%d', bind_address, port) logger.info('Streaming endpoint: %s', stream_url) logger.info('Snapshot endpoint: %s', snapshot_url) + logger.info('Controls endpoint: %s', '/controls') address = (bind_address, port) streaming_handler.output = output + streaming_handler.picam2 = self.picam2 streaming_handler.stream_url = stream_url streaming_handler.snapshot_url = snapshot_url + streaming_handler.exif_header = orientation_exif current_server = StreamingServer(address, streaming_handler) current_server.serve_forever() @abstractmethod - def configure(self): + def configure(self, + control_list: list[list[str]]=[], + flip_horizontal=False, + flip_vertical=False, + upsidedown=False): pass @abstractmethod @@ -64,5 +72,6 @@ def start_and_run_server(self, bind_address, port, stream_url='/stream', - snapshot_url='/snapshot'): + snapshot_url='/snapshot', + orientation_exif=0): pass diff --git a/spyglass/camera/csi.py b/spyglass/camera/csi.py index a584300..f5b3cf0 100644 --- a/spyglass/camera/csi.py +++ b/spyglass/camera/csi.py @@ -1,20 +1,34 @@ import io +import libcamera from picamera2.encoders import MJPEGEncoder from picamera2.outputs import FileOutput from threading import Condition from . import camera -from .. server import StreamingHandler +from ..server import StreamingHandler +from ..camera_options import process_controls class CSI(camera.Camera): - def configure(self): + def configure(self, + control_list: list[list[str]]=[], + flip_horizontal=False, + flip_vertical=False, + upsidedown=False): controls = self.create_controls() + c = process_controls(self.picam2, [tuple(ctrl) for ctrl in control_list]) + controls.update(c) + + transform = libcamera.Transform( + hflip=int(flip_horizontal or upsidedown), + vflip=int(flip_vertical or upsidedown) + ) self.picam2.configure( self.picam2.create_video_configuration( main={'size': (self.width, self.height)}, - controls=controls + controls=controls, + transform=transform ) ) @@ -22,7 +36,8 @@ def start_and_run_server(self, bind_address, port, stream_url='/stream', - snapshot_url='/snapshot'): + snapshot_url='/snapshot', + orientation_exif=0): class StreamingOutput(io.BufferedIOBase): def __init__(self): @@ -41,7 +56,8 @@ def write(self, buf): output, StreamingHandler(), stream_url=stream_url, - snapshot_url=snapshot_url + snapshot_url=snapshot_url, + orientation_exif=orientation_exif ) def stop(self): diff --git a/spyglass/camera/usb.py b/spyglass/camera/usb.py index b7a07d9..8a44419 100644 --- a/spyglass/camera/usb.py +++ b/spyglass/camera/usb.py @@ -1,16 +1,30 @@ +import libcamera + from . import camera from spyglass.server import StreamingHandler, StreamingServer -from .. import logger +from ..camera_options import process_controls class USB(camera.Camera): - def configure(self): + def configure(self, + control_list: list[list[str]]=[], + flip_horizontal=False, + flip_vertical=False, + upsidedown=False): controls = self.create_controls() + c = process_controls(self.picam2, [tuple(ctrl) for ctrl in control_list]) + controls.update(c) + + transform = libcamera.Transform( + hflip=int(flip_horizontal or upsidedown), + vflip=int(flip_vertical or upsidedown) + ) self.picam2.configure( self.picam2.create_preview_configuration( main={'size': (self.width, self.height), 'format': 'MJPEG'}, - controls=controls + controls=controls, + transform=transform ) ) @@ -18,7 +32,8 @@ def start_and_run_server(self, bind_address, port, stream_url='/stream', - snapshot_url='/snapshot'): + snapshot_url='/snapshot', + orientation_exif=0): class StreamingHandlerUSB(StreamingHandler): def get_frame(self): #TODO: Cuts framerate in 1/n with n streams open, add some kind of buffer @@ -30,8 +45,9 @@ def get_frame(self): self.picam2, StreamingHandlerUSB(), stream_url=stream_url, - snapshot_url=snapshot_url + snapshot_url=snapshot_url, + orientation_exif=orientation_exif ) - + def stop(self): self.picam2.stop() diff --git a/spyglass/camera_options.py b/spyglass/camera_options.py new file mode 100644 index 0000000..f104c39 --- /dev/null +++ b/spyglass/camera_options.py @@ -0,0 +1,105 @@ +import libcamera +import ast + +def parse_dictionary_to_html_page(camera, parsed_controls='None', processed_controls='None'): + html = """ + + + """ + html += f""" + + + + Camera Settings + + + """ + html += f""" + +

Available camera options

+

Parsed Controls: {parsed_controls}

+

Processed Controls: {processed_controls}

+ """ + for control, values in camera.camera_controls.items(): + html += f""" +
+
+

{control}

+
+
+ Min: + {values[0]} +
+
+ Max: + {values[1]} +
+
+ Default: + {values[2]} +
+
+
+
+ """ + html += """ + + + """ + return html + +def get_style(): + with (open('resources/controls_style.css', 'r')) as f: + return f.read() + +def process_controls(camera, controls: list[tuple[str, str]]) -> dict[str, any]: + controls_dict_lower = { k.lower(): k for k in camera.camera_controls.keys() } + if controls == None: + return {} + processed_controls = {} + for key, value in controls: + key = key.lower().strip() + if key.lower() in controls_dict_lower.keys(): + value = value.lower().strip() + k = controls_dict_lower[key] + v = parse_from_string(value) + processed_controls[k] = v + return processed_controls + +def parse_from_string(input_string: str) -> any: + try: + return ast.literal_eval(input_string) + except (ValueError, TypeError, SyntaxError): + pass + + if input_string.lower() in ['true', 'false']: + return input_string.lower() == 'true' + + return input_string + +def get_type_str(obj) -> str: + return str(type(obj)).split('\'')[1] + +def get_libcamera_controls_string(camera_path: str) -> str: + ctrls_str = "" + libcam_cm = libcamera.CameraManager.singleton() + cam = libcam_cm.cameras[0] + for k, v in cam.controls.items(): + def rectangle_to_tuple(rectangle): + return (rectangle.x, rectangle.y, rectangle.width, rectangle.height) + + if isinstance(v.min, libcamera.Rectangle): + min = rectangle_to_tuple(v.min) + max = rectangle_to_tuple(v.max) + default = rectangle_to_tuple(v.default) + else: + min = v.min + max = v.max + default = v.default + + str_first = f"{k.name} ({get_type_str(min)})" + str_second = f"min={min} max={max} default={default}" + str_indent = (30 - len(str_first)) * ' ' + ': ' + ctrls_str += str_first + str_indent + str_second + '\n' + + return ctrls_str.strip() diff --git a/spyglass/cli.py b/spyglass/cli.py index c8f7973..087ba45 100644 --- a/spyglass/cli.py +++ b/spyglass/cli.py @@ -1,13 +1,24 @@ -"""cli entry point for hellocli. +"""cli entry point for spyglass. -Parse command line arguments in, invoke hello. +Parse command line arguments in, invoke server. """ import argparse +import logging +import re import sys + import libcamera -import re + +from . import camera_options +from .exif import option_to_exif_orientation +from .__version__ import __version__ from .camera import init_camera + +MAX_WIDTH = 1920 +MAX_HEIGHT = 1920 + + def main(args=None): """Entry point for hello cli. @@ -15,6 +26,8 @@ def main(args=None): becomes sys.exit(main()). The __main__ entry point similarly wraps sys.exit(). """ + logging.info(f"Spyglass {__version__}") + if args is None: args = sys.argv[1:] @@ -25,16 +38,30 @@ def main(args=None): width, height = split_resolution(parsed_args.resolution) stream_url = parsed_args.stream_url snapshot_url = parsed_args.snapshot_url - - cam = init_camera(width, - height, - parsed_args.fps, - parse_autofocus(parsed_args.autofocus), - parsed_args.lensposition, - parse_autofocus_speed(parsed_args.autofocusspeed), - parsed_args.camera_num) + orientation_exif = parsed_args.orientation_exif + controls = parsed_args.controls + if parsed_args.controls_string: + controls += [c.split('=') for c in parsed_args.controls_string.split(',')] + if parsed_args.list_controls: + print('Available controls:\n'+camera_options.get_libcamera_controls_string(0)) + return + + cam = init_camera( + parsed_args.camera_num, + width, + height, + parsed_args.fps, + parse_autofocus(parsed_args.autofocus), + parsed_args.lensposition, + parse_autofocus_speed(parsed_args.autofocusspeed), + parsed_args.upsidedown, + parsed_args.flip_horizontal, + parsed_args.flip_vertical, + parsed_args.controls, + parsed_args.tuning_filter, + parsed_args.tuning_filter_dir) try: - cam.start_and_run_server(bind_address, port, stream_url, snapshot_url) + cam.start_and_run_server(bind_address, port, stream_url, snapshot_url, orientation_exif) finally: cam.stop() @@ -47,12 +74,20 @@ def resolution_type(arg_value, pat=re.compile(r"^\d+x\d+$")): return arg_value +def orientation_type(arg_value): + if arg_value in option_to_exif_orientation: + return option_to_exif_orientation[arg_value] + else: + raise argparse.ArgumentTypeError(f"invalid value: unknown orientation {arg_value}.") + + def parse_autofocus(arg_value): if arg_value == 'manual': return libcamera.controls.AfModeEnum.Manual elif arg_value == 'continuous': return libcamera.controls.AfModeEnum.Continuous - raise argparse.ArgumentTypeError("invalid value: manual or continuous expected.") + else: + raise argparse.ArgumentTypeError("invalid value: manual or continuous expected.") def parse_autofocus_speed(arg_value): @@ -60,16 +95,26 @@ def parse_autofocus_speed(arg_value): return libcamera.controls.AfSpeedEnum.Normal elif arg_value == 'fast': return libcamera.controls.AfSpeedEnum.Fast - raise argparse.ArgumentTypeError("invalid value: normal or fast expected.") + else: + raise argparse.ArgumentTypeError("invalid value: normal or fast expected.") def split_resolution(res): parts = res.split('x') w = int(parts[0]) h = int(parts[1]) + if w > MAX_WIDTH or h > MAX_HEIGHT: + raise argparse.ArgumentTypeError("Maximum supported resolution is 1920x1920") return w, h +def control_type(arg_value: str): + if '=' in arg_value: + return arg_value.split('=') + else: + raise argparse.ArgumentTypeError(f"Invalid control: Missing value: {arg_value}") + + # endregion args parsers @@ -92,7 +137,7 @@ def get_parser(): 'connections') parser.add_argument('-p', '--port', type=int, default=8080, help='Bind to port for incoming connections') parser.add_argument('-r', '--resolution', type=resolution_type, default='640x480', - help='Resolution of the images width x height') + help='Resolution of the images width x height. Maximum is 1920x1920.') parser.add_argument('-f', '--fps', type=int, default=15, help='Frames per second to capture') parser.add_argument('-st', '--stream_url', type=str, default='/stream', help='Sets the URL for the mjpeg stream') @@ -105,6 +150,36 @@ def get_parser(): 'Only used with Autofocus manual') parser.add_argument('-s', '--autofocusspeed', type=str, default='normal', choices=['normal', 'fast'], help='Autofocus speed. Only used with Autofocus continuous') + parser.add_argument('-ud', '--upsidedown', action='store_true', + help='Rotate the image by 180° (sensor level)') + parser.add_argument('-fh', '--flip_horizontal', action='store_true', + help='Mirror the image horizontally (sensor level)') + parser.add_argument('-fv', '--flip_vertical', action='store_true', + help='Mirror the image vertically (sensor level)') + parser.add_argument('-or', '--orientation_exif', type=orientation_type, default='h', + help='Set the image orientation using an EXIF header:\n' + ' h - Horizontal (normal)\n' + ' mh - Mirror horizontal\n' + ' r180 - Rotate 180\n' + ' mv - Mirror vertical\n' + ' mhr270 - Mirror horizontal and rotate 270 CW\n' + ' r90 - Rotate 90 CW\n' + ' mhr90 - Mirror horizontal and rotate 90 CW\n' + ' r270 - Rotate 270 CW' + ) + parser.add_argument('-c', '--controls', default=[], type=control_type, action='extend', nargs='*', + help='Define camera controls to start with spyglass. ' + 'Can be used multiple times.\n' + 'Format: =') + parser.add_argument('-cs', '--controls-string', default='', type=str, + help='Define camera controls to start with spyglass. ' + 'Input as a long string.\n' + 'Format: = =') + parser.add_argument('-tf', '--tuning_filter', type=str, default=None, nargs='?', const="", + help='Set a tuning filter file name.') + parser.add_argument('-tfd', '--tuning_filter_dir', type=str, default=None, nargs='?',const="", + help='Set the directory to look for tuning filters.') + parser.add_argument('--list-controls', action='store_true', help='List available camera controls and exits.') parser.add_argument('-n', '--camera_num', type=int, default=0, help='Camera number to be used') return parser diff --git a/spyglass/exif.py b/spyglass/exif.py new file mode 100644 index 0000000..3c1fa74 --- /dev/null +++ b/spyglass/exif.py @@ -0,0 +1,47 @@ +def create_exif_header(orientation: int): + if orientation <= 0: + return None + + return b''.join([ + b'\xFF\xD8', # Start of Image (SOI) marker + b'\xFF\xE1', # APP1 marker + b'\x00\x62', # Length of APP 1 segment (98 bytes) + b'\x45\x78\x69\x66', # EXIF identifier ("Exif" in ASCII) + b'\x00\x00', # Padding bytes + # TIFF header (with big-endian indicator) + b'\x4D\x4D', # Big endian + b'\x00\x2A', # TIFF magic number + b'\x00\x00\x00\x08', # Offset to first IFD (8 bytes) + # Image File Directory (IFD) + b'\x00\x05', # Number of entries in the IFD (5) + # v-- Orientation tag (tag number = 0x0112, type = USHORT, count = 1) + b'\x01\x12', b'\x00\x03', b'\x00\x00\x00\x01', + b'\x00', orientation.to_bytes(1, 'big'), b'\x00\x00', # Tag data + # v-- XResolution tag (tag number = 0x011A, type = UNSIGNED RATIONAL, count = 1) + b'\x01\x1A', b'\x00\x05', b'\x00\x00\x00\x01', + b'\x00\x00\x00\x4A', # Tag data (address) + # v-- YResolution tag (tag number = 0x011B, type = UNSIGNED RATIONAL, count = 1) + b'\x01\x1B', b'\x00\x05', b'\x00\x00\x00\x01', + b'\x00\x00\x00\x52', # Tag data (address) + # v-- ResolutionUnit tag (tag number = 0x0128, type = USHORT, count = 1) + b'\x01\x28', b'\x00\x03', b'\x00\x00\x00\x01', + b'\x00\x02\x00\x00', # 2 - Inch + # v-- YCbCrPositioning tag (tag number = 0x0213, type = USHORT, count = 1) + b'\x02\x13', b'\x00\x03', b'\x00\x00\x00\x01', + b'\x00\x01\x00\x00', # center of pixel array + b'\x00\x00\x00\x00', # Offset to next IFD 0 + b'\x00\x00\x00\x48\x00\x00\x00\x01', # XResolution value + b'\x00\x00\x00\x48\x00\x00\x00\x01' # YResolution value + ]) + + +option_to_exif_orientation = { + 'h': 1, + 'mh': 2, + 'r180': 3, + 'mv': 4, + 'mhr270': 5, + 'r90': 6, + 'mhr90': 7, + 'r270': 8 +} diff --git a/spyglass/server.py b/spyglass/server.py index 64619a1..67ac25a 100755 --- a/spyglass/server.py +++ b/spyglass/server.py @@ -3,22 +3,88 @@ import logging import socketserver from http import server -from . import logger +import io +import logging +import socketserver +from http import server +from threading import Condition +from spyglass.url_parsing import check_urls_match, get_url_params +from spyglass.exif import create_exif_header +from spyglass.camera_options import parse_dictionary_to_html_page, process_controls class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer): allow_reuse_address = True daemon_threads = True class StreamingHandler(server.BaseHTTPRequestHandler): - output = None - stream_url = '' - snapshot_url = '' + @property + def output(self): + return self._output + + @output.setter + def output(self, value): + self._output = value + + @property + def picam2(self): + return self._picam2 + + @picam2.setter + def picam2(self, value): + self._picam2 = value + + @property + def stream_url(self): + try: + return self._stream_url + except NameError: + return '/stream' + + @stream_url.setter + def stream_url(self, value): + self._stream_url = value + + @property + def snapshot_url(self): + try: + return self._snapshot_url + except NameError: + return '/snapshot' + + @snapshot_url.setter + def snapshot_url(self, value): + self._snapshot_url = value + + @property + def exif_header(self): + try: + return self._exif_header + except NameError: + return None + + @exif_header.setter + def exif_header(self, orientation_exif): + if orientation_exif > 0: + self._exif_header = create_exif_header(orientation_exif) + else: + self._exif_header = None def do_GET(self): - if self.path == self.stream_url: + if check_urls_match(self.stream_url, self.path): self.start_streaming() - elif self.path == self.snapshot_url: + elif check_urls_match(self.snapshot_url, self.path): self.send_snapshot() + elif check_urls_match('/controls', self.path): + parsed_controls = get_url_params(self.path) + parsed_controls = parsed_controls if parsed_controls else None + processed_controls = process_controls(self.picam2, parsed_controls) + self.picam2.set_controls(processed_controls) + content = parse_dictionary_to_html_page(self.picam2, parsed_controls, processed_controls).encode('utf-8') + self.send_response(200) + self.send_header('Content-Type', 'text/html') + self.send_header('Content-Length', len(content)) + self.end_headers() + self.wfile.write(content) else: self.send_error(404) self.end_headers() @@ -32,10 +98,17 @@ def start_streaming(self): while True: frame = self.get_frame() self.wfile.write(b'--FRAME\r\n') - self.send_jpeg_content_headers(frame) - self.end_headers() - self.wfile.write(frame) - self.wfile.write(b'\r\n') + if self.exif_header is None: + self.send_jpeg_content_headers(frame) + self.end_headers() + self.wfile.write(frame) + self.wfile.write(b'\r\n') + else: + self.send_jpeg_content_headers(frame, len(self.exif_header) - 2) + self.end_headers() + self.wfile.write(self.exif_header) + self.wfile.write(frame[2:]) + self.wfile.write(b'\r\n') except Exception as e: logging.warning('Removed streaming client %s: %s', self.client_address, str(e)) @@ -44,9 +117,15 @@ def send_snapshot(self): self.send_response(200) self.send_default_headers() frame = self.get_frame() - self.send_jpeg_content_headers(frame) - self.end_headers() - self.wfile.write(frame) + if self.exif_header is None: + self.send_jpeg_content_headers(frame) + self.end_headers() + self.wfile.write(frame) + else: + self.send_jpeg_content_headers(frame, len(self.exif_header) - 2) + self.end_headers() + self.wfile.write(self.exif_header) + self.wfile.write(frame[2:]) except Exception as e: logging.warning( 'Removed client %s: %s', @@ -57,23 +136,11 @@ def send_default_headers(self): self.send_header('Cache-Control', 'no-cache, private') self.send_header('Pragma', 'no-cache') - def send_jpeg_content_headers(self, frame): + def send_jpeg_content_headers(self, frame, extra_len=0): self.send_header('Content-Type', 'image/jpeg') - self.send_header('Content-Length', len(frame)) + self.send_header('Content-Length', str(len(frame) + extra_len)) def get_frame(self): with self.output.condition: self.output.condition.wait() return self.output.frame - -def run_server(bind_address, port, output, stream_url='/stream', snapshot_url='/snapshot'): - logger.info('Server listening on %s:%d', bind_address, port) - logger.info('Streaming endpoint: %s', stream_url) - logger.info('Snapshot endpoint: %s', snapshot_url) - address = (bind_address, port) - streaming_handler = StreamingHandler - streaming_handler.output = output - streaming_handler.stream_url = stream_url - streaming_handler.snapshot_url = snapshot_url - current_server = StreamingServer(address, streaming_handler) - current_server.serve_forever() diff --git a/spyglass/url_parsing.py b/spyglass/url_parsing.py new file mode 100644 index 0000000..3c07126 --- /dev/null +++ b/spyglass/url_parsing.py @@ -0,0 +1,49 @@ +from urllib.parse import urlparse, parse_qsl + +def check_paths_match(expected_url, incoming_url): + # Assign paths from URL into list + exp_paths = urlparse(expected_url.strip("/")).path.split("/") + inc_paths = urlparse(incoming_url.strip("/")).path.split("/") + + # Drop ip/hostname if present in path + if '.' in exp_paths[0]: exp_paths.pop(0) + if '.' in inc_paths[0]: inc_paths.pop(0) + + # Filter out empty strings + # This allows e.g. /stream/?action=stream for /stream?action=stream + exp_paths = list(filter(None, exp_paths)) + inc_paths = list(filter(None, inc_paths)) + + # Determine if match + if len(exp_paths)==len(inc_paths): + return all([exp == inc for exp, inc in zip(exp_paths, inc_paths)]) + + return False + +def get_url_params(url): + # Get URL params + params = parse_qsl(urlparse(url).query) + + return params + +def check_params_match(expected_url, incoming_url): + # Check URL params + exp_params = get_url_params(expected_url) + inc_params = get_url_params(incoming_url) + + # Create list of matching params + matching_params = set(exp_params) & set(inc_params) + + # Update list order for expected params + exp_params = set(exp_params) + + return matching_params==exp_params + +def check_urls_match(expected_url, incoming_url): + # Check URL paths + paths_match = check_paths_match(expected_url, incoming_url) + + # Check URL params + params_match = check_params_match(expected_url, incoming_url) + + return paths_match and params_match diff --git a/tests/test_cli.py b/tests/test_cli.py new file mode 100644 index 0000000..a0a3a4f --- /dev/null +++ b/tests/test_cli.py @@ -0,0 +1,474 @@ +import argparse +import pytest +from unittest.mock import MagicMock, ANY, patch + +AF_SPEED_ENUM_NORMAL = 1 +AF_SPEED_ENUM_FAST = 2 +AF_MODE_ENUM_CONTINUOUS = 2 +AF_MODE_ENUM_MANUAL = 3 + +DEFAULT_HEIGHT = 480 +DEFAULT_WIDTH = 640 +DEFAULT_FLIP_VERTICALLY = False +DEFAULT_FLIP_HORIZONTALLY = False +DEFAULT_UPSIDE_DOWN = False +DEFAULT_LENS_POSITION = 0.0 +DEFAULT_FPS = 15 +DEFAULT_AF_SPEED = AF_SPEED_ENUM_NORMAL +DEFAULT_AUTOFOCUS_MODE = AF_MODE_ENUM_CONTINUOUS +DEFAULT_CONTROLS = [] +DEFAULT_TUNING_FILTER = None +DEFAULT_TUNING_FILTER_DIR = None + + +@pytest.fixture(autouse=True) +def mock_libraries(mocker): + mock_libcamera = MagicMock() + mock_picamera2 = MagicMock() + mock_picamera2_encoders = MagicMock() + mock_picamera2_outputs = MagicMock() + mocker.patch.dict('sys.modules', { + 'libcamera': mock_libcamera, + 'picamera2': mock_picamera2, + 'picamera2.encoders': mock_picamera2_encoders, + 'picamera2.outputs': mock_picamera2_outputs, + }) + mocker.patch('libcamera.controls.AfModeEnum.Manual', AF_MODE_ENUM_MANUAL) + mocker.patch('libcamera.controls.AfModeEnum.Continuous', AF_MODE_ENUM_CONTINUOUS) + mocker.patch('libcamera.controls.AfSpeedEnum.Normal', AF_SPEED_ENUM_NORMAL) + mocker.patch('libcamera.controls.AfSpeedEnum.Fast', AF_SPEED_ENUM_FAST) + + +def test_parse_bindaddress(): + from spyglass import cli + args = cli.get_args(['-b', '1.2.3.4']) + assert args.bindaddress == '1.2.3.4' + + +def test_parse_port(): + from spyglass import cli + args = cli.get_args(['-p', '123']) + assert args.port == 123 + + +def test_parse_resolution(): + from spyglass import cli + args = cli.get_args(['-r', '100x200']) + assert args.resolution == '100x200' + + +def test_split_resolution(): + from spyglass import cli + (width, height) = cli.split_resolution('100x200') + assert width == 100 + assert height == 200 + + +def test_parse_tuning_filter(): + from spyglass import cli + args = cli.get_args(['-tf', 'filter']) + assert args.tuning_filter == 'filter' + + +def test_parse_tuning_filter_dir(): + from spyglass import cli + args = cli.get_args(['-tfd', 'dir']) + assert args.tuning_filter_dir == 'dir' + + +@patch("spyglass.server.run_server") +@patch("spyglass.camera.init_camera") +def test_init_camera_with_defaults(mock_spyglass_camera, mock_spyglass_server): + from spyglass import cli + import spyglass.camera + cli.main(args=[]) + spyglass.camera.init_camera.assert_called_once_with( + DEFAULT_WIDTH, + DEFAULT_HEIGHT, + DEFAULT_FPS, + DEFAULT_AUTOFOCUS_MODE, + DEFAULT_LENS_POSITION, + DEFAULT_AF_SPEED, + DEFAULT_UPSIDE_DOWN, + DEFAULT_FLIP_HORIZONTALLY, + DEFAULT_FLIP_VERTICALLY, + DEFAULT_CONTROLS, + DEFAULT_TUNING_FILTER, + DEFAULT_TUNING_FILTER_DIR + ) + + +@patch("spyglass.server.run_server") +@patch("spyglass.camera.init_camera") +def test_init_camera_resolution(mock_spyglass_server, mock_spyglass_camera): + from spyglass import cli + import spyglass.camera + cli.main(args=[ + '-r', '200x100' + ]) + spyglass.camera.init_camera.assert_called_once_with( + 200, + 100, + DEFAULT_FPS, + DEFAULT_AUTOFOCUS_MODE, + DEFAULT_LENS_POSITION, + DEFAULT_AF_SPEED, + DEFAULT_UPSIDE_DOWN, + DEFAULT_FLIP_HORIZONTALLY, + DEFAULT_FLIP_VERTICALLY, + DEFAULT_CONTROLS, + DEFAULT_TUNING_FILTER, + DEFAULT_TUNING_FILTER_DIR + ) + + +def test_raise_error_when_width_greater_than_maximum(): + from spyglass import cli + with pytest.raises(argparse.ArgumentTypeError): + cli.main(args=[ + '-r', '1921x1920' + ]) + + +def test_raise_error_when_height_greater_than_maximum(): + from spyglass import cli + with pytest.raises(argparse.ArgumentTypeError): + cli.main(args=[ + '-r', '1920x1921' + ]) + + +@patch("spyglass.server.run_server") +@patch("spyglass.camera.init_camera") +def test_init_camera_fps(mock_spyglass_server, mock_spyglass_camera): + from spyglass import cli + import spyglass.camera + cli.main(args=[ + '-f', '20' + ]) + spyglass.camera.init_camera.assert_called_once_with( + DEFAULT_WIDTH, + DEFAULT_HEIGHT, + 20, + DEFAULT_AUTOFOCUS_MODE, + DEFAULT_LENS_POSITION, + DEFAULT_AF_SPEED, + DEFAULT_UPSIDE_DOWN, + DEFAULT_FLIP_HORIZONTALLY, + DEFAULT_FLIP_VERTICALLY, + DEFAULT_CONTROLS, + DEFAULT_TUNING_FILTER, + DEFAULT_TUNING_FILTER_DIR + ) + + +@patch("spyglass.server.run_server") +@patch("spyglass.camera.init_camera") +def test_init_camera_af_manual(mock_spyglass_server, mock_spyglass_camera): + from spyglass import cli + import spyglass.camera + cli.main(args=[ + '-af', 'manual' + ]) + spyglass.camera.init_camera.assert_called_once_with( + DEFAULT_WIDTH, + DEFAULT_HEIGHT, + DEFAULT_FPS, + AF_MODE_ENUM_MANUAL, + DEFAULT_LENS_POSITION, + DEFAULT_AF_SPEED, + DEFAULT_UPSIDE_DOWN, + DEFAULT_FLIP_HORIZONTALLY, + DEFAULT_FLIP_VERTICALLY, + DEFAULT_CONTROLS, + DEFAULT_TUNING_FILTER, + DEFAULT_TUNING_FILTER_DIR + ) + + +@patch("spyglass.server.run_server") +@patch("spyglass.camera.init_camera") +def test_init_camera_af_continuous(mock_spyglass_server, mock_spyglass_camera): + from spyglass import cli + import spyglass.camera + cli.main(args=[ + '-af', 'continuous' + ]) + spyglass.camera.init_camera.assert_called_once_with( + DEFAULT_WIDTH, + DEFAULT_HEIGHT, + DEFAULT_FPS, + AF_MODE_ENUM_CONTINUOUS, + DEFAULT_LENS_POSITION, + DEFAULT_AF_SPEED, + DEFAULT_UPSIDE_DOWN, + DEFAULT_FLIP_HORIZONTALLY, + DEFAULT_FLIP_VERTICALLY, + DEFAULT_CONTROLS, + DEFAULT_TUNING_FILTER, + DEFAULT_TUNING_FILTER_DIR + ) + + +@patch("spyglass.server.run_server") +@patch("spyglass.camera.init_camera") +def test_init_camera_lens_position(mock_spyglass_server, mock_spyglass_camera): + from spyglass import cli + import spyglass.camera + cli.main(args=[ + '-l', '1.0' + ]) + spyglass.camera.init_camera.assert_called_once_with( + DEFAULT_WIDTH, + DEFAULT_HEIGHT, + DEFAULT_FPS, + DEFAULT_AUTOFOCUS_MODE, + 1.0, + DEFAULT_AF_SPEED, + DEFAULT_UPSIDE_DOWN, + DEFAULT_FLIP_HORIZONTALLY, + DEFAULT_FLIP_VERTICALLY, + DEFAULT_CONTROLS, + DEFAULT_TUNING_FILTER, + DEFAULT_TUNING_FILTER_DIR + ) + + +@patch("spyglass.server.run_server") +@patch("spyglass.camera.init_camera") +def test_init_camera_af_speed_normal(mock_spyglass_server, mock_spyglass_camera): + from spyglass import cli + import spyglass.camera + cli.main(args=[ + '-s', 'normal' + ]) + spyglass.camera.init_camera.assert_called_once_with( + DEFAULT_WIDTH, + DEFAULT_HEIGHT, + DEFAULT_FPS, + DEFAULT_AUTOFOCUS_MODE, + DEFAULT_LENS_POSITION, + AF_SPEED_ENUM_NORMAL, + DEFAULT_UPSIDE_DOWN, + DEFAULT_FLIP_HORIZONTALLY, + DEFAULT_FLIP_VERTICALLY, + DEFAULT_CONTROLS, + DEFAULT_TUNING_FILTER, + DEFAULT_TUNING_FILTER_DIR + ) + + +@patch("spyglass.server.run_server") +@patch("spyglass.camera.init_camera") +def test_init_camera_af_speed_fast(mock_spyglass_server, mock_spyglass_camera): + from spyglass import cli + import spyglass.camera + cli.main(args=[ + '-s', 'fast' + ]) + spyglass.camera.init_camera.assert_called_once_with( + DEFAULT_WIDTH, + DEFAULT_HEIGHT, + DEFAULT_FPS, + DEFAULT_AUTOFOCUS_MODE, + DEFAULT_LENS_POSITION, + AF_SPEED_ENUM_FAST, + DEFAULT_UPSIDE_DOWN, + DEFAULT_FLIP_HORIZONTALLY, + DEFAULT_FLIP_VERTICALLY, + DEFAULT_CONTROLS, + DEFAULT_TUNING_FILTER, + DEFAULT_TUNING_FILTER_DIR + ) + + +@patch("spyglass.server.run_server") +@patch("spyglass.camera.init_camera") +def test_init_camera_upside_down(mock_spyglass_server, mock_spyglass_camera): + from spyglass import cli + import spyglass.camera + cli.main(args=[ + '-ud' + ]) + spyglass.camera.init_camera.assert_called_once_with( + DEFAULT_WIDTH, + DEFAULT_HEIGHT, + DEFAULT_FPS, + DEFAULT_AUTOFOCUS_MODE, + DEFAULT_LENS_POSITION, + DEFAULT_AF_SPEED, + True, + DEFAULT_FLIP_HORIZONTALLY, + DEFAULT_FLIP_VERTICALLY, + DEFAULT_CONTROLS, + DEFAULT_TUNING_FILTER, + DEFAULT_TUNING_FILTER_DIR + ) + + +@patch("spyglass.server.run_server") +@patch("spyglass.camera.init_camera") +def test_init_camera_flip_horizontal(mock_spyglass_server, mock_spyglass_camera): + from spyglass import cli + import spyglass.camera + cli.main(args=[ + '-fh' + ]) + spyglass.camera.init_camera.assert_called_once_with( + DEFAULT_WIDTH, + DEFAULT_HEIGHT, + DEFAULT_FPS, + DEFAULT_AUTOFOCUS_MODE, + DEFAULT_LENS_POSITION, + DEFAULT_AF_SPEED, + DEFAULT_UPSIDE_DOWN, + True, + DEFAULT_FLIP_VERTICALLY, + DEFAULT_CONTROLS, + DEFAULT_TUNING_FILTER, + DEFAULT_TUNING_FILTER_DIR + ) + + +@patch("spyglass.server.run_server") +@patch("spyglass.camera.init_camera") +def test_init_camera_flip_vertical(mock_spyglass_server, mock_spyglass_camera): + from spyglass import cli + import spyglass.camera + cli.main(args=[ + '-fv' + ]) + spyglass.camera.init_camera.assert_called_once_with( + DEFAULT_WIDTH, + DEFAULT_HEIGHT, + DEFAULT_FPS, + DEFAULT_AUTOFOCUS_MODE, + DEFAULT_LENS_POSITION, + DEFAULT_AF_SPEED, + DEFAULT_UPSIDE_DOWN, + DEFAULT_FLIP_HORIZONTALLY, + True, + DEFAULT_CONTROLS, + DEFAULT_TUNING_FILTER, + DEFAULT_TUNING_FILTER_DIR + ) + +@patch("spyglass.server.run_server") +@patch("spyglass.camera.init_camera") +def test_init_camera_controls(mock_spyglass_server, mock_spyglass_camera): + from spyglass import cli + import spyglass.camera + cli.main(args=[ + '-c', 'brightness=-0.4', + '-c', 'awbenable=false' + ]) + spyglass.camera.init_camera.assert_called_once_with( + DEFAULT_WIDTH, + DEFAULT_HEIGHT, + DEFAULT_FPS, + DEFAULT_AUTOFOCUS_MODE, + DEFAULT_LENS_POSITION, + DEFAULT_AF_SPEED, + DEFAULT_UPSIDE_DOWN, + DEFAULT_FLIP_HORIZONTALLY, + DEFAULT_FLIP_VERTICALLY, + [['brightness', '-0.4'],['awbenable', 'false']], + DEFAULT_TUNING_FILTER, + DEFAULT_TUNING_FILTER_DIR + ) + + +@patch("spyglass.server.run_server") +@patch("spyglass.camera.init_camera") +def test_run_server_with_configuration_from_arguments(mock_spyglass_server, mock_spyglass_camera): + from spyglass import cli + import spyglass.server + cli.main(args=[ + '-b', '1.2.3.4', + '-p', '1234', + '-st', 'streaming-url', + '-sn', 'snapshot-url', + '-or', 'h' + ]) + spyglass.server.run_server.assert_called_once_with('1.2.3.4', 1234, ANY, ANY, 'streaming-url', 'snapshot-url', 1) + + +@patch("spyglass.server.run_server") +@patch("spyglass.camera.init_camera") +@pytest.mark.parametrize("input_value, expected_output", [ + ('h', 1), + ('mh', 2), + ('r180', 3), + ('mv', 4), + ('mhr270', 5), + ('r90', 6), + ('mhr90', 7), + ('r270', 8), +]) +def test_run_server_with_orientation(mock_spyglass_server, mock_spyglass_camera, input_value, expected_output): + from spyglass import cli + import spyglass.server + cli.main(args=[ + '-b', '1.2.3.4', + '-p', '1234', + '-st', 'streaming-url', + '-sn', 'snapshot-url', + '-or', input_value + ]) + spyglass.server.run_server.assert_called_once_with( + '1.2.3.4', + 1234, + ANY, + ANY, + 'streaming-url', + 'snapshot-url', + expected_output + ) + + +@patch("spyglass.server.run_server") +@patch("spyglass.camera.init_camera") +def test_init_camera_using_only_tuning_filter_file(mock_spyglass_server, mock_spyglass_camera): + from spyglass import cli + import spyglass.camera + cli.main(args=[ + '-tf', 'test', + ]) + spyglass.camera.init_camera.assert_called_once_with( + DEFAULT_WIDTH, + DEFAULT_HEIGHT, + DEFAULT_FPS, + DEFAULT_AUTOFOCUS_MODE, + DEFAULT_LENS_POSITION, + DEFAULT_AF_SPEED, + DEFAULT_UPSIDE_DOWN, + DEFAULT_FLIP_HORIZONTALLY, + DEFAULT_FLIP_VERTICALLY, + DEFAULT_CONTROLS, + "test", + DEFAULT_TUNING_FILTER_DIR + ) + +@patch("spyglass.server.run_server") +@patch("spyglass.camera.init_camera") +def test_init_camera_using_tuning_filters(mock_spyglass_server, mock_spyglass_camera): + from spyglass import cli + import spyglass.camera + cli.main(args=[ + '-tf', 'test', + '-tfd', 'test-dir', + ]) + spyglass.camera.init_camera.assert_called_once_with( + DEFAULT_WIDTH, + DEFAULT_HEIGHT, + DEFAULT_FPS, + DEFAULT_AUTOFOCUS_MODE, + DEFAULT_LENS_POSITION, + DEFAULT_AF_SPEED, + DEFAULT_UPSIDE_DOWN, + DEFAULT_FLIP_HORIZONTALLY, + DEFAULT_FLIP_VERTICALLY, + DEFAULT_CONTROLS, + "test", + "test-dir" + ) diff --git a/tests/test_exif.py b/tests/test_exif.py new file mode 100644 index 0000000..1650e60 --- /dev/null +++ b/tests/test_exif.py @@ -0,0 +1,17 @@ +import pytest + + +@pytest.mark.parametrize("input_value, expected_output", [ + ('h', 1), + ('mh', 2), + ('r180', 3), + ('mv', 4), + ('mhr270', 5), + ('r90', 6), + ('mhr90', 7), + ('r270', 8), +]) +def test_option_to_exif_orientation_map(input_value, expected_output): + from spyglass.exif import option_to_exif_orientation + orientation_value = option_to_exif_orientation[input_value] + assert orientation_value == expected_output diff --git a/tests/test_url_parsing.py b/tests/test_url_parsing.py new file mode 100644 index 0000000..5fb08ac --- /dev/null +++ b/tests/test_url_parsing.py @@ -0,0 +1,137 @@ +import pytest + + +@pytest.mark.parametrize("expected_url, incoming_url, expected_output", [ + ('/?a=b', '/?a=b', True), + ('/?a=b', '/?b=a', True), + ('/?a=b', '/?a=b&', True), + ('/?a=b', '/?b=c&d=e', True), + ('/?a=b', '/?a=b&c=d', True), + ('/?a=b', '/?c=d&a=b', True), + ('/?a=b&c=d', '/?a=b', True), + ('/?a=b&c=d', '/?a=b&c=d', True), + ('/?a=b&c=d', '/?c=d&a=b', True), + ('/?a=b&c=d', '/?d=e&a=b', True), + + ('/?a=b', '/a?a=b', False), + ('/?a=b', '/a?b=a', False), + ('/?a=b', '/a?b=c&d=e', False), + ('/?a=b&c=d', '/a?a=b', False), + ('/?a=b&c=d', '/a?a=b&c=d', False), + ('/?a=b&c=d', '/a?c=d&a=b', False), + + ('/a', '/a', True), + ('/a', '/b', False), + ('/a', '/a?b=c', True), + ('/a', '/a/?b=c', True), + ('/a', '/b/?b=c', False), + ('/a', '/?a=b', False), + + ('/a?a=b', '/a?a=b', True), + ('/a?a=b', '/a?b=a', True), + ('/a?a=b', '/a?b=c&d=e', True), + ('/a?a=b&c=d', '/a?a=b', True), + ('/a?a=b&c=d', '/a?a=b&c=d', True), + ('/a?a=b&c=d', '/a?c=d&a=b', True), + + ('/a?a=b', '/b?a=b', False), + ('/a?a=b', '/b?b=a', False), + ('/a?a=b', '/b?b=c&d=e', False), + ('/a?a=b&c=d', '/b?a=b', False), + ('/a?a=b&c=d', '/b?a=b&c=d', False), + ('/a?a=b&c=d', '/b?c=d&a=b', False) +]) +def test_check_paths_match(expected_url, incoming_url, expected_output): + from spyglass.url_parsing import check_paths_match + match_value = check_paths_match(expected_url, incoming_url) + assert match_value == expected_output + +@pytest.mark.parametrize("expected_url, incoming_url, expected_output", [ + ('/?a=b', '/?a=b', True), + ('/?a=b', '/?b=a', False), + ('/?a=b', '/?a=b&', True), + ('/?a=b', '/?b=c&d=e', False), + ('/?a=b', '/?a=b&c=d', True), + ('/?a=b', '/?c=d&a=b', True), + ('/?a=b&c=d', '/?a=b', False), + ('/?a=b&c=d', '/?a=b&c=d', True), + ('/?a=b&c=d', '/?c=d&a=b', True), + ('/?a=b&c=d', '/?d=e&a=b', False), + + ('/?a=b', '/a?a=b', True), + ('/?a=b', '/a?b=a', False), + ('/?a=b', '/a?b=c&d=e', False), + ('/?a=b&c=d', '/a?a=b', False), + ('/?a=b&c=d', '/a?a=b&c=d', True), + ('/?a=b&c=d', '/a?c=d&a=b', True), + + ('/a', '/a', True), + ('/a', '/b', True), + ('/a', '/a?b=c', True), + ('/a', '/a/?b=c', True), + ('/a', '/b/?b=c', True), + ('/a', '/?a=b', True), + + ('/a?a=b', '/a?a=b', True), + ('/a?a=b', '/a?b=a', False), + ('/a?a=b', '/a?b=c&d=e', False), + ('/a?a=b&c=d', '/a?a=b', False), + ('/a?a=b&c=d', '/a?a=b&c=d', True), + ('/a?a=b&c=d', '/a?c=d&a=b', True), + + ('/a?a=b', '/b?a=b', True), + ('/a?a=b', '/b?b=a', False), + ('/a?a=b', '/b?b=c&d=e', False), + ('/a?a=b&c=d', '/b?a=b', False), + ('/a?a=b&c=d', '/b?a=b&c=d', True), + ('/a?a=b&c=d', '/b?c=d&a=b', True) +]) +def test_check_params_match(expected_url, incoming_url, expected_output): + from spyglass.url_parsing import check_params_match + match_value = check_params_match(expected_url, incoming_url) + assert match_value == expected_output + +@pytest.mark.parametrize("expected_url, incoming_url, expected_output", [ + ('/?a=b', '/?a=b', True), + ('/?a=b', '/?b=a', False), + ('/?a=b', '/?a=b&', True), + ('/?a=b', '/?b=c&d=e', False), + ('/?a=b', '/?a=b&c=d', True), + ('/?a=b', '/?c=d&a=b', True), + ('/?a=b&c=d', '/?a=b', False), + ('/?a=b&c=d', '/?a=b&c=d', True), + ('/?a=b&c=d', '/?c=d&a=b', True), + ('/?a=b&c=d', '/?d=e&a=b', False), + + ('/?a=b', '/a?a=b', False), + ('/?a=b', '/a?b=a', False), + ('/?a=b', '/a?b=c&d=e', False), + ('/?a=b&c=d', '/a?a=b', False), + ('/?a=b&c=d', '/a?a=b&c=d', False), + ('/?a=b&c=d', '/a?c=d&a=b', False), + + ('/a', '/a', True), + ('/a', '/b', False), + ('/a', '/a?b=c', True), + ('/a', '/a/?b=c', True), + ('/a', '/b/?b=c', False), + ('/a', '/?a=b', False), + + ('/a?a=b', '/a?a=b', True), + ('/a?a=b', '/a?b=a', False), + ('/a?a=b', '/a?b=c&d=e', False), + ('/a?a=b&c=d', '/a?a=b', False), + ('/a?a=b&c=d', '/a?a=b&c=d', True), + ('/a?a=b&c=d', '/a?c=d&a=b', True), + + ('/a?a=b', '/b?a=b', False), + ('/a?a=b', '/b?b=a', False), + ('/a?a=b', '/b?b=c&d=e', False), + ('/a?a=b&c=d', '/b?a=b', False), + ('/a?a=b&c=d', '/b?a=b&c=d', False), + ('/a?a=b&c=d', '/b?c=d&a=b', False) +]) +def test_check_urls_match(expected_url, incoming_url, expected_output): + from spyglass.url_parsing import check_urls_match + match_value = check_urls_match(expected_url, incoming_url) + assert match_value == expected_output From 84ff1f48a8760eef4fcc6042c671831e8309ec26 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Thu, 4 Apr 2024 14:25:32 +0200 Subject: [PATCH 04/55] fix: fix multiple things Signed-off-by: Patrick Gehrsitz --- spyglass/camera/__init__.py | 4 +-- spyglass/camera/camera.py | 22 ++++++++++---- spyglass/camera/csi.py | 15 +++++++--- spyglass/camera/usb.py | 18 ++++++------ spyglass/server.py | 57 ------------------------------------- 5 files changed, 39 insertions(+), 77 deletions(-) diff --git a/spyglass/camera/__init__.py b/spyglass/camera/__init__.py index a277d81..108704d 100644 --- a/spyglass/camera/__init__.py +++ b/spyglass/camera/__init__.py @@ -29,8 +29,8 @@ def init_camera( picam2 = Picamera2(camera_num, tuning=tuning) if picam2._is_rpi_camera(): - cam = CSI(width, height, fps, autofocus, lens_position, autofocus_speed) + cam = CSI(picam2, width, height, fps, autofocus, lens_position, autofocus_speed) else: - cam = USB(width, height, fps, autofocus, lens_position, autofocus_speed) + cam = USB(picam2, width, height, fps, autofocus, lens_position, autofocus_speed) cam.configure(control_list, upsidedown, flip_horizontal, flip_vertical) return cam diff --git a/spyglass/camera/camera.py b/spyglass/camera/camera.py index 71ba375..110e903 100644 --- a/spyglass/camera/camera.py +++ b/spyglass/camera/camera.py @@ -1,7 +1,9 @@ from abc import ABC, abstractmethod from picamera2 import Picamera2 +import libcamera from .. import logger -from .. server import StreamingServer, StreamingHandler +from ..exif import create_exif_header +from ..server import StreamingServer, StreamingHandler class Camera(ABC): def __init__(self, @@ -22,12 +24,15 @@ def __init__(self, self.autofocus_speed = autofocus_speed def create_controls(self): - controls = {'FrameRate': self.fps} + controls = {} + + if 'FrameRate' in self.picam2.camera_controls: + controls['FrameRate'] = self.fps if 'AfMode' in self.picam2.camera_controls: controls['AfMode'] = self.autofocus controls['AfSpeed'] = self.autofocus_speed - if self.autofocus == self.libcamera.controls.AfModeEnum.Manual: + if self.autofocus == libcamera.controls.AfModeEnum.Manual: controls['LensPosition'] = self.lens_position else: print('Attached camera does not support autofocus') @@ -39,6 +44,7 @@ def _run_server(self, port, output, streaming_handler: StreamingHandler, + get_frame, stream_url='/stream', snapshot_url='/snapshot', orientation_exif=0): @@ -49,18 +55,22 @@ def _run_server(self, address = (bind_address, port) streaming_handler.output = output streaming_handler.picam2 = self.picam2 + streaming_handler.get_frame = get_frame streaming_handler.stream_url = stream_url streaming_handler.snapshot_url = snapshot_url - streaming_handler.exif_header = orientation_exif + if orientation_exif > 0: + streaming_handler.exif_header = create_exif_header(orientation_exif) + else: + streaming_handler.exif_header = None current_server = StreamingServer(address, streaming_handler) current_server.serve_forever() @abstractmethod def configure(self, control_list: list[list[str]]=[], + upsidedown=False, flip_horizontal=False, - flip_vertical=False, - upsidedown=False): + flip_vertical=False): pass @abstractmethod diff --git a/spyglass/camera/csi.py b/spyglass/camera/csi.py index f5b3cf0..7270ae4 100644 --- a/spyglass/camera/csi.py +++ b/spyglass/camera/csi.py @@ -12,9 +12,9 @@ class CSI(camera.Camera): def configure(self, control_list: list[list[str]]=[], + upsidedown=False, flip_horizontal=False, - flip_vertical=False, - upsidedown=False): + flip_vertical=False): controls = self.create_controls() c = process_controls(self.picam2, [tuple(ctrl) for ctrl in control_list]) controls.update(c) @@ -38,7 +38,7 @@ def start_and_run_server(self, stream_url='/stream', snapshot_url='/snapshot', orientation_exif=0): - + class StreamingOutput(io.BufferedIOBase): def __init__(self): self.frame = None @@ -49,12 +49,19 @@ def write(self, buf): self.frame = buf self.condition.notify_all() output = StreamingOutput() + def get_frame(self): + with output.condition: + output.condition.wait() + return output.frame + self.picam2.start_recording(MJPEGEncoder(), FileOutput(output)) + self._run_server( bind_address, port, output, - StreamingHandler(), + StreamingHandler, + get_frame, stream_url=stream_url, snapshot_url=snapshot_url, orientation_exif=orientation_exif diff --git a/spyglass/camera/usb.py b/spyglass/camera/usb.py index 8a44419..e3fa8f8 100644 --- a/spyglass/camera/usb.py +++ b/spyglass/camera/usb.py @@ -2,15 +2,15 @@ from . import camera -from spyglass.server import StreamingHandler, StreamingServer +from spyglass.server import StreamingHandler from ..camera_options import process_controls class USB(camera.Camera): def configure(self, control_list: list[list[str]]=[], + upsidedown=False, flip_horizontal=False, - flip_vertical=False, - upsidedown=False): + flip_vertical=False): controls = self.create_controls() c = process_controls(self.picam2, [tuple(ctrl) for ctrl in control_list]) controls.update(c) @@ -34,16 +34,18 @@ def start_and_run_server(self, stream_url='/stream', snapshot_url='/snapshot', orientation_exif=0): - class StreamingHandlerUSB(StreamingHandler): - def get_frame(self): - #TODO: Cuts framerate in 1/n with n streams open, add some kind of buffer - return self.output.capture_buffer() + def get_frame(self2): + #TODO: Cuts framerate in 1/n with n streams open, add some kind of buffer + return self.picam2.capture_buffer() + self.picam2.start() + self._run_server( bind_address, port, self.picam2, - StreamingHandlerUSB(), + StreamingHandler, + get_frame, stream_url=stream_url, snapshot_url=snapshot_url, orientation_exif=orientation_exif diff --git a/spyglass/server.py b/spyglass/server.py index 67ac25a..22fe55e 100755 --- a/spyglass/server.py +++ b/spyglass/server.py @@ -17,58 +17,6 @@ class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer): daemon_threads = True class StreamingHandler(server.BaseHTTPRequestHandler): - @property - def output(self): - return self._output - - @output.setter - def output(self, value): - self._output = value - - @property - def picam2(self): - return self._picam2 - - @picam2.setter - def picam2(self, value): - self._picam2 = value - - @property - def stream_url(self): - try: - return self._stream_url - except NameError: - return '/stream' - - @stream_url.setter - def stream_url(self, value): - self._stream_url = value - - @property - def snapshot_url(self): - try: - return self._snapshot_url - except NameError: - return '/snapshot' - - @snapshot_url.setter - def snapshot_url(self, value): - self._snapshot_url = value - - @property - def exif_header(self): - try: - return self._exif_header - except NameError: - return None - - @exif_header.setter - def exif_header(self, orientation_exif): - if orientation_exif > 0: - self._exif_header = create_exif_header(orientation_exif) - else: - self._exif_header = None - def do_GET(self): if check_urls_match(self.stream_url, self.path): self.start_streaming() @@ -139,8 +87,3 @@ def send_default_headers(self): def send_jpeg_content_headers(self, frame, extra_len=0): self.send_header('Content-Type', 'image/jpeg') self.send_header('Content-Length', str(len(frame) + extra_len)) - - def get_frame(self): - with self.output.condition: - self.output.condition.wait() - return self.output.frame From 6e59d1f98da59c549754721a86d8e83eca4b3870 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Thu, 4 Apr 2024 14:37:07 +0200 Subject: [PATCH 05/55] chore: adjust variables for better code highlighting Signed-off-by: Patrick Gehrsitz --- spyglass/camera/camera.py | 2 -- spyglass/camera/csi.py | 1 - spyglass/camera/usb.py | 3 +-- spyglass/server.py | 8 ++++++++ 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/spyglass/camera/camera.py b/spyglass/camera/camera.py index 110e903..78a81df 100644 --- a/spyglass/camera/camera.py +++ b/spyglass/camera/camera.py @@ -42,7 +42,6 @@ def create_controls(self): def _run_server(self, bind_address, port, - output, streaming_handler: StreamingHandler, get_frame, stream_url='/stream', @@ -53,7 +52,6 @@ def _run_server(self, logger.info('Snapshot endpoint: %s', snapshot_url) logger.info('Controls endpoint: %s', '/controls') address = (bind_address, port) - streaming_handler.output = output streaming_handler.picam2 = self.picam2 streaming_handler.get_frame = get_frame streaming_handler.stream_url = stream_url diff --git a/spyglass/camera/csi.py b/spyglass/camera/csi.py index 7270ae4..ac9c1a4 100644 --- a/spyglass/camera/csi.py +++ b/spyglass/camera/csi.py @@ -59,7 +59,6 @@ def get_frame(self): self._run_server( bind_address, port, - output, StreamingHandler, get_frame, stream_url=stream_url, diff --git a/spyglass/camera/usb.py b/spyglass/camera/usb.py index e3fa8f8..7994670 100644 --- a/spyglass/camera/usb.py +++ b/spyglass/camera/usb.py @@ -34,7 +34,7 @@ def start_and_run_server(self, stream_url='/stream', snapshot_url='/snapshot', orientation_exif=0): - def get_frame(self2): + def get_frame(inner_self): #TODO: Cuts framerate in 1/n with n streams open, add some kind of buffer return self.picam2.capture_buffer() @@ -43,7 +43,6 @@ def get_frame(self2): self._run_server( bind_address, port, - self.picam2, StreamingHandler, get_frame, stream_url=stream_url, diff --git a/spyglass/server.py b/spyglass/server.py index 22fe55e..c1f46ed 100755 --- a/spyglass/server.py +++ b/spyglass/server.py @@ -17,6 +17,14 @@ class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer): daemon_threads = True class StreamingHandler(server.BaseHTTPRequestHandler): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.picam2 = None + self.exif_header = None + self.stream_url = None + self.snapshot_url = None + self.get_frame = None + def do_GET(self): if check_urls_match(self.stream_url, self.path): self.start_streaming() From 69fc5b27eb71975b0a4ddd4fb1dbf59e8fb0d1ae Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Thu, 4 Apr 2024 17:06:59 +0200 Subject: [PATCH 06/55] refactor: refactor camera code Signed-off-by: Patrick Gehrsitz --- spyglass/camera/__init__.py | 5 --- spyglass/camera/camera.py | 87 ++++++++++++++++++++----------------- spyglass/camera/csi.py | 23 ---------- spyglass/camera/usb.py | 24 +--------- spyglass/cli.py | 32 +++++++------- 5 files changed, 64 insertions(+), 107 deletions(-) diff --git a/spyglass/camera/__init__.py b/spyglass/camera/__init__.py index 108704d..c7a863d 100644 --- a/spyglass/camera/__init__.py +++ b/spyglass/camera/__init__.py @@ -12,10 +12,6 @@ def init_camera( autofocus: str, lens_position: float, autofocus_speed: str, - upsidedown=False, - flip_horizontal=False, - flip_vertical=False, - control_list: list[list[str]]=[], tuning_filter=None, tuning_filter_dir=None ) -> Camera: @@ -32,5 +28,4 @@ def init_camera( cam = CSI(picam2, width, height, fps, autofocus, lens_position, autofocus_speed) else: cam = USB(picam2, width, height, fps, autofocus, lens_position, autofocus_speed) - cam.configure(control_list, upsidedown, flip_horizontal, flip_vertical) return cam diff --git a/spyglass/camera/camera.py b/spyglass/camera/camera.py index 78a81df..d090aa2 100644 --- a/spyglass/camera/camera.py +++ b/spyglass/camera/camera.py @@ -3,50 +3,65 @@ import libcamera from .. import logger from ..exif import create_exif_header +from ..camera_options import process_controls from ..server import StreamingServer, StreamingHandler class Camera(ABC): - def __init__(self, - picam2: Picamera2, - width: int, - height: int, - fps: int, - autofocus: str, - lens_position: float, - autofocus_speed: str): - + def __init__(self, picam2: Picamera2): self.picam2 = picam2 - self.width = width - self.height = height - self.fps = fps - self.autofocus = autofocus - self.lens_position = lens_position - self.autofocus_speed = autofocus_speed - def create_controls(self): + def create_controls(self, fps: int, autofocus: str, lens_position: float, autofocus_speed: str): controls = {} if 'FrameRate' in self.picam2.camera_controls: - controls['FrameRate'] = self.fps + controls['FrameRate'] = fps if 'AfMode' in self.picam2.camera_controls: - controls['AfMode'] = self.autofocus - controls['AfSpeed'] = self.autofocus_speed - if self.autofocus == libcamera.controls.AfModeEnum.Manual: - controls['LensPosition'] = self.lens_position + controls['AfMode'] = autofocus + controls['AfSpeed'] = autofocus_speed + if autofocus == libcamera.controls.AfModeEnum.Manual: + controls['LensPosition'] = lens_position else: print('Attached camera does not support autofocus') return controls + def configure(self, + width: int, + height: int, + fps: int, + autofocus: str, + lens_position: float, + autofocus_speed: str, + control_list: list[list[str]]=[], + upsidedown=False, + flip_horizontal=False, + flip_vertical=False): + controls = self.create_controls(fps, autofocus, lens_position, autofocus_speed) + c = process_controls(self.picam2, [tuple(ctrl) for ctrl in control_list]) + controls.update(c) + + transform = libcamera.Transform( + hflip=int(flip_horizontal or upsidedown), + vflip=int(flip_vertical or upsidedown) + ) + + self.picam2.configure( + self.picam2.create_video_configuration( + main={'size': (width, height)}, + controls=controls, + transform=transform + ) + ) + def _run_server(self, - bind_address, - port, - streaming_handler: StreamingHandler, - get_frame, - stream_url='/stream', - snapshot_url='/snapshot', - orientation_exif=0): + bind_address, + port, + streaming_handler: StreamingHandler, + get_frame, + stream_url='/stream', + snapshot_url='/snapshot', + orientation_exif=0): logger.info('Server listening on %s:%d', bind_address, port) logger.info('Streaming endpoint: %s', stream_url) logger.info('Snapshot endpoint: %s', snapshot_url) @@ -63,18 +78,6 @@ def _run_server(self, current_server = StreamingServer(address, streaming_handler) current_server.serve_forever() - @abstractmethod - def configure(self, - control_list: list[list[str]]=[], - upsidedown=False, - flip_horizontal=False, - flip_vertical=False): - pass - - @abstractmethod - def stop(self): - pass - @abstractmethod def start_and_run_server(self, bind_address, @@ -83,3 +86,7 @@ def start_and_run_server(self, snapshot_url='/snapshot', orientation_exif=0): pass + + @abstractmethod + def stop(self): + pass diff --git a/spyglass/camera/csi.py b/spyglass/camera/csi.py index ac9c1a4..375253b 100644 --- a/spyglass/camera/csi.py +++ b/spyglass/camera/csi.py @@ -7,31 +7,8 @@ from . import camera from ..server import StreamingHandler -from ..camera_options import process_controls class CSI(camera.Camera): - def configure(self, - control_list: list[list[str]]=[], - upsidedown=False, - flip_horizontal=False, - flip_vertical=False): - controls = self.create_controls() - c = process_controls(self.picam2, [tuple(ctrl) for ctrl in control_list]) - controls.update(c) - - transform = libcamera.Transform( - hflip=int(flip_horizontal or upsidedown), - vflip=int(flip_vertical or upsidedown) - ) - - self.picam2.configure( - self.picam2.create_video_configuration( - main={'size': (self.width, self.height)}, - controls=controls, - transform=transform - ) - ) - def start_and_run_server(self, bind_address, port, diff --git a/spyglass/camera/usb.py b/spyglass/camera/usb.py index 7994670..0aa922b 100644 --- a/spyglass/camera/usb.py +++ b/spyglass/camera/usb.py @@ -2,32 +2,10 @@ from . import camera -from spyglass.server import StreamingHandler +from ..server import StreamingHandler from ..camera_options import process_controls class USB(camera.Camera): - def configure(self, - control_list: list[list[str]]=[], - upsidedown=False, - flip_horizontal=False, - flip_vertical=False): - controls = self.create_controls() - c = process_controls(self.picam2, [tuple(ctrl) for ctrl in control_list]) - controls.update(c) - - transform = libcamera.Transform( - hflip=int(flip_horizontal or upsidedown), - vflip=int(flip_vertical or upsidedown) - ) - - self.picam2.configure( - self.picam2.create_preview_configuration( - main={'size': (self.width, self.height), 'format': 'MJPEG'}, - controls=controls, - transform=transform - ) - ) - def start_and_run_server(self, bind_address, port, diff --git a/spyglass/cli.py b/spyglass/cli.py index 087ba45..551e59b 100644 --- a/spyglass/cli.py +++ b/spyglass/cli.py @@ -33,12 +33,7 @@ def main(args=None): parsed_args = get_args(args) - bind_address = parsed_args.bindaddress - port = parsed_args.port width, height = split_resolution(parsed_args.resolution) - stream_url = parsed_args.stream_url - snapshot_url = parsed_args.snapshot_url - orientation_exif = parsed_args.orientation_exif controls = parsed_args.controls if parsed_args.controls_string: controls += [c.split('=') for c in parsed_args.controls_string.split(',')] @@ -48,20 +43,25 @@ def main(args=None): cam = init_camera( parsed_args.camera_num, - width, - height, - parsed_args.fps, - parse_autofocus(parsed_args.autofocus), - parsed_args.lensposition, - parse_autofocus_speed(parsed_args.autofocusspeed), - parsed_args.upsidedown, - parsed_args.flip_horizontal, - parsed_args.flip_vertical, - parsed_args.controls, parsed_args.tuning_filter, parsed_args.tuning_filter_dir) + + cam.configure(width, + height, + parsed_args.fps, + parse_autofocus(parsed_args.autofocus), + parsed_args.lensposition, + parse_autofocus_speed(parsed_args.autofocusspeed), + parsed_args.controls, + parsed_args.upsidedown, + parsed_args.flip_horizontal, + parsed_args.flip_vertical,) try: - cam.start_and_run_server(bind_address, port, stream_url, snapshot_url, orientation_exif) + cam.start_and_run_server(parsed_args.bind_address, + parsed_args.port, + parsed_args.stream_url, + parsed_args.snapshot_url, + parsed_args.orientation_exif) finally: cam.stop() From aa9c5da4551aa8e06f11831e415a34f0b3f77880 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Thu, 4 Apr 2024 17:38:21 +0200 Subject: [PATCH 07/55] chore: rearrange cli arguments Signed-off-by: Patrick Gehrsitz --- spyglass/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spyglass/cli.py b/spyglass/cli.py index 551e59b..36a60e3 100644 --- a/spyglass/cli.py +++ b/spyglass/cli.py @@ -175,11 +175,11 @@ def get_parser(): help='Define camera controls to start with spyglass. ' 'Input as a long string.\n' 'Format: = =') + parser.add_argument('--list-controls', action='store_true', help='List available camera controls and exits.') parser.add_argument('-tf', '--tuning_filter', type=str, default=None, nargs='?', const="", help='Set a tuning filter file name.') parser.add_argument('-tfd', '--tuning_filter_dir', type=str, default=None, nargs='?',const="", help='Set the directory to look for tuning filters.') - parser.add_argument('--list-controls', action='store_true', help='List available camera controls and exits.') parser.add_argument('-n', '--camera_num', type=int, default=0, help='Camera number to be used') return parser From 6cacb33ab2c2240a396f9e98089aafe1889e637a Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Fri, 5 Apr 2024 20:08:36 +0200 Subject: [PATCH 08/55] fix: fix crash Signed-off-by: Patrick Gehrsitz --- spyglass/camera/__init__.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/spyglass/camera/__init__.py b/spyglass/camera/__init__.py index c7a863d..a7dc2f7 100644 --- a/spyglass/camera/__init__.py +++ b/spyglass/camera/__init__.py @@ -6,12 +6,6 @@ def init_camera( camera_num: int, - width: int, - height: int, - fps: int, - autofocus: str, - lens_position: float, - autofocus_speed: str, tuning_filter=None, tuning_filter_dir=None ) -> Camera: @@ -25,7 +19,7 @@ def init_camera( picam2 = Picamera2(camera_num, tuning=tuning) if picam2._is_rpi_camera(): - cam = CSI(picam2, width, height, fps, autofocus, lens_position, autofocus_speed) + cam = CSI(picam2) else: - cam = USB(picam2, width, height, fps, autofocus, lens_position, autofocus_speed) + cam = USB(picam2) return cam From b485dab63022d67c87b148965c02dedb98fa755d Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Fri, 5 Apr 2024 20:51:21 +0200 Subject: [PATCH 09/55] fix: fix typo Signed-off-by: Patrick Gehrsitz --- spyglass/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spyglass/cli.py b/spyglass/cli.py index 36a60e3..533d092 100644 --- a/spyglass/cli.py +++ b/spyglass/cli.py @@ -57,7 +57,7 @@ def main(args=None): parsed_args.flip_horizontal, parsed_args.flip_vertical,) try: - cam.start_and_run_server(parsed_args.bind_address, + cam.start_and_run_server(parsed_args.bindaddress, parsed_args.port, parsed_args.stream_url, parsed_args.snapshot_url, From 1726a12747a526e183cee46258d429bbb22d0def Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Fri, 14 Jun 2024 21:29:02 +0200 Subject: [PATCH 10/55] docs: update README.md with new cli parameter Signed-off-by: Patrick Gehrsitz --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a88ed19..fcaafdb 100644 --- a/README.md +++ b/README.md @@ -57,11 +57,12 @@ On startup the following arguments are supported: | `--list-controls` | List all available libcamera controls onto the console. Those can be used with `--controls` | | | `-tf`, `--tuning_filter` | Set a tuning filter file name. | | | `-tfd`, `--tuning_filter_dir` | Set the directory to look for tuning filters. | | +| `-n`, `--camera_num` | Camera number to be used. All cameras with their number can be shown with `libcamera-hello`. | `0` | Starting the server without any argument is the same as ```shell -./run.py -b 0.0.0.0 -p 8080 -r 640x480 -f 15 -st '/stream' -sn '/snapshot' -af continuous -l 0.0 -s normal +./run.py -b 0.0.0.0 -p 8080 -r 640x480 -f 15 -st '/stream' -sn '/snapshot' -af continuous -l 0.0 -s normal -n 0 ``` The stream can then be accessed at `http://:8080/stream` From c4143eaa3f2f1f2c50e7cc7b77ea61d93e4c49d2 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Sun, 7 Jul 2024 18:57:14 +0200 Subject: [PATCH 11/55] test: fix tests with new methods Signed-off-by: Patrick Gehrsitz --- tests/test_cli.py | 365 ++++++++-------------------------------------- 1 file changed, 63 insertions(+), 302 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index a0a3a4f..5de2ac1 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -19,6 +19,7 @@ DEFAULT_CONTROLS = [] DEFAULT_TUNING_FILTER = None DEFAULT_TUNING_FILTER_DIR = None +DEFAULT_CAMERA_NUM = 0 @pytest.fixture(autouse=True) @@ -76,49 +77,67 @@ def test_parse_tuning_filter_dir(): assert args.tuning_filter_dir == 'dir' -@patch("spyglass.server.run_server") @patch("spyglass.camera.init_camera") -def test_init_camera_with_defaults(mock_spyglass_camera, mock_spyglass_server): +def test_init_camera_with_defaults(mock_spyglass_camera,): from spyglass import cli import spyglass.camera cli.main(args=[]) spyglass.camera.init_camera.assert_called_once_with( + DEFAULT_CAMERA_NUM, + DEFAULT_TUNING_FILTER, + DEFAULT_TUNING_FILTER_DIR + ) + +@patch("spyglass.camera.camera.Camera.configure") +@patch("spyglass.camera.init_camera") +def test_configure_with_defaults(mock_init_camera, mock_configure): + from spyglass import cli + + cli.main(args=[]) + cam_instance = mock_init_camera.return_value + cam_instance.configure.assert_called_once_with( DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_FPS, DEFAULT_AUTOFOCUS_MODE, DEFAULT_LENS_POSITION, DEFAULT_AF_SPEED, + DEFAULT_CONTROLS, DEFAULT_UPSIDE_DOWN, DEFAULT_FLIP_HORIZONTALLY, - DEFAULT_FLIP_VERTICALLY, - DEFAULT_CONTROLS, - DEFAULT_TUNING_FILTER, - DEFAULT_TUNING_FILTER_DIR + DEFAULT_FLIP_VERTICALLY ) - -@patch("spyglass.server.run_server") +@patch("spyglass.camera.camera.Camera.configure") @patch("spyglass.camera.init_camera") -def test_init_camera_resolution(mock_spyglass_server, mock_spyglass_camera): +def test_configure_with_parameters(mock_init_camera, mock_configure): from spyglass import cli - import spyglass.camera + cli.main(args=[ - '-r', '200x100' + '-n', '1', + '-tf', 'test', + '-tfd', 'test-dir', + '-r', '200x100', + '-f', '20', + '-af', 'manual', + '-l', '1.0', + '-s', 'normal', + '-ud', '-fh', '-fv', + '-c', 'brightness=-0.4', + '-c', 'awbenable=false', ]) - spyglass.camera.init_camera.assert_called_once_with( + cam_instance = mock_init_camera.return_value + cam_instance.configure.assert_called_once_with( 200, 100, - DEFAULT_FPS, - DEFAULT_AUTOFOCUS_MODE, - DEFAULT_LENS_POSITION, - DEFAULT_AF_SPEED, - DEFAULT_UPSIDE_DOWN, - DEFAULT_FLIP_HORIZONTALLY, - DEFAULT_FLIP_VERTICALLY, - DEFAULT_CONTROLS, - DEFAULT_TUNING_FILTER, - DEFAULT_TUNING_FILTER_DIR + 20, + AF_MODE_ENUM_MANUAL, + 1.0, + AF_SPEED_ENUM_NORMAL, + [['brightness', '-0.4'],['awbenable', 'false']], + True, + True, + True ) @@ -138,251 +157,35 @@ def test_raise_error_when_height_greater_than_maximum(): ]) -@patch("spyglass.server.run_server") -@patch("spyglass.camera.init_camera") -def test_init_camera_fps(mock_spyglass_server, mock_spyglass_camera): - from spyglass import cli - import spyglass.camera - cli.main(args=[ - '-f', '20' - ]) - spyglass.camera.init_camera.assert_called_once_with( - DEFAULT_WIDTH, - DEFAULT_HEIGHT, - 20, - DEFAULT_AUTOFOCUS_MODE, - DEFAULT_LENS_POSITION, - DEFAULT_AF_SPEED, - DEFAULT_UPSIDE_DOWN, - DEFAULT_FLIP_HORIZONTALLY, - DEFAULT_FLIP_VERTICALLY, - DEFAULT_CONTROLS, - DEFAULT_TUNING_FILTER, - DEFAULT_TUNING_FILTER_DIR - ) - - -@patch("spyglass.server.run_server") -@patch("spyglass.camera.init_camera") -def test_init_camera_af_manual(mock_spyglass_server, mock_spyglass_camera): - from spyglass import cli - import spyglass.camera - cli.main(args=[ - '-af', 'manual' - ]) - spyglass.camera.init_camera.assert_called_once_with( - DEFAULT_WIDTH, - DEFAULT_HEIGHT, - DEFAULT_FPS, - AF_MODE_ENUM_MANUAL, - DEFAULT_LENS_POSITION, - DEFAULT_AF_SPEED, - DEFAULT_UPSIDE_DOWN, - DEFAULT_FLIP_HORIZONTALLY, - DEFAULT_FLIP_VERTICALLY, - DEFAULT_CONTROLS, - DEFAULT_TUNING_FILTER, - DEFAULT_TUNING_FILTER_DIR - ) - - -@patch("spyglass.server.run_server") +@patch("spyglass.camera.camera.Camera.configure") @patch("spyglass.camera.init_camera") -def test_init_camera_af_continuous(mock_spyglass_server, mock_spyglass_camera): +def test_configure_camera_af_continuous_speed_fast(mock_init_camera, mock_configure): from spyglass import cli - import spyglass.camera - cli.main(args=[ - '-af', 'continuous' - ]) - spyglass.camera.init_camera.assert_called_once_with( - DEFAULT_WIDTH, - DEFAULT_HEIGHT, - DEFAULT_FPS, - AF_MODE_ENUM_CONTINUOUS, - DEFAULT_LENS_POSITION, - DEFAULT_AF_SPEED, - DEFAULT_UPSIDE_DOWN, - DEFAULT_FLIP_HORIZONTALLY, - DEFAULT_FLIP_VERTICALLY, - DEFAULT_CONTROLS, - DEFAULT_TUNING_FILTER, - DEFAULT_TUNING_FILTER_DIR - ) - -@patch("spyglass.server.run_server") -@patch("spyglass.camera.init_camera") -def test_init_camera_lens_position(mock_spyglass_server, mock_spyglass_camera): - from spyglass import cli - import spyglass.camera - cli.main(args=[ - '-l', '1.0' - ]) - spyglass.camera.init_camera.assert_called_once_with( - DEFAULT_WIDTH, - DEFAULT_HEIGHT, - DEFAULT_FPS, - DEFAULT_AUTOFOCUS_MODE, - 1.0, - DEFAULT_AF_SPEED, - DEFAULT_UPSIDE_DOWN, - DEFAULT_FLIP_HORIZONTALLY, - DEFAULT_FLIP_VERTICALLY, - DEFAULT_CONTROLS, - DEFAULT_TUNING_FILTER, - DEFAULT_TUNING_FILTER_DIR - ) - - -@patch("spyglass.server.run_server") -@patch("spyglass.camera.init_camera") -def test_init_camera_af_speed_normal(mock_spyglass_server, mock_spyglass_camera): - from spyglass import cli - import spyglass.camera - cli.main(args=[ - '-s', 'normal' - ]) - spyglass.camera.init_camera.assert_called_once_with( - DEFAULT_WIDTH, - DEFAULT_HEIGHT, - DEFAULT_FPS, - DEFAULT_AUTOFOCUS_MODE, - DEFAULT_LENS_POSITION, - AF_SPEED_ENUM_NORMAL, - DEFAULT_UPSIDE_DOWN, - DEFAULT_FLIP_HORIZONTALLY, - DEFAULT_FLIP_VERTICALLY, - DEFAULT_CONTROLS, - DEFAULT_TUNING_FILTER, - DEFAULT_TUNING_FILTER_DIR - ) - - -@patch("spyglass.server.run_server") -@patch("spyglass.camera.init_camera") -def test_init_camera_af_speed_fast(mock_spyglass_server, mock_spyglass_camera): - from spyglass import cli - import spyglass.camera cli.main(args=[ + '-af', 'continuous', '-s', 'fast' ]) - spyglass.camera.init_camera.assert_called_once_with( + cam_instance = mock_init_camera.return_value + cam_instance.configure.assert_called_once_with( DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_FPS, - DEFAULT_AUTOFOCUS_MODE, + AF_MODE_ENUM_CONTINUOUS, DEFAULT_LENS_POSITION, AF_SPEED_ENUM_FAST, - DEFAULT_UPSIDE_DOWN, - DEFAULT_FLIP_HORIZONTALLY, - DEFAULT_FLIP_VERTICALLY, - DEFAULT_CONTROLS, - DEFAULT_TUNING_FILTER, - DEFAULT_TUNING_FILTER_DIR - ) - - -@patch("spyglass.server.run_server") -@patch("spyglass.camera.init_camera") -def test_init_camera_upside_down(mock_spyglass_server, mock_spyglass_camera): - from spyglass import cli - import spyglass.camera - cli.main(args=[ - '-ud' - ]) - spyglass.camera.init_camera.assert_called_once_with( - DEFAULT_WIDTH, - DEFAULT_HEIGHT, - DEFAULT_FPS, - DEFAULT_AUTOFOCUS_MODE, - DEFAULT_LENS_POSITION, - DEFAULT_AF_SPEED, - True, - DEFAULT_FLIP_HORIZONTALLY, - DEFAULT_FLIP_VERTICALLY, DEFAULT_CONTROLS, - DEFAULT_TUNING_FILTER, - DEFAULT_TUNING_FILTER_DIR - ) - - -@patch("spyglass.server.run_server") -@patch("spyglass.camera.init_camera") -def test_init_camera_flip_horizontal(mock_spyglass_server, mock_spyglass_camera): - from spyglass import cli - import spyglass.camera - cli.main(args=[ - '-fh' - ]) - spyglass.camera.init_camera.assert_called_once_with( - DEFAULT_WIDTH, - DEFAULT_HEIGHT, - DEFAULT_FPS, - DEFAULT_AUTOFOCUS_MODE, - DEFAULT_LENS_POSITION, - DEFAULT_AF_SPEED, - DEFAULT_UPSIDE_DOWN, - True, - DEFAULT_FLIP_VERTICALLY, - DEFAULT_CONTROLS, - DEFAULT_TUNING_FILTER, - DEFAULT_TUNING_FILTER_DIR - ) - - -@patch("spyglass.server.run_server") -@patch("spyglass.camera.init_camera") -def test_init_camera_flip_vertical(mock_spyglass_server, mock_spyglass_camera): - from spyglass import cli - import spyglass.camera - cli.main(args=[ - '-fv' - ]) - spyglass.camera.init_camera.assert_called_once_with( - DEFAULT_WIDTH, - DEFAULT_HEIGHT, - DEFAULT_FPS, - DEFAULT_AUTOFOCUS_MODE, - DEFAULT_LENS_POSITION, - DEFAULT_AF_SPEED, DEFAULT_UPSIDE_DOWN, DEFAULT_FLIP_HORIZONTALLY, - True, - DEFAULT_CONTROLS, - DEFAULT_TUNING_FILTER, - DEFAULT_TUNING_FILTER_DIR + DEFAULT_FLIP_VERTICALLY ) -@patch("spyglass.server.run_server") -@patch("spyglass.camera.init_camera") -def test_init_camera_controls(mock_spyglass_server, mock_spyglass_camera): - from spyglass import cli - import spyglass.camera - cli.main(args=[ - '-c', 'brightness=-0.4', - '-c', 'awbenable=false' - ]) - spyglass.camera.init_camera.assert_called_once_with( - DEFAULT_WIDTH, - DEFAULT_HEIGHT, - DEFAULT_FPS, - DEFAULT_AUTOFOCUS_MODE, - DEFAULT_LENS_POSITION, - DEFAULT_AF_SPEED, - DEFAULT_UPSIDE_DOWN, - DEFAULT_FLIP_HORIZONTALLY, - DEFAULT_FLIP_VERTICALLY, - [['brightness', '-0.4'],['awbenable', 'false']], - DEFAULT_TUNING_FILTER, - DEFAULT_TUNING_FILTER_DIR - ) - -@patch("spyglass.server.run_server") +@patch("spyglass.camera.csi.CSI.start_and_run_server") @patch("spyglass.camera.init_camera") -def test_run_server_with_configuration_from_arguments(mock_spyglass_server, mock_spyglass_camera): +def test_run_server_with_configuration_from_arguments(mock_init_camera, mock_run_server): from spyglass import cli - import spyglass.server + cli.main(args=[ '-b', '1.2.3.4', '-p', '1234', @@ -390,10 +193,17 @@ def test_run_server_with_configuration_from_arguments(mock_spyglass_server, mock '-sn', 'snapshot-url', '-or', 'h' ]) - spyglass.server.run_server.assert_called_once_with('1.2.3.4', 1234, ANY, ANY, 'streaming-url', 'snapshot-url', 1) + cam_instance = mock_init_camera.return_value + cam_instance.start_and_run_server.assert_called_once_with( + '1.2.3.4', + 1234, + 'streaming-url', + 'snapshot-url', + 1 + ) -@patch("spyglass.server.run_server") +@patch("spyglass.camera.csi.CSI.start_and_run_server") @patch("spyglass.camera.init_camera") @pytest.mark.parametrize("input_value, expected_output", [ ('h', 1), @@ -405,7 +215,7 @@ def test_run_server_with_configuration_from_arguments(mock_spyglass_server, mock ('mhr90', 7), ('r270', 8), ]) -def test_run_server_with_orientation(mock_spyglass_server, mock_spyglass_camera, input_value, expected_output): +def test_run_server_with_orientation(mock_init_camera, mock_run_server, input_value, expected_output): from spyglass import cli import spyglass.server cli.main(args=[ @@ -415,60 +225,11 @@ def test_run_server_with_orientation(mock_spyglass_server, mock_spyglass_camera, '-sn', 'snapshot-url', '-or', input_value ]) - spyglass.server.run_server.assert_called_once_with( + cam_instance = mock_init_camera.return_value + cam_instance.start_and_run_server.assert_called_once_with( '1.2.3.4', 1234, - ANY, - ANY, 'streaming-url', 'snapshot-url', expected_output ) - - -@patch("spyglass.server.run_server") -@patch("spyglass.camera.init_camera") -def test_init_camera_using_only_tuning_filter_file(mock_spyglass_server, mock_spyglass_camera): - from spyglass import cli - import spyglass.camera - cli.main(args=[ - '-tf', 'test', - ]) - spyglass.camera.init_camera.assert_called_once_with( - DEFAULT_WIDTH, - DEFAULT_HEIGHT, - DEFAULT_FPS, - DEFAULT_AUTOFOCUS_MODE, - DEFAULT_LENS_POSITION, - DEFAULT_AF_SPEED, - DEFAULT_UPSIDE_DOWN, - DEFAULT_FLIP_HORIZONTALLY, - DEFAULT_FLIP_VERTICALLY, - DEFAULT_CONTROLS, - "test", - DEFAULT_TUNING_FILTER_DIR - ) - -@patch("spyglass.server.run_server") -@patch("spyglass.camera.init_camera") -def test_init_camera_using_tuning_filters(mock_spyglass_server, mock_spyglass_camera): - from spyglass import cli - import spyglass.camera - cli.main(args=[ - '-tf', 'test', - '-tfd', 'test-dir', - ]) - spyglass.camera.init_camera.assert_called_once_with( - DEFAULT_WIDTH, - DEFAULT_HEIGHT, - DEFAULT_FPS, - DEFAULT_AUTOFOCUS_MODE, - DEFAULT_LENS_POSITION, - DEFAULT_AF_SPEED, - DEFAULT_UPSIDE_DOWN, - DEFAULT_FLIP_HORIZONTALLY, - DEFAULT_FLIP_VERTICALLY, - DEFAULT_CONTROLS, - "test", - "test-dir" - ) From 378c3433985d6150c6b39fd19bd5d6a7b4be274f Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Sun, 7 Jul 2024 18:58:17 +0200 Subject: [PATCH 12/55] chore: remove empty file Signed-off-by: Patrick Gehrsitz --- spyglass/camera.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 spyglass/camera.py diff --git a/spyglass/camera.py b/spyglass/camera.py deleted file mode 100644 index e69de29..0000000 From da012aa02ba3d9b50edb34cb09f08f568ce76bc5 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Mon, 5 Aug 2024 13:53:51 +0200 Subject: [PATCH 13/55] fix: add missing config file parameter Signed-off-by: Patrick Gehrsitz --- resources/spyglass.conf | 3 +++ scripts/spyglass | 1 + 2 files changed, 4 insertions(+) diff --git a/resources/spyglass.conf b/resources/spyglass.conf index 1072b56..8e2a7f2 100644 --- a/resources/spyglass.conf +++ b/resources/spyglass.conf @@ -9,6 +9,9 @@ #### NOTE: Values has to be surrounded by double quotes! ("value") #### NOTE: If commented out or includes typos it will use hardcoded defaults! +#### Libcamera camera to use (INTEGER)[default: 0] +CAMERA_NUM="0" + #### Running Spyglass with proxy or Standalone (BOOL)[default: true] NO_PROXY="true" diff --git a/scripts/spyglass b/scripts/spyglass index 46b6261..e41d155 100755 --- a/scripts/spyglass +++ b/scripts/spyglass @@ -94,6 +94,7 @@ run_spyglass() { fi "${PY_BIN}" "$(dirname "${BASE_SPY_PATH}")/run.py" \ + --camera_num "${CAMERA_NUM:-0}" \ --bindaddress "${bind_adress}" \ --port "${HTTP_PORT:-8080}" \ --resolution "${RESOLUTION:-640x480}" \ From 97e9225bc954bad1019e7847ae4504eb4de2ab69 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Sun, 11 Aug 2024 11:16:12 +0200 Subject: [PATCH 14/55] refactor: use requests code instaed of numbers Signed-off-by: Patrick Gehrsitz --- spyglass/server.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/spyglass/server.py b/spyglass/server.py index c1f46ed..08d2923 100755 --- a/spyglass/server.py +++ b/spyglass/server.py @@ -6,6 +6,7 @@ import io import logging import socketserver +from requests import codes from http import server from threading import Condition from spyglass.url_parsing import check_urls_match, get_url_params @@ -36,18 +37,18 @@ def do_GET(self): processed_controls = process_controls(self.picam2, parsed_controls) self.picam2.set_controls(processed_controls) content = parse_dictionary_to_html_page(self.picam2, parsed_controls, processed_controls).encode('utf-8') - self.send_response(200) + self.send_response(codes.ok) self.send_header('Content-Type', 'text/html') self.send_header('Content-Length', len(content)) self.end_headers() self.wfile.write(content) else: - self.send_error(404) + self.send_error(codes.not_found) self.end_headers() def start_streaming(self): try: - self.send_response(200) + self.send_response(codes.ok) self.send_default_headers() self.send_header('Content-Type', 'multipart/x-mixed-replace; boundary=FRAME') self.end_headers() @@ -70,7 +71,7 @@ def start_streaming(self): def send_snapshot(self): try: - self.send_response(200) + self.send_response(codes.ok) self.send_default_headers() frame = self.get_frame() if self.exif_header is None: From 088eb9de953cbc4d5b8389911cf0bb7aa20d8929 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Sun, 11 Aug 2024 11:17:30 +0200 Subject: [PATCH 15/55] chore: update .gitignore Signed-off-by: Patrick Gehrsitz --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 5408877..faf5477 100644 --- a/.gitignore +++ b/.gitignore @@ -55,3 +55,5 @@ cover/ .idea/ venv +.venv +.vscode From adb4c821738088346c3994af39be9f32e0c37463 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Sun, 11 Aug 2024 11:18:09 +0200 Subject: [PATCH 16/55] chore: wip Signed-off-by: Patrick Gehrsitz --- spyglass/camera/camera.py | 24 +++++++++ spyglass/server.py | 111 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 132 insertions(+), 3 deletions(-) diff --git a/spyglass/camera/camera.py b/spyglass/camera/camera.py index d090aa2..41ee646 100644 --- a/spyglass/camera/camera.py +++ b/spyglass/camera/camera.py @@ -9,6 +9,7 @@ class Camera(ABC): def __init__(self, picam2: Picamera2): self.picam2 = picam2 + self.media_track = PicameraStreamTrack(self.picam2) def create_controls(self, fps: int, autofocus: str, lens_position: float, autofocus_speed: str): controls = {} @@ -68,6 +69,7 @@ def _run_server(self, logger.info('Controls endpoint: %s', '/controls') address = (bind_address, port) streaming_handler.picam2 = self.picam2 + streaming_handler.media_track = self.media_track streaming_handler.get_frame = get_frame streaming_handler.stream_url = stream_url streaming_handler.snapshot_url = snapshot_url @@ -90,3 +92,25 @@ def start_and_run_server(self, @abstractmethod def stop(self): pass + +import av +import time + +from aiortc import MediaStreamTrack + +from fractions import Fraction + +class PicameraStreamTrack(MediaStreamTrack): + kind = "video" + + def __init__(self, cam): + super().__init__() + self.cam = cam + + async def recv(self): + img = self.cam.capture_array() + pts = time.time() * 1000000 + new_frame = av.VideoFrame.from_ndarray(img, format='rgba') + new_frame.pts = int(pts) + new_frame.time_base = Fraction(1,1000000) + return new_frame diff --git a/spyglass/server.py b/spyglass/server.py index 08d2923..b6da7ab 100755 --- a/spyglass/server.py +++ b/spyglass/server.py @@ -3,24 +3,29 @@ import logging import socketserver from http import server -import io +import time +import uuid +import asyncio import logging import socketserver from requests import codes from http import server -from threading import Condition from spyglass.url_parsing import check_urls_match, get_url_params -from spyglass.exif import create_exif_header from spyglass.camera_options import parse_dictionary_to_html_page, process_controls +from aiortc import RTCSessionDescription, RTCPeerConnection, RTCIceCandidate, sdp class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer): allow_reuse_address = True daemon_threads = True class StreamingHandler(server.BaseHTTPRequestHandler): + pcs: dict[uuid.UUID, RTCPeerConnection] = {} + loop = asyncio.new_event_loop() + def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.picam2 = None + self.media_track = None self.exif_header = None self.stream_url = None self.snapshot_url = None @@ -42,10 +47,88 @@ def do_GET(self): self.send_header('Content-Length', len(content)) self.end_headers() self.wfile.write(content) + elif check_urls_match('/webrtc', self.path): + pass else: self.send_error(codes.not_found) self.end_headers() + def do_OPTIONS(self): + def response_headers(): + self.send_response(codes.no_content) + self.send_header('Access-Control-Allow-Origin', '*') + self.send_header('Access-Control-Allow-Credentials', False) + self.send_header('Access-Control-Allow-Methods', 'OPTIONS, GET, POST, PATCH, DELETE') + self.send_header('Access-Control-Allow-Headers', 'Authorization, Content-Type, If-Match') + + if self.headers.get("Access-Control-Request-Method") != None: + response_headers() + self.end_headers() + elif check_urls_match('/whip', self.path) or check_urls_match('/whep', self.path): + response_headers() + self.send_header('Access-Control-Expose-Headers', 'Link') + self.headers['Link'] = self.get_ICE_servers() + self.end_headers() + + def do_POST(self): + content_length = int(self.headers['Content-Length']) + offer_text = self.rfile.read(content_length).decode('utf-8') + offer = RTCSessionDescription(sdp=offer_text, type='offer') + + pc = RTCPeerConnection() + @pc.on("icecandidate") + async def on_connectionstatechange(): + print("Connection state is %s" % pc.connectionState) + if pc.connectionState == "failed": + await pc.close() + + asyncio.set_event_loop(StreamingHandler.loop) + pc.addTrack(self.media_track) + + asyncio.run(pc.setRemoteDescription(offer)) + answer = asyncio.run(pc.createAnswer()) + asyncio.run(pc.setLocalDescription(answer)) + + self.send_response(codes.created) + self.send_header("Content-Type", "application/sdp") + self.send_header("ETag", "*") + secret = uuid.uuid4() + self.send_header("ID", secret) + self.send_header('Access-Control-Allow-Origin', '*') + self.send_header('Access-Control-Allow-Credentials', False) + self.send_header("Access-Control-Expose-Headers", "ETag, ID, Accept-Patch, Link, Location") + self.send_header("Accept-Patch", "application/trickle-ice-sdpfrag") + self.headers['Link'] = self.get_ICE_servers() + self.send_header("Location", f'/whep/{secret}') + self.end_headers() + self.wfile.write(bytes(answer.sdp, 'utf-8')) + + StreamingHandler.pcs[str(secret)] = pc + + def do_PATCH(self): + if len(self.path.split('/')) < 3 or self.headers.get('Content-Type') != 'application/trickle-ice-sdpfrag': + return + content_length = int(self.headers['Content-Length']) + sdp_str = self.rfile.read(content_length).decode('utf-8') + candidates = self.parse_ice_candidates(sdp_str) + secret = self.path.split('/')[-1] + pc = StreamingHandler.pcs[secret] + for candidate in candidates: + asyncio.run(pc.addIceCandidate(candidate)) + # self.candidates = candidates + # cand_sdp = '\r\n'.join([sdp.candidate_to_sdp(cand) for cand in candidates]) + '\r\n' + # offer = asyncio.run(pc.createOffer()) + # offer.sdp += cand_sdp + # asyncio.set_event_loop(StreamingHandler.loop) + # asyncio.run(pc.setLocalDescription(offer)) + + self.send_response(codes.no_content) + self.send_header('Access-Control-Allow-Origin', '*') + self.send_header("Access-Control-Allow-Credentials", False) + self.end_headers() + + pc.start() + def start_streaming(self): try: self.send_response(codes.ok) @@ -96,3 +179,25 @@ def send_default_headers(self): def send_jpeg_content_headers(self, frame, extra_len=0): self.send_header('Content-Type', 'image/jpeg') self.send_header('Content-Length', str(len(frame) + extra_len)) + + def get_ICE_servers(self): + return None + + def parse_ice_candidates(self, sdp_message): + sdp_message = sdp_message.replace('\\r\\n', '\r\n') + + lines = sdp_message.splitlines() + + candidates = [] + cand_str = 'a=candidate:' + mid_str = 'a=mid:' + mid = '' + for line in lines: + if line.startswith(mid_str): + mid = line[len(mid_str):] + elif line.startswith(cand_str): + candidate_str = line[len(cand_str):] + candidate = sdp.candidate_from_sdp(candidate_str) + candidate.sdpMid = mid + candidates.append(candidate) + return candidates From 4ae8a21489b103ec407976f7f23fac56b22d34a3 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Sun, 11 Aug 2024 16:55:37 +0200 Subject: [PATCH 17/55] chore: wip Signed-off-by: Patrick Gehrsitz --- spyglass/server.py | 123 +++++++++++++++++++++++++-------------------- 1 file changed, 68 insertions(+), 55 deletions(-) diff --git a/spyglass/server.py b/spyglass/server.py index b6da7ab..626871f 100755 --- a/spyglass/server.py +++ b/spyglass/server.py @@ -71,63 +71,76 @@ def response_headers(): self.end_headers() def do_POST(self): - content_length = int(self.headers['Content-Length']) - offer_text = self.rfile.read(content_length).decode('utf-8') - offer = RTCSessionDescription(sdp=offer_text, type='offer') - - pc = RTCPeerConnection() - @pc.on("icecandidate") - async def on_connectionstatechange(): - print("Connection state is %s" % pc.connectionState) - if pc.connectionState == "failed": - await pc.close() - - asyncio.set_event_loop(StreamingHandler.loop) - pc.addTrack(self.media_track) - - asyncio.run(pc.setRemoteDescription(offer)) - answer = asyncio.run(pc.createAnswer()) - asyncio.run(pc.setLocalDescription(answer)) - - self.send_response(codes.created) - self.send_header("Content-Type", "application/sdp") - self.send_header("ETag", "*") - secret = uuid.uuid4() - self.send_header("ID", secret) - self.send_header('Access-Control-Allow-Origin', '*') - self.send_header('Access-Control-Allow-Credentials', False) - self.send_header("Access-Control-Expose-Headers", "ETag, ID, Accept-Patch, Link, Location") - self.send_header("Accept-Patch", "application/trickle-ice-sdpfrag") - self.headers['Link'] = self.get_ICE_servers() - self.send_header("Location", f'/whep/{secret}') - self.end_headers() - self.wfile.write(bytes(answer.sdp, 'utf-8')) - - StreamingHandler.pcs[str(secret)] = pc + async def post(): + if self.headers.get("Content-Type") != "application/sdp": + self.send_error(codes.bad) + return + content_length = int(self.headers['Content-Length']) + offer_text = self.rfile.read(content_length).decode('utf-8') + offer = RTCSessionDescription(sdp=offer_text, type='offer') + + pc = RTCPeerConnection() + secret = uuid.uuid4() + @pc.on('iceconnectionstatechange') + async def on_iceconnectionstatechange(): + print(f'ICE connection state {pc.iceConnectionState}') + @pc.on('connectionstatechange') + async def on_connectionstatechange(): + print(f'Connection state {pc.connectionState}') + if pc.connectionState == "failed": + await pc.close() + StreamingHandler.pcs.pop(str(secret)) + StreamingHandler.pcs[str(secret)] = pc + pc.addTrack(self.media_track) + + await pc.setRemoteDescription(offer) + answer = await pc.createAnswer() + await pc.setLocalDescription(answer) + + while pc.iceGatheringState != "complete": + await asyncio.sleep(1) + + self.send_response(codes.created) + self.send_header("Content-Type", "application/sdp") + self.send_header("ETag", "*") + + self.send_header("ID", secret) + self.send_header('Access-Control-Allow-Origin', '*') + self.send_header('Access-Control-Allow-Credentials', False) + self.send_header("Access-Control-Expose-Headers", "ETag, ID, Accept-Patch, Link, Location") + self.send_header("Accept-Patch", "application/trickle-ice-sdpfrag") + self.headers['Link'] = self.get_ICE_servers() + self.send_header("Location", f'/whep/{secret}') + self.end_headers() + self.wfile.write(bytes(answer.sdp, 'utf-8')) + while pc.connectionState != 'connected' or pc.connectionState != 'closed': + await asyncio.sleep(10) + print(StreamingHandler.pcs) + asyncio.run(post()) def do_PATCH(self): - if len(self.path.split('/')) < 3 or self.headers.get('Content-Type') != 'application/trickle-ice-sdpfrag': - return - content_length = int(self.headers['Content-Length']) - sdp_str = self.rfile.read(content_length).decode('utf-8') - candidates = self.parse_ice_candidates(sdp_str) - secret = self.path.split('/')[-1] - pc = StreamingHandler.pcs[secret] - for candidate in candidates: - asyncio.run(pc.addIceCandidate(candidate)) - # self.candidates = candidates - # cand_sdp = '\r\n'.join([sdp.candidate_to_sdp(cand) for cand in candidates]) + '\r\n' - # offer = asyncio.run(pc.createOffer()) - # offer.sdp += cand_sdp - # asyncio.set_event_loop(StreamingHandler.loop) - # asyncio.run(pc.setLocalDescription(offer)) - - self.send_response(codes.no_content) - self.send_header('Access-Control-Allow-Origin', '*') - self.send_header("Access-Control-Allow-Credentials", False) - self.end_headers() - - pc.start() + async def patch(): + if len(self.path.split('/')) < 3 or self.headers.get('Content-Type') != 'application/trickle-ice-sdpfrag': + self.send_response(codes.bad) + self.send_header('Access-Control-Allow-Origin', '*') + self.send_header('Access-Control-Allow-Credentials', False) + self.end_headers() + return + content_length = int(self.headers['Content-Length']) + sdp_str = self.rfile.read(content_length).decode('utf-8') + candidates = self.parse_ice_candidates(sdp_str) + secret = self.path.split('/')[-1] + pc = StreamingHandler.pcs[secret] + for candidate in candidates: + await pc.addIceCandidate(candidate) + + self.send_response(codes.no_content) + self.send_header('Access-Control-Allow-Origin', '*') + self.send_header('Access-Control-Allow-Credentials', False) + self.end_headers() + while pc.connectionState != 'connected' or pc.connectionState != 'closed': + await asyncio.sleep(10) + asyncio.run(patch()) def start_streaming(self): try: From d785864c3b78e4f522672bf77c7aa5b1d7981f94 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Sun, 11 Aug 2024 17:01:15 +0200 Subject: [PATCH 18/55] chore: add MediaMTX mention Signed-off-by: Patrick Gehrsitz --- spyglass/server.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spyglass/server.py b/spyglass/server.py index 626871f..768253c 100755 --- a/spyglass/server.py +++ b/spyglass/server.py @@ -54,6 +54,8 @@ def do_GET(self): self.end_headers() def do_OPTIONS(self): + # Adapted from MediaMTX http_server.go + # https://github.com/bluenviron/mediamtx/blob/main/internal/servers/webrtc/http_server.go#L173-L189 def response_headers(): self.send_response(codes.no_content) self.send_header('Access-Control-Allow-Origin', '*') @@ -71,6 +73,8 @@ def response_headers(): self.end_headers() def do_POST(self): + # Adapted from MediaMTX http_server.go + # https://github.com/bluenviron/mediamtx/blob/main/internal/servers/webrtc/http_server.go#L191-L246 async def post(): if self.headers.get("Content-Type") != "application/sdp": self.send_error(codes.bad) @@ -119,6 +123,8 @@ async def on_connectionstatechange(): asyncio.run(post()) def do_PATCH(self): + # Adapted from MediaMTX http_server.go + # https://github.com/bluenviron/mediamtx/blob/main/internal/servers/webrtc/http_server.go#L248-L287 async def patch(): if len(self.path.split('/')) < 3 or self.headers.get('Content-Type') != 'application/trickle-ice-sdpfrag': self.send_response(codes.bad) From faaa6ba698129bed9834b4a1f1b502b474628515 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Sun, 11 Aug 2024 19:19:08 +0200 Subject: [PATCH 19/55] fix: update requirements.txt Signed-off-by: Patrick Gehrsitz --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index c637e3b..69acd7b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ setuptools~=69.2.0 +aiortc~=1.9.0 From cecde163737cd936f26d946e7888e0ad512da158 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Mon, 12 Aug 2024 00:02:49 +0200 Subject: [PATCH 20/55] chore: wip Signed-off-by: Patrick Gehrsitz --- spyglass/camera/camera.py | 3 +++ spyglass/server.py | 25 +++++++++++-------------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/spyglass/camera/camera.py b/spyglass/camera/camera.py index 41ee646..4a64956 100644 --- a/spyglass/camera/camera.py +++ b/spyglass/camera/camera.py @@ -1,6 +1,7 @@ from abc import ABC, abstractmethod from picamera2 import Picamera2 import libcamera +import threading from .. import logger from ..exif import create_exif_header from ..camera_options import process_controls @@ -78,6 +79,8 @@ def _run_server(self, else: streaming_handler.exif_header = None current_server = StreamingServer(address, streaming_handler) + t = threading.Thread(target=StreamingHandler.loop.run_forever) + t.start() current_server.serve_forever() @abstractmethod diff --git a/spyglass/server.py b/spyglass/server.py index 768253c..e8b03ee 100755 --- a/spyglass/server.py +++ b/spyglass/server.py @@ -4,6 +4,7 @@ import socketserver from http import server import time +import sys import uuid import asyncio import logging @@ -81,19 +82,19 @@ async def post(): return content_length = int(self.headers['Content-Length']) offer_text = self.rfile.read(content_length).decode('utf-8') + #offer_text = offer_text.replace('sendrecv', 'recvonly') offer = RTCSessionDescription(sdp=offer_text, type='offer') pc = RTCPeerConnection() secret = uuid.uuid4() - @pc.on('iceconnectionstatechange') - async def on_iceconnectionstatechange(): - print(f'ICE connection state {pc.iceConnectionState}') @pc.on('connectionstatechange') async def on_connectionstatechange(): print(f'Connection state {pc.connectionState}') - if pc.connectionState == "failed": + if pc.connectionState == 'failed': await pc.close() + elif pc.connectionState == 'closed': StreamingHandler.pcs.pop(str(secret)) + print(StreamingHandler.pcs) StreamingHandler.pcs[str(secret)] = pc pc.addTrack(self.media_track) @@ -107,7 +108,7 @@ async def on_connectionstatechange(): self.send_response(codes.created) self.send_header("Content-Type", "application/sdp") self.send_header("ETag", "*") - + self.send_header("ID", secret) self.send_header('Access-Control-Allow-Origin', '*') self.send_header('Access-Control-Allow-Credentials', False) @@ -115,13 +116,11 @@ async def on_connectionstatechange(): self.send_header("Accept-Patch", "application/trickle-ice-sdpfrag") self.headers['Link'] = self.get_ICE_servers() self.send_header("Location", f'/whep/{secret}') + self.send_header('Content-Length', len(pc.localDescription.sdp)) self.end_headers() - self.wfile.write(bytes(answer.sdp, 'utf-8')) - while pc.connectionState != 'connected' or pc.connectionState != 'closed': - await asyncio.sleep(10) - print(StreamingHandler.pcs) - asyncio.run(post()) - + self.wfile.write(bytes(pc.localDescription.sdp, 'utf-8')) + asyncio.run_coroutine_threadsafe(post(), StreamingHandler.loop).result() + def do_PATCH(self): # Adapted from MediaMTX http_server.go # https://github.com/bluenviron/mediamtx/blob/main/internal/servers/webrtc/http_server.go#L248-L287 @@ -144,9 +143,7 @@ async def patch(): self.send_header('Access-Control-Allow-Origin', '*') self.send_header('Access-Control-Allow-Credentials', False) self.end_headers() - while pc.connectionState != 'connected' or pc.connectionState != 'closed': - await asyncio.sleep(10) - asyncio.run(patch()) + asyncio.run_coroutine_threadsafe(patch(), StreamingHandler.loop).result() def start_streaming(self): try: From 93d25c42999e5725784d9135c761d5a446e5441d Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Tue, 13 Aug 2024 15:06:28 +0200 Subject: [PATCH 21/55] chore: remove unused code Signed-off-by: Patrick Gehrsitz --- spyglass/server.py | 1 - 1 file changed, 1 deletion(-) diff --git a/spyglass/server.py b/spyglass/server.py index e8b03ee..089b7e1 100755 --- a/spyglass/server.py +++ b/spyglass/server.py @@ -82,7 +82,6 @@ async def post(): return content_length = int(self.headers['Content-Length']) offer_text = self.rfile.read(content_length).decode('utf-8') - #offer_text = offer_text.replace('sendrecv', 'recvonly') offer = RTCSessionDescription(sdp=offer_text, type='offer') pc = RTCPeerConnection() From 6bd453995fd3ba746127576e267e6d79e28e06d3 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Wed, 21 Aug 2024 21:44:02 +0200 Subject: [PATCH 22/55] feat: add webrtc path Signed-off-by: Patrick Gehrsitz --- spyglass/camera/camera.py | 31 +---- spyglass/camera/csi.py | 2 +- spyglass/camera/usb.py | 2 +- spyglass/camera/webcam_test.py | 165 +++++++++++++++++++++++++ spyglass/server.py | 217 --------------------------------- spyglass/server/__init__.py | 0 spyglass/server/controls.py | 21 ++++ spyglass/server/http_server.py | 59 +++++++++ spyglass/server/jpeg.py | 55 +++++++++ spyglass/server/webrtc_whep.py | 159 ++++++++++++++++++++++++ spyglass/url_parsing.py | 10 +- 11 files changed, 472 insertions(+), 249 deletions(-) create mode 100755 spyglass/camera/webcam_test.py delete mode 100755 spyglass/server.py create mode 100644 spyglass/server/__init__.py create mode 100644 spyglass/server/controls.py create mode 100755 spyglass/server/http_server.py create mode 100644 spyglass/server/jpeg.py create mode 100644 spyglass/server/webrtc_whep.py diff --git a/spyglass/camera/camera.py b/spyglass/camera/camera.py index 2940c4b..838f84f 100644 --- a/spyglass/camera/camera.py +++ b/spyglass/camera/camera.py @@ -7,7 +7,8 @@ from spyglass import logger from spyglass.exif import create_exif_header from spyglass.camera_options import process_controls -from spyglass.server import StreamingServer, StreamingHandler +from spyglass.server.http_server import StreamingServer, StreamingHandler +from spyglass.server.webrtc_whep import PicameraStreamTrack class Camera(ABC): def __init__(self, picam2: Picamera2): @@ -17,7 +18,7 @@ def __init__(self, picam2: Picamera2): def create_controls(self, fps: int, autofocus: str, lens_position: float, autofocus_speed: str): controls = {} - if 'FrameRate' in self.picam2.camera_controls: + if 'FrameDurationLimits' in self.picam2.camera_controls: controls['FrameRate'] = fps if 'AfMode' in self.picam2.camera_controls: @@ -81,8 +82,8 @@ def _run_server(self, else: streaming_handler.exif_header = None current_server = StreamingServer(address, streaming_handler) - t = threading.Thread(target=StreamingHandler.loop.run_forever) - t.start() + async_loop = threading.Thread(target=StreamingHandler.loop.run_forever) + async_loop.start() current_server.serve_forever() @abstractmethod @@ -97,25 +98,3 @@ def start_and_run_server(self, @abstractmethod def stop(self): pass - -import av -import time - -from aiortc import MediaStreamTrack - -from fractions import Fraction - -class PicameraStreamTrack(MediaStreamTrack): - kind = "video" - - def __init__(self, cam): - super().__init__() - self.cam = cam - - async def recv(self): - img = self.cam.capture_array() - pts = time.time() * 1000000 - new_frame = av.VideoFrame.from_ndarray(img, format='rgba') - new_frame.pts = int(pts) - new_frame.time_base = Fraction(1,1000000) - return new_frame diff --git a/spyglass/camera/csi.py b/spyglass/camera/csi.py index 3bec8e8..a290ec5 100644 --- a/spyglass/camera/csi.py +++ b/spyglass/camera/csi.py @@ -5,7 +5,7 @@ from threading import Condition from spyglass import camera -from spyglass.server import StreamingHandler +from spyglass.server.http_server import StreamingHandler class CSI(camera.Camera): def start_and_run_server(self, diff --git a/spyglass/camera/usb.py b/spyglass/camera/usb.py index b26adee..0aeff44 100644 --- a/spyglass/camera/usb.py +++ b/spyglass/camera/usb.py @@ -1,5 +1,5 @@ from spyglass import camera -from spyglass.server import StreamingHandler +from spyglass.server.http_server import StreamingHandler class USB(camera.Camera): def start_and_run_server(self, diff --git a/spyglass/camera/webcam_test.py b/spyglass/camera/webcam_test.py new file mode 100755 index 0000000..81b9393 --- /dev/null +++ b/spyglass/camera/webcam_test.py @@ -0,0 +1,165 @@ +import argparse +import asyncio +import json +import logging +import os +import platform +import ssl + +from aiohttp import web +from aiortc import RTCPeerConnection, RTCSessionDescription +from aiortc.contrib.media import MediaPlayer, MediaRelay +from aiortc.rtcrtpsender import RTCRtpSender + +ROOT = os.path.dirname(__file__) + + +relay = None +webcam = None + + +def create_local_tracks(play_from, decode): + global relay, webcam + + if play_from: + player = MediaPlayer(play_from, decode=decode) + return player.audio, player.video + else: + options = {"framerate": "30", "video_size": "640x480"} + if relay is None: + if platform.system() == "Darwin": + webcam = MediaPlayer( + "default:none", format="avfoundation", options=options + ) + elif platform.system() == "Windows": + webcam = MediaPlayer( + "video=Integrated Camera", format="dshow", options=options + ) + else: + # webcam = MediaPlayer("/dev/video8", format="v4l2", options=options) + webcam = MediaPlayer('https://download.tsi.telecom-paristech.fr/gpac/dataset/dash/uhd/mux_sources/hevcds_720p30_2M.mp4') + relay = MediaRelay() + return None, relay.subscribe(webcam.video) + + +def force_codec(pc, sender, forced_codec): + kind = forced_codec.split("/")[0] + codecs = RTCRtpSender.getCapabilities(kind).codecs + transceiver = next(t for t in pc.getTransceivers() if t.sender == sender) + transceiver.setCodecPreferences( + [codec for codec in codecs if codec.mimeType == forced_codec] + ) + + +async def index(request): + content = open(os.path.join(ROOT, "../../resources/index.html"), "r").read() + return web.Response(content_type="text/html", text=content) + + +async def javascript(request): + content = open(os.path.join(ROOT, "../../resources/client.js"), "r").read() + return web.Response(content_type="application/javascript", text=content) + + +async def offer(request): + params = await request.json() + offer = RTCSessionDescription(sdp=params["sdp"], type=params["type"]) + + pc = RTCPeerConnection() + pcs.add(pc) + + @pc.on("connectionstatechange") + async def on_connectionstatechange(): + print("Connection state is %s" % pc.connectionState) + if pc.connectionState == "failed": + await pc.close() + pcs.discard(pc) + + # open media source + audio, video = create_local_tracks( + args.play_from, decode=not args.play_without_decoding + ) + + if audio: + audio_sender = pc.addTrack(audio) + if args.audio_codec: + force_codec(pc, audio_sender, args.audio_codec) + elif args.play_without_decoding: + raise Exception("You must specify the audio codec using --audio-codec") + + if video: + video_sender = pc.addTrack(video) + if args.video_codec: + force_codec(pc, video_sender, args.video_codec) + elif args.play_without_decoding: + raise Exception("You must specify the video codec using --video-codec") + + await pc.setRemoteDescription(offer) + + answer = await pc.createAnswer() + await pc.setLocalDescription(answer) + + return web.Response( + content_type="application/json", + text=json.dumps( + {"sdp": pc.localDescription.sdp, "type": pc.localDescription.type} + ), + ) + + +pcs = set() + + +async def on_shutdown(app): + # close peer connections + coros = [pc.close() for pc in pcs] + await asyncio.gather(*coros) + pcs.clear() + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description="WebRTC webcam demo") + parser.add_argument("--cert-file", help="SSL certificate file (for HTTPS)") + parser.add_argument("--key-file", help="SSL key file (for HTTPS)") + parser.add_argument("--play-from", help="Read the media from a file and sent it.") + parser.add_argument( + "--play-without-decoding", + help=( + "Read the media without decoding it (experimental). " + "For now it only works with an MPEGTS container with only H.264 video." + ), + action="store_true", + ) + parser.add_argument( + "--host", default="0.0.0.0", help="Host for HTTP server (default: 0.0.0.0)" + ) + parser.add_argument( + "--port", type=int, default=8080, help="Port for HTTP server (default: 8080)" + ) + parser.add_argument("--verbose", "-v", action="count") + parser.add_argument( + "--audio-codec", help="Force a specific audio codec (e.g. audio/opus)" + ) + parser.add_argument( + "--video-codec", help="Force a specific video codec (e.g. video/H264)" + ) + + args = parser.parse_args() + + if args.verbose: + logging.basicConfig(level=logging.DEBUG) + else: + logging.basicConfig(level=logging.INFO) + + if args.cert_file: + ssl_context = ssl.SSLContext() + ssl_context.load_cert_chain(args.cert_file, args.key_file) + else: + ssl_context = None + + app = web.Application() + app.on_shutdown.append(on_shutdown) + app.router.add_get("/", index) + app.router.add_get("/client.js", javascript) + app.router.add_post("/offer", offer) + web.run_app(app, host=args.host, port=args.port, ssl_context=ssl_context) diff --git a/spyglass/server.py b/spyglass/server.py deleted file mode 100755 index ffc5e4c..0000000 --- a/spyglass/server.py +++ /dev/null @@ -1,217 +0,0 @@ -#!/usr/bin/python3 - -import socketserver - -import uuid -import asyncio -import socketserver - -from requests import codes -from http import server -from aiortc import RTCSessionDescription, RTCPeerConnection, sdp - -from spyglass import logger -from spyglass.url_parsing import check_urls_match, get_url_params -from spyglass.camera_options import parse_dictionary_to_html_page, process_controls - -class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer): - allow_reuse_address = True - daemon_threads = True - -class StreamingHandler(server.BaseHTTPRequestHandler): - pcs: dict[uuid.UUID, RTCPeerConnection] = {} - loop = asyncio.new_event_loop() - - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - self.picam2 = None - self.media_track = None - self.exif_header = None - self.stream_url = None - self.snapshot_url = None - self.get_frame = None - - def do_GET(self): - if check_urls_match(self.stream_url, self.path): - self.start_streaming() - elif check_urls_match(self.snapshot_url, self.path): - self.send_snapshot() - elif check_urls_match('/controls', self.path): - parsed_controls = get_url_params(self.path) - parsed_controls = parsed_controls if parsed_controls else None - processed_controls = process_controls(self.picam2, parsed_controls) - self.picam2.set_controls(processed_controls) - content = parse_dictionary_to_html_page(self.picam2, parsed_controls, processed_controls).encode('utf-8') - self.send_response(codes.ok) - self.send_header('Content-Type', 'text/html') - self.send_header('Content-Length', len(content)) - self.end_headers() - self.wfile.write(content) - elif check_urls_match('/webrtc', self.path): - pass - else: - self.send_error(codes.not_found) - self.end_headers() - - def do_OPTIONS(self): - # Adapted from MediaMTX http_server.go - # https://github.com/bluenviron/mediamtx/blob/main/internal/servers/webrtc/http_server.go#L173-L189 - def response_headers(): - self.send_response(codes.no_content) - self.send_header('Access-Control-Allow-Origin', '*') - self.send_header('Access-Control-Allow-Credentials', False) - self.send_header('Access-Control-Allow-Methods', 'OPTIONS, GET, POST, PATCH, DELETE') - self.send_header('Access-Control-Allow-Headers', 'Authorization, Content-Type, If-Match') - - if self.headers.get("Access-Control-Request-Method") != None: - response_headers() - self.end_headers() - elif check_urls_match('/whip', self.path) or check_urls_match('/whep', self.path): - response_headers() - self.send_header('Access-Control-Expose-Headers', 'Link') - self.headers['Link'] = self.get_ICE_servers() - self.end_headers() - - def do_POST(self): - # Adapted from MediaMTX http_server.go - # https://github.com/bluenviron/mediamtx/blob/main/internal/servers/webrtc/http_server.go#L191-L246 - async def post(): - if self.headers.get("Content-Type") != "application/sdp": - self.send_error(codes.bad) - return - content_length = int(self.headers['Content-Length']) - offer_text = self.rfile.read(content_length).decode('utf-8') - offer = RTCSessionDescription(sdp=offer_text, type='offer') - - pc = RTCPeerConnection() - secret = uuid.uuid4() - @pc.on('connectionstatechange') - async def on_connectionstatechange(): - print(f'Connection state {pc.connectionState}') - if pc.connectionState == 'failed': - await pc.close() - elif pc.connectionState == 'closed': - StreamingHandler.pcs.pop(str(secret)) - print(StreamingHandler.pcs) - StreamingHandler.pcs[str(secret)] = pc - pc.addTrack(self.media_track) - - await pc.setRemoteDescription(offer) - answer = await pc.createAnswer() - await pc.setLocalDescription(answer) - - while pc.iceGatheringState != "complete": - await asyncio.sleep(1) - - self.send_response(codes.created) - self.send_header("Content-Type", "application/sdp") - self.send_header("ETag", "*") - - self.send_header("ID", secret) - self.send_header('Access-Control-Allow-Origin', '*') - self.send_header('Access-Control-Allow-Credentials', False) - self.send_header("Access-Control-Expose-Headers", "ETag, ID, Accept-Patch, Link, Location") - self.send_header("Accept-Patch", "application/trickle-ice-sdpfrag") - self.headers['Link'] = self.get_ICE_servers() - self.send_header("Location", f'/whep/{secret}') - self.send_header('Content-Length', len(pc.localDescription.sdp)) - self.end_headers() - self.wfile.write(bytes(pc.localDescription.sdp, 'utf-8')) - asyncio.run_coroutine_threadsafe(post(), StreamingHandler.loop).result() - - def do_PATCH(self): - # Adapted from MediaMTX http_server.go - # https://github.com/bluenviron/mediamtx/blob/main/internal/servers/webrtc/http_server.go#L248-L287 - async def patch(): - if len(self.path.split('/')) < 3 or self.headers.get('Content-Type') != 'application/trickle-ice-sdpfrag': - self.send_response(codes.bad) - self.send_header('Access-Control-Allow-Origin', '*') - self.send_header('Access-Control-Allow-Credentials', False) - self.end_headers() - return - content_length = int(self.headers['Content-Length']) - sdp_str = self.rfile.read(content_length).decode('utf-8') - candidates = self.parse_ice_candidates(sdp_str) - secret = self.path.split('/')[-1] - pc = StreamingHandler.pcs[secret] - for candidate in candidates: - await pc.addIceCandidate(candidate) - - self.send_response(codes.no_content) - self.send_header('Access-Control-Allow-Origin', '*') - self.send_header('Access-Control-Allow-Credentials', False) - self.end_headers() - asyncio.run_coroutine_threadsafe(patch(), StreamingHandler.loop).result() - - def start_streaming(self): - try: - self.send_response(codes.ok) - self.send_default_headers() - self.send_header('Content-Type', 'multipart/x-mixed-replace; boundary=FRAME') - self.end_headers() - while True: - frame = self.get_frame() - self.wfile.write(b'--FRAME\r\n') - if self.exif_header is None: - self.send_jpeg_content_headers(frame) - self.end_headers() - self.wfile.write(frame) - self.wfile.write(b'\r\n') - else: - self.send_jpeg_content_headers(frame, len(self.exif_header) - 2) - self.end_headers() - self.wfile.write(self.exif_header) - self.wfile.write(frame[2:]) - self.wfile.write(b'\r\n') - except Exception as e: - logger.warning('Removed streaming client %s: %s', self.client_address, str(e)) - - def send_snapshot(self): - try: - self.send_response(codes.ok) - self.send_default_headers() - frame = self.get_frame() - if self.exif_header is None: - self.send_jpeg_content_headers(frame) - self.end_headers() - self.wfile.write(frame) - else: - self.send_jpeg_content_headers(frame, len(self.exif_header) - 2) - self.end_headers() - self.wfile.write(self.exif_header) - self.wfile.write(frame[2:]) - except Exception as e: - logger.warning( - 'Removed client %s: %s', - self.client_address, str(e)) - - def send_default_headers(self): - self.send_header('Age', 0) - self.send_header('Cache-Control', 'no-cache, private') - self.send_header('Pragma', 'no-cache') - - def send_jpeg_content_headers(self, frame, extra_len=0): - self.send_header('Content-Type', 'image/jpeg') - self.send_header('Content-Length', str(len(frame) + extra_len)) - - def get_ICE_servers(self): - return None - - def parse_ice_candidates(self, sdp_message): - sdp_message = sdp_message.replace('\\r\\n', '\r\n') - - lines = sdp_message.splitlines() - - candidates = [] - cand_str = 'a=candidate:' - mid_str = 'a=mid:' - mid = '' - for line in lines: - if line.startswith(mid_str): - mid = line[len(mid_str):] - elif line.startswith(cand_str): - candidate_str = line[len(cand_str):] - candidate = sdp.candidate_from_sdp(candidate_str) - candidate.sdpMid = mid - candidates.append(candidate) - return candidates diff --git a/spyglass/server/__init__.py b/spyglass/server/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/spyglass/server/controls.py b/spyglass/server/controls.py new file mode 100644 index 0000000..b6a6b4e --- /dev/null +++ b/spyglass/server/controls.py @@ -0,0 +1,21 @@ +from requests import codes + +from spyglass.url_parsing import get_url_params +from spyglass.camera_options import parse_dictionary_to_html_page, process_controls + +# Used for type hinting +from typing import TYPE_CHECKING +if TYPE_CHECKING: + from spyglass.server.http_server import StreamingHandler + +def do_GET(handler: 'StreamingHandler'): + parsed_controls = get_url_params(handler.path) + parsed_controls = parsed_controls if parsed_controls else None + processed_controls = process_controls(handler.picam2, parsed_controls) + handler.picam2.set_controls(processed_controls) + content = parse_dictionary_to_html_page(handler.picam2, parsed_controls, processed_controls).encode('utf-8') + handler.send_response(codes.ok) + handler.send_header('Content-Type', 'text/html') + handler.send_header('Content-Length', len(content)) + handler.end_headers() + handler.wfile.write(content) diff --git a/spyglass/server/http_server.py b/spyglass/server/http_server.py new file mode 100755 index 0000000..75f19b7 --- /dev/null +++ b/spyglass/server/http_server.py @@ -0,0 +1,59 @@ +#!/usr/bin/python3 + +import socketserver +import asyncio +import socketserver + +from http import server +from requests import codes + +from spyglass.server import jpeg, webrtc_whep, controls +from spyglass.url_parsing import check_urls_match + +class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer): + allow_reuse_address = True + daemon_threads = True + +class StreamingHandler(server.BaseHTTPRequestHandler): + loop = asyncio.new_event_loop() + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def do_GET(self): + if self.check_url(self.stream_url): + jpeg.start_streaming(self) + elif self.check_url(self.snapshot_url): + jpeg.send_snapshot(self) + elif self.check_url('/controls'): + controls.do_GET(self) + elif self.check_webrtc(): + pass + else: + self.send_error(codes.not_found) + + def do_OPTIONS(self): + if self.check_webrtc(): + webrtc_whep.do_OPTIONS(self) + else: + self.send_error(codes.not_found) + + def do_POST(self): + if self.check_webrtc(): + self.run_async_request(webrtc_whep.do_POST_async) + else: + self.send_error(codes.not_found) + + def do_PATCH(self): + if self.check_webrtc(): + self.run_async_request(webrtc_whep.do_PATCH_async) + else: + self.send_error(codes.not_found) + + def check_url(self, url, match_full_path=True): + return check_urls_match(url, self.path, match_full_path) + + def check_webrtc(self): + return self.check_url('/webrtc', match_full_path=False) + + def run_async_request(self, method): + asyncio.run_coroutine_threadsafe(method(self), StreamingHandler.loop).result() diff --git a/spyglass/server/jpeg.py b/spyglass/server/jpeg.py new file mode 100644 index 0000000..25c3dbc --- /dev/null +++ b/spyglass/server/jpeg.py @@ -0,0 +1,55 @@ +from requests import codes + +from spyglass import logger + +# Used for type hinting +from typing import TYPE_CHECKING +if TYPE_CHECKING: + from spyglass.server.http_server import StreamingHandler + +def start_streaming(handler: 'StreamingHandler'): + try: + send_default_headers(handler) + handler.send_header('Content-Type', 'multipart/x-mixed-replace; boundary=FRAME') + handler.end_headers() + while True: + frame = handler.get_frame() + handler.wfile.write(b'--FRAME\r\n') + if handler.exif_header is None: + send_jpeg_content_headers(handler, frame) + handler.wfile.write(frame) + handler.wfile.write(b'\r\n') + else: + send_jpeg_content_headers(handler, frame, len(handler.exif_header) - 2) + handler.wfile.write(handler.exif_header) + handler.wfile.write(frame[2:]) + handler.wfile.write(b'\r\n') + except Exception as e: + logger.warning('Removed streaming client %s: %s', handler.client_address, str(e)) + +def send_snapshot(handler: 'StreamingHandler'): + try: + send_default_headers(handler) + frame = handler.get_frame() + if handler.exif_header is None: + send_jpeg_content_headers(handler, frame) + handler.wfile.write(frame) + else: + send_jpeg_content_headers(handler, frame, len(handler.exif_header) - 2) + handler.wfile.write(handler.exif_header) + handler.wfile.write(frame[2:]) + except Exception as e: + logger.warning( + 'Removed client %s: %s', + handler.client_address, str(e)) + +def send_default_headers(handler: 'StreamingHandler'): + handler.send_response(codes.ok) + handler.send_header('Age', 0) + handler.send_header('Cache-Control', 'no-cache, private') + handler.send_header('Pragma', 'no-cache') + +def send_jpeg_content_headers(handler: 'StreamingHandler', frame, extra_len=0): + handler.send_header('Content-Type', 'image/jpeg') + handler.send_header('Content-Length', str(len(frame) + extra_len)) + handler.end_headers() diff --git a/spyglass/server/webrtc_whep.py b/spyglass/server/webrtc_whep.py new file mode 100644 index 0000000..518d3c1 --- /dev/null +++ b/spyglass/server/webrtc_whep.py @@ -0,0 +1,159 @@ +import uuid +import asyncio + +from requests import codes +from aiortc import RTCSessionDescription, RTCPeerConnection, sdp + +from spyglass.url_parsing import check_urls_match + +# Used for type hinting +from typing import TYPE_CHECKING +if TYPE_CHECKING: + from spyglass.server.http_server import StreamingHandler + +pcs: dict[uuid.UUID, RTCPeerConnection] = {} + +def send_default_headers(response_code: int, handler: 'StreamingHandler'): + handler.send_response(response_code) + handler.send_header('Access-Control-Allow-Origin', '*') + handler.send_header('Access-Control-Allow-Credentials', False) + +def do_OPTIONS(handler: 'StreamingHandler'): + # Adapted from MediaMTX http_server.go + # https://github.com/bluenviron/mediamtx/blob/main/internal/servers/webrtc/http_server.go#L173-L189 + def response_headers(): + send_default_headers(codes.no_content, handler) + handler.send_header('Access-Control-Allow-Methods', 'OPTIONS, GET, POST, PATCH, DELETE') + handler.send_header('Access-Control-Allow-Headers', 'Authorization, Content-Type, If-Match') + + if handler.headers.get("Access-Control-Request-Method") != None: + response_headers() + handler.end_headers() + elif check_urls_match('/webrtc/whip', handler.path) \ + or check_urls_match('/webrtc/whep', handler.path): + response_headers() + handler.send_header('Access-Control-Expose-Headers', 'Link') + handler.headers['Link'] = get_ICE_servers() + handler.end_headers() + +async def do_POST_async(handler: 'StreamingHandler'): + # Adapted from MediaMTX http_server.go + # https://github.com/bluenviron/mediamtx/blob/main/internal/servers/webrtc/http_server.go#L191-L246 + if handler.headers.get("Content-Type") != "application/sdp": + handler.send_error(codes.bad) + return + content_length = int(handler.headers['Content-Length']) + offer_text = handler.rfile.read(content_length).decode('utf-8') + offer = RTCSessionDescription(sdp=offer_text, type='offer') + + pc = RTCPeerConnection() + secret = uuid.uuid4() + @pc.on('connectionstatechange') + async def on_connectionstatechange(): + print(f'Connection state {pc.connectionState}') + if pc.connectionState == 'failed': + await pc.close() + elif pc.connectionState == 'closed': + pcs.pop(str(secret)) + print(f'{len(pcs)} connections still open.') + pcs[str(secret)] = pc + pc.addTrack(handler.media_track) + + await pc.setRemoteDescription(offer) + answer = await pc.createAnswer() + await pc.setLocalDescription(answer) + + while pc.iceGatheringState != "complete": + await asyncio.sleep(1) + + send_default_headers(codes.created, handler) + + handler.send_header("Content-Type", "application/sdp") + handler.send_header("ETag", "*") + + handler.send_header("ID", secret) + handler.send_header("Access-Control-Expose-Headers", "ETag, ID, Accept-Patch, Link, Location") + handler.send_header("Accept-Patch", "application/trickle-ice-sdpfrag") + handler.headers['Link'] = get_ICE_servers() + handler.send_header("Location", f'/whep/{secret}') + handler.send_header('Content-Length', len(pc.localDescription.sdp)) + handler.end_headers() + handler.wfile.write(bytes(pc.localDescription.sdp, 'utf-8')) + +async def do_PATCH_async(streaming_handler: 'StreamingHandler'): + # Adapted from MediaMTX http_server.go + # https://github.com/bluenviron/mediamtx/blob/main/internal/servers/webrtc/http_server.go#L248-L287 + if len(streaming_handler.path.split('/')) < 3 \ + or streaming_handler.headers.get('Content-Type') != 'application/trickle-ice-sdpfrag': + send_default_headers(codes.bad, streaming_handler) + streaming_handler.end_headers() + return + content_length = int(streaming_handler.headers['Content-Length']) + sdp_str = streaming_handler.rfile.read(content_length).decode('utf-8') + candidates = parse_ice_candidates(sdp_str) + secret = streaming_handler.path.split('/')[-1] + pc = pcs[secret] + for candidate in candidates: + await pc.addIceCandidate(candidate) + + send_default_headers(codes.no_content, streaming_handler) + streaming_handler.end_headers() + +def get_ICE_servers(): + return None + +def parse_ice_candidates(sdp_message): + sdp_message = sdp_message.replace('\\r\\n', '\r\n') + + lines = sdp_message.splitlines() + + candidates = [] + cand_str = 'a=candidate:' + mid_str = 'a=mid:' + mid = '' + for line in lines: + if line.startswith(mid_str): + mid = line[len(mid_str):] + elif line.startswith(cand_str): + candidate_str = line[len(cand_str):] + candidate = sdp.candidate_from_sdp(candidate_str) + candidate.sdpMid = mid + candidates.append(candidate) + return candidates + +import av +import time + +from aiortc import MediaStreamTrack + +from fractions import Fraction + +class PicameraStreamTrack(MediaStreamTrack): + kind = "video" + def __init__(self, cam): + super().__init__() + self.cam = cam + self.img = None + self.condition = asyncio.Condition() + from spyglass.server.http_server import StreamingHandler + StreamingHandler.loop.create_task(self.get_img()) + + async def get_img(self): + while True: + if len(pcs) > 0: + async with self.condition: + self.img = self.cam.capture_array() + self.condition.notify_all() + await asyncio.sleep(0) + else: + await asyncio.sleep(0.5) + + async def recv(self): + async with self.condition: + await self.condition.wait() + img = self.img + pts = time.time() * 1000000 + new_frame = av.VideoFrame.from_ndarray(img, format='rgba') + new_frame.pts = int(pts) + new_frame.time_base = Fraction(1,1000000) + return new_frame diff --git a/spyglass/url_parsing.py b/spyglass/url_parsing.py index 3c07126..64c6017 100644 --- a/spyglass/url_parsing.py +++ b/spyglass/url_parsing.py @@ -1,6 +1,6 @@ from urllib.parse import urlparse, parse_qsl -def check_paths_match(expected_url, incoming_url): +def check_paths_match(expected_url, incoming_url, match_full_path=True): # Assign paths from URL into list exp_paths = urlparse(expected_url.strip("/")).path.split("/") inc_paths = urlparse(incoming_url.strip("/")).path.split("/") @@ -15,7 +15,9 @@ def check_paths_match(expected_url, incoming_url): inc_paths = list(filter(None, inc_paths)) # Determine if match - if len(exp_paths)==len(inc_paths): + if match_full_path and len(exp_paths)==len(inc_paths): + return all([exp == inc for exp, inc in zip(exp_paths, inc_paths)]) + elif not match_full_path and len(exp_paths)<=len(inc_paths): return all([exp == inc for exp, inc in zip(exp_paths, inc_paths)]) return False @@ -39,9 +41,9 @@ def check_params_match(expected_url, incoming_url): return matching_params==exp_params -def check_urls_match(expected_url, incoming_url): +def check_urls_match(expected_url, incoming_url, match_full_path=True): # Check URL paths - paths_match = check_paths_match(expected_url, incoming_url) + paths_match = check_paths_match(expected_url, incoming_url, match_full_path) # Check URL params params_match = check_params_match(expected_url, incoming_url) From 1640062c9ba107d2afae7fea5a4d99675b385006 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Wed, 21 Aug 2024 21:52:54 +0200 Subject: [PATCH 23/55] fix: add missing webrtc endpoint logging Signed-off-by: Patrick Gehrsitz --- spyglass/camera/camera.py | 1 + 1 file changed, 1 insertion(+) diff --git a/spyglass/camera/camera.py b/spyglass/camera/camera.py index 838f84f..b2d69ec 100644 --- a/spyglass/camera/camera.py +++ b/spyglass/camera/camera.py @@ -71,6 +71,7 @@ def _run_server(self, logger.info('Streaming endpoint: %s', stream_url) logger.info('Snapshot endpoint: %s', snapshot_url) logger.info('Controls endpoint: %s', '/controls') + logger.info('WebRTC endpoint: %s', '/webrtc') address = (bind_address, port) streaming_handler.picam2 = self.picam2 streaming_handler.media_track = self.media_track From b63ca2c3b4ab52053830c5f011a90064c3aebfbc Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Wed, 21 Aug 2024 21:54:19 +0200 Subject: [PATCH 24/55] refactor: change to inline format string for endpoint logging Signed-off-by: Patrick Gehrsitz --- spyglass/camera/camera.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/spyglass/camera/camera.py b/spyglass/camera/camera.py index b2d69ec..4ad48d4 100644 --- a/spyglass/camera/camera.py +++ b/spyglass/camera/camera.py @@ -67,11 +67,11 @@ def _run_server(self, stream_url='/stream', snapshot_url='/snapshot', orientation_exif=0): - logger.info('Server listening on %s:%d', bind_address, port) - logger.info('Streaming endpoint: %s', stream_url) - logger.info('Snapshot endpoint: %s', snapshot_url) - logger.info('Controls endpoint: %s', '/controls') - logger.info('WebRTC endpoint: %s', '/webrtc') + logger.info(f'Server listening on {bind_address}:{port}') + logger.info(f'Streaming endpoint: {stream_url}') + logger.info(f'Snapshot endpoint: {snapshot_url}') + logger.info(f'Controls endpoint: /controls') + logger.info(f'WebRTC endpoint: /webrtc') address = (bind_address, port) streaming_handler.picam2 = self.picam2 streaming_handler.media_track = self.media_track From 4174d7a29526df1c921de0a627e6b7419a023a74 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Fri, 23 Aug 2024 22:19:52 +0200 Subject: [PATCH 25/55] chore: delete example file of aiortc This was accidentally commited Signed-off-by: Patrick Gehrsitz --- spyglass/camera/webcam_test.py | 165 --------------------------------- 1 file changed, 165 deletions(-) delete mode 100755 spyglass/camera/webcam_test.py diff --git a/spyglass/camera/webcam_test.py b/spyglass/camera/webcam_test.py deleted file mode 100755 index 81b9393..0000000 --- a/spyglass/camera/webcam_test.py +++ /dev/null @@ -1,165 +0,0 @@ -import argparse -import asyncio -import json -import logging -import os -import platform -import ssl - -from aiohttp import web -from aiortc import RTCPeerConnection, RTCSessionDescription -from aiortc.contrib.media import MediaPlayer, MediaRelay -from aiortc.rtcrtpsender import RTCRtpSender - -ROOT = os.path.dirname(__file__) - - -relay = None -webcam = None - - -def create_local_tracks(play_from, decode): - global relay, webcam - - if play_from: - player = MediaPlayer(play_from, decode=decode) - return player.audio, player.video - else: - options = {"framerate": "30", "video_size": "640x480"} - if relay is None: - if platform.system() == "Darwin": - webcam = MediaPlayer( - "default:none", format="avfoundation", options=options - ) - elif platform.system() == "Windows": - webcam = MediaPlayer( - "video=Integrated Camera", format="dshow", options=options - ) - else: - # webcam = MediaPlayer("/dev/video8", format="v4l2", options=options) - webcam = MediaPlayer('https://download.tsi.telecom-paristech.fr/gpac/dataset/dash/uhd/mux_sources/hevcds_720p30_2M.mp4') - relay = MediaRelay() - return None, relay.subscribe(webcam.video) - - -def force_codec(pc, sender, forced_codec): - kind = forced_codec.split("/")[0] - codecs = RTCRtpSender.getCapabilities(kind).codecs - transceiver = next(t for t in pc.getTransceivers() if t.sender == sender) - transceiver.setCodecPreferences( - [codec for codec in codecs if codec.mimeType == forced_codec] - ) - - -async def index(request): - content = open(os.path.join(ROOT, "../../resources/index.html"), "r").read() - return web.Response(content_type="text/html", text=content) - - -async def javascript(request): - content = open(os.path.join(ROOT, "../../resources/client.js"), "r").read() - return web.Response(content_type="application/javascript", text=content) - - -async def offer(request): - params = await request.json() - offer = RTCSessionDescription(sdp=params["sdp"], type=params["type"]) - - pc = RTCPeerConnection() - pcs.add(pc) - - @pc.on("connectionstatechange") - async def on_connectionstatechange(): - print("Connection state is %s" % pc.connectionState) - if pc.connectionState == "failed": - await pc.close() - pcs.discard(pc) - - # open media source - audio, video = create_local_tracks( - args.play_from, decode=not args.play_without_decoding - ) - - if audio: - audio_sender = pc.addTrack(audio) - if args.audio_codec: - force_codec(pc, audio_sender, args.audio_codec) - elif args.play_without_decoding: - raise Exception("You must specify the audio codec using --audio-codec") - - if video: - video_sender = pc.addTrack(video) - if args.video_codec: - force_codec(pc, video_sender, args.video_codec) - elif args.play_without_decoding: - raise Exception("You must specify the video codec using --video-codec") - - await pc.setRemoteDescription(offer) - - answer = await pc.createAnswer() - await pc.setLocalDescription(answer) - - return web.Response( - content_type="application/json", - text=json.dumps( - {"sdp": pc.localDescription.sdp, "type": pc.localDescription.type} - ), - ) - - -pcs = set() - - -async def on_shutdown(app): - # close peer connections - coros = [pc.close() for pc in pcs] - await asyncio.gather(*coros) - pcs.clear() - - -if __name__ == "__main__": - parser = argparse.ArgumentParser(description="WebRTC webcam demo") - parser.add_argument("--cert-file", help="SSL certificate file (for HTTPS)") - parser.add_argument("--key-file", help="SSL key file (for HTTPS)") - parser.add_argument("--play-from", help="Read the media from a file and sent it.") - parser.add_argument( - "--play-without-decoding", - help=( - "Read the media without decoding it (experimental). " - "For now it only works with an MPEGTS container with only H.264 video." - ), - action="store_true", - ) - parser.add_argument( - "--host", default="0.0.0.0", help="Host for HTTP server (default: 0.0.0.0)" - ) - parser.add_argument( - "--port", type=int, default=8080, help="Port for HTTP server (default: 8080)" - ) - parser.add_argument("--verbose", "-v", action="count") - parser.add_argument( - "--audio-codec", help="Force a specific audio codec (e.g. audio/opus)" - ) - parser.add_argument( - "--video-codec", help="Force a specific video codec (e.g. video/H264)" - ) - - args = parser.parse_args() - - if args.verbose: - logging.basicConfig(level=logging.DEBUG) - else: - logging.basicConfig(level=logging.INFO) - - if args.cert_file: - ssl_context = ssl.SSLContext() - ssl_context.load_cert_chain(args.cert_file, args.key_file) - else: - ssl_context = None - - app = web.Application() - app.on_shutdown.append(on_shutdown) - app.router.add_get("/", index) - app.router.add_get("/client.js", javascript) - app.router.add_post("/offer", offer) - web.run_app(app, host=args.host, port=args.port, ssl_context=ssl_context) From 3ce9bebb72285fb0544c569e82bc9466c2106972 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Sun, 1 Sep 2024 22:45:40 +0200 Subject: [PATCH 26/55] fix: fix PicameraStreamTrack condition loop for python<3.10 Signed-off-by: Patrick Gehrsitz --- spyglass/server/webrtc_whep.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spyglass/server/webrtc_whep.py b/spyglass/server/webrtc_whep.py index 518d3c1..eda725a 100644 --- a/spyglass/server/webrtc_whep.py +++ b/spyglass/server/webrtc_whep.py @@ -134,9 +134,10 @@ def __init__(self, cam): super().__init__() self.cam = cam self.img = None - self.condition = asyncio.Condition() from spyglass.server.http_server import StreamingHandler StreamingHandler.loop.create_task(self.get_img()) + asyncio.set_event_loop(StreamingHandler.loop) + self.condition = asyncio.Condition() async def get_img(self): while True: From 9a32d6cdfe9901500004c3a2e44bfaad663df9b0 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Sun, 1 Sep 2024 22:57:19 +0200 Subject: [PATCH 27/55] fix: increase sleep timer to appropriate value Signed-off-by: Patrick Gehrsitz --- spyglass/server/webrtc_whep.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spyglass/server/webrtc_whep.py b/spyglass/server/webrtc_whep.py index eda725a..b89f8f6 100644 --- a/spyglass/server/webrtc_whep.py +++ b/spyglass/server/webrtc_whep.py @@ -145,7 +145,7 @@ async def get_img(self): async with self.condition: self.img = self.cam.capture_array() self.condition.notify_all() - await asyncio.sleep(0) + await asyncio.sleep(0.01) else: await asyncio.sleep(0.5) From f9f39e230d1a7190f104333f05fc3737f200c218 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Wed, 4 Sep 2024 22:40:27 +0200 Subject: [PATCH 28/55] chore: update orientation_exif description Signed-off-by: Patrick Gehrsitz --- spyglass/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spyglass/cli.py b/spyglass/cli.py index 6370241..0924daf 100644 --- a/spyglass/cli.py +++ b/spyglass/cli.py @@ -158,7 +158,7 @@ def get_parser(): parser.add_argument('-fv', '--flip_vertical', action='store_true', help='Mirror the image vertically (sensor level)') parser.add_argument('-or', '--orientation_exif', type=orientation_type, default='h', - help='Set the image orientation using an EXIF header:\n' + help='Set the image orientation using an EXIF header. This does not work with WebRTC:\n' ' h - Horizontal (normal)\n' ' mh - Mirror horizontal\n' ' r180 - Rotate 180\n' From 03c5a25c488dd4bbaf48852a28829dc9e621fbc7 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Wed, 4 Sep 2024 22:46:36 +0200 Subject: [PATCH 29/55] feat: force H264 codec Signed-off-by: Patrick Gehrsitz --- spyglass/server/webrtc_whep.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/spyglass/server/webrtc_whep.py b/spyglass/server/webrtc_whep.py index b89f8f6..24eaafc 100644 --- a/spyglass/server/webrtc_whep.py +++ b/spyglass/server/webrtc_whep.py @@ -3,6 +3,7 @@ from requests import codes from aiortc import RTCSessionDescription, RTCPeerConnection, sdp +from aiortc.rtcrtpsender import RTCRtpSender from spyglass.url_parsing import check_urls_match @@ -57,7 +58,12 @@ async def on_connectionstatechange(): pcs.pop(str(secret)) print(f'{len(pcs)} connections still open.') pcs[str(secret)] = pc - pc.addTrack(handler.media_track) + sender = pc.addTrack(handler.media_track) + codecs = RTCRtpSender.getCapabilities('video').codecs + transceiver = next(t for t in pc.getTransceivers() if t.sender == sender) + transceiver.setCodecPreferences( + [codec for codec in codecs if codec.mimeType == 'video/H264'] + ) await pc.setRemoteDescription(offer) answer = await pc.createAnswer() From 01868c044ff5856125d052258fb28032b396209f Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Thu, 5 Sep 2024 17:44:47 +0200 Subject: [PATCH 30/55] feat: use h264 hw encoder of picamera2 Signed-off-by: Patrick Gehrsitz --- spyglass/camera/camera.py | 2 +- spyglass/camera/csi.py | 6 +++-- spyglass/server/webrtc_whep.py | 41 +++++++++++++--------------------- 3 files changed, 20 insertions(+), 29 deletions(-) diff --git a/spyglass/camera/camera.py b/spyglass/camera/camera.py index 4ad48d4..a6aae99 100644 --- a/spyglass/camera/camera.py +++ b/spyglass/camera/camera.py @@ -13,7 +13,7 @@ class Camera(ABC): def __init__(self, picam2: Picamera2): self.picam2 = picam2 - self.media_track = PicameraStreamTrack(self.picam2) + self.media_track = PicameraStreamTrack() def create_controls(self, fps: int, autofocus: str, lens_position: float, autofocus_speed: str): controls = {} diff --git a/spyglass/camera/csi.py b/spyglass/camera/csi.py index a290ec5..0ae1740 100644 --- a/spyglass/camera/csi.py +++ b/spyglass/camera/csi.py @@ -1,6 +1,6 @@ import io -from picamera2.encoders import MJPEGEncoder +from picamera2.encoders import MJPEGEncoder, H264Encoder from picamera2.outputs import FileOutput from threading import Condition @@ -30,7 +30,9 @@ def get_frame(inner_self): output.condition.wait() return output.frame - self.picam2.start_recording(MJPEGEncoder(), FileOutput(output)) + self.picam2.start() + self.picam2.start_encoder(MJPEGEncoder(), FileOutput(output)) + self.picam2.start_recording(H264Encoder(), self.media_track) self._run_server( bind_address, diff --git a/spyglass/server/webrtc_whep.py b/spyglass/server/webrtc_whep.py index 24eaafc..ac41144 100644 --- a/spyglass/server/webrtc_whep.py +++ b/spyglass/server/webrtc_whep.py @@ -128,39 +128,28 @@ def parse_ice_candidates(sdp_message): return candidates import av -import time from aiortc import MediaStreamTrack +from picamera2.outputs import Output from fractions import Fraction -class PicameraStreamTrack(MediaStreamTrack): +class PicameraStreamTrack(MediaStreamTrack, Output): kind = "video" - def __init__(self, cam): + def __init__(self): super().__init__() - self.cam = cam self.img = None - from spyglass.server.http_server import StreamingHandler - StreamingHandler.loop.create_task(self.get_img()) - asyncio.set_event_loop(StreamingHandler.loop) - self.condition = asyncio.Condition() - - async def get_img(self): - while True: - if len(pcs) > 0: - async with self.condition: - self.img = self.cam.capture_array() - self.condition.notify_all() - await asyncio.sleep(0.01) - else: - await asyncio.sleep(0.5) + self.pts = 0 + self.keyframe = False + + def outputframe(self, frame, keyframe=True, timestamp=None): + self.img = frame + self.keyframe = keyframe + self.pts = timestamp async def recv(self): - async with self.condition: - await self.condition.wait() - img = self.img - pts = time.time() * 1000000 - new_frame = av.VideoFrame.from_ndarray(img, format='rgba') - new_frame.pts = int(pts) - new_frame.time_base = Fraction(1,1000000) - return new_frame + await asyncio.sleep(0.01) + packet = av.packet.Packet(self.img) + packet.pts = self.pts + packet.time_base = Fraction(1,1000000) + return packet From 61d808112e5dbf5c0c492963d02a9d4eba6f3c8b Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Sat, 21 Sep 2024 23:23:22 +0200 Subject: [PATCH 31/55] fix: don't double use frames Signed-off-by: Patrick Gehrsitz --- spyglass/server/webrtc_whep.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/spyglass/server/webrtc_whep.py b/spyglass/server/webrtc_whep.py index ac41144..ef2efe8 100644 --- a/spyglass/server/webrtc_whep.py +++ b/spyglass/server/webrtc_whep.py @@ -131,6 +131,7 @@ def parse_ice_candidates(sdp_message): from aiortc import MediaStreamTrack from picamera2.outputs import Output +from queue import Queue from fractions import Fraction @@ -138,18 +139,17 @@ class PicameraStreamTrack(MediaStreamTrack, Output): kind = "video" def __init__(self): super().__init__() - self.img = None - self.pts = 0 - self.keyframe = False + self.img_queue = Queue() def outputframe(self, frame, keyframe=True, timestamp=None): - self.img = frame - self.keyframe = keyframe - self.pts = timestamp + self.img_queue.put((frame, keyframe, timestamp)) async def recv(self): - await asyncio.sleep(0.01) - packet = av.packet.Packet(self.img) - packet.pts = self.pts + while self.img_queue.empty(): + await asyncio.sleep(0.02) + img, keyframe, pts = self.img_queue.get() + packet = av.packet.Packet(img) + packet.pts = pts packet.time_base = Fraction(1,1000000) + packet.is_keyframe = keyframe return packet From 63eb7ff32c7cd07be4336ffac9213bbc1a767844 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Sun, 22 Sep 2024 20:54:44 +0200 Subject: [PATCH 32/55] fix: fix webrtc stream with multiple clients Signed-off-by: Patrick Gehrsitz --- spyglass/server/webrtc_whep.py | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/spyglass/server/webrtc_whep.py b/spyglass/server/webrtc_whep.py index ef2efe8..4b44cc5 100644 --- a/spyglass/server/webrtc_whep.py +++ b/spyglass/server/webrtc_whep.py @@ -4,6 +4,7 @@ from requests import codes from aiortc import RTCSessionDescription, RTCPeerConnection, sdp from aiortc.rtcrtpsender import RTCRtpSender +from aiortc.contrib.media import MediaRelay from spyglass.url_parsing import check_urls_match @@ -13,6 +14,7 @@ from spyglass.server.http_server import StreamingHandler pcs: dict[uuid.UUID, RTCPeerConnection] = {} +media_relay = MediaRelay() def send_default_headers(response_code: int, handler: 'StreamingHandler'): handler.send_response(response_code) @@ -58,7 +60,8 @@ async def on_connectionstatechange(): pcs.pop(str(secret)) print(f'{len(pcs)} connections still open.') pcs[str(secret)] = pc - sender = pc.addTrack(handler.media_track) + track = media_relay.subscribe(handler.media_track) + sender = pc.addTrack(track) codecs = RTCRtpSender.getCapabilities('video').codecs transceiver = next(t for t in pc.getTransceivers() if t.sender == sender) transceiver.setCodecPreferences( @@ -130,24 +133,34 @@ def parse_ice_candidates(sdp_message): import av from aiortc import MediaStreamTrack -from picamera2.outputs import Output -from queue import Queue - +from collections import deque from fractions import Fraction +from picamera2.outputs import Output class PicameraStreamTrack(MediaStreamTrack, Output): kind = "video" def __init__(self): super().__init__() - self.img_queue = Queue() + self.img_queue = deque(maxlen=60) + from spyglass.server.http_server import StreamingHandler + asyncio.set_event_loop(StreamingHandler.loop) + self.condition = asyncio.Condition() def outputframe(self, frame, keyframe=True, timestamp=None): - self.img_queue.put((frame, keyframe, timestamp)) + from spyglass.server.http_server import StreamingHandler + asyncio.run_coroutine_threadsafe(self.put_frame(frame, keyframe, timestamp), StreamingHandler.loop) + + async def put_frame(self, frame, keyframe=True, timestamp=None): + async with self.condition: + self.img_queue.append((frame, keyframe, timestamp)) + self.condition.notify_all() async def recv(self): - while self.img_queue.empty(): - await asyncio.sleep(0.02) - img, keyframe, pts = self.img_queue.get() + async with self.condition: + def not_empty(): + return len(self.img_queue) > 0 + await self.condition.wait_for(not_empty) + img, keyframe, pts = self.img_queue.popleft() packet = av.packet.Packet(img) packet.pts = pts packet.time_base = Fraction(1,1000000) From b9eec7cd27738ee00013c3b86964eeed734d0c4b Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Sun, 22 Sep 2024 20:55:05 +0200 Subject: [PATCH 33/55] fix: fix starting method of encoder Signed-off-by: Patrick Gehrsitz --- spyglass/camera/csi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spyglass/camera/csi.py b/spyglass/camera/csi.py index 0ae1740..50f37fa 100644 --- a/spyglass/camera/csi.py +++ b/spyglass/camera/csi.py @@ -32,7 +32,7 @@ def get_frame(inner_self): self.picam2.start() self.picam2.start_encoder(MJPEGEncoder(), FileOutput(output)) - self.picam2.start_recording(H264Encoder(), self.media_track) + self.picam2.start_encoder(H264Encoder(), self.media_track) self._run_server( bind_address, From 1768626517ecef7b9291c175d45e254b8833180b Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Sun, 22 Sep 2024 20:57:04 +0200 Subject: [PATCH 34/55] fix: fix order of starting methods Signed-off-by: Patrick Gehrsitz --- spyglass/camera/csi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spyglass/camera/csi.py b/spyglass/camera/csi.py index 50f37fa..1b3f2ff 100644 --- a/spyglass/camera/csi.py +++ b/spyglass/camera/csi.py @@ -30,9 +30,9 @@ def get_frame(inner_self): output.condition.wait() return output.frame - self.picam2.start() self.picam2.start_encoder(MJPEGEncoder(), FileOutput(output)) self.picam2.start_encoder(H264Encoder(), self.media_track) + self.picam2.start() self._run_server( bind_address, From fe658644b457122ff9491da558f619821bc98453 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Tue, 31 Dec 2024 16:43:48 +0100 Subject: [PATCH 35/55] refactor: move parsed_controls processing into parse_dictionary_to_html_page Signed-off-by: Patrick Gehrsitz --- spyglass/camera_options.py | 6 +++++- spyglass/server/controls.py | 1 - 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/spyglass/camera_options.py b/spyglass/camera_options.py index a2bc95a..645b63b 100644 --- a/spyglass/camera_options.py +++ b/spyglass/camera_options.py @@ -1,7 +1,11 @@ import libcamera import ast -def parse_dictionary_to_html_page(camera, parsed_controls='None', processed_controls='None'): +def parse_dictionary_to_html_page(camera, parsed_controls={}, processed_controls={}): + if not parsed_controls: + parsed_controls = 'None' + if not processed_controls: + processed_controls = 'None' html = """ diff --git a/spyglass/server/controls.py b/spyglass/server/controls.py index b6a6b4e..0a88141 100644 --- a/spyglass/server/controls.py +++ b/spyglass/server/controls.py @@ -10,7 +10,6 @@ def do_GET(handler: 'StreamingHandler'): parsed_controls = get_url_params(handler.path) - parsed_controls = parsed_controls if parsed_controls else None processed_controls = process_controls(handler.picam2, parsed_controls) handler.picam2.set_controls(processed_controls) content = parse_dictionary_to_html_page(handler.picam2, parsed_controls, processed_controls).encode('utf-8') From 21512f2507486e56c155126fcd8f0f95f3f5d83d Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Wed, 8 Jan 2025 22:47:20 +0100 Subject: [PATCH 36/55] feat: add webrtc_url parameter Signed-off-by: Patrick Gehrsitz --- resources/spyglass.conf | 3 +++ scripts/spyglass | 1 + spyglass/camera/camera.py | 6 +++++- spyglass/camera/csi.py | 2 ++ spyglass/camera/usb.py | 2 ++ spyglass/cli.py | 3 +++ spyglass/server/http_server.py | 4 ++-- spyglass/server/webrtc_whep.py | 6 +++--- 8 files changed, 21 insertions(+), 6 deletions(-) diff --git a/resources/spyglass.conf b/resources/spyglass.conf index 8e2a7f2..f8356a5 100644 --- a/resources/spyglass.conf +++ b/resources/spyglass.conf @@ -35,6 +35,9 @@ SNAPSHOT_URL="/snapshot" #### NOTE: use format as shown below to stay MJPG-Streamer URL compatible ## SNAPSHOT_URL="/?action=snapshot" +#### WebRTC URL (STRING)[default: /webrtc] +WEBRTC_URL="/webrtc" + #### Autofocus behavior (STRING:manual,continuous)[default: continuous] AUTO_FOCUS="continuous" diff --git a/scripts/spyglass b/scripts/spyglass index e41d155..5cbe3e8 100755 --- a/scripts/spyglass +++ b/scripts/spyglass @@ -101,6 +101,7 @@ run_spyglass() { --fps "${FPS:-15}" \ --stream_url "${STREAM_URL:-\/stream}" \ --snapshot_url "${SNAPSHOT_URL:-\/snapshot}" \ + --webrtc_url "${WEBRTC_URL:-\/webrtc}" \ --autofocus "${AUTO_FOCUS:-continuous}" \ --lensposition "${FOCAL_DIST:-0.0}" \ --autofocusspeed "${AF_SPEED:-normal}" \ diff --git a/spyglass/camera/camera.py b/spyglass/camera/camera.py index a6aae99..f16dca5 100644 --- a/spyglass/camera/camera.py +++ b/spyglass/camera/camera.py @@ -66,18 +66,21 @@ def _run_server(self, get_frame, stream_url='/stream', snapshot_url='/snapshot', + webrtc_url='/webrtc', orientation_exif=0): logger.info(f'Server listening on {bind_address}:{port}') logger.info(f'Streaming endpoint: {stream_url}') logger.info(f'Snapshot endpoint: {snapshot_url}') + logger.info(f'WebRTC endpoint: {webrtc_url}') logger.info(f'Controls endpoint: /controls') - logger.info(f'WebRTC endpoint: /webrtc') address = (bind_address, port) streaming_handler.picam2 = self.picam2 streaming_handler.media_track = self.media_track streaming_handler.get_frame = get_frame streaming_handler.stream_url = stream_url streaming_handler.snapshot_url = snapshot_url + streaming_handler.webrtc_url = webrtc_url + if orientation_exif > 0: streaming_handler.exif_header = create_exif_header(orientation_exif) else: @@ -93,6 +96,7 @@ def start_and_run_server(self, port, stream_url='/stream', snapshot_url='/snapshot', + webrtc_url='/webrtc', orientation_exif=0): pass diff --git a/spyglass/camera/csi.py b/spyglass/camera/csi.py index 1b3f2ff..83af203 100644 --- a/spyglass/camera/csi.py +++ b/spyglass/camera/csi.py @@ -13,6 +13,7 @@ def start_and_run_server(self, port, stream_url='/stream', snapshot_url='/snapshot', + webrtc_url='/webrtc', orientation_exif=0): class StreamingOutput(io.BufferedIOBase): @@ -41,6 +42,7 @@ def get_frame(inner_self): get_frame, stream_url=stream_url, snapshot_url=snapshot_url, + webrtc_url=webrtc_url, orientation_exif=orientation_exif ) diff --git a/spyglass/camera/usb.py b/spyglass/camera/usb.py index 0aeff44..f4261f9 100644 --- a/spyglass/camera/usb.py +++ b/spyglass/camera/usb.py @@ -7,6 +7,7 @@ def start_and_run_server(self, port, stream_url='/stream', snapshot_url='/snapshot', + webrtc_url='/webrtc', orientation_exif=0): def get_frame(inner_self): #TODO: Cuts framerate in 1/n with n streams open, add some kind of buffer @@ -21,6 +22,7 @@ def get_frame(inner_self): get_frame, stream_url=stream_url, snapshot_url=snapshot_url, + webrtc_url=webrtc_url, orientation_exif=orientation_exif ) diff --git a/spyglass/cli.py b/spyglass/cli.py index 0924daf..f12f0ad 100644 --- a/spyglass/cli.py +++ b/spyglass/cli.py @@ -65,6 +65,7 @@ def main(args=None): parsed_args.port, parsed_args.stream_url, parsed_args.snapshot_url, + parsed_args.webrtc_url, parsed_args.orientation_exif) finally: cam.stop() @@ -144,6 +145,8 @@ def get_parser(): help='Sets the URL for the mjpeg stream') parser.add_argument('-sn', '--snapshot_url', type=str, default='/snapshot', help='Sets the URL for snapshots (single frame of stream)') + parser.add_argument('-w', '--webrtc_url', type=str, default='/webrtc', + help='Sets the URL for the WebRTC stream') parser.add_argument('-af', '--autofocus', type=str, default='continuous', choices=['manual', 'continuous'], help='Autofocus mode') parser.add_argument('-l', '--lensposition', type=float, default=0.0, diff --git a/spyglass/server/http_server.py b/spyglass/server/http_server.py index 75f19b7..f7957a6 100755 --- a/spyglass/server/http_server.py +++ b/spyglass/server/http_server.py @@ -33,7 +33,7 @@ def do_GET(self): def do_OPTIONS(self): if self.check_webrtc(): - webrtc_whep.do_OPTIONS(self) + webrtc_whep.do_OPTIONS(self, self.webrtc_url) else: self.send_error(codes.not_found) @@ -53,7 +53,7 @@ def check_url(self, url, match_full_path=True): return check_urls_match(url, self.path, match_full_path) def check_webrtc(self): - return self.check_url('/webrtc', match_full_path=False) + return self.check_url(self.webrtc_url, match_full_path=False) def run_async_request(self, method): asyncio.run_coroutine_threadsafe(method(self), StreamingHandler.loop).result() diff --git a/spyglass/server/webrtc_whep.py b/spyglass/server/webrtc_whep.py index 4b44cc5..5efd4dc 100644 --- a/spyglass/server/webrtc_whep.py +++ b/spyglass/server/webrtc_whep.py @@ -21,7 +21,7 @@ def send_default_headers(response_code: int, handler: 'StreamingHandler'): handler.send_header('Access-Control-Allow-Origin', '*') handler.send_header('Access-Control-Allow-Credentials', False) -def do_OPTIONS(handler: 'StreamingHandler'): +def do_OPTIONS(handler: 'StreamingHandler', webrtc_url='/webrtc'): # Adapted from MediaMTX http_server.go # https://github.com/bluenviron/mediamtx/blob/main/internal/servers/webrtc/http_server.go#L173-L189 def response_headers(): @@ -32,8 +32,8 @@ def response_headers(): if handler.headers.get("Access-Control-Request-Method") != None: response_headers() handler.end_headers() - elif check_urls_match('/webrtc/whip', handler.path) \ - or check_urls_match('/webrtc/whep', handler.path): + elif check_urls_match(f'{webrtc_url}/whip', handler.path) \ + or check_urls_match(f'{webrtc_url}/whep', handler.path): response_headers() handler.send_header('Access-Control-Expose-Headers', 'Link') handler.headers['Link'] = get_ICE_servers() From 6404037f68fcb3148faf4c7ad59b0d373a7010fa Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Wed, 8 Jan 2025 23:16:52 +0100 Subject: [PATCH 37/55] chore: fix indentation Signed-off-by: Patrick Gehrsitz --- spyglass/server/webrtc_whep.py | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/spyglass/server/webrtc_whep.py b/spyglass/server/webrtc_whep.py index 5efd4dc..5e3bf5c 100644 --- a/spyglass/server/webrtc_whep.py +++ b/spyglass/server/webrtc_whep.py @@ -112,23 +112,23 @@ def get_ICE_servers(): return None def parse_ice_candidates(sdp_message): - sdp_message = sdp_message.replace('\\r\\n', '\r\n') - - lines = sdp_message.splitlines() - - candidates = [] - cand_str = 'a=candidate:' - mid_str = 'a=mid:' - mid = '' - for line in lines: - if line.startswith(mid_str): - mid = line[len(mid_str):] - elif line.startswith(cand_str): - candidate_str = line[len(cand_str):] - candidate = sdp.candidate_from_sdp(candidate_str) - candidate.sdpMid = mid - candidates.append(candidate) - return candidates + sdp_message = sdp_message.replace('\\r\\n', '\r\n') + + lines = sdp_message.splitlines() + + candidates = [] + cand_str = 'a=candidate:' + mid_str = 'a=mid:' + mid = '' + for line in lines: + if line.startswith(mid_str): + mid = line[len(mid_str):] + elif line.startswith(cand_str): + candidate_str = line[len(cand_str):] + candidate = sdp.candidate_from_sdp(candidate_str) + candidate.sdpMid = mid + candidates.append(candidate) + return candidates import av From 91aad536e0bb4bdf8593788a9f8ab02cd7992d0e Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Wed, 8 Jan 2025 23:34:17 +0100 Subject: [PATCH 38/55] fix: fix default url Signed-off-by: Patrick Gehrsitz --- scripts/spyglass | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/spyglass b/scripts/spyglass index 5cbe3e8..dff0e34 100755 --- a/scripts/spyglass +++ b/scripts/spyglass @@ -99,9 +99,9 @@ run_spyglass() { --port "${HTTP_PORT:-8080}" \ --resolution "${RESOLUTION:-640x480}" \ --fps "${FPS:-15}" \ - --stream_url "${STREAM_URL:-\/stream}" \ - --snapshot_url "${SNAPSHOT_URL:-\/snapshot}" \ - --webrtc_url "${WEBRTC_URL:-\/webrtc}" \ + --stream_url "${STREAM_URL:-/stream}" \ + --snapshot_url "${SNAPSHOT_URL:-/snapshot}" \ + --webrtc_url "${WEBRTC_URL:-/webrtc}" \ --autofocus "${AUTO_FOCUS:-continuous}" \ --lensposition "${FOCAL_DIST:-0.0}" \ --autofocusspeed "${AF_SPEED:-normal}" \ From 4d2f601219563f27b1c03bf3a29bbd022e49e854 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Wed, 8 Jan 2025 23:40:44 +0100 Subject: [PATCH 39/55] fix: fix crash if aiortc is missing and disable webrtc instead Signed-off-by: Patrick Gehrsitz --- spyglass/__init__.py | 6 ++++++ spyglass/camera/camera.py | 5 +++-- spyglass/camera/csi.py | 5 +++-- spyglass/server/http_server.py | 3 ++- spyglass/server/webrtc_whep.py | 20 +++++++++++++------- 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/spyglass/__init__.py b/spyglass/__init__.py index ca7c8bc..e57daac 100644 --- a/spyglass/__init__.py +++ b/spyglass/__init__.py @@ -1,5 +1,11 @@ """init py module.""" import logging +import importlib logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) + +if importlib.util.find_spec("aiortc"): + AIORTC_AVAILABLE=True +else: + AIORTC_AVAILABLE=False diff --git a/spyglass/camera/camera.py b/spyglass/camera/camera.py index f16dca5..316554e 100644 --- a/spyglass/camera/camera.py +++ b/spyglass/camera/camera.py @@ -4,7 +4,7 @@ from abc import ABC, abstractmethod from picamera2 import Picamera2 -from spyglass import logger +from spyglass import logger, AIORTC_AVAILABLE from spyglass.exif import create_exif_header from spyglass.camera_options import process_controls from spyglass.server.http_server import StreamingServer, StreamingHandler @@ -71,7 +71,8 @@ def _run_server(self, logger.info(f'Server listening on {bind_address}:{port}') logger.info(f'Streaming endpoint: {stream_url}') logger.info(f'Snapshot endpoint: {snapshot_url}') - logger.info(f'WebRTC endpoint: {webrtc_url}') + if AIORTC_AVAILABLE: + logger.info(f'WebRTC endpoint: {webrtc_url}') logger.info(f'Controls endpoint: /controls') address = (bind_address, port) streaming_handler.picam2 = self.picam2 diff --git a/spyglass/camera/csi.py b/spyglass/camera/csi.py index 83af203..e165c6f 100644 --- a/spyglass/camera/csi.py +++ b/spyglass/camera/csi.py @@ -4,7 +4,7 @@ from picamera2.outputs import FileOutput from threading import Condition -from spyglass import camera +from spyglass import camera, AIORTC_AVAILABLE from spyglass.server.http_server import StreamingHandler class CSI(camera.Camera): @@ -32,7 +32,8 @@ def get_frame(inner_self): return output.frame self.picam2.start_encoder(MJPEGEncoder(), FileOutput(output)) - self.picam2.start_encoder(H264Encoder(), self.media_track) + if AIORTC_AVAILABLE: + self.picam2.start_encoder(H264Encoder(), self.media_track) self.picam2.start() self._run_server( diff --git a/spyglass/server/http_server.py b/spyglass/server/http_server.py index f7957a6..acfbe89 100755 --- a/spyglass/server/http_server.py +++ b/spyglass/server/http_server.py @@ -7,6 +7,7 @@ from http import server from requests import codes +from spyglass import AIORTC_AVAILABLE from spyglass.server import jpeg, webrtc_whep, controls from spyglass.url_parsing import check_urls_match @@ -53,7 +54,7 @@ def check_url(self, url, match_full_path=True): return check_urls_match(url, self.path, match_full_path) def check_webrtc(self): - return self.check_url(self.webrtc_url, match_full_path=False) + return AIORTC_AVAILABLE and self.check_url(self.webrtc_url, match_full_path=False) def run_async_request(self, method): asyncio.run_coroutine_threadsafe(method(self), StreamingHandler.loop).result() diff --git a/spyglass/server/webrtc_whep.py b/spyglass/server/webrtc_whep.py index 5e3bf5c..86b7952 100644 --- a/spyglass/server/webrtc_whep.py +++ b/spyglass/server/webrtc_whep.py @@ -2,9 +2,6 @@ import asyncio from requests import codes -from aiortc import RTCSessionDescription, RTCPeerConnection, sdp -from aiortc.rtcrtpsender import RTCRtpSender -from aiortc.contrib.media import MediaRelay from spyglass.url_parsing import check_urls_match @@ -13,8 +10,13 @@ if TYPE_CHECKING: from spyglass.server.http_server import StreamingHandler -pcs: dict[uuid.UUID, RTCPeerConnection] = {} -media_relay = MediaRelay() +from spyglass import AIORTC_AVAILABLE +if AIORTC_AVAILABLE: + from aiortc import RTCSessionDescription, RTCPeerConnection, sdp + from aiortc.rtcrtpsender import RTCRtpSender + from aiortc.contrib.media import MediaRelay + pcs: dict[uuid.UUID, RTCPeerConnection] = {} + media_relay = MediaRelay() def send_default_headers(response_code: int, handler: 'StreamingHandler'): handler.send_response(response_code) @@ -130,9 +132,13 @@ def parse_ice_candidates(sdp_message): candidates.append(candidate) return candidates -import av -from aiortc import MediaStreamTrack +if AIORTC_AVAILABLE: + import av + from aiortc import MediaStreamTrack +else: + class MediaStreamTrack(): + pass from collections import deque from fractions import Fraction from picamera2.outputs import Output From 1d15beef7f0d14f5f62c4d167e867298e63e61e4 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Thu, 9 Jan 2025 00:30:05 +0100 Subject: [PATCH 40/55] feat: add option to disable WebRTC Signed-off-by: Patrick Gehrsitz --- scripts/spyglass | 7 +++++++ spyglass/cli.py | 7 ++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/scripts/spyglass b/scripts/spyglass index dff0e34..894e4a4 100755 --- a/scripts/spyglass +++ b/scripts/spyglass @@ -93,6 +93,12 @@ run_spyglass() { bind_adress="0.0.0.0" fi + if [[ "${DEACTIVATE_WEBRTC:-false}" == "true" ]]; then + deactivate_webrtc="--deactivate_webrtc" + else + deactivate_webrtc="" + fi + "${PY_BIN}" "$(dirname "${BASE_SPY_PATH}")/run.py" \ --camera_num "${CAMERA_NUM:-0}" \ --bindaddress "${bind_adress}" \ @@ -102,6 +108,7 @@ run_spyglass() { --stream_url "${STREAM_URL:-/stream}" \ --snapshot_url "${SNAPSHOT_URL:-/snapshot}" \ --webrtc_url "${WEBRTC_URL:-/webrtc}" \ + ${deactivate_webrtc} \ --autofocus "${AUTO_FOCUS:-continuous}" \ --lensposition "${FOCAL_DIST:-0.0}" \ --autofocusspeed "${AF_SPEED:-normal}" \ diff --git a/spyglass/cli.py b/spyglass/cli.py index f12f0ad..eed5523 100644 --- a/spyglass/cli.py +++ b/spyglass/cli.py @@ -8,7 +8,7 @@ import sys import libcamera -from spyglass import camera_options, logger +from spyglass import camera_options, logger, AIORTC_AVAILABLE from spyglass.exif import option_to_exif_orientation from spyglass.__version__ import __version__ from spyglass.camera import init_camera @@ -19,6 +19,7 @@ def main(args=None): + global AIORTC_AVAILABLE """Entry point for hello cli. The setup_py entry_point wraps this in sys.exit already so this effectively @@ -45,6 +46,8 @@ def main(args=None): if parsed_args.controls_string: controls += [c.split('=') for c in parsed_args.controls_string.split(',')] + AIORTC_AVAILABLE = AIORTC_AVAILABLE and not parsed_args.deactivate_webrtc + cam = init_camera( parsed_args.camera_num, parsed_args.tuning_filter, @@ -147,6 +150,8 @@ def get_parser(): help='Sets the URL for snapshots (single frame of stream)') parser.add_argument('-w', '--webrtc_url', type=str, default='/webrtc', help='Sets the URL for the WebRTC stream') + parser.add_argument('--deactivate_webrtc', action='store_true', + help='Deactivates WebRTC encoding (recommended on Pi5)') parser.add_argument('-af', '--autofocus', type=str, default='continuous', choices=['manual', 'continuous'], help='Autofocus mode') parser.add_argument('-l', '--lensposition', type=float, default=0.0, From cb8434e131b5c9775bfb785f13554120b1f8bbeb Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Thu, 9 Jan 2025 00:33:26 +0100 Subject: [PATCH 41/55] chore: remove single comma Signed-off-by: Patrick Gehrsitz --- spyglass/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spyglass/cli.py b/spyglass/cli.py index eed5523..41f23f2 100644 --- a/spyglass/cli.py +++ b/spyglass/cli.py @@ -62,7 +62,7 @@ def main(args=None): controls, parsed_args.upsidedown, parsed_args.flip_horizontal, - parsed_args.flip_vertical,) + parsed_args.flip_vertical) try: cam.start_and_run_server(parsed_args.bindaddress, parsed_args.port, From f733f5115fd545ef39f5b65b0c8545981103ef7d Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Fri, 10 Jan 2025 22:25:35 +0100 Subject: [PATCH 42/55] chore: change naming from deactivate to disable Signed-off-by: Patrick Gehrsitz --- scripts/spyglass | 8 ++++---- spyglass/cli.py | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/scripts/spyglass b/scripts/spyglass index 894e4a4..c9c9ced 100755 --- a/scripts/spyglass +++ b/scripts/spyglass @@ -93,10 +93,10 @@ run_spyglass() { bind_adress="0.0.0.0" fi - if [[ "${DEACTIVATE_WEBRTC:-false}" == "true" ]]; then - deactivate_webrtc="--deactivate_webrtc" + if [[ "${DISABLE_WEBRTC:-false}" == "true" ]]; then + disable_webrtc="--disable_webrtc" else - deactivate_webrtc="" + disable_webrtc="" fi "${PY_BIN}" "$(dirname "${BASE_SPY_PATH}")/run.py" \ @@ -108,7 +108,7 @@ run_spyglass() { --stream_url "${STREAM_URL:-/stream}" \ --snapshot_url "${SNAPSHOT_URL:-/snapshot}" \ --webrtc_url "${WEBRTC_URL:-/webrtc}" \ - ${deactivate_webrtc} \ + ${disable_webrtc} \ --autofocus "${AUTO_FOCUS:-continuous}" \ --lensposition "${FOCAL_DIST:-0.0}" \ --autofocusspeed "${AF_SPEED:-normal}" \ diff --git a/spyglass/cli.py b/spyglass/cli.py index 41f23f2..a5cd073 100644 --- a/spyglass/cli.py +++ b/spyglass/cli.py @@ -46,7 +46,7 @@ def main(args=None): if parsed_args.controls_string: controls += [c.split('=') for c in parsed_args.controls_string.split(',')] - AIORTC_AVAILABLE = AIORTC_AVAILABLE and not parsed_args.deactivate_webrtc + AIORTC_AVAILABLE = AIORTC_AVAILABLE and not parsed_args.disable_webrtc cam = init_camera( parsed_args.camera_num, @@ -150,8 +150,8 @@ def get_parser(): help='Sets the URL for snapshots (single frame of stream)') parser.add_argument('-w', '--webrtc_url', type=str, default='/webrtc', help='Sets the URL for the WebRTC stream') - parser.add_argument('--deactivate_webrtc', action='store_true', - help='Deactivates WebRTC encoding (recommended on Pi5)') + parser.add_argument('--disable_webrtc', action='store_true', + help='Disables WebRTC encoding (recommended on Pi5)') parser.add_argument('-af', '--autofocus', type=str, default='continuous', choices=['manual', 'continuous'], help='Autofocus mode') parser.add_argument('-l', '--lensposition', type=float, default=0.0, From 7f3bb6ea1044cc19e81f406512f7d505667eb9a2 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Fri, 10 Jan 2025 22:25:58 +0100 Subject: [PATCH 43/55] fix: add missing config option Signed-off-by: Patrick Gehrsitz --- resources/spyglass.conf | 3 +++ 1 file changed, 3 insertions(+) diff --git a/resources/spyglass.conf b/resources/spyglass.conf index f8356a5..1bcb749 100644 --- a/resources/spyglass.conf +++ b/resources/spyglass.conf @@ -38,6 +38,9 @@ SNAPSHOT_URL="/snapshot" #### WebRTC URL (STRING)[default: /webrtc] WEBRTC_URL="/webrtc" +#### Uncomment to disable WebRTC +#DISABLE_WEBRTC="true" + #### Autofocus behavior (STRING:manual,continuous)[default: continuous] AUTO_FOCUS="continuous" From ed1797afa6eac5bffc981c3251c406997bd3b54e Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Fri, 10 Jan 2025 22:29:37 +0100 Subject: [PATCH 44/55] chore: rename AIORTC_AVAILABLE to WEBRTC_ENABLED Signed-off-by: Patrick Gehrsitz --- spyglass/__init__.py | 4 ++-- spyglass/camera/camera.py | 4 ++-- spyglass/camera/csi.py | 4 ++-- spyglass/cli.py | 6 +++--- spyglass/server/http_server.py | 4 ++-- spyglass/server/webrtc_whep.py | 6 +++--- 6 files changed, 14 insertions(+), 14 deletions(-) diff --git a/spyglass/__init__.py b/spyglass/__init__.py index e57daac..b905352 100644 --- a/spyglass/__init__.py +++ b/spyglass/__init__.py @@ -6,6 +6,6 @@ logger = logging.getLogger(__name__) if importlib.util.find_spec("aiortc"): - AIORTC_AVAILABLE=True + WEBRTC_ENABLED=True else: - AIORTC_AVAILABLE=False + WEBRTC_ENABLED=False diff --git a/spyglass/camera/camera.py b/spyglass/camera/camera.py index 316554e..c82f607 100644 --- a/spyglass/camera/camera.py +++ b/spyglass/camera/camera.py @@ -4,7 +4,7 @@ from abc import ABC, abstractmethod from picamera2 import Picamera2 -from spyglass import logger, AIORTC_AVAILABLE +from spyglass import logger, WEBRTC_ENABLED from spyglass.exif import create_exif_header from spyglass.camera_options import process_controls from spyglass.server.http_server import StreamingServer, StreamingHandler @@ -71,7 +71,7 @@ def _run_server(self, logger.info(f'Server listening on {bind_address}:{port}') logger.info(f'Streaming endpoint: {stream_url}') logger.info(f'Snapshot endpoint: {snapshot_url}') - if AIORTC_AVAILABLE: + if WEBRTC_ENABLED: logger.info(f'WebRTC endpoint: {webrtc_url}') logger.info(f'Controls endpoint: /controls') address = (bind_address, port) diff --git a/spyglass/camera/csi.py b/spyglass/camera/csi.py index e165c6f..a611bba 100644 --- a/spyglass/camera/csi.py +++ b/spyglass/camera/csi.py @@ -4,7 +4,7 @@ from picamera2.outputs import FileOutput from threading import Condition -from spyglass import camera, AIORTC_AVAILABLE +from spyglass import camera, WEBRTC_ENABLED from spyglass.server.http_server import StreamingHandler class CSI(camera.Camera): @@ -32,7 +32,7 @@ def get_frame(inner_self): return output.frame self.picam2.start_encoder(MJPEGEncoder(), FileOutput(output)) - if AIORTC_AVAILABLE: + if WEBRTC_ENABLED: self.picam2.start_encoder(H264Encoder(), self.media_track) self.picam2.start() diff --git a/spyglass/cli.py b/spyglass/cli.py index a5cd073..b68b08f 100644 --- a/spyglass/cli.py +++ b/spyglass/cli.py @@ -8,7 +8,7 @@ import sys import libcamera -from spyglass import camera_options, logger, AIORTC_AVAILABLE +from spyglass import camera_options, logger, WEBRTC_ENABLED from spyglass.exif import option_to_exif_orientation from spyglass.__version__ import __version__ from spyglass.camera import init_camera @@ -19,7 +19,7 @@ def main(args=None): - global AIORTC_AVAILABLE + global WEBRTC_ENABLED """Entry point for hello cli. The setup_py entry_point wraps this in sys.exit already so this effectively @@ -46,7 +46,7 @@ def main(args=None): if parsed_args.controls_string: controls += [c.split('=') for c in parsed_args.controls_string.split(',')] - AIORTC_AVAILABLE = AIORTC_AVAILABLE and not parsed_args.disable_webrtc + WEBRTC_ENABLED = WEBRTC_ENABLED and not parsed_args.disable_webrtc cam = init_camera( parsed_args.camera_num, diff --git a/spyglass/server/http_server.py b/spyglass/server/http_server.py index acfbe89..29899f2 100755 --- a/spyglass/server/http_server.py +++ b/spyglass/server/http_server.py @@ -7,7 +7,7 @@ from http import server from requests import codes -from spyglass import AIORTC_AVAILABLE +from spyglass import WEBRTC_ENABLED from spyglass.server import jpeg, webrtc_whep, controls from spyglass.url_parsing import check_urls_match @@ -54,7 +54,7 @@ def check_url(self, url, match_full_path=True): return check_urls_match(url, self.path, match_full_path) def check_webrtc(self): - return AIORTC_AVAILABLE and self.check_url(self.webrtc_url, match_full_path=False) + return WEBRTC_ENABLED and self.check_url(self.webrtc_url, match_full_path=False) def run_async_request(self, method): asyncio.run_coroutine_threadsafe(method(self), StreamingHandler.loop).result() diff --git a/spyglass/server/webrtc_whep.py b/spyglass/server/webrtc_whep.py index 86b7952..cd19145 100644 --- a/spyglass/server/webrtc_whep.py +++ b/spyglass/server/webrtc_whep.py @@ -10,8 +10,8 @@ if TYPE_CHECKING: from spyglass.server.http_server import StreamingHandler -from spyglass import AIORTC_AVAILABLE -if AIORTC_AVAILABLE: +from spyglass import WEBRTC_ENABLED +if WEBRTC_ENABLED: from aiortc import RTCSessionDescription, RTCPeerConnection, sdp from aiortc.rtcrtpsender import RTCRtpSender from aiortc.contrib.media import MediaRelay @@ -133,7 +133,7 @@ def parse_ice_candidates(sdp_message): return candidates -if AIORTC_AVAILABLE: +if WEBRTC_ENABLED: import av from aiortc import MediaStreamTrack else: From 428b35767fa82df864732c94f7dcd8af4a7defcb Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Fri, 10 Jan 2025 23:45:51 +0100 Subject: [PATCH 45/55] fix: fix potential import issue Signed-off-by: Patrick Gehrsitz --- spyglass/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spyglass/__init__.py b/spyglass/__init__.py index b905352..a65fc77 100644 --- a/spyglass/__init__.py +++ b/spyglass/__init__.py @@ -1,6 +1,6 @@ """init py module.""" import logging -import importlib +import importlib.util logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) From b8f08a509f65546e1729d020d77f609e58537496 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Fri, 10 Jan 2025 23:46:48 +0100 Subject: [PATCH 46/55] feat: update installer to setup venv Signed-off-by: Patrick Gehrsitz --- Makefile | 7 +++++++ scripts/spyglass | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index fdf1648..da4cb2a 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,14 @@ all: $(MAKE) help install: ## Install Spyglass as service + @if [ "$$(id -u)" -eq 0 ]; then \ + echo "Please run without sudo/not as root"; \ + exit 1; \ + fi @mkdir -p $(CONF_PATH) + @printf "\nInstall virtual environment ...\n" + @python -m venv --system-site-packages .venv + @. .venv/bin/activate && pip install -r requirements.txt @printf "\nCopying systemd service file ...\n" @sudo cp -f "${PWD}/resources/spyglass.service" $(SYSTEMD) @sudo sed -i "s/%USER%/$(USER)/g" $(SYSTEMD)/spyglass.service diff --git a/scripts/spyglass b/scripts/spyglass index c9c9ced..308f641 100755 --- a/scripts/spyglass +++ b/scripts/spyglass @@ -14,7 +14,7 @@ set -Eeou pipefail ### Global Variables BASE_SPY_PATH="$(dirname "$(readlink -f "${0}")")" -PY_BIN="$(command -v python)" +PY_BIN="$(. .venv/bin/activate && command -v python)" SPYGLASS_CFG="${HOME}/printer_data/config/spyglass.conf" ### Helper Messages From 1f5ae2a64513a82dc3bb3af79acd9b9260525ad1 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Fri, 10 Jan 2025 23:50:42 +0100 Subject: [PATCH 47/55] chore: change wording Signed-off-by: Patrick Gehrsitz --- resources/spyglass.conf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/spyglass.conf b/resources/spyglass.conf index 1bcb749..d27950a 100644 --- a/resources/spyglass.conf +++ b/resources/spyglass.conf @@ -38,7 +38,7 @@ SNAPSHOT_URL="/snapshot" #### WebRTC URL (STRING)[default: /webrtc] WEBRTC_URL="/webrtc" -#### Uncomment to disable WebRTC +#### Disable WebRTC #DISABLE_WEBRTC="true" #### Autofocus behavior (STRING:manual,continuous)[default: continuous] From 00cc611d1f4017d5e8ee80bab58679ca219610bf Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Sun, 12 Jan 2025 17:04:49 +0100 Subject: [PATCH 48/55] fix: remove requests package dependency Signed-off-by: Patrick Gehrsitz --- spyglass/server/controls.py | 5 ++--- spyglass/server/http_server.py | 11 +++++------ spyglass/server/jpeg.py | 5 ++--- spyglass/server/requests_codes.py | 6 ++++++ spyglass/server/webrtc_whep.py | 13 ++++++------- 5 files changed, 21 insertions(+), 19 deletions(-) create mode 100644 spyglass/server/requests_codes.py diff --git a/spyglass/server/controls.py b/spyglass/server/controls.py index 0a88141..22532e3 100644 --- a/spyglass/server/controls.py +++ b/spyglass/server/controls.py @@ -1,5 +1,4 @@ -from requests import codes - +from spyglass.server import requests_codes from spyglass.url_parsing import get_url_params from spyglass.camera_options import parse_dictionary_to_html_page, process_controls @@ -13,7 +12,7 @@ def do_GET(handler: 'StreamingHandler'): processed_controls = process_controls(handler.picam2, parsed_controls) handler.picam2.set_controls(processed_controls) content = parse_dictionary_to_html_page(handler.picam2, parsed_controls, processed_controls).encode('utf-8') - handler.send_response(codes.ok) + handler.send_response(requests_codes.OK) handler.send_header('Content-Type', 'text/html') handler.send_header('Content-Length', len(content)) handler.end_headers() diff --git a/spyglass/server/http_server.py b/spyglass/server/http_server.py index 29899f2..b0c93db 100755 --- a/spyglass/server/http_server.py +++ b/spyglass/server/http_server.py @@ -5,10 +5,9 @@ import socketserver from http import server -from requests import codes from spyglass import WEBRTC_ENABLED -from spyglass.server import jpeg, webrtc_whep, controls +from spyglass.server import jpeg, webrtc_whep, controls, requests_codes from spyglass.url_parsing import check_urls_match class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer): @@ -30,25 +29,25 @@ def do_GET(self): elif self.check_webrtc(): pass else: - self.send_error(codes.not_found) + self.send_error(requests_codes.NOT_FOUND) def do_OPTIONS(self): if self.check_webrtc(): webrtc_whep.do_OPTIONS(self, self.webrtc_url) else: - self.send_error(codes.not_found) + self.send_error(requests_codes.NOT_FOUND) def do_POST(self): if self.check_webrtc(): self.run_async_request(webrtc_whep.do_POST_async) else: - self.send_error(codes.not_found) + self.send_error(requests_codes.NOT_FOUND) def do_PATCH(self): if self.check_webrtc(): self.run_async_request(webrtc_whep.do_PATCH_async) else: - self.send_error(codes.not_found) + self.send_error(requests_codes.NOT_FOUND) def check_url(self, url, match_full_path=True): return check_urls_match(url, self.path, match_full_path) diff --git a/spyglass/server/jpeg.py b/spyglass/server/jpeg.py index 25c3dbc..7a15ced 100644 --- a/spyglass/server/jpeg.py +++ b/spyglass/server/jpeg.py @@ -1,6 +1,5 @@ -from requests import codes - from spyglass import logger +from spyglass.server import requests_codes # Used for type hinting from typing import TYPE_CHECKING @@ -44,7 +43,7 @@ def send_snapshot(handler: 'StreamingHandler'): handler.client_address, str(e)) def send_default_headers(handler: 'StreamingHandler'): - handler.send_response(codes.ok) + handler.send_response(requests_codes.OK) handler.send_header('Age', 0) handler.send_header('Cache-Control', 'no-cache, private') handler.send_header('Pragma', 'no-cache') diff --git a/spyglass/server/requests_codes.py b/spyglass/server/requests_codes.py new file mode 100644 index 0000000..4fd4d00 --- /dev/null +++ b/spyglass/server/requests_codes.py @@ -0,0 +1,6 @@ +OK = 200 +CREATED = 201 +NO_CONTENT = 204 + +BAD = 400 +NOT_FOUND = 404 diff --git a/spyglass/server/webrtc_whep.py b/spyglass/server/webrtc_whep.py index cd19145..0b09581 100644 --- a/spyglass/server/webrtc_whep.py +++ b/spyglass/server/webrtc_whep.py @@ -1,9 +1,8 @@ import uuid import asyncio -from requests import codes - from spyglass.url_parsing import check_urls_match +from spyglass.server import requests_codes # Used for type hinting from typing import TYPE_CHECKING @@ -27,7 +26,7 @@ def do_OPTIONS(handler: 'StreamingHandler', webrtc_url='/webrtc'): # Adapted from MediaMTX http_server.go # https://github.com/bluenviron/mediamtx/blob/main/internal/servers/webrtc/http_server.go#L173-L189 def response_headers(): - send_default_headers(codes.no_content, handler) + send_default_headers(requests_codes.NO_CONTENT, handler) handler.send_header('Access-Control-Allow-Methods', 'OPTIONS, GET, POST, PATCH, DELETE') handler.send_header('Access-Control-Allow-Headers', 'Authorization, Content-Type, If-Match') @@ -45,7 +44,7 @@ async def do_POST_async(handler: 'StreamingHandler'): # Adapted from MediaMTX http_server.go # https://github.com/bluenviron/mediamtx/blob/main/internal/servers/webrtc/http_server.go#L191-L246 if handler.headers.get("Content-Type") != "application/sdp": - handler.send_error(codes.bad) + handler.send_error(requests_codes.BAD) return content_length = int(handler.headers['Content-Length']) offer_text = handler.rfile.read(content_length).decode('utf-8') @@ -77,7 +76,7 @@ async def on_connectionstatechange(): while pc.iceGatheringState != "complete": await asyncio.sleep(1) - send_default_headers(codes.created, handler) + send_default_headers(requests_codes.CREATED, handler) handler.send_header("Content-Type", "application/sdp") handler.send_header("ETag", "*") @@ -96,7 +95,7 @@ async def do_PATCH_async(streaming_handler: 'StreamingHandler'): # https://github.com/bluenviron/mediamtx/blob/main/internal/servers/webrtc/http_server.go#L248-L287 if len(streaming_handler.path.split('/')) < 3 \ or streaming_handler.headers.get('Content-Type') != 'application/trickle-ice-sdpfrag': - send_default_headers(codes.bad, streaming_handler) + send_default_headers(requests_codes.BAD, streaming_handler) streaming_handler.end_headers() return content_length = int(streaming_handler.headers['Content-Length']) @@ -107,7 +106,7 @@ async def do_PATCH_async(streaming_handler: 'StreamingHandler'): for candidate in candidates: await pc.addIceCandidate(candidate) - send_default_headers(codes.no_content, streaming_handler) + send_default_headers(requests_codes.NO_CONTENT, streaming_handler) streaming_handler.end_headers() def get_ICE_servers(): From d511c0624493fda96a3f2619b007d355eaae30c9 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Sun, 12 Jan 2025 20:50:36 +0100 Subject: [PATCH 49/55] fix: move init_camera import to set WEBRTC_ENABLED correct first Signed-off-by: Patrick Gehrsitz --- spyglass/cli.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/spyglass/cli.py b/spyglass/cli.py index b68b08f..b0c2b54 100644 --- a/spyglass/cli.py +++ b/spyglass/cli.py @@ -11,7 +11,6 @@ from spyglass import camera_options, logger, WEBRTC_ENABLED from spyglass.exif import option_to_exif_orientation from spyglass.__version__ import __version__ -from spyglass.camera import init_camera MAX_WIDTH = 1920 @@ -48,6 +47,9 @@ def main(args=None): WEBRTC_ENABLED = WEBRTC_ENABLED and not parsed_args.disable_webrtc + # Has to be imported after WEBRTC_ENABLED got set correctly + from spyglass.camera import init_camera + cam = init_camera( parsed_args.camera_num, parsed_args.tuning_filter, From 61e4945f63023b1c672b07c090511c853d8235be Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Mon, 13 Jan 2025 00:09:24 +0100 Subject: [PATCH 50/55] fix: use http library for HTTPStatus codes Signed-off-by: Patrick Gehrsitz --- spyglass/server/controls.py | 4 ++-- spyglass/server/http_server.py | 12 ++++++------ spyglass/server/jpeg.py | 4 ++-- spyglass/server/requests_codes.py | 6 ------ spyglass/server/webrtc_whep.py | 12 ++++++------ 5 files changed, 16 insertions(+), 22 deletions(-) delete mode 100644 spyglass/server/requests_codes.py diff --git a/spyglass/server/controls.py b/spyglass/server/controls.py index 22532e3..4640bf9 100644 --- a/spyglass/server/controls.py +++ b/spyglass/server/controls.py @@ -1,4 +1,4 @@ -from spyglass.server import requests_codes +from http import HTTPStatus from spyglass.url_parsing import get_url_params from spyglass.camera_options import parse_dictionary_to_html_page, process_controls @@ -12,7 +12,7 @@ def do_GET(handler: 'StreamingHandler'): processed_controls = process_controls(handler.picam2, parsed_controls) handler.picam2.set_controls(processed_controls) content = parse_dictionary_to_html_page(handler.picam2, parsed_controls, processed_controls).encode('utf-8') - handler.send_response(requests_codes.OK) + handler.send_response(HTTPStatus.OK) handler.send_header('Content-Type', 'text/html') handler.send_header('Content-Length', len(content)) handler.end_headers() diff --git a/spyglass/server/http_server.py b/spyglass/server/http_server.py index b0c93db..0e83c5d 100755 --- a/spyglass/server/http_server.py +++ b/spyglass/server/http_server.py @@ -4,10 +4,10 @@ import asyncio import socketserver -from http import server +from http import server, HTTPStatus from spyglass import WEBRTC_ENABLED -from spyglass.server import jpeg, webrtc_whep, controls, requests_codes +from spyglass.server import jpeg, webrtc_whep, controls from spyglass.url_parsing import check_urls_match class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer): @@ -29,25 +29,25 @@ def do_GET(self): elif self.check_webrtc(): pass else: - self.send_error(requests_codes.NOT_FOUND) + self.send_error(HTTPStatus.NOT_FOUND) def do_OPTIONS(self): if self.check_webrtc(): webrtc_whep.do_OPTIONS(self, self.webrtc_url) else: - self.send_error(requests_codes.NOT_FOUND) + self.send_error(HTTPStatus.NOT_FOUND) def do_POST(self): if self.check_webrtc(): self.run_async_request(webrtc_whep.do_POST_async) else: - self.send_error(requests_codes.NOT_FOUND) + self.send_error(HTTPStatus.NOT_FOUND) def do_PATCH(self): if self.check_webrtc(): self.run_async_request(webrtc_whep.do_PATCH_async) else: - self.send_error(requests_codes.NOT_FOUND) + self.send_error(HTTPStatus.NOT_FOUND) def check_url(self, url, match_full_path=True): return check_urls_match(url, self.path, match_full_path) diff --git a/spyglass/server/jpeg.py b/spyglass/server/jpeg.py index 7a15ced..3c9d136 100644 --- a/spyglass/server/jpeg.py +++ b/spyglass/server/jpeg.py @@ -1,5 +1,5 @@ from spyglass import logger -from spyglass.server import requests_codes +from http import HTTPStatus # Used for type hinting from typing import TYPE_CHECKING @@ -43,7 +43,7 @@ def send_snapshot(handler: 'StreamingHandler'): handler.client_address, str(e)) def send_default_headers(handler: 'StreamingHandler'): - handler.send_response(requests_codes.OK) + handler.send_response(HTTPStatus.OK) handler.send_header('Age', 0) handler.send_header('Cache-Control', 'no-cache, private') handler.send_header('Pragma', 'no-cache') diff --git a/spyglass/server/requests_codes.py b/spyglass/server/requests_codes.py deleted file mode 100644 index 4fd4d00..0000000 --- a/spyglass/server/requests_codes.py +++ /dev/null @@ -1,6 +0,0 @@ -OK = 200 -CREATED = 201 -NO_CONTENT = 204 - -BAD = 400 -NOT_FOUND = 404 diff --git a/spyglass/server/webrtc_whep.py b/spyglass/server/webrtc_whep.py index 0b09581..2496eba 100644 --- a/spyglass/server/webrtc_whep.py +++ b/spyglass/server/webrtc_whep.py @@ -2,7 +2,7 @@ import asyncio from spyglass.url_parsing import check_urls_match -from spyglass.server import requests_codes +from http import HTTPStatus # Used for type hinting from typing import TYPE_CHECKING @@ -26,7 +26,7 @@ def do_OPTIONS(handler: 'StreamingHandler', webrtc_url='/webrtc'): # Adapted from MediaMTX http_server.go # https://github.com/bluenviron/mediamtx/blob/main/internal/servers/webrtc/http_server.go#L173-L189 def response_headers(): - send_default_headers(requests_codes.NO_CONTENT, handler) + send_default_headers(HTTPStatus.NO_CONTENT, handler) handler.send_header('Access-Control-Allow-Methods', 'OPTIONS, GET, POST, PATCH, DELETE') handler.send_header('Access-Control-Allow-Headers', 'Authorization, Content-Type, If-Match') @@ -44,7 +44,7 @@ async def do_POST_async(handler: 'StreamingHandler'): # Adapted from MediaMTX http_server.go # https://github.com/bluenviron/mediamtx/blob/main/internal/servers/webrtc/http_server.go#L191-L246 if handler.headers.get("Content-Type") != "application/sdp": - handler.send_error(requests_codes.BAD) + handler.send_error(HTTPStatus.BAD_REQUEST) return content_length = int(handler.headers['Content-Length']) offer_text = handler.rfile.read(content_length).decode('utf-8') @@ -76,7 +76,7 @@ async def on_connectionstatechange(): while pc.iceGatheringState != "complete": await asyncio.sleep(1) - send_default_headers(requests_codes.CREATED, handler) + send_default_headers(HTTPStatus.CREATED, handler) handler.send_header("Content-Type", "application/sdp") handler.send_header("ETag", "*") @@ -95,7 +95,7 @@ async def do_PATCH_async(streaming_handler: 'StreamingHandler'): # https://github.com/bluenviron/mediamtx/blob/main/internal/servers/webrtc/http_server.go#L248-L287 if len(streaming_handler.path.split('/')) < 3 \ or streaming_handler.headers.get('Content-Type') != 'application/trickle-ice-sdpfrag': - send_default_headers(requests_codes.BAD, streaming_handler) + send_default_headers(HTTPStatus.BAD, streaming_handler) streaming_handler.end_headers() return content_length = int(streaming_handler.headers['Content-Length']) @@ -106,7 +106,7 @@ async def do_PATCH_async(streaming_handler: 'StreamingHandler'): for candidate in candidates: await pc.addIceCandidate(candidate) - send_default_headers(requests_codes.NO_CONTENT, streaming_handler) + send_default_headers(HTTPStatus.NO_CONTENT, streaming_handler) streaming_handler.end_headers() def get_ICE_servers(): From ea1bc2a555354d2cde5c9bc656f6b9d6e0d45d9a Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Mon, 13 Jan 2025 00:16:16 +0100 Subject: [PATCH 51/55] test: fix tests Signed-off-by: Patrick Gehrsitz --- tests/test_cli.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/test_cli.py b/tests/test_cli.py index 5de2ac1..db61b11 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -28,6 +28,7 @@ def mock_libraries(mocker): mock_picamera2 = MagicMock() mock_picamera2_encoders = MagicMock() mock_picamera2_outputs = MagicMock() + mock_picamera2_outputs.Output = object mocker.patch.dict('sys.modules', { 'libcamera': mock_libcamera, 'picamera2': mock_picamera2, @@ -191,6 +192,7 @@ def test_run_server_with_configuration_from_arguments(mock_init_camera, mock_run '-p', '1234', '-st', 'streaming-url', '-sn', 'snapshot-url', + '-w', 'webrtc-url', '-or', 'h' ]) cam_instance = mock_init_camera.return_value @@ -199,6 +201,7 @@ def test_run_server_with_configuration_from_arguments(mock_init_camera, mock_run 1234, 'streaming-url', 'snapshot-url', + 'webrtc-url', 1 ) @@ -223,6 +226,7 @@ def test_run_server_with_orientation(mock_init_camera, mock_run_server, input_va '-p', '1234', '-st', 'streaming-url', '-sn', 'snapshot-url', + '-w', 'webrtc-url', '-or', input_value ]) cam_instance = mock_init_camera.return_value @@ -231,5 +235,6 @@ def test_run_server_with_orientation(mock_init_camera, mock_run_server, input_va 1234, 'streaming-url', 'snapshot-url', + 'webrtc-url', expected_output ) From 2edee779fbc8f4ce5c826f4248c26268b2a4cd1d Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Mon, 13 Jan 2025 00:16:26 +0100 Subject: [PATCH 52/55] test: refactor code Signed-off-by: Patrick Gehrsitz --- tests/test_cli.py | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index db61b11..18910f2 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -81,19 +81,16 @@ def test_parse_tuning_filter_dir(): @patch("spyglass.camera.init_camera") def test_init_camera_with_defaults(mock_spyglass_camera,): from spyglass import cli - import spyglass.camera cli.main(args=[]) - spyglass.camera.init_camera.assert_called_once_with( + mock_spyglass_camera.assert_called_once_with( DEFAULT_CAMERA_NUM, DEFAULT_TUNING_FILTER, DEFAULT_TUNING_FILTER_DIR ) -@patch("spyglass.camera.camera.Camera.configure") @patch("spyglass.camera.init_camera") -def test_configure_with_defaults(mock_init_camera, mock_configure): +def test_configure_with_defaults(mock_init_camera): from spyglass import cli - cli.main(args=[]) cam_instance = mock_init_camera.return_value cam_instance.configure.assert_called_once_with( @@ -109,11 +106,9 @@ def test_configure_with_defaults(mock_init_camera, mock_configure): DEFAULT_FLIP_VERTICALLY ) -@patch("spyglass.camera.camera.Camera.configure") @patch("spyglass.camera.init_camera") -def test_configure_with_parameters(mock_init_camera, mock_configure): +def test_configure_with_parameters(mock_init_camera): from spyglass import cli - cli.main(args=[ '-n', '1', '-tf', 'test', @@ -158,11 +153,9 @@ def test_raise_error_when_height_greater_than_maximum(): ]) -@patch("spyglass.camera.camera.Camera.configure") @patch("spyglass.camera.init_camera") -def test_configure_camera_af_continuous_speed_fast(mock_init_camera, mock_configure): +def test_configure_camera_af_continuous_speed_fast(mock_init_camera): from spyglass import cli - cli.main(args=[ '-af', 'continuous', '-s', 'fast' @@ -182,11 +175,9 @@ def test_configure_camera_af_continuous_speed_fast(mock_init_camera, mock_config ) -@patch("spyglass.camera.csi.CSI.start_and_run_server") @patch("spyglass.camera.init_camera") -def test_run_server_with_configuration_from_arguments(mock_init_camera, mock_run_server): +def test_run_server_with_configuration_from_arguments(mock_init_camera): from spyglass import cli - cli.main(args=[ '-b', '1.2.3.4', '-p', '1234', @@ -206,7 +197,6 @@ def test_run_server_with_configuration_from_arguments(mock_init_camera, mock_run ) -@patch("spyglass.camera.csi.CSI.start_and_run_server") @patch("spyglass.camera.init_camera") @pytest.mark.parametrize("input_value, expected_output", [ ('h', 1), @@ -218,9 +208,8 @@ def test_run_server_with_configuration_from_arguments(mock_init_camera, mock_run ('mhr90', 7), ('r270', 8), ]) -def test_run_server_with_orientation(mock_init_camera, mock_run_server, input_value, expected_output): +def test_run_server_with_orientation(mock_init_camera, input_value, expected_output): from spyglass import cli - import spyglass.server cli.main(args=[ '-b', '1.2.3.4', '-p', '1234', From d0efecb24d3c4d4f209d27870368e771e6df4edd Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Mon, 13 Jan 2025 01:10:13 +0100 Subject: [PATCH 53/55] test: fix mocking of picamera2.outputs.Output Signed-off-by: Patrick Gehrsitz --- tests/test_cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 18910f2..98f7660 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -28,7 +28,7 @@ def mock_libraries(mocker): mock_picamera2 = MagicMock() mock_picamera2_encoders = MagicMock() mock_picamera2_outputs = MagicMock() - mock_picamera2_outputs.Output = object + mock_picamera2_outputs.Output = MagicMock mocker.patch.dict('sys.modules', { 'libcamera': mock_libcamera, 'picamera2': mock_picamera2, From 480f3c17d75760d1fdbd3a5526cd769815b2eb2b Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Tue, 14 Jan 2025 00:10:49 +0100 Subject: [PATCH 54/55] feat: limit max simultaneous webrtc connections Signed-off-by: Patrick Gehrsitz --- spyglass/server/webrtc_whep.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/spyglass/server/webrtc_whep.py b/spyglass/server/webrtc_whep.py index 2496eba..49e2aa2 100644 --- a/spyglass/server/webrtc_whep.py +++ b/spyglass/server/webrtc_whep.py @@ -15,6 +15,7 @@ from aiortc.rtcrtpsender import RTCRtpSender from aiortc.contrib.media import MediaRelay pcs: dict[uuid.UUID, RTCPeerConnection] = {} + max_connections = 20 media_relay = MediaRelay() def send_default_headers(response_code: int, handler: 'StreamingHandler'): @@ -46,6 +47,12 @@ async def do_POST_async(handler: 'StreamingHandler'): if handler.headers.get("Content-Type") != "application/sdp": handler.send_error(HTTPStatus.BAD_REQUEST) return + + # Limit simultanous clients to save resources + if len(pcs) >= max_connections: + handler.send_error(HTTPStatus.TOO_MANY_REQUESTS, message="Too many clients connected") + return + content_length = int(handler.headers['Content-Length']) offer_text = handler.rfile.read(content_length).decode('utf-8') offer = RTCSessionDescription(sdp=offer_text, type='offer') From 2bb3a33739980b617e64fa746592a5000fecb7fb Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Sat, 22 Feb 2025 17:29:24 +0100 Subject: [PATCH 55/55] docs: update readme with webrtc --- README.md | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5e7697d..0936ac6 100644 --- a/README.md +++ b/README.md @@ -46,18 +46,20 @@ On startup the following arguments are supported: | `-f`, `--fps` | Framerate in frames per second (fps). | `15` | | `-st`, `--stream_url` | Sets the URL for the mjpeg stream. | `/stream` | | `-sn`, `--snapshot_url` | Sets the URL for snapshots (single frame of stream). | `/snapshot` | +| `-w`, `--webrtc_url` | Sets the URL for WebRTC (H264 compressed stream). | `/webrtc` | | `-af`, `--autofocus` | Autofocus mode. Supported modes: `manual`, `continuous`. | `continuous` | | `-l`, `--lensposition` | Set focal distance. 0 for infinite focus, 0.5 for approximate 50cm. Only used with Autofocus manual. | `0.0` | | `-s`, `--autofocusspeed` | Autofocus speed. Supported values: `normal`, `fast`. Only used with Autofocus continuous | `normal` | -| `-ud`, `--upsidedown` | Rotate the image by 180° (see below) | | -| `-fh`, `--flip_horizontal` | Mirror the image horizontally (see below) | | -| `-fv`, `--flip_vertical` | Mirror the image vertically (see below) | | -| `-or`, `--orientation_exif` | Set the image orientation using an EXIF header (see below) | | +| `-ud`, `--upsidedown` | Rotate the image by 180° (see [below](#image-orientation)) | | +| `-fh`, `--flip_horizontal` | Mirror the image horizontally (see [below](#image-orientation)) | | +| `-fv`, `--flip_vertical` | Mirror the image vertically (see [below](#image-orientation)) | | +| `-or`, `--orientation_exif` | Set the image orientation using an EXIF header (see [below](#image-orientation)) | | | `-c`, `--controls` | Define camera controls to start spyglass with. Can be used multiple times. This argument expects the format \=\. | | -| `--list-controls` | List all available libcamera controls onto the console. Those can be used with `--controls` | | | `-tf`, `--tuning_filter` | Set a tuning filter file name. | | | `-tfd`, `--tuning_filter_dir` | Set the directory to look for tuning filters. | | | `-n`, `--camera_num` | Camera number to be used. All cameras with their number can be shown with `libcamera-hello`. | `0` | +| `--disable_webrtc` | Disables WebRTC encoding (recommended on Pi5). | | +| `--list-controls` | List all available libcamera controls onto the console. Those can be used with `--controls` | | Starting the server without any argument is the same as @@ -123,6 +125,14 @@ If you want to use Spyglass as a webcam source for [Mainsail]() add a webcam wit - URL Snapshot: `/webcam/snapshot` - Service: `V4L-MJPEG` +Alternatively you can use WebRTC. This will take less network bandwidth and might help to fix low fps: + +- URL Stream: `/webcam/webrtc` +- URL Snapshot: `/webcam/snapshot` +- Service: `WebRTC (MediaMTX)` + +WebRTC needs [aiortc](https://github.com/aiortc/aiortc) installed. This gets automatically installed with `make install` for further instructions, please see the [install](#install) chapter below. + ## Install as application If you want to install Spyglass globally on your machine you can use `python -m pip install .` to do so.