annotate libervia/cli/cmd_call.py @ 4188:5a835b320726

docker (backend-dev): update Debian version + getting OMEMO related packages from git is not necessary anymore
author Goffi <goffi@goffi.org>
date Mon, 11 Dec 2023 00:53:59 +0100
parents 849721e1563b
children 0f8ea0768a3b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4143
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Libervia CLI
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
20
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from argparse import ArgumentParser
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 import asyncio
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from functools import partial
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 import logging
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 import os
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
26
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from prompt_toolkit.input import create_input
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from prompt_toolkit.keys import Keys
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
29
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from libervia.backend.core.i18n import _
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from libervia.backend.tools.common import data_format
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 from libervia.cli.constants import Const as C
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 from libervia.frontends.tools import aio, jid
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 from rich.columns import Columns
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 from rich.console import group
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 from rich.live import Live
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 from rich.panel import Panel
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 from rich.text import Text
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
39
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 from . import base
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
41
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 __commands__ = ["Call"]
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
43
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
44
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 class WebRTCCall:
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 def __init__(self, host, profile: str, callee: jid.JID):
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 from libervia.frontends.tools import webrtc
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
48
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 aio.install_glib_asyncio_iteration()
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 self.host = host
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 self.profile = profile
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 self.webrtc = webrtc.WebRTC(host.bridge, profile)
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 self.webrtc.callee = callee
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 host.bridge.register_signal(
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 "ice_candidates_new", self.on_ice_candidates_new, "plugin"
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 )
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 host.bridge.register_signal("call_setup", self.on_call_setup, "plugin")
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 host.bridge.register_signal("call_ended", self.on_call_ended, "plugin")
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
59
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 @property
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 def sid(self) -> str | None:
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 return self.webrtc.sid
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
63
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 @sid.setter
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 def sid(self, new_sid: str | None) -> None:
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 self.webrtc.sid = new_sid
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
67
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 async def on_ice_candidates_new(
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 self, sid: str, candidates_s: str, profile: str
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 ) -> None:
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 if sid != self.webrtc.sid or profile != self.profile:
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 return
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 self.webrtc.on_ice_candidates_new(
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 data_format.deserialise(candidates_s),
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 )
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
76
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 async def on_call_setup(self, sid: str, setup_data_s: str, profile: str) -> None:
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 if sid != self.webrtc.sid or profile != self.profile:
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 return
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 setup_data = data_format.deserialise(setup_data_s)
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 try:
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 role = setup_data["role"]
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 sdp = setup_data["sdp"]
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 except KeyError:
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 self.host.disp(f"Invalid setup data received: {setup_data}", error=True)
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 return
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 if role == "initiator":
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 self.webrtc.on_accepted_call(sdp, profile)
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 elif role == "responder":
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 await self.webrtc.answer_call(sdp, profile)
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 else:
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 self.host.disp(
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 f"Invalid role received during setup: {setup_data}", error=True
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 )
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 # we want to be sure that call is ended if user presses `Ctrl + c` or anything
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 # else stops the session.
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 self.host.add_on_quit_callback(
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 lambda: self.host.bridge.call_end(sid, "", profile)
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 )
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
100
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 async def on_call_ended(self, sid: str, data_s: str, profile: str) -> None:
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 if sid != self.webrtc.sid or profile != self.profile:
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 return
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 await self.webrtc.end_call()
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 await self.host.a_quit()
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
106
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 async def start(self):
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 """Start a call.
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
109
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 To be used only if we are initiator
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 """
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 await self.webrtc.setup_call("initiator")
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 self.webrtc.start_pipeline()
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
114
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
115
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 class UI:
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 def __init__(self, host, webrtc):
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 self.host = host
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 self.webrtc = webrtc
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
120
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 def styled_shortcut_key(self, word: str, key: str | None = None) -> Text:
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 """Return a word with the specified key or the first letter underlined."""
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 if key is None:
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 key = word[0]
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 index = word.find(key)
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 before, keyword, after = word[:index], word[index], word[index + 1 :]
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 return Text(before) + Text(keyword, style="shortcut") + Text(after)
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
128
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 def get_micro_display(self):
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 if self.webrtc.audio_muted:
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 return Panel(Text("🔇 ") + self.styled_shortcut_key("Muted"), expand=False)
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 else:
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 return Panel(
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 Text("🎤 ") + self.styled_shortcut_key("Unmuted", "m"), expand=False
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 )
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
136
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 def get_video_display(self):
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 if self.webrtc.video_muted:
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 return Panel(Text("❌ ") + self.styled_shortcut_key("Video Off"), expand=False)
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 else:
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 return Panel(Text("🎥 ") + self.styled_shortcut_key("Video On"), expand=False)
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
142
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 def get_phone_display(self):
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 return Panel(Text("📞 ") + self.styled_shortcut_key("Hang up"), expand=False)
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
145
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 @group()
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 def generate_control_bar(self):
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 """Return the full interface display."""
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 yield Columns(
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 [
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 self.get_micro_display(),
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 self.get_video_display(),
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 self.get_phone_display(),
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 ],
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 expand=False,
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 title="Calling [bold center]{}[/]".format(self.webrtc.callee),
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 )
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
158
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 async def start(self):
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 done = asyncio.Event()
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 input = create_input()
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
162
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 def keys_ready(live):
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 for key_press in input.read_keys():
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 char = key_press.key.lower()
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 if char == "m":
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 # audio mute
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 self.webrtc.audio_muted = not self.webrtc.audio_muted
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
169 live.update(self.generate_control_bar(), refresh=True)
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 elif char == "v":
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 # video mute
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 self.webrtc.video_muted = not self.webrtc.video_muted
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 live.update(self.generate_control_bar(), refresh=True)
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 elif char == "h" or key_press.key == Keys.ControlC:
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
175 # Hang up
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
176 done.set()
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 elif char == "d":
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
178 # generate dot file for debugging. Only available if
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 # ``GST_DEBUG_DUMP_DOT_DIR`` is set. Filename is "pipeline.dot" with a
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 # timestamp.
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 if os.getenv("GST_DEBUG_DUMP_DOT_DIR"):
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 self.webrtc.generate_dot_file()
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 self.host.disp("Dot file generated.")
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
184
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 with Live(
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 self.generate_control_bar(),
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 console=self.host.console,
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 auto_refresh=False
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 ) as live:
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
190 with input.raw_mode():
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 with input.attach(partial(keys_ready, live)):
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
192 await done.wait()
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
193
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
194 await self.webrtc.end_call()
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
195 await self.host.a_quit()
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
196
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
197
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
198 class Common(base.CommandBase):
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
199
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
200 def add_parser_options(self):
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
201 self.parser.add_argument(
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
202 "--no-ui", action="store_true", help=_("disable user interface")
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
203 )
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
204
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
205 async def start(self):
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
206 root_logger = logging.getLogger()
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
207 # we don't want any formatting for messages from webrtc
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
208 for handler in root_logger.handlers:
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
209 handler.setFormatter(None)
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
210 if self.verbosity == 0:
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 root_logger.setLevel(logging.ERROR)
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
212 if self.verbosity >= 1:
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
213 root_logger.setLevel(logging.WARNING)
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
214 if self.verbosity >= 2:
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
215 root_logger.setLevel(logging.DEBUG)
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
216
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
217 async def start_ui(self, webrtc_call):
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
218 if not self.args.no_ui:
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
219 ui = UI(self.host, webrtc_call.webrtc)
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 await ui.start()
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
221
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
222
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 class Make(Common):
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
224 def __init__(self, host):
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
225 super().__init__(
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 host,
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
227 "make",
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
228 use_verbose=True,
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
229 help=_("start a call"),
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
230 )
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
231
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
232 def add_parser_options(self):
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
233 super().add_parser_options()
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
234 self.parser.add_argument(
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
235 "entity",
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
236 metavar="JID",
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
237 help=_("JIDs of entity to call"),
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
238 )
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
239
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
240 async def start(self):
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
241 await super().start()
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
242 callee = jid.JID(self.args.entity)
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
243 webrtc_call = WebRTCCall(self.host, self.profile, callee)
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
244 await webrtc_call.start()
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
245 await super().start_ui(webrtc_call)
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
246
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
247
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
248 class Receive(Common):
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
249 def __init__(self, host):
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
250 super().__init__(
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
251 host,
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
252 "receive",
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
253 use_verbose=True,
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
254 help=_("wait for a call"),
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
255 )
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
256
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
257 def add_parser_options(self):
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
258 super().add_parser_options()
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
259 auto_accept_group = self.parser.add_mutually_exclusive_group()
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
260 auto_accept_group.add_argument(
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
261 "-a",
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
262 "--auto-accept",
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
263 action="append",
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
264 metavar="JID",
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
265 default=[],
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
266 help=_("automatically accept call from this jid (can be used multiple times)")
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
267 )
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
268 auto_accept_group.add_argument(
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
269 "--auto-accept-all",
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
270 action="store_true",
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
271 help=_("automatically accept call from anybody")
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
272 )
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
273
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
274 async def on_action_new(
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
275 self, action_data_s: str, action_id: str, security_limit: int, profile: str
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
276 ) -> None:
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
277 if profile != self.profile:
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
278 return
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
279 action_data = data_format.deserialise(action_data_s)
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
280 if action_data.get("type") != C.META_TYPE_CALL:
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
281 return
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
282 peer_jid = jid.JID(action_data["from_jid"]).bare
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
283 caller = peer_jid.bare
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
284 if (
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
285 not self.args.auto_accept_all
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
286 and caller not in self.args.auto_accept
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
287 and not await self.host.confirm(
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
288 _("📞 Incoming call from {caller}, do you accept?").format(
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
289 caller=caller
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
290 )
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
291 )
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
292 ):
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
293 await self.host.bridge.action_launch(
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
294 action_id, data_format.serialise({"cancelled": True}), profile
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
295 )
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
296 return
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
297
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
298 self.disp(_("✅ Incoming call from {caller} accepted.").format(caller=caller))
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
299
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
300 webrtc_call = WebRTCCall(self.host, self.profile, peer_jid)
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
301 webrtc_call.sid = action_data["session_id"]
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
302 await self.host.bridge.action_launch(
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
303 action_id, data_format.serialise({"cancelled": False}), profile
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
304 )
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
305 await super().start_ui(webrtc_call)
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
306
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
307 async def start(self):
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
308 await super().start()
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
309 self.host.bridge.register_signal("action_new", self.on_action_new, "core")
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
310
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
311
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
312 class Call(base.CommandBase):
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
313 subcommands = (Make, Receive)
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
314
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
315 def __init__(self, host):
849721e1563b cli: `call` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
316 super().__init__(host, "call", use_profile=False, help=_("A/V calls and related"))