gpt4 book ai didi

android - PCM -> AAC (Encoder) -> PCM(Decoder) 实时正确优化

转载 作者:IT老高 更新时间:2023-10-28 23:06:34 29 4
gpt4 key购买 nike

我正在尝试实现

AudioRecord (MIC) ->

PCM -> AAC Encoder
AAC -> PCM Decode

-> AudioTrack?? (SPEAKER)

在 Android 4.1+ (API16) 上使用 MediaCodec

首先,我成功(但不确定是否优化)通过 MediaCodec 实现了 PCM -> AAC Encoder,如下所示

private boolean setEncoder(int rate)
{
encoder = MediaCodec.createEncoderByType("audio/mp4a-latm");
MediaFormat format = new MediaFormat();
format.setString(MediaFormat.KEY_MIME, "audio/mp4a-latm");
format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
format.setInteger(MediaFormat.KEY_SAMPLE_RATE, 44100);
format.setInteger(MediaFormat.KEY_BIT_RATE, 64 * 1024);//AAC-HE 64kbps
format.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectHE);
encoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
return true;
}

输入:PCM 比特率 = 44100(Hz) x 16(bit) x 1(Monoral) = 705600 bit/s

输出:AAC-HE 比特率 = 64 x 1024(bit) = 65536 bit/s

所以,数据大小近似压缩x11,我通过观察日志确认了这个工作

  • AudioRecoder﹕读取 4096 字节
  • AudioEncoder: 369 字节编码

数据大小近似压缩x11,目前为止还不错。

现在,我有一个 UDP 服务器来接收编码数据,然后对其进行解码。

解码器配置文件设置如下:

private boolean setDecoder(int rate)
{
decoder = MediaCodec.createDecoderByType("audio/mp4a-latm");
MediaFormat format = new MediaFormat();
format.setString(MediaFormat.KEY_MIME, "audio/mp4a-latm");
format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
format.setInteger(MediaFormat.KEY_SAMPLE_RATE, 44100);
format.setInteger(MediaFormat.KEY_BIT_RATE, 64 * 1024);//AAC-HE 64kbps
format.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectHE);
decoder.configure(format, null, null, 0);

return true;
}

由于 UDPserver 数据包缓冲区大小为 1024

  • UDPserver : 收到 1024 个字节

由于这是压缩的 AAC 数据,我希望解码大小为

大约1024 x11,但实际结果是

  • AudioDecoder﹕解码8192字节

大概是x8,感觉有点不对劲。

解码器代码如下:

    IOudpPlayer = new Thread(new Runnable()
{
public void run()
{
SocketAddress sockAddress;
String address;

int len = 1024;
byte[] buffer2 = new byte[len];
DatagramPacket packet;

byte[] data;

ByteBuffer[] inputBuffers;
ByteBuffer[] outputBuffers;

ByteBuffer inputBuffer;
ByteBuffer outputBuffer;

MediaCodec.BufferInfo bufferInfo;
int inputBufferIndex;
int outputBufferIndex;
byte[] outData;
try
{
decoder.start();
isPlaying = true;
while (isPlaying)
{
try
{
packet = new DatagramPacket(buffer2, len);
ds.receive(packet);

sockAddress = packet.getSocketAddress();
address = sockAddress.toString();

Log.d("UDP Receiver"," received !!! from " + address);

data = new byte[packet.getLength()];
System.arraycopy(packet.getData(), packet.getOffset(), data, 0, packet.getLength());

Log.d("UDP Receiver", data.length + " bytes received");

//===========
inputBuffers = decoder.getInputBuffers();
outputBuffers = decoder.getOutputBuffers();
inputBufferIndex = decoder.dequeueInputBuffer(-1);
if (inputBufferIndex >= 0)
{
inputBuffer = inputBuffers[inputBufferIndex];
inputBuffer.clear();

inputBuffer.put(data);

decoder.queueInputBuffer(inputBufferIndex, 0, data.length, 0, 0);
}

bufferInfo = new MediaCodec.BufferInfo();
outputBufferIndex = decoder.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);

Log.d("AudioDecoder", outData.length + " bytes decoded");

decoder.releaseOutputBuffer(outputBufferIndex, false);
outputBufferIndex = decoder.dequeueOutputBuffer(bufferInfo, 0);

}



//===========

}
catch (IOException e)
{
}
}

decoder.stop();

}
catch (Exception e)
{
}
}
});

完整代码:

https://gist.github.com/kenokabe/9029256

还需要权限:

 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>

成员(member)fadden为 Google 工作的人告诉我

看起来我没有在输出缓冲区上设置位置和限制。

我读过 VP8 Encoding Nexus 5 returns empty/0-Frames ,但不确定如何正确实现。


更新:我有点明白在哪里修改

Looks like I'm not setting position & limit on the output buffer.

,所以在Encoder和Decoder的while循环中添加2行如下:

 outputBuffer.position(bufferInfo.offset);
outputBuffer.limit(bufferInfo.offset + bufferInfo.size);

https://gist.github.com/kenokabe/9029256/revisions

但是结果是一样的。

现在,我认为,错误: W/SoftAAC2: AAC 解码器返回错误 16388,替换为静音。 表示此解码器从一开始就完全失败。这又是 数据不可搜索 问题。 Seeking in AAC streams on Android如果 AAC 解码器不能以这种方式处理流数据而只能添加一些 header ,那将是非常令人失望的。


