aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim')
-rw-r--r--OpenSim/Region/ClientStack/LindenUDP/J2KImage.cs354
-rw-r--r--OpenSim/Region/ClientStack/LindenUDP/LLImageManager.cs316
2 files changed, 354 insertions, 316 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}
diff --git a/OpenSim/Region/ClientStack/LindenUDP/LLImageManager.cs b/OpenSim/Region/ClientStack/LindenUDP/LLImageManager.cs
index 6bfab90..af359b3 100644
--- a/OpenSim/Region/ClientStack/LindenUDP/LLImageManager.cs
+++ b/OpenSim/Region/ClientStack/LindenUDP/LLImageManager.cs
@@ -263,320 +263,4 @@ namespace OpenSim.Region.ClientStack.LindenUDP
263 263
264 264
265 } 265 }
266
267 /*
268 *
269 * J2KImage
270 *
271 * We use this class to store image data and associated request data and attributes
272 *
273 *
274 *
275 */
276
277 public class J2KImage
278 {
279 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
280 public double m_designatedPriorityKey;
281 public double m_requestedPriority = 0.0d;
282 public uint m_lastSequence = 0;
283 public uint m_requestedPacketNumber;
284 public sbyte m_requestedDiscardLevel;
285 public UUID m_requestedUUID;
286 public IJ2KDecoder m_j2kDecodeModule;
287 public IAssetCache m_assetCache;
288 public OpenJPEG.J2KLayerInfo[] Layers = new OpenJPEG.J2KLayerInfo[0];
289 public AssetBase m_MissingSubstitute = null;
290 public bool m_decoded = false;
291 public bool m_completedSendAtCurrentDiscardLevel;
292
293 private sbyte m_discardLevel=-1;
294 private uint m_packetNumber;
295 private bool m_decoderequested = false;
296 private bool m_hasasset = false;
297 private bool m_asset_requested = false;
298 private bool m_sentinfo = false;
299 private uint m_stopPacket = 0;
300 private const int cImagePacketSize = 1000;
301 private const int cFirstPacketSize = 600;
302 private AssetBase m_asset = null;
303
304
305 public uint m_pPacketNumber
306 {
307 get { return m_packetNumber; }
308 }
309 public uint m_pStopPacketNumber
310 {
311 get { return m_stopPacket; }
312 }
313
314 public byte[] Data
315 {
316 get { return m_asset.Data; }
317 }
318
319 public ushort TexturePacketCount()
320 {
321 if (!m_decoded)
322 return 0;
323 return (ushort)(((m_asset.Data.Length - cFirstPacketSize + cImagePacketSize - 1) / cImagePacketSize) + 1);
324 }
325
326 public void J2KDecodedCallback(UUID AssetId, OpenJPEG.J2KLayerInfo[] layers)
327 {
328 Layers = layers;
329 m_decoded = true;
330 RunUpdate();
331 }
332
333 public void AssetDataCallback(UUID AssetID, AssetBase asset)
334 {
335 m_hasasset = true;
336 if (asset == null || asset.Data == null)
337 {
338 m_asset = m_MissingSubstitute;
339 }
340 else
341 {
342 m_asset = asset;
343 }
344 RunUpdate();
345 }
346
347 private int GetPacketForBytePosition(int bytePosition)
348 {
349 return ((bytePosition - cFirstPacketSize + cImagePacketSize - 1) / cImagePacketSize) + 1;
350 }
351 public int LastPacketSize()
352 {
353 if (m_packetNumber == 1)
354 return m_asset.Data.Length;
355 return (m_asset.Data.Length - cFirstPacketSize) % cImagePacketSize;
356 }
357
358 public int CurrentBytePosition()
359 {
360 if (m_packetNumber == 0)
361 return 0;
362 if (m_packetNumber == 1)
363 return cFirstPacketSize;
364
365 int result = cFirstPacketSize + ((int)m_packetNumber - 2) * cImagePacketSize;
366 if (result < 0)
367 {
368 result = cFirstPacketSize;
369 }
370 return result;
371 }
372 public bool SendFirstPacket(LLClientView client)
373 {
374
375 // Do we have less then 1 packet's worth of data?
376 if (m_asset.Data.Length <= cFirstPacketSize)
377 {
378 // Send only 1 packet
379 client.SendImageFirstPart(1, m_requestedUUID, (uint)m_asset.Data.Length, m_asset.Data, 2);
380 m_stopPacket = 0;
381 return true;
382 }
383 else
384 {
385 byte[] firstImageData = new byte[cFirstPacketSize];
386 try
387 {
388 Buffer.BlockCopy(m_asset.Data, 0, firstImageData, 0, (int)cFirstPacketSize);
389 client.SendImageFirstPart(TexturePacketCount(), m_requestedUUID, (uint)m_asset.Data.Length, firstImageData, 2);
390 }
391 catch (Exception)
392 {
393 m_log.Error("Texture block copy failed. Possibly out of memory?");
394 return true;
395 }
396 }
397 return false;
398
399 }
400 private bool SendPacket(LLClientView client)
401 {
402 bool complete = false;
403 int imagePacketSize = ((int)m_packetNumber == (TexturePacketCount())) ? LastPacketSize() : cImagePacketSize;
404
405 if ((CurrentBytePosition() + cImagePacketSize) > m_asset.Data.Length)
406 {
407 imagePacketSize = LastPacketSize();
408 complete=true;
409 if ((CurrentBytePosition() + imagePacketSize) > m_asset.Data.Length)
410 {
411 imagePacketSize = m_asset.Data.Length - CurrentBytePosition();
412 complete = true;
413 }
414 }
415
416 //It's concievable that the client might request packet one
417 //from a one packet image, which is really packet 0,
418 //which would leave us with a negative imagePacketSize..
419 if (imagePacketSize > 0)
420 {
421 byte[] imageData = new byte[imagePacketSize];
422 try
423 {
424 Buffer.BlockCopy(m_asset.Data, CurrentBytePosition(), imageData, 0, imagePacketSize);
425 }
426 catch (Exception e)
427 {
428 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());
429 return false;
430 }
431
432 //Send the packet
433 client.SendImageNextPart((ushort)(m_packetNumber-1), m_requestedUUID, imageData);
434
435 }
436 if (complete)
437 {
438 return false;
439 }
440 else
441 {
442 return true;
443 }
444
445
446 }
447 public bool SendPackets(LLClientView client)
448 {
449
450 if (!m_completedSendAtCurrentDiscardLevel)
451 {
452 if (m_packetNumber <= m_stopPacket)
453 {
454
455 bool SendMore = true;
456 if (!m_sentinfo || (m_packetNumber == 0))
457 {
458 if (SendFirstPacket(client))
459 {
460 SendMore = false;
461 }
462 m_sentinfo = true;
463 m_packetNumber++;
464 }
465
466 if (m_packetNumber < 2)
467 {
468 m_packetNumber = 2;
469 }
470
471 int count=0;
472 while (SendMore && count < 5 && m_packetNumber <= m_stopPacket)
473 {
474 count++;
475 SendMore = SendPacket(client);
476 m_packetNumber++;
477 }
478 if (m_packetNumber > m_stopPacket)
479 {
480
481 return true;
482
483 }
484
485 }
486
487 }
488 return false;
489 }
490
491 public void RunUpdate()
492 {
493 //This is where we decide what we need to update
494 //and assign the real discardLevel and packetNumber
495 //assuming of course that the connected client might be bonkers
496
497 if (!m_hasasset)
498 {
499
500 if (!m_asset_requested)
501 {
502 m_asset_requested = true;
503 m_assetCache.GetAsset(m_requestedUUID, AssetDataCallback, true);
504
505 }
506
507 }
508 else
509 {
510
511
512 if (!m_decoded)
513 {
514 //We need to decode the requested image first
515 if (!m_decoderequested)
516 {
517 //Request decode
518 m_decoderequested = true;
519 // Do we have a jpeg decoder?
520 if (m_j2kDecodeModule != null)
521 {
522 // Send it off to the jpeg decoder
523 m_j2kDecodeModule.decode(m_requestedUUID, Data, J2KDecodedCallback);
524
525 }
526 else
527 {
528 J2KDecodedCallback(m_requestedUUID, new OpenJPEG.J2KLayerInfo[0]);
529 }
530 }
531
532 }
533 else
534 {
535
536
537 //discardLevel of -1 means just update the priority
538 if (m_requestedDiscardLevel != -1)
539 {
540
541 //Evaluate the discard level
542 //First, is it positive?
543 if (m_requestedDiscardLevel >= 0)
544 {
545 if (m_requestedDiscardLevel > Layers.Length - 1)
546 {
547 m_discardLevel = (sbyte)(Layers.Length - 1);
548 }
549 else
550 {
551 m_discardLevel = m_requestedDiscardLevel;
552 }
553
554 //Calculate the m_stopPacket
555 if (Layers.Length > 0)
556 {
557 m_stopPacket = (uint)GetPacketForBytePosition(Layers[(Layers.Length - 1) - m_discardLevel].End);
558 }
559 else
560 {
561 m_stopPacket = TexturePacketCount();
562 }
563 //Don't reset packet number unless we're waiting or it's ahead of us
564 if (m_completedSendAtCurrentDiscardLevel || m_requestedPacketNumber>m_packetNumber)
565 {
566 m_packetNumber = m_requestedPacketNumber;
567 }
568
569 if (m_packetNumber <= m_stopPacket)
570 {
571 m_completedSendAtCurrentDiscardLevel = false;
572 }
573
574 }
575
576 }
577 }
578 }
579 }
580
581 }
582} 266}