-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclivrt.py
182 lines (164 loc) · 7.96 KB
/
clivrt.py
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
import configparser
import traceback
import logging
import asyncio
import io
import IPython.display as ipd
import grpc
import riva.client
import prompt_toolkit
from prompt_toolkit import PromptSession
from prompt_toolkit.completion import WordCompleter, NestedCompleter
from prompt_toolkit.shortcuts.prompt import prompt
from prompt_toolkit.styles import Style
# TODO Will use these soon to split the screen into sections
# from prompt_toolkit.application import Application
# from prompt_toolkit.layout.containers import HSplit, VSplit, Window, WindowAlign
# from prompt_toolkit.layout.controls import BufferControl, FormattedTextControl
# from prompt_toolkit.layout.layout import Layout
from cli.commands.call import Call
from cli.commands.hangup import Hangup
from cli.commands.say import Say
from cli.commands.login import Login
from cli.commands.logout import Logout
from cli.commands.quit import Quit
from cli.commands.lookup import Lookup
from cli.datamodel.session import Session
from cli.network.networkmanager import NetworkManager
from cli import printf
from cli.media import videotransformtrack
from copy import deepcopy
config = configparser.ConfigParser()
config.read('.clivrt')
logging.basicConfig(level=logging.WARNING)
loglevel_str = config.defaults().get('loglevel', 'WARNING')
if 'DEBUG' in loglevel_str.upper(): logging.getLogger().setLevel(logging.DEBUG)
if 'INFO' in loglevel_str.upper(): logging.getLogger().setLevel(logging.INFO)
if 'WARN' in loglevel_str.upper(): logging.getLogger().setLevel(logging.WARN)
if 'ERR' in loglevel_str.upper(): logging.getLogger().setLevel(logging.ERROR)
if 'CRI' in loglevel_str.upper(): logging.getLogger().setLevel(logging.CRITICAL)
better_completer = NestedCompleter.from_nested_dict({
'call': None, 'hangup': None, # 1-1 call
'join': {}, 'leave': {}, # join leave rooms
'login': None, 'logout': None, 'lookup': None, 'whoami': None, # directory and addressbook
'set': { # set various states (like availability)
'donotdisturb': None,
'away': None,
'available': None
},
'say': None, # text chat can be done via signaling server
# 'carrierpigeon': { # if the signaling server is offline we can manually connect peers
# 'gen-video-offer': None,
# 'gen-video-answer': {'from-video-offer': None},
# 'use-video-answer': None
# },
'exit': None, 'quit': None # exit
})
session = Session(config)
network_mgr = NetworkManager(session=session, config=config)
# NVIDIA RIVA setup here (might need to move into the network folder and a custom class)
asr_on = config.defaults().get('asr_on', 'false')
riva_asr = None
if asr_on.lower() == 'true':
riva_host_uri = config.defaults().get('asr_hosturl', 'localhost:50051')
logging.debug(f'Setting up connection to RIVA server text captions, host=' + riva_host_uri)
try:
auth = riva.client.Auth(uri=riva_host_uri)
riva_asr = riva.client.ASRService(auth)
offline_config = riva.client.RecognitionConfig(
encoding=riva.client.AudioEncoding.LINEAR_PCM,
max_alternatives=1,
profanity_filter=False,
enable_automatic_punctuation=True,
verbatim_transcripts=False,
audio_channel_count=1,
)
streaming_config = riva.client.StreamingRecognitionConfig(config=deepcopy(offline_config), interim_results=True)
# TODO RIVA check for server to make sure its up and running
#audio_chunk_iterator = riva.client.AudioChunkFileIterator(my_wav_file, 4800, riva.client.sleep_audio_length)
#response_generator = asr_service.streaming_response_generator(audio_chunk_iterator, streaming_config)
#riva.client.print_streaming(response_generator, show_intermediate=True)
raise Exception('RIVA is not yet implemented')
session.riva_connection.status = session.riva_connection.status.CONNECTED
except Exception as e:
logging.error(f'Failed to connect to asr server - captions will be unavailable, error=' + str(e))
session.riva_connection.status = session.riva_connection.status.NOTCONNECTED
tbstyle = Style.from_dict({
'bottom-toolbar': '#33475b bg:#ffffff',
})
def bottom_toolbar():
# TODO future file data transfer progress (like the pipenv bar)
# TODO debugging status - video sent/received packets + bytes + frames + bitrate + etc...
signaling_status = '⛔'
riva_status = ' '
if network_mgr.is_connected():
signaling_status = '📢 🔓'
if network_mgr.wsclient.secure: signaling_status = '📢 🔒'
if session.riva_connection is not None:
riva_status = session.riva_connection.getDescription()
# return prompt_toolkit.HTML(
return [('class:bottom-toolbar',
session.connection_status.getDescription() + \
' ▪️ ' + signaling_status + \
' ' + riva_status + \
' ▪️ (Press tab to autocomplete, ctrl+d to exit)')]
async def userprompt():
commands = {}
dummy = Call(commands, config, session, network_mgr)
dummy = Hangup(commands, config, session, network_mgr)
dummy = Say(commands, config, session, network_mgr)
dummy = Login(commands, config, session, network_mgr)
dummy = Logout(commands, config, session, network_mgr)
dummy = Quit(commands, config, session, network_mgr)
dummy = Lookup(commands, config, session, network_mgr)
# TODO load all the commands availble from the commands folder vs manually like above (also loop import classes)
prompt_session = PromptSession(
completer=better_completer, bottom_toolbar=bottom_toolbar, style=tbstyle)
while True:
try:
text = await prompt_session.prompt_async('> ')
if not text:
continue
else:
input_cmd_split = text.split()
input_cmd_name, input_cmd_args = input_cmd_split[0], input_cmd_split[1:]
command = commands.get(input_cmd_name) or None
if not command:
printf(f'<error>Unsupported command</error> {input_cmd_name}')
continue
show_help = False
for arg in input_cmd_args: # common args processing done here (versus in every command)
if arg == '-h':
show_help = True
break
if show_help:
command.show_help()
else:
await command.do_command(*input_cmd_args)
await asyncio.sleep(0)
except KeyboardInterrupt:
continue
except EOFError:
break
except Exception as e:
traceback.print_exc()
print('GoodBye!')
async def networktick():
while True:
if network_mgr.is_connected(): await network_mgr.tick()
await asyncio.sleep(0)
vidstyle = config.defaults().get('videostyle', 'just-ascii')
@network_mgr.pc.on('track')
def on_track(track):
logging.debug('Receiving %s' % track.kind)
if track.kind == 'video':
videotransform = videotransformtrack.VideoTransformTrack(track=network_mgr.remote_relay.subscribe(track), config=config) # Create a 'proxy' around the video for transforming
videotransform.ve.set_strategy(vidstyle)
network_mgr.recorder.addTrack(videotransform)
elif track.kind == 'audio':
network_mgr.recorder.addTrack(track)
#if asr_on: # TODO: play audio track if present and pass to RIVA if TTS is enabled
if __name__ == '__main__':
networktick = asyncio.get_event_loop().create_task(networktick())
prompt = asyncio.get_event_loop().create_task(userprompt())
asyncio.get_event_loop().run_forever()