Skip to content

Commit

Permalink
Code refactoring (now more abstracted from Telegram tgcalls. The Tele…
Browse files Browse the repository at this point in the history
…gram library has a minimum number of changes).

 The race conditions remains to be. GroupCall doesn't always stop properly...
  • Loading branch information
MarshalX committed Mar 14, 2021
1 parent 8391720 commit 6226f3f
Show file tree
Hide file tree
Showing 15 changed files with 142 additions and 491 deletions.
2 changes: 2 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DisableFormat: true
SortIncludes: false
26 changes: 16 additions & 10 deletions pytgcalls/pytgcalls/group_call_native.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,17 +285,19 @@ def remove_update_handler(self):
self.client.remove_handler(self._update_handler, self._handler_group)
self._handler_group = None

async def stop(self):
"""Properly stop tgcalls, remove pyrogram handler, leave from server side."""

await self.leave_current_group_call()
def __properly_stop(self):
self.remove_update_handler()
self.__deinit_native_instance()

self.my_ssrc = self.group_call = self.chat_peer = self.full_chat = None
self.is_connected = False

self.remove_update_handler()
self.__deinit_native_instance()
logger.debug('GroupCall stop.')
logger.debug('GroupCall properly stop.')

async def stop(self):
"""Properly stop tgcalls, remove pyrogram handler, leave from server side."""
logger.debug('Stop requested. Wait for disconnected status.')
self.__set_connection_mode(tgcalls.GroupConnectionMode.GroupConnectionModeNone)
await self.leave_current_group_call()

async def start(self, group: Union[str, int, InputPeerChannel, InputPeerChat], enable_action=True):
"""Start voice chat (join and play/record from initial values).
Expand Down Expand Up @@ -372,15 +374,17 @@ def restart_playout(self):
Note:
Device restart needed to apply new filename in tgcalls.
"""
self.__native_instance.reinitAudioInputDevice()

self.__native_instance.restartAudioInputDevice()

def restart_recording(self):
"""Start recording to outpufile from begin or just restart recording device.
Note:
Device restart needed to apply new filename in tgcalls.
"""
self.__native_instance.reinitAudioOutputDevice()

self.__native_instance.restartAudioOutputDevice()

def __participant_descriptions_required_callback(self, ssrcs_list: List[int]):
# TODO optimize
Expand Down Expand Up @@ -414,6 +418,8 @@ def __network_state_updated_callback(self, state: bool):
self.set_is_mute(False)
if self.enable_action:
self.__start_status_worker()
else:
self.__properly_stop()

self.trigger_handlers(GroupCallNativeAction.NETWORK_STATUS_CHANGED, self, state)

Expand Down
24 changes: 13 additions & 11 deletions pytgcalls/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,21 +456,23 @@ async def main(client1, client2, make_out, make_inc):
@group_call.on_playout_ended
async def playout_ended_handler(group_call, filename):
print(f'{filename} is ended')
await group_call.stop()
# await group_call.stop()

'''
await asyncio.sleep(10)
group_call.pause_playout()
group_call.pause_recording()
await asyncio.sleep(5)
group_call.resume_playout()
group_call.resume_recording()

# await asyncio.sleep(10)
# group_call.pause_playout()
# group_call.pause_recording()
# await asyncio.sleep(5)
# group_call.resume_playout()
# group_call.resume_recording()
await asyncio.sleep(5)
group_call.input_filename = 'input.raw'
await asyncio.sleep(10)
group_call.stop_playout()
await asyncio.sleep(5)
# group_call.stop_playout()
# group_call.stop_output()
await group_call.stop()
'''
print('pup')


'''
Expand Down
4 changes: 3 additions & 1 deletion tgcalls/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ include(cmake/lib_tgcalls.cmake)

list(APPEND SOURCES
${src_loc}/tgcalls.cpp
${src_loc}/FileAudioDevice.cpp
${src_loc}/FileAudioDevice.h
${src_loc}/FileAudioDeviceDescriptor.h
${src_loc}/NativeInstance.h
${src_loc}/NativeInstance.cpp
${src_loc}/RtcServer.h
${src_loc}/RtcServer.cpp
${src_loc}/InstanceHolder.h
${src_loc}/FileAudioDeviceDescriptor.h
)

pybind11_add_module(tgcalls ${SOURCES})
Expand Down
3 changes: 0 additions & 3 deletions tgcalls/cmake/lib_tgcalls.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ PRIVATE
VideoCaptureInterfaceImpl.h
VideoCapturerInterface.h

FileAudioDevice.cpp
FileAudioDevice.h

group/GroupInstanceCustomImpl.cpp
group/GroupInstanceCustomImpl.h
group/GroupNetworkManager.cpp
Expand Down
Loading

0 comments on commit 6226f3f

Please sign in to comment.