From 938b98d315983a9fc5db53f1d94f103fff981d56 Mon Sep 17 00:00:00 2001 From: shogo4405 Date: Mon, 24 Feb 2025 19:23:24 +0900 Subject: [PATCH] Support transtype option. --- .../Sources/SRT/SRTSocketOption.swift | 43 +++++++++++++++---- .../Tests/SRT/SRTSocketOptionTests.swift | 25 ++++++----- 2 files changed, 49 insertions(+), 19 deletions(-) diff --git a/SRTHaishinKit/Sources/SRT/SRTSocketOption.swift b/SRTHaishinKit/Sources/SRT/SRTSocketOption.swift index 261185e66..0a14b085a 100644 --- a/SRTHaishinKit/Sources/SRT/SRTSocketOption.swift +++ b/SRTHaishinKit/Sources/SRT/SRTSocketOption.swift @@ -2,13 +2,20 @@ import Foundation import libsrt enum SRTSocketOption: String, Sendable { - private static let boolStringLiterals: [String: Bool] = [ + private static let trueStringLiterals: [String: Bool] = [ "1": true, "on": true, "yes": true, "true": true ] + private static let falseStringLiterals: [String: Bool] = [ + "0": false, + "off": false, + "no": false, + "false": false + ] + static func from(uri: URL?) -> [SRTSocketOption: any Sendable] { guard let uri = uri else { return [:] @@ -28,9 +35,15 @@ enum SRTSocketOption: String, Sendable { options[option] = Int(item.value) case .bool: let key = String(describing: item.value).lowercased() - options[option] = boolStringLiterals[key] ?? false + if let bool = trueStringLiterals[key] { + options[option] = bool + } else if let bool = falseStringLiterals[key] { + options[option] = bool + } case .enumeration: - break + if let value = Transtype(rawValue: item.value.lowercased()) { + options[option] = value.cValue + } } } return options @@ -440,14 +453,28 @@ enum SRTSocketOption: String, Sendable { switch self { case .transtype: return [ - "live": Int32(SRTT_LIVE.rawValue), - "file": Int32(SRTT_FILE.rawValue) + "live": SRTT_LIVE, + "file": SRTT_FILE ] default: return nil } } + enum Transtype: String { + case live + case file + + var cValue: SRT_TRANSTYPE { + switch self { + case .live: + return SRTT_LIVE + case .file: + return SRTT_FILE + } + } + } + func setOption(_ socket: SRTSOCKET, value: any Sendable) throws { guard let data = data(value) else { throw SRTError.invalidOption(message: String(cString: srt_getlasterror_str())) @@ -500,12 +527,10 @@ enum SRTSocketOption: String, Sendable { case .enumeration: switch self { case .transtype: - guard - let key = value as? String, - var value = valmap?[key] as? Int32 else { + guard var value = value as? SRT_TRANSTYPE else { return nil } - return .init(bytes: &value, count: MemoryLayout.size(ofValue: value)) + return .init(bytes: &value.rawValue, count: MemoryLayout.size(ofValue: value.rawValue)) default: return nil } diff --git a/SRTHaishinKit/Tests/SRT/SRTSocketOptionTests.swift b/SRTHaishinKit/Tests/SRT/SRTSocketOptionTests.swift index 87af48deb..4128efe99 100644 --- a/SRTHaishinKit/Tests/SRT/SRTSocketOptionTests.swift +++ b/SRTHaishinKit/Tests/SRT/SRTSocketOptionTests.swift @@ -6,12 +6,13 @@ import libsrt @Suite struct SRTSocketOptionsTests { @Test func parseUri() async { - let url = URL(string: "srt://localhost:9000?passphrase=1234&streamid=5678&latency=1935&sndsyn=1") + let url = URL(string: "srt://localhost:9000?passphrase=1234&streamid=5678&latency=1935&sndsyn=1&transtype=file") let options = SRTSocketOption.from(uri: url) #expect(options[.passphrase] as! String == "1234") #expect(options[.streamid] as! String == "5678") #expect(options[.latency] as! Int32 == 1935) #expect(options[.sndsyn] as! Bool == true) + #expect(options[.transtype] as! SRT_TRANSTYPE == SRTT_FILE) } @Test func string() throws { @@ -19,6 +20,7 @@ import libsrt let string = "hello" try SRTSocketOption.streamid.setOption(socket, value: string) #expect(try SRTSocketOption.streamid.getOption(socket) == string.data(using: .ascii)) + srt_close(socket) } @Test func int32() throws { @@ -26,6 +28,7 @@ import libsrt var int32: Int32 = 100 try SRTSocketOption.latency.setOption(socket, value: int32) #expect(try SRTSocketOption.latency.getOption(socket) == Data(bytes: &int32, count: MemoryLayout.size)) + srt_close(socket) } @Test func int64() throws { @@ -33,6 +36,7 @@ import libsrt var int64: Int64 = 1000 try SRTSocketOption.inputbw.setOption(socket, value: int64) #expect(try SRTSocketOption.inputbw.getOption(socket) == Data(bytes: &int64, count: MemoryLayout.size)) + srt_close(socket) } @Test func bool() throws { @@ -40,18 +44,19 @@ import libsrt var bool = true try SRTSocketOption.tlpktdrop.setOption(socket, value: bool) #expect(try SRTSocketOption.tlpktdrop.getOption(socket) == Data(bytes: &bool, count: MemoryLayout.size)) + srt_close(socket) } @Test func transtype() throws { - /* - // ToDo - let socket = srt_create_socket() - let transtype = "live" - var result = SRTT_FILE.rawValue - try SRTSocketOption.transtype.setOption(socket, value: transtype) - print(try SRTSocketOption.transtype.getOption(socket).bytes) - #expect(try SRTSocketOption.transtype.getOption(socket) == Data(bytes: &result, count: MemoryLayout.size)) - */ + let socket = srt_create_socket() + var bool = true + // The default is true for Live mode, and false for File mode. + // It does not support transtype.getOption, so I will test it by observing changes in the surrounding properties. + #expect(try SRTSocketOption.nakreport.getOption(socket) == Data(bytes: &bool, count: MemoryLayout.size)) + try SRTSocketOption.transtype.setOption(socket, value: SRTT_FILE) + bool = false + #expect(try SRTSocketOption.nakreport.getOption(socket) == Data(bytes: &bool, count: MemoryLayout.size)) + srt_close(socket) } @Test func mode() throws {