Skip to content

Commit

Permalink
Provide audio and subtitles language codes in relevant metric payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
defagos committed Mar 6, 2025
1 parent 4ccd67f commit 2ce04e2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Sources/Monitoring/MetricsTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,12 @@ private extension MetricsTracker {
func errorData(from error: Error) -> MetricErrorData {
let error = error as NSError
return MetricErrorData(
audio: Self.languageCode(from: properties, for: .audible),
message: error.localizedDescription,
name: "\(error.domain)(\(error.code))",
position: Self.position(from: properties),
positionTimestamp: Self.positionTimestamp(from: properties),
subtitles: Self.languageCode(from: properties, for: .legible),
url: URL(string: properties?.metrics?.uri),
vpn: Self.isUsingVirtualPrivateNetwork()
)
Expand All @@ -161,6 +163,7 @@ private extension MetricsTracker {
let metrics = properties.metrics
return MetricStatusData(
airplay: properties.isExternalPlaybackActive,
audio: Self.languageCode(from: properties, for: .audible),
bandwidth: metrics?.observedBitrate,
bitrate: metrics?.indicatedBitrate,
bufferedDuration: Self.bufferedDuration(from: properties),
Expand All @@ -174,6 +177,7 @@ private extension MetricsTracker {
duration: stallDuration.toMilliseconds
),
streamType: Self.streamType(from: properties),
subtitles: Self.languageCode(from: properties, for: .legible),
url: metrics?.uri
)
}
Expand Down Expand Up @@ -320,6 +324,15 @@ private extension MetricsTracker {
properties.seekableTimeRange.duration.toMilliseconds
}

static func languageCode(from properties: TrackerProperties?, for characteristic: AVMediaCharacteristic) -> String? {
if case let .on(option) = properties?.currentMediaOption(for: characteristic) {
return languageCode(from: option)
}
else {
return nil
}
}

static func isUsingVirtualPrivateNetwork() -> Bool {
// Source: https://blog.tarkalabs.com/the-ultimate-vpn-detection-guide-for-ios-and-android-313b521186cb
guard let proxySettings = CFNetworkCopySystemProxySettings()?.takeRetainedValue() as? [String: Any],
Expand All @@ -330,4 +343,8 @@ private extension MetricsTracker {
key == "tap" || key == "ppp" || key.contains("tun") || key.contains("ipsec")
}
}

private static func languageCode(from option: AVMediaSelectionOption?) -> String? {
option?.locale?.language.languageCode?.identifier
}
}
2 changes: 2 additions & 0 deletions Sources/Monitoring/Types/MetricErrorData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import Foundation

struct MetricErrorData: Encodable {
let audio: String?
let message: String
let name: String
let position: Int?
let positionTimestamp: Int?
let subtitles: String?
let url: URL?
let vpn: Bool
}
2 changes: 2 additions & 0 deletions Sources/Monitoring/Types/MetricStatusData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

struct MetricStatusData: Encodable {
let airplay: Bool
let audio: String?
let bandwidth: Double?
let bitrate: Double?
let bufferedDuration: Int?
Expand All @@ -16,6 +17,7 @@ struct MetricStatusData: Encodable {
let positionTimestamp: Int?
let stall: Stall
let streamType: String?
let subtitles: String?
let url: String?
}

Expand Down

0 comments on commit 2ce04e2

Please sign in to comment.