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

 AST_MODULE_INFO_STANDARD_DEPRECATED (ASTERISK_GPL_KEY, "Silly NBS Stream Application")
 
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 char * app = "NBScat"
 

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

◆ AF_LOCAL

#define AF_LOCAL   AF_UNIX

Definition at line 66 of file app_nbscat.c.

Referenced by NBScat_exec().

◆ LOCAL_NBSCAT

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

Definition at line 62 of file app_nbscat.c.

Referenced by NBScatplay().

◆ NBSCAT

#define NBSCAT   "/usr/bin/nbscat8k"

Definition at line 63 of file app_nbscat.c.

Referenced by NBScatplay().

Function Documentation

◆ AST_MODULE_INFO_STANDARD_DEPRECATED()

AST_MODULE_INFO_STANDARD_DEPRECATED ( ASTERISK_GPL_KEY  ,
"Silly NBS Stream Application"   
)

Referenced by load_module().

◆ load_module()

static int load_module ( void  )
static

Definition at line 218 of file app_nbscat.c.

References app, AST_MODULE_INFO_STANDARD_DEPRECATED(), ast_register_application_xml, ASTERISK_GPL_KEY, and NBScat_exec().

219 {
221 }
static int NBScat_exec(struct ast_channel *chan, const char *data)
Definition: app_nbscat.c:111
static char * app
Definition: app_nbscat.c:69
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:626

◆ NBScat_exec()

static int NBScat_exec ( struct ast_channel chan,
const char *  data 
)
static

Definition at line 111 of file app_nbscat.c.

References AF_LOCAL, ao2_bump, ao2_cleanup, ast_channel_writeformat(), ast_debug, ast_format_slin, 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(), ast_frame::frametype, LOG_WARNING, NBScatplay(), ast_frame::offset, and timed_read().

Referenced by load_module().

112 {
113  int res=0;
114  int fds[2];
115  int ms = -1;
116  int pid = -1;
117  struct ast_format *owriteformat;
118  struct timeval next;
119  struct ast_frame *f;
120  struct myframe {
121  struct ast_frame f;
123  short frdata[160];
124  } myf;
125 
126  if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds)) {
127  ast_log(LOG_WARNING, "Unable to create socketpair\n");
128  return -1;
129  }
130 
131  ast_stopstream(chan);
132 
133  owriteformat = ao2_bump(ast_channel_writeformat(chan));
135  if (res < 0) {
136  ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
137  ao2_cleanup(owriteformat);
138  return -1;
139  }
140 
141  myf.f.frametype = AST_FRAME_VOICE;
142  myf.f.subclass.format = ast_format_slin;
143  myf.f.mallocd = 0;
144  myf.f.offset = AST_FRIENDLY_OFFSET;
145  myf.f.src = __PRETTY_FUNCTION__;
146  myf.f.delivery.tv_sec = 0;
147  myf.f.delivery.tv_usec = 0;
148  myf.f.data.ptr = myf.frdata;
149 
150  res = NBScatplay(fds[1]);
151  /* Wait 1000 ms first */
152  next = ast_tvnow();
153  next.tv_sec += 1;
154  if (res >= 0) {
155  pid = res;
156  /* Order is important -- there's almost always going to be mp3... we want to prioritize the
157  user */
158  for (;;) {
159  ms = ast_tvdiff_ms(next, ast_tvnow());
160  if (ms <= 0) {
161  res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata));
162  if (res > 0) {
163  myf.f.datalen = res;
164  myf.f.samples = res / 2;
165  if (ast_write(chan, &myf.f) < 0) {
166  res = -1;
167  break;
168  }
169  } else {
170  ast_debug(1, "No more mp3\n");
171  res = 0;
172  break;
173  }
174  next = ast_tvadd(next, ast_samp2tv(myf.f.samples, 8000));
175  } else {
176  ms = ast_waitfor(chan, ms);
177  if (ms < 0) {
178  ast_debug(1, "Hangup detected\n");
179  res = -1;
180  break;
181  }
182  if (ms) {
183  f = ast_read(chan);
184  if (!f) {
185  ast_debug(1, "Null frame == hangup() detected\n");
186  res = -1;
187  break;
188  }
189  if (f->frametype == AST_FRAME_DTMF) {
190  ast_debug(1, "User pressed a key\n");
191  ast_frfree(f);
192  res = 0;
193  break;
194  }
195  ast_frfree(f);
196  }
197  }
198  }
199  }
200  close(fds[0]);
201  close(fds[1]);
202  ast_frfree(&myf.f);
203 
204  if (pid > -1)
205  kill(pid, SIGKILL);
206  if (!res && owriteformat)
207  ast_set_write_format(chan, owriteformat);
208  ao2_cleanup(owriteformat);
209 
210  return res;
211 }
#define AF_LOCAL
Definition: app_nbscat.c:66
#define LOG_WARNING
Definition: logger.h:274
struct ast_frame * ast_read(struct ast_channel *chan)
Reads a frame.
Definition: channel.c:4251
Definition of a media format.
Definition: format.c:43
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:150
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:98
#define AST_FRAME_DTMF
#define ao2_bump(obj)
Definition: astobj2.h:491
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:444
#define ast_log
Definition: astobj2.c:42
#define AST_FRIENDLY_OFFSET
Offset into a frame&#39;s data buffer.
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:238
int ast_set_write_format(struct ast_channel *chan, struct ast_format *format)
Sets write format on channel chan.
Definition: channel.c:5765
struct timeval ast_tvadd(struct timeval a, struct timeval b)
Returns the sum of two timevals a + b.
Definition: extconf.c:2283
static int timed_read(int fd, void *data, int datalen)
Definition: app_nbscat.c:96
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:5068
int ast_waitfor(struct ast_channel *chan, int ms)
Wait for input on a channel.
Definition: channel.c:3137
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
struct ast_frame * next
#define ast_frfree(fr)
Data structure associated with a single frame of data.
enum ast_frame_type frametype
static int NBScatplay(int fd)
Definition: app_nbscat.c:71
struct ast_format * ast_format_slin
Built-in cached signed linear 8kHz format.
Definition: format_cache.c:41
struct ast_format * ast_channel_writeformat(struct ast_channel *chan)
int ast_stopstream(struct ast_channel *c)
Stops a stream.
Definition: file.c:187

