Asterisk - The Open Source Telephony Project GIT-master-545c459
Loading...
Searching...
No Matches
chan_websocket.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2025, Sangoma Technologies Corporation
5 *
6 * George Joseph <gjoseph@sangoma.com>
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/*! \file
20 *
21 * \author George Joseph <gjoseph@sangoma.com>
22 *
23 * \brief Websocket Media Channel
24 *
25 * \ingroup channel_drivers
26 */
27
28/*** MODULEINFO
29 <depend>res_http_websocket</depend>
30 <depend>res_websocket_client</depend>
31 <support_level>core</support_level>
32 ***/
33
34#include "asterisk.h"
35
36#include "asterisk/app.h"
37#include "asterisk/causes.h"
38#include "asterisk/channel.h"
39#include "asterisk/codec.h"
42#include "asterisk/frame.h"
43#include "asterisk/json.h"
44#include "asterisk/lock.h"
45#include "asterisk/mod_format.h"
46#include "asterisk/module.h"
47#include "asterisk/pbx.h"
48#include "asterisk/uuid.h"
49#include "asterisk/timing.h"
50#include "asterisk/translate.h"
52#include "asterisk/sorcery.h"
53
54static struct ast_sorcery *sorcery = NULL;
55
61
62static const char *msg_format_map[] = {
63 [WEBCHAN_CONTROL_MSG_FORMAT_PLAIN] = "plain-text",
66};
67
73
74/* This is from the perspective of the app, NOT Asterisk */
80
81static const char *websocket_media_direction_map[] = {
85};
86
88
89static struct ao2_container *instances = NULL;
90
120
121/*
122 * These are the indexes in the channel's file descriptor array
123 * not the file descriptors themselves.
124 */
125#define WS_TIMER_FDNO (AST_EXTENDED_FDS + 1)
126#define WS_WEBSOCKET_FDNO (AST_EXTENDED_FDS + 2)
127
128#define MEDIA_WEBSOCKET_OPTIMAL_FRAME_SIZE "MEDIA_WEBSOCKET_OPTIMAL_FRAME_SIZE"
129#define MEDIA_WEBSOCKET_CONNECTION_ID "MEDIA_WEBSOCKET_CONNECTION_ID"
130#define INCOMING_CONNECTION_ID "INCOMING"
131
132#define ANSWER_CHANNEL "ANSWER"
133#define HANGUP_CHANNEL "HANGUP"
134#define START_MEDIA_BUFFERING "START_MEDIA_BUFFERING"
135#define STOP_MEDIA_BUFFERING "STOP_MEDIA_BUFFERING"
136#define MARK_MEDIA "MARK_MEDIA"
137#define FLUSH_MEDIA "FLUSH_MEDIA"
138#define GET_DRIVER_STATUS "GET_STATUS"
139#define REPORT_QUEUE_DRAINED "REPORT_QUEUE_DRAINED"
140#define PAUSE_MEDIA "PAUSE_MEDIA"
141#define CONTINUE_MEDIA "CONTINUE_MEDIA"
142#define SET_MEDIA_DIRECTION "SET_MEDIA_DIRECTION"
143
144#define QUEUE_LENGTH_MAX 1000
145#define QUEUE_LENGTH_XOFF_LEVEL 900
146#define QUEUE_LENGTH_XON_LEVEL 800
147#define MAX_TEXT_MESSAGE_LEN MIN(128, (AST_WEBSOCKET_MAX_RX_PAYLOAD_SIZE - 1))
148
149/* Forward declarations */
150static int read_from_ws_and_queue(struct websocket_pvt *instance);
151static void _websocket_request_hangup(struct websocket_pvt *instance, int ast_cause,
152 enum ast_websocket_status_code tech_cause, int line, const char *function);
153static struct ast_channel *webchan_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause);
154static int webchan_call(struct ast_channel *ast, const char *dest, int timeout);
155static struct ast_frame *webchan_read(struct ast_channel *ast);
156static int webchan_write(struct ast_channel *ast, struct ast_frame *f);
157static int webchan_hangup(struct ast_channel *ast);
158static int webchan_send_dtmf_text(struct ast_channel *ast, char digit, unsigned int duration);
159static int set_channel_timer(struct websocket_pvt *instance);
160
161#define websocket_request_hangup(_instance, _cause, _tech) \
162 _websocket_request_hangup(_instance, _cause, _tech, __LINE__, __FUNCTION__)
163
165 .type = "WebSocket",
166 .description = "Media over WebSocket Channel Driver",
167 .requester = webchan_request,
168 .call = webchan_call,
169 .read = webchan_read,
170 .write = webchan_write,
171 .hangup = webchan_hangup,
172 .send_digit_end = webchan_send_dtmf_text,
173};
174
187
189{
191 return NULL;
192 }
193 return msg_format_map[value];
194}
195
196/*!
197 * \internal
198 * \brief Catch-all to print events that don't have any data.
199 * \warning Do not call directly.
200 */
201static char *_create_event_nodata(struct websocket_pvt *instance, char *event)
202{
203 char *payload = NULL;
205 struct ast_json * msg = ast_json_pack("{ s:s s:s }",
206 "event", event,
207 "channel_id", ast_channel_uniqueid(instance->channel));
208 if (!msg) {
209 return NULL;
210 }
212 ast_json_unref(msg);
213 } else {
214 payload = ast_strdup(event);
215 }
216
217 return payload;
218}
219
220#define _create_event_MEDIA_XON(_instance) _create_event_nodata(_instance, "MEDIA_XON");
221#define _create_event_MEDIA_XOFF(_instance) _create_event_nodata(_instance, "MEDIA_XOFF");
222#define _create_event_QUEUE_DRAINED(_instance) _create_event_nodata(_instance, "QUEUE_DRAINED");
223
224/*!
225 * \internal
226 * \brief Print the MEDIA_START event.
227 * \warning Do not call directly.
228 */
229static char *_create_event_MEDIA_START(struct websocket_pvt *instance)
230{
231 char *payload = NULL;
232
234 struct ast_json *msg = ast_json_pack("{s:s, s:s, s:s, s:s, s:s, s:i, s:i, s:b, s:b, s:o }",
235 "event", "MEDIA_START",
236 "connection_id", instance->connection_id,
237 "channel", ast_channel_name(instance->channel),
238 "channel_id", ast_channel_uniqueid(instance->channel),
239 "format", ast_format_get_name(instance->native_format),
240 "optimal_frame_size", instance->optimal_frame_size,
241 "ptime", instance->native_codec->default_ms,
242 "passthrough", instance->passthrough,
243 "unbuffered", instance->unbuffered,
244 "channel_variables", ast_json_channel_vars(ast_channel_varshead(
245 instance->channel))
246 );
247 if (!msg) {
248 return NULL;
249 }
251 ast_json_unref(msg);
252 } else {
253 ast_asprintf(&payload, "%s %s:%s %s:%s %s:%s %s:%s %s:%d %s:%d %s:%s %s:%s",
254 "MEDIA_START",
255 "connection_id", instance->connection_id,
256 "channel", ast_channel_name(instance->channel),
257 "channel_id", ast_channel_uniqueid(instance->channel),
258 "format", ast_format_get_name(instance->native_format),
259 "optimal_frame_size", instance->optimal_frame_size,
260 "ptime", instance->native_codec->default_ms,
261 "passthrough", instance->passthrough ? "true" : "false",
262 "unbuffered", instance->unbuffered ? "true" : "false"
263 );
264 }
265
266 return payload;
267}
268
269/*!
270 * \internal
271 * \brief Print the MEDIA_BUFFERING_COMPLETED event.
272 * \warning Do not call directly.
273 */
275 const char *id)
276{
277 char *payload = NULL;
279 struct ast_json *msg = ast_json_pack("{s:s, s:s, s:s}",
280 "event", "MEDIA_BUFFERING_COMPLETED",
281 "channel_id", ast_channel_uniqueid(instance->channel),
282 "correlation_id", S_OR(id, "")
283 );
284 if (!msg) {
285 return NULL;
286 }
288 ast_json_unref(msg);
289 } else {
290 ast_asprintf(&payload, "%s%s%s",
291 "MEDIA_BUFFERING_COMPLETED",
292 S_COR(id, " ",""), S_OR(id, ""));
293
294 }
295
296 return payload;
297}
298
299/*!
300 * \internal
301 * \brief Print the MEDIA_MARK_PROCESSED event.
302 * \warning Do not call directly.
303 */
305 const char *id)
306{
307 char *payload = NULL;
309 struct ast_json *msg = ast_json_pack("{s:s, s:s, s:s}",
310 "event", "MEDIA_MARK_PROCESSED",
311 "channel_id", ast_channel_uniqueid(instance->channel),
312 "correlation_id", S_OR(id, "")
313 );
314 if (!msg) {
315 return NULL;
316 }
318 ast_json_unref(msg);
319 } else {
320 ast_asprintf(&payload, "%s%s%s",
321 "MEDIA_MARK_PROCESSED",
322 S_COR(id, " ",""), S_OR(id, ""));
323
324 }
325
326 return payload;
327}
328
329/*!
330 * \internal
331 * \brief Print the DTMF_END event.
332 * \warning Do not call directly.
333 */
334static char *_create_event_DTMF_END(struct websocket_pvt *instance,
335 const char digit)
336{
337 char *payload = NULL;
339 struct ast_json *msg = ast_json_pack("{s:s, s:s, s:s#}",
340 "event", "DTMF_END",
341 "channel_id", ast_channel_uniqueid(instance->channel),
342 "digit", &digit, 1
343 );
344 if (!msg) {
345 return NULL;
346 }
348 ast_json_unref(msg);
349 } else {
350 ast_asprintf(&payload, "%s digit:%c channel_id:%s",
351 "DTMF_END", digit, ast_channel_uniqueid(instance->channel));
352 }
353
354 return payload;
355}
356
357/*!
358 * \internal
359 * \brief Print the STATUS event.
360 * \warning Do not call directly.
361 */
362static char *_create_event_STATUS(struct websocket_pvt *instance)
363{
364 char *payload = NULL;
365
367 struct ast_json *msg = ast_json_pack("{s:s, s:s, s:i, s:i, s:i, s:b, s:b, s:b }",
368 "event", "STATUS",
369 "channel_id", ast_channel_uniqueid(instance->channel),
370 "queue_length", instance->frame_queue_length,
371 "xon_level", QUEUE_LENGTH_XON_LEVEL,
372 "xoff_level", QUEUE_LENGTH_XOFF_LEVEL,
373 "queue_full", instance->queue_full,
374 "bulk_media", instance->bulk_media_in_progress,
375 "media_paused", instance->queue_paused
376 );
377 if (!msg) {
378 return NULL;
379 }
381 ast_json_unref(msg);
382 } else {
383 ast_asprintf(&payload, "%s channel_id:%s queue_length:%d xon_level:%d xoff_level:%d queue_full:%s bulk_media:%s media_paused:%s",
384 "STATUS",
385 ast_channel_uniqueid(instance->channel),
388 S_COR(instance->queue_full, "true", "false"),
389 S_COR(instance->bulk_media_in_progress, "true", "false"),
390 S_COR(instance->queue_paused, "true", "false")
391 );
392 }
393
394 return payload;
395}
396
397/*!
398 * \internal
399 * \brief Print the ERROR event.
400 * \warning Do not call directly.
401 */
402static __attribute__ ((format (gnu_printf, 2, 3))) char *_create_event_ERROR(
403 struct websocket_pvt *instance, const char *format, ...)
404{
405 char *payload = NULL;
406 char *error_text = NULL;
407 va_list ap;
408 int res = 0;
409
410 va_start(ap, format);
411 res = ast_vasprintf(&error_text, format, ap);
412 va_end(ap);
413 if (res < 0 || !error_text) {
414 return NULL;
415 }
416
417 if (instance->control_msg_format == WEBCHAN_CONTROL_MSG_FORMAT_JSON) {
418 struct ast_json *msg = ast_json_pack("{s:s, s:s, s:s}",
419 "event", "ERROR",
420 "channel_id", ast_channel_uniqueid(instance->channel),
421 "error_text", error_text);
422 ast_free(error_text);
423 if (!msg) {
424 return NULL;
425 }
427 ast_json_unref(msg);
428 } else {
429 ast_asprintf(&payload, "%s channel_id:%s error_text:%s",
430 "ERROR", ast_channel_uniqueid(instance->channel), error_text);
431 ast_free(error_text);
432 }
433
434 return payload;
435}
436
437/*!
438 * \def create_event
439 * \brief Use this macro to create events passing in any event-specific parameters.
440 */
441#define create_event(_instance, _event, ...) \
442 _create_event_ ## _event(_instance, ##__VA_ARGS__)
443
444/*!
445 * \def send_event
446 * \brief Use this macro to create and send events passing in any event-specific parameters.
447 */
448#define send_event(_instance, _event, ...) \
449({ \
450 int _res = -1; \
451 char *_payload = _create_event_ ## _event(_instance, ##__VA_ARGS__); \
452 if (_payload && _instance->websocket) { \
453 _res = ast_websocket_write_string(_instance->websocket, _payload); \
454 if (_res != 0) { \
455 ast_log(LOG_ERROR, "%s: Unable to send event %s\n", \
456 ast_channel_name(instance->channel), _payload); \
457 } else { \
458 ast_debug(3, "%s: Sent %s\n", \
459 ast_channel_name(instance->channel), _payload); \
460 }\
461 ast_free(_payload); \
462 } \
463 (_res); \
464})
465
466/*!
467 * \internal
468 *
469 * This function gets called by webchan_read which is triggered by the channel
470 * timer firing or by a websocket frame being received while in unbuffered mode.
471 * It always gets called at least every 20ms (or whatever the timer is set to)
472 * even if there are no frames in the queue.
473 */
474static struct ast_frame *dequeue_frame(struct websocket_pvt *instance)
475{
476 struct ast_frame *queued_frame = NULL;
477 SCOPED_LOCK(frame_queue_lock, &instance->frame_queue, AST_LIST_LOCK,
479
480 /*
481 * If the queue is paused, don't read a frame.
482 */
483 if (instance->queue_paused) {
484 return NULL;
485 }
486
487 /*
488 * We need to check if we need to send an XON before anything
489 * else because there are multiple escape paths in this function
490 * and we don't want to accidentally keep the queue in a "full"
491 * state.
492 */
493 if (instance->queue_full && instance->frame_queue_length < QUEUE_LENGTH_XON_LEVEL) {
494 instance->queue_full = 0;
495 ast_debug(4, "%s: WebSocket sending MEDIA_XON\n",
496 ast_channel_name(instance->channel));
497 send_event(instance, MEDIA_XON);
498 }
499
500 queued_frame = AST_LIST_REMOVE_HEAD(&instance->frame_queue, frame_list);
501
502 /*
503 * If there are no frames in the queue, we need to return NULL.
504 * We also need to send the QUEUE_DRAINED notification if we
505 * were requested to do so.
506 */
507 if (!queued_frame) {
508 if (instance->report_queue_drained) {
509 instance->report_queue_drained = 0;
510 ast_debug(4, "%s: WebSocket sending QUEUE_DRAINED\n",
511 ast_channel_name(instance->channel));
512 send_event(instance, QUEUE_DRAINED);
513 }
514 return NULL;
515 }
516
517 /*
518 * The only way a control frame could be present here is as
519 * a result of us calling queue_option_frame() in response
520 * to an incoming TEXT command from the websocket.
521 * We'll be safe and make sure it's a AST_CONTROL_OPTION
522 * frame anyway.
523 *
524 * It's quite possible that there are multiple control frames
525 * in a row in the queue so we need to process consecutive ones
526 * immediately.
527 *
528 * In any case, processing a control frame MUST not use up
529 * a media timeslot so after all control frames have been
530 * processed, we need to read an audio frame and process it.
531 */
532 while (queued_frame && queued_frame->frametype == AST_FRAME_CONTROL) {
533 if (queued_frame->subclass.integer == AST_CONTROL_OPTION) {
534 /*
535 * We just need to send the data to the websocket.
536 * The data should already be NULL terminated.
537 */
538 int res = ast_websocket_write_string(instance->websocket,
539 queued_frame->data.ptr);
540 if (res != 0) {
541 ast_log(LOG_ERROR, "%s: Unable to send event %s\n",
542 ast_channel_name(instance->channel), (char *)queued_frame->data.ptr);
543 } else {
544 ast_debug(4, "%s: Sent %s\n",
545 ast_channel_name(instance->channel), (char *)queued_frame->data.ptr);
546 }
547 }
548 /*
549 * We do NOT send these to the core so we need to free
550 * the frame and grab the next one. If it's also a
551 * control frame, we need to process it otherwise
552 * continue down in the function.
553 */
554 ast_frame_free(queued_frame, 0);
555 queued_frame = AST_LIST_REMOVE_HEAD(&instance->frame_queue, frame_list);
556 /*
557 * Jut FYI... We didn't bump the queue length when we added the control
558 * frames so we don't need to decrement it here.
559 */
560 }
561
562 /*
563 * If, after reading all control frames, there are no frames
564 * left in the queue, we need to return NULL.
565 */
566 if (!queued_frame) {
567 return NULL;
568 }
569
570 instance->frame_queue_length--;
571
572 return queued_frame;
573}
574
575static struct ast_frame *create_frame_from_buffer(struct websocket_pvt *instance,
576 char *buffer, size_t len)
577{
578 struct ast_frame fr = { 0, };
579 struct ast_frame *duped_frame = NULL;
580
581 AST_FRAME_SET_BUFFER(&fr, buffer, 0, len);
583 fr.subclass.format = instance->native_format;
584 if (instance->native_codec->samples_count) {
585 fr.samples = instance->native_codec->samples_count(&fr);
586 }
587
588 duped_frame = ast_frisolate(&fr);
589 if (!duped_frame) {
590 ast_log(LOG_WARNING, "%s: Failed to isolate frame\n",
591 ast_channel_name(instance->channel));
592 return NULL;
593 }
594
595 return duped_frame;
596}
597
598/*!
599 * \internal
600 *
601 * There are two file descriptors on this channel that can trigger
602 * this function...
603 *
604 * The timer fd (WS_TIMER_FDNO) which gets triggered at a constant
605 * rate determined by the format. In this case, we need to pull a
606 * frame OFF the queue and return it to the core.
607 *
608 * The websocket fd (WS_WEBSOCKET_FDNO) which gets triggered when
609 * there's incoming data to read from the websocket. In this case,
610 * we read the data and put it ON the queue. If in unbuffered mode,
611 * we'll read it off the queue immediately and return it. If not in
612 * unbuffered mode, we'll return a null frame now and the frame read
613 * will be processed by the timer tick when it comes to the head
614 * of the queue.
615 *
616 * The reason for queueing and dequeueing immediately in unbuffered
617 * mode is that read_from_ws_and_queue does a lot of work before adding
618 * a frame to the frame queue, if it even needs to. It's much simpler
619 * to just queue and dequeue, both from a code organization standpoint
620 * as well as an instruction-path-length standpoint, than it would be
621 * refactor that code so it can return a frame directly.
622 */
623static struct ast_frame *webchan_read(struct ast_channel *ast)
624{
625 struct websocket_pvt *instance = NULL;
626 struct ast_frame *native_frame = NULL;
627 int fdno = ast_channel_fdno(ast);
628
629 instance = ast_channel_tech_pvt(ast);
630 if (!instance) {
631 return NULL;
632 }
633
634 if (fdno != WS_TIMER_FDNO && fdno != WS_WEBSOCKET_FDNO) {
635 return &ast_null_frame;
636 }
637
638 if (fdno == WS_WEBSOCKET_FDNO) {
639 read_from_ws_and_queue(instance);
640 if (!instance->unbuffered) {
641 return &ast_null_frame;
642 }
643 } else {
645 ast_timer_ack(instance->timer, 1);
646 }
647 }
648
649 native_frame = dequeue_frame(instance);
650 if (!native_frame) {
651 if (instance->leftover_len > 0) {
652 native_frame = create_frame_from_buffer(instance, instance->leftover_data, instance->leftover_len);
653 if (native_frame) {
654 ast_debug(4, "%s: Triggered with no frame available but with %d bytes in leftover_data. Returning partial frame.\n",
655 ast_channel_name(ast), (int)instance->leftover_len);
656 instance->leftover_len = 0;
657 return native_frame;
658 }
659 }
660 ast_debug(4, "%s: Triggered with no frame available and no data in leftover_data. Returning NULL frame.\n",
661 ast_channel_name(ast));
662 return &ast_null_frame;
663 }
664
665 ast_debug(5, "%s: Dequeued %d byte frame. Left in buffer: %d\n",
666 ast_channel_name(ast), native_frame->datalen, (int)instance->leftover_len);
667
668 return native_frame;
669}
670
671static int queue_frame_from_buffer(struct websocket_pvt *instance,
672 char *buffer, size_t len)
673{
674 struct ast_frame *duped_frame = NULL;
675
676 duped_frame = create_frame_from_buffer(instance, buffer, len);
677 if (!duped_frame) {
678 return -1;
679 }
680
681 {
682 SCOPED_LOCK(frame_queue_lock, &instance->frame_queue, AST_LIST_LOCK,
684 AST_LIST_INSERT_TAIL(&instance->frame_queue, duped_frame, frame_list);
685 instance->frame_queue_length++;
686 if (!instance->queue_full && instance->frame_queue_length >= QUEUE_LENGTH_XOFF_LEVEL) {
687 instance->queue_full = 1;
688 send_event(instance, MEDIA_XOFF);
689 }
690 }
691
692 ast_debug(5, "%s: Queued %d byte frame\n", ast_channel_name(instance->channel),
693 duped_frame->datalen);
694
695 return 0;
696}
697
698static int queue_option_frame(struct websocket_pvt *instance,
699 char *buffer)
700{
701 struct ast_frame fr = { 0, };
702 struct ast_frame *duped_frame = NULL;
703
704 AST_FRAME_SET_BUFFER(&fr, buffer, 0, strlen(buffer) + 1);
707
708 duped_frame = ast_frisolate(&fr);
709 if (!duped_frame) {
710 ast_log(LOG_WARNING, "%s: Failed to isolate frame\n",
711 ast_channel_name(instance->channel));
712 return -1;
713 }
714
715 AST_LIST_LOCK(&instance->frame_queue);
716 AST_LIST_INSERT_TAIL(&instance->frame_queue, duped_frame, frame_list);
717 AST_LIST_UNLOCK(&instance->frame_queue);
718
719 ast_debug(4, "%s: Queued '%s' option frame\n",
720 ast_channel_name(instance->channel), buffer);
721
722 return 0;
723}
724
725#define ERROR_ON_PASSTHROUGH_MODE_RTN(instance, command) \
726({ \
727 if (instance->passthrough) { \
728 send_event(instance, ERROR, "%s not supported in passthrough mode", command); \
729 ast_debug(4, "%s: WebSocket in passthrough mode. Ignoring %s command.\n", \
730 ast_channel_name(instance->channel), command); \
731 return 0; \
732 } \
733})
734
735#define ERROR_ON_UNBUFFERED_MODE_RTN(instance, command) \
736({ \
737 if (instance->unbuffered) { \
738 send_event(instance, ERROR, "%s not supported in unbuffered mode", command); \
739 ast_debug(4, "%s: WebSocket in unbuffered mode. Ignoring %s command.\n", \
740 ast_channel_name(instance->channel), command); \
741 return 0; \
742 } \
743})
744
745#define ERROR_ON_INVALID_MEDIA_DIRECTION_RTN(instance, command, direction) \
746({ \
747 if (instance->media_direction == direction) { \
748 send_event(instance, ERROR, "%s not supported while media direction " \
749 "is '%s'", command, websocket_media_direction_map[direction]); \
750 ast_debug(4, "%s: WebSocket media direction is '%s'. Ignoring %s command.\n", \
751 ast_channel_name(instance->channel), websocket_media_direction_map[direction], command); \
752 return 0; \
753 } \
754})
755
756/*!
757 * \internal
758 * \brief Handle commands from the websocket
759 *
760 * \param instance
761 * \param buffer Allocated by caller so don't free.
762 * \retval 0 Success
763 * \retval -1 Failure
764 */
765static int handle_command(struct websocket_pvt *instance, char *buffer)
766{
767 int res = 0;
768 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
769 const char *command = NULL;
770 char *data = NULL;
771
773 struct ast_json_error json_error;
774
775 json = ast_json_load_buf(buffer, strlen(buffer), &json_error);
776 if (!json) {
777 send_event(instance, ERROR, "Unable to parse JSON command");
778 return -1;
779 }
780 command = ast_json_object_string_get(json, "command");
781 } else {
782 command = buffer;
783 data = strchr(buffer, ' ');
784 if (data) {
785 *data = '\0';
786 data++;
787 }
788 }
789
790 if (ast_strings_equal(command, ANSWER_CHANNEL)) {
792
793 } else if (ast_strings_equal(command, HANGUP_CHANNEL)) {
795
796 } else if (ast_strings_equal(command, START_MEDIA_BUFFERING)) {
797 ERROR_ON_PASSTHROUGH_MODE_RTN(instance, command);
799 if (instance->bulk_media_in_progress) {
800 send_event(instance, ERROR, "START_MEDIA_BUFFERING can't be called when media buffering is already active.\n");
801 return 0;
802 }
803
804 AST_LIST_LOCK(&instance->frame_queue);
805 instance->bulk_media_in_progress = 1;
806 instance->last_unbuffered = instance->unbuffered;
807 instance->unbuffered = 0;
808 AST_LIST_UNLOCK(&instance->frame_queue);
809
810 } else if (ast_strings_equal(command, STOP_MEDIA_BUFFERING)) {
811 const char *id;
812 char *option;
813 SCOPED_LOCK(frame_queue_lock, &instance->frame_queue, AST_LIST_LOCK,
815
817 id = ast_json_object_string_get(json, "correlation_id");
818 } else {
819 id = data;
820 }
821
822 ERROR_ON_PASSTHROUGH_MODE_RTN(instance, command);
824
825 if (!instance->bulk_media_in_progress) {
826 send_event(instance, ERROR, "STOP_MEDIA_BUFFERING can't be called when media buffering isn't active.\n");
827 return 0;
828 }
829
830 ast_debug(4, "%s: WebSocket %s '%s' with %d bytes in leftover_data.\n",
832 (int)instance->leftover_len);
833
834 instance->bulk_media_in_progress = 0;
835 instance->unbuffered = instance->last_unbuffered;
836 if (instance->leftover_len > 0) {
837 res = queue_frame_from_buffer(instance, instance->leftover_data, instance->leftover_len);
838 if (res != 0) {
839 return res;
840 }
841 }
842 instance->leftover_len = 0;
843 option = create_event(instance, MEDIA_BUFFERING_COMPLETED, id);
844 if (!option) {
845 return -1;
846 }
847 res = queue_option_frame(instance, option);
848 ast_free(option);
849
850 } else if (ast_strings_equal(command, MARK_MEDIA)) {
851 const char *id;
852 char *option;
853 SCOPED_LOCK(frame_queue_lock, &instance->frame_queue, AST_LIST_LOCK,
855
857
859 id = ast_json_object_string_get(json, "correlation_id");
860 } else {
861 id = data;
862 }
863
864 ast_debug(4, "%s: %s %s\n",
865 ast_channel_name(instance->channel), MARK_MEDIA, id);
866
867 option = create_event(instance, MEDIA_MARK_PROCESSED, id);
868 if (!option) {
869 return -1;
870 }
871 res = queue_option_frame(instance, option);
872 ast_free(option);
873
874 } else if (ast_strings_equal(command, FLUSH_MEDIA)) {
875 struct ast_frame *frame = NULL;
876
877 ERROR_ON_PASSTHROUGH_MODE_RTN(instance, command);
878 ERROR_ON_UNBUFFERED_MODE_RTN(instance, command);
879
880 AST_LIST_LOCK(&instance->frame_queue);
881 while ((frame = AST_LIST_REMOVE_HEAD(&instance->frame_queue, frame_list))) {
882 ast_frfree(frame);
883 }
884 instance->frame_queue_length = 0;
885 instance->bulk_media_in_progress = 0;
886 instance->leftover_len = 0;
887 instance->unbuffered = instance->last_unbuffered;
888 AST_LIST_UNLOCK(&instance->frame_queue);
889
890 } else if (ast_strings_equal(command, REPORT_QUEUE_DRAINED)) {
891 ERROR_ON_PASSTHROUGH_MODE_RTN(instance, command);
892
893 AST_LIST_LOCK(&instance->frame_queue);
894 instance->report_queue_drained = 1;
895 AST_LIST_UNLOCK(&instance->frame_queue);
896
897 } else if (ast_strings_equal(command, GET_DRIVER_STATUS)) {
898 return send_event(instance, STATUS);
899
900 } else if (ast_strings_equal(command, PAUSE_MEDIA)) {
901 ERROR_ON_PASSTHROUGH_MODE_RTN(instance, command);
902 ERROR_ON_UNBUFFERED_MODE_RTN(instance, command);
904 AST_LIST_LOCK(&instance->frame_queue);
905 instance->queue_paused = 1;
906 AST_LIST_UNLOCK(&instance->frame_queue);
907
908 } else if (ast_strings_equal(command, CONTINUE_MEDIA)) {
909 ERROR_ON_PASSTHROUGH_MODE_RTN(instance, command);
910 ERROR_ON_UNBUFFERED_MODE_RTN(instance, command);
912 AST_LIST_LOCK(&instance->frame_queue);
913 instance->queue_paused = 0;
914 AST_LIST_UNLOCK(&instance->frame_queue);
915
916 } else if (ast_strings_equal(command, SET_MEDIA_DIRECTION)) {
917 const char *direction;
918
919 ERROR_ON_PASSTHROUGH_MODE_RTN(instance, command);
920
922 send_event(instance, ERROR, "%s only supports JSON format.\n", command);
923 return 0;
924 }
925
926 direction = ast_json_object_string_get(json, "direction");
927 if (!direction) {
928 send_event(instance, ERROR, "%s requires a 'direction' parameter.\n", command);
929 return 0;
930 }
931
932 if (!strcmp("both", direction)) {
934 return 0;
935 }
936
937 if (!instance->timer) {
938 set_channel_timer(instance);
940 }
941
943
944 } else if (!strcmp("out", direction)) {
946 return 0;
947 }
948
949 if (!instance->timer) {
950 set_channel_timer(instance);
952 }
953
955
956 } else if (!strcmp("in", direction)) {
958 return 0;
959 }
960
961 if (instance->timer) {
963 ast_timer_close(instance->timer);
964 instance->timer = NULL;
966 }
967
969
970 } else {
971 send_event(instance, ERROR, "'%s' is not a valid direction for %s.\n",
972 direction, command);
973 return 0;
974 }
975
976 } else {
977 ast_log(LOG_WARNING, "%s: WebSocket %s command unknown\n",
978 ast_channel_name(instance->channel), command);
979 }
980
981 return res;
982}
983
984static int process_text_message(struct websocket_pvt *instance,
985 char *payload, uint64_t payload_len)
986{
987 char *command;
988
989 if (payload_len == 0) {
990 ast_log(LOG_WARNING, "%s: WebSocket TEXT message has 0 length\n",
991 ast_channel_name(instance->channel));
992 return 0;
993 }
994
995 if (payload_len > MAX_TEXT_MESSAGE_LEN) {
996 ast_log(LOG_WARNING, "%s: WebSocket TEXT message of length %d exceeds maximum length of %d\n",
997 ast_channel_name(instance->channel), (int)payload_len, MAX_TEXT_MESSAGE_LEN);
998 return 0;
999 }
1000
1001 /*
1002 * Unfortunately, payload is not NULL terminated even when it's
1003 * a TEXT frame so we need to allocate a new buffer, copy
1004 * the data into it, and NULL terminate it.
1005 */
1006 command = ast_alloca(payload_len + 1);
1007 memcpy(command, payload, payload_len); /* Safe */
1008 command[payload_len] = '\0';
1009 command = ast_strip(command);
1010
1011 ast_debug(4, "%s: Received: %s\n",
1012 ast_channel_name(instance->channel), command);
1013
1014 return handle_command(instance, command);
1015}
1016
1017static int process_binary_message(struct websocket_pvt *instance,
1018 char *payload, uint64_t payload_len)
1019{
1020 char *next_frame_ptr = NULL;
1021 size_t bytes_read = 0;
1022 int res = 0;
1023 size_t bytes_left = 0;
1024
1025 {
1026 SCOPED_LOCK(frame_queue_lock, &instance->frame_queue, AST_LIST_LOCK,
1028 if (instance->frame_queue_length >= QUEUE_LENGTH_MAX) {
1029 ast_debug(4, "%s: WebSocket queue is full. Ignoring incoming binary message.\n",
1030 ast_channel_name(instance->channel));
1031 return 0;
1032 }
1033 }
1034
1035 next_frame_ptr = payload;
1036 instance->bytes_read += payload_len;
1037
1038 if (instance->unbuffered) {
1039 res = queue_frame_from_buffer(instance, payload, payload_len);
1040 return res;
1041 }
1042
1043 if (instance->bulk_media_in_progress && instance->leftover_len > 0) {
1044 /*
1045 * We have leftover data from a previous websocket message.
1046 * Try to make a complete frame by appending data from
1047 * the current message to the leftover data.
1048 */
1049 char *append_ptr = instance->leftover_data + instance->leftover_len;
1050 size_t bytes_needed_for_frame = instance->optimal_frame_size - instance->leftover_len;
1051 /*
1052 * It's possible that even the current message doesn't have enough
1053 * data to make a complete frame.
1054 */
1055 size_t bytes_avail_to_copy = MIN(bytes_needed_for_frame, payload_len);
1056
1057 /*
1058 * Append whatever we can to the end of the leftover data
1059 * even if it's not enough to make a complete frame.
1060 */
1061 memcpy(append_ptr, payload, bytes_avail_to_copy);
1062
1063 /*
1064 * If leftover data is still short, just return and wait for the
1065 * next websocket message.
1066 */
1067 if (bytes_avail_to_copy < bytes_needed_for_frame) {
1068 ast_debug(4, "%s: Leftover data %d bytes but only %d new bytes available of %d needed. Appending and waiting for next message.\n",
1069 ast_channel_name(instance->channel), (int)instance->leftover_len, (int)bytes_avail_to_copy, (int)bytes_needed_for_frame);
1070 instance->leftover_len += bytes_avail_to_copy;
1071 return 0;
1072 }
1073
1074 res = queue_frame_from_buffer(instance, instance->leftover_data, instance->optimal_frame_size);
1075 if (res < 0) {
1076 return -1;
1077 }
1078
1079 /*
1080 * We stole data from the current payload so decrement payload_len
1081 * and set the next frame pointer after the data in payload
1082 * we just copied.
1083 */
1084 payload_len -= bytes_avail_to_copy;
1085 next_frame_ptr = payload + bytes_avail_to_copy;
1086
1087 ast_debug(5, "%s: --- BR: %4d FQ: %4d PL: %4d LOL: %3d P: %p NFP: %p OFF: %4d NPL: %4d BAC: %3d\n",
1088 ast_channel_name(instance->channel),
1089 instance->frame_queue_length,
1090 (int)instance->bytes_read,
1091 (int)(payload_len + bytes_avail_to_copy),
1092 (int)instance->leftover_len,
1093 payload,
1094 next_frame_ptr,
1095 (int)(next_frame_ptr - payload),
1096 (int)payload_len,
1097 (int)bytes_avail_to_copy
1098 );
1099
1100
1101 instance->leftover_len = 0;
1102 }
1103
1104 if (!instance->bulk_media_in_progress && instance->leftover_len > 0) {
1105 instance->leftover_len = 0;
1106 }
1107
1108 bytes_left = payload_len;
1109 while (bytes_read < payload_len && bytes_left >= instance->optimal_frame_size) {
1110 res = queue_frame_from_buffer(instance, next_frame_ptr,
1111 instance->optimal_frame_size);
1112 if (res < 0) {
1113 break;
1114 }
1115 bytes_read += instance->optimal_frame_size;
1116 next_frame_ptr += instance->optimal_frame_size;
1117 bytes_left -= instance->optimal_frame_size;
1118 }
1119
1120 if (instance->bulk_media_in_progress && bytes_left > 0) {
1121 /*
1122 * We have a partial frame. Save the leftover data.
1123 */
1124 ast_debug(5, "%s: +++ BR: %4d FQ: %4d PL: %4d LOL: %3d P: %p NFP: %p OFF: %4d BL: %4d\n",
1125 ast_channel_name(instance->channel),
1126 (int)instance->bytes_read,
1127 instance->frame_queue_length,
1128 (int)payload_len,
1129 (int)instance->leftover_len,
1130 payload,
1131 next_frame_ptr,
1132 (int)(next_frame_ptr - payload),
1133 (int)bytes_left
1134 );
1135 memcpy(instance->leftover_data, next_frame_ptr, bytes_left);
1136 instance->leftover_len = bytes_left;
1137 }
1138
1139 return 0;
1140}
1141
1142static int read_from_ws_and_queue(struct websocket_pvt *instance)
1143{
1144 uint64_t payload_len = 0;
1145 char *payload = NULL;
1146 enum ast_websocket_opcode opcode;
1147 int fragmented = 0;
1148 int res = 0;
1149
1150 if (!instance->websocket) {
1151 ast_log(LOG_WARNING, "%s: WebSocket session not found\n",
1152 ast_channel_name(instance->channel));
1153 return -1;
1154 }
1155
1156 res = ast_websocket_read(instance->websocket, &payload, &payload_len,
1157 &opcode, &fragmented);
1158
1159 if (res) {
1160 ast_debug(3, "%s: WebSocket read error\n",
1161 ast_channel_name(instance->channel));
1163 return -1;
1164 }
1165 ast_debug(5, "%s: WebSocket read %d bytes\n", ast_channel_name(instance->channel),
1166 (int)payload_len);
1167
1168 if (opcode == AST_WEBSOCKET_OPCODE_TEXT) {
1169 return process_text_message(instance, payload, payload_len);
1170 }
1171
1172 /*
1173 * PINGs and PONGs will have been handled by res_http_websocket.
1174 * We also need to ignore CONTINUATION frames as they will be accumulated
1175 * by res_http_websocket until the threshold set in websocket_handoff_to_channel()
1176 * is reached, then it will send us a TEXT or BINARY frame.
1177 */
1178 if (opcode == AST_WEBSOCKET_OPCODE_PING || opcode == AST_WEBSOCKET_OPCODE_PONG
1179 || opcode == AST_WEBSOCKET_OPCODE_CONTINUATION) {
1180 return 0;
1181 }
1182
1183 if (opcode == AST_WEBSOCKET_OPCODE_CLOSE) {
1184 ast_debug(3, "%s: WebSocket closed by remote\n",
1185 ast_channel_name(instance->channel));
1187 return -1;
1188 }
1189
1190 if (opcode == AST_WEBSOCKET_OPCODE_BINARY) {
1191 /* If the application's media direction is 'in', drop any media we receive from it */
1193 ast_debug(5, "%s: WebSocket dropped frame (application media direction is 'in')\n",
1194 ast_channel_name(instance->channel));
1195 return 0;
1196 }
1197 } else {
1198 ast_log(LOG_WARNING, "%s: WebSocket frame type %d not supported\n",
1199 ast_channel_name(instance->channel), (int)opcode);
1201 return 0;
1202 }
1203
1204 return process_binary_message(instance, payload, payload_len);
1205}
1206
1208{
1209 int res = 0;
1210 int nodelay = 1;
1211 struct ast_sockaddr *remote_addr = ast_websocket_remote_address(instance->websocket);
1212
1213 instance->remote_addr = ast_strdup(ast_sockaddr_stringify(remote_addr));
1214 ast_debug(3, "%s: WebSocket connection with %s established\n",
1215 ast_channel_name(instance->channel), instance->remote_addr);
1216
1217 if (setsockopt(ast_websocket_fd(instance->websocket),
1218 IPPROTO_TCP, TCP_NODELAY, (char *) &nodelay, sizeof(nodelay)) < 0) {
1219 ast_log(LOG_WARNING, "Failed to set TCP_NODELAY on websocket connection: %s\n", strerror(errno));
1220 }
1221
1222 /*
1223 * The way write timeouts are handled in iostream requires the socket to be
1224 * in non-blocking mode. This is fine for reads as well because we already
1225 * set the websocket file descriptor on the channel and let it call
1226 * webchan_read() when data is available.
1227 */
1229
1230 /*
1231 * Tell res_http_websocket to accumulate incoming WebSocket CONTINUATION frames
1232 * into chunks of 1024 bytes and send us a TEXT or BINARY frame when the threshold
1233 * is reached.
1234 */
1236
1238
1239 res = send_event(instance, MEDIA_START);
1240 if (res != 0 ) {
1241 if (instance->type == AST_WS_TYPE_SERVER) {
1243 } else {
1244 /*
1245 * We were called by webchan_call so just need to set causes.
1246 * The core will hangup the channel.
1247 */
1250 }
1251 return -1;
1252 }
1253
1254 if (!instance->no_auto_answer) {
1255 ast_debug(3, "%s: ANSWER by auto_answer\n", ast_channel_name(instance->channel));
1257 }
1258
1259 return 0;
1260}
1261
1262static void _websocket_request_hangup(struct websocket_pvt *instance, int ast_cause,
1263 enum ast_websocket_status_code tech_cause, int line, const char *function)
1264{
1265 if (!instance || !instance->channel) {
1266 return;
1267 }
1268 ast_debug(3, "%s:%s: Hangup requested from %s line %d. cause: %s(%d) tech_cause: %s(%d)",
1269 ast_channel_name(instance->channel), instance->remote_addr,
1270 function, line,
1271 ast_cause2str(ast_cause), ast_cause, ast_websocket_status_to_str(tech_cause), tech_cause);
1272
1273 if (tech_cause) {
1274 ast_channel_tech_hangupcause_set(instance->channel, tech_cause);
1275 }
1276 ast_queue_hangup_with_cause(instance->channel, ast_cause);
1277}
1278
1279/*! \brief Function called when we should write a frame to the channel */
1280static int webchan_write(struct ast_channel *ast, struct ast_frame *f)
1281{
1282 struct websocket_pvt *instance = ast_channel_tech_pvt(ast);
1283 int res = 0;
1284
1285 if (!instance || !instance->websocket) {
1286 ast_log(LOG_WARNING, "%s: WebSocket instance or client not found\n",
1287 ast_channel_name(ast));
1288 return -1;
1289 }
1290
1291 /* The app doesn't want media right now */
1293 return 0;
1294 }
1295
1296 if (f->frametype == AST_FRAME_CNG) {
1297 return 0;
1298 }
1299
1300 if (f->frametype != AST_FRAME_VOICE) {
1301 ast_log(LOG_WARNING, "%s: This WebSocket channel only supports AST_FRAME_VOICE frames\n",
1302 ast_channel_name(ast));
1303 return 0;
1304 }
1305
1307 ast_log(LOG_WARNING, "%s: This WebSocket channel only supports the '%s' format, not '%s'\n",
1310 return -1;
1311 }
1312
1314 (char *)f->data.ptr, (uint64_t)f->datalen);
1315 if (res != 0) {
1316 ast_log(LOG_WARNING, "%s: WebSocket write failure\n", ast_channel_name(ast));
1317 }
1318
1319 return res;
1320}
1321
1322/*!
1323 * \internal
1324 *
1325 * Called by the core to actually call the remote.
1326 * The core will hang up the channel if a non-zero is returned.
1327 * We just need to set hangup causes if appropriate.
1328 */
1329static int webchan_call(struct ast_channel *ast, const char *dest,
1330 int timeout)
1331{
1332 struct websocket_pvt *instance = ast_channel_tech_pvt(ast);
1334 struct webchan_conf_global *global_cfg = ast_sorcery_retrieve_by_id(sorcery, "global", "global");
1335 int global_write_timeout = global_cfg ? global_cfg->write_timeout : AST_DEFAULT_WEBSOCKET_WRITE_TIMEOUT;
1336
1337 ao2_cleanup(global_cfg);
1338
1339 if (!instance) {
1340 ast_log(LOG_WARNING, "%s: WebSocket instance not found\n",
1341 ast_channel_name(ast));
1343 return -1;
1344 }
1345
1346 if (instance->type == AST_WS_TYPE_SERVER) {
1347 ast_debug(3, "%s: Websocket call incoming\n", ast_channel_name(instance->channel));
1348 return 0;
1349 }
1350 ast_debug(3, "%s: Websocket call outgoing\n", ast_channel_name(instance->channel));
1351
1352 if (!instance->client) {
1353 ast_log(LOG_WARNING, "%s: WebSocket client not found\n",
1354 ast_channel_name(ast));
1356 return -1;
1357 }
1358
1359 ast_debug(3, "%s: WebSocket call requested to %s. cid: %s\n",
1360 ast_channel_name(ast), dest, instance->connection_id);
1361
1362 if (!ast_strlen_zero(instance->uri_params)) {
1364 }
1365
1366 instance->websocket = ast_websocket_client_connect(instance->client,
1367 instance, ast_channel_name(ast), &result);
1368 if (!instance->websocket || result != WS_OK) {
1369 ast_log(LOG_WARNING, "%s: WebSocket connection failed to %s: %s\n",
1372 return -1;
1373 }
1374
1375 /*
1376 * If websocket_client->write_timeout was set in websocket_client.conf, it will
1377 * have been applied to the websocket by ast_websocket_client_connect() above.
1378 * If it wasn't set in websocket_client.conf, the value will be INT_MAX and
1379 * and ast_websocket_client_connect() will have set AST_DEFAULT_WEBSOCKET_WRITE_TIMEOUT
1380 * on the websocket. However, the user may have set write_timeout in the global section
1381 * of chan_websocket.conf so if it wasn't set in websocket_client.conf, we'll now set
1382 * the websocket timeout to that. If they haven't set it in chan_websocket.conf either,
1383 * it'll default to AST_DEFAULT_WEBSOCKET_WRITE_TIMEOUT as well so the call below will
1384 * basically become a no-op.
1385 */
1386 if (instance->client->write_timeout == INT_MAX) {
1387 ast_websocket_set_timeout(instance->websocket, global_write_timeout);
1388 }
1389
1390 return websocket_handoff_to_channel(instance);
1391}
1392
1393static void websocket_destructor(void *data)
1394{
1395 struct websocket_pvt *instance = data;
1396 struct ast_frame *frame = NULL;
1397 ast_debug(3, "%s: WebSocket instance freed\n", instance->connection_id);
1398
1399 AST_LIST_LOCK(&instance->frame_queue);
1400 while ((frame = AST_LIST_REMOVE_HEAD(&instance->frame_queue, frame_list))) {
1401 ast_frfree(frame);
1402 }
1403 AST_LIST_UNLOCK(&instance->frame_queue);
1404
1405 if (instance->timer) {
1406 ast_timer_close(instance->timer);
1407 instance->timer = NULL;
1408 }
1409
1410 if (instance->channel) {
1411 ast_channel_unref(instance->channel);
1412 instance->channel = NULL;
1413 }
1414 if (instance->websocket) {
1415 ast_websocket_unref(instance->websocket);
1416 instance->websocket = NULL;
1417 }
1418
1419 ao2_cleanup(instance->client);
1420 instance->client = NULL;
1421
1422 ao2_cleanup(instance->native_codec);
1423 instance->native_codec = NULL;
1424
1425 ao2_cleanup(instance->native_format);
1426 instance->native_format = NULL;
1427
1428 if (instance->leftover_data) {
1429 ast_free(instance->leftover_data);
1430 instance->leftover_data = NULL;
1431 }
1432
1433 ast_free(instance->uri_params);
1434 ast_free(instance->remote_addr);
1435}
1436
1439 /*! \brief The name of the module owning this sorcery instance */
1441};
1442
1443static void instance_proxy_cb(void *weakproxy, void *data)
1444{
1445 struct instance_proxy *proxy = weakproxy;
1446 ast_debug(3, "%s: WebSocket instance removed from instances\n", proxy->connection_id);
1447 ao2_unlink(instances, weakproxy);
1448}
1449
1450static struct websocket_pvt* websocket_new(const char *chan_name,
1451 const char *connection_id, struct ast_format *fmt)
1452{
1453 RAII_VAR(struct instance_proxy *, proxy, NULL, ao2_cleanup);
1454 RAII_VAR(struct websocket_pvt *, instance, NULL, ao2_cleanup);
1455 char uuid[AST_UUID_STR_LEN];
1456 enum ast_websocket_type ws_type;
1457
1458 SCOPED_AO2WRLOCK(locker, instances);
1459
1462 ws_type = AST_WS_TYPE_SERVER;
1463 } else {
1464 ws_type = AST_WS_TYPE_CLIENT;
1465 }
1466
1467 proxy = ao2_weakproxy_alloc(sizeof(*proxy) + strlen(connection_id) + 1, NULL);
1468 if (!proxy) {
1469 return NULL;
1470 }
1471 strcpy(proxy->connection_id, connection_id); /* Safe */
1472
1473 instance = ao2_alloc(sizeof(*instance) + strlen(connection_id) + 1,
1475 if (!instance) {
1476 return NULL;
1477 }
1478 strcpy(instance->connection_id, connection_id); /* Safe */
1479
1480 instance->type = ws_type;
1481 if (ws_type == AST_WS_TYPE_CLIENT) {
1482 instance->client = ast_websocket_client_retrieve_by_id(instance->connection_id);
1483 if (!instance->client) {
1484 ast_log(LOG_ERROR, "%s: WebSocket client connection '%s' not found\n",
1485 chan_name, instance->connection_id);
1486 return NULL;
1487 }
1488 }
1489
1490 AST_LIST_HEAD_INIT(&instance->frame_queue);
1491
1492 /*
1493 * We need the codec to calculate the number of samples in a frame
1494 * so we'll get it once and store it in the instance.
1495 *
1496 * References for native_format and native_codec are now held by the
1497 * instance and will be released when the instance is destroyed.
1498 */
1499 instance->native_format = fmt;
1500 instance->native_codec = ast_format_get_codec(instance->native_format);
1501 /*
1502 * References for native_format and native_codec are now held by the
1503 * instance and will be released when the instance is destroyed.
1504 */
1505
1506 /*
1507 * It's not possible for us to re-time or re-frame media if the data
1508 * stream can't be broken up on arbitrary byte boundaries. This is usually
1509 * indicated by the codec's minimum_bytes being small (10 bytes or less).
1510 * We need to force the passthrough and unbuffered modes in this case.
1511 */
1512 if (instance->native_codec->minimum_bytes <= 10) {
1513 instance->passthrough = 1;
1514 instance->unbuffered = 1;
1515 instance->optimal_frame_size = 0;
1516 } else {
1517 instance->optimal_frame_size =
1518 (instance->native_codec->default_ms * instance->native_codec->minimum_bytes)
1519 / instance->native_codec->minimum_ms;
1520 instance->leftover_data = ast_calloc(1, instance->optimal_frame_size);
1521 if (!instance->leftover_data) {
1522 return NULL;
1523 }
1524 }
1525
1526 ast_debug(3,
1527 "%s: WebSocket channel native format '%s' Sample rate: %d ptime: %dms minms: %u minbytes: %u passthrough: %d optimal_frame_size: %d\n",
1528 chan_name, ast_format_get_name(instance->native_format),
1529 ast_format_get_sample_rate(instance->native_format),
1530 ast_format_get_default_ms(instance->native_format),
1531 ast_format_get_minimum_ms(instance->native_format),
1532 ast_format_get_minimum_bytes(instance->native_format),
1533 instance->passthrough,
1534 instance->optimal_frame_size);
1535
1536 /* We have exclusive access to proxy and sorcery, no need for locking here. */
1537 if (ao2_weakproxy_set_object(proxy, instance, OBJ_NOLOCK)) {
1538 return NULL;
1539 }
1540
1542 return NULL;
1543 }
1544
1545 if (!ao2_link_flags(instances, proxy, OBJ_NOLOCK)) {
1546 ast_log(LOG_ERROR, "%s: Unable to link WebSocket instance to instances\n",
1547 proxy->connection_id);
1548 return NULL;
1549 }
1550 ast_debug(3, "%s: WebSocket instance created and linked\n", proxy->connection_id);
1551
1552 return ao2_bump(instance);
1553}
1554
1555static int set_channel_timer(struct websocket_pvt *instance)
1556{
1557 int rate = 0;
1558 instance->timer = ast_timer_open();
1559 if (!instance->timer) {
1560 return -1;
1561 }
1562 /* Rate is the number of ticks per second, not the interval. */
1563 rate = 1000 / ast_format_get_default_ms(instance->native_format);
1564 ast_debug(3, "%s: WebSocket timer rate %d\n",
1565 ast_channel_name(instance->channel), rate);
1566 ast_timer_set_rate(instance->timer, rate);
1567 /*
1568 * Calling ast_channel_set_fd will cause the channel thread to call
1569 * webchan_read at 'rate' times per second.
1570 */
1572
1573 return 0;
1574}
1575
1576static int set_channel_variables(struct websocket_pvt *instance)
1577{
1578 char *pkt_size = NULL;
1579 int res = ast_asprintf(&pkt_size, "%d", instance->optimal_frame_size);
1580 if (res <= 0) {
1581 return -1;
1582 }
1583
1585 pkt_size);
1586 ast_free(pkt_size);
1588 instance->connection_id);
1589
1590 return 0;
1591}
1592
1594{
1595 char *params = ast_strdupa(uri_params);
1596 char *nvp = NULL;
1597 char *nv = NULL;
1598
1599 /*
1600 * uri_params should be a comma-separated list of key=value pairs.
1601 * For example:
1602 * name1=value1,name2=value2
1603 * We're verifying that each name and value either doesn't need
1604 * to be encoded or that it already is.
1605 */
1606
1607 while((nvp = ast_strsep(&params, ',', 0))) {
1608 /* nvp will be name1=value1 */
1609 while((nv = ast_strsep(&nvp, '=', 0))) {
1610 /* nv will be either name1 or value1 */
1611 if (!ast_uri_verify_encoded(nv)) {
1612 return 0;
1613 }
1614 }
1615 }
1616
1617 return 1;
1618}
1619
1620enum {
1621 OPT_WS_CODEC = (1 << 0),
1628};
1629
1630enum {
1640
1650
1651static struct ast_channel *webchan_request(const char *type,
1652 struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids,
1653 const struct ast_channel *requestor, const char *data, int *cause)
1654{
1655 char *parse;
1656 RAII_VAR(struct websocket_pvt *, instance, NULL, ao2_cleanup);
1657 struct ast_channel *chan = NULL;
1658 struct ast_format *fmt = NULL;
1659 struct ast_format_cap *caps = NULL;
1661 AST_APP_ARG(connection_id);
1663 );
1664 struct ast_flags opts = { 0, };
1665 char *opt_args[OPT_ARG_ARRAY_SIZE];
1666 const char *requestor_name = requestor ? ast_channel_name(requestor) :
1667 (assignedids && !ast_strlen_zero(assignedids->uniqueid) ? assignedids->uniqueid : "<unknown>");
1668 RAII_VAR(struct webchan_conf_global *, global_cfg, NULL, ao2_cleanup);
1669
1670 global_cfg = ast_sorcery_retrieve_by_id(sorcery, "global", "global");
1671
1672 ast_debug(3, "%s: WebSocket channel requested\n",
1673 requestor_name);
1674
1675 if (ast_strlen_zero(data)) {
1676 ast_log(LOG_ERROR, "%s: A connection id is required for the 'WebSocket' channel\n",
1677 requestor_name);
1678 goto failure;
1679 }
1680 parse = ast_strdupa(data);
1681 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
1682
1683 if (ast_strlen_zero(args.connection_id)) {
1684 ast_log(LOG_ERROR, "%s: connection_id is required for the 'WebSocket' channel\n",
1685 requestor_name);
1686 goto failure;
1687 }
1688
1689 if (!ast_strlen_zero(args.options)
1690 && ast_app_parse_options(websocket_options, &opts, opt_args,
1691 ast_strdupa(args.options))) {
1692 ast_log(LOG_ERROR, "%s: 'WebSocket' channel options '%s' parse error\n",
1693 requestor_name, args.options);
1694 goto failure;
1695 }
1696
1697 if (ast_test_flag(&opts, OPT_WS_CODEC)
1698 && !ast_strlen_zero(opt_args[OPT_ARG_WS_CODEC])) {
1699 fmt = ast_format_cache_get(opt_args[OPT_ARG_WS_CODEC]);
1700 } else {
1701 /*
1702 * If codec wasn't specified in the dial string,
1703 * use the first format in the capabilities.
1704 */
1705 fmt = ast_format_cap_get_format(cap, 0);
1706 }
1707
1708 if (!fmt) {
1709 ast_log(LOG_WARNING, "%s: No codec found for sending media to connection '%s'\n",
1710 requestor_name, args.connection_id);
1711 goto failure;
1712 }
1713
1714 ast_debug(3, "%s: Using format %s from %s\n",
1715 requestor_name, ast_format_get_name(fmt),
1716 ast_test_flag(&opts, OPT_WS_CODEC) ? "dialstring" : "requester");
1717
1718 instance = websocket_new(requestor_name, args.connection_id, fmt);
1719 if (!instance) {
1720 ast_log(LOG_ERROR, "%s: Failed to allocate WebSocket channel pvt\n",
1721 requestor_name);
1722 goto failure;
1723 }
1724
1725 instance->media_direction = WEBCHAN_MEDIA_DIRECTION_BOTH;
1727 if (!strcmp("both", opt_args[OPT_ARG_WS_MEDIA_DIRECTION])) {
1728 /* The default. Don't need to do anything here other than
1729 * ensure it is an allowed value. */
1730 } else if (!strcmp("out", opt_args[OPT_ARG_WS_MEDIA_DIRECTION])) {
1731 instance->media_direction = WEBCHAN_MEDIA_DIRECTION_OUT;
1732 } else if (!strcmp("in", opt_args[OPT_ARG_WS_MEDIA_DIRECTION])) {
1733 instance->media_direction = WEBCHAN_MEDIA_DIRECTION_IN;
1734 } else {
1735 ast_log(LOG_ERROR, "Unrecognized option for media direction: '%s'.\n",
1736 opt_args[OPT_ARG_WS_MEDIA_DIRECTION]);
1737 goto failure;
1738 }
1739 }
1740
1741 instance->no_auto_answer = ast_test_flag(&opts, OPT_WS_NO_AUTO_ANSWER);
1742
1743 /*
1744 * Passthrough requires unbuffered. If passthrough was forced by choice of
1745 * codec, unbuffered will already have been set. If passthrough was set by
1746 * the dialstring option, we need to force unbuffered.
1747 */
1748 if (!instance->passthrough) {
1749 instance->passthrough = ast_test_flag(&opts, OPT_WS_PASSTHROUGH);
1750 instance->unbuffered = instance->passthrough;
1751 }
1752 /*
1753 * If unbuffered hasn't been forced by passthrough, set it according to the
1754 * dialstring "u" option.
1755 */
1756 if (!instance->unbuffered) {
1757 instance->unbuffered = ast_test_flag(&opts, OPT_WS_UNBUFFERED);
1758 }
1759
1761 && !ast_strlen_zero(opt_args[OPT_ARG_WS_URI_PARAM])) {
1762 char *comma;
1763
1764 if (ast_strings_equal(args.connection_id, INCOMING_CONNECTION_ID)) {
1766 "%s: URI parameters are not allowed for 'WebSocket/INCOMING' channels\n",
1767 requestor_name);
1768 goto failure;
1769 }
1770
1771 ast_debug(3, "%s: Using URI parameters '%s'\n",
1772 requestor_name, opt_args[OPT_ARG_WS_URI_PARAM]);
1773
1775 ast_log(LOG_ERROR, "%s: Invalid URI parameters '%s' in WebSocket/%s dial string\n",
1776 requestor_name, opt_args[OPT_ARG_WS_URI_PARAM],
1777 args.connection_id);
1778 goto failure;
1779 }
1780
1781 instance->uri_params = ast_strdup(opt_args[OPT_ARG_WS_URI_PARAM]);
1782 comma = instance->uri_params;
1783 /*
1784 * The normal separator for query string components is an
1785 * ampersand ('&') but the Dial app interprets them as additional
1786 * channels to dial in parallel so we instruct users to separate
1787 * the parameters with commas (',') instead. We now have to
1788 * convert those commas back to ampersands.
1789 */
1790 while ((comma = strchr(comma,','))) {
1791 *comma = '&';
1792 }
1793 ast_debug(3, "%s: Using final URI '%s'\n", requestor_name, instance->uri_params);
1794 }
1795
1796 if (ast_test_flag(&opts, OPT_WS_MSG_FORMAT)) {
1797 instance->control_msg_format = control_msg_format_from_str(opt_args[OPT_ARG_WS_MSG_FORMAT]);
1798
1799 if (instance->control_msg_format == WEBCHAN_CONTROL_MSG_FORMAT_INVALID) {
1800 ast_log(LOG_WARNING, "%s: 'f/control message format' dialstring parameter value missing or invalid. "
1801 "Defaulting to 'plain-text'\n",
1802 requestor_name);
1803 instance->control_msg_format = WEBCHAN_CONTROL_MSG_FORMAT_PLAIN;
1804 }
1805 } else if (global_cfg) {
1806 instance->control_msg_format = global_cfg->control_msg_format;
1807 }
1808
1809 chan = ast_channel_alloc(1, AST_STATE_DOWN, "", "", "", "", "", assignedids,
1810 requestor, 0, "WebSocket/%s/%p", args.connection_id, instance);
1811 if (!chan) {
1812 ast_log(LOG_ERROR, "%s: Unable to alloc channel\n", requestor_name);
1813 goto failure;
1814 }
1815
1816 /* Prevent device state caching as this channel involves ephemeral destinations or sources */
1818 ast_debug(3, "%s: WebSocket channel %s allocated for connection %s\n",
1819 ast_channel_name(chan), requestor_name,
1820 instance->connection_id);
1821
1822 instance->channel = ao2_bump(chan);
1823 ast_channel_tech_set(instance->channel, &websocket_tech);
1824
1825 /* If the application's media direction is 'both' or 'out', we need the channel timer. */
1826 if (instance->media_direction != WEBCHAN_MEDIA_DIRECTION_IN
1827 && set_channel_timer(instance) != 0) {
1828 goto failure;
1829 }
1830
1831 if (set_channel_variables(instance) != 0) {
1832 goto failure;
1833 }
1834
1836 if (!caps) {
1837 ast_log(LOG_ERROR, "%s: Unable to alloc caps\n", requestor_name);
1838 goto failure;
1839 }
1840
1841 ast_format_cap_append(caps, instance->native_format, 0);
1842 ast_channel_nativeformats_set(instance->channel, caps);
1843 ast_channel_set_writeformat(instance->channel, instance->native_format);
1844 ast_channel_set_rawwriteformat(instance->channel, instance->native_format);
1845 ast_channel_set_readformat(instance->channel, instance->native_format);
1846 ast_channel_set_rawreadformat(instance->channel, instance->native_format);
1847 ast_channel_tech_pvt_set(chan, ao2_bump(instance));
1848 ast_channel_unlock(chan);
1849 ao2_cleanup(caps);
1850
1851 ast_debug(3, "%s: WebSocket channel created to %s\n",
1852 ast_channel_name(chan), args.connection_id);
1853
1854 return chan;
1855
1856failure:
1857 if (chan) {
1858 ast_channel_unlock(chan);
1859 }
1860 *cause = AST_CAUSE_FAILURE;
1861 return NULL;
1862}
1863
1864/*!
1865 * \internal
1866 *
1867 * Called by the core to hang up the channel.
1868 */
1869static int webchan_hangup(struct ast_channel *ast)
1870{
1871 struct websocket_pvt *instance = ast_channel_tech_pvt(ast);
1872
1873 if (!instance) {
1874 return -1;
1875 }
1876 ast_debug(3, "%s: WebSocket call hangup. cid: %s\n",
1877 ast_channel_name(ast), instance->connection_id);
1878
1879 if (instance->websocket) {
1881 ast_websocket_unref(instance->websocket);
1882 instance->websocket = NULL;
1883 }
1885
1886 /* Clean up the reference from adding the instance to the channel */
1887 ao2_cleanup(instance);
1888
1889 return 0;
1890}
1891
1892static int webchan_send_dtmf_text(struct ast_channel *ast, char digit, unsigned int duration)
1893{
1894 struct websocket_pvt *instance = ast_channel_tech_pvt(ast);
1895
1896 if (!instance) {
1897 return -1;
1898 }
1899
1900 return send_event(instance, DTMF_END, digit);
1901}
1902
1903/*!
1904 * \internal
1905 *
1906 * Called by res_http_websocket after a client has connected and
1907 * successfully upgraded from HTTP to WebSocket.
1908 *
1909 * Depends on incoming_ws_http_callback parsing the connection_id from
1910 * the HTTP request and storing it in get_params.
1911 */
1912static void incoming_ws_established_cb(struct ast_websocket *ast_ws_session,
1913 struct ast_variable *get_params, struct ast_variable *upgrade_headers)
1914{
1915 RAII_VAR(struct ast_websocket *, s, ast_ws_session, ast_websocket_unref);
1916 struct ast_variable *v;
1917 const char *connection_id = NULL;
1918 struct websocket_pvt *instance = NULL;
1919 struct webchan_conf_global *global_cfg = ast_sorcery_retrieve_by_id(sorcery, "global", "global");
1920 int global_write_timeout = global_cfg ? global_cfg->write_timeout : AST_DEFAULT_WEBSOCKET_WRITE_TIMEOUT;
1921
1922 ao2_cleanup(global_cfg);
1923
1924 ast_debug(3, "WebSocket established\n");
1925
1926 for (v = upgrade_headers; v; v = v->next) {
1927 ast_debug(4, "Header-> %s: %s\n", v->name, v->value);
1928 }
1929 for (v = get_params; v; v = v->next) {
1930 ast_debug(4, " Param-> %s: %s\n", v->name, v->value);
1931 }
1932
1933 connection_id = ast_variable_find_in_list(get_params, "CONNECTION_ID");
1934 if (!connection_id) {
1935 /*
1936 * This can't really happen because websocket_http_callback won't
1937 * let it get this far if it can't add the connection_id to the
1938 * get_params.
1939 * Just in case though...
1940 */
1941 ast_log(LOG_WARNING, "WebSocket connection id not found\n");
1944 return;
1945 }
1946
1947 instance = ao2_weakproxy_find(instances, connection_id, OBJ_SEARCH_KEY | OBJ_NOLOCK, "");
1948 if (!instance) {
1949 /*
1950 * This also can't really happen because websocket_http_callback won't
1951 * let it get this far if it can't find the instance.
1952 * Just in case though...
1953 */
1954 ast_log(LOG_WARNING, "%s: WebSocket instance not found\n", connection_id);
1957 return;
1958 }
1959 instance->websocket = ao2_bump(ast_ws_session);
1960
1961 ast_websocket_set_timeout(instance->websocket, global_write_timeout);
1962
1964 ao2_cleanup(instance);
1965 /*
1966 * The instance is the channel's responsibility now.
1967 * We just return here.
1968 */
1969}
1970
1971/*!
1972 * \internal
1973 *
1974 * Called by the core http server after a client connects but before
1975 * the upgrade from HTTP to Websocket. We need to save the URI in
1976 * the CONNECTION_ID in a get_param because it contains the connection UUID
1977 * we gave to the client when they used externalMedia to create the channel.
1978 * incoming_ws_established_cb() will use this to retrieve the chan_websocket
1979 * instance.
1980 */
1982 const struct ast_http_uri *urih, const char *uri,
1983 enum ast_http_method method, struct ast_variable *get_params,
1984 struct ast_variable *headers)
1985{
1986 struct ast_http_uri fake_urih = {
1988 };
1989 int res = 0;
1990 /*
1991 * Normally the http server will destroy the get_params
1992 * when the session ends but if there weren't any initially
1993 * and we create some and add them to the list, the http server
1994 * won't know about it so we have to destroy it ourselves.
1995 */
1996 int destroy_get_params = (get_params == NULL);
1997 struct ast_variable *v = NULL;
1998 RAII_VAR(struct websocket_pvt *, instance, NULL, ao2_cleanup);
1999
2000 ast_debug(2, "URI: %s Starting\n", uri);
2001
2002 /*
2003 * The client will have issued the GET request with a URI of
2004 * /media/<connection_id>
2005 *
2006 * Since this callback is registered for the /media URI prefix the
2007 * http server will strip that off the front of the URI passing in
2008 * only the path components after that in the 'uri' parameter.
2009 * This should leave only the connection id without a leading '/'.
2010 */
2011 instance = ao2_weakproxy_find(instances, uri, OBJ_SEARCH_KEY | OBJ_NOLOCK, "");
2012 if (!instance) {
2013 ast_log(LOG_WARNING, "%s: WebSocket instance not found\n", uri);
2014 ast_http_error(ser, 404, "Not found", "WebSocket instance not found");
2015 return -1;
2016 }
2017
2018 /*
2019 * We don't allow additional connections using the same connection id.
2020 */
2021 if (instance->websocket) {
2022 ast_log(LOG_WARNING, "%s: Websocket already connected for channel '%s'\n",
2023 uri, instance->channel ? ast_channel_name(instance->channel) : "unknown");
2024 ast_http_error(ser, 409, "Conflict", "Another websocket connection exists for this connection id");
2025 return -1;
2026 }
2027
2028 v = ast_variable_new("CONNECTION_ID", uri, "");
2029 if (!v) {
2030 ast_http_error(ser, 500, "Server error", "");
2031 return -1;
2032 }
2033 ast_variable_list_append(&get_params, v);
2034
2035 for (v = get_params; v; v = v->next) {
2036 ast_debug(4, " Param-> %s: %s\n", v->name, v->value);
2037 }
2038
2039 /*
2040 * This will ultimately call internal_ws_established_cb() so
2041 * this function will block until the websocket is closed and
2042 * internal_ws_established_cb() returns;
2043 */
2044 res = ast_websocket_uri_cb(ser, &fake_urih, uri, method,
2045 get_params, headers);
2046 if (destroy_get_params) {
2047 ast_variables_destroy(get_params);
2048 }
2049
2050 ast_debug(2, "URI: %s DONE\n", uri);
2051
2052 return res;
2053}
2054
2055static struct ast_http_uri http_uri = {
2057 .description = "Media over Websocket",
2058 .uri = "media",
2059 .has_subtree = 1,
2060 .data = NULL,
2061 .key = __FILE__,
2062 .no_decode_uri = 1,
2063};
2064
2068
2070 struct ast_variable *var, void *obj)
2071{
2072 struct webchan_conf_global *cfg = obj;
2073
2075
2077 ast_log(LOG_ERROR, "chan_websocket.conf: Invalid value '%s' for "
2078 "control_mesage_format. Must be 'plain-text' or 'json'\n",
2079 var->value);
2080 return -1;
2081 }
2082
2083 return 0;
2084}
2085
2086static int global_control_message_format_to_str(const void *obj, const intptr_t *args, char **buf)
2087{
2088 const struct webchan_conf_global *cfg = obj;
2089
2091
2092 return 0;
2093}
2094
2095static void *global_alloc(const char *name)
2096{
2098 sizeof(*cfg), NULL);
2099
2100 if (!cfg) {
2101 return NULL;
2102 }
2103
2104 return cfg;
2105}
2106
2107static int global_apply(const struct ast_sorcery *sorcery, void *obj)
2108{
2109 struct webchan_conf_global *cfg = obj;
2110
2111 ast_debug(1, "control_msg_format: %s\n",
2113
2114 if (cfg->write_timeout <= 0) {
2115 ast_log(LOG_WARNING, "The write_timeout parameter must be > 0\n");
2116 return -1;
2117 }
2118
2119 return 0;
2120}
2121
2122static int load_config(void)
2123{
2124 ast_debug(2, "Initializing Websocket Client Configuration\n");
2126 if (!sorcery) {
2127 ast_log(LOG_ERROR, "Failed to open sorcery\n");
2128 return -1;
2129 }
2130
2131 ast_sorcery_apply_default(sorcery, "global", "config",
2132 "chan_websocket.conf,criteria=type=global,single_object=yes,explicit_name=global");
2133
2135 ast_log(LOG_ERROR, "Failed to register chan_websocket global object with sorcery\n");
2137 sorcery = NULL;
2138 return -1;
2139 }
2140
2141 ast_sorcery_object_field_register_nodoc(sorcery, "global", "type", "", OPT_NOOP_T, 0, 0);
2142 ast_sorcery_register_cust(global, control_message_format, "plain-text");
2145
2147
2148 return 0;
2149}
2150
2151/*! \brief Function called when our module is unloaded */
2170
2171static int reload_module(void)
2172{
2173 ast_debug(2, "Reloading chan_websocket configuration\n");
2175
2176 return 0;
2177}
2178
2179/*! \brief Function called when our module is loaded */
2180static int load_module(void)
2181{
2182 int res = 0;
2183 struct ast_websocket_protocol *protocol;
2184
2185 res = load_config();
2186 if (res != 0) {
2188 }
2189
2192 }
2193
2196 ast_log(LOG_ERROR, "Unable to register channel class 'WebSocket'\n");
2197 unload_module();
2199 }
2200
2202 AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE, 17, instance_proxy_hash_fn,
2203 instance_proxy_sort_fn, instance_proxy_cmp_fn);
2204 if (!instances) {
2206 "Failed to allocate the chan_websocket instance registry\n");
2207 unload_module();
2209 }
2210
2212 if (!ast_ws_server) {
2213 unload_module();
2215 }
2216
2217 protocol = ast_websocket_sub_protocol_alloc("media");
2218 if (!protocol) {
2219 unload_module();
2221 }
2224
2226
2228}
2229
2230AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Websocket Media Channel",
2231 .support_level = AST_MODULE_SUPPORT_CORE,
2232 .load = load_module,
2233 .unload = unload_module,
2235 .load_pri = AST_MODPRI_CHANNEL_DRIVER,
2236 .requires = "res_http_websocket,res_websocket_client",
char digit
enum queue_result id
Definition app_queue.c:1790
#define var
Definition ast_expr2f.c:605
Asterisk main include file. File version handling, generic pbx functions.
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition astmm.h:288
#define ast_free(a)
Definition astmm.h:180
#define ast_strdup(str)
A wrapper for strdup()
Definition astmm.h:241
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition astmm.h:298
#define ast_vasprintf(ret, fmt, ap)
A wrapper for vasprintf()
Definition astmm.h:278
#define ast_asprintf(ret, fmt,...)
A wrapper for asprintf()
Definition astmm.h:267
#define ast_calloc(num, len)
A wrapper for calloc()
Definition astmm.h:202
#define ast_log
Definition astobj2.c:42
#define ao2_weakproxy_set_object(weakproxy, obj, flags)
Associate weakproxy with obj.
Definition astobj2.h:579
int ao2_weakproxy_subscribe(void *weakproxy, ao2_weakproxy_notification_cb cb, void *data, int flags)
Request notification when weakproxy points to NULL.
Definition astobj2.c:934
@ AO2_ALLOC_OPT_LOCK_RWLOCK
Definition astobj2.h:365
#define AO2_STRING_FIELD_CMP_FN(stype, field)
Creates a compare function for a structure string field.
Definition astobj2.h:2048
#define ao2_cleanup(obj)
Definition astobj2.h:1934
#define ao2_unlink(container, obj)
Remove an object from a container.
Definition astobj2.h:1578
#define ao2_link_flags(container, obj, flags)
Add an object to a container.
Definition astobj2.h:1554
#define AO2_STRING_FIELD_SORT_FN(stype, field)
Creates a sort function for a structure string field.
Definition astobj2.h:2064
#define ao2_weakproxy_find(c, arg, flags, tag)
Perform an ao2_find on a container with ao2_weakproxy objects, returning the real object.
Definition astobj2.h:1748
#define ao2_bump(obj)
Bump refcount on an AO2 object by one, returning the object.
Definition astobj2.h:480
#define ao2_weakproxy_alloc(data_size, destructor_fn)
Allocate an ao2_weakproxy object.
Definition astobj2.h:550
#define AO2_STRING_FIELD_HASH_FN(stype, field)
Creates a hash function for a structure string field.
Definition astobj2.h:2032
@ OBJ_NOLOCK
Assume that the ao2_container is already locked.
Definition astobj2.h:1063
@ OBJ_SEARCH_KEY
The arg parameter is a search key, but is not an object.
Definition astobj2.h:1101
#define ao2_alloc(data_size, destructor_fn)
Definition astobj2.h:409
#define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn)
Allocate and initialize a hash container with the desired number of buckets.
Definition astobj2.h:1303
@ AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE
Replace objects with duplicate keys in container.
Definition astobj2.h:1211
Internal Asterisk hangup causes.
#define AST_CAUSE_FAILURE
Definition causes.h:150
#define AST_CAUSE_NORMAL
Definition causes.h:151
#define AST_CAUSE_NETWORK_OUT_OF_ORDER
Definition causes.h:121
#define AST_CAUSE_NO_ROUTE_DESTINATION
Definition causes.h:100
static PGresult * result
Definition cel_pgsql.c:84
static const char type[]
#define ERROR_ON_PASSTHROUGH_MODE_RTN(instance, command)
#define FLUSH_MEDIA
static void _websocket_request_hangup(struct websocket_pvt *instance, int ast_cause, enum ast_websocket_status_code tech_cause, int line, const char *function)
#define QUEUE_LENGTH_XON_LEVEL
#define MARK_MEDIA
#define send_event(_instance, _event,...)
Use this macro to create and send events passing in any event-specific parameters.
static int incoming_ws_http_callback(struct ast_tcptls_session_instance *ser, const struct ast_http_uri *urih, const char *uri, enum ast_http_method method, struct ast_variable *get_params, struct ast_variable *headers)
#define ANSWER_CHANNEL
static void instance_proxy_cb(void *weakproxy, void *data)
static struct ast_frame * dequeue_frame(struct websocket_pvt *instance)
static void * global_alloc(const char *name)
@ OPT_WS_MEDIA_DIRECTION
@ OPT_WS_CODEC
@ OPT_WS_PASSTHROUGH
@ OPT_WS_URI_PARAM
@ OPT_WS_MSG_FORMAT
@ OPT_WS_NO_AUTO_ANSWER
@ OPT_WS_UNBUFFERED
static const char * control_msg_format_to_str(enum webchan_control_msg_format value)
static int validate_uri_parameters(const char *uri_params)
static int webchan_write(struct ast_channel *ast, struct ast_frame *f)
Function called when we should write a frame to the channel.
static char * _create_event_MEDIA_BUFFERING_COMPLETED(struct websocket_pvt *instance, const char *id)
static const char * msg_format_map[]
static int webchan_hangup(struct ast_channel *ast)
static struct ast_channel_tech websocket_tech
#define HANGUP_CHANNEL
static struct ast_frame * webchan_read(struct ast_channel *ast)
#define websocket_request_hangup(_instance, _cause, _tech)
#define MEDIA_WEBSOCKET_CONNECTION_ID
#define WS_WEBSOCKET_FDNO
static char * _create_event_MEDIA_MARK_PROCESSED(struct websocket_pvt *instance, const char *id)
static int read_from_ws_and_queue(struct websocket_pvt *instance)
webchan_control_msg_format
@ WEBCHAN_CONTROL_MSG_FORMAT_JSON
@ WEBCHAN_CONTROL_MSG_FORMAT_PLAIN
@ WEBCHAN_CONTROL_MSG_FORMAT_INVALID
static int handle_command(struct websocket_pvt *instance, char *buffer)
static int global_control_message_format_from_str(const struct aco_option *opt, struct ast_variable *var, void *obj)
static int webchan_send_dtmf_text(struct ast_channel *ast, char digit, unsigned int duration)
static struct ast_http_uri http_uri
static int reload_module(void)
#define GET_DRIVER_STATUS
static int set_channel_variables(struct websocket_pvt *instance)
webchan_media_direction
@ WEBCHAN_MEDIA_DIRECTION_BOTH
@ WEBCHAN_MEDIA_DIRECTION_OUT
@ WEBCHAN_MEDIA_DIRECTION_IN
static int process_text_message(struct websocket_pvt *instance, char *payload, uint64_t payload_len)
static void websocket_destructor(void *data)
static int queue_frame_from_buffer(struct websocket_pvt *instance, char *buffer, size_t len)
#define WS_TIMER_FDNO
static struct ast_sorcery * sorcery
#define ERROR_ON_INVALID_MEDIA_DIRECTION_RTN(instance, command, direction)
#define START_MEDIA_BUFFERING
static int set_channel_timer(struct websocket_pvt *instance)
static struct ast_frame * create_frame_from_buffer(struct websocket_pvt *instance, char *buffer, size_t len)
static char * _create_event_STATUS(struct websocket_pvt *instance)
static char * _create_event_DTMF_END(struct websocket_pvt *instance, const char digit)
#define QUEUE_LENGTH_XOFF_LEVEL
static const struct ast_app_option websocket_options[128]
#define PAUSE_MEDIA
#define create_event(_instance, _event,...)
Use this macro to create events passing in any event-specific parameters.
static struct ao2_container * instances
static char * _create_event_ERROR(struct websocket_pvt *instance, const char *format,...)
#define MEDIA_WEBSOCKET_OPTIMAL_FRAME_SIZE
#define INCOMING_CONNECTION_ID
static struct websocket_pvt * websocket_new(const char *chan_name, const char *connection_id, struct ast_format *fmt)
#define REPORT_QUEUE_DRAINED
#define CONTINUE_MEDIA
#define STOP_MEDIA_BUFFERING
#define ERROR_ON_UNBUFFERED_MODE_RTN(instance, command)
static int load_module(void)
Function called when our module is loaded.
static const char * websocket_media_direction_map[]
static int webchan_call(struct ast_channel *ast, const char *dest, int timeout)
static enum webchan_control_msg_format control_msg_format_from_str(const char *value)
static int process_binary_message(struct websocket_pvt *instance, char *payload, uint64_t payload_len)
static struct ast_websocket_server * ast_ws_server
#define SET_MEDIA_DIRECTION
static char * _create_event_MEDIA_START(struct websocket_pvt *instance)
static int unload_module(void)
Function called when our module is unloaded.
static int global_control_message_format_to_str(const void *obj, const intptr_t *args, char **buf)
#define MAX_TEXT_MESSAGE_LEN
static void incoming_ws_established_cb(struct ast_websocket *ast_ws_session, struct ast_variable *get_params, struct ast_variable *upgrade_headers)
#define QUEUE_LENGTH_MAX
static int websocket_handoff_to_channel(struct websocket_pvt *instance)
static struct ast_channel * webchan_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause)
static char * _create_event_nodata(struct websocket_pvt *instance, char *event)
static int load_config(void)
static int global_apply(const struct ast_sorcery *sorcery, void *obj)
static int queue_option_frame(struct websocket_pvt *instance, char *buffer)
@ OPT_ARG_WS_PASSTHROUGH
@ OPT_ARG_WS_URI_PARAM
@ OPT_ARG_WS_MEDIA_DIRECTION
@ OPT_ARG_WS_MSG_FORMAT
@ OPT_ARG_WS_CODEC
@ OPT_ARG_WS_NO_AUTO_ANSWER
@ OPT_ARG_WS_UNBUFFERED
@ OPT_ARG_ARRAY_SIZE
General Asterisk PBX channel definitions.
const char * ast_channel_name(const struct ast_channel *chan)
int ast_channel_tech_hangupcause(const struct ast_channel *chan)
void * ast_channel_tech_pvt(const struct ast_channel *chan)
void ast_channel_tech_hangupcause_set(struct ast_channel *chan, int value)
struct varshead * ast_channel_varshead(struct ast_channel *chan)
#define ast_channel_alloc(needqueue, state, cid_num, cid_name, acctcode, exten, context, assignedids, requestor, amaflag,...)
Create a channel structure.
Definition channel.h:1299
void ast_channel_nativeformats_set(struct ast_channel *chan, struct ast_format_cap *value)
int ast_channel_fdno(const struct ast_channel *chan)
@ AST_FLAG_DISABLE_DEVSTATE_CACHE
Definition channel.h:1049
void ast_channel_unregister(const struct ast_channel_tech *tech)
Unregister a channel technology.
Definition channel.c:571
int ast_queue_control(struct ast_channel *chan, enum ast_control_frame_type control)
Queue a control frame without payload.
Definition channel.c:1289
struct ast_flags * ast_channel_flags(struct ast_channel *chan)
int ast_queue_frame(struct ast_channel *chan, struct ast_frame *f)
Queue one or more frames to a channel's frame queue.
Definition channel.c:1171
const char * ast_channel_uniqueid(const struct ast_channel *chan)
int ast_queue_hangup_with_cause(struct ast_channel *chan, int cause)
Queue a hangup frame with hangupcause set.
Definition channel.c:1213
void ast_channel_set_rawreadformat(struct ast_channel *chan, struct ast_format *format)
void ast_channel_tech_pvt_set(struct ast_channel *chan, void *value)
void ast_channel_set_rawwriteformat(struct ast_channel *chan, struct ast_format *format)
void ast_channel_set_readformat(struct ast_channel *chan, struct ast_format *format)
int ast_channel_register(const struct ast_channel_tech *tech)
Register a channel technology (a new channel driver) Called by a channel module to register the kind ...
Definition channel.c:540
#define ast_channel_unref(c)
Decrease channel reference count.
Definition channel.h:3019
const char * ast_cause2str(int cause) attribute_pure
Gives the string form of a given cause code.
Definition channel.c:613
void ast_channel_set_fd(struct ast_channel *chan, int which, int fd)
Definition channel.c:2417
void ast_channel_internal_fd_clear(struct ast_channel *chan, int which)
void ast_channel_hangupcause_set(struct ast_channel *chan, int value)
void ast_channel_tech_set(struct ast_channel *chan, const struct ast_channel_tech *value)
#define ast_channel_unlock(chan)
Definition channel.h:2984
void ast_channel_set_writeformat(struct ast_channel *chan, struct ast_format *format)
@ AST_STATE_DOWN
Codec API.
@ AST_MEDIA_TYPE_UNKNOWN
Definition codec.h:31
@ OPT_NOOP_T
Type for a default handler that should do nothing.
char buf[BUFSIZE]
Definition eagi_proxy.c:66
struct ast_codec * ast_format_get_codec(const struct ast_format *format)
Get the codec associated with a format.
Definition format.c:324
unsigned int ast_format_get_minimum_bytes(const struct ast_format *format)
Get the minimum number of bytes expected in a frame for this format.
Definition format.c:374
unsigned int ast_format_get_sample_rate(const struct ast_format *format)
Get the sample rate of a media format.
Definition format.c:379
unsigned int ast_format_get_minimum_ms(const struct ast_format *format)
Get the minimum amount of media carried in this format.
Definition format.c:364
enum ast_format_cmp_res ast_format_cmp(const struct ast_format *format1, const struct ast_format *format2)
Compare two formats.
Definition format.c:201
@ AST_FORMAT_CMP_NOT_EQUAL
Definition format.h:38
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition format.c:334
unsigned int ast_format_get_default_ms(const struct ast_format *format)
Get the default framing size (in milliseconds) for a format.
Definition format.c:359
Media Format Cache API.
#define ast_format_cache_get(name)
Retrieve a named format from the cache.
int ast_format_cap_append_by_type(struct ast_format_cap *cap, enum ast_media_type type)
Add all codecs Asterisk knows about for a specific type to the capabilities structure.
Definition format_cap.c:216
struct ast_format * ast_format_cap_get_format(const struct ast_format_cap *cap, int position)
Get the format at a specific index.
Definition format_cap.c:408
@ AST_FORMAT_CAP_FLAG_DEFAULT
Definition format_cap.h:38
#define ast_format_cap_append(cap, format, framing)
Add format capability to capabilities structure.
Definition format_cap.h:99
#define ast_format_cap_alloc(flags)
Allocate a new ast_format_cap structure.
Definition format_cap.h:49
static const char name[]
Definition format_mp3.c:68
direction
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
static int uuid(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition func_uuid.c:52
ast_http_method
HTTP Request methods known by Asterisk.
Definition http.h:58
void ast_http_uri_unlink(struct ast_http_uri *urihandler)
Unregister a URI handler.
Definition http.c:779
void ast_http_error(struct ast_tcptls_session_instance *ser, int status, const char *title, const char *text)
Send HTTP error message and close socket.
Definition http.c:722
int ast_http_uri_link(struct ast_http_uri *urihandler)
Register a URI handler.
Definition http.c:747
Support for WebSocket connections within the Asterisk HTTP server and client WebSocket connections to...
int AST_OPTIONAL_API_NAME() ast_websocket_write(struct ast_websocket *session, enum ast_websocket_opcode opcode, char *payload, uint64_t payload_size)
Construct and transmit a WebSocket frame.
ast_websocket_status_code
Websocket Status Codes from RFC-6455.
@ AST_WEBSOCKET_STATUS_UNSUPPORTED_DATA
@ AST_WEBSOCKET_STATUS_NORMAL
@ AST_WEBSOCKET_STATUS_GOING_AWAY
@ AST_WEBSOCKET_STATUS_INTERNAL_ERROR
int AST_OPTIONAL_API_NAME() ast_websocket_server_add_protocol2(struct ast_websocket_server *server, struct ast_websocket_protocol *protocol)
Add a sub-protocol handler to the given server.
int AST_OPTIONAL_API_NAME() ast_websocket_write_string(struct ast_websocket *ws, const char *buf)
Construct and transmit a WebSocket frame containing string data.
void AST_OPTIONAL_API_NAME() ast_websocket_reconstruct_enable(struct ast_websocket *session, size_t bytes)
Enable multi-frame reconstruction up to a certain number of bytes.
struct ast_sockaddr *AST_OPTIONAL_API_NAME() ast_websocket_remote_address(struct ast_websocket *session)
Get the remote address for a WebSocket connected session.
int AST_OPTIONAL_API_NAME() ast_websocket_uri_cb(struct ast_tcptls_session_instance *ser, const struct ast_http_uri *urih, const char *uri, enum ast_http_method method, struct ast_variable *get_vars, struct ast_variable *headers)
Callback suitable for use with a ast_http_uri.
ast_websocket_result
Result code for a websocket client.
@ WS_OK
int AST_OPTIONAL_API_NAME() ast_websocket_fd(struct ast_websocket *session)
Get the file descriptor for a WebSocket session.
#define AST_DEFAULT_WEBSOCKET_WRITE_TIMEOUT
Default websocket write timeout, in ms.
ast_websocket_opcode
WebSocket operation codes.
@ AST_WEBSOCKET_OPCODE_PING
@ AST_WEBSOCKET_OPCODE_PONG
@ AST_WEBSOCKET_OPCODE_CONTINUATION
@ AST_WEBSOCKET_OPCODE_BINARY
@ AST_WEBSOCKET_OPCODE_CLOSE
@ AST_WEBSOCKET_OPCODE_TEXT
int AST_OPTIONAL_API_NAME() ast_websocket_set_nonblock(struct ast_websocket *session)
Set the socket of a WebSocket session to be non-blocking.
const char *AST_OPTIONAL_API_NAME() ast_websocket_status_to_str(enum ast_websocket_status_code code)
Convert a websocket status code to a string.
ast_websocket_type
WebSocket connection/configuration types.
@ AST_WS_TYPE_CLIENT
@ AST_WS_TYPE_SERVER
struct ast_websocket_protocol *AST_OPTIONAL_API_NAME() ast_websocket_sub_protocol_alloc(const char *name)
Allocate a websocket sub-protocol instance.
int AST_OPTIONAL_API_NAME() ast_websocket_set_timeout(struct ast_websocket *session, int timeout)
Set the timeout on a non-blocking WebSocket session.
int AST_OPTIONAL_API_NAME() ast_websocket_read(struct ast_websocket *session, char **payload, uint64_t *payload_len, enum ast_websocket_opcode *opcode, int *fragmented)
Read a WebSocket frame and handle it.
int AST_OPTIONAL_API_NAME() ast_websocket_close(struct ast_websocket *session, uint16_t reason)
Close a WebSocket session by sending a message with the CLOSE opcode and an optional code.
struct ast_websocket_server *AST_OPTIONAL_API_NAME() ast_websocket_server_create(void)
Creates a ast_websocket_server.
void AST_OPTIONAL_API_NAME() ast_websocket_unref(struct ast_websocket *session)
Decrease the reference count for a WebSocket session.
const char *AST_OPTIONAL_API_NAME() ast_websocket_result_to_str(enum ast_websocket_result result)
Convert a websocket result code to a string.
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#define AST_APP_ARG(name)
Define an application argument.
#define END_OPTIONS
#define AST_APP_OPTIONS(holder, options...)
Declares an array of options for an application.
#define AST_APP_OPTION_ARG(option, flagno, argno)
Declares an application option that accepts an argument.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define BEGIN_OPTIONS
#define AST_APP_OPTION(option, flagno)
Declares an application option that does not accept an argument.
#define AST_NONSTANDARD_APP_ARGS(args, parse, sep)
Performs the 'nonstandard' argument separation process for an application.
int ast_app_parse_options(const struct ast_app_option *options, struct ast_flags *flags, char **args, char *optstr)
Parses a string containing application options and sets flags/arguments.
Definition main/app.c:3067
const char * ast_variable_find_in_list(const struct ast_variable *list, const char *variable)
Gets the value of a variable from a variable list by name.
#define ast_variable_new(name, value, filename)
#define ast_variable_list_append(head, new_var)
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Definition extconf.c:1260
Asterisk internal frame definitions.
#define ast_frisolate(fr)
Makes a frame independent of any static storage.
void ast_frame_free(struct ast_frame *frame, int cache)
Frees a frame or list of frames.
Definition main/frame.c:176
#define ast_frfree(fr)
#define AST_FRAME_SET_BUFFER(fr, _base, _ofs, _datalen)
@ AST_FRAME_CONTROL
@ AST_CONTROL_ANSWER
@ AST_CONTROL_OPTION
struct ast_frame ast_null_frame
Definition main/frame.c:79
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_ERROR
#define LOG_WARNING
Asterisk JSON abstraction layer.
#define ast_json_object_string_get(object, key)
Get a string field from a JSON object.
Definition json.h:600
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition json.c:73
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition json.c:612
@ AST_JSON_COMPACT
Definition json.h:793
struct ast_json * ast_json_load_buf(const char *buffer, size_t buflen, struct ast_json_error *error)
Parse buffer with known length into a JSON object or array.
Definition json.c:585
struct ast_json * ast_json_channel_vars(struct varshead *channelvars)
Construct a JSON object from a ast_var_t list.
Definition json.c:941
char * ast_json_dump_string_format(struct ast_json *root, enum ast_json_encoding_format format)
Encode a JSON value to a string.
Definition json.c:484
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
#define AST_LIST_HEAD_INIT(head)
Initializes a list head structure.
#define AST_LIST_LOCK(head)
Locks a list.
Definition linkedlists.h:40
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
#define AST_LIST_UNLOCK(head)
Attempts to unlock a list.
#define AST_LIST_HEAD(name, type)
Defines a structure to be used to hold a list of specified type.
Asterisk locking-related definitions:
#define SCOPED_AO2WRLOCK(varname, obj)
scoped lock specialization for ao2 write locks.
Definition lock.h:621
#define SCOPED_LOCK(varname, lock, lockfunc, unlockfunc)
Scoped Locks.
Definition lock.h:590
int errno
Header for providers of file and format handling routines. Clients of these routines should include "...
Asterisk module definitions.
@ AST_MODFLAG_LOAD_ORDER
Definition module.h:331
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition module.h:557
@ AST_MODPRI_CHANNEL_DRIVER
Definition module.h:341
@ AST_MODULE_SUPPORT_CORE
Definition module.h:121
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition module.h:46
@ AST_MODULE_LOAD_SUCCESS
Definition module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition module.h:78
static char * ast_sockaddr_stringify(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() with default format.
Definition netsock2.h:256
Core PBX routines and definitions.
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name.
static int reload(void)
const char * method
Definition res_pjsip.c:1277
static struct @523 args
#define NULL
Definition resample.c:96
Sorcery Data Access Layer API.
#define ast_sorcery_unref(sorcery)
Decrease the reference count of a sorcery structure.
Definition sorcery.h:1500
#define ast_sorcery_object_field_register_nodoc(sorcery, type, name, default_val, opt_type, flags,...)
Register a field within an object without documentation.
Definition sorcery.h:987
#define ast_sorcery_register_cust(object, option, def_value)
Register a custom field within an object.
Definition sorcery.h:1767
void ast_sorcery_load(const struct ast_sorcery *sorcery)
Inform any wizards to load persistent objects.
Definition sorcery.c:1441
void * ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id)
Retrieve an object using its unique identifier.
Definition sorcery.c:1917
#define ast_sorcery_object_register(sorcery, type, alloc, transform, apply)
Register an object type.
Definition sorcery.h:837
void ast_sorcery_reload(const struct ast_sorcery *sorcery)
Inform any wizards to reload persistent objects.
Definition sorcery.c:1472
void * ast_sorcery_generic_alloc(size_t size, ao2_destructor_fn destructor)
Allocate a generic sorcery capable object.
Definition sorcery.c:1792
#define ast_sorcery_register_int(object, structure, option, field, def_value)
Register an int field as type OPT_INT_T within an object.
Definition sorcery.h:1710
#define ast_sorcery_apply_default(sorcery, type, name, data)
Definition sorcery.h:476
#define ast_sorcery_open()
Open a new sorcery structure.
Definition sorcery.h:406
int ast_strings_equal(const char *str1, const char *str2)
Compare strings for equality checking for NULL.
Definition strings.c:238
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one.
Definition strings.h:80
#define S_COR(a, b, c)
returns the equivalent of logic or for strings, with an additional boolean check: second one if not e...
Definition strings.h:87
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition strings.h:65
char * ast_strip(char *s)
Strip leading/trailing whitespace from a string.
Definition strings.h:223
char * ast_strsep(char **s, const char sep, uint32_t flags)
Act like strsep but ignore separators inside quotes.
Definition utils.c:1869
Generic container type.
Structure to pass both assignedid values to channel drivers.
Definition channel.h:606
const char * uniqueid
Definition channel.h:607
Structure to describe a channel "technology", ie a channel driver See for examples:
Definition channel.h:648
struct ast_format_cap * capabilities
Definition channel.h:652
const char *const type
Definition channel.h:649
Main Channel structure associated with a channel.
const char * data
Represents a media codec within Asterisk.
Definition codec.h:42
unsigned int default_ms
Default length of media carried (in milliseconds) in a frame.
Definition codec.h:58
int(* samples_count)(struct ast_frame *frame)
Retrieve the number of samples in a frame.
Definition codec.h:68
Structure used to handle boolean flags.
Definition utils.h:220
Format capabilities structure, holds formats + preference order + etc.
Definition format_cap.c:54
Definition of a media format.
Definition format.c:43
struct ast_format * format
Data structure associated with a single frame of data.
struct ast_frame_subclass subclass
enum ast_frame_type frametype
union ast_frame::@237 data
Definition of a URI handler.
Definition http.h:102
ast_http_callback callback
Definition http.h:107
void * data
Definition http.h:116
JSON parsing error information.
Definition json.h:887
Abstract JSON element (object, array, string, int, ...).
Socket address structure.
Definition netsock2.h:97
Full structure for sorcery.
Definition sorcery.c:231
describes a server instance
Definition tcptls.h:151
Structure for variables, used for configurations and for channel variables.
struct ast_variable * next
A websocket protocol implementation.
ast_websocket_callback session_established
Callback called when a new session is established. Mandatory.
Structure for a WebSocket server.
Structure definition for session.
char connection_id[0]
The name of the module owning this sorcery instance.
enum webchan_control_msg_format control_msg_format
char connection_id[0]
struct ast_format * native_format
struct ast_channel * channel
enum webchan_control_msg_format control_msg_format
struct websocket_pvt::@141 frame_queue
enum ast_websocket_type type
struct ast_timer * timer
pthread_t outbound_read_thread
struct ast_codec * native_codec
struct ast_websocket_client * client
struct ast_websocket * websocket
int value
Definition syslog.c:37
static struct aco_type global
static struct test_options options
Timing source management.
void ast_timer_close(struct ast_timer *handle)
Close an opened timing handle.
Definition timing.c:154
int ast_timer_ack(const struct ast_timer *handle, unsigned int quantity)
Acknowledge a timer event.
Definition timing.c:171
int ast_timer_set_rate(const struct ast_timer *handle, unsigned int rate)
Set the timing tick rate.
Definition timing.c:166
enum ast_timer_event ast_timer_get_event(const struct ast_timer *handle)
Retrieve timing event.
Definition timing.c:186
struct ast_timer * ast_timer_open(void)
Open a timer.
Definition timing.c:122
@ AST_TIMING_EVENT_EXPIRED
Definition timing.h:58
int ast_timer_fd(const struct ast_timer *handle)
Get a poll()-able file descriptor for a timer.
Definition timing.c:161
Support for translation of data formats. translate.c.
#define ast_test_flag(p, flag)
Definition utils.h:64
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition utils.h:981
#define MIN(a, b)
Definition utils.h:252
#define ARRAY_IN_BOUNDS(v, a)
Checks to see if value is within the bounds of the given array.
Definition utils.h:727
#define ast_set_flag(p, flag)
Definition utils.h:71
int ast_uri_verify_encoded(const char *string)
Verify if a string is valid as a URI component.
Definition utils.c:779
Universally unique identifier support.
#define AST_UUID_STR_LEN
Definition uuid.h:27
char * ast_uuid_generate_str(char *buf, size_t size)
Generate a UUID string.
Definition uuid.c:141
void ast_websocket_client_add_uri_params(struct ast_websocket_client *wc, const char *uri_params)
Add additional parameters to the URI.
struct ast_websocket_client * ast_websocket_client_retrieve_by_id(const char *id)
Retrieve a websocket client object by ID.
struct ast_websocket * ast_websocket_client_connect(struct ast_websocket_client *wc, void *lock_obj, const char *display_name, enum ast_websocket_result *result)
Connect to a websocket server using the configured authentication, retry and TLS options.