Commit Graph

9 Commits

Author SHA1 Message Date
George Joseph
f08020df80 chan_websocket: Add capability for JSON control messages and events.
With recent enhancements to chan_websocket, the original plain-text
implementation of control messages and events is now too limiting.  We
probably should have used JSON initially but better late than never.  Going
forward, enhancements that require control message or event changes will
only be done to the JSON variants and the plain-text variants are now
deprecated but not yet removed.

* Added the chan_websocket.conf config file that allows setting which control
message format to use globally: "json" or "plain-text".  "plain-text" is the
default for now to preserve existing behavior.

* Added a dialstring option `f(json|plain-text)` to allow the format to be
overridden on a call-by-call basis.  Again, 'plain-text' is the default for
now to preserve existing behavior.

The JSON for commands sent by the app to Asterisk must be...
`{ "command": "<command>" ... }` where `<command>` is one of `ANSWER`, `HANGUP`,
`START_MEDIA_BUFFERING`, etc.  The `STOP_MEDIA_BUFFERING` command takes an
additional, optional parameter to be returned in the corresponding
`MEDIA_BUFFERING_COMPLETED` event:
`{ "command": "STOP_MEDIA_BUFFERING", "correlation_id": "<correlation id>" }`.

The JSON for events sent from Asterisk to the app will be...
`{ "event": "<event>", "channel_id": "<channel_id>" ... }`.
The `MEDIA_START` event will now look like...

```
{
  "event": "MEDIA_START",
  "connection_id": "media_connection1",
  "channel": "WebSocket/media_connection1/0x5140001a0040",
  "channel_id": "1761245643.1",
  "format": "ulaw",
  "optimal_frame_size": 160,
  "ptime": 20,
  "channel_variables": {
    "DIALEDPEERNUMBER": "media_connection1/c(ulaw)",
    "MEDIA_WEBSOCKET_CONNECTION_ID": "media_connection1",
    "MEDIA_WEBSOCKET_OPTIMAL_FRAME_SIZE": "160"
  }
}
```

Note the addition of the channel variables which can't be supported
with the plain-text formatting.

The documentation will be updated with the exact formats for all commands
and events.

Resolves: #1546
Resolves: #1563

DeveloperNote: The chan_websocket plain-text control and event messages are now
deprecated (but remain the default) in favor of JSON formatted messages.
See https://docs.asterisk.org/Configuration/Channel-Drivers/WebSocket for
more information.

DeveloperNote: A "transport_data" parameter has been added to the
channels/externalMedia ARI endpoint which, for websocket, allows the caller
to specify parameters to be added to the dialstring for the channel.  For
instance, `"transport_data": "f(json)"`.
2025-11-04 19:27:56 +00:00
gauravs456
4da19a9459 chan_websocket: Add channel_id to MEDIA_START, DRIVER_STATUS and DTMF_END events.
Resolves: #1544
2025-10-23 12:08:48 +00:00
George Joseph
a63eec2fbb chan_websocket.c: Change payload references to command instead.
Some of the tests in process_text_message() were still comparing to the
websocket message payload instead of the "command" string.

Resolves: #1525
2025-10-08 15:54:57 +00:00
George Joseph
896850ca35 chan_websocket: Fix codec validation and add passthrough option.
* Fixed an issue in webchan_write() where we weren't detecting equivalent
  codecs properly.
* Added the "p" dialstring option that puts the channel driver in
  "passthrough" mode where it will not attempt to re-frame or re-time
  media coming in over the websocket from the remote app.  This can be used
  for any codec but MUST be used for codecs that use packet headers or whose
  data stream can't be broken up on arbitrary byte boundaries. In this case,
  the remote app is fully responsible for correctly framing and timing media
  sent to Asterisk and the MEDIA text commands that could be sent over the
  websocket are disabled.  Currently, passthrough mode is automatically set
  for the opus, speex and g729 codecs.
* Now calling ast_set_read_format() after ast_channel_set_rawreadformat() to
  ensure proper translation paths are set up when switching between native
  frames and slin silence frames.  This fixes an issue with codec errors
  when transcode_via_sln=yes.

Resolves: #1462
2025-09-22 17:21:43 +00:00
Joe Garlick
0deac782db chan_websocket.c: Add DTMF messages
Added DTMF messages to the chan_websocket feature.

When a user presses DTMF during a call over chan_websocket it will send a message like:
"DTMF_END digit:1"

Resolves: https://github.com/asterisk/asterisk-feature-requests/issues/70
2025-09-08 14:34:03 +00:00
George Joseph
e33185dedb chan_websocket: Allow additional URI parameters to be added to the outgoing URI.
* Added a new option to the WebSocket dial string to capture the additional
  URI parameters.
* Added a new API ast_uri_verify_encoded() that verifies that a string
  either doesn't need URI encoding or that it has already been encoded.
* Added a new API ast_websocket_client_add_uri_params() to add the params
  to the client websocket session.
* Added XML documentation that will show up with `core show application Dial`
  that shows how to use it.

Resolves: #1352

UserNote: A new WebSocket channel driver option `v` has been added to the
Dial application that allows you to specify additional URI parameters on
outgoing connections. Run `core show application Dial` from the Asterisk CLI
to see how to use it.
2025-08-20 15:33:44 +00:00
George Joseph
c1065d3444 chan_websocket: Fix buffer overrun when processing TEXT websocket frames.
ast_websocket_read() receives data into a fixed 64K buffer then continually
reallocates a final buffer that, after all continuation frames have been
received, is the exact length of the data received and returns that to the
caller.  process_text_message() in chan_websocket was attempting to set a
NULL terminator on the received payload assuming the payload buffer it
received was the large 64K buffer.  The assumption was incorrect so when it
tried to set a NULL terminator on the payload, it could, depending on the
state of the heap at the time, cause heap corruption.

process_text_message() now allocates its own payload_len + 1 sized buffer,
copies the payload received from ast_websocket_read() into it then NULL
terminates it prevent the possibility of the overrun and corruption.

Resolves: #1384
2025-08-20 14:42:21 +00:00
kodokaii
f9219284cb chan_websocket: Reset frame_queue_length to 0 after FLUSH_MEDIA
In the WebSocket channel driver, the FLUSH_MEDIA command clears all frames from
the queue but does not reset the frame_queue_length counter.

As a result, the driver incorrectly thinks the queue is full after flushing,
which prevents new multimedia frames from being sent, especially after multiple
flush commands.

This fix sets frame_queue_length to 0 after flushing, ensuring the queue state
is consistent with its actual content.

Fixes: #1304
2025-07-15 13:46:10 +00:00
George Joseph
5963e624e2 Media over Websocket Channel Driver
* Created chan_websocket which can exchange media over both inbound and
outbound websockets which the driver will frame and time.
See http://s.asterisk.net/mow for more information.

* res_http_websocket: Made defines for max message size public and converted
a few nuisance verbose messages to debugs.

* main/channel.c: Changed an obsolete nuisance error to a debug.

* ARI channels: Updated externalMedia to include chan_websocket as a supported
transport.

UserNote: A new channel driver "chan_websocket" is now available. It can
exchange media over both inbound and outbound websockets and will both frame
and re-time the media it receives.
See http://s.asterisk.net/mow for more information.

UserNote: The ARI channels/externalMedia API now includes support for the
WebSocket transport provided by chan_websocket.
2025-07-09 17:42:22 +00:00