Asterisk - The Open Source Telephony Project  GIT-master-1f78ee9
Data Structures | Functions | Variables
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"
#include "asterisk/format_cache.h"

Go to the source code of this file.

Data Structures

struct  nbs_pvt
 

Functions

 AST_MODULE_INFO_STANDARD_DEPRECATED (ASTERISK_GPL_KEY, "Network Broadcast Sound Support")
 
static int load_module (void)
 
static struct nbs_pvtnbs_alloc (const char *data)
 
static int nbs_call (struct ast_channel *ast, const 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 struct ast_assigned_ids *assignedids, const struct ast_channel *requestor)
 
static struct ast_channelnbs_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 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 char context [AST_MAX_EXTENSION] = "default"
 
static struct ast_channel_tech nbs_tech
 
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

◆ AST_MODULE_INFO_STANDARD_DEPRECATED()

AST_MODULE_INFO_STANDARD_DEPRECATED ( ASTERISK_GPL_KEY  ,
"Network Broadcast Sound Support"   
)

Referenced by load_module().

◆ load_module()

static int load_module ( void  )
static

Definition at line 257 of file chan_nbs.c.

References ao2_ref, ast_channel_register(), ast_format_cap_alloc, ast_format_cap_append, AST_FORMAT_CAP_FLAG_DEFAULT, ast_format_slin, ast_log, AST_MODULE_INFO_STANDARD_DEPRECATED(), AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, ASTERISK_GPL_KEY, ast_channel_tech::capabilities, LOG_ERROR, NULL, and type.

258 {
261  }
263  /* Make sure we can register our channel type */
265  ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
269  }
271 }
static struct ast_channel_tech nbs_tech
Definition: chan_nbs.c:71
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:535
#define NULL
Definition: resample.c:96
#define ast_log
Definition: astobj2.c:42
#define ao2_ref(o, delta)
Definition: astobj2.h:464
#define ast_format_cap_append(cap, format, framing)
Definition: format_cap.h:103
#define ast_format_cap_alloc(flags)
Definition: format_cap.h:52
#define LOG_ERROR
Definition: logger.h:285
struct ast_format_cap * capabilities
Definition: channel.h:633
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
static const char type[]
Definition: chan_nbs.c:53
struct ast_format * ast_format_slin
Built-in cached signed linear 8kHz format.
Definition: format_cache.c:41

◆ nbs_alloc()

static struct nbs_pvt* nbs_alloc ( const char *  data)
static

Definition at line 115 of file chan_nbs.c.

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

Referenced by nbs_request().

116 {
117  struct nbs_pvt *p;
118  int flags = 0;
119  char stream[256];
120  char *opts;
121 
122  ast_copy_string(stream, data, sizeof(stream));
123  if ((opts = strchr(stream, ':'))) {
124  *opts = '\0';
125  opts++;
126  } else
127  opts = "";
128  p = ast_calloc(1, sizeof(*p));
129  if (p) {
130  if (!ast_strlen_zero(opts)) {
131  if (strchr(opts, 'm'))
132  flags |= NBS_FLAG_MUTE;
133  if (strchr(opts, 'o'))
134  flags |= NBS_FLAG_OVERSPEAK;
135  if (strchr(opts, 'e'))
136  flags |= NBS_FLAG_EMERGENCY;
137  if (strchr(opts, 'O'))
138  flags |= NBS_FLAG_OVERRIDE;
139  } else
140  flags = NBS_FLAG_OVERSPEAK;
141 
142  ast_copy_string(p->stream, stream, sizeof(p->stream));
143  p->nbs = nbs_newstream("asterisk", stream, flags);
144  if (!p->nbs) {
145  ast_log(LOG_WARNING, "Unable to allocate new NBS stream '%s' with flags %d\n", stream, flags);
146  ast_free(p);
147  p = NULL;
148  } else {
149  /* Set for 8000 hz mono, 640 samples */
150  nbs_setbitrate(p->nbs, 8000);
151  nbs_setchannels(p->nbs, 1);
152  nbs_setblocksize(p->nbs, 640);
153  nbs_setblocking(p->nbs, 0);
154  }
155  }
156  return p;
157 }
#define LOG_WARNING
Definition: logger.h:274
NBS * nbs
Definition: chan_nbs.c:58
#define NULL
Definition: resample.c:96
#define ast_log
Definition: astobj2.c:42
#define ast_strlen_zero(a)
Definition: muted.c:73
#define ast_free(a)
Definition: astmm.h:182
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:401
char stream[80]
Definition: chan_nbs.c:61

