Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add seamless transfer support #393

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@
</intent-filter>
</service>

<receiver
android:exported="true"
android:name="androidx.mediarouter.media.MediaTransferReceiver" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried to figure out what seamless transfer is about. I am still not really sure. I haven't found any material what seamless transfer means, exactly. However, this is the problematic line. Adding this to the manifest makes the Cast session not start (or at least not notify us/CastSessionManager of the session start). I suspect by adding this we are overriding a behavior which makes the Cast-MediaRoute-related plumbing.

I have commented on the guidance doc to see if I can get some assistance from the Seoul team on this. I would think they are the ideal people to look into this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After asking around, from Olly who also is in the seamless transfer doc:

For seamless transfer and the part of the Cast SDK that does routing to work together properly, the Cast SDK needs updating. Before the Cast SDK can be updated, the MediaRouter AndroidX library needs updating. So there is a chain of pending things that need to happen.

So it would seem this patch cannot go in until we update the Cast SDK to the right version?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks very much for investigating. That seems to be the case.


</application>

</manifest>
1 change: 0 additions & 1 deletion app/src/main/java/com/example/android/uamp/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package com.example.android.uamp

import android.media.AudioManager
import android.os.Bundle
import android.util.Log
import android.view.Menu
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ buildscript {
androidx_car_version = '1.0.0-alpha7'
androidx_core_ktx_version = '1.3.1'
androidx_media_version = '1.0.1'
androidx_mediarouter_version = '1.2.0-rc01'
androidx_preference_version = '1.1.1'
androidx_test_runner_version = '1.3.0'
arch_lifecycle_version = '2.2.0'
Expand Down
1 change: 1 addition & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ dependencies {
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlin_coroutines_version"

api "androidx.media:media:$androidx_media_version"
implementation "androidx.mediarouter:mediarouter:$androidx_mediarouter_version"

api "com.google.code.gson:gson:$gson_version"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.media.MediaBrowserServiceCompat
import androidx.media.MediaBrowserServiceCompat.BrowserRoot.EXTRA_RECENT
import androidx.mediarouter.media.MediaControlIntent
import androidx.mediarouter.media.MediaRouteSelector
import androidx.mediarouter.media.MediaRouter
import androidx.mediarouter.media.MediaRouterParams
import com.example.android.uamp.media.extensions.album
import com.example.android.uamp.media.extensions.flag
import com.example.android.uamp.media.extensions.id
Expand Down Expand Up @@ -104,6 +108,10 @@ open class MusicService : MediaBrowserServiceCompat() {

private lateinit var storage: PersistentStorage

private lateinit var mediaRouteSelector: MediaRouteSelector
private lateinit var mediaRouter: MediaRouter
private val mediaRouterCallback = MediaRouterCallback()

/**
* This must be `by lazy` because the source won't initially be ready.
* See [MusicService.onLoadChildren] to see where it's accessed (and first
Expand Down Expand Up @@ -216,6 +224,18 @@ open class MusicService : MediaBrowserServiceCompat() {
packageValidator = PackageValidator(this, R.xml.allowed_media_browser_callers)

storage = PersistentStorage.getInstance(applicationContext)

mediaRouter = MediaRouter.getInstance(this)
mediaRouter.setMediaSessionCompat(mediaSession)
mediaRouteSelector = MediaRouteSelector.Builder()
.addControlCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK)
.build()
mediaRouter.routerParams =
MediaRouterParams.Builder().setTransferToLocalEnabled(true).build()
mediaRouter.addCallback(
mediaRouteSelector, mediaRouterCallback,
MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY
)
}

/**
Expand Down Expand Up @@ -250,6 +270,9 @@ open class MusicService : MediaBrowserServiceCompat() {
// Free ExoPlayer resources.
exoPlayer.removeListener(playerListener)
exoPlayer.release()

// Stop listening for route changes.
mediaRouter.removeCallback(mediaRouterCallback)
}

/**
Expand Down Expand Up @@ -650,6 +673,21 @@ open class MusicService : MediaBrowserServiceCompat() {
).show()
}
}

inner class MediaRouterCallback : MediaRouter.Callback() {
override fun onRouteSelected(
router: MediaRouter,
route: MediaRouter.RouteInfo,
reason: Int
) {
if (reason == MediaRouter.UNSELECT_REASON_ROUTE_CHANGED) {
Log.d(TAG, "Unselected because route changed, continue playback")
} else if (reason == MediaRouter.UNSELECT_REASON_STOPPED) {
Log.d(TAG, "Unselected because route was stopped, stop playback")
currentPlayer.stop()
}
}
}
}

/*
Expand Down