Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pytgcalls 0.0.11, tgcalls 0.0.7 #47

Merged
merged 4 commits into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions pytgcalls/pytgcalls/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
# You should have received a copy of the GNU Lesser General Public License v3
# along with tgcalls. If not, see <http://www.gnu.org/licenses/>.

from pytgcalls.group_call_native import GroupCallNative, GroupCallAction
from pytgcalls.group_call import GroupCall
from pytgcalls.group_call_native import GroupCallNative, GroupCallNativeAction, GroupCallNativeDispatcherMixin
from pytgcalls.action import Action
from pytgcalls.group_call import GroupCall, GroupCallAction
from pytgcalls.dispatcher import Dispatcher
from pytgcalls.dispatcher_mixin import DispatcherMixin

__all__ = ['GroupCallNative', 'GroupCall', 'Dispatcher', 'DispatcherMixin', 'GroupCallAction']
__version__ = '0.0.10'
__all__ = ['GroupCallNative', 'GroupCall', 'Dispatcher', 'DispatcherMixin', 'Action',
'GroupCallNativeAction', 'GroupCallNativeDispatcherMixin', 'GroupCallAction']
__version__ = '0.0.11'
26 changes: 26 additions & 0 deletions pytgcalls/pytgcalls/action.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# tgcalls - Python binding for tgcalls (c++ lib by Telegram)
# pytgcalls - Library connecting python binding for tgcalls and Pyrogram
# Copyright (C) 2020-2021 Il`ya (Marshal) <https://github.com/MarshalX>
#
# This file is part of tgcalls and pytgcalls.
#
# tgcalls and pytgcalls is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# tgcalls and pytgcalls is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License v3
# along with tgcalls. If not, see <http://www.gnu.org/licenses/>.


class Action:
def __set_name__(self, owner, name):
self.name = name

def __get__(self, instance, owner):
return self.name
6 changes: 3 additions & 3 deletions pytgcalls/pytgcalls/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ def __init__(self, available_actions):

def __build_handler_storage(self):
logger.debug('Build storage of handlers for dispatcher.')
return {action: [] for action in self.actions}
return {action: [] for action in dir(self.actions) if not action.startswith('_')}

def add_handler(self, callback, action):
logger.debug('Add handler..')
logger.debug(f'Add handler to {action} action..')
if not asyncio.iscoroutinefunction(callback):
raise RuntimeError('Sync callback does not supported')

Expand All @@ -52,7 +52,7 @@ def add_handler(self, callback, action):
return callback

def remove_handler(self, callback, action) -> bool:
logger.debug('Remove handler..')
logger.debug(f'Remove handler of {action} action..')
try:
handlers = self.__action_to_handlers[action]
for i in range(len(handlers)):
Expand Down
41 changes: 34 additions & 7 deletions pytgcalls/pytgcalls/group_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,52 @@

import pyrogram

from pytgcalls import GroupCallNative
import tgcalls
from pytgcalls import GroupCallNative, GroupCallNativeAction, GroupCallNativeDispatcherMixin, Action


class GroupCall(GroupCallNative):
class GroupCallAction(GroupCallNativeAction):
PLAYOUT_ENDED = Action()


class GroupCallDispatcherMixin(GroupCallNativeDispatcherMixin):

def on_playout_ended(self, func: callable):
return self.add_handler(func, GroupCallAction.PLAYOUT_ENDED)


class GroupCall(GroupCallNative, GroupCallDispatcherMixin):

def __init__(
self,
client: pyrogram.Client,
input_filename: str = '',
output_filename: str = '',
enable_logs_to_console=False,
path_to_log_file='group_call.log'
path_to_log_file='group_call.log',
play_on_repeat=True
):
super().__init__(client, enable_logs_to_console, path_to_log_file)
self.__use_file_audio_device = True
super(GroupCallDispatcherMixin, self).__init__(GroupCallAction)

self.play_on_repeat = play_on_repeat