◆ nbs_call()

static int nbs_call ( struct ast_channel ast,
const char *  dest,
int  timeout 
)
static

Definition at line 81 of file chan_nbs.c.

References ast_channel_name(), ast_channel_tech_pvt(), 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, and nbs_pvt::nbs.

82 {
83  struct nbs_pvt *p;
84 
85  p = ast_channel_tech_pvt(ast);
86 
88  ast_log(LOG_WARNING, "nbs_call called on %s, neither down nor reserved\n", ast_channel_name(ast));
89  return -1;
90  }
91  /* When we call, it just works, really, there's no destination... Just
92  ring the phone and wait for someone to answer */
93  ast_debug(1, "Calling %s on %s\n", dest, ast_channel_name(ast));
94 
95  /* If we can't connect, return congestion */
96  if (nbs_connect(p->nbs)) {
97  ast_log(LOG_WARNING, "NBS Connection failed on %s\n", ast_channel_name(ast));
99  } else {
102  }
103 
104  return 0;
105 }
int ast_queue_control(struct ast_channel *chan, enum ast_control_frame_type control)
Queue a control frame without payload.
Definition: channel.c:1227
void * ast_channel_tech_pvt(const struct ast_channel *chan)
#define LOG_WARNING
Definition: logger.h:274
ast_channel_state
ast_channel states
Definition: channelstate.h:35
NBS * nbs
Definition: chan_nbs.c:58
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:444
#define ast_log
Definition: astobj2.c:42
const char * ast_channel_name(const struct ast_channel *chan)
int ast_setstate(struct ast_channel *chan, enum ast_channel_state)
Change the state of a channel.
Definition: channel.c:7325

◆ nbs_destroy()

static void nbs_destroy ( struct nbs_pvt p)
static

Definition at line 107 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().

108 {
109  if (p->nbs)
110  nbs_delstream(p->nbs);
112  ast_free(p);
113 }
NBS * nbs
Definition: chan_nbs.c:58
#define ast_module_user_remove(user)
Definition: module.h:427
#define ast_free(a)
Definition: astmm.h:182
struct ast_module_user * u
Definition: chan_nbs.c:62

◆ nbs_hangup()

static int nbs_hangup ( struct ast_channel ast)
static

Definition at line 159 of file chan_nbs.c.

References ast_channel_name(), ast_channel_tech_pvt(), ast_channel_tech_pvt_set(), ast_debug, ast_log, ast_setstate(), AST_STATE_DOWN, LOG_WARNING, nbs_destroy(), and NULL.

160 {
161  struct nbs_pvt *p;
162  p = ast_channel_tech_pvt(ast);
163  ast_debug(1, "nbs_hangup(%s)\n", ast_channel_name(ast));
164  if (!ast_channel_tech_pvt(ast)) {
165  ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
166  return 0;
167  }
168  nbs_destroy(p);
171  return 0;
172 }
void * ast_channel_tech_pvt(const struct ast_channel *chan)
#define LOG_WARNING
Definition: logger.h:274
#define NULL
Definition: resample.c:96
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:444
#define ast_log
Definition: astobj2.c:42
const char * ast_channel_name(const struct ast_channel *chan)
int ast_setstate(struct ast_channel *chan, enum ast_channel_state)
Change the state of a channel.
Definition: channel.c:7325
void ast_channel_tech_pvt_set(struct ast_channel *chan, void *value)
static void nbs_destroy(struct nbs_pvt *p)
Definition: chan_nbs.c:107

◆ nbs_new()

static struct ast_channel* nbs_new ( struct nbs_pvt i,
int  state,
const struct ast_assigned_ids assignedids,
const struct ast_channel requestor 
)
static

Definition at line 193 of file chan_nbs.c.

