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.

160 lines
4.5KB

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