2023-01-31 21:13:39 +05:30
|
|
|
package com.github.libretube.helpers
|
2022-11-16 19:47:27 +05:30
|
|
|
|
|
|
|
import com.github.libretube.api.obj.PipedStream
|
|
|
|
import com.github.libretube.api.obj.Streams
|
2023-09-08 20:12:35 +05:30
|
|
|
import org.w3c.dom.Document
|
|
|
|
import org.w3c.dom.Element
|
2022-11-16 19:47:27 +05:30
|
|
|
import java.io.StringWriter
|
|
|
|
import javax.xml.parsers.DocumentBuilderFactory
|
|
|
|
import javax.xml.transform.TransformerFactory
|
|
|
|
import javax.xml.transform.dom.DOMSource
|
|
|
|
import javax.xml.transform.stream.StreamResult
|
|
|
|
|
|
|
|
// Based off of https://github.com/TeamPiped/Piped/blob/master/src/utils/DashUtils.js
|
|
|
|
|
2022-11-16 21:26:23 +05:30
|
|
|
object DashHelper {
|
2022-11-16 19:47:27 +05:30
|
|
|
|
2022-11-16 21:26:23 +05:30
|
|
|
private val builderFactory: DocumentBuilderFactory = DocumentBuilderFactory.newInstance()
|
|
|
|
private val transformerFactory: TransformerFactory = TransformerFactory.newInstance()
|
2022-11-16 19:47:27 +05:30
|
|
|
|
2022-11-16 21:26:23 +05:30
|
|
|
private data class AdapSetInfo(
|
|
|
|
val mimeType: String,
|
2023-07-16 20:41:27 +05:30
|
|
|
val formats: MutableList<PipedStream> = mutableListOf(),
|
2022-11-16 21:26:23 +05:30
|
|
|
val audioTrackId: String? = null,
|
2023-07-16 20:41:27 +05:30
|
|
|
val audioTrackType: String? = null,
|
|
|
|
val audioLocale: String? = null
|
2022-11-16 21:26:23 +05:30
|
|
|
)
|
2022-11-16 19:47:27 +05:30
|
|
|
|
2023-06-30 19:26:31 +05:30
|
|
|
fun createManifest(
|
|
|
|
streams: Streams,
|
|
|
|
supportsHdr: Boolean,
|
|
|
|
rewriteUrls: Boolean
|
|
|
|
): String {
|
2023-06-01 21:58:44 +05:30
|
|
|
val builder = builderFactory.newDocumentBuilder()
|
2022-11-16 19:47:27 +05:30
|
|
|
|
2022-11-16 21:26:23 +05:30
|
|
|
val doc = builder.newDocument()
|
|
|
|
val mpd = doc.createElement("MPD")
|
|
|
|
mpd.setAttribute("xmlns", "urn:mpeg:dash:schema:mpd:2011")
|
|
|
|
mpd.setAttribute("profiles", "urn:mpeg:dash:profile:full:2011")
|
|
|
|
mpd.setAttribute("minBufferTime", "PT1.5S")
|
|
|
|
mpd.setAttribute("type", "static")
|
|
|
|
mpd.setAttribute("mediaPresentationDuration", "PT${streams.duration}S")
|
2022-11-16 19:47:27 +05:30
|
|
|
|
2022-11-16 21:26:23 +05:30
|
|
|
val period = doc.createElement("Period")
|
2022-11-16 19:47:27 +05:30
|
|
|
|
2022-11-16 21:26:23 +05:30
|
|
|
val adapSetInfos = ArrayList<AdapSetInfo>()
|
2022-11-16 19:47:27 +05:30
|
|
|
|
2023-09-08 20:12:35 +05:30
|
|
|
for (
|
|
|
|
stream in streams.videoStreams
|
|
|
|
// used to avoid including LBRY HLS inside the streams in the manifest
|
|
|
|
.filter { !it.format.orEmpty().contains("HLS") }
|
|
|
|
.filter { supportsHdr || !it.quality.orEmpty().uppercase().contains("HDR") }
|
|
|
|
) {
|
|
|
|
// ignore dual format and OTF streams
|
|
|
|
if (!stream.videoOnly!! || stream.indexEnd!! <= 0) {
|
|
|
|
continue
|
|
|
|
}
|
2022-11-18 23:41:28 +05:30
|
|
|
|
2023-09-08 20:12:35 +05:30
|
|
|
val adapSetInfo = adapSetInfos.find { it.mimeType == stream.mimeType }
|
|
|
|
if (adapSetInfo != null) {
|
|
|
|
adapSetInfo.formats.add(stream)
|
|
|
|
continue
|
2022-11-16 21:26:23 +05:30
|
|
|
}
|
2023-09-08 20:12:35 +05:30
|
|
|
adapSetInfos.add(
|
|
|
|
AdapSetInfo(
|
|
|
|
stream.mimeType!!,
|
|
|
|
mutableListOf(stream)
|
|
|
|
)
|
|
|
|
)
|
2022-11-16 21:26:23 +05:30
|
|
|
}
|
|
|
|
|
2023-02-08 14:11:59 +05:30
|
|
|
for (stream in streams.audioStreams) {
|
2022-11-16 21:26:23 +05:30
|
|
|
val adapSetInfo =
|
2022-12-19 21:28:34 +05:30
|
|
|
adapSetInfos.find {
|
|
|
|
it.mimeType == stream.mimeType && it.audioTrackId == stream.audioTrackId
|
|
|
|
}
|
2022-11-16 21:26:23 +05:30
|
|
|
if (adapSetInfo != null) {
|
|
|
|
adapSetInfo.formats.add(stream)
|
|
|
|
continue
|
2022-11-16 19:47:27 +05:30
|
|
|
}
|
2022-11-16 21:26:23 +05:30
|
|
|
adapSetInfos.add(
|
|
|
|
AdapSetInfo(
|
|
|
|
stream.mimeType!!,
|
2023-07-16 20:41:27 +05:30
|
|
|
mutableListOf(stream),
|
2022-11-16 23:58:29 +05:30
|
|
|
stream.audioTrackId,
|
2023-07-16 20:41:27 +05:30
|
|
|
stream.audioTrackType,
|
|
|
|
stream.audioTrackLocale
|
2023-06-24 23:27:00 +05:30
|
|
|
)
|
2022-11-16 21:26:23 +05:30
|
|
|
)
|
|
|
|
}
|
2022-11-16 19:47:27 +05:30
|
|
|
|
2022-11-16 21:26:23 +05:30
|
|
|
for (adapSet in adapSetInfos) {
|
|
|
|
val adapSetElement = doc.createElement("AdaptationSet")
|
|
|
|
adapSetElement.setAttribute("mimeType", adapSet.mimeType)
|
|
|
|
adapSetElement.setAttribute("startWithSAP", "1")
|
|
|
|
adapSetElement.setAttribute("subsegmentAlignment", "true")
|
2023-07-16 20:41:27 +05:30
|
|
|
|
2022-11-16 21:26:23 +05:30
|
|
|
if (adapSet.audioTrackId != null) {
|
|
|
|
adapSetElement.setAttribute("lang", adapSet.audioTrackId.substring(0, 2))
|
2023-07-16 20:41:27 +05:30
|
|
|
} else if (adapSet.audioLocale != null) {
|
|
|
|
adapSetElement.setAttribute("lang", adapSet.audioLocale)
|
2022-11-16 21:26:23 +05:30
|
|
|
}
|
2022-11-16 19:47:27 +05:30
|
|
|
|
2023-07-16 21:03:37 +05:30
|
|
|
// Only add the Role element if there is a track type set
|
|
|
|
// This allows distinction between formats marked as original on YouTube and
|
|
|
|
// formats without track type info set
|
|
|
|
if (adapSet.audioTrackType != null) {
|
|
|
|
val roleElement = doc.createElement("Role")
|
|
|
|
roleElement.setAttribute("schemeIdUri", "urn:mpeg:dash:role:2011")
|
|
|
|
roleElement.setAttribute(
|
|
|
|
"value",
|
|
|
|
getRoleValueFromAudioTrackType(adapSet.audioTrackType)
|
|
|
|
)
|
|
|
|
adapSetElement.appendChild(roleElement)
|
|
|
|
}
|
2023-07-16 20:41:27 +05:30
|
|
|
|
2022-11-16 21:26:23 +05:30
|
|
|
val isVideo = adapSet.mimeType.contains("video")
|
2022-11-16 19:47:27 +05:30
|
|
|
|
2022-11-16 21:26:23 +05:30
|
|
|
if (isVideo) {
|
|
|
|
adapSetElement.setAttribute("scanType", "progressive")
|
|
|
|
}
|
2022-11-16 19:47:27 +05:30
|
|
|
|
2022-11-16 21:26:23 +05:30
|
|
|
for (stream in adapSet.formats) {
|
|
|
|
val rep = let {
|
|
|
|
if (isVideo) {
|
2023-06-19 23:41:12 +05:30
|
|
|
createVideoRepresentation(doc, stream, rewriteUrls)
|
2022-11-16 21:26:23 +05:30
|
|
|
} else {
|
2023-06-19 23:41:12 +05:30
|
|
|
createAudioRepresentation(doc, stream, rewriteUrls)
|
2022-11-16 19:47:27 +05:30
|
|
|
}
|
|
|
|
}
|
2022-11-16 21:26:23 +05:30
|
|
|
adapSetElement.appendChild(rep)
|
2022-11-16 19:47:27 +05:30
|
|
|
}
|
|
|
|
|
2022-11-16 21:26:23 +05:30
|
|
|
period.appendChild(adapSetElement)
|
|
|
|
}
|
2022-11-16 19:47:27 +05:30
|
|
|
|
2022-11-16 21:26:23 +05:30
|
|
|
mpd.appendChild(period)
|
2022-11-16 19:47:27 +05:30
|
|
|
|
2022-11-16 21:26:23 +05:30
|
|
|
doc.appendChild(mpd)
|
2022-11-16 19:47:27 +05:30
|
|
|
|
2022-11-16 21:26:23 +05:30
|
|
|
val domSource = DOMSource(doc)
|
|
|
|
val writer = StringWriter()
|
2022-11-16 19:47:27 +05:30
|
|
|
|
2022-11-16 21:26:23 +05:30
|
|
|
val transformer = transformerFactory.newTransformer()
|
|
|
|
transformer.transform(domSource, StreamResult(writer))
|
2022-11-16 19:47:27 +05:30
|
|
|
|
2022-11-16 21:26:23 +05:30
|
|
|
return writer.toString()
|
|
|
|
}
|
2022-11-16 19:47:27 +05:30
|
|
|
|
2023-06-30 19:26:31 +05:30
|
|
|
private fun createAudioRepresentation(
|
|
|
|
doc: Document,
|
|
|
|
stream: PipedStream,
|
|
|
|
rewriteUrls: Boolean
|
|
|
|
): Element {
|
2022-11-16 21:26:23 +05:30
|
|
|
val representation = doc.createElement("Representation")
|
|
|
|
representation.setAttribute("bandwidth", stream.bitrate.toString())
|
|
|
|
representation.setAttribute("codecs", stream.codec!!)
|
|
|
|
representation.setAttribute("mimeType", stream.mimeType!!)
|
2022-11-16 19:47:27 +05:30
|
|
|
|
2022-11-16 21:26:23 +05:30
|
|
|
val audioChannelConfiguration = doc.createElement("AudioChannelConfiguration")
|
|
|
|
audioChannelConfiguration.setAttribute(
|
|
|
|
"schemeIdUri",
|
2023-06-24 23:27:00 +05:30
|
|
|
"urn:mpeg:dash:23003:3:audio_channel_configuration:2011"
|
2022-11-16 21:26:23 +05:30
|
|
|
)
|
|
|
|
audioChannelConfiguration.setAttribute("value", "2")
|
2022-11-16 19:47:27 +05:30
|
|
|
|
2022-11-16 21:26:23 +05:30
|
|
|
val baseUrl = doc.createElement("BaseURL")
|
2023-06-19 23:41:12 +05:30
|
|
|
baseUrl.appendChild(doc.createTextNode(ProxyHelper.unwrapUrl(stream.url!!, rewriteUrls)))
|
2022-11-16 19:47:27 +05:30
|
|
|
|
2023-07-16 20:41:27 +05:30
|
|
|
representation.appendChild(audioChannelConfiguration)
|
|
|
|
representation.appendChild(baseUrl)
|
|
|
|
representation.appendChild(createSegmentBaseElement(doc, stream))
|
|
|
|
|
|
|
|
return representation
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun createSegmentBaseElement(
|
|
|
|
document: Document,
|
|
|
|
stream: PipedStream
|
|
|
|
): Element {
|
|
|
|
val segmentBase = document.createElement("SegmentBase")
|
2022-11-16 21:26:23 +05:30
|
|
|
segmentBase.setAttribute("indexRange", "${stream.indexStart}-${stream.indexEnd}")
|
2022-11-16 19:47:27 +05:30
|
|
|
|
2023-07-16 20:41:27 +05:30
|
|
|
val initialization = document.createElement("Initialization")
|
2022-11-16 21:26:23 +05:30
|
|
|
initialization.setAttribute("range", "${stream.initStart}-${stream.initEnd}")
|
|
|
|
segmentBase.appendChild(initialization)
|
2022-11-16 19:47:27 +05:30
|
|
|
|
2023-07-16 20:41:27 +05:30
|
|
|
return segmentBase
|
|
|
|
}
|
2022-11-16 19:47:27 +05:30
|
|
|
|
2023-07-16 21:03:37 +05:30
|
|
|
private fun getRoleValueFromAudioTrackType(audioTrackType: String): String {
|
2023-07-16 20:41:27 +05:30
|
|
|
return when (audioTrackType.lowercase()) {
|
|
|
|
"descriptive" -> "description"
|
|
|
|
"dubbed" -> "dub"
|
|
|
|
"original" -> "main"
|
|
|
|
else -> "alternate"
|
|
|
|
}
|
2022-11-16 21:26:23 +05:30
|
|
|
}
|
2022-11-16 19:47:27 +05:30
|
|
|
|
2023-06-30 19:26:31 +05:30
|
|
|
private fun createVideoRepresentation(
|
|
|
|
doc: Document,
|
|
|
|
stream: PipedStream,
|
|
|
|
rewriteUrls: Boolean
|
|
|
|
): Element {
|
2022-11-16 21:26:23 +05:30
|
|
|
val representation = doc.createElement("Representation")
|
|
|
|
representation.setAttribute("codecs", stream.codec!!)
|
|
|
|
representation.setAttribute("bandwidth", stream.bitrate.toString())
|
|
|
|
representation.setAttribute("width", stream.width.toString())
|
|
|
|
representation.setAttribute("height", stream.height.toString())
|
|
|
|
representation.setAttribute("maxPlayoutRate", "1")
|
|
|
|
representation.setAttribute("frameRate", stream.fps.toString())
|
2022-11-16 19:47:27 +05:30
|
|
|
|
2022-11-16 21:26:23 +05:30
|
|
|
val baseUrl = doc.createElement("BaseURL")
|
2023-06-19 23:41:12 +05:30
|
|
|
baseUrl.appendChild(doc.createTextNode(ProxyHelper.unwrapUrl(stream.url!!, rewriteUrls)))
|
2022-11-16 19:47:27 +05:30
|
|
|
|
2022-11-16 21:26:23 +05:30
|
|
|
val segmentBase = doc.createElement("SegmentBase")
|
|
|
|
segmentBase.setAttribute("indexRange", "${stream.indexStart}-${stream.indexEnd}")
|
2022-11-16 19:47:27 +05:30
|
|
|
|
2022-11-16 21:26:23 +05:30
|
|
|
val initialization = doc.createElement("Initialization")
|
|
|
|
initialization.setAttribute("range", "${stream.initStart}-${stream.initEnd}")
|
|
|
|
segmentBase.appendChild(initialization)
|
2022-11-16 19:47:27 +05:30
|
|
|
|
2022-11-16 21:26:23 +05:30
|
|
|
representation.appendChild(baseUrl)
|
|
|
|
representation.appendChild(segmentBase)
|
|
|
|
|
|
|
|
return representation
|
2022-11-16 19:47:27 +05:30
|
|
|
}
|
|
|
|
}
|