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.

168 lines
4.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. #if ! (JUCE_LINUX || JUCE_PROJUCER_LIVE_BUILD)
  22. #if JUCE_MAC || JUCE_IOS
  23. #include "../native/juce_mac_Video.h"
  24. #elif JUCE_WINDOWS
  25. #include "../native/juce_win32_Video.h"
  26. #elif JUCE_ANDROID
  27. #include "../native/juce_android_Video.h"
  28. #endif
  29. //==============================================================================
  30. VideoComponent::VideoComponent (bool useNativeControlsIfAvailable)
  31. : pimpl (new Pimpl (*this, useNativeControlsIfAvailable))
  32. {
  33. addAndMakeVisible (pimpl.get());
  34. }
  35. VideoComponent::~VideoComponent()
  36. {
  37. pimpl.reset();
  38. }
  39. Result VideoComponent::load (const File& file)
  40. {
  41. return loadInternal (file, false);
  42. }
  43. Result VideoComponent::load (const URL& url)
  44. {
  45. return loadInternal (url, false);
  46. }
  47. void VideoComponent::loadAsync (const URL& url, std::function<void(const URL&, Result)> callback)
  48. {
  49. if (callback == nullptr)
  50. {
  51. jassertfalse;
  52. return;
  53. }
  54. #if JUCE_ANDROID || JUCE_IOS || JUCE_MAC
  55. pimpl->loadAsync (url, callback);
  56. #else
  57. auto result = loadInternal (url, true);
  58. callback (url, result);
  59. #endif
  60. }
  61. void VideoComponent::closeVideo()
  62. {
  63. pimpl->close();
  64. // Closing on Android is async and resized() will be called internally by pimpl once
  65. // close operation finished.
  66. #if ! JUCE_ANDROID// TODO JUCE_IOS too?
  67. resized();
  68. #endif
  69. }
  70. bool VideoComponent::isVideoOpen() const { return pimpl->isOpen(); }
  71. File VideoComponent::getCurrentVideoFile() const { return pimpl->currentFile; }
  72. URL VideoComponent::getCurrentVideoURL() const { return pimpl->currentURL; }
  73. double VideoComponent::getVideoDuration() const { return pimpl->getDuration(); }
  74. Rectangle<int> VideoComponent::getVideoNativeSize() const { return pimpl->getNativeSize(); }
  75. void VideoComponent::play() { pimpl->play(); }
  76. void VideoComponent::stop() { pimpl->stop(); }
  77. bool VideoComponent::isPlaying() const { return pimpl->isPlaying(); }
  78. void VideoComponent::setPlayPosition (double newPos) { pimpl->setPosition (newPos); }
  79. double VideoComponent::getPlayPosition() const { return pimpl->getPosition(); }
  80. void VideoComponent::setPlaySpeed (double newSpeed) { pimpl->setSpeed (newSpeed); }
  81. double VideoComponent::getPlaySpeed() const { return pimpl->getSpeed(); }
  82. void VideoComponent::setAudioVolume (float newVolume) { pimpl->setVolume (newVolume); }
  83. float VideoComponent::getAudioVolume() const { return pimpl->getVolume(); }
  84. void VideoComponent::resized()
  85. {
  86. auto r = getLocalBounds();
  87. if (isVideoOpen() && ! r.isEmpty())
  88. {
  89. auto nativeSize = getVideoNativeSize();
  90. if (nativeSize.isEmpty())
  91. {
  92. // if we've just opened the file and are still waiting for it to
  93. // figure out the size, start our timer..
  94. if (! isTimerRunning())
  95. startTimer (50);
  96. }
  97. else
  98. {
  99. r = RectanglePlacement (RectanglePlacement::centred).appliedTo (nativeSize, r);
  100. stopTimer();
  101. }
  102. }
  103. else
  104. {
  105. stopTimer();
  106. }
  107. pimpl->setBounds (r);
  108. }
  109. void VideoComponent::timerCallback()
  110. {
  111. resized();
  112. }
  113. template<class FileOrURL>
  114. Result VideoComponent::loadInternal (const FileOrURL& fileOrUrl, bool loadAsync)
  115. {
  116. #if JUCE_ANDROID || JUCE_IOS
  117. ignoreUnused (fileOrUrl, loadAsync);
  118. // You need to use loadAsync on Android & iOS.
  119. jassertfalse;
  120. return Result::fail ("load() is not supported on this platform. Use loadAsync() instead.");
  121. #else
  122. auto result = pimpl->load (fileOrUrl);
  123. if (loadAsync)
  124. startTimer (50);
  125. else
  126. resized();
  127. return result;
  128. #endif
  129. }
  130. #endif
  131. } // namespace juce