Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Add reference to library generator
Wiki Markup
This page is under development; expect missing and incomplete information. Still, feel free to discuss on [asterisk-dev|].


h1. Project Overview

While Asterisk has a number of interfaces one could use for building telephony applications, they suffer from several significant problems.

* Channels identifiers are not stable. Some operations (like [masquerades|AST:Asterisk 11 ManagerEvent_Masquerade]) will change the id out from under you.
* The protocols (AMI and AGI) are non-standard and poorly documented, making them difficult to work with.
* AMI's message format is restricted to simple name/value pairs. Commands that need to pass back lists or structured data are very hackish.
* AMI event filtering is very course grained, and established in configuration instead of at runtime. This has lead to some creative solutions to dealing with the flood of events (most of which are not of interest).
* AGI is a synchronous interface, which really hinders making truly interactive applications.
** While AsyncAGI attempts to address this issue, it is an asynchronous wrapper around a synchronous implementation. Commands queue up after one another, and are not interruptible, leading to many of the same problems with AGI or FastAGI.
* Internally, AMI events are formatted into strings at the time they are created. This makes it very difficult to do anything with the events, except send them out AMI connections. This also makes using different protocol formats difficult.
* Different interfaces tend to implement the same logical command in different ways.

We're going to start with fixing one of the most glaring problems with the current APIs: [add a stable identifier to channels|], and use that identifier consistently throughout AMI. This solves a big problem for current AMI+AGI based applications.

Unfortunately, solving some of the larger problems would be very intrusive with the current API's, and introduce a host of breaking changes. Some are so fundamental, we would essentially be rewriting the interface. That's not acceptable; the current API's aren't going anywhere anytime soon.

