aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ClientStack/LindenUDP/J2KImage.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/ClientStack/LindenUDP/J2KImage.cs')
-rw-r--r--OpenSim/Region/ClientStack/LindenUDP/J2KImage.cs354
1 files changed, 354 insertions, 0 deletions
diff --git a/OpenSim/Region/ClientStack/LindenUDP/J2KImage.cs b/OpenSim/Region/ClientStack/LindenUDP/J2KImage.cs
new file mode 100644
index 0000000..d8cf9a7
--- /dev/null
+++ b/OpenSim/Region/ClientStack/LindenUDP/J2KImage.cs
@@ -0,0 +1,354 @@
1/*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSim Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28using System;
29using System.Collections.Generic;
30using OpenMetaverse;
31using OpenMetaverse.Imaging;
32using OpenSim.Framework;
33using OpenSim.Region.Framework.Interfaces;
34using log4net;
35using System.Reflection;
36
37namespace OpenSim.Region.ClientStack.LindenUDP
38{
39 /*
40 *
41 * J2KImage
42 *
43 * We use this class to store image data and associated request data and attributes
44 *
45 *
46 *
47 */
48
49 public class J2KImage
50 {
51 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
52 public double m_designatedPriorityKey;
53 public double m_requestedPriority = 0.0d;
54 public uint m_lastSequence = 0;
55 public uint m_requestedPacketNumber;
56 public sbyte m_requestedDiscardLevel;
57 public UUID m_requestedUUID;
58 public IJ2KDecoder m_j2kDecodeModule;
59 public IAssetCache m_assetCache;
60 public OpenJPEG.J2KLayerInfo[] Layers = new OpenJPEG.J2KLayerInfo[0];
61 public AssetBase m_MissingSubstitute = null;
62 public bool m_decoded = false;
63 public bool m_completedSendAtCurrentDiscardLevel;
64
65 private sbyte m_discardLevel=-1;
66 private uint m_packetNumber;
67 private bool m_decoderequested = false;
68 private bool m_hasasset = false;
69 private bool m_asset_requested = false;
70 private bool m_sentinfo = false;
71 private uint m_stopPacket = 0;
72 private const int cImagePacketSize = 1000;
73 private const int cFirstPacketSize = 600;
74 private AssetBase m_asset = null;
75
76
77 public uint m_pPacketNumber
78 {
79 get { return m_packetNumber; }
80 }
81 public uint m_pStopPacketNumber
82 {
83 get { return m_stopPacket; }
84 }
85
86 public byte[] Data
87 {
88 get { return m_asset.Data; }
89 }
90
91 public ushort TexturePacketCount()
92 {
93 if (!m_decoded)
94 return 0;
95 return (ushort)(((m_asset.Data.Length - cFirstPacketSize + cImagePacketSize - 1) / cImagePacketSize) + 1);
96 }
97
98 public void J2KDecodedCallback(UUID AssetId, OpenJPEG.J2KLayerInfo[] layers)
99 {
100 Layers = layers;
101 m_decoded = true;
102 RunUpdate();
103 }
104
105 public void AssetDataCallback(UUID AssetID, AssetBase asset)
106 {
107 m_hasasset = true;
108 if (asset == null || asset.Data == null)
109 {
110 m_asset = m_MissingSubstitute;
111 }
112 else
113 {
114 m_asset = asset;
115 }
116 RunUpdate();
117 }
118
119 private int GetPacketForBytePosition(int bytePosition)
120 {
121 return ((bytePosition - cFirstPacketSize + cImagePacketSize - 1) / cImagePacketSize) + 1;
122 }
123 public int LastPacketSize()
124 {
125 if (m_packetNumber == 1)
126 return m_asset.Data.Length;
127 return (m_asset.Data.Length - cFirstPacketSize) % cImagePacketSize;
128 }
129
130 public int CurrentBytePosition()
131 {
132 if (m_packetNumber == 0)
133 return 0;
134 if (m_packetNumber == 1)
135 return cFirstPacketSize;
136
137 int result = cFirstPacketSize + ((int)m_packetNumber - 2) * cImagePacketSize;
138 if (result < 0)
139 {
140 result = cFirstPacketSize;
141 }
142 return result;
143 }
144 public bool SendFirstPacket(LLClientView client)
145 {
146
147 // Do we have less then 1 packet's worth of data?
148 if (m_asset.Data.Length <= cFirstPacketSize)
149 {
150 // Send only 1 packet
151 client.SendImageFirstPart(1, m_requestedUUID, (uint)m_asset.Data.Length, m_asset.Data, 2);
152 m_stopPacket = 0;
153 return true;
154 }
155 else
156 {
157 byte[] firstImageData = new byte[cFirstPacketSize];
158 try
159 {
160 Buffer.BlockCopy(m_asset.Data, 0, firstImageData, 0, (int)cFirstPacketSize);
161 client.SendImageFirstPart(TexturePacketCount(), m_requestedUUID, (uint)m_asset.Data.Length, firstImageData, 2);
162 }
163 catch (Exception)
164 {
165 m_log.Error("Texture block copy failed. Possibly out of memory?");
166 return true;
167 }
168 }
169 return false;
170
171 }
172 private bool SendPacket(LLClientView client)
173 {
174 bool complete = false;
175 int imagePacketSize = ((int)m_packetNumber == (TexturePacketCount())) ? LastPacketSize() : cImagePacketSize;
176
177 if ((CurrentBytePosition() + cImagePacketSize) > m_asset.Data.Length)
178 {
179 imagePacketSize = LastPacketSize();
180 complete=true;
181 if ((CurrentBytePosition() + imagePacketSize) > m_asset.Data.Length)
182 {
183 imagePacketSize = m_asset.Data.Length - CurrentBytePosition();
184 complete = true;
185 }
186 }
187
188 //It's concievable that the client might request packet one
189 //from a one packet image, which is really packet 0,
190 //which would leave us with a negative imagePacketSize..
191 if (imagePacketSize > 0)
192 {
193 byte[] imageData = new byte[imagePacketSize];
194 try
195 {
196 Buffer.BlockCopy(m_asset.Data, CurrentBytePosition(), imageData, 0, imagePacketSize);
197 }
198 catch (Exception e)
199 {
200 m_log.Error("Error copying texture block. Out of memory? imagePacketSize was " + imagePacketSize.ToString() + " on packet " + m_packetNumber.ToString() + " out of " + m_stopPacket.ToString() + ". Exception: " + e.ToString());
201 return false;
202 }
203
204 //Send the packet
205 client.SendImageNextPart((ushort)(m_packetNumber-1), m_requestedUUID, imageData);
206
207 }
208 if (complete)
209 {
210 return false;
211 }
212 else
213 {
214 return true;
215 }
216
217
218 }
219 public bool SendPackets(LLClientView client)
220 {
221
222 if (!m_completedSendAtCurrentDiscardLevel)
223 {
224 if (m_packetNumber <= m_stopPacket)
225 {
226
227 bool SendMore = true;
228 if (!m_sentinfo || (m_packetNumber == 0))
229 {
230 if (SendFirstPacket(client))
231 {
232 SendMore = false;
233 }
234 m_sentinfo = true;
235 m_packetNumber++;
236 }
237
238 if (m_packetNumber < 2)
239 {
240 m_packetNumber = 2;
241 }
242
243 int count=0;
244 while (SendMore && count < 5 && m_packetNumber <= m_stopPacket)
245 {
246 count++;
247 SendMore = SendPacket(client);
248 m_packetNumber++;
249 }
250 if (m_packetNumber > m_stopPacket)
251 {
252
253 return true;
254
255 }
256
257 }
258
259 }
260 return false;
261 }
262
263 public void RunUpdate()
264 {
265 //This is where we decide what we need to update
266 //and assign the real discardLevel and packetNumber
267 //assuming of course that the connected client might be bonkers
268
269 if (!m_hasasset)
270 {
271
272 if (!m_asset_requested)
273 {
274 m_asset_requested = true;
275 m_assetCache.GetAsset(m_requestedUUID, AssetDataCallback, true);
276
277 }
278
279 }
280 else
281 {
282
283
284 if (!m_decoded)
285 {
286 //We need to decode the requested image first
287 if (!m_decoderequested)
288 {
289 //Request decode
290 m_decoderequested = true;
291 // Do we have a jpeg decoder?
292 if (m_j2kDecodeModule != null)
293 {
294 // Send it off to the jpeg decoder
295 m_j2kDecodeModule.decode(m_requestedUUID, Data, J2KDecodedCallback);
296
297 }
298 else
299 {
300 J2KDecodedCallback(m_requestedUUID, new OpenJPEG.J2KLayerInfo[0]);
301 }
302 }
303
304 }
305 else
306 {
307
308
309 //discardLevel of -1 means just update the priority
310 if (m_requestedDiscardLevel != -1)
311 {
312
313 //Evaluate the discard level
314 //First, is it positive?
315 if (m_requestedDiscardLevel >= 0)
316 {
317 if (m_requestedDiscardLevel > Layers.Length - 1)
318 {
319 m_discardLevel = (sbyte)(Layers.Length - 1);
320 }
321 else
322 {
323 m_discardLevel = m_requestedDiscardLevel;
324 }
325
326 //Calculate the m_stopPacket
327 if (Layers.Length > 0)
328 {
329 m_stopPacket = (uint)GetPacketForBytePosition(Layers[(Layers.Length - 1) - m_discardLevel].End);
330 }
331 else
332 {
333 m_stopPacket = TexturePacketCount();
334 }
335 //Don't reset packet number unless we're waiting or it's ahead of us
336 if (m_completedSendAtCurrentDiscardLevel || m_requestedPacketNumber>m_packetNumber)
337 {
338 m_packetNumber = m_requestedPacketNumber;
339 }
340
341 if (m_packetNumber <= m_stopPacket)
342 {
343 m_completedSendAtCurrentDiscardLevel = false;
344 }
345
346 }
347
348 }
349 }
350 }
351 }
352
353 }
354}