Skip to content

Commit

Permalink
MediaController: Add missing event flags (1/2)
Browse files Browse the repository at this point in the history
This is the first commit out of two. This change adds the missing event
flags for the onEvents() callback when MediaController is connected to a
media3 session (see MediaControllerImplBase). I updated the
MediaControllerListenerTest and MediaControllerStateMaskingTest with
assertions that on onEvents() is called alongside individual
Player.Listener callbacks.

There will be a follow-up change for the case where a MediaController is
connected to a legacy MediaSession (MediaControllerImplLegacy). I've
split this in two separate changes to make the size of the commit
manageable for reviewing.

#minor-release

PiperOrigin-RevId: 481933437
  • Loading branch information
christosts authored and rohitjoins committed Oct 24, 2022
1 parent 93ee1f4 commit 46d5a0e
Show file tree
Hide file tree
Showing 9 changed files with 1,267 additions and 374 deletions.

Large diffs are not rendered by default.

Expand Up @@ -51,6 +51,7 @@ interface IRemoteMediaSession {
void setIsPlayingAd(String sessionId, boolean isPlayingAd);
void setCurrentAdGroupIndex(String sessionId, int currentAdGroupIndex);
void setCurrentAdIndexInAdGroup(String sessionId, int currentAdIndexInAdGroup);
void setVolume(String sessionId, float volume);
void notifyPlayerError(String sessionId, in Bundle playerErrorBundle);
void notifyPlayWhenReadyChanged(String sessionId, boolean playWhenReady, int reason);
void notifyPlaybackStateChanged(String sessionId, int state);
Expand Down Expand Up @@ -79,6 +80,8 @@ interface IRemoteMediaSession {
void notifySeekBackIncrementChanged(String sessionId, long seekBackIncrementMs);
void notifySeekForwardIncrementChanged(String sessionId, long seekForwardIncrementMs);
void notifyDeviceVolumeChanged(String sessionId, int volume, boolean muted);
void decreaseDeviceVolume(String sessionId);
void increaseDeviceVolume(String sessionId);
void notifyCuesChanged(String sessionId, in Bundle cueGroup);
void notifyDeviceInfoChanged(String sessionId, in Bundle deviceInfo);
void notifyMediaMetadataChanged(String sessionId, in Bundle mediaMetadata);
Expand Down
Expand Up @@ -24,8 +24,10 @@
import android.view.WindowManager;
import androidx.annotation.Nullable;
import androidx.annotation.UiThread;
import androidx.media3.common.Player;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.common.util.Util;
import com.google.common.collect.ImmutableList;
import java.util.Locale;

/** Provides utility methods for testing purpose. */
Expand Down Expand Up @@ -127,5 +129,19 @@ static void setKeepScreenOn(Activity activity) {
}
}

/**
* Returns an {@link ImmutableList} with the {@linkplain Player.Event Events} contained in {@code
* events}. The contents of the list are in matching order with the {@linkplain Player.Event
* Events} returned by {@link Player.Events#get(int)}.
*/
// TODO(b/254265256): Move this method off test-session-common.
public static ImmutableList<@Player.Event Integer> getEventsAsList(Player.Events events) {
ImmutableList.Builder<@Player.Event Integer> list = new ImmutableList.Builder<>();
for (int i = 0; i < events.size(); i++) {
list.add(events.get(i));
}
return list.build();
}

private TestUtils() {}
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Expand Up @@ -35,13 +35,15 @@
import androidx.media3.test.session.common.HandlerThreadTestRule;
import androidx.media3.test.session.common.PollingCheck;
import androidx.media3.test.session.common.SurfaceActivity;
import androidx.media3.test.session.common.TestUtils;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.filters.LargeTest;
import androidx.test.rule.ActivityTestRule;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
Expand Down Expand Up @@ -140,6 +142,7 @@ public static List<Object[]> params() {
private final CountDownLatch countDownLatch;
private final AtomicInteger newSurfaceWidthRef;
private final AtomicInteger newSurfaceHeightRef;
private final AtomicReference<Player.Events> eventsRef;

@Rule
public ActivityTestRule<SurfaceActivity> activityRule =
Expand Down Expand Up @@ -178,9 +181,10 @@ public MediaControllerSurfaceSizeChangeTest(
this.sizeCondition = sizeCondition;
this.surfaceSizeChangedShouldBeCalled = surfaceSizeChangedShouldBeCalled;

countDownLatch = new CountDownLatch(1);
countDownLatch = new CountDownLatch(2);
newSurfaceWidthRef = new AtomicInteger(C.LENGTH_UNSET);
newSurfaceHeightRef = new AtomicInteger(C.LENGTH_UNSET);
eventsRef = new AtomicReference<>();
}

@Test
Expand Down Expand Up @@ -252,6 +256,12 @@ public void onSurfaceSizeChanged(int width, int height) {
newSurfaceHeightRef.set(height);
countDownLatch.countDown();
}

@Override
public void onEvents(Player player, Player.Events events) {
eventsRef.set(events);
countDownLatch.countDown();
}
}));
}

