Use pjsua2 lib

This commit is contained in:
Thijs Raymakers 2025-05-28 19:29:38 +02:00
parent a6ae7c4488
commit d437c5ef5c
No known key found for this signature in database
3 changed files with 66 additions and 10 deletions

1
README Normal file
View file

@ -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.

74
main.py
View file

@ -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))

View file

@ -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