Skip to content

Commit fc6fef5

Browse files
committed
Update version android (+3 squashed commits)
Change NetUtil to a non static implementation. The audio and video buffers are now held in their own NetUtil instances in their own thread. The audio can die while video keeps going. It's useful if the server is not sending audio but it needs proper disconnect handling instead of just stopping. Remove audio buffer from desktop audio Data source line has its own buffer Add microphone input (random) button
1 parent 85ffc36 commit fc6fef5

File tree

16 files changed

+62
-74
lines changed

16 files changed

+62
-74
lines changed

android/AndroidManifest.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.rolandoislas.drcsimclient"
44
android:versionCode="2"
5-
android:versionName="1.1" >
5+
android:versionName="1.1.1" >
66

77
<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="25" />
88

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ allprojects {
1515
apply plugin: "eclipse"
1616
apply plugin: "idea"
1717

18-
version = '1.1'
18+
version = '1.1.1'
1919
ext {
2020
appName = "drc-sim-client"
2121
gdxVersion = '1.9.4'

core/src/com/rolandoislas/drcsimclient/audio/AudioThread.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@
1010
*/
1111
public class AudioThread extends Thread {
1212
private final AudioUtil audioUtil;
13+
private final NetUtil netUtil;
1314
private boolean running = true;
1415

1516
public AudioThread() {
1617
this.audioUtil = new AudioUtil();
1718
this.setName("Network Thread: Audio");
19+
netUtil = new NetUtil();
1820
}
1921

2022
@Override
2123
public void run() {
2224
while (running) {
2325
try {
24-
byte[] packet = NetUtil.recv(sockets.socketAud, "audio");
26+
byte[] packet = netUtil.recv(sockets.socketAud);
2527
audioUtil.addData(packet);
2628
} catch (NetUtil.ReadTimeoutException ignore) {
2729
try {
@@ -40,4 +42,8 @@ public void dispose() {
4042
running = false;
4143
audioUtil.dispose();
4244
}
45+
46+
public void resetTimeout() {
47+
netUtil.resetTimeout();
48+
}
4349
}

core/src/com/rolandoislas/drcsimclient/config/Config.java

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class Config {
2525
public static final String JOYSTICK_LEFT_Y = "JOYSTICK_LEFT_Y";
2626
public static final String JOYSTICK_RIGHT_X = "JOYSTICK_RIGHT_X";
2727
public static final String JOYSTICK_RIGHT_Y = "JOYSTICK_RIGHT_Y";
28+
public static final String MIC_BLOW = "MIC_BLOW";
2829
public int buttonA;
2930
public int buttonB;
3031
public int buttonX;
@@ -42,6 +43,7 @@ public class Config {
4243
public int buttonMinus;
4344
public int buttonPlus;
4445
public int buttonHome;
46+
public int micBlow;
4547

4648
public void set(String item, int input) {}
4749

core/src/com/rolandoislas/drcsimclient/config/ConfigControllerConfig.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class ConfigControllerConfig extends Config {
1111
public int joystickRightY;
1212

1313
ConfigControllerConfig(String name) {
14-
config = Gdx.app.getPreferences("com.rolandoislas.controller." + name.replaceAll(" ", ""));
14+
config = Gdx.app.getPreferences("com.rolandoislas.drcsimclient.controller." + name.replaceAll(" ", ""));
1515
}
1616

1717
public void load() {
@@ -36,6 +36,7 @@ public void load() {
3636
joystickLeftY = config.getInteger(JOYSTICK_LEFT_Y, -1);
3737
joystickRightX = config.getInteger(JOYSTICK_RIGHT_X, -1);
3838
joystickRightY = config.getInteger(JOYSTICK_RIGHT_Y, -1);
39+
micBlow = config.getInteger(MIC_BLOW, -1);
3940
config.flush();
4041
}
4142

core/src/com/rolandoislas/drcsimclient/config/ConfigGeneral.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class ConfigGeneral extends Config {
1212
public int touchScreen;
1313

1414
public ConfigGeneral() {
15-
config = Gdx.app.getPreferences("com.rolandoislas.drcsimclient.config.touch");
15+
config = Gdx.app.getPreferences("com.rolandoislas.drcsimclient.config.general");
1616
}
1717

1818
@Override

core/src/com/rolandoislas/drcsimclient/config/ConfigKeyboard.java

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public void load() {
3232
buttonMinus = config.getInteger(BUTTON_MINUS, Input.Keys.X);
3333
buttonPlus = config.getInteger(BUTTON_PLUS, Input.Keys.C);
3434
buttonHome = config.getInteger(BUTTON_HOME, Input.Keys.Z);
35+
micBlow = config.getInteger(MIC_BLOW, Input.Keys.B);
3536
config.putInteger(BUTTON_A, buttonA);
3637
config.putInteger(BUTTON_B, buttonB);
3738
config.putInteger(BUTTON_X, buttonX);
@@ -49,6 +50,7 @@ public void load() {
4950
config.putInteger(BUTTON_MINUS, buttonMinus);
5051
config.putInteger(BUTTON_PLUS, buttonPlus);
5152
config.putInteger(BUTTON_HOME, buttonHome);
53+
config.putInteger(MIC_BLOW, micBlow);
5254
config.flush();
5355
}
5456

core/src/com/rolandoislas/drcsimclient/control/ControlController.java

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.rolandoislas.drcsimclient.config.ConfigController;
77
import com.rolandoislas.drcsimclient.config.ConfigControllerConfig;
88
import com.rolandoislas.drcsimclient.data.Constants;
9+
import com.rolandoislas.drcsimclient.net.Codec;
910
import com.rolandoislas.drcsimclient.stage.StageControl;
1011

1112
import static com.rolandoislas.drcsimclient.Client.sockets;
@@ -62,6 +63,9 @@ public void update() {
6263
buttonBits |= Constants.BUTTON_PLUS;
6364
if (controller.getButton(config.buttonHome))
6465
buttonBits |= Constants.BUTTON_HOME;
66+
// Microphone
67+
if (controller.getButton(config.micBlow))
68+
sockets.sendCommand(Constants.COMMAND_INPUT_MIC_BLOW, Codec.encodeInput(true));
6569
// Check joystick
6670
axes[0] = controller.getAxis(config.joystickLeftX);
6771
axes[1] = controller.getAxis(config.joystickLeftY);

core/src/com/rolandoislas/drcsimclient/control/ControlKeyboard.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.badlogic.gdx.Gdx;
44
import com.rolandoislas.drcsimclient.config.ConfigKeyboard;
55
import com.rolandoislas.drcsimclient.data.Constants;
6+
import com.rolandoislas.drcsimclient.net.Codec;
67
import com.rolandoislas.drcsimclient.stage.StageControl;
78

89
import static com.rolandoislas.drcsimclient.Client.sockets;
@@ -54,13 +55,16 @@ public void update() {
5455
if (Gdx.input.isKeyPressed(config.buttonHome))
5556
buttonbits |= Constants.BUTTON_HOME;
5657
sockets.sendButtonInput(buttonbits);
57-
// L3R3
58+
// Extra
5859
int extraButtonBits = 0;
5960
if (Gdx.input.isKeyPressed(config.buttonL3))
6061
extraButtonBits |= Constants.BUTTON_L3;
6162
if (Gdx.input.isKeyPressed(config.buttonR3))
6263
extraButtonBits |= Constants.BUTTON_R3;
6364
sockets.sendExtraButtonInput(extraButtonBits);
65+
// Mic
66+
if (Gdx.input.isKeyPressed(config.micBlow))
67+
sockets.sendCommand(Constants.COMMAND_INPUT_MIC_BLOW, Codec.encodeInput(true));
6468
// Joystick
6569
// TODO get joystick input based on mouse capture
6670
}

core/src/com/rolandoislas/drcsimclient/data/Constants.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
public class Constants {
99
// Info
1010
public static final String NAME = "DRC Sim Client";
11-
public static final String VERSION = "1.1.1";
11+
public static final String VERSION = "1.2";
1212
// Ports
1313
public static final int PORT_SERVER_VID = 50000;
1414
public static final int PORT_SERVER_AUD = 50001;
@@ -19,6 +19,7 @@ public class Constants {
1919
public static final String COMMAND_INPUT_BUTTON_EXTRA = "INPUT_L3R3"; // TODO rename on server
2020
public static final String COMMAND_INPUT_TOUCH = "INPUT_TOUCH";
2121
public static final String COMMAND_INPUT_JOYSTICK = "INPUT_JOYSTICK";
22+
public static final String COMMAND_INPUT_MIC_BLOW = "INPUT_MIC_BLOW";
2223
public static final String COMMAND_VIBRATE = "VIBRATE";
2324
public static final String COMMAND_PING = "PING";
2425
public static final String COMMAND_PONG = "PONG";

core/src/com/rolandoislas/drcsimclient/graphics/VideoThread.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
package com.rolandoislas.drcsimclient.graphics;
22

33
import com.rolandoislas.drcsimclient.net.NetUtil;
4-
import com.rolandoislas.drcsimclient.stage.StageConnect;
54
import com.rolandoislas.drcsimclient.util.logging.Logger;
65

7-
import static com.rolandoislas.drcsimclient.Client.setStage;
86
import static com.rolandoislas.drcsimclient.Client.sockets;
97

108
/**
119
* Created by rolando on 4/6/17.
1210
*/
1311
public class VideoThread extends Thread {
12+
private final NetUtil netUtil;
1413
private boolean running = true;
1514
private byte[] imageData = new byte[0];
1615

1716
public VideoThread() {
1817
this.setName("Network Thread: Video");
18+
netUtil = new NetUtil();
1919
}
2020

2121
@Override
2222
public void run() {
2323
while (running) {
2424
try {
25-
byte[] data = NetUtil.recv(sockets.socketVid, "video");
25+
byte[] data = netUtil.recv(sockets.socketVid);
2626
synchronized (this) {
2727
imageData = data;
2828
}

core/src/com/rolandoislas/drcsimclient/net/NetUtil.java

+22-26
Original file line numberDiff line numberDiff line change
@@ -17,50 +17,46 @@
1717
* Created by Rolando on 12/21/2016.
1818
*/
1919
public class NetUtil {
20-
private static HashMap<String, byte[]> buffers = new HashMap<String, byte[]>();
21-
private static HashMap<String, Long> timestamps = new HashMap<String, Long>();
22-
private static boolean pingSent = false;
20+
private byte[] buffer = new byte[0];
21+
private long timestamp = 0;
22+
private boolean pingSent = false;
2323

24-
public static byte[] recv(Socket socket, String bufferId) throws DisconnectedException, ReadTimeoutException {
24+
public byte[] recv(Socket socket) throws DisconnectedException, ReadTimeoutException {
2525
try {
2626
BufferedInputStream inStream = new BufferedInputStream(socket.getInputStream());
27-
if (!buffers.containsKey(bufferId))
28-
buffers.put(bufferId, new byte[0]);
29-
if (!timestamps.containsKey(bufferId))
30-
timestamps.put(bufferId, System.currentTimeMillis());
31-
while (!new String(buffers.get(bufferId)).contains(Codec.endDelimiter)) {
27+
if (timestamp == 0)
28+
timestamp = System.currentTimeMillis();
29+
while (!new String(buffer).contains(Codec.endDelimiter)) {
3230
// Disconnected
33-
long time = System.currentTimeMillis() - timestamps.get(bufferId);
34-
if (time >= 10000 && !Client.connect(sockets.getIp(), false)) {
31+
long time = System.currentTimeMillis() - timestamp;
32+
if (time >= 10000) {
3533
Logger.debug("Disconnected from server");
3634
clear();
3735
throw new DisconnectedException();
3836
}
3937
if (time >= 5000 && !pingSent) {
4038
Logger.debug("Sending PING command to server");
4139
sockets.sendCommand(Constants.COMMAND_PING);
42-
Client.connect(sockets.getIp(), false);
4340
pingSent = true;
4441
}
4542
// Timeout
4643
if (inStream.available() < 2)
4744
throw new ReadTimeoutException();
48-
timestamps.put(bufferId, System.currentTimeMillis());
45+
timestamp = System.currentTimeMillis();
4946
// Read
5047
byte[] read = new byte[100000];
5148
int numRead = inStream.read(read);
5249
read = Arrays.copyOfRange(read, 0, numRead);
5350
// Combine saved and new bytes
54-
byte[] newBytes = new byte[buffers.get(bufferId).length + read.length];
55-
System.arraycopy(buffers.get(bufferId), 0, newBytes, 0, buffers.get(bufferId).length);
56-
System.arraycopy(read, 0, newBytes, buffers.get(bufferId).length, read.length);
51+
byte[] newBytes = new byte[buffer.length + read.length];
52+
System.arraycopy(buffer, 0, newBytes, 0, buffer.length);
53+
System.arraycopy(read, 0, newBytes, buffer.length, read.length);
5754
// Save
58-
buffers.put(bufferId, newBytes);
55+
buffer = newBytes;
5956
}
60-
int index = Bytes.indexOf(buffers.get(bufferId), Codec.endDelimiter.getBytes());
61-
byte[] packet = Arrays.copyOfRange(buffers.get(bufferId), 0, index);
62-
buffers.put(bufferId, Arrays.copyOfRange(buffers.get(bufferId), index + Codec.endDelimiter.length(),
63-
buffers.get(bufferId).length));
57+
int index = Bytes.indexOf(buffer, Codec.endDelimiter.getBytes());
58+
byte[] packet = Arrays.copyOfRange(buffer, 0, index);
59+
buffer = Arrays.copyOfRange(buffer, index + Codec.endDelimiter.length(), buffer.length);
6460
return Codec.decode(packet);
6561
}
6662
catch (IOException e) {
@@ -70,14 +66,14 @@ public static byte[] recv(Socket socket, String bufferId) throws DisconnectedExc
7066
}
7167
}
7268

73-
static void clear() {
74-
timestamps.clear();
75-
buffers.clear();
69+
private void clear() {
70+
timestamp = 0;
71+
buffer = new byte[0];
7672
pingSent = false;
7773
}
7874

79-
public static void resetTimeout() {
80-
timestamps.clear();
75+
public void resetTimeout() {
76+
timestamp = 0;
8177
pingSent = false;
8278
}
8379

core/src/com/rolandoislas/drcsimclient/net/Sockets.java

-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ public void dispose() {
118118
e.printStackTrace();
119119
}
120120
}
121-
NetUtil.clear();
122121
}
123122

124123
public String getIp() {

core/src/com/rolandoislas/drcsimclient/stage/StageConfigController.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public class StageConfigController extends StageList {
3737
new String[]{"R3", ConfigControllerConfig.BUTTON_R3},
3838
new String[]{"Minus", ConfigControllerConfig.BUTTON_MINUS},
3939
new String[]{"Plus", ConfigControllerConfig.BUTTON_PLUS},
40-
new String[]{"Home", ConfigControllerConfig.BUTTON_HOME}
40+
new String[]{"Home", ConfigControllerConfig.BUTTON_HOME},
41+
new String[]{"Mic Blow", ConfigControllerConfig.MIC_BLOW}
4142
};
4243

4344
public StageConfigController(boolean enableDropdown) {

core/src/com/rolandoislas/drcsimclient/stage/StageControl.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,10 @@ public void act() {
8888
private void checkNetworkCommands() {
8989
CommandThread.Command command = commandThread.getCommand();
9090
// Handle command
91-
if (command.isCommand(Constants.COMMAND_PONG))
92-
NetUtil.resetTimeout();
91+
if (command.isCommand(Constants.COMMAND_PONG)) {
92+
audioThread.resetTimeout();
93+
audioThread.resetTimeout();
94+
}
9395
else if (command.isCommand(Constants.COMMAND_VIBRATE))
9496
for (Control control : controls)
9597
control.vibrate(1000);

desktop/src/com/rolandoislas/drcsimclient/desktop/audio/AudioDevice.java

+4-34
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@
99
*/
1010
public class AudioDevice implements com.rolandoislas.drcsimclient.audio.AudioDevice {
1111
private SourceDataLine line;
12-
private final byte[][] audioBuffer = new byte[15][832*2];
13-
private int audioPosRead = 0;
14-
private int getAudioPosWrite = 0;
15-
private long playTime = 0;
16-
private boolean running = true;
1712

1813
public AudioDevice() {
1914
AudioFormat af = new AudioFormat(48000, 16, 2, true, false);
@@ -31,32 +26,6 @@ public AudioDevice() {
3126
Logger.exception(e);
3227
Logger.warn("Audio format not supported by system.");
3328
}
34-
new Thread(new Runnable() {
35-
@Override
36-
public void run() {
37-
Logger.debug("Audio playback thread started");
38-
while (running)
39-
writeLoop();
40-
Logger.debug("Closing audio line");
41-
if (line != null && line.isOpen()) {
42-
line.drain();
43-
line.stop();
44-
line.close();
45-
}
46-
Logger.debug("Audio playback thread stopped");
47-
}
48-
}, "Audio Playback Thread").start();
49-
}
50-
51-
private void writeLoop() {
52-
if (System.currentTimeMillis() - playTime <= 15)
53-
return;
54-
playTime = System.currentTimeMillis();
55-
for (int samples = 0; samples < 2; samples++) {
56-
byte[] sample = audioBuffer[getAudioPosWrite++];
57-
write(sample, 0, sample.length);
58-
getAudioPosWrite %= audioBuffer.length;
59-
}
6029
}
6130

6231
@Override
@@ -66,13 +35,14 @@ public void setVolume(float volume) {
6635

6736
@Override
6837
public void dispose() {
69-
running = false;
38+
Logger.debug("Closing audio line");
39+
if (line != null && line.isOpen())
40+
line.close();
7041
}
7142

7243
@Override
7344
public void write(byte[] data) {
74-
audioBuffer[audioPosRead++] = data;
75-
audioPosRead %= audioBuffer.length;
45+
write(data, 0, data.length);
7646
}
7747

7848
public void write(byte[] data, int start, int stop) {

0 commit comments

Comments
 (0)