llaudiodecodemgr.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. /**
  2. * @file llaudiodecodemgr.cpp
  3. *
  4. * $LicenseInfo:firstyear=2003&license=viewergpl$
  5. *
  6. * Copyright (c) 2003-2009, Linden Research, Inc.
  7. *
  8. * Second Life Viewer Source Code
  9. * The source code in this file ("Source Code") is provided by Linden Lab
  10. * to you under the terms of the GNU General Public License, version 2.0
  11. * ("GPL"), unless you have obtained a separate licensing agreement
  12. * ("Other License"), formally executed by you and Linden Lab. Terms of
  13. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  14. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  15. *
  16. * There are special exceptions to the terms and conditions of the GPL as
  17. * it is applied to this Source Code. View the full text of the exception
  18. * in the file doc/FLOSS-exception.txt in this software distribution, or
  19. * online at
  20. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  21. *
  22. * By copying, modifying or distributing this software, you acknowledge
  23. * that you have read and understood your obligations described above,
  24. * and agree to abide by those obligations.
  25. *
  26. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  27. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  28. * COMPLETENESS OR PERFORMANCE.
  29. * $/LicenseInfo$
  30. */
  31. #include "linden_common.h"
  32. #include <iterator> // for VS2010
  33. #include <deque>
  34. #include "llaudiodecodemgr.h"
  35. #include "llapp.h"
  36. #include "llassetstorage.h"
  37. #include "llaudioengine.h"
  38. #include "lldir.h"
  39. #include "llendianswizzle.h"
  40. #include "llfilesystem.h"
  41. #include "llmemory.h"
  42. #include "llrefcount.h"
  43. #include "llstring.h"
  44. #include "llworkqueue.h"
  45. #include "llvorbisencode.h"
  46. #include "vorbis/codec.h"
  47. #include "vorbis/vorbisfile.h"
  48. extern LLAudioEngine* gAudiop;
  49. LLAudioDecodeMgr* gAudioDecodeMgrp = NULL;
  50. U32 LLAudioDecodeMgr::sMaxDecodes = 0;
  51. constexpr S32 WAV_HEADER_SIZE = 44;
  52. //////////////////////////////////////////////////////////////////////////////
  53. class LLVorbisDecodeState : public LLThreadSafeRefCount
  54. {
  55. protected:
  56. LOG_CLASS(LLVorbisDecodeState);
  57. virtual ~LLVorbisDecodeState();
  58. public:
  59. typedef LLPointer<LLVorbisDecodeState> ptr_t;
  60. LLVorbisDecodeState(const LLUUID& id, const std::string& out_filename);
  61. bool initDecode();
  62. bool decodeSection(); // Return true if done.
  63. void finishDecode();
  64. LL_INLINE bool isValid() const { return mValid; }
  65. LL_INLINE bool isDone() const { return mDone; }
  66. LL_INLINE bool isWritten() const { return mWritten; }
  67. LL_INLINE const LLUUID& getUUID() const { return mUUID; }
  68. private:
  69. void flushBadFile();
  70. protected:
  71. LLFileSystem* mInFilep;
  72. std::string mOutFilename;
  73. std::vector<U8> mWAVBuffer;
  74. LLUUID mUUID;
  75. OggVorbis_File mVF;
  76. S32 mCurrentSection;
  77. bool mValid;
  78. bool mDone;
  79. bool mWritten;
  80. };
  81. static size_t cache_read(void* ptr, size_t size, size_t nmemb, void* userdata)
  82. {
  83. if (size <= 0 || nmemb <= 0) return 0;
  84. LLFileSystem* file = (LLFileSystem*)userdata;
  85. if (file->read((U8*)ptr, (S32)(size * nmemb)))
  86. {
  87. S32 read = file->getLastBytesRead();
  88. return read / size;
  89. }
  90. return 0;
  91. }
  92. static S32 cache_seek(void* userdata, ogg_int64_t offset, S32 whence)
  93. {
  94. // Cache has 31 bits files
  95. if (offset > S32_MAX)
  96. {
  97. return -1;
  98. }
  99. LLFileSystem* file = (LLFileSystem*)userdata;
  100. S32 origin = 0;
  101. switch (whence)
  102. {
  103. case SEEK_SET:
  104. break;
  105. case SEEK_END:
  106. origin = file->getSize();
  107. break;
  108. case SEEK_CUR:
  109. origin = -1;
  110. break;
  111. default:
  112. llerrs << "Invalid whence argument" << llendl;
  113. }
  114. return file->seek((S32)offset, origin) ? 0 : -1;
  115. }
  116. static long cache_tell(void* userdata)
  117. {
  118. return ((LLFileSystem*)userdata)->tell();
  119. }
  120. LLVorbisDecodeState::LLVorbisDecodeState(const LLUUID& id,
  121. const std::string& out_filename)
  122. : mUUID(id),
  123. mOutFilename(out_filename),
  124. mInFilep(NULL),
  125. mCurrentSection(0),
  126. mDone(false),
  127. mValid(false),
  128. mWritten(false)
  129. {
  130. // No default value for mVF, is it an OGG structure ?
  131. }
  132. LLVorbisDecodeState::~LLVorbisDecodeState()
  133. {
  134. if (mInFilep)
  135. {
  136. delete mInFilep;
  137. // The fact that mInFilep is not NULL is a sign that mVF got
  138. // initialized via ov_open_callbacks(), so we must clear it. HB
  139. ov_clear(&mVF);
  140. }
  141. }
  142. void LLVorbisDecodeState::flushBadFile()
  143. {
  144. if (mInFilep)
  145. {
  146. llwarns << "Removing bad (invalid vorbis data) cache file for asset: "
  147. << mUUID << llendl;
  148. mInFilep->remove();
  149. }
  150. }
  151. bool LLVorbisDecodeState::initDecode()
  152. {
  153. ov_callbacks cache_callbacks;
  154. cache_callbacks.read_func = cache_read;
  155. cache_callbacks.seek_func = cache_seek;
  156. // We manage mInFilep life ourselves ! HB
  157. cache_callbacks.close_func = NULL;
  158. cache_callbacks.tell_func = cache_tell;
  159. mInFilep = new LLFileSystem(mUUID);
  160. if (!mInFilep)
  161. {
  162. llwarns << "Unable to open cache file for reading asset: " << mUUID
  163. << llendl;
  164. return false;
  165. }
  166. if (!mInFilep->getSize())
  167. {
  168. llwarns << "Empty cache file for asset: " << mUUID << llendl;
  169. flushBadFile();
  170. // This ensures we will not attempt to clear mVF in the destructor,
  171. // since it was not allocated via ov_open_callbacks() below. HB
  172. delete mInFilep;
  173. mInFilep = NULL;
  174. return false;
  175. }
  176. S32 r = ov_open_callbacks(mInFilep, &mVF, NULL, 0, cache_callbacks);
  177. if (r < 0)
  178. {
  179. llwarns << "Error " << r
  180. << " while opening Vorbis data streal for asset " << mUUID
  181. << ". This does not appear to be an Ogg bitstream." << llendl;
  182. flushBadFile();
  183. return false;
  184. }
  185. S32 sample_count = ov_pcm_total(&mVF, -1);
  186. size_t size_guess = (size_t)sample_count;
  187. vorbis_info* vi = ov_info(&mVF, -1);
  188. size_guess *= vi->channels;
  189. size_guess *= 2;
  190. size_guess += 2048;
  191. bool abort_decode = false;
  192. if (vi->channels < 1 || vi->channels > LLVORBIS_CLIP_MAX_CHANNELS)
  193. {
  194. abort_decode = true;
  195. llwarns << "Bad channel count: " << vi->channels << llendl;
  196. }
  197. if ((size_t)sample_count > LLVORBIS_CLIP_REJECT_SAMPLES)
  198. {
  199. abort_decode = true;
  200. llwarns << "Illegal sample count: " << sample_count << llendl;
  201. }
  202. if (size_guess > LLVORBIS_CLIP_REJECT_SIZE)
  203. {
  204. abort_decode = true;
  205. llwarns << "Illegal sample size: " << size_guess << llendl;
  206. }
  207. if (abort_decode)
  208. {
  209. llwarns << "Cancelling initDecode. Bad asset: " << mUUID
  210. << "Bad asset encoded by: "
  211. << ov_comment(&mVF, -1)->vendor << llendl;
  212. flushBadFile();
  213. return false;
  214. }
  215. try
  216. {
  217. mWAVBuffer.reserve(size_guess);
  218. mWAVBuffer.resize(WAV_HEADER_SIZE);
  219. }
  220. catch (const std::bad_alloc&)
  221. {
  222. llwarns << "Failure to allocate buffer for asset: " << mUUID << llendl;
  223. LLMemory::allocationFailed(size_guess);
  224. return false;
  225. }
  226. // Write the .wav format header
  227. // "RIFF"
  228. mWAVBuffer[0] = 0x52;
  229. mWAVBuffer[1] = 0x49;
  230. mWAVBuffer[2] = 0x46;
  231. mWAVBuffer[3] = 0x46;
  232. // Length = datalen + 36 (to be filled in later)
  233. mWAVBuffer[4] = 0x00;
  234. mWAVBuffer[5] = 0x00;
  235. mWAVBuffer[6] = 0x00;
  236. mWAVBuffer[7] = 0x00;
  237. // "WAVE"
  238. mWAVBuffer[8] = 0x57;
  239. mWAVBuffer[9] = 0x41;
  240. mWAVBuffer[10] = 0x56;
  241. mWAVBuffer[11] = 0x45;
  242. // "fmt "
  243. mWAVBuffer[12] = 0x66;
  244. mWAVBuffer[13] = 0x6D;
  245. mWAVBuffer[14] = 0x74;
  246. mWAVBuffer[15] = 0x20;
  247. // Chunk size = 16
  248. mWAVBuffer[16] = 0x10;
  249. mWAVBuffer[17] = 0x00;
  250. mWAVBuffer[18] = 0x00;
  251. mWAVBuffer[19] = 0x00;
  252. // Format (1 = PCM)
  253. mWAVBuffer[20] = 0x01;
  254. mWAVBuffer[21] = 0x00;
  255. // Number of channels
  256. mWAVBuffer[22] = 0x01;
  257. mWAVBuffer[23] = 0x00;
  258. // Samples per second
  259. mWAVBuffer[24] = 0x44;
  260. mWAVBuffer[25] = 0xAC;
  261. mWAVBuffer[26] = 0x00;
  262. mWAVBuffer[27] = 0x00;
  263. // Average bytes per second
  264. mWAVBuffer[28] = 0x88;
  265. mWAVBuffer[29] = 0x58;
  266. mWAVBuffer[30] = 0x01;
  267. mWAVBuffer[31] = 0x00;
  268. // Bytes to output at a single time
  269. mWAVBuffer[32] = 0x02;
  270. mWAVBuffer[33] = 0x00;
  271. // 16 bits per sample
  272. mWAVBuffer[34] = 0x10;
  273. mWAVBuffer[35] = 0x00;
  274. // "data"
  275. mWAVBuffer[36] = 0x64;
  276. mWAVBuffer[37] = 0x61;
  277. mWAVBuffer[38] = 0x74;
  278. mWAVBuffer[39] = 0x61;
  279. // These are the length of the data chunk, to be filled in later
  280. mWAVBuffer[40] = 0x00;
  281. mWAVBuffer[41] = 0x00;
  282. mWAVBuffer[42] = 0x00;
  283. mWAVBuffer[43] = 0x00;
  284. #if 0
  285. char** ptr = ov_comment(&mVF, -1)->user_comments;
  286. vorbis_info* vi = ov_info(&vf, -1);
  287. while (*ptr)
  288. {
  289. fprintf(stderr,"%s\n", *ptr++);
  290. }
  291. fprintf(stderr, "\nBitstream is %d channel, %ldHz\n", vi->channels,
  292. vi->rate);
  293. fprintf(stderr, "\nDecoded length: %ld samples\n",
  294. (long)ov_pcm_total(&vf, -1));
  295. fprintf(stderr, "Encoded by: %s\n\n", ov_comment(&vf, -1)->vendor);
  296. #endif
  297. return true;
  298. }
  299. bool LLVorbisDecodeState::decodeSection()
  300. {
  301. if (!mInFilep)
  302. {
  303. llwarns << "No cache file to decode for " << mUUID << llendl;
  304. return true;
  305. }
  306. if (mDone)
  307. {
  308. LL_DEBUGS("Audio") << "Already done with decode for " << mUUID
  309. << LL_ENDL;
  310. return true;
  311. }
  312. char pcmout[4096];
  313. long ret = ov_read(&mVF, pcmout, sizeof(pcmout), 0, 2, 1,
  314. &mCurrentSection);
  315. if (ret == 0)
  316. {
  317. // EOF
  318. mDone = true;
  319. mValid = true;
  320. // We are done, return true.
  321. return true;
  322. }
  323. if (ret < 0)
  324. {
  325. // Error in the stream. Not a problem, just reporting it in case we
  326. // (the app) cares. In this case, we do not.
  327. llwarns << "Bad vorbis decode for" << mUUID << llendl;
  328. mValid = false;
  329. mDone = true;
  330. flushBadFile();
  331. // We are done, return true.
  332. return true;
  333. }
  334. // We do not bother dealing with sample rate changes, etc, but you will
  335. // have to
  336. std::copy(pcmout, pcmout + ret, std::back_inserter(mWAVBuffer));
  337. // We are not yet done, return false.
  338. return false;
  339. }
  340. void LLVorbisDecodeState::finishDecode()
  341. {
  342. // Write "data" chunk length, in little-endian format
  343. S32 data_length = mWAVBuffer.size() - WAV_HEADER_SIZE;
  344. mWAVBuffer[40] = (data_length) & 0x000000FF;
  345. mWAVBuffer[41] = (data_length >> 8) & 0x000000FF;
  346. mWAVBuffer[42] = (data_length >> 16) & 0x000000FF;
  347. mWAVBuffer[43] = (data_length >> 24) & 0x000000FF;
  348. // Write overall "RIFF" length, in little-endian format
  349. data_length += 36;
  350. mWAVBuffer[4] = (data_length) & 0x000000FF;
  351. mWAVBuffer[5] = (data_length >> 8) & 0x000000FF;
  352. mWAVBuffer[6] = (data_length >> 16) & 0x000000FF;
  353. mWAVBuffer[7] = (data_length >> 24) & 0x000000FF;
  354. // Vorbis encode/decode messes up loop point transitions (pop)do a
  355. // cheap-and-cheesy crossfade
  356. S16* samplep;
  357. char pcmout[4096];
  358. S32 fade_length = llmin(128, (data_length - 36) / 8);
  359. if ((S32)mWAVBuffer.size() >= WAV_HEADER_SIZE + 2 * fade_length)
  360. {
  361. memcpy(pcmout, &mWAVBuffer[WAV_HEADER_SIZE], 2 * fade_length);
  362. }
  363. llendianswizzle(&pcmout, 2, fade_length);
  364. samplep = (S16*)pcmout;
  365. for (S32 i = 0 ; i < fade_length; ++i)
  366. {
  367. *samplep = llfloor(F32(*samplep) * (F32)i / (F32)fade_length);
  368. ++samplep;
  369. }
  370. llendianswizzle(&pcmout, 2, fade_length);
  371. if (WAV_HEADER_SIZE + 2 * fade_length < (S32)mWAVBuffer.size())
  372. {
  373. memcpy(&mWAVBuffer[WAV_HEADER_SIZE], pcmout, 2 * fade_length);
  374. }
  375. S32 near_end = mWAVBuffer.size() - 2 * fade_length;
  376. if ((S32)mWAVBuffer.size() >= near_end + 2 * fade_length)
  377. {
  378. memcpy(pcmout, &mWAVBuffer[near_end], (2 * fade_length));
  379. }
  380. llendianswizzle(&pcmout, 2, fade_length);
  381. samplep = (S16*)pcmout;
  382. for (S32 i = fade_length - 1; i >= 0; --i)
  383. {
  384. *samplep = llfloor(F32(*samplep) * (F32)i / (F32)fade_length);
  385. ++samplep;
  386. }
  387. llendianswizzle(&pcmout, 2, fade_length);
  388. if (near_end + 2 * fade_length < (S32)mWAVBuffer.size())
  389. {
  390. memcpy(&mWAVBuffer[near_end], pcmout, 2 * fade_length);
  391. }
  392. if (data_length == 36)
  393. {
  394. llwarns_once << "Bad Vorbis decode for " << mUUID << ", aborting."
  395. << llendl;
  396. mValid = false;
  397. flushBadFile();
  398. return; // We have finished
  399. }
  400. LL_DEBUGS("Audio") << "Starting file write for " << mUUID << LL_ENDL;
  401. LLFile oufile(mOutFilename, "wb");
  402. S64 file_size = mWAVBuffer.size();
  403. if (oufile.write(mWAVBuffer.data(), file_size) == file_size)
  404. {
  405. mDone = true;
  406. LL_DEBUGS("Audio") << "Decoded file written for " << mUUID
  407. << LL_ENDL;
  408. }
  409. else
  410. {
  411. llwarns << "Unable to write file for " << mUUID << llendl;
  412. mValid = false;
  413. }
  414. mWritten = true;
  415. }
  416. //////////////////////////////////////////////////////////////////////////////
  417. class LLAudioDecodeMgr::Impl
  418. {
  419. friend class LLAudioDecodeMgr;
  420. protected:
  421. LOG_CLASS(LLAudioDecodeMgr::Impl);
  422. public:
  423. Impl() = default;
  424. // Called from the main thread only.
  425. LL_INLINE void processQueue()
  426. {
  427. checkDecodesFinished();
  428. startMoreDecodes();
  429. }
  430. protected:
  431. void enqueueFinishAudio(const LLUUID& id,
  432. LLVorbisDecodeState::ptr_t& state);
  433. private:
  434. // Starts as many decodes from the queue as permitted.
  435. void startMoreDecodes();
  436. // Checks if any audio from in-progress decodes are ready to play. If so,
  437. // mark them ready for playback (or errored, in case of error).
  438. void checkDecodesFinished();
  439. // Returns the in-progress decode state, which may be an empty LLPointer if
  440. // there was an error and there is no more work to be done.
  441. LLVorbisDecodeState::ptr_t beginDecode(const LLUUID& id);
  442. // Returns true if finished.
  443. bool tryFinishAudio(const LLUUID& id, LLVorbisDecodeState::ptr_t state);
  444. protected:
  445. std::deque<LLUUID> mDecodeQueue;
  446. typedef fast_hmap<LLUUID, LLVorbisDecodeState::ptr_t> decodes_map_t;
  447. decodes_map_t mDecodes;
  448. uuid_list_t mBadAssetList;
  449. };
  450. // Called from the main thread only.
  451. void LLAudioDecodeMgr::Impl::startMoreDecodes()
  452. {
  453. if (!sMaxDecodes)
  454. {
  455. // At this point the "General" queue has not not yet been initialized.
  456. // Bail out to avoid storing an empty static weak pointer below ! HB
  457. LL_DEBUGS("Audio") << "General queue not yet ready. Aborting."
  458. << LL_ENDL;
  459. return;
  460. }
  461. if (LLApp::isExiting())
  462. {
  463. return;
  464. }
  465. static LLWorkQueue::weak_t main_queue =
  466. LLWorkQueue::getNamedInstance("mainloop");
  467. static LLWorkQueue::weak_t general_queue =
  468. LLWorkQueue::getNamedInstance("General");
  469. auto mainq = main_queue.lock();
  470. if (!mainq)
  471. {
  472. LL_DEBUGS("Audio") << "Main queue is gone ! Aborting." << LL_ENDL;
  473. return; // Queue is gone !
  474. }
  475. while (gAudioDecodeMgrp && !LLApp::isExiting() && !mDecodeQueue.empty())
  476. {
  477. const LLUUID id = mDecodeQueue.front();
  478. mDecodeQueue.pop_front();
  479. if (mDecodes.count(id) || (gAudiop && gAudiop->hasDecodedFile(id)))
  480. {
  481. // Do not decode the same file twice
  482. LL_DEBUGS("Audio") << id
  483. << " is already decoded or queued for decoding."
  484. << LL_ENDL;
  485. continue;
  486. }
  487. // Kick off a decode
  488. mDecodes.emplace(id, LLVorbisDecodeState::ptr_t(NULL));
  489. if (mainq->isClosed() ||
  490. !mainq->postTo(general_queue,
  491. // Work done on general queue
  492. [id, this]()
  493. {
  494. LLVorbisDecodeState::ptr_t state =
  495. beginDecode(id);
  496. return state;
  497. },
  498. // Callback to main thread
  499. [id, this](LLVorbisDecodeState::ptr_t state) mutable
  500. {
  501. // Let's ensure 'this' is still valid... HB
  502. if (gAudioDecodeMgrp)
  503. {
  504. enqueueFinishAudio(id, state);
  505. }
  506. }))
  507. {
  508. LL_DEBUGS("Audio") << "Failed to post decode for " << id
  509. << LL_ENDL;
  510. break;
  511. }
  512. LL_DEBUGS("Audio") << "Posted decode to \"General\" queue for " << id
  513. << LL_ENDL;
  514. if (mDecodes.size() > sMaxDecodes)
  515. {
  516. LL_DEBUGS("Audio") << "Decodes queue is full ("
  517. << mDecodes.size() << "/" << sMaxDecodes
  518. << ")" << LL_ENDL;
  519. break;
  520. }
  521. }
  522. }
  523. // Called from worker thread only.
  524. LLVorbisDecodeState::ptr_t LLAudioDecodeMgr::Impl::beginDecode(const LLUUID& id)
  525. {
  526. LL_DEBUGS("Audio") << "Decoding " << id << " from audio queue." << LL_ENDL;
  527. std::string d_path = gDirUtil.getFullPath(LL_PATH_CACHE, id.asString()) +
  528. ".dsf";
  529. LLVorbisDecodeState::ptr_t state = new LLVorbisDecodeState(id, d_path);
  530. if (!state->initDecode())
  531. {
  532. mBadAssetList.emplace(state->getUUID());
  533. return NULL;
  534. }
  535. // Decode in a loop until we are done
  536. while (!state->decodeSection()) ;
  537. if (!state->isDone() || !state->isValid())
  538. {
  539. // Decode stopped early, or something bad happened to the file during
  540. // decoding.
  541. mBadAssetList.emplace(state->getUUID());
  542. return NULL;
  543. }
  544. // Write of the decoded audio to the disk cache.
  545. state->finishDecode();
  546. return state;
  547. }
  548. // Called from the main thread only, with the result from the worker thread.
  549. void LLAudioDecodeMgr::Impl::enqueueFinishAudio(const LLUUID& id,
  550. LLVorbisDecodeState::ptr_t& state)
  551. {
  552. if (tryFinishAudio(id, state))
  553. {
  554. // Done early or aborted.
  555. llassert(mDecodes.count(id));
  556. mDecodes.erase(id);
  557. LL_DEBUGS("Audio") << "Finished decode of " << id
  558. << " - Decodes queue size = " << mDecodes.size()
  559. << LL_ENDL;
  560. return;
  561. }
  562. // Not done; enqueue it.
  563. mDecodes[id] = state;
  564. LL_DEBUGS("Audio") << "Enqueued decode for " << id
  565. << " - Decodes queue size = " << mDecodes.size()
  566. << LL_ENDL;
  567. }
  568. // Called from the main thread only.
  569. bool LLAudioDecodeMgr::Impl::tryFinishAudio(const LLUUID& id,
  570. LLVorbisDecodeState::ptr_t state)
  571. {
  572. if (!gAudiop || !(state && state->isWritten()))
  573. {
  574. return false;
  575. }
  576. LLAudioData* adp = gAudiop->getAudioData(id);
  577. if (!adp)
  578. {
  579. llwarns << "Missing audio data for " << id << llendl;
  580. return true;
  581. }
  582. bool valid = state && state->isValid();
  583. // Mark current decode finished regardless of success or failure
  584. adp->setHasCompletedDecode(true);
  585. // Flip flags for decoded data
  586. adp->setHasDecodeFailed(!valid);
  587. adp->setHasDecodedData(valid);
  588. // When finished decoding, there will also be a decoded wav file cached on
  589. // disk with the .dsf extension
  590. if (valid)
  591. {
  592. LL_DEBUGS("Audio") << "Valid decoded data for " << id << LL_ENDL;
  593. adp->setHasWAVLoadFailed(false);
  594. }
  595. return true;
  596. }
  597. // Called from the main thread only.
  598. void LLAudioDecodeMgr::Impl::checkDecodesFinished()
  599. {
  600. decodes_map_t::iterator it = mDecodes.begin();
  601. while (it != mDecodes.end())
  602. {
  603. const LLUUID& id = it->first;
  604. const LLVorbisDecodeState::ptr_t& state = it->second;
  605. if (tryFinishAudio(id, state))
  606. {
  607. it = mDecodes.erase(it);
  608. LL_DEBUGS("Audio") << "Finished decode of " << id
  609. << " - Decodes queue size = " << mDecodes.size()
  610. << LL_ENDL;
  611. }
  612. else
  613. {
  614. ++it;
  615. }
  616. }
  617. }
  618. //////////////////////////////////////////////////////////////////////////////
  619. LLAudioDecodeMgr::LLAudioDecodeMgr()
  620. : mImpl(new Impl)
  621. {
  622. }
  623. LLAudioDecodeMgr::~LLAudioDecodeMgr()
  624. {
  625. delete mImpl;
  626. }
  627. void LLAudioDecodeMgr::processQueue()
  628. {
  629. mImpl->processQueue();
  630. }
  631. bool LLAudioDecodeMgr::addDecodeRequest(const LLUUID& id)
  632. {
  633. if (mImpl->mBadAssetList.count(id))
  634. {
  635. // Do not try to decode identified corrupted assets.
  636. return false;
  637. }
  638. if (gAudiop && gAudiop->hasDecodedFile(id))
  639. {
  640. // Already have a decoded version, we do not need to decode it.
  641. return true;
  642. }
  643. if (gAssetStoragep &&
  644. gAssetStoragep->hasLocalAsset(id, LLAssetType::AT_SOUND))
  645. {
  646. // Just put it on the decode queue if not already there.
  647. std::deque<LLUUID>::iterator end = mImpl->mDecodeQueue.end();
  648. if (std::find(mImpl->mDecodeQueue.begin(), end, id) == end)
  649. {
  650. mImpl->mDecodeQueue.emplace_back(id);
  651. }
  652. return true;
  653. }
  654. return false;
  655. }