Skip to content

Commit

Permalink
Improved environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
spietras committed Apr 29, 2024
1 parent a88061a commit 6d01dcf
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 75 deletions.
9 changes: 4 additions & 5 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ services:
network: host
environment:
- "EMIPASS__SERVER__HOST=${EMIPASS__SERVER__HOST:-0.0.0.0}"
- "EMIPASS__SERVER__PORT=${EMIPASS__SERVER__PORT:-11000}"
- "EMIPASS__STREAMER__WHIP__HOST=${EMIPASS__STREAMER__WHIP__HOST:-0.0.0.0}"
- "EMIPASS__STREAMER__WHIP__PORTS=${EMIPASS__STREAMER__WHIP__PORTS:-11001}"
- "EMIPASS__STREAMER__RTP__MIN=${EMIPASS__STREAMER__RTP__MIN:-11002}"
- "EMIPASS__STREAMER__RTP__MAX=${EMIPASS__STREAMER__RTP__MAX:-11002}"
- "EMIPASS__SERVER__PORTS__HTTP=${EMIPASS__SERVER__PORTS__HTTP:-11000}"
- "EMIPASS__SERVER__PORTS__WHIP=${EMIPASS__SERVER__PORTS__WHIP:-11001}"
- "EMIPASS__SERVER__PORTS__RTP__MIN=${EMIPASS__SERVER__PORTS__RTP__MIN:-11002}"
- "EMIPASS__SERVER__PORTS__RTP__MAX=${EMIPASS__SERVER__PORTS__RTP__MAX:-11002}"
- "EMIPASS__STREAMER__STUN__HOST=${EMIPASS__STREAMER__STUN__HOST:-stun.l.google.com}"
- "EMIPASS__STREAMER__STUN__PORT=${EMIPASS__STREAMER__STUN__PORT:-19302}"
- "EMIPASS__STREAMER__TIMEOUT=${EMIPASS__STREAMER__TIMEOUT:-PT1M}"
Expand Down
19 changes: 8 additions & 11 deletions docs/docs/03-Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,17 @@ You can configure the app at runtime using various environment variables:
- `EMIPASS__SERVER__HOST` -
host to run the server on
(default: `0.0.0.0`)
- `EMIPASS__SERVER__PORT` -
port to run the server on
- `EMIPASS__SERVER__PORTS__HTTP` -
port to listen for HTTP requests on
(default: `11000`)
- `EMIPASS__STREAMER__WHIP__HOST` -
host to listen for connections on
(default: `0.0.0.0`)
- `EMIPASS__STREAMER__WHIP__PORTS` -
ports to select from when listening for connections
- `EMIPASS__SERVER__PORTS__WHIP` -
ports to select from when listening for WHIP requests
(default: `11001`)
- `EMIPASS__STREAMER__RTP__MIN` -
minimum port to select from when listening for connections
- `EMIPASS__SERVER__PORTS__RTP__MIN` -
minimum port to select from when listening for RTP connections
(default: `11002`)
- `EMIPASS__STREAMER__RTP__MAX` -
maximum port to select from when listening for connections
- `EMIPASS__SERVER__PORTS__RTP__MAX` -
maximum port to select from when listening for RTP connections
(default: `11002`)
- `EMIPASS__STREAMER__STUN__HOST` -
host of the STUN server
Expand Down
103 changes: 49 additions & 54 deletions src/emipass/config/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,58 @@
from emipass.config.base import BaseConfig


class ServerConfig(BaseModel):
"""Configuration for the server."""
class ServerRTPPortsConfig(BaseModel):
"""Configuration for the server RTP ports."""

