我理解流buffer
对IO一般很有用。
我想问一下buffer
应该如何在实时的voIP应用程序中使用,
例如stream in(Android)MediaRecorder输出 - > LocalSocket - > UDP / RTP
对我而言,看起来缓冲任何流都没有空间。
我说错了吗?还是我错过了重要的事情?
在这种情况下不使用缓冲区时设备资源效率(CPU和Buttery)如何?
伪代码
//UNIX type localSocket Server to receive stream from mediaRecorder
server = new LocalServerSocket(SOCKET_ADDRESS);
// local socket between mediaRecorder and localSocket Server
sender = new LocalSocket();
sender.connect(new LocalSocketAddress(SOCKET_ADDRESS));
//mediaRecorder
mRecorder = new MediaRecorder();
//.....................
//sender.getFileDescriptor() is set as the vitual Outputfile
mRecorder.setOutputFile(sender.getFileDescriptor());
mRecorder.prepare();
mRecorder.start();
//localSocket Server accept the stream to receiver
receiver = server.accept();
int val;
byte[] buf;
DatagramPacket packet;
ds = new DatagramSocket(localPort);
while (localServerLoop)
{
//receiver stream is read to (int)val
val = receiver.getInputStream().read();
//======localServer to remoteUDP======================
// (int)val to (byte)buf
buf = toBytes(val);
// byte to packet
packet = new DatagramPacket(buf, buf.length,
InetAddress.getByName(remoteHost), remotePort);
//send to remoteHost
ds.send(packet);
//=========================
}
答案 0 :(得分:0)
您正在从流中读取单个字节,因此没有什么可以缓冲。使用带有字节数组的read()
方法之一,这将是您的缓冲区。您还可以使用BufferedInputStream
等进行换行。您可能希望了解Java中的流如何工作,这不是Android特定的。
请参阅http://developer.android.com/reference/java/io/InputStream.html#read(byte[])