Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
SDK
/
exoplayer
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
7b5dcb37
authored
Dec 15, 2021
by
ibaker
Committed by
tonihei
Jan 05, 2022
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Move ImaAdsLoader Player.Listener implementation to internal class
PiperOrigin-RevId: 416613846
parent
47652c48
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
66 additions
and
61 deletions
libraries/exoplayer_ima/src/main/java/androidx/media3/exoplayer/ima/ImaAdsLoader.java
libraries/exoplayer_ima/src/test/java/androidx/media3/exoplayer/ima/FakeExoPlayer.java
libraries/exoplayer_ima/src/test/java/androidx/media3/exoplayer/ima/ImaAdsLoaderTest.java
libraries/exoplayer_ima/src/main/java/androidx/media3/exoplayer/ima/ImaAdsLoader.java
View file @
7b5dcb37
...
@@ -88,7 +88,7 @@ import java.util.Set;
...
@@ -88,7 +88,7 @@ import java.util.Set;
* SDK Open Measurement documentation</a> for more information.
* SDK Open Measurement documentation</a> for more information.
*/
*/
@UnstableApi
@UnstableApi
public
final
class
ImaAdsLoader
implements
Player
.
Listener
,
AdsLoader
{
public
final
class
ImaAdsLoader
implements
AdsLoader
{
static
{
static
{
MediaLibraryInfo
.
registerModule
(
"media3.exoplayer.ima"
);
MediaLibraryInfo
.
registerModule
(
"media3.exoplayer.ima"
);
...
@@ -388,6 +388,7 @@ public final class ImaAdsLoader implements Player.Listener, AdsLoader {
...
@@ -388,6 +388,7 @@ public final class ImaAdsLoader implements Player.Listener, AdsLoader {
private
final
ImaUtil
.
Configuration
configuration
;
private
final
ImaUtil
.
Configuration
configuration
;
private
final
Context
context
;
private
final
Context
context
;
private
final
ImaUtil
.
ImaFactory
imaFactory
;
private
final
ImaUtil
.
ImaFactory
imaFactory
;
private
final
PlayerListenerImpl
playerListener
;
private
final
HashMap
<
Object
,
AdTagLoader
>
adTagLoaderByAdsId
;
private
final
HashMap
<
Object
,
AdTagLoader
>
adTagLoaderByAdsId
;
private
final
HashMap
<
AdsMediaSource
,
AdTagLoader
>
adTagLoaderByAdsMediaSource
;
private
final
HashMap
<
AdsMediaSource
,
AdTagLoader
>
adTagLoaderByAdsMediaSource
;
private
final
Timeline
.
Period
period
;
private
final
Timeline
.
Period
period
;
...
@@ -404,6 +405,7 @@ public final class ImaAdsLoader implements Player.Listener, AdsLoader {
...
@@ -404,6 +405,7 @@ public final class ImaAdsLoader implements Player.Listener, AdsLoader {
this
.
context
=
context
.
getApplicationContext
();
this
.
context
=
context
.
getApplicationContext
();
this
.
configuration
=
configuration
;
this
.
configuration
=
configuration
;
this
.
imaFactory
=
imaFactory
;
this
.
imaFactory
=
imaFactory
;
playerListener
=
new
PlayerListenerImpl
();
supportedMimeTypes
=
ImmutableList
.
of
();
supportedMimeTypes
=
ImmutableList
.
of
();
adTagLoaderByAdsId
=
new
HashMap
<>();
adTagLoaderByAdsId
=
new
HashMap
<>();
adTagLoaderByAdsMediaSource
=
new
HashMap
<>();
adTagLoaderByAdsMediaSource
=
new
HashMap
<>();
...
@@ -534,7 +536,7 @@ public final class ImaAdsLoader implements Player.Listener, AdsLoader {
...
@@ -534,7 +536,7 @@ public final class ImaAdsLoader implements Player.Listener, AdsLoader {
if
(
player
==
null
)
{
if
(
player
==
null
)
{
return
;
return
;
}
}
player
.
addListener
(
this
);
player
.
addListener
(
playerListener
);
}
}
@Nullable
AdTagLoader
adTagLoader
=
adTagLoaderByAdsId
.
get
(
adsId
);
@Nullable
AdTagLoader
adTagLoader
=
adTagLoaderByAdsId
.
get
(
adsId
);
...
@@ -556,7 +558,7 @@ public final class ImaAdsLoader implements Player.Listener, AdsLoader {
...
@@ -556,7 +558,7 @@ public final class ImaAdsLoader implements Player.Listener, AdsLoader {
}
}
if
(
player
!=
null
&&
adTagLoaderByAdsMediaSource
.
isEmpty
())
{
if
(
player
!=
null
&&
adTagLoaderByAdsMediaSource
.
isEmpty
())
{
player
.
removeListener
(
this
);
player
.
removeListener
(
playerListener
);
player
=
null
;
player
=
null
;
}
}
}
}
...
@@ -564,7 +566,7 @@ public final class ImaAdsLoader implements Player.Listener, AdsLoader {
...
@@ -564,7 +566,7 @@ public final class ImaAdsLoader implements Player.Listener, AdsLoader {
@Override
@Override
public
void
release
()
{
public
void
release
()
{
if
(
player
!=
null
)
{
if
(
player
!=
null
)
{
player
.
removeListener
(
this
);
player
.
removeListener
(
playerListener
);
player
=
null
;
player
=
null
;
maybeUpdateCurrentAdTagLoader
();
maybeUpdateCurrentAdTagLoader
();
}
}
...
@@ -604,37 +606,6 @@ public final class ImaAdsLoader implements Player.Listener, AdsLoader {
...
@@ -604,37 +606,6 @@ public final class ImaAdsLoader implements Player.Listener, AdsLoader {
.
handlePrepareError
(
adGroupIndex
,
adIndexInAdGroup
,
exception
);
.
handlePrepareError
(
adGroupIndex
,
adIndexInAdGroup
,
exception
);
}
}
// Player.Listener implementation.
@Override
public
void
onTimelineChanged
(
Timeline
timeline
,
@Player
.
TimelineChangeReason
int
reason
)
{
if
(
timeline
.
isEmpty
())
{
// The player is being reset or contains no media.
return
;
}
maybeUpdateCurrentAdTagLoader
();
maybePreloadNextPeriodAds
();
}
@Override
public
void
onPositionDiscontinuity
(
Player
.
PositionInfo
oldPosition
,
Player
.
PositionInfo
newPosition
,
@Player
.
DiscontinuityReason
int
reason
)
{
maybeUpdateCurrentAdTagLoader
();
maybePreloadNextPeriodAds
();
}
@Override
public
void
onShuffleModeEnabledChanged
(
boolean
shuffleModeEnabled
)
{
maybePreloadNextPeriodAds
();
}
@Override
public
void
onRepeatModeChanged
(
@Player
.
RepeatMode
int
repeatMode
)
{
maybePreloadNextPeriodAds
();
}
// Internal methods.
// Internal methods.
private
void
maybeUpdateCurrentAdTagLoader
()
{
private
void
maybeUpdateCurrentAdTagLoader
()
{
...
@@ -674,7 +645,7 @@ public final class ImaAdsLoader implements Player.Listener, AdsLoader {
...
@@ -674,7 +645,7 @@ public final class ImaAdsLoader implements Player.Listener, AdsLoader {
}
}
private
void
maybePreloadNextPeriodAds
()
{
private
void
maybePreloadNextPeriodAds
()
{
@Nullable
Player
player
=
this
.
player
;
@Nullable
Player
player
=
ImaAdsLoader
.
this
.
player
;
if
(
player
==
null
)
{
if
(
player
==
null
)
{
return
;
return
;
}
}
...
@@ -708,6 +679,38 @@ public final class ImaAdsLoader implements Player.Listener, AdsLoader {
...
@@ -708,6 +679,38 @@ public final class ImaAdsLoader implements Player.Listener, AdsLoader {
nextAdTagLoader
.
maybePreloadAds
(
Util
.
usToMs
(
periodPositionUs
),
Util
.
usToMs
(
period
.
durationUs
));
nextAdTagLoader
.
maybePreloadAds
(
Util
.
usToMs
(
periodPositionUs
),
Util
.
usToMs
(
period
.
durationUs
));
}
}
private
final
class
PlayerListenerImpl
implements
Player
.
Listener
{
@Override
public
void
onTimelineChanged
(
Timeline
timeline
,
@Player
.
TimelineChangeReason
int
reason
)
{
if
(
timeline
.
isEmpty
())
{
// The player is being reset or contains no media.
return
;
}
maybeUpdateCurrentAdTagLoader
();
maybePreloadNextPeriodAds
();
}
@Override
public
void
onPositionDiscontinuity
(
Player
.
PositionInfo
oldPosition
,
Player
.
PositionInfo
newPosition
,
@Player
.
DiscontinuityReason
int
reason
)
{
maybeUpdateCurrentAdTagLoader
();
maybePreloadNextPeriodAds
();
}
@Override
public
void
onShuffleModeEnabledChanged
(
boolean
shuffleModeEnabled
)
{
maybePreloadNextPeriodAds
();
}
@Override
public
void
onRepeatModeChanged
(
@Player
.
RepeatMode
int
repeatMode
)
{
maybePreloadNextPeriodAds
();
}
}
/**
/**
* Default {@link ImaUtil.ImaFactory} for non-test usage, which delegates to {@link
* Default {@link ImaUtil.ImaFactory} for non-test usage, which delegates to {@link
* ImaSdkFactory}.
* ImaSdkFactory}.
...
...
libraries/exoplayer_ima/src/test/java/androidx/media3/exoplayer/ima/FakeExoPlayer.java
View file @
7b5dcb37
...
@@ -15,9 +15,12 @@
...
@@ -15,9 +15,12 @@
*/
*/
package
androidx
.
media3
.
exoplayer
.
ima
;
package
androidx
.
media3
.
exoplayer
.
ima
;
import
static
androidx
.
media3
.
common
.
util
.
Assertions
.
checkState
;
import
android.os.Looper
;
import
android.os.Looper
;
import
androidx.media3.common.C
;
import
androidx.media3.common.C
;
import
androidx.media3.common.MediaItem
;
import
androidx.media3.common.MediaItem
;
import
androidx.media3.common.PlaybackException
;
import
androidx.media3.common.Player
;
import
androidx.media3.common.Player
;
import
androidx.media3.common.Timeline
;
import
androidx.media3.common.Timeline
;
import
androidx.media3.common.TrackSelectionArray
;
import
androidx.media3.common.TrackSelectionArray
;
...
@@ -182,6 +185,29 @@ import androidx.media3.test.utils.StubExoPlayer;
...
@@ -182,6 +185,29 @@ import androidx.media3.test.utils.StubExoPlayer;
}
}
}
}
/**
* Sets an error on this player.
*
* <p>This will propagate the error to {@link Player.Listener#onPlayerError(PlaybackException)}
* and {@link Player.Listener#onPlayerErrorChanged(PlaybackException)} and will also update the
* state to {@link Player#STATE_IDLE}.
*
* <p>The player must be in {@link #STATE_BUFFERING} or {@link #STATE_READY}.
*/
@SuppressWarnings
(
"deprecation"
)
// Calling deprecated listener.onPlayerStateChanged()
public
void
setPlayerError
(
PlaybackException
error
)
{
checkState
(
state
==
STATE_BUFFERING
||
state
==
STATE_READY
);
this
.
state
=
Player
.
STATE_IDLE
;
listeners
.
sendEvent
(
Player
.
EVENT_PLAYBACK_STATE_CHANGED
,
listener
->
{
listener
.
onPlayerError
(
error
);
listener
.
onPlayerErrorChanged
(
error
);
listener
.
onPlayerStateChanged
(
playWhenReady
,
state
);
listener
.
onPlaybackStateChanged
(
state
);
});
}
// ExoPlayer methods. Other methods are unsupported.
// ExoPlayer methods. Other methods are unsupported.
@Override
@Override
...
...
libraries/exoplayer_ima/src/test/java/androidx/media3/exoplayer/ima/ImaAdsLoaderTest.java
View file @
7b5dcb37
...
@@ -42,7 +42,6 @@ import androidx.media3.common.AdOverlayInfo;
...
@@ -42,7 +42,6 @@ import androidx.media3.common.AdOverlayInfo;
import
androidx.media3.common.AdPlaybackState
;
import
androidx.media3.common.AdPlaybackState
;
import
androidx.media3.common.AdViewProvider
;
import
androidx.media3.common.AdViewProvider
;
import
androidx.media3.common.C
;
import
androidx.media3.common.C
;
import
androidx.media3.common.MediaItem
;
import
androidx.media3.common.MimeTypes
;
import
androidx.media3.common.MimeTypes
;
import
androidx.media3.common.PlaybackException
;
import
androidx.media3.common.PlaybackException
;
import
androidx.media3.common.Player
;
import
androidx.media3.common.Player
;
...
@@ -276,30 +275,7 @@ public final class ImaAdsLoaderTest {
...
@@ -276,30 +275,7 @@ public final class ImaAdsLoaderTest {
ExoPlaybackException
anException
=
ExoPlaybackException
anException
=
ExoPlaybackException
.
createForSource
(
ExoPlaybackException
.
createForSource
(
new
IOException
(),
PlaybackException
.
ERROR_CODE_IO_UNSPECIFIED
);
new
IOException
(),
PlaybackException
.
ERROR_CODE_IO_UNSPECIFIED
);
imaAdsLoader
.
onPlayerErrorChanged
(
anException
);
fakePlayer
.
setPlayerError
(
anException
);
imaAdsLoader
.
onPlayerError
(
anException
);
imaAdsLoader
.
onPositionDiscontinuity
(
new
Player
.
PositionInfo
(
/* windowUid= */
new
Object
(),
/* windowIndex= */
0
,
/* mediaItem= */
MediaItem
.
fromUri
(
"http://google.com/0"
),
/* periodUid= */
new
Object
(),
/* periodIndex= */
0
,
/* positionMs= */
10_000
,
/* contentPositionMs= */
0
,
/* adGroupIndex= */
-
1
,
/* adIndexInAdGroup= */
-
1
),
new
Player
.
PositionInfo
(
/* windowUid= */
new
Object
(),
/* windowIndex= */
1
,
/* mediaItem= */
MediaItem
.
fromUri
(
"http://google.com/1"
),
/* periodUid= */
new
Object
(),
/* periodIndex= */
0
,
/* positionMs= */
20_000
,
/* contentPositionMs= */
0
,
/* adGroupIndex= */
-
1
,
/* adIndexInAdGroup= */
-
1
),
Player
.
DISCONTINUITY_REASON_SEEK
);
adEventListener
.
onAdEvent
(
getAdEvent
(
AdEventType
.
CONTENT_RESUME_REQUESTED
,
/* ad= */
null
));
adEventListener
.
onAdEvent
(
getAdEvent
(
AdEventType
.
CONTENT_RESUME_REQUESTED
,
/* ad= */
null
));
imaAdsLoader
.
handlePrepareError
(
imaAdsLoader
.
handlePrepareError
(
adsMediaSource
,
/* adGroupIndex= */
0
,
/* adIndexInAdGroup= */
0
,
new
IOException
());
adsMediaSource
,
/* adGroupIndex= */
0
,
/* adIndexInAdGroup= */
0
,
new
IOException
());
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment