Wed Jan 8 2020 09:49:40

Asterisk developer's documentation


app_nbscat.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.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  * \brief Silly application to play an NBScat file -- uses nbscat8k
22  *
23  * \author Mark Spencer <markster@digium.com>
24  *
25  * \ingroup applications
26  */
27 
28 /*** MODULEINFO
29  <support_level>extended</support_level>
30  ***/
31 
32 #include "asterisk.h"
33 
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328209 $")
35 
36 #include <fcntl.h>
37 #include <sys/time.h>
38 #include <sys/socket.h>
39 #include <signal.h>
40 
41 #include "asterisk/lock.h"
42 #include "asterisk/file.h"
43 #include "asterisk/channel.h"
44 #include "asterisk/frame.h"
45 #include "asterisk/pbx.h"
46 #include "asterisk/module.h"
47 #include "asterisk/translate.h"
48 #include "asterisk/app.h"
49 
50 /*** DOCUMENTATION
51  <application name="NBScat" language="en_US">
52  <synopsis>
53  Play an NBS local stream.
54  </synopsis>
55  <syntax />
56  <description>
57  <para>Executes nbscat to listen to the local NBS stream.
58  User can exit by pressing any key.</para>
59  </description>
60  </application>
61  ***/
62 
63 #define LOCAL_NBSCAT "/usr/local/bin/nbscat8k"
64 #define NBSCAT "/usr/bin/nbscat8k"
65 
66 #ifndef AF_LOCAL
67 #define AF_LOCAL AF_UNIX
68 #endif
69 
70 static char *app = "NBScat";
71 
72 static int NBScatplay(int fd)
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 }
96 
97 static int timed_read(int fd, void *data, int datalen)
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 }
111 
112 static int NBScat_exec(struct ast_channel *chan, const char *data)
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 }
209 
210 static int unload_module(void)
211 {
212  return ast_unregister_application(app);
213 }
214 
215 static int load_module(void)
216 {
218 }
219 
220 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Silly NBS Stream Application");
#define LOCAL_NBSCAT
Definition: app_nbscat.c:63
Main Channel structure associated with a channel.
Definition: channel.h:742
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:396
Asterisk locking-related definitions:
Asterisk main include file. File version handling, generic pbx functions.
int offset
Definition: frame.h:156
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
format_t writeformat
Definition: channel.h:854
Support for translation of data formats. translate.c.
#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
static int NBScat_exec(struct ast_channel *chan, const char *data)
Definition: app_nbscat.c:112
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
Generic File Format Support. Should be included by clients of the file handling routines. File service providers should instead include mod_format.h.
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx.c:7705
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
General Asterisk PBX channel definitions.
#define AST_FRIENDLY_OFFSET
Offset into a frame&#39;s data buffer.
Definition: frame.h:204
#define ast_poll(a, b, c)
Definition: poll-compat.h:88
Asterisk internal frame definitions.
int datalen
Definition: frame.h:148
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
Core PBX routines and definitions.
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
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 char * app
Definition: app_nbscat.c:70
#define LOG_NOTICE
Definition: logger.h:133
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
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
static int load_module(void)
Definition: app_nbscat.c:215
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
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
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
Asterisk module definitions.
#define NBSCAT
Definition: app_nbscat.c:64
union ast_frame::@172 data
static int unload_module(void)
Definition: app_nbscat.c:210
int ast_stopstream(struct ast_channel *c)
Stops a stream.
Definition: file.c:128
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:437
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180
#define ast_opt_high_priority
Definition: options.h:108