Bug Summary

File:src/mod/formats/mod_tone_stream/mod_tone_stream.c
Location:line 174, column 3
Description:Value stored to 'fd' is never read

Annotated Source Code

1/*
2 * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3 * Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
4 *
5 * Version: MPL 1.1
6 *
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
11 *
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
15 * License.
16 *
17 * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
18 *
19 * The Initial Developer of the Original Code is
20 * Anthony Minessale II <anthm@freeswitch.org>
21 * Portions created by the Initial Developer are Copyright (C)
22 * the Initial Developer. All Rights Reserved.
23 *
24 * Contributor(s):
25 *
26 * Anthony Minessale II <anthm@freeswitch.org>
27 *
28 *
29 * mod_tone_stream.c -- Tone Generation Stream
30 *
31 */
32#include <switch.h>
33
34SWITCH_MODULE_LOAD_FUNCTION(mod_tone_stream_load)switch_status_t mod_tone_stream_load (switch_loadable_module_interface_t
**module_interface, switch_memory_pool_t *pool)
;
35SWITCH_MODULE_DEFINITION(mod_tone_stream, mod_tone_stream_load, NULL, NULL)static const char modname[] = "mod_tone_stream" ; __attribute__
((visibility("default"))) switch_loadable_module_function_table_t
mod_tone_stream_module_interface = { 5, mod_tone_stream_load
, ((void*)0), ((void*)0), SMODF_NONE }
;
36SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_tone_stream_shutdown)switch_status_t mod_tone_stream_shutdown (void);
37
38struct silence_handle {
39 int32_t samples;
40 int silence;
41 int forever;
42};
43
44static switch_status_t silence_stream_file_open(switch_file_handle_t *handle, const char *path)
45{
46
47 struct silence_handle *sh;
48 int ms;
49 char *p;
50
51 sh = switch_core_alloc(handle->memory_pool, sizeof(*sh))switch_core_perform_alloc(handle->memory_pool, sizeof(*sh)
, "mod_tone_stream.c", (const char *)__func__, 51)
;
52
53 ms = atoi(path);
54
55 if (ms > 0) {
56 sh->samples = (handle->samplerate / 1000) * ms;
57 } else {
58 sh->samples = 0;
59 sh->forever = 1;
60 }
61
62 if ((p = strchr(path, ',')(__extension__ (__builtin_constant_p (',') && !__builtin_constant_p
(path) && (',') == '\0' ? (char *) __rawmemchr (path
, ',') : __builtin_strchr (path, ',')))
)) {
63 p++;
64 ms = atoi(p);
65 if (ms > 0) {
66 sh->silence = ms;
67 }
68 }
69
70 handle->channels = 1;
71 handle->private_info = sh;
72
73 return SWITCH_STATUS_SUCCESS;
74}
75
76static switch_status_t silence_stream_file_close(switch_file_handle_t *handle)
77{
78 return SWITCH_STATUS_SUCCESS;
79}
80
81static switch_status_t silence_stream_file_read(switch_file_handle_t *handle, void *data, size_t *len)
82{
83 struct silence_handle *sh = handle->private_info;
84
85 if (!sh->forever) {
86 if (sh->samples <= 0) {
87 return SWITCH_STATUS_FALSE;
88 }
89
90 if (*len > (size_t) sh->samples) {
91 *len = sh->samples;
92 }
93
94 sh->samples -= (int32_t)*len;
95 }
96
97 switch_generate_sln_silence((int16_t *) data, (uint32_t)*len, handle->channels,
98 sh->silence ? sh->silence : (uint32_t)-1);
99
100 return SWITCH_STATUS_SUCCESS;
101}
102
103
104static int teletone_handler(teletone_generation_session_t *ts, teletone_tone_map_t *map)
105{
106 switch_buffer_t *audio_buffer = ts->user_data;
107 int wrote;
108
109 if (!audio_buffer) {
110 return -1;
111 }
112
113 wrote = teletone_mux_tones(ts, map);
114 switch_buffer_write(audio_buffer, ts->buffer, wrote * 2);
115
116 return 0;
117}
118
119static switch_status_t tone_stream_file_open(switch_file_handle_t *handle, const char *path)
120{
121 switch_buffer_t *audio_buffer = NULL((void*)0);
122 teletone_generation_session_t ts;
123 char *tonespec;
124 int loops = 0;
125 char *tmp;
126 int fd = -1;
127 char buf[1024] = "";
128 size_t len;
129
130 memset(&ts, 0, sizeof(ts));
131
132 tonespec = switch_core_strdup(handle->memory_pool, path)switch_core_perform_strdup(handle->memory_pool, path, "mod_tone_stream.c"
, (const char *)__func__, 132)
;
133
134 switch_buffer_create_dynamic(&audio_buffer, 1024, 1024, 0);
135 switch_assert(audio_buffer)((audio_buffer) ? (void) (0) : __assert_fail ("audio_buffer",
"mod_tone_stream.c", 135, __PRETTY_FUNCTION__))
;
136
137 if ((tmp = (char *)switch_stristr(";loops=", tonespec))) {
138 *tmp = '\0';
139 tmp += 7;
140 if (tmp) {
141 loops = atoi(tmp);
142 switch_buffer_set_loops(audio_buffer, loops);
143 }
144 }
145
146 if (handle->params) {
147 if ((tmp = switch_event_get_header(handle->params, "loops")switch_event_get_header_idx(handle->params, "loops", -1))) {
148 loops = atoi(tmp);
149 switch_buffer_set_loops(audio_buffer, loops);
150 }
151 }
152
153 if (!handle->samplerate) {
154 handle->samplerate = 8000;
155 }
156
157 handle->channels = 1;
158
159 teletone_init_session(&ts, 0, teletone_handler, audio_buffer);
160 ts.rate = handle->samplerate;
161 ts.channels = 1;
162
163 if (!strncasecmp(tonespec, "path=", 5)) {
164 tmp = tonespec + 5;
165 if ((fd = open(tmp, O_RDONLY00)) < 0) {
166 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "mod_tone_stream.c", (const char *)__func__
, 166, ((void*)0)
, SWITCH_LOG_WARNING, "Failed to open [%s]\n", tmp);
167 return SWITCH_STATUS_FALSE;
168 }
169
170 while ((len = switch_fd_read_line(fd, buf, sizeof(buf)))) {
171 teletone_run(&ts, buf);
172 }
173 close(fd);
174 fd = -1;
Value stored to 'fd' is never read
175 } else {
176 teletone_run(&ts, tonespec);
177 }
178
179 teletone_destroy_session(&ts);
180
181 handle->private_info = audio_buffer;
182
183 return SWITCH_STATUS_SUCCESS;
184}
185
186static switch_status_t tone_stream_file_close(switch_file_handle_t *handle)
187{
188 switch_buffer_t *audio_buffer = handle->private_info;
189
190 switch_buffer_destroy(&audio_buffer);
191 return SWITCH_STATUS_SUCCESS;
192}
193
194static switch_status_t tone_stream_file_read(switch_file_handle_t *handle, void *data, size_t *len)
195{
196 switch_buffer_t *audio_buffer = handle->private_info;
197 switch_size_t bytes;
198
199 if ((bytes = switch_buffer_read_loop(audio_buffer, data, *len * 2)) <= 0) {
200 *len = 0;
201 return SWITCH_STATUS_FALSE;
202 }
203
204 *len = bytes / 2;
205 return SWITCH_STATUS_SUCCESS;
206}
207
208/* Registration */
209
210static char *supported_formats[SWITCH_MAX_CODECS50] = { 0 };
211static char *silence_supported_formats[SWITCH_MAX_CODECS50] = { 0 };
212
213SWITCH_MODULE_LOAD_FUNCTION(mod_tone_stream_load)switch_status_t mod_tone_stream_load (switch_loadable_module_interface_t
**module_interface, switch_memory_pool_t *pool)
214{
215 switch_file_interface_t *file_interface;
216 supported_formats[0] = "tone_stream";
217 silence_supported_formats[0] = "silence_stream";
218
219 *module_interface = switch_loadable_module_create_module_interface(pool, modname);
220 file_interface = switch_loadable_module_create_interface(*module_interface, SWITCH_FILE_INTERFACE);
221 file_interface->interface_name = modname;
222 file_interface->extens = supported_formats;
223 file_interface->file_open = tone_stream_file_open;
224 file_interface->file_close = tone_stream_file_close;
225 file_interface->file_read = tone_stream_file_read;
226
227 file_interface = switch_loadable_module_create_interface(*module_interface, SWITCH_FILE_INTERFACE);
228 file_interface->interface_name = modname;
229 file_interface->extens = silence_supported_formats;
230 file_interface->file_open = silence_stream_file_open;
231 file_interface->file_close = silence_stream_file_close;
232 file_interface->file_read = silence_stream_file_read;
233
234 /* indicate that the module should continue to be loaded */
235 return SWITCH_STATUS_SUCCESS;
236}
237
238SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_tone_stream_shutdown)switch_status_t mod_tone_stream_shutdown (void)
239{
240 return SWITCH_STATUS_SUCCESS;
241}
242
243/* For Emacs:
244 * Local Variables:
245 * mode:c
246 * indent-tabs-mode:t
247 * tab-width:4
248 * c-basic-offset:4
249 * End:
250 * For VIM:
251 * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
252 */