Skip to content

Commit ccad728

Browse files
BPK session close fix (#38)
1 parent 2ea4ae2 commit ccad728

File tree

1 file changed

+62
-8
lines changed

1 file changed

+62
-8
lines changed

broadpeakplugin/src/main/java/com/kaltura/playkit/plugins/broadpeak/BroadpeakPlugin.java

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
import com.kaltura.playkit.PlayerEvent;
1818
import com.kaltura.tvplayer.PKMediaEntryInterceptor;
1919

20+
import java.util.ArrayList;
2021
import java.util.HashMap;
22+
import java.util.List;
2123
import java.util.Map;
2224

2325
import tv.broadpeak.smartlib.SmartLib;
@@ -31,11 +33,21 @@ public class BroadpeakPlugin extends PKPlugin implements PKMediaEntryInterceptor
3133
private static final PKLog log = PKLog.get("BroadpeakPlugin");
3234

3335
private MessageBus messageBus;
34-
private final Map<String, StreamingSession> sessionsMap = new HashMap<>();
36+
private final Map<String, StreamingSessionInfo> sessionsMap = new HashMap<>();
3537
private Player player;
3638
private BroadpeakConfig config;
3739
private Context context;
3840

41+
static class StreamingSessionInfo {
42+
private final StreamingSession session;
43+
private boolean updateMediaReceived;
44+
45+
public StreamingSessionInfo(StreamingSession session) {
46+
this.session = session;
47+
this.updateMediaReceived = false;
48+
}
49+
}
50+
3951
public static final Factory factory = new Factory() {
4052
@Override
4153
public String getName() {
@@ -93,8 +105,9 @@ protected void onLoad(final Player player, Object config, final MessageBus messa
93105

94106
this.messageBus.addListener(this, PlayerEvent.stopped, event -> {
95107
log.d("PlayerEvent stopped: calling stopStreamingSession");
108+
PlayerEvent.Stopped stoppedEvent = (PlayerEvent.Stopped)event;
96109
// Stop the session in case of Playback stop
97-
stopCurrentStreamingSession();
110+
stopStreamingSession(stoppedEvent.mediaSourceUrl);
98111
});
99112
}
100113

@@ -123,6 +136,16 @@ private void addGeneralConfig(BroadpeakConfig bpConfig) {
123136
@Override
124137
protected void onUpdateMedia(PKMediaConfig mediaConfig) {
125138
log.d("Start onUpdateMedia");
139+
if (mediaConfig != null
140+
&& mediaConfig.getMediaEntry() != null
141+
&& mediaConfig.getMediaEntry().getSources() != null
142+
&& mediaConfig.getMediaEntry().getSources().get(0) != null) {
143+
String currentSession = mediaConfig.getMediaEntry().getSources().get(0).getUrl();
144+
onUpdateMediaReceivedForStreamingSession(currentSession);
145+
cleanupRunningStreamingSessions(currentSession);
146+
} else {
147+
log.d("Empty mediaConfig, sessions cleanup skipped");
148+
}
126149
}
127150

128151
@Override
@@ -192,11 +215,42 @@ private void stopCurrentStreamingSession() {
192215
private void stopStreamingSession(String sessionKey) {
193216
log.d("stopStreamingSession called with sessionKey=[" + sessionKey + "]");
194217
if (sessionKey != null && sessionsMap.containsKey(sessionKey)) {
195-
StreamingSession session = sessionsMap.get(sessionKey);
196-
if (session != null) {
197-
session.stopStreamingSession();
218+
StreamingSessionInfo sessionInfo = sessionsMap.get(sessionKey);
219+
if (sessionInfo != null) {
220+
if (sessionInfo.updateMediaReceived) {
221+
sessionInfo.session.stopStreamingSession();
222+
sessionsMap.remove(sessionKey);
223+
} else {
224+
log.d("Session not stopped, updateMediaReceived false");
225+
}
226+
} else {
227+
log.d("Session not stopped, sessionInfo is null");
198228
}
199-
sessionsMap.remove(sessionKey);
229+
} else {
230+
log.d("sessionMap entry not found");
231+
}
232+
log.d("Finalizing stopStreamingSession call, number of active sessions = " + sessionsMap.size());
233+
}
234+
235+
private void onUpdateMediaReceivedForStreamingSession(String currentSession) {
236+
log.d("cleanupStreamingSessions called with currentSession=[" + currentSession + "]");
237+
if (currentSession != null && sessionsMap.containsKey(currentSession)) {
238+
StreamingSessionInfo sessionInfo = sessionsMap.get(currentSession);
239+
if (sessionInfo != null) {
240+
sessionInfo.updateMediaReceived = true;
241+
}
242+
}
243+
}
244+
245+
private void cleanupRunningStreamingSessions(String currentSession) {
246+
List<String> cleanedUpSessions = new ArrayList<>();
247+
for (Map.Entry<String, StreamingSessionInfo> sessionEntry : sessionsMap.entrySet()) {
248+
if (!sessionEntry.getKey().equals(currentSession)) {
249+
cleanedUpSessions.add(sessionEntry.getKey());
250+
}
251+
}
252+
for (String sessionKey : cleanedUpSessions) {
253+
stopStreamingSession(sessionKey);
200254
}
201255
}
202256

@@ -228,7 +282,6 @@ public void apply(PKMediaEntry mediaEntry, PKMediaEntryInterceptor.Listener list
228282
sendBroadpeakErrorEvent(errorCode, errorMessage);
229283
return;
230284
}
231-
sessionsMap.put(source.getUrl(), session);
232285

233286
addSessionConfig(session);
234287
session.attachPlayer(player, messageBus);
@@ -240,13 +293,14 @@ public void apply(PKMediaEntry mediaEntry, PKMediaEntryInterceptor.Listener list
240293
// Replace the URL
241294
log.d("Apply New Entry URL " + mediaEntry.getName() + " - " + mediaEntry.getId() + " url: " + result.getURL());
242295
source.setUrl(result.getURL());
296+
sessionsMap.put(source.getUrl(), new StreamingSessionInfo(session));
243297
} else {
244298
// Stop the session in case of error
245299
if (result != null) {
246300
errorCode = result.getErrorCode();
247301
errorMessage = result.getErrorMessage();
248302
}
249-
stopStreamingSession(source.getUrl());
303+
session.stopStreamingSession();
250304
// send event to MessageBus
251305
sendBroadpeakErrorEvent(errorCode, errorMessage);
252306
}

0 commit comments

Comments
 (0)