Wed Jan 8 2020 09:50:04

Asterisk developer's documentation


chan_nbs.c File Reference

Network broadcast sound support channel driver. More...

#include "asterisk.h"
#include <sys/socket.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <nbs.h>
#include "asterisk/lock.h"
#include "asterisk/channel.h"
#include "asterisk/config.h"
#include "asterisk/module.h"
#include "asterisk/pbx.h"
#include "asterisk/utils.h"

Go to the source code of this file.

Data Structures

struct  nbs_pvt
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
static int load_module (void)
 
static struct nbs_pvtnbs_alloc (void *data)
 
static int nbs_call (struct ast_channel *ast, char *dest, int timeout)
 
static void nbs_destroy (struct nbs_pvt *p)
 
static int nbs_hangup (struct ast_channel *ast)
 
static struct ast_channelnbs_new (struct nbs_pvt *i, int state, const char *linkedid)
 
static struct ast_channelnbs_request (const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause)
 
static struct ast_framenbs_xread (struct ast_channel *ast)
 
static int nbs_xwrite (struct ast_channel *ast, struct ast_frame *frame)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Network Broadcast Sound Support" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, }
 
static struct ast_module_infoast_module_info = &__mod_info
 
static char context [AST_MAX_EXTENSION] = "default"
 
static struct ast_channel_tech nbs_tech
 
static format_t prefformat = AST_FORMAT_SLINEAR
 
static const char tdesc [] = "Network Broadcast Sound Driver"
 
static const char type [] = "NBS"
 

Detailed Description

Network broadcast sound support channel driver.

Author
Mark Spencer marks.nosp@m.ter@.nosp@m.digiu.nosp@m.m.co.nosp@m.m

Definition in file chan_nbs.c.

Function Documentation

static void __reg_module ( void  )
static

Definition at line 293 of file chan_nbs.c.

static void __unreg_module ( void  )
static

Definition at line 293 of file chan_nbs.c.

static int load_module ( void  )
static

Definition at line 283 of file chan_nbs.c.

References ast_channel_register(), ast_log(), and LOG_ERROR.

284 {
285  /* Make sure we can register our channel type */
287  ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
288  return -1;
289  }
290  return 0;
291 }
static struct ast_channel_tech nbs_tech
Definition: chan_nbs.c:76
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:907
#define LOG_ERROR
Definition: logger.h:155
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
static const char type[]
Definition: chan_nbs.c:57
static struct nbs_pvt* nbs_alloc ( void *  data)
static

Definition at line 121 of file chan_nbs.c.

References ast_calloc, ast_copy_string(), ast_free, ast_log(), ast_strlen_zero(), LOG_WARNING, nbs_pvt::nbs, and nbs_pvt::stream.

Referenced by nbs_request().

122 {
123  struct nbs_pvt *p;
124  int flags = 0;
125  char stream[256];
126  char *opts;
127 
128  ast_copy_string(stream, data, sizeof(stream));
129  if ((opts = strchr(stream, ':'))) {
130  *opts = '\0';
131  opts++;
132  } else
133  opts = "";
134  p = ast_calloc(1, sizeof(*p));
135  if (p) {
136  if (!ast_strlen_zero(opts)) {
137  if (strchr(opts, 'm'))
138  flags |= NBS_FLAG_MUTE;
139  if (strchr(opts, 'o'))
140  flags |= NBS_FLAG_OVERSPEAK;
141  if (strchr(opts, 'e'))
142  flags |= NBS_FLAG_EMERGENCY;
143  if (strchr(opts, 'O'))
144  flags |= NBS_FLAG_OVERRIDE;
145  } else
146  flags = NBS_FLAG_OVERSPEAK;
147 
148  ast_copy_string(p->stream, stream, sizeof(p->stream));
149  p->nbs = nbs_newstream("asterisk", stream, flags);
150  if (!p->nbs) {
151  ast_log(LOG_WARNING, "Unable to allocate new NBS stream '%s' with flags %d\n", stream, flags);
152  ast_free(p);
153  p = NULL;
154  } else {
155  /* Set for 8000 hz mono, 640 samples */
156  nbs_setbitrate(p->nbs, 8000);
157  nbs_setchannels(p->nbs, 1);
158  nbs_setblocksize(p->nbs, 640);
159  nbs_setblocking(p->nbs, 0);
160  }
161  }
162  return p;
163 }
#define LOG_WARNING
Definition: logger.h:144
NBS * nbs
Definition: chan_nbs.c:62
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define ast_free(a)
Definition: astmm.h:97
#define ast_calloc(a, b)
Definition: astmm.h:82
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:223
char stream[80]
Definition: chan_nbs.c:65
static int nbs_call ( struct ast_channel ast,
char *  dest,
int  timeout 
)
static