References ast_channel_alloc, ast_channel_context_set(), ast_channel_exten_set(), ast_channel_name(), ast_channel_nativeformats_set(), ast_channel_rings_set(), ast_channel_set_fd(), ast_channel_set_rawreadformat(), ast_channel_set_rawwriteformat(), ast_channel_set_readformat(), ast_channel_set_writeformat(), ast_channel_tech_pvt_set(), ast_channel_tech_set(), ast_channel_unlock, ast_format_slin, ast_hangup(), ast_log, ast_module_user_add, ast_pbx_start(), AST_STATE_DOWN, AST_STATE_RING, ast_channel_tech::capabilities, context, LOG_WARNING, nbs_pvt::nbs, nbs_pvt::owner, nbs_pvt::stream, tmp(), and nbs_pvt::u.

Referenced by nbs_request().

194 {
195  struct ast_channel *tmp;
196  tmp = ast_channel_alloc(1, state, 0, 0, "", "s", context, assignedids, requestor, 0, "NBS/%s", i->stream);
197  if (tmp) {
199  ast_channel_set_fd(tmp, 0, nbs_fd(i->nbs));
200 
206  if (state == AST_STATE_RING)
207  ast_channel_rings_set(tmp, 1);
208  ast_channel_tech_pvt_set(tmp, i);
210  ast_channel_exten_set(tmp, "s");
211  ast_channel_language_set(tmp, "");
212  i->owner = tmp;
213  i->u = ast_module_user_add(tmp);
214  ast_channel_unlock(tmp);
215  if (state != AST_STATE_DOWN) {
216  if (ast_pbx_start(tmp)) {
217  ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ast_channel_name(tmp));
218  ast_hangup(tmp);
219  }
220  }
221  } else
222  ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
223  return tmp;
224 }
Main Channel structure associated with a channel.
static struct ast_channel_tech nbs_tech
Definition: chan_nbs.c:71
void ast_channel_set_writeformat(struct ast_channel *chan, struct ast_format *format)
void ast_channel_set_rawwriteformat(struct ast_channel *chan, struct ast_format *format)
#define LOG_WARNING
Definition: logger.h:274
enum ast_pbx_result ast_pbx_start(struct ast_channel *c)
Create a new thread and start the PBX.
Definition: pbx.c:4703
static int tmp()
Definition: bt_open.c:389
NBS * nbs
Definition: chan_nbs.c:58
void ast_channel_tech_set(struct ast_channel *chan, const struct ast_channel_tech *value)
#define ast_log
Definition: astobj2.c:42
void ast_channel_set_rawreadformat(struct ast_channel *chan, struct ast_format *format)
void ast_channel_rings_set(struct ast_channel *chan, int value)
void ast_channel_nativeformats_set(struct ast_channel *chan, struct ast_format_cap *value)
void ast_channel_set_readformat(struct ast_channel *chan, struct ast_format *format)
#define ast_module_user_add(chan)
Definition: module.h:426
struct ast_format_cap * capabilities
Definition: channel.h:633
#define ast_channel_unlock(chan)
Definition: channel.h:2838
void ast_hangup(struct ast_channel *chan)
Hang up a channel.
Definition: channel.c:2523
struct ast_module_user * u
Definition: chan_nbs.c:62
void ast_channel_set_fd(struct ast_channel *chan, int which, int fd)
Definition: channel.c:2406
void ast_channel_exten_set(struct ast_channel *chan, const char *value)
static char context[AST_MAX_EXTENSION]
Definition: chan_nbs.c:52
void ast_channel_context_set(struct ast_channel *chan, const char *value)
const char * ast_channel_name(const struct ast_channel *chan)
char stream[80]
Definition: chan_nbs.c:61
#define ast_channel_alloc(needqueue, state, cid_num, cid_name, acctcode, exten, context, assignedids, requestor, amaflag,...)
Create a channel structure.
Definition: channel.h:1242
struct ast_format * ast_format_slin
Built-in cached signed linear 8kHz format.
Definition: format_cache.c:41
void ast_channel_tech_pvt_set(struct ast_channel *chan, void *value)
struct ast_channel * owner
Definition: chan_nbs.c:59

◆ nbs_request()

static struct ast_channel * nbs_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

Definition at line 227 of file chan_nbs.c.

References ast_format_cap_get_names(), ast_format_cap_iscompatible_format(), AST_FORMAT_CAP_NAMES_LEN, AST_FORMAT_CMP_NOT_EQUAL, ast_format_slin, ast_log, AST_STATE_DOWN, ast_str_alloca, LOG_NOTICE, nbs_alloc(), nbs_destroy(), nbs_new(), NULL, and tmp().

