Mercurial > libervia-backend
annotate libervia/cli/cmd_call.py @ 4279:6276242736c3
doc (components): Documentation for the new `Conferences` component:
fix 445
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 05 Jul 2024 17:18:37 +0200 |
parents | 0d7bb4df2343 |
children |
rev | line source |
---|---|
4143 | 1 #!/usr/bin/env python3 |
2 | |
3 | |
4 # Libervia CLI | |
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 | |
21 from functools import partial | |
4210
9218d4331bb2
cli (call): `tui` output implementation:
Goffi <goffi@goffi.org>
parents:
4206
diff
changeset
|
22 import importlib |
4143 | 23 import logging |
4233
d01b8d002619
cli (call, file), frontends: implement webRTC data channel transfer:
Goffi <goffi@goffi.org>
parents:
4210
diff
changeset
|
24 from typing import Any |
4143 | 25 |
26 | |
27 from libervia.backend.core.i18n import _ | |
28 from libervia.backend.tools.common import data_format | |
29 from libervia.cli.constants import Const as C | |
4210
9218d4331bb2
cli (call): `tui` output implementation:
Goffi <goffi@goffi.org>
parents:
4206
diff
changeset
|
30 from libervia.frontends.tools import jid |
4233
d01b8d002619
cli (call, file), frontends: implement webRTC data channel transfer:
Goffi <goffi@goffi.org>
parents:
4210
diff
changeset
|
31 from libervia.frontends.tools.webrtc_models import CallData |
4143 | 32 |
33 from . import base | |
34 | |
35 __commands__ = ["Call"] | |
36 | |
37 | |
38 class Common(base.CommandBase): | |
39 | |
4206 | 40 def __init__(self, *args, **kwargs): |
41 super().__init__( | |
42 *args, | |
43 use_output=C.OUTPUT_CUSTOM, | |
44 extra_outputs={ | |
4210
9218d4331bb2
cli (call): `tui` output implementation:
Goffi <goffi@goffi.org>
parents:
4206
diff
changeset
|
45 #: automatically select best output for current platform |
4206 | 46 "default": self.auto_output, |
4210
9218d4331bb2
cli (call): `tui` output implementation:
Goffi <goffi@goffi.org>
parents:
4206
diff
changeset
|
47 #: simple output with GStreamer ``autovideosink`` |
9218d4331bb2
cli (call): `tui` output implementation:
Goffi <goffi@goffi.org>
parents:
4206
diff
changeset
|
48 "simple": partial(self.use_output, "simple"), |
9218d4331bb2
cli (call): `tui` output implementation:
Goffi <goffi@goffi.org>
parents:
4206
diff
changeset
|
49 #: Qt GUI |
9218d4331bb2
cli (call): `tui` output implementation:
Goffi <goffi@goffi.org>
parents:
4206
diff
changeset
|
50 "gui": partial(self.use_output, "gui"), |
9218d4331bb2
cli (call): `tui` output implementation:
Goffi <goffi@goffi.org>
parents:
4206
diff
changeset
|
51 #: experimental TUI output |
9218d4331bb2
cli (call): `tui` output implementation:
Goffi <goffi@goffi.org>
parents:
4206
diff
changeset
|
52 "tui": partial(self.use_output, "tui"), |
4206 | 53 }, |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4240
diff
changeset
|
54 **kwargs, |
4206 | 55 ) |
56 | |
4143 | 57 def add_parser_options(self): |
58 self.parser.add_argument( | |
59 "--no-ui", action="store_true", help=_("disable user interface") | |
60 ) | |
4233
d01b8d002619
cli (call, file), frontends: implement webRTC data channel transfer:
Goffi <goffi@goffi.org>
parents:
4210
diff
changeset
|
61 sources_group = self.parser.add_mutually_exclusive_group() |
d01b8d002619
cli (call, file), frontends: implement webRTC data channel transfer:
Goffi <goffi@goffi.org>
parents:
4210
diff
changeset
|
62 sources_group.add_argument( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4240
diff
changeset
|
63 "-s", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4240
diff
changeset
|
64 "--sources", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4240
diff
changeset
|
65 choices=["auto", "test"], |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4240
diff
changeset
|
66 default="auto", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4240
diff
changeset
|
67 help='Well-known sources to use (default: "auto").', |
4233
d01b8d002619
cli (call, file), frontends: implement webRTC data channel transfer:
Goffi <goffi@goffi.org>
parents:
4210
diff
changeset
|
68 ) |
d01b8d002619
cli (call, file), frontends: implement webRTC data channel transfer:
Goffi <goffi@goffi.org>
parents:
4210
diff
changeset
|
69 |
d01b8d002619
cli (call, file), frontends: implement webRTC data channel transfer:
Goffi <goffi@goffi.org>
parents:
4210
diff
changeset
|
70 def get_call_data_kw(self) -> dict[str, Any]: |
d01b8d002619
cli (call, file), frontends: implement webRTC data channel transfer:
Goffi <goffi@goffi.org>
parents:
4210
diff
changeset
|
71 """Get relevant keyword arguments for CallData""" |
d01b8d002619
cli (call, file), frontends: implement webRTC data channel transfer:
Goffi <goffi@goffi.org>
parents:
4210
diff
changeset
|
72 kwargs: dict[str, Any] = {} |
d01b8d002619
cli (call, file), frontends: implement webRTC data channel transfer:
Goffi <goffi@goffi.org>
parents:
4210
diff
changeset
|
73 if self.args.sources == "test": |
4240
79c8a70e1813
backend, frontend: prepare remote control:
Goffi <goffi@goffi.org>
parents:
4233
diff
changeset
|
74 from libervia.frontends.tools.webrtc_models import SourcesTest |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4240
diff
changeset
|
75 |
4240
79c8a70e1813
backend, frontend: prepare remote control:
Goffi <goffi@goffi.org>
parents:
4233
diff
changeset
|
76 kwargs["sources_data"] = SourcesTest() |
4233
d01b8d002619
cli (call, file), frontends: implement webRTC data channel transfer:
Goffi <goffi@goffi.org>
parents:
4210
diff
changeset
|
77 return kwargs |
d01b8d002619
cli (call, file), frontends: implement webRTC data channel transfer:
Goffi <goffi@goffi.org>
parents:
4210
diff
changeset
|
78 |
4143 | 79 async def start(self): |
80 root_logger = logging.getLogger() | |
81 # we don't want any formatting for messages from webrtc | |
82 for handler in root_logger.handlers: | |
83 handler.setFormatter(None) | |
84 if self.verbosity == 0: | |
85 root_logger.setLevel(logging.ERROR) | |
86 if self.verbosity >= 1: | |
87 root_logger.setLevel(logging.WARNING) | |
88 if self.verbosity >= 2: | |
89 root_logger.setLevel(logging.DEBUG) | |
90 | |
4210
9218d4331bb2
cli (call): `tui` output implementation:
Goffi <goffi@goffi.org>
parents:
4206
diff
changeset
|
91 async def auto_output(self, call_data: CallData) -> None: |
9218d4331bb2
cli (call): `tui` output implementation:
Goffi <goffi@goffi.org>
parents:
4206
diff
changeset
|
92 """Make a guess on the best output to use on current platform""" |
9218d4331bb2
cli (call): `tui` output implementation:
Goffi <goffi@goffi.org>
parents:
4206
diff
changeset
|
93 try: |
9218d4331bb2
cli (call): `tui` output implementation:
Goffi <goffi@goffi.org>
parents:
4206
diff
changeset
|
94 from .call_gui import AVCallUI |
9218d4331bb2
cli (call): `tui` output implementation:
Goffi <goffi@goffi.org>
parents:
4206
diff
changeset
|
95 except ImportError: |
9218d4331bb2
cli (call): `tui` output implementation:
Goffi <goffi@goffi.org>
parents:
4206
diff
changeset
|
96 # we can't import GUI, we may have missing modules |
9218d4331bb2
cli (call): `tui` output implementation:
Goffi <goffi@goffi.org>
parents:
4206
diff
changeset
|
97 await self.use_output("simple", call_data) |
4206 | 98 else: |
4210
9218d4331bb2
cli (call): `tui` output implementation:
Goffi <goffi@goffi.org>
parents:
4206
diff
changeset
|
99 if AVCallUI.can_run(): |
9218d4331bb2
cli (call): `tui` output implementation:
Goffi <goffi@goffi.org>
parents:
4206
diff
changeset
|
100 await self.use_output("gui", call_data) |
9218d4331bb2
cli (call): `tui` output implementation:
Goffi <goffi@goffi.org>
parents:
4206
diff
changeset
|
101 else: |
9218d4331bb2
cli (call): `tui` output implementation:
Goffi <goffi@goffi.org>
parents:
4206
diff
changeset
|
102 await self.use_output("simple", call_data) |
4206 | 103 |
4210
9218d4331bb2
cli (call): `tui` output implementation:
Goffi <goffi@goffi.org>
parents:
4206
diff
changeset
|
104 async def use_output(self, output_type, call_data: CallData): |
4206 | 105 try: |
4210
9218d4331bb2
cli (call): `tui` output implementation:
Goffi <goffi@goffi.org>
parents:
4206
diff
changeset
|
106 AVCall_module = importlib.import_module(f"libervia.cli.call_{output_type}") |
9218d4331bb2
cli (call): `tui` output implementation:
Goffi <goffi@goffi.org>
parents:
4206
diff
changeset
|
107 AVCallUI = AVCall_module.AVCallUI |
4206 | 108 except Exception as e: |
4210
9218d4331bb2
cli (call): `tui` output implementation:
Goffi <goffi@goffi.org>
parents:
4206
diff
changeset
|
109 self.disp(f"Error starting {output_type.upper()} UI: {e}", error=True) |
4206 | 110 self.host.quit(C.EXIT_ERROR) |
4210
9218d4331bb2
cli (call): `tui` output implementation:
Goffi <goffi@goffi.org>
parents:
4206
diff
changeset
|
111 else: |
9218d4331bb2
cli (call): `tui` output implementation:
Goffi <goffi@goffi.org>
parents:
4206
diff
changeset
|
112 try: |
9218d4331bb2
cli (call): `tui` output implementation:
Goffi <goffi@goffi.org>
parents:
4206
diff
changeset
|
113 await AVCallUI.run(self, call_data) |
9218d4331bb2
cli (call): `tui` output implementation:
Goffi <goffi@goffi.org>
parents:
4206
diff
changeset
|
114 except Exception as e: |
9218d4331bb2
cli (call): `tui` output implementation:
Goffi <goffi@goffi.org>
parents:
4206
diff
changeset
|
115 self.disp(f"Error running {output_type.upper()} UI: {e}", error=True) |
9218d4331bb2
cli (call): `tui` output implementation:
Goffi <goffi@goffi.org>
parents:
4206
diff
changeset
|
116 self.host.quit(C.EXIT_ERROR) |
4206 | 117 |
4143 | 118 |
119 class Make(Common): | |
120 def __init__(self, host): | |
121 super().__init__( | |
122 host, | |
123 "make", | |
124 use_verbose=True, | |
125 help=_("start a call"), | |
126 ) | |
127 | |
128 def add_parser_options(self): | |
129 super().add_parser_options() | |
130 self.parser.add_argument( | |
131 "entity", | |
132 metavar="JID", | |
133 help=_("JIDs of entity to call"), | |
134 ) | |
135 | |
136 async def start(self): | |
137 await super().start() | |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4240
diff
changeset
|
138 await super().output( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4240
diff
changeset
|
139 CallData(callee=jid.JID(self.args.entity), kwargs=self.get_call_data_kw()) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4240
diff
changeset
|
140 ) |
4143 | 141 |
142 | |
143 class Receive(Common): | |
144 def __init__(self, host): | |
145 super().__init__( | |
146 host, | |
147 "receive", | |
148 use_verbose=True, | |
149 help=_("wait for a call"), | |
150 ) | |
151 | |
152 def add_parser_options(self): | |
153 super().add_parser_options() | |
154 auto_accept_group = self.parser.add_mutually_exclusive_group() | |
155 auto_accept_group.add_argument( | |
156 "-a", | |
157 "--auto-accept", | |
158 action="append", | |
159 metavar="JID", | |
160 default=[], | |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4240
diff
changeset
|
161 help=_( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4240
diff
changeset
|
162 "automatically accept call from this jid (can be used multiple times)" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4240
diff
changeset
|
163 ), |
4143 | 164 ) |
165 auto_accept_group.add_argument( | |
166 "--auto-accept-all", | |
167 action="store_true", | |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4240
diff
changeset
|
168 help=_("automatically accept call from anybody"), |
4143 | 169 ) |
170 | |
171 async def on_action_new( | |
172 self, action_data_s: str, action_id: str, security_limit: int, profile: str | |
173 ) -> None: | |
174 if profile != self.profile: | |
175 return | |
176 action_data = data_format.deserialise(action_data_s) | |
177 if action_data.get("type") != C.META_TYPE_CALL: | |
178 return | |
179 peer_jid = jid.JID(action_data["from_jid"]).bare | |
180 caller = peer_jid.bare | |
181 if ( | |
182 not self.args.auto_accept_all | |
183 and caller not in self.args.auto_accept | |
184 and not await self.host.confirm( | |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4240
diff
changeset
|
185 _("📞 Incoming call from {caller}, do you accept?").format(caller=caller) |
4143 | 186 ) |
187 ): | |
188 await self.host.bridge.action_launch( | |
189 action_id, data_format.serialise({"cancelled": True}), profile | |
190 ) | |
191 return | |
192 | |
193 self.disp(_("✅ Incoming call from {caller} accepted.").format(caller=caller)) | |
194 | |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4240
diff
changeset
|
195 await super().output( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4240
diff
changeset
|
196 CallData( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4240
diff
changeset
|
197 callee=peer_jid, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4240
diff
changeset
|
198 sid=action_data["session_id"], |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4240
diff
changeset
|
199 action_id=action_id, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4240
diff
changeset
|
200 kwargs=self.get_call_data_kw(), |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4240
diff
changeset
|
201 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4240
diff
changeset
|
202 ) |
4143 | 203 |
204 async def start(self): | |
205 await super().start() | |
206 self.host.bridge.register_signal("action_new", self.on_action_new, "core") | |
207 | |
208 | |
209 class Call(base.CommandBase): | |
210 subcommands = (Make, Receive) | |
211 | |
212 def __init__(self, host): | |
213 super().__init__(host, "call", use_profile=False, help=_("A/V calls and related")) |