Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -498,21 +498,21 @@ dependencies {

// Native in-app audio playback, background service, system media
// controls, Bluetooth/headset controls and a MediaController-backed UI.
implementation("androidx.media3:media3-exoplayer:1.2.1")
implementation("androidx.media3:media3-datasource-okhttp:1.2.1")
implementation("androidx.media3:media3-exoplayer-hls:1.2.1")
implementation("androidx.media3:media3-exoplayer:1.9.0")
implementation("androidx.media3:media3-datasource-okhttp:1.9.0")
implementation("androidx.media3:media3-exoplayer-hls:1.9.0")
// Segmented provider-module path: DASH chunk source + CDM decryption.
// Pinned to the same 1.2.1 line as exoplayer/hls to avoid binary mismatch.
implementation("androidx.media3:media3-exoplayer-dash:1.2.1")
// Pinned to the same 1.9.0 line as exoplayer/hls to match 16 KB FFmpeg.
implementation("androidx.media3:media3-exoplayer-dash:1.9.0")
// MediaBrowserServiceCompat/MediaSessionCompat bridge used by Android
// Auto to browse the LastWave library and control the same player.
implementation("androidx.media:media:1.7.0")

// GPLv3 Media3-matched FFmpeg software decoder (distribution must comply).
// GPLv3 Media3-matched 16 KB-aligned FFmpeg software decoder (distribution must comply).
// The renderer factory prefers FFmpeg for every codec it supports so all
// devices decode through one deterministic, OEM-bug-free path; platform
// decoders remain as automatic fallbacks.
implementation("org.jellyfin.media3:media3-ffmpeg-decoder:1.2.1+1")
implementation("org.jellyfin.media3:media3-ffmpeg-decoder:1.9.0+1")

// NIO also backports NewPipe's URL encoder/decoder overloads below API 33.
coreLibraryDesugaring("com.android.tools:desugar_jdk_libs_nio:2.0.4")
Expand Down
4 changes: 4 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@
-keepclassmembers class com.lastwave.app.data.podcast.** { *; }


# Keep Media3 FFmpeg extension classes constructed directly or via reflection.
-keep class androidx.media3.decoder.ffmpeg.FfmpegAudioRenderer { *; }
-keep class androidx.media3.decoder.ffmpeg.FfmpegLibrary { *; }

# Shrink and optimize, but do not rename classes/members. LastWave is open
# source, so renaming hides nothing, and the scrambled single-letter names
# (a.b.c) are what Huorong's static TrojanDropper/Android.Agent heuristic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,41 @@ class ModuleFlacTranscoder @Inject constructor(
enableFloatOutput: Boolean,
enableAudioTrackPlaybackParams: Boolean,
): androidx.media3.exoplayer.audio.AudioSink = sink

override fun buildAudioRenderers(
context: Context,
extensionRendererMode: Int,
mediaCodecSelector: androidx.media3.exoplayer.mediacodec.MediaCodecSelector,
enableDecoderFallback: Boolean,
audioSink: androidx.media3.exoplayer.audio.AudioSink,
eventHandler: android.os.Handler,
eventListener: androidx.media3.exoplayer.audio.AudioRendererEventListener,
out: ArrayList<androidx.media3.exoplayer.Renderer>,
) {
com.lastwave.app.playback.FfmpegCompat.appendAudioRenderers(
context = context,
mediaCodecSelector = mediaCodecSelector,
enableDecoderFallback = enableDecoderFallback,
audioSink = audioSink,
eventHandler = eventHandler,
eventListener = eventListener,
out = out,
) {
super.buildAudioRenderers(
context,
extensionRendererMode,
mediaCodecSelector,
enableDecoderFallback,
audioSink,
eventHandler,
eventListener,
out,
)
}
}
}.apply {
setExtensionRendererMode(com.lastwave.app.playback.FfmpegCompat.recommendedExtensionRendererMode())
setEnableDecoderFallback(true)
}
val mediaSourceFactory = DefaultMediaSourceFactory(context)
.setDrmSessionManagerProvider { drmFactory.sessionManagerFor(descriptor) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -863,30 +863,7 @@ internal fun TrackDownloadManager.hasExpectedContainer(file: File, extension: St
}
}.getOrDefault(false)

/**
* Mirrors playback's spatial check (`MusicPlayer.isSpatialAudioSupportedOnDevice`):
* hardware decoder, bundled FFmpeg decoder, or Android S_V2+ Spatializer.
*/
internal fun TrackDownloadManager.isAtmosCapableDevice(): Boolean = runCatching {
val decoder = MediaCodecList(MediaCodecList.ALL_CODECS).codecInfos.any { info ->
!info.isEncoder && info.supportedTypes.any { type ->
type.equals("audio/eac3-joc", ignoreCase = true) ||
type.equals("audio/eac3", ignoreCase = true) ||
type.equals("audio/ac3", ignoreCase = true)
}
}
val ffmpeg = com.lastwave.app.playback.FfmpegCompat.isUsable && runCatching {
androidx.media3.decoder.ffmpeg.FfmpegLibrary.isAvailable() &&
androidx.media3.decoder.ffmpeg.FfmpegLibrary.supportsFormat(androidx.media3.common.MimeTypes.AUDIO_E_AC3)
}.getOrDefault(false)
if (decoder || ffmpeg) return@runCatching true
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S_V2) {
val am = context.getSystemService(AudioManager::class.java)
val spatializer = am?.spatializer
if (spatializer != null && (spatializer.isAvailable || spatializer.isEnabled)) {
return@runCatching true
}
}
false
}.getOrDefault(false)
/** Mirrors playback's spatial check (`MusicPlayer.isSpatialAudioSupportedOnDevice`). */
internal fun TrackDownloadManager.isAtmosCapableDevice(): Boolean =
com.lastwave.app.playback.FfmpegCompat.isAtmosCapable()

