how to continuously send data without blocking?
- by Donal Rafferty
I am trying to send rtp audio data from my Android application.
I currently can send 1 RTP packet with the code below and I also have another class that extends Thread that listens to and receives RTP packets.
My question is how do I continuously send my updated buffer through the packet payload without blocking the receiving thread?
public void run() {
isRecording = true;
android.os.Process.setThreadPriority
(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
int buffersize = AudioRecord.getMinBufferSize(8000,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT);
Log.d("BUFFERSIZE","Buffer size = " + buffersize);
arec = new AudioRecord(MediaRecorder.AudioSource.MIC,
8000,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,
buffersize);
short[] readBuffer = new short[80];
byte[] buffer = new byte[160];
arec.startRecording();
while(arec.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING){
int frames = arec.read(readBuffer, 0, 80);
@SuppressWarnings("unused")
int lenghtInBytes = codec.encode(readBuffer, 0, buffer, frames);
RtpPacket rtpPacket = new RtpPacket();
rtpPacket.setV(2);
rtpPacket.setX(0);
rtpPacket.setM(0);
rtpPacket.setPT(0);
rtpPacket.setSSRC(123342345);
rtpPacket.setPayload(buffer, 160);
try {
rtpSession2.sendRtpPacket(rtpPacket);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RtpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
So when I send on one device and receive on another I get decent audio, but when I send and receive on both I get broken sound like its taking turns to send and receive audio.
I have a feeling it could be to do with the while loop? it could be looping around in there and not letting anything else run?