I have a sample app with ViewController.swift
and Broadcast Upload app Extension
. Inside my project, I have two frameworks that I've created :
MySDK
, is a swift framework using to analyze and processCMSampleBuffer
in order to avoid that Broadcast App Extension consumes too much memory. This SDK has a singleton, a variableisReady
, functionsfunc initialize
andfunc analyzeSampleBuffer(_ sampleBuffer: CMSampleBuffer)
Broadcaster
, is a swift framework using to callMySDK
call for analyze and processCMSampleBuffer
(yes, sounds like a duplicate with MySDK, but I can't do otherwise, I need these two frameworks). This SDK has a singleton and following functions :func broadcastStarted(withSetupInfo setupInfo: [String : NSObject]?)
,func processSampleBuffer(_ sampleBuffer: CMSampleBuffer)
In this project I've added a Broadcast Upload App Extension
named BroadcastExtension
.
In his main file SampleHandler.swift
, in processSampleBuffer
function I use my Broadcaster SDK
to give responsability from app extension to Broadcaster SDK
and after to MySDK
to stream CMSampleBuffer
that I receive from app extension : Broadcaster.shared.processSampleBuffer(sampleBuffer)
. At the end, MySDK
succeeds to analyze and process CMSampleBuffer
but my broadcast upload app extension takes too much memory, and crash after X minutes after screen sharing ( 50Mb max for app extension ). How can I use less memory on app extension ?
Here are my files :
MySDK.swift :
import Foundation
import ReplayKit
@objcMembers public class MySDK {
public static let shared = MySDK()
public var isReady = false
public func initialize() {
// Init SDK
}
public func analyzeSampleBuffer(_ sampleBuffer: CMSampleBuffer) {
// Analyze
}
}
Broadcaster.swift :
import Foundation
import ReplayKit
import MySDK
@objcMembers public class Broadcaster: NSObject, Codable {
public static let shared = Broadcaster()
public func broadcastStarted(withSetupInfo setupInfo: [String : NSObject]?) {
MySDK.shared.isReady = true
}
public func processSampleBuffer(_ sampleBuffer: CMSampleBuffer) {
if MySDK.shared.isReady {
MySDK.shared.analyzeSampleBuffer(sampleBuffer)
}
}
}
SampleHandler.swift :
import ReplayKit
import Broadcaster
class SampleHandler: RPBroadcastSampleHandler {
override func broadcastStarted(withSetupInfo setupInfo: [String : NSObject]?) {
// User has requested to start the broadcast. Setup info from the UI extension can be supplied but optional.
Broadcaster.shared.broadcastStarted(withSetupInfo: setupInfo)
}
override func processSampleBuffer(_ sampleBuffer: CMSampleBuffer, with sampleBufferType: RPSampleBufferType) {
switch sampleBufferType {
case RPSampleBufferType.video:
// Handle video sample buffer
Broadcaster.shared.processSampleBuffer(sampleBuffer)
break
case RPSampleBufferType.audioApp:
break
case RPSampleBufferType.audioMic:
break
@unknown default:
fatalError("Unknown type of sample buffer")
}
}
}
I would like to get same shared Instance used in app, but in app extension. I tried to put group apps between app and app extension, and to use Userdefaults
with suiteName
corresponding to the group id, to send the shared instance, but when I receive in app extension, the address memory is not the same, that creates another instance of the object (I want a real singleton between app and app extension). I don't know how to save memory on app extension, and how to communicate between the 2 frameworks, app extension and app to use the same singleton on each part of project.