HLSStream.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using UnityEngine;
  6. namespace RenderHeads.Media.AVProVideo
  7. {
  8. public class HLSStream : Stream
  9. {
  10. public HLSStream(string filename, int width = 0, int height = 0, int bandwidth = 0)
  11. {
  12. this._streams = new List<Stream>();
  13. this._chunks = new List<Stream.Chunk>();
  14. this._width = width;
  15. this._height = height;
  16. this._bandwidth = bandwidth;
  17. this._streamURL = filename;
  18. try
  19. {
  20. string[] text2;
  21. if (filename.ToLower().StartsWith("http://") || filename.ToLower().StartsWith("https://"))
  22. {
  23. string text = new WebClient().DownloadString(filename);
  24. text2 = text.Split(new char[]
  25. {
  26. '\n'
  27. });
  28. }
  29. else
  30. {
  31. text2 = File.ReadAllLines(filename);
  32. }
  33. int num = filename.LastIndexOf('/');
  34. if (num < 0)
  35. {
  36. num = filename.LastIndexOf('\\');
  37. }
  38. string path = this._streamURL.Substring(0, num + 1);
  39. this.ParseFile(text2, path);
  40. }
  41. catch (Exception ex)
  42. {
  43. throw ex;
  44. }
  45. }
  46. public override int Width
  47. {
  48. get
  49. {
  50. return this._width;
  51. }
  52. }
  53. public override int Height
  54. {
  55. get
  56. {
  57. return this._height;
  58. }
  59. }
  60. public override int Bandwidth
  61. {
  62. get
  63. {
  64. return this._bandwidth;
  65. }
  66. }
  67. public override string URL
  68. {
  69. get
  70. {
  71. return this._streamURL;
  72. }
  73. }
  74. public override List<Stream.Chunk> GetAllChunks()
  75. {
  76. List<Stream.Chunk> list = new List<Stream.Chunk>();
  77. for (int i = 0; i < this._streams.Count; i++)
  78. {
  79. List<Stream.Chunk> allChunks = this._streams[i].GetAllChunks();
  80. list.AddRange(allChunks);
  81. }
  82. list.AddRange(this._chunks);
  83. return list;
  84. }
  85. public override List<Stream.Chunk> GetChunks()
  86. {
  87. return this._chunks;
  88. }
  89. public override List<Stream> GetAllStreams()
  90. {
  91. List<Stream> list = new List<Stream>();
  92. for (int i = 0; i < this._streams.Count; i++)
  93. {
  94. List<Stream> allStreams = this._streams[i].GetAllStreams();
  95. list.AddRange(allStreams);
  96. }
  97. list.AddRange(this._streams);
  98. return list;
  99. }
  100. public override List<Stream> GetStreams()
  101. {
  102. return this._streams;
  103. }
  104. private bool ExtractStreamInfo(string line, ref int width, ref int height, ref int bandwidth)
  105. {
  106. if (line.StartsWith("#EXT-X-STREAM-INF"))
  107. {
  108. int num = line.IndexOf("BANDWIDTH=");
  109. if (num >= 0)
  110. {
  111. int num2 = line.IndexOf(',', num + "BANDWIDTH=".Length);
  112. if (num2 < 0)
  113. {
  114. num2 = line.Length - 1;
  115. }
  116. if (num2 >= 0 && num2 - "BANDWIDTH=".Length > num)
  117. {
  118. int length = num2 - num - "BANDWIDTH=".Length;
  119. string s = line.Substring(num + "BANDWIDTH=".Length, length);
  120. if (!int.TryParse(s, out bandwidth))
  121. {
  122. bandwidth = 0;
  123. }
  124. }
  125. }
  126. else
  127. {
  128. bandwidth = 0;
  129. }
  130. int num3 = line.IndexOf("RESOLUTION=");
  131. if (num3 >= 0)
  132. {
  133. int num4 = line.IndexOf(',', num3 + "RESOLUTION=".Length);
  134. if (num4 < 0)
  135. {
  136. num4 = line.Length - 1;
  137. }
  138. if (num4 >= 0 && num4 - "RESOLUTION=".Length > num3)
  139. {
  140. int length2 = num4 - num3 - "RESOLUTION=".Length;
  141. string text = line.Substring(num3 + "RESOLUTION=".Length, length2);
  142. int num5 = text.IndexOf('x');
  143. if (num5 < 0 || !int.TryParse(text.Substring(0, num5), out width) || !int.TryParse(text.Substring(num5 + 1, text.Length - (num5 + 1)), out height))
  144. {
  145. width = (height = 0);
  146. }
  147. }
  148. }
  149. else
  150. {
  151. width = (height = 0);
  152. }
  153. return true;
  154. }
  155. return false;
  156. }
  157. private static bool IsChunk(string line)
  158. {
  159. return line.StartsWith("#EXTINF");
  160. }
  161. private void ParseFile(string[] text, string path)
  162. {
  163. bool flag = false;
  164. bool flag2 = false;
  165. int width = 0;
  166. int height = 0;
  167. int bandwidth = 0;
  168. for (int i = 0; i < text.Length; i++)
  169. {
  170. if (this.ExtractStreamInfo(text[i], ref width, ref height, ref bandwidth))
  171. {
  172. flag2 = true;
  173. flag = false;
  174. }
  175. else if (HLSStream.IsChunk(text[i]))
  176. {
  177. flag = true;
  178. flag2 = false;
  179. }
  180. else if (flag)
  181. {
  182. Stream.Chunk item;
  183. item.name = path + text[i];
  184. this._chunks.Add(item);
  185. flag = false;
  186. flag2 = false;
  187. }
  188. else if (flag2)
  189. {
  190. try
  191. {
  192. string filename = (text[i].IndexOf("://") >= 0) ? text[i] : (path + text[i]);
  193. HLSStream item2 = new HLSStream(filename, width, height, bandwidth);
  194. this._streams.Add(item2);
  195. }
  196. catch (Exception ex)
  197. {
  198. Debug.LogError(string.Concat(new string[]
  199. {
  200. "[AVProVideo]HLSParser cannot parse stream ",
  201. path,
  202. text[i],
  203. ", ",
  204. ex.Message
  205. }));
  206. }
  207. flag = false;
  208. flag2 = false;
  209. }
  210. else
  211. {
  212. flag = false;
  213. flag2 = false;
  214. }
  215. }
  216. }
  217. private const string BANDWITH_NAME = "BANDWIDTH=";
  218. private const string RESOLUTION_NAME = "RESOLUTION=";
  219. private const string CHUNK_TAG = "#EXTINF";
  220. private const string STREAM_TAG = "#EXT-X-STREAM-INF";
  221. private List<Stream> _streams;
  222. private List<Stream.Chunk> _chunks;
  223. private string _streamURL;
  224. private int _width;
  225. private int _height;
  226. private int _bandwidth;
  227. }
  228. }