From d437c5ef5cc17d1c434298a405b61205e5d3ba4e Mon Sep 17 00:00:00 2001 From: Thijs Raymakers Date: Wed, 28 May 2025 19:29:38 +0200 Subject: [PATCH] Use pjsua2 lib --- README | 1 + main.py | 74 ++++++++++++++++++++++++++++++++++++++++++------ requirements.txt | 1 - 3 files changed, 66 insertions(+), 10 deletions(-) create mode 100644 README diff --git a/README b/README new file mode 100644 index 0000000..b601587 --- /dev/null +++ b/README @@ -0,0 +1 @@ +Make sure you install pjsua2 from the pjproject submodule. See https://docs.pjsip.org/en/latest/pjsua2/building.html for installation instructions. diff --git a/main.py b/main.py index 0d1cdbe..f4c6c66 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,5 @@ import asyncio +import pjsua2 as pj from markupsafe import escape from unificontrol import UnifiClient from dotenv import load_dotenv @@ -6,7 +7,6 @@ from os import getenv from ssl import get_server_certificate from flask import Flask, request, render_template, Response, send_from_directory import os -from pyVoIP.VoIP import VoIPPhone app = Flask(__name__) app.config['TEMPLATES_AUTO_RELOAD'] = True @@ -15,6 +15,65 @@ global unifi VISITOR_COUNT_FILE = "visitor_count" SIP = None +def phone(hostname, username, password): + class MyAccount(pj.Account): + def __init__(self): + pj.Account.__init__(self) + + def onRegState(self, prm): + print("Registration state:", prm.code) + + class MyCall(pj.Call): + def __init__(self, acc, dest_uri): + pj.Call.__init__(self, acc) + self.makeCall(dest_uri, pj.CallOpParam(True)) + + def onCallState(self, prm): + ci = self.getInfo() + print("Call state:", ci.stateText) + + def onCallMediaState(self, prm): + ci = self.getInfo() + for mi in ci.media: + if mi.type == pj.MediaType.AUDIO and mi.status == pj.MediaStatus.ACTIVE: + aud_med = self.getMedia(mi.index) + aud_med.startTransmit(lib.endpoint.audDevManager().getPlaybackDevMedia()) + lib.endpoint.audDevManager().getCaptureDevMedia().startTransmit(aud_med) + +# Initialize lib + lib = pj.Endpoint() + lib.libCreate() + + ep_cfg = pj.EpConfig() + lib.libInit(ep_cfg) + + transport_cfg = pj.TransportConfig() + transport_cfg.port = 5060 + lib.transportCreate(pj.PJSIP_TRANSPORT_UDP, transport_cfg) + + lib.libStart() + +# Account config + acc_cfg = pj.AccountConfig() + acc_cfg.idUri = f"sip:{username}@{hostname}" + acc_cfg.regConfig.registrarUri = f"sip:{hostname}" + cred = pj.AuthCredInfo("digest", "*", username, 0, password) + acc_cfg.sipConfig.authCreds.append(cred) + + acc = MyAccount() + acc.create(acc_cfg) + +# Make call + call = MyCall(acc, f"sip:3002@{hostname}") + +# Keep the program running + import time + time.sleep(30) + +# Clean up + lib.libDestroy() + + def main(): if not os.path.exists(VISITOR_COUNT_FILE): os.mknod(VISITOR_COUNT_FILE) @@ -27,14 +86,11 @@ def main(): USERNAME = str(getenv("USERNAME")) PASSWORD = str(getenv("PASSWORD")) - global SIP - SIP=VoIPPhone( - str(getenv("SIP_SERVER")), int(getenv("SIP_PORT")), str(getenv("SIP_USERNAME")), str(getenv("SIP_PASSWORD"))) - SIP.start() - import time - for _ in range(1000): - print(SIP.get_status()) - time.sleep(1) + SIP_SERVER = str(getenv("SIP_SERVER")) + SIP_USERNAME = str(getenv("SIP_USERNAME")) + SIP_PASSWORD = str(getenv("SIP_PASSWORD")) + + phone(SIP_SERVER, SIP_USERNAME, SIP_PASSWORD) cert = get_server_certificate((HOSTNAME, 443)) diff --git a/requirements.txt b/requirements.txt index 15cd55b..ffb9ad1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,4 +13,3 @@ requests==2.32.3 unificontrol @ git+https://github.com/ThijsRay/unificontrol@bc595d0b17d38f45d624fe08c144e322484fe298 urllib3==2.4.0 Werkzeug==3.1.3 -pyVoIP==1.6.8