228 {
229  struct nbs_pvt *p;
230  struct ast_channel *tmp = NULL;
231 
234 
235  ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%s'\n",
236  ast_format_cap_get_names(cap, &cap_buf));
237  return NULL;
238  }
239  p = nbs_alloc(data);
240  if (p) {
241  tmp = nbs_new(p, AST_STATE_DOWN, assignedids, requestor);
242  if (!tmp)
243  nbs_destroy(p);
244  }
245  return tmp;
246 }
Main Channel structure associated with a channel.
#define AST_FORMAT_CAP_NAMES_LEN
Definition: format_cap.h:326
static int tmp()
Definition: bt_open.c:389
#define ast_str_alloca(init_len)
Definition: strings.h:800
#define NULL
Definition: resample.c:96
enum ast_format_cmp_res ast_format_cap_iscompatible_format(const struct ast_format_cap *cap, const struct ast_format *format)
Find if ast_format is within the capabilities of the ast_format_cap object.
Definition: format_cap.c:583
static struct nbs_pvt * nbs_alloc(const char *data)
Definition: chan_nbs.c:115
#define ast_log
Definition: astobj2.c:42
static struct ast_channel * nbs_new(struct nbs_pvt *i, int state, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor)
Definition: chan_nbs.c:193
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
#define LOG_NOTICE
Definition: logger.h:263
const char * ast_format_cap_get_names(const struct ast_format_cap *cap, struct ast_str **buf)
Get the names of codecs of a set of formats.
Definition: format_cap.c:736
struct ast_format * ast_format_slin
Built-in cached signed linear 8kHz format.
Definition: format_cache.c:41
static void nbs_destroy(struct nbs_pvt *p)
Definition: chan_nbs.c:107

◆ nbs_xread()

static struct ast_frame * nbs_xread ( struct ast_channel ast)
static

Definition at line 174 of file chan_nbs.c.

References ast_channel_name(), ast_debug, and ast_null_frame.

175 {
176  ast_debug(1, "Returning null frame on %s\n", ast_channel_name(ast));
177 
178  return &ast_null_frame;
179 }
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:444
struct ast_frame ast_null_frame
Definition: main/frame.c:79
const char * ast_channel_name(const struct ast_channel *chan)

◆ nbs_xwrite()

static int nbs_xwrite ( struct ast_channel ast,
struct ast_frame frame 
)
static

Definition at line 181 of file chan_nbs.c.

References ast_channel_tech_pvt(), AST_STATE_UP, ast_frame::data, ast_frame::datalen, nbs_pvt::nbs, and ast_frame::ptr.

182 {
183  struct nbs_pvt *p = ast_channel_tech_pvt(ast);
184  if (ast_channel_state(ast) != AST_STATE_UP) {
185  /* Don't try tos end audio on-hook */
186  return 0;
187  }
188  if (nbs_write(p->nbs, frame->data.ptr, frame->datalen / 2) < 0)
189  return -1;
190  return 0;
191 }
void * ast_channel_tech_pvt(const struct ast_channel *chan)
ast_channel_state
ast_channel states
Definition: channelstate.h:35
NBS * nbs
Definition: chan_nbs.c:58
union ast_frame::@255 data

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 248 of file chan_nbs.c.

References ao2_ref, ast_channel_unregister(), ast_channel_tech::capabilities, and NULL.

249 {
250  /* First, take us out of the channel loop */
254  return 0;
255 }
static struct ast_channel_tech nbs_tech
Definition: chan_nbs.c:71
void ast_channel_unregister(const struct ast_channel_tech *tech)
Unregister a channel technology.
Definition: channel.c:566
#define NULL
Definition: resample.c:96
#define ao2_ref(o, delta)
Definition: astobj2.h:464
struct ast_format_cap * capabilities
Definition: channel.h:633

Variable Documentation

◆ context

char context[AST_MAX_EXTENSION] = "default"
static

Definition at line 52 of file chan_nbs.c.

Referenced by nbs_new().

◆ nbs_tech

struct ast_channel_tech nbs_tech
static

Definition at line 71 of file chan_nbs.c.

◆ tdesc

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

Definition at line 50 of file chan_nbs.c.

◆ type

const char type[] = "NBS"
static

Definition at line 53 of file chan_nbs.c.

Referenced by load_module().