This leads us down the path of adding a new interface to Asterisk. We've got some time to put some though into this, so let's do it right. Since things are easier to talk about when they have a name, the new API work has the working title [Stasis|#bad-name].

We want the API to be familiar and approachable to developers. We don't want to force them to use C. In fact, we don't want to force them to use any particular programming language; the API should be accessible from the language and platform of their choice.

The API should be relatively high level, and not get stuck down in the details of what's happening inside of Asterisk. Asterisk may go through all sorts of shenanigans to do what it needs to do, but the API should provide a nice, clean abstraction.

In building out the new API, we don't want to repeat past mistakes by re-implement the same functionality several times in slightly different ways. There should be a single core implementation, that all of the API's use. This would improve consistency between the API's and reduce code duplication.

To accomplish this, Stasis will be a set of new modules for providing control and management interfaces into Asterisk. While Stasis will initially be focused on third party call control and monitoring, it should be extensible enough to provide configuration and provisioning API's in the future.

Internally to Asterisk, {{stasis-core}} will provide a message bus for interfacing with Asterisk objects. The {{stasis-http}} component will be built upon this message bus to expose a RESTful API, also utilizing WebSockets for asynchronous communication to the external applications.

The split between {{stasis-core}} and {{stasis-http}} should allow for other protocol bindings to be added in the future. This could even go so far as replacing the existing API's with Stasis implementations.

The selection of HTTP as the first binding for Stasis allows for very broad appeal, ease of use, and simplicity for writing client libraries to make it easier to write applications to the API.

h1. Requirements and Specification

h2. Stasis Requirements

* *Asynchronous Everything* \- Most applications need to be able to interrupt activities, and receive events as they happen. Blocking operations are the devil.
* *Version Stability* \- This is important. Like, really important. The current mechanisms may change dramatically between releases, which causes developers/integrators/etc. to keep running on older versions of Asterisk than anyone would like.
* *Interoperability* \- Stasis must work in a variety of environments, detailed below.
** *Programming Language* \- Stasis applications should be able to be easily written in a variety of languages.
* *Security* \- Reasonable security measures should be taken.
** Encryption: Since the API should not be accessible on a public network, encryption is not high in the priority list.
*** Even if connections are plain text, reasonable precautions should be taken with sensitive information (e.g. use challenge handshakes for authentication instead of passing plaintext passwords over the wire).
*** If a binding protocol (say, HTTP) supports encryption (say, HTTPS), then it should be supported for Stasis as well.
Not needed immediately, and way down on the priority list.
** Authentication: Client only authentication is sufficient. Should not be any more complicated than passwords/pre-shared secrets.
** Authorization: See [below|#Stasis Authorization Requirements].

h3. Authorization Requirements

Unfortunately, authorization is a tricky subject.

There are many different schemes for implementing authorization, and we've learned that if you're not careful you can end up with a scheme that doesn't provide the value that you hoped for, and is more costly than you expected. And this cost shows up in the complexity of both the implementation and the API.

It's also important to understand who you are authorizing. In the case of the API, we are authorizing applications for API access; not (necessarily) the end users of the phone system. It's somewhat similar to the three tier client/application/database relationship. The database authenticates the application, and determines what data the application can access. The application authenticates the client, and determines what subset of that data it exposes to the client. It's not a perfect comparison, but you get the idea.

Possible use cases to consider:

* *Host multiple companies in one PBX* \- "which channels are one particular user (or group) allowed to follow, manipulate and originate".
* *Read only applications* \- A monitoring application should not be able to affect the state of the system.

h2. Internal Improvements

* *Stable channel identifier* \- this is necessary for a reasonable Stasis API. Since so much of the world still depends on AMI, it should be updated to allow the stable id to be used in place of the current channel id
* *AMI Event Structure* \- AMI events should be generated into a key/value object pair instead of the {{printf}}\-style string formatting currently used. This would allow Stasis to reuse the existing events.
* *Improved implementation consistency* \- While not something we would address in the initial Stasis work, existing disparate implementations could be reworked to use a single, consistent {{stasis-core}} implementation.
* *Fix AMI Bridge Events*
** Current event precludes multi-party bridges (Only has {{Channel1}} and {{Channel2}})
** Some events in the system result in spurious Bridge events (such as [DTMF|])

h2. Use Cases

Note that at this point this it the list of _candidate_ use cases. Which ones we get to, and in what priority, have not been determined yet.

h3. Summary Level

Highest level use cases; more or less applications that could be build on top of Stasis.

* *Standard two party call* \- Very standard use case for Asterisk.
* *Conference* \- Multi-participant calls, in which media from one endpoint may be sent to two or more endpoints.
** Some participants may control the conference via DTMF key presses
** Some participants may be muted
** Indicate who is speaking
* *IVR* \- Interactive Voice Response.
** Plays media to caller
** Detects DTMF key presses from caller
** Record media from caller
* *Queue* \- Call dispatch queue.
** Get presence information from queue agents
** Originate calls to agents as needed
** When agent answers, bridge to most appropriate call from queue
** May add supervisor to agent/caller conversation
* *Voicemail*
** Record audio from caller
** Playback recorded audio
** Detect DTMF for media control (fast forward, skip, delete)

h3. User Level

* *Answer* \- An application may answer a ringing channel.
* *Hangup/Reject* \- An application may hangup an active channel, or reject a ringing channel.
* *Return to dialplan* \- An application may send a channel back to the dialplan to continue processing.
* *Originate* \- An application may originate a new channel.
* *DTMF Detection* \- DTMF, and other channel events.
* *Bridge* \- Two or more channels may be bridged together, so that media from any channel may be mixed and sent to the others
** *Speaker Events* \- Events indicating which channel(s) on the bridge are speaking.
* *Play* \- An application may specify media to be played on a channel.
* *Presence* \- An application may query the presence state of endpoints, and subscribe to presence updates.
* *Record* \- An application may record the media from a channel/bridge.
* *BLF* \- The application may light up the BLF on an endpoint.

h3. Sub-function Level

* *Media Control* \- During the playback of media, the application may issue fine-grained media control commands. (fast forward, pause, stop, etc.)
* *Mute participant* \- Within a bridge, an application may (un)mute individual channels, controlling which media streams are mixed and sent to other participants.

h2. Guidelines

h3. PBX vs. Toolkit

From the README in trunk: "Asterisk is an Open Source PBX and telephony toolkit." However, whether or not we are PBX-centric has implications for the API.

Currently, Asterisk leans more toward being a toolkit than a PBX. There is a very loose coupling between extensions and endpoints, as is typically defined by dialplan code in the extensions.conf file. There is no concept of 'inside' versus 'outside', unless you put it in the dialplan yourself. There is no standard definition of a 'call', or a 'user'. All of these vary depending upon your application, and being able to be applied to a variety of applications is what has made Asterisk so successful.

However, the primary application Asterisk is applied to is being a PBX. It is important that developers writing PBX applications aren't bogged down with general telephony toolkit details.

So while the PBX use cases are important, they should not undermine the general purpose toolkit use cases. Largely, this will influence default values and conventions of the API.

h3. Convention over Configuration

Continuing on with the theme of PBX vs. Toolkit, the API should adopt an approach of convention over configuration: reasonable defaults should be used wherever possible. Configuration should be possible, allowing users to specify their own values in place of these defaults.

h2. Configuration

h3. stasis-http.conf

h4. \[general\]

|| Parameter || Description || Type || Default Value ||
| enabled | Turns Stasis HTTP binding on or off \\
HTTP server must be enabled in http.conf for this to take effect | Boolean | True |
| use_manager_auth | Share authentication with AMI over HTTP. | Boolean | False |

h4. \[username\]

|| Parameter || Description || Type || Default Value ||
| password | Password for username | String | n/a |
| password_crypt | Encrypted password for username \\
Cannot use digest authentication | String | n/a |

h3. RealTime schemas

h2. APIs

h3. Applications

*Stasis* \- direct a call to a Stasis application.

* *name* \- Name of the application to direct the call to.


As [detailed below|#res_stasis_http], Stasis will expose a RESTful HTTP API for third party call control. This API should be documented using [Swagger|], which allows for not only the generation of usable, interactive documentation, but also allows for the generation of server stubs, reducing a lot of the tediousness required in implementing a web application in C.

Message formats will initially be in JSON, but care will be taken with message design so that adding support for XML will be straightforward.

* {{/asterisk}}
** {{GET /asterisk/info}} \- Gets Asterisk system information
* {{/endpoints}}
** {{GET /endpoints}} \- List available endpoints
** {{GET /endpoints\{endpointId\} -}} Details for an endpoint
* {{/channels}}
** {{GET /channels}} \- List active channels
** {{POST /channels}} \- Create a new channel (i.e. originate)
** {{GET /channels/\{channelId\} -}} Channel details
** {{DELETE /channels/\{channelId\} -}} Delete (i.e. hangup) a channel.
** {{POST /channels/\{channelId}/reject}} \- Reject a channel
** {{POST /channels/\{channelId}/answer}} \- Answer a channel
** {{POST /channels/\{channelId}/hangup}} \- Hangup a channel
** {{POST /channels/\{channelId}/mute}} \- Mute a channel
** {{POST /channels/\{channelId}/unmute}} \- Unmute a channel
** {{POST /channels/\{channelId}/record}} \- Start a recording
** {{POST /channels/\{channelId}/dial}} \- Originate a new channel and bridge it to this one.
* {{/bridges}}
** {{GET /bridges}} \- List active bridges
** {{POST /bridges}} \- Create a new bridge
** {{GET /bridges/\{bridgeId\} -}} Get bridge details
** {{DELETE /bridges/\{bridgeId\} -}} Delete bridge
** {{POST /bridges/\{bridgeId\}/add-channel}} \- Add a channel to a bridge
** {{POST /bridges/\{bridgeId\}/remove-channel}} \- Remove a channel from a bridge
** {{POST /bridges/\{bridgeId\}/record}} \- Start a recording
* {{/recordings}}
** {{GET /recordings}} \- List recordings
** {{GET /recordings/\{recordingId\} -}} Get recording details
** {{DELETE /recordings/\{recordingId\} -}} Delete recording
** {{POST /recordings/\{recordingId\}/stop}} \- Stop recording
** {{POST /recordings/\{recordingId\}/pause}} \- Pause recording
** {{POST /recordings/\{recordingId\}/unpause}} \- Unpause recording
** {{POST /recordings/\{recordingId\}/mute}} \- Mute recording
** {{POST /recordings/\{recordingId\}/unmute}} \- Unmute recording

A project is underway to write an [Asterisk Client Library Generator] which will be capable of producing comprehensive client libraries in several languages.  The generator uses the Swagger resource files included in Asterisk to generate the libraries.

h3. WebSocket Commands and Events

In addition to responding to commands from the application, Stasis will also need to asynchronously send events to the application, notifying the application of new channels, state changes, etc. There are also a few commands allowing the WebSocket client to subscribe to certain events.

As with the RESTful API, these messages will initially be in JSON, with XML coming along later.

* Commands
** *SubscribeToEndpoints* - Subscribe to events for a particular set of endpoints.
** *UnsubscribeToEndpoints*
** *ActivateApplication* - Subscribes to channel events for a specific Stasis application.
** *DeactivateApplication*
* Endpoint Events
** *EndpointStateChange* \- Indicates when endpoints go offline/online, and when channels activate/hangup on the endpoint.
* Channel Events
** *TelephonyEvent* \- DTMF, Hookflash, etc.
** *ChannelStateChange* \- Ringing, Established, Cleared, Held, Unheld
** *ChannelIncoming* \- Channel being routed to an application
* Bridge Events - sent to the Stasis application that created the bridge
** *BridgeCreated*
** *BridgeDestroyed*
** *ChannelEnteredBridge*
** *ChannelLeftBridge*

h1. Design

h2. Pretty Picture

Visually, this is how it all fits together.

edge [arrowhead="none"];
node [width=1.5];

{ rank = same; manager; "stasis-core"; "stasis-http"; }
{ rank = same; "Action Callbacks"; "Stasis Callbacks"; }
{ rank = same; "module X"; "stasis-façade"; }

manager -> "Action Callbacks" [arrowhead="vee", style="dashed"]
"Action Callbacks" -> "module X" [arrowtail="o", dir="back"]
"module X" -> "Internal APIs" [arrowhead="o"]
"stasis-core" -> "Stasis Callbacks" [arrowhead="vee", style="dashed"]
"Stasis Callbacks" -> "stasis-façade" [arrowtail="o", dir="back"]
"stasis-façade" -> "Action Callbacks" [arrowhead="vee", style="dashed"]
"stasis-façade" -> "Internal APIs" [arrowhead="vee", style="dashed"]
"stasis-core" -> "stasis-http" [arrowtail="vee", dir="back"]

"stasis-http" [fillcolor=green]
"stasis-core" [fillcolor=green]
"Stasis Callbacks" [fillcolor=green]
"stasis-façade" [fillcolor=green]

h2. Existing Components

Stasis will introduce several new modules into Asterisk. To understand how they interact with the existing system, it helps to know some of the existing components.

h3. manager

Existing AMI implementation. Modules may register callbacks for AMI actions, and dispatch AMI events via this component.

h3. Action Callbacks

Existing 'interfaces' for implementing AMI Actions. And by 'interface', I mean callback functions.

h3. Existing modules

An existing Asterisk module. It already has functions that implements AMI action handlers, and dispatch AMI events.

h3. Internal API's

Internal state and API's exposed within Asterisk, but not necessarily exposed to the outside world via AMI.

h2. New Components

h3. res_stasis_core

Core logic for the Stasis framework. This is a coordination layer between protocol bindings (as in stasis-http) and the underlying implementations in Asterisk modules.

The separation of responsibilities between stasis-core and stasis-http keeps the door open for other protocol bindings to be built upon the Stasis API's. While the exact details of how stasis-core exposes the internal API are not defined, at the moment we are leaning towards a generic message bus.

h3. res_stasis_http

Binding between Stasis and HTTP. While other Stasis bindings are possible, this design allows them to be brought up in addition to the HTTP binding.

h3. res_stasis_facade

This allows us to manage existing Asterisk modules without changing them. This wrapper will use the interfaces exposed by modules, taking advantage of both AMI handlers and internal API's to get at richer state information.

By reducing the amount of change required for existing modules to support Stasis, we reduce the risk of Stasis-induced bugs.

As an additional benefit, if we can keep changes to existing code to a minimum, Stasis may be made available as a patch for Asterisk 11.

h1. Test Plan

Each controller in the RESTful API should have at least one integration test validating that function. Many will have multiple tests to validate failure conditions (originate failed, etc.).

|| Test || Level || Description ||
| stasis_obj_to_json | Unit | Tests Stasis object to JSON codec |
| stasis_obj_to_xml | Unit | Tests Stasis object to XML codec |

h1. Project Planning

h2. JIRA Issues


h2. Contributors

|| Name || E-mail Address ||
| [~dlee] | |

h1. Reference Information

* Stable identifier for channels:
** [~dlee:Stable Identifiers for Asterisk]
* Stasis API:
*** Thread continues here:

h1. Footnotes

* {anchor:bad-name} While it may stand for "Some Thought Actually Spent In Specification", suggestions for a better name are welcome.