Introduction of freeswitch Open Source Communication python Module

  • 2021-12-04 10:23:06
  • OfStack

Directory 1, Overview 2, Environment 3, Install mod_python Module 4, python Script 5, Configure Startup 6, Test

1. Overview

freeswitch Support business development in multiple languages, including C/C + +, java, python, js, lua, Golang and so on.

freeswitch In use python When doing business development, there are two access modes, one is ESL interface, and the other is mod_python Module.

python The ESL interface of the socket Sockets and freeswitch Command interaction, including sending commands, command responses and event callbacks, is similar to adding a third-party module externally to control fs behavior.

The ESL interface section will be described in detail in subsequent chapters.

Today we are going to introduce the interior of fs 的mod_python Language support module, which allows us to use python Script development fs call control flow.

2. Environment

centos: CentOS release 7.0 (Final) or above

freeswitch: v 1.8. 7

GCC: 4.8. 5

3. Install the mod_python module

freeswitch When the source code is installed, it is not installed by default mod_python Module, we need to enter the directory to compile and install.


cd /root/freeswitch-1.8.7/src/mod/languages/mod_python
make install

cd /usr/local/freeswitch/mod
ll -tr
-rwxr-xr-x. 1 root root 753208 9 Month  14 10:41 mod_python.so
-rwxr-xr-x. 1 root root 1360 9 Month  14 10:41 mod_python.la

4. python script

Increase testapi.py Script


vi /usr/local/freeswitch/scripts/testapi.py
import freeswitch
def fsapi(session,stream,env,args):
    stream.write("hello")
    freeswitch.consoleLog("info","test")


Increase testapp.py Script


vi /usr/local/freeswitch/scripts/testapp.py
import freeswitch
def handler(session, args):
    session.answer()
    freeswitch.console_log("info","testCall\n")
    session.streamFile("local_stream://moh")
    freeswitch.msleep(3000)
    session.hangup()

5. Configure startup

Modify freeswitch Module load configuration file


cd /usr/local/freeswitch/conf/autoload_configs
vi modules.conf.xml
<!-- Languages -->
<load module="mod_python"/>


Modify dialplan Dial plan


cd /usr/local/freeswitch/conf/dialplan
vi public.xml
 … 
<include>
<context name="public">
<extension name="test">
<condition>
<action application="python" data="testapp"/>
</condition>
</extension>
 … 


Start freeswitch


cd /usr/local/freeswitch/bin
./freeswitch -nonat
2021-09-14 10:57:06.392800 [NOTICE] mod_python.c:551 Python Framework Loading...
2021-09-14 10:57:06.405965 [CONSOLE] switch_loadable_module.c:1540 Successfully Loaded [mod_python]
2021-09-14 10:57:06.405982 [NOTICE] switch_loadable_module.c:292 Adding Application 'python'
2021-09-14 10:57:06.406012 [NOTICE] switch_loadable_module.c:315 Adding Chat Application 'python'
2021-09-14 10:57:06.406030 [NOTICE] switch_loadable_module.c:338 Adding API Function 'pyrun'
2021-09-14 10:57:06.406046 [NOTICE] switch_loadable_module.c:338 Adding API Function 'python'

6. Test

In freeswitch Enter the command on the command line and use the pytho n calls the API interface


freeswitch@localhost.localdomain> python testapi
2021-09-14 11:13:56.068722 [NOTICE] mod_python.c:212 Invoking py module: testapi
2021-09-14 11:13:56.088701 [INFO] switch_cpp.cpp:1443 test
hello


In the log print, we can see that mod_python The module called the testapi Script, and printed " test "And" hello ".

Precautions, python Invoke command, python The script suffix ". py" should be removed.

Through other sip server Send the call request to this machine and view the log:


2021-09-14 11:24:40.988720 [NOTICE] switch_channel.c:1114 New Channel sofia/external/10011@192.168.0.110 [73b09c9b-6a62-4372-839b-4c076af7dfc2]
2021-09-14 11:24:40.988720 [INFO] mod_dialplan_xml.c:637 Processing 10011 <10011>->10012 in context public
2021-09-14 11:24:40.988720 [NOTICE] mod_python.c:212 Invoking py module: testapp
2021-09-14 11:24:40.988720 [NOTICE] sofia_media.c:92 Pre-Answer sofia/external/10011@192.168.0.110!
2021-09-14 11:24:40.988720 [NOTICE] switch_cpp.cpp:685 Channel [sofia/external/10011@192.168.0.110] has been answered
2021-09-14 11:24:40.988720 [INFO] switch_cpp.cpp:1443 testCall
2021-09-14 11:24:40.988720 [WARNING] mod_local_stream.c:870 Unknown source moh, trying 'default'
2021-09-14 11:24:40.988720 [ERR] mod_local_stream.c:878 Unknown source default
2021-09-14 11:24:43.988724 [NOTICE] switch_cpp.cpp:733 Hangup sofia/external/10011@192.168.0.110 [CS_EXECUTE] [NORMAL_CLEARING]
2021-09-14 11:24:44.008687 [NOTICE] switch_core_session.c:1744 Session 2 (sofia/external/10011@192.168.0.110) Ended
2021-09-14 11:24:44.008687 [NOTICE] switch_core_session.c:1748 Close Channel sofia/external/10011@192.168.0.110 [CS_DESTROY]


In the log print, we can see that during the execution of the dialplan dial plan, the mod_python Called " testapp ", testapp.py This call is answered in the script and the log is printed. " testcall "And hang up after 3 seconds.

Summary:

freeswitch When doing business development, it supports multi-language access, which is very convenient. Users can choose access methods and languages according to their own skill stack.

However, there must be differences in call performance between different languages, which requires users to test and evaluate the differences in actual use.


Related articles: