You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SampleHandler.swift 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright @ 2021-present 8x8, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import ReplayKit
  17. import JitsiMeetSDK
  18. private enum Constants {
  19. // the App Group ID value that the app and the broadcast extension targets are setup with. It differs for each app.
  20. static let appGroupIdentifier = "group.org.jitsi.meet.appgroup"
  21. }
  22. class SampleHandler: RPBroadcastSampleHandler {
  23. private var clientConnection: SocketConnection?
  24. private var uploader: SampleUploader?
  25. private var frameCount: Int = 0
  26. var socketFilePath: String {
  27. let sharedContainer = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: Constants.appGroupIdentifier)
  28. return sharedContainer?.appendingPathComponent("rtc_SSFD").path ?? ""
  29. }
  30. override init() {
  31. super.init()
  32. if let connection = SocketConnection(filePath: socketFilePath) {
  33. clientConnection = connection
  34. setupConnection()
  35. uploader = SampleUploader(connection: connection)
  36. }
  37. }
  38. override func broadcastStarted(withSetupInfo setupInfo: [String: NSObject]?) {
  39. // User has requested to start the broadcast. Setup info from the UI extension can be supplied but optional.
  40. print("broadcast started")
  41. frameCount = 0
  42. DarwinNotificationCenter.shared.postNotification(.broadcastStarted)
  43. openConnection()
  44. }
  45. override func broadcastPaused() {
  46. // User has requested to pause the broadcast. Samples will stop being delivered.
  47. }
  48. override func broadcastResumed() {
  49. // User has requested to resume the broadcast. Samples delivery will resume.
  50. }
  51. override func broadcastFinished() {
  52. // User has requested to finish the broadcast.
  53. DarwinNotificationCenter.shared.postNotification(.broadcastStopped)
  54. clientConnection?.close()
  55. }
  56. override func processSampleBuffer(_ sampleBuffer: CMSampleBuffer, with sampleBufferType: RPSampleBufferType) {
  57. switch sampleBufferType {
  58. case RPSampleBufferType.video:
  59. // very simple mechanism for adjusting frame rate by using every third frame
  60. frameCount += 1
  61. if frameCount % 3 == 0 {
  62. uploader?.send(sample: sampleBuffer)
  63. }
  64. default:
  65. break
  66. }
  67. }
  68. }
  69. private extension SampleHandler {
  70. func setupConnection() {
  71. clientConnection?.didClose = { [weak self] error in
  72. print("client connection did close \(String(describing: error))")
  73. if let error = error {
  74. self?.finishBroadcastWithError(error)
  75. } else {
  76. // the displayed failure message is more user friendly when using NSError instead of Error
  77. let JMScreenSharingStopped = 10001
  78. let customError = NSError(domain: RPRecordingErrorDomain, code: JMScreenSharingStopped, userInfo: [NSLocalizedDescriptionKey: "Screen sharing stopped"])
  79. self?.finishBroadcastWithError(customError)
  80. }
  81. }
  82. }
  83. func openConnection() {
  84. let queue = DispatchQueue(label: "broadcast.connectTimer")
  85. let timer = DispatchSource.makeTimerSource(queue: queue)
  86. timer.schedule(deadline: .now(), repeating: .milliseconds(100), leeway: .milliseconds(500))
  87. timer.setEventHandler { [weak self] in
  88. guard self?.clientConnection?.open() == true else {
  89. return
  90. }
  91. timer.cancel()
  92. }
  93. timer.resume()
  94. }
  95. }