markus / ConvertService / ConverterService / ImageFields / SmoothStreamingFile.cs @ d48260a2
이력 | 보기 | 이력해설 | 다운로드 (7.5 KB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.Linq; |
4 |
using System.Text; |
5 |
using System.Xml.Linq; |
6 |
using System.Diagnostics; |
7 |
using System.IO; |
8 |
using System.Globalization; |
9 |
using System.Net; |
10 |
|
11 |
namespace ImageFields |
12 |
{ |
13 |
internal class SmoothStreamingFile : IDisposable |
14 |
{ |
15 |
// Fields |
16 |
private static long factor = 0x3b9aca00L; |
17 |
private MemoryStream memoryStream; |
18 |
private MP4File[] mp4files = new MP4File[0x20]; |
19 |
private string rootFileName; |
20 |
private static char[] splitarray = new char[] { '\\', '_', '.' }; |
21 |
|
22 |
// Methods |
23 |
public SmoothStreamingFile(string fileName) |
24 |
{ |
25 |
this.rootFileName = fileName; |
26 |
this.memoryStream = new MemoryStream(0x6000); |
27 |
} |
28 |
|
29 |
public void Close() |
30 |
{ |
31 |
this.Dispose(); |
32 |
} |
33 |
|
34 |
public void CreateDirectory(object o, DirectoryEventArgs e) |
35 |
{ |
36 |
} |
37 |
|
38 |
public void Dispose() |
39 |
{ |
40 |
int maxlevel = 0; |
41 |
for (int i = 0; i < 0x20; i++) |
42 |
{ |
43 |
if (this.mp4files[i] != null) |
44 |
{ |
45 |
this.mp4files[i].Close(); |
46 |
this.mp4files[i] = null; |
47 |
maxlevel = i; |
48 |
} |
49 |
} |
50 |
this.WriteServerManifest(maxlevel); |
51 |
this.memoryStream.Close(); |
52 |
this.memoryStream = null; |
53 |
GC.SuppressFinalize(this); |
54 |
} |
55 |
|
56 |
~SmoothStreamingFile() |
57 |
{ |
58 |
this.Dispose(); |
59 |
} |
60 |
|
61 |
private string GetFileName(int level) |
62 |
{ |
63 |
return (Path.ChangeExtension(this.rootFileName, null) + "_" + level.ToString("00", CultureInfo.InvariantCulture) + ".ismv"); |
64 |
} |
65 |
|
66 |
public static void InputCompleted(object o, StreamEventArgs e) |
67 |
{ |
68 |
e.Stream.Close(); |
69 |
} |
70 |
|
71 |
public static void InputNeeded(object o, StreamEventArgs e) |
72 |
{ |
73 |
if (e.MimeType == "application/xml") |
74 |
{ |
75 |
Uri uri = new Uri(Path.ChangeExtension(e.FileName, "ismc"), UriKind.RelativeOrAbsolute); |
76 |
if (!uri.IsFile) |
77 |
{ |
78 |
throw new InvalidDataException("SmoothStreaming supports only local paths."); |
79 |
} |
80 |
string localPath = uri.LocalPath; |
81 |
e.Stream = new FileStream(localPath, FileMode.Open, FileAccess.Read); |
82 |
} |
83 |
else |
84 |
{ |
85 |
int num2; |
86 |
Debug.WriteLine("Input Event for: " + e.FileName); |
87 |
string[] strArray = e.FileName.Split(new char[] { '\\' }); |
88 |
string[] strArray2 = strArray[strArray.Length - 1].Split(splitarray); |
89 |
long num = (factor * int.Parse(strArray2[strArray2.Length - 2], CultureInfo.InvariantCulture)) + int.Parse(strArray2[strArray2.Length - 2], CultureInfo.InvariantCulture); |
90 |
string str2 = strArray[strArray.Length - 3]; |
91 |
string str3 = string.Empty; |
92 |
for (num2 = 0; num2 <= (strArray.Length - 4); num2++) |
93 |
{ |
94 |
str3 = str3 + strArray[num2] + '\\'; |
95 |
} |
96 |
FileStream input = new FileStream(str3 + str2.Substring(0, str2.Length - 6) + "_" + int.Parse(strArray[strArray.Length - 2], CultureInfo.InvariantCulture).ToString("00", CultureInfo.InvariantCulture) + ".ismv", FileMode.Open, FileAccess.Read); |
97 |
BinaryReader reader = new BinaryReader(input); |
98 |
reader.BaseStream.Seek(-4L, SeekOrigin.End); |
99 |
int num3 = IPAddress.NetworkToHostOrder(reader.ReadInt32()); |
100 |
reader.BaseStream.Seek((long)(-num3 + 0x1c), SeekOrigin.End); |
101 |
int num4 = IPAddress.NetworkToHostOrder(reader.ReadInt32()); |
102 |
long num5 = 0L; |
103 |
for (num2 = 0; num2 < num4; num2++) |
104 |
{ |
105 |
long num6 = IPAddress.NetworkToHostOrder(reader.ReadInt64()); |
106 |
num5 = IPAddress.NetworkToHostOrder(reader.ReadInt64()); |
107 |
if (num6 == num) |
108 |
{ |
109 |
break; |
110 |
} |
111 |
reader.BaseStream.Seek(3L, SeekOrigin.Current); |
112 |
} |
113 |
reader.BaseStream.Seek(num5 + 0x40L, SeekOrigin.Begin); |
114 |
int count = IPAddress.NetworkToHostOrder(reader.ReadInt32()); |
115 |
reader.ReadInt32(); |
116 |
byte[] buffer = new byte[count]; |
117 |
input.Read(buffer, 0, count); |
118 |
MemoryStream stream2 = new MemoryStream(buffer); |
119 |
e.Stream = stream2; |
120 |
reader.Close(); |
121 |
input.Close(); |
122 |
} |
123 |
} |
124 |
|
125 |
public void OutputCompleted(object o, StreamEventArgs e) |
126 |
{ |
127 |
Debug.WriteLine("Output Completed Event for: " + e.FileName); |
128 |
if (e.MimeType == "application/xml") |
129 |
{ |
130 |
e.Stream.Close(); |
131 |
} |
132 |
else |
133 |
{ |
134 |
string[] strArray = e.FileName.Split(splitarray); |
135 |
int index = int.Parse(strArray[strArray.Length - 4], CultureInfo.InvariantCulture); |
136 |
if (this.mp4files[index] == null) |
137 |
{ |
138 |
this.mp4files[index] = new MP4File(this.GetFileName(index)); |
139 |
} |
140 |
this.mp4files[index].WriteMP4Fragment(this.memoryStream.ToArray(), int.Parse(strArray[strArray.Length - 3], CultureInfo.InvariantCulture) + (factor * int.Parse(strArray[strArray.Length - 2], CultureInfo.InvariantCulture))); |
141 |
} |
142 |
} |
143 |
|
144 |
public void OutputNeeded(object o, StreamEventArgs e) |
145 |
{ |
146 |
Debug.WriteLine("Output Needed Event for: " + e.FileName); |
147 |
if (e.MimeType == "application/xml") |
148 |
{ |
149 |
e.Stream = new FileStream(Path.ChangeExtension(this.rootFileName, "ismc"), FileMode.Create); |
150 |
} |
151 |
else |
152 |
{ |
153 |
this.memoryStream.Position = 0L; |
154 |
e.Stream = this.memoryStream; |
155 |
} |
156 |
} |
157 |
|
158 |
private void WriteServerManifest(int maxlevel) |
159 |
{ |
160 |
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(this.rootFileName); |
161 |
XNamespace namespace2 = "http://www.w3.org/2001/SMIL20/Language"; |
162 |
XElement content = new XElement((XName)(namespace2 + "switch")); |
163 |
for (int i = 0; i <= maxlevel; i++) |
164 |
{ |
165 |
XElement element2 = new XElement((XName)(namespace2 + "param"), new object[] { new XAttribute("name", "trackID"), new XAttribute("value", "1"), new XAttribute("valuetype", "data") }); |
166 |
XElement element3 = new XElement((XName)(namespace2 + "param"), new object[] { new XAttribute("name", "trackName"), new XAttribute("value", "tile"), new XAttribute("valuetype", "data") }); |
167 |
XElement element4 = new XElement((XName)(namespace2 + "video"), new object[] { new XAttribute("src", Path.GetFileName(this.GetFileName(i))), new XAttribute("systemBitrate", i + 100), element2, element3 }); |
168 |
content.Add(element4); |
169 |
} |
170 |
XElement element5 = new XElement((XName)(namespace2 + "body"), content); |
171 |
XElement element6 = new XElement((XName)(namespace2 + "meta"), new object[] { new XAttribute("name", "clientManifestRelativePath"), new XAttribute("content", fileNameWithoutExtension + ".ismc") }); |
172 |
XElement element7 = new XElement((XName)(namespace2 + "head"), element6); |
173 |
new XElement((XName)(namespace2 + "smil"), new object[] { element7, element5 }).Save(Path.ChangeExtension(this.rootFileName, "ism")); |
174 |
} |
175 |
} |
176 |
|
177 |
|
178 |
|
179 |
} |