Permalink
Browse files

alac_decode implemented, but buggy. seqno bug corrected.

  • Loading branch information...
1 parent 375fcbb commit fb45f92b5421786eba99a996d6b5e854260362a4 @bencall committed Apr 23, 2011
Showing with 94 additions and 15 deletions.
  1. +1 −1 src/AudioData.java
  2. +92 −14 src/AudioServer.java
  3. +1 −0 src/UDPListener.java
View
2 src/AudioData.java
@@ -5,5 +5,5 @@
*/
public class AudioData {
public boolean ready;
- public byte[] data;
+ public int[] data;
}
View
106 src/AudioServer.java
@@ -5,16 +5,30 @@
*/
import java.io.IOException;
+import java.io.StringReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
+import java.security.KeyPair;
+import java.security.Security;
+import java.util.concurrent.locks.Condition;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+import javax.crypto.Cipher;
+import javax.crypto.spec.IvParameterSpec;
+import javax.crypto.spec.SecretKeySpec;
+
+import org.bouncycastle.jce.provider.BouncyCastleProvider;
+import org.bouncycastle.openssl.PEMReader;
import com.beatofthedrum.alacdecoder.*;
public class AudioServer implements UDPDelegate{
// Constantes
public static final int BUFFER_FRAMES = 512; // Total buffer size (number of frame)
public static final int START_FILL = 282; // Alac will wait till there are START_FILL frames in buffer
-
+ public static final int MAX_PACKET = 2048; // Also in UDPListener (possible to merge it in one place?)
+
// Variables d'instances
int[]fmtp; // Sound infos
AudioData[] audioBuffer; // Buffer audio
@@ -24,9 +38,23 @@
int writeIndex;
int actualBufSize;
DatagramSocket sock, csock;
-
+ // Keys
+ byte[] aesiv;
+ byte[] aeskey;
+ //Decoder
+ AlacFile alac;
+ int frameSize;
+ // Mutex locks
+ private final Lock lock = new ReentrantLock();
+ private final Condition bufferOkCond = lock.newCondition();
+
public AudioServer(byte[] aesiv, byte[] aeskey, int[] fmtp, int controlPort, int timingPort){
+ // Init instance var
this.fmtp = fmtp;
+ this.aesiv = aesiv;
+ this.aeskey = aeskey;
+
+ // Init functions
this.initDecoder();
this.initBuffer();
this.initRTP();
@@ -56,14 +84,14 @@ private void initRTP(){
}
private void initDecoder(){
-// int frameSize = fmtp[1];
+ frameSize = fmtp[1];
// int samplingRate = fmtp[11];
int sampleSize = fmtp[3];
if (sampleSize != 16){
return;
}
- AlacFile alac = AlacDecodeUtils.create_alac(sampleSize, 2);
+ alac = AlacDecodeUtils.create_alac(sampleSize, 2);
alac.setinfo_max_samples_per_frame = 3;
alac.setinfo_7a = fmtp[2];
alac.setinfo_sample_size = sampleSize;
@@ -92,32 +120,36 @@ public void packetReceived(DatagramSocket socket, DatagramPacket packet) {
}
//seqno is on two byte
- int seqno = ((int)pktp[2] << 8) + (pktp[3] & 0xff);
+ int seqno = (int)((pktp[2] & 0xff)*256 + (pktp[3] & 0xff));
+
this.putPacketInBuffer(seqno, pktp);
}
}
private void putPacketInBuffer(int seqno, byte[] data){
// Ring buffer may be implemented in a Hashtable in java (simplier), but is it fast enough?
+
+ // We lock the thread
+ lock.lock();
if(!synced){
writeIndex = seqno;
readIndex = seqno;
synced = true;
}
-
+ System.out.println("ID: " + (seqno % BUFFER_FRAMES));
if (seqno == writeIndex){ // Packet we expected
- audioBuffer[(seqno % BUFFER_FRAMES)].data = this.alac_decode(data); // With (seqno % BUFFER_FRAMES) we loop from 0 to BUFFER_FRAMES
- audioBuffer[(seqno % BUFFER_FRAMES)].ready = true;
+ // audioBuffer[(seqno % BUFFER_FRAMES)].data = this.alac_decode(data); // With (seqno % BUFFER_FRAMES) we loop from 0 to BUFFER_FRAMES
+ //audioBuffer[(seqno % BUFFER_FRAMES)].ready = true;
writeIndex++;
} else if(seqno > writeIndex){ // Too early, did we miss some packet between writeIndex and seqno?
this.request_resend(writeIndex, seqno);
- audioBuffer[(seqno % BUFFER_FRAMES)].data = this.alac_decode(data);
- audioBuffer[(seqno % BUFFER_FRAMES)].ready = true;
+ //audioBuffer[(seqno % BUFFER_FRAMES)].data = this.alac_decode(data);
+ //audioBuffer[(seqno % BUFFER_FRAMES)].ready = true;
writeIndex = seqno + 1;
} else if(seqno > readIndex){ // readIndex < seqno < writeIndex not yet played but too late. Still ok
- audioBuffer[(seqno % BUFFER_FRAMES)].data = this.alac_decode(data);
- audioBuffer[(seqno % BUFFER_FRAMES)].ready = true;
+ //audioBuffer[(seqno % BUFFER_FRAMES)].data = this.alac_decode(data);
+ //audioBuffer[(seqno % BUFFER_FRAMES)].ready = true;
} else {
System.err.println("Late packet with seq. numb.: " + seqno); // Really to late
}
@@ -128,8 +160,11 @@ private void putPacketInBuffer(int seqno, byte[] data){
actualBufSize = actualBufSize + BUFFER_FRAMES;
}
+ // We unlock the tread
+ lock.unlock();
+
if(!decoder_isStopped && actualBufSize > START_FILL){
- // TODO Signal to alac thread that it is ready
+// TODO bufferOkCond.signal();
}
}
@@ -139,8 +174,51 @@ private void request_resend(int writeIndex2, int seqno) {
}
- private byte[] alac_decode(byte[] data){
+ private int[] alac_decode(byte[] data){
// TODO Auto-generated method stub
+
+ byte[] packet = new byte[MAX_PACKET];
+ int i;
+ for (i=0; i+16<=data.length; i += 16){
+ // 16 byte block data
+ byte[] buffer = new byte[16];
+ for (int j=0 ; j<16 ; j++){
+ buffer[j] = data[i+j];
+ }
+
+ // Encrypt
+ byte[] buf = this.encryptAES(buffer);
+
+ // We merge everything
+ for (int j=0 ; j<16 ; j++){
+ packet[i+j] = buf[j];
+ }
+ }
+
+ // The rest of the packet is unencrypted
+ for (int k = 0; i<(data.length % 16); i++){
+ packet[i] = data[k];
+ }
+
+ int[] outbuffer = new int[frameSize*4]; // FrameSize * 4
+ int outputsize = 0;
+ AlacDecodeUtils.decode_frame(alac, packet, outbuffer, outputsize);
+
+ return outbuffer;
+ }
+
+
+ public byte[] encryptAES(byte[] array){
+ try{
+ SecretKeySpec k = new SecretKeySpec(aeskey, "AES");
+ Cipher c = Cipher.getInstance("AES/CBC/NoPadding");
+ c.init(Cipher.DECRYPT_MODE, k, new IvParameterSpec(aesiv));
+ return c.doFinal(array);
+
+ }catch(Exception e){
+ e.printStackTrace();
+ }
+
return null;
}
}
View
1 src/UDPListener.java
@@ -16,6 +16,7 @@
UDPDelegate delegate;
public UDPListener(DatagramSocket socket, UDPDelegate delegate){
+ super();
this.socket = socket;
this.delegate = delegate;
this.start();

0 comments on commit fb45f92

Please sign in to comment.