urlcast
An FCast-compatible media receiver for Android. Phones cast a video, a song or a picture to the device, and control it from there.
A receiver for the FCast protocol, in plain Kotlin. Phones and computers running
Grayjay or FCast's own sender apps find the device, hand it the URL of what to play, and then pause,
seek, change the volume and so on. The library speaks the protocol and keeps every connected sender
up to date. Playing the media is yours: hand the URL to Media3, MediaPlayer or an image view.
It speaks versions 2 and 3 of the protocol. Senders that know version 4 (encrypted, with queues and screen mirroring) fall back to 3, as they do for FUTO's own Android receiver.
It has no Pane API and no app classes. The Glassworks video pane wraps it with settings and entities.
Package dev.glassworks.urlcast, MIT licensed, minSdk 23.
Anyone on your network can cast. Before version 4 the protocol has no pairing and no passwords: whoever can reach the port can play something. Decide in
onPlaywhat you're willing to play, and offer a way to turn the receiver off.
Use it in your own app
lateinit var receiver: UrlcastReceiver
receiver = UrlcastReceiver(
name = "Kitchen panel", // what senders show
listener = object : UrlcastReceiver.Listener {
override fun onPlay(request: PlayRequest) {
val url = request.url ?: return // a manifest in request.content, or a playlist, works too
if (!isOnMyNetwork(url)) return receiver.playbackError("This panel only plays from your network")
player.open(url, request.headers, request.time ?: 0.0)
receiver.playbackChanged(Playback(PlaybackState.PLAYING, time = request.time ?: 0.0))
}
override fun onPause() = player.pause()
override fun onResume() = player.play()
override fun onSeek(seconds: Double) = player.seekTo(seconds)
override fun onSetVolume(volume: Double) { player.setVolume(volume); receiver.volumeChanged(volume) }
override fun onSetSpeed(speed: Double) = player.setSpeed(speed)
override fun onStop() { player.close(); receiver.playbackChanged(Playback.IDLE) }
},
)
val port = receiver.start() // 46899, the port senders expect
Then announce it, with Android's NsdManager or any mDNS responder:
val info = NsdServiceInfo().apply {
serviceName = "Kitchen panel"
serviceType = UrlcastReceiver.SERVICE_TYPE // "_fcast._tcp"
this.port = port
}
nsdManager.registerService(info, NsdManager.PROTOCOL_DNS_SD, registrationListener)
While something plays, call playbackChanged when the state changes and about once a second, so
senders' progress bars keep up. Call volumeChanged after onSetVolume, and whenever the volume
changes on the device. If playback started some other way, nowPlaying tells senders what's on.
Call receiver.stop() when you're done.
A playlist arrives as a PlayRequest whose container is application/json: request.playlist has
it when it came in content, and Playlist.parse reads one you fetched from url. Report the item
that plays in Playback.itemIndex, and send mediaItemEvent when an item starts, ends or changes;
senders subscribe to those (FCast's own SDK always asks for the end of each item).
What's in it
UrlcastReceiver |
The receiver: the TCP server on port 46899, the version handshake, every sender's commands, and the updates that keep senders in sync (playbackChanged, volumeChanged, nowPlaying, playbackError, mediaItemEvent, keyEvent). |
UrlcastReceiver.Listener |
What senders ask for. Calls come in order on their own thread, with no locks held, so a listener may call the receiver back. |
PlayRequest, Playlist, MediaItem, Metadata |
What to play: the URL or inline manifest, its MIME type, where to start, the HTTP headers to send with it, a title and a thumbnail. |
Playback, PlaybackState, Sender, MediaItemEvent |
What you report back, and who's connected. |
The manifest adds INTERNET. The library doesn't log. Up to 8 senders can be connected at once (one
more replaces the one connected longest), and a sender that stops reading is dropped instead of holding
up the others.
How FCast works
Each packet is a little-endian 32-bit size (the opcode and body), a one-byte opcode, and a UTF-8 JSON body, 32,000 bytes at most. In the order a sender uses them:
- Finding the device. Receivers announce
_fcast._tcpover mDNS. Senders can also connect to an address typed in, or one read from a QR code. - Versions. On connect, each side sends
Version. This receiver sends 3; a sender that never sends one is treated as version 2. From version 3, both sides sendInitial: their name and app, and, from the receiver, what's playing now, followed by where playback is. - Playing.
Playcarries the MIME type, a URL or inline content (a DASH or HLS manifest, or a playlist), where to start, the volume and speed, HTTP headers and metadata. The receiver fetches the media itself. Version 3 receivers tell every sender what's playing (PlayUpdate). - Controls.
Pause,Resume,Stop,Seek,SetVolume,SetSpeedandSetPlaylistItem. The receiver answers withPlaybackUpdate(state, time, duration, speed) andVolumeUpdate, to every sender, so phones that cast together stay in agreement. - Events. Version 3 senders subscribe to an item starting, ending or changing, and to key presses.
Gradle
Not on Maven Central yet. In this repository: implementation(project(":urlcast")). An Android library
(AAR), minSdk 23 (Android 6), with no dependencies beyond the Kotlin standard library and Android's
org.json.
Tests
./gradlew :urlcast:testDebugUnitTest.
-
UrlcastReceiverTestconnects senders written from the protocol over loopback: the version handshake for version 2 and 3 senders, play and every control, what each sender hears back, events and subscriptions, playlists, broken and oversized packets, and one sender too many. -
FcastSenderInteropTestdrives the receiver with FUTO's FCast sender SDK, the library FCast's own apps cast with, from the small Rust program ininterop/. It skips unlessURLCAST_SENDERpoints at that program; CI builds it from the pinnedCargo.lock:cargo build --release --locked --manifest-path libs/urlcast/interop/Cargo.toml URLCAST_SENDER=$PWD/libs/urlcast/interop/target/release/urlcast-interop-sender \ ./gradlew :urlcast:testDebugUnitTest --tests '*FcastSenderInteropTest*'
FCast
This library is an independent community implementation of the FCast protocol. It is not affiliated with FUTO. FCast is a registered trademark of FUTO.