◆ NBScatplay()

static int NBScatplay ( int  fd)
static

Definition at line 71 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, NBSCAT, and NULL.

Referenced by NBScat_exec().

72 {
73  int res;
74 
75  res = ast_safe_fork(0);
76  if (res < 0) {
77  ast_log(LOG_WARNING, "Fork failed\n");
78  }
79 
80  if (res) {
81  return res;
82  }
83 
86 
87  dup2(fd, STDOUT_FILENO);
88  ast_close_fds_above_n(STDERR_FILENO);
89  /* Most commonly installed in /usr/local/bin */
90  execl(NBSCAT, "nbscat8k", "-d", (char *)NULL);
91  execl(LOCAL_NBSCAT, "nbscat8k", "-d", (char *)NULL);
92  fprintf(stderr, "Execute of nbscat8k failed\n");
93  _exit(0);
94 }
#define LOCAL_NBSCAT
Definition: app_nbscat.c:62
#define LOG_WARNING
Definition: logger.h:274
void ast_close_fds_above_n(int n)
Common routine for child processes, to close all fds prior to exec(2)
Definition: main/app.c:2965
#define NULL
Definition: resample.c:96
#define ast_log
Definition: astobj2.c:42
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:1799
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: main/app.c:2970
#define NBSCAT
Definition: app_nbscat.c:63
#define ast_opt_high_priority
Definition: options.h:110

◆ timed_read()

static int timed_read ( int  fd,
void *  data,
int  datalen 
)
static

Definition at line 96 of file app_nbscat.c.

References ast_log, ast_poll, and LOG_NOTICE.

Referenced by NBScat_exec().

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

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 213 of file app_nbscat.c.

References app, and ast_unregister_application().

214 {
216 }
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
static char * app
Definition: app_nbscat.c:69

Variable Documentation

◆ app

char* app = "NBScat"
static

Definition at line 69 of file app_nbscat.c.

Referenced by load_module(), and unload_module().