Skip to content

Commit

Permalink
Enable PendingJob use on Python (#3817)
Browse files Browse the repository at this point in the history
  • Loading branch information
sauwming authored Jan 3, 2024
1 parent c6fd432 commit 86e22f4
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pjsip-apps/src/swig/pjsua2.i
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ using namespace pj;
%feature("director") FindBuddyMatch;
%feature("director") AudioMediaPlayer;
%feature("director") AudioMediaPort;
// PendingJob is only used on Python
#ifdef SWIGPYTHON
%feature("director") PendingJob;
#else
%ignore pj::PendingJob;
%ignore pj::Endpoint::utilAddPendingJob;
#endif

//
// STL stuff.
Expand Down Expand Up @@ -237,4 +244,11 @@ using namespace pj;
%}
#endif

#ifdef SWIGPYTHON
%pythonprepend pj::Endpoint::utilAddPendingJob(PendingJob *job) %{
# print('disowning job')
job.__disown__()
%}
#endif

%include "pjsua2/endpoint.hpp"
67 changes: 67 additions & 0 deletions pjsip-apps/src/swig/python/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import time
from collections import deque
import struct
import gc
from random import randint
import threading

write=sys.stdout.write

Expand Down Expand Up @@ -210,6 +213,69 @@ def ua_tonegen_test():

ep.libDestroy()

class RandomIntVal():
def __init__(self):
self.value = randint(0, 100000)

class TestJob(pj.PendingJob):
def __init__(self):
super().__init__()
self.val = RandomIntVal()
print("Job created id:", id(self), "value:", self.val.value)

def execute(self, is_pending):
print("Executing job value:", self.val.value, is_pending)

def __del__(self):
print("Job deleted id:", id(self), "value:", self.val.value)

def add_new_job1(ep):
print("Creating job 1")
job = TestJob()
print("Adding job 1")
ep.utilAddPendingJob(job)

# Function to be executed in a separate thread
def add_new_job2(ep):
ep.libRegisterThread("thread")
print("Creating job 2")
job = TestJob()
print("Adding job 2")
ep.utilAddPendingJob(job)

def ua_pending_job_test():
write("PendingJob test.." + "\r\n")
ep_cfg = pj.EpConfig()
ep_cfg.uaConfig.threadCnt = 0
ep_cfg.uaConfig.mainThreadOnly = True

ep = pj.Endpoint()
ep.libCreate()
ep.libInit(ep_cfg)
ep.libStart()

# test 1
# adding job from a separate function
add_new_job1(ep)

# test 2
# adding job from a different thread
my_thread = threading.Thread(target=add_new_job2, args=(ep,))
my_thread.start()

time.sleep(1)
print("Collecting gc 1")
gc.collect()

print("Handling events")
for _ in range(100):
ep.libHandleEvents(10)

print("Collecting gc 2")
gc.collect()

ep.libDestroy()

#
# main()
#
Expand All @@ -219,6 +285,7 @@ def ua_tonegen_test():
ua_run_log_test()
ua_run_ua_test()
ua_tonegen_test()
ua_pending_job_test()
sys.exit(0)


0 comments on commit 86e22f4

Please sign in to comment.