-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Livestream Latency Metrics (#15)
* Add some methods that parse tags * Add livestream metrics * Add example
- Loading branch information
1 parent
741da4d
commit 8e19a65
Showing
6 changed files
with
114 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
library-exo/src/main/java/com/mux/stats/sdk/muxstats/internal/HlsUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.mux.stats.sdk.muxstats.internal | ||
|
||
import androidx.annotation.OptIn | ||
import androidx.media3.common.Timeline | ||
import androidx.media3.common.Timeline.Window | ||
import androidx.media3.common.util.UnstableApi | ||
import androidx.media3.exoplayer.hls.HlsManifest | ||
import com.mux.stats.sdk.core.util.MuxLogger | ||
import com.mux.stats.sdk.muxstats.MuxStateCollector | ||
|
||
/* | ||
* HlsUtils.kt: Utility functions for working with HLS playlists in exoplayer | ||
*/ | ||
|
||
// lazily-cached check for the HLS extension, which may not be available at runtime | ||
@OptIn(UnstableApi::class) // opting-in to HlsManifest | ||
private val hlsExtensionAvailable: Boolean by lazy { | ||
try { | ||
Class.forName(HlsManifest::class.java.canonicalName!!) | ||
true | ||
} catch (e: ClassNotFoundException) { | ||
MuxLogger.w("isHlsExtensionAvailable", "HLS extension not found. Some features may not work") | ||
false | ||
} | ||
} | ||
|
||
/** | ||
* True when Exoplayer's HLS extension is available at runtime | ||
*/ | ||
@JvmSynthetic | ||
internal fun isHlsExtensionAvailable() = hlsExtensionAvailable | ||
|
||
/** | ||
* Add livestream data to a [MuxStateCollector] if the given [Window] represents a live stream | ||
*/ | ||
@JvmSynthetic | ||
internal fun MuxStateCollector.populateLiveStreamData(window: Window) { | ||
if (window.isLive()) { | ||
hlsManifestNewestTime = window.windowStartTimeMs | ||
hlsHoldBack = parseManifestTagL(window, "HOLD-BACK") | ||
hlsPartHoldBack = parseManifestTagL(window, "PART-HOLD-BACK") | ||
hlsPartTargetDuration = parseManifestTagL(window, "PART-TARGET") | ||
hlsTargetDuration = parseManifestTagL(window, "EXT-X-TARGETDURATION") | ||
} | ||
} | ||
|
||
/** | ||
* Parses manifest tags representing a named numerical value, returning the value as a Long | ||
*/ | ||
@JvmSynthetic | ||
internal fun parseManifestTagL(currentWindow: Window, tagName: String): Long { | ||
var value: String = parseManifestTag(currentWindow, tagName) | ||
value = value.replace(".", "") | ||
try { | ||
return value.toLong() | ||
} catch (e: NumberFormatException) { | ||
MuxLogger.exception(e, "Manifest Parsing", "Bad number format for value: $value") | ||
} | ||
return -1L | ||
} | ||
|
||
/** | ||
* Parses manifest tags representing a named numerical value, returning the value as a string | ||
*/ | ||
@OptIn(UnstableApi::class) | ||
@JvmSynthetic | ||
internal fun parseManifestTag(currentWindow: Timeline.Window, tagName: String): String { | ||
if (!isHlsExtensionAvailable()) { | ||
return "-1" | ||
} | ||
|
||
if (currentWindow.manifest != null && tagName.isNotEmpty()) { | ||
if (currentWindow.manifest is HlsManifest) { | ||
val manifest = currentWindow.manifest as HlsManifest | ||
for (tag in manifest.mediaPlaylist.tags) { | ||
if (tag.contains(tagName)) { | ||
var value = tag.split(tagName).toTypedArray()[1] | ||
if (value.contains(",")) { | ||
value = value.split(",").toTypedArray()[0] | ||
} | ||
if (value.startsWith("=") || value.startsWith(":")) { | ||
value = value.substring(1, value.length) | ||
} | ||
return value | ||
} | ||
} | ||
} | ||
} | ||
return "-1" | ||
} |
21 changes: 0 additions & 21 deletions
21
library-exo/src/main/java/com/mux/stats/sdk/muxstats/internal/Utils.kt
This file was deleted.
Oops, something went wrong.