%module(directors="1") pjsua2 // // Suppress few warnings // #pragma SWIG nowarn=312 // 312: nested struct (in types.h, sip_auth.h) // // Header section // %{ #include "pjsua2.hpp" using namespace std; using namespace pj; %} // // STL stuff. // %include "std_string.i" %include "std_vector.i" %include "std_map.i" // Android string handling %include "and_string.i" %template(StringVector) std::vector; %template(IntVector) std::vector; %template(ByteVector) std::vector; %template(StringToStringMap) std::map; #ifdef SWIGPYTHON %feature("director:except") { if( $error != NULL ) { PyObject *ptype, *pvalue, *ptraceback; PyErr_Fetch( &ptype, &pvalue, &ptraceback ); PyErr_Restore( ptype, pvalue, ptraceback ); PyErr_Print(); //Py_Exit(1); } } %typemap(out) void* { $result = PyLong_FromVoidPtr($1); } %apply void* { pj_oshandle_t }; %extend std::vector { void assign_from_bytes(PyObject* obj) { Py_buffer view; if (PyObject_GetBuffer(obj, &view, PyBUF_SIMPLE) != 0) { SWIG_Python_RaiseOrModifyTypeError("expected a bytes-like object (bytes, bytearray, memoryview)"); return; } $self->assign((unsigned char*)view.buf, (unsigned char*)view.buf + view.len); PyBuffer_Release(&view); } void copy_to_bytearray(PyObject* obj) { // Not using return value here, because in SWIG, using return value will cause one more copy Py_buffer view; if (PyObject_GetBuffer(obj, &view, PyBUF_WRITABLE) != 0) { SWIG_Python_RaiseOrModifyTypeError("expected a writable bytes-like object (bytearray, memoryview)"); return; } if (view.len < (Py_ssize_t)$self->size()) { PyBuffer_Release(&view); SWIG_Python_RaiseOrModifyTypeError("bytearray too small"); return; } if ($self->size() > 0) { memcpy(view.buf, $self->data(), $self->size()); } PyBuffer_Release(&view); } } #endif #ifdef SWIGCSHARP %typemap(throws, canthrow=1) pj::Error { SWIG_CSharpSetPendingException(SWIG_CSharpApplicationException, (std::string("C++ pj::Error:\n") + $1.info(true).c_str()).c_str()); return $null; } %typemap(ctype) void* "void *" %typemap(imtype) void* "System.IntPtr" %typemap(cstype) void* "System.IntPtr" %typemap(csin) void* "$csinput" %typemap(in) void* %{ $1 = $input; %} %typemap(out) void* %{ $result = $1; %} %typemap(csout, excode=SWIGEXCODE) void* { System.IntPtr cPtr = $imcall;$excode return cPtr; } %typemap(csvarout, excode=SWIGEXCODE2) void* %{ get { System.IntPtr cPtr = $imcall;$excode return cPtr; } %} #endif #ifdef SWIGJAVA // Allow C++ exceptions to be handled in Java %typemap(throws, throws="java.lang.Exception") pj::Error { jclass excep = jenv->FindClass("java/lang/Exception"); if (excep) jenv->ThrowNew(excep, $1.info(true).c_str()); return $null; } // Force the Error Java class to extend java.lang.Exception %typemap(javabase) pj::Error "java.lang.Exception"; %typemap(javacode) pj::Error %{ // Override getMessage() public String getMessage() { return getTitle(); } // Disable serialization (check ticket #1868) private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException { throw new java.io.NotSerializableException("Check ticket #1868!"); } private void readObject(java.io.ObjectInputStream in) throws java.io.IOException { throw new java.io.NotSerializableException("Check ticket #1868!"); } %} // map "Token"/"void *" as "long" %apply long long { void* }; // map "void **" as "long" %apply long long { void** }; // map "pjmedia_aud_dev_index" as "int" %apply int { pjmedia_aud_dev_index }; // map "unsigned char[20]" as "short array" for SslCertInfo.serialNo %include "arrays_java.i" %apply unsigned char[ANY] { unsigned char[20] }; // Constants from PJSIP libraries %include "enumtypeunsafe.swg" %javaconst(1); %pragma(java) jniclasscode=%{ static { try { System.loadLibrary("openh264"); } catch (UnsatisfiedLinkError e) { System.err.println("Failed to load native library openh264\n" + e); System.out.println("This could be safely ignored if you " + "don't use OpenH264 video codec."); } try { System.loadLibrary("pjsua2"); } catch (UnsatisfiedLinkError e) { System.err.println("Failed to load native library pjsua2\n" + e); } } %} #endif /* SWIGJAVA */ %include "symbols.i" // // Classes that can be extended in the target language // %feature("director") LogWriter; %feature("director") Endpoint; %feature("director") Account; %feature("director") Call; %feature("director") Buddy; %feature("director") FindBuddyMatch; %feature("director") AudioMediaPlayer; %feature("director") AudioMediaPort; %feature("director") AudioMediaAiPort; %feature("director") VideoRecorder; // PendingJob is only used on Python #ifdef SWIGPYTHON %feature("director") PendingJob; #else %ignore pj::PendingJob; %ignore pj::Endpoint::utilAddPendingJob; #endif %newobject pj::AuthChallenge::defer; %nocopyctor pj::AuthChallenge; %copyctor pj::OnCallMediaEventParam; // SWIG can't wrap 'operator=' — silence warning 503 for the copy-assign // operator. (The move ctor/assignment are hidden via #ifndef SWIG in the // header, since SWIG 4.x doesn't reliably match rvalue-ref signatures // in %ignore.) %ignore pj::DeferredResponse::operator=; // // Ignore stuffs in pjsua2 // %ignore fromPj; %ignore toPj; %import "pj/config_site.h" %import "pjsua2/config.hpp" // // Now include the API itself. // %include "pjsua2/types.hpp" %ignore pj::ContainerNode::op; %ignore pj::ContainerNode::data; %ignore container_node_op; %ignore container_node_internal_data; %include "pjsua2/persistent.hpp" %include "pjsua2/siptypes.hpp" %template(SockOptVector) std::vector; %template(SipHeaderVector) std::vector; %template(AuthCredInfoVector) std::vector; %template(SrtpCryptoVector) std::vector; %template(SipMultipartPartVector) std::vector; %template(BuddyVector) std::vector; %template(BuddyVector2) std::vector; %template(AudioMediaVector) std::vector; %template(AudioMediaVector2) std::vector; %template(VideoMediaVector) std::vector; %template(ToneDescVector) std::vector; %template(ToneDigitVector) std::vector; %template(ToneDigitMapVector) std::vector; %template(AudioDevInfoVector) std::vector; %template(AudioDevInfoVector2) std::vector; %template(CodecInfoVector) std::vector; %template(CodecInfoVector2) std::vector; %template(VideoDevInfoVector) std::vector; %template(VideoDevInfoVector2) std::vector; %template(CodecFmtpVector) std::vector; %template(MediaFormatAudioVector) std::vector; %template(MediaFormatVideoVector) std::vector; %template(CallMediaInfoVector) std::vector; %template(RtcpFbCapVector) std::vector; %template(SslCertNameVector) std::vector; #if defined(__ANDROID__) %ignore pj::WindowHandle::display; %ignore pj::WindowHandle::window; #endif /* pj::WindowHandle::setWindow() receives Surface object */ #if defined(SWIGJAVA) && defined(__ANDROID__) %{ #if defined(PJMEDIA_HAS_VIDEO) && PJMEDIA_HAS_VIDEO!=0 # include #else # define ANativeWindow_fromSurface(a,b) NULL #endif %} %typemap(in) jobject surface { $1 = ($input? (jobject)ANativeWindow_fromSurface(jenv, $input): NULL); } %extend pj::WindowHandle { void setWindow(jobject surface) { $self->window = surface; } } #else //%extend pj::WindowHandle { // void setWindow(long long hwnd) { $self->window = (void*)hwnd; } //} #endif %include "pjsua2/media.hpp" %include "pjsua2/presence.hpp" %include "pjsua2/account.hpp" %include "pjsua2/call.hpp" %ignore pj::JsonDocument::allocElement; %ignore pj::JsonDocument::getPool; %include "pjsua2/json.hpp" // Try force Java GC before destroying the lib: // - to avoid late destroy of PJ objects by GC // - to avoid destruction of PJ objects from a non-registered GC thread #ifdef SWIGJAVA %rename(libDestroy_) pj::Endpoint::libDestroy; %typemap(javacode) pj::Endpoint %{ public void libDestroy(long prmFlags) throws java.lang.Exception { Runtime.getRuntime().gc(); libDestroy_(prmFlags); } public void libDestroy() throws java.lang.Exception { Runtime.getRuntime().gc(); libDestroy_(); } %} #endif #ifdef SWIGPYTHON %pythonprepend pj::Endpoint::utilAddPendingJob(PendingJob *job) %{ # print('disowning job') job.__disown__() %} #endif %include "pjsua2/endpoint.hpp"