Dear Sirs...
I am trying to create a simple voip application using android. The settings are as follows:
1- Two android machines are being used: A phone, and a tablet.
2- Both machines are connected via "Wi-Fi Direct"
3- The connectivity is working perfectly fine and I can send and receive data without a problem
4- Each few milliseconds I get the contents of the recording buffer, and send it from the client to the server. No compression is being applied here. I just want to send raw audio samples for now.
5- the samples are played on the other device and I can distinguish that the speech is being transmitted however the noise is very high, so I am not sure what the reason is.
I the code of the client and server threads is shown below. I am sure there are things that I am missing here, so any advice is highly appreciated. I don't want to use an SDK, or anything else, I just want to identify what the problem is.
Thank you
public void run()
{
try
{
InetAddress serverAddr = InetAddress.getByName(this.ServerIPEditText.getText().toString());
int port=Integer.parseInt(this.ServerPortEditText.getText().toString());
Socket socket = new Socket(serverAddr, port);
OutputStream ServerStream=socket.getOutputStream();
AudioRecord recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
RECORDER_SAMPLERATE, RECORDER_CHANNELS,
RECORDER_AUDIO_ENCODING, RECORDER_SAMPLERATE * 4);
recorder.startRecording();
short[] buffer=new short[RECORDER_SAMPLERATE*4];
ByteBuffer byteBuf = ByteBuffer.allocate(2*RECORDER_SAMPLERATE*4);
while (true)
{
Thread.sleep(128);
int count=recorder.read(buffer, 0, buffer.length);
byteBuf.rewind();
for(int i=0;i<count;i++)
byteBuf.putShort(buffer[i]);
int length=count*2;
ServerStream.write(byteBuf.array(),0,length);
ServerStream.flush();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
Server thread:
public void run()
{
try
{
Socket client=this.MyServerSocket.accept();
InputStream ClientStream=client.getInputStream();
short[] buffer=new short[RECORDER_SAMPLERATE*4];
ByteBuffer byteBuf = ByteBuffer.allocate(2*RECORDER_SAMPLERATE*4);
int intSize = android.media.AudioTrack.getMinBufferSize(RECORDER_SAMPLERATE, RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING);
AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC,
RECORDER_SAMPLERATE, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, 44100*4, AudioTrack.MODE_STREAM);
at.play();
byte[] readBuffer=new byte[1024*4];
while (true)
{
int length=readBuffer.length;
int pos=0;
while (pos!=length)
{
pos+=ClientStream.read(readBuffer,pos,length-pos);
}
for(int i=0;i<length/2;i++)
{
int v=readBuffer[i*2]+readBuffer[i*2+1]-32768;
buffer[i]=(short) v;
}
at.write(buffer, 0, length/2);
}
}
catch (IOException e)
{
e.printStackTrace();
}
}