Refactor protocol interface to have separte audio and data paths

pull/24/head
sh123 2022-06-24 17:42:44 +03:00
rodzic 7215be4d3f
commit eabac19687
8 zmienionych plików z 78 dodań i 38 usunięć

Wyświetl plik

@ -19,7 +19,6 @@ import java.nio.ByteBuffer;
import java.util.Timer;
import java.util.TimerTask;
import com.radio.codec2talkie.connect.TcpIpSocketHandler;
import com.radio.codec2talkie.protocol.Callback;
import com.radio.codec2talkie.protocol.Protocol;
import com.radio.codec2talkie.protocol.ProtocolFactory;
@ -230,7 +229,7 @@ public class AudioProcessor extends Thread {
for (int i = 0; i < _recordAudioEncodedBuffer.length; i++) {
frame[i] = (byte)_recordAudioEncodedBuffer[i];
}
_protocol.send(frame);
_protocol.sendAudio(frame);
}
private void decodeAndPlayAudioFrame(byte[] audioFrame) {

Wyświetl plik

@ -44,15 +44,20 @@ public class AudioFrameAggregator implements Protocol {
}
@Override
public void send(byte[] frame) throws IOException {
public void sendAudio(byte[] frame) throws IOException {
if ( _outputBufferPos + frame.length >= _outputBufferSize) {
_childProtocol.send(Arrays.copyOf(_outputBuffer, _outputBufferPos));
_childProtocol.sendAudio(Arrays.copyOf(_outputBuffer, _outputBufferPos));
_outputBufferPos = 0;
}
System.arraycopy(frame, 0, _outputBuffer, _outputBufferPos, frame.length);
_outputBufferPos += frame.length;
}
@Override
public void sendData(byte[] dataPacket) throws IOException {
_childProtocol.sendData(dataPacket);
}
@Override
public boolean receive(Callback callback) throws IOException {
return _childProtocol.receive(new Callback() {
@ -88,7 +93,7 @@ public class AudioFrameAggregator implements Protocol {
@Override
public void flush() throws IOException {
if (_outputBufferPos > 0) {
_childProtocol.send(Arrays.copyOf(_outputBuffer, _outputBufferPos));
_childProtocol.sendAudio(Arrays.copyOf(_outputBuffer, _outputBufferPos));
_outputBufferPos = 0;
}
_childProtocol.flush();

Wyświetl plik

@ -1,7 +1,7 @@
package com.radio.codec2talkie.protocol;
public abstract class Callback {
abstract protected void onReceiveAudioFrames(byte [] frame);
abstract protected void onReceiveSignalLevel(byte [] rawData);
abstract protected void onReceiveAudioFrames(byte[] frame);
abstract protected void onReceiveSignalLevel(byte[] rawData);
abstract protected void onProtocolRxError();
}

Wyświetl plik

@ -191,18 +191,13 @@ public class Kiss implements Protocol {
};
@Override
public void send(byte [] frame) throws IOException {
// escape
ByteBuffer escapedFrame = escape(frame);
int escapedFrameSize = escapedFrame.position();
escapedFrame.rewind();
public void sendAudio(byte [] frame) throws IOException {
send(frame);
}
// send
startKissPacket(KISS_CMD_DATA);
while (escapedFrame.position() < escapedFrameSize) {
sendKissByte(escapedFrame.get());
}
completeKissPacket();
@Override
public void sendData(byte[] dataPacket) throws IOException {
send(dataPacket);
}
@Override
@ -229,6 +224,20 @@ public class Kiss implements Protocol {
}
}
private void send(byte[] data) throws IOException {
// escape
ByteBuffer escapedFrame = escape(data);
int escapedFrameSize = escapedFrame.position();
escapedFrame.rewind();
// send
startKissPacket(KISS_CMD_DATA);
while (escapedFrame.position() < escapedFrameSize) {
sendKissByte(escapedFrame.get());
}
completeKissPacket();
}
private void processCommand(byte b) {
switch (b) {
case KISS_CMD_DATA:

Wyświetl plik

@ -8,7 +8,8 @@ import java.io.IOException;
public interface Protocol {
void initialize(Transport transport, Context context) throws IOException;
void send(byte [] frame) throws IOException;
void sendAudio(byte[] frame) throws IOException;
void sendData(byte[] dataPacket) throws IOException;
boolean receive(Callback callback) throws IOException;
void flush() throws IOException;
void close();

Wyświetl plik

@ -24,10 +24,15 @@ public class Raw implements Protocol {
}
@Override
public void send(byte [] frame) throws IOException {
public void sendAudio(byte [] frame) throws IOException {
_transport.write(Arrays.copyOf(frame, frame.length));
}
@Override
public void sendData(byte[] dataPacket) throws IOException {
_transport.write(Arrays.copyOf(dataPacket, dataPacket.length));
}
@Override
public boolean receive(Callback callback) throws IOException {
int bytesRead = _transport.read(_rxDataBuffer);

Wyświetl plik

@ -44,11 +44,16 @@ public class RecorderPipe implements Protocol {
}
@Override
public void send(byte[] frame) throws IOException {
_childProtocol.send(frame);
public void sendAudio(byte[] frame) throws IOException {
_childProtocol.sendAudio(frame);
writeToFile(frame);
}
@Override
public void sendData(byte[] dataPacket) throws IOException {
_childProtocol.sendData(dataPacket);
}
@Override
public boolean receive(Callback callback) throws IOException {
return _childProtocol.receive(new Callback() {

Wyświetl plik

@ -42,24 +42,18 @@ public class ScramblerPipe implements Protocol {
}
@Override
public void send(byte[] audioFrame) throws IOException {
ScramblingTools.ScrambledData data = null;
try {
data = ScramblingTools.scramble(_scramblingKey, audioFrame, _iterationsCount);
} catch (NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeySpecException |
InvalidKeyException | BadPaddingException | IllegalBlockSizeException |
InvalidAlgorithmParameterException e) {
e.printStackTrace();
public void sendAudio(byte[] audioFrame) throws IOException {
byte[] result = scrable(audioFrame);
if (result != null) {
_childProtocol.sendData(result);
}
if (data != null) {
byte[] result = new byte[data.iv.length + data.salt.length + data.scrambledData.length];
}
System.arraycopy(data.iv, 0, result, 0, data.iv.length);
System.arraycopy(data.salt, 0, result, data.iv.length, data.salt.length);
System.arraycopy(data.scrambledData, 0, result, data.iv.length + data.salt.length, data.scrambledData.length);
_childProtocol.send(result);
@Override
public void sendData(byte[] dataPacket) throws IOException {
byte[] result = scrable(dataPacket);
if (result != null) {
_childProtocol.sendData(result);
}
}
@ -121,4 +115,26 @@ public class ScramblerPipe implements Protocol {
public void close() {
_childProtocol.close();
}
private byte[] scrable(byte[] srcData) throws IOException {
ScramblingTools.ScrambledData data = null;
try {
data = ScramblingTools.scramble(_scramblingKey, srcData, _iterationsCount);
} catch (NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeySpecException |
InvalidKeyException | BadPaddingException | IllegalBlockSizeException |
InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
if (data != null) {
byte[] result = new byte[data.iv.length + data.salt.length + data.scrambledData.length];
System.arraycopy(data.iv, 0, result, 0, data.iv.length);
System.arraycopy(data.salt, 0, result, data.iv.length, data.salt.length);
System.arraycopy(data.scrambledData, 0, result, data.iv.length + data.salt.length, data.scrambledData.length);
return result;
}
return null;
}
}