我通过类似API的套接字处理VoIP。 (使用窄带连接)
我需要编码每个语音帧(20ms)并通过所述api发送,然后在另一侧解码。
我尝试通过NDK与Opus合作,但它没有去任何地方所以我决定使用MediaCodec对AMR-NB进行编码/解码。
编码似乎有效,导致预期大小的缓冲区(160个原始字节到20个编码字节+ 7.9Kbps的报头)
但是当我处理编码缓冲区并尝试解码时,我收到INFO_OUTPUT_FORMAT_CHANGED
结果。
编码器:
// Set up recorder
int recordBufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
arec = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLE_RATE, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, recordBufferSize);
// Set Up codec
try {
encoder = MediaCodec.createEncoderByType(MediaFormat.MIMETYPE_AUDIO_AMR_NB);
MediaFormat format = new MediaFormat();
format.setString(MediaFormat.KEY_MIME, MediaFormat.MIMETYPE_AUDIO_AMR_NB);
format.setInteger(MediaFormat.KEY_SAMPLE_RATE, SAMPLE_RATE);
format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
format.setInteger(MediaFormat.KEY_BIT_RATE, bitrate.getVal());
encoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
} catch (IOException e) {
e.printStackTrace();
return;
}
// Start Recording
int read;
byte[] buffer1 = new byte[bufferSize];
ByteBuffer[] inputBuffers;
ByteBuffer[] outputBuffers;
ByteBuffer inputBuffer;
ByteBuffer outputBuffer;
MediaCodec.BufferInfo bufferInfo;
int inputBufferIndex;
int outputBufferIndex;
byte[] outData;
Frame frame;
try {
encoder.start();
arec.startRecording();
isRecording = true;
while (isRecording) {
read = arec.read(buffer1, 0, bufferSize);
inputBuffers = encoder.getInputBuffers();
outputBuffers = encoder.getOutputBuffers();
inputBufferIndex = encoder.dequeueInputBuffer(-1);
if (inputBufferIndex >= 0) {
inputBuffer = inputBuffers[inputBufferIndex];
inputBuffer.clear();
inputBuffer.put(buffer1);
encoder.queueInputBuffer(inputBufferIndex, 0, buffer1.length, 0, 0);
}
bufferInfo = new MediaCodec.BufferInfo();
outputBufferIndex = encoder.dequeueOutputBuffer(bufferInfo, 0);
while (outputBufferIndex >= 0) {
outputBuffer = outputBuffers[outputBufferIndex];
outputBuffer.position(bufferInfo.offset);
outputBuffer.limit(bufferInfo.offset + bufferInfo.size);
outData = new byte[bufferInfo.size];
outputBuffer.get(outData);
//-------------
frame = new Frame(outData);
handler.onFrameEncoded(frame);
//------------
encoder.releaseOutputBuffer(outputBufferIndex, false);
outputBufferIndex = encoder.dequeueOutputBuffer(bufferInfo, 0);
}
}
encoder.stop();
arec.stop();
} catch (Exception e) {
e.printStackTrace();
}
解码器:
@Override
public void onFrameEncoded(Frame frame) {
try {
MediaCodec decoder = MediaCodec.createDecoderByType(MediaFormat.MIMETYPE_AUDIO_AMR_NB);
MediaFormat format = new MediaFormat();
format.setString(MediaFormat.KEY_MIME, MediaFormat.MIMETYPE_AUDIO_AMR_NB);
format.setInteger(MediaFormat.KEY_SAMPLE_RATE, SAMPLE_RATE);
format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
format.setInteger(MediaFormat.KEY_BIT_RATE, 7950);
decoder.configure(format, null, null, 0);
decoder.start();
byte[] outData;
ByteBuffer[] inputBuffers;
ByteBuffer[] outputBuffers;
ByteBuffer inputBuffer;
ByteBuffer outputBuffer;
MediaCodec.BufferInfo bufferInfo;
int inputBufferIndex;
int outputBufferIndex;
inputBuffers = decoder.getInputBuffers();
outputBuffers = decoder.getOutputBuffers();
// Fill decoder input buffer
inputBufferIndex = decoder.dequeueInputBuffer(-1);
if (inputBufferIndex >= 0) {
inputBuffer = inputBuffers[inputBufferIndex];
inputBuffer.clear();
inputBuffer.put(frame.Buffer);
decoder.queueInputBuffer(inputBufferIndex, 0, frame.Buffer.length, 0, 0);
}
// Get Output
bufferInfo = new MediaCodec.BufferInfo();
outputBufferIndex = decoder.dequeueOutputBuffer(bufferInfo, -1);
if (outputBufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) { // Just a check I threw in
MediaFormat format2 = decoder.getOutputFormat(); // Returns format RAW
}
while (outputBufferIndex >= 0) {
outputBuffer = outputBuffers[outputBufferIndex];
outputBuffer.position(bufferInfo.offset);
outputBuffer.limit(bufferInfo.offset + bufferInfo.size);
outData = new byte[bufferInfo.size];
outputBuffer.get(outData);
// Log.d("Decoder", outData.length + " bytes encoded");
decoder.releaseOutputBuffer(outputBufferIndex, false);
outputBufferIndex = decoder.dequeueOutputBuffer(bufferInfo, 0);
}
} catch (IOException e) {
e.printStackTrace();
}
}
我到处寻找INFO_OUTPUT_FORMAT_CHANGED
,但没有找到任何可以帮助我的事情......
答案 0 :(得分:0)
这是预期的行为。如果您使用的是MediaMuxer,则会将MediaFormat传递给addTrack()
,这需要一些必须来自MediaCodec的信息。引入MediaMuxer时,此行为为added in API 18。
有关示例,请参阅EncodeAndMuxTest。
如果您不使用MediaMuxer,则可以忽略它。 (您可能只想记录它以确认一切仍然如此。)