Merge pull request 'fix(mobile): serve iOS lossless audio and gate Android Atmos' (#49) from t3/ios-alac-android-atmos into main
This commit was merged in pull request #49.
This commit is contained in:
commit
aef9fb905d
7 files changed
+54
-11
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import { dolbyAtmosSupported } from "@/lib/playback-capabilities"
|
||||
import { Ionicons } from "@expo/vector-icons"
|
||||
import { useAction, useMutation, useQuery } from "convex/react"
|
||||
import { Image } from "expo-image"
|
||||
@@ -374,7 +375,12 @@ function QualitySection({ user }: { user: CurrentUser }) {
|
||||
<SettingsCard title="Spatial Audio">
|
||||
<SwitchRow
|
||||
title="Prefer Dolby Atmos"
|
||||
subtitle="Use Atmos versions when they are available."
|
||||
subtitle={
|
||||
dolbyAtmosSupported
|
||||
? "Use Atmos versions when they are available."
|
||||
: "This device does not support Dolby Atmos playback."
|
||||
}
|
||||
disabled={!dolbyAtmosSupported}
|
||||
value={user.preferAtmos ?? false}
|
||||
onValueChange={(checked) =>
|
||||
void updateAtmos({ preferAtmos: checked }).catch((error: Error) =>
|
||||
@@ -636,10 +642,12 @@ function SwitchRow({
|
||||
subtitle,
|
||||
value,
|
||||
onValueChange,
|
||||
disabled,
|
||||
}: {
|
||||
title: string
|
||||
subtitle?: string
|
||||
value: boolean
|
||||
disabled?: boolean
|
||||
onValueChange: (checked: boolean) => void
|
||||
}) {
|
||||
const colors = useThemeColors()
|
||||
@@ -656,6 +664,7 @@ function SwitchRow({
|
||||
</View>
|
||||
<Switch
|
||||
value={value}
|
||||
disabled={disabled}
|
||||
onValueChange={onValueChange}
|
||||
{...(Platform.OS === "android"
|
||||
? {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { dolbyAtmosSupported } from "@/lib/playback-capabilities"
|
||||
import { NavigationBar } from "expo-navigation-bar"
|
||||
import { MusicEntityView } from "@/components/music-entity-view"
|
||||
import { Stack, useRouter } from "expo-router"
|
||||
@@ -103,7 +104,7 @@ export default function PlayerScreen() {
|
||||
? playbackFormat(
|
||||
playbackInfo.files,
|
||||
user.playbackQuality,
|
||||
user.preferAtmos ?? false
|
||||
dolbyAtmosSupported && (user.preferAtmos ?? false)
|
||||
)
|
||||
: null
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { dolbyAtmosSupported } from "@/lib/playback-capabilities"
|
||||
import {
|
||||
createAudioPlaylist,
|
||||
setAudioModeAsync,
|
||||
@@ -313,7 +314,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
? JSON.stringify([
|
||||
user._id,
|
||||
user.playbackQuality,
|
||||
user.preferAtmos ?? false,
|
||||
dolbyAtmosSupported && (user.preferAtmos ?? false),
|
||||
])
|
||||
: ""
|
||||
|
||||
@@ -625,7 +626,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
}
|
||||
if (!isCurrentLoad()) return
|
||||
|
||||
const loadKey = `${user.playbackQuality}:${user.preferAtmos ?? false}:${cookieHeader}`
|
||||
const loadKey = `${user.playbackQuality}:${dolbyAtmosSupported && (user.preferAtmos ?? false)}:${cookieHeader}`
|
||||
const needsRebuild =
|
||||
loadedLoadKeyRef.current !== loadKey ||
|
||||
nativePlaylistNeedsRebuild(
|
||||
@@ -667,7 +668,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
const plan = playbackStreamPlan(
|
||||
song.files,
|
||||
user.playbackQuality,
|
||||
user.preferAtmos ?? false
|
||||
dolbyAtmosSupported && (user.preferAtmos ?? false)
|
||||
)
|
||||
sources.set(
|
||||
song.id,
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import { requireOptionalNativeModule } from "expo"
|
||||
import { Platform } from "react-native"
|
||||
|
||||
// E-AC-3 support varies by Android device. Older native builds fail closed.
|
||||
function supportsDolbyAtmos(): boolean {
|
||||
if (Platform.OS === "ios") return true
|
||||
if (Platform.OS !== "android") return false
|
||||
try {
|
||||
return (
|
||||
requireOptionalNativeModule<{
|
||||
supportsDolbyAtmos?: () => boolean
|
||||
}>("AudioOutput")?.supportsDolbyAtmos?.() ?? false
|
||||
)
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
export const dolbyAtmosSupported = supportsDolbyAtmos()
|
||||
@@ -1,3 +1,4 @@
|
||||
import { Platform } from "react-native"
|
||||
import type { AudioSource } from "expo-audio"
|
||||
|
||||
export type PlaybackQuality = "low" | "medium" | "high" | "lossless"
|
||||
@@ -50,6 +51,7 @@ export function songPlayUrl(
|
||||
preferAtmos: boolean | undefined
|
||||
) {
|
||||
const params = new URLSearchParams()
|
||||
if (Platform.OS === "ios") params.set("losslessFormat", "alac")
|
||||
if (quality) params.set("quality", quality)
|
||||
if (preferAtmos) params.set("preferAtmos", "true")
|
||||
const query = params.toString()
|
||||
|
||||
+11
@@ -1,5 +1,6 @@
|
||||
package expo.modules.audiooutput
|
||||
|
||||
import android.media.MediaCodecList
|
||||
import android.os.Build
|
||||
import androidx.mediarouter.app.SystemOutputSwitcherDialogController
|
||||
import expo.modules.kotlin.functions.Queues
|
||||
@@ -10,6 +11,16 @@ class AudioOutputModule : Module() {
|
||||
override fun definition() = ModuleDefinition {
|
||||
Name("AudioOutput")
|
||||
|
||||
Function("supportsDolbyAtmos") {
|
||||
// Library Atmos tracks use E-AC-3. ExoPlayer needs a device decoder.
|
||||
MediaCodecList(MediaCodecList.REGULAR_CODECS).codecInfos.any { codec ->
|
||||
!codec.isEncoder && codec.supportedTypes.any { type ->
|
||||
type.equals("audio/eac3", ignoreCase = true) ||
|
||||
type.equals("audio/eac3-joc", ignoreCase = true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
AsyncFunction("showPicker") {
|
||||
val activity = appContext.currentActivity
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU || activity == null) {
|
||||
|
||||
@@ -122,11 +122,7 @@ export async function GET(
|
||||
}))!
|
||||
|
||||
const quality = request.nextUrl.searchParams.get("quality") as
|
||||
| "low"
|
||||
| "medium"
|
||||
| "high"
|
||||
| "lossless"
|
||||
| null
|
||||
"low" | "medium" | "high" | "lossless" | null
|
||||
|
||||
const preferAtmos =
|
||||
request.nextUrl.searchParams.get("preferAtmos") === "true"
|
||||
@@ -157,7 +153,11 @@ export async function GET(
|
||||
const metadata = await music.parseFile(song.files[originalCodec])
|
||||
|
||||
if (!transcodeNeeded && originalCodec === "lossless") {
|
||||
if (browser.name === "Safari" && metadata.format.codec === "ALAC") {
|
||||
// Native iOS playback does not identify itself as Safari.
|
||||
const acceptsAlac =
|
||||
browser.name === "Safari" ||
|
||||
request.nextUrl.searchParams.get("losslessFormat") === "alac"
|
||||
if (acceptsAlac && metadata.format.codec === "ALAC") {
|
||||
try {
|
||||
return await streamFileResponse(
|
||||
request,
|
||||
|
||||
Reference in new issue
Block a user