Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions callkit/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.png filter=lfs diff=lfs merge=lfs -text
62 changes: 62 additions & 0 deletions callkit/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

## User settings
xcuserdata/

## Obj-C/Swift specific
*.hmap

## App packaging
*.ipa
*.dSYM.zip
*.dSYM

## Playgrounds
timeline.xctimeline
playground.xcworkspace

# Swift Package Manager
#
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
# Packages/
# Package.pins
# Package.resolved
# *.xcodeproj
#
# Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata
# hence it is not needed unless you have added a package configuration file to your project
# .swiftpm

.build/

# CocoaPods
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
# Pods/
#
# Add this line if you want to avoid checking in source code from the Xcode workspace
# *.xcworkspace

# Carthage
#
# Add this line if you want to avoid checking in source code from Carthage dependencies.
# Carthage/Checkouts

Carthage/Build/

# fastlane
#
# It is recommended to not store the screenshots in the git repo.
# Instead, use fastlane to re-generate the screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://docs.fastlane.tools/best-practices/source-control/#source-control

fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots/**/*.png
fastlane/test_output
1 change: 1 addition & 0 deletions callkit/.swift-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5.9
4 changes: 4 additions & 0 deletions callkit/.swiftformat
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--exclude Sources/LiveKit/Protos
--header "/*\n * Copyright {year} LiveKit\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */"
--ifdef no-indent
--disable modifiersOnSameLine
64 changes: 64 additions & 0 deletions callkit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# CallKit + PushKit example

This example demonstrates how to use the LiveKit Swift SDK with CallKit and PushKit.

### Testing remote VoIP pushes

Use the [Push Notification Console](https://developer.apple.com/documentation/usernotifications/testing-notifications-using-the-push-notification-console) to send a `VoIP` push to your token.

You can obtain the push token by running the example app on a device.

### Key points when integrating LiveKit SDK with CallKit

CallKit on iOS sets AVAudioSession active automatically at specific timings.
By default, the LiveKit Swift SDK also configures the AVAudioSession automatically, which will interfere with CallKit.

We should ensure proper timing when configuring `AVAudioSession` and starting the SDK's internal `AVAudioEngine` between
[provider(_:didActivate:)](https://developer.apple.com/documentation/callkit/cxproviderdelegate/provider(_:didactivate:)) and [provider(_:didDeactivate:)](https://developer.apple.com/documentation/callkit/cxproviderdelegate/provider(_:diddeactivate:)).

Early in the process, we should disable automatic AVAudioSession configuration:
```swift
AudioManager.shared.audioSession.isAutomaticConfigurationEnabled = false
```

We should also ensure the SDK's internal `AVAudioEngine` will not start, even when subscribing to remote audio or publishing microphone:
```swift
try AudioManager.shared.setEngineAvailability(.none)
```

Now, in the `CXProviderDelegate`, we want to ensure `AVAudioSession.category` is set to `.playAndRecord` (if you will publish microphone).
We also want to allow the SDK's internal `AVAudioEngine` to start within the `didActivate` ~ `didDeactivate` window.
```swift
func provider(_: CXProvider, didActivate session: AVAudioSession) {
do {
try session.setCategory(.playAndRecord, mode: .voiceChat, options: [.mixWithOthers])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just become the delegate and do it under the hood?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, we could, but I’m starting to think that configuring AVAudioSession on the user’s behalf is not a good idea in the long run.

try AudioManager.shared.setEngineAvailability(.default)
} catch {
// Error
}
}

func provider(_: CXProvider, didDeactivate _: AVAudioSession) {
do {
try AudioManager.shared.setEngineAvailability(.none)
} catch {
// Error
}
}
```
> **NOTE:** We don't call `session.setActive(true)` since CallKit already activates it and Apple doesn't recommend calling it again.

With this setup, you can connect, publish, and subscribe to audio at timings convenient to your implementation and requirements. See the source code of this example for details.

### CallKit lock screen / background issues

These are issues I've encountered while implementing this example. Please let me know if there are better workarounds or if I'm incorrect.

1. [reportNewIncomingCall(with:update:completion:)](https://developer.apple.com/documentation/callkit/cxprovider/reportnewincomingcall(with:update:completion:)) must be invoked on the same thread as [pushRegistry(_:didReceiveIncomingPushWith:for:completion:)](https://developer.apple.com/documentation/pushkit/pkpushregistrydelegate/pushregistry(_:didreceiveincomingpushwith:for:completion:)), otherwise it will silently fail and not show the incoming call UI on the lock screen. Therefore, the async version may not work as intended.

2. You may need to set `AVAudioSession.category` to `.playAndRecord` before calling [reportNewIncomingCall(with:update:completion:)](https://developer.apple.com/documentation/callkit/cxprovider/reportnewincomingcall(with:update:completion:)) when your app is woken up in the background by [pushRegistry(_:didReceiveIncomingPushWith:for:completion:)](https://developer.apple.com/documentation/pushkit/pkpushregistrydelegate/pushregistry(_:didreceiveincomingpushwith:for:completion:)).

### References

[Responding to VoIP Notifications from PushKit](https://developer.apple.com/documentation/pushkit/responding-to-voip-notifications-from-pushkit)
[Developer Forums: CallKit does not activate audio session](https://developer.apple.com/forums/thread/783870)
10 changes: 10 additions & 0 deletions callkit/callkit-dev.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading