more WIP docker stuff

This commit is contained in:
Micha Albert 2024-09-09 20:03:16 -04:00
parent e75f8c944a
commit 8747a8ffe3
7 changed files with 44 additions and 15 deletions

View file

@ -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

View file

@ -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:
if proc:
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)
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

View file

@ -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" ]

View file

@ -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()

View file

@ -5,7 +5,7 @@ generator client {
}
datasource db {
provider = "postgresql"
provider = "sqlite"
url = "file:./db/dev.db"
}

View file

@ -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:

View file

@ -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