Skip to content

Commit

Permalink
fix #102: 投屏解析直播流
Browse files Browse the repository at this point in the history
  • Loading branch information
yichengchen committed May 13, 2024
1 parent 37036f0 commit 37edb78
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
56 changes: 37 additions & 19 deletions BilibiliLive/Module/DLNA/BiliBiliUpnpDMR.swift
Original file line number Diff line number Diff line change
Expand Up @@ -221,21 +221,21 @@ class BiliBiliUpnpDMR: NSObject {
handlePlay(json: JSON(parseJSON: frame.body))
session.sendEmpty()
case "Pause":
(topMost as? VideoPlayerViewController)?.player?.pause()
(topMost as? CommonPlayerViewController)?.player?.pause()
session.sendEmpty()
case "Resume":
(topMost as? VideoPlayerViewController)?.player?.play()
(topMost as? CommonPlayerViewController)?.player?.play()
session.sendEmpty()
case "SwitchDanmaku":
let json = JSON(parseJSON: frame.body)
(topMost as? VideoPlayerViewController)?.danMuView.isHidden = !json["open"].boolValue
(topMost as? CommonPlayerViewController)?.danMuView.isHidden = !json["open"].boolValue
session.sendEmpty()
case "Seek":
let json = JSON(parseJSON: frame.body)
(topMost as? VideoPlayerViewController)?.player?.seek(to: CMTime(seconds: json["seekTs"].doubleValue, preferredTimescale: 1), toleranceBefore: .zero, toleranceAfter: .zero)
session.sendEmpty()
case "Stop":
(topMost as? VideoPlayerViewController)?.dismiss(animated: true)
(topMost as? CommonPlayerViewController)?.dismiss(animated: true)
session.sendEmpty()
case "PlayUrl":
let json = JSON(parseJSON: frame.body)
Expand All @@ -256,22 +256,11 @@ class BiliBiliUpnpDMR: NSObject {
}

func handlePlay(json: JSON) {
let topMost = UIViewController.topMostViewController()
let aid = json["aid"].intValue
let cid = json["cid"].intValue
let epid = json["epid"].intValue
let player: VideoDetailViewController
if epid > 0 {
player = VideoDetailViewController.create(epid: epid)
let roomId = json["roomId"].stringValue
if roomId.count > 0, let room = Int(roomId) {
playLive(roomID: room)
} else {
player = VideoDetailViewController.create(aid: aid, cid: cid)
}
if let _ = AppDelegate.shared.window!.rootViewController?.presentedViewController {
AppDelegate.shared.window!.rootViewController?.dismiss(animated: false) {
player.present(from: UIViewController.topMostViewController(), direatlyEnterVideo: true)
}
} else {
player.present(from: topMost, direatlyEnterVideo: true)
playVideo(json: json)
}
}

Expand Down Expand Up @@ -313,6 +302,35 @@ class BiliBiliUpnpDMR: NSObject {
}
}

extension BiliBiliUpnpDMR {
func playLive(roomID: Int) {
let player = LivePlayerViewController()
player.room = LiveRoom(title: "", room_id: roomID, uname: "", keyframe: nil, face: nil, cover_from_user: nil)
UIViewController.topMostViewController().present(player, animated: true)
}

func playVideo(json: JSON) {
let aid = json["aid"].intValue
let cid = json["cid"].intValue
let epid = json["epid"].intValue

let player: VideoDetailViewController
if epid > 0 {
player = VideoDetailViewController.create(epid: epid)
} else {
player = VideoDetailViewController.create(aid: aid, cid: cid)
}
let topMost = UIViewController.topMostViewController()
if let _ = AppDelegate.shared.window!.rootViewController?.presentedViewController {
AppDelegate.shared.window!.rootViewController?.dismiss(animated: false) {
player.present(from: UIViewController.topMostViewController(), direatlyEnterVideo: true)
}
} else {
player.present(from: topMost, direatlyEnterVideo: true)
}
}
}

extension BiliBiliUpnpDMR: GCDAsyncUdpSocketDelegate {
func udpSocket(_ sock: GCDAsyncUdpSocket, didReceive data: Data, fromAddress address: Data, withFilterContext filterContext: Any?) {
var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
Expand Down
2 changes: 1 addition & 1 deletion BilibiliLive/Module/Live/LiveViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ struct LiveRoom: DisplayData, Codable {
let title: String
let room_id: Int
let uname: String
let keyframe: URL
let keyframe: URL?
let face: URL?
let cover_from_user: URL?

Expand Down

0 comments on commit 37edb78

Please sign in to comment.