diff --git a/BilibiliLive.xcodeproj/project.pbxproj b/BilibiliLive.xcodeproj/project.pbxproj index 3e8ab83a..f8bb0a42 100644 --- a/BilibiliLive.xcodeproj/project.pbxproj +++ b/BilibiliLive.xcodeproj/project.pbxproj @@ -33,6 +33,7 @@ 496400D32943431E0098ACA6 /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 496400D22943431E0098ACA6 /* Logger.swift */; }; 4973502B29161B770045C26B /* WeeklyWatchViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4973502A29161B770045C26B /* WeeklyWatchViewController.swift */; }; 4973502D29162A6D0045C26B /* StandardVideoCollectionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4973502C29162A6D0045C26B /* StandardVideoCollectionViewController.swift */; }; + 497361082BF1A16600ED213F /* Keys.swift in Sources */ = {isa = PBXBuildFile; fileRef = 497361072BF1A16600ED213F /* Keys.swift */; }; 498CF2902B63AABE0009793E /* dictionary_hash.c in Sources */ = {isa = PBXBuildFile; fileRef = 498CF2422B63AABE0009793E /* dictionary_hash.c */; }; 498CF2912B63AABE0009793E /* backward_references_hq.c in Sources */ = {isa = PBXBuildFile; fileRef = 498CF2432B63AABE0009793E /* backward_references_hq.c */; }; 498CF2922B63AABE0009793E /* histogram.c in Sources */ = {isa = PBXBuildFile; fileRef = 498CF2462B63AABE0009793E /* histogram.c */; }; @@ -157,6 +158,7 @@ 496400D22943431E0098ACA6 /* Logger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logger.swift; sourceTree = ""; }; 4973502A29161B770045C26B /* WeeklyWatchViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WeeklyWatchViewController.swift; sourceTree = ""; }; 4973502C29162A6D0045C26B /* StandardVideoCollectionViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StandardVideoCollectionViewController.swift; sourceTree = ""; }; + 497361072BF1A16600ED213F /* Keys.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Keys.swift; sourceTree = ""; }; 498CF2352B63AABE0009793E /* NSData+BrotliCompression.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSData+BrotliCompression.h"; sourceTree = ""; }; 498CF2362B63AABE0009793E /* LMBrotliCompression.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LMBrotliCompression.h; sourceTree = ""; }; 498CF2372B63AABE0009793E /* BrotliKit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BrotliKit.h; sourceTree = ""; }; @@ -657,6 +659,7 @@ F927ED6A2610391000EAB8E3 /* Vendor */, 2D3CD3F72635BD7000191419 /* Supporting Files */, 496400D22943431E0098ACA6 /* Logger.swift */, + 497361072BF1A16600ED213F /* Keys.swift */, ); path = BilibiliLive; sourceTree = ""; @@ -796,6 +799,7 @@ 4973502B29161B770045C26B /* WeeklyWatchViewController.swift in Sources */, AE7A3B20290298BE006FEBB0 /* Colors.swift in Sources */, 49FB8EC0291F4C520045D5DE /* VMaskProvider.swift in Sources */, + 497361082BF1A16600ED213F /* Keys.swift in Sources */, 49E5F85028AF73C500FAA3CE /* BilibiliVideoResourceLoaderDelegate.swift in Sources */, 498CF2A22B63AABE0009793E /* dictionary.c in Sources */, 494741C82902C45D005D6885 /* Array+..swift in Sources */, diff --git a/BilibiliLive/Component/Player/CommonPlayerViewController.swift b/BilibiliLive/Component/Player/CommonPlayerViewController.swift index 2d3e7758..fd19da2e 100644 --- a/BilibiliLive/Component/Player/CommonPlayerViewController.swift +++ b/BilibiliLive/Component/Player/CommonPlayerViewController.swift @@ -286,9 +286,16 @@ class CommonPlayerViewController: AVPlayerViewController { bit in String(format: "%.2fMbps", bit / 1024.0 / 1024.0) } + guard let player else { return "Player no init" } - guard let log = player?.currentItem?.accessLog() else { return "no log" } - guard let item = log.events.last else { return "no event log" } + var logs = """ + \(additionDebugInfo()) + time control status: \(player.timeControlStatus.rawValue) \(player.reasonForWaitingToPlay?.rawValue ?? "") + player status:\(player.status.rawValue) + """ + + guard let log = player.currentItem?.accessLog() else { return logs } + guard let item = log.events.last else { return logs } let uri = item.uri ?? "" let addr = item.serverAddress ?? "" let changes = item.numberOfServerAddressChanges @@ -298,14 +305,15 @@ class CommonPlayerViewController: AVPlayerViewController { let averageVideoBitrate = item.averageVideoBitrate let indicatedBitrate = item.indicatedBitrate let observedBitrate = item.observedBitrate - return """ + logs += """ uri:\(uri), ip:\(addr), change:\(changes) drop:\(dropped) stalls:\(stalls) bitrate audio:\(bitrateStr(averageAudioBitrate)), video: \(bitrateStr(averageVideoBitrate)) observedBitrate:\(bitrateStr(observedBitrate)) indicatedAverageBitrate:\(bitrateStr(indicatedBitrate)) - maskProvider: \(String(describing: maskProvider)) \(additionDebugInfo()) + maskProvider: \(String(describing: maskProvider))) """ + return logs } func additionDebugInfo() -> String { return "" } diff --git a/BilibiliLive/Keys.swift b/BilibiliLive/Keys.swift new file mode 100644 index 00000000..197799ac --- /dev/null +++ b/BilibiliLive/Keys.swift @@ -0,0 +1,13 @@ +// +// Keys.swift +// BilibiliLive +// +// Created by yicheng on 2024/5/13. +// + +import Foundation +enum Keys { + static let userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2 Safari/605.1.15" + static let liveReferer = "https://live.bilibili.com" + static let referer = "https://www.bilibili.com" +} diff --git a/BilibiliLive/Module/Live/LiveDanMuProvider.swift b/BilibiliLive/Module/Live/LiveDanMuProvider.swift index e19eb315..6d5474fd 100644 --- a/BilibiliLive/Module/Live/LiveDanMuProvider.swift +++ b/BilibiliLive/Module/Live/LiveDanMuProvider.swift @@ -35,8 +35,8 @@ class LiveDanMuProvider { Logger.info("Get room server info \(server.host):\(server.wss_port)") token = info.token var request = URLRequest(url: URL(string: "wss://\(server.host):\(server.wss_port)/sub")!) - request.allHTTPHeaderFields = ["User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36", - "Referer": "https://live.bilibili.com"] + request.allHTTPHeaderFields = ["User-Agent": Keys.userAgent, + "Referer": Keys.userAgent] websocket = WebSocket(request: request) websocket?.delegate = self websocket?.connect() diff --git a/BilibiliLive/Module/Live/LivePlayerViewController.swift b/BilibiliLive/Module/Live/LivePlayerViewController.swift index 8ff7f811..9aab05e3 100644 --- a/BilibiliLive/Module/Live/LivePlayerViewController.swift +++ b/BilibiliLive/Module/Live/LivePlayerViewController.swift @@ -77,12 +77,13 @@ class LivePlayerViewController: CommonPlayerViewController { danMuView.play() let headers: [String: String] = [ - "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36", - "Referer": "https://live.bilibili.com", + "User-Agent": Keys.userAgent, + "Referer": Keys.liveReferer, ] let asset = AVURLAsset(url: URL(string: url)!, options: ["AVURLAssetHTTPHeaderFieldsKey": headers]) playerItem = AVPlayerItem(asset: asset) player = AVPlayer(playerItem: playerItem) + player?.automaticallyWaitsToMinimizeStalling = false } else { showErrorAlertAndExit(title: "url is nil", message: "url: \(playInfo.first?.url.count ?? 0)") } @@ -137,7 +138,7 @@ class LivePlayerViewController: CommonPlayerViewController { } override func additionDebugInfo() -> String { - return "\n\(playInfo.first?.formate ?? "") \(playInfo.first?.current_qn ?? 0) failed: \(failCount)" + return "\(playInfo.first?.formate ?? "") \(playInfo.first?.current_qn ?? 0) failed: \(failCount)" } struct PlayInfo { diff --git a/BilibiliLive/Request/WebRequest.swift b/BilibiliLive/Request/WebRequest.swift index 64156988..97916da0 100644 --- a/BilibiliLive/Request/WebRequest.swift +++ b/BilibiliLive/Request/WebRequest.swift @@ -69,11 +69,11 @@ enum WebRequest { } if !afheaders.contains(where: { $0.name == "User-Agent" }) { - afheaders.add(.userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.2 Safari/605.1.15")) + afheaders.add(.userAgent(Keys.userAgent)) } if !afheaders.contains(where: { $0.name == "Referer" }) { - afheaders.add(HTTPHeader(name: "Referer", value: "https://www.bilibili.com")) + afheaders.add(HTTPHeader(name: "Referer", value: Keys.referer)) } var session = Session.default