WebRTCCommon.swift 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //
  2. // SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  3. // SPDX-License-Identifier: GPL-3.0-or-later
  4. //
  5. import Foundation
  6. @objcMembers final class WebRTCCommon: NSObject {
  7. public static let shared = WebRTCCommon()
  8. public lazy var peerConnectionFactory: RTCPeerConnectionFactory = {
  9. return RTCPeerConnectionFactory(encoderFactory: encoderFactory, decoderFactory: decoderFactory)
  10. }()
  11. private lazy var encoderFactory: RTCVideoEncoderFactory = {
  12. return RTCDefaultVideoEncoderFactory()
  13. }()
  14. private lazy var decoderFactory: RTCVideoDecoderFactory = {
  15. return RTCDefaultVideoDecoderFactory()
  16. }()
  17. private let webrtcClientDispatchQueue = DispatchQueue(label: "webrtcClientDispatchQueue")
  18. private override init() {
  19. super.init()
  20. }
  21. // Every call into the WebRTC library must be dispatched to this queue
  22. public func dispatch(_ work: @escaping @convention(block) () -> Void) {
  23. webrtcClientDispatchQueue.async(execute: work)
  24. }
  25. public func assertQueue() {
  26. dispatchPrecondition(condition: .onQueue(webrtcClientDispatchQueue))
  27. }
  28. public func printNumberOfOpenSocketDescriptors() {
  29. print("File descriptors: \(self.openFilePaths().filter { $0 == "?" }.count)")
  30. }
  31. private func openFilePaths() -> [String] {
  32. // from https://developer.apple.com/forums/thread/655225?answerId=623114022#623114022
  33. (0..<getdtablesize()).map { fd in
  34. // Return "" for invalid file descriptors.
  35. var flags: CInt = 0
  36. guard fcntl(fd, F_GETFL, &flags) >= 0 else {
  37. return ""
  38. }
  39. // Return "?" for file descriptors not associated with a path, for
  40. // example, a socket.
  41. var path = [CChar](repeating: 0, count: Int(MAXPATHLEN))
  42. guard fcntl(fd, F_GETPATH, &path) >= 0 else {
  43. return "?"
  44. }
  45. return String(cString: path)
  46. }
  47. }
  48. }