diff --git a/src/com/limelight/Game.java b/src/com/limelight/Game.java index 412a6bb7..48c1c413 100644 --- a/src/com/limelight/Game.java +++ b/src/com/limelight/Game.java @@ -45,63 +45,48 @@ public class Game extends Activity { @Override public boolean onKeyDown(int keyCode, KeyEvent event) { -// int player = OuyaController.getPlayerNumByDeviceId(event.getDeviceId()); switch (keyCode) { case OuyaController.BUTTON_MENU: - System.out.println("Pressed Menu Button"); - inputMap |= NvInputPacket.PLAY_FLAG; + inputMap |= NvInputPacket.BACK_FLAG; break; case OuyaController.BUTTON_DPAD_LEFT: - System.out.println("Pressed Dpad Left"); inputMap |= NvInputPacket.LEFT_FLAG; break; case OuyaController.BUTTON_DPAD_RIGHT: - System.out.println("Pressed Dpad Right"); inputMap |= NvInputPacket.RIGHT_FLAG; break; case OuyaController.BUTTON_DPAD_UP: - System.out.println("Pressed Dpad Up"); inputMap |= NvInputPacket.UP_FLAG; break; case OuyaController.BUTTON_DPAD_DOWN: - System.out.println("Pressed Dpad Down"); inputMap |= NvInputPacket.DOWN_FLAG; break; - case OuyaController.BUTTON_A: - System.out.println("Pressed A"); + case OuyaController.BUTTON_A: inputMap |= NvInputPacket.B_FLAG; break; case OuyaController.BUTTON_O: - System.out.println("Pressed O"); inputMap |= NvInputPacket.A_FLAG; break; case OuyaController.BUTTON_U: - System.out.println("Pressed U"); inputMap |= NvInputPacket.X_FLAG; break; case OuyaController.BUTTON_Y: - System.out.println("Pressed Y"); inputMap |= NvInputPacket.Y_FLAG; break; case OuyaController.BUTTON_L1: - System.out.println("Pressed L1"); inputMap |= NvInputPacket.LB_FLAG; break; case OuyaController.BUTTON_R1: - System.out.println("Pressed R1"); inputMap |= NvInputPacket.RB_FLAG; break; case OuyaController.BUTTON_L3: - System.out.println("Pressed L3"); inputMap |= NvInputPacket.LS_CLK_FLAG; break; case OuyaController.BUTTON_R3: - System.out.println("Pressed R3"); inputMap |= NvInputPacket.RS_CLK_FLAG; break; default: - System.out.println("Pressed some button: " + keyCode); return super.onKeyDown(keyCode, event); } sendInputPacket(); @@ -112,59 +97,45 @@ public class Game extends Activity { public boolean onKeyUp(int keyCode, KeyEvent event) { switch (keyCode) { case OuyaController.BUTTON_MENU: - System.out.println("Released Menu Button"); - inputMap &= ~NvInputPacket.PLAY_FLAG; + inputMap &= ~NvInputPacket.BACK_FLAG; break; case OuyaController.BUTTON_DPAD_LEFT: - System.out.println("Released Dpad Left"); inputMap &= ~NvInputPacket.LEFT_FLAG; break; case OuyaController.BUTTON_DPAD_RIGHT: - System.out.println("Released Dpad Right"); inputMap &= ~NvInputPacket.RIGHT_FLAG; break; case OuyaController.BUTTON_DPAD_UP: - System.out.println("Released Dpad Up"); inputMap &= ~NvInputPacket.UP_FLAG; break; case OuyaController.BUTTON_DPAD_DOWN: - System.out.println("Released Dpad Down"); inputMap &= ~NvInputPacket.DOWN_FLAG; break; - case OuyaController.BUTTON_A: - System.out.println("Released A"); + case OuyaController.BUTTON_A: inputMap &= ~NvInputPacket.B_FLAG; break; case OuyaController.BUTTON_O: - System.out.println("Released O"); inputMap &= ~NvInputPacket.A_FLAG; break; case OuyaController.BUTTON_U: - System.out.println("Released U"); inputMap &= ~NvInputPacket.X_FLAG; break; case OuyaController.BUTTON_Y: - System.out.println("Released Y"); inputMap &= ~NvInputPacket.Y_FLAG; break; case OuyaController.BUTTON_L1: - System.out.println("Released L1"); inputMap &= ~NvInputPacket.LB_FLAG; break; case OuyaController.BUTTON_R1: - System.out.println("Released R1"); inputMap &= ~NvInputPacket.RB_FLAG; break; case OuyaController.BUTTON_L3: - System.out.println("Released L3"); inputMap &= ~NvInputPacket.LS_CLK_FLAG; break; case OuyaController.BUTTON_R3: - System.out.println("Released R3"); inputMap &= ~NvInputPacket.RS_CLK_FLAG; break; default: - System.out.println("Released some button: " + keyCode); return super.onKeyUp(keyCode, event); } sendInputPacket(); @@ -188,25 +159,16 @@ public class Game extends Activity { RS_X = RS_Y = 0.0f; } - System.out.println("LS_X: " + LS_X + "\t" + - "LS_Y: " + LS_Y + "\t" + - "RS_X: " + RS_X + "\t" + - "RS_Y: " + RS_Y + "\t"); - leftStickX = (short)Math.round(LS_X * 0x7FFF); leftStickY = (short)Math.round(-LS_Y * 0x7FFF); rightStickX = (short)Math.round(RS_X * 0x7FFF); rightStickY = (short)Math.round(-RS_Y * 0x7FFF); - - System.out.printf("(0x%x 0x%x) (0x%x 0x%x)\n", leftStickX, leftStickY, rightStickX, rightStickY); - } + } float L2 = event.getAxisValue(OuyaController.AXIS_L2); float R2 = event.getAxisValue(OuyaController.AXIS_R2); - System.out.println("L2: " + L2 + "\t" + " R2: " + R2 + "\t"); - leftTrigger = (byte)Math.round(L2 * 0xFF); rightTrigger = (byte)Math.round(R2 * 0xFF); diff --git a/src/com/limelight/nvstream/NvAudioStream.java b/src/com/limelight/nvstream/NvAudioStream.java index 15bcee0f..0701bd4d 100644 --- a/src/com/limelight/nvstream/NvAudioStream.java +++ b/src/com/limelight/nvstream/NvAudioStream.java @@ -3,6 +3,7 @@ package com.limelight.nvstream; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; +import java.net.InetSocketAddress; import java.net.SocketException; import android.net.rtp.AudioGroup; import android.net.rtp.AudioStream; @@ -32,13 +33,13 @@ public class NvAudioStream { System.out.println("Joined"); }*/ - public void start() + public void start(final String host) { new Thread(new Runnable() { @Override public void run() { - DatagramSocket ds; + final DatagramSocket ds; try { ds = new DatagramSocket(PORT); } catch (SocketException e1) { @@ -46,6 +47,31 @@ public class NvAudioStream { e1.printStackTrace(); return; } + + new Thread(new Runnable() { + @Override + public void run() { + byte[] ping = new byte[]{0x50, 0x49, 0x4e, 0x47}; + for (;;) + { + DatagramPacket dgp = new DatagramPacket(ping, 0, ping.length); + dgp.setSocketAddress(new InetSocketAddress(host, PORT)); + try { + ds.send(dgp); + } catch (IOException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + break; + } + + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + break; + } + } + } + }).start(); for (;;) { diff --git a/src/com/limelight/nvstream/NvConnection.java b/src/com/limelight/nvstream/NvConnection.java index 7a07e562..b27fe3b7 100644 --- a/src/com/limelight/nvstream/NvConnection.java +++ b/src/com/limelight/nvstream/NvConnection.java @@ -49,6 +49,12 @@ public class NvConnection { performHandshake(); beginControlStream(); startController(); + + //new NvAudioStream().start(); + new NvVideoStream().start(host); + + controlStream.startJitterPackets(); + } catch (XmlPullParserException e) { e.printStackTrace(); displayToast(e.getMessage()); @@ -131,10 +137,4 @@ public class NvConnection { System.out.println("Starting input"); inputStream = new NvController(host); } - - public void doShit() throws XmlPullParserException, IOException - { - new NvAudioStream().start(); - new NvVideoStream().start(host); - } } diff --git a/src/com/limelight/nvstream/NvControl.java b/src/com/limelight/nvstream/NvControl.java index 87ec3e73..e506700a 100644 --- a/src/com/limelight/nvstream/NvControl.java +++ b/src/com/limelight/nvstream/NvControl.java @@ -12,9 +12,9 @@ public class NvControl { public static final int PORT = 47995; - public static final short PTYPE_1 = 0x1204; - public static final short PPAYLEN_1 = 0x0004; - public static final byte[] PPAYLOAD_1 = + public static final short PTYPE_HELLO = 0x1204; + public static final short PPAYLEN_HELLO = 0x0004; + public static final byte[] PPAYLOAD_HELLO = { (byte)0x00, (byte)0x05, @@ -97,7 +97,7 @@ public class NvControl { 402653184, 134218770, 419430400, - 167773203, + 167773202, 436207616, 855638290, 266779, @@ -150,12 +150,12 @@ public class NvControl { 34746368, (int)0xFE000000 }; + - public static final short PTYPE_HELLO = 0x1204; - public static final short PPAYLEN_HELLO = 0x0004; - public static final byte[] PPAYLOAD_HELLO = new byte[] { - 0x04, 0x00, 0x00, 0x05 - }; + public static final short PTYPE_JITTER = 0x140c; + public static final short PPAYLEN_JITTER = 0x10; + + private int seqNum; private Socket s; private InputStream in; @@ -173,12 +173,25 @@ public class NvControl { out.write(packet.toWire()); out.flush(); } + public NvControl.NvCtlResponse sendAndGetReply(NvCtlPacket packet) throws IOException { sendPacket(packet); return new NvCtlResponse(in); } + private void sendJitter() throws IOException + { + ByteBuffer bb = ByteBuffer.allocate(16).order(ByteOrder.LITTLE_ENDIAN); + + bb.putInt(0); + bb.putInt(77); + bb.putInt(888); + bb.putInt(seqNum += 2); + + sendPacket(new NvCtlPacket(PTYPE_JITTER, PPAYLEN_JITTER, bb.array())); + } + public void beginControl() throws IOException { System.out.println("CTL: Sending hello"); @@ -215,6 +228,30 @@ public class NvControl { }).start(); } + public void startJitterPackets() + { + new Thread(new Runnable() { + @Override + public void run() { + for (;;) + { + try { + sendJitter(); + } catch (IOException e1) { + e1.printStackTrace(); + break; + } + + try { + Thread.sleep(100); + } catch (InterruptedException e) { + break; + } + } + } + }).start(); + } + public void endControl() throws IOException { s.close(); diff --git a/src/com/limelight/nvstream/NvVideoStream.java b/src/com/limelight/nvstream/NvVideoStream.java index 4a2ee3b4..f081004b 100644 --- a/src/com/limelight/nvstream/NvVideoStream.java +++ b/src/com/limelight/nvstream/NvVideoStream.java @@ -4,6 +4,7 @@ import java.io.IOException; import java.io.InputStream; import java.net.DatagramPacket; import java.net.DatagramSocket; +import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketException; import java.net.UnknownHostException; @@ -27,10 +28,18 @@ public class NvVideoStream { try { System.out.println("VID: Waiting for first frame"); InputStream firstFrameStream = getFirstFrame(host); - firstFrameStream.read(); - System.out.println("VID: First frame: "+firstFrameStream.available()+1); - firstFrameStream.close(); - System.out.println("VID: Got first frame"); + + System.out.println(firstFrameStream.available()); + int i; + for (i = 0; i < 98; i++) + { + if (firstFrameStream.read() == -1) + { + System.out.println("EOF on FF"); + break; + } + } + System.out.println("VID: First frame read "+i); } catch (UnknownHostException e2) { // TODO Auto-generated catch block e2.printStackTrace(); @@ -41,7 +50,7 @@ public class NvVideoStream { return; } - DatagramSocket ds; + final DatagramSocket ds; try { ds = new DatagramSocket(PORT); } catch (SocketException e1) { @@ -49,6 +58,32 @@ public class NvVideoStream { e1.printStackTrace(); return; } + + // Ping thread + /*new Thread(new Runnable() { + @Override + public void run() { + byte[] ping = new byte[]{0x50, 0x49, 0x4e, 0x47}; + for (;;) + { + DatagramPacket dgp = new DatagramPacket(ping, 0, ping.length); + dgp.setSocketAddress(new InetSocketAddress(host, PORT)); + try { + ds.send(dgp); + } catch (IOException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + break; + } + + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + break; + } + } + } + }).start();*/ for (;;) {