diff --git a/BilibiliLive/Module/DLNA/BiliBiliUpnpDMR.swift b/BilibiliLive/Module/DLNA/BiliBiliUpnpDMR.swift index 05ebf1a..7e109d2 100644 --- a/BilibiliLive/Module/DLNA/BiliBiliUpnpDMR.swift +++ b/BilibiliLive/Module/DLNA/BiliBiliUpnpDMR.swift @@ -20,6 +20,7 @@ class BiliBiliUpnpDMR: NSObject { @MainActor private var sessions = Set() private var started = false private var ip: String? + private var boardcastTimer: Timer? private lazy var serverInfo: String = { let file = Bundle.main.url(forResource: "DLNAInfo", withExtension: "xml")! @@ -126,6 +127,8 @@ class BiliBiliUpnpDMR: NSObject { } func stop() { + boardcastTimer?.invalidate() + boardcastTimer = nil udp?.close() httpServer.stop() started = false @@ -161,6 +164,13 @@ class BiliBiliUpnpDMR: NSObject { Logger.warn("dmr start fail", err.localizedDescription) } } + boardcastTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { + [weak self] _ in + guard let self else { return } + if let data = getSSDPNotify().data(using: .utf8) { + udp.send(data, toHost: "239.255.255.250", port: 1900, withTimeout: 1, tag: 0) + } + } } private func getIPAddress() -> String? { @@ -212,6 +222,25 @@ class BiliBiliUpnpDMR: NSObject { """ } + private func getSSDPNotify() -> String { + guard let ip = ip ?? getIPAddress() else { + Logger.debug("no ip") + return "" + } + let text = """ + NOTIFY * HTTP/1.1 + Host: 239.255.255.250:1900 + Location: http://\(ip):9958/description.xml + Cache-Control: max-age=30 + Server: Linux/3.0.0, UPnP/1.0, Platinum/1.0.5.13 + NTS: ssdp:alive + USN: uuid:\(bUuid)::urn:schemas-upnp-org:device:MediaRenderer:1 + NT: urn:schemas-upnp-org:device:MediaRenderer:1 + + """ + return text + } + func handleEvent(frame: NVASession.NVAFrame, session: NVASession) { let topMost = UIViewController.topMostViewController() switch frame.action {