Skip to content

Commit

Permalink
Merge pull request #8 from flipkart-incubator/develop
Browse files Browse the repository at this point in the history
Merge develop to master
  • Loading branch information
anirudhramanan committed May 26, 2020
2 parents 49278aa + 3cbf13c commit 877b2b2
Show file tree
Hide file tree
Showing 70 changed files with 2,522 additions and 1,338 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class IMAPlayerActivity : AppCompatActivity() {
playerView = findViewById(R.id.video_view);
player = IMAPlayerManager(
this,
intent.extras?.get("url") as? String,
intent.extras?.get("response") as? String
intent.extras?.get("url").toString(),
intent.extras?.get("response").toString()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ import com.google.android.exoplayer2.source.smoothstreaming.SsMediaSource
import com.google.android.exoplayer2.ui.PlayerView
import com.google.android.exoplayer2.upstream.DataSource
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory
import com.google.android.exoplayer2.util.EventLogger
import com.google.android.exoplayer2.util.Util


class IMAPlayerManager(
context: Context,
url: String?,
Expand All @@ -50,6 +52,7 @@ class IMAPlayerManager(
playerView: PlayerView?
) { // Create a player instance.
player = ExoPlayerFactory.newSimpleInstance(context)
player?.addAnalyticsListener(EventLogger(null))
adsLoader.setPlayer(player)
playerView?.player = player

Expand Down Expand Up @@ -119,10 +122,13 @@ class IMAPlayerManager(
adsLoader = if (!TextUtils.isEmpty(url)) {
builder.buildForAdTag(Uri.parse(url))
} else {
val stringResponse = response ?: Utils.readFromAssets(
context,
"ad_response.xml"
)
var stringResponse = response ?: ""
if (TextUtils.isEmpty(stringResponse)) {
stringResponse = Utils.readFromAssets(
context,
"ad_response.xml"
)
}
builder.buildForAdsResponse(stringResponse)
}
dataSourceFactory = DefaultDataSourceFactory(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.flipkart.mediaads.demo.madman

import android.os.Bundle
import android.text.SpannableString
import androidx.appcompat.app.AppCompatActivity
import com.flipkart.mediaads.demo.R
import com.google.android.exoplayer2.ui.PlayerView
Expand All @@ -28,12 +29,12 @@ class MadmanPlayerActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_player)

playerView = findViewById(R.id.video_view);
playerView = findViewById(R.id.video_view)
player = MadmanPlayerManager(
this,
intent.extras?.get("url") as? String,
intent.extras?.get("response") as? String
);
intent.extras?.get("url").toString(),
intent.extras?.get("response").toString()
)
}

public override fun onResume() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import com.google.android.exoplayer2.source.smoothstreaming.SsMediaSource
import com.google.android.exoplayer2.ui.PlayerView
import com.google.android.exoplayer2.upstream.DataSource
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory
import com.google.android.exoplayer2.util.EventLogger
import com.google.android.exoplayer2.util.Util

class MadmanPlayerManager(
Expand Down Expand Up @@ -124,10 +125,13 @@ class MadmanPlayerManager(
adsLoader = if (!TextUtils.isEmpty(url)) {
builder.buildForAdUri(Uri.parse(url))
} else {
val stringResponse = response ?: readFromAssets(
context,
"ad_response.xml"
)
var stringResponse = response ?: ""
if (TextUtils.isEmpty(stringResponse)) {
stringResponse = readFromAssets(
context,
"ad_response.xml"
)
}
builder.buildForAdsResponse(stringResponse)
}
dataSourceFactory = DefaultDataSourceFactory(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ class MadmanAdLoader private constructor(
context: Context,
networkLayer: NetworkLayer,
private val adTagUri: Uri?,
private val adsResponse: String?
private val adsResponse: String?,
private val adEventListener: AdEventListener?,
private val adErrorListener: AdErrorListener?
) : Player.EventListener, AdsLoader, AdPlayer, ContentProgressProvider, AdErrorListener,
AdLoadListener, AdEventListener {
private var debug = true
Expand Down Expand Up @@ -167,6 +169,19 @@ class MadmanAdLoader private constructor(
*/
(private val context: Context, private val networkLayer: NetworkLayer) {

private var adEventListener: AdEventListener? = null
private var adErrorListener: AdErrorListener? = null

fun setAdEventListener(listener: AdEventListener): Builder {
this.adEventListener = listener
return this
}

fun setAdErrorListener(listener: AdErrorListener): Builder {
this.adErrorListener = listener
return this
}

/**
* Returns a new [MadmanAdLoader] for the specified ad tag.
*
Expand All @@ -180,7 +195,9 @@ class MadmanAdLoader private constructor(
context,
networkLayer,
adTagUri,
null
null,
adEventListener,
adErrorListener
)
}

Expand All @@ -196,7 +213,9 @@ class MadmanAdLoader private constructor(
context,
networkLayer,
null,
adsResponse
adsResponse,
adEventListener,
adErrorListener
)
}
}
Expand All @@ -215,8 +234,6 @@ class MadmanAdLoader private constructor(
adCallbacks = ArrayList(/* initialCapacity= */1)

madman = Madman.Builder()
.setAdErrorListener(this)
.setAdEventListener(this)
.setAdLoadListener(this)
.setNetworkLayer(networkLayer)
.build(context)
Expand Down Expand Up @@ -249,12 +266,10 @@ class MadmanAdLoader private constructor(
DefaultAdRenderer.Builder().setPlayer(this).setContainer(adViewGroup).build(null)

if (adTagUri != null) {
val request = NetworkAdRequest()
request.url = adTagUri.toString()
val request = NetworkAdRequest(adTagUri.toString())
madman.requestAds(request, adRenderer)
} else {
val request = StringAdRequest()
request.response = adsResponse
val request = StringAdRequest(adsResponse ?: "")
madman.requestAds(request, adRenderer)
}
}
Expand Down Expand Up @@ -360,6 +375,14 @@ class MadmanAdLoader private constructor(
override fun onAdManagerLoaded(manager: AdManager) {
pendingAdRequestContext = null
this.adsManager = manager
this.adsManager?.addAdEventListener(this)
this.adsManager?.addAdErrorListener(this)
if (adEventListener != null) {
this.adsManager?.addAdEventListener(adEventListener)
}
if (adErrorListener != null) {
this.adsManager?.addAdErrorListener(adErrorListener)
}
if (player != null) {
// If a player is attached already, start playback immediately.
try {
Expand All @@ -370,6 +393,10 @@ class MadmanAdLoader private constructor(
}
}

override fun onAdManagerLoadFailed(error: AdErrorListener.AdError) {
onAdError(error)
}

// AdEvent.AdEventListener implementation.

override fun onAdEvent(event: AdEventListener.AdEvent) {
Expand Down Expand Up @@ -416,31 +443,35 @@ class MadmanAdLoader private constructor(
if (player == null) {
return lastContentProgress ?: Progress.UNDEFINED
}
val hasContentDuration = contentDurationMs != C.TIME_UNSET
val hasContentDuration =
contentDurationMs != C.TIME_UNSET
val contentPositionMs: Long
if (pendingContentPositionMs != C.TIME_UNSET) {
sentPendingContentPositionMs = true
contentPositionMs = pendingContentPositionMs
expectedAdGroupIndex =
adPlaybackState.getAdGroupIndexForPositionUs(C.msToUs(contentPositionMs))
expectedAdGroupIndex = adPlaybackState.getAdGroupIndexForPositionUs(
C.msToUs(contentPositionMs)
)
} else if (fakeContentProgressElapsedRealtimeMs != C.TIME_UNSET) {
val elapsedSinceEndMs =
SystemClock.elapsedRealtime() - fakeContentProgressElapsedRealtimeMs
contentPositionMs = fakeContentProgressOffsetMs + elapsedSinceEndMs
expectedAdGroupIndex =
adPlaybackState.getAdGroupIndexForPositionUs(C.msToUs(contentPositionMs))
} else if (sentContentComplete) {
contentPositionMs = this.contentDurationMs
expectedAdGroupIndex = adPlaybackState.getAdGroupIndexForPositionUs(
C.msToUs(contentPositionMs)
)
} else if (adState == AD_STATE_NONE && !playingAd && hasContentDuration) {
contentPositionMs = player?.currentPosition ?: 0
// Update the expected ad group index for the current content position. The update is delayed
// until MAXIMUM_PRELOAD_DURATION_MS before the ad so that an ad group load error delivered
// just after an ad group isn't incorrectly attributed to the next ad group.
// until MAXIMUM_PRELOAD_DURATION_MS before the ad so that an ad group load error delivered
// just after an ad group isn't incorrectly attributed to the next ad group.
val nextAdGroupIndex = adPlaybackState.getAdGroupIndexAfterPositionUs(
C.msToUs(contentPositionMs), C.msToUs(contentDurationMs)
C.msToUs(contentPositionMs),
C.msToUs(contentDurationMs)
)
if (nextAdGroupIndex != expectedAdGroupIndex && nextAdGroupIndex != C.INDEX_UNSET) {
var nextAdGroupTimeMs = C.usToMs(adPlaybackState.adGroupTimesUs[nextAdGroupIndex])
var nextAdGroupTimeMs = C.usToMs(
adPlaybackState.adGroupTimesUs[nextAdGroupIndex]
)
if (nextAdGroupTimeMs == C.TIME_END_OF_SOURCE) {
nextAdGroupTimeMs = contentDurationMs
}
Expand All @@ -449,11 +480,10 @@ class MadmanAdLoader private constructor(
}
}
} else {
return Progress(-1, -1)
return Progress.UNDEFINED
}
val contentDurationMs =
if (hasContentDuration) this.contentDurationMs else DURATION_UNSET

if (hasContentDuration) contentDurationMs else DURATION_UNSET
return Progress(contentPositionMs, contentDurationMs)
}

Expand Down Expand Up @@ -647,6 +677,10 @@ class MadmanAdLoader private constructor(
}

override fun onPositionDiscontinuity(@Player.DiscontinuityReason reason: Int) {
Log.d(
TAG,
"onTimelineChanged/onPositionDiscontinuity"
)
if (adsManager == null) {
return
}
Expand Down Expand Up @@ -953,7 +987,7 @@ class MadmanAdLoader private constructor(
private fun getAdIndexInAdGroupToLoad(adGroupIndex: Int): Int {
@AdState val states = adPlaybackState.adGroups?.get(adGroupIndex)?.states ?: IntArray(0)
var adIndexInAdGroup = 0
while ((adIndexInAdGroup < states.size && states[adIndexInAdGroup] != AdPlaybackState.AD_STATE_UNAVAILABLE)) {
while (adIndexInAdGroup < states.size && states[adIndexInAdGroup] != AdPlaybackState.AD_STATE_UNAVAILABLE) {
adIndexInAdGroup++
}
return if (adIndexInAdGroup == states.size) C.INDEX_UNSET else adIndexInAdGroup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class DefaultNetworkLayerTest {
doAnswer(answer).`when`(mockOkHttpCall).enqueue(anyObject())

networkLayer.fetch(
NetworkAdRequest().apply { url = "https://www.google.com" },
NetworkAdRequest("").apply { url = "https://www.google.com" },
mockNetworkListener,
mockCancellationSignal
)
Expand Down Expand Up @@ -107,7 +107,7 @@ class DefaultNetworkLayerTest {
doAnswer(answer).`when`(mockOkHttpCall).enqueue(anyObject())

networkLayer.fetch(
NetworkAdRequest().apply { url = "https://www.google.com" },
NetworkAdRequest("").apply { url = "https://www.google.com" },
mockNetworkListener,
mockCancellationSignal
)
Expand Down Expand Up @@ -142,7 +142,7 @@ class DefaultNetworkLayerTest {
doAnswer(answer).`when`(mockOkHttpCall).enqueue(anyObject())

networkLayer.fetch(
NetworkAdRequest().apply { url = "https://www.google.com" },
NetworkAdRequest("").apply { url = "https://www.google.com" },
mockNetworkListener,
mockCancellationSignal
)
Expand Down
Loading

0 comments on commit 877b2b2

Please sign in to comment.