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.

298 lines
8.2KB

  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. QTMovie* movie = 0;
  48. FileInputStream* const fin = dynamic_cast <FileInputStream*> (movieStream);
  49. if (fin != 0)
  50. {
  51. movieFile = fin->getFile();
  52. movie = [QTMovie movieWithFile: juceStringToNS (movieFile.getFullPathName())
  53. error: nil];
  54. }
  55. else
  56. {
  57. MemoryBlock temp;
  58. movieStream->readIntoMemoryBlock (temp);
  59. const char* const suffixesToTry[] = { ".mov", ".mp3", ".avi", ".m4a" };
  60. for (int i = 0; i < numElementsInArray (suffixesToTry); ++i)
  61. {
  62. movie = [QTMovie movieWithDataReference: [QTDataReference dataReferenceWithReferenceToData: [NSData dataWithBytes: temp.getData()
  63. length: temp.getSize()]
  64. name: [NSString stringWithCString: suffixesToTry[i]]
  65. MIMEType: @""]
  66. error: nil];
  67. if (movie != 0)
  68. break;
  69. }
  70. }
  71. return movie;
  72. }
  73. bool QuickTimeMovieComponent::loadMovie (InputStream* movieStream,
  74. const bool controllerVisible)
  75. {
  76. closeMovie();
  77. if (getPeer() == 0)
  78. {
  79. // To open a movie, this component must be visible inside a functioning window, so that
  80. // the QT control can be assigned to the window.
  81. jassertfalse
  82. return false;
  83. }
  84. movie = openMovieFromStream (movieStream, movieFile);
  85. [theMovie retain];
  86. QTMovieView* view = (QTMovieView*) getView();
  87. [view setMovie: theMovie];
  88. [view setControllerVisible: controllerVisible];
  89. setLooping (looping);
  90. return movie != nil;
  91. }
  92. void QuickTimeMovieComponent::closeMovie()
  93. {
  94. stop();
  95. QTMovieView* view = (QTMovieView*) getView();
  96. [view setMovie: nil];
  97. [theMovie release];
  98. movie = 0;
  99. movieFile = File::nonexistent;
  100. }
  101. bool QuickTimeMovieComponent::isMovieOpen() const
  102. {
  103. return movie != nil;
  104. }
  105. const File QuickTimeMovieComponent::getCurrentMovieFile() const
  106. {
  107. return movieFile;
  108. }
  109. void QuickTimeMovieComponent::play()
  110. {
  111. [theMovie play];
  112. }
  113. void QuickTimeMovieComponent::stop()
  114. {
  115. [theMovie stop];
  116. }
  117. bool QuickTimeMovieComponent::isPlaying() const
  118. {
  119. return movie != 0 && [theMovie rate] != 0;
  120. }
  121. void QuickTimeMovieComponent::setPosition (const double seconds)
  122. {
  123. if (movie != 0)
  124. {
  125. QTTime t;
  126. t.timeValue = (uint64) (100000.0 * seconds);
  127. t.timeScale = 100000;
  128. t.flags = 0;
  129. [theMovie setCurrentTime: t];
  130. }
  131. }
  132. double QuickTimeMovieComponent::getPosition() const
  133. {
  134. if (movie == 0)
  135. return 0.0;
  136. QTTime t = [theMovie currentTime];
  137. return t.timeValue / (double) t.timeScale;
  138. }
  139. void QuickTimeMovieComponent::setSpeed (const float newSpeed)
  140. {
  141. [theMovie setRate: newSpeed];
  142. }
  143. double QuickTimeMovieComponent::getMovieDuration() const
  144. {
  145. if (movie == 0)
  146. return 0.0;
  147. QTTime t = [theMovie duration];
  148. return t.timeValue / (double) t.timeScale;
  149. }
  150. void QuickTimeMovieComponent::setLooping (const bool shouldLoop)
  151. {
  152. looping = shouldLoop;
  153. [theMovie setAttribute: [NSNumber numberWithBool: shouldLoop]
  154. forKey: QTMovieLoopsAttribute];
  155. }
  156. bool QuickTimeMovieComponent::isLooping() const
  157. {
  158. return looping;
  159. }
  160. void QuickTimeMovieComponent::setMovieVolume (const float newVolume)
  161. {
  162. [theMovie setVolume: newVolume];
  163. }
  164. float QuickTimeMovieComponent::getMovieVolume() const
  165. {
  166. return movie != 0 ? [theMovie volume] : 0.0f;
  167. }
  168. void QuickTimeMovieComponent::getMovieNormalSize (int& width, int& height) const
  169. {
  170. width = 0;
  171. height = 0;
  172. if (movie != 0)
  173. {
  174. NSSize s = [[theMovie attributeForKey: QTMovieNaturalSizeAttribute] sizeValue];
  175. width = s.width;
  176. height = s.height;
  177. }
  178. }
  179. void QuickTimeMovieComponent::paint (Graphics& g)
  180. {
  181. if (movie == 0)
  182. g.fillAll (Colours::black);
  183. }
  184. bool QuickTimeMovieComponent::isControllerVisible() const
  185. {
  186. return controllerVisible;
  187. }
  188. //==============================================================================
  189. bool QuickTimeMovieComponent::loadMovie (const File& movieFile_,
  190. const bool isControllerVisible)
  191. {
  192. const bool ok = loadMovie ((InputStream*) movieFile_.createInputStream(), isControllerVisible);
  193. movieFile = movieFile_;
  194. return ok;
  195. }
  196. void QuickTimeMovieComponent::goToStart()
  197. {
  198. setPosition (0.0);
  199. }
  200. void QuickTimeMovieComponent::setBoundsWithCorrectAspectRatio (const Rectangle& spaceToFitWithin,
  201. const RectanglePlacement& placement)
  202. {
  203. int normalWidth, normalHeight;
  204. getMovieNormalSize (normalWidth, normalHeight);
  205. if (normalWidth > 0 && normalHeight > 0 && ! spaceToFitWithin.isEmpty())
  206. {
  207. double x = 0.0, y = 0.0, w = normalWidth, h = normalHeight;
  208. placement.applyTo (x, y, w, h,
  209. spaceToFitWithin.getX(), spaceToFitWithin.getY(),
  210. spaceToFitWithin.getWidth(), spaceToFitWithin.getHeight());
  211. if (w > 0 && h > 0)
  212. {
  213. setBounds (roundDoubleToInt (x), roundDoubleToInt (y),
  214. roundDoubleToInt (w), roundDoubleToInt (h));
  215. }
  216. }
  217. else
  218. {
  219. setBounds (spaceToFitWithin);
  220. }
  221. }
  222. //==============================================================================
  223. #if ! (JUCE_MAC && JUCE_64BIT)
  224. bool juce_OpenQuickTimeMovieFromStream (InputStream* movieStream, Movie& result, Handle& dataHandle)
  225. {
  226. if (movieStream == 0)
  227. return false;
  228. File file;
  229. QTMovie* movie = openMovieFromStream (movieStream, file);
  230. if (movie != nil)
  231. result = [movie quickTimeMovie];
  232. return movie != nil;
  233. }
  234. #endif
  235. #endif