Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

juce_ContentSharer.cpp 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. #if JUCE_CONTENT_SHARING
  21. //==============================================================================
  22. class ContentSharer::PrepareImagesThread : private Thread
  23. {
  24. public:
  25. PrepareImagesThread (ContentSharer& cs, const Array<Image>& imagesToUse,
  26. ImageFileFormat* imageFileFormatToUse)
  27. : Thread ("ContentSharer::PrepareImagesThread"),
  28. owner (cs),
  29. images (imagesToUse),
  30. imageFileFormat (imageFileFormatToUse == nullptr ? new PNGImageFormat()
  31. : imageFileFormatToUse),
  32. extension (imageFileFormat->getFormatName().toLowerCase())
  33. {
  34. startThread();
  35. }
  36. ~PrepareImagesThread() override
  37. {
  38. signalThreadShouldExit();
  39. waitForThreadToExit (10000);
  40. }
  41. private:
  42. void run() override
  43. {
  44. for (const auto& image : images)
  45. {
  46. if (threadShouldExit())
  47. return;
  48. File tempFile = File::createTempFile (extension);
  49. if (! tempFile.create().wasOk())
  50. break;
  51. std::unique_ptr<FileOutputStream> outputStream (tempFile.createOutputStream());
  52. if (outputStream == nullptr)
  53. break;
  54. if (imageFileFormat->writeImageToStream (image, *outputStream))
  55. owner.temporaryFiles.add (tempFile);
  56. }
  57. finish();
  58. }
  59. void finish()
  60. {
  61. MessageManager::callAsync ([this]() { owner.filesToSharePrepared(); });
  62. }
  63. ContentSharer& owner;
  64. const Array<Image> images;
  65. std::unique_ptr<ImageFileFormat> imageFileFormat;
  66. String extension;
  67. };
  68. //==============================================================================
  69. class ContentSharer::PrepareDataThread : private Thread
  70. {
  71. public:
  72. PrepareDataThread (ContentSharer& cs, const MemoryBlock& mb)
  73. : Thread ("ContentSharer::PrepareDataThread"),
  74. owner (cs),
  75. data (mb)
  76. {
  77. startThread();
  78. }
  79. ~PrepareDataThread() override
  80. {
  81. signalThreadShouldExit();
  82. waitForThreadToExit (10000);
  83. }
  84. private:
  85. void run() override
  86. {
  87. File tempFile = File::createTempFile ("data");
  88. if (tempFile.create().wasOk())
  89. {
  90. if (auto outputStream = std::unique_ptr<FileOutputStream> (tempFile.createOutputStream()))
  91. {
  92. size_t pos = 0;
  93. size_t totalSize = data.getSize();
  94. while (pos < totalSize)
  95. {
  96. if (threadShouldExit())
  97. return;
  98. size_t numToWrite = std::min ((size_t) 8192, totalSize - pos);
  99. outputStream->write (data.begin() + pos, numToWrite);
  100. pos += numToWrite;
  101. }
  102. owner.temporaryFiles.add (tempFile);
  103. }
  104. }
  105. finish();
  106. }
  107. void finish()
  108. {
  109. MessageManager::callAsync ([this]() { owner.filesToSharePrepared(); });
  110. }
  111. ContentSharer& owner;
  112. const MemoryBlock data;
  113. };
  114. #endif
  115. //==============================================================================
  116. JUCE_IMPLEMENT_SINGLETON (ContentSharer)
  117. ContentSharer::ContentSharer() {}
  118. ContentSharer::~ContentSharer() { clearSingletonInstance(); }
  119. void ContentSharer::shareFiles (const Array<URL>& files,
  120. std::function<void (bool, const String&)> callbackToUse)
  121. {
  122. #if JUCE_CONTENT_SHARING
  123. startNewShare (callbackToUse);
  124. pimpl->shareFiles (files);
  125. #else
  126. ignoreUnused (files);
  127. // Content sharing is not available on this platform!
  128. jassertfalse;
  129. if (callbackToUse)
  130. callbackToUse (false, "Content sharing is not available on this platform!");
  131. #endif
  132. }
  133. #if JUCE_CONTENT_SHARING
  134. void ContentSharer::startNewShare (std::function<void (bool, const String&)> callbackToUse)
  135. {
  136. // You should not start another sharing operation before the previous one is finished.
  137. // Forcibly stopping a previous sharing operation is rarely a good idea!
  138. jassert (pimpl == nullptr);
  139. pimpl.reset();
  140. prepareDataThread = nullptr;
  141. prepareImagesThread = nullptr;
  142. deleteTemporaryFiles();
  143. // You need to pass a valid callback.
  144. jassert (callbackToUse);
  145. callback = std::move (callbackToUse);
  146. pimpl.reset (createPimpl());
  147. }
  148. #endif
  149. void ContentSharer::shareText (const String& text,
  150. std::function<void (bool, const String&)> callbackToUse)
  151. {
  152. #if JUCE_CONTENT_SHARING
  153. startNewShare (callbackToUse);
  154. pimpl->shareText (text);
  155. #else
  156. ignoreUnused (text);
  157. // Content sharing is not available on this platform!
  158. jassertfalse;
  159. if (callbackToUse)
  160. callbackToUse (false, "Content sharing is not available on this platform!");
  161. #endif
  162. }
  163. void ContentSharer::shareImages (const Array<Image>& images,
  164. std::function<void (bool, const String&)> callbackToUse,
  165. ImageFileFormat* imageFileFormatToUse)
  166. {
  167. #if JUCE_CONTENT_SHARING
  168. startNewShare (callbackToUse);
  169. prepareImagesThread.reset (new PrepareImagesThread (*this, images, imageFileFormatToUse));
  170. #else
  171. ignoreUnused (images, imageFileFormatToUse);
  172. // Content sharing is not available on this platform!
  173. jassertfalse;
  174. if (callbackToUse)
  175. callbackToUse (false, "Content sharing is not available on this platform!");
  176. #endif
  177. }
  178. #if JUCE_CONTENT_SHARING
  179. void ContentSharer::filesToSharePrepared()
  180. {
  181. Array<URL> urls;
  182. for (const auto& tempFile : temporaryFiles)
  183. urls.add (URL (tempFile));
  184. prepareImagesThread = nullptr;
  185. prepareDataThread = nullptr;
  186. pimpl->shareFiles (urls);
  187. }
  188. #endif
  189. void ContentSharer::shareData (const MemoryBlock& mb,
  190. std::function<void (bool, const String&)> callbackToUse)
  191. {
  192. #if JUCE_CONTENT_SHARING
  193. startNewShare (callbackToUse);
  194. prepareDataThread.reset (new PrepareDataThread (*this, mb));
  195. #else
  196. ignoreUnused (mb);
  197. if (callbackToUse)
  198. callbackToUse (false, "Content sharing not available on this platform!");
  199. #endif
  200. }
  201. void ContentSharer::sharingFinished (bool succeeded, const String& errorDescription)
  202. {
  203. deleteTemporaryFiles();
  204. std::function<void (bool, String)> cb;
  205. std::swap (cb, callback);
  206. String error (errorDescription);
  207. #if JUCE_CONTENT_SHARING
  208. pimpl.reset();
  209. #endif
  210. if (cb)
  211. cb (succeeded, error);
  212. }
  213. void ContentSharer::deleteTemporaryFiles()
  214. {
  215. for (auto& f : temporaryFiles)
  216. f.deleteFile();
  217. temporaryFiles.clear();
  218. }
  219. } // namespace juce