J2KImage.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. using System;
  28. using System.Collections.Generic;
  29. using OpenMetaverse;
  30. using OpenMetaverse.Imaging;
  31. using OpenSim.Framework;
  32. using OpenSim.Region.Framework.Interfaces;
  33. using OpenSim.Services.Interfaces;
  34. using log4net;
  35. using System.Reflection;
  36. namespace OpenSim.Region.ClientStack.LindenUDP
  37. {
  38. /// <summary>
  39. /// We use this class to store image data and associated request data and attributes
  40. /// </summary>
  41. public class J2KImage
  42. {
  43. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. public double m_designatedPriorityKey;
  45. public double m_requestedPriority = 0.0d;
  46. public uint m_lastSequence = 0;
  47. public uint m_requestedPacketNumber;
  48. public sbyte m_requestedDiscardLevel;
  49. public UUID m_requestedUUID;
  50. public IJ2KDecoder m_j2kDecodeModule;
  51. public IAssetService m_assetCache;
  52. public OpenJPEG.J2KLayerInfo[] Layers = new OpenJPEG.J2KLayerInfo[0];
  53. public AssetBase m_MissingSubstitute = null;
  54. public bool m_decoded = false;
  55. public bool m_completedSendAtCurrentDiscardLevel;
  56. private sbyte m_discardLevel=-1;
  57. private uint m_packetNumber;
  58. private bool m_decoderequested = false;
  59. private bool m_hasasset = false;
  60. private bool m_asset_requested = false;
  61. private bool m_sentinfo = false;
  62. private uint m_stopPacket = 0;
  63. private const int cImagePacketSize = 1000;
  64. private const int cFirstPacketSize = 600;
  65. private AssetBase m_asset = null;
  66. private LLImageManager m_image;
  67. public J2KImage(LLImageManager image)
  68. {
  69. m_image = image;
  70. }
  71. public uint m_pPacketNumber
  72. {
  73. get { return m_packetNumber; }
  74. }
  75. public uint m_pStopPacketNumber
  76. {
  77. get { return m_stopPacket; }
  78. }
  79. public byte[] Data
  80. {
  81. get
  82. {
  83. if (m_asset != null)
  84. return m_asset.Data;
  85. else
  86. return null;
  87. }
  88. }
  89. public ushort TexturePacketCount()
  90. {
  91. if (!m_decoded)
  92. return 0;
  93. try
  94. {
  95. return (ushort)(((m_asset.Data.Length - cFirstPacketSize + cImagePacketSize - 1) / cImagePacketSize) + 1);
  96. }
  97. catch (Exception)
  98. {
  99. // If the asset is missing/destroyed/truncated, we will land
  100. // here
  101. //
  102. return 0;
  103. }
  104. }
  105. public void J2KDecodedCallback(UUID AssetId, OpenJPEG.J2KLayerInfo[] layers)
  106. {
  107. m_image.m_outstandingtextures++;
  108. Layers = layers;
  109. m_decoded = true;
  110. RunUpdate();
  111. }
  112. public void AssetDataCallback(UUID AssetID, AssetBase asset)
  113. {
  114. m_hasasset = true;
  115. if (asset == null || asset.Data == null)
  116. {
  117. m_asset = m_MissingSubstitute;
  118. }
  119. else
  120. {
  121. m_asset = asset;
  122. }
  123. RunUpdate();
  124. }
  125. protected void AssetReceived(string id, Object sender, AssetBase asset)
  126. {
  127. UUID assetID = UUID.Zero;
  128. if (asset != null)
  129. assetID = asset.FullID;
  130. AssetDataCallback(assetID, asset);
  131. }
  132. private int GetPacketForBytePosition(int bytePosition)
  133. {
  134. return ((bytePosition - cFirstPacketSize + cImagePacketSize - 1) / cImagePacketSize) + 1;
  135. }
  136. public int LastPacketSize()
  137. {
  138. if (m_packetNumber == 1)
  139. return m_asset.Data.Length;
  140. int lastsize = (m_asset.Data.Length - cFirstPacketSize) % cImagePacketSize;
  141. //If the last packet size is zero, it's really cImagePacketSize, it sits on the boundary
  142. if (lastsize == 0)
  143. {
  144. lastsize = cImagePacketSize;
  145. }
  146. return lastsize;
  147. }
  148. public int CurrentBytePosition()
  149. {
  150. if (m_packetNumber == 0)
  151. return 0;
  152. if (m_packetNumber == 1)
  153. return cFirstPacketSize;
  154. int result = cFirstPacketSize + ((int)m_packetNumber - 2) * cImagePacketSize;
  155. if (result < 0)
  156. {
  157. result = cFirstPacketSize;
  158. }
  159. return result;
  160. }
  161. public bool SendFirstPacket(LLClientView client)
  162. {
  163. // this means we don't have
  164. if (Data == null)
  165. {
  166. client.SendImageNotFound(m_requestedUUID);
  167. m_log.WarnFormat("[TEXTURE]: Got null Data element on a asset {0}.. and the missing image Data property is al", m_requestedUUID);
  168. return true;
  169. }
  170. // Do we have less then 1 packet's worth of data?
  171. else if (m_asset.Data.Length <= cFirstPacketSize)
  172. {
  173. // Send only 1 packet
  174. client.SendImageFirstPart(1, m_requestedUUID, (uint)m_asset.Data.Length, m_asset.Data, 2);
  175. m_stopPacket = 0;
  176. return true;
  177. }
  178. else
  179. {
  180. byte[] firstImageData = new byte[cFirstPacketSize];
  181. try
  182. {
  183. Buffer.BlockCopy(m_asset.Data, 0, firstImageData, 0, (int)cFirstPacketSize);
  184. client.SendImageFirstPart(TexturePacketCount(), m_requestedUUID, (uint)m_asset.Data.Length, firstImageData, 2);
  185. }
  186. catch (Exception)
  187. {
  188. m_log.Error("Texture block copy failed. Possibly out of memory?");
  189. return true;
  190. }
  191. }
  192. return false;
  193. }
  194. private bool SendPacket(LLClientView client)
  195. {
  196. bool complete = false;
  197. int imagePacketSize = ((int)m_packetNumber == (TexturePacketCount())) ? LastPacketSize() : cImagePacketSize;
  198. try
  199. {
  200. if ((CurrentBytePosition() + cImagePacketSize) > m_asset.Data.Length)
  201. {
  202. imagePacketSize = LastPacketSize();
  203. complete=true;
  204. if ((CurrentBytePosition() + imagePacketSize) > m_asset.Data.Length)
  205. {
  206. imagePacketSize = m_asset.Data.Length - CurrentBytePosition();
  207. complete = true;
  208. }
  209. }
  210. // It's concievable that the client might request packet one
  211. // from a one packet image, which is really packet 0,
  212. // which would leave us with a negative imagePacketSize..
  213. if (imagePacketSize > 0)
  214. {
  215. byte[] imageData = new byte[imagePacketSize];
  216. try
  217. {
  218. Buffer.BlockCopy(m_asset.Data, CurrentBytePosition(), imageData, 0, imagePacketSize);
  219. }
  220. catch (Exception e)
  221. {
  222. 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());
  223. return false;
  224. }
  225. //Send the packet
  226. client.SendImageNextPart((ushort)(m_packetNumber-1), m_requestedUUID, imageData);
  227. }
  228. if (complete)
  229. {
  230. return false;
  231. }
  232. else
  233. {
  234. return true;
  235. }
  236. }
  237. catch (Exception)
  238. {
  239. return false;
  240. }
  241. }
  242. public bool SendPackets(LLClientView client, int maxpack)
  243. {
  244. if (!m_completedSendAtCurrentDiscardLevel)
  245. {
  246. if (m_packetNumber <= m_stopPacket)
  247. {
  248. bool SendMore = true;
  249. if (!m_sentinfo || (m_packetNumber == 0))
  250. {
  251. if (SendFirstPacket(client))
  252. {
  253. SendMore = false;
  254. }
  255. m_sentinfo = true;
  256. m_packetNumber++;
  257. }
  258. // bool ignoreStop = false;
  259. if (m_packetNumber < 2)
  260. {
  261. m_packetNumber = 2;
  262. }
  263. int count = 0;
  264. while (SendMore && count < maxpack && m_packetNumber <= m_stopPacket)
  265. {
  266. count++;
  267. SendMore = SendPacket(client);
  268. m_packetNumber++;
  269. }
  270. if (m_packetNumber > m_stopPacket)
  271. {
  272. return true;
  273. }
  274. }
  275. }
  276. return false;
  277. }
  278. public void RunUpdate()
  279. {
  280. //This is where we decide what we need to update
  281. //and assign the real discardLevel and packetNumber
  282. //assuming of course that the connected client might be bonkers
  283. if (!m_hasasset)
  284. {
  285. if (!m_asset_requested)
  286. {
  287. m_asset_requested = true;
  288. m_assetCache.Get(m_requestedUUID.ToString(), this, AssetReceived);
  289. }
  290. }
  291. else
  292. {
  293. if (!m_decoded)
  294. {
  295. //We need to decode the requested image first
  296. if (!m_decoderequested)
  297. {
  298. //Request decode
  299. m_decoderequested = true;
  300. // Do we have a jpeg decoder?
  301. if (m_j2kDecodeModule != null)
  302. {
  303. if (Data == null)
  304. {
  305. J2KDecodedCallback(m_requestedUUID, new OpenJPEG.J2KLayerInfo[0]);
  306. }
  307. // Send it off to the jpeg decoder
  308. m_j2kDecodeModule.decode(m_requestedUUID, Data, J2KDecodedCallback);
  309. }
  310. else
  311. {
  312. J2KDecodedCallback(m_requestedUUID, new OpenJPEG.J2KLayerInfo[0]);
  313. }
  314. }
  315. }
  316. else
  317. {
  318. //discardLevel of -1 means just update the priority
  319. if (m_requestedDiscardLevel != -1)
  320. {
  321. //Evaluate the discard level
  322. //First, is it positive?
  323. if (m_requestedDiscardLevel >= 0)
  324. {
  325. if (m_requestedDiscardLevel > Layers.Length - 1)
  326. {
  327. m_discardLevel = (sbyte)(Layers.Length - 1);
  328. }
  329. else
  330. {
  331. m_discardLevel = m_requestedDiscardLevel;
  332. }
  333. //Calculate the m_stopPacket
  334. if (Layers.Length > 0)
  335. {
  336. m_stopPacket = (uint)GetPacketForBytePosition(Layers[(Layers.Length - 1) - m_discardLevel].End);
  337. //I don't know why, but the viewer seems to expect the final packet if the file
  338. //is just one packet bigger.
  339. if (TexturePacketCount() == m_stopPacket + 1)
  340. {
  341. m_stopPacket = TexturePacketCount();
  342. }
  343. }
  344. else
  345. {
  346. m_stopPacket = TexturePacketCount();
  347. }
  348. //Don't reset packet number unless we're waiting or it's ahead of us
  349. if (m_completedSendAtCurrentDiscardLevel || m_requestedPacketNumber>m_packetNumber)
  350. {
  351. m_packetNumber = m_requestedPacketNumber;
  352. }
  353. if (m_packetNumber <= m_stopPacket)
  354. {
  355. m_completedSendAtCurrentDiscardLevel = false;
  356. }
  357. }
  358. }
  359. else
  360. {
  361. m_packetNumber = m_stopPacket;
  362. }
  363. }
  364. }
  365. }
  366. }
  367. }