0

I am using Baresip in my Swift project to implement SIP functionalities:

import Foundation

    enum SipError: Error {
        case libre
        case config
        case stack
        case modules
        case userAgent
        case call
    }

    final class CallController {

        required init?(agent: inout OpaquePointer?) throws {
            guard libre_init() == 0 else { throw SipError.libre }

            // Initialize dynamic modules.
            mod_init()

            // Make configure file.
            if let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first {
                conf_path_set(path)
            }
            guard conf_configure() == 0 else { throw SipError.config }

            // Initialize the SIP stack.
            guard ua_init("SIP", 1, 1, 1, 0) == 0 else { throw SipError.stack }

            // Load modules.
            guard conf_modules() == 0 else { throw SipError.modules }

            let addr = "sip:7003:789@192.168.123.103;transport=udp;answermode=manual"

            // Start user agent.
            guard ua_alloc(&agent, addr) == 0 else { throw SipError.userAgent }

            uag_event_register(ua_event_handler, NULL)

            // Make an outgoing call.
            guard ua_connect(agent, nil, nil, "sip:7002@192.168.123.103", nil, VIDMODE_OFF) == 0 else { throw SipError.call }

            // Start the main loop.
            re_main(nil)
        }

        func close(agent: OpaquePointer) {
            mem_deref(UnsafeMutablePointer(agent))
            ua_close()
            mod_close()

            // Close and check for memory leaks.
            libre_close()
            tmr_debug()
            mem_debug()
        }

    }

To be able to track call events, I need to implement a handler function with this definition:

typedef void()  ua_event_h(struct ua *ua, enum ua_event ev, struct call *call, const char *prm, void *arg)

In C, it is done like this:

static void ua_event_handler(struct ua *ua, enum ua_event ev,
                     struct call *call, const char *prm, void *arg)
{
    //Handle events
    re_printf("ua event: %s\n", uag_event_str(ev));
}

uag_event_register(ua_event_handler, NULL); 

However, I don't know what datatypes I should use in Swift instead of the C datatypes if I want to write the same function in Swift. I would appreciate it if someone could help me understand how I should do it.

HemOdd
  • 697
  • 1
  • 7
  • 23
  • I have implemented the same library, and I get an error "Extra argument in call" in make call line.... I think you have added extra arguments. – SAMIR RATHOD Jul 30 '20 at 14:43

1 Answers1

0
uag_event_register( {(agent, event, call, param, args) in
        let e = String(cString: uag_event_str(event))
        debugPrint("User Agent \(e)")
    }, nil)

Does this answer your question?

Watermamal
  • 357
  • 3
  • 12