Asterisk - The Open Source Telephony Project  18.5.0
architecture.h
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2009, Digium, Inc.
5  *
6  * Russell Bryant <[email protected]>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18 
19 /*!
20  * \file
21  * \author Russell Bryant <[email protected]>
22  */
23 
24 /*!
25 \page AsteriskArchitecture Asterisk Architecture Overview
26 \author Russell Bryant <[email protected]>
27 
28 <hr>
29 
30 \section ArchTOC Table of Contents
31 
32  -# \ref ArchIntro
33  -# \ref ArchLayout
34  -# \ref ArchInterfaces
35  -# \ref ArchInterfaceCodec
36  -# \ref ArchInterfaceFormat
37  -# \ref ArchInterfaceAPIs
38  -# \ref ArchInterfaceAMI
39  -# \ref ArchInterfaceChannelDrivers
40  -# \ref ArchInterfaceBridge
41  -# \ref ArchInterfaceCDR
42  -# \ref ArchInterfaceCEL
43  -# \ref ArchInterfaceDialplanApps
44  -# \ref ArchInterfaceDialplanFuncs
45  -# \ref ArchInterfaceRTP
46  -# \ref ArchInterfaceTiming
47  -# \ref ArchThreadingModel
48  -# \ref ArchChannelThreads
49  -# \ref ArchMonitorThreads
50  -# \ref ArchServiceThreads
51  -# \ref ArchOtherThreads
52  -# \ref ArchConcepts
53  -# \ref ArchConceptBridging
54  -# \ref ArchCodeFlows
55  -# \ref ArchCodeFlowPlayback
56  -# \ref ArchCodeFlowBridge
57  -# \ref ArchDataStructures
58  -# \ref ArchAstobj2
59  -# \ref ArchLinkedLists
60  -# \ref ArchDLinkedLists
61  -# \ref ArchHeap
62  -# \ref ArchDebugging
63  -# \ref ArchThreadDebugging
64  -# \ref ArchMemoryDebugging
65 
66 <hr>
67 
68 \section ArchIntro Introduction
69 
70 This section of the documentation includes an overview of the Asterisk architecture
71 from a developer's point of view. For detailed API discussion, see the documentation
72 associated with public API header files. This documentation assumes some knowledge
73 of what Asterisk is and how to use it.
74 
75 The intent behind this documentation is to start looking at Asterisk from a high
76 level and progressively dig deeper into the details. It begins with talking about
77 the different types of components that make up Asterisk and eventually will go
78 through interactions between these components in different use cases.
79 
80 Throughout this documentation, many links are also provided as references to more
81 detailed information on related APIs, as well as the related source code to what
82 is being discussed.
83 
84 Feedback and contributions to this documentation are very welcome. Please send your
85 comments to the asterisk-dev mailing list on http://lists.digium.com/.
86 
87 Thank you, and enjoy Asterisk!
88 
89 
90 \section ArchLayout Modular Architecture
91 
92 Asterisk is a highly modularized application. There is a core application that
93 is built from the source in the <code>main/</code> directory. However, it is
94 not very useful by itself.
95 
96 There are many modules that are loaded at runtime. Asterisk modules have names that
97 give an indication as to what functionality they provide, but the name is not special
98 in any technical sense. When Asterisk loads a module, the module registers the
99 functionality that it provides with the Asterisk core.
100 
101  -# Asterisk starts
102  -# Asterisk loads modules
103  -# Modules say "Hey Asterisk! I am a module. I can provide functionality X, Y,
104  and Z. Let me know when you'd like to use my functionality!"
105 
106 
107 \section ArchInterfaces Abstract Interface types
108 
109 There are many types of interfaces that modules can implement and register their
110 implementations of with the Asterisk core. Any module is allowed to register as
111 many of these different interfaces as they would like. Generally, related
112 functionality is grouped into a single module.
113 
114 In this section, the types of interfaces are discussed. Later, there will
115 be discussions about how different components interact in various scenarios.
116 
117 \subsection ArchInterfaceCodec Codec Interpreter
118 
119 An implementation of the codec interpreter interface provides the ability to
120 convert between two codecs. Asterisk currently only has the ability to translate
121 between audio codecs.
122 
123 These modules have no knowledge about phone calls or anything else about why
124 they are being asked to convert audio. They just get audio samples as input
125 in their specified input format, and are expected to provide audio in the
126 specified output format.
127 
128 It is possible to have multiple paths to get from codec A to codec B once many
129 codec implementations are registered. After modules have been loaded, Asterisk
130 builds a translation table with measurements of the performance of each codec
131 translator so that it can always find the best path to get from A to B.
132 
133 Codec modules typically live in the <code>codecs/</code> directory in the
134 source tree.
135 
136 For a list of codec interpreter implementations, see \ref codecs.
137 
138 For additional information on the codec interpreter API, see the interface
139 definition in <code>include/asterisk/translate.h</code>.
140 
141 For core implementation details related to the codec interpreter API, see
142 <code>main/translate.c</code>.
143 
144 \subsection ArchInterfaceFormat File Format Handler
145 
146 An implementation of the file format handler interface provides Asterisk the
147 ability to read and optionally write files. File format handlers may provide
148 access to audio, video, or image files.
149 
150 The interface for a file format handler is rather primitive. A module simply
151 tells the Asterisk core that it can handle files with a given %extension,
152 for example, ".wav". It also says that after reading the file, it will
153 provide audio in the form of codec X. If a file format handler provides the
154 ability to write out files, it also must specify what codec the audio should
155 be in before provided to the file format handler.
156 
157 File format modules typically live in the <code>formats/</code> directory in the
158 source tree.
159 
160 For a list of file format handler implementations, see \ref formats.
161 
162 For additional information on the file format handler API, see the interface
163 definition in <code>include/asterisk/file.h</code>.
164 
165 For core implementation details related to the file format API, see
166 <code>main/file.c</code>.
167 
168 \subsection ArchInterfaceAPIs C API Providers
169 
170 There are some C APIs in Asterisk that are optional. Core APIs are built into
171 the main application and are always available. Optional C APIs are provided
172 by a module and are only available for use when the module is loaded. Some of
173 these API providers also contain their own interfaces that other modules can
174 implement and register.
175 
176 Modules that provide a C API typically live in the <code>res/</code> directory
177 in the source tree.
178 
179 Some examples of modules that provide C APIs (potentially among other things) are:
180  - res_musiconhold.c
181  - res_calendar.c
182  - provides a calendar technology interface.
183  - res_odbc.c
184  - res_ael_share.c
185  - res_crypto.c
186  - res_curl.c
187  - res_xmpp.c
188  - res_monitor.c
189  - res_smdi.c
190  - res_speech.c
191  - provides a speech recognition engine interface.
192 
193 \subsection ArchInterfaceAMI Manager Interface (AMI) Actions
194 
195 The Asterisk manager interface is a socket interface for monitoring and control
196 of Asterisk. It is a core feature built in to the main application. However,
197 modules can register %actions that may be requested by clients.
198 
199 Modules that register manager %actions typically do so as auxiliary functionality
200 to complement whatever main functionality it provides. For example, a module that
201 provides call conferencing services may have a manager action that will return the
202 list of participants in a conference.
203 
204 \subsection ArchInterfaceCLI CLI Commands
205 
206 The Asterisk CLI is a feature implemented in the main application. Modules may
207 register additional CLI commands.
208 
209 \subsection ArchInterfaceChannelDrivers Channel Drivers
210 
211 The Asterisk channel driver interface is the most complex and most important
212 interface available. The Asterisk channel API provides the telephony protocol
213 abstraction which allows all other Asterisk features to work independently of
214 the telephony protocol in use.
215 
216 The specific interface that channel drivers implement is the ast_channel_tech
217 interface. A channel driver must implement functions that perform various
218 call signaling tasks. For example, they must implement a method for initiating
219 a call and hanging up a call. The ast_channel data structure is the abstract
220 channel data structure. Each ast_channel instance has an associated
221 ast_channel_tech which identifies the channel type. An ast_channel instance
222 represents one leg of a call (a connection between Asterisk and an endpoint).
223 
224 Channel drivers typically live in the <code>channels/</code> directory in the
225 source tree.
226 
227 For a list of channel driver implementations, see \ref channel_drivers.
228 
229 For additional information on the channel API, see
230 <code>include/asterisk/channel.h</code>.
231 
232 For additional implementation details regarding the core ast_channel API, see
233 <code>main/channel.c</code>.
234 
235 \subsection ArchInterfaceBridge Bridging Technologies
236 
237 Bridging is the operation which connects two or more channels together. A simple
238 two channel bridge is a normal A to B phone call, while a multi-party bridge would
239 be something like a 3-way call or a full conference call.
240 
241 The bridging API allows modules to register bridging technologies. An implementation
242 of a bridging technology knows how to take two (or optionally more) channels and
243 connect them together. Exactly how this happens is up to the implementation.
244 
245 This interface is used such that the code that needs to pass audio between channels
246 doesn't need to know how it is done. Underneath, the conferencing may be done in
247 the kernel (via DAHDI), via software methods inside of Asterisk, or could be done
248 in hardware in the future if someone implemented a module to do so.
249 
250 At the time of this writing, the bridging API is still relatively new, so it is
251 not used everywhere that bridging operations are performed. The ConfBridge dialplan
252 application is a new conferencing application which has been implemented on top of
253 this bridging API.
254 
255 Bridging technology modules typically live in the <code>bridges/</code> directory
256 in the source tree.
257 
258 For a list of bridge technology implementations, see \ref bridges.
259 
260 For additional information on the bridging API, see
261 \arg <code>include/asterisk/bridge.h</code>
262 \arg <code>include/asterisk/bridge_technology.h</code>
263 \arg <code>include/asterisk/bridge_channel.h</code>
264 \arg <code>include/asterisk/bridge_features.h</code>
265 \arg <code>include/asterisk/bridge_after.h</code>
266 
267 For additional implementation details regarding the core bridging API, see
268 <code>main/bridge.c</code> and <code>main/bridge_channel.c</code>.
269 
270 \subsection ArchInterfaceCDR Call Detail Record (CDR) Handlers
271 
272 The Asterisk core implements functionality for keeping records of calls. These
273 records are built while calls are processed and live in data structures. At the
274 end of the call, these data structures are released. Before the records are thrown
275 away, they are passed in to all of the registered CDR handlers. These handlers may
276 write out the records to a file, post them to a database, etc.
277 
278 CDR modules typically live in the <code>cdr</code> directory in the source tree.
279 
280 For a list of CDR handlers, see \ref cdr_drivers.
281 
282 For additional information on the CDR API, see
283 <code>include/asterisk/cdr.h</code>.
284 
285 For additional implementation details regarding CDR handling, see
286 <code>main/cdr.c</code>.
287 
288 \subsection ArchInterfaceCEL Call Event Logging (CEL) Handlers
289 
290 The Asterisk core includes a generic event system that allows Asterisk components
291 to report events that can be subscribed to by other parts of the system. One of
292 the things built on this event system is Call Event Logging (CEL).
293 
294 CEL is similar to CDR in that they are both for tracking call history. While CDR
295 records are typically have a one record to one call relationship, CEL events are
296 many events to one call. The CEL modules look very similar to CDR modules.
297 
298 CEL modules typically live in the <code>cel/</code> directory in the source tree.
299 
300 For a list of CEL handlers, see cel_drivers.
301 
302 For additional information about the CEL API, see
303 <code>include/asterisk/cel.h</code>.
304 
305 For additional implementation details for the CEL API, see <code>main/cel.c</code>.
306 
307 \subsection ArchInterfaceDialplanApps Dialplan Applications
308 
309 Dialplan applications implement features that interact with calls that can be
310 executed from the Asterisk dialplan. For example, in <code>extensions.conf</code>:
311 
312 <code>exten => 123,1,NoOp()</code>
313 
314 In this case, NoOp is the application. Of course, NoOp doesn't actually do
315 anything.
316 
317 These applications use a %number of APIs available in Asterisk to interact with
318 the channel. One of the most important tasks of an application is to continuously
319 read audio from the channel, and also write audio back to the channel. The details
320 of how this is done is usually hidden behind an API call used to play a file or wait
321 for digits to be pressed by a caller.
322 
323 In addition to interacting with the channel that originally executed the application,
324 dialplan applications sometimes also create additional outbound channels.
325 For example, the Dial() application creates an outbound channel and bridges it to the
326 inbound channel. Further discussion about the functionality of applications will be
327 discussed in detailed use cases.
328 
329 Dialplan applications are typically found in the <code>apps/</code> directory in
330 the source tree.
331 
332 For a list of dialplan applications, see \ref applications.
333 
334 For details on the API used to register an application with the Asterisk core, see
335 <code>include/asterisk/pbx.h</code>.
336 
337 \subsection ArchInterfaceDialplanFuncs Dialplan Functions
338 
339 As the name suggests, dialplan functions, like dialplan applications, are primarily
340 used from the Asterisk dialplan. Functions are used mostly in the same way that
341 variables are used in the dialplan. They provide a read and/or write interface, with
342 optional arguments. While they behave similarly to variables, they storage and
343 retrieval of a value is more complex than a simple variable with a text value.
344 
345 For example, the <code>CHANNEL()</code> dialplan function allows you to access
346 data on the current channel.
347 
348 <code>exten => 123,1,NoOp(This channel has the name: ${CHANNEL(name)})</code>
349 
350 Dialplan functions are typically found in the <code>funcs/</code> directory in
351 the source tree.
352 
353 For a list of dialplan function implementations, see \ref functions.
354 
355 For details on the API used to register a dialplan function with the Asterisk core,
356 see <code>include/asterisk/pbx.h</code>.
357 
358 \subsection ArchInterfaceRTP RTP Engines
359 
360 The Asterisk core provides an API for handling RTP streams. However, the actual
361 handling of these streams is done by modules that implement the RTP engine interface.
362 Implementations of an RTP engine typically live in the <code>res/</code> directory
363 of the source tree, and have a <code>res_rtp_</code> prefix in their name.
364 
365 \subsection ArchInterfaceTiming Timing Interfaces
366 
367 The Asterisk core implements an API that can be used by components that need access
368 to timing services. For example, a timer is used to send parts of an audio file at
369 proper intervals when playing back a %sound file to a caller. The API relies on
370 timing interface implementations to provide a source for reliable timing.
371 
372 Timing interface implementations are typically found in the <code>res/</code>
373 subdirectory of the source tree.
374 
375 For a list of timing interface implementations, see \ref timing_interfaces.
376 
377 For additional information on the timing API, see <code>include/asterisk/timing.h</code>.
378 
379 For additional implementation details for the timing API, see <code>main/timing.c</code>.
380 
381 
382 \section ArchThreadingModel Asterisk Threading Model
383 
384 Asterisk is a very heavily multi threaded application. It uses the POSIX threads API
385 to manage threads and related services such as locking. Almost all of the Asterisk code
386 that interacts with pthreads does so by going through a set of wrappers used for
387 debugging and code reduction.
388 
389 Threads in Asterisk can be classified as one of the following types:
390 
391  - Channel threads (sometimes referred to as PBX threads)
392  - Network Monitor threads
393  - Service connection threads
394  - Other threads
395 
396 \subsection ArchChannelThreads Channel Threads
397 
398 A channel is a fundamental concept in Asterisk. Channels are either inbound
399 or outbound. An inbound channel is created when a call comes in to the Asterisk
400 system. These channels are the ones that execute the Asterisk dialplan. A thread
401 is created for every channel that executes the dialplan. These threads are referred
402 to as a channel thread. They are sometimes also referred to as a PBX thread, since
403 one of the primary tasks of the thread is to execute the Asterisk dialplan for an
404 inbound call.
405 
406 A channel thread starts out by only being responsible for a single Asterisk channel.
407 However, there are cases where a second channel may also live in a channel thread.
408 When an inbound channel executes an application such as <code>Dial()</code>, an
409 outbound channel is created and bridged to the inbound channel once it answers.
410 
411 Dialplan applications always execute in the context of a channel thread. Dialplan
412 functions almost always do, as well. However, it is possible to read and write
413 dialplan functions from an asynchronous interface such as the Asterisk CLI or the
414 manager interface (AMI). However, it is still always the channel thread that is
415 the owner of the ast_channel data structure.
416 
417 \subsection ArchMonitorThreads Network Monitor Threads
418 
419 Network monitor threads exist in almost every major channel driver in Asterisk.
420 They are responsible for monitoring whatever network they are connected to (whether
421 that is an IP network, the PSTN, etc.) and monitor for incoming calls or other types
422 of incoming %requests. They handle the initial connection setup steps such as
423 authentication and dialed %number validation. Finally, once the call setup has been
424 completed, the monitor threads will create an instance of an Asterisk channel
425 (ast_channel), and start a channel thread to handle the call for the rest of its
426 lifetime.
427 
428 \subsection ArchServiceThreads Service Connection Threads
429 
430 There are a %number of TCP based services that use threads, as well. Some examples
431 include SIP and the AMI. In these cases, threads are used to handle each TCP
432 connection.
433 
434 The Asterisk CLI also operates in a similar manner. However, instead of TCP, the
435 Asterisk CLI operates using connections to a UNIX %domain socket.
436 
437 \subsection ArchOtherThreads Other Threads
438 
439 There are other miscellaneous threads throughout the system that perform a specific task.
440 For example, the event API (include/asterisk/event.h) uses a thread internally
441 (main/event.c) to handle asychronous event dispatching. The devicestate API
442 (include/asterisk/devicestate.h) uses a thread internally (main/devicestate.c)
443 to asynchronously process device state changes.
444 
445 
446 \section ArchConcepts Other Architecture Concepts
447 
448 This section covers some other important Asterisk architecture concepts.
449 
450 \subsection ArchConceptBridging Channel Bridging
451 
452 As previously mentioned when discussing the bridging technology interface
453 (\ref ArchInterfaceBridge), bridging is the act of connecting one or more channel
454 together so that they may pass audio between each other. However, it was also
455 mentioned that most of the code in Asterisk that does bridging today does not use
456 this new bridging infrastructure. So, this section discusses the legacy bridging
457 functionality that is used by the <code>Dial()</code> and <code>Queue()</code>
458 applications.
459 
460 When one of these applications decides it would like to bridge two channels together,
461 it does so by executing the ast_channel_bridge() API call. From there, there are
462 two types of bridges that may occur.
463 
464  -# <b>Generic Bridge:</b> A generic bridge (ast_generic_bridge()) is a bridging
465  method that works regardless of what channel technologies are in use. It passes
466  all audio and signaling through the Asterisk abstract channel and frame interfaces
467  so that they can be communicated between channel drivers of any type. While this
468  is the most flexible, it is also the least efficient bridging method due to the
469  levels of abstraction necessary.
470  -# <b>Native Bridge:</b> Channel drivers have the option of implementing their own
471  bridging functionality. Specifically, this means to implement the bridge callback
472  in the ast_channel_tech structure. If two channels of the same type are bridged,
473  a native bridge method is available, and Asterisk does not have a reason to force
474  the call to stay in the core of Asterisk, then the native bridge function will be
475  invoked. This allows channel drivers to take advantage of the fact that the
476  channels are the same type to optimize bridge processing. In the case of a DAHDI
477  channel, this may mean that the channels are bridged natively on hardware. In the
478  case of SIP, this means that Asterisk can direct the audio to flow between the
479  endpoints and only require the signaling to continue to flow through Asterisk.
480 
481 
482 \section ArchCodeFlows Code Flow Examples
483 
484 Now that there has been discussion about the various components that make up Asterisk,
485 this section goes through examples to demonstrate how these components work together
486 to provide useful functionality.
487 
488 \subsection ArchCodeFlowPlayback SIP Call to File Playback
489 
490 This example consists of a call that comes in to Asterisk via the SIP protocol.
491 Asterisk accepts this call, plays back a %sound file to the caller, and then hangs up.
492 
493 Example dialplan:
494 
495 <code>exten => 5551212,1,Answer()</code><br/>
496 <code>exten => 5551212,n,Playback(demo-congrats)</code><br/>
497 <code>exten => 5551212,n,Hangup()</code><br/>
498 
499  -# <b>Call Setup:</b> An incoming SIP INVITE begins this scenario. It is received by
500  the SIP channel driver (chan_sip.c). Specifically, the monitor thread in chan_sip
501  is responsible for handling this incoming request. Further, the monitor thread
502  is responsible for completing any handshake necessary to complete the call setup
503  process.
504  -# <b>Accept Call:</b> Once the SIP channel driver has completed the call setup process,
505  it accepts the call and initiates the call handling process in Asterisk. To do so,
506  it must allocate an instance of an abstract channel (ast_channel) using the
507  ast_channel_alloc() API call. This instance of an ast_channel will be referred to
508  as a SIP channel. The SIP channel driver will take care of SIP specific channel
509  initialization. Once the channel has been created and initialized, a channel thread
510  is created to handle the call (ast_pbx_start()).
511  -# <b>Run the Dialplan:</b>: The main loop that runs in the channel thread is the code
512  responsible for looking for the proper extension and then executing it. This loop
513  lives in ast_pbx_run() in main/pbx.c.
514  -# <b>Answer the Call:</b>: Once the dialplan is being executed, the first application
515  that is executed is <code>Answer()</code>. This application is a built in
516  application that is defined in main/pbx.c. The <code>Answer()</code> application
517  code simply executes the ast_answer() API call. This API call operates on an
518  ast_channel. It handles generic ast_channel hangup processing, as well as executes
519  the answer callback function defined in the associated ast_channel_tech for the
520  active channel. In this case, the sip_answer() function in chan_sip.c will get
521  executed to handle the SIP specific operations required to answer a call.
522  -# <b>Play the File:</b> The next step of the dialplan says to play back a %sound file
523  to the caller. The <code>Playback()</code> application will be executed.
524  The code for this application is in apps/app_playback.c. The code in the application
525  is pretty simple. It does argument handling and uses API calls to play back the
526  file, ast_streamfile(), ast_waitstream(), and ast_stopstream(), which set up file
527  playback, wait for the file to finish playing, and then free up resources. Some
528  of the important operations of these API calls are described in steps here:
529  -# <b>Open a File:</b> The file format API is responsible for opening the %sound file.
530  It will start by looking for a file that is encoded in the same format that the
531  channel is expecting to receive audio in. If that is not possible, it will find
532  another type of file that can be translated into the codec that the channel is
533  expecting. Once a file is found, the appropriate file format interface is invoked
534  to handle reading the file and turning it into internal Asterisk audio frames.
535  -# <b>Set up Translation:</b> If the encoding of the audio data in the file does not
536  match what the channel is expecting, the file API will use the codec translation
537  API to set up a translation path. The translate API will invoke the appropriate
538  codec translation interface(s) to get from the source to the destination format
539  in the most efficient way available.
540  -# <b>Feed Audio to the Caller:</b> The file API will invoke the timer API to know
541  how to send out audio frames from the file in proper intervals. At the same time,
542  Asterisk must also continuously service the incoming audio from the channel since
543  it will continue to arrive in real time. However, in this scenario, it will just
544  get thrown away.
545  -# <b>Hang up the Call:</b> Once the <code>Playback()</code> application has finished,
546  the dialplan execution loop continues to the next step in the dialplan, which is
547  <code>Hangup()</code>. This operates in a very similar manner to <code>Answer()</code>
548  in that it handles channel type agnostic hangup handling, and then calls down into
549  the SIP channel interface to handle SIP specific hangup processing. At this point,
550  even if there were more steps in the dialplan, processing would stop since the channel
551  has been hung up. The channel thread will exit the dialplan processing loop and
552  destroy the ast_channel data structure.
553 
554 \subsection ArchCodeFlowBridge SIP to IAX2 Bridged Call
555 
556 This example consists of a call that comes in to Asterisk via the SIP protocol. Asterisk
557 then makes an outbound call via the IAX2 protocol. When the far end over IAX2 answers,
558 the call is bridged.
559 
560 Example dialplan:
561 
562 <code>exten => 5551212,n,Dial(IAX2/mypeer)</code><br/>
563 
564  -# <b>Call Setup:</b> An incoming SIP INVITE begins this scenario. It is received by
565  the SIP channel driver (chan_sip.c). Specifically, the monitor thread in chan_sip
566  is responsible for handling this incoming request. Further, the monitor thread
567  is responsible for completing any handshake necessary to complete the call setup
568  process.
569  -# <b>Accept Call:</b> Once the SIP channel driver has completed the call setup process,
570  it accepts the call and initiates the call handling process in Asterisk. To do so,
571  it must allocate an instance of an abstract channel (ast_channel) using the
572  ast_channel_alloc() API call. This instance of an ast_channel will be referred to
573  as a SIP channel. The SIP channel driver will take care of SIP specific channel
574  initialization. Once the channel has been created and initialized, a channel thread
575  is created to handle the call (ast_pbx_start()).
576  -# <b>Run the Dialplan:</b>: The main loop that runs in the channel thread is the code
577  responsible for looking for the proper extension and then executing it. This loop
578  lives in ast_pbx_run() in main/pbx.c.
579  -# <b>Execute Dial()</b>: The only step in this dialplan is to execute the
580  <code>Dial()</code> application.
581  -# <b>Create an Outbound Channel:</b> The <code>Dial()</code> application needs to
582  create an outbound ast_channel. It does this by first using the ast_request()
583  API call to request a channel called <code>IAX2/mypeer</code>. This API call
584  is a part of the core channel API (include/asterisk/channel.h). It will find
585  a channel driver of type <code>IAX2</code> and then execute the request callback
586  in the appropriate ast_channel_tech interface. In this case, it is iax2_request()
587  in channels/chan_iax2.c. This asks the IAX2 channel driver to allocate an
588  ast_channel of type IAX2 and initialize it. The <code>Dial()</code> application
589  will then execute the ast_call() API call for this new ast_channel. This will
590  call into the call callback of the ast_channel_tech, iax2_call(), which requests
591  that the IAX2 channel driver initiate the outbound call.
592  -# <b>Wait for Answer:</b> At this point, the Dial() application waits for the
593  outbound channel to answer the call. While it does this, it must continue to
594  service the incoming audio on both the inbound and outbound channels. The loop
595  that does this is very similar to every other channel servicing loop in Asterisk.
596  The core features of a channel servicing loop include ast_waitfor() to wait for
597  frames on a channel, and then ast_read() on a channel once frames are available.
598  -# <b>Handle Answer:</b> Once the far end answers the call, the <code>Dial()</code>
599  application will communicate this back to the inbound SIP channel. It does this
600  by calling the ast_answer() core channel API call.
601  -# <b>Make Channels Compatible:</b> Before the two ends of the call can be connected,
602  Asterisk must make them compatible to talk to each other. Specifically, the two
603  channels may be sending and expecting to receive audio in a different format than
604  the other channel. The API call ast_channel_make_compatible() sets up translation
605  paths for each channel by instantiating codec translators as necessary.
606  -# <b>Bridge the Channels:</b> Now that both the inbound and outbound channels are
607  fully established, they can be connected together. This connection between the
608  two channels so that they can pass audio and signaling back and forth is referred
609  to as a bridge. The API call that handles the bridge is ast_channel_bridge().
610  In this case, the main loop of the bridge is a generic bridge, ast_generic_bridge(),
611  which is the type of bridge that works regardless of the two channel types. A
612  generic bridge will almost always be used if the two channels are not of the same
613  type. The core functionality of a bridge loop is ast_waitfor() on both channels.
614  Then, when frames arrive on a channel, they are read using ast_read(). After reading
615  a frame, they are written to the other channel using ast_write().
616  -# <b>Breaking the Bridge</b>: This bridge will continue until some event occurs that
617  causes the bridge to be broken, and control to be returned back down to the
618  <code>Dial()</code> application. For example, if one side of the call hangs up,
619  the bridge will stop.
620  -# <b>Hanging Up:</b>: After the bridge stops, control will return to the
621  <code>Dial()</code> application. The application owns the outbound channel since
622  that is where it was created. So, the outbound IAX2 channel will be destroyed
623  before <code>Dial()</code> is complete. Destroying the channel is done by using
624  the ast_hangup() API call. The application will return back to the dialplan
625  processing loop. From there, the loop will see that there is nothing else to
626  execute, so it will hangup on the inbound channel as well using the ast_hangup()
627  function. ast_hangup() performs a number of channel type independent hangup
628  tasks, but also executes the hangup callback of ast_channel_tech (sip_hangup()).
629  Finally, the channel thread exits.
630 
631 
632 \section ArchDataStructures Asterisk Data Structures
633 
634 Asterisk provides generic implementations of a number of data structures.
635 
636 \subsection ArchAstobj2 Astobj2
637 
638 Astobj2 stands for the Asterisk Object model, version 2. The API is defined in
639 include/asterisk/astobj2.h. Some internal implementation details for astobj2 can
640 be found in main/astobj2.c. There is a version 1, and it still exists in the
641 source tree. However, it is considered deprecated.
642 
643 Astobj2 provides reference counted object handling. It also provides a container
644 interface for astobj2 objects. The container provided is a hash table.
645 
646 See the astobj2 API for more details about how to use it. Examples can be found
647 all over the code base.
648 
649 \subsection ArchLinkedLists Linked Lists
650 
651 Asterisk provides a set of macros for handling linked lists. They are defined in
652 include/asterisk/linkedlists.h.
653 
654 \subsection ArchDLinkedLists Doubly Linked Lists
655 
656 Asterisk provides a set of macros for handling doubly linked lists, as well. They
657 are defined in include/asterisk/dlinkedlists.h.
658 
659 \subsection ArchHeap Heap
660 
661 Asterisk provides an implementation of the max heap data structure. The API is defined
662 in include/asterisk/heap.h. The internal implementation details can be found in
663 main/heap.c.
664 
665 
666 \section ArchDebugging Asterisk Debugging Tools
667 
668 Asterisk includes a %number of built in debugging tools to help in diagnosing common
669 types of problems.
670 
671 \subsection ArchThreadDebugging Thread Debugging
672 
673 Asterisk keeps track of a list of all active threads on the system. A list of threads
674 can be viewed from the Asterisk CLI by running the command
675 <code>core show threads</code>.
676 
677 Asterisk has a compile time option called <code>DEBUG_THREADS</code>. When this is on,
678 the pthread wrapper API in Asterisk keeps track of additional information related to
679 threads and locks to aid in debugging. In addition to just keeping a list of threads,
680 Asterisk also maintains information about every lock that is currently held by any
681 thread on the system. It also knows when a thread is blocking while attempting to
682 acquire a lock. All of this information is extremely useful when debugging a deadlock.
683 This data can be acquired from the Asterisk CLI by running the
684 <code>core show locks</code> CLI command.
685 
686 The definitions of these wrappers can be found in <code>include/asterisk/lock.h</code>
687 and <code>include/asterisk/utils.h</code>. Most of the implementation details can be
688 found in <code>main/utils.c</code>.
689 
690 \subsection ArchMemoryDebugging Memory debugging
691 
692 Dynamic memory management in Asterisk is handled through a %number of wrappers defined
693 in <code>include/asterisk/utils.h</code>. By default, all of these wrappers use the
694 standard C library malloc(), free(), etc. functions. However, if Asterisk is compiled
695 with the MALLOC_DEBUG option enabled, additional memory debugging is included.
696 
697 The Asterisk memory debugging system provides the following features:
698 
699  - Track all current allocations including their size and the file, function, and line
700  %number where they were initiated.
701  - When releasing memory, do some basic fence checking to see if anything wrote into the
702  few bytes immediately surrounding an allocation.
703  - Get notified when attempting to free invalid memory.
704 
705 A %number of CLI commands are provided to access data on the current set of memory
706 allocations. Those are:
707 
708  - <code>memory show summary</code>
709  - <code>memory show allocations</code>
710 
711 The implementation of this memory debugging system can be found in
712 <code>main/astmm.c</code>.
713 
714 
715 <hr>
716 Return to the \ref ArchTOC
717  */