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.

167 lines
4.8KB

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