Ticket #556: New object oriented Python abstraction for pjsua API

git-svn-id: https://svn.pjsip.org/repos/pjproject/trunk@2119 74dad513-b988-da41-8d7b-12977e46ad98
This commit is contained in:
Benny Prijono 2008-07-10 22:41:20 +00:00
parent ba736c4c19
commit 9c4611432c
13 changed files with 11685 additions and 28 deletions

View File

@ -237,7 +237,7 @@ Package=<4>
###############################################################################
Project: "py_pjsua"=".\py_pjsua.dsp" - Package Owner=<4>
Project: "python_pjsua"=".\python_pjsua.dsp" - Package Owner=<4>
Package=<5>
{{{
@ -245,33 +245,6 @@ Package=<5>
Package=<4>
{{{
Begin Project Dependency
Project_Dep_Name pjlib
End Project Dependency
Begin Project Dependency
Project_Dep_Name pjlib_util
End Project Dependency
Begin Project Dependency
Project_Dep_Name pjmedia
End Project Dependency
Begin Project Dependency
Project_Dep_Name pjmedia_codec
End Project Dependency
Begin Project Dependency
Project_Dep_Name pjsip_core
End Project Dependency
Begin Project Dependency
Project_Dep_Name pjsip_simple
End Project Dependency
Begin Project Dependency
Project_Dep_Name pjsip_ua
End Project Dependency
Begin Project Dependency
Project_Dep_Name pjsua_lib
End Project Dependency
Begin Project Dependency
Project_Dep_Name pjnath
End Project Dependency
Begin Project Dependency
Project_Dep_Name libgsmcodec
End Project Dependency
@ -290,6 +263,33 @@ Package=<4>
Begin Project Dependency
Project_Dep_Name libsrtp
End Project Dependency
Begin Project Dependency
Project_Dep_Name pjlib
End Project Dependency
Begin Project Dependency
Project_Dep_Name pjlib_util
End Project Dependency
Begin Project Dependency
Project_Dep_Name pjmedia
End Project Dependency
Begin Project Dependency
Project_Dep_Name pjmedia_codec
End Project Dependency
Begin Project Dependency
Project_Dep_Name pjnath
End Project Dependency
Begin Project Dependency
Project_Dep_Name pjsip_core
End Project Dependency
Begin Project Dependency
Project_Dep_Name pjsip_simple
End Project Dependency
Begin Project Dependency
Project_Dep_Name pjsip_ua
End Project Dependency
Begin Project Dependency
Project_Dep_Name pjsua_lib
End Project Dependency
}}}
###############################################################################

View File

@ -0,0 +1,116 @@
# Microsoft Developer Studio Project File - Name="python_pjsua" - Package Owner=<4>
# Microsoft Developer Studio Generated Build File, Format Version 6.00
# ** DO NOT EDIT **
# TARGTYPE "Win32 (x86) Dynamic-Link Library" 0x0102
CFG=python_pjsua - Win32 Debug
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
!MESSAGE use the Export Makefile command and run
!MESSAGE
!MESSAGE NMAKE /f "python_pjsua.mak".
!MESSAGE
!MESSAGE You can specify a configuration when running NMAKE
!MESSAGE by defining the macro CFG on the command line. For example:
!MESSAGE
!MESSAGE NMAKE /f "python_pjsua.mak" CFG="python_pjsua - Win32 Debug"
!MESSAGE
!MESSAGE Possible choices for configuration are:
!MESSAGE
!MESSAGE "python_pjsua - Win32 Release" (based on "Win32 (x86) Dynamic-Link Library")
!MESSAGE "python_pjsua - Win32 Debug" (based on "Win32 (x86) Dynamic-Link Library")
!MESSAGE
# Begin Project
# PROP AllowPerConfigDependencies 0
# PROP Scc_ProjName ""
# PROP Scc_LocalPath ""
CPP=cl.exe
MTL=midl.exe
RSC=rc.exe
!IF "$(CFG)" == "python_pjsua - Win32 Release"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 0
# PROP BASE Output_Dir "Release"
# PROP BASE Intermediate_Dir "Release"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 0
# PROP Output_Dir ".\output\python_pjsua-i386-win32-vc6-release"
# PROP Intermediate_Dir ".\output\python_pjsua-i386-win32-vc6-release"
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /MT /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "PY_PJSUA_EXPORTS" /YX /FD /c
# ADD CPP /nologo /MT /W3 /GX /O2 /I "..\..\pjlib\include" /I "..\..\pjlib-util\include" /I "..\..\pjmedia\include" /I "..\..\pjsip\include" /I "../../pjnath/include" /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "PY_PJSUA_EXPORTS" /FR /YX /FD /c
# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32
# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32
# ADD BASE RSC /l 0x421 /d "NDEBUG"
# ADD RSC /l 0x421 /d "NDEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386
# ADD LINK32 python24.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib dsound.lib dxguid.lib netapi32.lib mswsock.lib ws2_32.lib iphlpapi.lib /nologo /dll /map /machine:I386 /nodefaultlib:"libcmt.lib" /out:"..\lib\_pjsua.pyd" /libpath:"../../pjlib/lib" /libpath:"../../pjlib-util/lib" /libpath:"../../pjmedia/lib" /libpath:"../../pjsip/lib"
# SUBTRACT LINK32 /nodefaultlib
!ELSEIF "$(CFG)" == "python_pjsua - Win32 Debug"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 1
# PROP BASE Output_Dir "Debug"
# PROP BASE Intermediate_Dir "Debug"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 1
# PROP Output_Dir ".\output\python_pjsua-i386-win32-vc6-debug"
# PROP Intermediate_Dir ".\output\python_pjsua-i386-win32-vc6-debug"
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "PY_PJSUA_EXPORTS" /YX /FD /GZ /c
# ADD CPP /nologo /MTd /W4 /Gm /GX /ZI /Od /I "..\..\pjlib\include" /I "..\..\pjlib-util\include" /I "..\..\pjmedia\include" /I "..\..\pjsip\include" /I "../../pjnath/include" /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "PY_PJSUA_EXPORTS" /FR /YX /FD /GZ /c
# ADD BASE MTL /nologo /D "_DEBUG" /mktyplib203 /win32
# ADD MTL /nologo /D "_DEBUG" /mktyplib203 /win32
# ADD BASE RSC /l 0x421 /d "_DEBUG"
# ADD RSC /l 0x421 /d "_DEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /pdbtype:sept
# ADD LINK32 python24_d.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib dsound.lib dxguid.lib netapi32.lib mswsock.lib ws2_32.lib iphlpapi.lib /nologo /dll /debug /machine:I386 /out:"..\lib\_pjsua_d.pyd" /pdbtype:sept /libpath:"../../pjlib/lib" /libpath:"../../pjlib-util/lib" /libpath:"../../pjmedia/lib" /libpath:"../../pjsip/lib" /libpath:"F:\incoming\projects\divusi\Python-2.4\Python-2.4\PCbuild" /libpath:"F:\incoming\projects\divusi\Python-2.4\Python-2.4\PC\VC6"
!ENDIF
# Begin Target
# Name "python_pjsua - Win32 Release"
# Name "python_pjsua - Win32 Debug"
# Begin Group "Source Files"
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
# Begin Source File
SOURCE=..\src\python\_pjsua.c
# End Source File
# Begin Source File
SOURCE=..\src\python\_pjsua.def
# End Source File
# End Group
# Begin Group "Header Files"
# PROP Default_Filter "h;hpp;hxx;hm;inl"
# Begin Source File
SOURCE=..\src\python\_pjsua.h
# End Source File
# End Group
# Begin Group "Resource Files"
# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
# End Group
# End Target
# End Project

View File

@ -0,0 +1,2 @@
This Python module is now deprecated. Please use the new implementation under
pjsip-apps/python directory.

View File

@ -0,0 +1,6 @@
all:
python setup.py install
clean:
python setup.py clean
rm -rf ./build

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,2 @@
EXPORTS
init_pjsua

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,17 @@
include ../../../build.mak
lib_dir:
@for token in `echo $(APP_LDFLAGS)`; do \
echo $$token | grep L | sed 's/-L//'; \
done
inc_dir:
@for token in `echo $(APP_CFLAGS)`; do \
echo $$token | grep I | sed 's/-I//'; \
done
libs:
@for token in `echo $(APP_LDLIBS)`; do \
echo $$token | grep \\-l | sed 's/-l//'; \
done

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,153 @@
# $Id:$
#
# SIP call sample.
#
# Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
#
import sys
import pjsua as pj
LOG_LEVEL=3
current_call = None
# Logging callback
def log_cb(level, str, len):
print str,
# Callback to receive events from account
class MyAccountCallback(pj.AccountCallback):
def __init__(self, account):
pj.AccountCallback.__init__(self, account)
# Notification on incoming call
def on_incoming_call(self, call):
global current_call
if current_call:
call.answer(486, "Busy")
return
print "Incoming call from ", call.info().remote_uri
print "Press 'a' to answer"
current_call = call
call_cb = MyCallCallback(current_call)
current_call.set_callback(call_cb)
current_call.answer(180)
# Callback to receive events from Call
class MyCallCallback(pj.CallCallback):
def __init__(self, call):
pj.CallCallback.__init__(self, call)
# Notification when call state has changed
def on_state(self):
global current_call
print "Call with", self.call.info().remote_uri,
print "is", self.call.info().state_text,
print "last code =", self.call.info().last_code,
print "(" + self.call.info().last_reason + ")"
if self.call.info().state == pj.CallState.DISCONNECTED:
current_call = None
# Notification when call's media state has changed.
def on_media_state(self):
if self.call.info().media_state == pj.MediaState.ACTIVE:
# Connect the call to sound device
call_slot = self.call.info().conf_slot
pj.Lib.instance().conf_connect(call_slot, 0)
pj.Lib.instance().conf_connect(0, call_slot)
print "Media is now active"
else:
print "Media is inactive"
# Function to make call
def make_call(uri):
try:
print "Making call to", uri
call = acc.make_call(uri)
call_cb = MyCallCallback(call)
call.set_callback(call_cb)
return call
except pj.Error, e:
print "Error: " + str(e)
return None
# Create library instance
lib = pj.Lib()
try:
# Init library with default config and some customized
# logging config.
lib.init(log_cfg = pj.LogConfig(level=LOG_LEVEL, callback=log_cb))
# Create UDP transport which listens to any available port
transport = lib.create_transport(pj.TransportType.UDP,
pj.TransportConfig(0))
print "\nListening on", transport.info().host,
print "port", transport.info().port, "\n"
# Start the library
lib.start()
# Create local account
acc = lib.create_account_for_transport(transport)
acc_cb = MyAccountCallback(acc)
acc.set_callback(acc_cb)
# If argument is specified then make call to the URI
if len(sys.argv) > 1:
current_call = make_call(sys.argv[1])
my_sip_uri = "sip:" + transport.info().host + \
":" + str(transport.info().port)
# Menu loop
while True:
print "My SIP URI is", my_sip_uri
print "Menu: m=make call, h=hangup call, a=answer call, q=quit"
input = sys.stdin.readline().rstrip("\r\n")
if input == "m":
if current_call:
print "Already have another call"
continue
print "Enter destination URI to call: ",
input = sys.stdin.readline().rstrip("\r\n")
if input == "":
continue
current_call = make_call(input)
elif input == "h":
if not current_call:
print "There is no call"
continue
current_call.hangup()
elif input == "a":
if not current_call:
print "There is no call"
continue
current_call.answer(200)
elif input == "q":
break
# Shutdown the library
lib.destroy()
lib = None
except pj.Error, e:
print "Exception: " + str(e)
lib.destroy()
lib = None

View File

@ -0,0 +1,113 @@
# $Id:$
#
# Presence and instant messaging
#
# Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
#
import sys
import pjsua as pj
import threading
LOG_LEVEL = 3
def log_cb(level, str, len):
print str,
class MyBuddyCallback(pj.BuddyCallback):
def __init__(self, buddy):
pj.BuddyCallback.__init__(self, buddy)
def on_state(self):
print "Buddy", self.buddy.info().uri, "is",
print self.buddy.info().online_text
def on_pager(self, mime_type, body):
print "Instant message from", self.buddy.info().uri,
print "(", mime_type, "):"
print body
def on_pager_status(self, body, im_id, code, reason):
if code >= 300:
print "Message delivery failed for message",
print body, "to", self.buddy.info().uri, ":", reason
def on_typing(self, is_typing):
if is_typing:
print self.buddy.info().uri, "is typing"
else:
print self.buddy.info().uri, "stops typing"
lib = pj.Lib()
try:
# Init library with default config and some customized
# logging config.
lib.init(log_cfg = pj.LogConfig(level=LOG_LEVEL, callback=log_cb))
# Create UDP transport which listens to any available port
transport = lib.create_transport(pj.TransportType.UDP,
pj.TransportConfig(0))
print "\nListening on", transport.info().host,
print "port", transport.info().port, "\n"
# Start the library
lib.start()
# Create local account
acc = lib.create_account_for_transport(transport)
my_sip_uri = "sip:" + transport.info().host + \
":" + str(transport.info().port)
buddy = None
# Menu loop
while True:
print "My SIP URI is", my_sip_uri
print "Menu: a=add buddy, t=toggle online status, i=send IM, q=quit"
input = sys.stdin.readline().rstrip("\r\n")
if input == "a":
# Add buddy
print "Enter buddy URI: ",
input = sys.stdin.readline().rstrip("\r\n")
if input == "":
continue
buddy = acc.add_buddy(input)
cb = MyBuddyCallback(buddy)
buddy.set_callback(cb)
buddy.subscribe()
elif input == "t":
acc.set_basic_status(not acc.info().online_status)
elif input == "i":
if not buddy:
print "Add buddy first"
continue
buddy.send_typing_ind(True)
print "Type the message: ",
input = sys.stdin.readline().rstrip("\r\n")
if input == "":
buddy.send_typing_ind(False)
continue
buddy.send_pager(input)
elif input == "q":
break
# Shutdown the library
lib.destroy()
lib = None
except pj.Error, e:
print "Exception: " + str(e)
lib.destroy()
lib = None

View File

@ -0,0 +1,56 @@
# $Id:$
#
# SIP account and registration sample. In this sample, the program
# will block to wait until registration is complete
#
# Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
#
import sys
import pjsua as pj
import threading
def log_cb(level, str, len):
print str,
class MyAccountCallback(pj.AccountCallback):
sem = None
def __init__(self, account):
pj.AccountCallback.__init__(self, account)
def wait(self):
self.sem = threading.Semaphore(0)
self.sem.acquire()
def on_reg_state(self):
if self.sem:
if self.account.info().reg_status >= 200:
self.sem.release()
lib = pj.Lib()
try:
lib.init(log_cfg = pj.LogConfig(level=4, callback=log_cb))
lib.create_transport(pj.TransportType.UDP, pj.TransportConfig(5080))
lib.start()
acc = lib.create_account(pj.AccountConfig("pjsip.org", "bennylp", "***"))
acc_cb = MyAccountCallback(acc)
acc.set_callback(acc_cb)
acc_cb.wait()
print "\n"
print "Registration complete, status=", acc.info().reg_status, \
"(" + acc.info().reg_reason + ")"
print "\nPress ENTER to quit"
sys.stdin.readline()
lib.destroy()
lib = None
except pj.Error, e:
print "Exception: " + str(e)
lib.destroy()

View File

@ -0,0 +1,44 @@
from distutils.core import setup, Extension
import os
import sys
# Fill in pj_inc_dirs
pj_inc_dirs = []
f = os.popen("make -f helper.mak inc_dir")
for line in f:
pj_inc_dirs.append(line.rstrip("\r\n"))
f.close()
# Fill in pj_lib_dirs
pj_lib_dirs = []
f = os.popen("make -f helper.mak lib_dir")
for line in f:
pj_lib_dirs.append(line.rstrip("\r\n"))
f.close()
# Fill in pj_libs
pj_libs = []
f = os.popen("make -f helper.mak libs")
for line in f:
pj_libs.append(line.rstrip("\r\n"))
f.close()
# Mac OS X depedencies
if sys.platform == 'darwin':
extra_link_args = ["-framework", "CoreFoundation",
"-framework", "AudioToolbox"]
else:
extra_link_args = []
setup(name="_pjsua", version="0.9",
ext_modules = [
Extension("_pjsua",
["_pjsua.c"],
define_macros=[('PJ_AUTOCONF', '1'),],
include_dirs=pj_inc_dirs,
library_dirs=pj_lib_dirs,
libraries=pj_libs,
extra_link_args=extra_link_args),
])