Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace CountDownTimer with Handler+Thread #77

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
246 changes: 103 additions & 143 deletions tvplayer/src/main/java/com/kaltura/tvplayer/KalturaPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import android.content.Context;
import android.net.Uri;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.SystemClock;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -55,6 +56,7 @@
import com.kaltura.tvplayer.playlist.PlaylistController;

import com.kaltura.tvplayer.playlist.PlaylistEvent;
import com.kaltura.tvplayer.playlist.ProviderPlaylistOptions;
import com.kaltura.tvplayer.utils.ConfigResolver;
import com.kaltura.tvplayer.utils.NetworkUtils;

Expand All @@ -68,8 +70,8 @@ public abstract class KalturaPlayer {
private static final PKLog log = PKLog.get("KalturaPlayer");

public static final String DEFAULT_OVP_SERVER_URL = "https://cdnapisec.kaltura.com/";
public static final int COUNT_DOWN_TOTAL = 5000;
public static final int COUNT_DOWN_INTERVAL = 100;
private static final int RETRIEVE_WAIT_TIMEOUT = 5000;
private static final int RETRIEVE_WAIT_INTERVAL = 100;
public static final String OKHTTP = "okhttp";

static boolean playerConfigRetrieved;
Expand Down Expand Up @@ -113,6 +115,16 @@ public enum Type {
private PlayerInitOptions initOptions;
private PlaylistController playlistController;

private HandlerThread retrieveWaitThread;
private Handler retrieveWaitHandler;
{
retrieveWaitThread = new HandlerThread("RetrieveWaitThread");
retrieveWaitThread.start(); // quit() when done waiting
retrieveWaitHandler = new Handler(retrieveWaitThread.getLooper());

// retrieveWaitThread and retrieveWaitHandler are set to null when they are no longer needed.
}

KalturaPlayer(Context context, Type tvPlayerType, PlayerInitOptions initOptions) {

this.context = context;
Expand Down Expand Up @@ -691,42 +703,12 @@ public void loadPlaylistById(@NonNull OVPPlaylistIdOptions playlistOptions, @Non
if (!isValidOVPPlayer())
return;

new CountDownTimer(COUNT_DOWN_TOTAL, COUNT_DOWN_INTERVAL) {
@Override
public void onTick(long millisUntilFinished) {
if (playerConfigRetrieved || (initOptions != null && initOptions.tvPlayerParams != null)) {
cancel();
log.d("OVP loadPlaylist by id Done");
if (playerConfigRetrieved) {
initOptions.setTVPlayerParams(PlayerConfigManager.retrieve(Type.ovp, initOptions.partnerId));
}
populatePartnersValues();
final PlaylistProvider provider = playlistOptions.buildPlaylistProvider(getServerUrl(), getPartnerId(), playlistOptions.ks);
provider.load(response -> playlistLoadCompleted(response, (playlist, error) -> {
if (error != null) {
return;
}
PlaylistController playlistController = new PKPlaylistController(KalturaPlayer.this, playlist, PKPlaylistType.OVP_ID);
playlistController.setPlaylistOptions(playlistOptions);
controllerListener.onPlaylistControllerComplete(playlistController, null);
setPlaylistController(playlistController);
if (messageBus != null) {
messageBus.post(new PlaylistEvent.PlaylistStarted(playlist));
}
playlistController.playItem(playlistOptions.startIndex, autoPlay);
}));
}
}

@Override
public void onFinish() {
log.e("OVP loadPlaylist by id KalturaPlayerNotInitializedError");
controllerListener.onPlaylistControllerComplete(null, KalturaPlaylistInitializedError);
}
}.start();
runAfterRetrieve(
() -> loadPlaylistHelper(playlistOptions, controllerListener, PKPlaylistType.OVP_ID),
() -> controllerListener.onPlaylistControllerComplete(null, KalturaPlaylistInitializedError)
);
}


public void loadPlaylist(@NonNull OVPPlaylistOptions playlistOptions, @NonNull final OnPlaylistControllerListener controllerListener) {

if (!isValidOVPPlayer())
Expand All @@ -739,39 +721,10 @@ public void loadPlaylist(@NonNull OVPPlaylistOptions playlistOptions, @NonNull f
return;
}

new CountDownTimer(COUNT_DOWN_TOTAL, COUNT_DOWN_INTERVAL) {
@Override
public void onTick(long millisUntilFinished) {
if (playerConfigRetrieved || (initOptions != null && initOptions.tvPlayerParams != null)) {
cancel();
log.d("OVP loadPlaylist Done");
if (playerConfigRetrieved) {
initOptions.setTVPlayerParams(PlayerConfigManager.retrieve(Type.ovp, initOptions.partnerId));
}
populatePartnersValues();
final PlaylistProvider provider = playlistOptions.buildPlaylistProvider(getServerUrl(), getPartnerId(), playlistOptions.ks);
provider.load(response -> playlistLoadCompleted(response, (playlist, error) -> {
if (error != null) {
return;
}
PlaylistController playlistController = new PKPlaylistController(KalturaPlayer.this, playlist, PKPlaylistType.OVP_LIST);
playlistController.setPlaylistOptions(playlistOptions);
controllerListener.onPlaylistControllerComplete(playlistController, null);
setPlaylistController(playlistController);
if (messageBus != null) {
messageBus.post(new PlaylistEvent.PlaylistStarted(playlist));
}
playlistController.playItem(playlistOptions.startIndex, autoPlay);
}));
}
}

@Override
public void onFinish() {
log.e("OVP loadPlaylist KalturaPlaylistInitializedError");
controllerListener.onPlaylistControllerComplete(null, KalturaPlaylistInitializedError);
}
}.start();
runAfterRetrieve(
() -> loadPlaylistHelper(playlistOptions, controllerListener, PKPlaylistType.OVP_LIST),
() -> controllerListener.onPlaylistControllerComplete(null, KalturaPlaylistInitializedError)
);
}

public void loadPlaylist(@NonNull OTTPlaylistOptions playlistOptions, @NonNull final OnPlaylistControllerListener controllerListener) {
Expand All @@ -785,39 +738,11 @@ public void loadPlaylist(@NonNull OTTPlaylistOptions playlistOptions, @NonNull f
}
return;
}
new CountDownTimer(COUNT_DOWN_TOTAL, COUNT_DOWN_INTERVAL) {
@Override
public void onTick(long millisUntilFinished) {
if (playerConfigRetrieved || (initOptions != null && initOptions.tvPlayerParams != null)) {
cancel();
log.d("OTT loadPlaylist Done");
if (playerConfigRetrieved) {
initOptions.setTVPlayerParams(PlayerConfigManager.retrieve(Type.ott, initOptions.partnerId));
}
populatePartnersValues();
final PlaylistProvider provider = playlistOptions.buildPlaylistProvider(getServerUrl(), getPartnerId(), playlistOptions.ks);
provider.load(response -> playlistLoadCompleted(response, (playlist, error) -> {
if (error != null) {
return;
}
PlaylistController playlistController = new PKPlaylistController(KalturaPlayer.this, playlist, PKPlaylistType.OTT_LIST);
playlistController.setPlaylistOptions(playlistOptions);
controllerListener.onPlaylistControllerComplete(playlistController, null);
setPlaylistController(playlistController);
if (messageBus != null) {
messageBus.post(new PlaylistEvent.PlaylistStarted(playlist));
}
playlistController.playItem(playlistOptions.startIndex, autoPlay);
}));
}
}

@Override
public void onFinish() {
log.e("OTT loadPlaylist KalturaPlayerNotInitializedError");
controllerListener.onPlaylistControllerComplete(null, KalturaPlaylistInitializedError);
}
}.start();
runAfterRetrieve(
() -> loadPlaylistHelper(playlistOptions, controllerListener, PKPlaylistType.OTT_LIST),
() -> controllerListener.onPlaylistControllerComplete(null, KalturaPlaylistInitializedError)
);
}

public void loadPlaylist(@NonNull BasicPlaylistOptions playlistOptions, @NonNull final OnPlaylistControllerListener controllerListener) {
Expand Down Expand Up @@ -858,34 +783,33 @@ public void loadPlaylist(@NonNull BasicPlaylistOptions playlistOptions, @NonNull
playlistController.playItem(playlistOptions.startIndex, autoPlay);
}

private void loadPlaylistHelper(@NonNull ProviderPlaylistOptions playlistOptions, @NonNull OnPlaylistControllerListener controllerListener, PKPlaylistType playlistType) {
final PlaylistProvider provider = playlistOptions.buildPlaylistProvider(getServerUrl(), getPartnerId(), playlistOptions.ks);
provider.load(response -> playlistLoadCompleted(response, (playlist, error) -> {
if (error != null) {
return;
}
PlaylistController playlistController = new PKPlaylistController(KalturaPlayer.this, playlist, playlistType);
playlistController.setPlaylistOptions(playlistOptions);
controllerListener.onPlaylistControllerComplete(playlistController, null);
setPlaylistController(playlistController);
if (messageBus != null) {
messageBus.post(new PlaylistEvent.PlaylistStarted(playlist));
}
playlistController.playItem(playlistOptions.startIndex, autoPlay);
}));
}

public void loadMedia(@NonNull OTTMediaOptions mediaOptions, @NonNull final OnEntryLoadListener listener) {

if (!isValidOTTPlayer())
return;

prepareLoadMedia(mediaOptions);

new CountDownTimer(COUNT_DOWN_TOTAL, COUNT_DOWN_INTERVAL) {
@Override
public void onTick(long millisUntilFinished) {
if (playerConfigRetrieved || (initOptions != null && initOptions.tvPlayerParams != null)) {
cancel();
log.d("OTT loadMedia Done");
if (playerConfigRetrieved) {
initOptions.setTVPlayerParams(PlayerConfigManager.retrieve(Type.ott, initOptions.partnerId));
}
populatePartnersValues();
final MediaEntryProvider provider = mediaOptions.buildMediaProvider(getServerUrl(), getPartnerId());
provider.load(response -> mediaLoadCompleted(response, listener));
}
}

@Override
public void onFinish() {
log.e("KalturaPlayerNotInitializedError");
listener.onEntryLoadComplete(null, KalturaPlayerNotInitializedError);
}
}.start();
runAfterRetrieve(
() -> loadMediaHelper(mediaOptions, listener),
() -> listener.onEntryLoadComplete(null, KalturaPlayerNotInitializedError));
}

public void loadMedia(@NonNull OVPMediaOptions mediaOptions, @NonNull final OnEntryLoadListener listener) {
Expand All @@ -895,27 +819,15 @@ public void loadMedia(@NonNull OVPMediaOptions mediaOptions, @NonNull final OnEn

prepareLoadMedia(mediaOptions);

new CountDownTimer(COUNT_DOWN_TOTAL, COUNT_DOWN_INTERVAL) {
@Override
public void onTick(long millisUntilFinished) {
if (playerConfigRetrieved || (initOptions != null && initOptions.tvPlayerParams != null)) {
cancel();
log.d("OVP loadMedia Done");
if (playerConfigRetrieved) {
initOptions.setTVPlayerParams(PlayerConfigManager.retrieve(Type.ovp, initOptions.partnerId));
}
populatePartnersValues();
final MediaEntryProvider provider = mediaOptions.buildMediaProvider(getServerUrl(), getPartnerId());
provider.load(response -> mediaLoadCompleted(response, listener));
}
}
runAfterRetrieve(
() -> loadMediaHelper(mediaOptions, listener),
() -> listener.onEntryLoadComplete(null, KalturaPlayerNotInitializedError)
);
}

@Override
public void onFinish() {
log.e("KalturaPlayerNotInitializedError");
listener.onEntryLoadComplete(null, KalturaPlayerNotInitializedError);
}
}.start();
private void loadMediaHelper(@NonNull MediaOptions mediaOptions, @NonNull OnEntryLoadListener listener) {
final MediaEntryProvider provider = mediaOptions.buildMediaProvider(getServerUrl(), getPartnerId());
provider.load(response -> mediaLoadCompleted(response, listener));
}

private boolean isValidOVPPlayer() {
Expand Down Expand Up @@ -1021,6 +933,54 @@ private PhoenixAnalyticsConfig getPhoenixAnalyticsConfig() {
return new PhoenixAnalyticsConfig(getPartnerId(), getServerUrl(), getKS(), Consts.DEFAULT_ANALYTICS_TIMER_INTERVAL_HIGH_SEC);
}

private boolean runIfReady(Runnable onReady) {
if (playerConfigRetrieved || (initOptions != null && initOptions.tvPlayerParams != null)) {
log.d("READY!");
if (playerConfigRetrieved) {
initOptions.setTVPlayerParams(PlayerConfigManager.retrieve(getTvPlayerType(), initOptions.partnerId));
}
populatePartnersValues();
onReady.run();
return true;
}
return false;
}

private void runAfterRetrieve(Runnable onReady, Runnable onTimeout) {

if (runIfReady(onReady)) {
return;
}

final long startTime = SystemClock.elapsedRealtime();

retrieveWaitHandler.post(new Runnable() {

@Override
public void run() {
if (runIfReady(onReady)) {
retrieveWaitThread.quit();
retrieveWaitThread = null;
retrieveWaitHandler = null;

} else {
log.d("Not ready yet " + (SystemClock.elapsedRealtime() - startTime));

if (SystemClock.elapsedRealtime() - startTime < RETRIEVE_WAIT_TIMEOUT) {
// Try again in 100ms
retrieveWaitHandler.postDelayed(this, RETRIEVE_WAIT_INTERVAL);
} else {
log.e("Timed out waiting for retrieve");
onTimeout.run();
retrieveWaitThread.quit();
retrieveWaitThread = null;
retrieveWaitHandler = null;
}
}
}
});
}

public interface OnEntryLoadListener {
void onEntryLoadComplete(PKMediaEntry entry, ErrorElement error);
}
Expand Down