Skip to content

Commit

Permalink
Add playSessionId to playManifest URLs (kaltura#196)
Browse files Browse the repository at this point in the history
* UrlDecorator allows "decorating" (changing) a URL before usage.

PKMediaConfig now has a UrlDecorator field -- app can set it per media.

* PlayManifestSessionIdDecorator -- UrlDecorator that adds playSessionId to playManifest URLs.

* Moved sessionId value from PlayManifestSessionIdDecorator to PlayerController (Player.getSessionId()).

* Moved urlDecorator instance from PKMediaConfig to Player.

* Replaced UrlDecorator with RequestInfo.Decorator.

RequestInfo contains url and headers. The decorator allows changing both.

Added Player.Settings. Allows changing player behavior -- for now only setting contentRequestDecorator.

* Rename PlayManifestSessionIdDecorator to KalturaPlaybackRequestDecorator.

* PlayerLoader: set contentRequestDecorator to KalturaPlaybackRequestDecorator.

* fix coach

* Removed Player.Settings getter and added javadoc.

* Moved KalturaPlaybackRequestDecorator to plugins.playback package.

* Added a plugin that wraps KalturaPlaybackRequestDecorator.

* Remove KalturaPlaybackUrlPlugin. It's not needed.

* Encapsulate KalturaPlaybackRequestDecorator setup.

* Made the request decorator's constructor private.
  • Loading branch information
noamtamim authored Apr 2, 2017
1 parent 36d50f6 commit 851c2f1
Show file tree
Hide file tree
Showing 12 changed files with 247 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@

import com.kaltura.playkit.PKEvent;
import com.kaltura.playkit.PKMediaConfig;
import com.kaltura.playkit.PKRequestInfo;
import com.kaltura.playkit.Player;
import com.kaltura.playkit.ads.AdController;
import com.kaltura.playkit.player.PlayerView;

import java.util.UUID;

/**
* Created by zivilan on 11/12/2016.
*/
Expand All @@ -18,6 +21,17 @@ public class MockPlayer implements Player {
private int duration = 100;
private long currentPosition = 0;

@Override
public Settings getSettings() {
return new Settings() {

@Override
public Settings setContentRequestDecorator(PKRequestInfo.Decorator contentRequestDecorator) {
return null;
}
};
}

@Override
public void prepare(@NonNull PKMediaConfig playerConfig) {

Expand Down Expand Up @@ -128,6 +142,11 @@ public AdController getAdController() {
return null;
}

@Override
public UUID getSessionId() {
return null;
}

public void setDuration(int duration){
this.duration = duration;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ public PKMediaConfig setMediaEntry(PKMediaEntry mediaEntry) {
return this;
}
}

38 changes: 38 additions & 0 deletions playkit/src/main/java/com/kaltura/playkit/PKRequestInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.kaltura.playkit;

import android.net.Uri;

import java.util.Map;

public class PKRequestInfo {

private Uri url;
private Map<String, String> headers;

public PKRequestInfo(Uri url, Map<String, String> headers) {
this.url = url;
this.headers = headers;
}

public Uri getUrl() {
return url;
}

public PKRequestInfo setUrl(Uri url) {
this.url = url;
return this;
}

public Map<String, String> getHeaders() {
return headers;
}

public PKRequestInfo setHeaders(Map<String, String> headers) {
this.headers = headers;
return this;
}

public interface Decorator {
PKRequestInfo getRequestInfo(PKRequestInfo requestInfo);
}
}
25 changes: 25 additions & 0 deletions playkit/src/main/java/com/kaltura/playkit/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,30 @@
import com.kaltura.playkit.player.PlayerView;
import com.kaltura.playkit.utils.Consts;

import java.util.UUID;

/**
* Created by Noam Tamim @ Kaltura on 18/09/2016.
*/
public interface Player {

/**
* Interface used for setting optional Player settings.
*/
interface Settings {
/**
* Set the Player's contentRequestDecorator.
* @param contentRequestDecorator
* @return Player Settings.
*/
Settings setContentRequestDecorator(PKRequestInfo.Decorator contentRequestDecorator);
}

/**
* Get the Player's {@link Settings} object, for setting some optional properties.
* @return Player Settings.
*/
Settings getSettings();

/**
* Prepare the player for playback.
Expand Down Expand Up @@ -134,5 +153,11 @@ public interface Player {
void seekTo(long position);

AdController getAdController();

/**
* Get the Player's SessionId. The SessionId is initialized when the player loads.
* @return Player's SessionId, as a UUID object.
*/
UUID getSessionId();
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@
import com.kaltura.playkit.ads.AdController;
import com.kaltura.playkit.player.PlayerView;

import java.util.UUID;

public class PlayerDecoratorBase implements Player {


@Override
public Settings getSettings() {
return player.getSettings();
}

@Override
public void prepare(@NonNull PKMediaConfig mediaConfig) {
player.prepare(mediaConfig);
Expand All @@ -33,6 +40,11 @@ public AdController getAdController() {
return player.getAdController();
}

@Override
public final UUID getSessionId() {
return player.getSessionId();
}

@Override
public void play() {
player.play();
Expand Down
4 changes: 4 additions & 0 deletions playkit/src/main/java/com/kaltura/playkit/PlayerLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.support.annotation.Nullable;

import com.kaltura.playkit.player.PlayerController;
import com.kaltura.playkit.plugins.playback.KalturaPlaybackRequestDecorator;

import java.util.ArrayList;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -41,6 +42,9 @@ class PlayerLoader extends PlayerDecoratorBase {

public void load(@NonNull PKPluginConfigs pluginsConfig) {
PlayerController playerController = new PlayerController(context);

// By default, set Kaltura decorator.
KalturaPlaybackRequestDecorator.setup(playerController);

playerController.setEventListener(new PKEvent.Listener() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import com.kaltura.playkit.BuildConfig;
import com.kaltura.playkit.PKLog;
import com.kaltura.playkit.PKMediaFormat;
import com.kaltura.playkit.PKMediaSource;
import com.kaltura.playkit.PlaybackParamsInfo;
import com.kaltura.playkit.PlayerEvent;
import com.kaltura.playkit.PlayerState;
Expand Down Expand Up @@ -69,7 +68,8 @@ class ExoPlayerWrapper implements PlayerEngine, ExoPlayer.EventListener {
private DeferredDrmSessionManager drmSessionManager;

private PlayerEvent.Type currentEvent;
private PlayerState currentState = PlayerState.IDLE, previousState;
private PlayerState currentState = PlayerState.IDLE;
private PlayerState previousState;

private Factory mediaDataSourceFactory;
private Handler mainHandler = new Handler(Looper.getMainLooper());
Expand Down Expand Up @@ -149,25 +149,25 @@ private DefaultTrackSelector initializeTrackSelector() {
return trackSelector;
}

private void preparePlayer(PKMediaSource pkMediaSource) {
private void preparePlayer(PKMediaSourceConfig sourceConfig) {
sameErrorOccurrenceCounter = 0;
drmSessionManager.setMediaSource(pkMediaSource);
drmSessionManager.setMediaSource(sourceConfig.mediaSource);

shouldGetTracksInfo = true;
this.lastPlayedSource = Uri.parse(pkMediaSource.getUrl());
MediaSource mediaSource = buildExoMediaSource(pkMediaSource);
this.lastPlayedSource = sourceConfig.getUrl();
MediaSource mediaSource = buildExoMediaSource(sourceConfig);
player.prepare(mediaSource, shouldResetPlayerPosition, shouldResetPlayerPosition);
changeState(PlayerState.LOADING);
}

private MediaSource buildExoMediaSource(PKMediaSource source) {
PKMediaFormat format = source.getMediaFormat();
private MediaSource buildExoMediaSource(PKMediaSourceConfig sourceConfig) {
PKMediaFormat format = sourceConfig.mediaSource.getMediaFormat();
if (format == null) {
// TODO: error?
return null;
}

Uri uri = Uri.parse(source.getUrl());
Uri uri = sourceConfig.getUrl();


switch (format) {
Expand Down Expand Up @@ -357,13 +357,13 @@ public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray tra
}

@Override
public void load(PKMediaSource mediaSource) {
public void load(PKMediaSourceConfig mediaSourceConfig) {
log.d("load");
if (player == null) {
initializePlayer();
}

preparePlayer(mediaSource);
preparePlayer(mediaSourceConfig);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import com.kaltura.playkit.PKDrmParams;
import com.kaltura.playkit.PKLog;
import com.kaltura.playkit.PKMediaSource;
import com.kaltura.playkit.PlaybackParamsInfo;
import com.kaltura.playkit.PlayerEvent;
import com.kaltura.playkit.PlayerState;
Expand All @@ -37,12 +36,12 @@ public class MediaPlayerWrapper implements PlayerEngine, SurfaceHolder.Callback,

private static final PKLog log = PKLog.get("MediaPlayerWrapper");

private static int ILLEGAL_STATEׁ_OPERATION = -38;
private static final int ILLEGAL_STATEׁ_OPERATION = -38;

private Context context;
private MediaPlayer player;
private MediaPlayerView mediaPlayerView;
private PKMediaSource mediaSource;
private PKMediaSourceConfig mediaSourceConfig;
private String assetUri;

private String licenseUri;
Expand Down Expand Up @@ -82,14 +81,16 @@ public void onEvent(DrmEvent event) {
}

@Override
public void load(PKMediaSource mediaSource) {
public void load(PKMediaSourceConfig mediaSourceConfig) {
log.d("load");
if (currentState != null && this.mediaSource != null && !this.mediaSource.equals(mediaSource) && prepareState != PREPARING) {

if (currentState != null && this.mediaSourceConfig != null && !this.mediaSourceConfig.equals(mediaSourceConfig) && prepareState != PREPARING) {
player.reset();
currentState = PlayerState.IDLE;
prepareState = PrepareState.NOT_PREPARED;
}
this.mediaSource = mediaSource;

this.mediaSourceConfig = mediaSourceConfig;
if ((currentState == null || currentState == PlayerState.IDLE) && prepareState != PREPARING) {
initializePlayer();
}
Expand All @@ -103,7 +104,7 @@ private void initializePlayer() {
//player.setAudioStreamType(AudioManager.STREAM_MUSIC);
//player.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT);

assetUri = mediaSource.getUrl();
assetUri = mediaSourceConfig.getUrl().toString();
String assetAcquireUri = getWidevineAssetAcquireUri(assetUri);
try {
mediaPlayerView.getSurfaceHolder().addCallback(this);
Expand All @@ -113,7 +114,7 @@ private void initializePlayer() {
log.e(e.toString());
}
if (drmClient.needToAcquireRights(assetAcquireUri)) {
List<PKDrmParams> drmData = mediaSource.getDrmData();
List<PKDrmParams> drmData = mediaSourceConfig.mediaSource.getDrmData();
if (drmData != null) {
licenseUri = drmData.get(0).getLicenseUri();
drmClient.acquireRights(assetAcquireUri, licenseUri);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.kaltura.playkit.player;

import android.net.Uri;

import com.kaltura.playkit.PKMediaSource;
import com.kaltura.playkit.PKRequestInfo;

/**
* Created by Noam Tamim @ Kaltura on 29/03/2017.
*/
class PKMediaSourceConfig {
PKMediaSource mediaSource;
PKRequestInfo.Decorator decorator;

PKMediaSourceConfig(PKMediaSource mediaSource, PKRequestInfo.Decorator decorator) {
this.mediaSource = mediaSource;
this.decorator = decorator;
}

Uri getUrl() {
Uri uri = Uri.parse(mediaSource.getUrl());
if (decorator == null) {
return uri;
} else {
return decorator.getRequestInfo(new PKRequestInfo(uri, null)).getUrl();
}
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

PKMediaSourceConfig that = (PKMediaSourceConfig) o;

if (mediaSource != null ? !mediaSource.equals(that.mediaSource) : that.mediaSource != null)
return false;
return decorator != null ? decorator.equals(that.decorator) : that.decorator == null;
}

@Override
public int hashCode() {
int result = mediaSource != null ? mediaSource.hashCode() : 0;
result = 31 * result + (decorator != null ? decorator.hashCode() : 0);
return result;
}
}
Loading

0 comments on commit 851c2f1

Please sign in to comment.