Location>code7788 >text

Wrapping custom softphone sdk with pjsip

Popularity:847 ℃/2024-12-18 01:17:27

Environment: window10_x64 & vs2022
pjsip version: 2.14.1
Python version: 3.9.13

Recently, there are softphone sdk development needs on windows environment, need to develop dynamic libraries to the upper application call, today finishing the use of pjsip package simple custom softphone sdk notes, and provide related resources to download.

I will expand on the following:

  • Functional Description
  • interface design
  • interface implementation
  • Sample interface calls (python and c#)
  • Supporting Resources Download

For pjsip compilation and usage, see this article:

/MikeZhang/p/18542615/pjsip-vs2022

I. Functional description

Library Name:
Function:
Windows 10 environment, package pjlib library to upper layer application in order to realize softphone function based on sip protocol.

The overall structure is as follows:

Realization Principle:
1) Package sdk based on pjsip;
2) Encapsulate basic operations and callback functions;

The full code is available from the following sources:

Get it by replying 20241217 after following WeChat (chatting about blogging, you can scan the code at the end of the article).

II. Interface design

1、init

functionality

Initialization interface for initializing dll libraries.

interface statement

int init(int sock_type, int local_port);

join

sock_type : soket type, set as follows
1 => udp
2 => tcp (follow-up support)
3 => tls (follow-up support)

local_port : Local listening port, set to 0 for random port.

return value

0: Success
-1: Failure

2、set_incoming_cb

functionality
Set the inbound callback function to handle inbound requests.

interface statement

int set_incoming_cb(pCallBack cb);

join

cb : a callback function with the following format

int(*pCallBack)(int, int);

return value

0: Success
-1: Failure

3、acc_reg

functionality
Account registration operation that provides account information to register to a specified server.

interface statement

int acc_reg(char* sip_domain, char* sip_user, char* sip_passwd,char* realm);

join

sip_domain : register server

sip_user : user name

sip_passwd : Password

realm : registration domain

return value

account id : Success
-1: Failure

4、acc_unreg

functionality
The account logout operation logs out the specified account.

interface statement

int acc_unreg(int acc_id);

join

acc_id : account id

return value

call id : Success
-1: Failure

5、acc_make_call

functionality
Outbound call operation to make an outbound call using the specified account.

interface statement

int acc_make_call(int acc_id, const char* dst_num);

join

acc_id : account id
dst_num : Called number

return value

call id : Success
-1: Failure

6、acc_answer_call

functionality

Hang up operation to hang up the specified call.

interface statement

int acc_answer_call(int call_id);

join

call_id : The id of the call to be answered.

return value

0: Success
-1: Failure

7、acc_hangup_call

functionality
Hang up operation to hang up the specified call.

interface statement

int acc_hangup_call(int call_id);

join

call_id : The id value of the call to be hung.

return value

0: Success
-1: Failure

8、destory

functionality
Recycling pjsua resources.

interface statement

int destory();

join

not have

return value

0: Success
-1: Failure

III. Interface implementation

Use vs2022 for dll wrapping, with configuration type set to dll type.

1. Preparation of header documents;

The full content is available from the following sources:

Get it by replying 20241217 after following WeChat (chatting about blogging, you can scan the code at the end of the article).
2、Write cpp file

The full content is available from the following sources:

Get it by replying 20241217 after following WeChat (chatting about blogging, you can scan the code at the end of the article).

3, compiled into a dynamic library

Right click on the sipClient project and select "Generate" to compile.

The packaged vs2022 project files and compiled dll files can be obtained from the following sources:

Get it by replying 20241217 after following WeChat (chatting about blogging, you can scan the code at the end of the article).

IV. Sample code (python)

Here is an example of how to call a dll library in python.

Note that the dll here is still not the same as the python version pjsua, there is no requirement for the python version.

For compiling the python version of pjsua, see the following article:

/MikeZhang/p/

/MikeZhang/p/

1. Incoming call handling

Feature Description:
1) Register for an account;
2) Incoming calls are answered automatically;
3) Wait for a specific amount of time before performing a hang-up operation;
4) Hang-up operation;
5) Logout operation;

The sample code is as follows ():

import time
from ctypes import *

pDll = CDLL("./")

# typedef int (*Func)(int, int);
cbType = CFUNCTYPE(c_int, c_int, c_int)

def incoming_callback(acc_id, call_id):
    print("python callback called >>>>>>>>>>>>>>>>")
    print("acc_id : {} , call_id : {}".format(acc_id, call_id))
    pDll.acc_answer_call(call_id)
    (30)
    pDll.acc_hangup_call(call_id)
    return 0

(1,0)
incoming_cb = cbType(incoming_callback)
pDll.set_incoming_cb(incoming_cb)

reg_host=b"192.168.137.100:5060"

domain=create_string_buffer(reg_host)
user=create_string_buffer(b"1002")
passwd=create_string_buffer(b"0000")
realm=create_string_buffer(b"*")

acc_id = pDll.acc_reg(domain,user,passwd,realm)

print("acc_id : ",acc_id)

(1)
print("wait call")
input("#############################\n")

pDll.acc_unreg(acc_id)

(1)
()

The registration effect is as follows:

The auto-answer effect is as follows:

2、Manual outbound call

Feature Description:
1) Register for an account;
2) Initiate outbound call requests;
3) Hang-up operation;
4) Logout operation;

The sample code is as follows ():

import ctypes,time

pDll = ("./")

(1,0)

reg_host=b"192.168.137.100:5060"

domain=ctypes.create_string_buffer(reg_host)
user=ctypes.create_string_buffer(b"1002")
passwd=ctypes.create_string_buffer(b"0000")
realm=ctypes.create_string_buffer(b"*")

acc_id = pDll.acc_reg(domain,user,passwd,realm)

print("acc_id : ",acc_id)

input("#############################")

#dst_uri = ctypes.create_string_buffer(b"sip:1000@" + reg_host)
dst_num = ctypes.create_string_buffer(b"1000")
callid = pDll.acc_make_call(acc_id,dst_num)

input("#############################")

pDll.acc_hangup_call(callid)

input("#############################")

pDll.acc_unreg(acc_id)

(1)
()

The call effect is as follows:

The reception effect is as follows:

Runnable python scripts and dll files are available from the sources provided at the end of the article.

V. Sample code (c#)

Here is a reference to the python sample code in c# to show how to call the sipClient library.

1. Incoming call handling

Feature Description:
1) Register for an account;
2) Incoming calls are answered automatically;
3) Wait for a specific amount of time before performing a hang-up operation;
4) Hang-up operation;
5) Logout operation;

The sample code is as follows (csAutoAnswer1):

The full content is available from can be accessed from the sources provided at the end of the article.

The running effect is as follows:

Corresponding engineering documents () are available from the sources provided at the end of the text.

2、Manual outbound call

Feature Description:
1) Register for an account;
2) Initiate outbound call requests;
3) Hang-up operation;
4) Logout operation;

The sample code is as follows (csManualCallout1):

The full content is available from can be accessed from the sources provided at the end of the article.

The outbound call effect is as follows:

Corresponding engineering documents () are available from the sources provided at the end of the text.

VI. Downloading of resources

This article covers the source code and related documentation, which can be obtained from the following sources:

Get it by replying 20241217 after following WeChat (chatting about blogging, you can scan the code at the end of the article).