4ad4a15fd575ee0d781ee5dea235f30d5e934300
[asterisk/asterisk.git] / apps / app_record.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Trivial application to record a sound file
5  * 
6  * Copyright (C) 2001, Linux Support Services, Inc.
7  *
8  * Matthew Fredrickson <creslin@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13  
14 #include <asterisk/lock.h>
15 #include <asterisk/file.h>
16 #include <asterisk/logger.h>
17 #include <asterisk/channel.h>
18 #include <asterisk/pbx.h>
19 #include <asterisk/module.h>
20 #include <asterisk/translate.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <pthread.h>
24
25 static char *tdesc = "Trivial Record Application";
26
27 static char *app = "Record";
28
29 static char *synopsis = "Record to a file";
30
31 static char *descrip = 
32 "  Record(filename:extension): Records from the  channel into a given\n"
33 "filename. If the file exists it will be overwritten. The 'extension'\n"
34 "is the extension of the file type  to  be  recorded (wav, gsm, etc).\n"
35 "Returns -1 when the user hangs up.\n";
36
37 STANDARD_LOCAL_USER;
38
39 LOCAL_USER_DECL;
40
41 static int record_exec(struct ast_channel *chan, void *data)
42 {
43         int res = 0;
44         int count = 0;
45         int percentflag = 0;
46         char fil[256];
47         char tmp[256];
48         char ext[10];
49         char * vdata;  /* Used so I don't have to typecast every use of *data */
50         int i = 0;
51         int j = 0;
52         
53         struct ast_filestream *s = '\0';
54         struct localuser *u;
55         struct ast_frame *f = NULL;
56         
57         vdata = data; /* explained above */
58         
59
60         /* The next few lines of code parse out the filename and header from the input string */
61         if (!vdata) { /* no data implies no filename or anything is present */
62                 ast_log(LOG_WARNING, "Record requires an argument (filename)\n");
63                 return -1;
64         }
65         
66         for (; vdata[i] && (vdata[i] != ':') ; i++ ) {
67                 if ((vdata[i] == '%') && (vdata[i+1] == 'd')) {
68                         percentflag = 1;                      /* the wildcard is used */
69                 }
70                 
71                 if (i == strlen(vdata) ) {
72                         ast_log(LOG_WARNING, "No extension found\n");
73                         return -1;
74                 }
75                 fil[i] = vdata[i];
76         }
77         fil[i++] = '\0';
78
79         for (; j < 10 && i < strlen(data); i++, j++)
80                 ext[j] = vdata[i];
81         ext[j] = '\0';
82         /* done parsing */
83         
84         
85         /* these are to allow the use of the %d in the config file for a wild card of sort to
86           create a new file with the inputed name scheme */
87         if (percentflag) {
88                 do {
89                         snprintf(tmp, 256, fil, count);
90                         count++;
91                 } while ( ast_fileexists(tmp, ext, chan->language) != -1 );
92         } else
93                 strncpy(tmp, fil, 256-1);
94         /* end of routine mentioned */
95
96         LOCAL_USER_ADD(u);
97
98         if (chan->state != AST_STATE_UP) {
99                 res = ast_answer(chan); /* Shouldn't need this, but checking to see if channel is already answered
100                                          * Theoretically asterisk should already have answered before running the app */
101         }
102         
103         if (!res) {
104                 /* Some code to play a nice little beep to signify the start of the record operation */
105                 res = ast_streamfile(chan, "beep", chan->language);
106                 if (!res) {
107                         res = ast_waitstream(chan, "");
108                 } else {
109                         ast_log(LOG_WARNING, "ast_streamfile failed on %s\n", chan->name);
110                 }
111                 ast_stopstream(chan);
112                 /* The end of beep code.  Now the recording starts */
113                 s = ast_writefile( tmp, ext, NULL, O_CREAT|O_TRUNC|O_WRONLY , 0, 0644);
114         
115                 if (s) {
116                         while (ast_waitfor(chan, -1) > -1) {
117                                 f = ast_read(chan);
118                                 if (!f) {
119                                         res = -1;
120                                         break;
121                                 }
122                                 if (f->frametype == AST_FRAME_VOICE) {
123                                         res = ast_writestream(s, f);
124                                         
125                                         if (res) {
126                                                 ast_log(LOG_WARNING, "Problem writing frame\n");
127                                                 break;
128                                         }
129                                 }
130                                 if ((f->frametype == AST_FRAME_DTMF) &&
131                                         (f->subclass == '#')) {
132                                         ast_frfree(f);
133                                         break;
134                                 }
135                                 ast_frfree(f);
136                         }
137                         if (!f) {
138                                         ast_log(LOG_DEBUG, "Got hangup\n");
139                                         res = -1;
140                         }
141                         ast_closestream(s);
142                 } else                  
143                         ast_log(LOG_WARNING, "Could not create file %s\n", fil);
144         } else
145                 ast_log(LOG_WARNING, "Could not answer channel '%s'\n", chan->name);
146
147         LOCAL_USER_REMOVE(u);
148         return res;
149 }
150
151 int unload_module(void)
152 {
153         STANDARD_HANGUP_LOCALUSERS;
154         return ast_unregister_application(app);
155 }
156
157 int load_module(void)
158 {
159         return ast_register_application(app, record_exec, synopsis, descrip);
160 }
161
162 char *description(void)
163 {
164         return tdesc;
165 }
166
167 int usecount(void)
168 {
169         int res;
170         STANDARD_USECOUNT(res);
171         return res;
172 }
173
174 char *key()
175 {
176         return ASTERISK_GPL_KEY;
177 }