Skip to content

Commit

Permalink
Support transtype option.
Browse files Browse the repository at this point in the history
  • Loading branch information
shogo4405 committed Feb 24, 2025
1 parent 6f52a2c commit 938b98d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 19 deletions.
43 changes: 34 additions & 9 deletions SRTHaishinKit/Sources/SRT/SRTSocketOption.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 [:]
Expand All @@ -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
Expand Down Expand Up @@ -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()))
Expand Down Expand Up @@ -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
}
Expand Down
25 changes: 15 additions & 10 deletions SRTHaishinKit/Tests/SRT/SRTSocketOptionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,57 @@ 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 {
let socket = srt_create_socket()
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 {
let socket = srt_create_socket()
var int32: Int32 = 100
try SRTSocketOption.latency.setOption(socket, value: int32)
#expect(try SRTSocketOption.latency.getOption(socket) == Data(bytes: &int32, count: MemoryLayout<Int32>.size))
srt_close(socket)
}

@Test func int64() throws {
let socket = srt_create_socket()
var int64: Int64 = 1000
try SRTSocketOption.inputbw.setOption(socket, value: int64)
#expect(try SRTSocketOption.inputbw.getOption(socket) == Data(bytes: &int64, count: MemoryLayout<Int64>.size))
srt_close(socket)
}

@Test func bool() throws {
let socket = srt_create_socket()
var bool = true
try SRTSocketOption.tlpktdrop.setOption(socket, value: bool)
#expect(try SRTSocketOption.tlpktdrop.getOption(socket) == Data(bytes: &bool, count: MemoryLayout<Bool>.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<Int32>.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<Bool>.size))
try SRTSocketOption.transtype.setOption(socket, value: SRTT_FILE)
bool = false
#expect(try SRTSocketOption.nakreport.getOption(socket) == Data(bytes: &bool, count: MemoryLayout<Bool>.size))
srt_close(socket)
}

@Test func mode() throws {
Expand Down

0 comments on commit 938b98d

Please sign in to comment.