[AZ-178] Implement streaming video detection endpoint

- Added `/detect/video` endpoint for true streaming video detection, allowing inference to start as upload bytes arrive.
- Introduced `run_detect_video_stream` method in the inference module to handle video processing from a file-like object.
- Updated media hashing to include a new function for computing hashes directly from files with minimal I/O.
- Enhanced documentation to reflect changes in video processing and API behavior.

Made-with: Cursor
This commit is contained in:
Oleksandr Bezdieniezhnykh
2026-04-01 03:11:43 +03:00
parent e65d8da6a3
commit be4cab4fcb
42 changed files with 2983 additions and 29 deletions
+153
View File
@@ -0,0 +1,153 @@
"""
AZ-178: Streaming video detection with real AI inference.
Uses video_1_faststart.mp4. Stops after 10 seconds.
Requires services (run via run-tests.sh) for model download.
Run: sh run-tests.sh -k test_frames_decoded
"""
import os
import threading
import time
from pathlib import Path
import pytest
FIXTURES_DIR = Path(__file__).resolve().parent.parent / "e2e" / "fixtures"
FASTSTART_PATH = FIXTURES_DIR / "video_1_faststart.mp4"
@pytest.fixture(scope="module")
def faststart_video():
if FASTSTART_PATH.is_file():
return str(FASTSTART_PATH)
source = FIXTURES_DIR / "video_1.mp4"
if not source.is_file():
pytest.skip(f"missing source fixture {source}")
import shutil
import subprocess
ffmpeg = shutil.which("ffmpeg")
if not ffmpeg:
pytest.skip("ffmpeg not found")
subprocess.run(
[ffmpeg, "-y", "-i", str(source), "-c", "copy", "-movflags", "+faststart", str(FASTSTART_PATH)],
capture_output=True, check=True,
)
return str(FASTSTART_PATH)
def test_frames_decoded_while_upload_in_progress(faststart_video):
from streaming_buffer import StreamingBuffer
loader_url = os.environ.get("LOADER_URL") or os.environ.get("MOCK_LOADER_URL")
if not loader_url:
pytest.skip("LOADER_URL/MOCK_LOADER_URL not set — run via run-tests.sh for real detections")
from inference import Inference, ai_config_from_dict
from loader_http_client import LoaderHttpClient
client = LoaderHttpClient(loader_url)
inf = Inference(client)
if not inf.is_engine_ready:
pytest.skip("AI engine not available (model download failed)")
# Arrange
ai_cfg = ai_config_from_dict({})
file_size = os.path.getsize(faststart_video)
chunk_size = 64 * 1024
buf = StreamingBuffer(total_size=file_size)
bytes_written = [0]
stop_flag = threading.Event()
writer_start = [0.0]
detections_log = []
first_det_time = []
inf_error = []
from constants_inf import get_annotation_name
def on_annotation(annotation, percent):
now = time.monotonic()
if not first_det_time:
first_det_time.append(now)
written_mb = bytes_written[0] / (1024 * 1024)
pct_file = bytes_written[0] * 100 / file_size
elapsed = now - writer_start[0]
det_strs = [
f"{get_annotation_name(d.cls)}:{d.confidence*100:.0f}% @({d.x:.3f},{d.y:.3f} {d.w:.3f}x{d.h:.3f})"
for d in annotation.detections
]
detections_log.append((now, annotation, percent))
print(f" DET | {elapsed:7.2f}s | {written_mb:8.1f} MB | {pct_file:5.1f}% file | "
f"{percent:3d}% video | {len(annotation.detections)} dets | {det_strs}")
def on_status(media_name, count):
print(f" STATUS | {media_name}: {count} total detections")
def writer():
writer_start[0] = time.monotonic()
with open(faststart_video, "rb") as f:
while not stop_flag.is_set():
chunk = f.read(chunk_size)
if not chunk:
break
buf.append(chunk)
bytes_written[0] += len(chunk)
time.sleep(0.001)
buf.close_writer()
def run_inference():
try:
inf.run_detect_video_stream(buf, ai_cfg, "streaming_test", on_annotation, on_status)
except Exception as e:
inf_error.append(e)
print(f"\n Video: {file_size/(1024*1024):.1f} MB (faststart)")
print(f" {'':>6s} {'Time':>8s} {'Written':>10s} {'% File':>7s} {'% Vid':>5s} {'Dets':>4s} Labels")
print(f" {'-'*80}")
# Act
wt = threading.Thread(target=writer, daemon=True)
wt.start()
inf_thread = threading.Thread(target=run_inference, daemon=True)
inf_thread.start()
inf_thread.join(timeout=10.0)
inf.stop()
stop_flag.set()
buf.close_writer()
wt.join(timeout=5)
inf_thread.join(timeout=5)
try:
buf.close()
os.unlink(buf.path)
except Exception:
pass
# Assert
written_mb = bytes_written[0] / (1024 * 1024)
print(f"\n {'='*60}")
print(f" RESULTS")
print(f" {'='*60}")
print(f" Detections received: {len(detections_log)}")
print(f" File uploaded: {written_mb:.1f} / {file_size/(1024*1024):.1f} MB")
if first_det_time:
ttfd = first_det_time[0] - writer_start[0]
pct_at_first = bytes_written[0] * 100 / file_size
print(f" Time to first detection: {ttfd:.3f}s")
if pct_at_first < 100:
print(f" >>> STREAMING CONFIRMED: detections arrived while upload in progress")
else:
print(f" >>> Detections arrived after full upload")
else:
print(f" Time to first detection: (none — no detections in 10s)")
if inf_error:
print(f" Inference error: {inf_error[0]}")
print(f" {'='*60}\n")
assert not inf_error, f"Inference error: {inf_error}"
assert len(detections_log) > 0, "no detections received in 10s"
+425
View File
@@ -0,0 +1,425 @@
import asyncio
import base64
import json
import os
import tempfile
import threading
import time
from unittest.mock import MagicMock, patch
import pytest
class TestStreamingBuffer:
def test_sequential_write_read(self):
# Arrange
from streaming_buffer import StreamingBuffer
buf = StreamingBuffer()
try:
buf.append(b"hello")
buf.append(b" world")
buf.close_writer()
# Act
result = buf.read(-1)
# Assert
assert result == b"hello world"
finally:
buf.close()
os.unlink(buf.path)
def test_read_blocks_until_data_available(self):
# Arrange
from streaming_buffer import StreamingBuffer
buf = StreamingBuffer()
results = []
def writer():
time.sleep(0.1)
buf.append(b"data")
buf.close_writer()
t = threading.Thread(target=writer)
t.start()
# Act
results.append(buf.read(4))
t.join(timeout=5)
# Assert
assert results == [b"data"]
buf.close()
os.unlink(buf.path)
def test_read_returns_empty_on_eof(self):
# Arrange
from streaming_buffer import StreamingBuffer
buf = StreamingBuffer()
buf.close_writer()
# Act
result = buf.read(1024)
# Assert
assert result == b""
buf.close()
os.unlink(buf.path)
def test_concurrent_write_read_chunked(self):
# Arrange
from streaming_buffer import StreamingBuffer
buf = StreamingBuffer()
chunks_written = [b"aaa", b"bbb", b"ccc"]
read_data = bytearray()
def writer():
for c in chunks_written:
time.sleep(0.02)
buf.append(c)
buf.close_writer()
def reader():
while True:
chunk = buf.read(1024)
if not chunk:
break
read_data.extend(chunk)
wt = threading.Thread(target=writer)
rt = threading.Thread(target=reader)
# Act
wt.start()
rt.start()
wt.join(timeout=5)
rt.join(timeout=5)
# Assert
assert bytes(read_data) == b"aaabbbccc"
buf.close()
os.unlink(buf.path)
def test_seek_set_and_reread(self):
# Arrange
from streaming_buffer import StreamingBuffer
buf = StreamingBuffer()
buf.append(b"0123456789")
buf.close_writer()
# Act
buf.read(5)
buf.seek(2, 0)
result = buf.read(3)
# Assert
assert result == b"234"
buf.close()
os.unlink(buf.path)
def test_seek_end_blocks_until_eof(self):
# Arrange
from streaming_buffer import StreamingBuffer
buf = StreamingBuffer()
positions = []
def writer():
time.sleep(0.1)
buf.append(b"abcdef")
buf.close_writer()
t = threading.Thread(target=writer)
t.start()
# Act
pos = buf.seek(0, 2)
positions.append(pos)
t.join(timeout=5)
# Assert
assert positions[0] == 6
buf.close()
os.unlink(buf.path)
def test_tell_tracks_position(self):
# Arrange
from streaming_buffer import StreamingBuffer
buf = StreamingBuffer()
buf.append(b"data")
buf.close_writer()
# Assert
assert buf.tell() == 0
buf.read(2)
assert buf.tell() == 2
buf.close()
os.unlink(buf.path)
def test_file_persisted_to_disk(self):
# Arrange
from streaming_buffer import StreamingBuffer
buf = StreamingBuffer()
payload = b"x" * 10000
# Act
buf.append(payload)
buf.close_writer()
# Assert
with open(buf.path, "rb") as f:
assert f.read() == payload
buf.close()
os.unlink(buf.path)
def test_written_property(self):
# Arrange
from streaming_buffer import StreamingBuffer
buf = StreamingBuffer()
buf.append(b"abc")
buf.append(b"defgh")
buf.close_writer()
# Assert
assert buf.written == 8
buf.close()
os.unlink(buf.path)
def test_seekable_readable(self):
# Arrange
from streaming_buffer import StreamingBuffer
buf = StreamingBuffer()
buf.close_writer()
# Assert
assert buf.seekable() is True
assert buf.readable() is True
assert buf.writable() is False
buf.close()
os.unlink(buf.path)
class TestMediaContentHashFromFile:
def test_small_file_matches_bytes_version(self):
# Arrange
from media_hash import compute_media_content_hash, compute_media_content_hash_from_file
data = b"hello world"
with tempfile.NamedTemporaryFile(delete=False) as f:
f.write(data)
path = f.name
# Act
hash_bytes = compute_media_content_hash(data)
hash_file = compute_media_content_hash_from_file(path)
# Assert
assert hash_file == hash_bytes
os.unlink(path)
def test_large_file_matches_bytes_version(self):
# Arrange
from media_hash import compute_media_content_hash, compute_media_content_hash_from_file
data = os.urandom(50_000)
with tempfile.NamedTemporaryFile(delete=False) as f:
f.write(data)
path = f.name
# Act
hash_bytes = compute_media_content_hash(data)
hash_file = compute_media_content_hash_from_file(path)
# Assert
assert hash_file == hash_bytes
os.unlink(path)
def test_virtual_flag(self):
# Arrange
from media_hash import compute_media_content_hash_from_file
with tempfile.NamedTemporaryFile(delete=False) as f:
f.write(b"test")
path = f.name
# Act
normal = compute_media_content_hash_from_file(path, virtual=False)
virtual = compute_media_content_hash_from_file(path, virtual=True)
# Assert
assert virtual == f"V{normal}"
os.unlink(path)
def test_exact_boundary_3072_bytes(self):
# Arrange
from media_hash import compute_media_content_hash, compute_media_content_hash_from_file
data = os.urandom(3072)
with tempfile.NamedTemporaryFile(delete=False) as f:
f.write(data)
path = f.name
# Act
hash_bytes = compute_media_content_hash(data)
hash_file = compute_media_content_hash_from_file(path)
# Assert
assert hash_file == hash_bytes
os.unlink(path)
def _access_jwt(sub: str = "u1") -> str:
raw = json.dumps(
{"exp": int(time.time()) + 3600, "sub": sub}, separators=(",", ":")
).encode()
payload = base64.urlsafe_b64encode(raw).decode().rstrip("=")
return f"h.{payload}.s"
class _FakeInfStream:
is_engine_ready = True
def run_detect_video_stream(
self, readable, ai_cfg, media_name, on_annotation, status_callback=None
):
while True:
chunk = readable.read(4096)
if not chunk:
break
if status_callback:
status_callback(media_name, 0)
def run_detect_video(self, *a, **kw):
pass
def run_detect_image(self, *a, **kw):
pass
class TestDetectVideoEndpoint:
@pytest.fixture(autouse=True)
def reset_inference(self):
import main
main.inference = None
yield
main.inference = None
def test_streaming_upload_returns_started(self):
# Arrange
import main
from media_hash import compute_media_content_hash
video_body = b"fake-video-" * 200
content_hash = compute_media_content_hash(video_body)
mock_post = MagicMock()
mock_post.return_value.status_code = 201
mock_put = MagicMock()
mock_put.return_value.status_code = 204
token = _access_jwt()
with tempfile.TemporaryDirectory() as vd:
os.environ["VIDEOS_DIR"] = vd
from fastapi.testclient import TestClient
client = TestClient(main.app)
with (
patch.object(main, "get_inference", return_value=_FakeInfStream()),
patch.object(main.http_requests, "post", mock_post),
patch.object(main.http_requests, "put", mock_put),
):
# Act
r = client.post(
"/detect/video",
content=video_body,
headers={
"X-Filename": "test.mp4",
"Authorization": f"Bearer {token}",
},
)
# Assert
assert r.status_code == 200
data = r.json()
assert data["status"] == "started"
assert data["mediaId"] == content_hash
stored = os.path.join(vd, f"{content_hash}.mp4")
assert os.path.isfile(stored)
with open(stored, "rb") as f:
assert f.read() == video_body
def test_non_auth_cleanup(self):
# Arrange
import main
video_body = b"noauth-vid-" * 100
with tempfile.TemporaryDirectory() as vd:
os.environ["VIDEOS_DIR"] = vd
from fastapi.testclient import TestClient
client = TestClient(main.app)
with patch.object(main, "get_inference", return_value=_FakeInfStream()):
# Act
r = client.post(
"/detect/video",
content=video_body,
headers={"X-Filename": "test.mp4"},
)
# Assert
assert r.status_code == 200
assert r.json()["status"] == "started"
def test_rejects_non_video_extension(self):
# Arrange
import main
from fastapi.testclient import TestClient
client = TestClient(main.app)
# Act
r = client.post(
"/detect/video",
content=b"data",
headers={"X-Filename": "photo.jpg"},
)
# Assert
assert r.status_code == 400
def test_data_flows_through_streaming_buffer(self):
# Arrange
import main
from streaming_buffer import StreamingBuffer
received_chunks = []
class _CaptureInf(_FakeInfStream):
def run_detect_video_stream(
self, readable, ai_cfg, media_name, on_annotation, status_callback=None
):
while True:
chunk = readable.read(4096)
if not chunk:
break
received_chunks.append(chunk)
video_body = b"A" * 10000
with tempfile.TemporaryDirectory() as vd:
os.environ["VIDEOS_DIR"] = vd
from fastapi.testclient import TestClient
client = TestClient(main.app)
with patch.object(main, "get_inference", return_value=_CaptureInf()):
# Act
r = client.post(
"/detect/video",
content=video_body,
headers={"X-Filename": "v.mp4"},
)
# Assert
assert r.status_code == 200
all_received = b"".join(received_chunks)
assert all_received == video_body