|
26 | 26 | import android.content.Context;
|
27 | 27 | import android.os.Bundle;
|
28 | 28 | import android.os.Handler;
|
| 29 | +import android.os.HandlerThread; |
29 | 30 | import android.os.Looper;
|
30 | 31 | import android.support.v4.media.session.MediaControllerCompat;
|
31 | 32 | import android.support.v4.media.session.MediaSessionCompat;
|
32 | 33 | import android.support.v4.media.session.PlaybackStateCompat;
|
| 34 | +import androidx.core.content.ContextCompat; |
33 | 35 | import androidx.media3.common.ForwardingPlayer;
|
34 | 36 | import androidx.media3.common.MediaItem;
|
| 37 | +import androidx.media3.common.PlaybackParameters; |
35 | 38 | import androidx.media3.common.Player;
|
36 | 39 | import androidx.media3.common.util.ConditionVariable;
|
37 | 40 | import androidx.media3.exoplayer.ExoPlayer;
|
|
47 | 50 | import androidx.test.filters.MediumTest;
|
48 | 51 | import com.google.common.collect.ImmutableList;
|
49 | 52 | import com.google.common.collect.Lists;
|
| 53 | +import com.google.common.util.concurrent.FutureCallback; |
| 54 | +import com.google.common.util.concurrent.Futures; |
50 | 55 | import com.google.common.util.concurrent.ListenableFuture;
|
51 | 56 | import com.google.common.util.concurrent.SettableFuture;
|
52 | 57 | import java.util.ArrayList;
|
@@ -242,6 +247,70 @@ public ListenableFuture<List<MediaItem>> onAddMediaItems(
|
242 | 247 | service.blockUntilAllControllersUnbind(TIMEOUT_MS);
|
243 | 248 | }
|
244 | 249 |
|
| 250 | + @Test |
| 251 | + public void onPlayRequested_doesNotCauseDeadlock_ifPlaybackThreadIsNotMain() throws Exception { |
| 252 | + HandlerThread ht = new HandlerThread("MSSTest:PlaybackThread"); |
| 253 | + ht.start(); |
| 254 | + TestServiceRegistry testServiceRegistry = TestServiceRegistry.getInstance(); |
| 255 | + ConditionVariable playerToldToSpeedUp = new ConditionVariable(); |
| 256 | + AtomicReference<MediaController> mediaController = new AtomicReference<>(); |
| 257 | + AtomicReference<MediaSession> mediaSession = new AtomicReference<>(); |
| 258 | + testServiceRegistry.setOnGetSessionHandler( |
| 259 | + controllerInfo -> { |
| 260 | + MockMediaSessionService service = |
| 261 | + (MockMediaSessionService) testServiceRegistry.getServiceInstance(); |
| 262 | + Player player = new ExoPlayer.Builder(service).setLooper(ht.getLooper()).build(); |
| 263 | + player.addListener( |
| 264 | + new Player.Listener() { |
| 265 | + @Override |
| 266 | + public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) { |
| 267 | + if (playbackParameters.speed == 2f) { |
| 268 | + playerToldToSpeedUp.open(); |
| 269 | + } |
| 270 | + } |
| 271 | + }); |
| 272 | + mediaSession.set(new MediaSession.Builder(service, player).build()); |
| 273 | + return mediaSession.get(); |
| 274 | + }); |
| 275 | + TestHandler handler = new TestHandler(Looper.getMainLooper()); |
| 276 | + TestHandler bgHandler = new TestHandler(ht.getLooper()); |
| 277 | + handler.post( |
| 278 | + () -> { |
| 279 | + ListenableFuture<MediaController> controllerFuture = |
| 280 | + new MediaController.Builder(context, token).buildAsync(); |
| 281 | + Futures.addCallback( |
| 282 | + controllerFuture, |
| 283 | + new FutureCallback<MediaController>() { |
| 284 | + @Override |
| 285 | + public void onSuccess(MediaController controller) { |
| 286 | + controller.addMediaItem(new MediaItem.Builder().setMediaId("media_id").build()); |
| 287 | + controller.play(); |
| 288 | + bgHandler.post( |
| 289 | + () -> |
| 290 | + handler.post( |
| 291 | + () -> { |
| 292 | + controller.setPlaybackSpeed(2f); |
| 293 | + mediaController.set(controller); |
| 294 | + })); |
| 295 | + } |
| 296 | + |
| 297 | + @Override |
| 298 | + public void onFailure(Throwable t) { |
| 299 | + throw new RuntimeException(t); |
| 300 | + } |
| 301 | + }, |
| 302 | + ContextCompat.getMainExecutor(context)); |
| 303 | + }); |
| 304 | + playerToldToSpeedUp.block(TIMEOUT_MS); |
| 305 | + if (!playerToldToSpeedUp.isOpen()) { |
| 306 | + ht.interrupt(); // avoid deadlocking the test forever. |
| 307 | + } |
| 308 | + assertThat(playerToldToSpeedUp.isOpen()).isTrue(); |
| 309 | + handler.postAndSync(() -> mediaController.get().release()); |
| 310 | + mediaSession.get().release(); |
| 311 | + ht.quitSafely(); |
| 312 | + } |
| 313 | + |
245 | 314 | @Test
|
246 | 315 | public void onCreate_withCustomLayout_correctSessionStateFromOnConnect() throws Exception {
|
247 | 316 | SessionCommand command1 = new SessionCommand("command1", Bundle.EMPTY);
|
|
0 commit comments