Wed Jan 8 2020 09:49:39

Asterisk developer's documentation


app_dictate.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2005, Anthony Minessale II
5  *
6  * Anthony Minessale II <anthmct@yahoo.com>
7  *
8  * Donated by Sangoma Technologies <http://www.samgoma.com>
9  *
10  * See http://www.asterisk.org for more information about
11  * the Asterisk project. Please do not directly contact
12  * any of the maintainers of this project for assistance;
13  * the project provides a web site, mailing lists and IRC
14  * channels for your use.
15  *
16  * This program is free software, distributed under the terms of
17  * the GNU General Public License Version 2. See the LICENSE file
18  * at the top of the source tree.
19  */
20 
21 /*! \file
22  *
23  * \brief Virtual Dictation Machine Application For Asterisk
24  *
25  * \author Anthony Minessale II <anthmct@yahoo.com>
26  *
27  * \ingroup applications
28  */
29 
30 /*** MODULEINFO
31  <support_level>extended</support_level>
32  ***/
33 
34 #include "asterisk.h"
35 
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 370642 $")
37 
38 #include <sys/stat.h>
39 
40 #include "asterisk/paths.h" /* use ast_config_AST_SPOOL_DIR */
41 #include "asterisk/file.h"
42 #include "asterisk/pbx.h"
43 #include "asterisk/module.h"
44 #include "asterisk/say.h"
45 #include "asterisk/app.h"
46 
47 /*** DOCUMENTATION
48  <application name="Dictate" language="en_US">
49  <synopsis>
50  Virtual Dictation Machine.
51  </synopsis>
52  <syntax>
53  <parameter name="base_dir" />
54  <parameter name="filename" />
55  </syntax>
56  <description>
57  <para>Start dictation machine using optional <replaceable>base_dir</replaceable> for files.</para>
58  </description>
59  </application>
60  ***/
61 
62 static const char app[] = "Dictate";
63 
64 typedef enum {
65  DFLAG_RECORD = (1 << 0),
66  DFLAG_PLAY = (1 << 1),
67  DFLAG_TRUNC = (1 << 2),
68  DFLAG_PAUSE = (1 << 3),
69 } dflags;
70 
71 typedef enum {
75 } dmodes;
76 
77 #define ast_toggle_flag(it,flag) if(ast_test_flag(it, flag)) ast_clear_flag(it, flag); else ast_set_flag(it, flag)
78 
79 static int play_and_wait(struct ast_channel *chan, char *file, char *digits)
80 {
81  int res = -1;
82  if (!ast_streamfile(chan, file, chan->language)) {
83  res = ast_waitstream(chan, digits);
84  }
85  return res;
86 }
87 
88 static int dictate_exec(struct ast_channel *chan, const char *data)
89 {
90  char *path = NULL, filein[256], *filename = "";
91  char *parse;
93  AST_APP_ARG(base);
94  AST_APP_ARG(filename);
95  );
96  char dftbase[256];
97  char *base;
98  struct ast_flags flags = {0};
99  struct ast_filestream *fs;
100  struct ast_frame *f = NULL;
101  int ffactor = 320 * 80,
102  res = 0,
103  done = 0,
104  oldr = 0,
105  lastop = 0,
106  samples = 0,
107  speed = 1,
108  digit = 0,
109  len = 0,
110  maxlen = 0,
111  mode = 0;
112 
113  snprintf(dftbase, sizeof(dftbase), "%s/dictate", ast_config_AST_SPOOL_DIR);
114  if (!ast_strlen_zero(data)) {
115  parse = ast_strdupa(data);
116  AST_STANDARD_APP_ARGS(args, parse);
117  } else
118  args.argc = 0;
119 
120  if (args.argc && !ast_strlen_zero(args.base)) {
121  base = args.base;
122  } else {
123  base = dftbase;
124  }
125  if (args.argc > 1 && args.filename) {
126  filename = args.filename;
127  }
128  oldr = chan->readformat;
129  if ((res = ast_set_read_format(chan, AST_FORMAT_SLINEAR)) < 0) {
130  ast_log(LOG_WARNING, "Unable to set to linear mode.\n");
131  return -1;
132  }
133 
134  if (chan->_state != AST_STATE_UP) {
135  ast_answer(chan);
136  }
137  ast_safe_sleep(chan, 200);
138  for (res = 0; !res;) {
139  if (ast_strlen_zero(filename)) {
140  if (ast_app_getdata(chan, "dictate/enter_filename", filein, sizeof(filein), 0) ||
141  ast_strlen_zero(filein)) {
142  res = -1;
143  break;
144  }
145  } else {
146  ast_copy_string(filein, filename, sizeof(filein));
147  filename = "";
148  }
149  ast_mkdir(base, 0755);
150  len = strlen(base) + strlen(filein) + 2;
151  if (!path || len > maxlen) {
152  path = ast_alloca(len);
153  memset(path, 0, len);
154  maxlen = len;
155  } else {
156  memset(path, 0, maxlen);
157  }
158 
159  snprintf(path, len, "%s/%s", base, filein);
160  fs = ast_writefile(path, "raw", NULL, O_CREAT|O_APPEND, 0, AST_FILE_MODE);
161  mode = DMODE_PLAY;
162  memset(&flags, 0, sizeof(flags));
163  ast_set_flag(&flags, DFLAG_PAUSE);
164  digit = play_and_wait(chan, "dictate/forhelp", AST_DIGIT_ANY);
165  done = 0;
166  speed = 1;
167  res = 0;
168  lastop = 0;
169  samples = 0;
170  while (!done && ((res = ast_waitfor(chan, -1)) > -1) && fs && (f = ast_read(chan))) {
171  if (digit) {
172  struct ast_frame fr = {AST_FRAME_DTMF, { .integer = digit } };
173  ast_queue_frame(chan, &fr);
174  digit = 0;
175  }
176  if ((f->frametype == AST_FRAME_DTMF)) {
177  int got = 1;
178  switch(mode) {
179  case DMODE_PLAY:
180  switch (f->subclass.integer) {
181  case '1':
182  ast_set_flag(&flags, DFLAG_PAUSE);
183  mode = DMODE_RECORD;
184  break;
185  case '2':
186  speed++;
187  if (speed > 4) {
188  speed = 1;
189  }
190  res = ast_say_number(chan, speed, AST_DIGIT_ANY, chan->language, NULL);
191  break;
192  case '7':
193  samples -= ffactor;
194  if(samples < 0) {
195  samples = 0;
196  }
197  ast_seekstream(fs, samples, SEEK_SET);
198  break;
199  case '8':
200  samples += ffactor;
201  ast_seekstream(fs, samples, SEEK_SET);
202  break;
203 
204  default:
205  got = 0;
206  }
207  break;
208  case DMODE_RECORD:
209  switch (f->subclass.integer) {
210  case '1':
211  ast_set_flag(&flags, DFLAG_PAUSE);
212  mode = DMODE_PLAY;
213  break;
214  case '8':
215  ast_toggle_flag(&flags, DFLAG_TRUNC);
216  lastop = 0;
217  break;
218  default:
219  got = 0;
220  }
221  break;
222  default:
223  got = 0;
224  }
225  if (!got) {
226  switch (f->subclass.integer) {
227  case '#':
228  done = 1;
229  continue;
230  break;
231  case '*':
232  ast_toggle_flag(&flags, DFLAG_PAUSE);
233  if (ast_test_flag(&flags, DFLAG_PAUSE)) {
234  digit = play_and_wait(chan, "dictate/pause", AST_DIGIT_ANY);
235  } else {
236  digit = play_and_wait(chan, mode == DMODE_PLAY ? "dictate/playback" : "dictate/record", AST_DIGIT_ANY);
237  }
238  break;
239  case '0':
240  ast_set_flag(&flags, DFLAG_PAUSE);
241  digit = play_and_wait(chan, "dictate/paused", AST_DIGIT_ANY);
242  switch(mode) {
243  case DMODE_PLAY:
244  digit = play_and_wait(chan, "dictate/play_help", AST_DIGIT_ANY);
245  break;
246  case DMODE_RECORD:
247  digit = play_and_wait(chan, "dictate/record_help", AST_DIGIT_ANY);
248  break;
249  }
250  if (digit == 0) {
251  digit = play_and_wait(chan, "dictate/both_help", AST_DIGIT_ANY);
252  } else if (digit < 0) {
253  done = 1;
254  break;
255  }
256  break;
257  }
258  }
259 
260  } else if (f->frametype == AST_FRAME_VOICE) {
261  switch(mode) {
262  struct ast_frame *fr;
263  int x;
264  case DMODE_PLAY:
265  if (lastop != DMODE_PLAY) {
266  if (ast_test_flag(&flags, DFLAG_PAUSE)) {
267  digit = play_and_wait(chan, "dictate/playback_mode", AST_DIGIT_ANY);
268  if (digit == 0) {
269  digit = play_and_wait(chan, "dictate/paused", AST_DIGIT_ANY);
270  } else if (digit < 0) {
271  break;
272  }
273  }
274  if (lastop != DFLAG_PLAY) {
275  lastop = DFLAG_PLAY;
276  ast_closestream(fs);
277  if (!(fs = ast_openstream(chan, path, chan->language)))
278  break;
279  ast_seekstream(fs, samples, SEEK_SET);
280  chan->stream = NULL;
281  }
282  lastop = DMODE_PLAY;
283  }
284 
285  if (!ast_test_flag(&flags, DFLAG_PAUSE)) {
286  for (x = 0; x < speed; x++) {
287  if ((fr = ast_readframe(fs))) {
288  ast_write(chan, fr);
289  samples += fr->samples;
290  ast_frfree(fr);
291  fr = NULL;
292  } else {
293  samples = 0;
294  ast_seekstream(fs, 0, SEEK_SET);
295  }
296  }
297  }
298  break;
299  case DMODE_RECORD:
300  if (lastop != DMODE_RECORD) {
301  int oflags = O_CREAT | O_WRONLY;
302  if (ast_test_flag(&flags, DFLAG_PAUSE)) {
303  digit = play_and_wait(chan, "dictate/record_mode", AST_DIGIT_ANY);
304  if (digit == 0) {
305  digit = play_and_wait(chan, "dictate/paused", AST_DIGIT_ANY);
306  } else if (digit < 0) {
307  break;
308  }
309  }
310  lastop = DMODE_RECORD;
311  ast_closestream(fs);
312  if ( ast_test_flag(&flags, DFLAG_TRUNC)) {
313  oflags |= O_TRUNC;
314  digit = play_and_wait(chan, "dictate/truncating_audio", AST_DIGIT_ANY);
315  } else {
316  oflags |= O_APPEND;
317  }
318  fs = ast_writefile(path, "raw", NULL, oflags, 0, AST_FILE_MODE);
319  if (ast_test_flag(&flags, DFLAG_TRUNC)) {
320  ast_seekstream(fs, 0, SEEK_SET);
321  ast_clear_flag(&flags, DFLAG_TRUNC);
322  } else {
323  ast_seekstream(fs, 0, SEEK_END);
324  }
325  }
326  if (!ast_test_flag(&flags, DFLAG_PAUSE)) {
327  res = ast_writestream(fs, f);
328  }
329  break;
330  }
331 
332  }
333 
334  ast_frfree(f);
335  }
336  }
337  if (oldr) {
338  ast_set_read_format(chan, oldr);
339  }
340  return 0;
341 }
342 
343 static int unload_module(void)
344 {
345  int res;
346  res = ast_unregister_application(app);
347  return res;
348 }
349 
350 static int load_module(void)
351 {
353 }
354 
355 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Virtual Dictation Machine");
int ast_safe_sleep(struct ast_channel *chan, int ms)
Wait for a specified amount of time, looking for hangups.
Definition: channel.c:1916
union ast_frame_subclass subclass
Definition: frame.h:146
Main Channel structure associated with a channel.
Definition: channel.h:742
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:396
int ast_streamfile(struct ast_channel *c, const char *filename, const char *preflang)
Streams a file.
Definition: file.c:946
Asterisk main include file. File version handling, generic pbx functions.
int ast_app_getdata(struct ast_channel *c, const char *prompt, char *s, int maxlen, int timeout)
Plays a stream and gets DTMF data from a channel.
Definition: app.c:178
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: utils.h:653
#define AST_DIGIT_ANY
Definition: file.h:47
#define ast_test_flag(p, flag)
Definition: utils.h:63
#define ast_toggle_flag(it, flag)
Definition: app_dictate.c:77
#define ast_set_flag(p, flag)
Definition: utils.h:70
#define LOG_WARNING
Definition: logger.h:144
#define AST_FRAME_DTMF
Definition: frame.h:128
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
Definition: app.h:572
dflags
Definition: app_dictate.c:64
struct ast_frame * ast_read(struct ast_channel *chan)
Reads a frame.
Definition: channel.c:4383
Generic File Format Support. Should be included by clients of the file handling routines. File service providers should instead include mod_format.h.
dmodes
Definition: app_dictate.c:71
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx.c:7705
#define AST_FILE_MODE
Definition: asterisk.h:36
int ast_set_read_format(struct ast_channel *chan, format_t format)
Sets read format on channel chan Set read format for channel to whichever component of &quot;format&quot; is be...
Definition: channel.c:5301
Asterisk file paths, configured in asterisk.conf.
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
Core PBX routines and definitions.
int ast_queue_frame(struct ast_channel *chan, struct ast_frame *f)
Queue one or more frames to a channel&#39;s frame queue.
Definition: channel.c:1558
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: utils.h:663
static int play_and_wait(struct ast_channel *chan, char *file, char *digits)
Definition: app_dictate.c:79
static struct @350 args
struct ast_frame * ast_readframe(struct ast_filestream *s)
Read a frame from a filestream.
Definition: file.c:737
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
enum ast_channel_state _state
Definition: channel.h:839
struct ast_filestream * ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
Starts writing a file.
Definition: file.c:1049
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 int load_module(void)
Definition: app_dictate.c:350
int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence)
Seeks into stream.
Definition: file.c:879
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1858
int ast_closestream(struct ast_filestream *f)
Closes a stream.
Definition: file.c:904
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
const char * ast_config_AST_SPOOL_DIR
Definition: asterisk.c:259
Structure used to handle boolean flags.
Definition: utils.h:200
#define ast_clear_flag(p, flag)
Definition: utils.h:77
static const char app[]
Definition: app_dictate.c:62
This structure is allocated by file.c in one chunk, together with buf_size and desc_size bytes of mem...
Definition: mod_format.h:100
#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
int ast_say_number(struct ast_channel *chan, int num, const char *ints, const char *lang, const char *options)
says a number
Definition: channel.c:8397
format_t readformat
Definition: channel.h:853
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:223
int ast_writestream(struct ast_filestream *fs, struct ast_frame *f)
Writes a frame to a stream.
Definition: file.c:150
int ast_waitstream(struct ast_channel *c, const char *breakon)
Waits for a stream to stop or digit to be pressed.
Definition: file.c:1343
int ast_answer(struct ast_channel *chan)
Answer a channel.
Definition: channel.c:3086
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
static int dictate_exec(struct ast_channel *chan, const char *data)
Definition: app_dictate.c:88
static int unload_module(void)
Definition: app_dictate.c:343
#define AST_APP_ARG(name)
Define an application argument.
Definition: app.h:555
enum ast_frame_type frametype
Definition: frame.h:144
#define ast_frfree(fr)
Definition: frame.h:583
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
Definition: app.h:604
struct ast_filestream * stream
Definition: channel.h:757
Say numbers and dates (maybe words one day too)
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
Asterisk module definitions.
const ast_string_field language
Definition: channel.h:787
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:437
struct ast_filestream * ast_openstream(struct ast_channel *chan, const char *filename, const char *preflang)
Opens stream for use in seeking, playing.
Definition: file.c:636
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180
int samples
Definition: frame.h:150
int ast_mkdir(const char *path, int mode)
Recursively create directory path.
Definition: utils.c:2151