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