summaryrefslogtreecommitdiff
path: root/kvmd/apps/media/server.py
blob: 1f96b3536c195bdad047a90afaa7955d9d6516c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# ========================================================================== #
#                                                                            #
#    KVMD - The main PiKVM daemon.                                           #
#                                                                            #
#    Copyright (C) 2020  Maxim Devaev <[email protected]>                    #
#                                                                            #
#    This program is free software: you can redistribute it and/or modify    #
#    it under the terms of the GNU General Public License as published by    #
#    the Free Software Foundation, either version 3 of the License, or       #
#    (at your option) any later version.                                     #
#                                                                            #
#    This program is distributed in the hope that it will be useful,         #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of          #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           #
#    GNU General Public License for more details.                            #
#                                                                            #
#    You should have received a copy of the GNU General Public License       #
#    along with this program.  If not, see <https://www.gnu.org/licenses/>.  #
#                                                                            #
# ========================================================================== #


import asyncio
import dataclasses

from aiohttp.web import Request
from aiohttp.web import WebSocketResponse

from ...logging import get_logger

from ... import tools
from ... import aiotools

from ...htserver import exposed_http
from ...htserver import exposed_ws
from ...htserver import WsSession
from ...htserver import HttpServer

from ...clients.streamer import StreamerError
from ...clients.streamer import StreamerPermError
from ...clients.streamer import StreamerFormats
from ...clients.streamer import BaseStreamerClient


# =====
@dataclasses.dataclass
class _Source:
    type:         str
    fmt:          str
    streamer:     BaseStreamerClient
    meta:         dict = dataclasses.field(default_factory=dict)
    clients:      dict[WsSession, "_Client"] = dataclasses.field(default_factory=dict)
    key_required: bool = dataclasses.field(default=False)


@dataclasses.dataclass
class _Client:
    ws:     WsSession
    src:    _Source
    queue:  asyncio.Queue[dict]
    sender: (asyncio.Task | None) = dataclasses.field(default=None)


class MediaServer(HttpServer):
    __K_VIDEO = "video"

    __F_H264 = "h264"
    __F_JPEG = "jpeg"

    __Q_SIZE = 32

    def __init__(
        self,
        h264_streamer: (BaseStreamerClient | None),
        jpeg_streamer: (BaseStreamerClient | None),
    ) -> None:

        super().__init__()

        self.__srcs: list[_Source] = []
        if h264_streamer:
            self.__srcs.append(_Source(self.__K_VIDEO, self.__F_H264, h264_streamer, {"profile_level_id": "42E01F"}))
        if jpeg_streamer:
            self.__srcs.append(_Source(self.__K_VIDEO, self.__F_JPEG, jpeg_streamer))

    # =====

    @exposed_http("GET", "/ws")
    async def __ws_handler(self, req: Request) -> WebSocketResponse:
        async with self._ws_session(req) as ws:
            media: dict = {self.__K_VIDEO: {}}
            for src in self.__srcs:
                media[src.type][src.fmt] = src.meta
            await ws.send_event("media", media)
            return (await self._ws_loop(ws))

    @exposed_ws(0)
    async def __ws_bin_ping_handler(self, ws: WsSession, _: bytes) -> None:
        await ws.send_bin(255, b"")  # Ping-pong

    @exposed_ws("start")
    async def __ws_start_handler(self, ws: WsSession, event: dict) -> None:
        try:
            req_type = str(event.get("type"))
            req_fmt = str(event.get("format"))
        except Exception:
            return
        src: (_Source | None) = None
        for cand in self.__srcs:
            if ws in cand.clients:
                return  # Don't allow any double streaming
            if (cand.type, cand.fmt) == (req_type, req_fmt):
                src = cand
        if src:
            client = _Client(ws, src, asyncio.Queue(self.__Q_SIZE))
            client.sender = aiotools.create_deadly_task(str(ws), self.__sender(client))
            src.clients[ws] = client
            get_logger(0).info("Streaming %s to %s ...", src.streamer, ws)

    # =====

    async def _init_app(self) -> None:
        logger = get_logger(0)
        for src in self.__srcs:
            logger.info("Starting streamer %s ...", src.streamer)
            aiotools.create_deadly_task(str(src.streamer), self.__streamer(src))
        self._add_exposed(self)

    async def _on_shutdown(self) -> None:
        logger = get_logger(0)
        logger.info("Stopping system tasks ...")
        await aiotools.stop_all_deadly_tasks()
        logger.info("Disconnecting clients ...")
        await self._close_all_wss()
        logger.info("On-Shutdown complete")

    async def _on_ws_closed(self, ws: WsSession) -> None:
        for src in self.__srcs:
            client = src.clients.pop(ws, None)
            if client and client.sender:
                get_logger(0).info("Closed stream for %s", ws)
                client.sender.cancel()
                return

    # =====

    async def __sender(self, client: _Client) -> None:
        need_key = StreamerFormats.is_diff(client.src.streamer.get_format())
        if need_key:
            client.src.key_required = True
        has_key = False
        while True:
            frame = await client.queue.get()
            has_key = (not need_key or has_key or frame["key"])
            if has_key:
                try:
                    await client.ws.send_bin(1, frame["key"].to_bytes() + frame["data"])
                except Exception:
                    pass

    async def __streamer(self, src: _Source) -> None:
        logger = get_logger(0)
        while True:
            if len(src.clients) == 0:
                await asyncio.sleep(1)
                continue
            try:
                async with src.streamer.reading() as read_frame:
                    while len(src.clients) > 0:
                        frame = await read_frame(src.key_required)
                        if frame["key"]:
                            src.key_required = False
                        for client in src.clients.values():
                            try:
                                client.queue.put_nowait(frame)
                            except asyncio.QueueFull:
                                # Если какой-то из клиентов не справляется, очищаем ему очередь и запрашиваем кейфрейм.
                                # Я вижу у такой логики кучу минусов, хз как себя покажет, но лучше пока ничего не придумал.
                                tools.clear_queue(client.queue)
                                src.key_required = True
                            except Exception:
                                pass
            except StreamerError as ex:
                if isinstance(ex, StreamerPermError):
                    logger.exception("Streamer failed: %s", src.streamer)
                else:
                    logger.error("Streamer error: %s: %s", src.streamer, tools.efmt(ex))
            except Exception:
                get_logger(0).exception("Unexpected streamer error: %s", src.streamer)
            await asyncio.sleep(1)