Asterisk - The Open Source Telephony Project GIT-master-f36a736
chan_audiosocket.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2019, CyCore Systems, Inc
5 *
6 * Seán C McCord <scm@cycoresys.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 Seán C McCord <scm@cycoresys.com>
22 *
23 * \brief AudioSocket Channel
24 *
25 * \ingroup channel_drivers
26 */
27
28/*** MODULEINFO
29 <depend>res_audiosocket</depend>
30 <support_level>extended</support_level>
31 ***/
32
33#include "asterisk.h"
34#include <uuid/uuid.h>
35
36#include "asterisk/channel.h"
37#include "asterisk/module.h"
39#include "asterisk/pbx.h"
40#include "asterisk/acl.h"
41#include "asterisk/app.h"
42#include "asterisk/causes.h"
44
45#define FD_OUTPUT 1 /* A fd of -1 means an error, 0 is stdin */
46
48 int svc; /* The file descriptor for the AudioSocket instance */
49 char id[38]; /* The UUID identifying this AudioSocket instance */
51
52/* Forward declarations */
53static struct ast_channel *audiosocket_request(const char *type,
54 struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids,
55 const struct ast_channel *requestor, const char *data, int *cause);
56static int audiosocket_call(struct ast_channel *ast, const char *dest, int timeout);
57static int audiosocket_hangup(struct ast_channel *ast);
58static struct ast_frame *audiosocket_read(struct ast_channel *ast);
59static int audiosocket_write(struct ast_channel *ast, struct ast_frame *f);
60
61/* AudioSocket channel driver declaration */
63 .type = "AudioSocket",
64 .description = "AudioSocket Channel Driver",
65 .requester = audiosocket_request,
66 .call = audiosocket_call,
67 .hangup = audiosocket_hangup,
68 .read = audiosocket_read,
69 .write = audiosocket_write,
70};
71
72/*! \brief Function called when we should read a frame from the channel */
73static struct ast_frame *audiosocket_read(struct ast_channel *ast)
74{
75 struct audiosocket_instance *instance;
76
77 /* The channel should always be present from the API */
78 instance = ast_channel_tech_pvt(ast);
79 if (instance == NULL || instance->svc < FD_OUTPUT) {
80 return NULL;
81 }
82 return ast_audiosocket_receive_frame(instance->svc);
83}
84
85/*! \brief Function called when we should write a frame to the channel */
86static int audiosocket_write(struct ast_channel *ast, struct ast_frame *f)
87{
88 struct audiosocket_instance *instance;
89
90 /* The channel should always be present from the API */
91 instance = ast_channel_tech_pvt(ast);
92 if (instance == NULL || instance->svc < 1) {
93 return -1;
94 }
95 return ast_audiosocket_send_frame(instance->svc, f);
96}
97
98/*! \brief Function called when we should actually call the destination */
99static int audiosocket_call(struct ast_channel *ast, const char *dest, int timeout)
100{
101 struct audiosocket_instance *instance = ast_channel_tech_pvt(ast);
102
104
105 return ast_audiosocket_init(instance->svc, instance->id);
106}
107
108/*! \brief Function called when we should hang the channel up */
109static int audiosocket_hangup(struct ast_channel *ast)
110{
111 struct audiosocket_instance *instance;
112
113 /* The channel should always be present from the API */
114 instance = ast_channel_tech_pvt(ast);
115 if (instance != NULL && instance->svc > 0) {
116 close(instance->svc);
117 }
118
120 ast_free(instance);
121
122 return 0;
123}
124
125enum {
127};
128
129enum {
133
137
138/*! \brief Function called when we should prepare to call the unicast destination */
139static struct ast_channel *audiosocket_request(const char *type,
140 struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids,
141 const struct ast_channel *requestor, const char *data, int *cause)
142{
143 char *parse;
144 struct audiosocket_instance *instance = NULL;
145 struct ast_sockaddr address;
146 struct ast_channel *chan;
147 struct ast_format_cap *caps = NULL;
148 struct ast_format *fmt = NULL;
149 uuid_t uu;
150 int fd = -1;
152 AST_APP_ARG(destination);
153 AST_APP_ARG(idStr);
155 );
156 struct ast_flags opts = { 0, };
157 char *opt_args[OPT_ARG_ARRAY_SIZE];
158
159 if (ast_strlen_zero(data)) {
160 ast_log(LOG_ERROR, "Destination is required for the 'AudioSocket' channel\n");
161 goto failure;
162 }
163 parse = ast_strdupa(data);
164 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
165
166 if (ast_strlen_zero(args.destination)) {
167 ast_log(LOG_ERROR, "Destination is required for the 'AudioSocket' channel\n");
168 goto failure;
169 }
171 (&address, args.destination, PARSE_PORT_REQUIRE, AST_AF_UNSPEC)) {
172 ast_log(LOG_ERROR, "Destination '%s' could not be parsed\n", args.destination);
173 goto failure;
174 }
175
176 if (ast_strlen_zero(args.idStr)) {
177 ast_log(LOG_ERROR, "UUID is required for the 'AudioSocket' channel\n");
178 goto failure;
179 }
180 if (uuid_parse(args.idStr, uu)) {
181 ast_log(LOG_ERROR, "Failed to parse UUID '%s'\n", args.idStr);
182 goto failure;
183 }
184
185 if (!ast_strlen_zero(args.options)
186 && ast_app_parse_options(audiosocket_options, &opts, opt_args,
187 ast_strdupa(args.options))) {
188 ast_log(LOG_ERROR, "'AudioSocket' channel options '%s' parse error\n",
189 args.options);
190 goto failure;
191 }
192
196 if (!fmt) {
197 ast_log(LOG_ERROR, "Codec '%s' not found for AudioSocket connection to '%s'\n",
198 opt_args[OPT_ARG_AUDIOSOCKET_CODEC], args.destination);
199 goto failure;
200 }
201 } else {
202 fmt = ast_format_cap_get_format(cap, 0);
203 if (!fmt) {
204 ast_log(LOG_ERROR, "No codec available for AudioSocket connection to '%s'\n",
205 args.destination);
206 goto failure;
207 }
208 }
209
211 if (!caps) {
212 goto failure;
213 }
214
215 instance = ast_calloc(1, sizeof(*instance));
216 if (!instance) {
217 ast_log(LOG_ERROR, "Failed to allocate AudioSocket channel pvt\n");
218 goto failure;
219 }
220 ast_copy_string(instance->id, args.idStr, sizeof(instance->id));
221
222 if ((fd = ast_audiosocket_connect(args.destination, NULL)) < 0) {
223 goto failure;
224 }
225 instance->svc = fd;
226
227 chan = ast_channel_alloc(1, AST_STATE_DOWN, "", "", "", "", "", assignedids,
228 requestor, 0, "AudioSocket/%s-%s", args.destination, args.idStr);
229 if (!chan) {
230 goto failure;
231 }
232 ast_channel_set_fd(chan, 0, fd);
233
235
236 ast_format_cap_append(caps, fmt, 0);
242
243 ast_channel_tech_pvt_set(chan, instance);
244
245 pbx_builtin_setvar_helper(chan, "AUDIOSOCKET_UUID", args.idStr);
246 pbx_builtin_setvar_helper(chan, "AUDIOSOCKET_SERVICE", args.destination);
247
248 ast_channel_unlock(chan);
249
250 ao2_ref(fmt, -1);
251 ao2_ref(caps, -1);
252 return chan;
253
254failure:
255 *cause = AST_CAUSE_FAILURE;
256 ao2_cleanup(fmt);
257 ao2_cleanup(caps);
258 if (instance != NULL) {
259 ast_free(instance);
260 if (fd >= 0) {
261 close(fd);
262 }
263 }
264
265 return NULL;
266}
267
268/*! \brief Function called when our module is unloaded */
269static int unload_module(void)
270{
274
275 return 0;
276}
277
278/*! \brief Function called when our module is loaded */
279static int load_module(void)
280{
283 }
286 ast_log(LOG_ERROR, "Unable to register channel class AudioSocket");
290 }
291
293}
294
296 .support_level = AST_MODULE_SUPPORT_EXTENDED,
297 .load = load_module,
298 .unload = unload_module,
299 .load_pri = AST_MODPRI_CHANNEL_DRIVER,
300 .requires = "res_audiosocket",
Access Control of various sorts.
Asterisk main include file. File version handling, generic pbx functions.
#define ast_free(a)
Definition: astmm.h:180
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
#define ast_log
Definition: astobj2.c:42
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
Internal Asterisk hangup causes.
#define AST_CAUSE_FAILURE
Definition: causes.h:150
@ OPT_ARG_AUDIOSOCKET_CODEC
@ OPT_ARG_ARRAY_SIZE
#define FD_OUTPUT
struct audiosocket_instance audiosocket_instance
static int audiosocket_hangup(struct ast_channel *ast)
Function called when we should hang the channel up.
static int audiosocket_write(struct ast_channel *ast, struct ast_frame *f)
Function called when we should write a frame to the channel.
static const struct ast_app_option audiosocket_options[128]
static struct ast_channel_tech audiosocket_channel_tech
static struct ast_channel * audiosocket_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)
Function called when we should prepare to call the unicast destination.
@ OPT_AUDIOSOCKET_CODEC
static int load_module(void)
Function called when our module is loaded.
static int audiosocket_call(struct ast_channel *ast, const char *dest, int timeout)
Function called when we should actually call the destination.
static int unload_module(void)
Function called when our module is unloaded.
static struct ast_frame * audiosocket_read(struct ast_channel *ast)
Function called when we should read a frame from the channel.
static const char type[]
Definition: chan_ooh323.c:109
General Asterisk PBX channel definitions.
void * ast_channel_tech_pvt(const 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)
void ast_channel_unregister(const struct ast_channel_tech *tech)
Unregister a channel technology.
Definition: channel.c:570
int ast_queue_control(struct ast_channel *chan, enum ast_control_frame_type control)
Queue a control frame without payload.
Definition: channel.c:1250
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:539
void ast_channel_set_fd(struct ast_channel *chan, int which, int fd)
Definition: channel.c:2445
void ast_channel_tech_set(struct ast_channel *chan, const struct ast_channel_tech *value)
#define ast_channel_unlock(chan)
Definition: channel.h:2969
void ast_channel_set_writeformat(struct ast_channel *chan, struct ast_format *format)
@ AST_STATE_DOWN
Definition: channelstate.h:36
@ AST_MEDIA_TYPE_UNKNOWN
Definition: codec.h:31
char * address
Definition: f2c.h:59
Media Format Cache API.
#define ast_format_cache_get(name)
Retrieve a named format from the cache.
Definition: format_cache.h:278
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:400
@ 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
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_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:3066
@ PARSE_PORT_REQUIRE
@ AST_CONTROL_ANSWER
#define LOG_ERROR
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_EXTENDED
Definition: module.h:122
#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
int ast_sockaddr_resolve_first_af(struct ast_sockaddr *addr, const char *name, int flag, int family)
Return the first entry from ast_sockaddr_resolve filtered by address family.
Definition: netsock2.c:337
@ AST_AF_UNSPEC
Definition: netsock2.h:54
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.
AudioSocket support functions.
const int ast_audiosocket_init(const int svc, const char *id)
Send the initial message to an AudioSocket server.
const int ast_audiosocket_send_frame(const int svc, const struct ast_frame *f)
Send an Asterisk audio frame to an AudioSocket server.
struct ast_frame * ast_audiosocket_receive_frame(const int svc)
Receive an Asterisk frame from an AudioSocket server.
const int ast_audiosocket_connect(const char *server, struct ast_channel *chan)
Send the initial message to an AudioSocket server.
#define NULL
Definition: resample.c:96
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
Structure to pass both assignedid values to channel drivers.
Definition: channel.h:606
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
Structure used to handle boolean flags.
Definition: utils.h:199
Format capabilities structure, holds formats + preference order + etc.
Definition: format_cap.c:54
Definition of a media format.
Definition: format.c:43
Data structure associated with a single frame of data.
Socket address structure.
Definition: netsock2.h:97
const char * args
static struct test_options options
#define ast_test_flag(p, flag)
Definition: utils.h:63