Skip to content

Commit

Permalink
fix #94, boardcast ssdp notify to make bilibili oversea version upnp …
Browse files Browse the repository at this point in the history
…discovery work
  • Loading branch information
yichengchen committed Jun 10, 2024
1 parent 930a50b commit 29ccd20
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions BilibiliLive/Module/DLNA/BiliBiliUpnpDMR.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class BiliBiliUpnpDMR: NSObject {
@MainActor private var sessions = Set<NVASession>()
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")!
Expand Down Expand Up @@ -126,6 +127,8 @@ class BiliBiliUpnpDMR: NSObject {
}

func stop() {
boardcastTimer?.invalidate()
boardcastTimer = nil
udp?.close()
httpServer.stop()
started = false
Expand Down Expand Up @@ -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? {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 29ccd20

Please sign in to comment.