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.

263 lines
11KB

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