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
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions tvplayer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ dependencies {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
}
254 changes: 111 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,11 @@

import android.content.Context;
import android.net.Uri;
import android.os.CountDownTimer;
import android.os.Build;
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 +57,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 +71,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 +116,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 +704,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();
runAfterLoadConfig(
() -> 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 +722,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();
runAfterLoadConfig(
() -> loadPlaylistHelper(playlistOptions, controllerListener, PKPlaylistType.OVP_LIST),
() -> controllerListener.onPlaylistControllerComplete(null, KalturaPlaylistInitializedError)
);
}

public void loadPlaylist(@NonNull OTTPlaylistOptions playlistOptions, @NonNull final OnPlaylistControllerListener controllerListener) {
Expand All @@ -785,39 +739,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();
runAfterLoadConfig(
() -> 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 +784,34 @@ 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();
runAfterLoadConfig(
"OTT loadMedia",
() -> loadMediaHelper(mediaOptions, listener),
() -> listener.onEntryLoadComplete(null, KalturaPlayerNotInitializedError));
}

public void loadMedia(@NonNull OVPMediaOptions mediaOptions, @NonNull final OnEntryLoadListener listener) {
Expand All @@ -895,27 +821,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));
}
}
runAfterLoadConfig(
() -> 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 +935,60 @@ private PhoenixAnalyticsConfig getPhoenixAnalyticsConfig() {
return new PhoenixAnalyticsConfig(getPartnerId(), getServerUrl(), getKS(), Consts.DEFAULT_ANALYTICS_TIMER_INTERVAL_HIGH_SEC);
}

private boolean runIfReady(String logActionName, Runnable onReady) {
if (playerConfigRetrieved || (initOptions != null && initOptions.tvPlayerParams != null)) {
if (playerConfigRetrieved) {
initOptions.setTVPlayerParams(PlayerConfigManager.retrieve(getTvPlayerType(), initOptions.partnerId));
}
populatePartnersValues();
onReady.run();
log.d(logActionName + ": success after player config was ready");
return true;
}
return false;
}

private void runAfterLoadConfig(String logActionName, Runnable onReady, Runnable onTimeout) {

if (runIfReady(logActionName, onReady)) {
return;
}

final long startTime = SystemClock.elapsedRealtime();

retrieveWaitHandler.post(new Runnable() {

@Override
public void run() {
if (runIfReady(logActionName, onReady)) {
quitConfigWaitThread();

} 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();
quitConfigWaitThread();
}
}
}
});
}

private void quitConfigWaitThread() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
retrieveWaitThread.quitSafely();
} else {
retrieveWaitThread.quit(); // Less safe, but we have a very small percentage of
}
retrieveWaitThread = null;
retrieveWaitHandler = null;
}

public interface OnEntryLoadListener {
void onEntryLoadComplete(PKMediaEntry entry, ErrorElement error);
}
Expand Down
4 changes: 3 additions & 1 deletion tvplayer/src/main/java/com/kaltura/tvplayer/KnownPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ enum KnownPlugin {
public final String className;
public final PKPlugin.Factory factory;

private boolean registered;

KnownPlugin(String className, PKPlugin.Factory factory) {
this.className = className;
this.factory = factory;
Expand All @@ -41,7 +43,7 @@ enum KnownPlugin {
this(className, null);
}

protected static void registerPluginByName(Context context, String pluginClassName) {
void registerPluginByName(Context context, String pluginClassName) {
try {
Class pluginClass = Class.forName(pluginClassName);
final Field factoryField = pluginClass.getField("factory");
Expand Down