Skip to content

Commit a27c689

Browse files
authored
Merge pull request #42 from moha-abdi/feature/incoming-calls
Feature/incoming calls
2 parents 6a9451b + 4103de5 commit a27c689

8 files changed

Lines changed: 488 additions & 175 deletions

File tree

PySIP/codecs/__init__.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
21
from .g711 import PcmaDecoder, PcmaEncoder, PcmuDecoder, PcmuEncoder
32
from .codec_info import CodecInfo
43

54

6-
CODECS = [
7-
CodecInfo.PCMA,
8-
CodecInfo.PCMU,
9-
CodecInfo.EVENT
10-
]
5+
CODECS = [CodecInfo.PCMA, CodecInfo.PCMU, CodecInfo.EVENT]
116

127

138
def get_encoder(codec: CodecInfo):
@@ -18,6 +13,7 @@ def get_encoder(codec: CodecInfo):
1813
else:
1914
raise ValueError(f"No encoder found for: {codec}")
2015

16+
2117
def get_decoder(codec: CodecInfo):
2218
if codec == CodecInfo.PCMA:
2319
return PcmaDecoder()

PySIP/codecs/codec_info.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ def __int__(self) -> int:
4646
return int(self.value)
4747
except ValueError:
4848
pass
49-
raise Exception(
50-
self.description + " is a dynamically assigned payload"
51-
)
49+
raise Exception(self.description + " is a dynamically assigned payload")
5250

5351
def __str__(self) -> str:
5452
if isinstance(self.value, int):
@@ -68,6 +66,7 @@ def __str__(self) -> str:
6866
L16 = 11, 44100, 1, "L16"
6967
QCELP = 12, 8000, 1, "QCELP"
7068
CN = 13, 8000, 1, "CN"
69+
OPUS = 107, 48000, 2, "opus"
7170
# MPA channel varries, should be defined in the RTP packet.
7271
MPA = 14, 90000, 0, "MPA"
7372
G728 = 15, 8000, 1, "G728"

PySIP/sip_account.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import asyncio
22
from concurrent.futures import ThreadPoolExecutor
3+
from functools import wraps
34
import logging
4-
from typing import List, Literal, Optional
5+
from typing import Callable, List, Literal, Optional
56

67
from .utils.logger import logger
78
from .filters import ConnectionType
@@ -34,6 +35,7 @@ def __init__(
3435
self.__client_task = None
3536
self.__sip_client = None
3637
self.__calls: List[SipCall] = []
38+
self.__pending_callbacks: List[Callable] = []
3739
if self.connection_type == "AUTO":
3840
self.__setup_connection_type()
3941

@@ -97,6 +99,11 @@ async def register(self):
9799
caller_id=self.caller_id or "",
98100
sip_core=self.sip_core,
99101
)
102+
# Register any pending callbacks
103+
for callback in self.__pending_callbacks:
104+
self.__sip_client._register_callback("incoming_call_cb", callback)
105+
self.__pending_callbacks = [] # clear pending callbacks
106+
100107
self.__client_task = asyncio.create_task(self.__sip_client.run())
101108
try:
102109
await asyncio.wait_for(self.__sip_client.registered.wait(), 4)
@@ -139,3 +146,14 @@ def remove_call(self, call: SipCall):
139146
self.__calls.remove(call)
140147
except ValueError:
141148
pass
149+
150+
def on_incoming_call(self, func):
151+
@wraps(func)
152+
async def wrapper(call: SipCall):
153+
return await func(call)
154+
155+
if self.__sip_client:
156+
self.__sip_client._register_callback("incoming_call_cb", wrapper)
157+
else:
158+
self.__pending_callbacks.append(wrapper)
159+
return

0 commit comments

Comments
 (0)