Skip to content

Unofficial YouTube Music / Spotify Premium client for Android, built in Liquid Glass. Audio effects, Spotify Connect, listening history, 8 languages. Rust core on librespot.

YouTube songs dissolve into each other the way Spotify's do

Lelonioe786645fa1f8
diff --git a/app/build.gradle.kts b/app/build.gradle.kts
index 9f804e5..a33c536 100644
--- a/app/build.gradle.kts
+++ b/app/build.gradle.kts
@@ -188,6 +188,7 @@ dependencies {
implementation(libs.media3.common)
implementation(libs.media3.session)
implementation(libs.media3.exoplayer)
+ implementation(libs.media3.ffmpeg.decoder)
// DASH, for Spotify's own video: its manifest is a set of segment
// templates, which is what a DASH source is built to walk.
implementation(libs.media3.exoplayer.dash)
diff --git a/app/src/main/java/dev/lelonio/square/backend/youtube/MixerRateProcessor.kt b/app/src/main/java/dev/lelonio/square/backend/youtube/MixerRateProcessor.kt
new file mode 100644
index 0000000..aa11c06
--- /dev/null
+++ b/app/src/main/java/dev/lelonio/square/backend/youtube/MixerRateProcessor.kt
@@ -0,0 +1,39 @@
+package dev.lelonio.square.backend.youtube
+
+import androidx.media3.common.audio.AudioProcessor
+import androidx.media3.common.audio.SonicAudioProcessor
+import androidx.media3.common.util.UnstableApi
+import java.nio.ByteBuffer
+
+/**
+ * Brings every stream to the mixer's rate, and keeps doing it.
+ *
+ * Sonic resamples, but forgets the rate it was asked for on every reset, and
+ * the sink resets its processors whenever the player stops: the first songs
+ * reached the mixer at 48 kHz and the ones after a stop went round it at 44.1,
+ * crossfade and all. This puts the rate back after each reset. Sonic is final,
+ * hence a wrapper rather than a subclass.
+ */
+@UnstableApi
+class MixerRateProcessor : AudioProcessor {
+
+ private val sonic = SonicAudioProcessor().apply { setOutputSampleRateHz(PcmMixer.RATE) }
+
+ override fun configure(inputAudioFormat: AudioProcessor.AudioFormat) = sonic.configure(inputAudioFormat)
+ override fun isActive() = sonic.isActive
+ override fun queueInput(inputBuffer: ByteBuffer) = sonic.queueInput(inputBuffer)
+ override fun queueEndOfStream() = sonic.queueEndOfStream()
+ override fun getOutput(): ByteBuffer = sonic.output
+ override fun isEnded() = sonic.isEnded
+ override fun getDurationAfterProcessorApplied(durationUs: Long) = sonic.getDurationAfterProcessorApplied(durationUs)
+
+ @Suppress("DEPRECATION")
+ override fun flush() = sonic.flush()
+
+ override fun flush(streamMetadata: AudioProcessor.StreamMetadata) = sonic.flush(streamMetadata)
+
+ override fun reset() {
+ sonic.reset()
+ sonic.setOutputSampleRateHz(PcmMixer.RATE)
+ }
+}
diff --git a/app/src/main/java/dev/lelonio/square/backend/youtube/OfficialVideos.kt b/app/src/main/java/dev/lelonio/square/backend/youtube/OfficialVideos.kt
new file mode 100644
index 0000000..df7fefc
--- /dev/null
+++ b/app/src/main/java/dev/lelonio/square/backend/youtube/OfficialVideos.kt
@@ -0,0 +1,66 @@
+package dev.lelonio.square.backend.youtube
+
+import com.metrolist.innertube.YouTube
+import com.metrolist.innertube.models.SongItem
+import kotlinx.coroutines.runBlocking
+import kotlinx.coroutines.withTimeoutOrNull
+import org.schabi.newpipe.extractor.stream.StreamInfo
+
+/**
+ * The official music video of a song, for the video button.
+ *
+ * A song on YouTube Music is its own upload, and its picture is the cover held
+ * still for three minutes. The video people mean is a separate upload, found
+ * here the way a listener would find it: the same title by the same artist,
+ * among the videos, and about as long. Played whole, sound included, because
+ * a video's intro and ending are not the record's.
+ */
+object OfficialVideos {
+
+ private class Found(val videoId: String?)
+
+ private val found = java.util.concurrent.ConcurrentHashMap<String, Found>()
+
+ /** Blocking: called from the stream resolver, on a loading thread. */
+ fun forSong(songId: String, song: StreamInfo): String? {
+ found[songId]?.let { return it.videoId }
+ val title = song.name.orEmpty()
+ val artist = song.uploaderName.orEmpty().removeSuffix(" - Topic").trim()
+ val seconds = song.duration.toInt()
+ var answered = false
+ val match = runBlocking {
+ withTimeoutOrNull(TIMEOUT_MS) {
+ val results = YouTube.search("$title $artist".trim(), YouTube.SearchFilter.FILTER_VIDEO)
+ .getOrNull()?.items
+ answered = results != null
+ results.orEmpty()
+ .filterIsInstance<SongItem>()
+ .take(CANDIDATES)
+ .firstOrNull { isVideoOf(it, title, artist, seconds) }
+ }
+ }
+ if (match != null || answered) found[songId] = Found(match?.id)
+ if (match == null && answered) android.util.Log.i(TAG, "no official video for $songId \"$title\"")
+ return match?.id
+ }
+
+ private fun isVideoOf(video: SongItem, title: String, artist: String, seconds: Int): Boolean {
+ if (video.musicVideoType != "MUSIC_VIDEO_TYPE_OMV") return false
+ val wanted = squash(title)
+ if (wanted.isEmpty() || !squash(video.title).contains(wanted)) return false
+ val name = squash(artist)
+ val sameArtist = name.isEmpty() ||
+ video.artists.any { squash(it.name).let { credited -> credited == name || credited.contains(name) } } ||
+ squash(video.title).contains(name)
+ if (!sameArtist) return false
+ val length = video.duration ?: return true
+ // A video runs longer than its record, by an intro or a scene, not shorter.
+ return seconds <= 0 || length in (seconds - 15)..(seconds + 180)
+ }
+
+ private fun squash(text: String) = text.lowercase().filter { it.isLetterOrDigit() }
+
+ private const val TIMEOUT_MS = 6_000L
+ private const val CANDIDATES = 6
+ private const val TAG = "SquareYTVideo"
+}
diff --git a/app/src/main/java/dev/lelonio/square/backend/youtube/PcmMixer.kt b/app/src/main/java/dev/lelonio/square/backend/youtube/PcmMixer.kt
new file mode 100644
index 0000000..1ae808e
--- /dev/null
+++ b/app/src/main/java/dev/lelonio/square/backend/youtube/PcmMixer.kt
@@ -0,0 +1,703 @@
+package dev.lelonio.square.backend.youtube
+
+import android.content.Context
+import android.media.AudioDeviceInfo
+import android.media.AudioFormat
+import android.media.AudioTrack
+import android.os.Process
+import androidx.media3.common.C
+import androidx.media3.common.PlaybackParameters
+import androidx.media3.common.util.UnstableApi
+import androidx.media3.exoplayer.audio.AudioOutput
+import androidx.media3.exoplayer.audio.AudioOutputProvider
+import androidx.media3.exoplayer.audio.AudioTrackAudioOutputProvider
+import androidx.media3.exoplayer.audio.ForwardingAudioOutputProvider
+import java.nio.ByteBuffer
+import java.nio.ByteOrder
+import java.util.concurrent.CopyOnWriteArraySet
+import java.util.concurrent.Executors
+import kotlin.math.PI
+import kotlin.math.cos
+import kotlin.math.max
+import kotlin.math.min
+import kotlin.math.sqrt
+
+/**
+ * One speaker for the YouTube players, the way the Spotify engine has one.
+ *
+ * ExoPlayer gives every player its own `AudioTrack`, and two tracks cannot be
+ * lined up closer than their reported positions allow, which on this hardware
+ * is far enough apart to hear a stretch of the outgoing song twice when it is
+ * handed from one player to the other. Here each player keeps its decoder and
+ * its processors (speed, pitch, karaoke) and writes the finished PCM into a
+ * [MixerChannel]; one thread sums the channels into one track. Two copies of
+ * the same song can then be swapped at the very sample, see [handOver].
+ *
+ * Fixed format: float stereo at [RATE]. The players resample to it with Sonic
+ * (see YouTubePlayerFactory); anything else falls back to an ordinary track.
+ */
+@UnstableApi
+class PcmMixer {
+
+ private val lock = Object()
+ private val channels = mutableListOf<MixerChannel>()
+ private val track: AudioTrack
+ private val thread: Thread
+ @Volatile private var running = true
+
+ /** Frames handed to the track since it was built. */
+ @Volatile private var framesWritten = 0L
+
+ // What the track has played, as last read, and when. Read from the players'
+ // threads through [playedFrames]; written by the mixing thread.
+ private val clockLock = Object()
+ private var headFrames = 0L
+ private var headNanos = 0L
+ private var headAdvancing = false
+ private var lastRawHead = 0L
+ private var headWraps = 0L
+ private var lastPlayed = 0L
+ private val timestamp = android.media.AudioTimestamp()
+
+ private var pending: Handover? = null
+ private val worker = Executors.newSingleThreadExecutor { r ->
+ Thread(r, "SquareMixerAlign").apply { isDaemon = true }
+ }
+
+ init {
+ val minBuffer = AudioTrack.getMinBufferSize(
+ RATE, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_FLOAT,
+ )
+ track = AudioTrack.Builder()
+ .setAudioAttributes(
+ android.media.AudioAttributes.Builder()
+ .setUsage(android.media.AudioAttributes.USAGE_MEDIA)
+ .setContentType(android.media.AudioAttributes.CONTENT_TYPE_MUSIC)
+ .build(),
+ )
+ .setAudioFormat(
+ AudioFormat.Builder()
+ .setEncoding(AudioFormat.ENCODING_PCM_FLOAT)
+ .setSampleRate(RATE)
+ .setChannelMask(AudioFormat.CHANNEL_OUT_STEREO)
+ .build(),
+ )
+ .setTransferMode(AudioTrack.MODE_STREAM)
+ // Short: a fade is applied as the audio is mixed, so the track's
+ // buffer is how late every volume change is heard.
+ .setBufferSizeInBytes(max(minBuffer, TRACK_BUFFER_FRAMES * 2 * 4))
+ .build()
+ thread = Thread(::loop, "SquareMixer").apply {
+ isDaemon = true
+ start()
+ }
+ }
+
+ val audioSessionId: Int get() = track.audioSessionId
+
+ internal fun add(channel: MixerChannel) = synchronized(lock) {
+ channels += channel
+ lock.notifyAll()
+ }
+
+ internal fun remove(channel: MixerChannel) = synchronized(lock) {
+ channels -= channel
+ lock.notifyAll()
+ }
+
+ internal fun wake() = synchronized(lock) { lock.notifyAll() }
+
+ /** Frames of the track that have reached the speaker, or near enough. */
+ internal fun playedFrames(): Long = synchronized(clockLock) {
+ val estimate = if (!headAdvancing) {
+ headFrames
+ } else {
+ val since = (System.nanoTime() - headNanos).coerceIn(0, MAX_EXTRAPOLATION_NS)
+ min(headFrames + since * RATE / 1_000_000_000L, framesWritten)
+ }
+ // Never backwards: a video is shown against this clock, and a clock
+ // that steps back a few ms and forward again holds and drops frames.
+ lastPlayed = max(lastPlayed, estimate)
+ lastPlayed
+ }
+
+ /**
+ * Moves what [from] is playing onto [to], which must be playing the same
+ * song a little ahead, held back by [MixerSlot.held] so its frames pile up.
+ *
+ * The last moment of [from] is looked for in [to]'s pile; [to] then starts
+ * from the frame after it, in the same block in which [from] goes silent,
+ * with a block-long dissolve between the two. [done] is called on the
+ * mixing thread: false when the two could not be matched.
+ */
+ fun handOver(from: MixerSlot, to: MixerSlot, done: (Boolean) -> Unit) {
+ synchronized(lock) {
+ pending?.done?.invoke(false)
+ pending = Handover(from, to, done, System.nanoTime())
+ lock.notifyAll()
+ }
+ }
+
+ /**
+ * Lets what [slot] has already decoded play out on its own, fading to
+ * nothing over [fadeMs], while the player moves on: the outgoing half of a
+ * change of song the listener asked for, as the engine has it. The pile
+ * holds up to two seconds ahead, so there is no decoder to wait for.
+ *
+ * False when there is nothing to fade.
+ */
+ fun detach(slot: MixerSlot, fadeMs: Int): Boolean = synchronized(lock) {
+ val channel = slot.channel ?: return false
+ if (!channel.playing || channel.queuedFrames() == 0) return false
+ channel.orphan(slot.gain(), max(1, fadeMs * RATE / 1000))
+ slot.channel = null
+ lock.notifyAll()
+ true
+ }
+
+ fun cancelHandOver() = synchronized(lock) {
+ pending?.done?.invoke(false)
+ pending = null
+ }
+
+ fun release() {
+ running = false
+ wake()
+ thread.join(500)
+ worker.shutdownNow()
+ runCatching { track.stop() }
+ track.release()
+ }
+
+ private fun loop() {
+ Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_AUDIO)
+ val block = FloatArray(BLOCK_FRAMES * 2)
+ while (running) {
+ val active: List<MixerChannel>
+ synchronized(lock) {
+ channels.removeAll { it.finished }
+ while (running && channels.none { it.playing }) {
+ if (track.playState == AudioTrack.PLAYSTATE_PLAYING) {
+ track.pause()
+ readHead(advancing = false)
+ }
+ lock.wait(250)
+ }
+ active = channels.filter { it.playing }
+ }
+ if (!running) break
+ if (track.playState != AudioTrack.PLAYSTATE_PLAYING) track.play()
+ block.fill(0f)
+ runHandover()
+ for (channel in active) {
+ if (channel.slot.held && !channel.orphaned) continue
+ channel.mixInto(block, BLOCK_FRAMES, framesWritten)
+ }
+ var offset = 0
+ while (offset < block.size && running) {
+ val wrote = track.write(block, offset, block.size - offset, AudioTrack.WRITE_BLOCKING)
+ if (wrote < 0) break
+ offset += wrote
+ }
+ framesWritten += BLOCK_FRAMES
+ readHead(advancing = true)
+ }
+ }
+
+ /**
+ * Where the track is, and since when. The timestamp says both exactly, once
+ * the track has one; the head position only moves in the output's periods,
+ * so it is dated by when it last moved, not by when it was read.
+ */
+ private fun readHead(advancing: Boolean) {
+ val now = System.nanoTime()
+ val stamped = track.getTimestamp(timestamp) && now - timestamp.nanoTime in 0..STALE_TIMESTAMP_NS
+ val raw = track.playbackHeadPosition.toLong() and 0xFFFF_FFFFL
+ synchronized(clockLock) {
+ if (raw < lastRawHead) headWraps++
+ val moved = raw != lastRawHead
+ lastRawHead = raw
+ if (stamped && advancing) {
+ headFrames = timestamp.framePosition
+ headNanos = timestamp.nanoTime
+ } else if (moved || !advancing || !headAdvancing) {
+ headFrames = (headWraps shl 32) + raw
+ headNanos = now
+ }
+ headAdvancing = advancing
+ }
+ }
+
+ private fun runHandover() {
+ val request = synchronized(lock) { pending } ?: return
+ fun finish(ok: Boolean) {
+ synchronized(lock) { if (pending === request) pending = null }
+ request.done(ok)
+ }
+ val source = request.from.channel
+ val target = request.to.channel
+ if (source == null || target == null || System.nanoTime() - request.startedNanos > HANDOVER_TIMEOUT_NS) {
+ android.util.Log.w(TAG, "handover gave up: source=${source != null} target=${target != null}")
+ finish(false)
+ return
+ }
+ val result = request.result
+ if (request.computing && result == null) return
+ if (result != null && (result.source !== source || result.target !== target)) {
+ request.result = null
+ request.computing = false
+ return
+ }
+ if (result == null) {
+ // Wait until the source has said enough to be recognised and the
+ // target has piled up enough to recognise it in.
+ if (source.historyFrames() < REF_FRAMES || target.queuedFrames() < MIN_PILE_FRAMES) return
+ val reference = source.history(REF_FRAMES)
+ val consumed = source.consumedFrames()
+ val pile = target.pileMono()
+ // Where the timestamps put it: within a few tens of ms, never a bar
+ // off, which in a looped track the correlation alone can be.
+ val heardUs = source.mediaTimeAt(consumed)
+ val expected = heardUs?.let { target.frameAt(it) }?.let { (it - target.consumedFrames()).toInt() }
+ request.computing = true
+ worker.execute {
+ val (offset, score) = locate(reference, pile, expected?.minus(REF_FRAMES))
+ request.result = Match(source, target, consumed, offset, score)
+ request.computing = false
+ }
+ return
+ }
+ request.result = null
+ val skip = result.offset + REF_FRAMES + (source.consumedFrames() - result.consumed)
+ if (result.score < MIN_SCORE || skip < 0) {
+ if (++request.attempts >= MAX_ATTEMPTS) finish(false)
+ return
+ }
+ if (skip + BLOCK_FRAMES > target.queuedFrames()) {
+ // The source has outrun the pile: more will not come while it is held full.
+ if (target.queuedFrames() >= target.capacity - BLOCK_FRAMES || ++request.attempts >= MAX_ATTEMPTS) {
+ finish(false)
+ }
+ return
+ }
+ target.skip(skip.toInt())
+ request.from.fade = 0f
+ request.to.fade = 1f
+ request.to.held = false
+ finish(true)
+ }
+
+ private class Handover(
+ val from: MixerSlot,
+ val to: MixerSlot,
+ val done: (Boolean) -> Unit,
+ val startedNanos: Long,
+ ) {
+ @Volatile var computing = false
+ @Volatile var result: Match? = null
+ var attempts = 0
+ }
+
+ private class Match(
+ val source: MixerChannel,
+ val target: MixerChannel,
+ val consumed: Long,
+ val offset: Int,
+ val score: Float,
+ )
+
+ companion object {
+ const val RATE = 48_000
+ private const val BLOCK_FRAMES = 480
+ private const val TRACK_BUFFER_FRAMES = 3_840
+ private const val MAX_EXTRAPOLATION_NS = 100_000_000L
+ /** A timestamp older than this is from before a pause, not a reading. */
+ private const val STALE_TIMESTAMP_NS = 500_000_000L
+ internal const val REF_FRAMES = 4_096
+ private const val MIN_PILE_FRAMES = RATE / 2
+ private const val MIN_SCORE = 0.6f
+ private const val MAX_ATTEMPTS = 8
+ private const val HANDOVER_TIMEOUT_NS = 3_000_000_000L
+ private const val DECIMATION = 4
+ /** How far from the timestamps' guess the match may be: 100 ms. */
+ private const val SEARCH_RADIUS = RATE / 10
+ internal const val TAG = "SquareMixer"
+
+ /**
+ * Where [reference] starts inside [pile], by normalised cross-correlation:
+ * coarsely on both decimated, then sample by sample around the best.
+ */
+ internal fun locate(reference: FloatArray, pile: FloatArray, around: Int?): Pair<Int, Float> {
+ if (pile.size < reference.size) return -1 to 0f
+ val ref = decimate(reference)
+ val hay = decimate(pile)
+ val refEnergy = ref.sumOf { (it * it).toDouble() }
+ if (refEnergy < 1e-6) return -1 to 0f
+ val prefix = DoubleArray(hay.size + 1)
+ for (i in hay.indices) prefix[i + 1] = prefix[i] + hay[i] * hay[i]
+ var best = 0
+ var bestScore = -1.0
+ val first = around?.let { max(0, (it - SEARCH_RADIUS) / DECIMATION) } ?: 0
+ val last = around?.let { min(hay.size - ref.size, (it + SEARCH_RADIUS) / DECIMATION) } ?: (hay.size - ref.size)
+ if (first > last) return -1 to 0f
+ for (k in first..last) {
+ var dot = 0.0
+ for (i in ref.indices) dot += ref[i] * hay[k + i]
+ val energy = prefix[k + ref.size] - prefix[k]
+ if (energy <= 0) continue
+ val score = dot / sqrt(refEnergy * energy)
+ if (score > bestScore) {
+ bestScore = score
+ best = k
+ }
+ }
+ val fullEnergy = reference.sumOf { (it * it).toDouble() }
+ var fine = best * DECIMATION
+ var fineScore = -1.0
+ val from = max(0, best * DECIMATION - 2 * DECIMATION)
+ val to = min(pile.size - reference.size, best * DECIMATION + 2 * DECIMATION)
+ for (k in from..to) {
+ var dot = 0.0
+ var energy = 0.0
+ for (i in reference.indices) {
+ val v = pile[k + i]
+ dot += reference[i] * v
+ energy += v * v
+ }
+ if (energy <= 0) continue
+ val score = dot / sqrt(fullEnergy * energy)
+ if (score > fineScore) {
+ fineScore = score
+ fine = k
+ }
+ }
+ return fine to fineScore.toFloat()
+ }
+
+ private fun decimate(samples: FloatArray): FloatArray {
+ val out = FloatArray(samples.size / DECIMATION)
+ for (i in out.indices) {
+ var sum = 0f
+ for (j in 0 until DECIMATION) sum += samples[i * DECIMATION + j]
+ out[i] = sum / DECIMATION
+ }
+ return out
+ }
+ }
+}
+
+/**
+ * One player's place in the mix, kept across the outputs ExoPlayer builds and
+ * throws away (it releases its output on every seek).
+ */
+class MixerSlot(
+ /** Whose volume this slot obeys: the tail follows the main player's, ducking included. */
+ private val follows: MixerSlot? = null,
+ initialFade: Float = 1f,
+) {
+ /** The controller's own multiplier, on top of the player's volume. */
+ @Volatile var fade = initialFade
+
+ /** While true the slot's frames are left to pile up rather than played. */
+ @Volatile var held = false
+
+ /** The volume the player itself asked for. */
+ @Volatile internal var volume = 1f
+
+ @Volatile internal var channel: MixerChannel? = null
+
+ /**
+ * Whether the player's audio goes through the mixer: true from its first
+ * mixer output, and still true in the gap between one output and the next.
+ */
+ @Volatile var mixed = false
+ internal set
+
+ internal fun gain(): Float = fade * (follows ?: this).volume
+}
+
+/** One output of one player, as ExoPlayer sees it. */
+@UnstableApi
+class MixerChannel internal constructor(
+ private val mixer: PcmMixer,
+ internal val slot: MixerSlot,
+ private val inputChannels: Int,
+ private val floatInput: Boolean,
+) : AudioOutput {
+
+ internal val capacity = PcmMixer.RATE * 2
+ private val ring = FloatArray(capacity * 2)
+ private var readIndex = 0
+ private var queued = 0
+ private var consumed = 0L
+
+ // (channel frame, media time in µs) at the start of each buffer written:
+ // what lets the two copies of a song be lined up roughly before precisely.
+ private val stamps = ArrayDeque<LongArray>()
+ private var lastStampUs = Long.MIN_VALUE
+ private var lastGain = -1f
+
+ private val history = FloatArray(PcmMixer.REF_FRAMES)
+ private var historyIndex = 0
+ private var historyCount = 0
+
+ // Which frames of the track carry which frames of this channel: (track frame
+ // the block started at, channel frames consumed before it, frames in it).
+ private val blocks = ArrayDeque<LongArray>()
+ private var playedBase = 0L
+
+ @Volatile internal var playing = false
+ private val listeners = CopyOnWriteArraySet<AudioOutput.Listener>()
+ private var released = false
+
+ init {
+ slot.channel = this
+ mixer.add(this)
+ }
+
+ override fun play() {
+ playing = true
+ mixer.wake()
+ }
+
+ override fun pause() {
+ // An orphan plays out whatever its player does next.
+ if (!orphaned) playing = false
+ }
+
+ // Set by [orphan]: the gain it started from, how long its fade is, how far in.
+ @Volatile internal var orphaned = false
+ private set
+ private var orphanFrom = 0f
+ private var orphanTotal = 1
+ private var orphanDone = 0
+
+ /** True once an orphan has faded out or run dry; the mixer drops it then. */
+ @Volatile internal var finished = false
+ private set
+
+ internal fun orphan(gain: Float, frames: Int) = synchronized(this) {
+ orphanFrom = gain
+ orphanTotal = frames
+ orphanDone = 0
+ orphaned = true
+ playing = true
+ }
+
+ override fun write(buffer: ByteBuffer, encodedAccessUnitCount: Int, presentationTimeUs: Long): Boolean {
+ val bytesPerSample = if (floatInput) 4 else 2
+ val frameBytes = bytesPerSample * inputChannels
+ synchronized(this) {
+ if (released) return true
+ val available = buffer.remaining() / frameBytes
+ if (presentationTimeUs >= 0 && presentationTimeUs != lastStampUs) {
+ stamps.addLast(longArrayOf(consumed + queued, presentationTimeUs))
+ lastStampUs = presentationTimeUs
+ while (stamps.size > MAX_STAMPS) stamps.removeFirst()
+ }
+ val frames = min(available, capacity - queued)
+ val data = buffer.duplicate().order(ByteOrder.nativeOrder())
+ var write = (readIndex + queued) % capacity
+ for (f in 0 until frames) {
+ val left: Float
+ val right: Float
+ if (floatInput) {
+ left = data.getFloat()
+ right = if (inputChannels > 1) data.getFloat() else left
+ repeat(inputChannels - 2) { data.getFloat() }
+ } else {
+ left = data.getShort() / 32768f
+ right = if (inputChannels > 1) data.getShort() / 32768f else left
+ repeat(inputChannels - 2) { data.getShort() }
+ }
+ ring[write * 2] = left
+ ring[write * 2 + 1] = right
+ write = (write + 1) % capacity
+ }
+ queued += frames
+ buffer.position(buffer.position() + frames * frameBytes)
+ return frames == available
+ }
+ }
+
+ /** Adds this channel's next frames to [block], which starts at [trackFrame]. */
+ internal fun mixInto(block: FloatArray, frames: Int, trackFrame: Long) = synchronized(this) {
+ val target = slot.gain()
+ val start = if (lastGain < 0) target else lastGain
+ lastGain = target
+ val n = min(frames, queued)
+ if (orphaned && (n == 0 || orphanDone >= orphanTotal)) {
+ finished = true
+ return@synchronized
+ }
+ if (n == 0) return@synchronized
+ for (i in 0 until n) {
+ val gain = if (orphaned) {
+ val x = min(1f, (orphanDone + i).toFloat() / orphanTotal)
+ orphanFrom * cos(x * PI.toFloat() / 2)
+ } else {
+ start + (target - start) * (i + 1) / frames
+ }
+ val left = ring[readIndex * 2]
+ val right = ring[readIndex * 2 + 1]
+ block[i * 2] += left * gain
+ block[i * 2 + 1] += right * gain
+ history[historyIndex] = (left + right) * 0.5f
+ historyIndex = (historyIndex + 1) % history.size
+ if (historyCount < history.size) historyCount++
+ readIndex = (readIndex + 1) % capacity
+ }
+ queued -= n
+ if (orphaned) orphanDone += n
+ blocks.addLast(longArrayOf(trackFrame, consumed, n.toLong()))
+ consumed += n
+ while (blocks.size > MAX_BLOCKS) blocks.removeFirst().let { playedBase = it[1] + it[2] }
+ }
+
+ /** Drops [frames] from the front of the pile, counted as played. */
+ internal fun skip(frames: Int) = synchronized(this) {
+ val n = min(frames, queued)
+ readIndex = (readIndex + n) % capacity
+ queued -= n
+ // Heard as a jump forward once the track reaches the next block, which
+ // starts from here: the frames skipped were the source's to play.
+ consumed += n
+ lastGain = 0f
+ }
+
+ internal fun queuedFrames(): Int = synchronized(this) { queued }
+
+ internal fun consumedFrames(): Long = synchronized(this) { consumed }
+
+ internal fun historyFrames(): Int = synchronized(this) { historyCount }
+
+ /** The last [frames] this channel played, oldest first, as mono. */
+ internal fun history(frames: Int): FloatArray = synchronized(this) {
+ FloatArray(frames) { i -> history[(historyIndex - frames + i + history.size * 2) % history.size] }
+ }
+
+ /** Everything piled up, oldest first, as mono. */
+ /** The media time of channel frame [frame], from the stamps around it. */
+ internal fun mediaTimeAt(frame: Long): Long? = synchronized(this) {
+ val before = stamps.lastOrNull { it[0] <= frame } ?: return null
+ val after = stamps.firstOrNull { it[0] > frame }
+ if (after == null || after[0] == before[0]) {
+ return before[1] + (frame - before[0]) * 1_000_000L / PcmMixer.RATE
+ }
+ before[1] + (after[1] - before[1]) * (frame - before[0]) / (after[0] - before[0])
+ }
+
+ /** The channel frame that carries media time [timeUs], from the stamps around it. */
+ internal fun frameAt(timeUs: Long): Long? = synchronized(this) {
+ val before = stamps.lastOrNull { it[1] <= timeUs } ?: return null
+ val after = stamps.firstOrNull { it[1] > timeUs }
+ if (after == null || after[1] == before[1]) {
+ return before[0] + (timeUs - before[1]) * PcmMixer.RATE / 1_000_000L
+ }
+ before[0] + (after[0] - before[0]) * (timeUs - before[1]) / (after[1] - before[1])
+ }
+
+ internal fun pileMono(): FloatArray = synchronized(this) {
+ FloatArray(queued) { i ->
+ val at = (readIndex + i) % capacity
+ (ring[at * 2] + ring[at * 2 + 1]) * 0.5f
+ }
+ }
+
+ private fun playedFrames(): Long = synchronized(this) {
+ val heard = mixer.playedFrames()
+ while (blocks.isNotEmpty()) {
+ val first = blocks.first()
+ if (heard >= first[0] + first[2]) {
+ playedBase = max(playedBase, first[1] + first[2])
+ blocks.removeFirst()
+ } else {
+ break
+ }
+ }
+ val first = blocks.firstOrNull() ?: return playedBase
+ if (heard <= first[0]) return playedBase
+ first[1] + (heard - first[0])
+ }
+
+ override fun flush() = synchronized(this) {
+ if (orphaned) return@synchronized
+ readIndex = 0
+ queued = 0
+ consumed = 0
+ playedBase = 0
+ blocks.clear()
+ stamps.clear()
+ lastStampUs = Long.MIN_VALUE
+ historyCount = 0
+ }
+
+ override fun stop() = Unit
+
+ override fun release() {
+ synchronized(this) {
+ if (released) return
+ released = true
+ if (!orphaned) playing = false
+ }
+ if (slot.channel === this) slot.channel = null
+ // An orphan stays in the mix until its fade is over; see PcmMixer.detach.
+ if (!orphaned) mixer.remove(this)
+ listeners.forEach { it.onReleased() }
+ }
+
+ override fun setVolume(volume: Float) {
+ slot.volume = volume
+ }
+
+ override fun isOffloadedPlayback() = false
+ override fun getAudioSessionId() = mixer.audioSessionId
+ override fun getSampleRate() = PcmMixer.RATE
+ override fun getBufferSizeInFrames() = capacity.toLong()
+ override fun getPositionUs(): Long = playedFrames() * 1_000_000L / PcmMixer.RATE
+ override fun getPlaybackParameters(): PlaybackParameters = PlaybackParameters.DEFAULT
+ override fun isStalled() = false
+ override fun addListener(listener: AudioOutput.Listener) { listeners += listener }
+ override fun removeListener(listener: AudioOutput.Listener) { listeners -= listener }
+ override fun setPlaybackParameters(playbackParams: PlaybackParameters) = Unit
+ override fun setOffloadDelayPadding(delayInFrames: Int, paddingInFrames: Int) = Unit
+ override fun setOffloadEndOfStream() = Unit
+ override fun attachAuxEffect(effectId: Int) = Unit
+ override fun setAuxEffectSendLevel(level: Float) = Unit
+ override fun setPreferredDevice(preferredDevice: AudioDeviceInfo?) = Unit
+
+ private companion object {
+ /** About two seconds of blocks: far more than the track ever holds unplayed. */
+ const val MAX_BLOCKS = 200
+
+ /** Buffers are a few ms each: comfortably over the two-second pile. */
+ const val MAX_STAMPS = 2_000
+ }
+}
+
+/**
+ * Hands ExoPlayer a [MixerChannel] for every output it asks for in the mixer's
+ * format, and an ordinary track for anything else.
+ */
+@UnstableApi
+class MixerOutputProvider(
+ context: Context,
+ private val mixer: PcmMixer,
+ private val slot: MixerSlot,
+) : ForwardingAudioOutputProvider(AudioTrackAudioOutputProvider.Builder(context).build()) {
+
+ override fun getAudioOutput(config: AudioOutputProvider.OutputConfig): AudioOutput {
+ val channels = Integer.bitCount(config.channelMask)
+ val mixable = config.sampleRate == PcmMixer.RATE &&
+ (config.encoding == C.ENCODING_PCM_16BIT || config.encoding == C.ENCODING_PCM_FLOAT) &&
+ channels in 1..8 && !config.isOffload && !config.isTunneling
+ if (!mixable) {
+ android.util.Log.w(PcmMixer.TAG, "not mixable: ${config.sampleRate} Hz enc=${config.encoding} ch=$channels")
+ slot.channel = null
+ slot.mixed = false
+ return super.getAudioOutput(config)
+ }
+ slot.mixed = true
+ return MixerChannel(mixer, slot, channels, config.encoding == C.ENCODING_PCM_FLOAT)
+ }
+}
diff --git a/app/src/main/java/dev/lelonio/square/backend/youtube/SkipFadePlayer.kt b/app/src/main/java/dev/lelonio/square/backend/youtube/SkipFadePlayer.kt
index c2aefed..f687db9 100644
--- a/app/src/main/java/dev/lelonio/square/backend/youtube/SkipFadePlayer.kt
+++ b/app/src/main/java/dev/lelonio/square/backend/youtube/SkipFadePlayer.kt
@@ -5,22 +5,7 @@ import androidx.media3.common.Player
import androidx.media3.common.util.UnstableApi
import androidx.media3.common.ForwardingPlayer
-/**
- * The YouTube Music player, with the song being left behind faded out first.
- *
- * Everything that changes what is playing arrives here: the skip buttons, a
- * song picked from a list, a queue handed over whole. Each of them used to cut
- * the music dead and start the next one at full volume, which beside the
- * Spotify source, where the engine dissolves the two into each other, sounded
- * like the app changing its mind rather than changing song.
- *
- * One player holds one song, so the two halves cannot overlap the way the
- * engine's do; see [YouTubeFadeController.changeWith], which owns the volume
- * and runs them one after the other.
- *
- * A seek inside the song that is playing is not a change of song and is passed
- * straight through: dragging the progress bar is not something to dissolve.
- */
+/** Keeps the public queue on the main player while its outgoing audio overlaps the next song. */
@UnstableApi
class SkipFadePlayer(
player: Player,
@@ -31,6 +16,12 @@ class SkipFadePlayer(
private fun changing(change: () -> Unit) = fades.changeWith(fadeMs(), change)
+ override fun release() {
+ fades.release()
+ super.release()
+ fades.releaseMixer()
+ }
+
override fun seekToNext() = changing { super.seekToNext() }
override fun seekToNextMediaItem() = changing { super.seekToNextMediaItem() }
diff --git a/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubeBackend.kt b/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubeBackend.kt
index abf6e08..4ea5b03 100644
--- a/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubeBackend.kt
+++ b/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubeBackend.kt
@@ -40,6 +40,7 @@ import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
+import kotlinx.coroutines.sync.withPermit
/**
* YouTube Music, over two clients rather than one.
@@ -418,20 +419,46 @@ class YouTubeBackend(private val account: YouTubeAccount) : MusicBackend {
artworkUrl = item.thumbnail,
)
- private fun toCatalogTrack(item: SongItem) = CatalogTrack(
- uri = "$TRACK_PREFIX${item.id}",
- name = item.title,
- artist = item.artists.joinToString(", ") { it.name },
- album = item.album?.name.orEmpty(),
- durationMs = item.duration?.takeIf { it > 0 }?.times(1000L) ?: 0L,
- explicit = item.explicit,
- artworkUrl = item.thumbnail.atSize(COVER_SIZE),
- )
+ private fun toCatalogTrack(item: SongItem): CatalogTrack {
+ // Every name its own way in, and the record too: what makes the lines
+ // under the title in the player lead somewhere, as they do for Spotify.
+ // A video's byline arrives as runs, and the joining words between the
+ // names ("," "&" "e" "and") come through as artists of their own.
+ val named = item.artists.filter { artist ->
+ val name = artist.name.trim()
+ name.isNotEmpty() && name.lowercase() !in BYLINE_JOINERS && name.any { it.isLetterOrDigit() }
+ }
+ val credited = named.map { artist ->
+ dev.lelonio.square.data.CatalogArtist(
+ name = artist.name.trim(),
+ uri = artist.id?.takeIf { it.isNotBlank() }?.let { "$ARTIST_PREFIX$it" },
+ )
+ }
+ return CatalogTrack(
+ uri = "$TRACK_PREFIX${item.id}",
+ name = item.title,
+ artist = named.joinToString(", ") { it.name.trim() },
+ artistUri = credited.firstNotNullOfOrNull { it.uri },
+ artists = credited,
+ album = item.album?.name.orEmpty(),
+ albumUri = item.album?.id?.takeIf { it.startsWith("MPRE") }?.let { "$ALBUM_PREFIX$it" },
+ durationMs = item.duration?.takeIf { it > 0 }?.times(1000L) ?: 0L,
+ explicit = item.explicit,
+ artworkUrl = item.thumbnail.atSize(COVER_SIZE),
+ )
+ }
override suspend fun tracksOf(uri: String): List<CatalogTrack> = withContext(Dispatchers.IO) {
val id = uri.substringAfterLast(':')
when {
- uri.startsWith(TRACK_PREFIX) -> emptyList()
+ uri.startsWith(TRACK_PREFIX) || uri.startsWith(VIDEO_PREFIX) -> emptyList()
+
+ // Videos stay videos: no swapping them for their songs here, which
+ // is the whole reason somebody opened this list.
+ uri.startsWith(VIDEOS_PREFIX) -> YouTube.playlist(id).getOrThrow().songs.map(::toVideoTrack)
+
+ // A record opened from the player, by the album's own id.
+ uri.startsWith(ALBUM_PREFIX) -> YouTube.album(id).getOrThrow().songs.map(::toCatalogTrack)
// An artist has no track list of its own, so what "open an artist"
// means here is the same as everywhere else: their best-known
@@ -450,17 +477,160 @@ class YouTubeBackend(private val account: YouTubeAccount) : MusicBackend {
.map(::toCatalogTrack)
}
- else -> YouTube.playlist(id).getOrThrow()
- .songs
- .onEach { song ->
+ else -> {
+ val listed = YouTube.playlist(id).getOrThrow().songs
+ val songs = asSongs(listed)
+ listed.zip(songs).forEach { (entry, song) ->
// What taking a song out of this list will need: YouTube
- // removes an entry, not a song, and names the entry here.
- song.setVideoId?.let { entries["$id/${song.id}"] = it }
+ // removes an entry, not a song, and names the entry here —
+ // under the song it now plays as, too, if it was a video.
+ entry.setVideoId?.let {
+ entries["$id/${entry.id}"] = it
+ entries["$id/${song.id}"] = it
+ }
}
- .map(::toCatalogTrack)
+ songs.map(::toCatalogTrack)
+ }
+ }
+ }
+
+ /** What [songFor] found for a video, including that it found nothing. */
+ private class Counterpart(val song: SongItem?)
+
+ private val counterparts = java.util.concurrent.ConcurrentHashMap<String, Counterpart>()
+
+ /**
+ * [items] with every music video swapped for the song it is a video of.
+ *
+ * A video saved to a list plays its own soundtrack: the intro, the skit in
+ * the middle, the sound effects, and a title no lyrics site knows. YouTube
+ * Music has the record itself as a separate entry, and that is what should
+ * play. Looked up a few at a time and given up on quickly, so a long list
+ * opens at most a few seconds late the first time and at once after.
+ */
+ private suspend fun asSongs(items: List<SongItem>): List<SongItem> = coroutineScope {
+ val gate = kotlinx.coroutines.sync.Semaphore(COUNTERPART_PARALLELISM)
+ items.map { item ->
+ async {
+ if (!isVideo(item)) item
+ else gate.withPermit { songFor(item) } ?: item
+ }
+ }.awaitAll()
+ }
+
+ private fun isVideo(item: SongItem) =
+ !item.isEpisode && item.uploadEntityId == null &&
+ (item.musicVideoType == MUSIC_VIDEO_TYPE_OMV || item.musicVideoType == MUSIC_VIDEO_TYPE_UGC)
+
+ private suspend fun songFor(video: SongItem): SongItem? {
+ counterparts[video.id]?.let { return it.song }
+ val artist = video.artists.firstOrNull()?.name.orEmpty()
+ val title = songTitleOf(video.title, artist)
+ var answered = false
+ val found = kotlinx.coroutines.withTimeoutOrNull(COUNTERPART_TIMEOUT_MS) {
+ val results = YouTube.search("$title $artist".trim(), YouTube.SearchFilter.FILTER_SONG)
+ .getOrNull()?.items
+ answered = results != null
+ results.orEmpty()
+ .filterIsInstance<SongItem>()
+ .take(COUNTERPART_CANDIDATES)
+ .firstOrNull { isSongOf(video, title, it) }
+ }
+ // A timeout is not an answer: asked again next time.
+ if (found != null || answered) counterparts[video.id] = Counterpart(found)
+ if (found == null && answered) android.util.Log.i(TAG, "no song for video ${video.id} \"${video.title}\"")
+ return found
+ }
+
+ /** A video's title as a song's: no "(Official Video)", no "Artist - " in front. */
+ private fun songTitleOf(title: String, artist: String): String {
+ var clean = title.replace(VIDEO_TAG, " ")
+ if (artist.isNotEmpty()) {
+ clean = clean.replace(Regex("^\\s*${Regex.escape(artist)}\\s*[-–—:|]\\s*", RegexOption.IGNORE_CASE), "")
+ }
+ return clean.replace(Regex("\\s+"), " ").trim().ifEmpty { title }
+ }
+
+ private fun isSongOf(video: SongItem, title: String, song: SongItem): Boolean {
+ if (song.musicVideoType != null && song.musicVideoType != MUSIC_VIDEO_TYPE_ATV) return false
+ val wanted = squash(title)
+ val got = squash(song.title)
+ if (wanted.isEmpty() || got.isEmpty()) return false
+ val sameTitle = wanted == got || (kotlin.math.min(wanted.length, got.length) >= 4 && (wanted.contains(got) || got.contains(wanted)))
+ if (!sameTitle) return false
+ val channel = video.artists.map { squash(it.name).replace(CHANNEL_NOISE, "") }
+ val sameArtist = song.artists.any { credited ->
+ val name = squash(credited.name)
+ name.isNotEmpty() && (channel.any { it == name || it.contains(name) } || squash(video.title).contains(name))
+ }
+ if (!sameArtist) return false
+ val a = video.duration
+ val b = song.duration
+ return a == null || b == null || kotlin.math.abs(a - b) <= COUNTERPART_DURATION_SLACK_S
+ }
+
+ private fun squash(text: String) = text.lowercase().filter { it.isLetterOrDigit() }
+
+ /**
+ * An artist's page as YouTube Music lays it out: their best-known songs, then
+ * each shelf under its own heading — albums, singles, videos, performances,
+ * the lists they are in, the artists their fans also play.
+ */
+ suspend fun artistPage(uri: String): dev.lelonio.square.data.YouTubeArtistPage? = withContext(Dispatchers.IO) {
+ val id = publicArtistId(uri.removePrefix(ARTIST_PREFIX))
+ val page = runCatching { YouTube.artist(id).getOrThrow() }
+ .onFailure { android.util.Log.w(TAG, "artist $id", it) }
+ .getOrNull() ?: return@withContext null
+ artists[id] = ArtistFacts(channelId = page.artist.channelId ?: id, subscribed = page.isSubscribed)
+
+ // The first shelf of songs is the "top songs" list; any later one is
+ // videos, whose pictures are wide.
+ val top = page.sections.firstOrNull { section -> section.items.isNotEmpty() && section.items.all { it is SongItem } }
+ val shelves = page.sections.filter { it !== top }.mapNotNull { section ->
+ val more = moreUriOf(section.moreEndpoint?.browseId)
+ val songs = section.items.filterIsInstance<SongItem>()
+ if (songs.size == section.items.size && songs.isNotEmpty()) {
+ return@mapNotNull dev.lelonio.square.data.ArtistShelf(
+ title = section.title,
+ tracks = songs.map(::toVideoTrack),
+ moreUri = section.moreEndpoint?.browseId?.takeIf { it.startsWith("VL") }
+ ?.let { "$VIDEOS_PREFIX${it.removePrefix("VL")}" },
+ )
+ }
+ val items = section.items.mapNotNull { item ->
+ val playlist = toCatalogPlaylist(item) ?: return@mapNotNull null
+ SearchItem(
+ uri = playlist.uri,
+ title = playlist.name,
+ // What the reference writes under a record: the year.
+ subtitle = (item as? AlbumItem)?.year?.toString() ?: playlist.subtitle.orEmpty(),
+ artworkUrl = playlist.artworkUrl,
+ )
+ }
+ items.takeIf { it.isNotEmpty() }?.let {
+ dev.lelonio.square.data.ArtistShelf(title = section.title, items = it)
+ }
}
+ dev.lelonio.square.data.YouTubeArtistPage(
+ name = page.artist.title,
+ artworkUrl = page.artist.thumbnail?.atSize(HERO_SIZE),
+ description = page.description?.takeIf { it.isNotBlank() },
+ subscribers = page.subscriberCountText?.takeIf { it.isNotBlank() },
+ monthlyListeners = page.monthlyListenerCount?.takeIf { it.isNotBlank() },
+ topSongs = top?.items.orEmpty().filterIsInstance<SongItem>().map(::toCatalogTrack),
+ topSongsMoreUri = moreUriOf(top?.moreEndpoint?.browseId),
+ shelves = shelves,
+ )
}
+ /** A video as a video: its wide picture, and an address that plays the picture. */
+ private fun toVideoTrack(item: SongItem) =
+ toCatalogTrack(item).copy(uri = "$VIDEO_PREFIX${item.id}", artworkUrl = item.thumbnail)
+
+ /** A shelf's "see all", where it leads to a list this app can open. */
+ private fun moreUriOf(browseId: String?): String? =
+ browseId?.takeIf { it.startsWith("VL") }?.let { "$PLAYLIST_PREFIX${it.removePrefix("VL")}" }
+
/** The lists the library said the account can write to; see [canWriteTo]. */
private val editable = java.util.concurrent.ConcurrentHashMap.newKeySet<String>()
@@ -619,10 +789,31 @@ class YouTubeBackend(private val account: YouTubeAccount) : MusicBackend {
YouTubePlayerFactory.create(host)
companion object {
+ private const val TAG = "SquareYouTube"
+ private const val HERO_SIZE = 1200
+ private val BYLINE_JOINERS = setOf("&", "e", "and", "y", "et", "und", "x", "i", "и", "ve", "और", "feat.", "ft.")
+ private const val MUSIC_VIDEO_TYPE_OMV = "MUSIC_VIDEO_TYPE_OMV"
+ private const val MUSIC_VIDEO_TYPE_UGC = "MUSIC_VIDEO_TYPE_UGC"
+ private const val MUSIC_VIDEO_TYPE_ATV = "MUSIC_VIDEO_TYPE_ATV"
+ private const val COUNTERPART_PARALLELISM = 6
+ private const val COUNTERPART_TIMEOUT_MS = 5_000L
+ private const val COUNTERPART_CANDIDATES = 5
+ /** An intro or an outro the record does not have, at most. */
+ private const val COUNTERPART_DURATION_SLACK_S = 75
+ private val VIDEO_TAG = Regex(
+ "[(\\[][^)\\]]*(official|video|lyric|audio|visuali[sz]er|\\bmv\\b|\\bhd\\b|\\b4k\\b|clip)[^)\\]]*[)\\]]",
+ RegexOption.IGNORE_CASE,
+ )
+ private val CHANNEL_NOISE = Regex("vevo|topic|official|music|tv$")
const val URI_SCHEME = "ytmusic:"
const val TRACK_PREFIX = "ytmusic:track:"
const val PLAYLIST_PREFIX = "ytmusic:playlist:"
const val ARTIST_PREFIX = "ytmusic:artist:"
+ const val ALBUM_PREFIX = "ytmusic:album:"
+ /** A video played as one, picture and all, whatever the video button says. */
+ const val VIDEO_PREFIX = "ytmusic:video:"
+ /** A list of videos, opened from an artist's video shelf. */
+ const val VIDEOS_PREFIX = "ytmusic:videos:"
/** YouTube Music's own browse id for the account's playlist library. */
private const val LOG_TAG = "SquareYouTube"
diff --git a/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubeCredits.kt b/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubeCredits.kt
new file mode 100644
index 0000000..f7bbdeb
--- /dev/null
+++ b/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubeCredits.kt
@@ -0,0 +1,111 @@
+package dev.lelonio.square.backend.youtube
+
+import dev.lelonio.square.backend.spotify.SpotifyCredits
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.withContext
+import okhttp3.MediaType.Companion.toMediaType
+import okhttp3.OkHttpClient
+import okhttp3.Request
+import okhttp3.RequestBody.Companion.toRequestBody
+import org.json.JSONArray
+import org.json.JSONObject
+
+/**
+ * Who made a YouTube Music song, from the same page as "View song credits"
+ * there: performed by, written by, produced by, and whose metadata it is.
+ *
+ * The page answers without an account, so it is asked as YouTube Music's web
+ * client asks it, and handed to the panel in the shape the Spotify credits
+ * already have.
+ */
+object YouTubeCredits {
+
+ private val http = OkHttpClient()
+
+ /** Null when the song has none or anything goes wrong: a panel, not a feature. */
+ suspend fun of(trackUri: String): SpotifyCredits.Credits? = withContext(Dispatchers.IO) {
+ val videoId = YouTubeBackend.videoIdOfUri(trackUri).takeIf { it.isNotEmpty() }
+ ?: return@withContext null
+ runCatching {
+ val body = JSONObject()
+ .put(
+ "context",
+ JSONObject().put(
+ "client",
+ JSONObject()
+ .put("clientName", "WEB_REMIX")
+ .put("clientVersion", com.metrolist.innertube.models.YouTubeClient.WEB_REMIX.clientVersion)
+ .put("hl", java.util.Locale.getDefault().language),
+ ),
+ )
+ .put("browseId", "MPTC$videoId")
+ val request = Request.Builder()
+ .url("https://music.youtube.com/youtubei/v1/browse?prettyPrint=false")
+ .header("Origin", "https://music.youtube.com")
+ .post(body.toString().toRequestBody("application/json".toMediaType()))
+ .build()
+ val text = http.newCall(request).execute().use { response ->
+ if (!response.isSuccessful) return@withContext null
+ response.body?.string()
+ } ?: return@withContext null
+ parse(JSONObject(text))
+ }.onFailure { android.util.Log.w(TAG, "credits for $videoId", it) }.getOrNull()
+ }
+
+ private fun parse(root: JSONObject): SpotifyCredits.Credits? {
+ val dialog = find(root, "dismissableDialogRenderer") ?: return null
+ val title = dialog.optJSONObject("metadata")
+ ?.optJSONObject("musicMultiRowListItemRenderer")
+ ?.optJSONObject("title")
+ ?.let(::text)
+ .orEmpty()
+ val roles = mutableListOf<SpotifyCredits.Role>()
+ val sources = mutableListOf<String>()
+ val sections = dialog.optJSONArray("sections") ?: JSONArray()
+ for (i in 0 until sections.length()) {
+ val section = sections.optJSONObject(i)
+ ?.optJSONObject("dismissableDialogContentSectionRenderer") ?: continue
+ val heading = section.optJSONObject("title")?.let(::text).orEmpty()
+ val runs = section.optJSONObject("subtitle")?.optJSONArray("runs") ?: continue
+ val people = mutableListOf<SpotifyCredits.Person>()
+ for (r in 0 until runs.length()) {
+ val run = runs.optJSONObject(r) ?: continue
+ // One name per line; the lines between them are runs of their own.
+ val name = run.optString("text").trim().takeIf { it.isNotEmpty() } ?: continue
+ val artistId = run.optJSONObject("navigationEndpoint")
+ ?.optJSONObject("browseEndpoint")
+ ?.optString("browseId")
+ ?.takeIf { it.startsWith("UC") }
+ people += SpotifyCredits.Person(
+ name = name,
+ subroles = emptyList(),
+ uri = artistId?.let { "${YouTubeBackend.ARTIST_PREFIX}$it" },
+ )
+ }
+ // The last section names who supplied the data, which is the label.
+ if (heading.contains("provided by", ignoreCase = true) || i == sections.length() - 1 &&
+ heading.contains("metadata", ignoreCase = true)
+ ) {
+ sources += people.map { it.name }
+ } else if (people.isNotEmpty()) {
+ roles += SpotifyCredits.Role(heading, people)
+ }
+ }
+ return SpotifyCredits.Credits(title, roles, sources).takeUnless { it.isEmpty }
+ }
+
+ private fun text(runs: JSONObject): String {
+ val array = runs.optJSONArray("runs") ?: return runs.optString("simpleText")
+ return (0 until array.length()).joinToString("") { array.optJSONObject(it)?.optString("text").orEmpty() }
+ }
+
+ /** The first object under [key], however deep: the page nests it in an action. */
+ private fun find(node: Any?, key: String): JSONObject? = when (node) {
+ is JSONObject -> node.optJSONObject(key) ?: node.keys().asSequence()
+ .firstNotNullOfOrNull { find(node.opt(it), key) }
+ is JSONArray -> (0 until node.length()).firstNotNullOfOrNull { find(node.opt(it), key) }
+ else -> null
+ }
+
+ private const val TAG = "SquareYTCredits"
+}
diff --git a/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubeFadeController.kt b/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubeFadeController.kt
index 77386cf..f2f711f 100644
--- a/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubeFadeController.kt
+++ b/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubeFadeController.kt
@@ -1,76 +1,91 @@
package dev.lelonio.square.backend.youtube
import androidx.media3.common.MediaItem
+import androidx.media3.common.PlaybackException
+import androidx.media3.common.PlaybackParameters
import androidx.media3.common.Player
+import androidx.media3.common.util.UnstableApi
import androidx.media3.exoplayer.ExoPlayer
import dev.lelonio.square.data.CrossfadeStore
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
+import kotlinx.coroutines.SupervisorJob
+import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
+import kotlinx.coroutines.suspendCancellableCoroutine
+import kotlinx.coroutines.withTimeoutOrNull
+import kotlin.coroutines.resume
import kotlin.math.PI
import kotlin.math.cos
import kotlin.math.sin
/**
- * Manages smooth volume transitions (fade out / fade in) between tracks on YouTube Music.
+ * Dissolves one YouTube song into the next, the way the Spotify engine does.
*
- * ExoPlayer plays gapless sequentially; this controller applies equal-power volume
- * shaping as a track nears its end, and smoothly ramps the next track up upon transition.
+ * The main player owns the queue and moves to the next song as the dissolve
+ * begins. The song it leaves carries on in a second player, the tail, which has
+ * been playing the same song held back in the [PcmMixer]; the mixer swaps the
+ * two at the very sample, so the handover cannot be heard, and the two songs
+ * are then summed into one speaker on an equal-power curve.
*/
+@UnstableApi
class YouTubeFadeController(
private val player: ExoPlayer,
private val crossfadeStore: CrossfadeStore?,
+ private val mixer: PcmMixer,
+ private val mainSlot: MixerSlot,
+ private val tailSlot: MixerSlot,
+ private val createOutgoing: () -> ExoPlayer,
) : Player.Listener {
+ private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
+ private var ticker: Job? = null
+ private var transition: Job? = null
+ private var outgoing: ExoPlayer? = null
+ private var preparedItem: MediaItem? = null
+ private var changing = false
+ private var internalChange = false
- private val scope = CoroutineScope(Dispatchers.Main.immediate)
- private var tickerJob: Job? = null
- private var rampJob: Job? = null
- private var skipJob: Job? = null
- private var isFadingOut = false
+ /** Which transition owns the state; bumped by every new one and every cancel. */
+ private var generations = 0
- /**
- * Whether a change the listener asked for is being dissolved right now.
- *
- * While it is, this controller's own rules about the volume are off: the
- * ticker would put it back to full between the two halves of the fade, and
- * the transition and the seek that the change itself causes would each
- * bring the incoming song in at once.
- */
- private var skipping = false
+ init { player.addListener(this) }
- init {
- player.addListener(this)
+ override fun onIsPlayingChanged(isPlaying: Boolean) {
+ // Buffering the incoming song must not interrupt the outgoing audio.
+ syncTailPlayback()
+ if (!isPlaying) {
+ ticker?.cancel()
+ ticker = null
+ }
+ if (isPlaying && ticker?.isActive != true) {
+ ticker = scope.launch {
+ while (isActive) {
+ checkEnd()
+ delay(40)
+ }
+ }
+ }
}
override fun onPlaybackStateChanged(playbackState: Int) {
- if (playbackState == Player.STATE_READY && player.playWhenReady) {
- startTicker()
- } else if (playbackState == Player.STATE_ENDED || playbackState == Player.STATE_IDLE) {
- stopTicker()
- resetVolume()
+ if (!internalChange && (playbackState == Player.STATE_IDLE || playbackState == Player.STATE_ENDED)) {
+ cancelOverlap()
}
}
- override fun onPlayWhenReadyChanged(playWhenReady: Boolean, reason: Int) {
- if (playWhenReady && player.playbackState == Player.STATE_READY) {
- startTicker()
- } else {
- stopTicker()
- }
+ override fun onPlayWhenReadyChanged(playWhenReady: Boolean, reason: Int) = syncTailPlayback()
+
+ override fun onPlaybackSuppressionReasonChanged(playbackSuppressionReason: Int) = syncTailPlayback()
+
+ override fun onPlaybackParametersChanged(playbackParameters: PlaybackParameters) {
+ outgoing?.playbackParameters = playbackParameters
}
override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) {
- isFadingOut = false
- if (skipping) return
- val fadeMs = crossfadeStore?.durationMs() ?: 0
- if (fadeMs > 0) {
- rampUp(minOf(fadeMs / 2, 1500))
- } else {
- resetVolume()
- }
+ if (!internalChange) cancelOverlap()
}
override fun onPositionDiscontinuity(
@@ -78,129 +93,277 @@ class YouTubeFadeController(
newPosition: Player.PositionInfo,
reason: Int,
) {
- if (reason == Player.DISCONTINUITY_REASON_SEEK) {
- isFadingOut = false
- if (skipping) return
- resetVolume()
- }
+ if (!internalChange && reason == Player.DISCONTINUITY_REASON_SEEK) cancelOverlap()
}
- private fun startTicker() {
- if (tickerJob?.isActive == true) return
- tickerJob = scope.launch {
- while (isActive) {
- checkFade()
- delay(80)
- }
- }
+ private fun syncTailPlayback() {
+ outgoing?.playWhenReady = changing && player.playWhenReady && player.playbackSuppressionReason == 0
}
- private fun stopTicker() {
- tickerJob?.cancel()
- tickerJob = null
+ private fun prepareOutgoing(): ExoPlayer? {
+ val item = player.currentMediaItem ?: return null
+ val tail = outgoing ?: createOutgoing().also { instance ->
+ outgoing = instance
+ instance.addListener(object : Player.Listener {
+ override fun onPlayerError(error: PlaybackException) {
+ android.util.Log.w(TAG, "outgoing stream unavailable", error)
+ cancelOverlap()
+ }
+ })
+ }
+ if (preparedItem != item) {
+ preparedItem = item
+ tail.pause()
+ tail.repeatMode = Player.REPEAT_MODE_OFF
+ tail.playbackParameters = player.playbackParameters
+ tail.setMediaItem(item, player.currentPosition)
+ tail.prepare()
+ }
+ return tail
}
- private fun checkFade() {
- if (skipping) return
+ private fun checkEnd() {
+ if (changing || !player.isPlaying) return
val fadeMs = crossfadeStore?.durationMs() ?: 0
if (fadeMs <= 0) {
- if (player.volume != 1f) player.volume = 1f
+ if (preparedItem != null) cancelOverlap()
return
}
-
- if (rampJob?.isActive == true) return
-
- val duration = player.duration
- val position = player.currentPosition
- if (duration <= 0 || position <= 0 || duration <= fadeMs) return
-
- val remaining = duration - position
- if (remaining in 1..fadeMs) {
- isFadingOut = true
- val x = remaining.toFloat() / fadeMs.toFloat()
- // Equal-power curve: sin(x * pi / 2) so remaining=fadeMs -> 1.0, remaining=0 -> 0.0
- val volume = sin(x * (PI.toFloat() / 2f)).coerceIn(0f, 1f)
- player.volume = volume
- } else if (!isFadingOut && player.volume < 1f) {
- player.volume = 1f
+ val remaining = player.duration - player.currentPosition
+ if (player.duration <= fadeMs || remaining <= 0) return
+ val next = if (player.repeatMode == Player.REPEAT_MODE_ONE) player.currentMediaItemIndex
+ else player.nextMediaItemIndex
+ if (next < 0) return
+ // Resolve and buffer the tail ahead of time, without making it audible.
+ if (remaining <= fadeMs + 5_000) prepareOutgoing()
+ if (remaining <= fadeMs && outgoing?.playbackState == Player.STATE_READY) {
+ changeWith(fadeMs) { player.seekToDefaultPosition(next) }
}
}
- private fun rampUp(durationMs: Int) {
- rampJob?.cancel()
- if (durationMs <= 0) {
- resetVolume()
+ fun changeWith(fadeMs: Int, change: () -> Unit) {
+ if (changing) cancelOverlap()
+ if (fadeMs <= 0 || !player.isPlaying) {
+ cancelOverlap()
+ change()
return
}
- player.volume = 0f
- rampJob = scope.launch {
- val steps = 20
- val stepDelay = (durationMs / steps).toLong().coerceAtLeast(10L)
- for (i in 1..steps) {
- if (!isActive) break
- val x = i.toFloat() / steps.toFloat()
- // Equal-power fade in
- val volume = sin(x * (PI.toFloat() / 2f)).coerceIn(0f, 1f)
- player.volume = volume
- delay(stepDelay)
+ val tail = outgoing?.takeIf {
+ preparedItem == player.currentMediaItem && it.playbackState == Player.STATE_READY
+ }
+ // A skip far from the end has no tail buffered, and building one would delay
+ // the skip by a whole stream resolution: what the mixer already holds of
+ // the song is enough for the listener's short fade.
+ if (tail == null || !mainSlot.mixed) {
+ if (!crossWith(fadeMs, change)) dip(fadeMs, change)
+ return
+ }
+ changing = true
+ val generation = ++generations
+ transition = scope.launch {
+ try {
+ tailSlot.fade = 0f
+ tailSlot.held = true
+ tail.playbackParameters = player.playbackParameters
+ // A little ahead of what is heard, so that by the time the tail has
+ // piled up its first frames the main player has reached them.
+ tail.seekTo(player.currentPosition + TAIL_LEAD_MS)
+ tail.play()
+ val handed = withTimeoutOrNull(HANDOVER_WAIT_MS) {
+ suspendCancellableCoroutine { continuation ->
+ continuation.invokeOnCancellation { mixer.cancelHandOver() }
+ mixer.handOver(mainSlot, tailSlot) { ok ->
+ scope.launch { if (continuation.isActive) continuation.resume(ok) }
+ }
+ }
+ } ?: false
+ if (!handed) {
+ android.util.Log.w(TAG, "no handover, crossing what is buffered instead")
+ retire(tail)
+ val short = fadeMs.coerceAtMost(MAX_BUFFERED_FADE_MS)
+ if (mixer.detach(mainSlot, short)) {
+ mainSlot.fade = 0f
+ internalChange = true
+ try { change() } finally { internalChange = false }
+ rise(short)
+ } else {
+ dipNow(fadeMs, change)
+ }
+ return@launch
+ }
+ // The mixer has silenced the main player and opened the tail.
+ internalChange = true
+ try { change() } finally { internalChange = false }
+ android.util.Log.i(TAG, "overlap started: ${tail.currentMediaItem?.mediaId} -> ${player.currentMediaItem?.mediaId}, ${fadeMs}ms")
+ var elapsed = 0L
+ var previous = player.currentPosition
+ while (elapsed < fadeMs) {
+ delay(20)
+ if (!player.playWhenReady || player.playbackSuppressionReason != 0) {
+ tail.pause()
+ previous = player.currentPosition
+ continue
+ }
+ tail.play()
+ val position = player.currentPosition
+ if (player.isPlaying) elapsed += (position - previous).coerceAtLeast(0)
+ previous = position
+ val x = (elapsed.toFloat() / fadeMs).coerceIn(0f, 1f)
+ mainSlot.fade = sin(x * PI.toFloat() / 2)
+ tailSlot.fade = cos(x * PI.toFloat() / 2)
+ }
+ android.util.Log.i(TAG, "overlap completed")
+ } finally {
+ // A cancelled job reaches here only after whatever cancelled it has moved
+ // on, possibly into a new overlap: that one owns the state now.
+ if (generation == generations) {
+ transition = null
+ changing = false
+ retire(tail)
+ setMainFade(1f)
+ }
}
- player.volume = 1f
}
}
/**
- * Takes the song out, makes the listener's change, and brings in what it
- * lands on.
- *
- * One after the other rather than over each other: this player holds one
- * song at a time, so there is no moment where both are audible. What the
- * listener asked for is late by the length of the first half, which is why
- * that half is the shorter thing a dissolve can be and still be one.
- *
- * Nothing to fade out of, or the fade turned off, and the change happens
- * where it was asked for, on the caller's own thread.
+ * The engine's change of song: the outgoing one fades out of what the mixer
+ * holds of it while the player moves on at once, and the incoming one rises
+ * on its own clock, from when it is actually heard.
*/
- fun changeWith(fadeMs: Int, change: () -> Unit) {
- if (fadeMs <= 0 || !player.isPlaying || player.volume <= 0f) {
- change()
- return
+ private fun crossWith(fadeMs: Int, change: () -> Unit): Boolean {
+ if (!mainSlot.mixed || !mixer.detach(mainSlot, fadeMs)) return false
+ changing = true
+ val generation = ++generations
+ mainSlot.fade = 0f
+ internalChange = true
+ try { change() } finally { internalChange = false }
+ transition = scope.launch {
+ try {
+ rise(fadeMs)
+ } finally {
+ if (generation == generations) {
+ transition = null
+ changing = false
+ setMainFade(1f)
+ }
+ }
+ }
+ return true
+ }
+
+ /** Brings the main player up over [fadeMs] of its own playback. */
+ private suspend fun rise(fadeMs: Int) {
+ var elapsed = 0L
+ var previous = player.currentPosition
+ val deadline = System.currentTimeMillis() + RISE_GIVE_UP_MS
+ while (elapsed < fadeMs && System.currentTimeMillis() < deadline) {
+ delay(16)
+ val position = player.currentPosition
+ if (player.isPlaying) elapsed += (position - previous).coerceIn(0, 100)
+ previous = position
+ mainSlot.fade = sin((elapsed.toFloat() / fadeMs).coerceIn(0f, 1f) * PI.toFloat() / 2)
}
- skipJob?.cancel()
- skipping = true
- skipJob = scope.launch {
+ }
+
+ /** Out, change, in, on the main player alone: half of [fadeMs] each way. */
+ private fun dip(fadeMs: Int, change: () -> Unit) {
+ changing = true
+ val generation = ++generations
+ transition = scope.launch {
try {
- val half = (fadeMs / 2).coerceAtLeast(MIN_HALF_MS)
- val from = player.volume
- step(half) { x -> from * cos(x * (PI.toFloat() / 2f)) }
- player.volume = 0f
- change()
- step(half) { x -> sin(x * (PI.toFloat() / 2f)) }
- player.volume = 1f
+ dipNow(fadeMs, change)
} finally {
- skipping = false
+ if (generation == generations) {
+ transition = null
+ changing = false
+ setMainFade(1f)
+ }
}
}
}
- /** One half of a dissolve, as a curve read from nothing to everything. */
- private suspend fun step(durationMs: Int, curve: (Float) -> Float) {
- val steps = SKIP_STEPS
- val wait = (durationMs / steps).toLong().coerceAtLeast(8L)
- for (i in 1..steps) {
- val x = i.toFloat() / steps.toFloat()
- player.volume = curve(x).coerceIn(0f, 1f)
- delay(wait)
+ private suspend fun dipNow(fadeMs: Int, change: () -> Unit) {
+ val half = (fadeMs / 2).coerceIn(MIN_HALF_MS, MAX_HALF_MS)
+ ramp(half) { x -> setMainFade(cos(x * PI.toFloat() / 2)) }
+ setMainFade(0f)
+ internalChange = true
+ try { change() } finally { internalChange = false }
+ ramp(half) { x -> setMainFade(sin(x * PI.toFloat() / 2)) }
+ }
+
+ /** Through the mixer when the main player is in it, on the player itself otherwise. */
+ private fun setMainFade(value: Float) {
+ if (mainSlot.mixed) {
+ mainSlot.fade = value
+ if (player.volume != 1f) player.volume = 1f
+ } else {
+ mainSlot.fade = 1f
+ player.volume = value
}
}
- private fun resetVolume() {
- rampJob?.cancel()
- player.volume = 1f
+ private suspend fun ramp(durationMs: Int, step: (Float) -> Unit) {
+ var elapsed = 0
+ while (elapsed < durationMs) {
+ delay(16)
+ elapsed += 16
+ step((elapsed.toFloat() / durationMs).coerceIn(0f, 1f))
+ }
}
+
+ /**
+ * Silences the tail and holds it where it is.
+ *
+ * Not stopped: releasing its decoder while the main player is changing song
+ * aborts inside MediaCodec (`ubsan: sub-overflow` in `updateMediametrics`) on
+ * this hardware. The decoder goes when the tail is next given a song, seconds
+ * before an end, with the main player steady.
+ */
+ private fun retire(tail: ExoPlayer) {
+ tailSlot.fade = 0f
+ tailSlot.held = true
+ tail.pause()
+ preparedItem = null
+ }
+
+ private fun cancelOverlap() {
+ generations++
+ transition?.cancel()
+ transition = null
+ changing = false
+ mixer.cancelHandOver()
+ outgoing?.let(::retire)
+ setMainFade(1f)
+ }
+
+ fun release() {
+ player.removeListener(this)
+ scope.cancel()
+ outgoing?.release()
+ outgoing = null
+ }
+
+ /** After the main player is gone too: it writes into the mixer until then. */
+ fun releaseMixer() = mixer.release()
}
-/** How many volume steps each half of a listener's dissolve is made of. */
-private const val SKIP_STEPS = 16
+private const val TAG = "SquareYTFade"
-/** Shorter than this and a fade is a click with a delay in front of it. */
+/** Shorter than this and a dip is a click with a delay in front of it. */
private const val MIN_HALF_MS = 120
+
+/** A skip is late by this much at most: it is the listener's, not the song's end. */
+private const val MAX_HALF_MS = 400
+
+/** The most of a song the mixer holds ahead, with a margin: see PcmMixer. */
+private const val MAX_BUFFERED_FADE_MS = 1_500
+
+/** A song that will not start is not waited on with the volume down. */
+private const val RISE_GIVE_UP_MS = 20_000L
+
+/** How far ahead of the heard position the tail starts piling up. */
+private const val TAIL_LEAD_MS = 300L
+
+/** Longer than the mixer's own timeout, which is the one meant to fire. */
+private const val HANDOVER_WAIT_MS = 4_000L
diff --git a/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubePlayerFactory.kt b/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubePlayerFactory.kt
index 398797f..c5925cc 100644
--- a/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubePlayerFactory.kt
+++ b/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubePlayerFactory.kt
@@ -24,7 +24,7 @@ import org.schabi.newpipe.extractor.stream.StreamInfo
object YouTubePlayerFactory {
fun create(host: PlaybackHost): Player {
- val loadControl = androidx.media3.exoplayer.DefaultLoadControl.Builder()
+ fun loadControl() = androidx.media3.exoplayer.DefaultLoadControl.Builder()
.setBufferDurationsMs(
/* minBufferMs = */ 30_000,
/* maxBufferMs = */ 60_000,
@@ -36,21 +36,25 @@ object YouTubePlayerFactory {
val crossfadeStore = (host.context.applicationContext as? dev.lelonio.square.SquareApplication)?.crossfade
- return ExoPlayer.Builder(host.context, renderers(host))
- .setMediaSourceFactory(
- DefaultMediaSourceFactory(
- ResolvingDataSource.Factory(
- // Files as well as the network: a kept song resolves to
- // one on the phone, which an HTTP source cannot open.
- androidx.media3.datasource.DefaultDataSource.Factory(
- host.context,
- DefaultHttpDataSource.Factory(),
- ),
- YouTubeStreamResolver(host.context.applicationContext),
- ),
- ),
- )
- .setLoadControl(loadControl)
+ // One speaker for both players; see PcmMixer.
+ val mixer = PcmMixer()
+ val mainSlot = MixerSlot()
+ val tailSlot = MixerSlot(follows = mainSlot, initialFade = 0f).apply { held = true }
+
+ val dataSources = ResolvingDataSource.Factory(
+ // Files as well as the network: a kept song resolves to one on the
+ // phone, which an HTTP source cannot open.
+ androidx.media3.datasource.DefaultDataSource.Factory(
+ host.context,
+ DefaultHttpDataSource.Factory(),
+ ),
+ YouTubeStreamResolver(host.context.applicationContext),
+ )
+
+ fun buildPlayer(handleFocus: Boolean, slot: MixerSlot) =
+ ExoPlayer.Builder(host.context, renderers(host, MixerOutputProvider(host.context, mixer, slot)))
+ .setMediaSourceFactory(VideoAwareSourceFactory(DefaultMediaSourceFactory(dataSources), dataSources))
+ .setLoadControl(loadControl())
.setHandleAudioBecomingNoisy(true)
// Ducks and pauses for whatever else wants the speaker. The Spotify
// path gets this from its own output; ExoPlayer will not ask for focus
@@ -60,13 +64,29 @@ object YouTubePlayerFactory {
.setUsage(androidx.media3.common.C.USAGE_MEDIA)
.setContentType(androidx.media3.common.C.AUDIO_CONTENT_TYPE_MUSIC)
.build(),
- /* handleAudioFocus = */ true,
+ /* handleAudioFocus = */ handleFocus,
)
.setLooper(host.looper)
.build()
- .let { exo ->
+
+ return buildPlayer(true, mainSlot).let { exo ->
exo.addAnalyticsListener(Diagnostics)
- val fades = YouTubeFadeController(exo, crossfadeStore)
+ // Video for a video, sound for a song, as the queue moves.
+ exo.addListener(object : Player.Listener {
+ override fun onMediaItemTransition(mediaItem: androidx.media3.common.MediaItem?, reason: Int) {
+ YouTubeVideoMode.onItem(mediaItem?.mediaId)
+ // The next one asked for now, while this one plays.
+ val next = exo.nextMediaItemIndex
+ if (next >= 0) {
+ exo.getMediaItemAt(next).mediaId
+ .takeIf { it.startsWith(YouTubeBackend.URI_SCHEME) }
+ ?.let(YouTubeStreams::prefetch)
+ }
+ }
+ })
+ val fades = YouTubeFadeController(exo, crossfadeStore, mixer, mainSlot, tailSlot) {
+ buildPlayer(false, tailSlot)
+ }
val preferences = (host.context.applicationContext as? dev.lelonio.square.SquareApplication)
?.preferences
// The talking and the sketches around a music video; see SponsorBlock.
@@ -131,8 +151,43 @@ object YouTubePlayerFactory {
* has no such trouble, and it is what the Spotify path already effectively
* does through its own output.
*/
- private fun renderers(host: PlaybackHost) =
+ private fun renderers(
+ host: PlaybackHost,
+ output: androidx.media3.exoplayer.audio.AudioOutputProvider,
+ ) =
object : androidx.media3.exoplayer.DefaultRenderersFactory(host.context) {
+ // Video stays on the phone's hardware decoders: FFmpeg's are
+ // software, and a 1080p picture through them falls behind and stalls.
+ override fun buildVideoRenderers(
+ context: android.content.Context,
+ extensionRendererMode: Int,
+ mediaCodecSelector: androidx.media3.exoplayer.mediacodec.MediaCodecSelector,
+ enableDecoderFallback: Boolean,
+ eventHandler: android.os.Handler,
+ eventListener: androidx.media3.exoplayer.video.VideoRendererEventListener,
+ allowedVideoJoiningTimeMs: Long,
+ out: java.util.ArrayList<androidx.media3.exoplayer.Renderer>,
+ ) = super.buildVideoRenderers(
+ context,
+ EXTENSION_RENDERER_MODE_OFF,
+ mediaCodecSelector,
+ enableDecoderFallback,
+ eventHandler,
+ eventListener,
+ allowedVideoJoiningTimeMs,
+ out,
+ )
+
+ init {
+ // FFmpeg ahead of the phone's own decoders. Releasing a platform
+ // audio codec while another song is starting aborts the whole
+ // app on this hardware, inside MediaCodec's metrics
+ // (`ubsan: sub-overflow` in updateMediametrics); a decoder of the
+ // app's own has no such code to fall into. The platform ones stay
+ // behind it for whatever FFmpeg cannot decode.
+ setExtensionRendererMode(EXTENSION_RENDERER_MODE_PREFER)
+ }
+
override fun buildAudioSink(
context: android.content.Context,
enableFloatOutput: Boolean,
@@ -140,14 +195,18 @@ object YouTubePlayerFactory {
) = androidx.media3.exoplayer.audio.DefaultAudioSink.Builder(context)
.setEnableFloatOutput(enableFloatOutput)
.setEnableAudioTrackPlaybackParams(false)
+ .setAudioOutputProvider(output)
// Karaoke, ahead of Sonic. The default chain was all this sink
// had, so the dial moved and the voice stayed: the removal is a
// processor of this app's, and a chain that does not list it
// does not run it. Before the stretcher, because it works on
// what the two channels share and a stretched pair shares less.
.setAudioProcessorChain(
+ // Every stream at the mixer's rate, so a 44.1 kHz song and
+ // a 48 kHz one can be summed; see MixerRateProcessor.
androidx.media3.exoplayer.audio.DefaultAudioSink.DefaultAudioProcessorChain(
dev.lelonio.square.playback.VocalAudioProcessor(),
+ MixerRateProcessor(),
),
)
.build()
@@ -172,14 +231,23 @@ private class YouTubeStreamResolver(
override fun resolveDataSpec(dataSpec: DataSpec): DataSpec {
val uri = dataSpec.uri.toString()
- if (!uri.startsWith(YouTubeBackend.TRACK_PREFIX)) return dataSpec
+ if (!uri.startsWith(YouTubeBackend.TRACK_PREFIX) && !uri.startsWith(YouTubeBackend.VIDEO_PREFIX)) {
+ return dataSpec
+ }
+ // "#video" and "#audio" are the two halves of a video played in high
+ // quality; see VideoAwareSourceFactory.
+ val part = dataSpec.uri.fragment
+ val base = uri.substringBefore('#')
+ val video = part == null && YouTubeVideoMode.wantsVideo(base)
// A song kept on the phone plays from the phone. Not while the video is
// showing and there is a network to fetch it over: the file is the
// sound alone.
- val kept = dev.lelonio.square.download.YouTubeDownloads.fileFor(context, uri)
- if (kept != null &&
- (!YouTubeVideoMode.enabled.value || dev.lelonio.square.playback.OfflineMode.active.value)
+ val kept = dev.lelonio.square.download.YouTubeDownloads.fileFor(context, base)
+ // Never for half of a video: the picture is the official video's, and
+ // its sound has to be that video's too or the two drift apart.
+ if (kept != null && part == null &&
+ (!video || dev.lelonio.square.playback.OfflineMode.active.value)
) {
return dataSpec.withUri(android.net.Uri.fromFile(kept))
}
@@ -190,14 +258,36 @@ private class YouTubeStreamResolver(
// a YouTube track never builds one.
YouTubeStreams.ensureNewPipe()
- val videoId = YouTubeBackend.videoIdOfUri(uri)
- val info = StreamInfo.getInfo(ServiceList.YouTube, "${YouTubeStreams.WATCH_URL}$videoId")
+ val songId = YouTubeBackend.videoIdOfUri(base)
+ // A song watched rather than heard is its official video, sound and
+ // all; see YouTubeStreams.watchSource. A video is already itself.
+ val watching = part != null || video
+ val (videoId, info) = if (watching && base.startsWith(YouTubeBackend.TRACK_PREFIX)) {
+ YouTubeStreams.watchSource(songId)
+ } else {
+ songId to YouTubeStreams.info(songId)
+ }
+
+ // The picture alone, as sharp as is worth it on a phone: H.264 first,
+ // which every phone decodes in hardware, then whatever else there is.
+ if (part == PART_VIDEO) {
+ val picture = info.videoOnlyStreams
+ .filter { !it.content.isNullOrEmpty() && it.height in 1..MAX_VIDEO_HEIGHT }
+ .sortedWith(
+ compareByDescending<org.schabi.newpipe.extractor.stream.VideoStream> { it.height }
+ .thenByDescending { it.format == org.schabi.newpipe.extractor.MediaFormat.MPEG_4 },
+ )
+ .firstOrNull()?.content
+ ?: info.videoStreams.filter { !it.content.isNullOrEmpty() }.maxByOrNull { it.height }?.content
+ ?: error("nessun video per $videoId")
+ return dataSpec.withUri(android.net.Uri.parse(picture))
+ }
// With the video showing, one of YouTube's muxed streams: those are the
// only ones carrying picture and sound in a single file, and a single
// file is what a progressive source can play. They top out at 720p,
// which is the price of not building a DASH manifest here.
- val url = if (YouTubeVideoMode.enabled.value) {
+ val url = if (video && part == null) {
info.videoStreams
.filter { !it.content.isNullOrEmpty() }
.maxByOrNull { it.height }
@@ -208,9 +298,17 @@ private class YouTubeStreamResolver(
// Audio-only otherwise, best bitrate: a muxed stream would carry a video
// track nobody is going to look at, over a connection that is often the
// scarce resource. Also the fallback when a track has no muxed stream.
+ // AAC before Opus, even at a lower rate: the app decodes AAC itself
+ // (FFmpeg, see renderers), while Opus goes to the platform decoder,
+ // whose release aborts the app on this hardware.
?: info.audioStreams
.filter { !it.content.isNullOrEmpty() }
- .maxByOrNull { it.averageBitrate }
+ .sortedWith(
+ compareByDescending<org.schabi.newpipe.extractor.stream.AudioStream> {
+ it.format == org.schabi.newpipe.extractor.MediaFormat.M4A
+ }.thenByDescending { it.averageBitrate },
+ )
+ .firstOrNull()
?.content
?: error("nessuna traccia audio per $videoId")
@@ -219,10 +317,107 @@ private class YouTubeStreamResolver(
}
+/**
+ * Plays a video as two sources side by side: the picture alone and the sound
+ * alone. YouTube serves anything sharper than 360p only that way; the file that
+ * carries both is the small one.
+ *
+ * Decided when the item is added, so it applies to videos (which always want
+ * their picture) and to songs added while the video button is on.
+ */
+@UnstableApi
+private class VideoAwareSourceFactory(
+ private val base: DefaultMediaSourceFactory,
+ dataSources: androidx.media3.datasource.DataSource.Factory,
+) : androidx.media3.exoplayer.source.MediaSource.Factory by base {
+
+ private val progressive = androidx.media3.exoplayer.source.ProgressiveMediaSource.Factory(dataSources)
+
+ override fun createMediaSource(mediaItem: androidx.media3.common.MediaItem): androidx.media3.exoplayer.source.MediaSource {
+ val uri = mediaItem.localConfiguration?.uri?.toString() ?: return base.createMediaSource(mediaItem)
+ val ours = uri.startsWith(YouTubeBackend.TRACK_PREFIX) || uri.startsWith(YouTubeBackend.VIDEO_PREFIX)
+ if (!ours || !YouTubeVideoMode.wantsVideo(uri)) return base.createMediaSource(mediaItem)
+ fun half(part: String) = progressive.createMediaSource(
+ mediaItem.buildUpon().setUri("$uri#$part").build(),
+ )
+ return androidx.media3.exoplayer.source.MergingMediaSource(half(PART_VIDEO), half(PART_AUDIO))
+ }
+}
+
+private const val PART_VIDEO = "video"
+private const val PART_AUDIO = "audio"
+
+/** Past this a phone screen shows no difference, and the data plan does. */
+private const val MAX_VIDEO_HEIGHT = 1080
+
/** NewPipe's one-time setup, shared by the player and the downloads. */
object YouTubeStreams {
const val WATCH_URL = "https://www.youtube.com/watch?v="
+ private val infos = java.util.concurrent.ConcurrentHashMap<String, Pair<Long, StreamInfo>>()
+ private val watched = java.util.concurrent.ConcurrentHashMap<String, Pair<Long, Pair<String, StreamInfo>>>()
+ private val locks = java.util.concurrent.ConcurrentHashMap<String, Any>()
+
+ /** One fetch per key at a time: the two halves of a video ask at once. */
+ private fun lockFor(key: String): Any = locks.computeIfAbsent(key) { Any() }
+
+ /**
+ * What YouTube says about a video, kept for a while: the two halves of a
+ * video ask within milliseconds of each other, and the URLs stay good for
+ * hours.
+ */
+ fun info(videoId: String): StreamInfo = synchronized(lockFor(videoId)) {
+ val now = android.os.SystemClock.elapsedRealtime()
+ infos[videoId]?.takeIf { now - it.first < INFO_TTL_MS }?.let { return it.second }
+ ensureNewPipe()
+ val fresh = StreamInfo.getInfo(ServiceList.YouTube, "$WATCH_URL$videoId")
+ infos[videoId] = now to fresh
+ if (infos.size > 64) infos.entries.minByOrNull { it.value.first }?.let { infos.remove(it.key) }
+ fresh
+ }
+
+ /**
+ * What to show for a song when it is watched: its official video where
+ * there is one that can be played, the song's own upload otherwise. Decided
+ * once per song and kept, so the picture and the sound — asked for
+ * separately — always come from the same upload. A video that answers
+ * "age-restricted" cannot be watched without an account, and is passed over.
+ */
+ fun watchSource(songId: String): Pair<String, StreamInfo> = synchronized(lockFor("watch:$songId")) {
+ val now = android.os.SystemClock.elapsedRealtime()
+ watched[songId]?.takeIf { now - it.first < INFO_TTL_MS }?.let { return it.second }
+ val song = info(songId)
+ val chosen = runCatching { OfficialVideos.forSong(songId, song) }.getOrNull()
+ ?.let { id ->
+ runCatching { id to info(id) }
+ .onFailure { android.util.Log.i("SquareYTVideo", "official video $id unplayable: ${it.message}") }
+ .getOrNull()
+ }
+ ?: (songId to song)
+ watched[songId] = now to chosen
+ chosen
+ }
+
+ private val prefetcher = java.util.concurrent.Executors.newSingleThreadExecutor { r ->
+ Thread(r, "SquareYTPrefetch").apply { isDaemon = true }
+ }
+
+ /** Asks ahead for what [uri] will need, so the next item starts at once. */
+ fun prefetch(uri: String) {
+ val id = YouTubeBackend.videoIdOfUri(uri)
+ prefetcher.execute {
+ runCatching {
+ if (uri.startsWith(YouTubeBackend.TRACK_PREFIX) && YouTubeVideoMode.wantsVideo(uri)) {
+ watchSource(id)
+ } else {
+ info(id)
+ }
+ }
+ }
+ }
+
+ private const val INFO_TTL_MS = 60 * 60 * 1000L
+
@Volatile
private var initialised = false
diff --git a/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubeVideoMode.kt b/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubeVideoMode.kt
index 77719c0..fb4cfe5 100644
--- a/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubeVideoMode.kt
+++ b/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubeVideoMode.kt
@@ -30,11 +30,19 @@ object YouTubeVideoMode {
* queue, and the position is put back by hand because stopping loses it.
*/
fun toggle(player: Player) {
+ val onVideo = player.currentMediaItem?.mediaId?.startsWith(YouTubeBackend.VIDEO_PREFIX) == true
_enabled.value = !_enabled.value
+ // On a video the button means "this one as sound", and the mode goes
+ // back to following the queue once it moves on.
+ audioOnly = onVideo && !_enabled.value
+ byItem = onVideo && _enabled.value
val index = player.currentMediaItemIndex
val position = player.currentPosition
val wasPlaying = player.isPlaying
player.stop()
+ // A new source for the item, so it is built for the mode it is now in:
+ // the picture comes as its own stream; see VideoAwareSourceFactory.
+ player.currentMediaItem?.let { player.replaceMediaItem(index, it) }
player.seekTo(index, position)
player.prepare()
if (wasPlaying) player.play()
@@ -58,5 +66,36 @@ object YouTubeVideoMode {
/** Back to sound alone — for when the source or the track changes under it. */
fun reset() {
_enabled.value = false
+ byItem = false
+ audioOnly = false
}
+
+ /**
+ * Whether the mode is on because the item playing is a video, rather than
+ * because somebody pressed the button: it then goes off again with the item.
+ */
+ @Volatile
+ var byItem = false
+ private set
+
+ /** Set when the listener asked for the video playing to be sound alone. */
+ @Volatile
+ private var audioOnly = false
+
+ /** Follows the queue: on for a video, off again for the song after it. */
+ fun onItem(mediaId: String?) {
+ audioOnly = false
+ val video = mediaId?.startsWith(YouTubeBackend.VIDEO_PREFIX) == true
+ if (video && !_enabled.value) {
+ byItem = true
+ _enabled.value = true
+ } else if (!video && byItem) {
+ byItem = false
+ _enabled.value = false
+ }
+ }
+
+ /** Whether [uri] should open as video: a video always, a song when the button says so. */
+ fun wantsVideo(uri: String): Boolean =
+ if (uri.startsWith(YouTubeBackend.VIDEO_PREFIX)) !audioOnly else _enabled.value && !byItem
}
diff --git a/app/src/main/java/dev/lelonio/square/data/SearchResults.kt b/app/src/main/java/dev/lelonio/square/data/SearchResults.kt
index 85cf5bd..e21cc0d 100644
--- a/app/src/main/java/dev/lelonio/square/data/SearchResults.kt
+++ b/app/src/main/java/dev/lelonio/square/data/SearchResults.kt
@@ -94,3 +94,29 @@ fun SearchDto.toResults(
)
},
)
+
+/**
+ * One row of an artist page as the source lays it out: its own heading, in
+ * the source's own words and order.
+ */
+data class ArtistShelf(
+ val title: String,
+ /** Records, lists and artists, opened as pages. */
+ val items: List<SearchItem> = emptyList(),
+ /** Videos and performances, played where they are. */
+ val tracks: List<CatalogTrack> = emptyList(),
+ /** The whole of it, where the source has a page for that. */
+ val moreUri: String? = null,
+)
+
+/** Everything a YouTube Music artist page holds. */
+data class YouTubeArtistPage(
+ val name: String,
+ val artworkUrl: String?,
+ val description: String?,
+ val subscribers: String?,
+ val monthlyListeners: String?,
+ val topSongs: List<CatalogTrack>,
+ val topSongsMoreUri: String?,
+ val shelves: List<ArtistShelf>,
+)
diff --git a/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt b/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt
index ad20bd9..df491ea 100644
--- a/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt
@@ -257,6 +257,12 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
* Only the phone's own music can be in this state; see openLocalFiles.
*/
val needsPermission: Boolean = false,
+ /** YouTube's count of the channel's subscribers, as it words it. */
+ val subscribers: String? = null,
+ /** An artist page's rows in the source's own order; see ArtistShelf. */
+ val shelves: List<dev.lelonio.square.data.ArtistShelf> = emptyList(),
+ /** Where the top songs' "see all" leads, when the source has a list for it. */
+ val topSongsMoreUri: String? = null,
)
private val container get() = getApplication<SquareApplication>()
@@ -1856,7 +1862,8 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
/** Asked for the open track; a repeat for the same one is free. */
fun loadCredits(trackUri: String?) {
- if (trackUri == null || !trackUri.startsWith("spotify:track:")) {
+ val youtube = trackUri?.startsWith(dev.lelonio.square.backend.youtube.YouTubeBackend.TRACK_PREFIX) == true
+ if (trackUri == null || (!trackUri.startsWith("spotify:track:") && !youtube)) {
creditsJob?.cancel()
creditsFor = null
_credits.value = null
@@ -1870,7 +1877,11 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
_credits.value = null
_creditsLoading.value = true
creditsJob = viewModelScope.launch {
- val found = dev.lelonio.square.backend.spotify.SpotifyCredits.of(trackUri)
+ val found = if (youtube) {
+ dev.lelonio.square.backend.youtube.YouTubeCredits.of(trackUri)
+ } else {
+ dev.lelonio.square.backend.spotify.SpotifyCredits.of(trackUri)
+ }
if (creditsFor == trackUri) {
_credits.value = found
_creditsLoading.value = false
@@ -3583,6 +3594,31 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
// than page by page: the Spotify paths below are built
// around the Web API's paging and the access point's
// per-track lookups, neither of which exists here.
+ val youtube = container.activeBackend as? dev.lelonio.square.backend.youtube.YouTubeBackend
+ val artistPage = if (kind == DetailKind.ARTIST && youtube != null) {
+ youtube.artistPage(playlist.uri)
+ } else {
+ null
+ }
+ if (artistPage != null) {
+ if (isActive && _playlist.value.uri == playlist.uri) {
+ publishPlaylist(
+ base.copy(
+ name = base.name.ifEmpty { artistPage.name },
+ artworkUrl = artistPage.artworkUrl ?: base.artworkUrl,
+ tracks = artistPage.topSongs,
+ notes = base.notes ?: artistPage.description,
+ subscribers = artistPage.subscribers,
+ monthlyListeners = artistPage.monthlyListeners,
+ shelves = artistPage.shelves,
+ topSongsMoreUri = artistPage.topSongsMoreUri,
+ following = _playlist.value.following,
+ loading = false,
+ ),
+ )
+ }
+ return@launch
+ }
val tracks = container.activeBackend.tracksOf(playlist.uri)
if (isActive && _playlist.value.uri == playlist.uri) {
publishPlaylist(base.copy(tracks = tracks, loading = false))
diff --git a/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt b/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt
index 8d3bf19..5194a95 100644
--- a/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt
@@ -1139,6 +1139,14 @@ fun SquareApp(
// touches what is playing.
val landscape = androidx.compose.ui.platform.LocalConfiguration.current.orientation ==
android.content.res.Configuration.ORIENTATION_LANDSCAPE
+ // The rotation the video buttons asked for is let go of once there is no
+ // video: the app turns with the phone again.
+ val rotationContext = androidx.compose.ui.platform.LocalContext.current
+ LaunchedEffect(videoOn || spotifyVideoOn) {
+ if (!(videoOn || spotifyVideoOn)) {
+ dev.lelonio.square.ui.player.lockVideoOrientation(rotationContext, null)
+ }
+ }
if (landscape && (videoOn || spotifyVideoOn) && player != null) {
SquareTheme(seed = accent) {
dev.lelonio.square.ui.player.FullScreenVideo(
@@ -2786,7 +2794,13 @@ fun SquareApp(
// resolved before the app kept it — it is
// looked up on the tap rather than left inert.
onOpenAlbum = playback.mediaId
- ?.takeIf { it.startsWith("spotify:track:") }
+ ?.takeIf {
+ it.startsWith("spotify:track:") ||
+ // YouTube has no lookup to fall back
+ // on: the record's address or nothing.
+ (it.startsWith(dev.lelonio.square.backend.youtube.YouTubeBackend.TRACK_PREFIX) &&
+ playback.albumUri != null)
+ }
?.let { track ->
{
scope.launch {
@@ -3064,8 +3078,10 @@ fun SquareApp(
// A video is streamed, so there is
// nothing to offer offline.
!offlineNow &&
- playback.mediaId
- ?.startsWith("ytmusic:track:") == true
+ playback.mediaId?.let { id ->
+ id.startsWith("ytmusic:track:") ||
+ id.startsWith(dev.lelonio.square.backend.youtube.YouTubeBackend.VIDEO_PREFIX)
+ } == true
}
?.let { { YouTubeVideoMode.toggle(it) } },
videoOn = videoOn || spotifyVideoOn,
diff --git a/app/src/main/java/dev/lelonio/square/ui/library/PlaylistScreen.kt b/app/src/main/java/dev/lelonio/square/ui/library/PlaylistScreen.kt
index 33708d4..945f882 100644
--- a/app/src/main/java/dev/lelonio/square/ui/library/PlaylistScreen.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/library/PlaylistScreen.kt
@@ -140,6 +140,8 @@ import com.adamglin.phosphoricons.bold.DotsThree
import com.adamglin.phosphoricons.bold.Plus
import com.adamglin.phosphoricons.bold.Check
import com.adamglin.phosphoricons.bold.Star
+import com.adamglin.phosphoricons.bold.Users
+import com.adamglin.phosphoricons.bold.Waveform
import com.adamglin.phosphoricons.bold.ArrowsDownUp
import com.adamglin.phosphoricons.bold.ArrowCircleDown
import com.adamglin.phosphoricons.fill.Shuffle
@@ -640,6 +642,8 @@ fun PlaylistScreen(
onToggleFollow = onToggleFollow,
infoOpen = infoOpen,
onToggleInfo = { infoOpen = !infoOpen },
+ subscribers = state.subscribers,
+ monthlyListeners = state.monthlyListeners,
pageColor = pageColor,
backdrop = pageBackdrop,
collapse = collapseFraction,
@@ -740,7 +744,8 @@ fun PlaylistScreen(
followers = state.followers,
genres = state.genres,
origin = state.origin,
- bio = state.notes,
+ // Shown on the page itself now, under "About".
+ bio = null,
)
}
}
@@ -777,6 +782,23 @@ fun PlaylistScreen(
}
}
+ // Who they are, a few lines of it and a word to open the rest, on
+ // the page rather than behind the "i": the reference leads with it.
+ if (isArtist && query.isBlank()) {
+ state.notes?.takeIf { it.isNotBlank() }?.let { notes ->
+ item(contentType = "artistNotes") {
+ Column(Modifier.padding(top = 14.dp)) {
+ Text(
+ stringResource(R.string.about),
+ style = MaterialTheme.typography.titleMedium,
+ modifier = Modifier.padding(start = 24.dp),
+ )
+ EditorialNotes(notes)
+ }
+ }
+ }
+ }
+
if (state.tracks.isNotEmpty()) {
item(contentType = "sectionTitle") {
SectionHeader(
@@ -793,9 +815,25 @@ fun PlaylistScreen(
// Only where there is more than what is shown.
expandable = isArtist &&
query.isBlank() &&
- visible.size > TOP_SONGS,
+ (visible.size > TOP_SONGS || state.topSongsMoreUri != null),
expanded = topSongsOpen,
- onToggle = { topSongsOpen = !topSongsOpen },
+ onToggle = {
+ // The whole list, where the source has one; the
+ // rest of what is already here otherwise.
+ val more = state.topSongsMoreUri
+ if (more != null) {
+ onOpenItem(
+ dev.lelonio.square.data.SearchItem(
+ uri = more,
+ title = state.name,
+ subtitle = "",
+ artworkUrl = state.artworkUrl,
+ ),
+ )
+ } else {
+ topSongsOpen = !topSongsOpen
+ }
+ },
)
}
}
@@ -944,6 +982,44 @@ fun PlaylistScreen(
// past the artist to reach the artist. Albums, then singles, then
// the records that are somebody else's, then what Spotify built
// around them — the order is how much of the artist is in each.
+ // The source's own rows, in its order and under its headings.
+ if (query.isBlank()) {
+ state.shelves.forEachIndexed { index, shelf ->
+ item(key = "shelf $index ${shelf.title}", contentType = "shelf") {
+ val more = shelf.moreUri?.let { uri ->
+ {
+ // Named for whose it is, under their picture:
+ // "Video" alone, on a blank page, said nothing.
+ onOpenItem(
+ dev.lelonio.square.data.SearchItem(
+ uri = uri,
+ title = "${state.name} · ${shelf.title}",
+ subtitle = state.name,
+ artworkUrl = state.artworkUrl
+ ?: shelf.tracks.firstOrNull()?.artworkUrl
+ ?: shelf.items.firstOrNull()?.artworkUrl,
+ ),
+ )
+ }
+ }
+ if (shelf.tracks.isNotEmpty()) {
+ VideoStrip(
+ shelf = shelf,
+ onPlay = { at -> onPlay(shelf.tracks, at, false) },
+ onMore = more,
+ )
+ } else {
+ AlbumStrip(
+ albums = shelf.items,
+ title = shelf.title,
+ onOpen = onOpenItem,
+ onMore = more,
+ )
+ }
+ }
+ }
+ }
+
if (state.albums.isNotEmpty() && query.isBlank()) {
item(contentType = "albums") {
AlbumStrip(
@@ -1755,6 +1831,9 @@ private fun ArtistHeader(
onToggleFollow: () -> Unit,
infoOpen: Boolean,
onToggleInfo: () -> Unit,
+ /** The channel's subscribers, as YouTube words the number; null elsewhere. */
+ subscribers: String?,
+ monthlyListeners: String?,
pageColor: Color,
backdrop: Backdrop,
collapse: () -> Float,
@@ -1908,6 +1987,28 @@ private fun ArtistHeader(
}
}
+
+ // How big they are, in the two numbers the reference puts under
+ // the buttons: who follows the channel and who listens in a month.
+ val subscriberLine = subscribers?.let { count ->
+ if (count.any { it.isWhitespace() }) count else stringResource(R.string.subscribers_count, count)
+ }
+ val monthlyLine = monthlyListeners?.let { count ->
+ if (count.any { it.isLetter() }) {
+ count
+ } else {
+ pluralStringResource(R.plurals.monthly_listeners, 2, count)
+ }
+ }
+ if (subscriberLine != null || monthlyLine != null) {
+ Row(
+ Modifier.padding(top = 16.dp),
+ horizontalArrangement = Arrangement.spacedBy(8.dp),
+ ) {
+ subscriberLine?.let { StatPill(PhosphorIcons.Bold.Users, it, ink) }
+ monthlyLine?.let { StatPill(PhosphorIcons.Bold.Waveform, it, ink) }
+ }
+ }
}
CollapsedBar(
@@ -2269,13 +2370,11 @@ private fun AlbumStrip(
albums: List<dev.lelonio.square.data.SearchItem>,
title: String,
onOpen: (dev.lelonio.square.data.SearchItem) -> Unit,
+ /** The whole shelf, where the source has a page for it. */
+ onMore: (() -> Unit)? = null,
) {
Column(Modifier.padding(top = 18.dp)) {
- Text(
- title,
- style = MaterialTheme.typography.titleMedium,
- modifier = Modifier.padding(start = 24.dp, bottom = 10.dp),
- )
+ ShelfTitle(title, onMore)
androidx.compose.foundation.lazy.LazyRow(
contentPadding = PaddingValues(horizontal = 24.dp),
horizontalArrangement = Arrangement.spacedBy(14.dp),
@@ -2286,13 +2385,16 @@ private fun AlbumStrip(
.width(132.dp)
.pressable({ onOpen(album) }),
) {
+ // A person is a portrait, round; a record is a sleeve.
+ val round = album.uri.contains(":artist:")
+ val corner = if (round) 66.dp else 14.dp
Artwork(
url = album.artworkUrl,
title = album.title,
modifier = Modifier
.size(132.dp)
- .softShadow(RoundedCornerShape(14.dp), elevation = 14.dp),
- corner = 14.dp,
+ .softShadow(RoundedCornerShape(corner), elevation = 14.dp),
+ corner = corner,
decodeSize = 132.dp,
)
Text(
@@ -2315,6 +2417,114 @@ private fun AlbumStrip(
}
}
+/** A shelf's heading, with the way to all of it at the end where there is one. */
+@Composable
+private fun ShelfTitle(title: String, onMore: (() -> Unit)?) {
+ Row(
+ Modifier
+ .fillMaxWidth()
+ .padding(start = 24.dp, end = 18.dp, bottom = 10.dp),
+ verticalAlignment = Alignment.CenterVertically,
+ ) {
+ Text(
+ title,
+ style = MaterialTheme.typography.titleMedium,
+ modifier = Modifier.weight(1f),
+ maxLines = 1,
+ overflow = TextOverflow.Ellipsis,
+ )
+ if (onMore != null) {
+ val shape = RoundedCornerShape(percent = 50)
+ Text(
+ stringResource(R.string.show_all),
+ style = MaterialTheme.typography.labelMedium,
+ color = MaterialTheme.colorScheme.onSurface,
+ modifier = Modifier
+ .clip(shape)
+ .background(MaterialTheme.colorScheme.onSurface.copy(alpha = 0.10f))
+ .pressable(onMore, shape = shape)
+ .padding(horizontal = 12.dp, vertical = 6.dp),
+ )
+ }
+ }
+}
+
+/** One of the two numbers under an artist's buttons. */
+@Composable
+private fun StatPill(icon: androidx.compose.ui.graphics.vector.ImageVector, text: String, ink: Color) {
+ val shape = RoundedCornerShape(percent = 50)
+ Row(
+ Modifier
+ .clip(shape)
+ .background(ink.copy(alpha = 0.10f))
+ .border(0.6.dp, ink.copy(alpha = 0.28f), shape)
+ .padding(horizontal = 12.dp, vertical = 6.dp),
+ verticalAlignment = Alignment.CenterVertically,
+ ) {
+ Icon(icon, contentDescription = null, tint = ink.copy(alpha = 0.9f), modifier = Modifier.size(14.dp))
+ Text(
+ text,
+ style = MaterialTheme.typography.labelMedium,
+ color = ink,
+ maxLines = 1,
+ modifier = Modifier.padding(start = 6.dp),
+ )
+ }
+}
+
+/**
+ * Videos and performances: wide pictures, because that is what they are, and
+ * played where they are rather than opened as a page.
+ */
+@Composable
+private fun VideoStrip(
+ shelf: dev.lelonio.square.data.ArtistShelf,
+ onPlay: (Int) -> Unit,
+ onMore: (() -> Unit)?,
+) {
+ Column(Modifier.padding(top = 18.dp)) {
+ ShelfTitle(shelf.title, onMore)
+ androidx.compose.foundation.lazy.LazyRow(
+ contentPadding = PaddingValues(horizontal = 24.dp),
+ horizontalArrangement = Arrangement.spacedBy(14.dp),
+ ) {
+ itemsIndexed(items = shelf.tracks, key = { index, track -> "$index ${track.uri}" }) { index, track ->
+ Column(
+ Modifier
+ .width(200.dp)
+ .pressable({ onPlay(index) }),
+ ) {
+ Artwork(
+ url = track.artworkUrl,
+ title = track.name,
+ modifier = Modifier
+ .size(width = 200.dp, height = 112.dp)
+ .softShadow(RoundedCornerShape(12.dp), elevation = 12.dp),
+ corner = 12.dp,
+ decodeSize = 200.dp,
+ )
+ Text(
+ track.name,
+ style = MaterialTheme.typography.bodyMedium,
+ maxLines = 1,
+ overflow = TextOverflow.Ellipsis,
+ modifier = Modifier.padding(top = 8.dp),
+ )
+ if (track.artist.isNotBlank()) {
+ Text(
+ track.artist,
+ style = MaterialTheme.typography.bodySmall,
+ color = MaterialTheme.colorScheme.onSurfaceVariant,
+ maxLines = 1,
+ overflow = TextOverflow.Ellipsis,
+ )
+ }
+ }
+ }
+ }
+ }
+}
+
@Composable
private fun SectionHeader(
title: String,
diff --git a/app/src/main/java/dev/lelonio/square/ui/player/FullScreenVideo.kt b/app/src/main/java/dev/lelonio/square/ui/player/FullScreenVideo.kt
index 5d5c081..c7f5254 100644
--- a/app/src/main/java/dev/lelonio/square/ui/player/FullScreenVideo.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/player/FullScreenVideo.kt
@@ -25,6 +25,7 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
+import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
@@ -40,6 +41,8 @@ import com.adamglin.phosphoricons.fill.Play
import com.adamglin.phosphoricons.fill.SkipBack
import com.adamglin.phosphoricons.fill.SkipForward
import dev.lelonio.square.R
+import com.adamglin.phosphoricons.Regular
+import com.adamglin.phosphoricons.regular.CornersIn
import dev.lelonio.square.ui.library.formatDuration
/**
@@ -148,6 +151,34 @@ fun FullScreenVideo(
)
}
+ // Back to upright, with the controls: the way out for a rotation the
+ // button asked for, which turning the phone does not undo.
+ val context = androidx.compose.ui.platform.LocalContext.current
+ androidx.compose.animation.AnimatedVisibility(
+ visible = showControls,
+ enter = androidx.compose.animation.fadeIn(androidx.compose.animation.core.tween(180)),
+ exit = androidx.compose.animation.fadeOut(androidx.compose.animation.core.tween(220)),
+ modifier = Modifier
+ .align(Alignment.TopEnd)
+ .padding(horizontal = 20.dp, vertical = 16.dp),
+ ) {
+ Box(
+ Modifier
+ .size(40.dp)
+ .clip(androidx.compose.foundation.shape.CircleShape)
+ .background(Color.Black.copy(alpha = 0.4f))
+ .clickable { lockVideoOrientation(context, landscape = false) },
+ contentAlignment = Alignment.Center,
+ ) {
+ Icon(
+ PhosphorIcons.Regular.CornersIn,
+ contentDescription = stringResource(R.string.exit_full_screen),
+ tint = Color.White,
+ modifier = Modifier.size(20.dp),
+ )
+ }
+ }
+
// What is playing, at the top, with the controls: on a screen with no
// cover and no page around it, this is the only thing naming the song.
androidx.compose.animation.AnimatedVisibility(
@@ -227,3 +258,19 @@ fun FullScreenVideo(
/** How long the controls stay up before getting out of the picture. */
private const val CONTROLS_LINGER_MS = 3_500L
+
+/**
+ * Turns the app for the video: [landscape] true lies it on its side whichever
+ * way the phone is held, false stands it back up, null hands the decision back
+ * to the phone.
+ */
+fun lockVideoOrientation(context: android.content.Context, landscape: Boolean?) {
+ var at: android.content.Context? = context
+ while (at is android.content.ContextWrapper && at !is android.app.Activity) at = at.baseContext
+ val activity = at as? android.app.Activity ?: return
+ activity.requestedOrientation = when (landscape) {
+ true -> android.content.pm.ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
+ false -> android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
+ null -> android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
+ }
+}
diff --git a/app/src/main/java/dev/lelonio/square/ui/player/PlayerScreen.kt b/app/src/main/java/dev/lelonio/square/ui/player/PlayerScreen.kt
index 6f06d9f..458555a 100644
--- a/app/src/main/java/dev/lelonio/square/ui/player/PlayerScreen.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/player/PlayerScreen.kt
@@ -112,6 +112,7 @@ import dev.lelonio.square.ui.theme.softShadow
import com.adamglin.phosphoricons.regular.MonitorPlay
import com.adamglin.phosphoricons.regular.MusicNotes
import com.adamglin.PhosphorIcons
+import com.adamglin.phosphoricons.regular.DeviceRotate
import com.adamglin.phosphoricons.Fill
import com.adamglin.phosphoricons.Regular
import com.adamglin.phosphoricons.fill.Heart
@@ -995,20 +996,50 @@ fun PlayerScreen(
// is what fills the slot when no panel is open.
Stage.COVER -> Box(Modifier.fillMaxSize())
- Stage.VIDEO -> Box(
+ Stage.VIDEO -> Column(
Modifier.fillMaxSize(),
- contentAlignment = Alignment.Center,
+ verticalArrangement = Arrangement.Center,
+ horizontalAlignment = Alignment.CenterHorizontally,
) {
// Nullable even in this stage: the crossfade
// keeps drawing the one it is leaving, and
// the controller it holds is gone the moment
// the activity stops.
- videoPlayer?.let {
- VideoStage(
- it,
- videoAttachKey,
- protectedContent = videoMode,
- ) { ambient = it }
+ Box(contentAlignment = Alignment.Center) {
+ videoPlayer?.let {
+ VideoStage(
+ it,
+ videoAttachKey,
+ protectedContent = videoMode,
+ ) { ambient = it }
+ }
+ }
+ // Under the picture, at its edge, the way into
+ // the whole screen without turning the phone.
+ val context = androidx.compose.ui.platform.LocalContext.current
+ Row(
+ Modifier
+ .fillMaxWidth()
+ .padding(top = 10.dp, end = 4.dp),
+ horizontalArrangement = Arrangement.End,
+ ) {
+ Box(
+ Modifier
+ .size(36.dp)
+ .clip(CircleShape)
+ .background(Color.Black.copy(alpha = 0.35f))
+ .pressable(onClick = {
+ dev.lelonio.square.ui.player.lockVideoOrientation(context, landscape = true)
+ }),
+ contentAlignment = Alignment.Center,
+ ) {
+ Icon(
+ com.adamglin.PhosphorIcons.Regular.DeviceRotate,
+ contentDescription = stringResource(R.string.rotate_video),
+ tint = Color.White,
+ modifier = Modifier.size(18.dp),
+ )
+ }
}
}
diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml
index 16076b1..6934bfa 100644
--- a/app/src/main/res/values-de/strings.xml
+++ b/app/src/main/res/values-de/strings.xml
@@ -472,4 +472,7 @@
<string name="search_library">Bibliothek durchsuchen</string>
<string name="skip_fade">Überblenden beim Wechseln</string>
<string name="skip_fade_note">Der übersprungene oder ersetzte Titel blendet aus, während der gewählte aufkommt. Bei Spotify überlappen sich beide, bei YouTube Music folgt einer auf den anderen.</string>
+ <string name="subscribers_count">%s Abonnenten</string>
+ <string name="exit_full_screen">Vollbild beenden</string>
+ <string name="rotate_video">Ins Vollbild drehen</string>
</resources>
diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml
index 99f91e6..9f2f0d2 100644
--- a/app/src/main/res/values-es/strings.xml
+++ b/app/src/main/res/values-es/strings.xml
@@ -478,4 +478,7 @@
<string name="mood_mixes">Mixes por estado de ánimo</string>
<string name="skip_fade">Fundido al cambiar de canción</string>
<string name="skip_fade_note">La canción que saltas o sustituyes se desvanece mientras sube la que has elegido. Con Spotify las dos se superponen; en YouTube Music una sigue a la otra.</string>
+ <string name="subscribers_count">%s suscriptores</string>
+ <string name="exit_full_screen">Salir de pantalla completa</string>
+ <string name="rotate_video">Girar a pantalla completa</string>
</resources>
diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml
index 56d2640..26cdab9 100644
--- a/app/src/main/res/values-fr/strings.xml
+++ b/app/src/main/res/values-fr/strings.xml
@@ -472,4 +472,7 @@
<string name="search_library">Rechercher dans la bibliothèque</string>
<string name="skip_fade">Fondu au changement de morceau</string>
<string name="skip_fade_note">Le morceau que vous passez ou remplacez s\'efface pendant que celui choisi monte. Avec Spotify les deux se superposent, sur YouTube Music l\'un suit l\'autre.</string>
+ <string name="subscribers_count">%s abonnés</string>
+ <string name="exit_full_screen">Quitter le plein écran</string>
+ <string name="rotate_video">Pivoter en plein écran</string>
</resources>
diff --git a/app/src/main/res/values-hi/strings.xml b/app/src/main/res/values-hi/strings.xml
index 38de083..7312736 100644
--- a/app/src/main/res/values-hi/strings.xml
+++ b/app/src/main/res/values-hi/strings.xml
@@ -487,4 +487,7 @@
<string name="search_library">लाइब्रेरी में खोजें</string>
<string name="skip_fade">गाना बदलते समय फ़ेड</string>
<string name="skip_fade_note">जिस गाने को आप छोड़ते या बदलते हैं वह धीरे-धीरे शांत होता है और चुना हुआ गाना उभरता है. Spotify पर दोनों एक साथ चलते हैं, YouTube Music पर एक के बाद एक.</string>
+ <string name="subscribers_count">%s सब्सक्राइबर</string>
+ <string name="exit_full_screen">फ़ुल स्क्रीन से बाहर निकलें</string>
+ <string name="rotate_video">फ़ुल स्क्रीन के लिए घुमाएँ</string>
</resources>
diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml
index 8287e15..ca19901 100644
--- a/app/src/main/res/values-it/strings.xml
+++ b/app/src/main/res/values-it/strings.xml
@@ -473,4 +473,7 @@
<string name="search_library">Cerca nella libreria</string>
<string name="skip_fade">Dissolvenza al cambio brano</string>
<string name="skip_fade_note">Il brano che salti o sostituisci sfuma mentre sale quello che hai scelto. Con Spotify i due si sovrappongono, su YouTube Music uno segue l\'altro.</string>
+ <string name="subscribers_count">%s iscritti</string>
+ <string name="exit_full_screen">Esci da schermo intero</string>
+ <string name="rotate_video">Ruota a schermo intero</string>
</resources>
diff --git a/app/src/main/res/values-pt/strings.xml b/app/src/main/res/values-pt/strings.xml
index d02fdbf..5797e8e 100644
--- a/app/src/main/res/values-pt/strings.xml
+++ b/app/src/main/res/values-pt/strings.xml
@@ -472,4 +472,7 @@
<string name="search_library">Pesquisar na biblioteca</string>
<string name="skip_fade">Fade ao mudar de música</string>
<string name="skip_fade_note">A música que você pula ou substitui desaparece enquanto a escolhida sobe. No Spotify as duas se sobrepõem; no YouTube Music uma segue a outra.</string>
+ <string name="subscribers_count">%s inscritos</string>
+ <string name="exit_full_screen">Sair da tela cheia</string>
+ <string name="rotate_video">Girar para tela cheia</string>
</resources>
diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml
index cb05050..a03e914 100644
--- a/app/src/main/res/values-ru/strings.xml
+++ b/app/src/main/res/values-ru/strings.xml
@@ -499,4 +499,7 @@
<string name="mood_mixes">Миксы по настроению</string>
<string name="skip_fade">Плавная смена трека</string>
<string name="skip_fade_note">Трек, который вы переключаете или заменяете, затихает, пока поднимается выбранный. В источнике Spotify они накладываются, в YouTube Music идут друг за другом.</string>
+ <string name="subscribers_count">%s подписчиков</string>
+ <string name="exit_full_screen">Выйти из полноэкранного режима</string>
+ <string name="rotate_video">Повернуть на весь экран</string>
</resources>
diff --git a/app/src/main/res/values-tr/strings.xml b/app/src/main/res/values-tr/strings.xml
index d00d3d2..9fe6eb0 100644
--- a/app/src/main/res/values-tr/strings.xml
+++ b/app/src/main/res/values-tr/strings.xml
@@ -485,4 +485,7 @@
<string name="search_library">Kitaplıkta ara</string>
<string name="skip_fade">Şarkı değişiminde geçiş</string>
<string name="skip_fade_note">Atladığınız veya değiştirdiğiniz şarkı kısılırken seçtiğiniz şarkı yükselir. Spotify kaynağında ikisi üst üste biner, YouTube Music\'te biri diğerini izler.</string>
+ <string name="subscribers_count">%s abone</string>
+ <string name="exit_full_screen">Tam ekrandan çık</string>
+ <string name="rotate_video">Tam ekran için döndür</string>
</resources>
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 4b03ca5..682fc12 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -499,4 +499,7 @@
<string name="mood_mixes">Mood Mixes</string>
<string name="skip_fade">Fade when changing song</string>
<string name="skip_fade_note">A song you skip or replace fades out while the one you chose comes up. On the Spotify source the two overlap; on YouTube Music one follows the other.</string>
+ <string name="subscribers_count">%s subscribers</string>
+ <string name="exit_full_screen">Exit full screen</string>
+ <string name="rotate_video">Rotate to full screen</string>
</resources>
diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml
index 863ad04..36af3b5 100644
--- a/gradle/libs.versions.toml
+++ b/gradle/libs.versions.toml
@@ -31,6 +31,8 @@ media3-session = { module = "androidx.media3:media3-session", version.ref = "med
# The Canvas clip, and every note of the YouTube Music backend. Spotify audio
# still comes from the librespot engine instead.
media3-exoplayer = { module = "androidx.media3:media3-exoplayer", version.ref = "media3" }
+# Software audio decoding for the YouTube player; see YouTubePlayerFactory.renderers.
+media3-ffmpeg-decoder = { module = "org.jellyfin.media3:media3-ffmpeg-decoder", version = "1.9.0+1" }
media3-exoplayer-dash = { module = "androidx.media3:media3-exoplayer-dash", version.ref = "media3" }
media3-exoplayer-hls = { module = "androidx.media3:media3-exoplayer-hls", version.ref = "media3" }
media3-ui = { module = "androidx.media3:media3-ui", version.ref = "media3" }