Wed Jan 8 2020 09:49:54

Asterisk developer's documentation


app_nbscat.c File Reference

Silly application to play an NBScat file – uses nbscat8k. More...

#include "asterisk.h"
#include <fcntl.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <signal.h>
#include "asterisk/lock.h"
#include "asterisk/file.h"
#include "asterisk/channel.h"
#include "asterisk/frame.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/translate.h"
#include "asterisk/app.h"

Go to the source code of this file.

Macros

#define AF_LOCAL   AF_UNIX
 
#define LOCAL_NBSCAT   "/usr/local/bin/nbscat8k"
 
#define NBSCAT   "/usr/bin/nbscat8k"
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
static int load_module (void)
 
static int NBScat_exec (struct ast_channel *chan, const char *data)
 
static int NBScatplay (int fd)
 
static int timed_read (int fd, void *data, int datalen)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Silly NBS Stream Application" , .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 char * app = "NBScat"
 
static struct ast_module_infoast_module_info = &__mod_info
 

Detailed Description

Silly application to play an NBScat file – uses nbscat8k.

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

Definition in file app_nbscat.c.

Macro Definition Documentation

#define AF_LOCAL   AF_UNIX

Definition at line 67 of file app_nbscat.c.

Referenced by NBScat_exec().

#define LOCAL_NBSCAT   "/usr/local/bin/nbscat8k"

Definition at line 63 of file app_nbscat.c.

Referenced by NBScatplay().

#define NBSCAT   "/usr/bin/nbscat8k"

Definition at line 64 of file app_nbscat.c.

Referenced by NBScatplay().

Function Documentation

static void __reg_module ( void  )
static

Definition at line 220 of file app_nbscat.c.

static void __unreg_module ( void  )
static

Definition at line 220 of file app_nbscat.c.

static int load_module ( void  )
static

Definition at line 215 of file app_nbscat.c.

References ast_register_application_xml, and NBScat_exec().

216 {
218 }
static int NBScat_exec(struct ast_channel *chan, const char *data)
Definition: app_nbscat.c:112
static char * app
Definition: app_nbscat.c:70
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:437
static int NBScat_exec ( struct ast_channel chan,
const char *  data 
)
static

Definition at line 112 of file app_nbscat.c.

References AF_LOCAL, ast_debug, AST_FORMAT_SLINEAR, AST_FRAME_DTMF, AST_FRAME_VOICE, ast_frfree, AST_FRIENDLY_OFFSET, ast_log(), ast_read(), ast_samp2tv(), ast_set_write_format(), ast_stopstream(), ast_tvadd(), ast_tvdiff_ms(), ast_tvnow(), ast_waitfor(), ast_write(), f, ast_frame::frametype, LOG_WARNING, NBScatplay(), ast_frame::offset, timed_read(), and ast_channel::writeformat.

Referenced by load_module().

