break;
default:
obj.put("state", "idle");
break;
}
return new Response(HttpServer.HTTP_OK, "text/plain", obj.toJSONString());
} else {
// Nothing is playing
JSONObject obj = new JSONObject();
obj.put("time", 0);
obj.put("duration", 0);
obj.put("state", "stopped");
return new Response(HttpServer.HTTP_OK, "text/plain", obj.toJSONString());
}
}
} else if (uri.startsWith(prefixes[1])) { // devices
// https://code.google.com/p/json-simple/wiki/EncodingExamples
JSONArray list = new JSONArray();
TrackedDialServers trackedDialServers = deviceFinder.getTrackedDialServers();
for (DialServer dialServer : trackedDialServers) {
JSONObject obj = new JSONObject();
obj.put("name", dialServer.getFriendlyName());
obj.put("ip_address", dialServer.getIpAddress().getHostAddress());
list.add(obj);
}
return new Response(HttpServer.HTTP_OK, "text/plain", list.toJSONString());
}
} else if (method.equals("POST")) {
if (uri.startsWith(prefixes[0])) { // playback
String device = parms.getProperty("device");
if (device != null) {
String stream = parms.getProperty("stream");
String file = parms.getProperty("file");
String state = parms.getProperty("state");
String transcode = parms.getProperty("transcode");
String transcodeParameters = parms.getProperty("transcode-parameters");
Log.d(LOG_TAG, "transcodeParameters="+transcodeParameters);
if (stream != null) {
try {
if (playbackMap.get(device) == null) {
DialServer dialServer = deviceFinder.getTrackedDialServers().findDialServer(InetAddress.getByName(device));
if (dialServer != null) {
RestPlaybackListener playbackListener = new RestPlaybackListener(device);
playbackMap.put(device, new Playback(platform, appId, dialServer, playbackListener));
playbackListenerMap.put(device, playbackListener);
}
}
Playback playback = playbackMap.get(device);
if (playback != null) {
playback.stream(stream);
return new Response(HttpServer.HTTP_OK, "text/plain", "Ok");
}
} catch (Exception e1) {
Log.e(LOG_TAG, "playback", e1);
}
} else if (file != null) {
try {
if (playbackMap.get(device) == null) {
DialServer dialServer = deviceFinder.getTrackedDialServers().findDialServer(InetAddress.getByName(device));
if (dialServer != null) {
RestPlaybackListener playbackListener = new RestPlaybackListener(device);
playbackMap.put(device, new Playback(platform, appId, dialServer, playbackListener));
playbackListenerMap.put(device, playbackListener);
}
}
Playback playback = playbackMap.get(device);
if (transcodeParameters!=null) {
playback.setTranscodingParameters(transcodeParameters);
}
if (playback != null) {
playback.play(file, transcode!=null);
return new Response(HttpServer.HTTP_OK, "text/plain", "Ok");
}
} catch (Exception e1) {
Log.e(LOG_TAG, "playback", e1);
}
} else if (state != null) {
try {
if (playbackMap.get(device) == null) {
DialServer dialServer = deviceFinder.getTrackedDialServers().findDialServer(InetAddress.getByName(device));
if (dialServer != null) {
RestPlaybackListener playbackListener = new RestPlaybackListener(device);
playbackMap.put(device, new Playback(platform, appId, dialServer, playbackListener));
playbackListenerMap.put(device, playbackListener);
}
}
Playback playback = playbackMap.get(device);
if (playback != null) {
// Handle case where current app wasn't started with caster
playback.setDialServer(deviceFinder.getTrackedDialServers().findDialServer(InetAddress.getByName(device)));
// Change the playback state
if (state.equals("play")) {
playback.doPlay();
return new Response(HttpServer.HTTP_OK, "text/plain", "Ok");
} else if (state.equals("pause")) {
playback.doPause();
return new Response(HttpServer.HTTP_OK, "text/plain", "Ok");
} else if (state.equals("stop")) {
playback.doStop();
playbackMap.remove(device);
playbackListenerMap.remove(device);
return new Response(HttpServer.HTTP_OK, "text/plain", "Ok");
} else {
Log.e(LOG_TAG, "playback invalid state: "+state);
}
}
} catch (Exception e1) {
Log.e(LOG_TAG, "playback", e1);
}
}
}
}
}
return new Response(HttpServer.HTTP_BADREQUEST, "text/plain", "Bad Request");
}
@Override
public String[] uriPrefixes() {
return prefixes;