media formats: re-architect handling of media for performance improvements
[asterisk/asterisk.git] / apps / app_milliwatt.c
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 Digital Milliwatt Test
22  *
23  * \author Mark Spencer <markster@digium.com>
24  * 
25  * \ingroup applications
26  */
27
28 /*** MODULEINFO
29         <support_level>core</support_level>
30  ***/
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 #include "asterisk/module.h"
37 #include "asterisk/channel.h"
38 #include "asterisk/pbx.h"
39 #include "asterisk/indications.h"
40 #include "asterisk/format_cache.h"
41
42 /*** DOCUMENTATION
43         <application name="Milliwatt" language="en_US">
44                 <synopsis>
45                         Generate a Constant 1004Hz tone at 0dbm (mu-law).
46                 </synopsis>
47                 <syntax>
48                         <parameter name="options">
49                                 <optionlist>
50                                         <option name="o">
51                                                 <para>Generate the tone at 1000Hz like previous version.</para>
52                                         </option>
53                                 </optionlist>
54                         </parameter>
55                 </syntax>
56                 <description>
57                         <para>Previous versions of this application generated the tone at 1000Hz.  If for
58                         some reason you would prefer that behavior, supply the <literal>o</literal> option to get the
59                         old behavior.</para>
60                 </description>
61         </application>
62  ***/
63
64 static const char app[] = "Milliwatt";
65
66 static const char digital_milliwatt[] = {0x1e,0x0b,0x0b,0x1e,0x9e,0x8b,0x8b,0x9e} ;
67
68 static void *milliwatt_alloc(struct ast_channel *chan, void *params)
69 {
70         return ast_calloc(1, sizeof(int));
71 }
72
73 static void milliwatt_release(struct ast_channel *chan, void *data)
74 {
75         ast_free(data);
76         return;
77 }
78
79 static int milliwatt_generate(struct ast_channel *chan, void *data, int len, int samples)
80 {
81         unsigned char buf[AST_FRIENDLY_OFFSET + 640];
82         const int maxsamples = ARRAY_LEN(buf) - (AST_FRIENDLY_OFFSET / sizeof(buf[0]));
83         int i, *indexp = (int *) data, res;
84         struct ast_frame wf = {
85                 .frametype = AST_FRAME_VOICE,
86                 .offset = AST_FRIENDLY_OFFSET,
87                 .src = __FUNCTION__,
88         };
89
90         wf.subclass.format = ast_format_ulaw;
91         wf.data.ptr = buf + AST_FRIENDLY_OFFSET;
92
93         /* Instead of len, use samples, because channel.c generator_force
94         * generate(chan, tmp, 0, 160) ignores len. In any case, len is
95         * a multiple of samples, given by number of samples times bytes per
96         * sample. In the case of ulaw, len = samples. for signed linear
97         * len = 2 * samples */
98         if (samples > maxsamples) {
99                 ast_log(LOG_WARNING, "Only doing %d samples (%d requested)\n", maxsamples, samples);
100                 samples = maxsamples;
101         }
102
103         len = samples * sizeof (buf[0]);
104         wf.datalen = len;
105         wf.samples = samples;
106
107         /* create a buffer containing the digital milliwatt pattern */
108         for (i = 0; i < len; i++) {
109                 buf[AST_FRIENDLY_OFFSET + i] = digital_milliwatt[(*indexp)++];
110                 *indexp &= 7;
111         }
112
113         res = ast_write(chan, &wf);
114         ast_frfree(&wf);
115
116         if (res < 0) {
117                 ast_log(LOG_WARNING,"Failed to write frame to '%s': %s\n",ast_channel_name(chan),strerror(errno));
118                 return -1;
119         }
120
121         return 0;
122 }
123
124 static struct ast_generator milliwattgen = {
125         .alloc = milliwatt_alloc,
126         .release = milliwatt_release,
127         .generate = milliwatt_generate,
128 };
129
130 static int old_milliwatt_exec(struct ast_channel *chan)
131 {
132         ast_set_write_format(chan, ast_format_ulaw);
133         ast_set_read_format(chan, ast_format_ulaw);
134
135         if (ast_channel_state(chan) != AST_STATE_UP) {
136                 ast_answer(chan);
137         }
138
139         if (ast_activate_generator(chan,&milliwattgen,"milliwatt") < 0) {
140                 ast_log(LOG_WARNING,"Failed to activate generator on '%s'\n",ast_channel_name(chan));
141                 return -1;
142         }
143
144         while (!ast_safe_sleep(chan, 10000))
145                 ;
146
147         ast_deactivate_generator(chan);
148
149         return -1;
150 }
151
152 static int milliwatt_exec(struct ast_channel *chan, const char *data)
153 {
154         const char *options = data;
155         int res = -1;
156
157         if (!ast_strlen_zero(options) && strchr(options, 'o')) {
158                 return old_milliwatt_exec(chan);
159         }
160
161         res = ast_playtones_start(chan, 23255, "1004/1000", 0);
162
163         while (!res) {
164                 res = ast_safe_sleep(chan, 10000);
165         }
166
167         return res;
168 }
169
170 static int unload_module(void)
171 {
172         return ast_unregister_application(app);
173 }
174
175 static int load_module(void)
176 {
177         return ast_register_application_xml(app, milliwatt_exec);
178 }
179
180 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Digital Milliwatt (mu-law) Test Application");