123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using UnityEngine;
- namespace RenderHeads.Media.AVProVideo
- {
- public class HLSStream : Stream
- {
- public HLSStream(string filename, int width = 0, int height = 0, int bandwidth = 0)
- {
- this._streams = new List<Stream>();
- this._chunks = new List<Stream.Chunk>();
- this._width = width;
- this._height = height;
- this._bandwidth = bandwidth;
- this._streamURL = filename;
- try
- {
- string[] text2;
- if (filename.ToLower().StartsWith("http://") || filename.ToLower().StartsWith("https://"))
- {
- string text = new WebClient().DownloadString(filename);
- text2 = text.Split(new char[]
- {
- '\n'
- });
- }
- else
- {
- text2 = File.ReadAllLines(filename);
- }
- int num = filename.LastIndexOf('/');
- if (num < 0)
- {
- num = filename.LastIndexOf('\\');
- }
- string path = this._streamURL.Substring(0, num + 1);
- this.ParseFile(text2, path);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public override int Width
- {
- get
- {
- return this._width;
- }
- }
- public override int Height
- {
- get
- {
- return this._height;
- }
- }
- public override int Bandwidth
- {
- get
- {
- return this._bandwidth;
- }
- }
- public override string URL
- {
- get
- {
- return this._streamURL;
- }
- }
- public override List<Stream.Chunk> GetAllChunks()
- {
- List<Stream.Chunk> list = new List<Stream.Chunk>();
- for (int i = 0; i < this._streams.Count; i++)
- {
- List<Stream.Chunk> allChunks = this._streams[i].GetAllChunks();
- list.AddRange(allChunks);
- }
- list.AddRange(this._chunks);
- return list;
- }
- public override List<Stream.Chunk> GetChunks()
- {
- return this._chunks;
- }
- public override List<Stream> GetAllStreams()
- {
- List<Stream> list = new List<Stream>();
- for (int i = 0; i < this._streams.Count; i++)
- {
- List<Stream> allStreams = this._streams[i].GetAllStreams();
- list.AddRange(allStreams);
- }
- list.AddRange(this._streams);
- return list;
- }
- public override List<Stream> GetStreams()
- {
- return this._streams;
- }
- private bool ExtractStreamInfo(string line, ref int width, ref int height, ref int bandwidth)
- {
- if (line.StartsWith("#EXT-X-STREAM-INF"))
- {
- int num = line.IndexOf("BANDWIDTH=");
- if (num >= 0)
- {
- int num2 = line.IndexOf(',', num + "BANDWIDTH=".Length);
- if (num2 < 0)
- {
- num2 = line.Length - 1;
- }
- if (num2 >= 0 && num2 - "BANDWIDTH=".Length > num)
- {
- int length = num2 - num - "BANDWIDTH=".Length;
- string s = line.Substring(num + "BANDWIDTH=".Length, length);
- if (!int.TryParse(s, out bandwidth))
- {
- bandwidth = 0;
- }
- }
- }
- else
- {
- bandwidth = 0;
- }
- int num3 = line.IndexOf("RESOLUTION=");
- if (num3 >= 0)
- {
- int num4 = line.IndexOf(',', num3 + "RESOLUTION=".Length);
- if (num4 < 0)
- {
- num4 = line.Length - 1;
- }
- if (num4 >= 0 && num4 - "RESOLUTION=".Length > num3)
- {
- int length2 = num4 - num3 - "RESOLUTION=".Length;
- string text = line.Substring(num3 + "RESOLUTION=".Length, length2);
- int num5 = text.IndexOf('x');
- if (num5 < 0 || !int.TryParse(text.Substring(0, num5), out width) || !int.TryParse(text.Substring(num5 + 1, text.Length - (num5 + 1)), out height))
- {
- width = (height = 0);
- }
- }
- }
- else
- {
- width = (height = 0);
- }
- return true;
- }
- return false;
- }
- private static bool IsChunk(string line)
- {
- return line.StartsWith("#EXTINF");
- }
- private void ParseFile(string[] text, string path)
- {
- bool flag = false;
- bool flag2 = false;
- int width = 0;
- int height = 0;
- int bandwidth = 0;
- for (int i = 0; i < text.Length; i++)
- {
- if (this.ExtractStreamInfo(text[i], ref width, ref height, ref bandwidth))
- {
- flag2 = true;
- flag = false;
- }
- else if (HLSStream.IsChunk(text[i]))
- {
- flag = true;
- flag2 = false;
- }
- else if (flag)
- {
- Stream.Chunk item;
- item.name = path + text[i];
- this._chunks.Add(item);
- flag = false;
- flag2 = false;
- }
- else if (flag2)
- {
- try
- {
- string filename = (text[i].IndexOf("://") >= 0) ? text[i] : (path + text[i]);
- HLSStream item2 = new HLSStream(filename, width, height, bandwidth);
- this._streams.Add(item2);
- }
- catch (Exception ex)
- {
- Debug.LogError(string.Concat(new string[]
- {
- "[AVProVideo]HLSParser cannot parse stream ",
- path,
- text[i],
- ", ",
- ex.Message
- }));
- }
- flag = false;
- flag2 = false;
- }
- else
- {
- flag = false;
- flag2 = false;
- }
- }
- }
- private const string BANDWITH_NAME = "BANDWIDTH=";
- private const string RESOLUTION_NAME = "RESOLUTION=";
- private const string CHUNK_TAG = "#EXTINF";
- private const string STREAM_TAG = "#EXT-X-STREAM-INF";
- private List<Stream> _streams;
- private List<Stream.Chunk> _chunks;
- private string _streamURL;
- private int _width;
- private int _height;
- private int _bandwidth;
- }
- }
|