"Fossies" - the Fresh Open Source Software Archive 
Member "jitsi-meet-7315/ios/app/broadcast-extension/SampleHandler.swift" (2 Jun 2023, 4147 Bytes) of package /linux/misc/jitsi-meet-7315.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Swift source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
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
17 import ReplayKit
18
19 private enum Constants {
20 // the App Group ID value that the app and the broadcast extension targets are setup with. It differs for each app.
21 static let appGroupIdentifier = "group.org.jitsi.meet.appgroup"
22 }
23
24 class SampleHandler: RPBroadcastSampleHandler {
25
26 private var clientConnection: SocketConnection?
27 private var uploader: SampleUploader?
28
29 private var frameCount: Int = 0
30
31 var socketFilePath: String {
32 let sharedContainer = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: Constants.appGroupIdentifier)
33
34 return sharedContainer?.appendingPathComponent("rtc_SSFD").path ?? ""
35 }
36
37 override init() {
38 super.init()
39 if let connection = SocketConnection(filePath: socketFilePath) {
40 clientConnection = connection
41 setupConnection()
42
43 uploader = SampleUploader(connection: connection)
44 }
45 }
46
47 override func broadcastStarted(withSetupInfo setupInfo: [String: NSObject]?) {
48 // User has requested to start the broadcast. Setup info from the UI extension can be supplied but optional.
49 print("broadcast started")
50
51 frameCount = 0
52
53 DarwinNotificationCenter.shared.postNotification(.broadcastStarted)
54 openConnection()
55 }
56
57 override func broadcastPaused() {
58 // User has requested to pause the broadcast. Samples will stop being delivered.
59 }
60
61 override func broadcastResumed() {
62 // User has requested to resume the broadcast. Samples delivery will resume.
63 }
64
65 override func broadcastFinished() {
66 // User has requested to finish the broadcast.
67 DarwinNotificationCenter.shared.postNotification(.broadcastStopped)
68 clientConnection?.close()
69 }
70
71 override func processSampleBuffer(_ sampleBuffer: CMSampleBuffer, with sampleBufferType: RPSampleBufferType) {
72 switch sampleBufferType {
73 case RPSampleBufferType.video:
74 // very simple mechanism for adjusting frame rate by using every third frame
75 frameCount += 1
76 if frameCount % 3 == 0 {
77 uploader?.send(sample: sampleBuffer)
78 }
79 default:
80 break
81 }
82 }
83 }
84
85 private extension SampleHandler {
86
87 func setupConnection() {
88 clientConnection?.didClose = { [weak self] error in
89 print("client connection did close \(String(describing: error))")
90
91 if let error = error {
92 self?.finishBroadcastWithError(error)
93 } else {
94 // the displayed failure message is more user friendly when using NSError instead of Error
95 let JMScreenSharingStopped = 10001
96 let customError = NSError(domain: RPRecordingErrorDomain, code: JMScreenSharingStopped, userInfo: [NSLocalizedDescriptionKey: "Screen sharing stopped"])
97 self?.finishBroadcastWithError(customError)
98 }
99 }
100 }
101
102 func openConnection() {
103 let queue = DispatchQueue(label: "broadcast.connectTimer")
104 let timer = DispatchSource.makeTimerSource(queue: queue)
105 timer.schedule(deadline: .now(), repeating: .milliseconds(100), leeway: .milliseconds(500))
106 timer.setEventHandler { [weak self] in
107 guard self?.clientConnection?.open() == true else {
108 return
109 }
110
111 timer.cancel()
112 }
113
114 timer.resume()
115 }
116 }