|
|
@@ -11,6 +11,8 @@ import java.util.List;
|
|
|
public final class AudioFrameDecoder extends ByteToMessageDecoder {
|
|
|
private static final int MAX_WAV_BYTES = 8 * 1024 * 1024;
|
|
|
private final int rawPcmBytes;
|
|
|
+ // 损坏WAV头之后寻找下一条元数据或有效WAV,不能将坏头残留误当80K PCM等待。
|
|
|
+ private boolean resynchronizing;
|
|
|
public AudioFrameDecoder() { this(0); }
|
|
|
public AudioFrameDecoder(int rawPcmBytes) {
|
|
|
if (rawPcmBytes < 0 || rawPcmBytes > MAX_WAV_BYTES - 44 || rawPcmBytes % 2 != 0) {
|
|
|
@@ -33,16 +35,19 @@ public final class AudioFrameDecoder extends ByteToMessageDecoder {
|
|
|
return;
|
|
|
}
|
|
|
out.add(in.readRetainedSlice(end - start));
|
|
|
+ resynchronizing = false;
|
|
|
} else if (prefix.equals("RIFF")) {
|
|
|
if (in.readableBytes() < 12) return;
|
|
|
long length = in.getUnsignedIntLE(start + 4) + 8;
|
|
|
if (length < 44 || length > MAX_WAV_BYTES || in.getInt(start + 8) != 0x57415645) {
|
|
|
in.skipBytes(4);
|
|
|
+ resynchronizing = true;
|
|
|
continue;
|
|
|
}
|
|
|
if (in.readableBytes() < length) return;
|
|
|
out.add(in.readRetainedSlice((int) length));
|
|
|
- } else if (rawPcmBytes > 0) {
|
|
|
+ resynchronizing = false;
|
|
|
+ } else if (rawPcmBytes > 0 && !resynchronizing) {
|
|
|
if (in.readableBytes() < rawPcmBytes) return;
|
|
|
ByteBuf wav = Unpooled.buffer(44 + rawPcmBytes);
|
|
|
wav.writeInt(0x52494646).writeIntLE(36 + rawPcmBytes).writeInt(0x57415645);
|