Asterisk - The Open Source Telephony Project  18.5.0
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 <[email protected]>
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 <[email protected]>
24  *
25  * \ingroup applications
26  */
27 
28 /*** MODULEINFO
29  <support_level>deprecated</support_level>
30  ***/
31 
32 #include "asterisk.h"
33 
34 #include <fcntl.h>
35 #include <sys/time.h>
36 #include <sys/socket.h>
37 #include <signal.h>
38 
39 #include "asterisk/lock.h"
40 #include "asterisk/file.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/frame.h"
43 #include "asterisk/pbx.h"
44 #include "asterisk/module.h"
45 #include "asterisk/translate.h"
46 #include "asterisk/app.h"
47 #include "asterisk/format_cache.h"
48 
49 /*** DOCUMENTATION
50  <application name="NBScat" language="en_US">
51  <synopsis>
52  Play an NBS local stream.
53  </synopsis>
54  <syntax />
55  <description>
56  <para>Executes nbscat to listen to the local NBS stream.
57  User can exit by pressing any key.</para>
58  </description>
59  </application>
60  ***/
61 
62 #define LOCAL_NBSCAT "/usr/local/bin/nbscat8k"
63 #define NBSCAT "/usr/bin/nbscat8k"
64 
65 #ifndef AF_LOCAL
66 #define AF_LOCAL AF_UNIX
67 #endif
68 
69 static char *app = "NBScat";
70 
71 static int NBScatplay(int fd)
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 }
95 
96 static int timed_read(int fd, void *data, int datalen)
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 }
110 
111 static int NBScat_exec(struct ast_channel *chan, const char *data)
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 }
212 
213 static int unload_module(void)
214 {
216 }
217 
218 static int load_module(void)
219 {
221 }
222 
223 AST_MODULE_INFO_STANDARD_DEPRECATED(ASTERISK_GPL_KEY, "Silly NBS Stream Application");
#define LOCAL_NBSCAT
Definition: app_nbscat.c:62
AST_MODULE_INFO_STANDARD_DEPRECATED(ASTERISK_GPL_KEY, "Silly NBS Stream Application")
Main Channel structure associated with a channel.
Asterisk locking-related definitions:
Asterisk main include file. File version handling, generic pbx functions.
Support for translation of data formats. translate.c.
#define AF_LOCAL
Definition: app_nbscat.c:66
#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:3042
struct ast_frame * ast_read(struct ast_channel *chan)
Reads a frame.
Definition: channel.c:4302
static int NBScat_exec(struct ast_channel *chan, const char *data)
Definition: app_nbscat.c:111
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
Generic File Format Support. Should be included by clients of the file handling routines. File service providers should instead include mod_format.h.
#define NULL
Definition: resample.c:96
#define AST_FRAME_DTMF
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
#define ao2_bump(obj)
Definition: astobj2.h:491
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
General Asterisk PBX channel definitions.
#define AST_FRIENDLY_OFFSET
Offset into a frame&#39;s data buffer.
#define ast_poll(a, b, c)
Definition: poll-compat.h:88
Asterisk internal frame definitions.
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:5890
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: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:3047
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
static char * app
Definition: app_nbscat.c:69
#define LOG_NOTICE
Definition: logger.h:263
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:5189
int ast_waitfor(struct ast_channel *chan, int ms)
Wait for input on a channel.
Definition: channel.c:3171
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
static int load_module(void)
Definition: app_nbscat.c:218
#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
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
struct ast_format * ast_format_slin
Built-in cached signed linear 8kHz format.
Definition: format_cache.c:41
Asterisk module definitions.
struct ast_format * ast_channel_writeformat(struct ast_channel *chan)
#define NBSCAT
Definition: app_nbscat.c:63
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
static int unload_module(void)
Definition: app_nbscat.c:213
int ast_stopstream(struct ast_channel *c)
Stops a stream.
Definition: file.c:187
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:626
#define ast_opt_high_priority
Definition: options.h:110
Media Format Cache API.