Skip to content

Commit

Permalink
merge PlaybackInfoProvider and PlayerHelper into PlayerAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
stonko1994 committed Aug 23, 2024
1 parent 96e2ce8 commit c72fe5e
Show file tree
Hide file tree
Showing 9 changed files with 269 additions and 220 deletions.

This file was deleted.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package com.bitmovin.analytics.conviva;

import com.bitmovin.analytics.conviva.helper.WithEventEmitter;
import com.bitmovin.player.api.Player;
import com.bitmovin.player.api.media.video.quality.VideoQuality;
import com.conviva.sdk.ConvivaSdkConstants;

import java.util.HashMap;

public class DefaultPlayerAdapter implements PlayerAdapter {
private final Player player;

public DefaultPlayerAdapter(Player player) {
this.player = player;
}

@Override
public ConvivaSdkConstants.PlayerState getPlayerState() {
ConvivaSdkConstants.PlayerState state;
if (player.isPaused()) {
state = ConvivaSdkConstants.PlayerState.PAUSED;
} else if (player.isStalled()) {
state = ConvivaSdkConstants.PlayerState.BUFFERING;
} else {
state = ConvivaSdkConstants.PlayerState.PLAYING;
}
return state;
}

@Override
public HashMap<String, Object[]> getPlaybackVideoData() {
HashMap<String, Object[]> videoData = new HashMap<>();
VideoQuality playbackVideoData = player.getPlaybackVideoData();
if (playbackVideoData != null) {
videoData.put(ConvivaSdkConstants.PLAYBACK.RESOLUTION, new Object[]{playbackVideoData.getWidth(), playbackVideoData.getHeight()});
videoData.put(ConvivaSdkConstants.PLAYBACK.BITRATE, new Object[]{playbackVideoData.getBitrate() / 1000});
final int peakBitrate = playbackVideoData.getPeakBitrate();
if (peakBitrate != VideoQuality.BITRATE_NO_VALUE) {
videoData.put(ConvivaSdkConstants.PLAYBACK.BITRATE, new Object[]{peakBitrate / 1000});
}
final int averageBitrate = playbackVideoData.getAverageBitrate();
if (averageBitrate != VideoQuality.BITRATE_NO_VALUE) {
videoData.put(ConvivaSdkConstants.PLAYBACK.AVG_BITRATE, new Object[]{averageBitrate / 1000});
}
videoData.put(ConvivaSdkConstants.PLAYBACK.RENDERED_FRAMERATE, new Object[]{Math.round(playbackVideoData.getFrameRate())});
}
return videoData;
}

@Override
public String getStreamTitle() {
return player.getSource() == null ? null : player.getSource().getConfig().getTitle();
}

@Override
public String getStreamType() {
return player.getSource() == null ? null : player.getSource().getConfig().getType().name();
}

@Override
public String getStreamUrl() {
return player.getSource() == null ? null : player.getSource().getConfig().getUrl();
}


@Override
public boolean isAd() {
return player.isAd();
}

@Override
public boolean isLive() {
return player.isLive();
}

@Override
public boolean isPaused() {
return player.isPaused();
}

@Override
public boolean isPlaying() {
return player.isPlaying();
}

@Override
public double getDuration() {
return player.getDuration();
}

@Override
public long getPlayHeadTimeMillis() {
if (player.isLive()) {
double playerTimeShiftMax = player.getMaxTimeShift();
double playerTimeShift = player.getTimeShift();
long playerDurationMs = -(Math.round(playerTimeShiftMax * 1000));
return playerDurationMs - -(Math.round(playerTimeShift * 1000));

} else {
double currentTime = player.getCurrentTime();
return (long) (currentTime * 1000);
}
}

@Override
public void withEventEmitter(WithEventEmitter withEventEmitter) {
withEventEmitter.call(player);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.bitmovin.analytics.conviva;

import com.bitmovin.analytics.conviva.helper.WithEventEmitter;
import com.conviva.sdk.ConvivaSdkConstants;

import java.util.HashMap;

public interface PlayerAdapter {
ConvivaSdkConstants.PlayerState getPlayerState();

HashMap<String, Object[]> getPlaybackVideoData();

boolean isAd();

String getStreamTitle();

String getStreamType();

String getStreamUrl();

long getPlayHeadTimeMillis();

boolean isLive();

double getDuration();

void withEventEmitter(WithEventEmitter withEventEmitter);

boolean isPaused();

boolean isPlaying();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.bitmovin.analytics.conviva.helper;

import com.bitmovin.player.api.event.Event;
import com.bitmovin.player.api.event.JavaEventEmitter;

public interface WithEventEmitter {
void call(JavaEventEmitter<Event> player);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.util.Log;

import com.bitmovin.analytics.conviva.ConvivaAnalyticsIntegration;
import com.bitmovin.analytics.conviva.PlayerAdapter;
import com.conviva.sdk.ConvivaAdAnalytics;
import com.conviva.sdk.ConvivaSdkConstants;
import com.conviva.sdk.ConvivaVideoAnalytics;
Expand All @@ -15,12 +16,12 @@ public class DefaultSsaiApi implements SsaiApi {
private static final String TAG = "DefaultSsaiApi";
private final ConvivaVideoAnalytics convivaVideoAnalytics;
private final ConvivaAdAnalytics convivaAdAnalytics;
private final PlaybackInfoProvider player;
private final PlayerAdapter playerAdapter;

public DefaultSsaiApi(ConvivaVideoAnalytics convivaVideoAnalytics, ConvivaAdAnalytics convivaAdAnalytics, PlaybackInfoProvider player) {
public DefaultSsaiApi(ConvivaVideoAnalytics convivaVideoAnalytics, ConvivaAdAnalytics convivaAdAnalytics, PlayerAdapter playerAdapter) {
this.convivaVideoAnalytics = convivaVideoAnalytics;
this.convivaAdAnalytics = convivaAdAnalytics;
this.player = player;
this.playerAdapter = playerAdapter;
}

private boolean isAdBreakActive = false;
Expand Down Expand Up @@ -76,8 +77,8 @@ public void reportAdStarted(AdInfo adInfo) {
}

private void reportInitialAdMetrics() {
convivaAdAnalytics.reportAdMetric(ConvivaSdkConstants.PLAYBACK.PLAYER_STATE, player.getPlayerState());
HashMap<String, Object[]> playbackVideoData = player.getPlaybackVideoData();
convivaAdAnalytics.reportAdMetric(ConvivaSdkConstants.PLAYBACK.PLAYER_STATE, playerAdapter.getPlayerState());
HashMap<String, Object[]> playbackVideoData = playerAdapter.getPlaybackVideoData();
for (Map.Entry<String, Object[]> entry : playbackVideoData.entrySet()) {
convivaAdAnalytics.reportAdMetric(entry.getKey(), entry.getValue());
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bitmovin.analytics.conviva.ssai

import android.util.Log
import com.bitmovin.analytics.conviva.PlayerAdapter
import com.conviva.sdk.ConvivaAdAnalytics
import com.conviva.sdk.ConvivaSdkConstants
import com.conviva.sdk.ConvivaVideoAnalytics
Expand Down Expand Up @@ -29,13 +30,13 @@ import strikt.assertions.isTrue
class DefaultSsaiApiTest {
private val videoAnalytics: ConvivaVideoAnalytics = mockk(relaxed = true)
private val adAnalytics: ConvivaAdAnalytics = mockk()
private val playbackInfoProvider = mockk<PlaybackInfoProvider>()
private val playerAdapter = mockk<PlayerAdapter>()
private lateinit var ssaiApi: DefaultSsaiApi

@Before
fun beforeTest() {
every { playbackInfoProvider.playerState } returns ConvivaSdkConstants.PlayerState.PLAYING
every { playbackInfoProvider.playbackVideoData } returns hashMapOf<String, Array<Any>>(
every { playerAdapter.playerState } returns ConvivaSdkConstants.PlayerState.PLAYING
every { playerAdapter.playbackVideoData } returns hashMapOf<String, Array<Any>>(
ConvivaSdkConstants.PLAYBACK.BITRATE to arrayOf(1),
ConvivaSdkConstants.PLAYBACK.RESOLUTION to arrayOf(800, 1600),
ConvivaSdkConstants.PLAYBACK.RENDERED_FRAMERATE to arrayOf(60),
Expand All @@ -54,13 +55,13 @@ class DefaultSsaiApiTest {
ssaiApi = DefaultSsaiApi(
videoAnalytics,
adAnalytics,
playbackInfoProvider,
playerAdapter,
)
}

@After
fun afterTest() {
clearMocks(videoAnalytics, adAnalytics, playbackInfoProvider)
clearMocks(videoAnalytics, adAnalytics, playerAdapter)
}


Expand Down Expand Up @@ -131,7 +132,7 @@ class DefaultSsaiApiTest {

@Test
fun `reports ad playback state playing to conviva when ad starts while paused`() {
every { playbackInfoProvider.playerState } returns ConvivaSdkConstants.PlayerState.PAUSED
every { playerAdapter.playerState } returns ConvivaSdkConstants.PlayerState.PAUSED

ssaiApi.reportAdBreakStarted()
ssaiApi.reportAdStarted(SsaiApi.AdInfo())
Expand All @@ -146,7 +147,7 @@ class DefaultSsaiApiTest {

@Test
fun `reports ad playback state buffering to conviva when ad starts while stalling`() {
every { playbackInfoProvider.playerState } returns
every { playerAdapter.playerState } returns
ConvivaSdkConstants.PlayerState.BUFFERING

ssaiApi.reportAdBreakStarted()
Expand Down

0 comments on commit c72fe5e

Please sign in to comment.