From e9d9032d3034035b141feb33ffdb4cffc74eaa09 Mon Sep 17 00:00:00 2001 From: Dimitris C Date: Thu, 10 Sep 2020 19:01:35 +0300 Subject: [PATCH] Removes unneeded stuff --- AudioStreaming.xcodeproj/project.pbxproj | 8 --- .../Extensions/Stream+DispatchQueue.swift | 40 ------------ .../Core/Network/OutputStreamWriter.swift | 49 --------------- .../Streaming/AudioPlayer/AudioPlayer.swift | 8 --- .../AudioPlayerConfiguration.swift | 8 --- .../AudioPlayer/AudioRendererContext.swift | 6 -- .../AudioSource/AudioStreamSource.swift | 33 ---------- .../AudioSource/RemoteAudioSource.swift | 61 +------------------ 8 files changed, 2 insertions(+), 211 deletions(-) delete mode 100644 AudioStreaming/Core/Extensions/Stream+DispatchQueue.swift delete mode 100644 AudioStreaming/Core/Network/OutputStreamWriter.swift diff --git a/AudioStreaming.xcodeproj/project.pbxproj b/AudioStreaming.xcodeproj/project.pbxproj index b9aa290..51307ae 100644 --- a/AudioStreaming.xcodeproj/project.pbxproj +++ b/AudioStreaming.xcodeproj/project.pbxproj @@ -35,12 +35,10 @@ B5667B3E249BC43100D93F85 /* AudioPlayerRenderProcessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5667B3D249BC43000D93F85 /* AudioPlayerRenderProcessor.swift */; }; B59DF10424916FD50043C498 /* DispatchQueue+Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = B59DF10324916FD50043C498 /* DispatchQueue+Helpers.swift */; }; B59DF1A32493E90C0043C498 /* AudioFileStream+Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = B59DF1A22493E90C0043C498 /* AudioFileStream+Helpers.swift */; }; - B5A1254C24DD92C600862F64 /* OutputStreamWriter.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5A1254B24DD92C600862F64 /* OutputStreamWriter.swift */; }; B5AEDBB824744153007D8101 /* AudioStreaming.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B5AEDBAE24744153007D8101 /* AudioStreaming.framework */; }; B5AEDBBF24744153007D8101 /* AudioStreaming.h in Headers */ = {isa = PBXBuildFile; fileRef = B5AEDBB124744153007D8101 /* AudioStreaming.h */; settings = {ATTRIBUTES = (Public, ); }; }; B5B3B7CC248647ED00656828 /* AudioPlayerState.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5B3B7CB248647ED00656828 /* AudioPlayerState.swift */; }; B5B3B7D72486993B00656828 /* AudioStreamingMac.h in Headers */ = {isa = PBXBuildFile; fileRef = B5B3B7D52486993B00656828 /* AudioStreamingMac.h */; settings = {ATTRIBUTES = (Public, ); }; }; - B5E1DE2324B706A500955BFB /* Stream+DispatchQueue.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5E1DE2224B706A500955BFB /* Stream+DispatchQueue.swift */; }; B5E1DE2524B70B4200955BFB /* AudioPlayerConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5E1DE2424B70B4200955BFB /* AudioPlayerConfiguration.swift */; }; B5EF954E247DA5AC003E8FF8 /* NetworkingClientTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5EF954D247DA5AC003E8FF8 /* NetworkingClientTests.swift */; }; B5EF9555247E9393003E8FF8 /* AudioEntry.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5EF9554247E9393003E8FF8 /* AudioEntry.swift */; }; @@ -104,7 +102,6 @@ B5667B3D249BC43000D93F85 /* AudioPlayerRenderProcessor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AudioPlayerRenderProcessor.swift; sourceTree = ""; }; B59DF10324916FD50043C498 /* DispatchQueue+Helpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "DispatchQueue+Helpers.swift"; sourceTree = ""; }; B59DF1A22493E90C0043C498 /* AudioFileStream+Helpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AudioFileStream+Helpers.swift"; sourceTree = ""; }; - B5A1254B24DD92C600862F64 /* OutputStreamWriter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OutputStreamWriter.swift; sourceTree = ""; }; B5AEDBAE24744153007D8101 /* AudioStreaming.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = AudioStreaming.framework; sourceTree = BUILT_PRODUCTS_DIR; }; B5AEDBB124744153007D8101 /* AudioStreaming.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AudioStreaming.h; sourceTree = ""; }; B5AEDBB224744153007D8101 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -114,7 +111,6 @@ B5B3B7D32486993B00656828 /* AudioStreamingMac.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = AudioStreamingMac.framework; sourceTree = BUILT_PRODUCTS_DIR; }; B5B3B7D52486993B00656828 /* AudioStreamingMac.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AudioStreamingMac.h; sourceTree = ""; }; B5B3B7D62486993B00656828 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; - B5E1DE2224B706A500955BFB /* Stream+DispatchQueue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Stream+DispatchQueue.swift"; sourceTree = ""; }; B5E1DE2424B70B4200955BFB /* AudioPlayerConfiguration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AudioPlayerConfiguration.swift; sourceTree = ""; }; B5EF954D247DA5AC003E8FF8 /* NetworkingClientTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkingClientTests.swift; sourceTree = ""; }; B5EF9554247E9393003E8FF8 /* AudioEntry.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AudioEntry.swift; sourceTree = ""; }; @@ -159,7 +155,6 @@ B5276B73247D4D9F00D2F56A /* NetworkSessionDelegate.swift */, B5F883C22477DC4400D277C1 /* NetworkDataStream.swift */, B5276B71247D4D5B00D2F56A /* NetworkTasksMap.swift */, - B5A1254B24DD92C600862F64 /* OutputStreamWriter.swift */, ); path = Network; sourceTree = ""; @@ -189,7 +184,6 @@ children = ( B55CE97724813BCA0001C498 /* UnsafeMutablePointer+Helpers.swift */, B59DF10324916FD50043C498 /* DispatchQueue+Helpers.swift */, - B5E1DE2224B706A500955BFB /* Stream+DispatchQueue.swift */, B55F77D024D82CD50057F431 /* AVAudioUnit+Convenience.swift */, B51B9F9924DBE5BF00BDEAA2 /* AVAudioFormat+Convenience.swift */, ); @@ -504,7 +498,6 @@ buildActionMask = 2147483647; files = ( B5EF955D247ECBB1003E8FF8 /* RemoteAudioSource.swift in Sources */, - B5E1DE2324B706A500955BFB /* Stream+DispatchQueue.swift in Sources */, B59DF10424916FD50043C498 /* DispatchQueue+Helpers.swift in Sources */, B5B3B7CC248647ED00656828 /* AudioPlayerState.swift in Sources */, B51B9F9A24DBE5BF00BDEAA2 /* AVAudioFormat+Convenience.swift in Sources */, @@ -535,7 +528,6 @@ B5276B74247D4D9F00D2F56A /* NetworkSessionDelegate.swift in Sources */, B55F77D624DACE140057F431 /* BufferContext.swift in Sources */, B55CE97824813BCA0001C498 /* UnsafeMutablePointer+Helpers.swift in Sources */, - B5A1254C24DD92C600862F64 /* OutputStreamWriter.swift in Sources */, B5F883B62476DADB00D277C1 /* Protected.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/AudioStreaming/Core/Extensions/Stream+DispatchQueue.swift b/AudioStreaming/Core/Extensions/Stream+DispatchQueue.swift deleted file mode 100644 index eed690f..0000000 --- a/AudioStreaming/Core/Extensions/Stream+DispatchQueue.swift +++ /dev/null @@ -1,40 +0,0 @@ -// -// Created by Dimitrios Chatzieleftheriou on 09/07/2020. -// Copyright © 2020 Decimal. All rights reserved. -// - -import Foundation - -extension InputStream { - /// Sets the InputStream to the specified DispatchQueue - /// - /// - parameter queue: A `DispatchQueue` object the `InputStream` is attached to. - func set(on queue: DispatchQueue) { - CFReadStreamSetDispatchQueue(self, queue) - } - - /// Unsets the InputStream from the attached DispatchQueue - /// - /// This sets the `DispatchQueue` to `nil` - func unsetFromQueue() { - CFReadStreamSetDispatchQueue(self, nil) - } - -} - -extension OutputStream { - /// Sets the OutputStream to the specified DispatchQueue - /// - /// - parameter queue: A `DispatchQueue` object the `OutputStream` is attached to. - func set(on queue: DispatchQueue) { - CFWriteStreamSetDispatchQueue(self, queue) - } - - /// Unsets the OutputStream from the attached DispatchQueue - /// - /// This sets the `DispatchQueue` to `nil` - func unsetFromQueue() { - CFWriteStreamSetDispatchQueue(self, nil) - } - -} diff --git a/AudioStreaming/Core/Network/OutputStreamWriter.swift b/AudioStreaming/Core/Network/OutputStreamWriter.swift deleted file mode 100644 index 46becec..0000000 --- a/AudioStreaming/Core/Network/OutputStreamWriter.swift +++ /dev/null @@ -1,49 +0,0 @@ -// -// Created by Dimitrios Chatzieleftheriou on 07/08/2020. -// Copyright © 2020 Decimal. All rights reserved. -// - -/// An object that writes the received data from a network request to a specified `OutputStream` -final class OutputStreamWriter { - /// The accumulated data received from the URLSession - private var dataReceived = Data() - /// Keeps track of the total written bytes in the output stream - private var totalBytesWritten: Int = 0 - - /// Write the data on the `OutputStream` - /// - /// - parameter stream: An `OutputStream` for the data to be written in. - /// - parameter bufferSize: A `Int` value indicating the max buffer size to be written at a given time. - /// - /// - returns: An `Int` value indicating the accumulated written bytes. - func writeData(on stream: OutputStream, bufferSize: Int) -> Int { - guard !dataReceived.isEmpty else { return 0 } - - // gets the underlying byte buffer and writes to the stream - let sliceCount = min(bufferSize, dataReceived.count) - let slice = dataReceived[.. Int in - guard slice.count > 0 else { return 0 } - // "safe" to force unwrap here as we check if the count is not 0 - let base = buffer.baseAddress!.assumingMemoryBound(to: UInt8.self) - return stream.write(base, maxLength: buffer.count) - } - // check if `stream.write` returns an error and return - guard written > 0 else { return -1 } - - if dataReceived.count >= written { - dataReceived.removeSubrange(.. AudioPlayerConfiguration { let defaultValues = AudioPlayerConfiguration.default - let readBufferSize = self.readBufferSize == 0 ? defaultValues.readBufferSize : self.readBufferSize let bufferSizeInSeconds = self.bufferSizeInSeconds == 0 ? defaultValues.bufferSizeInSeconds : self.bufferSizeInSeconds let secondsRequiredToStartPlaying = self.secondsRequiredToStartPlaying == 0 ? defaultValues.secondsRequiredToStartPlaying : self.secondsRequiredToStartPlaying let gracePeriodAfterSeekInSeconds = self.gracePeriodAfterSeekInSeconds == 0 ? defaultValues.gracePeriodAfterSeekInSeconds : self.gracePeriodAfterSeekInSeconds @@ -70,7 +63,6 @@ public struct AudioPlayerConfiguration: Equatable { defaultValues.secondsRequiredToStartPlayingAfterBufferUnderun : self.secondsRequiredToStartPlayingAfterBufferUnderun return AudioPlayerConfiguration(flushQueueOnSeek: self.flushQueueOnSeek, - readBufferSize: readBufferSize, bufferSizeInSeconds: bufferSizeInSeconds, secondsRequiredToStartPlaying: secondsRequiredToStartPlaying, gracePeriodAfterSeekInSeconds: gracePeriodAfterSeekInSeconds, diff --git a/AudioStreaming/Streaming/AudioPlayer/AudioRendererContext.swift b/AudioStreaming/Streaming/AudioPlayer/AudioRendererContext.swift index eb37db5..283115d 100644 --- a/AudioStreaming/Streaming/AudioPlayer/AudioRendererContext.swift +++ b/AudioStreaming/Streaming/AudioPlayer/AudioRendererContext.swift @@ -18,9 +18,6 @@ final class AudioRendererContext { let lock = UnfairLock() - let readBufferSize: Int - let readBuffer: UnsafeMutablePointer - let bufferContext: BufferContext let seekRequest: SeekRequest @@ -41,8 +38,6 @@ final class AudioRendererContext { let configuration: AudioPlayerConfiguration init(configuration: AudioPlayerConfiguration, outputAudioFormat: AVAudioFormat) { self.configuration = configuration - self.readBufferSize = configuration.readBufferSize - self.readBuffer = UnsafeMutablePointer.uint8pointer(of: readBufferSize) self.seekRequest = SeekRequest() let canonicalStream = outputAudioFormat.basicStreamDescription @@ -64,7 +59,6 @@ final class AudioRendererContext { /// Deallocates buffer resources public func clean() { - readBuffer.deallocate() inOutAudioBufferList.deallocate() audioBuffer.mData?.deallocate() } diff --git a/AudioStreaming/Streaming/AudioSource/AudioStreamSource.swift b/AudioStreaming/Streaming/AudioSource/AudioStreamSource.swift index f88c14b..df43fd1 100644 --- a/AudioStreaming/Streaming/AudioSource/AudioStreamSource.swift +++ b/AudioStreaming/Streaming/AudioSource/AudioStreamSource.swift @@ -26,19 +26,6 @@ protocol CoreAudioStreamSource: class { /// An `AudioStreamSourceDelegate` object to listen for events from the source var delegate: AudioStreamSourceDelegate? { get set } - /// Reads up to a given number of bytes into a given buffer. - /// - parameter buffer: A mutable pointer of `UInt8` to hold the current buffer of stream - /// - parameter size: The maximum length for the buffer to read - /// - returns: As per `InputStream` documentation - /// - A positive number indicates the number of bytes read. - /// - 0 indicates that the end of the buffer was reached. - /// - -1 means that the operation failed; more information about the error can be obtained with streamError. - func read(into buffer: UnsafeMutablePointer, size: Int) -> Int - - func setup() - - func removeFromQueue() - /// Closes the underlying stream func close() @@ -51,30 +38,10 @@ protocol CoreAudioStreamSource: class { protocol AudioStreamSource: CoreAudioStreamSource { - var inputStream: InputStream? { get } - /// The `DispatchQueue` network object will receive data var sourceQueue: DispatchQueue { get } /// A `MetadataStreamSource` object that handles the metadata parsing var metadataStreamProccessor: MetadataStreamSource { get } - /// Returns `true` if the source has bytes available to be processed - var hasBytesAvailable: Bool { get } - - /// The status of the stream - var streamStatus: InputStream.Status { get } - -} - -extension AudioStreamSource { - var hasBytesAvailable: Bool { - guard let stream = inputStream else { return false } - return stream.hasBytesAvailable - } - - var streamStatus: InputStream.Status { - guard let stream = inputStream else { return .error } - return stream.streamStatus - } } diff --git a/AudioStreaming/Streaming/AudioSource/RemoteAudioSource.swift b/AudioStreaming/Streaming/AudioSource/RemoteAudioSource.swift index 1c526e4..dfc7c61 100644 --- a/AudioStreaming/Streaming/AudioSource/RemoteAudioSource.swift +++ b/AudioStreaming/Streaming/AudioSource/RemoteAudioSource.swift @@ -6,10 +6,7 @@ import Foundation import AudioToolbox -public class RemoteAudioSource: NSObject, AudioStreamSource { - - var inputStream: InputStream? - var readBufferSize: Int = 0 +public class RemoteAudioSource: AudioStreamSource { weak var delegate: AudioStreamSourceDelegate? @@ -50,7 +47,6 @@ public class RemoteAudioSource: NSObject, AudioStreamSource { metadataStreamSource: MetadataStreamSource, url: URL, sourceQueue: DispatchQueue, - readBufferSize: Int, httpHeaders: [String: String]) { self.networking = networking self.metadataStreamProccessor = metadataStreamSource @@ -60,13 +56,11 @@ public class RemoteAudioSource: NSObject, AudioStreamSource { self.httpStatusCode = 0 self.relativePosition = 0 self.seekOffset = 0 - self.readBufferSize = readBufferSize } convenience init(networking: NetworkingClient, url: URL, sourceQueue: DispatchQueue, - readBufferSize: Int, httpHeaders: [String: String]) { let metadataParser = MetadataParser() let metadataProccessor = MetadataStreamProcessor(parser: metadataParser.eraseToAnyParser()) @@ -74,38 +68,19 @@ public class RemoteAudioSource: NSObject, AudioStreamSource { metadataStreamSource: metadataProccessor, url: url, sourceQueue: sourceQueue, - readBufferSize: readBufferSize, httpHeaders: httpHeaders) } convenience init(networking: NetworkingClient, url: URL, - sourceQueue: DispatchQueue, - readBufferSize: Int) { + sourceQueue: DispatchQueue) { self.init(networking: networking, url: url, sourceQueue: sourceQueue, - readBufferSize: readBufferSize, httpHeaders: [:]) } - func setup() { - guard let stream = inputStream else { - return - } -// stream.delegate = self -// stream.set(on: sourceQueue) - } - - func removeFromQueue() { - guard let stream = inputStream else { return } - stream.delegate = nil - stream.unsetFromQueue() - } - func close() { - inputStream?.close() - inputStream = nil streamRequest?.cancel() if let streamTask = streamRequest { networking.remove(task: streamTask) @@ -114,8 +89,6 @@ public class RemoteAudioSource: NSObject, AudioStreamSource { } func seek(at offset: Int) { -// dispatchPrecondition(condition: .onQueue(sourceQueue)) - close() relativePosition = 0 @@ -129,31 +102,8 @@ public class RemoteAudioSource: NSObject, AudioStreamSource { performOpen(seek: offset) } - func read(into buffer: UnsafeMutablePointer, size: Int) -> Int { - performRead(into: buffer, size: size) - } - - // MARK: Private - private func performRead(into buffer: UnsafeMutablePointer, size: Int) -> Int { - guard size != 0 else { return 0 } - guard let stream = inputStream else { return 0 } - - var read: Int = 0 - // Metadata parsing - if metadataStreamProccessor.canProccessMetadata { - read = metadataStreamProccessor.proccessFromRead(into: buffer, size: size, using: stream) - } else { - read = stream.read(buffer, maxLength: size) - } - - guard read > 0 else { return read } - relativePosition += read - - return read - } - private func performOpen(seek seekOffset: Int) { let urlRequest = buildUrlRequest(with: url, seekIfNeeded: seekOffset) @@ -197,13 +147,6 @@ public class RemoteAudioSource: NSObject, AudioStreamSource { } } - private func performSoftSetup() { - guard let stream = inputStream else { - return - } - stream.set(on: sourceQueue) - } - @discardableResult private func parseResponseHeader(response: HTTPURLResponse?) -> Bool { guard let response = response else { return false }