My iPhone input tool (iph) opens an audio stream through com.apple.coredevice.displayservice so the phone will accept taps. When I added screen recording, the input session and the recorder started sharing that service. The idevice crate only had one way to stop streaming: stopAll, which ends every session's streams on the phone at once.
The phone also accepts a list of individual streams to stop (stopAll: false, with each stream's RemoteSSRC in identifiers). On iOS 27.2, stopping the recorder's video by its RemoteSSRC left the input audio running, and taps still worked. Sending those XPC requests directly over RemoteXpcClient brought up a separate issue: what happens to the connection when a call times out mid-reply.
Both callers wrap MediaControl calls in tokio::time::timeout:
If no video datagram arrives for more than 2.5 seconds (a still screen can stay silent that long), the recorder checks whether the phone still lists its stream, with a 2-second timeout on status().
Shutting a session down wraps stop_session() in a 5-second timeout.
MediaControl sends one request at a time and reads the next message on the reply channel. If a call times out after its request has been queued, the phone can still reply. Keeping link: Link directly in MediaControl and reusing the connection would let the next call take that reply as its own.
Instead, MediaControl holds link: Option<Link> and moves it out of self before touching the wire:
pub struct MediaControl {
adapter: AdapterHandle,
rsd: RsdHandshake,
link: Option<Link>,
}
impl MediaControl {
async fn invoke(
&mut self,
feature: &str,
input: Dictionary,
action: Option<&str>,
) -> Result<plist::Value> {
let mut link = match self.link.take() {
Some(link) => link,
None => self.open_link().await?,
};
link.0
.send_object(request(feature, input, action), true)
.await
.map_err(|e| anyhow!("{e:?}"))?;
let res = link.0.recv().await.map_err(|e| anyhow!("{e:?}"))?;
self.link = Some(link);
output(res)
}
}
Holding link inside the future makes drop-on-cancel do the cleanup automatically:
If a timeout cancels invoke after self.link.take() and before the link is put back, self.link stays None, and any link the future owns drops with it. The same happens on any Err from send_object or recv (including framing or decode errors). There is no separate poisoned: bool flag to forget to check.
self.link = Some(link) runs before output(res). Once recv returns a decoded plist, a CoreDevice.error inside it still means the wire framing stayed in sync, so the connection is put back and reused.
In the recorder's liveness check (VideoStream::gone), letting invoke reconnect lazily inside the 2-second status() timeout could let a slow reconnect burn the whole status budget. Calling relink() first (a no-op while self.link is Some) gives the RSD connect and XPC handshake their own 5-second timeout:
let running = async {
bounded("display service", 5, self.media.relink()).await?;
let status = bounded("status", 2, self.media.status()).await?;
Ok(!media::streams_of(&status, &self.session_id.to_string()).is_empty())
};
Here bounded wraps tokio::time::timeout. The first check that finds no matching stream ends the recording. If reconnecting or fetching status fails, it takes three failed checks in a row.
On the phone side, if stop_own times out or the daemon is hit with kill -9, an unstopped stream keeps encoding until its RTCP timeout (an hour in a kill -9 test). While the phone lists streams, stop_session checks whether ours have disappeared, and if they persist it can return an error. It tries stopAll only if no other streams are listed. A missing sessions array also triggers stopAll, and if the next status reply still omits the list, that counts as success. Until stop_own returns Ok, a small marker file on disk keeps each client session UUID so later runs can retry stopping leftovers.
The full MediaControl implementation and tests are in src/device/media.rs.