Compare commits

...

5 Commits

Author SHA1 Message Date
Riza Sulistyo 6bbdf8b1b9 Remove unused imports 2023-12-06 11:50:08 +07:00
Riza Sulistyo f0af58927c Use reference counting alternative 2023-11-24 09:51:51 +07:00
Riza Sulistyo b50fe8c207 Add samples 2023-11-14 16:42:06 +07:00
Riza Sulistyo 9db2772d9d Prevent auto job deletion 2023-11-08 19:37:11 +07:00
Riza Sulistyo 4058363cc1 Enable PendingJob use in PJSUA2 swig generated wrapper class 2023-11-08 10:47:25 +07:00
5 changed files with 138 additions and 13 deletions

View File

@ -86,6 +86,31 @@ class MyObserver implements MyAppObserver {
public void notifyCallMediaEvent(MyCall call, OnCallMediaEventParam prm) {}
}
class MyJob extends PendingJob
{
@Override
public void execute(boolean is_pending) {
System.out.println("Executing job is_pending:" + is_pending);
}
protected void finalize() {
System.out.println("Job deleted");
}
}
class MyJobHub
{
Endpoint ep;
public MyJobHub(Endpoint inEp) {
ep = inEp;
}
public void setNewJob() {
MyJob job = new MyJob();
ep.utilAddPendingJob(job);
}
}
class MyShutdownHook extends Thread {
Thread thread;
MyShutdownHook(Thread thr) {
@ -99,13 +124,14 @@ class MyShutdownHook extends Thread {
;
}
}
}
}
public class sample {
private static MyApp app = new MyApp();
private static MyObserver observer = new MyObserver();
private static MyAccount account = null;
private static AccountConfig accCfg = null;
private static AccountConfig accCfg = null;
private static MyJobHub jobHub = new MyJobHub(MyApp.ep);
// Snippet code to set native window to output video
/*
@ -135,16 +161,16 @@ public class sample {
account = app.addAcc(accCfg);
accCfg.setIdUri("sip:test@pjsip.org");
AccountSipConfig sipCfg = accCfg.getSipConfig();
AccountSipConfig sipCfg = accCfg.getSipConfig();
AuthCredInfoVector ciVec = sipCfg.getAuthCreds();
ciVec.add(new AuthCredInfo("Digest",
ciVec.add(new AuthCredInfo("Digest",
"*",
"test",
0,
"passwd"));
StringVector proxy = sipCfg.getProxies();
proxy.add("sip:pjsip.org;transport=tcp");
proxy.add("sip:pjsip.org;transport=tcp");
AccountRegConfig regCfg = accCfg.getRegConfig();
regCfg.setRegistrarUri("sip:pjsip.org");
@ -152,11 +178,14 @@ public class sample {
} else {
account = app.accList.get(0);
accCfg = account.cfg;
}
}
try {
account.modify(accCfg);
} catch (Exception e) {}
} catch (Exception e) {}
jobHub.setNewJob();
System.gc();
while (!Thread.currentThread().isInterrupted()) {
// Handle events

View File

@ -121,6 +121,7 @@ using namespace pj;
%feature("director") FindBuddyMatch;
%feature("director") AudioMediaPlayer;
%feature("director") AudioMediaPort;
%feature("director") PendingJob;
//
// STL stuff.
@ -184,6 +185,9 @@ using namespace pj;
%template(RtcpFbCapVector) std::vector<pj::RtcpFbCap>;
%template(SslCertNameVector) std::vector<pj::SslCertName>;
%refobject pj::RefCountObj "$this->addRef();"
%unrefobject pj::RefCountObj "$this->delRef();"
#if defined(__ANDROID__)
%ignore pj::WindowHandle::display;
%ignore pj::WindowHandle::window;

View File

@ -3,6 +3,7 @@ import sys
import time
from collections import deque
import struct
import gc
write=sys.stdout.write
@ -210,6 +211,56 @@ def ua_tonegen_test():
ep.libDestroy()
class TestJob(pj.PendingJob):
def __init__(self, hub):
super().__init__(False)
self.__id__ = randint(0, 100000)
self.__hub__ = hub
def execute(self, is_pending):
print("executing job id: ", self.__id__)
self.__hub__.delJob(self.__id__)
def getId(self):
return self.__id__
def __del__(self):
print("Job deleted")
class MyJob(pj.PendingJob):
def __init__(self):
super().__init__()
def execute(self, is_pending):
print("Executing job, is_pending: ", is_pending)
class MyJobHub() :
def __init__(self, ep):
self.__ep__ = ep
def setNewJob(self):
job = MyJob()
self.__ep__.utilAddPendingJob(job)
def delJob(self, id):
del self.__jobList__[id]
def ua_pending_job_test():
jobs = {}
write("PendingJob test.." + "\r\n")
ep_cfg = pj.EpConfig()
ep = pj.Endpoint()
ep.libCreate()
ep.libInit(ep_cfg)
ep.libStart()
hub = MyJobHub(ep)
hub.setNewJob()
gc.collect()
ep.libDestroy()
#
# main()
#
@ -219,6 +270,7 @@ if __name__ == "__main__":
ua_run_log_test()
ua_run_ua_test()
ua_tonegen_test()
ua_pending_job_test()
sys.exit(0)

View File

@ -1159,8 +1159,35 @@ struct EpConfig : public PersistentObject
};
/* This will add reference counting capability. It is useful to enable
* object destruction based on reference counting.
*/
struct RefCountObj {
/** Return the current reference count. */
int refCount() const { return count; }
/** Add the reference count. */
int addRef() const { return add_ref(); }
/**
* Remove or delete reference count. This might delete the object if
* the reference count is 0.
*/
int delRef() const;
protected:
virtual ~RefCountObj() = 0;
int add_ref() const { return ++count; }
int del_ref() const { return --count; }
private:
mutable int count;
};
/* This represents posted job */
struct PendingJob
struct PendingJob : public RefCountObj
{
/** Perform the job */
virtual void execute(bool is_pending) = 0;

View File

@ -579,6 +579,17 @@ struct PendingLog : public PendingJob
}
};
///////////////////////////////////////////////////////////////////////////////
int RefCountObj::delRef() const
{
if (refCount() == 0 || del_ref() == 0 ) {
delete this;
return 0;
}
return refCount();
}
RefCountObj::~RefCountObj() {}
///////////////////////////////////////////////////////////////////////////////
/*
* Endpoint instance
@ -611,7 +622,8 @@ Endpoint& Endpoint::instance() PJSUA2_THROW(Error)
Endpoint::~Endpoint()
{
while (!pendingJobs.empty()) {
delete pendingJobs.front();
PendingJob *job = pendingJobs.front();
job->delRef();
pendingJobs.pop_front();
}
@ -638,11 +650,11 @@ void Endpoint::utilAddPendingJob(PendingJob *job)
enum {
MAX_PENDING_JOBS = 1024
};
job->addRef();
/* See if we can execute immediately */
if (!mainThreadOnly || pj_thread_this()==mainThread) {
job->execute(false);
delete job;
job->delRef();
return;
}
@ -651,7 +663,8 @@ void Endpoint::utilAddPendingJob(PendingJob *job)
pj_enter_critical_section();
for (unsigned i=0; i<NUMBER_TO_DISCARD; ++i) {
delete pendingJobs.back();
PendingJob *job = pendingJobs.back();
job->delRef();
pendingJobs.pop_back();
}
@ -710,7 +723,7 @@ void Endpoint::performPendingJobs()
if (job) {
job->execute(true);
delete job;
job->delRef();
} else
break;
}