00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 # ifdef HAVE_CONFIG_H
00027 # include "config.h"
00028 # endif
00029
00030 # include "global.h"
00031
00032 # ifdef HAVE_SYS_TYPES_H
00033 # include <sys/types.h>
00034 # endif
00035
00036 # ifdef HAVE_SYS_WAIT_H
00037 # include <sys/wait.h>
00038 # endif
00039
00040 # ifdef HAVE_UNISTD_H
00041 # include <unistd.h>
00042 # endif
00043
00044 # ifdef HAVE_FCNTL_H
00045 # include <fcntl.h>
00046 # endif
00047
00048 # include <stdlib.h>
00049
00050 # ifdef HAVE_ERRNO_H
00051 # include <errno.h>
00052 # endif
00053
00054 # include "stream.h"
00055 # include "frame.h"
00056 # include "synth.h"
00057 # include "decoder.h"
00058
00062 void mad_decoder_init(struct mad_decoder *decoder, void *data,
00063 input_callback input_func,
00064 header_callback header_func,
00065 filter_callback filter_func,
00066 output_callback output_func,
00067 error_callback error_func,
00068 message_callback message_func)
00069 {
00070 decoder->mode = -1;
00071
00072 decoder->options = 0;
00073
00074 decoder->async.pid = 0;
00075 decoder->async.in = -1;
00076 decoder->async.out = -1;
00077
00078 decoder->sync = 0;
00079
00080 decoder->cb_data = data;
00081
00082 decoder->input_func = input_func;
00083 decoder->header_func = header_func;
00084 decoder->filter_func = filter_func;
00085 decoder->output_func = output_func;
00086 decoder->error_func = error_func;
00087 decoder->message_func = message_func;
00088 }
00089
00090 int mad_decoder_finish(struct mad_decoder *decoder)
00091 {
00092 # if defined(USE_ASYNC)
00093 if (decoder->mode == MAD_DECODER_MODE_ASYNC && decoder->async.pid) {
00094 pid_t pid;
00095 int status;
00096
00097 close(decoder->async.in);
00098
00099 do
00100 pid = waitpid(decoder->async.pid, &status, 0);
00101 while (pid == -1 && errno == EINTR);
00102
00103 decoder->mode = -1;
00104
00105 close(decoder->async.out);
00106
00107 decoder->async.pid = 0;
00108 decoder->async.in = -1;
00109 decoder->async.out = -1;
00110
00111 if (pid == -1)
00112 return -1;
00113
00114 return (!WIFEXITED(status) || WEXITSTATUS(status)) ? -1 : 0;
00115 }
00116 # endif
00117
00118 return 0;
00119 }
00120
00121 # if defined(USE_ASYNC)
00122 static
00123 enum mad_flow send_io(int fd, void const *data, size_t len)
00124 {
00125 char const *ptr = data;
00126 ssize_t count;
00127
00128 while (len) {
00129 do
00130 count = write(fd, ptr, len);
00131 while (count == -1 && errno == EINTR);
00132
00133 if (count == -1)
00134 return MAD_FLOW_BREAK;
00135
00136 len -= count;
00137 ptr += count;
00138 }
00139
00140 return MAD_FLOW_CONTINUE;
00141 }
00142
00143 static
00144 enum mad_flow receive_io(int fd, void *buffer, size_t len)
00145 {
00146 char *ptr = buffer;
00147 ssize_t count;
00148
00149 while (len) {
00150 do
00151 count = read(fd, ptr, len);
00152 while (count == -1 && errno == EINTR);
00153
00154 if (count == -1)
00155 return (errno == EAGAIN) ? MAD_FLOW_IGNORE : MAD_FLOW_BREAK;
00156 else if (count == 0)
00157 return MAD_FLOW_STOP;
00158
00159 len -= count;
00160 ptr += count;
00161 }
00162
00163 return MAD_FLOW_CONTINUE;
00164 }
00165
00166 static
00167 enum mad_flow receive_io_blocking(int fd, void *buffer, size_t len)
00168 {
00169 int flags, blocking;
00170 enum mad_flow result;
00171
00172 flags = fcntl(fd, F_GETFL);
00173 if (flags == -1)
00174 return MAD_FLOW_BREAK;
00175
00176 blocking = flags & ~O_NONBLOCK;
00177
00178 if (blocking != flags &&
00179 fcntl(fd, F_SETFL, blocking) == -1)
00180 return MAD_FLOW_BREAK;
00181
00182 result = receive_io(fd, buffer, len);
00183
00184 if (flags != blocking &&
00185 fcntl(fd, F_SETFL, flags) == -1)
00186 return MAD_FLOW_BREAK;
00187
00188 return result;
00189 }
00190
00191 static
00192 enum mad_flow send(int fd, void const *message, unsigned int size)
00193 {
00194 enum mad_flow result;
00195
00196
00197
00198 result = send_io(fd, &size, sizeof(size));
00199
00200
00201
00202 if (result == MAD_FLOW_CONTINUE)
00203 result = send_io(fd, message, size);
00204
00205 return result;
00206 }
00207
00208 static
00209 enum mad_flow receive(int fd, void **message, unsigned int *size)
00210 {
00211 enum mad_flow result;
00212 unsigned int actual;
00213
00214 if (*message == 0)
00215 *size = 0;
00216
00217
00218
00219 result = receive_io(fd, &actual, sizeof(actual));
00220
00221
00222
00223 if (result == MAD_FLOW_CONTINUE) {
00224 if (actual > *size)
00225 actual -= *size;
00226 else {
00227 *size = actual;
00228 actual = 0;
00229 }
00230
00231 if (*size > 0) {
00232 if (*message == 0) {
00233 *message = malloc(*size);
00234 if (*message == 0)
00235 return MAD_FLOW_BREAK;
00236 }
00237
00238 result = receive_io_blocking(fd, *message, *size);
00239 }
00240
00241
00242
00243 while (actual && result == MAD_FLOW_CONTINUE) {
00244 char sink[256];
00245 unsigned int len;
00246
00247 len = actual > sizeof(sink) ? sizeof(sink) : actual;
00248
00249 result = receive_io_blocking(fd, sink, len);
00250
00251 actual -= len;
00252 }
00253 }
00254
00255 return result;
00256 }
00257
00258 static
00259 enum mad_flow check_message(struct mad_decoder *decoder)
00260 {
00261 enum mad_flow result;
00262 void *message = 0;
00263 unsigned int size;
00264
00265 result = receive(decoder->async.in, &message, &size);
00266
00267 if (result == MAD_FLOW_CONTINUE) {
00268 if (decoder->message_func == 0)
00269 size = 0;
00270 else {
00271 result = decoder->message_func(decoder->cb_data, message, &size);
00272
00273 if (result == MAD_FLOW_IGNORE ||
00274 result == MAD_FLOW_BREAK)
00275 size = 0;
00276 }
00277
00278 if (send(decoder->async.out, message, size) != MAD_FLOW_CONTINUE)
00279 result = MAD_FLOW_BREAK;
00280 }
00281
00282 if (message)
00283 free(message);
00284
00285 return result;
00286 }
00287 # endif
00288
00289 static
00290 enum mad_flow error_default(void *data, struct mad_stream *stream,
00291 struct mad_frame *frame)
00292 {
00293 int *bad_last_frame = data;
00294
00295 switch (stream->error) {
00296 case MAD_ERROR_BADCRC:
00297 if (*bad_last_frame)
00298 mad_frame_mute(frame);
00299 else
00300 *bad_last_frame = 1;
00301
00302 return MAD_FLOW_IGNORE;
00303
00304 default:
00305 return MAD_FLOW_CONTINUE;
00306 }
00307 }
00308
00309 static
00310 int run_sync(struct mad_decoder *decoder)
00311 {
00312 error_callback error_func;
00313 void *error_data;
00314 int bad_last_frame = 0;
00315 struct mad_stream *stream;
00316 struct mad_frame *frame;
00317 struct mad_synth *synth;
00318 int result = 0;
00319
00320 if (decoder->input_func == 0)
00321 return 0;
00322
00323 if (decoder->error_func) {
00324 error_func = decoder->error_func;
00325 error_data = decoder->cb_data;
00326 }
00327 else {
00328 error_func = error_default;
00329 error_data = &bad_last_frame;
00330 }
00331
00332 stream = &decoder->sync->stream;
00333 frame = &decoder->sync->frame;
00334 synth = &decoder->sync->synth;
00335
00336 mad_stream_init(stream);
00337 mad_frame_init(frame);
00338 mad_synth_init(synth);
00339
00340 mad_stream_options(stream, decoder->options);
00341
00342 do {
00343 switch (decoder->input_func(decoder->cb_data, stream)) {
00344 case MAD_FLOW_STOP:
00345 goto done;
00346 case MAD_FLOW_BREAK:
00347 goto fail;
00348 case MAD_FLOW_IGNORE:
00349 continue;
00350 case MAD_FLOW_CONTINUE:
00351 break;
00352 }
00353
00354 while (1) {
00355 # if defined(USE_ASYNC)
00356 if (decoder->mode == MAD_DECODER_MODE_ASYNC) {
00357 switch (check_message(decoder)) {
00358 case MAD_FLOW_IGNORE:
00359 case MAD_FLOW_CONTINUE:
00360 break;
00361 case MAD_FLOW_BREAK:
00362 goto fail;
00363 case MAD_FLOW_STOP:
00364 goto done;
00365 }
00366 }
00367 # endif
00368
00369 if (decoder->header_func) {
00370 if (mad_header_decode(&frame->header, stream) == -1) {
00371 if (!MAD_RECOVERABLE(stream->error))
00372 break;
00373
00374 switch (error_func(error_data, stream, frame)) {
00375 case MAD_FLOW_STOP:
00376 goto done;
00377 case MAD_FLOW_BREAK:
00378 goto fail;
00379 case MAD_FLOW_IGNORE:
00380 case MAD_FLOW_CONTINUE:
00381 default:
00382 continue;
00383 }
00384 }
00385
00386 switch (decoder->header_func(decoder->cb_data, &frame->header)) {
00387 case MAD_FLOW_STOP:
00388 goto done;
00389 case MAD_FLOW_BREAK:
00390 goto fail;
00391 case MAD_FLOW_IGNORE:
00392 continue;
00393 case MAD_FLOW_CONTINUE:
00394 break;
00395 }
00396 }
00397
00398 if (mad_frame_decode(frame, stream) == -1) {
00399 if (!MAD_RECOVERABLE(stream->error))
00400 break;
00401
00402 switch (error_func(error_data, stream, frame)) {
00403 case MAD_FLOW_STOP:
00404 goto done;
00405 case MAD_FLOW_BREAK:
00406 goto fail;
00407 case MAD_FLOW_IGNORE:
00408 break;
00409 case MAD_FLOW_CONTINUE:
00410 default:
00411 continue;
00412 }
00413 }
00414 else
00415 bad_last_frame = 0;
00416
00417 if (decoder->filter_func) {
00418 switch (decoder->filter_func(decoder->cb_data, stream, frame)) {
00419 case MAD_FLOW_STOP:
00420 goto done;
00421 case MAD_FLOW_BREAK:
00422 goto fail;
00423 case MAD_FLOW_IGNORE:
00424 continue;
00425 case MAD_FLOW_CONTINUE:
00426 break;
00427 }
00428 }
00429
00430 mad_synth_frame(synth, frame);
00431
00432 if (decoder->output_func) {
00433 switch (decoder->output_func(decoder->cb_data,
00434 &frame->header, &synth->pcm)) {
00435 case MAD_FLOW_STOP:
00436 goto done;
00437 case MAD_FLOW_BREAK:
00438 goto fail;
00439 case MAD_FLOW_IGNORE:
00440 case MAD_FLOW_CONTINUE:
00441 break;
00442 }
00443 }
00444 }
00445 }
00446 while (stream->error == MAD_ERROR_BUFLEN);
00447
00448 fail:
00449 result = -1;
00450
00451 done:
00452 mad_synth_finish(synth);
00453 mad_frame_finish(frame);
00454 mad_stream_finish(stream);
00455
00456 return result;
00457 }
00458
00459 # if defined(USE_ASYNC)
00460 static
00461 int run_async(struct mad_decoder *decoder)
00462 {
00463 pid_t pid;
00464 int ptoc[2], ctop[2], flags;
00465
00466 if (pipe(ptoc) == -1)
00467 return -1;
00468
00469 if (pipe(ctop) == -1) {
00470 close(ptoc[0]);
00471 close(ptoc[1]);
00472 return -1;
00473 }
00474
00475 flags = fcntl(ptoc[0], F_GETFL);
00476 if (flags == -1 ||
00477 fcntl(ptoc[0], F_SETFL, flags | O_NONBLOCK) == -1) {
00478 close(ctop[0]);
00479 close(ctop[1]);
00480 close(ptoc[0]);
00481 close(ptoc[1]);
00482 return -1;
00483 }
00484
00485 pid = fork();
00486 if (pid == -1) {
00487 close(ctop[0]);
00488 close(ctop[1]);
00489 close(ptoc[0]);
00490 close(ptoc[1]);
00491 return -1;
00492 }
00493
00494 decoder->async.pid = pid;
00495
00496 if (pid) {
00497
00498
00499 close(ptoc[0]);
00500 close(ctop[1]);
00501
00502 decoder->async.in = ctop[0];
00503 decoder->async.out = ptoc[1];
00504
00505 return 0;
00506 }
00507
00508
00509
00510 close(ptoc[1]);
00511 close(ctop[0]);
00512
00513 decoder->async.in = ptoc[0];
00514 decoder->async.out = ctop[1];
00515
00516 _exit(run_sync(decoder));
00517
00518
00519 return -1;
00520 }
00521 # endif
00522
00526 int mad_decoder_run(struct mad_decoder *decoder, enum mad_decoder_mode mode)
00527 {
00528 int result;
00529 int (*run)(struct mad_decoder *) = 0;
00530
00531 switch (decoder->mode = mode) {
00532 case MAD_DECODER_MODE_SYNC:
00533 run = run_sync;
00534 break;
00535
00536 case MAD_DECODER_MODE_ASYNC:
00537 # if defined(USE_ASYNC)
00538 run = run_async;
00539 # endif
00540 break;
00541 }
00542
00543 if (run == 0)
00544 return -1;
00545
00546 decoder->sync = malloc(sizeof(*decoder->sync));
00547 if (decoder->sync == 0)
00548 return -1;
00549
00550 result = run(decoder);
00551
00552 free(decoder->sync);
00553 decoder->sync = 0;
00554
00555 return result;
00556 }
00557
00561 int mad_decoder_message(struct mad_decoder *decoder,
00562 void *message, unsigned int *len)
00563 {
00564 # if defined(USE_ASYNC)
00565 if (decoder->mode != MAD_DECODER_MODE_ASYNC ||
00566 send(decoder->async.out, message, *len) != MAD_FLOW_CONTINUE ||
00567 receive(decoder->async.in, &message, len) != MAD_FLOW_CONTINUE)
00568 return -1;
00569
00570 return 0;
00571 # else
00572 return -1;
00573 # endif
00574 }