Definition at line 87 of file chan_nbs.c.

References ast_channel::_state, AST_CONTROL_ANSWER, AST_CONTROL_CONGESTION, ast_debug, ast_log(), ast_queue_control(), ast_setstate(), AST_STATE_DOWN, AST_STATE_RESERVED, AST_STATE_RINGING, LOG_WARNING, ast_channel::name, nbs_pvt::nbs, and ast_channel::tech_pvt.

88 {
89  struct nbs_pvt *p;
90 
91  p = ast->tech_pvt;
92 
93  if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
94  ast_log(LOG_WARNING, "nbs_call called on %s, neither down nor reserved\n", ast->name);
95  return -1;
96  }
97  /* When we call, it just works, really, there's no destination... Just
98  ring the phone and wait for someone to answer */
99  ast_debug(1, "Calling %s on %s\n", dest, ast->name);
100 
101  /* If we can't connect, return congestion */
102  if (nbs_connect(p->nbs)) {
103  ast_log(LOG_WARNING, "NBS Connection failed on %s\n", ast->name);
105  } else {
108  }
109 
110  return 0;
111 }
int ast_queue_control(struct ast_channel *chan, enum ast_control_frame_type control)
Queue a control frame with payload.
Definition: channel.c:1601
void * tech_pvt
Definition: channel.h:744
#define LOG_WARNING
Definition: logger.h:144
NBS * nbs
Definition: chan_nbs.c:62
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
enum ast_channel_state _state
Definition: channel.h:839
const ast_string_field name
Definition: channel.h:787
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
int ast_setstate(struct ast_channel *chan, enum ast_channel_state)
Change the state of a channel.
Definition: channel.c:7119
static void nbs_destroy ( struct nbs_pvt p)
static

Definition at line 113 of file chan_nbs.c.

References ast_free, ast_module_user_remove, nbs_pvt::nbs, and nbs_pvt::u.

Referenced by nbs_hangup(), and nbs_request().

114 {
115  if (p->nbs)
116  nbs_delstream(p->nbs);
118  ast_free(p);
119 }
NBS * nbs
Definition: chan_nbs.c:62
#define ast_module_user_remove(user)
Definition: module.h:269
#define ast_free(a)
Definition: astmm.h:97
struct ast_module_user * u
Definition: chan_nbs.c:67
static int nbs_hangup ( struct ast_channel ast)
static

Definition at line 165 of file chan_nbs.c.

References ast_debug, ast_log(), ast_setstate(), AST_STATE_DOWN, LOG_WARNING, ast_channel::name, nbs_destroy(), and ast_channel::tech_pvt.

166 {
167  struct nbs_pvt *p;
168  p = ast->tech_pvt;
169  ast_debug(1, "nbs_hangup(%s)\n", ast->name);
170  if (!ast->tech_pvt) {
171  ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
172  return 0;
173  }
174  nbs_destroy(p);
175  ast->tech_pvt = NULL;
177  return 0;
178 }
void * tech_pvt
Definition: channel.h:744
#define LOG_WARNING
Definition: logger.h:144
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
const ast_string_field name
Definition: channel.h:787
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
int ast_setstate(struct ast_channel *chan, enum ast_channel_state)
Change the state of a channel.
Definition: channel.c:7119
static void nbs_destroy(struct nbs_pvt *p)
Definition: chan_nbs.c:113
static struct ast_channel* nbs_new ( struct nbs_pvt i,
int  state,
const char *  linkedid 
)
static

Definition at line 223 of file chan_nbs.c.

References ast_channel_alloc(), ast_channel_set_fd(), ast_copy_string(), ast_hangup(), ast_log(), ast_module_user_add, ast_pbx_start(), AST_STATE_DOWN, AST_STATE_RING, ast_string_field_set, ast_channel::context, ast_channel::exten, language, LOG_WARNING, ast_channel::name, ast_channel::nativeformats, nbs_pvt::nbs, nbs_tech, nbs_pvt::owner, prefformat, ast_channel::rawreadformat, ast_channel::rawwriteformat, ast_channel::readformat, ast_channel::rings, nbs_pvt::stream, ast_channel::tech, ast_channel::tech_pvt, nbs_pvt::u, and ast_channel::writeformat.

Referenced by nbs_request().

