diff --git a/.gitignore b/.gitignore
index a3fb63f4..76d68936 100644
--- a/.gitignore
+++ b/.gitignore
@@ -25,3 +25,7 @@ fastlane/test_output
.codegraph/
.rtk/
CLAUDE.md
+
+# Generated
+.kotlin/
+__pycache__/
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 571e7566..cca26768 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -38,6 +38,61 @@ casting, a new interface, and AriaCompanion 2.
recognizes apps that block audio capture.
- **Onboarding**, Material You colors on Android 12+, home-screen **widget**.
+### Sound and timing
+- **Sleep timer** on Now Playing (15 to 90 minutes). The sound fades out over the last 30
+ seconds, then the cast stops. Every cast also starts with a one-second fade-in.
+- **Sound per speaker**: bass, treble and level (-12 to +12 dB) and an optional loudness
+ leveler, set for each speaker while casting and remembered for next time. Works with
+ AriaCast, AirPlay, Cast, DLNA, Snapcast and VBAN speakers.
+- **Rooms stay lined up**: each speaker's sync delay is now saved and applied the next time
+ that speaker joins a cast.
+- **Known speakers**: a public list of which speakers are **supported**, **supported with quirks** or
+ **unsupported**, with what to look for and what to do, kept in its own repository,
+ [AriaCast/known-speakers](https://github.com/AriaCast/known-speakers), so that adding a speaker
+ never rebuilds the app. The app downloads it once a day (Settings โ Known speakers, where it can be
+ switched off, updated by hand and browsed) and ships no copy, so the list is empty until the first
+ download. Only a newer, valid list is taken, and what the file may change is limited to five
+ settings (whether DLNA gets a Stop first or a Play after each track, the stream's content type, a
+ starting delay, and whether titles and artwork are sent), each within fixed limits and written only
+ by maintainers. The connection doctor shows a note, in your language where the list has one, while
+ you cast to a speaker with a documented quirk. DLNA speakers now report manufacturer and model.
+- **More speaker logging**: every line logged while casting to a speaker is now attributed to it,
+ and each speaker keeps its own history, so a chatty one can't push another's out. New lines:
+ session start and end (with the speaker's model and why it ended); for DLNA the HTTP status and
+ timing of every Stop, SetAVTransportURI, Play and SetVolume, the UPnP error when a renderer
+ refuses, what the renderer does with the stream (its user-agent, how long it listened), and
+ its transport state (`PLAYING`, `PAUSED_PLAYBACK`, `STOPPED`) a moment after Play and on every
+ change; for Google Cast every player state change and why it went idle; for AriaCast the
+ receiver's handshake, how long a connection lasted and each reconnect, and the receiver's own
+ underrun and overrun counts. DLNA speakers are asked their state every 10 seconds while you
+ cast to them, which is one small request.
+- **"Does it work?"**: the first time you have cast to a speaker for a minute, AriaCast asks once
+ whether it works: a dialog if the app is open, a notification with the two answers if not. "It
+ works" is kept on the phone, and offered for the public list only if the list does not already say the
+ speaker is supported. "There's a problem" asks whether it plays with something wrong or not at all and
+ what happens, then opens the pre-filled report with that written in. Nothing is sent unless you open
+ the report; speakers that a Sendspin server pulls from are not asked about. If casting to a single
+ speaker fails in that first minute, it asks instead whether to report it, with "doesn't work"
+ already chosen.
+ Only speakers are asked about or reported: AriaCast receivers, Snapcast, VBAN and Sendspin are
+ software on a phone, a computer or a server, so they are left out. A speaker is told apart by its
+ brand and model, never by the name you gave it: if it does not say its model, the report form
+ leaves "Speaker" empty for you to fill in. For "it works" no log is copied, since there is nothing
+ to explain.
+- **Look for other devices** (Settings โ Known speakers): a scan of about eight seconds, started by
+ you, for devices AriaCast cannot play to: UPnP devices that are not renderers, and mDNS services such
+ as Spotify Connect, Sonos, Amazon, HomeKit, HEOS and Bose SoundTouch. Speakers the app already lists
+ are left out. Tapping one opens a pre-filled report with what the scan learned (name, maker, model,
+ service types; addresses shortened), so a speaker the app cannot find can still be added to the list.
+ Nothing is kept or sent unless you open the report.
+- **Report speaker**: long-press a speaker (or use the doctor) to open a pre-filled report on GitHub
+ with who the speaker is and what is known about it; its log lines are copied to paste in, with
+ addresses shortened. A bot in the known-speakers repository records it: a speaker already listed has
+ the report counted (the listed status follows the most reported one, and entries a maintainer
+ checked never change), a new one is added. The log is never copied into the list, and a person can
+ send three reports a day. "Share log" in the same menu sends the whole report through the share
+ sheet instead.
+
### Built in, previously plugins
- Music Assistant player picker, connection doctor, visualizer, add speaker by address.
- **Plugin API v2**: plugins describe their UI with `ui.render()`; the v1 view methods
diff --git a/README.md b/README.md
index 6fdc523f..ed969129 100644
--- a/README.md
+++ b/README.md
@@ -54,6 +54,12 @@ Colors follow your wallpaper (Material You) on Android 12 and newer. Phones get
- **Bridge mode**: forward what this device receives to AirPlay, Cast, DLNA, Snapcast or Sendspin speakers.
- **Full-screen receiver** for tablets and TVs: artwork colors, clock, "scan to cast here" QR code, burn-in protection. Works with the Android TV remote.
- **Video casting** to Google Cast, DLNA and AirPlay, with subtitles and seeking.
+- **Known speakers**: a public list of which speakers are supported, supported with quirks or not
+ supported, kept in [AriaCast/known-speakers](https://github.com/AriaCast/known-speakers) and
+ downloaded by the app once a day (Settings โ Known speakers; it can be switched off). The first time
+ you cast to a speaker for a minute, AriaCast asks whether it works, and you can **report** it, or look
+ for devices it cannot play to, with a pre-filled report on GitHub. A bot records it: a speaker already
+ listed has the report counted, a new one is added. Logs stay yours.
- **Connection doctor**: checks the path from this device to the speaker step by step and tells you which step is broken.
- **Web remote**: control AriaCast from any browser on your network through a private link.
- **Quick Settings tiles**, a home-screen **widget**, **NFC tags** that start a cast when tapped, and **automation** intents for Tasker and MacroDroid (opt-in).
diff --git a/app/build.gradle.kts b/app/build.gradle.kts
index bb18388b..b0447585 100644
--- a/app/build.gradle.kts
+++ b/app/build.gradle.kts
@@ -22,6 +22,9 @@ if (keystorePropertiesFile.exists()) {
}
android {
+ testOptions {
+ unitTests.isIncludeAndroidResources = true
+ }
namespace = "com.aria.ariacast"
compileSdk = 36
ndkVersion = "27.1.12297006"
@@ -146,6 +149,9 @@ dependencies {
implementation(libs.jmdns)
testImplementation(libs.junit)
+ // Starts the real app on the JVM: StartupSmokeTest catches crashes that only show at runtime.
+ testImplementation("org.robolectric:robolectric:4.18.1")
+ testImplementation("androidx.test:core-ktx:1.6.1")
// Self-signed TLS certificates for the fake Google Cast receiver in CastClientTest.
testImplementation(libs.bouncycastle.bcpkix)
androidTestImplementation(libs.androidx.junit)
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index ef309462..1e83c388 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -81,6 +81,22 @@
+
+
+
+
+
+
(null)
+ /** Time left on the sleep timer, or null when none is set. */
+ val sleepTimerRemainingMs: StateFlow = _sleepTimerRemainingMs.asStateFlow()
+
+ /** Stops the cast after [minutes], fading the sound out over the last 30 seconds. */
+ fun setSleepTimer(minutes: Int) {
+ if (minutes <= 0) { cancelSleepTimer(); return }
+ if (_state.value != CastState.CASTING && _state.value != CastState.CONNECTING) return
+ sleepTimerJob?.cancel()
+ masterGain.startTimer(minutes * 60_000L)
+ _sleepTimerRemainingMs.value = masterGain.remainingMs()
+ sleepTimerJob = scope.launch {
+ while (isActive) {
+ val left = masterGain.remainingMs() ?: break
+ _sleepTimerRemainingMs.value = left
+ if (left == 0L) {
+ PacketLogger.log(PacketDirection.IN, PacketType.CONTROL, "Sleep timer ended: stopping the cast")
+ withContext(Dispatchers.Main) { stopCasting() }
+ break
+ }
+ delay(minOf(left, 1_000L))
+ }
+ }
+ }
+
+ fun cancelSleepTimer() {
+ sleepTimerJob?.cancel()
+ sleepTimerJob = null
+ masterGain.cancelTimer()
+ _sleepTimerRemainingMs.value = null
+ }
+
private val _metadata = MutableStateFlow(null)
val metadata: StateFlow = _metadata.asStateFlow()
@@ -524,7 +573,7 @@ class AudioCastService : Service() {
) {
sessionJob?.cancel()
- _activeDestinations.value = destinations
+ _activeDestinations.value = destinations.map(::withSavedDelay)
_state.value = CastState.CONNECTING
sessionJob = SupervisorJob()
val sessionScope = CoroutineScope(Dispatchers.IO + sessionJob!!)
@@ -568,6 +617,8 @@ class AudioCastService : Service() {
return
}
currentSource = source
+ cancelSleepTimer()
+ masterGain.beginSession()
_sourceKind.value = source.kind
if (source.muteLocalPlayback) {
// Playback capture happens before the volume is applied, so muting the phone
@@ -608,6 +659,7 @@ class AudioCastService : Service() {
else "Source is producing a real audio signal"
)
}
+ masterGain.process(frame)
_audioBufferFlow.emit(frame)
}
PacketLogger.log(PacketDirection.IN, PacketType.AUDIO, "Source ${source.kind.id} ended")
@@ -628,12 +680,76 @@ class AudioCastService : Service() {
}
}
+ private suspend fun askAboutSpeakerLater(dest: CastDestination) {
+ val ledger = FeedbackLedger.of(sharedPreferences)
+ val key = SpeakerFeedback.key(dest.platform, dest.name)
+ // Either a minute passes, or the cast fails first: then the question is whether to report it.
+ val failedEarly = withTimeoutOrNull(SpeakerFeedback.ASK_AFTER_MS) { _state.first { it == CastState.ERROR }; true } == true
+ if (failedEarly) {
+ if (SpeakerFeedback.shouldAskAfterFailure(_activeDestinations.value.size, ledger.asked(key), dest.platform)) {
+ ledger.markAsked(key, System.currentTimeMillis())
+ askAboutSpeaker(dest, failed = true)
+ }
+ return
+ }
+ val stillInCast = _activeDestinations.value.any { destKey(it) == destKey(dest) }
+ if (!SpeakerFeedback.shouldAsk(_state.value == CastState.CASTING, stillInCast, ledger.asked(key), dest.platform)) return
+ ledger.markAsked(key, System.currentTimeMillis())
+ askAboutSpeaker(dest)
+ }
+
+ /** A dialog if the app is on screen, a notification with the two answers if not. */
+ private fun askAboutSpeaker(dest: CastDestination, failed: Boolean = false) {
+ val castState = listOfNotNull(_state.value.name, _lastError.value?.let { "last error: $it" }).joinToString(", ")
+ fun open(answer: String? = null) = Intent(this, SpeakerFeedbackActivity::class.java)
+ .putExtra(SpeakerFeedbackActivity.EXTRA_CAST_STATE, castState)
+ .putExtra(SpeakerFeedbackActivity.EXTRA_PLATFORM, dest.platform)
+ .putExtra(SpeakerFeedbackActivity.EXTRA_NAME, dest.name)
+ .putExtra(SpeakerFeedbackActivity.EXTRA_HOST, dest.host)
+ .putExtra(SpeakerFeedbackActivity.EXTRA_EXTRA, dest.extra)
+ .apply { answer?.let { putExtra(SpeakerFeedbackActivity.EXTRA_ANSWER, it) } }
+ .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
+ if (AppVisibility.visible) {
+ startActivity(open(if (failed) SpeakerFeedbackActivity.ANSWER_FAILED else null))
+ return
+ }
+ fun pending(code: Int, answer: String?) =
+ PendingIntent.getActivity(this, code, open(answer), PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
+ val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
+ manager.createNotificationChannel(NotificationChannel(FEEDBACK_CHANNEL_ID, getString(R.string.feedback_channel), NotificationManager.IMPORTANCE_LOW))
+ manager.notify(
+ SpeakerFeedbackActivity.NOTIFICATION_ID,
+ NotificationCompat.Builder(this, FEEDBACK_CHANNEL_ID)
+ .setSmallIcon(R.drawable.ic_tile_icon)
+ .setContentTitle(getString(if (failed) R.string.feedback_failed_title else R.string.feedback_title, dest.name))
+ .setContentText(getString(if (failed) R.string.feedback_notification_failed else R.string.feedback_notification))
+ .setContentIntent(pending(FEEDBACK_REQUEST_OPEN, if (failed) SpeakerFeedbackActivity.ANSWER_FAILED else null))
+ .apply {
+ if (!failed) {
+ addAction(0, getString(R.string.feedback_works), pending(FEEDBACK_REQUEST_WORKS, SpeakerFeedbackActivity.ANSWER_WORKS))
+ addAction(0, getString(R.string.feedback_problem), pending(FEEDBACK_REQUEST_PROBLEM, SpeakerFeedbackActivity.ANSWER_PROBLEM))
+ }
+ }
+ .setAutoCancel(true)
+ .setOnlyAlertOnce(true)
+ .build()
+ )
+ }
+
/** One job per destination, so speakers can be added or removed while casting. */
private val destinationJobs = java.util.concurrent.ConcurrentHashMap()
private fun destKey(d: CastDestination) = "${d.platform}|${d.host}|${d.port}"
private fun launchDestination(scope: CoroutineScope, dest: CastDestination, withVideo: Boolean) {
- destinationJobs[destKey(dest)] = scope.launch {
+ val started = SystemClock.elapsedRealtime()
+ PacketLogger.log(PacketDirection.OUT, PacketType.HANDSHAKE,
+ "Session start: ${DeviceIdentity.of(dest.platform, dest.name, dest.extra).reportLine()}, address=${dest.host}:${dest.port}", device = dest.host)
+ DeviceQuirks.matching(DeviceIdentity.of(dest.platform, dest.name, dest.extra)).takeIf { it.isNotEmpty() }?.let { known ->
+ val config = configFor(dest)
+ PacketLogger.log(PacketDirection.OUT, PacketType.HANDSHAKE,
+ "Known speaker: ${known.joinToString { it.id }}${if (config == SpeakerConfig()) "" else ", settings from the list: $config"}", device = dest.host)
+ }
+ val job = scope.launch(DeviceContext.element(dest.host)) {
when (dest.platform) {
"DLNA" -> startDlnaSession(dest)
"Google Cast" -> startGoogleCastSession(dest)
@@ -650,6 +766,18 @@ class AudioCastService : Service() {
}
}
}
+ // After a minute of playing, ask once whether the speaker works (see SpeakerFeedback).
+ if (SpeakerFeedback.isAskable(dest.platform)) scope.launch { askAboutSpeakerLater(dest) }
+ job.invokeOnCompletion { cause ->
+ val how = when (cause) {
+ null -> "finished"
+ is CancellationException -> "stopped"
+ else -> "failed: ${cause.message ?: cause.javaClass.simpleName}"
+ }
+ PacketLogger.log(PacketDirection.IN, PacketType.HANDSHAKE,
+ "Session end: ${dest.name} after ${(SystemClock.elapsedRealtime() - started) / 1000} s ($how)", device = dest.host)
+ }
+ destinationJobs[destKey(dest)] = job
}
/**
@@ -664,9 +792,10 @@ class AudioCastService : Service() {
startDlnaHttpServer(); startArtworkServer()
}
if ((dest.platform == "AirPlay" || dest.platform == "AirPlay2") && dacpServer == null) startDacpServer()
- _activeDestinations.value = _activeDestinations.value + dest
- launchDestination(scope, dest, withVideo = false)
- PacketLogger.log(PacketDirection.OUT, PacketType.HANDSHAKE, "Added ${dest.name} to the cast")
+ val withDelay = withSavedDelay(dest)
+ _activeDestinations.value = _activeDestinations.value + withDelay
+ launchDestination(scope, withDelay, withVideo = false)
+ PacketLogger.log(PacketDirection.OUT, PacketType.HANDSHAKE, "Added ${dest.name} to the cast", device = dest.host)
_metadata.value?.let { sendMetadata(it) }
updateNotification()
}
@@ -684,7 +813,7 @@ class AudioCastService : Service() {
controlSessions.remove(dest.host)
}
_activeDestinations.value = remaining
- PacketLogger.log(PacketDirection.OUT, PacketType.HANDSHAKE, "Removed ${dest.name} from the cast")
+ PacketLogger.log(PacketDirection.OUT, PacketType.HANDSHAKE, "Removed ${dest.name} from the cast", device = dest.host)
updateNotification()
}
@@ -706,6 +835,10 @@ class AudioCastService : Service() {
while (isActive) {
val clientSocket = try { serverSocket.accept() } catch (e: Exception) { null } ?: continue
launch {
+ val peer = clientSocket.inetAddress?.hostAddress
+ var streamStarted = 0L
+ var streamedBytes = 0L
+ var streamEnd = "cast stopped"
try {
clientSocket.setTcpNoDelay(true)
clientSocket.soTimeout = 30000
@@ -730,7 +863,10 @@ class AudioCastService : Service() {
ua.contains("Darwin", ignoreCase = true) ||
ua.contains("libmpv", ignoreCase = true)
- val contentType = if (isApple) "audio/x-wav" else "audio/wav"
+ val listed = _activeDestinations.value.firstOrNull { it.host == peer }?.let(::configFor)?.dlnaContentType
+ val contentType = listed ?: if (isApple) "audio/x-wav" else "audio/wav"
+ PacketLogger.log(PacketDirection.IN, PacketType.HANDSHAKE,
+ "Stream request from $peer: $requestLine, user-agent=\"${ua.take(80)}\"${headers["range"]?.let { ", range=$it" } ?: ""}, answering $contentType", device = peer)
val responseHeaders = StringBuilder()
responseHeaders.append("HTTP/1.1 200 OK\r\n")
@@ -749,6 +885,7 @@ class AudioCastService : Service() {
if (requestLine.startsWith("HEAD")) {
output.flush()
+ PacketLogger.log(PacketDirection.OUT, PacketType.HANDSHAKE, "Answered a HEAD request from $peer", device = peer)
return@launch
}
@@ -771,17 +908,27 @@ class AudioCastService : Service() {
}
output.write(header.array())
- audioBufferFlow.collect { buffer ->
+ val dsp = dspFor(peer ?: "")
+ streamStarted = SystemClock.elapsedRealtime()
+ PacketLogger.log(PacketDirection.OUT, PacketType.AUDIO, "Streaming to $peer", device = peer)
+ audioBufferFlow.collect { raw ->
+ val buffer = dsp.process(raw)
try {
output.write(buffer)
output.flush()
+ streamedBytes += buffer.size
} catch (e: Exception) {
+ streamEnd = "write failed: ${e.message ?: e.javaClass.simpleName}"
throw CancellationException("Client disconnected")
}
}
} catch (e: Exception) {
Log.d(TAG, "Streaming session ended: ${e.message}")
} finally {
+ if (streamStarted != 0L) {
+ PacketLogger.log(PacketDirection.IN, PacketType.AUDIO,
+ "Stream to $peer ended after ${(SystemClock.elapsedRealtime() - streamStarted) / 1000} s, ${streamedBytes / 1024} KB sent ($streamEnd)", device = peer)
+ }
try { clientSocket.close() } catch (e: Exception) {}
}
}
@@ -794,6 +941,77 @@ class AudioCastService : Service() {
}
}
+ /**
+ * Posts one SOAP action to a renderer and logs what came back: the HTTP status, how long it
+ * took and the UPnP error if it sent one. Network errors are logged and rethrown; an HTTP
+ * error status is only logged, as it always was.
+ */
+ private suspend fun dlnaSoap(dest: CastDestination, url: String, service: String, action: String, body: String): HttpResponse {
+ val started = SystemClock.elapsedRealtime()
+ try {
+ val response = client.post(url) {
+ header("SoapAction", "\"urn:schemas-upnp-org:service:$service:1#$action\"")
+ contentType(ContentType.parse("text/xml; charset=utf-8"))
+ setBody(body)
+ }
+ val ms = SystemClock.elapsedRealtime() - started
+ val fault = if (response.status.isSuccess()) null else runCatching { Dlna.soapFault(response.bodyAsText()) }.getOrNull()
+ PacketLogger.log(PacketDirection.OUT, PacketType.CONTROL,
+ "DLNA $action -> ${response.status.value} in $ms ms${fault?.let { " ($it)" } ?: ""}", device = dest.host)
+ return response
+ } catch (e: CancellationException) {
+ throw e
+ } catch (e: Exception) {
+ PacketLogger.log(PacketDirection.IN, PacketType.CONTROL,
+ "DLNA $action failed after ${SystemClock.elapsedRealtime() - started} ms: ${e.message ?: e.javaClass.simpleName}", device = dest.host)
+ throw e
+ }
+ }
+
+ private fun soapEnvelope(service: String, action: String, args: String) =
+ "" +
+ "$args"
+
+ /**
+ * Asks the renderer what it is doing, a moment after Play and then every few seconds, and logs
+ * every change. "Play was accepted" and "it is playing" are different facts, and renderers
+ * that stall (paused at 0:00, stopped after a track) only show it here.
+ */
+ private suspend fun watchDlnaTransport(dest: CastDestination, controlUrl: String) {
+ var last: String? = null
+ var failures = 0
+ delay(DLNA_FIRST_CHECK_MS)
+ while (currentCoroutineContext().isActive) {
+ try {
+ val text = dlnaStateCheck(controlUrl)
+ val state = Dlna.transportState(text) ?: "unknown"
+ if (state != last) {
+ PacketLogger.log(PacketDirection.IN, PacketType.CONTROL,
+ "DLNA transport state: $state${last?.let { " (was $it)" } ?: ""}", device = dest.host)
+ last = state
+ }
+ failures = 0
+ } catch (e: CancellationException) {
+ throw e
+ } catch (e: Exception) {
+ failures++
+ if (failures == 1 || failures == DLNA_CHECK_GIVE_UP) {
+ PacketLogger.log(PacketDirection.IN, PacketType.CONTROL,
+ "DLNA state check failed ($failures in a row): ${e.message ?: e.javaClass.simpleName}", device = dest.host)
+ }
+ if (failures >= DLNA_CHECK_GIVE_UP) return
+ }
+ delay(DLNA_CHECK_EVERY_MS)
+ }
+ }
+
+ private suspend fun dlnaStateCheck(controlUrl: String): String =
+ client.post(controlUrl) {
+ header("SoapAction", "\"urn:schemas-upnp-org:service:AVTransport:1#GetTransportInfo\"")
+ contentType(ContentType.parse("text/xml; charset=utf-8"))
+ setBody(soapEnvelope("AVTransport", "GetTransportInfo", "0"))
+ }.bodyAsText()
+
private suspend fun startDlnaSession(dest: CastDestination) {
val (controlUrl, _) = getDlnaControlUrls(dest.extra)
if (controlUrl == null) return
@@ -801,45 +1019,34 @@ class AudioCastService : Service() {
val streamUrl = "http://$myIp:$STREAM_PORT/stream.wav"
try {
- try {
- val stopBody = """
-
-
- 0
-
-
- """.trimIndent()
- client.post(controlUrl) {
- header("SoapAction", "\"urn:schemas-upnp-org:service:AVTransport:1#Stop\"")
- contentType(ContentType.parse("text/xml; charset=utf-8"))
- setBody(stopBody)
- }
- } catch (e: Exception) {}
+ if (configFor(dest).dlnaSkipInitialStop == true) {
+ PacketLogger.log(PacketDirection.OUT, PacketType.CONTROL, "DLNA Stop skipped (setting from the list)", device = dest.host)
+ } else {
+ try {
+ dlnaSoap(dest, controlUrl, "AVTransport", "Stop", soapEnvelope("AVTransport", "Stop", "0"))
+ } catch (e: CancellationException) {
+ throw e
+ } catch (e: Exception) {}
+ }
val metadataXml = createDidlMetadata(streamUrl, _metadata.value)
val setUriBody = """0${escapeXml(streamUrl)}${escapeXml(metadataXml)}"""
- client.post(controlUrl) {
- header("SoapAction", "\"urn:schemas-upnp-org:service:AVTransport:1#SetAVTransportURI\"")
- contentType(ContentType.parse("text/xml; charset=utf-8"))
- setBody(setUriBody)
- }
+ dlnaSoap(dest, controlUrl, "AVTransport", "SetAVTransportURI", setUriBody)
val playBody = """01"""
- client.post(controlUrl) {
- header("SoapAction", "\"urn:schemas-upnp-org:service:AVTransport:1#Play\"")
- contentType(ContentType.parse("text/xml; charset=utf-8"))
- setBody(playBody)
- }
+ dlnaSoap(dest, controlUrl, "AVTransport", "Play", playBody)
_state.value = CastState.CASTING
updateNotification()
+ watchDlnaTransport(dest, controlUrl)
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
Log.e(TAG, "DLNA session failed for ${dest.name}: ${e.message}")
+ PacketLogger.log(PacketDirection.IN, PacketType.HANDSHAKE, "DLNA session failed: ${e.message ?: e.javaClass.simpleName}", device = dest.host)
_state.value = CastState.ERROR
}
}
@@ -872,14 +1079,14 @@ class AudioCastService : Service() {
client.launch(sharedPreferences.getString(KEY_CAST_RECEIVER_APP_ID, null)
?.trim()?.uppercase()?.takeIf { it.matches(Regex("[0-9A-F]{8}")) } ?: CastClient.DEFAULT_MEDIA_RECEIVER)
client.load(streamUrl, "audio/wav", castMetadata(_metadata.value))
- PacketLogger.log(PacketDirection.OUT, PacketType.HANDSHAKE, "Google Cast: ${dest.name} playing $streamUrl")
+ PacketLogger.log(PacketDirection.OUT, PacketType.HANDSHAKE, "Google Cast: ${dest.name} playing $streamUrl", device = dest.host)
} catch (e: CancellationException) {
client.close()
throw e
} catch (e: Exception) {
client.close()
Log.e(TAG, "Google Cast session failed for ${dest.name}: ${e.message}")
- PacketLogger.log(PacketDirection.IN, PacketType.HANDSHAKE, "Google Cast ${dest.name} failed: ${e.message ?: e.javaClass.simpleName}")
+ PacketLogger.log(PacketDirection.IN, PacketType.HANDSHAKE, "Google Cast ${dest.name} failed: ${e.message ?: e.javaClass.simpleName}", device = dest.host)
if (++failures >= 3) {
_state.value = CastState.ERROR
return@withContext
@@ -895,7 +1102,7 @@ class AudioCastService : Service() {
try {
val reason = ended.await()
Log.w(TAG, "Google Cast ${dest.name} ended: $reason")
- PacketLogger.log(PacketDirection.IN, PacketType.HANDSHAKE, "Google Cast ${dest.name} ended: $reason")
+ PacketLogger.log(PacketDirection.IN, PacketType.HANDSHAKE, "Google Cast ${dest.name} ended: $reason", device = dest.host)
if (reason == "receiver app stopped" || reason == "receiver app closed") {
// The user switched the device to something else - don't fight them for it.
_state.value = CastState.ERROR
@@ -945,7 +1152,7 @@ class AudioCastService : Service() {
} catch (e: Exception) {
val label = if (airplay2) "AirPlay 2" else "AirPlay"
Log.e(TAG, "$label session failed for ${dest.name}: ${e.message}")
- PacketLogger.log(PacketDirection.IN, PacketType.HANDSHAKE, "$label ${dest.name} failed: ${e.message ?: e.javaClass.simpleName}")
+ PacketLogger.log(PacketDirection.IN, PacketType.HANDSHAKE, "$label ${dest.name} failed: ${e.message ?: e.javaClass.simpleName}", device = dest.host)
if (e is AirPlayBusyException || e is AirPlayAuthRequiredException || ++failures >= 3) {
_state.value = CastState.ERROR
return@withContext
@@ -962,7 +1169,7 @@ class AudioCastService : Service() {
airplayStreamers[dest.host] = streamer
lastSentMetadata.remove(dest.host)
lastSentArtwork.remove(dest.host)
- PacketLogger.log(PacketDirection.OUT, PacketType.HANDSHAKE, "${dest.name}: streaming (${streamer.javaClass.simpleName})")
+ PacketLogger.log(PacketDirection.OUT, PacketType.HANDSHAKE, "${dest.name}: streaming (${streamer.javaClass.simpleName})", device = dest.host)
_state.value = CastState.CASTING
updateNotification()
_metadata.value?.let { pushAirPlayMetadata(dest.host, streamer, it) }
@@ -971,7 +1178,8 @@ class AudioCastService : Service() {
try {
val reason = coroutineScope {
val feeder = launch {
- audioBufferFlow.collect { chunk -> streamer.writePcm(resampler.resample(chunk)) }
+ val dsp = dspFor(dest.host)
+ audioBufferFlow.collect { chunk -> streamer.writePcm(resampler.resample(dsp.process(chunk))) }
}
// "Streaming" alone doesn't say whether the receiver is playing: log what it
// has asked for (timing requests, resends) once it has had time to start.
@@ -979,7 +1187,7 @@ class AudioCastService : Service() {
var elapsed = 0L
for (at in longArrayOf(5_000, 30_000)) {
delay(at - elapsed); elapsed = at
- PacketLogger.log(PacketDirection.OUT, PacketType.STATS, "${dest.name} after ${at / 1000}s: ${streamer.statsSummary()}")
+ PacketLogger.log(PacketDirection.OUT, PacketType.STATS, "${dest.name} after ${at / 1000}s: ${streamer.statsSummary()}", device = dest.host)
}
}
val r = ended.await()
@@ -988,7 +1196,7 @@ class AudioCastService : Service() {
r
}
Log.w(TAG, "AirPlay ${dest.name} dropped: $reason (${streamer.statsSummary()})")
- PacketLogger.log(PacketDirection.IN, PacketType.HANDSHAKE, "${dest.name} dropped the session: $reason - reconnecting")
+ PacketLogger.log(PacketDirection.IN, PacketType.HANDSHAKE, "${dest.name} dropped the session: $reason - reconnecting", device = dest.host)
} finally {
airplayStreamers.remove(dest.host, streamer)
withContext(NonCancellable) {
@@ -1037,7 +1245,7 @@ class AudioCastService : Service() {
// e.g. shairport-sync in AirPlay 2 mode, which needs PTP (privileged ports
// on Android). Every AirPlay 2 receiver also speaks AirPlay 1 on the same port.
Log.i(TAG, "${dest.name}: ${e.message}; falling back to AirPlay 1")
- PacketLogger.log(PacketDirection.IN, PacketType.HANDSHAKE, "${dest.name}: AirPlay 2 timing unsupported, using AirPlay 1")
+ PacketLogger.log(PacketDirection.IN, PacketType.HANDSHAKE, "${dest.name}: AirPlay 2 timing unsupported, using AirPlay 1", device = dest.host)
val raop = RaopClient(
host = dest.host,
port = if (dest.port > 0) dest.port else 7000,
@@ -1230,14 +1438,15 @@ class AudioCastService : Service() {
_state.value = CastState.CASTING
updateNotification()
PacketLogger.log(PacketDirection.OUT, PacketType.HANDSHAKE,
- "VBAN stream '$streamName' to ${dest.name} (${dest.host}:$port), ${PcmFormat.SAMPLE_RATE} Hz stereo 16-bit PCM")
- audioBufferFlow.collect { streamer.send(it) }
+ "VBAN stream '$streamName' to ${dest.name} (${dest.host}:$port), ${PcmFormat.SAMPLE_RATE} Hz stereo 16-bit PCM", device = dest.host)
+ val dsp = dspFor(dest.host)
+ audioBufferFlow.collect { streamer.send(dsp.process(it)) }
}
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
Log.e(TAG, "VBAN session failed for ${dest.name}: ${e.message}")
- PacketLogger.log(PacketDirection.OUT, PacketType.AUDIO, "VBAN to ${dest.name} failed: ${e.message}")
+ PacketLogger.log(PacketDirection.OUT, PacketType.AUDIO, "VBAN to ${dest.name} failed: ${e.message}", device = dest.host)
_state.value = CastState.ERROR
}
}
@@ -1375,8 +1584,9 @@ class AudioCastService : Service() {
// 5. Stream audio
try {
- audioBufferFlow.collect { buffer ->
- audioOut.write(buffer)
+ val dsp = dspFor(dest.host)
+ audioBufferFlow.collect { raw ->
+ audioOut.write(dsp.process(raw))
}
} finally {
try { audioSocket.close() } catch (_: Exception) {}
@@ -1416,26 +1626,30 @@ class AudioCastService : Service() {
private suspend fun startAudioSession(dest: CastDestination) {
var sentFramesCount = 0L
var reconnectAttempts = 0
+ var connectedAt = 0L
while (currentCoroutineContext().isActive) {
try {
client.webSocket(host = dest.host, port = dest.port, path = "/audio") audioSocket@{
reconnectAttempts = 0
- PacketLogger.log(PacketDirection.OUT, PacketType.HANDSHAKE, "Audio socket connected to ${dest.name} (${dest.host}:${dest.port})")
+ connectedAt = SystemClock.elapsedRealtime()
+ PacketLogger.log(PacketDirection.OUT, PacketType.HANDSHAKE, "Audio socket connected to ${dest.name} (${dest.host}:${dest.port})", device = dest.host)
if (dest.platform != "AriaCast") {
val handshakeFrame = try {
withTimeout(3000L) { incoming.receive() }
} catch (e: Exception) {
Log.w(TAG, "Audio handshake timed out for ${dest.host}: ${e.message}")
- PacketLogger.log(PacketDirection.IN, PacketType.HANDSHAKE, "Handshake timed out for ${dest.name}: ${e.message}")
+ PacketLogger.log(PacketDirection.IN, PacketType.HANDSHAKE, "Handshake timed out for ${dest.name}: ${e.message}", device = dest.host)
return@audioSocket
}
if (handshakeFrame !is Frame.Text) {
- PacketLogger.log(PacketDirection.IN, PacketType.HANDSHAKE, "Unexpected handshake frame type from ${dest.name}")
+ PacketLogger.log(PacketDirection.IN, PacketType.HANDSHAKE, "Unexpected handshake frame type from ${dest.name}", device = dest.host)
return@audioSocket
}
+ // The receiver's own words (sample rate, frame size) say what it expects of us.
+ PacketLogger.log(PacketDirection.IN, PacketType.HANDSHAKE, "Receiver handshake: ${handshakeFrame.readText().take(200)}", device = dest.host)
}
_state.value = CastState.CASTING
@@ -1443,8 +1657,9 @@ class AudioCastService : Service() {
val delayQueue = java.util.LinkedList()
- audioBufferFlow.collect { buffer: ByteArray ->
- delayQueue.add(buffer)
+ val dsp = dspFor(dest.host)
+ audioBufferFlow.collect { raw: ByteArray ->
+ delayQueue.add(dsp.process(raw))
val currentDelay = _activeDestinations.value.find { it.host == dest.host }?.delayMs ?: 0
val requiredFrames = currentDelay / 20
@@ -1473,9 +1688,12 @@ class AudioCastService : Service() {
} catch (e: Exception) {
if (e is CancellationException) throw e
Log.e(TAG, "Audio WS connection to ${dest.host}:${dest.port} failed: ${e.message}", e)
- PacketLogger.log(PacketDirection.IN, PacketType.HANDSHAKE, "Audio connection to ${dest.name} failed: ${e.message ?: e.javaClass.simpleName}")
+ val upFor = if (connectedAt != 0L) " after ${(SystemClock.elapsedRealtime() - connectedAt) / 1000} s, $sentFramesCount frames sent" else ""
+ connectedAt = 0L
+ PacketLogger.log(PacketDirection.IN, PacketType.HANDSHAKE, "Audio connection to ${dest.name} failed$upFor: ${e.message ?: e.javaClass.simpleName}", device = dest.host)
reconnectAttempts++
val delayTime = (RECONNECT_INITIAL_BACKOFF * 2.0.pow(reconnectAttempts.toDouble().coerceAtMost(5.0))).toLong()
+ PacketLogger.log(PacketDirection.OUT, PacketType.HANDSHAKE, "Reconnecting to ${dest.name} in $delayTime ms (attempt $reconnectAttempts)", device = dest.host)
delay(delayTime)
}
}
@@ -1619,14 +1837,17 @@ class AudioCastService : Service() {
val dB = AirPlayVolume.percentToDb(p)
Log.d(TAG, "Setting receiver volume to $p% ($dB dB)")
scope.launch {
- val airplay = synchronized(airplayStreamers) { airplayStreamers.values.toList() }
- airplay.forEach { try { it.setVolume(dB) } catch (_: Exception) {} }
+ PacketLogger.log(PacketDirection.OUT, PacketType.CONTROL, "Volume set to $p%")
+ val airplay = synchronized(airplayStreamers) { airplayStreamers.toMap() }
+ airplay.forEach { (host, streamer) ->
+ try { streamer.setVolume(dB) } catch (e: Exception) { logVolumeFailure(host, "AirPlay", p, e) }
+ }
- val casts = synchronized(castClients) { castClients.values.toList() }
- casts.forEach { c ->
+ val casts = synchronized(castClients) { castClients.toMap() }
+ casts.forEach { (host, c) ->
try {
if (p == 0) c.setMuted(true) else { if (c.muted) c.setMuted(false); c.setVolume(p / 100.0) }
- } catch (_: Exception) {}
+ } catch (e: Exception) { logVolumeFailure(host, "Google Cast", p, e) }
}
val controlSnapshot = synchronized(controlSessions) { controlSessions.values.toList() }
@@ -1641,7 +1862,7 @@ class AudioCastService : Service() {
_activeDestinations.value.filter { it.platform == "DLNA" }.forEach { dest ->
val (_, rcUrl) = getDlnaControlUrls(dest.extra)
- if (rcUrl != null) setDlnaVolume(rcUrl, p)
+ if (rcUrl != null) setDlnaVolume(dest.host, rcUrl, p)
}
}
}
@@ -1659,18 +1880,81 @@ class AudioCastService : Service() {
PacketLogger.log(PacketDirection.IN, PacketType.CONTROL, "Receiver volume changed to $p%")
}
- private suspend fun setDlnaVolume(rcUrl: String, percent: Int) {
+ private fun logVolumeFailure(host: String, protocol: String, percent: Int, e: Exception) {
+ PacketLogger.log(PacketDirection.OUT, PacketType.CONTROL,
+ "$protocol volume $percent% failed: ${e.message ?: e.javaClass.simpleName}", device = host)
+ }
+
+ private suspend fun setDlnaVolume(host: String, rcUrl: String, percent: Int) {
try {
val body = """0Master$percent"""
- client.post(rcUrl) {
+ val response = client.post(rcUrl) {
header("SoapAction", "\"urn:schemas-upnp-org:service:RenderingControl:1#SetVolume\"")
contentType(ContentType.parse("text/xml; charset=utf-8"))
setBody(body)
}
- } catch (_: Exception) {}
+ if (!response.status.isSuccess()) {
+ PacketLogger.log(PacketDirection.OUT, PacketType.CONTROL,
+ "DLNA SetVolume($percent) -> ${response.status.value}${runCatching { Dlna.soapFault(response.bodyAsText()) }.getOrNull()?.let { " ($it)" } ?: ""}", device = host)
+ }
+ } catch (e: CancellationException) {
+ throw e
+ } catch (e: Exception) {
+ PacketLogger.log(PacketDirection.OUT, PacketType.CONTROL, "DLNA SetVolume($percent) failed: ${e.message ?: e.javaClass.simpleName}", device = host)
+ }
+ }
+
+ private val _speakerSounds = MutableStateFlow