self._input_filename = input_filename or ''
self._output_filename = output_filename or ''

def __create_file_audio_device_descriptor(self):
file_audio_device_descriptor = tgcalls.FileAudioDeviceDescriptor()
file_audio_device_descriptor.getInputFilename = self.__get_input_filename_callback
file_audio_device_descriptor.getOutputFilename = self.__get_output_filename_callback
file_audio_device_descriptor.isEndlessPlayout = self.__is_endless_playout_callback
file_audio_device_descriptor.playoutEndedCallback = self.__playout_ended_callback

return file_audio_device_descriptor

async def start(self, group: Union[str, int], enable_action=True):
await super().start(group, enable_action)

await self._start_group_call(
self.__use_file_audio_device, self.__get_input_filename_callback, self.__get_output_filename_callback
)
await self._start_group_call(self.__create_file_audio_device_descriptor())

def stop_playout(self):
self.input_filename = ''
Expand Down Expand Up @@ -78,3 +99,9 @@ def __get_input_filename_callback(self):

def __get_output_filename_callback(self):
return self._output_filename

def __is_endless_playout_callback(self):
return self.play_on_repeat

def __playout_ended_callback(self, input_filename: str):
self.trigger_handlers(GroupCallAction.PLAYOUT_ENDED, self, input_filename)
52 changes: 21 additions & 31 deletions pytgcalls/pytgcalls/group_call_native.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
import asyncio
import json
import logging
from enum import Enum
from typing import Callable, List, Union
from typing import List, Union

import pyrogram
from pyrogram import raw
Expand All @@ -31,6 +30,7 @@
from pyrogram.raw.types import InputPeerChannel, InputPeerChat

import tgcalls
from .action import Action
from .dispatcher_mixin import DispatcherMixin

logger = logging.getLogger(__name__)
Expand All @@ -39,14 +39,14 @@
int_ssrc = lambda ssrc: ssrc if ssrc < 2 ** 31 else ssrc - 2 ** 32


class GroupCallAction(Enum):
NETWORK_STATUS_CHANGED = 0
class GroupCallNativeAction:
NETWORK_STATUS_CHANGED = Action()


class GroupCallDispatcherMixin(DispatcherMixin):
class GroupCallNativeDispatcherMixin(DispatcherMixin):

def on_network_status_changed(self, func: callable):
return self.add_handler(func, GroupCallAction.NETWORK_STATUS_CHANGED)
return self.add_handler(func, GroupCallNativeAction.NETWORK_STATUS_CHANGED)


def parse_call_participant(participant_data):
Expand All @@ -58,7 +58,7 @@ def parse_call_participant(participant_data):
return native_participant


class GroupCallNative(GroupCallDispatcherMixin):
class GroupCallNative(GroupCallNativeDispatcherMixin):
SEND_ACTION_UPDATE_EACH = 0.45

