|
1 | 1 | import CoreMedia
|
2 | 2 | import Defaults
|
3 | 3 | import Foundation
|
| 4 | +import Logging |
4 | 5 | #if !os(macOS)
|
5 | 6 | import UIKit
|
6 | 7 | #endif
|
@@ -75,6 +76,10 @@ protocol PlayerBackend {
|
75 | 76 | }
|
76 | 77 |
|
77 | 78 | extension PlayerBackend {
|
| 79 | + var logger: Logger { |
| 80 | + return Logger(label: "stream.yattee.player.backend") |
| 81 | + } |
| 82 | + |
78 | 83 | func seek(to time: CMTime, seekType: SeekType, completionHandler: ((Bool) -> Void)? = nil) {
|
79 | 84 | model.seek.registerSeek(at: time, type: seekType, restore: currentTime)
|
80 | 85 | seek(to: time, seekType: seekType, completionHandler: completionHandler)
|
@@ -140,63 +145,95 @@ extension PlayerBackend {
|
140 | 145 | }
|
141 | 146 |
|
142 | 147 | func bestPlayable(_ streams: [Stream], maxResolution: ResolutionSetting, formatOrder: [QualityProfile.Format]) -> Stream? {
|
143 |
| - // filter out non-HLS streams and streams with resolution more than maxResolution |
| 148 | + logger.info("Starting bestPlayable function") |
| 149 | + logger.info("Total streams received: \(streams.count)") |
| 150 | + logger.info("Max resolution allowed: \(String(describing: maxResolution.value))") |
| 151 | + logger.info("Format order: \(formatOrder)") |
| 152 | + |
| 153 | + // Filter out non-HLS streams and streams with resolution more than maxResolution |
144 | 154 | let nonHLSStreams = streams.filter {
|
145 |
| - $0.kind != .hls && $0.resolution <= maxResolution.value |
| 155 | + let isHLS = $0.kind == .hls |
| 156 | + let isWithinResolution = $0.resolution <= maxResolution.value |
| 157 | + logger.info("Stream ID: \($0.id) - Kind: \(String(describing: $0.kind)) - Resolution: \(String(describing: $0.resolution)) - Bitrate: \($0.bitrate ?? 0)") |
| 158 | + logger.info("Is HLS: \(isHLS), Is within resolution: \(isWithinResolution)") |
| 159 | + return !isHLS && isWithinResolution |
146 | 160 | }
|
| 161 | + logger.info("Non-HLS streams after filtering: \(nonHLSStreams.count)") |
147 | 162 |
|
148 |
| - // find max resolution and bitrate from non-HLS streams |
| 163 | + // Find max resolution and bitrate from non-HLS streams |
149 | 164 | let bestResolutionStream = nonHLSStreams.max { $0.resolution < $1.resolution }
|
150 | 165 | let bestBitrateStream = nonHLSStreams.max { $0.bitrate ?? 0 < $1.bitrate ?? 0 }
|
151 | 166 |
|
| 167 | + logger.info("Best resolution stream: \(String(describing: bestResolutionStream?.id)) with resolution: \(String(describing: bestResolutionStream?.resolution))") |
| 168 | + logger.info("Best bitrate stream: \(String(describing: bestBitrateStream?.id)) with bitrate: \(String(describing: bestBitrateStream?.bitrate))") |
| 169 | + |
152 | 170 | let bestResolution = bestResolutionStream?.resolution ?? maxResolution.value
|
153 | 171 | let bestBitrate = bestBitrateStream?.bitrate ?? bestResolutionStream?.resolution.bitrate ?? maxResolution.value.bitrate
|
154 | 172 |
|
155 |
| - return streams.map { stream in |
| 173 | + logger.info("Final best resolution selected: \(String(describing: bestResolution))") |
| 174 | + logger.info("Final best bitrate selected: \(bestBitrate)") |
| 175 | + |
| 176 | + let adjustedStreams = streams.map { stream in |
156 | 177 | if stream.kind == .hls {
|
| 178 | + logger.info("Adjusting HLS stream ID: \(stream.id)") |
157 | 179 | stream.resolution = bestResolution
|
158 | 180 | stream.bitrate = bestBitrate
|
159 | 181 | stream.format = .hls
|
160 | 182 | } else if stream.kind == .stream {
|
| 183 | + logger.info("Adjusting non-HLS stream ID: \(stream.id)") |
161 | 184 | stream.format = .stream
|
162 | 185 | }
|
163 | 186 | return stream
|
164 | 187 | }
|
165 |
| - .filter { stream in |
166 |
| - stream.resolution <= maxResolution.value |
| 188 | + |
| 189 | + let filteredStreams = adjustedStreams.filter { stream in |
| 190 | + let isWithinResolution = stream.resolution <= maxResolution.value |
| 191 | + logger.info("Filtered stream ID: \(stream.id) - Is within max resolution: \(isWithinResolution)") |
| 192 | + return isWithinResolution |
167 | 193 | }
|
168 |
| - .max { lhs, rhs in |
| 194 | + |
| 195 | + logger.info("Filtered streams count after adjustments: \(filteredStreams.count)") |
| 196 | + |
| 197 | + let bestStream = filteredStreams.max { lhs, rhs in |
169 | 198 | if lhs.resolution == rhs.resolution {
|
170 | 199 | guard let lhsFormat = QualityProfile.Format(rawValue: lhs.format.rawValue),
|
171 | 200 | let rhsFormat = QualityProfile.Format(rawValue: rhs.format.rawValue)
|
172 | 201 | else {
|
173 |
| - print("Failed to extract lhsFormat or rhsFormat") |
| 202 | + logger.info("Failed to extract lhsFormat or rhsFormat for streams \(lhs.id) and \(rhs.id)") |
174 | 203 | return false
|
175 | 204 | }
|
176 | 205 |
|
177 | 206 | let lhsFormatIndex = formatOrder.firstIndex(of: lhsFormat) ?? Int.max
|
178 | 207 | let rhsFormatIndex = formatOrder.firstIndex(of: rhsFormat) ?? Int.max
|
179 | 208 |
|
| 209 | + logger.info("Comparing formats for streams \(lhs.id) and \(rhs.id) - LHS Format Index: \(lhsFormatIndex), RHS Format Index: \(rhsFormatIndex)") |
| 210 | + |
180 | 211 | return lhsFormatIndex > rhsFormatIndex
|
181 | 212 | }
|
182 | 213 |
|
| 214 | + logger.info("Comparing resolutions for streams \(lhs.id) and \(rhs.id) - LHS Resolution: \(String(describing: lhs.resolution)), RHS Resolution: \(String(describing: rhs.resolution))") |
| 215 | + |
183 | 216 | return lhs.resolution < rhs.resolution
|
184 | 217 | }
|
| 218 | + |
| 219 | + logger.info("Best stream selected: \(String(describing: bestStream?.id)) with resolution: \(String(describing: bestStream?.resolution)) and format: \(String(describing: bestStream?.format))") |
| 220 | + |
| 221 | + return bestStream |
185 | 222 | }
|
186 | 223 |
|
187 | 224 | func updateControls(completionHandler: (() -> Void)? = nil) {
|
188 |
| - print("updating controls") |
| 225 | + logger.info("updating controls") |
189 | 226 |
|
190 | 227 | guard model.presentingPlayer, !model.controls.presentingOverlays else {
|
191 |
| - print("ignored controls update") |
| 228 | + logger.info("ignored controls update") |
192 | 229 | completionHandler?()
|
193 | 230 | return
|
194 | 231 | }
|
195 | 232 |
|
196 | 233 | DispatchQueue.main.async(qos: .userInteractive) {
|
197 | 234 | #if !os(macOS)
|
198 | 235 | guard UIApplication.shared.applicationState != .background else {
|
199 |
| - print("not performing controls updates in background") |
| 236 | + logger.info("not performing controls updates in background") |
200 | 237 | completionHandler?()
|
201 | 238 | return
|
202 | 239 | }
|
|
0 commit comments