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.

261 lines
11KB

  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_USE_CAMERA || DOXYGEN
  21. //==============================================================================
  22. /**
  23. Controls any video capture devices that might be available.
  24. Use getAvailableDevices() to list the devices that are attached to the
  25. system, then call openDevice() or openDeviceAsync() to open one for use.
  26. Once you have a CameraDevice object, you can get a viewer component from it,
  27. and use its methods to stream to a file or capture still-frames.
  28. @tags{Video}
  29. */
  30. class JUCE_API CameraDevice
  31. {
  32. public:
  33. /** Destructor. */
  34. virtual ~CameraDevice();
  35. //==============================================================================
  36. /** Returns a list of the available cameras on this machine.
  37. You can open one of these devices by calling openDevice() or openDeviceAsync().
  38. */
  39. static StringArray getAvailableDevices();
  40. /** Synchronously opens a camera device. This function should not be used on iOS or
  41. Android, use openDeviceAsync() instead.
  42. The index parameter indicates which of the items returned by getAvailableDevices()
  43. to open.
  44. The size constraints allow the method to choose between different resolutions if
  45. the camera supports this. If the resolution can't be specified (e.g. on the Mac)
  46. then these will be ignored.
  47. On Mac, if highQuality is false, then the camera will be opened in preview mode
  48. which will allow the OS to drop frames if the computer cannot keep up in processing
  49. the frames.
  50. */
  51. static CameraDevice* openDevice (int deviceIndex,
  52. int minWidth = 128, int minHeight = 64,
  53. int maxWidth = 1024, int maxHeight = 768,
  54. bool highQuality = true);
  55. using OpenCameraResultCallback = std::function<void (CameraDevice*, const String& /*error*/)>;
  56. /** Asynchronously opens a camera device on iOS (iOS 7+) or Android (API 21+).
  57. On other platforms, the function will simply call openDevice(). Upon completion,
  58. resultCallback will be invoked with valid CameraDevice* and an empty error
  59. String on success, or nullptr CameraDevice and a non-empty error String on failure.
  60. This is the preferred method of opening a camera device, because it works on all
  61. platforms, whereas synchronous openDevice() does not work on iOS & Android.
  62. The index parameter indicates which of the items returned by getAvailableDevices()
  63. to open.
  64. The size constraints allow the method to choose between different resolutions if
  65. the camera supports this. If the resolution can't be specified then these will be
  66. ignored.
  67. On iOS, if you want to switch a device, it is more efficient to open a new device
  68. before closing the older one, because this way both devices can share the same
  69. underlying camera session. Otherwise, the session needs to be close first, and this
  70. is a lengthy process that can take several seconds.
  71. The Android implementation currently supports a maximum recording resolution of
  72. 1080p. Choosing a larger size will result in larger pictures taken, but the video
  73. will be capped at 1080p.
  74. */
  75. static void openDeviceAsync (int deviceIndex,
  76. OpenCameraResultCallback resultCallback,
  77. int minWidth = 128, int minHeight = 64,
  78. int maxWidth = 1024, int maxHeight = 768,
  79. bool highQuality = true);
  80. //==============================================================================
  81. /** Returns the name of this device */
  82. const String& getName() const noexcept { return name; }
  83. /** Creates a component that can be used to display a preview of the
  84. video from this camera.
  85. Note: While you can change the size of the preview component, the actual
  86. preview display may be smaller than the size requested, because the correct
  87. aspect ratio is maintained automatically.
  88. */
  89. Component* createViewerComponent();
  90. //==============================================================================
  91. /** Triggers a still picture capture. Upon completion, pictureTakenCallback will
  92. be invoked on a message thread.
  93. On Android, before calling takeStillPicture(), you need to create a preview with
  94. createViewerComponent() and you need to make it visible on screen.
  95. Android does not support simultaneous video recording and still picture capture.
  96. */
  97. void takeStillPicture (std::function<void (const Image&)> pictureTakenCallback);
  98. /** Starts recording video to the specified file.
  99. You should use getFileExtension() to find out the correct extension to
  100. use for your filename.
  101. If the file exists, it will be deleted before the recording starts.
  102. This method may not start recording instantly, so if you need to know the
  103. exact time at which the file begins, you can call getTimeOfFirstRecordedFrame()
  104. after the recording has finished.
  105. The quality parameter can be 0, 1, or 2, to indicate low, medium, or high. It may
  106. or may not be used, depending on the driver.
  107. On Android, before calling startRecordingToFile(), you need to create a preview with
  108. createViewerComponent() and you need to make it visible on screen.
  109. The Android camera also requires exclusive access to the audio device, so make sure
  110. you close any open audio devices with AudioDeviceManager::closeAudioDevice() first.
  111. Android does not support simultaneous video recording and still picture capture.
  112. @see AudioDeviceManager::closeAudioDevice, AudioDeviceManager::restartLastAudioDevice
  113. */
  114. void startRecordingToFile (const File& file, int quality = 2);
  115. /** Stops recording, after a call to startRecordingToFile(). */
  116. void stopRecording();
  117. /** Returns the file extension that should be used for the files
  118. that you pass to startRecordingToFile().
  119. This may be platform-specific, e.g. ".mov" or ".avi".
  120. */
  121. static String getFileExtension();
  122. /** After calling stopRecording(), this method can be called to return the timestamp
  123. of the first frame that was written to the file.
  124. */
  125. Time getTimeOfFirstRecordedFrame() const;
  126. /** Set this callback to be notified whenever an error occurs. You may need to close
  127. and reopen the device to be able to use it further. */
  128. std::function<void (const String& /*error*/)> onErrorOccurred;
  129. //==============================================================================
  130. /**
  131. Receives callbacks with individual frames from a CameraDevice. It is mainly
  132. useful for processing multiple frames that has to be done as quickly as
  133. possible. The callbacks can be called from any thread.
  134. If you just need to take one picture, you should use takeStillPicture() instead.
  135. @see CameraDevice::addListener
  136. */
  137. class JUCE_API Listener
  138. {
  139. public:
  140. Listener() {}
  141. virtual ~Listener() {}
  142. /** This method is called when a new image arrives.
  143. This may be called by any thread, so be careful about thread-safety,
  144. and make sure that you process the data as quickly as possible to
  145. avoid glitching!
  146. Simply add a listener to be continuously notified about new frames becoming
  147. available and remove the listener when you no longer need new frames.
  148. If you just need to take one picture, use takeStillPicture() instead.
  149. @see CameraDevice::takeStillPicture
  150. */
  151. virtual void imageReceived (const Image& image) = 0;
  152. };
  153. /** Adds a listener to receive images from the camera.
  154. Be very careful not to delete the listener without first removing it by calling
  155. removeListener().
  156. */
  157. void addListener (Listener* listenerToAdd);
  158. /** Removes a listener that was previously added with addListener(). */
  159. void removeListener (Listener* listenerToRemove);
  160. private:
  161. String name;
  162. struct Pimpl;
  163. std::unique_ptr<Pimpl> pimpl;
  164. struct ViewerComponent;
  165. friend struct ViewerComponent;
  166. CameraDevice (const String& name, int index,
  167. int minWidth, int minHeight, int maxWidth, int maxHeight, bool highQuality);
  168. #if JUCE_ANDROID || JUCE_IOS
  169. class CameraFactory;
  170. #endif
  171. #if JUCE_ANDROID
  172. friend void juce_cameraDeviceStateClosed (int64);
  173. friend void juce_cameraDeviceStateDisconnected (int64);
  174. friend void juce_cameraDeviceStateError (int64, int);
  175. friend void juce_cameraDeviceStateOpened (int64, void*);
  176. friend void juce_cameraCaptureSessionActive (int64, void*);
  177. friend void juce_cameraCaptureSessionClosed (int64, void*);
  178. friend void juce_cameraCaptureSessionConfigureFailed (int64, void*);
  179. friend void juce_cameraCaptureSessionConfigured (int64, void*);
  180. friend void juce_cameraCaptureSessionReady (int64, void*);
  181. friend void juce_cameraCaptureSessionCaptureCompleted (int64, bool, void*, void*, void*);
  182. friend void juce_cameraCaptureSessionCaptureFailed (int64, bool, void*, void*, void*);
  183. friend void juce_cameraCaptureSessionCaptureProgressed (int64, bool, void*, void*, void*);
  184. friend void juce_cameraCaptureSessionCaptureSequenceAborted (int64, bool, void*, int);
  185. friend void juce_cameraCaptureSessionCaptureSequenceCompleted (int64, bool, void*, int, int64);
  186. friend void juce_cameraCaptureSessionCaptureStarted (int64, bool, void*, void*, int64, int64);
  187. friend void juce_deviceOrientationChanged (int64, int);
  188. #endif
  189. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CameraDevice)
  190. };
  191. #endif
  192. } // namespace juce