annotate libervia/desktop_kivy/plugins/plugin_transfer_voice.py @ 512:644a8d165e5a

plugin calls: use the new binding feature of webrtc module: the new binding feature is used to update the "Desktop" button if sharing is cancelled by user, or stopped somehow. rel 434
author Goffi <goffi@goffi.org>
date Thu, 18 Jan 2024 23:31:29 +0100
parents b3cedbee561d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
379
1da3c379205b fixed shebangs
Goffi <goffi@goffi.org>
parents: 378
diff changeset
1 #!/usr/bin/env python3
1da3c379205b fixed shebangs
Goffi <goffi@goffi.org>
parents: 378
diff changeset
2
95
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
3
493
b3cedbee561d refactoring: rename `cagou` to `libervia.desktop_kivy` + update imports and names following backend changes
Goffi <goffi@goffi.org>
parents: 491
diff changeset
4 #Libervia Desktop-Kivy
461
3c9ba4a694ef dates update
Goffi <goffi@goffi.org>
parents: 400
diff changeset
5 # Copyright (C) 2016-2021 Jérôme Poisson (goffi@goffi.org)
95
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
6
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
11
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
16
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
19
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
20
493
b3cedbee561d refactoring: rename `cagou` to `libervia.desktop_kivy` + update imports and names following backend changes
Goffi <goffi@goffi.org>
parents: 491
diff changeset
21 from libervia.backend.core import log as logging
95
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
22 log = logging.getLogger(__name__)
493
b3cedbee561d refactoring: rename `cagou` to `libervia.desktop_kivy` + update imports and names following backend changes
Goffi <goffi@goffi.org>
parents: 491
diff changeset
23 from libervia.backend.core.i18n import _
95
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from kivy.uix.boxlayout import BoxLayout
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
25 import sys
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
26 import time
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from kivy.clock import Clock
96
641678ddc26c upload (voice): fixed crash on desktop
Goffi <goffi@goffi.org>
parents: 95
diff changeset
28 from kivy import properties
95
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
29 if sys.platform == "android":
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from plyer import audio
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
31
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
32
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
33 PLUGIN_INFO = {
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 284
diff changeset
34 "name": _("voice"),
95
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
35 "main": "VoiceRecorder",
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
36 "platforms": ["android"],
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 284
diff changeset
37 "description": _("transmit a voice record"),
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 284
diff changeset
38 "icon_medium": "{media}/icons/muchoslava/png/micro_off_50.png",
400
71f51198478c android: handle runtime permissions:
Goffi <goffi@goffi.org>
parents: 379
diff changeset
39 "android_permissons": ["RECORD_AUDIO"],
95
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
40 }
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
41
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
42
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
43 class VoiceRecorder(BoxLayout):
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
44 callback = properties.ObjectProperty()
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
45 cancel_cb = properties.ObjectProperty()
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
46 recording = properties.BooleanProperty(False)
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
47 playing = properties.BooleanProperty(False)
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
48 time = properties.NumericProperty(0)
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
49
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
50 def __init__(self, **kwargs):
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
51 super(VoiceRecorder, self).__init__(**kwargs)
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
52 self._started_at = None
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
53 self._counter_timer = None
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
54 self._play_timer = None
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
55 self.record_time = None
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
56 self.audio = audio
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
57 self.audio.file_path = "/sdcard/cagou_record.3gp"
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
58
491
203755bbe0fe massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 461
diff changeset
59 def _update_timer(self, dt):
95
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
60 self.time = int(time.time() - self._started_at)
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
61
491
203755bbe0fe massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 461
diff changeset
62 def switch_recording(self):
95
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
63 if self.playing:
491
203755bbe0fe massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 461
diff changeset
64 self._stop_playing()
95
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
65 if self.recording:
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
66 try:
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
67 audio.stop()
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
68 except Exception as e:
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
69 # an exception can happen if record is pressed
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
70 # repeatedly in a short time (not a normal use)
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 284
diff changeset
71 log.warning("Exception on stop: {}".format(e))
95
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
72 self._counter_timer.cancel()
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
73 self.time = self.time + 1
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
74 else:
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
75 audio.start()
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
76 self._started_at = time.time()
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
77 self.time = 0
491
203755bbe0fe massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 461
diff changeset
78 self._counter_timer = Clock.schedule_interval(self._update_timer, 1)
95
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
79
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
80 self.recording = not self.recording
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
81
491
203755bbe0fe massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 461
diff changeset
82 def _stop_playing(self, __=None):
95
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
83 if self.record_time is None:
491
203755bbe0fe massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 461
diff changeset
84 log.error("_stop_playing should no be called when record_time is None")
95
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
85 return
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
86 audio.stop()
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
87 self.playing = False
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
88 self.time = self.record_time
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
89 if self._counter_timer is not None:
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
90 self._counter_timer.cancel()
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
91
491
203755bbe0fe massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 461
diff changeset
92 def play_record(self):
95
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
93 if self.recording:
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
94 return
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
95 if self.playing:
491
203755bbe0fe massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 461
diff changeset
96 self._stop_playing()
95
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
97 else:
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
98 try:
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
99 audio.play()
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
100 except Exception as e:
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
101 # an exception can happen in the same situation
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
102 # as for audio.stop() above (i.e. bad record)
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 284
diff changeset
103 log.warning("Exception on play: {}".format(e))
95
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
104 self.time = 0
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
105 return
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
106
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
107 self.playing = True
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
108 self.record_time = self.time
491
203755bbe0fe massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 461
diff changeset
109 Clock.schedule_once(self._stop_playing, self.time + 1)
95
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
110 self._started_at = time.time()
3e3c097b07b7 upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff changeset
111 self.time = 0
491
203755bbe0fe massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 461
diff changeset
112 self._counter_timer = Clock.schedule_interval(self._update_timer, 0.5)