【发布时间】:2014-04-24 15:53:15
【问题描述】:
当我们需要流式传输音频时,没有太多关于应用此编解码器的信息。如果不应用编解码器,我的代码就像在 2 个设备之间建立通信的魅力一样工作,但我需要以这种格式编码/解码,因为我需要与服务器而不是在两个设备之间进行流式传输(我正在使用 2 个设备测试此代码)。
如果你们中的任何人都能看到我的问题的关键在哪里,我正在寻找机会。我尝试了输入参数的不同配置。也许,我使用的编解码器是错误的(我从一个具有 Apache 许可证的项目中获取它们。)
这些值在记录器-发送器中设置,就像在播放器-接收器设备中一样:
private int port=50005;
private int sampleRate = 8000 ;//44100;
private int channelConfig = AudioFormat.CHANNEL_OUT_MONO;
private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
int minBufSize = AudioTrack.getMinBufferSize(sampleRate, channelConfig, audioFormat);
注意:播放器中的 CHANNEL_OUT_MONO 和录音器中的 CHANNEL_IN_MONO。
这些是我的方法:
public void startStreamingEncoding() {
Thread streamThread = new Thread(new Runnable() {
@Override
public void run() {
try {
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
DatagramSocket socket = new DatagramSocket();
short[] buffer = new short[minBufSize];
DatagramPacket packet;
final InetAddress destination = InetAddress.getByName(ip_receiver);
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,sampleRate,channelConfig,audioFormat,minBufSize*10);
recorder.startRecording();
/////Encoding:
Encoder encoder = new G711UCodec();
byte[] outBuffer = new byte[minBufSize];
while(status == true) {
//reading data from MIC into buffer
minBufSize = recorder.read(buffer, 0, buffer.length);
//Encoding:
encoder.encode(buffer, minBufSize, outBuffer, 0);
//putting buffer in the packet
packet = new DatagramPacket (outBuffer, outBuffer.length, destination,port);
socket.send(packet);
}
} catch(UnknownHostException e) {
Log.e("VS", "UnknownHostException");
} catch (IOException e) {
e.printStackTrace();
Log.e("VS", "IOException");
}
}
});
streamThread.start();
}
以及播放和解码流的方法:
public void playerAudioDecoding()
{
Thread thrd = new Thread(new Runnable() {
@Override
public void run()
{
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
AudioTrack track = new AudioTrack(AudioManager.STREAM_MUSIC,
sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, minBufSize,
AudioTrack.MODE_STREAM);
track.play();
Decoder decoder = new G711UCodec();
try
{
DatagramSocket sock = new DatagramSocket(port);
byte[] buf = new byte[minBufSize];
while(true)
{
DatagramPacket pack = new DatagramPacket(buf, minBufSize);
sock.receive(pack);
//Decoding:
int size = pack.getData().length;
short[] shortArray = new short[size];
decoder.decode(shortArray, pack.getData(), minBufSize, 0);
byte[] array = MyShortToByte(shortArray);
track.write(array, 0, array.length);
}
}
catch (SocketException se)
{
Log.e("Error", "SocketException: " + se.toString());
}
catch (IOException ie)
{
Log.e("Error", "IOException" + ie.toString());
}
} // end run
});
thrd.start();
}
这是我使用 Apache 许可证的编解码器类:
public class G711UCodec implements Encoder, Decoder {
// s00000001wxyz...s000wxyz
// s0000001wxyza...s001wxyz
// s000001wxyzab...s010wxyz
// s00001wxyzabc...s011wxyz
// s0001wxyzabcd...s100wxyz
// s001wxyzabcde...s101wxyz
// s01wxyzabcdef...s110wxyz
// s1wxyzabcdefg...s111wxyz
private static byte[] table13to8 = new byte[8192];
private static short[] table8to16 = new short[256];
static {
// b13 --> b8
for (int p = 1, q = 0; p <= 0x80; p <<= 1, q+=0x10) {
for (int i = 0, j = (p << 4) - 0x10; i < 16; i++, j += p) {
int v = (i + q) ^ 0x7F;
byte value1 = (byte) v;
byte value2 = (byte) (v + 128);
for (int m = j, e = j + p; m < e; m++) {
table13to8[m] = value1;
table13to8[8191 - m] = value2;
}
}
}
// b8 --> b16
for (int q = 0; q <= 7; q++) {
for (int i = 0, m = (q << 4); i < 16; i++, m++) {
int v = (((i + 0x10) << q) - 0x10) << 3;
table8to16[m ^ 0x7F] = (short) v;
table8to16[(m ^ 0x7F) + 128] = (short) (65536 - v);
}
}
}
public int decode(short[] b16, byte[] b8, int count, int offset) {
for (int i = 0, j = offset; i < count; i++, j++) {
b16[i] = table8to16[b8[j] & 0xFF];
}
return count;
}
public int encode(short[] b16, int count, byte[] b8, int offset) {
for (int i = 0, j = offset; i < count; i++, j++) {
b8[j] = table13to8[(b16[i] >> 4) & 0x1FFF];
}
return count;
}
public int getSampleCount(int frameSize) {
return frameSize;
}
}
真的,我不知道会发生什么;如果我将 sampleRate 更改为 4000,我可以识别我的声音和一些单词,但会有很多回声。我再说一遍,如果禁用编码/解码过程并在 PCM 中进行流式传输,质量非常好。让看看是否有人可以帮助我并提前感谢。
【问题讨论】:
-
一些旧代码我使用了两倍推荐的 AudioTrack 缓冲区和 10 倍的 AudioRecord 缓冲区。我不记得确切原因,但可能与您的问题有关。
-
我正在研究一个基于 RTP/UDP 的 PCM 解决方案,我经常听到这种咔哒声、碰撞声,几乎是鼓声,似乎与收到的样本一致,你有这个问题吗?有什么想法吗?
-
很久没有玩过这个特性/实现了。我记得在开始时我重现了与您相同的声音问题,但我认为我们解决了围绕缓冲播放的问题。我不能给你一个解决方案,因为我不记得了......只有我知道使用我发布的编码,音频很好。
标签: android codec pcm audiorecord audiotrack