113 {
114  int res=0;
115  int fds[2];
116  int ms = -1;
117  int pid = -1;
118  int owriteformat;
119  struct timeval next;
120  struct ast_frame *f;
121  struct myframe {
122  struct ast_frame f;
124  short frdata[160];
125  } myf;
126 
127  if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds)) {
128  ast_log(LOG_WARNING, "Unable to create socketpair\n");
129  return -1;
130  }
131 
132  ast_stopstream(chan);
133 
134  owriteformat = chan->writeformat;
136  if (res < 0) {
137  ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
138  return -1;
139  }
140 
141  res = NBScatplay(fds[1]);
142  /* Wait 1000 ms first */
143  next = ast_tvnow();
144  next.tv_sec += 1;
145  if (res >= 0) {
146  pid = res;
147  /* Order is important -- there's almost always going to be mp3... we want to prioritize the
148  user */
149  for (;;) {
150  ms = ast_tvdiff_ms(next, ast_tvnow());
151  if (ms <= 0) {
152  res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata));
153  if (res > 0) {
154  myf.f.frametype = AST_FRAME_VOICE;
155  myf.f.subclass.codec = AST_FORMAT_SLINEAR;
156  myf.f.datalen = res;
157  myf.f.samples = res / 2;
158  myf.f.mallocd = 0;
159  myf.f.offset = AST_FRIENDLY_OFFSET;
160  myf.f.src = __PRETTY_FUNCTION__;
161  myf.f.delivery.tv_sec = 0;
162  myf.f.delivery.tv_usec = 0;
163  myf.f.data.ptr = myf.frdata;
164  if (ast_write(chan, &myf.f) < 0) {
165  res = -1;
166  break;
167  }
168  } else {
169  ast_debug(1, "No more mp3\n");
170  res = 0;
171  break;
172  }
173  next = ast_tvadd(next, ast_samp2tv(myf.f.samples, 8000));
174  } else {
175  ms = ast_waitfor(chan, ms);
176  if (ms < 0) {
177  ast_debug(1, "Hangup detected\n");
178  res = -1;
179  break;
180  }
181  if (ms) {
182  f = ast_read(chan);
183  if (!f) {
184  ast_debug(1, "Null frame == hangup() detected\n");
185  res = -1;
186  break;
187  }
188  if (f->frametype == AST_FRAME_DTMF) {
189  ast_debug(1, "User pressed a key\n");
190  ast_frfree(f);
191  res = 0;
192  break;
193  }
194  ast_frfree(f);
195  }
196  }
197  }
198  }
199  close(fds[0]);
200  close(fds[1]);
201 
202  if (pid > -1)
203  kill(pid, SIGKILL);
204  if (!res && owriteformat)
205  ast_set_write_format(chan, owriteformat);
206 
207  return res;
208 }
int offset
Definition: frame.h:156
format_t writeformat
Definition: channel.h:854
#define AF_LOCAL
Definition: app_nbscat.c:67
#define LOG_WARNING
Definition: logger.h:144
#define AST_FRAME_DTMF
Definition: frame.h:128
struct ast_frame * ast_read(struct ast_channel *chan)
Reads a frame.
Definition: channel.c:4383
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:142
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:90
int ast_set_write_format(struct ast_channel *chan, format_t format)
Sets write format on channel chan Set write format for channel to whichever component of &quot;format&quot; is ...
Definition: channel.c:5307
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
#define AST_FRIENDLY_OFFSET
Offset into a frame&#39;s data buffer.
Definition: frame.h:204
struct timeval ast_samp2tv(unsigned int _nsamp, unsigned int _rate)
Returns a timeval corresponding to the duration of n samples at rate r. Useful to convert samples to ...
Definition: time.h:191
struct timeval ast_tvadd(struct timeval a, struct timeval b)
Returns the sum of two timevals a + b.
Definition: utils.c:1587
static int timed_read(int fd, void *data, int datalen)
Definition: app_nbscat.c:97
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 struct ast_format f[]
Definition: format_g726.c:181
int ast_write(struct ast_channel *chan, struct ast_frame *frame)
Write a frame to a channel This function writes the given frame to the indicated channel.
Definition: channel.c:4916
#define AST_FORMAT_SLINEAR
Definition: frame.h:254
int ast_waitfor(struct ast_channel *chan, int ms)
Wait for input on a channel.
Definition: channel.c:3539
struct ast_frame * next
Definition: frame.h:164
Data structure associated with a single frame of data.
Definition: frame.h:142
enum ast_frame_type frametype
Definition: frame.h:144
static int NBScatplay(int fd)
Definition: app_nbscat.c:72
#define ast_frfree(fr)
Definition: frame.h:583
int ast_stopstream(struct ast_channel *c)
Stops a stream.
Definition: file.c:128
static int NBScatplay ( int  fd)
static

Definition at line 72 of file app_nbscat.c.

References ast_close_fds_above_n(), ast_log(), ast_opt_high_priority, ast_safe_fork(), ast_set_priority(), LOCAL_NBSCAT, LOG_WARNING, and NBSCAT.

Referenced by NBScat_exec().

73 {
74  int res;
75 
76  res = ast_safe_fork(0);
77  if (res < 0) {
78  ast_log(LOG_WARNING, "Fork failed\n");
79  }
80 
81  if (res) {
82  return res;
83  }
84 
87 
88  dup2(fd, STDOUT_FILENO);
89  ast_close_fds_above_n(STDERR_FILENO);
90  /* Most commonly installed in /usr/local/bin */
91  execl(NBSCAT, "nbscat8k", "-d", (char *)NULL);
92  execl(LOCAL_NBSCAT, "nbscat8k", "-d", (char *)NULL);
93  fprintf(stderr, "Execute of nbscat8k failed\n");
94  _exit(0);
95 }
#define LOCAL_NBSCAT
Definition: app_nbscat.c:63
int ast_safe_fork(int stop_reaper)
Common routine to safely fork without a chance of a signal handler firing badly in the child...
Definition: app.c:2242
#define LOG_WARNING
Definition: logger.h:144
int ast_set_priority(int)
We set ourselves to a high priority, that we might pre-empt everything else. If your PBX has heavy ac...
Definition: asterisk.c:1650
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
void ast_close_fds_above_n(int n)
Common routine for child processes, to close all fds prior to exec(2)
Definition: app.c:2237
#define NBSCAT
Definition: app_nbscat.c:64
#define ast_opt_high_priority
Definition: options.h:108
static int timed_read ( int  fd,
void *  data,
int  datalen 
)
static

Definition at line 97 of file app_nbscat.c.

References ast_log(), ast_poll, and LOG_NOTICE.

Referenced by NBScat_exec().

98 {
99  int res;
100  struct pollfd fds[1];
101  fds[0].fd = fd;
102  fds[0].events = POLLIN;
103  res = ast_poll(fds, 1, 2000);
104  if (res < 1) {
105  ast_log(LOG_NOTICE, "Selected timed out/errored out with %d\n", res);
106  return -1;
107  }
108  return read(fd, data, datalen);
109 
110 }
#define ast_poll(a, b, c)
Definition: poll-compat.h:88
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
static int unload_module ( void  )
static

Definition at line 210 of file app_nbscat.c.

References ast_unregister_application().

211 {
213 }
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx.c:7705
static char * app
Definition: app_nbscat.c:70

Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Silly NBS Stream Application" , .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 220 of file app_nbscat.c.

char* app = "NBScat"
static

Definition at line 70 of file app_nbscat.c.

Definition at line 220 of file app_nbscat.c.