Always close the sound devices when done playing a file

This commit is contained in:
Pierre Pronchery 2014-10-21 23:38:38 +02:00
parent a165a92bcc
commit a016cf1eb1
2 changed files with 24 additions and 18 deletions

View File

@ -134,6 +134,7 @@ static int _event_audio_play(OSS * oss, char const * sample);
static int _event_audio_play_chunk(OSS * oss, FILE * fp);
static int _event_audio_play_chunk_riff(OSS * oss, FILE * fp, RIFFChunk * rc);
static int _event_audio_play_chunk_wave(OSS * oss, FILE * fp, RIFFChunk * rc);
static int _event_audio_play_close(OSS * oss, int fd, int ret);
static int _event_audio_play_file(OSS * oss, char const * filename);
static int _event_audio_play_open(OSS * oss, char const * device, FILE * fp,
WaveFormat * wf, RIFFChunk * rc);
@ -247,9 +248,12 @@ static int _event_audio_play_chunk_wave(OSS * oss, FILE * fp, RIFFChunk * rc)
{
/* read the current WAVE chunk */
if(rc->ckSize < sizeof(rc2))
return -1;
return -_event_audio_play_close(oss, fd, 1);
if(fread(&rc2, sizeof(rc2), 1, fp) != 1)
return -oss->helper->error(NULL, strerror(errno), 1);
{
oss->helper->error(NULL, strerror(errno), 1);
return -_event_audio_play_close(oss, fd, 1);
}
#if 0 /* FIXME for big endian */
/* FIXME implement */
#endif
@ -262,10 +266,7 @@ static int _event_audio_play_chunk_wave(OSS * oss, FILE * fp, RIFFChunk * rc)
if(strncmp(rc2.ckID, fmt, sizeof(fmt)) == 0)
{
if(fd >= 0)
{
close(fd);
return -1;
}
return -_event_audio_play_close(oss, fd, 1);
if(rc->ckSize < sizeof(wf)
|| rc2.ckSize < sizeof(wf)
|| fread(&wf, sizeof(wf), 1, fp) != 1)
@ -294,21 +295,15 @@ static int _event_audio_play_chunk_wave(OSS * oss, FILE * fp, RIFFChunk * rc)
if(fd < 0)
return -1;
if(_event_audio_play_write(oss, rc, &rc2, fp, fd) != 0)
break;
return -_event_audio_play_close(oss, fd, 1);
}
/* skip the rest of the chunk */
if(fseek(fp, rc2.ckSize, SEEK_CUR) != 0)
{
if(fd >= 0)
close(fd);
return -1;
}
return -_event_audio_play_close(oss, fd, 1);
rc->ckSize -= rc2.ckSize;
rc2.ckSize = 0;
}
if(fd >= 0)
close(fd);
return 0;
return _event_audio_play_close(oss, fd, 0);
}
static int _event_audio_play_file(OSS * oss, char const * filename)
@ -325,6 +320,13 @@ static int _event_audio_play_file(OSS * oss, char const * filename)
return 0;
}
static int _event_audio_play_close(OSS * oss, int fd, int ret)
{
if(fd >= 0 && close(fd) != 0)
oss->helper->error(NULL, strerror(errno), 1);
return ret;
}
static int _event_audio_play_open(OSS * oss, char const * device, FILE * fp,
WaveFormat * wf, RIFFChunk * rc)
{

View File

@ -67,7 +67,7 @@ static int _oss_error(Phone * phone, char const * message, int ret)
/* usage */
static int _usage(void)
{
fputs("Usage: oss filename\n", stderr);
fputs("Usage: oss sample...\n", stderr);
return 1;
}
@ -77,6 +77,7 @@ static int _usage(void)
/* main */
int main(int argc, char * argv[])
{
int ret = 0;
int o;
while((o = getopt(argc, argv, "")) != -1)
@ -85,7 +86,10 @@ int main(int argc, char * argv[])
default:
return _usage();
}
if(optind + 1 != argc)
if(optind == argc)
return _usage();
return (_oss(argv[optind]) == 0) ? 0 : 2;
while(optind < argc)
if(_oss(argv[optind++]) != 0)
ret = 2;
return ret;
}