diff --git a/active-stream-proxy/Dockerfile b/active-stream-proxy/Dockerfile index 620fead..292aec2 100644 --- a/active-stream-proxy/Dockerfile +++ b/active-stream-proxy/Dockerfile @@ -2,6 +2,8 @@ FROM python:3.12-alpine WORKDIR /usr/src/app +RUN apk add --no-cache ffmpeg + COPY requirements.txt ./ RUN pip install --no-cache-dir -r requirements.txt diff --git a/active-stream-proxy/main.py b/active-stream-proxy/main.py index 7243917..10d3453 100644 --- a/active-stream-proxy/main.py +++ b/active-stream-proxy/main.py @@ -2,16 +2,25 @@ import subprocess import time import requests +time.sleep(5) + active_stream = requests.get("http://backend:8000/api/v1/active_stream").text +print(active_stream) + old_active_stream = active_stream proc = None +if active_stream != "": + proc = subprocess.Popen(["/bin/ffmpeg", "-re", "-i", f"rtmp://mediamtx:1935/{active_stream}", "-c:a copy", "rtmp://host.containers.internal:1936/active-input"]) + + while True: - proc = subprocess.Popen([f"ffmpeg -re -i rtmp://mediamtx:1935/{active_stream} -c:a copy rtmp://host.containers.internal:1936/active-input"], shell=True) time.sleep(3) active_stream = requests.get("http://backend:8000/api/v1/active_stream").text if old_active_stream is not active_stream: - proc.terminate() - proc = subprocess.Popen([f"ffmpeg -re -i rtmp://mediamtx:1935/{active_stream} -c:a copy rtmp://host.containers.internal:1936/active-input"], shell=True) + if proc: + proc.terminate() + print("e") + proc = subprocess.Popen(["/bin/ffmpeg", "-re", "-i", f"rtmp://mediamtx:1935/{active_stream}", "-c:a copy", "rtmp://host.containers.internal:1936/active-input"]) old_active_stream = active_stream diff --git a/backend/Dockerfile b/backend/Dockerfile index f00ed05..2598748 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -12,10 +12,12 @@ RUN apt-get install -y python3-opencv RUN pip install --no-cache-dir -r requirements.txt -COPY main.py schema.prisma . +COPY schema.prisma . -COPY migrations . +ADD migrations . RUN prisma generate +COPY main.py . + CMD [ "fastapi", "run", "main.py" ] diff --git a/backend/main.py b/backend/main.py index f54fea6..f854c1b 100644 --- a/backend/main.py +++ b/backend/main.py @@ -6,6 +6,7 @@ from contextlib import asynccontextmanager from datetime import datetime from secrets import choice, token_hex from typing import Dict, List +import time import cv2 import httpx @@ -151,12 +152,6 @@ async def check_for_new(): @asynccontextmanager async def lifespan(app: FastAPI): - await update_active() - scheduler.start() - scheduler.add_job(update_active, IntervalTrigger(minutes=5)) - scheduler.add_job(check_for_new, IntervalTrigger(seconds=3)) - scheduler.add_job(rotate_fernet_key, IntervalTrigger(minutes=30)) - await rotate_fernet_key() await db.connect() async with httpx.AsyncClient() as client: for stream in await db.stream.find_many(): @@ -164,6 +159,11 @@ async def lifespan(app: FastAPI): f"http://{os.environ['MEDIAMTX_IP']}:9997/v3/config/paths/add/" + stream.key, json={"name": stream.key}, ) + scheduler.start() + scheduler.add_job(update_active, IntervalTrigger(minutes=5)) + scheduler.add_job(check_for_new, IntervalTrigger(seconds=3)) + scheduler.add_job(rotate_fernet_key, IntervalTrigger(minutes=30)) + await rotate_fernet_key() yield scheduler.shutdown() await db.disconnect() diff --git a/backend/schema.prisma b/backend/schema.prisma index 0edb7f5..dca2c2e 100644 --- a/backend/schema.prisma +++ b/backend/schema.prisma @@ -5,7 +5,7 @@ generator client { } datasource db { - provider = "postgresql" + provider = "sqlite" url = "file:./db/dev.db" } diff --git a/docker-compose.yml b/docker-compose.yml index 7e4064b..12132ed 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,9 +1,12 @@ services: mediamtx: - network_mode: host build: context: ./mediamtx dockerfile: Dockerfile + ports: + - "8889:8889" + - "1935:1935" + - "9997:9997" web-frontend: build: context: ./tiling-frontend @@ -12,8 +15,8 @@ services: - tiling_frontend_build:/usr/src/app/dist live-stream: depends_on: - web-frontend: - condition: service_completed_successfully + active_stream_proxy: + condition: service_started build: context: ./live-stream dockerfile: Dockerfile @@ -23,6 +26,8 @@ services: YT_STREAM_KEY: ${YT_STREAM_KEY} backend: env_file: .backend.env + ports: + - "8000:8000" build: context: ./backend dockerfile: Dockerfile @@ -37,6 +42,8 @@ services: condition: service_completed_successfully mediamtx: condition: service_started + backend: + condition: service_started volumes: mediamtx_recordings: tiling_frontend_build: diff --git a/mediamtx/mediamtx.yml b/mediamtx/mediamtx.yml index ccb9689..6ba7b24 100644 --- a/mediamtx/mediamtx.yml +++ b/mediamtx/mediamtx.yml @@ -11,3 +11,12 @@ pathDefaults: recordDeleteAfter: 0s webrtcICEServers2: - url: stun:stun.l.google.com:19302 +authInternalUsers: + # Username. 'any' means any user, including anonymous ones. +- user: any + # Password. Not used in case of 'any' user. + pass: + # IPs or networks allowed to use this user. An empty list means any IP. + ips: [] + permissions: + - action: api