Expand Down Expand Up @@ -360,6 +370,8 @@ private void waitCallbackAndAssert() throws Exception {
assertThat(countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(newSurfaceWidthRef.get()).isEqualTo(expectedWidthFromCallback);
assertThat(newSurfaceHeightRef.get()).isEqualTo(expectedHeightFromCallback);
assertThat(TestUtils.getEventsAsList(eventsRef.get()))
.containsExactly(Player.EVENT_SURFACE_SIZE_CHANGED);
} else {
assertThat(countDownLatch.await(NO_RESPONSE_TIMEOUT_MS, MILLISECONDS)).isFalse();
}
Expand Down
Expand Up @@ -857,6 +857,17 @@ public void setTrackSelectionParameters(String sessionId, Bundle parameters)
});
}

@Override
public void setVolume(String sessionId, float volume) throws RemoteException {
runOnHandler(
() -> {
MediaSession session = sessionMap.get(sessionId);
MockPlayer player = (MockPlayer) session.getPlayer();
player.setVolume(volume);
player.notifyVolumeChanged();
});
}

@Override
public void notifyAvailableCommandsChanged(String sessionId, Bundle commandsBundle)
throws RemoteException {
Expand Down Expand Up @@ -970,6 +981,28 @@ public void notifyDeviceVolumeChanged(String sessionId, int volume, boolean mute
});
}

@Override
public void decreaseDeviceVolume(String sessionId) throws RemoteException {
runOnHandler(
() -> {
MediaSession session = sessionMap.get(sessionId);
MockPlayer player = (MockPlayer) session.getPlayer();
player.decreaseDeviceVolume();
player.notifyDeviceVolumeChanged();
});
}

@Override
public void increaseDeviceVolume(String sessionId) throws RemoteException {
runOnHandler(
() -> {
MediaSession session = sessionMap.get(sessionId);
MockPlayer player = (MockPlayer) session.getPlayer();
player.increaseDeviceVolume();
player.notifyDeviceVolumeChanged();
});
}

@Override
public void notifyCuesChanged(String sessionId, Bundle cueGroupBundle) throws RemoteException {
CueGroup cueGroup = CueGroup.CREATOR.fromBundle(cueGroupBundle);
Expand Down
Expand Up @@ -655,11 +655,13 @@ public void setDeviceVolume(int volume) {

@Override
public void increaseDeviceVolume() {
deviceVolume += 1;
checkNotNull(conditionVariables.get(METHOD_INCREASE_DEVICE_VOLUME)).open();
}

@Override
public void decreaseDeviceVolume() {
deviceVolume -= 1;
checkNotNull(conditionVariables.get(METHOD_DECREASE_DEVICE_VOLUME)).open();
}

Expand Down Expand Up @@ -1204,6 +1206,12 @@ public void notifyDeviceVolumeChanged() {
}
}

public void notifyVolumeChanged() {
for (Listener listener : listeners) {
listener.onVolumeChanged(volume);
}
}

@SuppressWarnings("deprecation") // Implementing and calling deprecated listener method.
public void notifyCuesChanged() {
for (Listener listener : listeners) {
Expand Down
Expand Up @@ -282,6 +282,10 @@ public void setCurrentAdIndexInAdGroup(int currentAdIndexInAdGroup) throws Remot
binder.setCurrentAdIndexInAdGroup(sessionId, currentAdIndexInAdGroup);
}

public void setVolume(float volume) throws RemoteException {
binder.setVolume(sessionId, volume);
}

public void notifyPlayWhenReadyChanged(
boolean playWhenReady, @Player.PlaybackSuppressionReason int reason)
throws RemoteException {
Expand Down Expand Up @@ -399,6 +403,14 @@ public void notifyDeviceVolumeChanged(int volume, boolean muted) throws RemoteEx
binder.notifyDeviceVolumeChanged(sessionId, volume, muted);
}

public void decreaseDeviceVolume() throws RemoteException {
binder.decreaseDeviceVolume(sessionId);
}

public void increaseDeviceVolume() throws RemoteException {
binder.increaseDeviceVolume(sessionId);
}

public void notifyCuesChanged(CueGroup cueGroup) throws RemoteException {
binder.notifyCuesChanged(sessionId, cueGroup.toBundle());
}
Expand Down

0 comments on commit 46d5a0e

Please sign in to comment.