65 lines
2.0 KiB
Python
65 lines
2.0 KiB
Python
"""Minimal video-plane sample that receives frames with recv_into()."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
import sys
|
|
|
|
import yaml
|
|
|
|
try:
|
|
from omnisocket import MSG_TYPE_BINARY, Session, VIDEO_DEFAULTS
|
|
except ImportError:
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent / "python"))
|
|
from omnisocket import MSG_TYPE_BINARY, Session, VIDEO_DEFAULTS
|
|
|
|
|
|
def load_config() -> dict:
|
|
config_path = Path(__file__).resolve().parent / "config" / "omnisocket_demo.yaml"
|
|
if not config_path.exists():
|
|
return {}
|
|
with config_path.open("r", encoding="utf-8") as file:
|
|
return yaml.safe_load(file) or {}
|
|
|
|
|
|
def main() -> None:
|
|
config = load_config()
|
|
transport_cfg = config.get("transport", {})
|
|
video_cfg = config.get("video_receiver", {})
|
|
|
|
session = Session()
|
|
session.connect(
|
|
server_addr=str(transport_cfg.get("server_addr", "127.0.0.1:10909")),
|
|
peer_id=str(video_cfg.get("peer_id", "peer-a-video")),
|
|
relay_via=str(transport_cfg.get("relay_via", "")),
|
|
bind_ip=str(transport_cfg.get("bind_ip", "")),
|
|
bind_device=str(transport_cfg.get("bind_device", "")),
|
|
**VIDEO_DEFAULTS,
|
|
)
|
|
|
|
buffer = bytearray(int(video_cfg.get("buffer_bytes", 65536)))
|
|
frame_count = 0
|
|
try:
|
|
while True:
|
|
meta = session.recv_into(buffer, timeout_ms=1000)
|
|
if meta is None:
|
|
continue
|
|
if meta["msg_type"] != MSG_TYPE_BINARY:
|
|
print(f"ignore non-binary message: {meta}")
|
|
continue
|
|
frame = bytes(buffer[: meta["body_len"]])
|
|
sequence = int.from_bytes(frame[:8], "big") if len(frame) >= 8 else -1
|
|
print(
|
|
f"received frame={frame_count} remote_seq={sequence} "
|
|
f"bytes={meta['body_len']} from={meta['from']}"
|
|
)
|
|
frame_count += 1
|
|
except KeyboardInterrupt:
|
|
pass
|
|
finally:
|
|
session.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|