The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

295 lines
8.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-9 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. // (This file gets included by juce_mac_NativeCode.mm, rather than being
  19. // compiled on its own).
  20. #if JUCE_INCLUDED_FILE && JUCE_QUICKTIME
  21. #define theMovie ((QTMovie*) movie)
  22. //==============================================================================
  23. QuickTimeMovieComponent::QuickTimeMovieComponent()
  24. : movie (0)
  25. {
  26. setOpaque (true);
  27. setVisible (true);
  28. QTMovieView* view = [[QTMovieView alloc] initWithFrame: NSMakeRect (0, 0, 100.0f, 100.0f)];
  29. setView (view);
  30. }
  31. QuickTimeMovieComponent::~QuickTimeMovieComponent()
  32. {
  33. closeMovie();
  34. setView (0);
  35. }
  36. bool QuickTimeMovieComponent::isQuickTimeAvailable() throw()
  37. {
  38. return true;
  39. }
  40. static QTMovie* openMovieFromStream (InputStream* movieStream, File& movieFile)
  41. {
  42. // unfortunately, QTMovie objects can only be created on the main thread..
  43. jassert (MessageManager::getInstance()->isThisTheMessageThread());
  44. QTMovie* movie = 0;
  45. FileInputStream* const fin = dynamic_cast <FileInputStream*> (movieStream);
  46. if (fin != 0)
  47. {
  48. movieFile = fin->getFile();
  49. movie = [QTMovie movieWithFile: juceStringToNS (movieFile.getFullPathName())
  50. error: nil];
  51. }
  52. else
  53. {
  54. MemoryBlock temp;
  55. movieStream->readIntoMemoryBlock (temp);
  56. const char* const suffixesToTry[] = { ".mov", ".mp3", ".avi", ".m4a" };
  57. for (int i = 0; i < numElementsInArray (suffixesToTry); ++i)
  58. {
  59. movie = [QTMovie movieWithDataReference: [QTDataReference dataReferenceWithReferenceToData: [NSData dataWithBytes: temp.getData()
  60. length: temp.getSize()]
  61. name: [NSString stringWithCString: suffixesToTry[i]]
  62. MIMEType: @""]
  63. error: nil];
  64. if (movie != 0)
  65. break;
  66. }
  67. }
  68. return movie;
  69. }
  70. bool QuickTimeMovieComponent::loadMovie (InputStream* movieStream,
  71. const bool controllerVisible)
  72. {
  73. closeMovie();
  74. if (getPeer() == 0)
  75. {
  76. // To open a movie, this component must be visible inside a functioning window, so that
  77. // the QT control can be assigned to the window.
  78. jassertfalse
  79. return false;
  80. }
  81. movie = openMovieFromStream (movieStream, movieFile);
  82. [theMovie retain];
  83. QTMovieView* view = (QTMovieView*) getView();
  84. [view setMovie: theMovie];
  85. [view setControllerVisible: controllerVisible];
  86. setLooping (looping);
  87. return movie != nil;
  88. }
  89. void QuickTimeMovieComponent::closeMovie()
  90. {
  91. stop();
  92. QTMovieView* view = (QTMovieView*) getView();
  93. [view setMovie: nil];
  94. [theMovie release];
  95. movie = 0;
  96. movieFile = File::nonexistent;
  97. }
  98. bool QuickTimeMovieComponent::isMovieOpen() const
  99. {
  100. return movie != nil;
  101. }
  102. const File QuickTimeMovieComponent::getCurrentMovieFile() const
  103. {
  104. return movieFile;
  105. }
  106. void QuickTimeMovieComponent::play()
  107. {
  108. [theMovie play];
  109. }
  110. void QuickTimeMovieComponent::stop()
  111. {
  112. [theMovie stop];
  113. }
  114. bool QuickTimeMovieComponent::isPlaying() const
  115. {
  116. return movie != 0 && [theMovie rate] != 0;
  117. }
  118. void QuickTimeMovieComponent::setPosition (const double seconds)
  119. {
  120. if (movie != 0)
  121. {
  122. QTTime t;
  123. t.timeValue = (uint64) (100000.0 * seconds);
  124. t.timeScale = 100000;
  125. t.flags = 0;
  126. [theMovie setCurrentTime: t];
  127. }
  128. }
  129. double QuickTimeMovieComponent::getPosition() const
  130. {
  131. if (movie == 0)
  132. return 0.0;
  133. QTTime t = [theMovie currentTime];
  134. return t.timeValue / (double) t.timeScale;
  135. }
  136. void QuickTimeMovieComponent::setSpeed (const float newSpeed)
  137. {
  138. [theMovie setRate: newSpeed];
  139. }
  140. double QuickTimeMovieComponent::getMovieDuration() const
  141. {
  142. if (movie == 0)
  143. return 0.0;
  144. QTTime t = [theMovie duration];
  145. return t.timeValue / (double) t.timeScale;
  146. }
  147. void QuickTimeMovieComponent::setLooping (const bool shouldLoop)
  148. {
  149. looping = shouldLoop;
  150. [theMovie setAttribute: [NSNumber numberWithBool: shouldLoop]
  151. forKey: QTMovieLoopsAttribute];
  152. }
  153. bool QuickTimeMovieComponent::isLooping() const
  154. {
  155. return looping;
  156. }
  157. void QuickTimeMovieComponent::setMovieVolume (const float newVolume)
  158. {
  159. [theMovie setVolume: newVolume];
  160. }
  161. float QuickTimeMovieComponent::getMovieVolume() const
  162. {
  163. return movie != 0 ? [theMovie volume] : 0.0f;
  164. }
  165. void QuickTimeMovieComponent::getMovieNormalSize (int& width, int& height) const
  166. {
  167. width = 0;
  168. height = 0;
  169. if (movie != 0)
  170. {
  171. NSSize s = [[theMovie attributeForKey: QTMovieNaturalSizeAttribute] sizeValue];
  172. width = s.width;
  173. height = s.height;
  174. }
  175. }
  176. void QuickTimeMovieComponent::paint (Graphics& g)
  177. {
  178. if (movie == 0)
  179. g.fillAll (Colours::black);
  180. }
  181. bool QuickTimeMovieComponent::isControllerVisible() const
  182. {
  183. return controllerVisible;
  184. }
  185. //==============================================================================
  186. bool QuickTimeMovieComponent::loadMovie (const File& movieFile_,
  187. const bool isControllerVisible)
  188. {
  189. const bool ok = loadMovie ((InputStream*) movieFile_.createInputStream(), isControllerVisible);
  190. movieFile = movieFile_;
  191. return ok;
  192. }
  193. void QuickTimeMovieComponent::goToStart()
  194. {
  195. setPosition (0.0);
  196. }
  197. void QuickTimeMovieComponent::setBoundsWithCorrectAspectRatio (const Rectangle& spaceToFitWithin,
  198. const RectanglePlacement& placement)
  199. {
  200. int normalWidth, normalHeight;
  201. getMovieNormalSize (normalWidth, normalHeight);
  202. if (normalWidth > 0 && normalHeight > 0 && ! spaceToFitWithin.isEmpty())
  203. {
  204. double x = 0.0, y = 0.0, w = normalWidth, h = normalHeight;
  205. placement.applyTo (x, y, w, h,
  206. spaceToFitWithin.getX(), spaceToFitWithin.getY(),
  207. spaceToFitWithin.getWidth(), spaceToFitWithin.getHeight());
  208. if (w > 0 && h > 0)
  209. {
  210. setBounds (roundDoubleToInt (x), roundDoubleToInt (y),
  211. roundDoubleToInt (w), roundDoubleToInt (h));
  212. }
  213. }
  214. else
  215. {
  216. setBounds (spaceToFitWithin);
  217. }
  218. }
  219. //==============================================================================
  220. #if ! (JUCE_MAC && JUCE_64BIT)
  221. bool juce_OpenQuickTimeMovieFromStream (InputStream* movieStream, Movie& result, Handle& dataHandle)
  222. {
  223. if (movieStream == 0)
  224. return false;
  225. File file;
  226. QTMovie* movie = openMovieFromStream (movieStream, file);
  227. if (movie != nil)
  228. result = [movie quickTimeMovie];
  229. return movie != nil;
  230. }
  231. #endif
  232. #endif