diff --git a/toxygen/callbacks.py b/toxygen/callbacks.py index e280c06..5151637 100644 --- a/toxygen/callbacks.py +++ b/toxygen/callbacks.py @@ -315,7 +315,7 @@ def callback_audio(toxav, friend_number, samples, audio_samples_per_channel, aud """ New audio chunk """ - Profile.get_instance().call.chunk( + Profile.get_instance().call.audio_chunk( bytes(samples[:audio_samples_per_channel * 2 * audio_channels_count]), audio_channels_count, rate) diff --git a/toxygen/calls.py b/toxygen/calls.py index 2f02b83..7dc6b67 100644 --- a/toxygen/calls.py +++ b/toxygen/calls.py @@ -4,14 +4,14 @@ import threading import settings from toxav_enums import * # TODO: play sound until outgoing call will be started or cancelled and add timeout -# TODO: add widget for call -CALL_TYPE = { - 'NONE': 0, - 'AUDIO': 1, - 'VIDEO': 2 -} -# TODO: rewrite (make class) + +class Call: + + def __init__(self, audio=False, video=False): + self.audio = audio + self.video = video + # TODO: add widget for call class AV: @@ -20,7 +20,7 @@ class AV: self._toxav = toxav self._running = True - self._calls = {} # dict: key - friend number, value - call type + self._calls = {} # dict: key - friend number, value - Call instance self._audio = None self._audio_stream = None @@ -33,15 +33,30 @@ class AV: self._audio_duration = 60 self._audio_sample_count = self._audio_rate * self._audio_channels * self._audio_duration // 1000 + def stop(self): + self._running = False + self.stop_audio_thread() + def __contains__(self, friend_number): return friend_number in self._calls + # ----------------------------------------------------------------------------------------------------------------- + # Calls + # ----------------------------------------------------------------------------------------------------------------- + def __call__(self, friend_number, audio, video): """Call friend with specified number""" self._toxav.call(friend_number, 32 if audio else 0, 5000 if video else 0) - self._calls[friend_number] = CALL_TYPE['AUDIO'] + self._calls[friend_number] = Call(audio, video) self.start_audio_thread() + def accept_call(self, friend_number, audio_enabled, video_enabled): + + if self._running: + self._calls[friend_number] = Call(audio_enabled, video_enabled) + self._toxav.answer(friend_number, 32 if audio_enabled else 0, 5000 if video_enabled else 0) + self.start_audio_thread() + def finish_call(self, friend_number, by_friend=False): if not by_friend: @@ -51,9 +66,21 @@ class AV: if not len(self._calls): self.stop_audio_thread() - def stop(self): - self._running = False - self.stop_audio_thread() + def toxav_call_state_cb(self, friend_number, state): + """ + New call state + """ + pass # TODO: ignore? + # if self._running: + # + # if state & TOXAV_FRIEND_CALL_STATE['ACCEPTING_A']: + # self._calls[friend_number].audio = True + # if state & TOXAV_FRIEND_CALL_STATE['ACCEPTING_V']: + # self._calls[friend_number].video = True + + # ----------------------------------------------------------------------------------------------------------------- + # Threads + # ----------------------------------------------------------------------------------------------------------------- def start_audio_thread(self): """ @@ -93,7 +120,11 @@ class AV: self._out_stream.close() self._out_stream = None - def chunk(self, samples, channels_count, rate): + # ----------------------------------------------------------------------------------------------------------------- + # Incoming chunks + # ----------------------------------------------------------------------------------------------------------------- + + def audio_chunk(self, samples, channels_count, rate): """ Incoming chunk """ @@ -106,6 +137,13 @@ class AV: output=True) self._out_stream.write(samples) + def video_chunk(self): + pass + + # ----------------------------------------------------------------------------------------------------------------- + # AV sending + # ----------------------------------------------------------------------------------------------------------------- + def send_audio(self): """ This method sends audio to friends @@ -116,7 +154,7 @@ class AV: pcm = self._audio_stream.read(self._audio_sample_count) if pcm: for friend in self._calls: - if self._calls[friend] & 1: + if self._calls[friend].audio: try: self._toxav.audio_send_frame(friend, pcm, self._audio_sample_count, self._audio_channels, self._audio_rate) @@ -126,20 +164,3 @@ class AV: pass time.sleep(0.01) - - def accept_call(self, friend_number, audio_enabled, video_enabled): - - if self._running: - self._calls[friend_number] = int(video_enabled) * 2 + int(audio_enabled) - self._toxav.answer(friend_number, 32 if audio_enabled else 0, 5000 if video_enabled else 0) - self.start_audio_thread() - - def toxav_call_state_cb(self, friend_number, state): - """ - New call state - """ - if self._running: - - if state & TOXAV_FRIEND_CALL_STATE['ACCEPTING_A']: - self._calls[friend_number] |= 1 - diff --git a/toxygen/profile.py b/toxygen/profile.py index 3f48cc9..e89bf2b 100644 --- a/toxygen/profile.py +++ b/toxygen/profile.py @@ -39,6 +39,7 @@ class Profile(basecontact.BaseContact, Singleton): self._tox = tox self._file_transfers = {} # dict of file transfers. key - tuple (friend_number, file_number) self._call = calls.AV(tox.AV) # object with data about calls + self._call_widgets = {} # dict of incoming call widgets self._incoming_calls = set() self._load_history = True self._waiting_for_reconnection = False @@ -1237,10 +1238,9 @@ class Profile(basecontact.BaseContact, Singleton): self._messages.scrollToBottom() else: friend.actions = True - # TODO: dict of widgets - self._call_widget = avwidgets.IncomingCallWidget(friend_number, text, friend.name) - self._call_widget.set_pixmap(friend.get_pixmap()) - self._call_widget.show() + self._call_widgets[friend_number] = avwidgets.IncomingCallWidget(friend_number, text, friend.name) + self._call_widgets[friend_number].set_pixmap(friend.get_pixmap()) + self._call_widgets[friend_number].show() def accept_call(self, friend_number, audio, video): """ @@ -1250,8 +1250,7 @@ class Profile(basecontact.BaseContact, Singleton): self._screen.active_call() if friend_number in self._incoming_calls: self._incoming_calls.remove(friend_number) - if hasattr(self, '_call_widget'): - del self._call_widget + del self._call_widgets[friend_number] def stop_call(self, friend_number, by_friend): """ @@ -1265,8 +1264,8 @@ class Profile(basecontact.BaseContact, Singleton): self._screen.call_finished() self._call.finish_call(friend_number, by_friend) # finish or decline call if hasattr(self, '_call_widget'): - self._call_widget.close() - del self._call_widget + self._call_widget[friend_number].close() + del self._call_widget[friend_number] friend = self.get_friend_by_number(friend_number) friend.append_message(InfoMessage(text, time.time())) if friend_number == self.get_active_number():