224 {
225  struct ast_channel *tmp;
226  tmp = ast_channel_alloc(1, state, 0, 0, "", "s", context, linkedid, 0, "NBS/%s", i->stream);
227  if (tmp) {
228  tmp->tech = &nbs_tech;
229  ast_channel_set_fd(tmp, 0, nbs_fd(i->nbs));
230  tmp->nativeformats = prefformat;
231  tmp->rawreadformat = prefformat;
232  tmp->rawwriteformat = prefformat;
233  tmp->writeformat = prefformat;
234  tmp->readformat = prefformat;
235  if (state == AST_STATE_RING)
236  tmp->rings = 1;
237  tmp->tech_pvt = i;
238  ast_copy_string(tmp->context, context, sizeof(tmp->context));
239  ast_copy_string(tmp->exten, "s", sizeof(tmp->exten));
240  ast_string_field_set(tmp, language, "");
241  i->owner = tmp;
242  i->u = ast_module_user_add(tmp);
243  if (state != AST_STATE_DOWN) {
244  if (ast_pbx_start(tmp)) {
245  ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
246  ast_hangup(tmp);
247  }
248  }
249  } else
250  ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
251  return tmp;
252 }
int ast_hangup(struct ast_channel *chan)
Hang up a channel.
Definition: channel.c:2804
Main Channel structure associated with a channel.
Definition: channel.h:742
static struct ast_channel_tech nbs_tech
Definition: chan_nbs.c:76
int rings
Definition: channel.h:840
static format_t prefformat
Definition: chan_nbs.c:54
format_t writeformat
Definition: channel.h:854
void * tech_pvt
Definition: channel.h:744
char context[AST_MAX_CONTEXT]
Definition: channel.h:868
#define LOG_WARNING
Definition: logger.h:144
enum ast_pbx_result ast_pbx_start(struct ast_channel *c)
Create a new thread and start the PBX.
Definition: pbx.c:5879
format_t rawwriteformat
Definition: channel.h:856
NBS * nbs
Definition: chan_nbs.c:62
format_t nativeformats
Definition: channel.h:852
const ast_string_field linkedid
Definition: channel.h:787
format_t rawreadformat
Definition: channel.h:855
struct ast_channel * ast_channel_alloc(int needqueue, int state, const char *cid_num, const char *cid_name, const char *acctcode, const char *exten, const char *context, const char *linkedid, const int amaflag, const char *name_fmt,...)
Definition: channel.c:9825
static char language[MAX_LANGUAGE]
Definition: chan_alsa.c:108
#define ast_module_user_add(chan)
Definition: module.h:268
const ast_string_field name
Definition: channel.h:787
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
struct ast_module_user * u
Definition: chan_nbs.c:67
void ast_channel_set_fd(struct ast_channel *chan, int which, int fd)
Definition: channel.c:2631
static char context[AST_MAX_EXTENSION]
Definition: chan_nbs.c:56
format_t readformat
Definition: channel.h:853
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:223
char stream[80]
Definition: chan_nbs.c:65
struct ast_channel_tech * tech
Definition: channel.h:743
char exten[AST_MAX_EXTENSION]
Definition: channel.h:869
struct ast_channel * owner
Definition: chan_nbs.c:63
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
Definition: stringfields.h:344
static struct ast_channel * nbs_request ( const char *  type,
format_t  format,
const struct ast_channel requestor,
void *  data,
int *  cause 
)
static

Definition at line 255 of file chan_nbs.c.

References AST_FORMAT_SLINEAR, ast_getformatname(), ast_log(), AST_STATE_DOWN, format, ast_channel::linkedid, LOG_NOTICE, nbs_alloc(), nbs_destroy(), and nbs_new().

256 {
257  format_t oldformat;
258  struct nbs_pvt *p;
259  struct ast_channel *tmp = NULL;
260 
261  oldformat = format;
263  if (!format) {
264  ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%s'\n", ast_getformatname(oldformat));
265  return NULL;
266  }
267  p = nbs_alloc(data);
268  if (p) {
269  tmp = nbs_new(p, AST_STATE_DOWN, requestor ? requestor->linkedid : NULL);
270  if (!tmp)
271  nbs_destroy(p);
272  }
273  return tmp;
274 }
Main Channel structure associated with a channel.
Definition: channel.h:742
static struct nbs_pvt * nbs_alloc(void *data)
Definition: chan_nbs.c:121
static struct ast_channel * nbs_new(struct nbs_pvt *i, int state, const char *linkedid)
Definition: chan_nbs.c:223
const char * data
Definition: channel.h:755
const ast_string_field linkedid
Definition: channel.h:787
char * ast_getformatname(format_t format)
Get the name of a format.
Definition: frame.c:578
int64_t format_t
Definition: frame_defs.h:32
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define LOG_NOTICE
Definition: logger.h:133
#define AST_FORMAT_SLINEAR
Definition: frame.h:254
static snd_pcm_format_t format
Definition: chan_alsa.c:93
static void nbs_destroy(struct nbs_pvt *p)
Definition: chan_nbs.c:113
static struct ast_frame * nbs_xread ( struct ast_channel ast)
static

