17
17
import com .kaltura .playkit .PlayerEvent ;
18
18
import com .kaltura .tvplayer .PKMediaEntryInterceptor ;
19
19
20
+ import java .util .ArrayList ;
20
21
import java .util .HashMap ;
22
+ import java .util .List ;
21
23
import java .util .Map ;
22
24
23
25
import tv .broadpeak .smartlib .SmartLib ;
@@ -31,11 +33,21 @@ public class BroadpeakPlugin extends PKPlugin implements PKMediaEntryInterceptor
31
33
private static final PKLog log = PKLog .get ("BroadpeakPlugin" );
32
34
33
35
private MessageBus messageBus ;
34
- private final Map <String , StreamingSession > sessionsMap = new HashMap <>();
36
+ private final Map <String , StreamingSessionInfo > sessionsMap = new HashMap <>();
35
37
private Player player ;
36
38
private BroadpeakConfig config ;
37
39
private Context context ;
38
40
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
+
39
51
public static final Factory factory = new Factory () {
40
52
@ Override
41
53
public String getName () {
@@ -93,8 +105,9 @@ protected void onLoad(final Player player, Object config, final MessageBus messa
93
105
94
106
this .messageBus .addListener (this , PlayerEvent .stopped , event -> {
95
107
log .d ("PlayerEvent stopped: calling stopStreamingSession" );
108
+ PlayerEvent .Stopped stoppedEvent = (PlayerEvent .Stopped )event ;
96
109
// Stop the session in case of Playback stop
97
- stopCurrentStreamingSession ( );
110
+ stopStreamingSession ( stoppedEvent . mediaSourceUrl );
98
111
});
99
112
}
100
113
@@ -123,6 +136,16 @@ private void addGeneralConfig(BroadpeakConfig bpConfig) {
123
136
@ Override
124
137
protected void onUpdateMedia (PKMediaConfig mediaConfig ) {
125
138
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
+ }
126
149
}
127
150
128
151
@ Override
@@ -192,11 +215,42 @@ private void stopCurrentStreamingSession() {
192
215
private void stopStreamingSession (String sessionKey ) {
193
216
log .d ("stopStreamingSession called with sessionKey=[" + sessionKey + "]" );
194
217
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" );
198
228
}
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 );
200
254
}
201
255
}
202
256
@@ -228,7 +282,6 @@ public void apply(PKMediaEntry mediaEntry, PKMediaEntryInterceptor.Listener list
228
282
sendBroadpeakErrorEvent (errorCode , errorMessage );
229
283
return ;
230
284
}
231
- sessionsMap .put (source .getUrl (), session );
232
285
233
286
addSessionConfig (session );
234
287
session .attachPlayer (player , messageBus );
@@ -240,13 +293,14 @@ public void apply(PKMediaEntry mediaEntry, PKMediaEntryInterceptor.Listener list
240
293
// Replace the URL
241
294
log .d ("Apply New Entry URL " + mediaEntry .getName () + " - " + mediaEntry .getId () + " url: " + result .getURL ());
242
295
source .setUrl (result .getURL ());
296
+ sessionsMap .put (source .getUrl (), new StreamingSessionInfo (session ));
243
297
} else {
244
298
// Stop the session in case of error
245
299
if (result != null ) {
246
300
errorCode = result .getErrorCode ();
247
301
errorMessage = result .getErrorMessage ();
248
302
}
249
- stopStreamingSession (source . getUrl () );
303
+ session . stopStreamingSession ();
250
304
// send event to MessageBus
251
305
sendBroadpeakErrorEvent (errorCode , errorMessage );
252
306
}
0 commit comments