Navigation
  • Home
  • Work
  • About
  • Writing
  • Contact
Socials
LinkedInGitHubEmailResume
Writing
Code by AsjadAsjad Anwar
  • Work
  • About
  • Writing
  • Contact
22 August 2026 · 6 min read

Push-to-talk on iOS with PushToTalk and WebRTC

AdvenShare is an app for off-road racing teams. Its radio feature has to work like a handheld radio: the phone is locked and mounted in a vehicle, a channel is always playing, and a driver presses a button on the steering wheel to talk. The mic must open while the button is held and close the instant it is released.

Audio transport is a WebRTC SFU (LiveKit), one room per channel. The hard half is iOS: who owns the audio session, and what may open the microphone in the background.

Two possible session owners

The first working version used CallKit: reporting an outgoing call makes iOS grant background audio, at the price of a fake phone call.

The current version uses Apple's PushToTalk framework for the system talk UI on the lock screen and Dynamic Island, and for accessory button events. A single flag chooses between the two owners, and three small functions are the only place that knows which one is active:

private func sessionOwnerStartChannel(name: String) {
    if Flags.applePTT {
        PTTBridge.shared.startChannel(name: name, listenOnly: role == .listenOnly)
    } else {
        CallBridge.shared.reportStartedCall(name: name)
    }
}

The CallKit path stays in the code as an escape hatch until field testing is signed off.

Channel lifecycle

Joining a channel does four things in order: fetch a room token from the backend, connect the room, store a session object for that channel, and, if it is the first channel, register a PushToTalk channel with the system.

PTChannelManager is created asynchronously. A join can arrive before it exists, so the request is queued and flushed when the manager is ready.

Once the system confirms the join, the app marks the service ready, sets the transmission mode and enables accessory button events on iOS 17 and later:

func channelManager(_ manager: PTChannelManager,
                    didJoinChannel uuid: UUID,
                    reason: PTChannelJoinReason) {
    manager.setServiceStatus(.ready, channelUUID: uuid) { _ in }
    applyTransmissionMode(uuid)   // .fullDuplex or .listenOnly
    if #available(iOS 17.0, *) {
        manager.setAccessoryButtonEventsEnabled(true, channelUUID: uuid) { _ in }
    }
}

Setting the transmission mode can fail right after joining, so it retries twice with a half-second delay.

The session object always starts muted, whatever role the token grants. Nobody joins a channel with a live mic.

The audio session handoff

While you are only listening, LiveKit manages the audio session automatically. When a transmit starts from the system UI or an accessory, the PushToTalk framework takes the session, reconfigures it and activates it. If LiveKit starts capturing before that, its audio unit dies when the framework reconfigures the session. The symptom is a green mic indicator and no voice.

So the transmit callback records intent and nothing else:

func transmitBegan() {
    guard role != .listenOnly, !isAdminMuted else {
        PTTBridge.shared.stopTransmitting()
        return
    }
    wantsTransmit = true   // do not unmute here
}

Capture starts only in didActivate. At that point LiveKit is switched to manual session configuration and its audio engine is forced to rebuild on the session the framework just activated:

func sessionActivated() {
    guard wantsTransmit else { return }
    let audio = AudioManager.shared
    audio.audioSession.isAutomaticConfigurationEnabled = false
    try? audio.setEngineAvailability(.none)
    try? audio.setEngineAvailability(.default)
    unmuteMic()
}

On didDeactivate the reverse happens: automatic configuration back on, another engine restart, and recording kept prepared so the next press opens quickly. The same reclaim runs on return to the foreground after a background transmit. Leaving all channels also resets to automatic, so LiveKit is never stuck in manual mode for the next join.

Background: what I did not use

The PushToTalk framework is designed so a server can wake a suspended app with a push when someone else starts talking. I did not build that path. The app only logs the ephemeral push token, and the incoming-push delegate returns "leave channel".

Instead, receivers stay connected to the room in the background under the audio background mode, and incoming audio simply keeps playing. The framework is used for transmitting and for the system UI, not for waking up. The cost is that a dropped connection has to be repaired by the app itself.

Hardware buttons

There are two input paths. Accessories the system understands arrive through the framework's transmit callbacks. Dedicated BLE push-to-talk buttons are handled by the app directly as a CoreBluetooth central, subscribed to a characteristic that notifies press and release. Several can be connected at once, and they reconnect automatically.

The press handler is a small state machine:

  • A press must be held about 180 ms before the mic opens. Bumps do not transmit.
  • Release mutes synchronously, then re-checks at 50, 150 and 300 ms in case an async unmute landed late.
  • A short cooldown follows each release. A press during cooldown is remembered and starts when it ends.
  • Each press gets an id, so a delayed callback from an old press cannot open the mic for a new one.

The unmute itself re-checks after its await:

await room.localParticipant.setMicrophone(enabled: true)
if role == .pushToTalk && !buttonHeld {
    await room.localParticipant.setMicrophone(enabled: false)
    return
}

Multiple channels

A user can be in several channels at once, each with its own room. The limit comes from the server in the token response and defaults to four.

The system only knows about one of them. The primary channel is registered with PushToTalk and owns the mic. The others are listen-only until promoted. Switching primary force-mutes both the old and the new channel, then updates the system channel descriptor so the lock screen shows the right name.

Each BLE button can be assigned to a channel. Pressing a button assigned to a non-primary channel switches primary to it first. If you are not in that channel, you get a warning instead of a transmit on the wrong channel.

What broke in the field

The system removes the channel. A leave can arrive with an unknown reason. Rejoining is only permitted in the foreground, so the app flags it, keeps playing audio over the room, and re-registers when it is next active, capped at three attempts a minute. If the leave reason is system policy, it does not fight. If the user left from the system UI, all channels are left.

Restored channels after a kill. After a force-quit, the system restores the PushToTalk channel on next launch with no room behind it. The app leaves that stale channel immediately.

Rooms drop. Network loss, a server restart, or the app being suspended during a phone call all close the room. Recoverable disconnect reasons are an allow-list. For those, the app fetches a fresh token and reconnects in place with backoff from one to thirty seconds. A 403 on the token means access was revoked, and it leaves.

Phone calls. On interruption the mic is released at once. When the interruption ends, the session is reactivated and LiveKit reclaimed, unless a new transmit already owns the session.

Video players stole the session. Stream players set the .playback category, which has no input, and killed the mic. They also deactivated the session on teardown. Now a player touches the session only when no voice channel is joined.

Takeaways

  • Decide on one audio session owner and route every start, end and update through one seam.
  • With PushToTalk, record transmit intent on begin and start capture only in didActivate.
  • After the framework borrows the session, rebuild the WebRTC audio engine. Toggling the mic is not enough.
  • Treat release as the critical path: mute synchronously, then verify again.
  • Assume the system will remove your channel and the network will drop your room, and make both recoveries silent.
All writing

Let’s work
together

Get in touch
theasjadanwar@gmail.com+92 321 4010734Resume (PDF)LinkedInGitHub
Version2026 edition
Based inLahore, Pakistan (UTC+5)
SocialsLinkedInGitHub