def __init__(
Expand All @@ -67,7 +67,7 @@ def __init__(
enable_logs_to_console: bool,
path_to_log_file: str
):
super().__init__(GroupCallAction)
super().__init__(GroupCallNativeAction)
self.client = client

self.__native_instance = None
Expand Down Expand Up @@ -100,9 +100,16 @@ def __deinit_native_instance(self):
del tmp
logger.debug('Native instance destroyed.')

def __setup_native_instance(self):
def __create_and_setup_native_instance(self):
logger.debug('Create a new native instance..')
native_instance = tgcalls.NativeInstance()
native_instance = tgcalls.NativeInstance(self.enable_logs_to_console, self.path_to_log_file)

native_instance.setupGroupCall(
self.__emit_join_payload_callback,
self.__network_state_updated_callback,
self.__participant_descriptions_required_callback
)

logger.debug('Native instance created.')

return native_instance
Expand Down Expand Up @@ -240,7 +247,7 @@ async def start(self, group: Union[str, int], enable_action=True):

handler_group = await self.__set_and_get_handler_group()
self.client.add_handler(self._update_handler, handler_group)
self.__native_instance = self.__setup_native_instance()
self.__native_instance = self.__create_and_setup_native_instance()

self.enable_action = enable_action
self.my_user_id = await self.client.storage.user_id()
Expand All @@ -252,26 +259,9 @@ async def reconnect(self):
await self.stop()
await self.start(chat_peer, enable_action)

async def _start_group_call(
self,
use_file_audio_device: bool,
get_input_filename_callback: Callable,
get_output_filename_callback: Callable
):
async def _start_group_call(self, *args):
logger.debug('Start native group call..')
# TODO move callbacks to __setup_native_instance
self.__native_instance.startGroupCall(
self.enable_logs_to_console,
self.path_to_log_file,

use_file_audio_device,

self.__emit_join_payload_callback,
self.__network_state_updated_callback,
self.__participant_descriptions_required_callback,
get_input_filename_callback,
get_output_filename_callback
)
self.__native_instance.startGroupCall(*args)

def set_is_mute(self, is_muted: bool):
logger.debug(f'Set is muted. New value: {is_muted}.')
Expand Down Expand Up @@ -321,7 +311,7 @@ def __network_state_updated_callback(self, state: bool):
if self.enable_action:
self.__start_status_worker()

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

logger.debug(f'New network state is {self.is_connected}.')

Expand Down
2 changes: 1 addition & 1 deletion pytgcalls/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
long_description=readme,
long_description_content_type='text/markdown',
packages=packages,
install_requires=['tgcalls == 0.0.6', 'pyrogram >= 1.1.13'],
install_requires=['tgcalls == 0.0.7', 'pyrogram >= 1.1.13'],
python_requires="~=3.6",
include_package_data=True,
classifiers=[
Expand Down
20 changes: 11 additions & 9 deletions pytgcalls/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,22 +445,24 @@ async def main(client1, client2, make_out, make_inc):

# @client2.on_message(filters.text & filters.outgoing & ~filters.edited & filters.command('test', prefixes='!'))
# async def test(client, message):
group_call = GroupCall(client2, 'input.raw', '', True, '')
await group_call.start('@MarshalCh')
group_call = GroupCall(client2, '6s.raw', '', False, '')
await group_call.start('@MarshalCm')

group_call.add_handler(
network_status_changed_handler,
GroupCallAction.NETWORK_STATUS_CHANGED
)

from random import randint
@group_call.on_playout_ended
async def playout_ended_handler(group_call, filename):
print(f'{filename} is ended')

e = True
while 1:
# break
await asyncio.sleep(5)
await group_call.set_my_volume(1 if e else 200)
e = not e
group_call.play_on_repeat = False
await asyncio.sleep(15)
group_call.restart_playout()
await asyncio.sleep(15)
group_call.play_on_repeat = True
group_call.restart_playout()

'''

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def build_extension(self, ext):

setup(
name='tgcalls',
version=f'0.0.6',
version=f'0.0.7',
author='Il\'ya Semyonov',
author_email='[email protected]',
url='https://github.com/MarshalX/tgcalls',
Expand Down
1 change: 1 addition & 0 deletions tgcalls/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ list(APPEND SOURCES
${src_loc}/RtcServer.h
${src_loc}/RtcServer.cpp
${src_loc}/InstanceHolder.h
${src_loc}/FileAudioDeviceDescriptor.h
)

pybind11_add_module(tgcalls ${SOURCES})
Expand Down
14 changes: 14 additions & 0 deletions tgcalls/src/FileAudioDeviceDescriptor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include <string>


class FileAudioDeviceDescriptor {
public:
std::function<std::string()> _getInputFilename = nullptr;
std::function<std::string()> _getOutputFilename = nullptr;

std::function<bool()> _isEndlessPlayout = nullptr;

std::function<void(std::string)> _playoutEndedCallback = nullptr;
};
Loading