Skip to content

Commit

Permalink
fix: remove warning and refactor & fix ad workflow (#4235)
Browse files Browse the repository at this point in the history
  • Loading branch information
freeboub authored Oct 17, 2024
1 parent f04b233 commit 7501880
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 73 deletions.
3 changes: 0 additions & 3 deletions android/src/main/java/com/brentvatne/common/api/AdsProps.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.brentvatne.common.api

import android.net.Uri
import android.text.TextUtils
import com.brentvatne.common.toolbox.DebugLog
import com.brentvatne.common.toolbox.ReactBridgeUtils
import com.facebook.react.bridge.ReadableMap

Expand All @@ -26,8 +25,6 @@ class AdsProps {
@JvmStatic
fun parse(src: ReadableMap?): AdsProps {
val adsProps = AdsProps()
DebugLog.w("olivier", "uri: parse AdsProps")

if (src != null) {
val uriString = ReactBridgeUtils.safeGetString(src, PROP_AD_TAG_URL)
if (TextUtils.isEmpty(uriString)) {
Expand Down
4 changes: 2 additions & 2 deletions android/src/main/java/com/brentvatne/common/api/CMCDProps.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ data class CMCDProps(

return (0 until array.size()).mapNotNull { i ->
val item = array.getMap(i)
val key = item?.getString("key")
val value = when (item?.getType("value")) {
val key = item.getString("key")
val value = when (item.getType("value")) {
ReadableType.Number -> item.getDouble("value")
ReadableType.String -> item.getString("value")
else -> null
Expand Down
28 changes: 12 additions & 16 deletions android/src/main/java/com/brentvatne/exoplayer/ExoPlayerView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ExoPlayerView(private val context: Context) :
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
private var adOverlayFrameLayout: FrameLayout
private var adOverlayFrameLayout: FrameLayout? = null
val isPlaying: Boolean
get() = player != null && player?.isPlaying == true

Expand Down Expand Up @@ -72,12 +72,9 @@ class ExoPlayerView(private val context: Context) :

updateSurfaceView(viewType)

adOverlayFrameLayout = FrameLayout(context)

layout.addView(shutterView, 1, layoutParams)
if (localStyle.subtitlesFollowVideo) {
layout.addView(subtitleLayout, layoutParams)
layout.addView(adOverlayFrameLayout, layoutParams)
}

addViewInLayout(layout, 0, aspectRatioParams)
Expand Down Expand Up @@ -194,22 +191,21 @@ class ExoPlayerView(private val context: Context) :
}
}

private fun hideShutterView() {
shutterView.setVisibility(INVISIBLE)
surfaceView?.setAlpha(1f)
}

private fun showShutterView() {
shutterView.setVisibility(VISIBLE)
surfaceView?.setAlpha(0f)
}

var adsShown = false
fun showAds() {
adOverlayFrameLayout.setVisibility(View.VISIBLE)
if (!adsShown) {
adOverlayFrameLayout = FrameLayout(context)
layout.addView(adOverlayFrameLayout, layoutParams)
adsShown = true
}
}

fun hideAds() {
adOverlayFrameLayout.setVisibility(View.GONE)
if (adsShown) {
layout.removeView(adOverlayFrameLayout)
adOverlayFrameLayout = null
adsShown = false
}
}

fun updateShutterViewVisibility() {
Expand Down
100 changes: 48 additions & 52 deletions android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@
import androidx.media3.exoplayer.source.MediaSource;
import androidx.media3.exoplayer.source.MergingMediaSource;
import androidx.media3.exoplayer.source.ProgressiveMediaSource;
import androidx.media3.exoplayer.source.SingleSampleMediaSource;
import androidx.media3.exoplayer.source.TrackGroupArray;
import androidx.media3.exoplayer.source.ads.AdsMediaSource;
import androidx.media3.exoplayer.trackselection.AdaptiveTrackSelection;
Expand Down Expand Up @@ -827,23 +826,6 @@ private void initializePlayerCore(ReactExoplayerView self) {
mediaSourceFactory.setDataSourceFactory(RNVSimpleCache.INSTANCE.getCacheFactory(buildHttpDataSourceFactory(true)));
}

if (BuildConfig.USE_EXOPLAYER_IMA) {
AdsProps adProps = source.getAdsProps();

// Create an AdsLoader.
ImaAdsLoader.Builder imaLoaderBuilder = new ImaAdsLoader
.Builder(themedReactContext)
.setAdEventListener(this)
.setAdErrorListener(this);

if (adProps != null && adProps.getAdLanguage() != null) {
ImaSdkSettings imaSdkSettings = ImaSdkFactory.getInstance().createImaSdkSettings();
imaSdkSettings.setLanguage(adProps.getAdLanguage());
imaLoaderBuilder.setImaSdkSettings(imaSdkSettings);
}
adsLoader = imaLoaderBuilder.build();
}

mediaSourceFactory.setLocalAdInsertionComponents(unusedAdTagUri -> adsLoader, exoPlayerView);

player = new ExoPlayer.Builder(getContext(), renderersFactory)
Expand All @@ -858,9 +840,6 @@ private void initializePlayerCore(ReactExoplayerView self) {
player.setVolume(muted ? 0.f : audioVolume * 1);
exoPlayerView.setPlayer(player);

if (adsLoader != null) {
adsLoader.setPlayer(player);
}
audioBecomingNoisyReceiver.setListener(self);
bandwidthMeter.addEventListener(new Handler(), self);
setPlayWhenReady(!isPaused);
Expand All @@ -875,6 +854,41 @@ private void initializePlayerCore(ReactExoplayerView self) {
}
}

private AdsMediaSource initializeAds(MediaSource videoSource, Source runningSource) {
AdsProps adProps = runningSource.getAdsProps();
Uri uri = runningSource.getUri();
if (adProps != null && uri != null) {
Uri adTagUrl = adProps.getAdTagUrl();
if (adTagUrl != null) {
exoPlayerView.showAds();
// Create an AdsLoader.
ImaAdsLoader.Builder imaLoaderBuilder = new ImaAdsLoader
.Builder(themedReactContext)
.setAdEventListener(this)
.setAdErrorListener(this);

if (adProps.getAdLanguage() != null) {
ImaSdkSettings imaSdkSettings = ImaSdkFactory.getInstance().createImaSdkSettings();
imaSdkSettings.setLanguage(adProps.getAdLanguage());
imaLoaderBuilder.setImaSdkSettings(imaSdkSettings);
}
adsLoader = imaLoaderBuilder.build();
adsLoader.setPlayer(player);
if (adsLoader != null) {
DefaultMediaSourceFactory mediaSourceFactory = new DefaultMediaSourceFactory(mediaDataSourceFactory)
.setLocalAdInsertionComponents(unusedAdTagUri -> adsLoader, exoPlayerView);
DataSpec adTagDataSpec = new DataSpec(adTagUrl);
return new AdsMediaSource(videoSource,
adTagDataSpec,
ImmutableList.of(uri, adTagUrl),
mediaSourceFactory, adsLoader, exoPlayerView);
}
}
}
exoPlayerView.hideAds();
return null;
}

private DrmSessionManager initializePlayerDrm() {
DrmSessionManager drmSessionManager = null;
DRMProps drmProps = source.getDrmProps();
Expand Down Expand Up @@ -908,32 +922,17 @@ private void initializePlayerSource(Source runningSource) {
return;
}
// init source to manage ads and external text tracks
MediaSource subtitlesSource = buildTextSource();
MediaSource videoSource = buildMediaSource(runningSource.getUri(), runningSource.getExtension(), drmSessionManager, runningSource.getCropStartMs(), runningSource.getCropEndMs());
MediaSource mediaSourceWithAds = null;
Uri adTagUrl = null;
if (source.getAdsProps() != null) {
adTagUrl = source.getAdsProps().getAdTagUrl();
}
if (adTagUrl != null && adsLoader != null) {
DefaultMediaSourceFactory mediaSourceFactory = new DefaultMediaSourceFactory(mediaDataSourceFactory)
.setLocalAdInsertionComponents(unusedAdTagUri -> adsLoader, exoPlayerView);
DataSpec adTagDataSpec = new DataSpec(adTagUrl);
DebugLog.w(TAG, "ads " + adTagUrl);
mediaSourceWithAds = new AdsMediaSource(videoSource, adTagDataSpec, ImmutableList.of(runningSource.getUri(), adTagUrl), mediaSourceFactory, adsLoader, exoPlayerView);
exoPlayerView.showAds();
}
MediaSource mediaSource;
if (subtitlesSource == null) {
mediaSource = Objects.requireNonNullElse(mediaSourceWithAds, videoSource);
} else {
ArrayList<MediaSource> mediaSourceList = new ArrayList<>();
mediaSourceList.add(subtitlesSource);
mediaSourceList.add(0, Objects.requireNonNullElse(mediaSourceWithAds, videoSource));
MediaSource[] mediaSourceArray = mediaSourceList.toArray(
new MediaSource[mediaSourceList.size()]
);
MediaSource videoSource = buildMediaSource(runningSource.getUri(),
runningSource.getExtension(),
drmSessionManager,
runningSource.getCropStartMs(),
runningSource.getCropEndMs());
MediaSource mediaSourceWithAds = initializeAds(videoSource, runningSource);
MediaSource mediaSource = Objects.requireNonNullElse(mediaSourceWithAds, videoSource);

MediaSource subtitlesSource = buildTextSource();
if (subtitlesSource != null) {
MediaSource[] mediaSourceArray = {mediaSource, subtitlesSource};
mediaSource = new MergingMediaSource(mediaSourceArray);
}

Expand Down Expand Up @@ -1132,6 +1131,7 @@ private MediaSource buildMediaSource(Uri uri, String overrideExtension, DrmSessi
drmProvider = new DefaultDrmSessionManagerProvider();
}


switch (type) {
case CONTENT_TYPE_SS:
if(!BuildConfig.USE_EXOPLAYER_SMOOTH_STREAMING) {
Expand Down Expand Up @@ -1905,7 +1905,8 @@ public void setSrc(Source source) {
}

if (!isSourceEqual) {
reloadSource();
playerNeedsSource = true;
initializePlayer();
}
} else {
clearSrc();
Expand All @@ -1932,11 +1933,6 @@ public void setReportBandwidth(boolean reportBandwidth) {
mReportBandwidth = reportBandwidth;
}

private void reloadSource() {
playerNeedsSource = true;
initializePlayer();
}

public void setResizeModeModifier(@ResizeMode.Mode int resizeMode) {
if (exoPlayerView != null) {
exoPlayerView.setResizeMode(resizeMode);
Expand Down

0 comments on commit 7501880

Please sign in to comment.