Asterisk [1], asterisk
Asterisk [1] is an open-source Telephone Application Platform under the GPLv2 protocol. In short, Asterisk is a server application that can initiate a call, receive a call, and customize a call.
1.2.1 Channel Driver
The Channel Driver Interface of asterisk is the most complex and important available interface. The channel API of asteisk provides the abstraction of various communication protocols, so that the various features of asterisk do not have to care about specific communication protocols. This component is mainly responsible for communication in the asterisk channel abstraction and specific communication protocol implementation.
The asterisk Channel Driver Interface is defined as the ast_channel_tech interface. This interface defines some methods that must be implemented by the channel driver. The first method to implement the channel driver is the ast_channel factory method, that is, requester in ast_channel_tech. After an asterisk channel is created, whether the channel is in the incoming or outgoing direction, the ast_channel_tech implementation associated with the channel is responsible for instantiating and initializing the corresponding ast_channel of the channel.
After the ast_channel is created, the structure contains an ast_channel_tech pointer for creating the channel. Of course, there are many other operations that need to be handled according to specific technical methods. Figure 1.2 shows the two channels in asterisk, and figure 1.4 shows the two bridging channels and how the channel technology is implemented.
The most important methods in ast_channel_tech include:
· Requester: Used to request to the channel driver and instantiate an ast_channel object, and perform initialization Based on the channel type.
· Call: the user initiates an outbound call to the terminal indicated by ast_channel.
· Answer: it is called when asterisk decides to respond to the inbound call associated with ast_channel.
· Hangup: it is called when the system determines that the current call should be hung up. The channel driver needs to communicate with the terminal according to certain protocols.
· Indicate: after a call starts, some other events are generated. You need to notify the terminal of these events. For example, if the device is kept, this function will be called.
· Send_digit_begin: This function is called when the terminal device starts to send DTMF buttons to asterisk.
· Send_digit_end: This function is called when the terminal device sends the DTMF key to asterisk.
· Read: When the asterisk core needs to read an ast_frame data frame from the terminal, the read function is called. An ast_frame frame is an abstract structure used in asterisk to encapsulate media (such as audio or video) and signals.
· Write: use this function to send an ast_frame frame to the terminal device. Generally, channel-Driven Data Processing (collection, etc.) and packaging make the data packet suitable for the communication protocol used, and then send the packaged data to the terminal.
· Bridge: The local bridging function in this channel type. As mentioned above, local bridging is a more efficient bridge method provided by the channel driver for two channels of the same type, instead of completing all the signaling streams and media streams through an additional abstraction layer. This is extremely important for providing performance.
After the call ends, the abstract channel processing code in the asterisk core calls the hangup function in ast_channel_tech and destroys the ast_channel object.
1.1.2 channel bridging
A more familiar call scenario is the connection between two phones. In this scenario, there are two telephone terminals connected to the Asterisk system, so there are two channels in this call.
Figure 1.2 two leg calls represent two channels
The two channels in asterisk are displayed. Figure 1.4 is expanded to show the two bridging channels and how the channel technology is implemented.
Figure 1.4 channel technology and channel Abstraction Layer
Before proceeding to the example, let's take a look at the asterisk dialing scheme's syntax for handling the number 1234.
Note that the 1234 number is randomly selected.
After the number is called, three dialing solution applications are called,
Answer the call, play a sound file, and then stop the call.
; Define the rules forwhat happens when someone dials 1234 .;
Exten => 1234,1, Answer ()
Same => n, Playback (demo-congrats)
Same => n, Hangup (). csharpcode,. csharpcode
Pre {font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: # ffffff;/* white-space: pre ;*/}. csharpcode pre {margin: 0em ;}. csharpcode. rem {color: #008000 ;}. csharpcode. kwrd {color: # 0000ff ;}. csharpcode. str {color: #006080 ;}. csharpcode. op {color: # effecc0 ;}. csharpcode. preproc {color: # cc6633 ;}. csharpcode. asp {background-color: # ffff00 ;}. csharpcode. html {color: #800000 ;}. csharpcode. attr {color: # ff0000 ;}. csharpcode. alt {background-color: # f4f4f4; width: 100%; margin: 0em ;}. csharpcode. lnum {color: #606060 ;}
The exten keyword is used to define extension.
On the right side of the exten row, 1234 is the call rule defined for call 1234.
The following 1 is the first operation performed when the number 1234 is called. Answer tells the system to Answer the call. The following two rows start with the same keyword and define the following rules for the extension above. Here is the rule for the next step of 1234.
N is the next operation. The subsequent items indicate the specific actions to be performed in the dialing scheme.
1.1.1 Channel
1.1.2 channel bridging
A more familiar call scenario is the connection between two phones. In this scenario, there are two telephone terminals connected to the Asterisk system, so there are two channels in this call.
Figure 1.2 two leg calls represent two channels
When the asterisk channel is connected together like above, it is called a Channel Bridge. After a Channel Bridge is executed, the two channels are bridging together to transfer media information between the two channels.
All media streams are negotiated through asterisk. Asterisk supports recording, audio operations, and transcoding between different technologies.
You can use the following two methods to bridge the two channels: Generic bridge and local bridge. Generic bridging works normally regardless of the channel technology used. This bridging uses asterisk's abstract channel interface to transmit all the audio and signaling data. This bridging method is the most complex and effective.
Local bridging is a bridge related to the technology used by communication. If the two channels use the same media transmission technology, they can use a more efficient method instead of using the asterisk abstraction layer in a way similar to different technologies.
Determine whether to use generic or local bridging through the comparison between the two channels. If both channels support local bridging, local bridging is used. Otherwise, generic bridging is used. To determine whether two channels support the same local bridging method, you can simply use the c function pointer for comparison. This comparison method is not the most elegant method, but we have not met any situations that cannot meet our needs. Local bridging of channels will be discussed in section 1.2. Figure 1.3 illustrates an example of local bridging.
Figure 3 local bridging
1.1.3 Frame
In the asterisk code, a call communication is completed by frame. A frame is an instance of the data structure ast_frame.
The list of supported frame types in Asterisk is statically defined. Each type of frame is identified by a number-encoded type and a subtype. The include/asterisk/frame. h file in the complete frame Type list is as follows:
· VOICE: these frames carry part of the VOICE stream.
· VIDEO: these frames carry some VIDEO streams.
· MODEM: The data encoding in such frames. For example, T.38 uses an IP network to send faxes. This frame type is mainly used to process faxes. It must be noted that such data frames must be continuous and cannot be interrupted to ensure that the Peer end can correctly decode the data. This is different from AUDIO frames. AUDIO frames can be transcoded using different AUDIO codes. Although AUDIO quality is sacrificed, network bandwidth is reduced.
· CONTROL: This frame includes the call signaling message. These frames are usually used to indicate the call signaling time, including phone connection, suspension, and persistence.
· DTMF_BEGIN: Start of a number. Generally, a user starts to press a DTMF button on the phone. (Dual-Tone Multi-frequency: dual-tonemultifrequency dual-Tone Multi-frequencyDTMF)
· DTMF_END: End of a number. This frame is the DTMF button on the end phone of the caller.
1.2 Asterisk component Abstraction
Asterisk is a highly modular application. Including a core application, which can be built by compiling the main directory of the Asterisk code tree. However, the core of light is usually useless. Core applications mainly Process Module registration, and some code includes how to connect abstract interfaces to complete telephone calls. The specific implementation interface can be loaded to the system module at the runtime.
By default, all modules are placed in the pre-defined module File directory of asterisk. All modules under this directory will be loaded after the main application starts. The reason for this design is to keep it simpler. There is also a configuration file in asterisk, in which you can define the order of loaded modules and loaded modules. This may seem a bit difficult to configure, but you can choose which modules do not need to be loaded. The biggest advantage of this is to reduce the memory usage of the application, and sometimes it will also help improve system security. The best practice is not to load modules that can accept network connections if not needed.
After the module is loaded, it registers the abstract component interface implemented by this module with the main application of asterisk. The interfaces that the module can implement and register with the asterisk core are of various types. Generally, the associated functions are placed in a module.
1.2.1 Channel Driver
The Channel Driver Interface of asterisk is the most complex and important available interface. The channel API of asteisk provides the abstraction of various communication protocols, so that the various features of asterisk do not have to care about specific communication protocols. This component is mainly responsible for communication in the asterisk channel abstraction and specific communication protocol implementation.
The asterisk Channel Driver Interface is defined as the ast_channel_tech interface. This interface defines some methods that must be implemented by the channel driver. The first method to implement the channel driver is the ast_channel factory method, that is, requester in ast_channel_tech. After an asterisk channel is created, whether the channel is in the incoming or outgoing direction, the ast_channel_tech implementation associated with the channel is responsible for instantiating and initializing the corresponding ast_channel of the channel.
After the ast_channel is created, the structure contains an ast_channel_tech pointer for creating the channel. Of course, there are many other operations that need to be handled according to specific technical methods. Figure 1.2 shows the two channels in asterisk, and figure 1.4 shows the two bridging channels and how the channel technology is implemented.
Figure 1.4 channel technology and channel Abstraction Layer
The most important methods in ast_channel_tech include:
· Requester: Used to request to the channel driver and instantiate an ast_channel object, and perform initialization Based on the channel type.
· Call: the user initiates an outbound call to the terminal indicated by ast_channel.
· Answer: it is called when asterisk decides to respond to the inbound call associated with ast_channel.
· Hangup: it is called when the system determines that the current call should be hung up. The channel driver needs to communicate with the terminal according to certain protocols.
· Indicate: after a call starts, some other events are generated. You need to notify the terminal of these events. For example, if the device is kept, this function will be called.
· Send_digit_begin: This function is called when the terminal device starts to send DTMF buttons to asterisk.
· Send_digit_end: This function is called when the terminal device sends the DTMF key to asterisk.
· Read: When the asterisk core needs to read an ast_frame data frame from the terminal, the read function is called. An ast_frame frame is an abstract structure used in asterisk to encapsulate media (such as audio or video) and signals.
· Write: use this function to send an ast_frame frame to the terminal device. Generally, channel-Driven Data Processing (collection, etc.) and packaging make the data packet suitable for the communication protocol used, and then send the packaged data to the terminal.
· Bridge: The local bridging function in this channel type. As mentioned above, local bridging is a more efficient bridge method provided by the channel driver for two channels of the same type, instead of completing all the signaling streams and media streams through an additional abstraction layer. This is extremely important for providing performance.
After the call ends, the abstract channel processing code in the asterisk core calls the hangup function in ast_channel_tech and destroys the ast_channel object.
1.2.2 dialing Application
The asterisk administrator sets the call route through the dialing plan in/etc/asterisk/extensions. conf. The dialing scheme is composed of a series of call routing rules (extension. After a telephone call enters the system, the system uses the called number to find the corresponding extension in the dialing scheme that the call should use. Extension includes a series of dial-up solution applications that can be executed on this channel. The applications used in the dial-up scheme are maintained by the Application Registration mechanism in asterisk. Application Registration is completed when the corresponding module is loaded.
Through the asterisk dialing scheme, multiple applications can be used together to customize the call handling process. Complicated customization that cannot be completed using the provided dialing scheme ,\
1.2.4 encoding and conversion
In the VOIP world, we use a variety of different encodings to encode the media and send the encoded data to the network. There are a lot of codes available for selection, but there will be some sacrifices in media quality, CPU consumption, bandwidth demand. Asterisk supports a variety of compression encodings and can convert the encoding formats when necessary.
When a call is established, Asterisk tries to make the two terminals use the same media encoding format, so that transcoding is not required. However, this is only an ideal situation. Even if there is a common encoding, transcoding is still required. For example, if the asterisk is configured to process the audio data that passes through the system, such as increasing or decreasing the volume. Asterisk can also perform call recording by configuring. If the format of the configured recording file is inconsistent with the call encoding format, encoding is still required.
Note: Coding negotiation
The methods used to determine the encoding used by a media stream are specific to the various technologies used to connect to the asterisk. In some cases, for example, calls over a traditional telephone network do not need to be negotiated. However, in other cases, especially the IP protocol, a negotiation mechanism is required to negotiate the common encoding by describing the terminal capabilities and the preferred encoding.
The following uses the SIP protocol as an example to describe how to negotiate the code after the call to the asterisk.
Note: Coding negotiation
The methods used to determine the encoding used by a media stream are specific to the various technologies used to connect to the asterisk. In some cases, for example, calls over a traditional telephone network do not need to be negotiated. However, in other cases, especially the IP protocol, a negotiation mechanism is required to negotiate the common encoding by describing the terminal capabilities and the preferred encoding.
The following uses the SIP protocol as an example to describe how to negotiate the code after the call to the asterisk.
1. The terminal initiates a call request to asterisk, which contains the encoding format that the terminal wants to use.
2. Asterisk queries the voice encoding priority configured by the Administrator and selects the highest priority encoding. This encoding is not only the encoding of the asterisk priority table, but also supported by the terminal.
The poor processing of Asterisk encoding is complicated coding, especially video encoding. The requirements for coding negotiation have been very complex over the past 10 years. In order to better process new audio encoding and better support for video encoding, we still need to do a lot of work. This is a new priority for the next release of asterisk.
The encoding conversion module provides one or more ast_translator interfaces. The encoding converter has source and target format attributes. It provides a callback function to convert a piece of media information from the source format to the target format. The encoding converter itself does not know the call itself. All you need to know is how to convert the media from one format to another.
For more information about the converter API, see include/asterisk/translate. h and main/translate. c. The implementation of converter abstraction can be found in the codecs directory.