Definition at line 180 of file chan_nbs.c.

References ast_debug, ast_frame::data, ast_frame::datalen, ast_frame::delivery, nbs_pvt::fr, ast_frame::mallocd, ast_channel::name, ast_frame::offset, ast_frame::ptr, ast_frame::samples, ast_frame::src, ast_channel::tech_pvt, and type.

181 {
182  struct nbs_pvt *p = ast->tech_pvt;
183 
184 
185  /* Some nice norms */
186  p->fr.datalen = 0;
187  p->fr.samples = 0;
188  p->fr.data.ptr = NULL;
189  p->fr.src = type;
190  p->fr.offset = 0;
191  p->fr.mallocd=0;
192  p->fr.delivery.tv_sec = 0;
193  p->fr.delivery.tv_usec = 0;
194 
195  ast_debug(1, "Returning null frame on %s\n", ast->name);
196 
197  return &p->fr;
198 }
int offset
Definition: frame.h:156
void * ptr
Definition: frame.h:160
void * tech_pvt
Definition: channel.h:744
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
const char * src
Definition: frame.h:158
int datalen
Definition: frame.h:148
const ast_string_field name
Definition: channel.h:787
static const char type[]
Definition: chan_nbs.c:57
struct timeval delivery
Definition: frame.h:162
int mallocd
Definition: frame.h:152
struct ast_frame fr
Definition: chan_nbs.c:66
union ast_frame::@172 data
int samples
Definition: frame.h:150
static int nbs_xwrite ( struct ast_channel ast,
struct ast_frame frame 
)
static

Definition at line 200 of file chan_nbs.c.

References ast_channel::_state, AST_FORMAT_SLINEAR, AST_FRAME_IMAGE, AST_FRAME_VOICE, ast_getformatname(), ast_log(), AST_STATE_UP, ast_frame_subclass::codec, ast_frame::data, ast_frame::datalen, ast_frame::frametype, LOG_WARNING, nbs_pvt::nbs, ast_frame::ptr, ast_frame::subclass, and ast_channel::tech_pvt.

201 {
202  struct nbs_pvt *p = ast->tech_pvt;
203  /* Write a frame of (presumably voice) data */
204  if (frame->frametype != AST_FRAME_VOICE) {
205  if (frame->frametype != AST_FRAME_IMAGE)
206  ast_log(LOG_WARNING, "Don't know what to do with frame type '%d'\n", frame->frametype);
207  return 0;
208  }
209  if (!(frame->subclass.codec &
210  (AST_FORMAT_SLINEAR))) {
211  ast_log(LOG_WARNING, "Cannot handle frames in %s format\n", ast_getformatname(frame->subclass.codec));
212  return 0;
213  }
214  if (ast->_state != AST_STATE_UP) {
215  /* Don't try tos end audio on-hook */
216  return 0;
217  }
218  if (nbs_write(p->nbs, frame->data.ptr, frame->datalen / 2) < 0)
219  return -1;
220  return 0;
221 }
union ast_frame_subclass subclass
Definition: frame.h:146
void * ptr
Definition: frame.h:160
void * tech_pvt
Definition: channel.h:744
#define LOG_WARNING
Definition: logger.h:144
NBS * nbs
Definition: chan_nbs.c:62
format_t codec
Definition: frame.h:137
int datalen
Definition: frame.h:148
char * ast_getformatname(format_t format)
Get the name of a format.
Definition: frame.c:578
enum ast_channel_state _state
Definition: channel.h:839
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define AST_FORMAT_SLINEAR
Definition: frame.h:254
enum ast_frame_type frametype
Definition: frame.h:144
union ast_frame::@172 data
static int unload_module ( void  )
static

Definition at line 276 of file chan_nbs.c.

References ast_channel_unregister().

277 {
278  /* First, take us out of the channel loop */
280  return 0;
281 }
static struct ast_channel_tech nbs_tech
Definition: chan_nbs.c:76
void ast_channel_unregister(const struct ast_channel_tech *tech)
Unregister a channel technology.
Definition: channel.c:938

Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Network Broadcast Sound Support" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, }
static

Definition at line 293 of file chan_nbs.c.

Definition at line 293 of file chan_nbs.c.

char context[AST_MAX_EXTENSION] = "default"
static

Definition at line 56 of file chan_nbs.c.

struct ast_channel_tech nbs_tech
static

Definition at line 76 of file chan_nbs.c.

Referenced by nbs_new().

format_t prefformat = AST_FORMAT_SLINEAR
static

Definition at line 54 of file chan_nbs.c.

Referenced by nbs_new().

const char tdesc[] = "Network Broadcast Sound Driver"
static

Definition at line 51 of file chan_nbs.c.