Skip to content

Commit

Permalink
Don't show error dialog when broadcast stops without error (Obj-C bri…
Browse files Browse the repository at this point in the history
…dge) (#519)

This is an alternative to #517 that uses the same Objective-C bridge as
[used in
Telegram](https://github.com/TelegramMessenger/Telegram-iOS/blob/9a46522b431782147cc1d95304180d91d398dd82/submodules/BroadcastUploadHelpers/Sources/BroadcastUploadHelpers.m),
instead of some selector and IMP casting magic in Swift

---------

Co-authored-by: hiroshihorie <[email protected]>
  • Loading branch information
bcherry and hiroshihorie authored Dec 4, 2024
1 parent eab7650 commit 81a80c9
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 8 deletions.
5 changes: 5 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@ let package = Package(
.package(url: "https://github.com/vapor/jwt-kit.git", from: "4.13.4"),
],
targets: [
.target(
name: "LKObjCHelpers",
publicHeadersPath: "include"
),
.target(
name: "LiveKit",
dependencies: [
.product(name: "LiveKitWebRTC", package: "webrtc-xcframework"),
.product(name: "SwiftProtobuf", package: "swift-protobuf"),
.product(name: "Logging", package: "swift-log"),
"LKObjCHelpers",
],
resources: [
.process("PrivacyInfo.xcprivacy"),
Expand Down
5 changes: 5 additions & 0 deletions [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,17 @@ let package = Package(
.package(url: "https://github.com/vapor/jwt-kit.git", from: "4.13.4"),
],
targets: [
.target(
name: "LKObjCHelpers",
publicHeadersPath: "include"
),
.target(
name: "LiveKit",
dependencies: [
.product(name: "LiveKitWebRTC", package: "webrtc-xcframework"),
.product(name: "SwiftProtobuf", package: "swift-protobuf"),
.product(name: "Logging", package: "swift-log"),
"LKObjCHelpers",
],
resources: [
.process("PrivacyInfo.xcprivacy"),
Expand Down
19 changes: 19 additions & 0 deletions Sources/LKObjCHelpers/LKObjcCHelpers.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#import "LKObjcHelpers.h"

@implementation LKObjCHelpers

NS_ASSUME_NONNULL_BEGIN

+ (void)finishBroadcastWithoutError:(RPBroadcastSampleHandler *)handler API_AVAILABLE(macos(11.0)) {
// Call finishBroadcastWithError with nil error, which ends the broadcast without an error popup
// This is unsupported/undocumented but appears to work and is preferable to an error dialog with a cryptic default message
// See https://stackoverflow.com/a/63402492 for more discussion
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnonnull"
[handler finishBroadcastWithError:nil];
#pragma clang diagnostic pop
}

NS_ASSUME_NONNULL_END

@end
8 changes: 8 additions & 0 deletions Sources/LKObjCHelpers/include/LKObjcHelpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#import <Foundation/Foundation.h>
#import <ReplayKit/ReplayKit.h>

@interface LKObjCHelpers : NSObject

+ (void)finishBroadcastWithoutError:(RPBroadcastSampleHandler *)handler API_AVAILABLE(macos(11.0));

@end
36 changes: 28 additions & 8 deletions Sources/LiveKit/Broadcast/Uploader/LKSampleHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import ReplayKit
#endif

import LKObjCHelpers

open class LKSampleHandler: RPBroadcastSampleHandler {
private var clientConnection: BroadcastUploadSocketConnection?
private var uploader: SampleUploader?
Expand Down Expand Up @@ -78,18 +80,36 @@ open class LKSampleHandler: RPBroadcastSampleHandler {
}
}

/// Override point to change the behavior when the socket connection has closed.
/// The default behavior is to pass errors through, and otherwise show nothing to the user.
///
/// You should call `finishBroadcastWithError` in your implementation, but you can
/// add custom logging or present a custom error to the user instead.
///
/// To present a custom error message:
/// ```
/// self.finishBroadcastWithError(NSError(
/// domain: RPRecordingErrorDomain,
/// code: 10001,
/// userInfo: [NSLocalizedDescriptionKey: "My Custom Error Message"]
/// ))
/// ```
open func connectionDidClose(error: Error?) {
if let error {
finishBroadcastWithError(error)
} else {
LKObjCHelpers.finishBroadcastWithoutError(self)
}
}

private func setupConnection() {
clientConnection?.didClose = { [weak self] error in
logger.log(level: .debug, "client connection did close \(String(describing: error))")

if let error {
self?.finishBroadcastWithError(error)
} else {
// the displayed failure message is more user friendly when using NSError instead of Error
let LKScreenSharingStopped = 10001
let customError = NSError(domain: RPRecordingErrorDomain, code: LKScreenSharingStopped, userInfo: [NSLocalizedDescriptionKey: "Screen sharing stopped"])
self?.finishBroadcastWithError(customError)
guard let self else {
return
}

self.connectionDidClose(error: error)
}
}

Expand Down

0 comments on commit 81a80c9

Please sign in to comment.