37 changes: 34 additions & 3 deletions app/src/main/java/com/lastwave/app/playback/DirectTrackStream.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package com.lastwave.app.playback

import android.media.AudioAttributes
import android.media.AudioDeviceInfo
import android.media.AudioFormat
import android.media.AudioTrack
import android.media.PlaybackParams
Expand All @@ -47,7 +48,10 @@ import java.nio.ByteOrder
* track accepts them. [probeDirect] runs that test before playback starts.
*/
@RequiresApi(Build.VERSION_CODES.S)
class DirectTrackStream(private val attributes: AudioAttributes) {
class DirectTrackStream(
private val attributes: AudioAttributes,
private val usbBitPerfect: UsbBitPerfectOutput? = null,
) {

private var track: AudioTrack? = null
var encoding: Int = -1
Expand All @@ -56,6 +60,8 @@ class DirectTrackStream(private val attributes: AudioAttributes) {
private set
var channelCount: Int = 0
private set
var isBitPerfectMixerActive: Boolean = false
private set
private var bytesPerFrame = 0

/** Converted bytes AudioTrack has not taken yet (never more than one input buffer). */
Expand All @@ -79,7 +85,13 @@ class DirectTrackStream(private val attributes: AudioAttributes) {
}
}

fun open(rateHz: Int, channels: Int, wireEncoding: Int): Boolean {
fun open(
rateHz: Int,
channels: Int,
wireEncoding: Int,
preferredUsbDevice: AudioDeviceInfo? = null,
requestBitPerfectMixer: Boolean = false,
): Boolean {
release()
val androidEncoding = androidEncodingOf(wireEncoding) ?: return false
val mask = when (channels) {
Expand All @@ -93,9 +105,20 @@ class DirectTrackStream(private val attributes: AudioAttributes) {
.setEncoding(androidEncoding)
.setChannelMask(mask)
.build()
if (usbBitPerfect != null) {
usbBitPerfect.setAttributes(attributes)
usbBitPerfect.setDevice(preferredUsbDevice)
usbBitPerfect.setFormat(if (requestBitPerfectMixer) format else null)
usbBitPerfect.setEnabled(requestBitPerfectMixer && preferredUsbDevice != null)
}
val bitPerfectGranted = requestBitPerfectMixer && usbBitPerfect?.isConfigured() == true

val frameBytes = InternalDirectStream.bytesPerSample(wireEncoding) * channels
val minBytes = AudioTrack.getMinBufferSize(rateHz, mask, androidEncoding)
if (minBytes <= 0) return false
if (minBytes <= 0) {
usbBitPerfect?.setEnabled(false)
return false
}
// ~500 ms: a music player, not a game. Large buffers also keep the
// track off the FAST mixer path, which would skew the direct test.
val size = maxOf(minBytes * 4, rateHz / 2 * frameBytes)
Expand All @@ -108,13 +131,18 @@ class DirectTrackStream(private val attributes: AudioAttributes) {
.build()
if (created.state != AudioTrack.STATE_INITIALIZED) {
created.release()
usbBitPerfect?.setEnabled(false)
return false
}
if (preferredUsbDevice != null) {
runCatching { created.preferredDevice = preferredUsbDevice }
}
track = created
encoding = wireEncoding
sampleRateHz = rateHz
channelCount = channels
bytesPerFrame = frameBytes
isBitPerfectMixerActive = bitPerfectGranted
dead = false
routedDeviceId = null
runCatching { created.addOnRoutingChangedListener(routingListener, Handler(Looper.getMainLooper())) }
Expand All @@ -133,6 +161,7 @@ class DirectTrackStream(private val attributes: AudioAttributes) {
* is mixed. Null = inconclusive. Must run before [play].
*/
fun probeDirect(): Boolean? {
if (isBitPerfectMixerActive) return true
val target = track ?: return null
return try {
target.playbackParams = PlaybackParams().setSpeed(1.01f).setPitch(1.0f)
Expand Down Expand Up @@ -221,12 +250,14 @@ class DirectTrackStream(private val attributes: AudioAttributes) {
val target = track
track = null
pending = null
isBitPerfectMixerActive = false
if (target != null) {
runCatching { target.removeOnRoutingChangedListener(routingListener) }
runCatching { target.pause() }
runCatching { target.flush() }
runCatching { target.release() }
}
usbBitPerfect?.setEnabled(false)
resetCounters()
}

Expand Down
Loading
Loading