├── CHANGELOG.rst ├── CMakeLists.txt ├── LICENSE ├── README.rst ├── cmake └── modules │ ├── FindGphoto2.cmake │ ├── Findudev.cmake │ └── LibFindMacros.cmake └── src ├── gphoto-preview.c ├── gphoto-preview.h ├── gphoto-udev.c ├── gphoto-udev.h ├── gphoto-utils.c ├── gphoto-utils.h ├── obs-gphoto.c ├── timelapse.c └── timelapse.h /CHANGELOG.rst: -------------------------------------------------------------------------------- 1 | ========== 2 | Change Log 3 | ========== 4 | 5 | `v0.3.0` (2017-10-24) 6 | --------------------- 7 | * Add manual capture support to timelapse. 8 | 9 | `v0.2.0` (2017-10-22) 10 | --------------------- 11 | * Add timelapse function. -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | project(obs-gphoto) 3 | set(obs-gphoto_DESCRIPTION "Allows connect DSLR cameras with obs-studio through gPhoto on Linux.") 4 | set(obs-gphoto_VERSION_MAJOR 0) 5 | set(obs-gphoto_VERSION_MINOR 3) 6 | set(obs-gphoto_VERSION_PATCH 0) 7 | set(CMAKE_BUILD_TYPE Release) 8 | 9 | list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules/") 10 | 11 | find_package(LibObs) 12 | if(${LibObs_FOUND}) 13 | message(STATUS "LibObs FOUND") 14 | else() 15 | message(FATAL_ERROR "LibObs NOT FOUND") 16 | endif() 17 | 18 | find_package(Gphoto2) 19 | if(${GPHOTO2_FOUND}) 20 | message(STATUS "Gphoto2 FOUND") 21 | else() 22 | message(FATAL_ERROR "Gphoto2 NOT FOUND") 23 | endif() 24 | 25 | find_package(ImageMagick COMPONENTS MagickCore) 26 | if(${ImageMagick_MagickCore_FOUND}) 27 | message(STATUS "MagickCore FOUND") 28 | else() 29 | message(FATAL_ERROR "MagickCore NOT FOUND") 30 | endif() 31 | 32 | find_package(udev) 33 | if(NOT UDEV_FOUND OR DISABLE_UDEV) 34 | message(STATUS "udev disabled for v4l2 plugin") 35 | else() 36 | set(gphoto-udev_SOURCES src/gphoto-udev.c src/gphoto-udev.h) 37 | add_definitions(-DHAVE_UDEV) 38 | endif() 39 | 40 | math(EXPR BITS "8*${CMAKE_SIZEOF_VOID_P}") 41 | set(PLUGIN_DIRECTORY "${CMAKE_BINARY_DIR}/build/obs-gphoto") 42 | set(PLUGIN_BIN_DIRECTORY "${PLUGIN_DIRECTORY}/bin/${BITS}bit") 43 | 44 | set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PLUGIN_BIN_DIRECTORY}) 45 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PLUGIN_BIN_DIRECTORY}) 46 | 47 | include_directories(src ${LIBOBS_INCLUDE_DIRS} ${Gphoto2_INCLUDE_DIRS} ${ImageMagick_MagickCore_INCLUDE_DIRS} ${UDEV_INCLUDE_DIR}) 48 | 49 | set(SOURCE_FILES src/obs-gphoto.c src/gphoto-utils.c src/gphoto-utils.h ${gphoto-udev_SOURCES} 50 | src/gphoto-preview.c src/gphoto-preview.h 51 | src/timelapse.c src/timelapse.h) 52 | 53 | add_library(obs-gphoto MODULE ${SOURCE_FILES}) 54 | 55 | SET_TARGET_PROPERTIES(obs-gphoto PROPERTIES PREFIX "") 56 | target_link_libraries(obs-gphoto ${LIBOBS_LIBRARIES} ${Gphoto2_LIBRARIES} ${ImageMagick_LIBRARIES} ${UDEV_LIBRARIES}) 57 | 58 | # install 59 | if(${SYSTEM_INSTALL}) 60 | install(TARGETS obs-gphoto DESTINATION ${LIBOBS_PLUGIN_DESTINATION}) 61 | else() 62 | install(DIRECTORY ${PLUGIN_DIRECTORY} DESTINATION $ENV{HOME}/.config/obs-studio/plugins USE_SOURCE_PERMISSIONS) 63 | endif() 64 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | ========== 2 | obs-gphoto 3 | ========== 4 | 5 | Allows connect DSLR cameras with obs-studio through gPhoto on Linux. At now tested only Canon cameras. 6 | 7 | .. image:: https://img.shields.io/badge/Donate-PayPal-blue.svg 8 | :target: https://www.paypal.me/AeternusAtterratio 9 | .. image:: https://img.shields.io/badge/Donate-YaMoney-orange.svg 10 | :target: https://money.yandex.ru/to/410011005689134 11 | 12 | 13 | ------ 14 | v0.3.0 15 | ------ 16 | 17 | MODULES 18 | ======= 19 | gPhoto live preview capture 20 | --------------------------- 21 | Allows capture cameras preview like video. 22 | 23 | Timelapse photo capture 24 | ----------------------- 25 | Allows capture photo with some intervals(if interval set to 0 work only manual capture) or manual with hotkey and camera capture button, to show work in progress on good picture quality, or to compile timelapse video in future. 26 | 27 | REQUIREMENTS 28 | ============ 29 | 30 | * *obs-studio* 31 | * *libgphoto >= 2.5.10* 32 | * *libmagickcore* 33 | * *libudev(optional)* 34 | 35 | INSTALLATION 36 | ============ 37 | 38 | For ArchLinux: 39 | -------------- 40 | 41 | there is a `package in AUR`_. 42 | .. _`package in AUR`: https://aur.archlinux.org/packages/obs-gphoto/ 43 | 44 | 45 | For installation from source: 46 | ============================= 47 | 48 | Fedora: 49 | ------- 50 | Install requirements: :code:`dnf install libgphoto2-devel obs-studio-devel ImageMagick-devel systemd-devel` 51 | 52 | General: 53 | -------- 54 | * :code:`git clone https://github.com/Atterratio/obs-gphoto` 55 | * :code:`cd obs-gphoto` 56 | * :code:`cmake . -DSYSTEM_INSTALL=0` for local installation or :code:`cmake . -DSYSTEM_INSTALL=1` for system installation 57 | * :code:`make` 58 | * :code:`make install` 59 | -------------------------------------------------------------------------------- /cmake/modules/FindGphoto2.cmake: -------------------------------------------------------------------------------- 1 | # - Find the native sqlite3 includes and library 2 | # 3 | # This module defines 4 | # GPHOTO2_INCLUDE_DIR, where to find libgphoto2 header files 5 | # GPHOTO2_LIBRARIES, the libraries to link against to use libgphoto2 6 | # GPHOTO2_FOUND, If false, do not try to use libgphoto2. 7 | # GPHOTO2_VERSION_STRING, e.g. 2.4.14 8 | # GPHOTO2_VERSION_MAJOR, e.g. 2 9 | # GPHOTO2_VERSION_MINOR, e.g. 4 10 | # GPHOTO2_VERSION_PATCH, e.g. 14 11 | # 12 | # also defined, but not for general use are 13 | # GPHOTO2_LIBRARY, where to find the sqlite3 library. 14 | 15 | 16 | #============================================================================= 17 | # Copyright 2010 henrik andersson 18 | #============================================================================= 19 | 20 | include(LibFindMacros) 21 | 22 | SET(GPHOTO2_FIND_REQUIRED ${Gphoto2_FIND_REQUIRED}) 23 | 24 | find_path(GPHOTO2_INCLUDE_DIR gphoto2/gphoto2.h) 25 | mark_as_advanced(GPHOTO2_INCLUDE_DIR) 26 | 27 | set(GPHOTO2_NAMES ${GPHOTO2_NAMES} gphoto2 libgphoto2) 28 | set(GPHOTO2_PORT_NAMES ${GPHOTO2_PORT_NAMES} gphoto2_port libgphoto2_port) 29 | find_library(GPHOTO2_LIBRARY NAMES ${GPHOTO2_NAMES} ) 30 | find_library(GPHOTO2_PORT_LIBRARY NAMES ${GPHOTO2_PORT_NAMES} ) 31 | mark_as_advanced(GPHOTO2_LIBRARY) 32 | mark_as_advanced(GPHOTO2_PORT_LIBRARY) 33 | 34 | # Detect libgphoto2 version 35 | libfind_pkg_check_modules(PC_GPHOTO2 libgphoto2) 36 | if(PC_GPHOTO2_FOUND) 37 | set(GPHOTO2_VERSION_STRING "${PC_GPHOTO2_VERSION}") 38 | endif() 39 | 40 | # handle the QUIETLY and REQUIRED arguments and set GPHOTO2_FOUND to TRUE if 41 | # all listed variables are TRUE 42 | include(FindPackageHandleStandardArgs) 43 | find_package_handle_standard_args(GPHOTO2 DEFAULT_MSG GPHOTO2_LIBRARY GPHOTO2_INCLUDE_DIR) 44 | 45 | IF(GPHOTO2_FOUND) 46 | SET(Gphoto2_LIBRARIES ${GPHOTO2_LIBRARY} ${GPHOTO2_PORT_LIBRARY}) 47 | SET(Gphoto2_INCLUDE_DIRS ${GPHOTO2_INCLUDE_DIR}) 48 | 49 | # libgphoto2 dynamically loads and unloads usb library 50 | # without calling any cleanup functions (since they are absent from libusb-0.1). 51 | # This leaves usb event handling threads running with invalid callback and return addresses, 52 | # which causes a crash after any usb event is generated, at least in Mac OS X. 53 | # libusb1 backend does correctly call exit function, but ATM it crashes anyway. 54 | # Workaround is to link against libusb so that it wouldn't get unloaded. 55 | IF(APPLE) 56 | find_library(USB_LIBRARY NAMES usb-1.0 libusb-1.0) 57 | mark_as_advanced(USB_LIBRARY) 58 | IF(USB_LIBRARY) 59 | SET(Gphoto2_LIBRARIES ${Gphoto2_LIBRARIES} ${USB_LIBRARY}) 60 | ENDIF(USB_LIBRARY) 61 | ENDIF(APPLE) 62 | 63 | ENDIF(GPHOTO2_FOUND) 64 | -------------------------------------------------------------------------------- /cmake/modules/Findudev.cmake: -------------------------------------------------------------------------------- 1 | # - try to find the udev library 2 | # 3 | # Cache Variables: (probably not for direct use in your scripts) 4 | # UDEV_INCLUDE_DIR 5 | # UDEV_SOURCE_DIR 6 | # UDEV_LIBRARY 7 | # 8 | # Non-cache variables you might use in your CMakeLists.txt: 9 | # UDEV_FOUND 10 | # UDEV_INCLUDE_DIRS 11 | # UDEV_LIBRARIES 12 | # 13 | # Requires these CMake modules: 14 | # FindPackageHandleStandardArgs (known included with CMake >=2.6.2) 15 | # 16 | # Original Author: 17 | # 2014 Kevin M. Godby 18 | # 19 | # Distributed under the Boost Software License, Version 1.0. 20 | # (See accompanying file LICENSE_1_0.txt or copy at 21 | # http://www.boost.org/LICENSE_1_0.txt) 22 | 23 | set(UDEV_ROOT_DIR 24 | "${UDEV_ROOT_DIR}" 25 | CACHE 26 | PATH 27 | "Directory to search for udev") 28 | 29 | find_package(PkgConfig QUIET) 30 | if(PKG_CONFIG_FOUND) 31 | pkg_check_modules(PC_LIBUDEV libudev) 32 | endif() 33 | 34 | find_library(UDEV_LIBRARY 35 | NAMES 36 | udev 37 | PATHS 38 | ${PC_LIBUDEV_LIBRARY_DIRS} 39 | ${PC_LIBUDEV_LIBDIR} 40 | HINTS 41 | "${UDEV_ROOT_DIR}" 42 | PATH_SUFFIXES 43 | lib 44 | ) 45 | 46 | get_filename_component(_libdir "${UDEV_LIBRARY}" PATH) 47 | 48 | find_path(UDEV_INCLUDE_DIR 49 | NAMES 50 | libudev.h 51 | PATHS 52 | ${PC_LIBUDEV_INCLUDE_DIRS} 53 | ${PC_LIBUDEV_INCLUDEDIR} 54 | HINTS 55 | "${_libdir}" 56 | "${_libdir}/.." 57 | "${UDEV_ROOT_DIR}" 58 | PATH_SUFFIXES 59 | include 60 | ) 61 | 62 | include(FindPackageHandleStandardArgs) 63 | find_package_handle_standard_args(UDEV 64 | DEFAULT_MSG 65 | UDEV_LIBRARY 66 | UDEV_INCLUDE_DIR 67 | ) 68 | 69 | if(UDEV_FOUND) 70 | list(APPEND UDEV_LIBRARIES ${UDEV_LIBRARY}) 71 | list(APPEND UDEV_INCLUDE_DIRS ${UDEV_INCLUDE_DIR}) 72 | mark_as_advanced(UDEV_ROOT_DIR) 73 | endif() 74 | 75 | mark_as_advanced(UDEV_INCLUDE_DIR 76 | UDEV_LIBRARY) 77 | 78 | -------------------------------------------------------------------------------- /cmake/modules/LibFindMacros.cmake: -------------------------------------------------------------------------------- 1 | # Version 2.2 2 | # Public Domain, originally written by Lasse Kärkkäinen 3 | # Maintained at https://github.com/Tronic/cmake-modules 4 | # Please send your improvements as pull requests on Github. 5 | 6 | # Find another package and make it a dependency of the current package. 7 | # This also automatically forwards the "REQUIRED" argument. 8 | # Usage: libfind_package( [extra args to find_package]) 9 | macro (libfind_package PREFIX PKG) 10 | set(${PREFIX}_args ${PKG} ${ARGN}) 11 | if (${PREFIX}_FIND_REQUIRED) 12 | set(${PREFIX}_args ${${PREFIX}_args} REQUIRED) 13 | endif() 14 | find_package(${${PREFIX}_args}) 15 | set(${PREFIX}_DEPENDENCIES ${${PREFIX}_DEPENDENCIES};${PKG}) 16 | unset(${PREFIX}_args) 17 | endmacro() 18 | 19 | # A simple wrapper to make pkg-config searches a bit easier. 20 | # Works the same as CMake's internal pkg_check_modules but is always quiet. 21 | macro (libfind_pkg_check_modules) 22 | find_package(PkgConfig QUIET) 23 | if (PKG_CONFIG_FOUND) 24 | pkg_check_modules(${ARGN} QUIET) 25 | endif() 26 | endmacro() 27 | 28 | # Avoid useless copy&pasta by doing what most simple libraries do anyway: 29 | # pkg-config, find headers, find library. 30 | # Usage: libfind_pkg_detect( FIND_PATH [other args] FIND_LIBRARY [other args]) 31 | # E.g. libfind_pkg_detect(SDL2 sdl2 FIND_PATH SDL.h PATH_SUFFIXES SDL2 FIND_LIBRARY SDL2) 32 | function (libfind_pkg_detect PREFIX) 33 | # Parse arguments 34 | set(argname pkgargs) 35 | foreach (i ${ARGN}) 36 | if ("${i}" STREQUAL "FIND_PATH") 37 | set(argname pathargs) 38 | elseif ("${i}" STREQUAL "FIND_LIBRARY") 39 | set(argname libraryargs) 40 | else() 41 | set(${argname} ${${argname}} ${i}) 42 | endif() 43 | endforeach() 44 | if (NOT pkgargs) 45 | message(FATAL_ERROR "libfind_pkg_detect requires at least a pkg_config package name to be passed.") 46 | endif() 47 | # Find library 48 | libfind_pkg_check_modules(${PREFIX}_PKGCONF ${pkgargs}) 49 | if (pathargs) 50 | find_path(${PREFIX}_INCLUDE_DIR NAMES ${pathargs} HINTS ${${PREFIX}_PKGCONF_INCLUDE_DIRS}) 51 | endif() 52 | if (libraryargs) 53 | find_library(${PREFIX}_LIBRARY NAMES ${libraryargs} HINTS ${${PREFIX}_PKGCONF_LIBRARY_DIRS}) 54 | endif() 55 | endfunction() 56 | 57 | # Extracts a version #define from a version.h file, output stored to _VERSION. 58 | # Usage: libfind_version_header(Foobar foobar/version.h FOOBAR_VERSION_STR) 59 | # Fourth argument "QUIET" may be used for silently testing different define names. 60 | # This function does nothing if the version variable is already defined. 61 | function (libfind_version_header PREFIX VERSION_H DEFINE_NAME) 62 | # Skip processing if we already have a version or if the include dir was not found 63 | if (${PREFIX}_VERSION OR NOT ${PREFIX}_INCLUDE_DIR) 64 | return() 65 | endif() 66 | set(quiet ${${PREFIX}_FIND_QUIETLY}) 67 | # Process optional arguments 68 | foreach(arg ${ARGN}) 69 | if (arg STREQUAL "QUIET") 70 | set(quiet TRUE) 71 | else() 72 | message(AUTHOR_WARNING "Unknown argument ${arg} to libfind_version_header ignored.") 73 | endif() 74 | endforeach() 75 | # Read the header and parse for version number 76 | set(filename "${${PREFIX}_INCLUDE_DIR}/${VERSION_H}") 77 | if (NOT EXISTS ${filename}) 78 | if (NOT quiet) 79 | message(AUTHOR_WARNING "Unable to find ${${PREFIX}_INCLUDE_DIR}/${VERSION_H}") 80 | endif() 81 | return() 82 | endif() 83 | file(READ "${filename}" header) 84 | string(REGEX REPLACE ".*#[ \t]*define[ \t]*${DEFINE_NAME}[ \t]*\"([^\n]*)\".*" "\\1" match "${header}") 85 | # No regex match? 86 | if (match STREQUAL header) 87 | if (NOT quiet) 88 | message(AUTHOR_WARNING "Unable to find \#define ${DEFINE_NAME} \"\" from ${${PREFIX}_INCLUDE_DIR}/${VERSION_H}") 89 | endif() 90 | return() 91 | endif() 92 | # Export the version string 93 | set(${PREFIX}_VERSION "${match}" PARENT_SCOPE) 94 | endfunction() 95 | 96 | # Do the final processing once the paths have been detected. 97 | # If include dirs are needed, ${PREFIX}_PROCESS_INCLUDES should be set to contain 98 | # all the variables, each of which contain one include directory. 99 | # Ditto for ${PREFIX}_PROCESS_LIBS and library files. 100 | # Will set ${PREFIX}_FOUND, ${PREFIX}_INCLUDE_DIRS and ${PREFIX}_LIBRARIES. 101 | # Also handles errors in case library detection was required, etc. 102 | function (libfind_process PREFIX) 103 | # Skip processing if already processed during this configuration run 104 | if (${PREFIX}_FOUND) 105 | return() 106 | endif() 107 | 108 | set(found TRUE) # Start with the assumption that the package was found 109 | 110 | # Did we find any files? Did we miss includes? These are for formatting better error messages. 111 | set(some_files FALSE) 112 | set(missing_headers FALSE) 113 | 114 | # Shorthands for some variables that we need often 115 | set(quiet ${${PREFIX}_FIND_QUIETLY}) 116 | set(required ${${PREFIX}_FIND_REQUIRED}) 117 | set(exactver ${${PREFIX}_FIND_VERSION_EXACT}) 118 | set(findver "${${PREFIX}_FIND_VERSION}") 119 | set(version "${${PREFIX}_VERSION}") 120 | 121 | # Lists of config option names (all, includes, libs) 122 | unset(configopts) 123 | set(includeopts ${${PREFIX}_PROCESS_INCLUDES}) 124 | set(libraryopts ${${PREFIX}_PROCESS_LIBS}) 125 | 126 | # Process deps to add to 127 | foreach (i ${PREFIX} ${${PREFIX}_DEPENDENCIES}) 128 | if (DEFINED ${i}_INCLUDE_OPTS OR DEFINED ${i}_LIBRARY_OPTS) 129 | # The package seems to export option lists that we can use, woohoo! 130 | list(APPEND includeopts ${${i}_INCLUDE_OPTS}) 131 | list(APPEND libraryopts ${${i}_LIBRARY_OPTS}) 132 | else() 133 | # If plural forms don't exist or they equal singular forms 134 | if ((NOT DEFINED ${i}_INCLUDE_DIRS AND NOT DEFINED ${i}_LIBRARIES) OR 135 | ({i}_INCLUDE_DIR STREQUAL ${i}_INCLUDE_DIRS AND ${i}_LIBRARY STREQUAL ${i}_LIBRARIES)) 136 | # Singular forms can be used 137 | if (DEFINED ${i}_INCLUDE_DIR) 138 | list(APPEND includeopts ${i}_INCLUDE_DIR) 139 | endif() 140 | if (DEFINED ${i}_LIBRARY) 141 | list(APPEND libraryopts ${i}_LIBRARY) 142 | endif() 143 | else() 144 | # Oh no, we don't know the option names 145 | message(FATAL_ERROR "We couldn't determine config variable names for ${i} includes and libs. Aieeh!") 146 | endif() 147 | endif() 148 | endforeach() 149 | 150 | if (includeopts) 151 | list(REMOVE_DUPLICATES includeopts) 152 | endif() 153 | 154 | if (libraryopts) 155 | list(REMOVE_DUPLICATES libraryopts) 156 | endif() 157 | 158 | string(REGEX REPLACE ".*[ ;]([^ ;]*(_INCLUDE_DIRS|_LIBRARIES))" "\\1" tmp "${includeopts} ${libraryopts}") 159 | if (NOT tmp STREQUAL "${includeopts} ${libraryopts}") 160 | message(AUTHOR_WARNING "Plural form ${tmp} found in config options of ${PREFIX}. This works as before but is now deprecated. Please only use singular forms INCLUDE_DIR and LIBRARY, and update your find scripts for LibFindMacros > 2.0 automatic dependency system (most often you can simply remove the PROCESS variables entirely).") 161 | endif() 162 | 163 | # Include/library names separated by spaces (notice: not CMake lists) 164 | unset(includes) 165 | unset(libs) 166 | 167 | # Process all includes and set found false if any are missing 168 | foreach (i ${includeopts}) 169 | list(APPEND configopts ${i}) 170 | if (NOT "${${i}}" STREQUAL "${i}-NOTFOUND") 171 | list(APPEND includes "${${i}}") 172 | else() 173 | set(found FALSE) 174 | set(missing_headers TRUE) 175 | endif() 176 | endforeach() 177 | 178 | # Process all libraries and set found false if any are missing 179 | foreach (i ${libraryopts}) 180 | list(APPEND configopts ${i}) 181 | if (NOT "${${i}}" STREQUAL "${i}-NOTFOUND") 182 | list(APPEND libs "${${i}}") 183 | else() 184 | set (found FALSE) 185 | endif() 186 | endforeach() 187 | 188 | # Version checks 189 | if (found AND findver) 190 | if (NOT version) 191 | message(WARNING "The find module for ${PREFIX} does not provide version information, so we'll just assume that it is OK. Please fix the module or remove package version requirements to get rid of this warning.") 192 | elseif (version VERSION_LESS findver OR (exactver AND NOT version VERSION_EQUAL findver)) 193 | set(found FALSE) 194 | set(version_unsuitable TRUE) 195 | endif() 196 | endif() 197 | 198 | # If all-OK, hide all config options, export variables, print status and exit 199 | if (found) 200 | foreach (i ${configopts}) 201 | mark_as_advanced(${i}) 202 | endforeach() 203 | if (NOT quiet) 204 | message(STATUS "Found ${PREFIX} ${${PREFIX}_VERSION}") 205 | if (LIBFIND_DEBUG) 206 | message(STATUS " ${PREFIX}_DEPENDENCIES=${${PREFIX}_DEPENDENCIES}") 207 | message(STATUS " ${PREFIX}_INCLUDE_OPTS=${includeopts}") 208 | message(STATUS " ${PREFIX}_INCLUDE_DIRS=${includes}") 209 | message(STATUS " ${PREFIX}_LIBRARY_OPTS=${libraryopts}") 210 | message(STATUS " ${PREFIX}_LIBRARIES=${libs}") 211 | endif() 212 | set (${PREFIX}_INCLUDE_OPTS ${includeopts} PARENT_SCOPE) 213 | set (${PREFIX}_LIBRARY_OPTS ${libraryopts} PARENT_SCOPE) 214 | set (${PREFIX}_INCLUDE_DIRS ${includes} PARENT_SCOPE) 215 | set (${PREFIX}_LIBRARIES ${libs} PARENT_SCOPE) 216 | set (${PREFIX}_FOUND TRUE PARENT_SCOPE) 217 | endif() 218 | return() 219 | endif() 220 | 221 | # Format messages for debug info and the type of error 222 | set(vars "Relevant CMake configuration variables:\n") 223 | foreach (i ${configopts}) 224 | mark_as_advanced(CLEAR ${i}) 225 | set(val ${${i}}) 226 | if ("${val}" STREQUAL "${i}-NOTFOUND") 227 | set (val "") 228 | elseif (val AND NOT EXISTS ${val}) 229 | set (val "${val} (does not exist)") 230 | else() 231 | set(some_files TRUE) 232 | endif() 233 | set(vars "${vars} ${i}=${val}\n") 234 | endforeach() 235 | set(vars "${vars}You may use CMake GUI, cmake -D or ccmake to modify the values. Delete CMakeCache.txt to discard all values and force full re-detection if necessary.\n") 236 | if (version_unsuitable) 237 | set(msg "${PREFIX} ${${PREFIX}_VERSION} was found but") 238 | if (exactver) 239 | set(msg "${msg} only version ${findver} is acceptable.") 240 | else() 241 | set(msg "${msg} version ${findver} is the minimum requirement.") 242 | endif() 243 | else() 244 | if (missing_headers) 245 | set(msg "We could not find development headers for ${PREFIX}. Do you have the necessary dev package installed?") 246 | elseif (some_files) 247 | set(msg "We only found some files of ${PREFIX}, not all of them. Perhaps your installation is incomplete or maybe we just didn't look in the right place?") 248 | if(findver) 249 | set(msg "${msg} This could also be caused by incompatible version (if it helps, at least ${PREFIX} ${findver} should work).") 250 | endif() 251 | else() 252 | set(msg "We were unable to find package ${PREFIX}.") 253 | endif() 254 | endif() 255 | 256 | # Fatal error out if REQUIRED 257 | if (required) 258 | set(msg "REQUIRED PACKAGE NOT FOUND\n${msg} This package is REQUIRED and you need to install it or adjust CMake configuration in order to continue building ${CMAKE_PROJECT_NAME}.") 259 | message(FATAL_ERROR "${msg}\n${vars}") 260 | endif() 261 | # Otherwise just print a nasty warning 262 | if (NOT quiet) 263 | message(WARNING "WARNING: MISSING PACKAGE\n${msg} This package is NOT REQUIRED and you may ignore this warning but by doing so you may miss some functionality of ${CMAKE_PROJECT_NAME}. \n${vars}") 264 | endif() 265 | endfunction() -------------------------------------------------------------------------------- /src/gphoto-preview.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "gphoto-preview.h" 4 | #include "gphoto-utils.h" 5 | #if HAVE_UDEV 6 | #include "gphoto-udev.h" 7 | #endif 8 | 9 | 10 | 11 | static const char *capture_getname(void *vptr) { 12 | UNUSED_PARAMETER(vptr); 13 | return obs_module_text("gPhoto live preview capture"); 14 | } 15 | 16 | static void capture_defaults(obs_data_t *settings) { 17 | obs_data_set_default_int(settings, "fps", 30); 18 | } 19 | 20 | static bool capture_camera_selected(obs_properties_t *props, obs_property_t *prop, obs_data_t *settings){ 21 | UNUSED_PARAMETER(props); 22 | UNUSED_PARAMETER(prop); 23 | obs_data_set_string(settings, "changed", "camera"); 24 | 25 | return true; 26 | } 27 | 28 | static bool capture_fps_selected(obs_properties_t *props, obs_property_t *prop, obs_data_t *settings){ 29 | UNUSED_PARAMETER(props); 30 | UNUSED_PARAMETER(prop); 31 | obs_data_set_string(settings, "changed", "fps"); 32 | 33 | return true; 34 | } 35 | 36 | static obs_properties_t *capture_properties(void *vptr){ 37 | struct preview_data *data = vptr; 38 | 39 | obs_properties_t *props = obs_properties_create(); 40 | obs_data_t *settings = obs_source_get_settings(data->source); 41 | int cam_count = gp_list_count(data->cam_list); 42 | if(cam_count > 0) { 43 | obs_property_t *cam_list = obs_properties_add_list(props, "camera_name", obs_module_text("Camera"), 44 | OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING); 45 | property_cam_list(data->cam_list, cam_list); 46 | obs_property_set_modified_callback(cam_list, capture_camera_selected); 47 | 48 | 49 | obs_property_t *fps_list = obs_properties_add_list(props, "fps", obs_module_text("FPS"), 50 | OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT); 51 | obs_property_list_add_int(fps_list, "25", 25); 52 | obs_property_list_add_int(fps_list, "30", 30); 53 | obs_property_list_add_int(fps_list, "60", 60); 54 | obs_property_set_modified_callback(fps_list, capture_fps_selected); 55 | 56 | if (data->camera) { 57 | pthread_mutex_lock(&data->camera_mutex); 58 | create_autofocus_property(props, settings, data->camera, data->gp_context); 59 | create_manualfocus_property(props, settings, data->camera, data->gp_context); 60 | create_obs_property_from_camera_config(props, settings, obs_module_text("Shutter Speed"), 61 | data->camera, data->gp_context, "shutterspeed"); 62 | create_obs_property_from_camera_config(props, settings, obs_module_text("Aperture"), 63 | data->camera, data->gp_context, "aperture"); 64 | create_obs_property_from_camera_config(props, settings, obs_module_text("ISO"), 65 | data->camera, data->gp_context, "iso"); 66 | create_obs_property_from_camera_config(props, settings, obs_module_text("White balance"), 67 | data->camera, data->gp_context, "whitebalance"); 68 | create_obs_property_from_camera_config(props, settings, obs_module_text("Picture style"), 69 | data->camera, data->gp_context, "picturestyle"); 70 | pthread_mutex_unlock(&data->camera_mutex); 71 | } 72 | } 73 | 74 | obs_data_release(settings); 75 | 76 | return props; 77 | } 78 | 79 | static void *capture_thread(void *vptr){ 80 | struct preview_data *data = vptr; 81 | uint8_t *texture_data = malloc(data->width * data->height * 4); 82 | uint64_t cur_time = os_gettime_ns(); 83 | 84 | struct obs_source_frame frame = { 85 | .data = {[0] = texture_data}, 86 | .linesize = {[0] = data->width*4}, 87 | .width = data->width, 88 | .height = data->height, 89 | .format = VIDEO_FORMAT_BGRX 90 | }; 91 | 92 | while (os_event_try(data->event) == EAGAIN){ 93 | frame.timestamp = cur_time; 94 | pthread_mutex_lock(&data->camera_mutex); 95 | gphoto_capture_preview(data->camera, data->gp_context, data->width, data->height, texture_data); 96 | pthread_mutex_unlock(&data->camera_mutex); 97 | obs_source_output_video(data->source, &frame); 98 | switch (data->fps){ 99 | case 60: 100 | os_sleepto_ns(cur_time += 15000000); 101 | break; 102 | case 30: 103 | os_sleepto_ns(cur_time += 30000000); 104 | break; 105 | case 25: 106 | os_sleepto_ns(cur_time += 40000000); 107 | break; 108 | default: 109 | os_sleepto_ns(cur_time += 50000000); 110 | break; 111 | } 112 | } 113 | 114 | free(texture_data); 115 | 116 | return NULL; 117 | } 118 | 119 | static void capture_init(void *vptr){ 120 | struct preview_data *data = vptr; 121 | CameraFile *cam_file = NULL; 122 | const char *image_data = NULL; 123 | unsigned long data_size = NULL; 124 | Image *image = NULL; 125 | ImageInfo *image_info = AcquireImageInfo(); 126 | ExceptionInfo *exception = AcquireExceptionInfo(); 127 | 128 | if (gp_file_new(&cam_file) < GP_OK) { 129 | blog(LOG_WARNING, "What???\n"); 130 | } else { 131 | if (gp_camera_by_name(&data->camera, data->camera_name, data->cam_list, data->gp_context) < GP_OK) { 132 | blog(LOG_WARNING, "Can't get camera.\n"); 133 | } else { 134 | if (gp_camera_init(data->camera, data->gp_context) < GP_OK) { 135 | blog(LOG_WARNING, "Can't init camera.\n"); 136 | } else { 137 | if (gp_camera_capture_preview(data->camera, cam_file, data->gp_context) < GP_OK) { 138 | blog(LOG_WARNING, "Can't capture preview.\n"); 139 | } else { 140 | if (gp_file_get_data_and_size(cam_file, &image_data, &data_size) < GP_OK) { 141 | blog(LOG_WARNING, "Can't get image data.\n"); 142 | } else { 143 | image = BlobToImage(image_info, image_data, data_size, exception); 144 | if (exception->severity != UndefinedException) { 145 | CatchException(exception); 146 | blog(LOG_WARNING, "ImageMagic error: %s.\n", (char *)exception->severity); 147 | exception->severity = UndefinedException; 148 | } else { 149 | data->width = (uint32_t)image->magick_columns; 150 | data->height = (uint32_t)image->magick_rows; 151 | 152 | os_event_init(&data->event, OS_EVENT_TYPE_MANUAL); 153 | pthread_create(&data->thread, NULL, capture_thread, data); 154 | } 155 | } 156 | } 157 | } 158 | } 159 | } 160 | 161 | if(image_data){ 162 | free(image_data); 163 | } 164 | if(image_info){ 165 | DestroyImageInfo(image_info); 166 | } 167 | if(image){ 168 | DestroyImageList(image); 169 | } 170 | if(exception){ 171 | DestroyExceptionInfo(exception); 172 | } 173 | } 174 | 175 | static void capture_terminate(void *vptr){ 176 | struct preview_data *data = vptr; 177 | 178 | if(data->event) { 179 | os_event_signal(data->event); 180 | if(data->thread != 0){ 181 | pthread_join(data->thread, NULL); 182 | } 183 | os_event_destroy(data->event); 184 | } 185 | 186 | gp_camera_exit(data->camera, data->gp_context); 187 | gp_camera_free(data->camera); 188 | data->camera = NULL; 189 | } 190 | 191 | static void capture_update(void *vptr, obs_data_t *settings){ 192 | struct preview_data *data = vptr; 193 | 194 | const char *changed = obs_data_get_string(settings, "changed"); 195 | 196 | if (strcmp(changed, "camera") == 0) { 197 | data->camera_name = obs_data_get_string(settings, "camera_name"); 198 | if (data->source->active) { 199 | capture_terminate(data); 200 | pthread_mutex_lock(&data->camera_mutex); 201 | capture_init(data); 202 | pthread_mutex_unlock(&data->camera_mutex); 203 | obs_source_update_properties(data->source); 204 | if(data->autofocus) { 205 | pthread_mutex_lock(&data->camera_mutex); 206 | set_autofocus(data->camera, data->gp_context); 207 | pthread_mutex_unlock(&data->camera_mutex); 208 | } 209 | } 210 | } 211 | 212 | if(strcmp(changed, "fps") == 0){ 213 | data->fps = obs_data_get_int(settings, "fps"); 214 | } 215 | 216 | if (strcmp(changed, "autofocus") == 0) { 217 | data->autofocus = obs_data_get_bool(settings, "autofocusdrive"); 218 | if(data->autofocus) { 219 | pthread_mutex_lock(&data->camera_mutex); 220 | set_autofocus(data->camera, data->gp_context); 221 | pthread_mutex_unlock(&data->camera_mutex); 222 | }else{ 223 | pthread_mutex_lock(&data->camera_mutex); 224 | cancel_autofocus(data->camera, data->gp_context); 225 | pthread_mutex_unlock(&data->camera_mutex); 226 | } 227 | } 228 | 229 | if (strcmp(changed, "manualfocus") == 0) { 230 | const char *manual_focus = obs_data_get_string(settings, "manualfocus"); 231 | pthread_mutex_lock(&data->camera_mutex); 232 | set_manualfocus(manual_focus, data->camera, data->gp_context); 233 | pthread_mutex_unlock(&data->camera_mutex); 234 | } 235 | 236 | if (strcmp(changed, "auto_prop") == 0) { 237 | pthread_mutex_lock(&data->camera_mutex); 238 | set_camera_config(settings, data->camera, data->gp_context); 239 | pthread_mutex_unlock(&data->camera_mutex); 240 | } 241 | } 242 | 243 | #if HAVE_UDEV 244 | static void capture_camera_added(void *vptr, calldata_t *calldata) { 245 | UNUSED_PARAMETER(calldata); 246 | struct preview_data *data = vptr; 247 | int i, count; 248 | const char *camera_name; 249 | if(!data->camera){ 250 | pthread_mutex_lock(&data->camera_mutex); 251 | gphoto_cam_list(data->cam_list, data->gp_context); 252 | count = gp_list_count(data->cam_list); 253 | for(i=0; icam_list, i, &camera_name); 255 | if (strcmp(camera_name, data->camera_name) == 0) { 256 | capture_init(data); 257 | obs_source_update_properties(data->source); 258 | if(data->autofocus) { 259 | set_autofocus(data->camera, data->gp_context); 260 | } 261 | } 262 | } 263 | pthread_mutex_unlock(&data->camera_mutex); 264 | } 265 | } 266 | 267 | static void capture_camera_removed(void *vptr, calldata_t *calldata) { 268 | UNUSED_PARAMETER(calldata); 269 | struct preview_data *data = vptr; 270 | int i, count; 271 | const char *camera_name; 272 | if(data->camera){ 273 | pthread_mutex_lock(&data->camera_mutex); 274 | gphoto_cam_list(data->cam_list, data->gp_context); 275 | pthread_mutex_unlock(&data->camera_mutex); 276 | count = gp_list_count(data->cam_list); 277 | for(i=0; icam_list, i, &camera_name); 279 | if (strcmp(camera_name, data->camera_name) == 0) { 280 | return; 281 | } 282 | } 283 | capture_terminate(data); 284 | obs_source_update_properties(data->source); 285 | } 286 | } 287 | #endif 288 | 289 | static void capture_show(void *vptr) { 290 | struct preview_data *data = vptr; 291 | if (strcmp(data->camera_name, "") != 0) { 292 | if (!data->source->active && !data->camera) { 293 | capture_terminate(data); 294 | pthread_mutex_lock(&data->camera_mutex); 295 | capture_init(data); 296 | pthread_mutex_unlock(&data->camera_mutex); 297 | obs_source_update_properties(data->source); 298 | if(data->autofocus) { 299 | pthread_mutex_lock(&data->camera_mutex); 300 | set_autofocus(data->camera, data->gp_context); 301 | pthread_mutex_unlock(&data->camera_mutex); 302 | } 303 | } 304 | } 305 | } 306 | 307 | static void capture_hide(void *vptr) { 308 | struct preview_data *data = vptr; 309 | if(data->source->active) { 310 | capture_terminate(data); 311 | } 312 | } 313 | 314 | static void *capture_create(obs_data_t *settings, obs_source_t *source){ 315 | struct preview_data *data = bzalloc(sizeof(struct preview_data)); 316 | 317 | pthread_mutex_init(&data->camera_mutex, NULL); 318 | 319 | data->source = source; 320 | data->gp_context = gp_context_new(); 321 | 322 | gp_list_new(&data->cam_list); 323 | gphoto_cam_list(data->cam_list, data->gp_context); 324 | 325 | data->camera_name = obs_data_get_string(settings, "camera_name"); 326 | data->fps = obs_data_get_int(settings, "fps"); 327 | data->autofocus = obs_data_get_bool(settings, "autofocusdrive"); 328 | 329 | #if HAVE_UDEV 330 | gphoto_init_udev(); 331 | signal_handler_t *sh = gphoto_get_udev_signalhandler(); 332 | 333 | signal_handler_connect(sh, "device_added", &capture_camera_added, data); 334 | signal_handler_connect(sh, "device_removed", &capture_camera_removed, data); 335 | #endif 336 | 337 | return data; 338 | } 339 | 340 | static void capture_destroy(void *vptr) { 341 | struct preview_data *data = vptr; 342 | 343 | if(data->source->active){ 344 | capture_terminate(data); 345 | } 346 | 347 | pthread_mutex_destroy(&data->camera_mutex); 348 | gp_context_unref(data->gp_context); 349 | gp_list_free(data->cam_list); 350 | 351 | signal_handler_t *sh = gphoto_get_udev_signalhandler(); 352 | 353 | signal_handler_disconnect(sh, "device_added", capture_camera_added, data); 354 | signal_handler_disconnect(sh, "device_removed", capture_camera_removed, data); 355 | 356 | gphoto_unref_udev(); 357 | 358 | bfree(vptr); 359 | } 360 | 361 | static uint32_t capture_getwidth(void *vptr) { 362 | struct preview_data *data = vptr; 363 | return data->width; 364 | } 365 | 366 | static uint32_t capture_getheight(void *vptr) { 367 | struct preview_data *data = vptr; 368 | return data->height; 369 | } 370 | 371 | struct obs_source_info capture_preview_info = { 372 | .id = "gphoto-capture-preview", 373 | .type = OBS_SOURCE_TYPE_INPUT, 374 | .output_flags = OBS_SOURCE_ASYNC_VIDEO | OBS_SOURCE_DO_NOT_DUPLICATE, 375 | 376 | .get_name = capture_getname, 377 | .get_defaults = capture_defaults, 378 | .get_properties = capture_properties, 379 | .create = capture_create, 380 | .destroy = capture_destroy, 381 | .update = capture_update, 382 | .show = capture_show, 383 | .hide = capture_hide, 384 | .get_width = capture_getwidth, 385 | .get_height = capture_getheight, 386 | }; -------------------------------------------------------------------------------- /src/gphoto-preview.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct preview_data { 6 | /* settings */ 7 | const char *camera_name; 8 | long long int fps; 9 | bool autofocus; 10 | 11 | /* internal data */ 12 | obs_source_t *source; 13 | pthread_t thread; 14 | os_event_t *event; 15 | pthread_mutex_t camera_mutex; 16 | 17 | uint32_t width; 18 | uint32_t height; 19 | 20 | 21 | CameraList *cam_list; 22 | Camera *camera; 23 | GPContext *gp_context; 24 | }; -------------------------------------------------------------------------------- /src/gphoto-udev.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | enum udev_action { 5 | UDEV_ACTION_ADDED, 6 | UDEV_ACTION_REMOVED, 7 | UDEV_ACTION_UNKNOWN 8 | }; 9 | 10 | static const char *udev_signals[] = { 11 | "void device_added(string device)", 12 | "void device_removed(string device)", 13 | NULL 14 | }; 15 | 16 | /* global data */ 17 | static uint_fast32_t udev_refs = 0; 18 | static pthread_mutex_t udev_mutex = PTHREAD_MUTEX_INITIALIZER; 19 | 20 | static pthread_t udev_thread; 21 | static os_event_t *udev_event; 22 | 23 | static signal_handler_t *udev_signalhandler = NULL; 24 | 25 | 26 | static enum udev_action udev_action_to_enum(const char *action) { 27 | if (!action) 28 | return UDEV_ACTION_UNKNOWN; 29 | 30 | if (!strncmp("add", action, 3)) 31 | return UDEV_ACTION_ADDED; 32 | if (!strncmp("remove", action, 6)) 33 | return UDEV_ACTION_REMOVED; 34 | 35 | return UDEV_ACTION_UNKNOWN; 36 | } 37 | 38 | static inline void udev_signal_event(struct udev_device *dev){ 39 | enum udev_action action; 40 | struct calldata data; 41 | 42 | action = udev_action_to_enum(udev_device_get_action(dev)); 43 | 44 | pthread_mutex_lock(&udev_mutex); 45 | 46 | switch (action) { 47 | case UDEV_ACTION_ADDED: 48 | signal_handler_signal(udev_signalhandler, 49 | "device_added", &data); 50 | break; 51 | case UDEV_ACTION_REMOVED: 52 | signal_handler_signal(udev_signalhandler, 53 | "device_removed", &data); 54 | break; 55 | default: 56 | break; 57 | } 58 | 59 | pthread_mutex_unlock(&udev_mutex); 60 | } 61 | 62 | static void *udev_event_thread(void *vptr) { 63 | UNUSED_PARAMETER(vptr); 64 | 65 | int fd; 66 | fd_set fds; 67 | struct timeval tv; 68 | struct udev *udev; 69 | struct udev_monitor *mon; 70 | struct udev_device *dev; 71 | 72 | /* set up udev monitoring */ 73 | udev = udev_new(); 74 | mon = udev_monitor_new_from_netlink(udev, "udev"); 75 | udev_monitor_filter_add_match_subsystem_devtype(mon, "usb", NULL); 76 | if (udev_monitor_enable_receiving(mon) < 0) 77 | return NULL; 78 | 79 | /* set up fds */ 80 | fd = udev_monitor_get_fd(mon); 81 | 82 | while (os_event_try(udev_event) == EAGAIN) { 83 | FD_ZERO(&fds); 84 | FD_SET(fd, &fds); 85 | tv.tv_sec = 1; 86 | tv.tv_usec = 0; 87 | 88 | if (select(fd + 1, &fds, NULL, NULL, &tv) <= 0) 89 | continue; 90 | 91 | dev = udev_monitor_receive_device(mon); 92 | if (!dev) 93 | continue; 94 | 95 | udev_signal_event(dev); 96 | 97 | udev_device_unref(dev); 98 | } 99 | 100 | udev_monitor_unref(mon); 101 | udev_unref(udev); 102 | 103 | return NULL; 104 | } 105 | 106 | void gphoto_init_udev(void) { 107 | pthread_mutex_lock(&udev_mutex); 108 | 109 | /* set up udev */ 110 | if (udev_refs == 0) { 111 | if (os_event_init(&udev_event, OS_EVENT_TYPE_MANUAL) != 0) 112 | goto fail; 113 | if (pthread_create(&udev_thread, NULL, udev_event_thread, NULL) != 0) 114 | goto fail; 115 | 116 | udev_signalhandler = signal_handler_create(); 117 | if (!udev_signalhandler) 118 | goto fail; 119 | signal_handler_add_array(udev_signalhandler, udev_signals); 120 | 121 | } 122 | udev_refs++; 123 | 124 | fail: 125 | pthread_mutex_unlock(&udev_mutex); 126 | } 127 | 128 | void gphoto_unref_udev(void) { 129 | pthread_mutex_lock(&udev_mutex); 130 | 131 | /* unref udev monitor */ 132 | if (udev_refs && --udev_refs == 0) { 133 | os_event_signal(udev_event); 134 | pthread_join(udev_thread, NULL); 135 | os_event_destroy(udev_event); 136 | 137 | if (udev_signalhandler) 138 | signal_handler_destroy(udev_signalhandler); 139 | udev_signalhandler = NULL; 140 | } 141 | 142 | pthread_mutex_unlock(&udev_mutex); 143 | } 144 | 145 | signal_handler_t *gphoto_get_udev_signalhandler(void){ 146 | return udev_signalhandler; 147 | } -------------------------------------------------------------------------------- /src/gphoto-udev.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void gphoto_init_udev(void); 4 | void gphoto_unref_udev(void); 5 | signal_handler_t *gphoto_get_udev_signalhandler(void); -------------------------------------------------------------------------------- /src/gphoto-utils.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "gphoto-preview.h" 7 | 8 | static GPPortInfoList *portinfolist = NULL; 9 | static CameraAbilitiesList *abilities = NULL; 10 | 11 | static int sample_open_camera (Camera ** camera, const char *model, const char *port, GPContext *context) { 12 | //TODO: understand how this example from the repository works and, if necessary, rewrite it. 13 | int ret, m, p; 14 | CameraAbilities a; 15 | GPPortInfo pi; 16 | 17 | ret = gp_camera_new (camera); 18 | if (ret < GP_OK) return ret; 19 | 20 | if (!abilities) { 21 | /* Load all the camera drivers we have... */ 22 | ret = gp_abilities_list_new (&abilities); 23 | if (ret < GP_OK) return ret; 24 | ret = gp_abilities_list_load (abilities, context); 25 | if (ret < GP_OK) return ret; 26 | } 27 | 28 | /* First lookup the model / driver */ 29 | m = gp_abilities_list_lookup_model (abilities, model); 30 | if (m < GP_OK) return ret; 31 | ret = gp_abilities_list_get_abilities (abilities, m, &a); 32 | if (ret < GP_OK) return ret; 33 | ret = gp_camera_set_abilities (*camera, a); 34 | if (ret < GP_OK) return ret; 35 | 36 | if (!portinfolist) { 37 | /* Load all the port drivers we have... */ 38 | ret = gp_port_info_list_new (&portinfolist); 39 | if (ret < GP_OK) return ret; 40 | ret = gp_port_info_list_load (portinfolist); 41 | if (ret < 0) return ret; 42 | ret = gp_port_info_list_count (portinfolist); 43 | if (ret < 0) return ret; 44 | } 45 | 46 | /* Then associate the camera with the specified port */ 47 | p = gp_port_info_list_lookup_path (portinfolist, port); 48 | switch (p) { 49 | case GP_ERROR_UNKNOWN_PORT: 50 | break; 51 | default: 52 | break; 53 | } 54 | if (p < GP_OK) return p; 55 | 56 | ret = gp_port_info_list_get_info (portinfolist, p, &pi); 57 | if (ret < GP_OK) return ret; 58 | ret = gp_camera_set_port_info (*camera, pi); 59 | if (ret < GP_OK) return ret; 60 | return GP_OK; 61 | } 62 | 63 | int gp_camera_by_name(Camera **camera, const char *name, CameraList *cam_list, GPContext *context) { 64 | int i, count, ret; 65 | const char *camera_name, *usb_port; 66 | 67 | count = gp_list_count(cam_list); 68 | for(i=0; iseverity != UndefinedException) { 112 | CatchException(exception); 113 | blog(LOG_WARNING, "ImageMagic error: %s.\n", (char *)exception->severity); 114 | exception->severity = UndefinedException; 115 | } else { 116 | ExportImagePixels(image, 0, 0, (const size_t)width, (const size_t)height, "BGRA", CharPixel, texture_data, 117 | exception); 118 | if (exception->severity != UndefinedException) { 119 | CatchException(exception); 120 | blog(LOG_WARNING, "ImageMagic error: %s.\n", (char *)exception->severity); 121 | exception->severity = UndefinedException; 122 | } 123 | } 124 | } 125 | } 126 | } 127 | 128 | if(image_data){ 129 | free(image_data); 130 | } 131 | if(image_info){ 132 | DestroyImageInfo(image_info); 133 | } 134 | if(image){ 135 | DestroyImageList(image); 136 | } 137 | if(exception){ 138 | DestroyExceptionInfo(exception); 139 | } 140 | if(cam_file){ 141 | //TODO: SIGSEGV here, can't understand why! 142 | //gp_file_free(cam_file); 143 | } 144 | } 145 | 146 | void gphoto_capture(Camera *camera, GPContext *context, int width, int height, uint8_t *texture_data){ 147 | CameraFile *cam_file = NULL; 148 | CameraFilePath camera_file_path; 149 | const char *image_data = NULL; 150 | unsigned long data_size = NULL; 151 | Image *image = NULL; 152 | ImageInfo *image_info = AcquireImageInfo(); 153 | ExceptionInfo *exception = AcquireExceptionInfo(); 154 | 155 | if (gp_file_new(&cam_file) < GP_OK){ 156 | blog(LOG_WARNING, "What???\n"); 157 | }else { 158 | if (gp_camera_capture(camera, GP_CAPTURE_IMAGE, &camera_file_path, context) < GP_OK) { 159 | blog(LOG_WARNING, "Can't capture photo.\n"); 160 | } else { 161 | if (gp_camera_file_get(camera, camera_file_path.folder, camera_file_path.name, 162 | GP_FILE_TYPE_NORMAL, cam_file, context) < GP_OK) { 163 | blog(LOG_WARNING, "Can't get photo from camera.\n"); 164 | } else { 165 | if (gp_file_get_data_and_size(cam_file, &image_data, &data_size) < GP_OK) { 166 | blog(LOG_WARNING, "Can't get image data.\n"); 167 | } else { 168 | gp_camera_file_delete(camera, camera_file_path.folder, camera_file_path.name, context); 169 | image = BlobToImage(image_info, image_data, data_size, exception); 170 | if (exception->severity != UndefinedException) { 171 | CatchException(exception); 172 | blog(LOG_WARNING, "ImageMagic error: %s.\n", (char *) exception->severity); 173 | exception->severity = UndefinedException; 174 | } else { 175 | ExportImagePixels(image, 0, 0, (const size_t)width, (const size_t)height, "BGRA", CharPixel, texture_data, exception); 176 | if (exception->severity != UndefinedException) { 177 | CatchException(exception); 178 | blog(LOG_WARNING, "ImageMagic error: %s.\n", (char *) exception->severity); 179 | exception->severity = UndefinedException; 180 | } 181 | } 182 | } 183 | } 184 | } 185 | } 186 | 187 | if(image_data){ 188 | free(image_data); 189 | } 190 | if(image_info){ 191 | DestroyImageInfo(image_info); 192 | } 193 | if(image){ 194 | DestroyImageList(image); 195 | } 196 | if(exception){ 197 | DestroyExceptionInfo(exception); 198 | } 199 | if(cam_file){ 200 | //TODO: SIGSEGV here, can't understand why! 201 | //gp_file_free(cam_file); 202 | } 203 | } 204 | 205 | int gphoto_cam_list(CameraList *cam_list, GPContext *context){ 206 | int ret; 207 | gp_list_reset(cam_list); 208 | ret = gp_camera_autodetect(cam_list, context); 209 | return ret; 210 | } 211 | 212 | int cancel_autofocus(Camera *camera, GPContext *context){ 213 | int ret = -1; 214 | CameraWidget *widget = NULL; 215 | bool toggle; 216 | 217 | if (gp_camera_get_single_config(camera, "autofocusdrive", &widget, context) == GP_OK) { 218 | toggle = FALSE; 219 | gp_widget_set_value(widget, &toggle); 220 | ret = gp_camera_set_single_config(camera, "autofocusdrive", widget, context); 221 | } 222 | 223 | if (gp_camera_get_single_config(camera, "cancelautofocus", &widget, context) == GP_OK) { 224 | toggle = TRUE; 225 | gp_widget_set_value(widget, &toggle); 226 | gp_camera_set_single_config(camera, "cancelautofocus", widget, context); 227 | } 228 | 229 | if (widget) { 230 | gp_widget_free(widget); 231 | } 232 | return ret; 233 | } 234 | 235 | obs_property_t *camera_config_to_obs_property(char *config_name, Camera *camera, GPContext *context, obs_properties_t *props, 236 | const char *prop_name, const char *prop_description) { 237 | CameraWidget *widget = NULL; 238 | CameraWidgetType type; 239 | obs_property_t *p = NULL; 240 | int i, count; 241 | float min, max, step; 242 | const char *choose_val; 243 | if (gp_camera_get_single_config(camera, config_name, &widget, context) < GP_OK) { 244 | blog(LOG_WARNING, "Can't get single config %s for camera.\n", config_name); 245 | } else { 246 | if (gp_widget_get_type(widget, &type) < GP_OK) { 247 | blog(LOG_WARNING, "Can't get type for config %s.\n", config_name); 248 | } else { 249 | switch (type) { 250 | case GP_WIDGET_TEXT: 251 | p = obs_properties_add_text(props, prop_name, obs_module_text(prop_description), 252 | OBS_TEXT_DEFAULT); 253 | goto out; 254 | case GP_WIDGET_RANGE: 255 | if(gp_widget_get_range(widget, &min, &max, &step) == GP_OK) { 256 | p = obs_properties_add_float_slider(props, prop_name, obs_module_text(prop_description), 257 | min, max, step); 258 | } 259 | goto out; 260 | case GP_WIDGET_TOGGLE: 261 | p = obs_properties_add_bool(props, prop_name, obs_module_text(prop_description)); 262 | goto out; 263 | case GP_WIDGET_RADIO: 264 | case GP_WIDGET_MENU: 265 | p = obs_properties_add_list(props, prop_name, obs_module_text(prop_description), 266 | OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING); 267 | count = gp_widget_count_choices(widget); 268 | for (i = 0; i < count; i++) { 269 | if(gp_widget_get_choice(widget, i, &choose_val) == GP_OK) { 270 | obs_property_list_add_string(p, choose_val, choose_val); 271 | } 272 | } 273 | goto out; 274 | default: 275 | goto out; 276 | } 277 | } 278 | } 279 | out: 280 | if(widget){ 281 | gp_widget_free(widget); 282 | } 283 | return p; 284 | } 285 | 286 | static bool obs_property_calback(obs_properties_t *props, obs_property_t *prop, obs_data_t *settings){ 287 | UNUSED_PARAMETER(props); 288 | obs_data_set_string(settings, "changed", "auto_prop"); 289 | obs_data_set_string(settings, "auto_prop", obs_property_name(prop)); 290 | return true; 291 | } 292 | 293 | int create_obs_property_from_camera_config(obs_properties_t *props, obs_data_t *settings, const char *prop_description, 294 | Camera *camera, GPContext *context, char *config_name){ 295 | int ret = -1; 296 | char *text; 297 | float range; 298 | bool toggle; 299 | char *radio; 300 | obs_property_t *p; 301 | CameraWidget *widget = NULL; 302 | p = camera_config_to_obs_property(config_name, camera, context, props, config_name, prop_description); 303 | obs_property_set_modified_callback(p, obs_property_calback); 304 | enum obs_property_type p_type = obs_property_get_type(p); 305 | switch (p_type){ 306 | case OBS_PROPERTY_TEXT: 307 | ret = gp_camera_get_single_config(camera, config_name, &widget, context); 308 | if(ret == GP_OK){ 309 | gp_widget_get_value(widget, &text); 310 | obs_data_set_default_string(settings, config_name, text); 311 | } 312 | goto out; 313 | case OBS_PROPERTY_FLOAT: 314 | ret = gp_camera_get_single_config(camera, config_name, &widget, context); 315 | if(ret == GP_OK){ 316 | gp_widget_get_value(widget, &range); 317 | obs_data_set_default_double(settings, config_name, range); 318 | } 319 | goto out; 320 | case OBS_PROPERTY_BOOL: 321 | ret = gp_camera_get_single_config(camera, config_name, &widget, context); 322 | if(ret == GP_OK){ 323 | gp_widget_get_value(widget, &toggle); 324 | obs_data_set_default_bool(settings, config_name, toggle); 325 | } 326 | goto out; 327 | case OBS_PROPERTY_LIST: 328 | ret = gp_camera_get_single_config(camera, config_name, &widget, context); 329 | if(ret == GP_OK){ 330 | gp_widget_get_value(widget, &radio); 331 | obs_data_set_default_string(settings, config_name, radio); 332 | } 333 | goto out; 334 | default: 335 | goto out; 336 | 337 | } 338 | out: 339 | if(widget){ 340 | gp_widget_free(widget); 341 | } 342 | return ret; 343 | } 344 | 345 | int set_camera_config(obs_data_t *settings, Camera *camera, GPContext *context){ 346 | int ret = -1; 347 | const char *text; 348 | double range; 349 | bool toggle; 350 | CameraWidget *widget = NULL; 351 | CameraWidgetType type; 352 | const char *name = obs_data_get_string(settings, "auto_prop"); 353 | 354 | cancel_autofocus(camera, context); 355 | 356 | if (gp_camera_get_single_config(camera, name, &widget, context) < GP_OK) { 357 | blog(LOG_WARNING, "Can't get single config %s for camera.\n", name); 358 | } else { 359 | if (gp_widget_get_type(widget, &type) < GP_OK) { 360 | blog(LOG_WARNING, "Can't get type for config %s.\n", name); 361 | } else { 362 | switch (type) { 363 | case GP_WIDGET_TEXT: 364 | text = obs_data_get_string(settings, name); 365 | ret = gp_widget_set_value(widget, text); 366 | goto out; 367 | case GP_WIDGET_RANGE: 368 | range = obs_data_get_double(settings, name); 369 | ret = gp_widget_set_value(widget, &range); //It's right? 370 | goto out; 371 | case GP_WIDGET_TOGGLE: 372 | toggle = obs_data_get_bool(settings, name); 373 | ret = gp_widget_set_value(widget, &toggle); 374 | goto out; 375 | case GP_WIDGET_RADIO: 376 | case GP_WIDGET_MENU: 377 | text = obs_data_get_string(settings, name); 378 | ret = gp_widget_set_value(widget, text); 379 | goto out; 380 | default: 381 | goto out; 382 | } 383 | } 384 | } 385 | out: 386 | if(widget){ 387 | ret = gp_camera_set_single_config(camera, name, widget, context); 388 | gp_widget_free(widget); 389 | } 390 | return ret; 391 | } 392 | 393 | static bool autofocus_property_calback(obs_properties_t *props, obs_property_t *prop, obs_data_t *settings){ 394 | UNUSED_PARAMETER(prop); 395 | obs_data_set_string(settings, "changed", "autofocus"); 396 | bool enabled = obs_data_get_bool(settings, "autofocusdrive"); 397 | //TODO: remove indian code 398 | obs_property_t *manualfocusdrive = obs_properties_get(props, "manualfocusdrive"); 399 | obs_property_t *near1 = obs_properties_get(props, "Near 1"); 400 | obs_property_t *near2 = obs_properties_get(props, "Near 2"); 401 | obs_property_t *near3 = obs_properties_get(props, "Near 3"); 402 | obs_property_t *far1 = obs_properties_get(props, "Far 1"); 403 | obs_property_t *far2 = obs_properties_get(props, "Far 2"); 404 | obs_property_t *far3 = obs_properties_get(props, "Far 3"); 405 | obs_property_set_visible(manualfocusdrive, !enabled); 406 | obs_property_set_visible(near1, !enabled); 407 | obs_property_set_visible(near2, !enabled); 408 | obs_property_set_visible(near3, !enabled); 409 | obs_property_set_visible(far1, !enabled); 410 | obs_property_set_visible(far2, !enabled); 411 | obs_property_set_visible(far3, !enabled); 412 | 413 | return true; 414 | } 415 | 416 | int create_autofocus_property(obs_properties_t *props, obs_data_t *settings, Camera *camera, GPContext *context) { 417 | int ret = -1; 418 | bool toggle; 419 | CameraWidget *widget = NULL; 420 | CameraWidgetType type; 421 | obs_property_t *p; 422 | 423 | if (gp_camera_get_single_config(camera, "autofocusdrive", &widget, context) < GP_OK) { 424 | blog(LOG_WARNING, "Can't get single config autofocusdrive for camera.\n"); 425 | } else { 426 | if (gp_widget_get_type(widget, &type) < GP_OK) { 427 | blog(LOG_WARNING, "Can't get type for config autofocusdrive.\n"); 428 | } else { 429 | if (type == GP_WIDGET_TOGGLE) { 430 | p = obs_properties_add_bool(props, "autofocusdrive", obs_module_text("Auto Focus")); 431 | obs_property_set_modified_callback(p, autofocus_property_calback); 432 | gp_widget_get_value(widget, &toggle); 433 | obs_data_set_default_bool(settings, "autofocusdrive", FALSE); 434 | } 435 | } 436 | } 437 | if (widget) { 438 | gp_widget_free(widget); 439 | } 440 | return ret; 441 | } 442 | 443 | int set_autofocus(Camera *camera, GPContext *context){ 444 | int ret = -1; 445 | CameraWidget *widget = NULL; 446 | CameraWidgetType type; 447 | bool toggle; 448 | 449 | cancel_autofocus(camera, context); 450 | 451 | if (gp_camera_get_single_config(camera, "autofocusdrive", &widget, context) < GP_OK) { 452 | blog(LOG_WARNING, "Can't get single config autofocusdrive for camera.\n"); 453 | } else { 454 | if (gp_widget_get_type(widget, &type) < GP_OK) { 455 | blog(LOG_WARNING, "Can't get type for config autofocusdrive.\n"); 456 | } else { 457 | if (type == GP_WIDGET_TOGGLE) { 458 | gp_widget_set_value(widget, &toggle); 459 | ret = gp_camera_set_single_config(camera, "autofocusdrive", widget, context); 460 | return ret; 461 | } 462 | } 463 | } 464 | if (widget) { 465 | gp_widget_free(widget); 466 | } 467 | return ret; 468 | } 469 | 470 | int set_manualfocus(const char *value, Camera *camera, GPContext *context){ 471 | int ret = -1; 472 | CameraWidget *widget = NULL; 473 | CameraWidgetType type; 474 | 475 | cancel_autofocus(camera, context); 476 | 477 | if (gp_camera_get_single_config(camera, "manualfocusdrive", &widget, context) < GP_OK) { 478 | blog(LOG_WARNING, "Can't get single config manualfocusdrive for camera.\n"); 479 | } else { 480 | if (gp_widget_get_type(widget, &type) < GP_OK) { 481 | blog(LOG_WARNING, "Can't get type for config manualfocusdrive.\n"); 482 | } else { 483 | if (type == GP_WIDGET_RADIO){ 484 | ret = gp_widget_set_value(widget, value); 485 | } 486 | } 487 | } 488 | if(widget){ 489 | ret = gp_camera_set_single_config(camera, "manualfocusdrive", widget, context); 490 | gp_widget_free(widget); 491 | } 492 | return ret; 493 | } 494 | 495 | static bool manual_radio_focus_callback(obs_properties_t *props, obs_property_t *prop, void *vptr){ 496 | UNUSED_PARAMETER(props); 497 | struct preview_data *data = vptr; 498 | const char *value = obs_property_name(prop); 499 | pthread_mutex_lock(&data->camera_mutex); 500 | set_manualfocus(value, data->camera, data->gp_context); 501 | pthread_mutex_unlock(&data->camera_mutex); 502 | 503 | return TRUE; 504 | } 505 | 506 | int create_manualfocus_property(obs_properties_t *props, obs_data_t *settings, Camera *camera, GPContext *context){ 507 | int ret = -1, count ; 508 | float min, max, step, range; 509 | CameraWidget *widget = NULL; 510 | CameraWidgetType type; 511 | obs_property_t *p; 512 | if(gp_camera_get_single_config(camera, "manualfocusdrive", &widget, context) == GP_OK){ 513 | if(gp_widget_get_type(widget, &type) == GP_OK) { 514 | if (type == GP_WIDGET_RADIO) { 515 | count = gp_widget_count_choices(widget); 516 | //TODO: remove indian code. If loop can't Set btn name and text. 517 | if (count == 7){ 518 | obs_properties_add_button(props, "Near 3", "<<<", manual_radio_focus_callback); 519 | obs_properties_add_button(props, "Near 2", "<<", manual_radio_focus_callback); 520 | obs_properties_add_button(props, "Near 1", "<", manual_radio_focus_callback); 521 | obs_properties_add_button(props, "Far 1", ">", manual_radio_focus_callback); 522 | obs_properties_add_button(props, "Far 2", ">>", manual_radio_focus_callback); 523 | obs_properties_add_button(props, "Far 3", ">>>", manual_radio_focus_callback); 524 | } 525 | } else { 526 | if (type == GP_WIDGET_RANGE) { 527 | if (gp_widget_get_range(widget, &min, &max, &step) == GP_OK) { 528 | p = obs_properties_add_float_slider(props, "manualfocusdrive", obs_module_text("Manual focus"), 529 | min, max, step); 530 | obs_property_set_modified_callback(p, obs_property_calback); 531 | gp_widget_get_value(widget, &range); 532 | obs_data_set_default_double(settings, "manualfocusdrive", range); 533 | } 534 | } 535 | } 536 | } 537 | } 538 | if(widget){ 539 | gp_widget_free(widget); 540 | } 541 | return ret; 542 | } -------------------------------------------------------------------------------- /src/gphoto-utils.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int gp_camera_by_name(Camera **camera, const char *name, CameraList *cam_list, GPContext *context); 6 | void property_cam_list(CameraList *cam_list, obs_property_t *prop); 7 | void gphoto_capture_preview(Camera *camera, GPContext *context, int width, int height, uint8_t *texture_data); 8 | void gphoto_capture(Camera *camera, GPContext *context, int width, int height, uint8_t *texture_data); 9 | int gphoto_cam_list(CameraList *cam_list, GPContext *context); 10 | 11 | int cancel_autofocus(Camera *camera, GPContext *context); 12 | 13 | obs_property_t *camera_config_to_obs_property(char *config_name, Camera *camera, GPContext *context, obs_properties_t *props, 14 | const char *prop_name, const char *prop_description); 15 | int create_obs_property_from_camera_config(obs_properties_t *props, obs_data_t *settings, const char *prop_description, 16 | Camera *camera, GPContext *context, char *config_name); 17 | int set_camera_config(obs_data_t *settings, Camera *camera, GPContext *context); 18 | 19 | int create_autofocus_property(obs_properties_t *props, obs_data_t *settings, Camera *camera, GPContext *context); 20 | int set_autofocus(Camera *camera, GPContext *context); 21 | 22 | int create_manualfocus_property(obs_properties_t *props, obs_data_t *settings, Camera *camera, GPContext *context); 23 | int set_manualfocus(const char *value, Camera *camera, GPContext *context); -------------------------------------------------------------------------------- /src/obs-gphoto.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | OBS_DECLARE_MODULE() 4 | OBS_MODULE_USE_DEFAULT_LOCALE("obs-gphoto", "en-US") 5 | 6 | extern struct obs_source_info capture_preview_info; 7 | extern struct obs_source_info timelapse_capture_info; 8 | 9 | bool obs_module_load(void) { 10 | obs_register_source(&capture_preview_info); 11 | obs_register_source(&timelapse_capture_info); 12 | return true; 13 | } -------------------------------------------------------------------------------- /src/timelapse.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "timelapse.h" 4 | #include "gphoto-utils.h" 5 | #if HAVE_UDEV 6 | #include "gphoto-udev.h" 7 | #endif 8 | 9 | 10 | 11 | static const char *timelapse_getname(void *vptr) { 12 | UNUSED_PARAMETER(vptr); 13 | return obs_module_text("Timelapse photo capture"); 14 | } 15 | 16 | static void timelapse_defaults(obs_data_t *settings) { 17 | obs_data_set_default_int(settings, "interval", 30); 18 | } 19 | 20 | static bool test_capture_callback(obs_properties_t *props, obs_property_t *prop, void *vptr){ 21 | UNUSED_PARAMETER(prop); 22 | UNUSED_PARAMETER(props); 23 | struct timelapse_data *data = vptr; 24 | 25 | pthread_mutex_lock(&data->camera_mutex); 26 | gphoto_capture(data->camera, data->gp_context, data->width, data->height, data->texture_data); 27 | pthread_mutex_unlock(&data->camera_mutex); 28 | 29 | obs_enter_graphics(); 30 | gs_texture_set_image(data->texture, data->texture_data, data->width * 4, false); 31 | obs_leave_graphics(); 32 | 33 | return TRUE; 34 | } 35 | 36 | static void capture_hotkey_pressed(void *vptr, obs_hotkey_id id, obs_hotkey_t *key, bool pressed){ 37 | UNUSED_PARAMETER(id); 38 | UNUSED_PARAMETER(key); 39 | struct timelapse_data *data = vptr; 40 | uint64_t delta_time = os_gettime_ns() - data->last_capture_time; 41 | if(pressed && delta_time >= 500000000 && obs_source_active(data->source)) { 42 | pthread_mutex_lock(&data->camera_mutex); 43 | gphoto_capture(data->camera, data->gp_context, data->width, data->height, data->texture_data); 44 | pthread_mutex_unlock(&data->camera_mutex); 45 | 46 | obs_enter_graphics(); 47 | gs_texture_set_image(data->texture, data->texture_data, data->width * 4, false); 48 | obs_leave_graphics(); 49 | data->last_capture_time = os_gettime_ns(); 50 | } 51 | } 52 | 53 | static bool timelapse_camera_selected(obs_properties_t *props, obs_property_t *prop, obs_data_t *settings){ 54 | UNUSED_PARAMETER(props); 55 | UNUSED_PARAMETER(prop); 56 | obs_data_set_string(settings, "changed", "camera"); 57 | 58 | return true; 59 | } 60 | 61 | static bool timelapse_interval_changed(obs_properties_t *props, obs_property_t *prop, obs_data_t *settings){ 62 | UNUSED_PARAMETER(props); 63 | UNUSED_PARAMETER(prop); 64 | obs_data_set_string(settings, "changed", "interval"); 65 | 66 | return true; 67 | } 68 | 69 | static obs_properties_t *timelapse_properties(void *vptr){ 70 | struct timelapse_data *data = vptr; 71 | 72 | obs_properties_t *props = obs_properties_create(); 73 | obs_data_t *settings = obs_source_get_settings(data->source); 74 | 75 | int cam_count = gp_list_count(data->cam_list); 76 | if(cam_count > 0) { 77 | obs_property_t *cam_list = obs_properties_add_list(props, "camera_name", obs_module_text("Camera"), 78 | OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING); 79 | property_cam_list(data->cam_list, cam_list); 80 | obs_property_set_modified_callback(cam_list, timelapse_camera_selected); 81 | 82 | 83 | obs_property_t *interval = obs_properties_add_int(props, "interval", obs_module_text("Interval(in seconds)"), 84 | 0, 100000, 1); 85 | obs_property_set_modified_callback(interval, timelapse_interval_changed); 86 | 87 | if (data->camera) { 88 | obs_properties_add_button(props, "test_capture", obs_module_text("Test Capture"), test_capture_callback); 89 | pthread_mutex_lock(&data->camera_mutex); 90 | create_obs_property_from_camera_config(props, settings, obs_module_text("Image Format"), 91 | data->camera, data->gp_context, "imageformat"); 92 | create_obs_property_from_camera_config(props, settings, obs_module_text("Shutter Speed"), 93 | data->camera, data->gp_context, "shutterspeed"); 94 | create_obs_property_from_camera_config(props, settings, obs_module_text("Aperture"), 95 | data->camera, data->gp_context, "aperture"); 96 | create_obs_property_from_camera_config(props, settings, obs_module_text("ISO"), 97 | data->camera, data->gp_context, "iso"); 98 | create_obs_property_from_camera_config(props, settings, obs_module_text("White balance"), 99 | data->camera, data->gp_context, "whitebalance"); 100 | create_obs_property_from_camera_config(props, settings, obs_module_text("Picture style"), 101 | data->camera, data->gp_context, "picturestyle"); 102 | pthread_mutex_unlock(&data->camera_mutex); 103 | } 104 | } 105 | 106 | obs_data_release(settings); 107 | 108 | return props; 109 | } 110 | 111 | static void timelapse_init(void *vptr) { 112 | struct timelapse_data *data = vptr; 113 | CameraFile *cam_file = NULL; 114 | CameraFilePath camera_file_path; 115 | const char *image_data = NULL; 116 | unsigned long data_size = NULL; 117 | Image *image = NULL; 118 | ImageInfo *image_info = AcquireImageInfo(); 119 | ExceptionInfo *exception = AcquireExceptionInfo(); 120 | 121 | if (gp_file_new(&cam_file) < GP_OK) { 122 | blog(LOG_WARNING, "What???\n"); 123 | } else { 124 | if (gp_camera_by_name(&data->camera, data->camera_name, data->cam_list, data->gp_context) < GP_OK) { 125 | blog(LOG_WARNING, "Can't get camera.\n"); 126 | } else { 127 | if (gp_camera_init(data->camera, data->gp_context) < GP_OK) { 128 | blog(LOG_WARNING, "Can't init camera.\n"); 129 | } else { 130 | if (gp_camera_capture(data->camera, GP_CAPTURE_IMAGE, &camera_file_path, data->gp_context) < GP_OK) { 131 | blog(LOG_WARNING, "Can't capture photo.\n"); 132 | } else { 133 | if (gp_camera_file_get(data->camera, camera_file_path.folder, camera_file_path.name, 134 | GP_FILE_TYPE_NORMAL, cam_file, data->gp_context) < GP_OK) { 135 | blog(LOG_WARNING, "Can't get photo from camera.\n"); 136 | } else { 137 | if (gp_file_get_data_and_size(cam_file, &image_data, &data_size) < GP_OK) { 138 | blog(LOG_WARNING, "Can't get image data.\n"); 139 | } else { 140 | gp_camera_file_delete(data->camera, camera_file_path.folder, camera_file_path.name, 141 | data->gp_context); 142 | image = BlobToImage(image_info, image_data, data_size, exception); 143 | if (exception->severity != UndefinedException) { 144 | CatchException(exception); 145 | blog(LOG_WARNING, "ImageMagic error: %s.\n", (char *) exception->severity); 146 | exception->severity = UndefinedException; 147 | } else { 148 | data->width = (uint32_t) image->magick_columns; 149 | data->height = (uint32_t) image->magick_rows; 150 | 151 | data->texture_data = malloc(data->width * data->height * 4); 152 | 153 | ExportImagePixels(image, 0, 0, data->width, data->height, "BGRA", CharPixel, 154 | data->texture_data, 155 | exception); 156 | if (exception->severity != UndefinedException) { 157 | CatchException(exception); 158 | blog(LOG_WARNING, "ImageMagic error: %s.\n", (char *) exception->severity); 159 | exception->severity = UndefinedException; 160 | } else { 161 | obs_enter_graphics(); 162 | data->texture = gs_texture_create(data->width, data->height, GS_BGRA, 1, 163 | &data->texture_data, 164 | GS_DYNAMIC); 165 | obs_leave_graphics(); 166 | goto exit; 167 | } 168 | } 169 | } 170 | } 171 | } 172 | } 173 | } 174 | } 175 | 176 | data->width = 0; 177 | data->height = 0; 178 | obs_enter_graphics(); 179 | data->texture = gs_texture_create(data->width, data->height, GS_BGRA, 1, NULL, GS_DYNAMIC); 180 | obs_leave_graphics(); 181 | 182 | exit: 183 | if(image_data){ 184 | free(image_data); 185 | } 186 | if(image_info){ 187 | DestroyImageInfo(image_info); 188 | } 189 | if(image){ 190 | DestroyImageList(image); 191 | } 192 | if(exception){ 193 | DestroyExceptionInfo(exception); 194 | } 195 | } 196 | 197 | static void timelapse_terminate(void *vptr){ 198 | struct timelapse_data *data = vptr; 199 | 200 | gp_camera_exit(data->camera, data->gp_context); 201 | gp_camera_free(data->camera); 202 | data->camera = NULL; 203 | free(data->texture_data); 204 | } 205 | 206 | static void timelapse_update(void *vptr, obs_data_t *settings){ 207 | struct timelapse_data *data = vptr; 208 | 209 | const char *changed = obs_data_get_string(settings, "changed"); 210 | 211 | if (strcmp(changed, "camera") == 0) { 212 | data->camera_name = obs_data_get_string(settings, "camera_name"); 213 | if (data->source->active) { 214 | timelapse_terminate(data); 215 | pthread_mutex_lock(&data->camera_mutex); 216 | timelapse_init(data); 217 | pthread_mutex_unlock(&data->camera_mutex); 218 | obs_source_update_properties(data->source); 219 | if(data->autofocus) { 220 | pthread_mutex_lock(&data->camera_mutex); 221 | set_autofocus(data->camera, data->gp_context); 222 | pthread_mutex_unlock(&data->camera_mutex); 223 | } 224 | } 225 | } 226 | 227 | if(strcmp(changed, "interval") == 0){ 228 | data->interval = obs_data_get_int(settings, "interval"); 229 | } 230 | 231 | if (strcmp(changed, "autofocus") == 0) { 232 | data->autofocus = obs_data_get_bool(settings, "autofocusdrive"); 233 | if(data->autofocus) { 234 | pthread_mutex_lock(&data->camera_mutex); 235 | set_autofocus(data->camera, data->gp_context); 236 | pthread_mutex_unlock(&data->camera_mutex); 237 | }else{ 238 | pthread_mutex_lock(&data->camera_mutex); 239 | cancel_autofocus(data->camera, data->gp_context); 240 | pthread_mutex_unlock(&data->camera_mutex); 241 | } 242 | } 243 | 244 | if (strcmp(changed, "manualfocus") == 0) { 245 | const char *manual_focus = obs_data_get_string(settings, "manualfocus"); 246 | pthread_mutex_lock(&data->camera_mutex); 247 | set_manualfocus(manual_focus, data->camera, data->gp_context); 248 | pthread_mutex_unlock(&data->camera_mutex); 249 | } 250 | 251 | if (strcmp(changed, "auto_prop") == 0) { 252 | pthread_mutex_lock(&data->camera_mutex); 253 | set_camera_config(settings, data->camera, data->gp_context); 254 | pthread_mutex_unlock(&data->camera_mutex); 255 | } 256 | } 257 | 258 | #if HAVE_UDEV 259 | static void timelapse_camera_added(void *vptr, calldata_t *calldata) { 260 | UNUSED_PARAMETER(calldata); 261 | struct timelapse_data *data = vptr; 262 | int i, count; 263 | const char *camera_name; 264 | if(!data->camera){ 265 | pthread_mutex_lock(&data->camera_mutex); 266 | gphoto_cam_list(data->cam_list, data->gp_context); 267 | count = gp_list_count(data->cam_list); 268 | for(i=0; icam_list, i, &camera_name); 270 | if (strcmp(camera_name, data->camera_name) == 0) { 271 | timelapse_init(data); 272 | pthread_mutex_unlock(&data->camera_mutex); 273 | obs_source_update_properties(data->source); 274 | if(data->autofocus) { 275 | pthread_mutex_lock(&data->camera_mutex); 276 | set_autofocus(data->camera, data->gp_context); 277 | pthread_mutex_unlock(&data->camera_mutex); 278 | } 279 | return; 280 | } 281 | } 282 | pthread_mutex_unlock(&data->camera_mutex); 283 | } 284 | } 285 | 286 | static void timelapse_camera_removed(void *vptr, calldata_t *calldata) { 287 | UNUSED_PARAMETER(calldata); 288 | struct timelapse_data *data = vptr; 289 | int i, count; 290 | const char *camera_name; 291 | if(data->camera){ 292 | pthread_mutex_lock(&data->camera_mutex); 293 | gphoto_cam_list(data->cam_list, data->gp_context); 294 | pthread_mutex_unlock(&data->camera_mutex); 295 | count = gp_list_count(data->cam_list); 296 | for(i=0; icam_list, i, &camera_name); 298 | if (strcmp(camera_name, data->camera_name) == 0) { 299 | pthread_mutex_unlock(&data->camera_mutex); 300 | return; 301 | } 302 | } 303 | timelapse_terminate(data); 304 | obs_source_update_properties(data->source); 305 | } 306 | } 307 | #endif 308 | 309 | static void timelapse_show(void *vptr) { 310 | struct timelapse_data *data = vptr; 311 | if (strcmp(data->camera_name, "") != 0) { 312 | if (!data->source->active && !data->camera) { 313 | timelapse_terminate(data); 314 | pthread_mutex_lock(&data->camera_mutex); 315 | timelapse_init(data); 316 | pthread_mutex_unlock(&data->camera_mutex); 317 | obs_source_update_properties(data->source); 318 | if(data->autofocus) { 319 | pthread_mutex_lock(&data->camera_mutex); 320 | set_autofocus(data->camera, data->gp_context); 321 | pthread_mutex_unlock(&data->camera_mutex); 322 | } 323 | } 324 | }else{ 325 | data->width = 0; 326 | data->height = 0; 327 | obs_enter_graphics(); 328 | data->texture = gs_texture_create(data->width, data->height, GS_BGRA, 1, NULL, GS_DYNAMIC); 329 | obs_leave_graphics(); 330 | } 331 | } 332 | 333 | static void timelapse_hide(void *vptr) { 334 | struct timelapse_data *data = vptr; 335 | if(data->source->active) { 336 | timelapse_terminate(data); 337 | } 338 | } 339 | 340 | static void *timelapse_create(obs_data_t *settings, obs_source_t *source){ 341 | struct timelapse_data *data = bzalloc(sizeof(struct timelapse_data)); 342 | 343 | pthread_mutex_init(&data->camera_mutex, NULL); 344 | 345 | data->source = source; 346 | data->gp_context = gp_context_new(); 347 | 348 | gp_list_new(&data->cam_list); 349 | gphoto_cam_list(data->cam_list, data->gp_context); 350 | 351 | data->camera_name = obs_data_get_string(settings, "camera_name"); 352 | data->interval = obs_data_get_int(settings, "interval"); 353 | data->autofocus = obs_data_get_bool(settings, "autofocusdrive"); 354 | data->time_elapsed = 0; 355 | 356 | data->capture_key = obs_hotkey_register_source(source, "timelapse.capture", 357 | obs_module_text("Capture hotkey"), capture_hotkey_pressed, data); 358 | data->last_capture_time = os_gettime_ns(); 359 | 360 | #if HAVE_UDEV 361 | gphoto_init_udev(); 362 | signal_handler_t *sh = gphoto_get_udev_signalhandler(); 363 | 364 | signal_handler_connect(sh, "device_added", &timelapse_camera_added, data); 365 | signal_handler_connect(sh, "device_removed", &timelapse_camera_removed, data); 366 | #endif 367 | 368 | return data; 369 | } 370 | 371 | static void timelapse_destroy(void *vptr) { 372 | struct timelapse_data *data = vptr; 373 | 374 | if(data->source->active){ 375 | timelapse_terminate(data); 376 | } 377 | 378 | pthread_mutex_destroy(&data->camera_mutex); 379 | gp_context_unref(data->gp_context); 380 | gp_list_free(data->cam_list); 381 | 382 | obs_enter_graphics(); 383 | gs_texture_destroy(data->texture); 384 | data->texture = NULL; 385 | obs_leave_graphics(); 386 | 387 | signal_handler_t *sh = gphoto_get_udev_signalhandler(); 388 | 389 | signal_handler_disconnect(sh, "device_added", timelapse_camera_added, data); 390 | signal_handler_disconnect(sh, "device_removed", timelapse_camera_removed, data); 391 | 392 | gphoto_unref_udev(); 393 | 394 | bfree(vptr); 395 | } 396 | 397 | static uint32_t timelapse_getwidth(void *vptr) { 398 | struct timelapse_data *data = vptr; 399 | return data->width; 400 | } 401 | 402 | static uint32_t timelapse_getheight(void *vptr) { 403 | struct timelapse_data *data = vptr; 404 | return data->height; 405 | } 406 | 407 | static void timelapse_render(void *vptr, gs_effect_t *effect) { 408 | struct timelapse_data *data = vptr; 409 | 410 | gs_reset_blend_state(); 411 | gs_effect_set_texture(gs_effect_get_param_by_name(effect, "image"), data->texture); 412 | gs_draw_sprite(data->texture, 0, data->width, data->height); 413 | } 414 | 415 | static void timelapse_tick(void *vptr, float seconds) { 416 | struct timelapse_data *data = vptr; 417 | void *event_data; 418 | CameraEventType evtype; 419 | CameraFilePath *path; 420 | CameraFile *cam_file = NULL; 421 | const char *image_data = NULL; 422 | unsigned long data_size = NULL; 423 | Image *image = NULL; 424 | ImageInfo *image_info = AcquireImageInfo(); 425 | ExceptionInfo *exception = AcquireExceptionInfo(); 426 | 427 | if(data->camera){ 428 | pthread_mutex_lock(&data->camera_mutex); 429 | data->time_elapsed += seconds; 430 | if (data->time_elapsed >= data->interval && data->interval > 0) { 431 | gphoto_capture(data->camera, data->gp_context, data->width, data->height, data->texture_data); 432 | 433 | obs_enter_graphics(); 434 | gs_texture_set_image(data->texture, data->texture_data, data->width * 4, false); 435 | obs_leave_graphics(); 436 | 437 | data->time_elapsed = 0; 438 | } else { 439 | gp_camera_wait_for_event(data->camera, 100, &evtype, &event_data, data->gp_context); 440 | path = event_data; 441 | if (evtype == GP_EVENT_FILE_ADDED) { 442 | if (gp_file_new(&cam_file) < GP_OK) { 443 | blog(LOG_WARNING, "What???\n"); 444 | } else { 445 | if (gp_camera_file_get(data->camera, path->folder, path->name, 446 | GP_FILE_TYPE_NORMAL, cam_file, data->gp_context) < GP_OK) { 447 | blog(LOG_WARNING, "Can't get photo from camera.\n"); 448 | } else { 449 | if (gp_file_get_data_and_size(cam_file, &image_data, &data_size) < GP_OK) { 450 | blog(LOG_WARNING, "Can't get image data.\n"); 451 | } else { 452 | gp_camera_file_delete(data->camera, path->folder, path->name, data->gp_context); 453 | image = BlobToImage(image_info, image_data, data_size, exception); 454 | if (exception->severity != UndefinedException) { 455 | CatchException(exception); 456 | blog(LOG_WARNING, "ImageMagic error: %s.\n", (char *) exception->severity); 457 | exception->severity = UndefinedException; 458 | } else { 459 | ExportImagePixels(image, 0, 0, (const size_t) data->width, 460 | (const size_t) data->height, 461 | "BGRA", CharPixel, data->texture_data, 462 | exception); 463 | if (exception->severity != UndefinedException) { 464 | CatchException(exception); 465 | blog(LOG_WARNING, "ImageMagic error: %s.\n", (char *) exception->severity); 466 | exception->severity = UndefinedException; 467 | } else { 468 | obs_enter_graphics(); 469 | gs_texture_set_image(data->texture, data->texture_data, data->width * 4, false); 470 | obs_leave_graphics(); 471 | } 472 | } 473 | } 474 | } 475 | } 476 | } 477 | } 478 | pthread_mutex_unlock(&data->camera_mutex); 479 | } 480 | 481 | if (image_data) { 482 | free(image_data); 483 | } 484 | if (image_info) { 485 | DestroyImageInfo(image_info); 486 | } 487 | if (image) { 488 | DestroyImageList(image); 489 | } 490 | if (exception) { 491 | DestroyExceptionInfo(exception); 492 | } 493 | if (cam_file) { 494 | //TODO: SIGSEGV here, can't understand why! 495 | //gp_file_free(cam_file); 496 | } 497 | } 498 | 499 | struct obs_source_info timelapse_capture_info = { 500 | .id = "timelapse-capture", 501 | .type = OBS_SOURCE_TYPE_INPUT, 502 | .output_flags = OBS_SOURCE_VIDEO, 503 | 504 | .get_name = timelapse_getname, 505 | .get_defaults = timelapse_defaults, 506 | .get_properties = timelapse_properties, 507 | .create = timelapse_create, 508 | .destroy = timelapse_destroy, 509 | .update = timelapse_update, 510 | .show = timelapse_show, 511 | .hide = timelapse_hide, 512 | .get_width = timelapse_getwidth, 513 | .get_height = timelapse_getheight, 514 | .video_render = timelapse_render, 515 | .video_tick = timelapse_tick, 516 | }; -------------------------------------------------------------------------------- /src/timelapse.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct timelapse_data { 6 | /* settings */ 7 | const char *camera_name; 8 | long long int interval; 9 | bool autofocus; 10 | 11 | /* internal data */ 12 | obs_source_t *source; 13 | pthread_mutex_t camera_mutex; 14 | 15 | uint32_t width; 16 | uint32_t height; 17 | uint8_t *texture_data; 18 | gs_texture_t *texture; 19 | float time_elapsed; 20 | 21 | CameraList *cam_list; 22 | Camera *camera; 23 | GPContext *gp_context; 24 | 25 | obs_hotkey_id capture_key; 26 | uint64_t last_capture_time; 27 | }; --------------------------------------------------------------------------------