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.

301 lines
8.4KB

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