Compare commits

...

8 Commits

Author SHA1 Message Date
tanhakabir bf724ab04e Merge branch 'master' into issue-67 2021-03-22 23:06:44 -07:00
tanhakabir 81d93fd886 Merge branch 'master' into issue-67 2021-03-18 01:06:17 -07:00
tanhakabir 22bbe7fa5a checkpoint 2021-03-09 16:58:51 -08:00
tanhakabir 475a2a2b37 add seeking to throttler diagram 2021-03-06 21:13:36 -08:00
tanhakabir b4b571f68b create throttler state machine diagram 2021-03-06 21:06:46 -08:00
tanhakabir 1dfc5095e9 Merge branch 'master' into issue-67 2021-03-06 20:36:27 -08:00
tanhakabir b6da69c798 checkpoint 2021-03-06 18:37:04 -08:00
tanhakabir ce07c919a9 add states to logs 2021-03-05 18:20:50 -08:00
5 changed files with 102 additions and 25 deletions
+4
View File
@@ -15,6 +15,7 @@
831B263D357A5FA2DDC7B1AE4B374092 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5A16F4CFC63FAC439D7A04994F579A03 /* Foundation.framework */; };
8F93DB166237195ED222EE55B6404625 /* Pods-SwiftAudioPlayer_Example-umbrella.h in Headers */ = {isa = PBXBuildFile; fileRef = 3B0B76CB1439F4D361322144E5A65C3A /* Pods-SwiftAudioPlayer_Example-umbrella.h */; settings = {ATTRIBUTES = (Public, ); }; };
A40DBE292391D9CA00F86146 /* Data.swift in Sources */ = {isa = PBXBuildFile; fileRef = A40DBE282391D9C900F86146 /* Data.swift */; };
A411CE3E25F55C0E0039E1CD /* AudioThrottlerNew.swift in Sources */ = {isa = PBXBuildFile; fileRef = A411CE3D25F55C0E0039E1CD /* AudioThrottlerNew.swift */; };
A411CE4625F9609D0039E1CD /* SAPlayerFeatures.swift in Sources */ = {isa = PBXBuildFile; fileRef = A411CE4525F9609D0039E1CD /* SAPlayerFeatures.swift */; };
A41AA0D2238BB9B600A467E1 /* SAPlayingStatus.swift in Sources */ = {isa = PBXBuildFile; fileRef = A41AA0D1238BB9B600A467E1 /* SAPlayingStatus.swift */; };
A4681FC6220113880018AB51 /* SAPlayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = A4681F8D2200E00E0018AB51 /* SAPlayer.swift */; };
@@ -101,6 +102,7 @@
A19C8F889C787C19BE4123C1896AF501 /* Pods-SwiftAudioPlayer_Example-resources.sh */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.script.sh; path = "Pods-SwiftAudioPlayer_Example-resources.sh"; sourceTree = "<group>"; };
A39F2A138CF40C1051CA9E227429A86D /* SwiftAudioPlayer.modulemap */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.module; path = SwiftAudioPlayer.modulemap; sourceTree = "<group>"; };
A40DBE282391D9C900F86146 /* Data.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Data.swift; sourceTree = "<group>"; };
A411CE3D25F55C0E0039E1CD /* AudioThrottlerNew.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AudioThrottlerNew.swift; sourceTree = "<group>"; };
A411CE4525F9609D0039E1CD /* SAPlayerFeatures.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SAPlayerFeatures.swift; sourceTree = "<group>"; };
A41AA0D1238BB9B600A467E1 /* SAPlayingStatus.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SAPlayingStatus.swift; sourceTree = "<group>"; };
A4523BC8220A0B3C0079C4BC /* Credited_LICENSE */ = {isa = PBXFileReference; lastKnownFileType = text; path = Credited_LICENSE; sourceTree = "<group>"; };
@@ -297,6 +299,7 @@
A4681FB52200FDF30018AB51 /* Converter */,
A4681FAA2200F8280018AB51 /* Parser */,
A4681FA82200F5A20018AB51 /* AudioThrottler.swift */,
A411CE3D25F55C0E0039E1CD /* AudioThrottlerNew.swift */,
);
path = Engine;
sourceTree = "<group>";
@@ -574,6 +577,7 @@
A4681FCD2201139E0018AB51 /* AudioStreamEngine.swift in Sources */,
A411CE4625F9609D0039E1CD /* SAPlayerFeatures.swift in Sources */,
A4681FD9220113CD0018AB51 /* AudioStreamWorker.swift in Sources */,
A411CE3E25F55C0E0039E1CD /* AudioThrottlerNew.swift in Sources */,
A4681FDF220113E20018AB51 /* DirectorThreadSafeClosures.swift in Sources */,
A4681FCB220113980018AB51 /* AudioEngine.swift in Sources */,
);
+33 -11
View File
@@ -42,6 +42,15 @@ protocol AudioThrottleable {
}
class AudioThrottler: AudioThrottleable {
enum State: String {
case INIT
case WAITING_FOR_DATA
case RECEIVING_DATA
case PUSH_DATA
case END_OF_DATA
case CLEAN_UP
}
private class NetworkDataWrapper: NSObject {
let startOffset: UInt
var data: Data
@@ -93,6 +102,7 @@ class AudioThrottler: AudioThrottleable {
//Init
let url: AudioURL
weak var delegate: AudioThrottleDelegate?
private var state: State
private var networkData: [NetworkDataWrapper] = []
var shouldThrottle = false
@@ -110,12 +120,16 @@ class AudioThrottler: AudioThrottleable {
var largestPollingOffsetDifference: UInt64 = 1
required init(withRemoteUrl url: AudioURL, withDelegate delegate: AudioThrottleDelegate) {
self.state = .INIT
self.url = url
self.delegate = delegate
self.state = .WAITING_FOR_DATA
AudioDataManager.shared.startStream(withRemoteURL: url) { [weak self] (pto: StreamProgressPTO) in
guard let self = self else {return}
Log.debug("received stream data of size \(pto.getData().count) and progress: \(pto.getProgress())")
guard let self = self else { return }
if !self.shouldThrottle { self.state = .RECEIVING_DATA }
Log.debug("received stream data of size \(pto.getData().count) and progress: \(pto.getProgress())", state: self.state.rawValue)
self.delegate?.didUpdate(networkStreamProgress: pto.getProgress())
if let totalBytesExpected = pto.getTotalBytesExpected() {
@@ -129,7 +143,8 @@ class AudioThrottler: AudioThrottleable {
self.networkData.append(wrappedNetworkData)
if !self.shouldThrottle {
Log.debug("sending up packet from stream untrottled at start: \(wrappedNetworkData.startOffset)")
self.state = .PUSH_DATA
Log.debug("sending up packet from stream untrottled at start: \(wrappedNetworkData.startOffset)", state: self.state.rawValue)
//NOTE: the order here matters.
//We have to set to true before sending up to be processed because
//tellByteOffset() is ran in a separate thread than this one
@@ -143,6 +158,7 @@ class AudioThrottler: AudioThrottleable {
func tellAudioFormatFound() {
shouldThrottle = true //the above layer has enough info that we can throttle
self.state = .RECEIVING_DATA
}
func tellBytesPerAudioPacket(count: UInt64) {
@@ -152,14 +168,14 @@ class AudioThrottler: AudioThrottleable {
}
func tellByteOffset(offset: UInt64) {
Log.debug("offset \(offset)")
Log.debug("offset \(offset)", state: self.state.rawValue)
for wrappedNetworkData in networkData {
if wrappedNetworkData.containsOffset(UInt(offset)) {
Log.debug("offset: \(offset) within network packet of range: \(wrappedNetworkData.startOffset) to \(wrappedNetworkData.endOffset) is next sent: \(wrappedNetworkData.isNextSent())")
Log.debug("offset: \(offset) within network packet of range: \(wrappedNetworkData.startOffset) to \(wrappedNetworkData.endOffset) is next sent: \(wrappedNetworkData.isNextSent())", state: self.state.rawValue)
if wrappedNetworkData.alreadySent {
Log.debug("already sent offset: \(offset) within network packet of range: \(wrappedNetworkData.startOffset) to \(wrappedNetworkData.endOffset)")
Log.debug("already sent offset: \(offset) within network packet of range: \(wrappedNetworkData.startOffset) to \(wrappedNetworkData.endOffset)", state: self.state.rawValue)
var bytesSent: UInt = 0
var current = wrappedNetworkData
@@ -169,14 +185,16 @@ class AudioThrottler: AudioThrottleable {
while bytesSent < largestPollingOffsetDifference {
if let next = current.next {
if !next.alreadySent {
Log.info("Sending next network packet with range: \(next.startOffset) to \(next.endOffset), have sent \(bytesSent) bytes so far from \(largestPollingOffsetDifference) bytes")
self.state = .PUSH_DATA
Log.info("Sending next network packet with range: \(next.startOffset) to \(next.endOffset), have sent \(bytesSent) bytes so far from \(largestPollingOffsetDifference) bytes", self.state.rawValue)
next.alreadySent = true
delegate?.shouldProcess(networkData: next.data)
}
bytesSent += next.byteCount
current = next
} else {
Log.debug("next package doesn't exist, bytes sent so far: \(bytesSent)")
Log.debug("next package doesn't exist, bytes sent so far: \(bytesSent)", state: self.state.rawValue)
self.state = .END_OF_DATA
return
}
}
@@ -184,16 +202,19 @@ class AudioThrottler: AudioThrottleable {
return
}
Log.info("Found network packet to send with range: \(wrappedNetworkData.startOffset) to \(wrappedNetworkData.endOffset)")
Log.info("Found network packet to send with range: \(wrappedNetworkData.startOffset) to \(wrappedNetworkData.endOffset)", self.state.rawValue)
wrappedNetworkData.alreadySent = true
self.state = .PUSH_DATA
delegate?.shouldProcess(networkData: wrappedNetworkData.data)
return
} else {
self.state = .END_OF_DATA
}
}
}
func tellSeek(offset: UInt64) {
Log.info("seek with offset: \(offset)")
Log.info("seek with offset: \(offset)", self.state.rawValue)
if networkData.count == 0 {
byteOffsetBecauseOfSeek = UInt(offset)
@@ -225,7 +246,7 @@ class AudioThrottler: AudioThrottleable {
d.alreadySent = false
wrappedData.alreadySent = true
Log.info("\(d) ::: \(wrappedData)")
Log.info("\(d) ::: \(wrappedData)", self.state.rawValue)
delegate?.shouldProcess(networkData: wrappedData.data)
return
@@ -242,5 +263,6 @@ class AudioThrottler: AudioThrottleable {
func invalidate() {
AudioDataManager.shared.deleteStream(withRemoteURL: url)
self.state = .CLEAN_UP
}
}
+49
View File
@@ -0,0 +1,49 @@
//
// AudioThrottlerNew.swift
// SwiftAudioPlayer
//
// Created by Tanha Kabir on 3/7/21.
//
import Foundation
class AudioThrottlerNew: AudioThrottleable {
enum State: String {
case INIT
case WAITING_FOR_DATA
case RECEIVING_DATA
case PUSH_DATA
case END_OF_DATA
case CLEAN_UP
}
private var delegate: AudioThrottleDelegate
required init(withRemoteUrl url: AudioURL, withDelegate delegate: AudioThrottleDelegate) {
self.delegate = delegate
}
func tellAudioFormatFound() {
<#code#>
}
func tellByteOffset(offset: UInt64) {
<#code#>
}
func tellSeek(offset: UInt64) {
<#code#>
}
func tellBytesPerAudioPacket(count: UInt64) {
<#code#>
}
func pollRangeOfBytesAvailable() -> (UInt64, UInt64) {
<#code#>
}
func invalidate() {
<#code#>
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 295 KiB

+16 -14
View File
@@ -23,6 +23,8 @@ enum LogLevel: Int {
// Specify which types of log messages to display. Default level is set to WARN, which means Log will print any log messages of type only WARN, ERROR, MONITOR, and TEST. To print DEBUG and INFO logs, set the level to a lower value.
var logLevel: LogLevel = LogLevel.MONITOR
typealias State = String
class Log {
private init() {}
@@ -42,11 +44,11 @@ class Log {
- Parameter functionName: automatically generated based on the function that called this function
- Parameter lineNumber: automatically generated based on the line that called this function
*/
public static func test(_ logMessage: Any, classPath: String = #file, functionName: String = #function, lineNumber: Int = #line) {
public static func test(_ logMessage: Any, _ state:State? = nil, classPath: String = #file, functionName: String = #function, lineNumber: Int = #line) {
let fileName = URLUtil.getNameFromStringPath(classPath)
if logLevel.rawValue <= LogLevel.TEST.rawValue {
let log = OSLog(subsystem: SUBSYSTEM, category: "TEST ❇️❇️❇️❇️")
os_log("%@:%@:%d:: %@", log: log, fileName, functionName, lineNumber, "\(logMessage)")
os_log("%@:%@:%d:: %@", log: log, fileName, functionName, lineNumber, "\(state ?? "") \(logMessage)")
}
}
@@ -63,16 +65,16 @@ class Log {
- Parameter functionName: automatically generated based on the function that called this function
- Parameter lineNumber: automatically generated based on the line that called this function
*/
public static func error(_ logMessage: Any, classPath: String = #file, functionName: String = #function, lineNumber: Int = #line) {
public static func error(_ logMessage: Any, _ state:State? = nil, classPath: String = #file, functionName: String = #function, lineNumber: Int = #line) {
let fileName = URLUtil.getNameFromStringPath(classPath)
if logLevel.rawValue <= LogLevel.ERROR.rawValue {
let log = OSLog(subsystem: SUBSYSTEM, category: "ERROR 🛑🛑🛑🛑")
os_log("%@:%@:%d:: %@", log: log, fileName, functionName, lineNumber, "\(logMessage)")
os_log("%@:%@:%d:: %@", log: log, fileName, functionName, lineNumber, "\(state ?? "") \(logMessage)")
}
if logLevel.rawValue <= LogLevel.EXTERNAL_DEBUG.rawValue {
let log = OSLog(subsystem: SUBSYSTEM, category: "WARNING")
os_log("%@:%@:%d:: %@", log: log, fileName, functionName, lineNumber, "\(logMessage)")
os_log("%@:%@:%d:: %@", log: log, fileName, functionName, lineNumber, "\(state ?? "") \(logMessage)")
}
}
@@ -89,11 +91,11 @@ class Log {
- Parameter functionName: automatically generated based on the function that called this function
- Parameter lineNumber: automatically generated based on the line that called this function
*/
public static func monitor(_ logMessage: Any, classPath: String = #file, functionName: String = #function, lineNumber: Int = #line) {
public static func monitor(_ logMessage: Any, _ state:State? = nil, classPath: String = #file, functionName: String = #function, lineNumber: Int = #line) {
let fileName = URLUtil.getNameFromStringPath(classPath)
if logLevel.rawValue <= LogLevel.ERROR.rawValue {
let log = OSLog(subsystem: SUBSYSTEM, category: "ERROR 🔥🔥🔥🔥")
os_log("%@:%@:%d:: %@", log: log, fileName, functionName, lineNumber, "\(logMessage)")
os_log("%@:%@:%d:: %@", log: log, fileName, functionName, lineNumber, "\(state ?? "") \(logMessage)")
}
}
@@ -110,16 +112,16 @@ class Log {
- Parameter functionName: automatically generated based on the function that called this function
- Parameter lineNumber: automatically generated based on the line that called this function
*/
public static func warn(_ logMessage: Any, classPath: String = #file, functionName: String = #function, lineNumber: Int = #line) {
public static func warn(_ logMessage: Any, _ state:State? = nil, classPath: String = #file, functionName: String = #function, lineNumber: Int = #line) {
let fileName = URLUtil.getNameFromStringPath(classPath)
if logLevel.rawValue <= LogLevel.WARN.rawValue {
let log = OSLog(subsystem: SUBSYSTEM, category: "WARN ⚠️⚠️⚠️⚠️")
os_log("%@:%@:%d:: %@", log: log, fileName, functionName, lineNumber, "\(logMessage)")
os_log("%@:%@:%d:: %@", log: log, fileName, functionName, lineNumber, "\(state ?? "") \(logMessage)")
}
if logLevel.rawValue <= LogLevel.EXTERNAL_DEBUG.rawValue {
let log = OSLog(subsystem: SUBSYSTEM, category: "DEBUG")
os_log("%@:%@:%d:: %@", log: log, fileName, functionName, lineNumber, "\(logMessage)")
os_log("%@:%@:%d:: %@", log: log, fileName, functionName, lineNumber, "\(state ?? "") \(logMessage)")
}
}
@@ -136,11 +138,11 @@ class Log {
- Parameter functionName: automatically generated based on the function that called this function
- Parameter lineNumber: automatically generated based on the line that called this function
*/
public static func info(_ logMessage: Any, classPath: String = #file, functionName: String = #function, lineNumber: Int = #line) {
public static func info(_ logMessage: Any, _ state:State? = nil, classPath: String = #file, functionName: String = #function, lineNumber: Int = #line) {
let fileName = URLUtil.getNameFromStringPath(classPath)
if logLevel.rawValue <= LogLevel.INFO.rawValue {
let log = OSLog(subsystem: SUBSYSTEM, category: "INFO 🖤🖤🖤🖤")
os_log("%@:%@:%d:: %@", log: log, fileName, functionName, lineNumber, "\(logMessage)")
os_log("%@:%@:%d:: %@", log: log, fileName, functionName, lineNumber, "\(state ?? "") \(logMessage)")
}
}
@@ -157,11 +159,11 @@ class Log {
- Parameter functionName: automatically generated based on the function that called this function
- Parameter lineNumber: automatically generated based on the line that called this function
*/
public static func debug(_ logMessage: Any?..., classPath: String = #file, functionName: String = #function, lineNumber: Int = #line) {
public static func debug(_ logMessage: Any?..., state:State? = nil, classPath: String = #file, functionName: String = #function, lineNumber: Int = #line) {
let fileName = URLUtil.getNameFromStringPath(classPath)
if logLevel.rawValue <= LogLevel.DEBUG.rawValue {
let log = OSLog(subsystem: SUBSYSTEM, category: "DEBUG 🐝🐝🐝🐝")
os_log("%@:%@:%d:: %@", log: log, fileName, functionName, lineNumber, "\(logMessage)")
os_log("%@:%@:%d:: %@", log: log, fileName, functionName, lineNumber, "\(state ?? "") \(logMessage)")
}
}