1

I am planning to create swift SDK using PJSIP. I have created XCPjsua.h file and XCPjsua.c file. I am interacting with XCPjsua.c file using XCPjsua.h header file and I have below methods

int startPjsip(char *sipUser, char* sipDomain);


 /**
* Make VoIP call.
* @param destUri the uri of the receiver, something like "sip:192.168.43.106:5080"; 
*/

    2. void makeCall(char* destUri);

    3. void endCall();

From my .swift class I can import XCPjsua.h and I can call startPjsip(), makeCall(), endCall() methods. Is there a way to send delegate callbacks or notifications to swift class from this XCPjsua.c file.

For ex: if I get Incoming call, XCPjsua.c file will receive the incoming call. From XCPjsua.c if I want notify to swift class that "You have received incoming call" how can I do that?

Vladyslav Zavalykhatko
  • 15,202
  • 8
  • 65
  • 100
Cherry
  • 699
  • 1
  • 7
  • 20
  • 2
    https://stackoverflow.com/a/38334529/2065045 When making the call send a callback, or when initializing register the callback for the events – teixeiras May 30 '17 at 13:15

1 Answers1

1

Since you have control over the code in XCPjsua.[ch], you don't even have to worry about writing a wrapper: you can define the callback type and the function(s) using the callback as you wish, in a "Swifty" way.

Here is a super-simplified example, where the callback is a function that takes no arguments and returns nothing. A Swift callback is provided to C code as a closure. You can make it much fancier (and practical/realistic) and if you run into problems, please let people here know.

In XCPjsua.h, which you can import into your bridging header:

// Callback type
typedef void(*call_received_cb_t)();

// A C function that monitors for incoming calls.  It takes a callback
// as a parameter and will call it when a call comes in.
void monitorIncoming(call_received_cb_t cb);

An implementation in XCPjsua.c:

void monitorIncoming(call_received_cb_t cb) {
    puts("Monitoring for incoming calls...");
    // Received a call!
    cb();
}

Finally, here is some Swift code:

monitorIncoming({ print("Processing an incoming call in Swift!")})
Anatoli P
  • 4,791
  • 1
  • 18
  • 22