diff options
Diffstat (limited to 'OpenSim/Region/ClientStack/Linden/UDP/J2KImage.cs')
-rw-r--r-- | OpenSim/Region/ClientStack/Linden/UDP/J2KImage.cs | 401 |
1 files changed, 401 insertions, 0 deletions
diff --git a/OpenSim/Region/ClientStack/Linden/UDP/J2KImage.cs b/OpenSim/Region/ClientStack/Linden/UDP/J2KImage.cs new file mode 100644 index 0000000..9dd6663 --- /dev/null +++ b/OpenSim/Region/ClientStack/Linden/UDP/J2KImage.cs | |||
@@ -0,0 +1,401 @@ | |||
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 OpenSimulator 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 | |||
28 | using System; | ||
29 | using System.Collections.Generic; | ||
30 | using OpenMetaverse; | ||
31 | using OpenMetaverse.Imaging; | ||
32 | using OpenSim.Framework; | ||
33 | using OpenSim.Region.Framework.Interfaces; | ||
34 | using OpenSim.Services.Interfaces; | ||
35 | using log4net; | ||
36 | using System.Reflection; | ||
37 | |||
38 | namespace OpenSim.Region.ClientStack.LindenUDP | ||
39 | { | ||
40 | /// <summary> | ||
41 | /// Stores information about a current texture download and a reference to the texture asset | ||
42 | /// </summary> | ||
43 | public class J2KImage | ||
44 | { | ||
45 | private const int IMAGE_PACKET_SIZE = 1000; | ||
46 | private const int FIRST_PACKET_SIZE = 600; | ||
47 | |||
48 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
49 | |||
50 | public uint LastSequence; | ||
51 | public float Priority; | ||
52 | public uint StartPacket; | ||
53 | public sbyte DiscardLevel; | ||
54 | public UUID TextureID; | ||
55 | public IJ2KDecoder J2KDecoder; | ||
56 | public IAssetService AssetService; | ||
57 | public UUID AgentID; | ||
58 | public IInventoryAccessModule InventoryAccessModule; | ||
59 | public OpenJPEG.J2KLayerInfo[] Layers; | ||
60 | public bool IsDecoded; | ||
61 | public bool HasAsset; | ||
62 | public C5.IPriorityQueueHandle<J2KImage> PriorityQueueHandle; | ||
63 | |||
64 | private uint m_currentPacket; | ||
65 | private bool m_decodeRequested; | ||
66 | private bool m_assetRequested; | ||
67 | private bool m_sentInfo; | ||
68 | private uint m_stopPacket; | ||
69 | private byte[] m_asset; | ||
70 | private LLImageManager m_imageManager; | ||
71 | |||
72 | public J2KImage(LLImageManager imageManager) | ||
73 | { | ||
74 | m_imageManager = imageManager; | ||
75 | } | ||
76 | |||
77 | /// <summary> | ||
78 | /// Sends packets for this texture to a client until packetsToSend is | ||
79 | /// hit or the transfer completes | ||
80 | /// </summary> | ||
81 | /// <param name="client">Reference to the client that the packets are destined for</param> | ||
82 | /// <param name="packetsToSend">Maximum number of packets to send during this call</param> | ||
83 | /// <param name="packetsSent">Number of packets sent during this call</param> | ||
84 | /// <returns>True if the transfer completes at the current discard level, otherwise false</returns> | ||
85 | public bool SendPackets(LLClientView client, int packetsToSend, out int packetsSent) | ||
86 | { | ||
87 | packetsSent = 0; | ||
88 | |||
89 | if (m_currentPacket <= m_stopPacket) | ||
90 | { | ||
91 | bool sendMore = true; | ||
92 | |||
93 | if (!m_sentInfo || (m_currentPacket == 0)) | ||
94 | { | ||
95 | sendMore = !SendFirstPacket(client); | ||
96 | |||
97 | m_sentInfo = true; | ||
98 | ++m_currentPacket; | ||
99 | ++packetsSent; | ||
100 | } | ||
101 | if (m_currentPacket < 2) | ||
102 | { | ||
103 | m_currentPacket = 2; | ||
104 | } | ||
105 | |||
106 | while (sendMore && packetsSent < packetsToSend && m_currentPacket <= m_stopPacket) | ||
107 | { | ||
108 | sendMore = SendPacket(client); | ||
109 | ++m_currentPacket; | ||
110 | ++packetsSent; | ||
111 | } | ||
112 | } | ||
113 | |||
114 | return (m_currentPacket > m_stopPacket); | ||
115 | } | ||
116 | |||
117 | public void RunUpdate() | ||
118 | { | ||
119 | //This is where we decide what we need to update | ||
120 | //and assign the real discardLevel and packetNumber | ||
121 | //assuming of course that the connected client might be bonkers | ||
122 | |||
123 | if (!HasAsset) | ||
124 | { | ||
125 | if (!m_assetRequested) | ||
126 | { | ||
127 | m_assetRequested = true; | ||
128 | AssetService.Get(TextureID.ToString(), this, AssetReceived); | ||
129 | } | ||
130 | } | ||
131 | else | ||
132 | { | ||
133 | if (!IsDecoded) | ||
134 | { | ||
135 | //We need to decode the requested image first | ||
136 | if (!m_decodeRequested) | ||
137 | { | ||
138 | //Request decode | ||
139 | m_decodeRequested = true; | ||
140 | // Do we have a jpeg decoder? | ||
141 | if (J2KDecoder != null) | ||
142 | { | ||
143 | if (m_asset == null) | ||
144 | { | ||
145 | J2KDecodedCallback(TextureID, new OpenJPEG.J2KLayerInfo[0]); | ||
146 | } | ||
147 | else | ||
148 | { | ||
149 | // Send it off to the jpeg decoder | ||
150 | J2KDecoder.BeginDecode(TextureID, m_asset, J2KDecodedCallback); | ||
151 | } | ||
152 | |||
153 | } | ||
154 | else | ||
155 | { | ||
156 | J2KDecodedCallback(TextureID, new OpenJPEG.J2KLayerInfo[0]); | ||
157 | } | ||
158 | } | ||
159 | } | ||
160 | else | ||
161 | { | ||
162 | // Check for missing image asset data | ||
163 | if (m_asset == null) | ||
164 | { | ||
165 | m_log.Warn("[J2KIMAGE]: RunUpdate() called with missing asset data (no missing image texture?). Canceling texture transfer"); | ||
166 | m_currentPacket = m_stopPacket; | ||
167 | return; | ||
168 | } | ||
169 | |||
170 | if (DiscardLevel >= 0 || m_stopPacket == 0) | ||
171 | { | ||
172 | // This shouldn't happen, but if it does, we really can't proceed | ||
173 | if (Layers == null) | ||
174 | { | ||
175 | m_log.Warn("[J2KIMAGE]: RunUpdate() called with missing Layers. Canceling texture transfer"); | ||
176 | m_currentPacket = m_stopPacket; | ||
177 | return; | ||
178 | } | ||
179 | |||
180 | int maxDiscardLevel = Math.Max(0, Layers.Length - 1); | ||
181 | |||
182 | // Treat initial texture downloads with a DiscardLevel of -1 a request for the highest DiscardLevel | ||
183 | if (DiscardLevel < 0 && m_stopPacket == 0) | ||
184 | DiscardLevel = (sbyte)maxDiscardLevel; | ||
185 | |||
186 | // Clamp at the highest discard level | ||
187 | DiscardLevel = (sbyte)Math.Min(DiscardLevel, maxDiscardLevel); | ||
188 | |||
189 | //Calculate the m_stopPacket | ||
190 | if (Layers.Length > 0) | ||
191 | { | ||
192 | m_stopPacket = (uint)GetPacketForBytePosition(Layers[(Layers.Length - 1) - DiscardLevel].End); | ||
193 | //I don't know why, but the viewer seems to expect the final packet if the file | ||
194 | //is just one packet bigger. | ||
195 | if (TexturePacketCount() == m_stopPacket + 1) | ||
196 | { | ||
197 | m_stopPacket = TexturePacketCount(); | ||
198 | } | ||
199 | } | ||
200 | else | ||
201 | { | ||
202 | m_stopPacket = TexturePacketCount(); | ||
203 | } | ||
204 | |||
205 | //Give them at least two packets, to play nice with some broken viewers (SL also behaves this way) | ||
206 | if (m_stopPacket == 1 && Layers[0].End > FIRST_PACKET_SIZE) m_stopPacket++; | ||
207 | |||
208 | m_currentPacket = StartPacket; | ||
209 | } | ||
210 | } | ||
211 | } | ||
212 | } | ||
213 | |||
214 | private bool SendFirstPacket(LLClientView client) | ||
215 | { | ||
216 | if (client == null) | ||
217 | return false; | ||
218 | |||
219 | if (m_asset == null) | ||
220 | { | ||
221 | m_log.Warn("[J2KIMAGE]: Sending ImageNotInDatabase for texture " + TextureID); | ||
222 | client.SendImageNotFound(TextureID); | ||
223 | return true; | ||
224 | } | ||
225 | else if (m_asset.Length <= FIRST_PACKET_SIZE) | ||
226 | { | ||
227 | // We have less then one packet's worth of data | ||
228 | client.SendImageFirstPart(1, TextureID, (uint)m_asset.Length, m_asset, 2); | ||
229 | m_stopPacket = 0; | ||
230 | return true; | ||
231 | } | ||
232 | else | ||
233 | { | ||
234 | // This is going to be a multi-packet texture download | ||
235 | byte[] firstImageData = new byte[FIRST_PACKET_SIZE]; | ||
236 | |||
237 | try { Buffer.BlockCopy(m_asset, 0, firstImageData, 0, FIRST_PACKET_SIZE); } | ||
238 | catch (Exception) | ||
239 | { | ||
240 | m_log.ErrorFormat("[J2KIMAGE]: Texture block copy for the first packet failed. textureid={0}, assetlength={1}", TextureID, m_asset.Length); | ||
241 | return true; | ||
242 | } | ||
243 | |||
244 | client.SendImageFirstPart(TexturePacketCount(), TextureID, (uint)m_asset.Length, firstImageData, (byte)ImageCodec.J2C); | ||
245 | } | ||
246 | return false; | ||
247 | } | ||
248 | |||
249 | private bool SendPacket(LLClientView client) | ||
250 | { | ||
251 | if (client == null) | ||
252 | return false; | ||
253 | |||
254 | bool complete = false; | ||
255 | int imagePacketSize = ((int)m_currentPacket == (TexturePacketCount())) ? LastPacketSize() : IMAGE_PACKET_SIZE; | ||
256 | |||
257 | try | ||
258 | { | ||
259 | if ((CurrentBytePosition() + IMAGE_PACKET_SIZE) > m_asset.Length) | ||
260 | { | ||
261 | imagePacketSize = LastPacketSize(); | ||
262 | complete = true; | ||
263 | if ((CurrentBytePosition() + imagePacketSize) > m_asset.Length) | ||
264 | { | ||
265 | imagePacketSize = m_asset.Length - CurrentBytePosition(); | ||
266 | complete = true; | ||
267 | } | ||
268 | } | ||
269 | |||
270 | // It's concievable that the client might request packet one | ||
271 | // from a one packet image, which is really packet 0, | ||
272 | // which would leave us with a negative imagePacketSize.. | ||
273 | if (imagePacketSize > 0) | ||
274 | { | ||
275 | byte[] imageData = new byte[imagePacketSize]; | ||
276 | int currentPosition = CurrentBytePosition(); | ||
277 | |||
278 | try { Buffer.BlockCopy(m_asset, currentPosition, imageData, 0, imagePacketSize); } | ||
279 | catch (Exception e) | ||
280 | { | ||
281 | m_log.ErrorFormat("[J2KIMAGE]: Texture block copy for the first packet failed. textureid={0}, assetlength={1}, currentposition={2}, imagepacketsize={3}, exception={4}", | ||
282 | TextureID, m_asset.Length, currentPosition, imagePacketSize, e.Message); | ||
283 | return false; | ||
284 | } | ||
285 | |||
286 | //Send the packet | ||
287 | client.SendImageNextPart((ushort)(m_currentPacket - 1), TextureID, imageData); | ||
288 | } | ||
289 | |||
290 | return !complete; | ||
291 | } | ||
292 | catch (Exception) | ||
293 | { | ||
294 | return false; | ||
295 | } | ||
296 | } | ||
297 | |||
298 | private ushort TexturePacketCount() | ||
299 | { | ||
300 | if (!IsDecoded) | ||
301 | return 0; | ||
302 | |||
303 | if (m_asset == null) | ||
304 | return 0; | ||
305 | |||
306 | if (m_asset.Length <= FIRST_PACKET_SIZE) | ||
307 | return 1; | ||
308 | |||
309 | return (ushort)(((m_asset.Length - FIRST_PACKET_SIZE + IMAGE_PACKET_SIZE - 1) / IMAGE_PACKET_SIZE) + 1); | ||
310 | } | ||
311 | |||
312 | private int GetPacketForBytePosition(int bytePosition) | ||
313 | { | ||
314 | return ((bytePosition - FIRST_PACKET_SIZE + IMAGE_PACKET_SIZE - 1) / IMAGE_PACKET_SIZE) + 1; | ||
315 | } | ||
316 | |||
317 | private int LastPacketSize() | ||
318 | { | ||
319 | if (m_currentPacket == 1) | ||
320 | return m_asset.Length; | ||
321 | int lastsize = (m_asset.Length - FIRST_PACKET_SIZE) % IMAGE_PACKET_SIZE; | ||
322 | //If the last packet size is zero, it's really cImagePacketSize, it sits on the boundary | ||
323 | if (lastsize == 0) | ||
324 | { | ||
325 | lastsize = IMAGE_PACKET_SIZE; | ||
326 | } | ||
327 | return lastsize; | ||
328 | } | ||
329 | |||
330 | private int CurrentBytePosition() | ||
331 | { | ||
332 | if (m_currentPacket == 0) | ||
333 | return 0; | ||
334 | if (m_currentPacket == 1) | ||
335 | return FIRST_PACKET_SIZE; | ||
336 | |||
337 | int result = FIRST_PACKET_SIZE + ((int)m_currentPacket - 2) * IMAGE_PACKET_SIZE; | ||
338 | if (result < 0) | ||
339 | { | ||
340 | result = FIRST_PACKET_SIZE; | ||
341 | } | ||
342 | return result; | ||
343 | } | ||
344 | |||
345 | private void J2KDecodedCallback(UUID AssetId, OpenJPEG.J2KLayerInfo[] layers) | ||
346 | { | ||
347 | Layers = layers; | ||
348 | IsDecoded = true; | ||
349 | RunUpdate(); | ||
350 | } | ||
351 | |||
352 | private void AssetDataCallback(UUID AssetID, AssetBase asset) | ||
353 | { | ||
354 | HasAsset = true; | ||
355 | |||
356 | if (asset == null || asset.Data == null) | ||
357 | { | ||
358 | if (m_imageManager.MissingImage != null) | ||
359 | { | ||
360 | m_asset = m_imageManager.MissingImage.Data; | ||
361 | } | ||
362 | else | ||
363 | { | ||
364 | m_asset = null; | ||
365 | IsDecoded = true; | ||
366 | } | ||
367 | } | ||
368 | else | ||
369 | { | ||
370 | m_asset = asset.Data; | ||
371 | } | ||
372 | |||
373 | RunUpdate(); | ||
374 | } | ||
375 | |||
376 | private void AssetReceived(string id, Object sender, AssetBase asset) | ||
377 | { | ||
378 | UUID assetID = UUID.Zero; | ||
379 | if (asset != null) | ||
380 | assetID = asset.FullID; | ||
381 | else if ((InventoryAccessModule != null) && (sender != InventoryAccessModule)) | ||
382 | { | ||
383 | // Unfortunately we need this here, there's no other way. | ||
384 | // This is due to the fact that textures opened directly from the agent's inventory | ||
385 | // don't have any distinguishing feature. As such, in order to serve those when the | ||
386 | // foreign user is visiting, we need to try again after the first fail to the local | ||
387 | // asset service. | ||
388 | string assetServerURL = string.Empty; | ||
389 | if (InventoryAccessModule.IsForeignUser(AgentID, out assetServerURL)) | ||
390 | { | ||
391 | m_log.DebugFormat("[J2KIMAGE]: texture {0} not found in local asset storage. Trying user's storage.", id); | ||
392 | AssetService.Get(assetServerURL + "/" + id, InventoryAccessModule, AssetReceived); | ||
393 | return; | ||
394 | } | ||
395 | } | ||
396 | |||
397 | AssetDataCallback(assetID, asset); | ||
398 | |||
399 | } | ||
400 | } | ||
401 | } | ||