UPDATE2:UDP 接收器出错,因此修改

https://gist.github.com/kenokabe/9029256

现在,错误

W/SoftAAC2:AAC 解码器返回错误 16388,替换为静音。消失了!!

因此,它表明解码器至少可以正常工作,

但是,这是 1 个周期的日志:

D/AudioRecoder﹕ 4096 bytes read
D/AudioEncoder﹕ 360 bytes encoded
D/UDP Receiver﹕ received !!! from /127.0.0.1:39000
D/UDP Receiver﹕ 360 bytes received
D/AudioDecoder﹕ 8192 bytes decoded

PCM(4096)->AACencoded(360)->UDP-AAC(360)->(应该是)PCM(8192)

最终结果大约是原始 PCM 大小的 2 倍,还是有问题。


所以我的问题是

  1. 您能否正确优化我的示例代码以使其正常工作?

  2. 使用 AudioTrack API 实时播放解码的 PCM 原始数据是否正确,您能告诉我正确的方法吗?示例代码表示赞赏。

谢谢。

PS。我的项目目标是 Android4.1+(API16),我读过 API18(Andeoid 4.3+) 上的东西更容易,但是出于明显的兼容性原因,不幸的是,我不得不在这里跳过 MediaMuxer 等...

最佳答案

经过测试,这是我通过修改您的代码得出的结论:

 package com.example.app;

import android.app.Activity;

import android.media.AudioManager;
import android.media.MediaCodecInfo;
import android.media.MediaFormat;
import android.os.Bundle;

import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaCodec;

import android.media.MediaRecorder.AudioSource;

import android.util.Log;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketAddress;
import java.net.SocketException;
import java.nio.ByteBuffer;

public class MainActivity extends Activity
{
private AudioRecord recorder;
private AudioTrack player;

private MediaCodec encoder;
private MediaCodec decoder;

private short audioFormat = AudioFormat.ENCODING_PCM_16BIT;
private short channelConfig = AudioFormat.CHANNEL_IN_MONO;

private int bufferSize;
private boolean isRecording;
private boolean isPlaying;

private Thread IOrecorder;

private Thread IOudpPlayer;


private DatagramSocket ds;
private final int localPort = 39000;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

IOrecorder = new Thread(new Runnable()
{
public void run()
{
int read;
byte[] buffer1 = new byte[bufferSize];

ByteBuffer[] inputBuffers;
ByteBuffer[] outputBuffers;

ByteBuffer inputBuffer;
ByteBuffer outputBuffer;

MediaCodec.BufferInfo bufferInfo;
int inputBufferIndex;
int outputBufferIndex;

byte[] outData;

DatagramPacket packet;
try
{
encoder.start();
recorder.startRecording();
isRecording = true;
while (isRecording)
{
read = recorder.read(buffer1, 0, bufferSize);
// Log.d("AudioRecoder", read + " bytes read");
//------------------------

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);


// Log.d("AudioEncoder ", outData.length + " bytes encoded");
//-------------
packet = new DatagramPacket(outData, outData.length,
InetAddress.getByName("127.0.0.1"), localPort);
ds.send(packet);
//------------

encoder.releaseOutputBuffer(outputBufferIndex, false);
outputBufferIndex = encoder.dequeueOutputBuffer(bufferInfo, 0);

}
// ----------------------;

}
encoder.stop();
recorder.stop();
}
catch (Exception e)
{
e.printStackTrace();
}
}
});



IOudpPlayer = new Thread(new Runnable()
{
public void run()
{
SocketAddress sockAddress;
String address;

int len = 2048
byte[] buffer2 = new byte[len];
DatagramPacket packet;

byte[] data;

ByteBuffer[] inputBuffers;
ByteBuffer[] outputBuffers;

ByteBuffer inputBuffer;
ByteBuffer outputBuffer;

MediaCodec.BufferInfo bufferInfo;
int inputBufferIndex;
int outputBufferIndex;
byte[] outData;
try
{
player.play();
decoder.start();
isPlaying = true;
while (isPlaying)
{
try
{
packet = new DatagramPacket(buffer2, len);
ds.receive(packet);

sockAddress = packet.getSocketAddress();
address = sockAddress.toString();

// Log.d("UDP Receiver"," received !!! from " + address);

data = new byte[packet.getLength()];
System.arraycopy(packet.getData(), packet.getOffset(), data, 0, packet.getLength());

// Log.d("UDP Receiver", data.length + " bytes received");

//===========
inputBuffers = decoder.getInputBuffers();
outputBuffers = decoder.getOutputBuffers();
inputBufferIndex = decoder.dequeueInputBuffer(-1);
if (inputBufferIndex >= 0)
{
inputBuffer = inputBuffers[inputBufferIndex];
inputBuffer.clear();

inputBuffer.put(data);

decoder.queueInputBuffer(inputBufferIndex, 0, data.length, 0, 0);
}

bufferInfo = new MediaCodec.BufferInfo();
outputBufferIndex = decoder.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);

// Log.d("AudioDecoder", outData.length + " bytes decoded");

player.write(outData, 0, outData.length);

decoder.releaseOutputBuffer(outputBufferIndex, false);
outputBufferIndex = decoder.dequeueOutputBuffer(bufferInfo, 0..

关于android - PCM -> AAC (Encoder) -> PCM(Decoder) 实时正确优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21804390/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com