haybale-stream 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- haybale_stream/__init__.py +23 -0
- haybale_stream/backends/__init__.py +41 -0
- haybale_stream/backends/base.py +215 -0
- haybale_stream/backends/convert.py +31 -0
- haybale_stream/backends/ndi.py +160 -0
- haybale_stream/backends/omt.py +97 -0
- haybale_stream/backends/sources.py +55 -0
- haybale_stream/backends/spout.py +117 -0
- haybale_stream/backends/syphon.py +142 -0
- haybale_stream/haybale.toml +21 -0
- haybale_stream/nodes/__init__.py +5 -0
- haybale_stream/nodes/receive_frame.py +95 -0
- haybale_stream/nodes/receive_frame_emit.py +176 -0
- haybale_stream/nodes/send_frame.py +82 -0
- haybale_stream/nodes/settings.py +66 -0
- haybale_stream/py.typed +0 -0
- haybale_stream-0.0.1.dist-info/METADATA +87 -0
- haybale_stream-0.0.1.dist-info/RECORD +20 -0
- haybale_stream-0.0.1.dist-info/WHEEL +4 -0
- haybale_stream-0.0.1.dist-info/entry_points.txt +2 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"""Haybale Stream: send and receive video frames over NDI, OMT, Syphon and Spout."""
|
|
2
|
+
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
from haywire.core.library.base import BaseLibrary
|
|
6
|
+
from haywire.core.library.decorator import library
|
|
7
|
+
from haywire.core.node.registry import NodeRegistry
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@library(file_watcher=False)
|
|
11
|
+
class Library(BaseLibrary):
|
|
12
|
+
"""Stream library: frame-sharing nodes over the backends in ``haybale_stream.backends``."""
|
|
13
|
+
|
|
14
|
+
def register_components(self):
|
|
15
|
+
"""Register the node folder."""
|
|
16
|
+
base_path = Path(__file__).parent
|
|
17
|
+
self.add_folder_to_registry(folder_path=str(base_path / "nodes"), registry_cls=NodeRegistry)
|
|
18
|
+
|
|
19
|
+
def validate(self) -> bool:
|
|
20
|
+
return True
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
__all__ = ["Library"]
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"""Frame-sharing backends behind one sender/receiver contract. See ``base``."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from .base import Backend, BackendUnavailable, ClockedSender, Frame, Receiver, Sender
|
|
6
|
+
from .ndi import NDIBackend
|
|
7
|
+
from .omt import OMTBackend
|
|
8
|
+
from .spout import SpoutBackend
|
|
9
|
+
from .syphon import SyphonBackend
|
|
10
|
+
|
|
11
|
+
#: Every backend by key, in menu order. Importing this is safe on any platform: native
|
|
12
|
+
#: dependencies are imported only when a sender, receiver or discovery is actually used.
|
|
13
|
+
BACKENDS: dict[str, type[Backend]] = {
|
|
14
|
+
b.key: b for b in (NDIBackend, OMTBackend, SyphonBackend, SpoutBackend)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def get_backend(key: str) -> type[Backend]:
|
|
19
|
+
"""Return the backend registered as ``key`` (case-insensitive)."""
|
|
20
|
+
try:
|
|
21
|
+
return BACKENDS[key.lower()]
|
|
22
|
+
except KeyError:
|
|
23
|
+
raise KeyError(f"unknown backend {key!r}; known: {', '.join(BACKENDS)}") from None
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def available_backends() -> list[type[Backend]]:
|
|
27
|
+
"""The backends that can run on this machine right now."""
|
|
28
|
+
return [b for b in BACKENDS.values() if b.is_available()]
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
__all__ = [
|
|
32
|
+
"BACKENDS",
|
|
33
|
+
"Backend",
|
|
34
|
+
"BackendUnavailable",
|
|
35
|
+
"ClockedSender",
|
|
36
|
+
"Frame",
|
|
37
|
+
"Receiver",
|
|
38
|
+
"Sender",
|
|
39
|
+
"available_backends",
|
|
40
|
+
"get_backend",
|
|
41
|
+
]
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
"""The backend-neutral sender/receiver contract.
|
|
2
|
+
|
|
3
|
+
A backend moves **packed 8-bit 4-channel frames** (``H×W×4`` uint8) between processes.
|
|
4
|
+
It knows nothing about haywire or ``ImageValue``: ``convert.py`` bridges the two, so a
|
|
5
|
+
backend module imports only its own native dependency, and only when it is used.
|
|
6
|
+
|
|
7
|
+
Channel order is a property of the backend (``Backend.channel_order``): NDI and Spout
|
|
8
|
+
take RGBA, OMT and Syphon's raw readback are BGRA. The bridge converts once, on the
|
|
9
|
+
way in or out, so a backend never swaps channels itself.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import logging
|
|
15
|
+
import threading
|
|
16
|
+
from abc import ABC, abstractmethod
|
|
17
|
+
from dataclasses import dataclass
|
|
18
|
+
from typing import ClassVar, Self
|
|
19
|
+
|
|
20
|
+
import numpy as np
|
|
21
|
+
|
|
22
|
+
log = logging.getLogger(__name__)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class BackendUnavailable(RuntimeError):
|
|
26
|
+
"""The backend cannot run here: wrong platform, or its native dependency is missing."""
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@dataclass(frozen=True)
|
|
30
|
+
class Frame:
|
|
31
|
+
"""One received frame: ``pixels`` is ``H×W×4`` uint8 in the backend's channel order."""
|
|
32
|
+
|
|
33
|
+
pixels: np.ndarray
|
|
34
|
+
timestamp: float | None = None
|
|
35
|
+
|
|
36
|
+
@property
|
|
37
|
+
def width(self) -> int:
|
|
38
|
+
return int(self.pixels.shape[1])
|
|
39
|
+
|
|
40
|
+
@property
|
|
41
|
+
def height(self) -> int:
|
|
42
|
+
return int(self.pixels.shape[0])
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class Sender(ABC):
|
|
46
|
+
"""Publishes frames under a name that receivers discover."""
|
|
47
|
+
|
|
48
|
+
def __init__(self, name: str) -> None:
|
|
49
|
+
self.name = name
|
|
50
|
+
|
|
51
|
+
@abstractmethod
|
|
52
|
+
def open(self) -> None:
|
|
53
|
+
"""Start publishing. Raises ``BackendUnavailable`` if the backend cannot run."""
|
|
54
|
+
|
|
55
|
+
@abstractmethod
|
|
56
|
+
def send(self, pixels: np.ndarray, frame_rate: float = 30.0) -> None:
|
|
57
|
+
"""Publish one ``H×W×4`` uint8 frame. May return before the frame is on the wire."""
|
|
58
|
+
|
|
59
|
+
@abstractmethod
|
|
60
|
+
def close(self) -> None:
|
|
61
|
+
"""Stop publishing and release native resources. Safe to call twice."""
|
|
62
|
+
|
|
63
|
+
def __enter__(self) -> Self:
|
|
64
|
+
self.open()
|
|
65
|
+
return self
|
|
66
|
+
|
|
67
|
+
def __exit__(self, *exc: object) -> None:
|
|
68
|
+
self.close()
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class ClockedSender(Sender):
|
|
72
|
+
"""A sender whose native send call blocks for one frame period (NDI, OMT).
|
|
73
|
+
|
|
74
|
+
The caller's thread must not block on the network clock, so ``send`` only stores the
|
|
75
|
+
newest frame; a worker thread writes it. Frames sent faster than the clock are
|
|
76
|
+
dropped (latest wins). With ``steady_stream`` the last frame is resent while the
|
|
77
|
+
caller is idle, which keeps receivers from showing "no signal" on a still image.
|
|
78
|
+
"""
|
|
79
|
+
|
|
80
|
+
def __init__(self, name: str, steady_stream: bool = False) -> None:
|
|
81
|
+
super().__init__(name)
|
|
82
|
+
self.steady_stream = steady_stream
|
|
83
|
+
self._latest: tuple[np.ndarray, float] | None = None
|
|
84
|
+
self._lock = threading.Lock()
|
|
85
|
+
self._new = threading.Event()
|
|
86
|
+
self._stop = threading.Event()
|
|
87
|
+
self._thread: threading.Thread | None = None
|
|
88
|
+
self.frames_written = 0
|
|
89
|
+
|
|
90
|
+
@abstractmethod
|
|
91
|
+
def _open_native(self) -> None: ...
|
|
92
|
+
|
|
93
|
+
@abstractmethod
|
|
94
|
+
def _write(self, pixels: np.ndarray, frame_rate: float) -> None:
|
|
95
|
+
"""Blocking native write; runs on the worker thread only."""
|
|
96
|
+
|
|
97
|
+
@abstractmethod
|
|
98
|
+
def _close_native(self) -> None: ...
|
|
99
|
+
|
|
100
|
+
def open(self) -> None:
|
|
101
|
+
self._open_native()
|
|
102
|
+
self._stop.clear()
|
|
103
|
+
self._thread = threading.Thread(
|
|
104
|
+
target=self._run, name=f"{type(self).__name__}-{self.name}", daemon=True
|
|
105
|
+
)
|
|
106
|
+
self._thread.start()
|
|
107
|
+
|
|
108
|
+
def send(self, pixels: np.ndarray, frame_rate: float = 30.0) -> None:
|
|
109
|
+
# Own the bytes: the caller's array may be a read-only view of an image other nodes
|
|
110
|
+
# still read, and native writers want a writable buffer. np.array always copies.
|
|
111
|
+
frame = np.array(pixels, dtype=np.uint8, order="C")
|
|
112
|
+
with self._lock:
|
|
113
|
+
self._latest = (frame, frame_rate)
|
|
114
|
+
self._new.set()
|
|
115
|
+
|
|
116
|
+
def _run(self) -> None:
|
|
117
|
+
while not self._stop.is_set():
|
|
118
|
+
if (not self.steady_stream or self._latest is None) and not self._new.wait(0.1):
|
|
119
|
+
continue
|
|
120
|
+
self._new.clear()
|
|
121
|
+
with self._lock:
|
|
122
|
+
latest = self._latest
|
|
123
|
+
if latest is None or self._stop.is_set():
|
|
124
|
+
continue
|
|
125
|
+
try:
|
|
126
|
+
self._write(*latest)
|
|
127
|
+
self.frames_written += 1
|
|
128
|
+
except Exception:
|
|
129
|
+
log.exception("%s send failed", type(self).__name__)
|
|
130
|
+
self._stop.wait(0.1)
|
|
131
|
+
|
|
132
|
+
def close(self) -> None:
|
|
133
|
+
self._stop.set()
|
|
134
|
+
self._new.set()
|
|
135
|
+
thread, self._thread = self._thread, None
|
|
136
|
+
if thread is not None:
|
|
137
|
+
thread.join(timeout=5.0)
|
|
138
|
+
self._close_native()
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
class Receiver(ABC):
|
|
142
|
+
"""Receives frames from one named source."""
|
|
143
|
+
|
|
144
|
+
def __init__(self, source: str) -> None:
|
|
145
|
+
self.source = source
|
|
146
|
+
|
|
147
|
+
@abstractmethod
|
|
148
|
+
def open(self, timeout: float = 5.0) -> None:
|
|
149
|
+
"""Connect, waiting up to ``timeout`` s for the source to appear.
|
|
150
|
+
|
|
151
|
+
Raises ``BackendUnavailable`` or ``LookupError`` (source not found).
|
|
152
|
+
"""
|
|
153
|
+
|
|
154
|
+
@abstractmethod
|
|
155
|
+
def poll(self, timeout: float = 0.0) -> Frame | None:
|
|
156
|
+
"""Return the newest frame not yet returned, or ``None`` if there is none (waits up to ``timeout`` s)."""
|
|
157
|
+
|
|
158
|
+
@abstractmethod
|
|
159
|
+
def close(self) -> None:
|
|
160
|
+
"""Disconnect and release native resources. Safe to call twice."""
|
|
161
|
+
|
|
162
|
+
def __enter__(self) -> Self:
|
|
163
|
+
self.open()
|
|
164
|
+
return self
|
|
165
|
+
|
|
166
|
+
def __exit__(self, *exc: object) -> None:
|
|
167
|
+
self.close()
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
class Backend(ABC):
|
|
171
|
+
"""One transport: knows whether it can run here, lists sources, builds senders and receivers."""
|
|
172
|
+
|
|
173
|
+
key: ClassVar[str]
|
|
174
|
+
label: ClassVar[str]
|
|
175
|
+
#: ``sys.platform`` prefixes the backend runs on.
|
|
176
|
+
platforms: ClassVar[tuple[str, ...]] = ("linux", "darwin", "win32")
|
|
177
|
+
#: Channel order of the frames this backend sends and receives: ``"rgba"`` or ``"bgra"``.
|
|
178
|
+
channel_order: ClassVar[str] = "rgba"
|
|
179
|
+
#: Whether a sender can be told to resend its last frame (see ``ClockedSender``).
|
|
180
|
+
supports_steady_stream: ClassVar[bool] = False
|
|
181
|
+
|
|
182
|
+
@classmethod
|
|
183
|
+
def supported_here(cls) -> bool:
|
|
184
|
+
import sys
|
|
185
|
+
|
|
186
|
+
return sys.platform.startswith(cls.platforms)
|
|
187
|
+
|
|
188
|
+
@classmethod
|
|
189
|
+
def unavailable_reason(cls) -> str | None:
|
|
190
|
+
"""Why this backend cannot run here, or ``None`` when it can."""
|
|
191
|
+
if not cls.supported_here():
|
|
192
|
+
return f"{cls.label} is not available on this platform (needs {'/'.join(cls.platforms)})"
|
|
193
|
+
return cls._check_dependency()
|
|
194
|
+
|
|
195
|
+
@classmethod
|
|
196
|
+
def is_available(cls) -> bool:
|
|
197
|
+
return cls.unavailable_reason() is None
|
|
198
|
+
|
|
199
|
+
@classmethod
|
|
200
|
+
@abstractmethod
|
|
201
|
+
def _check_dependency(cls) -> str | None:
|
|
202
|
+
"""Import the native dependency; return a message if it is missing, else ``None``."""
|
|
203
|
+
|
|
204
|
+
@classmethod
|
|
205
|
+
@abstractmethod
|
|
206
|
+
def discover(cls, wait: float = 0.0) -> list[str]:
|
|
207
|
+
"""Names of the sources currently visible. ``wait`` seconds to let discovery settle."""
|
|
208
|
+
|
|
209
|
+
@classmethod
|
|
210
|
+
@abstractmethod
|
|
211
|
+
def create_sender(cls, name: str, *, steady_stream: bool = False) -> Sender: ...
|
|
212
|
+
|
|
213
|
+
@classmethod
|
|
214
|
+
@abstractmethod
|
|
215
|
+
def create_receiver(cls, source: str) -> Receiver: ...
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"""Bridge between ``ImageValue`` and the packed 4-channel frames backends move."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
from haybale_image.value import ChannelOrder, ImageValue, Layout, convert_layout
|
|
7
|
+
|
|
8
|
+
from .base import Backend, Frame
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def image_to_pixels(image: ImageValue, order: str) -> np.ndarray:
|
|
12
|
+
"""Return ``image`` as a C-contiguous ``H×W×4`` uint8 array in ``order`` (``"rgba"`` / ``"bgra"``).
|
|
13
|
+
|
|
14
|
+
Gray and RGB images get an opaque alpha. The array may be shared with ``image``'s
|
|
15
|
+
cache and is read-only: copy before writing.
|
|
16
|
+
"""
|
|
17
|
+
rgba = convert_layout(image, Layout.RGBA)
|
|
18
|
+
channel = ChannelOrder.BGR if order == "bgra" else ChannelOrder.RGB
|
|
19
|
+
return np.ascontiguousarray(rgba.array(np.uint8, channel))
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def pixels_to_image(pixels: np.ndarray, order: str) -> ImageValue:
|
|
23
|
+
"""Wrap an ``H×W×4`` uint8 frame in ``order`` as an RGBA ``ImageValue`` (no copy)."""
|
|
24
|
+
if pixels.ndim != 3 or pixels.shape[2] != 4 or pixels.dtype != np.uint8:
|
|
25
|
+
raise ValueError(f"expected an H×W×4 uint8 frame, got {pixels.dtype} {pixels.shape}")
|
|
26
|
+
channel = ChannelOrder.BGR if order == "bgra" else ChannelOrder.RGB
|
|
27
|
+
return ImageValue.from_array(pixels, order=channel)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def frame_to_image(frame: Frame, backend: type[Backend]) -> ImageValue:
|
|
31
|
+
return pixels_to_image(frame.pixels, backend.channel_order)
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
"""NDI backend, on cyndilib (wheels bundle the NDI runtime on Linux, macOS and Windows)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import time
|
|
6
|
+
from fractions import Fraction
|
|
7
|
+
|
|
8
|
+
import numpy as np
|
|
9
|
+
|
|
10
|
+
from .base import Backend, BackendUnavailable, ClockedSender, Frame, Receiver, Sender
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class NDISender(ClockedSender):
|
|
14
|
+
def __init__(self, name: str, steady_stream: bool = False) -> None:
|
|
15
|
+
super().__init__(name, steady_stream)
|
|
16
|
+
self._sender = None
|
|
17
|
+
self._size: tuple[int, int] | None = None
|
|
18
|
+
self._rate: Fraction | None = None
|
|
19
|
+
|
|
20
|
+
def _open_native(self) -> None:
|
|
21
|
+
from cyndilib.sender import Sender as NativeSender
|
|
22
|
+
|
|
23
|
+
self._sender = NativeSender(self.name)
|
|
24
|
+
|
|
25
|
+
def _configure(self, width: int, height: int, frame_rate: float) -> None:
|
|
26
|
+
"""(Re)build the video frame; NDI can only swap it while the sender is closed."""
|
|
27
|
+
from cyndilib.video_frame import VideoSendFrame
|
|
28
|
+
from cyndilib.wrapper.ndi_structs import FourCC
|
|
29
|
+
|
|
30
|
+
sender = self._sender
|
|
31
|
+
if self._size is not None:
|
|
32
|
+
sender.close()
|
|
33
|
+
frame = VideoSendFrame()
|
|
34
|
+
frame.set_fourcc(FourCC.RGBA)
|
|
35
|
+
frame.set_resolution(width, height)
|
|
36
|
+
rate = Fraction(frame_rate).limit_denominator(1001)
|
|
37
|
+
frame.set_frame_rate(rate)
|
|
38
|
+
sender.set_video_frame(frame)
|
|
39
|
+
sender.open()
|
|
40
|
+
self._size, self._rate = (width, height), rate
|
|
41
|
+
|
|
42
|
+
def _write(self, pixels: np.ndarray, frame_rate: float) -> None:
|
|
43
|
+
height, width = pixels.shape[:2]
|
|
44
|
+
rate = Fraction(frame_rate).limit_denominator(1001)
|
|
45
|
+
if self._size != (width, height) or self._rate != rate:
|
|
46
|
+
self._configure(width, height, frame_rate)
|
|
47
|
+
self._sender.write_video(pixels.reshape(-1))
|
|
48
|
+
|
|
49
|
+
def _close_native(self) -> None:
|
|
50
|
+
sender, self._sender = self._sender, None
|
|
51
|
+
if sender is not None and self._size is not None:
|
|
52
|
+
sender.close()
|
|
53
|
+
self._size = None
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class NDIReceiver(Receiver):
|
|
57
|
+
def __init__(self, source: str) -> None:
|
|
58
|
+
super().__init__(source)
|
|
59
|
+
self._finder = None
|
|
60
|
+
self._receiver = None
|
|
61
|
+
self._video = None
|
|
62
|
+
self._last_ts: float | None = None
|
|
63
|
+
|
|
64
|
+
def open(self, timeout: float = 5.0) -> None:
|
|
65
|
+
from cyndilib.finder import Finder
|
|
66
|
+
from cyndilib.receiver import Receiver as NativeReceiver
|
|
67
|
+
from cyndilib.video_frame import VideoFrameSync
|
|
68
|
+
from cyndilib.wrapper.ndi_recv import RecvBandwidth, RecvColorFormat
|
|
69
|
+
|
|
70
|
+
finder = Finder()
|
|
71
|
+
finder.open()
|
|
72
|
+
try:
|
|
73
|
+
deadline = time.monotonic() + timeout
|
|
74
|
+
found = None
|
|
75
|
+
while found is None:
|
|
76
|
+
finder.wait_for_sources(0.5)
|
|
77
|
+
found = next((s for s in finder if s.name == self.source), None)
|
|
78
|
+
if time.monotonic() >= deadline:
|
|
79
|
+
break
|
|
80
|
+
if found is None:
|
|
81
|
+
raise LookupError(f"NDI source {self.source!r} not found")
|
|
82
|
+
receiver = NativeReceiver(
|
|
83
|
+
color_format=RecvColorFormat.RGBX_RGBA, bandwidth=RecvBandwidth.highest
|
|
84
|
+
)
|
|
85
|
+
video = VideoFrameSync()
|
|
86
|
+
receiver.frame_sync.set_video_frame(video)
|
|
87
|
+
receiver.set_source(found)
|
|
88
|
+
except BaseException:
|
|
89
|
+
finder.close()
|
|
90
|
+
raise
|
|
91
|
+
self._finder, self._receiver, self._video = finder, receiver, video
|
|
92
|
+
|
|
93
|
+
def poll(self, timeout: float = 0.0) -> Frame | None:
|
|
94
|
+
if self._receiver is None:
|
|
95
|
+
return None
|
|
96
|
+
deadline = time.monotonic() + timeout
|
|
97
|
+
while True:
|
|
98
|
+
# frame_sync repeats the last frame; the timestamp tells a new one from a repeat.
|
|
99
|
+
self._receiver.frame_sync.capture_video()
|
|
100
|
+
video = self._video
|
|
101
|
+
if video.get_data_size() > 0:
|
|
102
|
+
ts = video.get_timestamp_posix()
|
|
103
|
+
if ts != self._last_ts:
|
|
104
|
+
self._last_ts = ts
|
|
105
|
+
width, height = video.get_resolution()
|
|
106
|
+
pixels = np.array(video.get_array(), dtype=np.uint8).reshape(height, width, 4)
|
|
107
|
+
return Frame(pixels, ts)
|
|
108
|
+
if time.monotonic() >= deadline:
|
|
109
|
+
return None
|
|
110
|
+
time.sleep(0.005)
|
|
111
|
+
|
|
112
|
+
def close(self) -> None:
|
|
113
|
+
receiver, self._receiver = self._receiver, None
|
|
114
|
+
finder, self._finder = self._finder, None
|
|
115
|
+
if receiver is not None:
|
|
116
|
+
receiver.disconnect()
|
|
117
|
+
if finder is not None:
|
|
118
|
+
finder.close()
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
class NDIBackend(Backend):
|
|
122
|
+
key = "ndi"
|
|
123
|
+
label = "NDI"
|
|
124
|
+
channel_order = "rgba"
|
|
125
|
+
supports_steady_stream = True
|
|
126
|
+
|
|
127
|
+
@classmethod
|
|
128
|
+
def _check_dependency(cls) -> str | None:
|
|
129
|
+
try:
|
|
130
|
+
import cyndilib # noqa: F401
|
|
131
|
+
except ImportError as exc:
|
|
132
|
+
return f"NDI needs the 'cyndilib' package ({exc})"
|
|
133
|
+
return None
|
|
134
|
+
|
|
135
|
+
@classmethod
|
|
136
|
+
def discover(cls, wait: float = 0.0) -> list[str]:
|
|
137
|
+
if (reason := cls.unavailable_reason()) is not None:
|
|
138
|
+
raise BackendUnavailable(reason)
|
|
139
|
+
from cyndilib.finder import Finder
|
|
140
|
+
|
|
141
|
+
finder = Finder()
|
|
142
|
+
finder.open()
|
|
143
|
+
try:
|
|
144
|
+
if wait > 0:
|
|
145
|
+
finder.wait_for_sources(wait)
|
|
146
|
+
return sorted(s.name for s in finder)
|
|
147
|
+
finally:
|
|
148
|
+
finder.close()
|
|
149
|
+
|
|
150
|
+
@classmethod
|
|
151
|
+
def create_sender(cls, name: str, *, steady_stream: bool = False) -> Sender:
|
|
152
|
+
if (reason := cls.unavailable_reason()) is not None:
|
|
153
|
+
raise BackendUnavailable(reason)
|
|
154
|
+
return NDISender(name, steady_stream)
|
|
155
|
+
|
|
156
|
+
@classmethod
|
|
157
|
+
def create_receiver(cls, source: str) -> Receiver:
|
|
158
|
+
if (reason := cls.unavailable_reason()) is not None:
|
|
159
|
+
raise BackendUnavailable(reason)
|
|
160
|
+
return NDIReceiver(source)
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"""OMT (Open Media Transport) backend, on cyomtlib (wheels bundle libomt)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
|
|
7
|
+
from .base import Backend, BackendUnavailable, ClockedSender, Frame, Receiver, Sender
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class OMTSender(ClockedSender):
|
|
11
|
+
def __init__(self, name: str, steady_stream: bool = False) -> None:
|
|
12
|
+
super().__init__(name, steady_stream)
|
|
13
|
+
self._sender = None
|
|
14
|
+
|
|
15
|
+
def _open_native(self) -> None:
|
|
16
|
+
import cyomtlib as omt
|
|
17
|
+
|
|
18
|
+
self._sender = omt.Sender(self.name)
|
|
19
|
+
|
|
20
|
+
@property
|
|
21
|
+
def address(self) -> str | None:
|
|
22
|
+
"""The name receivers see (OMT prefixes the host name)."""
|
|
23
|
+
return None if self._sender is None else self._sender.address
|
|
24
|
+
|
|
25
|
+
def _write(self, pixels: np.ndarray, frame_rate: float) -> None:
|
|
26
|
+
import cyomtlib as omt
|
|
27
|
+
|
|
28
|
+
self._sender.send_video(pixels, omt.Codec.BGRA, frame_rate=frame_rate, flags=omt.VideoFlags.ALPHA)
|
|
29
|
+
|
|
30
|
+
def _close_native(self) -> None:
|
|
31
|
+
sender, self._sender = self._sender, None
|
|
32
|
+
if sender is not None:
|
|
33
|
+
sender.close()
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class OMTReceiver(Receiver):
|
|
37
|
+
def __init__(self, source: str) -> None:
|
|
38
|
+
super().__init__(source)
|
|
39
|
+
self._receiver = None
|
|
40
|
+
|
|
41
|
+
def open(self, timeout: float = 5.0) -> None:
|
|
42
|
+
import cyomtlib as omt
|
|
43
|
+
|
|
44
|
+
self._receiver = omt.Receiver(
|
|
45
|
+
self.source, frame_types=omt.FrameType.VIDEO, video_format=omt.PreferredVideoFormat.BGRA
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
def poll(self, timeout: float = 0.0) -> Frame | None:
|
|
49
|
+
import cyomtlib as omt
|
|
50
|
+
|
|
51
|
+
if self._receiver is None:
|
|
52
|
+
return None
|
|
53
|
+
frame = self._receiver.receive(omt.FrameType.VIDEO, timeout_ms=int(timeout * 1000))
|
|
54
|
+
if not isinstance(frame, omt.VideoFrame):
|
|
55
|
+
return None
|
|
56
|
+
# packed() views libomt's buffer, valid until the next receive: take a copy.
|
|
57
|
+
return Frame(np.array(frame.packed(), dtype=np.uint8))
|
|
58
|
+
|
|
59
|
+
def close(self) -> None:
|
|
60
|
+
receiver, self._receiver = self._receiver, None
|
|
61
|
+
if receiver is not None:
|
|
62
|
+
receiver.close()
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class OMTBackend(Backend):
|
|
66
|
+
key = "omt"
|
|
67
|
+
label = "OMT"
|
|
68
|
+
channel_order = "bgra"
|
|
69
|
+
supports_steady_stream = True
|
|
70
|
+
|
|
71
|
+
@classmethod
|
|
72
|
+
def _check_dependency(cls) -> str | None:
|
|
73
|
+
try:
|
|
74
|
+
import cyomtlib # noqa: F401
|
|
75
|
+
except ImportError as exc:
|
|
76
|
+
return f"OMT needs the 'cyomtlib' package ({exc})"
|
|
77
|
+
return None
|
|
78
|
+
|
|
79
|
+
@classmethod
|
|
80
|
+
def discover(cls, wait: float = 0.0) -> list[str]:
|
|
81
|
+
if (reason := cls.unavailable_reason()) is not None:
|
|
82
|
+
raise BackendUnavailable(reason)
|
|
83
|
+
import cyomtlib as omt
|
|
84
|
+
|
|
85
|
+
return sorted(omt.discover(wait=wait))
|
|
86
|
+
|
|
87
|
+
@classmethod
|
|
88
|
+
def create_sender(cls, name: str, *, steady_stream: bool = False) -> Sender:
|
|
89
|
+
if (reason := cls.unavailable_reason()) is not None:
|
|
90
|
+
raise BackendUnavailable(reason)
|
|
91
|
+
return OMTSender(name, steady_stream)
|
|
92
|
+
|
|
93
|
+
@classmethod
|
|
94
|
+
def create_receiver(cls, source: str) -> Receiver:
|
|
95
|
+
if (reason := cls.unavailable_reason()) is not None:
|
|
96
|
+
raise BackendUnavailable(reason)
|
|
97
|
+
return OMTReceiver(source)
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"""Non-blocking source lists for UI dropdowns.
|
|
2
|
+
|
|
3
|
+
Discovery can take seconds (NDI, OMT wait for the network), and a dropdown is built on the
|
|
4
|
+
UI thread. ``cached_sources`` returns what was last found and refreshes in the background.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import logging
|
|
10
|
+
import threading
|
|
11
|
+
import time
|
|
12
|
+
|
|
13
|
+
from . import BACKENDS
|
|
14
|
+
|
|
15
|
+
log = logging.getLogger(__name__)
|
|
16
|
+
|
|
17
|
+
_REFRESH_EVERY = 3.0
|
|
18
|
+
_lock = threading.Lock()
|
|
19
|
+
_cache: dict[str, list[str]] = {}
|
|
20
|
+
_stamp: dict[str, float] = {}
|
|
21
|
+
_running: set[str] = set()
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _refresh(key: str) -> None:
|
|
25
|
+
try:
|
|
26
|
+
found = BACKENDS[key].discover(wait=1.0)
|
|
27
|
+
except Exception:
|
|
28
|
+
log.debug("discovery failed for %s", key, exc_info=True)
|
|
29
|
+
found = []
|
|
30
|
+
with _lock:
|
|
31
|
+
_cache[key] = found
|
|
32
|
+
_stamp[key] = time.monotonic()
|
|
33
|
+
_running.discard(key)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def cached_sources(key: str) -> list[str]:
|
|
37
|
+
"""Sources last seen on backend ``key`` (``[]`` until the first refresh finishes)."""
|
|
38
|
+
backend = BACKENDS.get(key)
|
|
39
|
+
if backend is None or not backend.is_available():
|
|
40
|
+
return []
|
|
41
|
+
with _lock:
|
|
42
|
+
stale = time.monotonic() - _stamp.get(key, float("-inf")) > _REFRESH_EVERY
|
|
43
|
+
if stale and key not in _running:
|
|
44
|
+
_running.add(key)
|
|
45
|
+
threading.Thread(target=_refresh, args=(key,), name=f"hb-discover-{key}", daemon=True).start()
|
|
46
|
+
return list(_cache.get(key, []))
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def all_cached_sources() -> list[str]:
|
|
50
|
+
"""Sources from every runnable backend, de-duplicated, for a backend-agnostic dropdown."""
|
|
51
|
+
seen: dict[str, None] = {}
|
|
52
|
+
for key in BACKENDS:
|
|
53
|
+
for name in cached_sources(key):
|
|
54
|
+
seen.setdefault(name)
|
|
55
|
+
return list(seen)
|