Skip to content

Commit 6a5c92d

Browse files
author
Chris Bellew
committed
Expire active connection for a Plex server an hour after it has been detected. This will fix the case where a server is defined while it is local, then accessed when it is remote (if more than an hour has elapsed, that is).
1 parent 4e7b337 commit 6a5c92d

File tree

10 files changed

+277
-295
lines changed

10 files changed

+277
-295
lines changed

mobile/src/main/java/com/atomjack/vcfp/CastPlayerManager.java

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import com.atomjack.shared.Logger;
77
import com.atomjack.shared.PlayerState;
88
import com.atomjack.shared.Preferences;
9+
import com.atomjack.vcfp.interfaces.ActiveConnectionHandler;
10+
import com.atomjack.vcfp.model.Connection;
911
import com.atomjack.vcfp.model.PlexClient;
1012
import com.atomjack.vcfp.model.PlexMedia;
1113
import com.atomjack.vcfp.model.PlexTrack;
@@ -154,10 +156,20 @@ public VideoCastManager getCastManager() {
154156
}
155157

156158
// This will send a message to the cast device to load the passed in media
157-
public void loadMedia(PlexMedia media, List<PlexMedia> album, int offset) {
159+
public void loadMedia(PlexMedia media, List<PlexMedia> album, final int offset) {
158160
nowPlayingMedia = media;
159161
nowPlayingAlbum = album;
160-
sendMessage(buildMedia(offset));
162+
nowPlayingMedia.server.findServerConnection(new ActiveConnectionHandler() {
163+
@Override
164+
public void onSuccess(Connection connection) {
165+
sendMessage(buildMedia(connection, offset));
166+
}
167+
168+
@Override
169+
public void onFailure(int statusCode) {
170+
171+
}
172+
});
161173
}
162174

163175
public void getPlaybackState() {
@@ -176,17 +188,26 @@ public void stop() {
176188
sendMessage(PARAMS.ACTION_STOP);
177189
}
178190

179-
public void seekTo(int seconds) {
180-
JSONObject obj = new JSONObject();
181-
try {
182-
obj.put(PARAMS.ACTION, PARAMS.ACTION_SEEK);
183-
obj.put(PARAMS.OFFSET, seconds);
184-
if(nowPlayingMedia instanceof PlexVideo)
185-
obj.put(PARAMS.SRC, getTranscodeUrl(nowPlayingMedia, seconds));
186-
obj.put(PARAMS.RESUME, VoiceControlForPlexApplication.getInstance().prefs.get(Preferences.RESUME, false));
187-
sendMessage(obj);
188-
} catch (Exception ex) {}
191+
public void seekTo(final int seconds) {
192+
nowPlayingMedia.server.findServerConnection(new ActiveConnectionHandler() {
193+
@Override
194+
public void onSuccess(Connection connection) {
195+
JSONObject obj = new JSONObject();
196+
try {
197+
obj.put(PARAMS.ACTION, PARAMS.ACTION_SEEK);
198+
obj.put(PARAMS.OFFSET, seconds);
199+
if(nowPlayingMedia instanceof PlexVideo)
200+
obj.put(PARAMS.SRC, getTranscodeUrl(nowPlayingMedia, connection, seconds));
201+
obj.put(PARAMS.RESUME, VoiceControlForPlexApplication.getInstance().prefs.get(Preferences.RESUME, false));
202+
sendMessage(obj);
203+
} catch (Exception ex) {}
204+
}
189205

206+
@Override
207+
public void onFailure(int statusCode) {
208+
209+
}
210+
});
190211
}
191212

192213
public void doNext() {
@@ -347,12 +368,12 @@ public void onCastDeviceDetected(final MediaRouter.RouteInfo info) {
347368
};
348369
}
349370

350-
public String getTranscodeUrl(PlexMedia media) {
351-
return getTranscodeUrl(media, 0);
371+
public String getTranscodeUrl(PlexMedia media, Connection connection) {
372+
return getTranscodeUrl(media, connection, 0);
352373
}
353374

354-
public String getTranscodeUrl(PlexMedia media, int offset) {
355-
String url = media.server.activeConnection.uri;
375+
public String getTranscodeUrl(PlexMedia media, Connection connection, int offset) {
376+
String url = connection.uri;
356377
url += String.format("/%s/:/transcode/universal/start?", media instanceof PlexVideo ? "video" : "audio");
357378
QueryString qs = new QueryString("path", String.format("http://127.0.0.1:32400%s", media.key));
358379
qs.add("mediaIndex", "0");
@@ -390,11 +411,11 @@ public void setTransientToken(String transientToken) {
390411
this.transientToken = transientToken;
391412
}
392413

393-
public JSONObject buildMedia() {
394-
return buildMedia(0);
414+
public JSONObject buildMedia(Connection connection) {
415+
return buildMedia(connection, 0);
395416
}
396417

397-
public JSONObject buildMedia(int offset) {
418+
public JSONObject buildMedia(Connection connection, int offset) {
398419
JSONObject data = new JSONObject();
399420
try {
400421
data.put(PARAMS.ACTION, PARAMS.ACTION_LOAD);
@@ -404,7 +425,7 @@ public JSONObject buildMedia(int offset) {
404425
data.put(PARAMS.MEDIA_TYPE, nowPlayingMedia instanceof PlexVideo ? PARAMS.MEDIA_TYPE_VIDEO : PARAMS.MEDIA_TYPE_AUDIO);
405426
data.put(PARAMS.RESUME, VoiceControlForPlexApplication.getInstance().prefs.get(Preferences.RESUME, false));
406427
data.put(PARAMS.CLIENT, VoiceControlForPlexApplication.gsonWrite.toJson(mClient));
407-
data.put(PARAMS.SRC, getTranscodeUrl(nowPlayingMedia, offset));
428+
data.put(PARAMS.SRC, getTranscodeUrl(nowPlayingMedia, connection, offset));
408429
data.put(PARAMS.PLAYLIST, getPlaylistJson());
409430
} catch (Exception ex) {
410431
ex.printStackTrace();

mobile/src/main/java/com/atomjack/vcfp/VoiceControlForPlexApplication.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@
3838
import com.atomjack.vcfp.activities.CastActivity;
3939
import com.atomjack.vcfp.activities.MainActivity;
4040
import com.atomjack.vcfp.activities.NowPlayingActivity;
41+
import com.atomjack.vcfp.interfaces.ActiveConnectionHandler;
4142
import com.atomjack.vcfp.interfaces.BitmapHandler;
42-
import com.atomjack.vcfp.interfaces.ServerFindHandler;
43+
import com.atomjack.vcfp.model.Connection;
4344
import com.atomjack.vcfp.model.MediaContainer;
4445
import com.atomjack.vcfp.model.PlexClient;
4546
import com.atomjack.vcfp.model.PlexMedia;
@@ -234,11 +235,11 @@ public static void addPlexServer(PlexServer addedServer, final Runnable onFinish
234235
}
235236
final PlexServer server = serverToAdd == null ? addedServer : serverToAdd;
236237
try {
237-
server.findServerConnection(new ServerFindHandler() {
238+
server.findServerConnection(new ActiveConnectionHandler() {
238239
@Override
239-
public void onSuccess() {
240-
Logger.d("active connection: %s", server.activeConnection);
241-
String url = String.format("http://%s:%s/library/sections/", server.activeConnection.address, server.activeConnection.port);
240+
public void onSuccess(Connection connection) {
241+
Logger.d("active connection: %s", connection);
242+
String url = String.format("http://%s:%s/library/sections/", connection.address, connection.port);
242243
if(server.accessToken != null)
243244
url += String.format("?%s=%s", PlexHeaders.XPlexToken, server.accessToken);
244245
AsyncHttpClient httpClient = new AsyncHttpClient();

mobile/src/main/java/com/atomjack/vcfp/activities/CastActivity.java

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,8 @@
1111
import com.atomjack.vcfp.interfaces.AfterTransientTokenRequest;
1212
import com.atomjack.shared.Logger;
1313
import com.atomjack.shared.PlayerState;
14-
import com.atomjack.vcfp.PlexHeaders;
1514
import com.atomjack.shared.Preferences;
16-
import com.atomjack.vcfp.QueryString;
1715
import com.atomjack.vcfp.R;
18-
import com.atomjack.vcfp.Utils;
1916
import com.atomjack.vcfp.VCFPCastConsumer;
2017
import com.atomjack.vcfp.VoiceControlForPlexApplication;
2118
import com.atomjack.vcfp.model.PlexClient;
@@ -31,8 +28,6 @@ public class CastActivity extends PlayerActivity {
3128

3229
private PlayerState currentState = PlayerState.STOPPED;
3330

34-
private String transientToken;
35-
3631
private List<PlexMedia> nowPlayingAlbum;
3732

3833
private Dialog connectingDialog;
@@ -112,7 +107,6 @@ private void init() {
112107
@Override
113108
public void success(String token) {
114109
Logger.d("Got transient token: %s", token);
115-
transientToken = token;
116110
castPlayerManager.setTransientToken(token);
117111
beginPlayback();
118112
}
@@ -149,8 +143,6 @@ private void hideConnectingDialog() {
149143
}
150144

151145
private void beginPlayback() {
152-
// String url = castPlayerManager.getTranscodeUrl(nowPlayingMedia);
153-
// Logger.d("url: %s", url);
154146
Logger.d("duration: %s", nowPlayingMedia.duration);
155147

156148
Logger.d("offset is %d", getOffset(nowPlayingMedia));
@@ -162,43 +154,6 @@ private void beginPlayback() {
162154
}
163155
}
164156

165-
private String getTranscodeUrl(PlexMedia media, String transientToken) {
166-
String url = media.server.activeConnection.uri;
167-
url += "/video/:/transcode/universal/start?";
168-
QueryString qs = new QueryString("path", String.format("http://127.0.0.1:32400%s", media.key));
169-
qs.add("mediaIndex", "0");
170-
qs.add("partIndex", "0");
171-
qs.add("protocol", "http");
172-
qs.add("offset", Integer.toString(getOffset(media)));
173-
qs.add("fastSeek", "1");
174-
qs.add("directPlay", "0");
175-
qs.add("directStream", "1");
176-
qs.add("videoQuality", "60");
177-
qs.add("videoResolution", "1024x768");
178-
qs.add("maxVideoBitrate", "2000");
179-
qs.add("subtitleSize", "100");
180-
qs.add("audioBoost", "100");
181-
// TODO: Fix this
182-
qs.add("session", Utils.generateRandomString());
183-
qs.add(PlexHeaders.XPlexClientIdentifier, VoiceControlForPlexApplication.getInstance().prefs.getUUID());
184-
qs.add(PlexHeaders.XPlexProduct, String.format("%s Chromecast", getString(R.string.app_name)));
185-
qs.add(PlexHeaders.XPlexDevice, mClient.castDevice.getModelName());
186-
qs.add(PlexHeaders.XPlexDeviceName, mClient.castDevice.getModelName());
187-
qs.add(PlexHeaders.XPlexPlatform, mClient.castDevice.getModelName());
188-
if(transientToken != null)
189-
qs.add(PlexHeaders.XPlexToken, transientToken);
190-
qs.add(PlexHeaders.XPlexPlatformVersion, "1.0");
191-
try {
192-
qs.add(PlexHeaders.XPlexVersion, getPackageManager().getPackageInfo(getPackageName(), 0).versionName);
193-
} catch (Exception ex) {
194-
ex.printStackTrace();
195-
}
196-
// TODO: Fix this
197-
if(VoiceControlForPlexApplication.getInstance().prefs.getString(Preferences.PLEX_USERNAME) != null)
198-
qs.add(PlexHeaders.XPlexUsername, VoiceControlForPlexApplication.getInstance().prefs.getString(Preferences.PLEX_USERNAME));
199-
return url + qs.toString();
200-
}
201-
202157
@Override
203158
protected void onResume() {
204159
Logger.d("CastActivity onResume");

mobile/src/main/java/com/atomjack/vcfp/activities/VCFPActivity.java

Lines changed: 57 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@
3939
import com.atomjack.vcfp.PlexSubscription;
4040
import com.atomjack.shared.Preferences;
4141
import com.atomjack.vcfp.R;
42+
import com.atomjack.vcfp.interfaces.ActiveConnectionHandler;
4243
import com.atomjack.vcfp.interfaces.BitmapHandler;
4344
import com.atomjack.vcfp.interfaces.ScanHandler;
44-
import com.atomjack.vcfp.interfaces.ServerFindHandler;
4545
import com.atomjack.shared.UriDeserializer;
4646
import com.atomjack.shared.UriSerializer;
4747
import com.atomjack.vcfp.VoiceControlForPlexApplication;
4848
import com.atomjack.vcfp.adapters.PlexListAdapter;
49+
import com.atomjack.vcfp.model.Connection;
4950
import com.atomjack.vcfp.model.MediaContainer;
5051
import com.atomjack.vcfp.model.PlexClient;
5152
import com.atomjack.vcfp.model.PlexDevice;
@@ -184,44 +185,34 @@ protected void onSubscriptionMessage(Timeline timeline) {
184185
private void getPlayingMedia(final PlexServer server, final Timeline timeline) {
185186
Logger.d("[VCFPActivity] getPlayingMedia: %s", timeline.key);
186187
// TODO: Find out why server can sometimes be null
187-
server.findServerConnection(new ServerFindHandler() {
188-
@Override
189-
public void onSuccess() {
190-
PlexHttpClient.get(server, timeline.key, new PlexHttpMediaContainerHandler() {
191-
@Override
192-
public void onSuccess(MediaContainer mediaContainer) {
193-
if(timeline.type.equals("video"))
194-
nowPlayingMedia = mediaContainer.videos.get(0);
195-
else if(timeline.type.equals("music"))
196-
nowPlayingMedia = mediaContainer.tracks.get(0);
197-
else {
198-
// TODO: Handle failure
199-
Logger.d("Failed to get media with type %s", timeline.type);
200-
}
201-
202-
if(nowPlayingMedia != null) {
203-
nowPlayingMedia.server = server;
204-
205-
VoiceControlForPlexApplication.getInstance().setNotification(mClient, mCurrentState, nowPlayingMedia);
206-
if (timeline.continuing != null && timeline.continuing.equals("1"))
207-
continuing = true;
208-
onMediaChange();
209-
sendWearPlaybackChange();
210-
}
211-
}
188+
PlexHttpClient.get(server, timeline.key, new PlexHttpMediaContainerHandler() {
189+
@Override
190+
public void onSuccess(MediaContainer mediaContainer) {
191+
if(timeline.type.equals("video"))
192+
nowPlayingMedia = mediaContainer.videos.get(0);
193+
else if(timeline.type.equals("music"))
194+
nowPlayingMedia = mediaContainer.tracks.get(0);
195+
else {
196+
// TODO: Handle failure
197+
Logger.d("Failed to get media with type %s", timeline.type);
198+
}
212199

213-
@Override
214-
public void onFailure(Throwable error) {
215-
// TODO: Handle failure
216-
}
217-
});
218-
}
200+
if(nowPlayingMedia != null) {
201+
nowPlayingMedia.server = server;
219202

220-
@Override
221-
public void onFailure(int statusCode) {
203+
VoiceControlForPlexApplication.getInstance().setNotification(mClient, mCurrentState, nowPlayingMedia);
204+
if (timeline.continuing != null && timeline.continuing.equals("1"))
205+
continuing = true;
206+
onMediaChange();
207+
sendWearPlaybackChange();
208+
}
209+
}
222210

223-
}
224-
});
211+
@Override
212+
public void onFailure(Throwable error) {
213+
// TODO: Handle failure
214+
}
215+
});
225216
}
226217

227218
private boolean isSubscribed() {
@@ -608,32 +599,42 @@ public void onUnsubscribed() {
608599
}
609600

610601
private void getThumb(final String thumb, final PlexMedia media) {
611-
String url = String.format("http://%s:%s%s", media.server.activeConnection.address, media.server.activeConnection.port, thumb);
612-
if(media.server.accessToken != null)
613-
url += String.format("?%s=%s", PlexHeaders.XPlexToken, media.server.accessToken);
614-
615-
PlexHttpClient.getClient().get(url, new BinaryHttpResponseHandler() {
602+
media.server.findServerConnection(new ActiveConnectionHandler() {
616603
@Override
617-
public void onSuccess(int i, Header[] headers, byte[] imageData) {
618-
InputStream is = new ByteArrayInputStream(imageData);
604+
public void onSuccess(Connection connection) {
605+
String url = String.format("http://%s:%s%s", connection.address, connection.port, thumb);
606+
if(media.server.accessToken != null)
607+
url += String.format("?%s=%s", PlexHeaders.XPlexToken, media.server.accessToken);
608+
609+
PlexHttpClient.getClient().get(url, new BinaryHttpResponseHandler() {
610+
@Override
611+
public void onSuccess(int i, Header[] headers, byte[] imageData) {
612+
InputStream is = new ByteArrayInputStream(imageData);
613+
614+
try {
615+
is.reset();
616+
} catch (IOException e) {
617+
e.printStackTrace();
618+
}
619619

620-
try {
621-
is.reset();
622-
} catch (IOException e) {
623-
e.printStackTrace();
624-
}
620+
// Save the downloaded image into the disk cache so we don't have to download it again
621+
try {
622+
mSimpleDiskCache.put(media.getCacheKey(thumb), is);
623+
} catch (Exception ex) {
624+
ex.printStackTrace();
625+
}
626+
setThumb(is);
627+
}
625628

626-
// Save the downloaded image into the disk cache so we don't have to download it again
627-
try {
628-
mSimpleDiskCache.put(media.getCacheKey(thumb), is);
629-
} catch (Exception ex) {
630-
ex.printStackTrace();
631-
}
632-
setThumb(is);
629+
@Override
630+
public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
631+
632+
}
633+
});
633634
}
634635

635636
@Override
636-
public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
637+
public void onFailure(int statusCode) {
637638

638639
}
639640
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.atomjack.vcfp.interfaces;
2+
3+
import com.atomjack.vcfp.model.Connection;
4+
5+
public interface ActiveConnectionHandler {
6+
public void onSuccess(Connection connection);
7+
public void onFailure(int statusCode);
8+
}

mobile/src/main/java/com/atomjack/vcfp/interfaces/ServerFindHandler.java

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)