| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
#include "asterisk.h" |
| 36 |
|
| 37 |
#include <sys/time.h> |
| 38 |
#include <sys/types.h> |
| 39 |
#include <signal.h> |
| 40 |
|
| 41 |
#include "asterisk/lock.h" |
| 42 |
#include "asterisk/file.h" |
| 43 |
#include "asterisk/channel.h" |
| 44 |
#include "asterisk/frame.h" |
| 45 |
#include "asterisk/pbx.h" |
| 46 |
#include "asterisk/module.h" |
| 47 |
#include "asterisk/translate.h" |
| 48 |
#include "asterisk/app.h" |
| 49 |
#include "asterisk/format_cache.h" |
| 50 |
|
| 51 |
#define LOCAL_MPG_123 "/usr/local/bin/mpg123" |
| 52 |
#define MPG_123 "/usr/bin/mpg123" |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
static char *app = "MP3Player"; |
| 84 |
|
| 85 |
static int mp3play(const char *filename, unsigned int sampling_rate, int fd) |
| 86 |
{ |
| 87 |
int res; |
| 88 |
char sampling_rate_str[8]; |
| 89 |
|
| 90 |
res = ast_safe_fork(0); |
| 91 |
if (res < 0) |
| 92 |
ast_log(LOG_WARNING, "Fork failed\n"); |
| 93 |
if (res) { |
| 94 |
return res; |
| 95 |
} |
| 96 |
if (ast_opt_high_priority) |
| 97 |
ast_set_priority(0); |
| 98 |
|
| 99 |
dup2(fd, STDOUT_FILENO); |
| 100 |
ast_close_fds_above_n(STDERR_FILENO); |
| 101 |
|
| 102 |
snprintf(sampling_rate_str, 8, "%u", sampling_rate); |
| 103 |
|
| 104 |
|
| 105 |
if (!strncasecmp(filename, "http://", 7) && strstr(filename, ".m3u")) { |
| 106 |
char buffer_size_str[8]; |
| 107 |
snprintf(buffer_size_str, 8, "%u", (int) 0.5*2*sampling_rate/1000); |
| 108 |
|
| 109 |
execl(LOCAL_MPG_123, "mpg123", "-e", "s16", "-q", "-s", "-b", buffer_size_str, "-f", "8192", "--mono", "-r", sampling_rate_str, "-@", filename, (char *)NULL); |
| 110 |
|
| 111 |
execl(MPG_123, "mpg123", "-e", "s16", "-q", "-s", "-b", buffer_size_str, "-f", "8192", "--mono", "-r", sampling_rate_str, "-@", filename, (char *)NULL); |
| 112 |
|
| 113 |
execlp("mpg123", "mpg123", "-e", "s16", "-q", "-s", "-b", buffer_size_str, "-f", "8192", "--mono", "-r", sampling_rate_str, "-@", filename, (char *)NULL); |
| 114 |
} |
| 115 |
else if (!strncasecmp(filename, "http://", 7)) { |
| 116 |
char buffer_size_str[8]; |
| 117 |
snprintf(buffer_size_str, 8, "%u", 6*2*sampling_rate/1000); |
| 118 |
|
| 119 |
execl(LOCAL_MPG_123, "mpg123", "-e", "s16", "-q", "-s", "-b", buffer_size_str, "-f", "8192", "--mono", "-r", sampling_rate_str, filename, (char *)NULL); |
| 120 |
|
| 121 |
execl(MPG_123, "mpg123", "-e", "s16", "-q", "-s", "-b", buffer_size_str, "-f", "8192", "--mono", "-r", sampling_rate_str, filename, (char *)NULL); |
| 122 |
|
| 123 |
execlp("mpg123", "mpg123", "-e", "s16", "-q", "-s", "-b", buffer_size_str, "-f", "8192", "--mono", "-r", sampling_rate_str, filename, (char *)NULL); |
| 124 |
} |
| 125 |
else if (strstr(filename, ".m3u")) { |
| 126 |
|
| 127 |
execl(LOCAL_MPG_123, "mpg123", "-e", "s16", "-q", "-z", "-s", "-f", "8192", "--mono", "-r", sampling_rate_str, "-@", filename, (char *)NULL); |
| 128 |
|
| 129 |
execl(MPG_123, "mpg123", "-e", "s16", "-q", "-z", "-s", "-f", "8192", "--mono", "-r", sampling_rate_str, "-@", filename, (char *)NULL); |
| 130 |
|
| 131 |
execlp("mpg123", "mpg123", "-e", "s16", "-q", "-z", "-s", "-f", "8192", "--mono", "-r", sampling_rate_str, "-@", filename, (char *)NULL); |
| 132 |
} |
| 133 |
else { |
| 134 |
|
| 135 |
execl(MPG_123, "mpg123", "-e", "s16", "-q", "-s", "-f", "8192", "--mono", "-r", sampling_rate_str, filename, (char *)NULL); |
| 136 |
|
| 137 |
execl(LOCAL_MPG_123, "mpg123", "-e", "s16", "-q", "-s", "-f", "8192", "--mono", "-r", sampling_rate_str, filename, (char *)NULL); |
| 138 |
|
| 139 |
execlp("mpg123", "mpg123", "-e", "s16", "-q", "-s", "-f", "8192", "--mono", "-r", sampling_rate_str, filename, (char *)NULL); |
| 140 |
} |
| 141 |
|
| 142 |
fprintf(stderr, "Execute of mpg123 failed\n"); |
| 143 |
_exit(0); |
| 144 |
} |
| 145 |
|
| 146 |
static int timed_read(int fd, void *data, int datalen, int timeout, int pid) |
| 147 |
{ |
| 148 |
int res; |
| 149 |
int i; |
| 150 |
struct pollfd fds[1]; |
| 151 |
fds[0].fd = fd; |
| 152 |
fds[0].events = POLLIN; |
| 153 |
for (i = 0; i < timeout; i++) { |
| 154 |
res = ast_poll(fds, 1, 1000); |
| 155 |
if (res > 0) { |
| 156 |
break; |
| 157 |
} else if (res == 0) { |
| 158 |
|
| 159 |
kill(pid, 0); |
| 160 |
if (errno == ESRCH) { |
| 161 |
return -1; |
| 162 |
} |
| 163 |
} else { |
| 164 |
ast_log(LOG_NOTICE, "error polling mpg123: %s\n", strerror(errno)); |
| 165 |
return -1; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
if (i == timeout) { |
| 170 |
ast_log(LOG_NOTICE, "Poll timed out.\n"); |
| 171 |
return -1; |
| 172 |
} |
| 173 |
|
| 174 |
return read(fd, data, datalen); |
| 175 |
|
| 176 |
} |
| 177 |
|
| 178 |
static int mp3_exec(struct ast_channel *chan, const char *data) |
| 179 |
{ |
| 180 |
int res=0; |
| 181 |
int fds[2]; |
| 182 |
int ms = -1; |
| 183 |
int pid = -1; |
| 184 |
RAII_VAR(struct ast_format *, owriteformat, NULL, ao2_cleanup); |
| 185 |
int timeout = 2; |
| 186 |
int startedmp3 = 0; |
| 187 |
struct timeval next; |
| 188 |
struct ast_frame *f; |
| 189 |
struct myframe { |
| 190 |
struct ast_frame f; |
| 191 |
char offset[AST_FRIENDLY_OFFSET]; |
| 192 |
short frdata[160]; |
| 193 |
} myf = { |
| 194 |
.f = { 0, }, |
| 195 |
}; |
| 196 |
struct ast_format * native_format; |
| 197 |
unsigned int sampling_rate; |
| 198 |
struct ast_format * write_format; |
| 199 |
|
| 200 |
if (ast_strlen_zero(data)) { |
| 201 |
ast_log(LOG_WARNING, "MP3 Playback requires an argument (filename)\n"); |
| 202 |
return -1; |
| 203 |
} |
| 204 |
|
| 205 |
if (pipe(fds)) { |
| 206 |
ast_log(LOG_WARNING, "Unable to create pipe\n"); |
| 207 |
return -1; |
| 208 |
} |
| 209 |
|
| 210 |
ast_stopstream(chan); |
| 211 |
|
| 212 |
native_format = ast_format_cap_get_format(ast_channel_nativeformats(chan), 0); |
| 213 |
sampling_rate = ast_format_get_sample_rate(native_format); |
| 214 |
write_format = ast_format_cache_get_slin_by_rate(sampling_rate); |
| 215 |
|
| 216 |
owriteformat = ao2_bump(ast_channel_writeformat(chan)); |
| 217 |
res = ast_set_write_format(chan, write_format); |
| 218 |
if (res < 0) { |
| 219 |
ast_log(LOG_WARNING, "Unable to set write format to signed linear\n"); |
| 220 |
return -1; |
| 221 |
} |
| 222 |
|
| 223 |
myf.f.frametype = AST_FRAME_VOICE; |
| 224 |
myf.f.subclass.format = write_format; |
| 225 |
myf.f.mallocd = 0; |
| 226 |
myf.f.offset = AST_FRIENDLY_OFFSET; |
| 227 |
myf.f.src = __PRETTY_FUNCTION__; |
| 228 |
myf.f.delivery.tv_sec = 0; |
| 229 |
myf.f.delivery.tv_usec = 0; |
| 230 |
myf.f.data.ptr = myf.frdata; |
| 231 |
|
| 232 |
res = mp3play(data, sampling_rate, fds[1]); |
| 233 |
if (!strncasecmp(data, "http://", 7)) { |
| 234 |
timeout = 10; |
| 235 |
} |
| 236 |
|
| 237 |
next = ast_tvnow(); |
| 238 |
next.tv_sec += 1; |
| 239 |
if (res >= 0) { |
| 240 |
pid = res; |
| 241 |
|
| 242 |
|
| 243 |
for (;;) { |
| 244 |
ms = ast_tvdiff_ms(next, ast_tvnow()); |
| 245 |
if (ms <= 0) { |
| 246 |
res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata), timeout, pid); |
| 247 |
if (res > 0) { |
| 248 |
myf.f.datalen = res; |
| 249 |
myf.f.samples = res / 2; |
| 250 |
startedmp3 = 1; |
| 251 |
if (ast_write(chan, &myf.f) < 0) { |
| 252 |
res = -1; |
| 253 |
break; |
| 254 |
} |
| 255 |
} else { |
| 256 |
ast_debug(1, "No more mp3\n"); |
| 257 |
if (!startedmp3) { |
| 258 |
if (!strncasecmp(data, "https://", 8)) { |
| 259 |
ast_log(LOG_WARNING, "%s() does not support HTTPS streams. Use HTTP instead.\n", app); |
| 260 |
} |
| 261 |
ast_log(LOG_WARNING, "MP3 stream '%s' is broken or nonexistent\n", data); |
| 262 |
} |
| 263 |
res = 0; |
| 264 |
break; |
| 265 |
} |
| 266 |
next = ast_tvadd(next, ast_samp2tv(myf.f.samples, sampling_rate)); |
| 267 |
} else { |
| 268 |
ms = ast_waitfor(chan, ms); |
| 269 |
if (ms < 0) { |
| 270 |
ast_debug(1, "Hangup detected\n"); |
| 271 |
res = -1; |
| 272 |
break; |
| 273 |
} |
| 274 |
if (ms) { |
| 275 |
f = ast_read(chan); |
| 276 |
if (!f) { |
| 277 |
ast_debug(1, "Null frame == hangup() detected\n"); |
| 278 |
res = -1; |
| 279 |
break; |
| 280 |
} |
| 281 |
if (f->frametype == AST_FRAME_DTMF) { |
| 282 |
ast_debug(1, "User pressed a key\n"); |
| 283 |
ast_frfree(f); |
| 284 |
res = 0; |
| 285 |
break; |
| 286 |
} |
| 287 |
ast_frfree(f); |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
close(fds[0]); |
| 293 |
close(fds[1]); |
| 294 |
|
| 295 |
if (pid > -1) |
| 296 |
kill(pid, SIGKILL); |
| 297 |
if (!res && owriteformat) |
| 298 |
ast_set_write_format(chan, owriteformat); |
| 299 |
|
| 300 |
ast_frfree(&myf.f); |
| 301 |
|
| 302 |
return res; |
| 303 |
} |
| 304 |
|
| 305 |
static int unload_module(void) |
| 306 |
{ |
| 307 |
return ast_unregister_application(app); |
| 308 |
} |
| 309 |
|
| 310 |
static int load_module(void) |
| 311 |
{ |
| 312 |
return ast_register_application_xml(app, mp3_exec); |
| 313 |
} |
| 314 |
|
| 315 |
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Silly MP3 Application"); |