implement basic oop for mpv ipc
This commit is contained in:
parent
b2626441fc
commit
c91070ae15
6 changed files with 77 additions and 24 deletions
|
|
@ -31,8 +31,7 @@ public class MPV implements TaskProcess {
|
||||||
socket = new MPVSocket();
|
socket = new MPVSocket();
|
||||||
socket.waitForConnection(MPV_SOCKET_PATH);
|
socket.waitForConnection(MPV_SOCKET_PATH);
|
||||||
|
|
||||||
// TODO: remove test code
|
socket.writeCommand(new MPVSetProperty(MPVProperty.VOLUME, 0));
|
||||||
socket.writeCommandRaw(new MPVCommandRaw("set_property", "volume", "0"));
|
|
||||||
|
|
||||||
return isAlive();
|
return isAlive();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
27
src/main/java/com/mykola2312/mptv/mpv/MPVCommand.java
Normal file
27
src/main/java/com/mykola2312/mptv/mpv/MPVCommand.java
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
package com.mykola2312.mptv.mpv;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
|
public abstract class MPVCommand {
|
||||||
|
private int requestId;
|
||||||
|
|
||||||
|
public void setRequestId(int requestId) {
|
||||||
|
this.requestId = requestId;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract List<String> serializeCommand();
|
||||||
|
|
||||||
|
public byte[] serialize() throws JsonProcessingException {
|
||||||
|
MPVCommandRaw command = new MPVCommandRaw();
|
||||||
|
command.request_id = requestId;
|
||||||
|
command.command = serializeCommand();
|
||||||
|
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
String jsonCommand = mapper.writeValueAsString(command) + "\n";
|
||||||
|
return jsonCommand.getBytes(StandardCharsets.UTF_8);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,24 +1,8 @@
|
||||||
package com.mykola2312.mptv.mpv;
|
package com.mykola2312.mptv.mpv;
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.util.List;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
|
|
||||||
public class MPVCommandRaw {
|
public class MPVCommandRaw {
|
||||||
public ArrayList<String> command;
|
public int request_id;
|
||||||
|
public List<String> command;
|
||||||
public MPVCommandRaw(String name, String... args) {
|
|
||||||
command = new ArrayList<>();
|
|
||||||
command.add(name);
|
|
||||||
command.addAll(Arrays.asList(args));
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte[] serialize() throws JsonProcessingException {
|
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
|
||||||
String jsonCommand = mapper.writeValueAsString(this);
|
|
||||||
return jsonCommand.getBytes(StandardCharsets.UTF_8);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
15
src/main/java/com/mykola2312/mptv/mpv/MPVProperty.java
Normal file
15
src/main/java/com/mykola2312/mptv/mpv/MPVProperty.java
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
package com.mykola2312.mptv.mpv;
|
||||||
|
|
||||||
|
public enum MPVProperty {
|
||||||
|
VOLUME ("volume");
|
||||||
|
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
private MPVProperty(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return this.name;
|
||||||
|
}
|
||||||
|
}
|
||||||
29
src/main/java/com/mykola2312/mptv/mpv/MPVSetProperty.java
Normal file
29
src/main/java/com/mykola2312/mptv/mpv/MPVSetProperty.java
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
package com.mykola2312.mptv.mpv;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class MPVSetProperty extends MPVCommand {
|
||||||
|
private final MPVProperty property;
|
||||||
|
private final String value;
|
||||||
|
|
||||||
|
public MPVSetProperty(MPVProperty property, String value) {
|
||||||
|
this.property = property;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MPVSetProperty(MPVProperty property, int value) {
|
||||||
|
this.property = property;
|
||||||
|
this.value = String.valueOf(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public MPVSetProperty(MPVProperty property, float value) {
|
||||||
|
this.property = property;
|
||||||
|
this.value = String.valueOf(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected List<String> serializeCommand() {
|
||||||
|
return Arrays.asList("set_property", property.toString(), value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -46,12 +46,11 @@ public class MPVSocket {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void writeCommandRaw(MPVCommandRaw command) {
|
public void writeCommand(MPVCommand command) {
|
||||||
try {
|
try {
|
||||||
AFOutputStream output = socket.getOutputStream();
|
AFOutputStream output = socket.getOutputStream();
|
||||||
|
|
||||||
output.write(command.serialize());
|
output.write(command.serialize());
|
||||||
output.write('\n');
|
|
||||||
output.flush();
|
output.flush();
|
||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
logger.error("failed to serialize command", e);
|
logger.error("failed to serialize command", e);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue