use safeTimer for micHandler
This commit is contained in:
1 parent
76d138d64d
commit
63808c9e9c
3 files changed
+16
-17
No files matched your search
@@ -23,7 +23,7 @@ public class MicHandler: NSObject {
|
||||
|
||||
private var pcm16Buffer: [Int16] = []
|
||||
private let bufferQueue = DispatchQueue(label: "pcm.buffer.queue")
|
||||
private var timer: DispatchSourceTimer?
|
||||
private var timer: SafeTimer?
|
||||
|
||||
/*
|
||||
private var recordedBuffers: [AVAudioPCMBuffer] = []
|
||||
@@ -157,6 +157,7 @@ public class MicHandler: NSObject {
|
||||
@objc public func startTapping() {
|
||||
// recordedBuffers.removeAll()
|
||||
isRecording = true
|
||||
self.timer?.resume()
|
||||
/*
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + seconds) { [weak self] in
|
||||
guard let self = self else { return }
|
||||
@@ -169,6 +170,7 @@ public class MicHandler: NSObject {
|
||||
|
||||
@objc public func stopTapping(stopEngine:Bool) {
|
||||
isRecording = false
|
||||
self.timer?.suspend()
|
||||
// engine.inputNode.removeTap(onBus: 0)
|
||||
if(stopEngine){
|
||||
playerNode.stop()
|
||||
@@ -243,13 +245,9 @@ public class MicHandler: NSObject {
|
||||
engine.connect(engine.inputNode, to: audioSink as! AVAudioNode, format: micInputFormat)
|
||||
|
||||
// 开定时器,每 20ms 触发一次
|
||||
self.timer = DispatchSource.makeTimerSource(queue: .global())
|
||||
self.timer?.schedule(deadline: .now()+0.1, repeating: 0.02)
|
||||
self.timer?.setEventHandler { [weak self] in
|
||||
self?.sendOpusFrame()
|
||||
self.timer = SafeTimer(interval:0.02, delay: 0.05) {
|
||||
self.sendOpusFrame()
|
||||
}
|
||||
self.timer?.resume()
|
||||
|
||||
}
|
||||
else{
|
||||
engine.attach(playerNode)
|
||||
|
||||
@@ -11,12 +11,14 @@ class SafeTimer {
|
||||
private var timer: DispatchSourceTimer
|
||||
private(set) var isRunning = false
|
||||
private var interval: TimeInterval = 0
|
||||
private var delay: TimeInterval = 0
|
||||
|
||||
init(interval: TimeInterval = 1.0, queue: DispatchQueue = .global(), handler: @escaping () -> Void) {
|
||||
init(interval: TimeInterval = 1.0, queue: DispatchQueue = .global(), delay: TimeInterval = 0, handler: @escaping () -> Void) {
|
||||
timer = DispatchSource.makeTimerSource(queue: queue)
|
||||
timer.schedule(deadline: .now(), repeating: interval)
|
||||
timer.schedule(deadline: .now() + delay, repeating: interval)
|
||||
timer.setEventHandler(handler: handler)
|
||||
self.interval = interval
|
||||
self.delay = delay
|
||||
// 注意:这里不调用 resume,保持 suspended,等 start() 时才运行
|
||||
}
|
||||
|
||||
@@ -37,7 +39,7 @@ class SafeTimer {
|
||||
timer.suspend()
|
||||
isRunning = false
|
||||
}
|
||||
timer.schedule(deadline: .now(), repeating: self.interval) // 重置时间
|
||||
timer.schedule(deadline: .now() + self.delay, repeating: self.interval) // 重置时间
|
||||
timer.resume()
|
||||
isRunning = true
|
||||
}
|
||||
|
||||
@@ -1275,18 +1275,17 @@
|
||||
- (void) stageComplete:(const char*)stageName {
|
||||
_micStreamInitialized = false;
|
||||
if(strcmp(stageName, "mic stream establishment")==0){
|
||||
|
||||
if(_streamConfig.redirectMic){
|
||||
dispatch_after((int64_t)(5 * NSEC_PER_SEC), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
self->_micStreamInitialized = true;
|
||||
self->micHandler = [MicHandler new];
|
||||
[self->micHandler startTapping];
|
||||
});
|
||||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
||||
self->_micStreamInitialized = true;
|
||||
self->micHandler = [MicHandler new];
|
||||
[self->micHandler startTapping];
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if(strcmp(stageName, "mic stream unsupported or unintialized")==0){
|
||||
NSLog(@"blablablala");
|
||||
_micStreamInitialized = false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user