host: str = Field(
"0.0.0.0",
title="Host",
description="Host to run the server on.",
min: int = Field(
11002,
ge=1,
le=65535,
title="Minimum",
description="Minimum port to select from when listening for RTP connections.",
)
port: int = Field(
11000,
ge=0,
max: int = Field(
11002,
ge=1,
le=65535,
title="Port",
description="Port to run the server on.",
title="Maximum",
description="Maximum port to select from when listening for RTP connections.",
)

@model_validator(mode="after")
def validate_ports(self) -> Self:
if self.min > self.max:
message = "Minimum port cannot be greater than maximum port."
raise ValueError(message)

class WHIPConfig(BaseModel):
"""Configuration for the WHIP server."""
return self

host: str = Field(
"0.0.0.0",
title="Host",
description="Host to listen for connections on.",

class ServerPortsConfig(BaseModel):
"""Configuration for the server ports."""

http: int = Field(
11000,
ge=0,
le=65535,
title="HTTP",
description="Port to listen for HTTP requests on.",
)
ports: set[Annotated[int, Field(..., ge=1, le=65535)]] = Field(
whip: set[Annotated[int, Field(..., ge=1, le=65535)]] = Field(
{11001},
min_length=1,
title="Ports",
description="Ports to select from when listening for connections.",
title="WHIP",
description="Ports to select from when listening for WHIP requests.",
)
rtp: ServerRTPPortsConfig = Field(
ServerRTPPortsConfig(),
title="RTP",
description="Configuration for the server RTP ports.",
)

@field_validator("ports", mode="before")
@field_validator("whip", mode="before")
@classmethod
def validate_ports(cls, v: Any) -> Any:
def validate_whip(cls, v: Any) -> Any:
if isinstance(v, int):
v = {v}
elif isinstance(v, str):
Expand All @@ -49,32 +66,20 @@ def validate_ports(cls, v: Any) -> Any:
return v


class RTPConfig(BaseModel):
"""Configuration for the RTP server."""
class ServerConfig(BaseModel):
"""Configuration for the server."""

min: int = Field(
11002,
ge=0,
le=65535,
title="Minimum Port",
description="Minimum port to select from when listening for connections.",
host: str = Field(
"0.0.0.0",
title="Host",
description="Host to run the server on.",
)
max: int = Field(
11002,
ge=0,
le=65535,
title="Maximum Port",
description="Maximum port to select from when listening for connections.",
ports: ServerPortsConfig = Field(
ServerPortsConfig(),
title="Ports",
description="Configuration for the server ports.",
)

@model_validator(mode="after")
def validate_ports(self) -> Self:
if self.min > self.max:
message = "Minimum port cannot be greater than maximum port."
raise ValueError(message)

return self


class STUNConfig(BaseModel):
"""Configuration for the STUN server."""
Expand All @@ -96,16 +101,6 @@ class STUNConfig(BaseModel):
class StreamerConfig(BaseModel):
"""Configuration for the streamer."""

whip: WHIPConfig = Field(
WHIPConfig(),
title="WHIP",
description="Configuration for the WHIP server.",
)
rtp: RTPConfig = Field(
RTPConfig(),
title="RTP",
description="Configuration for the RTP server.",
)
stun: STUNConfig = Field(
STUNConfig(),
title="STUN",
Expand Down
2 changes: 1 addition & 1 deletion src/emipass/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ def run(self) -> None:
uvicorn.run(
self._app,
host=config.host,
port=config.port,
port=config.ports.http,
)
6 changes: 3 additions & 3 deletions src/emipass/streaming/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ def _build_input_node(self, port: int, stun: STUNServer) -> GStreamerNode:
return GStreamerNode(
element="customwhipserversrc",
properties={
"address": f"http://{self._config.streamer.whip.host}:{port}",
"address": f"http://{self._config.server.host}:{port}",
"stun": f"stun://{stun.host}:{stun.port}",
"min": self._config.streamer.rtp.min,
"max": self._config.streamer.rtp.max,
"min": self._config.server.ports.rtp.min,
"max": self._config.server.ports.rtp.max,
},
)

Expand Down
2 changes: 1 addition & 1 deletion src/emipass/streaming/streamer.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async def _reserve_port(self) -> int:

async with self._lock:
used = await self._store.get()
available = self._config.streamer.whip.ports - used
available = self._config.server.ports.whip - used

if not available:
raise NoPortsAvailableError()
Expand Down

0 comments on commit 6d01dcf

Please sign in to comment.