-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMPDParser.cs
More file actions
100 lines (90 loc) · 4.07 KB
/
MPDParser.cs
File metadata and controls
100 lines (90 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using UnityEngine;
using UnityEngine.Networking;
public class MPDParser
{
private List<Representation> representations = new List<Representation>();
public IEnumerator FetchMPD(string url)
{
Debug.Log("----->>>>> Fetching MPD from: " + url);
using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
{
yield return webRequest.SendWebRequest();
if (webRequest.result == UnityWebRequest.Result.ConnectionError || webRequest.result == UnityWebRequest.Result.ProtocolError)
{
Debug.LogError("----->>>>> MPD fetch error: " + webRequest.error);
yield break;
}
ParseMPD(webRequest.downloadHandler.text);
}
}
void ParseMPD(string xmlText)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlText);
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xmlDoc.NameTable);
namespaceManager.AddNamespace("mpd", "urn:mpeg:dash:schema:mpd:2011");
XmlNodeList xmlRepresentations = xmlDoc.SelectNodes("//mpd:AdaptationSet/mpd:Representation", namespaceManager);
foreach (XmlNode xmlNode in xmlRepresentations)
{
if (xmlNode is XmlElement repElement)
{
Representation rep = new Representation
{
Id = repElement.GetAttribute("id"),
Bandwidth = int.Parse(repElement.GetAttribute("bandwidth")),
Media = ((XmlElement)repElement.ParentNode.SelectSingleNode("mpd:SegmentTemplate", namespaceManager)).GetAttribute("media"),
Initialization = ((XmlElement)repElement.ParentNode.SelectSingleNode("mpd:SegmentTemplate", namespaceManager)).GetAttribute("initialization"),
Timescale = int.Parse(((XmlElement)repElement.ParentNode.SelectSingleNode("mpd:SegmentTemplate", namespaceManager)).GetAttribute("timescale")),
Duration = int.Parse(((XmlElement)repElement.ParentNode.SelectSingleNode("mpd:SegmentTemplate", namespaceManager)).GetAttribute("duration"))
};
representations.Add(rep);
}
}
}
public List<Representation> GetRepresentations()
{
return representations;
}
}
public class Representation
{
public string Id;
public int Bandwidth;
public string Media;
public string Initialization;
public int Timescale;
public int Duration;
}
// using System.Collections.Generic;
// using System.Xml;
// using UnityEngine;
// public class MPDParser
// {
// public List<Representation> ParseMPD(string xmlText)
// {
// List<Representation> representations = new List<Representation>();
// XmlDocument xmlDoc = new XmlDocument();
// xmlDoc.LoadXml(xmlText);
// XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xmlDoc.NameTable);
// namespaceManager.AddNamespace("mpd", "urn:mpeg:dash:schema:mpd:2011");
// XmlNodeList xmlRepresentations = xmlDoc.SelectNodes("//mpd:AdaptationSet/mpd:Representation", namespaceManager);
// foreach (XmlNode xmlNode in xmlRepresentations)
// {
// if (xmlNode is XmlElement repElement)
// {
// Representation rep = new Representation
// {
// Id = repElement.GetAttribute("id"),
// Bandwidth = int.Parse(repElement.GetAttribute("bandwidth")),
// Media = ((XmlElement)repElement.ParentNode.SelectSingleNode("mpd:SegmentTemplate", namespaceManager)).GetAttribute("media"),
// Timescale = int.Parse(((XmlElement)repElement.ParentNode.SelectSingleNode("mpd:SegmentTemplate", namespaceManager)).GetAttribute("timescale"))
// };
// representations.Add(rep);
// }
// }
// return representations;
// }
// }