[Audio API] Repair the damage to the input chain

The input chain could hang up indefinitely, and MAD decoder didn't
indicate end of file properly.

Signed-off-by: Christopher Snowhill <kode54@gmail.com>
main
Christopher Snowhill 2022-07-10 16:24:08 -07:00
parent e595dbd525
commit f8a8a57cf0
3 changed files with 24 additions and 12 deletions

View File

@ -444,11 +444,16 @@ static void convert_be_to_le(uint8_t *buffer, size_t bitsPerSample, size_t bytes
chunk = [self convert]; chunk = [self convert];
} }
if(!chunk) { if(!chunk) {
continue; if(paused) {
} continue;
@autoreleasepool { } else if(!streamFormatChanged) {
[self writeChunk:chunk]; break;
chunk = nil; }
} else {
@autoreleasepool {
[self writeChunk:chunk];
chunk = nil;
}
} }
if(streamFormatChanged) { if(streamFormatChanged) {
[self cleanUp]; [self cleanUp];

View File

@ -187,7 +187,7 @@ static void *kInputNodeContext = &kInputNodeContext;
chunk = [decoder readAudio]; chunk = [decoder readAudio];
} }
if(chunk) { if(chunk && [chunk frameCount]) {
@autoreleasepool { @autoreleasepool {
[self writeChunk:chunk]; [self writeChunk:chunk];
chunk = nil; chunk = nil;

View File

@ -470,7 +470,7 @@
return ret; return ret;
} }
- (void)writeOutput { - (BOOL)writeOutput {
unsigned long startingSample = 0; unsigned long startingSample = 0;
unsigned long sampleCount = _synth.pcm.length; unsigned long sampleCount = _synth.pcm.length;
@ -485,7 +485,7 @@
// Past the end of the file. // Past the end of the file.
if(totalFrames - _endPadding <= _framesDecoded) { if(totalFrames - _endPadding <= _framesDecoded) {
// DLog(@"End of file. Not writing."); // DLog(@"End of file. Not writing.");
return; return YES;
} }
// Clip this for the following calculation, so this doesn't underflow // Clip this for the following calculation, so this doesn't underflow
@ -497,6 +497,11 @@
// DLog(@"End of file. %li", totalFrames - _endPadding - _framesDecoded); // DLog(@"End of file. %li", totalFrames - _endPadding - _framesDecoded);
sampleCount = totalFrames - _endPadding - _framesDecoded + startingSample; sampleCount = totalFrames - _endPadding - _framesDecoded + startingSample;
} }
} else {
// Past the end of the file.
if(totalFrames <= _framesDecoded) {
return YES;
}
} }
// We haven't even gotten to the start yet // We haven't even gotten to the start yet
@ -504,7 +509,7 @@
// DLog(@"Skipping entire sample"); // DLog(@"Skipping entire sample");
_framesDecoded += sampleCount; _framesDecoded += sampleCount;
framesToSkip -= sampleCount; framesToSkip -= sampleCount;
return; return NO;
} }
framesToSkip = 0; framesToSkip = 0;
@ -536,6 +541,8 @@
// FILE *f = fopen("data.raw", "a"); // FILE *f = fopen("data.raw", "a");
// fwrite(_outputBuffer, channels * 2, _outputFrames, f); // fwrite(_outputBuffer, channels * 2, _outputFrames, f);
// fclose(f); // fclose(f);
return NO;
} }
- (int)decodeMPEGFrame { - (int)decodeMPEGFrame {
@ -687,8 +694,6 @@
} }
- (AudioChunk *)readAudio { - (AudioChunk *)readAudio {
int framesRead = 0;
if(!_firstFrame) if(!_firstFrame)
[self syncFormat]; [self syncFormat];
@ -712,7 +717,9 @@
else if(r == -1) // Unrecoverable error else if(r == -1) // Unrecoverable error
break; break;
[self writeOutput]; if([self writeOutput]) {
return nil;
}
// DLog(@"Wrote output"); // DLog(@"Wrote output");
[self syncFormat]; [self syncFormat];