├── .clang-format ├── .gitignore ├── .gitmodules ├── .travis.yml ├── LICENSE ├── Makefile.am ├── README.md ├── autogen.sh ├── configure.ac ├── dronecode-camera-manager.service.in ├── m4 ├── .gitignore ├── attributes.m4 └── ax_cxx_compile_stdcxx.m4 ├── plugins ├── AeroAtomIspCamera │ ├── CameraDeviceAeroAtomIsp.cpp │ ├── CameraDeviceAeroAtomIsp.h │ ├── PluginAeroAtomIsp.cpp │ └── PluginAeroAtomIsp.h ├── CustomCamera │ ├── CameraDeviceCustom.cpp │ ├── CameraDeviceCustom.h │ ├── PluginCustom.cpp │ └── PluginCustom.h ├── GazeboCamera │ ├── CameraDeviceGazebo.cpp │ ├── CameraDeviceGazebo.h │ ├── PluginGazebo.cpp │ └── PluginGazebo.h ├── RealSenseCamera │ ├── CameraDeviceRealSense.cpp │ ├── CameraDeviceRealSense.h │ ├── PluginRealSense.cpp │ └── PluginRealSense.h └── V4l2Camera │ ├── CameraDeviceV4l2.cpp │ ├── CameraDeviceV4l2.h │ ├── PluginV4l2.cpp │ └── PluginV4l2.h ├── samples ├── config │ ├── aero.conf │ ├── config.sample │ ├── gazebo.conf │ ├── picam.conf │ └── ubuntu.conf └── def │ ├── camera-def-gazebo.xml │ ├── camera-def-picam.xml │ ├── camera-def-rs-rgb.xml │ └── camera-def-uvc.xml ├── src ├── CameraComponent.cpp ├── CameraComponent.h ├── CameraDevice.h ├── CameraParameters.cpp ├── CameraParameters.h ├── CameraServer.cpp ├── CameraServer.h ├── ImageCapture.h ├── ImageCaptureGst.cpp ├── ImageCaptureGst.h ├── PluginBase.h ├── PluginManager.cpp ├── PluginManager.h ├── VideoCapture.h ├── VideoCaptureGst.cpp ├── VideoCaptureGst.h ├── VideoStream.h ├── VideoStreamRtsp.cpp ├── VideoStreamRtsp.h ├── VideoStreamUdp.cpp ├── VideoStreamUdp.h ├── avahi_publisher.cpp ├── avahi_publisher.h ├── conf_file.cpp ├── conf_file.h ├── glib_mainloop.cpp ├── glib_mainloop.h ├── log.cpp ├── log.h ├── macro.h ├── main.cpp ├── mainloop.cpp ├── mainloop.h ├── mavlink_server.cpp ├── mavlink_server.h ├── pollable.cpp ├── pollable.h ├── settings.cpp ├── settings.h ├── socket.cpp ├── socket.h ├── util.c ├── util.h ├── v4l2_interface.cpp └── v4l2_interface.h ├── test ├── test_camera_parameters.cpp ├── test_mavlink_protocol.cpp └── test_rtsp_udp_stream_discovery.cpp ├── tools ├── checkpatch ├── export_v4l2_param_xml │ ├── Makefile │ └── export_v4l2_param_xml.cpp └── git-clang-format └── valgrind.supp /.clang-format: -------------------------------------------------------------------------------- 1 | --- 2 | BasedOnStyle: WebKit 3 | 4 | AlignAfterOpenBracket: true 5 | AlignEscapedNewlinesLeft: true 6 | AlignTrailingComments: true 7 | AllowShortBlocksOnASingleLine: false 8 | AllowShortFunctionsOnASingleLine: Inline 9 | ColumnLimit: 100 10 | Cpp11BracedListStyle: true 11 | DerivePointerAlignment: false 12 | IndentCaseLabels: false 13 | Language: Cpp 14 | NamespaceIndentation: None 15 | PointerAlignment: Right 16 | SortIncludes: true 17 | SpaceAfterCStyleCast: false 18 | SpaceInEmptyParentheses: false 19 | Standard: Cpp11 20 | TabWidth: 4 21 | ... 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.lo 3 | *.la 4 | *.a 5 | /*.tar.xz 6 | /*.md5sum 7 | .deps/ 8 | .libs/ 9 | .dirstamp 10 | /Makefile 11 | Makefile.in 12 | 13 | /aclocal.m4 14 | /autom4te.cache 15 | /build-aux/ 16 | /dronecode-camera-manager.service 17 | /config.h 18 | /config.h.in 19 | /config.log 20 | /config.status 21 | /configure 22 | /include/mavlink 23 | /libtool 24 | /perf.data 25 | /perf.data.old 26 | /stamp-h1 27 | 28 | *~ 29 | .*.swp 30 | cscope.out 31 | tags 32 | 33 | /dcm 34 | /test/test-camera-parameters 35 | /test/test-custom-stream 36 | /test/test-mavlink-protocol 37 | /test/test-rtsp-udp-stream-discovery 38 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "modules/mavlink"] 2 | path = modules/mavlink 3 | url = https://github.com/mavlink/mavlink.git 4 | branch = master 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: generic 2 | 3 | services: docker 4 | 5 | before_install: 6 | - CONTAINER=$(docker run -it -d -v $PWD:/build ubuntu:16.04 /bin/bash) 7 | - docker exec -i $CONTAINER /bin/bash -c "apt-get update -qq && apt-get install -y -qq libavahi-glib-dev libavahi-client-dev libavahi-core-dev libgstreamer1.0-dev libgstrtspserver-1.0-dev libgstreamer-plugins-base1.0-dev build-essential autoconf libtool wget libgstrtspserver-1.0-dev python2.7 python-future" 8 | - docker exec -i $CONTAINER /bin/bash -c "apt-get update -qq && apt-get install -y -qq software-properties-common python-software-properties" 9 | - docker exec -i $CONTAINER /bin/bash -c "add-apt-repository http://packages.osrfoundation.org/gazebo/ubuntu && wget --quiet http://packages.osrfoundation.org/gazebo.key -O - | apt-key add -" 10 | - docker exec -i $CONTAINER /bin/bash -c "apt-get update -qq && apt-get install -y -qq libgazebo8-dev" 11 | - docker exec -i $CONTAINER /bin/bash -c "add-apt-repository http://realsense-alm-public.s3.amazonaws.com/apt-repo | apt-key adv --keyserver keys.gnupg.net --recv-key D6FB2970" 12 | - docker exec -i $CONTAINER /bin/bash -c "apt-get update -qq && apt-get install -y -qq librealsense-dev" 13 | 14 | script: 15 | - docker exec -i $CONTAINER /bin/bash -c "cd /build && ./autogen.sh c --with-systemdsystemunitdir=/usr/lib/systemd/system && make -j && make -j test" 16 | - docker exec -i $CONTAINER /bin/bash -c "cd /build && make distclean && ./autogen.sh c --with-systemdsystemunitdir=/usr/lib/systemd/system --enable-aero --enable-realsense && make -j && make -j test" 17 | - docker exec -i $CONTAINER /bin/bash -c "cd /build && make distclean && ./autogen.sh c --with-systemdsystemunitdir=/usr/lib/systemd/system --enable-aero --enable-realsense --enable-mavlink --enable-avahi && make -j && make -j test" 18 | - docker exec -i $CONTAINER /bin/bash -c "cd /build && make distclean && ./autogen.sh c --with-systemdsystemunitdir=/usr/lib/systemd/system --enable-mavlink --enable-realsense --enable-avahi && make -j && make -j test" 19 | - docker exec -i $CONTAINER /bin/bash -c "cd /build && make distclean && ./autogen.sh c --with-systemdsystemunitdir=/usr/lib/systemd/system --enable-gazebo --enable-mavlink && make -j && make -j test" 20 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | 3 | Version 2.0, January 2004 4 | 5 | http://www.apache.org/licenses/ 6 | 7 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 8 | 9 | 1. Definitions. 10 | 11 | "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. 16 | 17 | "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. 18 | 19 | "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. 20 | 21 | "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. 22 | 23 | "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). 24 | 25 | "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. 26 | 27 | "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." 28 | 29 | "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 30 | 31 | 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 32 | 33 | 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 34 | 35 | 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: 36 | 37 | You must give any other recipients of the Work or Derivative Works a copy of this License; and 38 | You must cause any modified files to carry prominent notices stating that You changed the files; and 39 | You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and 40 | If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. 41 | 42 | You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 43 | 44 | 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 45 | 46 | 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 47 | 48 | 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 49 | 50 | 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 51 | 52 | 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. 53 | 54 | END OF TERMS AND CONDITIONS 55 | -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | DISTCLEAN_LOCAL_HOOKS = 2 | EXTRA_DIST = 3 | CLEANFILES = $(BUILT_FILES) 4 | noinst_LTLIBRARIES = 5 | bin_PROGRAMS = 6 | noinst_PROGRAMS = 7 | noinst_SCRIPTS = 8 | BUILT_FILES = 9 | ACLOCAL_AMFLAGS = -I m4 ${ACLOCAL_FLAGS} 10 | AM_MAKEFLAGS = --no-print-directory 11 | 12 | GCC_COLORS ?= 'yes' 13 | export GCC_COLORS 14 | 15 | BUILT_SOURCES = 16 | test_deps = 17 | 18 | clean-local: 19 | rm -rf $(top_builddir)/include/mavlink 20 | rm -rf $(EXTRA_PROGRAMS) 21 | 22 | #if SYSTEMD 23 | systemdsystemunitdir = @SYSTEMD_SYSTEMUNITDIR@ 24 | systemdsystemunit_DATA = dronecode-camera-manager.service 25 | #endif 26 | 27 | 28 | AM_CPPFLAGS = \ 29 | -include $(abs_top_builddir)/config.h \ 30 | -DSYSCONFDIR=\""$(sysconfdir)"\" 31 | 32 | AM_CFLAGS = \ 33 | -pipe \ 34 | -Wall \ 35 | -W \ 36 | -Wextra \ 37 | -Wno-inline \ 38 | -Wundef \ 39 | -Wformat=2 \ 40 | -Wlogical-op \ 41 | -Wsign-compare \ 42 | -Wformat-security \ 43 | -Wmissing-include-dirs \ 44 | -Wformat-nonliteral \ 45 | -Wold-style-definition \ 46 | -Wpointer-arith \ 47 | -Winit-self \ 48 | -Wdeclaration-after-statement \ 49 | -Wfloat-equal \ 50 | -Wmissing-prototypes \ 51 | -Wstrict-prototypes \ 52 | -Wredundant-decls \ 53 | -Wmissing-declarations \ 54 | -Wmissing-noreturn \ 55 | -Wshadow \ 56 | -Wendif-labels \ 57 | -Wstrict-aliasing=3 \ 58 | -Wwrite-strings \ 59 | -Wno-long-long \ 60 | -Wno-overlength-strings \ 61 | -Wno-unused-parameter \ 62 | -Wno-missing-field-initializers \ 63 | -Wno-unused-result \ 64 | -Wnested-externs \ 65 | -Wchar-subscripts \ 66 | -Wtype-limits \ 67 | -Wuninitialized \ 68 | -fno-common \ 69 | -fdiagnostics-show-option \ 70 | -fvisibility=hidden \ 71 | -ffunction-sections \ 72 | -fdata-sections \ 73 | ${GST_CFLAGS} 74 | 75 | AM_CXXFLAGS = \ 76 | -I$(top_builddir)/src \ 77 | -I$(srcdir)/ \ 78 | -pipe \ 79 | -Wall \ 80 | -W \ 81 | -Wextra \ 82 | -Wno-inline \ 83 | -Wundef \ 84 | -Wformat=2 \ 85 | -Wlogical-op \ 86 | -Wsign-compare \ 87 | -Wformat-security \ 88 | -Wmissing-include-dirs \ 89 | -Wformat-nonliteral \ 90 | -Wpointer-arith \ 91 | -Winit-self \ 92 | -Wfloat-equal \ 93 | -Wredundant-decls \ 94 | -Wmissing-declarations \ 95 | -Wmissing-noreturn \ 96 | -Wshadow \ 97 | -Wendif-labels \ 98 | -Wstrict-aliasing=3 \ 99 | -Wwrite-strings \ 100 | -Wno-long-long \ 101 | -Wno-overlength-strings \ 102 | -Wno-unused-parameter \ 103 | -Wno-missing-field-initializers \ 104 | -Wno-unused-result \ 105 | -Wchar-subscripts \ 106 | -Wtype-limits \ 107 | -Wuninitialized \ 108 | -fno-common \ 109 | -fdiagnostics-show-option \ 110 | -fvisibility=hidden \ 111 | -ffunction-sections \ 112 | -fdata-sections \ 113 | ${GST_CFLAGS} 114 | 115 | AM_LDFLAGS = \ 116 | -Wl,--as-needed \ 117 | -Wl,--no-undefined \ 118 | -Wl,--gc-sections 119 | 120 | BASE_FILES = \ 121 | src/conf_file.cpp \ 122 | src/conf_file.h \ 123 | src/glib_mainloop.cpp \ 124 | src/glib_mainloop.h \ 125 | src/log.cpp \ 126 | src/log.h \ 127 | src/macro.h \ 128 | src/mainloop.cpp \ 129 | src/mainloop.h \ 130 | src/pollable.cpp \ 131 | src/pollable.h \ 132 | src/socket.cpp \ 133 | src/socket.h \ 134 | src/util.c \ 135 | src/util.h \ 136 | src/CameraParameters.cpp \ 137 | src/CameraParameters.h \ 138 | src/CameraComponent.cpp \ 139 | src/CameraComponent.h \ 140 | src/CameraServer.cpp \ 141 | src/CameraServer.h \ 142 | src/CameraDevice.h \ 143 | src/ImageCapture.h \ 144 | src/ImageCaptureGst.h \ 145 | src/ImageCaptureGst.cpp \ 146 | src/VideoStream.h \ 147 | src/VideoStreamUdp.h \ 148 | src/VideoStreamUdp.cpp \ 149 | src/VideoStreamRtsp.h \ 150 | src/VideoStreamRtsp.cpp \ 151 | src/VideoCapture.h \ 152 | src/VideoCaptureGst.h \ 153 | src/VideoCaptureGst.cpp \ 154 | src/PluginManager.h \ 155 | src/PluginManager.cpp \ 156 | src/PluginBase.h \ 157 | src/v4l2_interface.cpp \ 158 | src/v4l2_interface.h \ 159 | plugins/V4l2Camera/PluginV4l2.h\ 160 | plugins/V4l2Camera/PluginV4l2.cpp \ 161 | plugins/V4l2Camera/CameraDeviceV4l2.cpp \ 162 | plugins/V4l2Camera/CameraDeviceV4l2.h 163 | 164 | if ENABLE_REALSENSE 165 | BASE_FILES += \ 166 | plugins/RealSenseCamera/PluginRealSense.h \ 167 | plugins/RealSenseCamera/PluginRealSense.cpp \ 168 | plugins/RealSenseCamera/CameraDeviceRealSense.h \ 169 | plugins/RealSenseCamera/CameraDeviceRealSense.cpp 170 | endif 171 | 172 | if ENABLE_AERO 173 | BASE_FILES += \ 174 | plugins/AeroAtomIspCamera/PluginAeroAtomIsp.h \ 175 | plugins/AeroAtomIspCamera/PluginAeroAtomIsp.cpp \ 176 | plugins/AeroAtomIspCamera/CameraDeviceAeroAtomIsp.h \ 177 | plugins/AeroAtomIspCamera/CameraDeviceAeroAtomIsp.cpp 178 | endif 179 | 180 | if ENABLE_CUSTOM 181 | BASE_FILES += \ 182 | plugins/CustomCamera/PluginCustom.h \ 183 | plugins/CustomCamera/PluginCustom.cpp \ 184 | plugins/CustomCamera/CameraDeviceCustom.h \ 185 | plugins/CustomCamera/CameraDeviceCustom.cpp 186 | endif 187 | 188 | 189 | bin_PROGRAMS += dcm 190 | 191 | SED_PROCESS = $(AM_V_GEN) $(MKDIR_P) $(dir $@) && \ 192 | $(SED) -e 's,@bindir\@,$(bindir),g' \ 193 | < $< > $@ 194 | 195 | %.service: %.service.in Makefile 196 | $(SED_PROCESS) 197 | 198 | 199 | dcm_SOURCES = \ 200 | ${BASE_FILES} \ 201 | src/main.cpp 202 | 203 | dcm_LDADD = $(GLIB_LIBS) $(GST_LIBS) $(GZB_LIBS) 204 | 205 | CLEANFILES += dronecode-camera-manager.service 206 | 207 | #Samples 208 | EXTRA_PROGRAMS = test/test-custom-stream 209 | 210 | test_test_custom_stream_LDADD = ${dcm_LDADD} 211 | 212 | test_test_custom_stream_SOURCES = \ 213 | $(BASE_FILES) \ 214 | src/main.cpp \ 215 | plugins/CustomCamera/PluginCustom.cpp \ 216 | plugins/CustomCamera/PluginCustom.h \ 217 | plugins/CustomCamera/CameraDeviceCustom.cpp \ 218 | plugins/CustomCamera/CameraDeviceCustom.h 219 | 220 | if ENABLE_AVAHI 221 | EXTRA_PROGRAMS += test/test-rtsp-udp-stream-discovery 222 | BASE_FILES += \ 223 | src/avahi_publisher.cpp \ 224 | src/avahi_publisher.h 225 | 226 | AM_CXXFLAGS += ${AVAHI_CFLAGS} 227 | AM_CXXFLAGS += ${AVAHI_CFLAGS} 228 | dcm_LDADD += $(AVAHI_LIBS) 229 | 230 | test_test_rtsp_udp_stream_discovery_SOURCES = \ 231 | test/test_rtsp_udp_stream_discovery.cpp \ 232 | src/log.cpp \ 233 | src/log.h \ 234 | src/glib_mainloop.cpp \ 235 | src/glib_mainloop.h \ 236 | src/mainloop.cpp \ 237 | src/mainloop.h 238 | 239 | test_test_rtsp_udp_stream_discovery_LDADD = $(GLIB_LIBS) $(AVAHI_LIBS) 240 | endif 241 | 242 | if ENABLE_MAVLINK 243 | 244 | EXTRA_PROGRAMS += test/test-camera-parameters 245 | 246 | test_test_camera_parameters_SOURCES = \ 247 | src/CameraParameters.cpp \ 248 | src/CameraParameters.h \ 249 | test/test_camera_parameters.cpp \ 250 | src/log.cpp \ 251 | src/log.h \ 252 | src/util.c \ 253 | src/util.h 254 | 255 | test_test_camera_parameters_LDADD = $(GLIB_LIBS) 256 | 257 | EXTRA_PROGRAMS += test/test-mavlink-protocol 258 | 259 | BUILT_SOURCES += include/mavlink/ardupilotmega/mavlink.h 260 | include/mavlink/ardupilotmega/mavlink.h: modules/mavlink/pymavlink/tools/mavgen.py modules/mavlink/message_definitions/v1.0/ardupilotmega.xml 261 | $(AM_V_GEN)$(PYTHON) $(srcdir)/modules/mavlink/pymavlink/tools/mavgen.py \ 262 | -o include/mavlink \ 263 | --lang C \ 264 | --wire-protocol 2.0 \ 265 | $(srcdir)/modules/mavlink/message_definitions/v1.0/ardupilotmega.xml 266 | 267 | AM_CPPFLAGS += \ 268 | -I$(abs_top_builddir)/include/mavlink \ 269 | -I$(abs_top_builddir)/include/mavlink/ardupilotmega 270 | 271 | BASE_FILES += \ 272 | src/mavlink_server.cpp \ 273 | src/mavlink_server.h 274 | 275 | test_test_mavlink_protocol_SOURCES = \ 276 | test/test_mavlink_protocol.cpp \ 277 | src/log.cpp \ 278 | src/log.h \ 279 | src/glib_mainloop.cpp \ 280 | src/glib_mainloop.h \ 281 | src/mainloop.cpp \ 282 | src/mainloop.h \ 283 | src/pollable.cpp \ 284 | src/pollable.h \ 285 | src/socket.cpp \ 286 | src/socket.h \ 287 | src/util.c \ 288 | src/util.h 289 | 290 | test_test_mavlink_protocol_LDADD = $(GLIB_LIBS) 291 | if ENABLE_AVAHI 292 | test_test_mavlink_protocol_LDADD += $(AVAHI_LIBS) 293 | endif 294 | 295 | test_deps = test_test_mavlink_protocol_LDADD 296 | endif 297 | 298 | if ENABLE_GAZEBO 299 | 300 | AM_CXXFLAGS += \ 301 | ${GZB_CFLAGS} 302 | 303 | BASE_FILES += \ 304 | plugins/GazeboCamera/PluginGazebo.h \ 305 | plugins/GazeboCamera/PluginGazebo.cpp \ 306 | plugins/GazeboCamera/CameraDeviceGazebo.h \ 307 | plugins/GazeboCamera/CameraDeviceGazebo.cpp 308 | endif 309 | 310 | .PHONY: test 311 | test: $(test_deps) 312 | $(MAKE) $(EXTRA_PROGRAMS) 313 | 314 | # ------------------------------------------------------------------------------ 315 | # coverity 316 | # ------------------------------------------------------------------------------ 317 | 318 | #TODO 319 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # THIS REPOSITORY HAS BEEN DEPRECATED 2 | 3 | # Dronecode Camera Manager 4 | 5 | [![Build Status](https://travis-ci.org/Dronecode/camera-manager.svg?branch=master)](https://travis-ci.org/intel/camera-streaming-daemon) 6 | 7 | Coverity Scan Build Status 9 | 10 | 11 | The [Dronecode Camera Manager](https://camera-manager.dronecode.org/en/) (DCM) is an extensible Linux camera server for interfacing cameras with the Dronecode platform. 12 | 13 | It provides a [MAVLink Camera Protocol](https://mavlink.io/en/protocol/camera.html) compatible API for video and image capture etc., and an RTSP service for advertising and sharing video streams. The server can connect to and manage multiple cameras, and has been designed so that it can be extended to support new camera types and protocols when needed. 14 | 15 | > **Tip** The DCM is the easiest way for Camera OEMs to interface with the Dronecode platform. Many cameras will just work "out of the box". At most OEMs will need to implement a camera integration layer. 16 | 17 | Full instructions for [building](https://camera-manager.dronecode.org/en/getting_started/), [using](https://camera-manager.dronecode.org/en/guide/overview.html), [extending](https://camera-manager.dronecode.org/en/guide/extending.html) and [contributing](https://camera-manager.dronecode.org/en/contribute/) to the camera manager can be found in the [guide](https://camera-manager.dronecode.org/en/). 18 | -------------------------------------------------------------------------------- /autogen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | set -e 4 | 5 | oldpwd=$(pwd) 6 | topdir=$(dirname $0) 7 | cd $topdir 8 | 9 | autoreconf --force --install --symlink 10 | 11 | libdir() { 12 | echo $(cd "$1/$(gcc -print-multi-os-directory)"; pwd) 13 | } 14 | 15 | args="\ 16 | --sysconfdir=/etc \ 17 | --localstatedir=/var \ 18 | --libdir=$(libdir /usr/lib) \ 19 | " 20 | 21 | if [ -f "$topdir/.config.args" ]; then 22 | args="$args $(cat $topdir/.config.args)" 23 | fi 24 | 25 | if [ ! -L /bin ]; then 26 | args="$args \ 27 | --with-rootprefix= \ 28 | --with-rootlibdir=$(libdir /lib) \ 29 | " 30 | fi 31 | 32 | cd $oldpwd 33 | 34 | if [ $# -ge 1 ]; then 35 | cmd=$1 36 | shift 37 | fi 38 | 39 | args="$args $@" 40 | 41 | if [ "$cmd" = "c" ]; then 42 | $topdir/configure CFLAGS='-g -O2' CXXFLAGS='-g -O2' $args 43 | make clean 44 | elif [ "$cmd" = "g" ]; then 45 | $topdir/configure CFLAGS='-g -Og' CXXFLAGS='-g -O2' $args 46 | make clean 47 | elif [ "$cmd" = "l" ]; then 48 | $topdir/configure CC=clang CXX=clang++ CFLAGS='-g -O2' CXXFLAGS='-g -O2' $args 49 | make clean 50 | elif [ "$cmd" = "a" ]; then 51 | $topdir/configure CFLAGS='-g -O2 -Wsuggest-attribute=pure -Wsuggest-attribute=const' CXXFLAGS='-g -O2 -Wsuggest-attribute=pure -Wsuggest-attribute=const' $args 52 | make clean 53 | elif [ "$cmd" = "s" ]; then 54 | scan-build $topdir/configure CFLAGS='-g -O0 -std=gnu11' CXXFLAGS='-g -O0 -std=gnu++11' $args 55 | scan-build make 56 | else 57 | echo 58 | echo "----------------------------------------------------------------" 59 | echo "Initialized build system. For a common configuration please run:" 60 | echo "----------------------------------------------------------------" 61 | echo 62 | echo "$topdir/configure CFLAGS='-g -O2' CXXFLAGS='-g -O2' $args" 63 | echo 64 | fi 65 | -------------------------------------------------------------------------------- /configure.ac: -------------------------------------------------------------------------------- 1 | AC_PREREQ(2.64) 2 | AC_INIT([dronecode-camera-manager], 3 | [1], 4 | [], 5 | [dronecode-camera-manager], 6 | []) 7 | 8 | PKG_CHECK_MODULES(GLIB, [glib-2.0]) 9 | PKG_CHECK_MODULES(GST, [gstreamer-rtsp-1.0, gstreamer-1.0, gstreamer-rtsp-server-1.0 gstreamer-app-1.0]) 10 | 11 | AC_CONFIG_SRCDIR([src/main.cpp]) 12 | AC_CONFIG_MACRO_DIR([m4]) 13 | AC_CONFIG_HEADERS(config.h) 14 | AC_CONFIG_AUX_DIR([build-aux]) 15 | AC_ARG_ENABLE([realsense], AS_HELP_STRING([--enable-realsense], [Enable Realsense Support]), 16 | [ 17 | AC_SEARCH_LIBS([rs_create_context], [realsense], 18 | [ 19 | enable_realsense=yes 20 | AM_CONDITIONAL([ENABLE_REALSENSE], true) 21 | ], 22 | [ 23 | AC_MSG_ERROR([librealsense not installed, cannot enable realsense support]) 24 | enable_realsense=no 25 | AM_CONDITIONAL([ENABLE_REALSENSE], false) 26 | ]) 27 | ], 28 | [ 29 | enable_realsense=no 30 | AM_CONDITIONAL([ENABLE_REALSENSE], false) 31 | ]) 32 | AC_ARG_ENABLE([aero], AS_HELP_STRING([--enable-aero], [Include streams specific for Intel Aero drone]), 33 | [ 34 | enable_aero=yes 35 | AM_CONDITIONAL([ENABLE_AERO], true) 36 | ], 37 | [ 38 | enable_aero=no 39 | AM_CONDITIONAL([ENABLE_AERO], false) 40 | ]) 41 | 42 | AC_ARG_ENABLE([custom], AS_HELP_STRING([--enable-custom], [Include custom camera device]), 43 | [ 44 | enable_custom=yes 45 | AM_CONDITIONAL([ENABLE_CUSTOM], true) 46 | ], 47 | [ 48 | enable_custom=no 49 | AM_CONDITIONAL([ENABLE_CUSTOM], false) 50 | ]) 51 | 52 | AC_ARG_ENABLE([mavlink], 53 | AS_HELP_STRING([--enable-mavlink], [Enable MAVLink advertisement]), 54 | [ 55 | enable_mavlink=yes 56 | AM_CONDITIONAL([ENABLE_MAVLINK], true) 57 | AC_DEFINE(ENABLE_MAVLINK, 1, [Enable MAVLink advertisement]) 58 | ], 59 | [ 60 | enable_mavlink=no 61 | AM_CONDITIONAL([ENABLE_MAVLINK], false) 62 | ]) 63 | 64 | AC_ARG_ENABLE([avahi], 65 | AS_HELP_STRING([--enable-avahi], [Enable avahi advertisement]), 66 | [ 67 | PKG_CHECK_MODULES(AVAHI, [avahi-client, avahi-core, avahi-glib]) 68 | enable_avahi=yes 69 | AM_CONDITIONAL([ENABLE_AVAHI], true) 70 | AC_DEFINE(ENABLE_AVAHI, 1, [Enable avahi advertisement]) 71 | ], 72 | [ 73 | enable_avahi=no 74 | AM_CONDITIONAL([ENABLE_AVAHI], false) 75 | ]) 76 | 77 | 78 | AC_ARG_ENABLE([gazebo], 79 | AS_HELP_STRING([--enable-gazebo], [Enable Gazebo Simulator Mode]), 80 | [ 81 | PKG_CHECK_MODULES(GZB, [gazebo]) 82 | enable_gazebo=yes 83 | AM_CONDITIONAL([ENABLE_GAZEBO], true) 84 | AC_DEFINE(ENABLE_GAZEBO, 1, [Enable Gazebo Simulator Mode]) 85 | ], 86 | [ 87 | enable_gazebo=no 88 | AM_CONDITIONAL([ENABLE_GAZEBO], false) 89 | ]) 90 | 91 | 92 | AC_USE_SYSTEM_EXTENSIONS 93 | AC_SYS_LARGEFILE 94 | AC_PREFIX_DEFAULT([/usr]) 95 | PKG_PROG_PKG_CONFIG 96 | AM_MAINTAINER_MODE([enable]) 97 | AM_INIT_AUTOMAKE([check-news foreign 1.11 silent-rules tar-pax no-dist-gzip dist-xz subdir-objects color-tests parallel-tests]) 98 | AM_SILENT_RULES([yes]) 99 | 100 | ##################################################################### 101 | # Program checks and configurations 102 | ##################################################################### 103 | 104 | AC_PROG_CC_C99 105 | AX_CXX_COMPILE_STDCXX([11], [ext], [mandatory]) 106 | AM_PATH_PYTHON([2.7]) 107 | 108 | ##################################################################### 109 | 110 | LT_INIT([disable-static pic-only]) 111 | 112 | ##################################################################### 113 | # Function and structure checks 114 | ##################################################################### 115 | 116 | AC_MSG_CHECKING([whether _Static_assert() is supported]) 117 | AC_COMPILE_IFELSE( 118 | [AC_LANG_SOURCE([[_Static_assert(1, "Test");]])], 119 | [AC_DEFINE([HAVE_STATIC_ASSERT], [1], [Define if _Static_assert() is available]) 120 | AC_MSG_RESULT([yes])], 121 | [AC_MSG_RESULT([no])]) 122 | 123 | AC_MSG_CHECKING([whether _Noreturn is supported]) 124 | AC_COMPILE_IFELSE( 125 | [AC_LANG_SOURCE([[_Noreturn int foo(void) { exit(0); }]])], 126 | [AC_DEFINE([HAVE_NORETURN], [1], [Define if _Noreturn is available]) 127 | AC_MSG_RESULT([yes])], 128 | [AC_MSG_RESULT([no])]) 129 | 130 | 131 | ##################################################################### 132 | # --with- 133 | ##################################################################### 134 | 135 | AC_ARG_WITH([rootlibdir], 136 | AS_HELP_STRING([--with-rootlibdir=DIR], [rootfs directory to install shared libraries]), 137 | [], [with_rootlibdir=$libdir]) 138 | AC_ARG_WITH([systemdsystemunitdir], 139 | AC_HELP_STRING([--with-systemdsystemunitdir=DIR], 140 | [path to systemd system unit directory]), 141 | [path_systemunitdir=${withval}]) 142 | if (test "${enable_systemd}" != "no" && test -z "${path_systemunitdir}"); then 143 | AC_MSG_CHECKING([systemd system unit dir]) 144 | path_systemunitdir="`$PKG_CONFIG --variable=systemdsystemunitdir systemd`" 145 | if (test -z "${path_systemunitdir}"); then 146 | AC_MSG_ERROR([systemd system unit directory is required]) 147 | fi 148 | AC_MSG_RESULT([${path_systemunitdir}]) 149 | fi 150 | 151 | AC_SUBST([rootlibdir], [$with_rootlibdir]) 152 | 153 | ##################################################################### 154 | # --enable- 155 | ##################################################################### 156 | AC_ARG_ENABLE(systemd, AC_HELP_STRING([--disable-systemd], 157 | [disable systemd integration]), [enable_systemd=${enableval}]) 158 | AM_CONDITIONAL(SYSTEMD, test "${enable_systemd}" != "no") 159 | 160 | ##################################################################### 161 | # Default CFLAGS and LDFLAGS 162 | ##################################################################### 163 | DEFAULT_FLAGS=" -fstack-protector-all -fPIE -fPIC -O2 -D_FORTIFY_SOURCE=2 -pthread -Wformat -Wformat-security" 164 | CFLAGS+=$DEFAULT_FLAGS 165 | CXXFLAGS+=$DEFAULT_FLAGS 166 | LDFLAGS+=" -z noexecstack -z relro -z now" 167 | 168 | ##################################################################### 169 | # Generate files from *.in 170 | ##################################################################### 171 | 172 | AC_CONFIG_FILES([ 173 | Makefile 174 | ]) 175 | 176 | ##################################################################### 177 | AC_SUBST(SYSTEMD_SYSTEMUNITDIR, [${path_systemunitdir}]) 178 | 179 | AC_OUTPUT 180 | AC_MSG_RESULT([ 181 | $PACKAGE $VERSION 182 | ========================== 183 | 184 | prefix: ${prefix} 185 | sysconfdir: ${sysconfdir} 186 | libdir: ${libdir} 187 | rootlibdir: ${rootlibdir} 188 | includedir: ${includedir} 189 | bindir: ${bindir} 190 | 191 | C compiler: ${CC} 192 | C++ compiler: ${CXX} 193 | CFLAGS: ${CFLAGS} 194 | CXXFLAGS: ${CXXFLAGS} 195 | LDFLAGS: ${LDFLAGS} 196 | 197 | RealSense support: $enable_realsense 198 | MAVLink support: $enable_mavlink 199 | AVAHI support: $enable_avahi 200 | Intel Aero support: $enable_aero 201 | Gazebo support: $enable_gazebo 202 | Custom support: $enable_custom 203 | ]) 204 | -------------------------------------------------------------------------------- /dronecode-camera-manager.service.in: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=Dronecode Camera Manager 3 | After=multi-user.target 4 | 5 | [Service] 6 | Type=simple 7 | ExecStart=@bindir@/dcm 8 | Restart=on-failure 9 | 10 | [Install] 11 | WantedBy=multi-user.target 12 | -------------------------------------------------------------------------------- /m4/.gitignore: -------------------------------------------------------------------------------- 1 | libtool.m4 2 | ltoptions.m4 3 | ltsugar.m4 4 | ltversion.m4 5 | lt~obsolete.m4 6 | gtk-doc.m4 7 | -------------------------------------------------------------------------------- /plugins/AeroAtomIspCamera/CameraDeviceAeroAtomIsp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2018 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #pragma once 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #include "CameraDevice.h" 25 | #include "CameraParameters.h" 26 | 27 | class CameraDeviceAeroAtomIsp final : public CameraDevice { 28 | public: 29 | CameraDeviceAeroAtomIsp(std::string device); 30 | ~CameraDeviceAeroAtomIsp(); 31 | std::string getDeviceId() const; 32 | Status getInfo(CameraInfo &camInfo) const; 33 | bool isGstV4l2Src() const; 34 | Status init(CameraParameters &camParam); 35 | Status uninit(); 36 | Status start(); 37 | Status stop(); 38 | Status read(CameraData &data); 39 | Status setParam(CameraParameters &camParam, const std::string param, const char *param_value, 40 | const size_t value_size, const int param_type); 41 | Status resetParams(CameraParameters &camParam); 42 | Status setSize(const uint32_t width, const uint32_t height); 43 | Status getSize(uint32_t &width, uint32_t &height) const; 44 | Status getSupportedSizes(std::vector &sizes) const; 45 | Status setPixelFormat(const CameraParameters::PixelFormat format); 46 | Status getPixelFormat(CameraParameters::PixelFormat &format) const; 47 | Status getSupportedPixelFormats(std::vector &formats) const; 48 | Status setMode(const CameraParameters::Mode mode); 49 | Status getMode(CameraParameters::Mode &mode) const; 50 | Status getSupportedModes(std::vector &modes) const; 51 | Status setFrameRate(const uint32_t fps); 52 | Status getFrameRate(uint32_t &fps) const; 53 | Status getSupportedFrameRates(uint32_t &minFps, uint32_t &maxFps) const; 54 | Status setCameraDefinitionUri(const std::string uri); 55 | std::string getCameraDefinitionUri() const; 56 | 57 | private: 58 | CameraDevice::Status init(); 59 | Status setState(const CameraDevice::State state); 60 | CameraDevice::State getState() const; 61 | int allocFrameBuffer(int bufCnt, size_t bufSize); 62 | int freeFrameBuffer(); 63 | int pollCamera(int fd); 64 | std::string mDeviceId; 65 | std::atomic mState; 66 | uint32_t mWidth; 67 | uint32_t mHeight; 68 | CameraParameters::PixelFormat mPixelFormat; 69 | CameraParameters::Mode mMode; 70 | uint32_t mFrmRate; 71 | std::string mCamDefUri; 72 | std::mutex mLock; 73 | int mFd; 74 | void **mFrameBuffer; 75 | size_t mFrameBufferSize; 76 | uint32_t mFrameBufferCnt; 77 | }; 78 | -------------------------------------------------------------------------------- /plugins/AeroAtomIspCamera/PluginAeroAtomIsp.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2018 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #include 19 | #include 20 | 21 | #include "CameraDeviceAeroAtomIsp.h" 22 | #include "PluginAeroAtomIsp.h" 23 | 24 | static PluginAeroAtomIsp aero; 25 | 26 | PluginAeroAtomIsp::PluginAeroAtomIsp() 27 | : PluginBase() 28 | { 29 | /* 30 | * 1. Discover the list of camera devices for this Plugin Class 31 | * 2. Add the IDs of the devices in the list 32 | */ 33 | 34 | discoverCameras(mCamList); 35 | } 36 | 37 | PluginAeroAtomIsp::~PluginAeroAtomIsp() 38 | { 39 | } 40 | 41 | std::vector PluginAeroAtomIsp::getCameraDevices() 42 | { 43 | return mCamList; 44 | } 45 | 46 | std::shared_ptr PluginAeroAtomIsp::createCameraDevice(std::string deviceID) 47 | { 48 | /* check if the device exists in the list */ 49 | if (std::find(mCamList.begin(), mCamList.end(), deviceID) == mCamList.end()) { 50 | log_error("Camera Device not found : %s", deviceID.c_str()); 51 | return nullptr; 52 | } 53 | 54 | return std::make_shared(deviceID); 55 | } 56 | 57 | void PluginAeroAtomIsp::discoverCameras(std::vector &camList) 58 | { 59 | /* 60 | * Add the logic to discover the camera devices 61 | * For RealSense cameras, its hardcoded 62 | */ 63 | 64 | /* Bottom Camera */ 65 | camList.push_back("bottom"); 66 | /* Front Camera*/ 67 | /* camList.push_back("front"); */ 68 | 69 | return; 70 | } 71 | -------------------------------------------------------------------------------- /plugins/AeroAtomIspCamera/PluginAeroAtomIsp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2018 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #pragma once 19 | 20 | #include 21 | #include 22 | 23 | #include "CameraDevice.h" 24 | #include "PluginBase.h" 25 | 26 | class PluginAeroAtomIsp final : public PluginBase { 27 | public: 28 | PluginAeroAtomIsp(); 29 | ~PluginAeroAtomIsp(); 30 | 31 | std::vector getCameraDevices(); 32 | std::shared_ptr createCameraDevice(std::string); 33 | 34 | private: 35 | std::vector mCamList; 36 | void discoverCameras(std::vector &camList); 37 | }; 38 | -------------------------------------------------------------------------------- /plugins/CustomCamera/CameraDeviceCustom.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2017 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #pragma once 19 | #include 20 | #include 21 | #include 22 | 23 | #include "CameraDevice.h" 24 | #include "CameraParameters.h" 25 | 26 | class CameraDeviceCustom final : public CameraDevice { 27 | public: 28 | CameraDeviceCustom(std::string device); 29 | ~CameraDeviceCustom(); 30 | std::string getDeviceId() const; 31 | Status getInfo(CameraInfo &camInfo) const; 32 | bool isGstV4l2Src() const; 33 | Status init(CameraParameters &camParam); 34 | Status uninit(); 35 | Status start(); 36 | Status stop(); 37 | Status read(CameraData &data); 38 | Status setParam(CameraParameters &camParam, const std::string param, const char *param_value, 39 | const size_t value_size, const int param_type); 40 | Status resetParams(CameraParameters &camParam); 41 | Status setSize(const uint32_t width, const uint32_t height); 42 | Status getSize(uint32_t &width, uint32_t &height) const; 43 | Status getSupportedSizes(std::vector &sizes) const; 44 | Status setPixelFormat(const CameraParameters::PixelFormat format); 45 | Status getPixelFormat(CameraParameters::PixelFormat &format) const; 46 | Status getSupportedPixelFormats(std::vector &formats) const; 47 | Status setMode(const CameraParameters::Mode mode); 48 | Status getMode(CameraParameters::Mode &mode) const; 49 | Status getSupportedModes(std::vector &modes) const; 50 | Status setFrameRate(const uint32_t fps); 51 | Status getFrameRate(uint32_t &fps) const; 52 | Status getSupportedFrameRates(uint32_t &minFps, uint32_t &maxFps); 53 | Status setCameraDefinitionUri(const std::string uri); 54 | std::string getCameraDefinitionUri() const; 55 | std::string getOverlayText() const; 56 | 57 | private: 58 | // Declare parameter name & ID 59 | // Possible values of the params are in camera definition file 60 | static const char PARAMETER_CUSTOM_UINT8[]; 61 | static const int ID_PARAMETER_CUSTOM_UINT8; 62 | static const char PARAMETER_CUSTOM_UINT32[]; 63 | static const int ID_PARAMETER_CUSTOM_UINT32; 64 | static const char PARAMETER_CUSTOM_INT32[]; 65 | static const int ID_PARAMETER_CUSTOM_INT32; 66 | static const char PARAMETER_CUSTOM_REAL32[]; 67 | static const int ID_PARAMETER_CUSTOM_REAL32; 68 | static const char PARAMETER_CUSTOM_ENUM[]; 69 | static const int ID_PARAMETER_CUSTOM_ENUM; 70 | std::string mDeviceId; 71 | std::atomic mState; 72 | uint32_t mWidth; 73 | uint32_t mHeight; 74 | CameraParameters::PixelFormat mPixelFormat; 75 | CameraParameters::Mode mMode; 76 | uint32_t mFrmRate; 77 | std::string mCamDefUri; 78 | std::string mOvText; 79 | }; 80 | -------------------------------------------------------------------------------- /plugins/CustomCamera/PluginCustom.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2018 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | #include 20 | #include 21 | 22 | #include "CameraDeviceCustom.h" 23 | #include "PluginCustom.h" 24 | 25 | static PluginCustom custom; 26 | 27 | PluginCustom::PluginCustom() 28 | : PluginBase() 29 | { 30 | /* 31 | * 1. Discover the list of camera devices for this Plugin Class 32 | * 2. Add the IDs of the devices in the list 33 | */ 34 | 35 | discoverCameras(mCamList); 36 | } 37 | 38 | PluginCustom::~PluginCustom() 39 | { 40 | } 41 | 42 | std::vector PluginCustom::getCameraDevices() 43 | { 44 | return mCamList; 45 | } 46 | 47 | std::shared_ptr PluginCustom::createCameraDevice(std::string deviceID) 48 | { 49 | // check if the device exists in the list 50 | if (std::find(mCamList.begin(), mCamList.end(), deviceID) == mCamList.end()) { 51 | log_error("Camera Device not found : %s", deviceID.c_str()); 52 | return nullptr; 53 | } 54 | 55 | return std::make_shared(deviceID); 56 | } 57 | 58 | void PluginCustom::discoverCameras(std::vector &camList) 59 | { 60 | /* 61 | * 1. Add the logic to discover the camera devices 62 | * 2. For V4L2, its scanning the video* nodes in /dev/ dir 63 | * 3. For Gazebo, the topics with "camera/image" is assumed to be a camera 64 | */ 65 | 66 | return; 67 | } 68 | -------------------------------------------------------------------------------- /plugins/CustomCamera/PluginCustom.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2018 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | #include 20 | #include 21 | 22 | #include "CameraDevice.h" 23 | #include "PluginBase.h" 24 | 25 | class PluginCustom final : public PluginBase { 26 | public: 27 | PluginCustom(); 28 | ~PluginCustom(); 29 | 30 | std::vector getCameraDevices(); 31 | std::shared_ptr createCameraDevice(std::string); 32 | 33 | private: 34 | std::vector mCamList; 35 | void discoverCameras(std::vector &camList); 36 | }; 37 | -------------------------------------------------------------------------------- /plugins/GazeboCamera/CameraDeviceGazebo.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2017 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #pragma once 19 | #include 20 | #include 21 | #include 22 | 23 | #include "CameraDevice.h" 24 | #include "CameraParameters.h" 25 | 26 | class CameraDeviceGazebo final : public CameraDevice { 27 | public: 28 | CameraDeviceGazebo(std::string device); 29 | ~CameraDeviceGazebo(); 30 | std::string getDeviceId() const; 31 | Status getInfo(CameraInfo &camInfo) const; 32 | bool isGstV4l2Src() const; 33 | Status init(CameraParameters &camParam); 34 | Status uninit(); 35 | Status start(); 36 | Status stop(); 37 | Status read(CameraData &data); 38 | Status setParam(CameraParameters &camParam, const std::string param, const char *param_value, 39 | const size_t value_size, const int param_type); 40 | Status resetParams(CameraParameters &camParam); 41 | Status getSize(uint32_t &width, uint32_t &height) const; 42 | Status getPixelFormat(CameraParameters::PixelFormat &format) const; 43 | Status setMode(const CameraParameters::Mode mode); 44 | Status getMode(CameraParameters::Mode &mode) const; 45 | Status setCameraDefinitionUri(const std::string uri); 46 | std::string getCameraDefinitionUri() const; 47 | std::string getOverlayText() const; 48 | 49 | private: 50 | // Declare parameter name & ID 51 | // Possible values of the params are in camera definition file 52 | static const char PARAMETER_CUSTOM_UINT8[]; 53 | static const int ID_PARAMETER_CUSTOM_UINT8; 54 | static const char PARAMETER_CUSTOM_UINT32[]; 55 | static const int ID_PARAMETER_CUSTOM_UINT32; 56 | static const char PARAMETER_CUSTOM_INT32[]; 57 | static const int ID_PARAMETER_CUSTOM_INT32; 58 | static const char PARAMETER_CUSTOM_REAL32[]; 59 | static const int ID_PARAMETER_CUSTOM_REAL32; 60 | static const char PARAMETER_CUSTOM_ENUM[]; 61 | static const int ID_PARAMETER_CUSTOM_ENUM; 62 | int setOverlayText(std::string text); 63 | void cbOnImages(ConstImagesStampedPtr &_msg); 64 | int getImage(const gazebo::msgs::Image &_msg); 65 | std::string mDeviceId; 66 | std::atomic mState; 67 | uint32_t mWidth; 68 | uint32_t mHeight; 69 | CameraParameters::Mode mMode; 70 | CameraParameters::PixelFormat mPixelFormat; 71 | std::string mCamDefUri; 72 | std::string mTopicName; 73 | gazebo::transport::NodePtr mNode; 74 | gazebo::transport::SubscriberPtr mSub; 75 | std::mutex mLock; 76 | std::vector mFrameBuffer = {}; 77 | std::string mOvText; 78 | }; 79 | -------------------------------------------------------------------------------- /plugins/GazeboCamera/PluginGazebo.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2018 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include "CameraDeviceGazebo.h" 26 | #include "PluginGazebo.h" 27 | 28 | #define GZB_CMD_TOPIC "gz topic -l | grep \"camera/image\" " 29 | 30 | static PluginGazebo gzb; 31 | 32 | PluginGazebo::PluginGazebo() 33 | : PluginBase() 34 | { 35 | discoverCameras(mCamList); 36 | } 37 | 38 | PluginGazebo::~PluginGazebo() 39 | { 40 | } 41 | 42 | std::vector PluginGazebo::getCameraDevices() 43 | { 44 | return mCamList; 45 | } 46 | 47 | std::shared_ptr PluginGazebo::createCameraDevice(std::string deviceID) 48 | { 49 | // check if the device exists in the list 50 | if (std::find(mCamList.begin(), mCamList.end(), deviceID) == mCamList.end()) { 51 | log_error("Camera Device not found : %s", deviceID.c_str()); 52 | return nullptr; 53 | } 54 | 55 | return std::make_shared(deviceID); 56 | } 57 | 58 | void PluginGazebo::discoverCameras(std::vector &camList) 59 | { 60 | std::string result; 61 | std::array buffer; 62 | std::shared_ptr pipe(popen(GZB_CMD_TOPIC, "r"), pclose); 63 | if (!pipe) { 64 | log_error("popen() failed!"); 65 | return; 66 | } 67 | while (!feof(pipe.get())) { 68 | if (fgets(buffer.data(), 128, pipe.get()) != nullptr) { 69 | result = buffer.data(); 70 | // Remove newline at the end 71 | result.pop_back(); 72 | camList.push_back(result); 73 | } 74 | } 75 | 76 | return; 77 | } 78 | -------------------------------------------------------------------------------- /plugins/GazeboCamera/PluginGazebo.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2018 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | #include 20 | #include 21 | 22 | #include "CameraDevice.h" 23 | #include "PluginBase.h" 24 | 25 | class PluginGazebo final : public PluginBase { 26 | public: 27 | PluginGazebo(); 28 | ~PluginGazebo(); 29 | 30 | std::vector getCameraDevices(); 31 | std::shared_ptr createCameraDevice(std::string); 32 | 33 | private: 34 | std::vector mCamList; 35 | void discoverCameras(std::vector &camList); 36 | }; 37 | -------------------------------------------------------------------------------- /plugins/RealSenseCamera/CameraDeviceRealSense.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2018 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #pragma once 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #include 25 | 26 | #include "CameraDevice.h" 27 | #include "CameraParameters.h" 28 | 29 | class CameraDeviceRealSense final : public CameraDevice { 30 | public: 31 | CameraDeviceRealSense(std::string device); 32 | ~CameraDeviceRealSense(); 33 | std::string getDeviceId() const; 34 | Status getInfo(CameraInfo &camInfo) const; 35 | bool isGstV4l2Src() const; 36 | Status init(CameraParameters &camParam); 37 | Status uninit(); 38 | Status start(); 39 | Status stop(); 40 | Status read(CameraData &data); 41 | Status setParam(CameraParameters &camParam, const std::string param, const char *param_value, 42 | const size_t value_size, const int param_type); 43 | Status resetParams(CameraParameters &camParam); 44 | Status setSize(const uint32_t width, const uint32_t height); 45 | Status getSize(uint32_t &width, uint32_t &height) const; 46 | Status getSupportedSizes(std::vector &sizes) const; 47 | Status setPixelFormat(const CameraParameters::PixelFormat format); 48 | Status getPixelFormat(CameraParameters::PixelFormat &format) const; 49 | Status getSupportedPixelFormats(std::vector &formats) const; 50 | Status setMode(const CameraParameters::Mode mode); 51 | Status getMode(CameraParameters::Mode &mode) const; 52 | Status getSupportedModes(std::vector &modes) const; 53 | Status setFrameRate(const uint32_t fps); 54 | Status getFrameRate(uint32_t &fps) const; 55 | Status getSupportedFrameRates(uint32_t &minFps, uint32_t &maxFps) const; 56 | Status setCameraDefinitionUri(const std::string uri); 57 | std::string getCameraDefinitionUri() const; 58 | 59 | private: 60 | Status setState(const CameraDevice::State state); 61 | CameraDevice::State getState() const; 62 | std::string mDeviceId; 63 | std::atomic mState; 64 | uint32_t mWidth; 65 | uint32_t mHeight; 66 | CameraParameters::PixelFormat mPixelFormat; 67 | CameraParameters::Mode mMode; 68 | uint32_t mFrmRate; 69 | std::string mCamDefUri; 70 | std::mutex mLock; 71 | uint8_t *mFrameBuffer; 72 | size_t mFrameBufferSize; 73 | rs_device *mRSDev; 74 | rs_context *mRSCtx; 75 | int mRSStream; 76 | static int sStrmCnt; 77 | }; 78 | -------------------------------------------------------------------------------- /plugins/RealSenseCamera/PluginRealSense.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2018 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | #include 20 | #include 21 | 22 | #include "CameraDeviceRealSense.h" 23 | #include "PluginRealSense.h" 24 | 25 | static PluginRealSense rs; 26 | 27 | PluginRealSense::PluginRealSense() 28 | : PluginBase() 29 | { 30 | /* 31 | * 1. Discover the list of camera devices for this Plugin Class 32 | * 2. Add the IDs of the devices in the list 33 | */ 34 | 35 | discoverCameras(mCamList); 36 | } 37 | 38 | PluginRealSense::~PluginRealSense() 39 | { 40 | } 41 | 42 | std::vector PluginRealSense::getCameraDevices() 43 | { 44 | return mCamList; 45 | } 46 | 47 | std::shared_ptr PluginRealSense::createCameraDevice(std::string deviceID) 48 | { 49 | /* check if the device exists in the list */ 50 | if (std::find(mCamList.begin(), mCamList.end(), deviceID) == mCamList.end()) { 51 | log_error("Camera Device not found : %s", deviceID.c_str()); 52 | return nullptr; 53 | } 54 | 55 | return std::make_shared(deviceID); 56 | } 57 | 58 | void PluginRealSense::discoverCameras(std::vector &camList) 59 | { 60 | /* 61 | * Add the logic to discover the camera devices 62 | * For RealSense cameras, its hardcoded 63 | */ 64 | 65 | /* Depth Camera */ 66 | camList.push_back("rsdepth"); 67 | /* Infrared Camera*/ 68 | camList.push_back("rsir"); 69 | /* Infrared Camera2*/ 70 | camList.push_back("rsir2"); 71 | 72 | return; 73 | } 74 | -------------------------------------------------------------------------------- /plugins/RealSenseCamera/PluginRealSense.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2018 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #pragma once 19 | 20 | #include 21 | #include 22 | 23 | #include "CameraDevice.h" 24 | #include "PluginBase.h" 25 | 26 | class PluginRealSense final : public PluginBase { 27 | public: 28 | PluginRealSense(); 29 | ~PluginRealSense(); 30 | 31 | std::vector getCameraDevices(); 32 | std::shared_ptr createCameraDevice(std::string); 33 | 34 | private: 35 | std::vector mCamList; 36 | void discoverCameras(std::vector &camList); 37 | }; 38 | -------------------------------------------------------------------------------- /plugins/V4l2Camera/CameraDeviceV4l2.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2017 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #pragma once 19 | #include 20 | #include 21 | 22 | #include "CameraDevice.h" 23 | #include "CameraParameters.h" 24 | 25 | class CameraDeviceV4l2 final : public CameraDevice { 26 | public: 27 | CameraDeviceV4l2(std::string device); 28 | ~CameraDeviceV4l2(); 29 | std::string getDeviceId() const; 30 | Status getInfo(CameraInfo &camInfo) const; 31 | bool isGstV4l2Src() const; 32 | Status init(CameraParameters &camParam); 33 | Status uninit(); 34 | Status start(); 35 | Status stop(); 36 | Status setParam(CameraParameters &camParam, const std::string param, const char *param_value, 37 | const size_t value_size, const int param_type); 38 | Status resetParams(CameraParameters &camParam); 39 | Status setSize(const uint32_t width, const uint32_t height); 40 | Status setPixelFormat(const CameraParameters::PixelFormat format); 41 | Status setMode(const CameraParameters::Mode mode); 42 | Status getMode(CameraParameters::Mode &mode) const; 43 | Status setCameraDefinitionUri(const std::string uri); 44 | std::string getCameraDefinitionUri() const; 45 | 46 | private: 47 | std::string mDeviceId; 48 | std::string mCardName; 49 | std::string mDriverName; 50 | std::string mCamDefURI; 51 | uint32_t mVersion; 52 | CameraParameters::Mode mMode; 53 | int initInfo(); 54 | int initParams(CameraParameters &camParam); 55 | int declareParams(CameraParameters &camParam); 56 | int resetV4l2Params(CameraParameters &camParam); 57 | int declareV4l2Params(CameraParameters &camParam); 58 | std::string getParamName(int cid); 59 | int getParamId(int cid); 60 | CameraParameters::param_type getParamType(v4l2_ctrl_type type); 61 | int getV4l2ControlId(int paramId); 62 | int setV4l2Control(int ctrl_id, int value); 63 | }; 64 | -------------------------------------------------------------------------------- /plugins/V4l2Camera/PluginV4l2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2018 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | #include 20 | 21 | #include "PluginV4l2.h" 22 | #include "v4l2_interface.h" 23 | 24 | static PluginV4l2 v4l2; 25 | 26 | PluginV4l2::PluginV4l2() 27 | : PluginBase() 28 | { 29 | v4l2_list_devices(mCamList); 30 | } 31 | 32 | PluginV4l2::~PluginV4l2() 33 | { 34 | } 35 | 36 | std::vector PluginV4l2::getCameraDevices() 37 | { 38 | return mCamList; 39 | } 40 | 41 | std::shared_ptr PluginV4l2::createCameraDevice(std::string deviceID) 42 | { 43 | // check if the device exists in the list 44 | if (std::find(mCamList.begin(), mCamList.end(), deviceID) == mCamList.end()) { 45 | log_error("Camera Device not found : %s", deviceID.c_str()); 46 | return nullptr; 47 | } 48 | 49 | return std::make_shared(deviceID); 50 | } 51 | -------------------------------------------------------------------------------- /plugins/V4l2Camera/PluginV4l2.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2018 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | #include 20 | #include 21 | 22 | #include "CameraDevice.h" 23 | #include "CameraDeviceV4l2.h" 24 | #include "PluginBase.h" 25 | 26 | class PluginV4l2 final : public PluginBase { 27 | public: 28 | PluginV4l2(); 29 | ~PluginV4l2(); 30 | 31 | std::vector getCameraDevices(); 32 | std::shared_ptr createCameraDevice(std::string); 33 | 34 | private: 35 | std::vector mCamList; 36 | }; 37 | -------------------------------------------------------------------------------- /samples/config/aero.conf: -------------------------------------------------------------------------------- 1 | [gstreamer] 2 | muxer=rtph264pay 3 | encoder=vaapih264enc 4 | converter=autovideoconvert 5 | 6 | [v4l2] 7 | blacklist=video0,video1,video3,video4,video5,video6,video7,video8,video9,video10,video11,video12 8 | 9 | [mavlink] 10 | broadcast_addr=127.0.0.1 11 | port=80550 12 | rtsp_server_addr=192.168.8.1 13 | 14 | [uri] 15 | video13=http://192.168.8.1:8000/camera-def-rs-rgb.xml 16 | -------------------------------------------------------------------------------- /samples/config/config.sample: -------------------------------------------------------------------------------- 1 | # camera-streaming-daemon configuration file is composed of sections, 2 | # each section has some keys and values. They 3 | # are case insensitive, so `Key=Value` is the same as `key=value`. 4 | # 5 | # [This-is-a-section] 6 | # ThisIsAKey=ThisIsAValue 7 | # =ThisIsAValue 8 | # Following specifications of expected sessions and key/values. 9 | # 10 | # Section [Mavlink]: 11 | # 12 | # Keys: 13 | # Port 14 | # MAVLink destination UDP port. 15 | # Default: 14550 16 | # 17 | # System_ID 18 | # System ID of the Camera-Streaming-Daemon to be used in MAVLink 19 | # communications. 20 | # Default: 42 21 | # 22 | # Rtsp_Server_Addr 23 | # IP address or hostname of the interface where the rtsp server is 24 | # running. This is the address that will be used by the client to 25 | # make the rtsp request. 26 | # Default: 0.0.0.0 27 | # 28 | # Broadcast_Addr 29 | # Broadcast address to send MAVLink heartbeat messages. 30 | # Default: 255.255.255.255 31 | # 32 | # Section [Gstreamer]: 33 | # 34 | # Keys: 35 | # Muxer 36 | # Muxer used to create the gstreamer pipeline 37 | # Default: rtph264pay 38 | # 39 | # Converter 40 | # Converter used to create the gstreamer pipeline 41 | # Default: autovideoconvert 42 | # 43 | # Encoder 44 | # Muxer used to create the gstreamer pipeline 45 | # Default: x264enc 46 | # 47 | # Section [V4L2] 48 | # 49 | # Keys: 50 | # Blacklist 51 | # Coma separated list of /dev/video devices that won't be exported by 52 | # camera-streaming-daemon. If the video streams from cameras /dev/video123 53 | # and /dev/video456 can't be exported, set: 54 | # blacklist = video123,video456 55 | # Default: 56 | # 57 | # Section [uri]: 58 | # 59 | # Keys: 60 | # 61 | # URI for the Camera Definition File of the camera device 62 | # video0 = http://ipofdronefromgroundstation/video0_camera_definition.xml 63 | # 64 | # 65 | # Section [gazebo]: 66 | # 67 | # Keys: 68 | # camtopic 69 | # Gazebo topic where camera images are published. 70 | # 71 | # 72 | # Section [imgcap]: 73 | # 74 | # Keys: 75 | # width 76 | # Width of the image to be captured in pixels. 77 | # Default: Full width of camera frame for sensor type (i.e 1080P - 1920, 720P - 1280, etc) 78 | # Possible Values: Dependent on camera sensor 79 | # 80 | # height 81 | # Height of the image to be captured in pixels. 82 | # Default: Full height of camera frame for sensor type (i.e 1080P - 1080, 720P - 720, etc) 83 | # Possible Values: Dependent on camera sensor 84 | # 85 | # format 86 | # Image format 87 | # Default: 2 (JPEG) 88 | # Possible Values: 89 | # 1 - Not Supported (IMAGE_FILE_RAW) 90 | # 2 - JPEG Format (IMAGE_FILE_JPEG) 91 | # 3 - Not Supported (IMAGE_FILE_EXIF) 92 | # 4 - Not Supported (IMAGE_FILE_TIFF) 93 | # 5 - Not Supported (IMAGE_FILE_GIF) 94 | # 6 - Not Supported (IMAGE_FILE_PNG) 95 | # 7 - Not Supported (IMAGE_FILE_BMP) 96 | # 97 | # location 98 | # Location of the image file to write 99 | # Default: /tmp/ 100 | # Possible Values: The path should be accessible and writeable 101 | # 102 | # 103 | # Section [vidcap]: 104 | # 105 | # Keys: 106 | # width 107 | # Width of the video to be captured in pixels. 108 | # Default: Full width of camera frame for sensor type (i.e 1080P - 1920, 720P - 1280, etc) 109 | # Possible Values: Dependent on camera sensor 110 | # 111 | # height 112 | # Height of the video to be captured in pixels. 113 | # Default: Full height of camera frame for sensor type (i.e 1080P - 1080, 720P - 720, etc) 114 | # Possible Values: Dependent on camera sensor 115 | # 116 | # framerate 117 | # Camera framerate for video capture 118 | # Default: Default framerate of camera sensor (eg - 25) 119 | # Possible Values: Dependent on camera sensor 120 | # 121 | # bitrate 122 | # Bitrate of the encoded video data in kbps 123 | # Default: 512 124 | # Possible Values: [1,2048000] 125 | # 126 | # encoder 127 | # Video encoding format 128 | # Default: 3 (AVC) 129 | # Possible Values: 130 | # 1 - Not Supported (VIDEO_CODING_H263) 131 | # 2 - Not Supported (VIDEO_CODING_MPEG4) 132 | # 3 - H.264 (VIDEO_CODING_AVC) 133 | # 4 - Not Supported (VIDEO_CODING_MJPEG) 134 | # 5 - Not Supported (VIDEO_CODING_WMV) 135 | # 136 | # format 137 | # Video file format 138 | # Default: 1 (MP4) 139 | # Possible Values: 140 | # 1 - Moving Pictures Expert Group 4 (VIDEO_FILE_MP4) 141 | # 2 - Not Supported (VIDEO_FILE_AVI) 142 | # 3 - Not Supported (VIDEO_FILE_WMV) 143 | # 4 - Not Supported (VIDEO_FILE_FLV) 144 | # 5 - Not Supported (VIDEO_FILE_MOV) 145 | # 146 | # location 147 | # Location of the video file to write 148 | # Default: /tmp/ 149 | # Possible Values: The path that is accessible and writeable 150 | # 151 | # Section [rtsp]: 152 | # 153 | # Keys: 154 | # pipeline 155 | # A gstreamer pipeline to transmit the video to the ground station. 156 | # Default: none 157 | # [rtsp] 158 | # pipeline=v4l2src device=/dev/video0 ! videoconvert ! video/x-raw, format=I420 ! x264enc speed-preset=ultrafast tune=zerolatency ! rtph264pay name=pay0 159 | # 160 | -------------------------------------------------------------------------------- /samples/config/gazebo.conf: -------------------------------------------------------------------------------- 1 | [mavlink] 2 | port=14550 3 | broadcast_addr=127.0.0.255 4 | rtsp_server_addr=127.0.0.1 5 | system_id=1 6 | 7 | [uri] 8 | gazebo=http://127.0.0.1:8000/camera-def-gazebo.xml 9 | 10 | [imgcap] 11 | location=~/Temp/ 12 | 13 | [gazebo] 14 | camtopic=~/typhoon_h480/cgo3_camera_link/camera/image 15 | 16 | [rtsp] 17 | pipeline=appsrc name=mysrc ! videoconvert ! video/x-raw, format=I420 ! vaapih264enc ! rtph264pay name=pay0 18 | -------------------------------------------------------------------------------- /samples/config/picam.conf: -------------------------------------------------------------------------------- 1 | [mavlink] 2 | port=14550 3 | broadcast_addr=127.0.0.255 4 | rtsp_server_addr=127.0.0.1 5 | system_id=1 6 | 7 | [uri] 8 | video0=http://127.0.0.1:8000/camera-def-picam.xml 9 | 10 | [vidcap] 11 | width=640 12 | height=480 13 | framerate=25 14 | bitrate=1000 15 | encoder=6 16 | format=1 17 | location=/tmp/ 18 | 19 | [rtsp] 20 | pipeline=rpicamsrc bitrate=1000000 ! video/x-h264, width=640, height=480, framerate=25/1 ! h264parse ! rtph264pay name=pay0 21 | 22 | -------------------------------------------------------------------------------- /samples/config/ubuntu.conf: -------------------------------------------------------------------------------- 1 | [mavlink] 2 | port=14550 3 | broadcast_addr=127.0.0.255 4 | rtsp_server_addr=127.0.0.1 5 | system_id=1 6 | 7 | [uri] 8 | video0=http://127.0.0.1:8000/camera-def-uvc.xml 9 | 10 | [imgcap] 11 | width=640 12 | height=480 13 | format=2 14 | location=/tmp/ 15 | 16 | [vidcap] 17 | width=640 18 | height=480 19 | framerate=25 20 | bitrate=1000 21 | encoder=3 22 | format=1 23 | location=/tmp/ 24 | 25 | [rtsp] 26 | pipeline=v4l2src device=/dev/video0 ! videoconvert ! video/x-raw, format=I420 ! x264enc speed-preset=ultrafast tune=zerolatency ! rtph264pay name=pay0 27 | -------------------------------------------------------------------------------- /samples/def/camera-def-gazebo.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Gazebo Camera 5 | PX4 SITL 6 | 7 | 8 | 9 | 10 | Camera Mode 11 | 12 | 17 | 19 | 20 | 21 | 22 | ParamInt32 23 | 24 | 25 | ParamUint32 26 | 27 | 28 | ParamInt32 29 | 30 | 31 | ParamInt32 32 | 33 | 34 | ParamEnum 35 | 36 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /samples/def/camera-def-picam.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | PiCam 5 | Raspberry PI Cam 6 | 7 | 8 | 9 | Camera Mode 10 | 11 | 16 | 18 | 19 | 20 | 21 | Brightness 22 | 23 | 24 | Contrast 25 | 26 | 27 | Saturation 28 | 29 | 30 | Gain 31 | 32 | 33 | Sharpness 34 | 35 | 36 | Backlight Compensation 37 | 38 | 39 | Power Line Frequency 40 | 41 | 45 | 46 | 47 | White Balance Mode 48 | 49 | 55 | 56 | 57 | 58 | White Balance Temperature 59 | 60 | 61 | Exposure Mode 62 | 63 | 69 | 70 | 71 | 72 | Exposure Absolute 73 | 74 | 75 | Exposure Auto Priority 76 | 77 | 80 | 81 | 82 | 83 | -------------------------------------------------------------------------------- /samples/def/camera-def-rs-rgb.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | RealSense RGB Camera 5 | Intel Corporation 6 | 7 | 8 | 9 | 10 | Camera Mode 11 | 12 | 17 | 19 | 20 | 21 | 22 | Brightness 23 | 24 | 25 | Contrast 26 | 27 | 28 | Saturation 29 | 30 | 31 | Hue 32 | 33 | 34 | Gamma 35 | 36 | 37 | Gain 38 | 39 | 40 | Sharpness 41 | 42 | 43 | Backlight Compensation 44 | 45 | 46 | Power Line Frequency 47 | 48 | 52 | 53 | 54 | White Balance Mode 55 | 56 | 62 | 63 | 64 | 65 | White Balance Temperature 66 | 67 | 68 | Exposure Mode 69 | 70 | 76 | 77 | 78 | 79 | Exposure Absolute 80 | 81 | 82 | Video Resolution 83 | 84 | 105 | 106 | 107 | 108 | -------------------------------------------------------------------------------- /samples/def/camera-def-uvc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | UVC Camera 5 | Logitech C270HD Webcam 6 | 7 | 8 | 9 | Camera Mode 10 | 11 | 16 | 18 | 19 | 20 | 21 | Brightness 22 | 23 | 24 | Contrast 25 | 26 | 27 | Saturation 28 | 29 | 30 | Gain 31 | 32 | 33 | Sharpness 34 | 35 | 36 | Backlight Compensation 37 | 38 | 39 | Power Line Frequency 40 | 41 | 45 | 46 | 47 | White Balance Mode 48 | 49 | 55 | 56 | 57 | 58 | White Balance Temperature 59 | 60 | 61 | Exposure Mode 62 | 63 | 69 | 70 | 71 | 72 | Exposure Absolute 73 | 74 | 75 | Exposure Auto Priority 76 | 77 | 80 | 81 | 82 | 83 | -------------------------------------------------------------------------------- /src/CameraComponent.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2017 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #pragma once 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include "CameraDevice.h" 26 | #include "CameraParameters.h" 27 | #include "ImageCapture.h" 28 | #include "VideoCapture.h" 29 | #include "VideoStream.h" 30 | #include "log.h" 31 | 32 | struct StorageInfo { 33 | uint8_t storage_id; 34 | uint8_t storage_count; 35 | uint8_t status; 36 | float total_capacity; 37 | float used_capacity; 38 | float available_capacity; 39 | float read_speed; 40 | float write_speed; 41 | }; 42 | 43 | class CameraDevice; 44 | 45 | class CameraComponent { 46 | public: 47 | CameraComponent(std::shared_ptr device); 48 | virtual ~CameraComponent(); 49 | int start(); 50 | int stop(); 51 | const CameraInfo &getCameraInfo() const; 52 | const StorageInfo &getStorageInfo() const; 53 | const std::map &getParamList() const; 54 | int getParamType(const char *param_id, size_t id_size); 55 | virtual int getParam(const char *param_id, size_t id_size, char *param_value, 56 | size_t value_size); 57 | virtual int setParam(const char *param_id, size_t id_size, const char *param_value, 58 | size_t value_size, int param_type); 59 | virtual int setCameraMode(CameraParameters::Mode mode); 60 | virtual CameraParameters::Mode getCameraMode(); 61 | typedef std::function capture_callback_t; 62 | int setImageCaptureLocation(std::string imgPath); 63 | int setImageCaptureSettings(ImageSettings &imgSetting); 64 | void getImageCaptureStatus(uint8_t &status, int &interval); 65 | virtual int startImageCapture(int interval, int count, capture_callback_t cb); 66 | virtual int stopImageCapture(); 67 | void cbImageCaptured(int result, int seq_num); 68 | int setVideoCaptureLocation(std::string vidPath); 69 | int setVideoCaptureSettings(VideoSettings &vidSetting); 70 | virtual int startVideoCapture(int status_freq); 71 | virtual int stopVideoCapture(); 72 | virtual uint8_t getVideoCaptureStatus(); 73 | int startVideoStream(const bool isUdp); 74 | int stopVideoStream(); 75 | uint8_t getVideoStreamStatus() const; 76 | int resetCameraSettings(void); 77 | 78 | private: 79 | std::string mCamDevName; /* Camera device name */ 80 | CameraInfo mCamInfo; /* Camera Information Structure */ 81 | StorageInfo mStoreInfo; /* Storage Information Structure */ 82 | CameraParameters mCamParam; /* Camera Parameters Object */ 83 | std::shared_ptr mCamDev; /* Camera Device Object */ 84 | std::shared_ptr mImgCap; /* Image Capture Object */ 85 | std::function mImgCapCB; 86 | std::string mImgPath; 87 | std::shared_ptr mImgSetting; /* Image Setting Structure */ 88 | std::shared_ptr mVidCap; /* Video Capture Object */ 89 | std::string mVidPath; 90 | std::shared_ptr mVidSetting; /* Video Setting Structure */ 91 | std::shared_ptr mVidStream; /* Video Streaming Object*/ 92 | 93 | void initStorageInfo(struct StorageInfo &storeInfo); 94 | int setVideoFrameFormat(uint32_t param_value); 95 | int setVideoSize(uint32_t param_value); 96 | std::string toString(const char *buf, size_t buf_size); 97 | }; 98 | -------------------------------------------------------------------------------- /src/CameraParameters.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2017 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include "CameraParameters.h" 24 | #include "log.h" 25 | 26 | const char CameraParameters::CAMERA_MODE[] = "camera-mode"; 27 | const char CameraParameters::BRIGHTNESS[] = "brightness"; 28 | const char CameraParameters::CONTRAST[] = "contrast"; 29 | const char CameraParameters::SATURATION[] = "saturation"; 30 | const char CameraParameters::HUE[] = "hue"; 31 | const char CameraParameters::WHITE_BALANCE_MODE[] = "wb-mode"; 32 | const char CameraParameters::GAMMA[] = "gamma"; 33 | const char CameraParameters::GAIN[] = "gain"; 34 | const char CameraParameters::POWER_LINE_FREQ_MODE[] = "power-mode"; 35 | const char CameraParameters::WHITE_BALANCE_TEMPERATURE[] = "wb-temp"; 36 | const char CameraParameters::SHARPNESS[] = "sharpness"; 37 | const char CameraParameters::BACKLIGHT_COMPENSATION[] = "backlight"; 38 | const char CameraParameters::EXPOSURE_MODE[] = "exp-mode"; 39 | const char CameraParameters::EXPOSURE[] = "exposure"; 40 | const char CameraParameters::EXPOSURE_ABSOLUTE[] = "exp-absolute"; 41 | const char CameraParameters::EXPOSURE_AUTO_PRIORITY[] = "exp-priority"; 42 | const char CameraParameters::IMAGE_SIZE[] = "image-size"; 43 | const char CameraParameters::IMAGE_FORMAT[] = "image-format"; 44 | const char CameraParameters::PIXEL_FORMAT[] = "pixel-format"; 45 | const char CameraParameters::SCENE_MODE[] = "scene-mode"; 46 | const char CameraParameters::VIDEO_SIZE[] = "video-size"; 47 | const char CameraParameters::VIDEO_FRAME_FORMAT[] = "video-format"; 48 | const char CameraParameters::IMAGE_CAPTURE[] = "img-capture"; 49 | const char CameraParameters::VIDEO_CAPTURE[] = "vid-capture"; 50 | const char CameraParameters::VIDEO_SNAPSHOT[] = "vid-snapshot"; 51 | const char CameraParameters::IMAGE_VIDEOSHOT[] = "img-videoshot"; 52 | 53 | /* ++Need to check if required, as we have defined values as enums++ */ 54 | 55 | // Values for white balance settings. 56 | const char CameraParameters::WHITE_BALANCE_AUTO[] = "auto"; 57 | const char CameraParameters::WHITE_BALANCE_INCANDESCENT[] = "incandescent"; 58 | const char CameraParameters::WHITE_BALANCE_FLUORESCENT[] = "fluorescent"; 59 | const char CameraParameters::WHITE_BALANCE_WARM_FLUORESCENT[] = "warm-fluorescent"; 60 | const char CameraParameters::WHITE_BALANCE_DAYLIGHT[] = "daylight"; 61 | const char CameraParameters::WHITE_BALANCE_CLOUDY_DAYLIGHT[] = "cloudy-daylight"; 62 | const char CameraParameters::WHITE_BALANCE_TWILIGHT[] = "twilight"; 63 | const char CameraParameters::WHITE_BALANCE_SHADE[] = "shade"; 64 | /* --Need to check if required-- */ 65 | 66 | CameraParameters::CameraParameters() 67 | { 68 | // initParamIdType(); 69 | } 70 | 71 | CameraParameters::~CameraParameters() 72 | { 73 | } 74 | 75 | bool CameraParameters::setParameterValuesSupported(std::string param, std::string value) 76 | { 77 | if (param.size() > CAM_PARAM_ID_LEN || value.size() > CAM_PARAM_VALUE_LEN) 78 | return false; 79 | 80 | paramValuesSupported[param] = value; 81 | return true; 82 | } 83 | 84 | bool CameraParameters::setParameterIdType(std::string param, int paramId, int paramType) 85 | { 86 | // TODO :: Check if the param is not already declared 87 | // TODO :: Check the paramid doesnt clash with others 88 | 89 | if (param.empty() || paramId < 0 || paramType < 0) { 90 | log_error("Invalid Argument"); 91 | return false; 92 | } 93 | 94 | paramIdType[param] = std::make_pair(paramId, paramType); 95 | return true; 96 | } 97 | 98 | int CameraParameters::getParameterID(std::string param) 99 | { 100 | int ret = -1; 101 | std::map>::iterator it = paramIdType.find(param); 102 | if (it != paramIdType.end()) { 103 | ret = it->second.first; 104 | } else 105 | ret = -1; 106 | return ret; 107 | } 108 | 109 | int CameraParameters::getParameterType(std::string param) 110 | { 111 | int ret = -1; 112 | std::map>::iterator it = paramIdType.find(param); 113 | if (it != paramIdType.end()) { 114 | ret = it->second.second; 115 | } else 116 | ret = -1; 117 | return ret; 118 | } 119 | 120 | bool CameraParameters::setParameter(std::string key, std::string value) 121 | { 122 | if (key.size() > CAM_PARAM_ID_LEN || value.size() > CAM_PARAM_VALUE_LEN) 123 | return false; 124 | 125 | paramValue[key] = value; 126 | return true; 127 | } 128 | 129 | bool CameraParameters::setParameter(std::string param_id, float param_value) 130 | { 131 | cam_param_union_t u; 132 | u.param_float = param_value; 133 | std::string str(reinterpret_cast(u.bytes), CAM_PARAM_VALUE_LEN); 134 | return setParameter(param_id, str); 135 | } 136 | bool CameraParameters::setParameter(std::string param_id, uint32_t param_value) 137 | { 138 | cam_param_union_t u; 139 | u.param_uint32 = param_value; 140 | std::string str(reinterpret_cast(u.bytes), CAM_PARAM_VALUE_LEN); 141 | return setParameter(param_id, str); 142 | } 143 | bool CameraParameters::setParameter(std::string param_id, int32_t param_value) 144 | { 145 | cam_param_union_t u; 146 | u.param_int32 = param_value; 147 | std::string str(reinterpret_cast(u.bytes), CAM_PARAM_VALUE_LEN); 148 | return setParameter(param_id, str); 149 | } 150 | bool CameraParameters::setParameter(std::string param_id, uint8_t param_value) 151 | { 152 | cam_param_union_t u; 153 | u.param_uint8 = param_value; 154 | std::string str(reinterpret_cast(u.bytes), CAM_PARAM_VALUE_LEN); 155 | return setParameter(param_id, str); 156 | } 157 | 158 | std::string CameraParameters::getParameter(std::string key) 159 | { 160 | if (paramValue.find(key) == paramValue.end()) 161 | return std::string(); 162 | else 163 | return paramValue[key]; 164 | } 165 | 166 | void CameraParameters::initParamIdType() 167 | { 168 | paramIdType[CAMERA_MODE] = std::make_pair(PARAM_ID_CAMERA_MODE, PARAM_TYPE_UINT32); 169 | paramIdType[BRIGHTNESS] = std::make_pair(PARAM_ID_BRIGHTNESS, PARAM_TYPE_UINT32); 170 | paramIdType[CONTRAST] = std::make_pair(PARAM_ID_CONTRAST, PARAM_TYPE_UINT32); 171 | paramIdType[SATURATION] = std::make_pair(PARAM_ID_SATURATION, PARAM_TYPE_UINT32); 172 | paramIdType[HUE] = std::make_pair(PARAM_ID_HUE, PARAM_TYPE_INT32); 173 | paramIdType[WHITE_BALANCE_MODE] 174 | = std::make_pair(PARAM_ID_WHITE_BALANCE_MODE, PARAM_TYPE_UINT32); 175 | paramIdType[GAMMA] = std::make_pair(PARAM_ID_GAMMA, PARAM_TYPE_UINT32); 176 | paramIdType[GAIN] = std::make_pair(PARAM_ID_GAIN, PARAM_TYPE_UINT32); 177 | paramIdType[POWER_LINE_FREQ_MODE] 178 | = std::make_pair(PARAM_ID_POWER_LINE_FREQ_MODE, PARAM_TYPE_UINT32); 179 | paramIdType[WHITE_BALANCE_TEMPERATURE] 180 | = std::make_pair(PARAM_ID_WHITE_BALANCE_TEMPERATURE, PARAM_TYPE_UINT32); 181 | paramIdType[SHARPNESS] = std::make_pair(PARAM_ID_SHARPNESS, PARAM_TYPE_UINT32); 182 | paramIdType[BACKLIGHT_COMPENSATION] 183 | = std::make_pair(PARAM_ID_BACKLIGHT_COMPENSATION, PARAM_TYPE_UINT32); 184 | paramIdType[EXPOSURE_MODE] = std::make_pair(PARAM_ID_EXPOSURE_MODE, PARAM_TYPE_UINT32); 185 | paramIdType[EXPOSURE_ABSOLUTE] = std::make_pair(PARAM_ID_EXPOSURE_ABSOLUTE, PARAM_TYPE_UINT32); 186 | paramIdType[IMAGE_SIZE] = std::make_pair(PARAM_ID_IMAGE_SIZE, PARAM_TYPE_UINT32); 187 | paramIdType[IMAGE_FORMAT] = std::make_pair(PARAM_ID_IMAGE_FORMAT, PARAM_TYPE_UINT32); 188 | paramIdType[PIXEL_FORMAT] = std::make_pair(PARAM_ID_PIXEL_FORMAT, PARAM_TYPE_UINT32); 189 | paramIdType[SCENE_MODE] = std::make_pair(PARAM_ID_SCENE_MODE, PARAM_TYPE_UINT32); 190 | paramIdType[VIDEO_SIZE] = std::make_pair(PARAM_ID_VIDEO_SIZE, PARAM_TYPE_UINT32); 191 | paramIdType[VIDEO_FRAME_FORMAT] 192 | = std::make_pair(PARAM_ID_VIDEO_FRAME_FORMAT, PARAM_TYPE_UINT32); 193 | paramIdType[IMAGE_CAPTURE] = std::make_pair(PARAM_ID_IMAGE_CAPTURE, PARAM_TYPE_UINT32); 194 | paramIdType[VIDEO_CAPTURE] = std::make_pair(PARAM_ID_VIDEO_CAPTURE, PARAM_TYPE_UINT32); 195 | paramIdType[VIDEO_SNAPSHOT] = std::make_pair(PARAM_ID_VIDEO_SNAPSHOT, PARAM_TYPE_UINT32); 196 | paramIdType[IMAGE_VIDEOSHOT] = std::make_pair(PARAM_ID_IMAGE_VIDEOSHOT, PARAM_TYPE_UINT32); 197 | } 198 | -------------------------------------------------------------------------------- /src/CameraParameters.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2017 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #pragma once 19 | #include 20 | #include 21 | #include 22 | 23 | #include "log.h" 24 | 25 | #define CAM_PARAM_ID_LEN 16 26 | #define CAM_PARAM_VALUE_LEN 128 27 | 28 | class CameraParameters { 29 | public: 30 | CameraParameters(); 31 | virtual ~CameraParameters(); 32 | const std::map &getParameterList() const { return paramValue; } 33 | bool setParameterValuesSupported(std::string param, std::string value); 34 | bool setParameterIdType(std::string param, int paramId, int paramType); 35 | int getParameterType(std::string param); 36 | int getParameterID(std::string param); 37 | bool setParameter(std::string param, std::string value); 38 | bool setParameter(std::string param_id, float param_value); 39 | bool setParameter(std::string param_id, uint32_t param_value); 40 | bool setParameter(std::string param_id, int32_t param_value); 41 | bool setParameter(std::string param_id, uint8_t param_value); 42 | std::string getParameter(std::string param); 43 | 44 | typedef struct { 45 | union { 46 | float param_float; 47 | int32_t param_int32; 48 | uint32_t param_uint32; 49 | int16_t param_int16; 50 | uint16_t param_uint16; 51 | int8_t param_int8; 52 | uint8_t param_uint8; 53 | uint8_t bytes[CAM_PARAM_VALUE_LEN]; 54 | }; 55 | uint8_t type; 56 | } cam_param_union_t; 57 | 58 | enum param_type { 59 | PARAM_TYPE_UINT8 = 1, 60 | PARAM_TYPE_INT8, 61 | PARAM_TYPE_UINT16, 62 | PARAM_TYPE_INT16, 63 | PARAM_TYPE_UINT32, 64 | PARAM_TYPE_INT32, 65 | PARAM_TYPE_UINT64, 66 | PARAM_TYPE_INT64, 67 | PARAM_TYPE_REAL32, 68 | PARAM_TYPE_REAL64 69 | }; 70 | 71 | enum camera_cap_flags { 72 | CAMERA_CAP_CAPTURE_VIDEO = 1, 73 | CAMERA_CAP_CAPTURE_IMAGE = 2, 74 | CAMERA_CAP_HAS_MODES = 4, 75 | CAMERA_CAP_CAN_CAPTURE_IMAGE_IN_VIDEO_MODE = 8, 76 | CAMERA_CAP_CAN_CAPTURE_VIDEO_IN_IMAGE_MODE = 16, 77 | CAMERA_CAP_HAS_IMAGE_SURVEY_MODE = 32 78 | }; 79 | 80 | // List of the params 81 | static const char CAMERA_MODE[]; 82 | static const char BRIGHTNESS[]; 83 | static const char CONTRAST[]; 84 | static const char SATURATION[]; 85 | static const char HUE[]; 86 | static const char WHITE_BALANCE_MODE[]; 87 | static const char GAMMA[]; 88 | static const char GAIN[]; 89 | static const char POWER_LINE_FREQ_MODE[]; 90 | static const char WHITE_BALANCE_TEMPERATURE[]; 91 | static const char SHARPNESS[]; 92 | static const char BACKLIGHT_COMPENSATION[]; 93 | static const char EXPOSURE_MODE[]; 94 | static const char EXPOSURE[]; 95 | static const char EXPOSURE_ABSOLUTE[]; 96 | static const char EXPOSURE_AUTO_PRIORITY[]; 97 | static const char IMAGE_SIZE[]; 98 | static const char IMAGE_FORMAT[]; 99 | static const char PIXEL_FORMAT[]; 100 | static const char SCENE_MODE[]; 101 | static const char VIDEO_SIZE[]; 102 | static const char VIDEO_FRAME_FORMAT[]; 103 | static const char IMAGE_CAPTURE[]; 104 | static const char VIDEO_CAPTURE[]; 105 | static const char VIDEO_SNAPSHOT[]; 106 | static const char IMAGE_VIDEOSHOT[]; 107 | 108 | // Values for white balance settings. 109 | static const char WHITE_BALANCE_AUTO[]; 110 | static const char WHITE_BALANCE_INCANDESCENT[]; 111 | static const char WHITE_BALANCE_FLUORESCENT[]; 112 | static const char WHITE_BALANCE_WARM_FLUORESCENT[]; 113 | static const char WHITE_BALANCE_DAYLIGHT[]; 114 | static const char WHITE_BALANCE_CLOUDY_DAYLIGHT[]; 115 | static const char WHITE_BALANCE_TWILIGHT[]; 116 | static const char WHITE_BALANCE_SHADE[]; 117 | 118 | // ID for parameters 119 | static const int PARAM_ID_CAMERA_MODE = 1; 120 | static const int PARAM_ID_BRIGHTNESS = 2; 121 | static const int PARAM_ID_CONTRAST = 3; 122 | static const int PARAM_ID_SATURATION = 4; 123 | static const int PARAM_ID_HUE = 5; 124 | static const int PARAM_ID_WHITE_BALANCE_MODE = 6; 125 | static const int PARAM_ID_GAMMA = 7; 126 | static const int PARAM_ID_GAIN = 8; 127 | static const int PARAM_ID_POWER_LINE_FREQ_MODE = 9; 128 | static const int PARAM_ID_WHITE_BALANCE_TEMPERATURE = 10; 129 | static const int PARAM_ID_SHARPNESS = 11; 130 | static const int PARAM_ID_BACKLIGHT_COMPENSATION = 12; 131 | static const int PARAM_ID_EXPOSURE_MODE = 13; 132 | static const int PARAM_ID_EXPOSURE_ABSOLUTE = 14; 133 | static const int PARAM_ID_IMAGE_SIZE = 15; 134 | static const int PARAM_ID_IMAGE_FORMAT = 16; 135 | static const int PARAM_ID_PIXEL_FORMAT = 17; 136 | static const int PARAM_ID_SCENE_MODE = 18; 137 | static const int PARAM_ID_VIDEO_SIZE = 19; 138 | static const int PARAM_ID_VIDEO_FRAME_FORMAT = 20; 139 | static const int PARAM_ID_IMAGE_CAPTURE = 21; 140 | static const int PARAM_ID_VIDEO_CAPTURE = 22; 141 | static const int PARAM_ID_VIDEO_SNAPSHOT = 23; 142 | static const int PARAM_ID_IMAGE_VIDEOSHOT = 24; 143 | static const int PARAM_ID_EXPOSURE_AUTO_PRIORITY = 25; 144 | static const int PARAM_ID_EXPOSURE = 26; 145 | 146 | // ID for image sizes 147 | static const int ID_IMAGE_SIZE_3264x2448 = 1; 148 | static const int ID_IMAGE_SIZE_3264x1836 = 2; 149 | static const int ID_IMAGE_SIZE_1920x1080 = 3; 150 | 151 | // ID for video sizes 152 | static const int ID_VIDEO_SIZE_1920x1080x30 = 1; 153 | static const int ID_VIDEO_SIZE_1920x1080x15 = 2; 154 | static const int ID_VIDEO_SIZE_1280x720x30 = 3; 155 | static const int ID_VIDEO_SIZE_1280x720x15 = 4; 156 | static const int ID_VIDEO_SIZE_960x540x30 = 5; 157 | static const int ID_VIDEO_SIZE_960x540x15 = 6; 158 | static const int ID_VIDEO_SIZE_848x480x30 = 7; 159 | static const int ID_VIDEO_SIZE_848x480x15 = 8; 160 | static const int ID_VIDEO_SIZE_640x480x60 = 9; 161 | static const int ID_VIDEO_SIZE_640x480x30 = 10; 162 | static const int ID_VIDEO_SIZE_640x480x15 = 11; 163 | static const int ID_VIDEO_SIZE_640x360x30 = 12; 164 | static const int ID_VIDEO_SIZE_640x360x15 = 13; 165 | static const int ID_VIDEO_SIZE_424x240x30 = 14; 166 | static const int ID_VIDEO_SIZE_424x240x15 = 15; 167 | static const int ID_VIDEO_SIZE_320x240x60 = 16; 168 | static const int ID_VIDEO_SIZE_320x240x30 = 17; 169 | static const int ID_VIDEO_SIZE_320x240x15 = 18; 170 | static const int ID_VIDEO_SIZE_320x180x30 = 19; 171 | static const int ID_VIDEO_SIZE_320x180x60 = 20; 172 | 173 | // ID for White Balance Modes 174 | static const int ID_WHITE_BALANCE_MANUAL = 0; 175 | static const int ID_WHITE_BALANCE_AUTO = 1; 176 | static const int ID_WHITE_BALANCE_INCANDESCENT = 2; 177 | static const int ID_WHITE_BALANCE_FLUORESCENT = 3; 178 | static const int ID_WHITE_BALANCE_WARM_FLUORESCENT = 4; 179 | static const int ID_WHITE_BALANCE_DAYLIGHT = 5; 180 | static const int ID_WHITE_BALANCE_CLOUDY_DAYLIGHT = 6; 181 | static const int ID_WHITE_BALANCE_TWILIGHT = 7; 182 | static const int ID_WHITE_BALANCE_SHADE = 8; 183 | 184 | // ID for exposure modes 185 | static const int ID_EXPOSURE_MANUAL = 0; 186 | static const int ID_EXPOSURE_AUTO = 1; 187 | 188 | /** 189 | * Modes of camera device. 190 | */ 191 | enum Mode { MODE_STILL = 0, MODE_VIDEO = 1, MODE_IMG_SURVEY = 2 }; 192 | 193 | /** 194 | * Pixel formats of the camera image. 195 | */ 196 | enum PixelFormat { 197 | PIXEL_FORMAT_MIN = 0, 198 | PIXEL_FORMAT_GREY, /* 8 bpp monochrome images */ 199 | PIXEL_FORMAT_YUV420, /* 12 bpp YUV 4:2:0 */ 200 | PIXEL_FORMAT_YUV422P, /* 16 bpp YVU422 planar */ 201 | PIXEL_FORMAT_UYVY, /* 16 bpp YUV 4:2:2 */ 202 | PIXEL_FORMAT_RGB24, /* 24 bpp RGB 8:8:8 */ 203 | PIXEL_FORMAT_RGB32, /* 32 bpp RGB 8:8:8:8 */ 204 | PIXEL_FORMAT_MAX = 99 205 | }; 206 | 207 | typedef enum IMAGE_FILE_FORMAT { 208 | IMAGE_FILE_MIN, 209 | IMAGE_FILE_RAW, /* Raw Format */ 210 | IMAGE_FILE_JPEG, /* JPEG Format */ 211 | IMAGE_FILE_EXIF, /* EXIF Format */ 212 | IMAGE_FILE_TIFF, /* TIFF Format */ 213 | IMAGE_FILE_GIF, /* GIF Format */ 214 | IMAGE_FILE_PNG, /* PNG Format */ 215 | IMAGE_FILE_BMP, /* BMP Format */ 216 | IMAGE_FILE_MAX = 99 217 | } IMAGE_FILE_FORMAT; 218 | 219 | typedef enum VIDEO_FILE_FORMAT { 220 | VIDEO_FILE_MIN, 221 | VIDEO_FILE_MP4, /* Moving Pictures Expert Group 4 */ 222 | VIDEO_FILE_AVI, /* Audio Video Interleave */ 223 | VIDEO_FILE_WMV, /* Windows Media Video */ 224 | VIDEO_FILE_FLV, /* Flash Video Format */ 225 | VIDEO_FILE_MOV, /* Apple QuickTime Movie */ 226 | VIDEO_FILE_MAX = 99 227 | } VIDEO_FILE_FORMAT; 228 | 229 | typedef enum VIDEO_CODING_FORMAT { 230 | VIDEO_CODING_MIN, 231 | VIDEO_CODING_H263, /* H.263 */ 232 | VIDEO_CODING_MPEG4, /* MPEG-4*/ 233 | VIDEO_CODING_AVC, /* H.264/AVC */ 234 | VIDEO_CODING_MJPEG, /* Motion JPEG */ 235 | VIDEO_CODING_WMV, /* Windows Media Video */ 236 | VIDEO_CODING_MAX = 99 237 | } VIDEO_CODING_FORMAT; 238 | 239 | // TODO :: Make exhaustive list of parameters and its possible values 240 | private: 241 | void initParamIdType(); 242 | std::map paramValue; 243 | std::map paramValuesSupported; 244 | std::map> paramIdType; /*Key->ID,Type*/ 245 | }; 246 | -------------------------------------------------------------------------------- /src/CameraServer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2017 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #pragma once 19 | #include 20 | #include 21 | #include 22 | 23 | #include "conf_file.h" 24 | #ifdef ENABLE_MAVLINK 25 | #include "mavlink_server.h" 26 | #endif 27 | #ifdef ENABLE_AVAHI 28 | #include "avahi_publisher.h" 29 | #endif 30 | 31 | #include "CameraComponent.h" 32 | #include "PluginManager.h" 33 | 34 | class CameraServer { 35 | public: 36 | CameraServer(const ConfFile &conf); 37 | ~CameraServer(); 38 | void start(); 39 | void stop(); 40 | 41 | private: 42 | void addCameraInformation(const std::shared_ptr &device); 43 | std::set readBlacklistDevices(const ConfFile &conf) const; 44 | std::string readURI(const ConfFile &conf, std::string deviceID); 45 | std::string readRTSPPipeline(const ConfFile &conf, std::string deviceID); 46 | bool readImgCapSettings(const ConfFile &conf, ImageSettings &imgSetting) const; 47 | std::string readImgCapLocation(const ConfFile &conf) const; 48 | bool readVidCapSettings(const ConfFile &conf, VideoSettings &vidSetting) const; 49 | std::string readVidCapLocation(const ConfFile &conf) const; 50 | std::string readGazeboCamTopic(const ConfFile &conf) const; 51 | PluginManager mPluginManager; 52 | 53 | #ifdef ENABLE_MAVLINK 54 | MavlinkServer mMavlinkServer; 55 | #endif 56 | #ifdef ENABLE_AVAHI 57 | std::unique_ptr mAvahiPublisher; 58 | #endif 59 | 60 | std::map> mCamInfoMap; 61 | std::vector compList; 62 | }; 63 | -------------------------------------------------------------------------------- /src/ImageCapture.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2017 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #pragma once 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #include "CameraParameters.h" 25 | 26 | struct ImageSettings { 27 | int width; 28 | int height; 29 | CameraParameters::IMAGE_FILE_FORMAT fileFormat; 30 | }; 31 | 32 | class ImageCapture { 33 | public: 34 | ImageCapture() {} 35 | ~ImageCapture() {} 36 | 37 | enum State { 38 | STATE_ERROR = -1, 39 | STATE_IDLE = 0, 40 | STATE_INIT = 1, 41 | STATE_RUN = 2, 42 | }; 43 | 44 | virtual int init() = 0; 45 | virtual int uninit() = 0; 46 | virtual int start(int num, int interval, std::function cb) = 0; 47 | virtual int stop() = 0; 48 | virtual int getState() = 0; 49 | virtual int setInterval(int interval) = 0; 50 | virtual int getInterval() = 0; 51 | virtual int setResolution(int imgWidth, int imgHeight) = 0; 52 | virtual int setFormat(CameraParameters::IMAGE_FILE_FORMAT imgFormat) = 0; 53 | virtual int setLocation(const std::string imgPath) = 0; 54 | }; 55 | -------------------------------------------------------------------------------- /src/ImageCaptureGst.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2017 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #pragma once 19 | #include 20 | 21 | #include "CameraDevice.h" 22 | #include "ImageCapture.h" 23 | 24 | class ImageCaptureGst final : public ImageCapture { 25 | public: 26 | ImageCaptureGst(std::shared_ptr camDev); 27 | ImageCaptureGst(std::shared_ptr camDev, struct ImageSettings &imgSetting); 28 | ~ImageCaptureGst(); 29 | 30 | int init(); 31 | int uninit(); 32 | int start(int interval, int count, std::function cb); 33 | int stop(); 34 | int getState(); 35 | int setInterval(int interval); 36 | int getInterval(); 37 | int setResolution(int imgWidth, int imgHeight); 38 | int setFormat(CameraParameters::IMAGE_FILE_FORMAT imgFormat); 39 | int setLocation(const std::string imgPath); 40 | std::shared_ptr mCamDev; 41 | 42 | private: 43 | static int imgCount; 44 | int setState(int state); 45 | int click(); 46 | void captureThread(int num); 47 | int createV4l2Pipeline(); 48 | std::string getGstImgEncName(int format); 49 | std::string getGstPixFormat(CameraParameters::PixelFormat pixFormat); 50 | std::string getImgExt(int format); 51 | std::string getGstPipelineNameV4l2(); 52 | int createAppsrcPipeline(); 53 | std::string mDevice; 54 | std::atomic mState; 55 | uint32_t mWidth; /* Image Width*/ 56 | uint32_t mHeight; /* Image Height*/ 57 | CameraParameters::IMAGE_FILE_FORMAT mFormat; /* Image File Format*/ 58 | uint32_t mInterval; /* Image Capture interval */ 59 | std::string mPath; /* Image File Destination Path*/ 60 | uint32_t mCamWidth; /* Camera Frame Width*/ 61 | uint32_t mCamHeight; /* Camera Frame Height*/ 62 | CameraParameters::PixelFormat mCamPixFormat; /* Camera Frame Pixel Format*/ 63 | std::function mResultCB; 64 | std::thread mThread; 65 | }; 66 | -------------------------------------------------------------------------------- /src/PluginBase.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2018 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include "CameraDevice.h" 26 | 27 | /** 28 | * The PluginBase is base class for plugins. Plugins are factories for a class of CameraDevices. 29 | * For example - PluginV4l2 creates CameraDevice object for V4L2 cameras discovered in the system. 30 | * Plugins that gets compiled will self-register on execution using static initialization. 31 | * PluginManager maintains a table of Plugins and camera devices associated with each Plugin. 32 | * To add support for custom type of camera, implement a custom Plugin derived from PluginBase. 33 | * This custom Plugin will create custom CameraDevice. 34 | */ 35 | 36 | class PluginBase { 37 | public: 38 | /** 39 | * Destructor. Erases itself from the list. 40 | */ 41 | virtual ~PluginBase() 42 | { 43 | std::vector pluginList = getPlugins(); 44 | pluginList.erase(std::remove(pluginList.begin(), pluginList.end(), this), pluginList.end()); 45 | } 46 | 47 | /** 48 | * Get the list of plugin objects. 49 | * 50 | * @return Reference to vector of plugin object. 51 | */ 52 | static std::vector &getPlugins() 53 | { 54 | static std::vector pluginList; 55 | return pluginList; 56 | } 57 | 58 | /** 59 | * Get the list of camera devices discovered by plugin. 60 | * 61 | * @return Vector of names(ID) of camera devices. 62 | */ 63 | virtual std::vector getCameraDevices() = 0; 64 | 65 | /** 66 | * Create a CameraDevice. 67 | * 68 | * @param[in] deviceID Camera device identifier(name) 69 | * 70 | * @return Pointer to CameraDevice object created or nullptr on failure 71 | */ 72 | virtual std::shared_ptr createCameraDevice(std::string deviceID) = 0; 73 | 74 | protected: 75 | /** 76 | * Constructor. Self-registers by adding itself to the list. 77 | */ 78 | PluginBase() { getPlugins().push_back(this); } 79 | }; 80 | -------------------------------------------------------------------------------- /src/PluginManager.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2018 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | #include "PluginManager.h" 20 | 21 | // build a map of camera device(key) and their plugin(value) 22 | PluginManager::PluginManager() 23 | { 24 | // for all plugins, get the list of camera devices discovered 25 | for (PluginBase *plugin : PluginBase::getPlugins()) { 26 | for (std::string deviceID : plugin->getCameraDevices()) { 27 | // add the (device,factory) pair to map 28 | mPluginMap[deviceID] = plugin; 29 | } 30 | } 31 | } 32 | 33 | PluginManager::~PluginManager() 34 | { 35 | } 36 | 37 | std::vector PluginManager::listCameraDevices() 38 | { 39 | std::vector pluginList; 40 | for (auto const &element : mPluginMap) { 41 | pluginList.push_back(element.first); 42 | } 43 | return pluginList; 44 | } 45 | 46 | std::shared_ptr PluginManager::createCameraDevice(std::string deviceID) 47 | { 48 | if (mPluginMap.find(deviceID) == mPluginMap.end()) { 49 | return nullptr; 50 | } else { 51 | return mPluginMap[deviceID]->createCameraDevice(deviceID); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/PluginManager.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2018 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | #include 20 | #include 21 | #include 22 | 23 | #include "PluginBase.h" 24 | 25 | class PluginManager { 26 | public: 27 | PluginManager(); 28 | ~PluginManager(); 29 | std::vector listCameraDevices(); 30 | std::shared_ptr createCameraDevice(std::string deviceID); 31 | 32 | private: 33 | std::map mPluginMap; 34 | }; 35 | -------------------------------------------------------------------------------- /src/VideoCapture.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2017 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #pragma once 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #include "CameraParameters.h" 25 | 26 | struct VideoSettings { 27 | int width; 28 | int height; 29 | int frameRate; 30 | int bitRate; // in kbps 31 | CameraParameters::VIDEO_CODING_FORMAT encoder; 32 | CameraParameters::VIDEO_FILE_FORMAT fileFormat; 33 | }; 34 | 35 | class VideoCapture { 36 | public: 37 | VideoCapture() {} 38 | ~VideoCapture() {} 39 | 40 | enum State { 41 | STATE_ERROR = -1, 42 | STATE_IDLE = 0, 43 | STATE_INIT = 1, 44 | STATE_RUN = 2, 45 | }; 46 | 47 | virtual int init() = 0; 48 | virtual int uninit() = 0; 49 | virtual int start() = 0; 50 | virtual int stop() = 0; 51 | virtual int getState() = 0; 52 | virtual int setResolution(int vidWidth, int vidHeight) = 0; 53 | virtual int getResolution(int &vidWidth, int &vidHeight) = 0; 54 | virtual int setEncoder(CameraParameters::VIDEO_CODING_FORMAT vidEnc) = 0; 55 | virtual int setFormat(CameraParameters::VIDEO_FILE_FORMAT fileFormat) = 0; 56 | virtual int setBitRate(int bitRate) = 0; 57 | virtual int setFrameRate(int frameRate) = 0; 58 | virtual int setLocation(const std::string vidPath) = 0; 59 | virtual std::string getLocation() = 0; 60 | }; 61 | -------------------------------------------------------------------------------- /src/VideoCaptureGst.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2018 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #pragma once 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include "CameraDevice.h" 26 | #include "VideoCapture.h" 27 | 28 | class VideoCaptureGst final : public VideoCapture { 29 | public: 30 | VideoCaptureGst(std::shared_ptr camDev); 31 | VideoCaptureGst(std::shared_ptr camDev, struct VideoSettings &vidSetting); 32 | ~VideoCaptureGst(); 33 | 34 | int init(); 35 | int uninit(); 36 | int start(); 37 | int stop(); 38 | int getState(); 39 | int setResolution(int imgWidth, int imgHeight); 40 | int getResolution(int &imgWidth, int &imgHeight); 41 | int setBitRate(int bitRate); 42 | int setFrameRate(int frameRate); 43 | int setEncoder(CameraParameters::VIDEO_CODING_FORMAT vidEnc); 44 | int setFormat(CameraParameters::VIDEO_FILE_FORMAT fileFormat); 45 | int setLocation(const std::string vidPath); 46 | std::string getLocation(); 47 | 48 | private: 49 | static int vidCount; 50 | int setState(int state); 51 | std::string getGstEncName(int format); 52 | std::string getGstParserName(int format); 53 | std::string getGstMuxerName(int format); 54 | std::string getFileExt(int format); 55 | std::string getGstV4l2PipelineName(); 56 | int createV4l2Pipeline(); 57 | int destroyPipeline(); 58 | std::shared_ptr mCamDev; 59 | std::atomic mState; 60 | int mWidth; 61 | int mHeight; 62 | int mBitRate; 63 | int mFrmRate; 64 | CameraParameters::VIDEO_CODING_FORMAT mEnc; 65 | CameraParameters::VIDEO_FILE_FORMAT mFileFmt; 66 | std::string mFilePath; 67 | GstElement *mPipeline; 68 | }; 69 | -------------------------------------------------------------------------------- /src/VideoStream.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2018 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #pragma once 19 | 20 | class VideoStream { 21 | public: 22 | VideoStream() {} 23 | ~VideoStream() {} 24 | 25 | enum State { STATE_ERROR = -1, STATE_IDLE = 0, STATE_INIT = 1, STATE_RUN = 2 }; 26 | 27 | virtual int init() = 0; 28 | virtual int uninit() = 0; 29 | virtual int start() = 0; 30 | virtual int stop() = 0; 31 | virtual int getState() = 0; 32 | virtual int setResolution(int imgWidth, int imgHeight) = 0; 33 | virtual int getResolution(int &imgWidth, int &imgHeight) = 0; 34 | virtual int setFormat(int vidFormat) = 0; 35 | virtual int getFormat() = 0; 36 | // The host/IP/Multicast group to send the packets to 37 | virtual int setAddress(std::string ipAddr) = 0; 38 | virtual std::string getAddress() = 0; 39 | // The port to send the packets to 40 | virtual int setPort(uint32_t port) = 0; 41 | virtual int getPort() = 0; 42 | virtual int setTextOverlay(std::string text, int timeSec) { return -1; }; 43 | virtual std::string getTextOverlay() { return {}; }; 44 | }; 45 | -------------------------------------------------------------------------------- /src/VideoStreamRtsp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2018 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #pragma once 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | #include 25 | #include 26 | 27 | #include "CameraDevice.h" 28 | #include "VideoStream.h" 29 | #include "log.h" 30 | 31 | class VideoStreamRtsp final : public VideoStream { 32 | public: 33 | VideoStreamRtsp(std::shared_ptr camDev); 34 | ~VideoStreamRtsp(); 35 | 36 | int init(); 37 | int uninit(); 38 | int start(); 39 | int stop(); 40 | int getState(); 41 | int setResolution(int imgWidth, int imgHeight); 42 | int getResolution(int &imgWidth, int &imgHeight); 43 | int setFormat(int vidFormat); 44 | int getFormat(); 45 | // The host/IP/Multicast group to send the packets to 46 | int setAddress(std::string ipAddr); 47 | std::string getAddress(); 48 | // The port to send the packets to 49 | int setPort(uint32_t port); 50 | int getPort(); 51 | int getCameraResolution(uint32_t &width, uint32_t &height); 52 | CameraParameters::PixelFormat getCameraPixelFormat(); 53 | std::string getGstPipeline(std::map ¶ms); 54 | GstBuffer *readFrame(); 55 | std::shared_ptr getCameraDevice() { return mCamDev; }; 56 | 57 | private: 58 | GstRTSPServer *createRtspServer(); 59 | void destroyRtspServer(); 60 | void attachRtspServer(); 61 | int setState(int state); 62 | int startRtspServer(); 63 | int stopRtspServer(); 64 | std::shared_ptr mCamDev; 65 | std::atomic mState; 66 | uint32_t mWidth; 67 | uint32_t mHeight; 68 | CameraParameters::VIDEO_CODING_FORMAT mEncFormat; 69 | std::string mHost; 70 | uint32_t mPort; 71 | std::string mPath; 72 | static GstRTSPServer *mServer; 73 | static bool isAttach; 74 | static uint32_t refCnt; 75 | }; 76 | -------------------------------------------------------------------------------- /src/VideoStreamUdp.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2018 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | #include 20 | #include 21 | #include 22 | 23 | #include "VideoStreamUdp.h" 24 | #include "log.h" 25 | 26 | VideoStreamUdp::VideoStreamUdp(std::shared_ptr camDev) 27 | : mCamDev(camDev) 28 | , mState(STATE_IDLE) 29 | , mWidth(640) 30 | , mHeight(360) 31 | , mHost("127.0.0.1") 32 | , mPort(5600) 33 | , mOvText("") 34 | , mOvTime(30) 35 | , mOvFrmCnt(0) 36 | , mPipeline(nullptr) 37 | , mTextOverlay(nullptr) 38 | { 39 | log_info("%s Device:%s", __func__, mCamDev->getDeviceId().c_str()); 40 | 41 | mOvText = mCamDev->getDeviceId(); 42 | mOvFrmCnt = mOvTime * 25; 43 | } 44 | 45 | VideoStreamUdp::~VideoStreamUdp() 46 | { 47 | } 48 | 49 | int VideoStreamUdp::init() 50 | { 51 | setState(STATE_INIT); 52 | return 0; 53 | } 54 | 55 | int VideoStreamUdp::uninit() 56 | { 57 | setState(STATE_IDLE); 58 | return 0; 59 | } 60 | 61 | int VideoStreamUdp::start() 62 | { 63 | log_info("%s::%s", typeid(this).name(), __func__); 64 | int ret = -1; 65 | ret = createAppsrcPipeline(); 66 | setState(STATE_RUN); 67 | return ret; 68 | } 69 | 70 | int VideoStreamUdp::stop() 71 | { 72 | log_info("%s::%s", typeid(this).name(), __func__); 73 | int ret = -1; 74 | ret = destroyAppsrcPipeline(); 75 | setState(STATE_INIT); 76 | return ret; 77 | } 78 | 79 | int VideoStreamUdp::getState() 80 | { 81 | return mState; 82 | } 83 | 84 | int VideoStreamUdp::setState(int state) 85 | { 86 | // TODO :: Set state only as per FSM 87 | mState = state; 88 | return 0; 89 | } 90 | 91 | int VideoStreamUdp::setResolution(int imgWidth, int imgHeight) 92 | { 93 | // TODO::Check if the argument is valid 94 | mWidth = imgWidth; 95 | mHeight = imgHeight; 96 | return 0; 97 | } 98 | 99 | int VideoStreamUdp::getResolution(int &imgWidth, int &imgHeight) 100 | { 101 | imgWidth = mWidth; 102 | imgHeight = mHeight; 103 | return 0; 104 | } 105 | 106 | int VideoStreamUdp::setFormat(int vidFormat) 107 | { 108 | // TODO::Check if the argument is valid 109 | return 0; 110 | } 111 | 112 | int VideoStreamUdp::getFormat() 113 | { 114 | return 0; 115 | } 116 | 117 | int VideoStreamUdp::setAddress(std::string ipAddr) 118 | { 119 | // TODO::Check if the argument is valid 120 | mHost = ipAddr; 121 | return 0; 122 | } 123 | 124 | std::string VideoStreamUdp::getAddress() 125 | { 126 | return mHost; 127 | } 128 | 129 | int VideoStreamUdp::setPort(uint32_t port) 130 | { 131 | // TODO::Check if the argument is valid 132 | mPort = port; 133 | return 0; 134 | } 135 | 136 | int VideoStreamUdp::getPort() 137 | { 138 | return mPort; 139 | } 140 | 141 | int VideoStreamUdp::setTextOverlay(std::string text, int timeSec) 142 | { 143 | mOvText = text; 144 | mOvTime = timeSec; 145 | // TODO :: Replace 25 with camera frame rate 146 | mOvFrmCnt = 25 * mOvTime; 147 | return 0; 148 | } 149 | 150 | std::string VideoStreamUdp::getTextOverlay() 151 | { 152 | return mOvText; 153 | } 154 | 155 | GstBuffer *VideoStreamUdp::readFrame() 156 | { 157 | GstBuffer *buffer; 158 | static GstClockTime timestamp = 0; 159 | CameraData data; 160 | CameraDevice::Status ret = mCamDev->read(data); 161 | if (ret == CameraDevice::Status::SUCCESS) { 162 | gsize size = data.bufSize; 163 | gsize offset = 0; 164 | gsize maxsize = size; 165 | buffer = gst_buffer_new_wrapped_full((GstMemoryFlags)0, data.buf, maxsize, offset, size, 166 | NULL, NULL); 167 | } else { 168 | log_error("Camera returned no frame"); 169 | // TODO :: Change the multiplication factor based on pix format 170 | gsize size = mWidth * mHeight * 3; 171 | buffer = gst_buffer_new_allocate(NULL, size, NULL); 172 | // this makes the image white 173 | gst_buffer_memset(buffer, 0, 0xff, size); 174 | } 175 | 176 | // Add timestamp 177 | GST_BUFFER_PTS(buffer) = timestamp; 178 | GST_BUFFER_DURATION(buffer) = gst_util_uint64_scale_int(1, GST_SECOND, 25); 179 | timestamp += GST_BUFFER_DURATION(buffer); 180 | 181 | // Add Overlay 182 | std::string camText = mCamDev->getOverlayText(); 183 | if (mOvText.compare(camText) != 0 && !camText.empty()) { 184 | mOvText = camText; 185 | mOvFrmCnt = 25 * mOvTime; 186 | } 187 | 188 | if (mOvFrmCnt || mOvFrmCnt == -1) { 189 | g_object_set(G_OBJECT(mTextOverlay), "text", mOvText.c_str(), "valignment", 2, "halignment", 190 | 0, "shaded-background", TRUE, NULL); 191 | if (mOvFrmCnt > 0) 192 | mOvFrmCnt--; 193 | } else { 194 | g_object_set(G_OBJECT(mTextOverlay), "text", "", NULL); 195 | } 196 | return buffer; 197 | } 198 | 199 | static void cb_need_data(GstAppSrc *appsrc, guint unused_size, gpointer user_data) 200 | { 201 | GstFlowReturn ret; 202 | VideoStreamUdp *obj = (VideoStreamUdp *)user_data; 203 | 204 | GstBuffer *buffer = obj->readFrame(); 205 | if (buffer) { 206 | g_signal_emit_by_name(appsrc, "push-buffer", buffer, &ret); 207 | if (ret != GST_FLOW_OK) { 208 | // some error 209 | log_error("Error in sending data to gst pipeline"); 210 | } 211 | } 212 | } 213 | 214 | static void cb_enough_data(GstAppSrc *src, gpointer user_data) 215 | { 216 | } 217 | 218 | static gboolean cb_seek_data(GstAppSrc *src, guint64 offset, gpointer user_data) 219 | { 220 | return TRUE; 221 | } 222 | 223 | int VideoStreamUdp::createAppsrcPipeline() 224 | { 225 | log_info("%s::%s", typeid(this).name(), __func__); 226 | 227 | int ret = 0; 228 | gboolean link_ok; 229 | GstElement *src, *conv, *enc, *parser, *payload, *sink; 230 | GstCaps *caps; 231 | 232 | mPipeline = gst_pipeline_new("UdpStream"); 233 | src = gst_element_factory_make("appsrc", "VideoSrc"); 234 | conv = gst_element_factory_make("videoconvert", "Conv"); 235 | mTextOverlay = gst_element_factory_make("textoverlay", "textoverlay"); 236 | enc = gst_element_factory_make("x264enc", "H264Enc"); 237 | parser = gst_element_factory_make("h264parse", "Parser"); 238 | payload = gst_element_factory_make("rtph264pay", "H264Rtp"); 239 | sink = gst_element_factory_make("udpsink", "UdpSink"); 240 | 241 | // TODO::Check if all the elements are created 242 | if (!mPipeline || !src || !conv || !mTextOverlay || !enc || !parser || !payload || !sink) { 243 | log_error("One element could not be created. Exiting.\n"); 244 | return -1; 245 | } 246 | 247 | // Set appsrc caps 248 | // TODO :: Remove the format hardcode 249 | gst_app_src_set_caps(GST_APP_SRC(src), 250 | gst_caps_new_simple("video/x-raw", "format", G_TYPE_STRING, "RGB", "width", 251 | G_TYPE_INT, mWidth, "height", G_TYPE_INT, mHeight, 252 | "framerate", GST_TYPE_FRACTION, 25, 1, NULL)); 253 | 254 | // Setup appsrc 255 | g_object_set(G_OBJECT(src), "is-live", TRUE, "format", GST_FORMAT_TIME, NULL); 256 | 257 | // Setup convertor 258 | caps = gst_caps_new_simple("video/x-raw", "format", G_TYPE_STRING, "I420", NULL); 259 | 260 | // Setup encoder 261 | 262 | // Setup payload 263 | 264 | // Setup sink 265 | g_object_set(G_OBJECT(sink), "host", mHost.c_str(), NULL); 266 | g_object_set(G_OBJECT(sink), "port", mPort, NULL); 267 | 268 | // Add element to bin 269 | // gst_bin_add_many(GST_BIN(mPipeline), src, conv, enc, parser, payload, sink, NULL); 270 | gst_bin_add_many(GST_BIN(mPipeline), src, conv, mTextOverlay, enc, parser, payload, sink, NULL); 271 | 272 | // Link src to sink 273 | gst_element_link_many(src, conv, NULL); 274 | link_ok = gst_element_link_filtered(conv, mTextOverlay, caps); 275 | if (!link_ok) { 276 | log_error("Failed to link convertor and encoder!"); 277 | } 278 | gst_element_link_many(mTextOverlay, enc, payload, sink, NULL); 279 | 280 | // Connect signals 281 | GstAppSrcCallbacks cbs; 282 | cbs.need_data = cb_need_data; 283 | cbs.enough_data = cb_enough_data; 284 | cbs.seek_data = cb_seek_data; 285 | gst_app_src_set_callbacks(GST_APP_SRC_CAST(src), &cbs, this, NULL); 286 | 287 | // Set pipeline to play 288 | gst_element_set_state(mPipeline, GST_STATE_PLAYING); 289 | 290 | return ret; 291 | } 292 | 293 | int VideoStreamUdp::destroyAppsrcPipeline() 294 | { 295 | log_info("%s::%s", typeid(this).name(), __func__); 296 | 297 | int ret = 0; 298 | 299 | // clean up 300 | gst_element_set_state(mPipeline, GST_STATE_NULL); 301 | gst_object_unref(GST_OBJECT(mPipeline)); 302 | 303 | return ret; 304 | } 305 | -------------------------------------------------------------------------------- /src/VideoStreamUdp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2018 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #pragma once 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | #include "CameraDevice.h" 25 | #include "VideoStream.h" 26 | 27 | class VideoStreamUdp final : public VideoStream { 28 | public: 29 | VideoStreamUdp(std::shared_ptr camDev); 30 | ~VideoStreamUdp(); 31 | 32 | int init(); 33 | int uninit(); 34 | int start(); 35 | int stop(); 36 | int getState(); 37 | int setResolution(int imgWidth, int imgHeight); 38 | int getResolution(int &imgWidth, int &imgHeight); 39 | int setFormat(int vidFormat); 40 | int getFormat(); 41 | // The host/IP/Multicast group to send the packets to 42 | int setAddress(std::string ipAddr); 43 | std::string getAddress(); 44 | // The port to send the packets to 45 | int setPort(uint32_t port); 46 | int getPort(); 47 | int setTextOverlay(std::string text, int timeSec); 48 | std::string getTextOverlay(); 49 | GstBuffer *readFrame(); 50 | 51 | private: 52 | int setState(int state); 53 | int createAppsrcPipeline(); 54 | int destroyAppsrcPipeline(); 55 | std::shared_ptr mCamDev; 56 | std::atomic mState; 57 | uint32_t mWidth; 58 | uint32_t mHeight; 59 | std::string mHost; 60 | uint32_t mPort; 61 | std::string mOvText; 62 | int mOvTime; // Time in sec to keep overlay, -1 forever 63 | int mOvFrmCnt; // framerate * mOvTime 64 | GstElement *mPipeline; 65 | GstElement *mTextOverlay; 66 | }; 67 | -------------------------------------------------------------------------------- /src/avahi_publisher.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2017 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #ifdef ENABLE_AVAHI 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #include "avahi_publisher.h" 27 | #include "log.h" 28 | #include "mainloop.h" 29 | 30 | AvahiPublisher::AvahiPublisher(std::map> &_strMap, int _port, 31 | const char *_type) 32 | : is_running(false) 33 | , avahi_poll(nullptr) 34 | , info_map(_strMap) 35 | , client(nullptr) 36 | , group(nullptr) 37 | , port(_port) 38 | , type(_type) 39 | { 40 | } 41 | 42 | AvahiPublisher::~AvahiPublisher() 43 | { 44 | stop(); 45 | } 46 | 47 | void AvahiPublisher::avahi_client_cb(AvahiClient *c, AvahiClientState state, void *userdata) 48 | { 49 | assert(c); 50 | AvahiPublisher *publisher = (AvahiPublisher *)userdata; 51 | 52 | switch (state) { 53 | case AVAHI_CLIENT_S_RUNNING: 54 | publisher->publish_services(c); 55 | break; 56 | 57 | case AVAHI_CLIENT_FAILURE: 58 | log_error("Failed to create avahi client: %s", avahi_strerror(avahi_client_errno(c))); 59 | // TODO: Restart avahi publisher 60 | break; 61 | 62 | case AVAHI_CLIENT_S_COLLISION: 63 | case AVAHI_CLIENT_S_REGISTERING: 64 | publisher->reset_services(); 65 | break; 66 | 67 | case AVAHI_CLIENT_CONNECTING:; 68 | } 69 | } 70 | 71 | void AvahiPublisher::entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, 72 | void *userdata) 73 | { 74 | assert(g); 75 | 76 | /* Called whenever the entry group state changes */ 77 | 78 | switch (state) { 79 | case AVAHI_ENTRY_GROUP_COLLISION: 80 | log_debug("Service name collision. Ignoring service"); 81 | // TODO: Handle error 82 | break; 83 | 84 | case AVAHI_ENTRY_GROUP_FAILURE: 85 | log_error("Failed to create avahi group"); 86 | // TODO: Restart avahi publisher 87 | break; 88 | 89 | default:; 90 | } 91 | } 92 | 93 | void AvahiPublisher::reset_services() 94 | { 95 | if (group) 96 | avahi_entry_group_reset(group); 97 | } 98 | 99 | AvahiStringList *AvahiPublisher::txt_record_from_list(const std::vector &txtList) 100 | { 101 | AvahiStringList *lst = NULL; 102 | for (auto txt : txtList) { 103 | lst = avahi_string_list_add(lst, txt.c_str()); 104 | } 105 | 106 | return lst; 107 | } 108 | 109 | void AvahiPublisher::publish_services(AvahiClient *c) 110 | { 111 | int ret; 112 | 113 | if (!group) { 114 | group = avahi_entry_group_new(c, entry_group_callback, NULL); 115 | if (!group) { 116 | log_error("Failed to create avahi group: %s", avahi_strerror(avahi_client_errno(c))); 117 | // TODO: Restart avahi publisher 118 | } 119 | } 120 | 121 | if (avahi_entry_group_is_empty(group)) { 122 | for (auto kv : info_map) { 123 | AvahiStringList *strlist = txt_record_from_list(kv.second); 124 | ret = avahi_entry_group_add_service_strlst(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_INET, 125 | (AvahiPublishFlags)0, (kv.first).c_str(), 126 | type, NULL, NULL, port, strlist); 127 | avahi_string_list_free(strlist); 128 | if (ret != 0) { 129 | // TODO: handle error 130 | log_error("Failed to add service %s", (kv.first).c_str()); 131 | } 132 | } 133 | 134 | ret = avahi_entry_group_commit(group); 135 | if (ret < 0) { 136 | // TODO: handle error 137 | log_error("Failed to add services to avahi group."); 138 | } 139 | } 140 | } 141 | 142 | void AvahiPublisher::start() 143 | { 144 | log_info("AVAHI START"); 145 | if (is_running) 146 | return; 147 | 148 | if (info_map.size() == 0) { 149 | log_debug("No streams found. Not publishing avahi services"); 150 | return; 151 | } 152 | 153 | is_running = true; 154 | int error; 155 | 156 | client = avahi_client_new(Mainloop::get_mainloop()->get_avahi_poll_api(), (AvahiClientFlags)0, 157 | avahi_client_cb, this, &error); 158 | if (!client) { 159 | log_error("Unalbe to create avahi client. Video streams won't be published as avahi services."); 160 | } 161 | } 162 | 163 | void AvahiPublisher::stop() 164 | { 165 | if (!is_running) 166 | return; 167 | is_running = false; 168 | 169 | avahi_client_free(client); 170 | client = nullptr; 171 | group = nullptr; 172 | } 173 | #endif 174 | -------------------------------------------------------------------------------- /src/avahi_publisher.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2017 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #pragma once 19 | 20 | #ifdef ENABLE_AVAHI 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | class AvahiPublisher { 30 | public: 31 | AvahiPublisher(std::map> &_strMap, int _port, 32 | const char *_type); 33 | ~AvahiPublisher(); 34 | void start(); 35 | void stop(); 36 | 37 | private: 38 | bool is_running; 39 | const AvahiPoll *avahi_poll; 40 | std::map> &info_map; 41 | AvahiClient *client; 42 | AvahiEntryGroup *group; 43 | int port; 44 | const char *type; 45 | 46 | void publish_services(AvahiClient *c); 47 | void reset_services(); 48 | static void avahi_client_cb(AvahiClient *c, AvahiClientState state, void *userdata); 49 | static void entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, 50 | void *userdata); 51 | AvahiStringList *txt_record_from_list(const std::vector &txtList); 52 | }; 53 | #endif 54 | -------------------------------------------------------------------------------- /src/conf_file.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2017 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #pragma once 19 | 20 | /** 21 | * Load and parse multiple conf files, offering methods to extract the configuration options to user 22 | * structs. 23 | */ 24 | class ConfFile { 25 | public: 26 | ConfFile() 27 | : _files(nullptr) 28 | , _sections(nullptr){}; 29 | ~ConfFile(); 30 | 31 | struct section_iter { 32 | const char *name; 33 | size_t name_len; 34 | void *ptr; 35 | }; 36 | 37 | /** 38 | * Table with information on how to parse options from conf file. 39 | * 40 | * @param key The key to the option field 41 | * @param required If the field is required, extract_options() will fail if it is not found 42 | * @param parser_func Function to parse the value of the desired option, where val is the 43 | * pointer to value as string, val_len is size of the value, storage is a pointer to the area to 44 | * store the parsed value and storage_len is the length of the storage area. 45 | * @param storage.offset The offset from the beggining of the struct used in extract_options() 46 | * to store the values. 47 | * @param storage.len The length in bytes of the storage poited by @a storage_offset. 48 | */ 49 | struct OptionsTable { 50 | const char *key; 51 | bool required; 52 | int (*parser_func)(const char *val, size_t val_len, void *storage, size_t storage_len); 53 | struct { 54 | off_t offset; 55 | size_t len; 56 | } storage; 57 | }; 58 | 59 | /** 60 | * Load and parse a configuration file. 61 | * 62 | * Open the file using mmap and parse it, keeping an internal structure with pointers to conf 63 | * sections and options, in order to do a quick extraction using extract_options(). 64 | * When an option in a section is already present before parse is called, the option is 65 | * overrided by the option from the new file. Other options from sections are not changed. 66 | * Files are kept open until release_all() is called or the ConfFile object is destructed. 67 | * 68 | * @param filename The conf filename 69 | * @return errno on IO or parsing errors or @c 0 if successful 70 | */ 71 | int parse(const char *filename); 72 | 73 | /** 74 | * Release all opened files and internal structures from this ConfFile. 75 | */ 76 | void release_all(); 77 | 78 | /** 79 | * Extract options set in @a table from section @a section_name 80 | * 81 | * @param section_name Name of the section to extract options. 82 | * @param table Array of TableOption with information on which fields are going to be 83 | * extracted and how to extract them. 84 | * @param table_len The number of elements in @a table. 85 | * @param data A pointer to the struct that will be used to hold the extracted data. 86 | */ 87 | int extract_options(const char *section_name, const OptionsTable table[], size_t table_len, 88 | void *data) const; 89 | 90 | /** 91 | * Extract options set in @a table from section @a iter 92 | * 93 | * @param iter An section_iter filled by get_sections() 94 | * @param table Array of TableOption with information on which fields are going to be 95 | * extracted and how to extract them. 96 | * @param table_len The number of elements in @a table. 97 | * @param data A pointer to the struct that will be used to hold the extracted data. 98 | * 99 | * @see get_sections() 100 | */ 101 | int extract_options(struct section_iter *iter, const OptionsTable table[], size_t table_len, 102 | void *data) const; 103 | 104 | /** 105 | * Extract option set in @a field from section @a section_name 106 | * 107 | * @param section_name Name of the section to extract options. 108 | * @param key Name of the field that need to be extracted 109 | * @param value A double pointer to char used to output the address of the value extracted. 110 | * On success, value holds pointer to null terminated string. 111 | * Caller must free the memory allocated for value 112 | */ 113 | int extract_options(const char *section_name, const char *key, char **value) const; 114 | 115 | /** 116 | * Get next section name from iterator that matches the shell wildcard @a pattern. 117 | * 118 | * If iter.ptr is @c nullptr get_sections() will look for first occurence of @a pattern in 119 | * section list and fill iter.name with a pointer to section name, iter.name_len with the 120 | * size of section name and iter.ptr with an iterator to be used in consecutive calls. 121 | * If iter.ptr is filled with value from previous calls, get_section() will look for consective 122 | * occurences of @a pattern. 123 | * 124 | * @param pattern A shell wilcard pattern. 125 | * @param iter A instance of struct section_iter to be filled section data that matches @a 126 | * pattern. 127 | * 128 | * @return 0 if a section is found or -ENOENT if no section matches the pattern. 129 | */ 130 | int get_sections(const char *pattern, struct section_iter *iter); 131 | 132 | // Helpers 133 | static int parse_bool(const char *val, size_t val_len, void *storage, size_t storage_len); 134 | static int parse_stl_set(const char *val, size_t val_len, void *storage, size_t storage_len); 135 | static int parse_str_dup(const char *val, size_t val_len, void *storage, size_t storage_len); 136 | static int parse_str_buf(const char *val, size_t val_len, void *storage, size_t storage_len); 137 | 138 | #define DECLARE_PARSE_INT(_type) \ 139 | static int parse_##_type(const char *val, size_t val_len, void *storage, size_t storage_len) 140 | DECLARE_PARSE_INT(i); 141 | DECLARE_PARSE_INT(ul); 142 | DECLARE_PARSE_INT(ull); 143 | #undef DECLARE_PARSE_INT 144 | 145 | private: 146 | struct conffile *_files; 147 | struct section *_sections; 148 | 149 | int _parse_file(const char *addr, size_t len, const char *filename); 150 | struct section *_find_section(const char *section_name, size_t len) const; 151 | struct config *_find_config(struct section *s, const char *key_name, size_t key_len) const; 152 | 153 | struct section *_add_section(const char *addr, size_t len, int line, const char *filename); 154 | int _add_config(struct section *s, const char *entry, size_t entry_len, const char *filename, 155 | int line); 156 | void _trim(const char **str, size_t *len); 157 | int _extract_options_from_section(struct section *s, const OptionsTable table[], 158 | size_t table_len, void *data) const; 159 | }; 160 | 161 | /* 162 | * Helper to create the parameter storage from OptionsTable, using _field from _struct. 163 | */ 164 | #define OPTIONS_TABLE_STRUCT_FIELD(_struct, _field) \ 165 | { \ 166 | offsetof(_struct, _field), sizeof(_struct::_field) \ 167 | } 168 | -------------------------------------------------------------------------------- /src/glib_mainloop.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2017 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #include "glib_mainloop.h" 25 | #include "log.h" 26 | 27 | static GMainLoop *gmainloop = nullptr; 28 | 29 | struct fd_handler { 30 | bool (*cb)(const void *data, int flags); 31 | const void *data; 32 | }; 33 | 34 | static gboolean exit_signal_handler(gpointer mainloop) 35 | { 36 | g_main_loop_quit((GMainLoop *)mainloop); 37 | return true; 38 | } 39 | 40 | static void setup_signal_handlers() 41 | { 42 | GSource *source = NULL; 43 | GMainContext *ctx = g_main_loop_get_context(gmainloop); 44 | 45 | source = g_unix_signal_source_new(SIGINT); 46 | g_source_set_callback(source, exit_signal_handler, gmainloop, NULL); 47 | g_source_attach(source, ctx); 48 | 49 | source = g_unix_signal_source_new(SIGTERM); 50 | g_source_set_callback(source, exit_signal_handler, gmainloop, NULL); 51 | g_source_attach(source, ctx); 52 | } 53 | 54 | GlibMainloop::GlibMainloop() 55 | #ifdef ENABLE_AVAHI 56 | : avahi_poll(nullptr) 57 | #endif 58 | { 59 | gmainloop = g_main_loop_new(NULL, FALSE); 60 | mainloop = this; 61 | setup_signal_handlers(); 62 | } 63 | 64 | GlibMainloop::~GlibMainloop() 65 | { 66 | mainloop = nullptr; 67 | #ifdef ENABLE_AVAHI 68 | if (avahi_poll) { 69 | avahi_glib_poll_free(avahi_poll); 70 | avahi_poll = nullptr; 71 | } 72 | #endif 73 | 74 | g_main_loop_unref(gmainloop); 75 | gmainloop = nullptr; 76 | } 77 | 78 | void GlibMainloop::loop() 79 | { 80 | g_main_loop_run(gmainloop); 81 | } 82 | 83 | #ifdef ENABLE_AVAHI 84 | const AvahiPoll *GlibMainloop::get_avahi_poll_api() 85 | { 86 | if (!avahi_poll) 87 | avahi_poll = avahi_glib_poll_new(NULL, G_PRIORITY_DEFAULT); 88 | return avahi_glib_poll_get(avahi_poll); 89 | } 90 | #endif 91 | 92 | void GlibMainloop::quit() 93 | { 94 | g_main_loop_quit(gmainloop); 95 | } 96 | 97 | unsigned int GlibMainloop::add_timeout(unsigned int timeout_msec, bool (*cb)(void *), 98 | const void *data) 99 | { 100 | assert(cb); 101 | 102 | return g_timeout_add(timeout_msec, (GSourceFunc)cb, (void *)data); 103 | } 104 | 105 | void GlibMainloop::del_timeout(unsigned int timeout_handler) 106 | { 107 | g_source_remove(timeout_handler); 108 | } 109 | 110 | static gboolean fd_io_cb(gint fd, GIOCondition condition, gpointer user_data) 111 | { 112 | int flags = 0; 113 | struct fd_handler *handler; 114 | 115 | assert(user_data); 116 | handler = (struct fd_handler *)user_data; 117 | 118 | log_debug("Poll IO fd: %d flag: %d", fd, condition); 119 | 120 | if (condition & G_IO_IN) 121 | flags |= Mainloop::IO_IN; 122 | if (condition & G_IO_OUT) 123 | flags |= Mainloop::IO_OUT; 124 | 125 | return handler->cb(handler->data, flags); 126 | } 127 | 128 | static void fd_del_cb(void *data) 129 | { 130 | assert(data); 131 | 132 | delete (struct fd_handler *)data; 133 | } 134 | 135 | int GlibMainloop::add_fd(int fd, int flags, bool (*cb)(const void *data, int flags), const void *data) 136 | { 137 | struct fd_handler *handler; 138 | int glib_flags = 0; 139 | 140 | assert(cb); 141 | 142 | if (flags & Mainloop::IO_IN) 143 | glib_flags |= G_IO_IN; 144 | if (flags & Mainloop::IO_OUT) 145 | glib_flags |= G_IO_OUT; 146 | 147 | if (!glib_flags) { 148 | log_error("No valid flags are set. flags=%d", flags); 149 | return -EINVAL; 150 | } 151 | 152 | handler = new fd_handler{cb, data}; 153 | log_debug("Adding fd: %d glib_flags: %d", fd, glib_flags); 154 | return g_unix_fd_add_full(0, fd, (GIOCondition)glib_flags, fd_io_cb, handler, fd_del_cb); 155 | } 156 | 157 | void GlibMainloop::remove_fd(int handler) 158 | { 159 | assert(handler > 0); 160 | 161 | g_source_remove(handler); 162 | } 163 | -------------------------------------------------------------------------------- /src/glib_mainloop.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2017 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #pragma once 19 | 20 | #ifdef ENABLE_AVAHI 21 | #include 22 | #endif 23 | #include 24 | 25 | #include "mainloop.h" 26 | 27 | class GlibMainloop : public Mainloop { 28 | public: 29 | GlibMainloop(); 30 | ~GlibMainloop(); 31 | void loop() override; 32 | void quit() override; 33 | #ifdef ENABLE_AVAHI 34 | const AvahiPoll *get_avahi_poll_api() override; 35 | #endif 36 | 37 | unsigned int add_timeout(unsigned int timeout_msec, bool (*cb)(void *), 38 | const void *data) override; 39 | void del_timeout(unsigned int timeout_handler) override; 40 | int add_fd(int fd, int flags, bool (*cb)(const void *data, int flags), const void *data) override; 41 | void remove_fd(int handler) override; 42 | 43 | #ifdef ENABLE_AVAHI 44 | private: 45 | AvahiGLibPoll *avahi_poll; 46 | #endif 47 | }; 48 | -------------------------------------------------------------------------------- /src/log.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2017 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #include "log.h" 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | #define COLOR_RED "\033[31m" 28 | #define COLOR_ORANGE "\033[33m" 29 | #define COLOR_BLUE "\033[34m" 30 | #define COLOR_GREEN "\033[32m" 31 | #define COLOR_RESET "\033[0m" 32 | 33 | Log::Level Log::_max_level = Level::INFO; 34 | int Log::_target_fd = -1; 35 | bool Log::_show_colors; 36 | 37 | const char *Log::_get_color(Level level) 38 | { 39 | if (!_show_colors) 40 | return nullptr; 41 | 42 | switch (level) { 43 | case Level::ERROR: 44 | return COLOR_RED; 45 | case Level::WARNING: 46 | return COLOR_ORANGE; 47 | case Level::INFO: 48 | return COLOR_BLUE; 49 | case Level::DEBUG: 50 | return COLOR_GREEN; 51 | } 52 | 53 | return nullptr; 54 | } 55 | 56 | int Log::open() 57 | { 58 | assert_or_return(_target_fd < 0, -1); 59 | 60 | /* for now, only logging is supported only to stderr */ 61 | _target_fd = STDERR_FILENO; 62 | 63 | if (isatty(_target_fd)) 64 | _show_colors = true; 65 | 66 | return 0; 67 | } 68 | 69 | int Log::close() 70 | { 71 | /* see _target_fd on open() */ 72 | fflush(stderr); 73 | 74 | return 0; 75 | } 76 | 77 | void Log::set_max_level(Level level) 78 | { 79 | _max_level = level; 80 | } 81 | 82 | void Log::logv(Level level, const char *format, va_list ap) 83 | { 84 | struct iovec iovec[6] = { }; 85 | const char *color; 86 | int n = 0; 87 | char buffer[LINE_MAX]; 88 | int save_errno; 89 | 90 | if (_max_level < level) 91 | return; 92 | 93 | /* so %m works as expected */ 94 | save_errno = errno; 95 | 96 | color = _get_color(level); 97 | 98 | if (color) 99 | IOVEC_SET_STRING(iovec[n++], color); 100 | 101 | errno = save_errno; 102 | vsnprintf(buffer, sizeof(buffer), format, ap); 103 | 104 | IOVEC_SET_STRING(iovec[n++], buffer); 105 | 106 | if (color) 107 | IOVEC_SET_STRING(iovec[n++], COLOR_RESET); 108 | 109 | IOVEC_SET_STRING(iovec[n++], "\n"); 110 | 111 | (void)writev(_target_fd, iovec, n); 112 | } 113 | 114 | void Log::log(Level level, const char *format, ...) 115 | { 116 | va_list ap; 117 | 118 | va_start(ap, format); 119 | logv(level, format, ap); 120 | va_end(ap); 121 | } 122 | -------------------------------------------------------------------------------- /src/log.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2017 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #pragma once 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | #include "macro.h" 25 | 26 | class Log { 27 | public: 28 | enum class Level { 29 | ERROR = 0, 30 | WARNING, 31 | INFO, 32 | DEBUG, 33 | }; 34 | 35 | static int open(); 36 | static int close(); 37 | 38 | static Level get_max_level() _pure_ { return _max_level; } 39 | static void set_max_level(Level level); 40 | 41 | static void logv(Level level, const char *format, va_list ap); 42 | static void log(Level level, const char *format, ...) _printf_format_(2, 3); 43 | 44 | protected: 45 | static const char *_get_color(Level level); 46 | 47 | static int _target_fd; 48 | static Level _max_level; 49 | static bool _show_colors; 50 | }; 51 | 52 | #define log_debug(...) Log::log(Log::Level::DEBUG, __VA_ARGS__) 53 | #define log_info(...) Log::log(Log::Level::INFO, __VA_ARGS__) 54 | #define log_warning(...) Log::log(Log::Level::WARNING, __VA_ARGS__) 55 | #define log_error(...) Log::log(Log::Level::ERROR, __VA_ARGS__) 56 | 57 | #define assert_or_return(exp, ...) \ 58 | do { \ 59 | if (__builtin_expect(!(exp), 0)) { \ 60 | log_warning("Expresssion `" #exp "` is false"); \ 61 | return __VA_ARGS__; \ 62 | } \ 63 | } while (0) 64 | -------------------------------------------------------------------------------- /src/macro.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2017 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #pragma once 19 | 20 | #define _must_check_ __attribute__((warn_unused_result)) 21 | #define _printf_format_(a, b) __attribute__((format(printf, a, b))) 22 | #define _unused_ __attribute__((unused)) 23 | #define _always_inline_ __inline__ __attribute__((always_inline)) 24 | #define _cleanup_(x) __attribute__((cleanup(x))) 25 | #define _pure_ __attribute__((pure)) 26 | #define _packed_ __attribute__((packed)) 27 | 28 | #define IOVEC_SET_STRING(i, s) \ 29 | do { \ 30 | struct iovec *_i = &(i); \ 31 | char *_s = (char *)(s); \ 32 | _i->iov_base = _s; \ 33 | _i->iov_len = strlen(_s); \ 34 | } while (0) 35 | -------------------------------------------------------------------------------- /src/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2017 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | #include "conf_file.h" 28 | #include "glib_mainloop.h" 29 | #include "log.h" 30 | #include "settings.h" 31 | #include "util.h" 32 | #include "CameraServer.h" 33 | 34 | #define DEFAULT_CONFFILE "/etc/dcm/main.conf" 35 | #define DEFAULT_CONF_DIR "/etc/dcm/config.d" 36 | 37 | struct options { 38 | const char *filename; 39 | const char *conf_dir; 40 | }; 41 | 42 | static void help(FILE *fp) 43 | { 44 | fprintf( 45 | fp, 46 | "%s [OPTIONS...]\n\n" 47 | " -c --conf-file .conf file with configurations for " 48 | "dronecode-camera-manager.\n" 49 | " -d --conf-dir Directory where to look for .conf files overriding\n" 50 | " default conf file.\n" 51 | " -g --debug-log-level Set debug log level. Levels are\n" 52 | " \n" 53 | " -v --verbose Verbose. Same as --debug-log-level=debug\n" 54 | " -h --help Print this message\n", 55 | program_invocation_short_name); 56 | } 57 | 58 | static const char *get_default_file_name() 59 | { 60 | char *s; 61 | 62 | s = getenv("DCM_CONF_FILE"); 63 | if (s) 64 | return s; 65 | 66 | return DEFAULT_CONFFILE; 67 | } 68 | 69 | static const char *get_default_conf_dir() 70 | { 71 | char *s; 72 | 73 | s = getenv("DCM_CONF_DIR"); 74 | if (s) 75 | return s; 76 | 77 | return DEFAULT_CONF_DIR; 78 | } 79 | 80 | static int cmpstr(const void *s1, const void *s2) 81 | { 82 | return strcmp(*(const char **)s1, *(const char **)s2); 83 | } 84 | 85 | static int parse_conf_files(ConfFile &conf, struct options *opt) 86 | { 87 | DIR *dir; 88 | struct dirent *ent; 89 | int ret = 0; 90 | char *files[128] = {}; 91 | int i = 0, j = 0; 92 | 93 | if (opt->filename != nullptr) { 94 | // If filename was specified then use it and fail if it doesn't exist 95 | ret = conf.parse(opt->filename); 96 | if (ret < 0) { 97 | return ret; 98 | } 99 | } else { 100 | // Otherwise open default conf file 101 | ret = conf.parse(get_default_file_name()); 102 | // If there's no default conf file, everything is good 103 | if (ret < 0 && ret != -ENOENT) { 104 | return ret; 105 | } 106 | } 107 | 108 | // Then, parse all files on configuration directory 109 | dir = opendir(opt->conf_dir); 110 | if (!dir) 111 | return 0; 112 | 113 | while ((ent = readdir(dir))) { 114 | char path[PATH_MAX]; 115 | struct stat st; 116 | 117 | ret = snprintf(path, sizeof(path), "%s/%s", opt->conf_dir, ent->d_name); 118 | if (ret >= (int)sizeof(path)) { 119 | log_error("Couldn't open directory %s", opt->conf_dir); 120 | ret = -EINVAL; 121 | goto fail; 122 | } 123 | if (stat(path, &st) < 0 || !S_ISREG(st.st_mode)) { 124 | continue; 125 | } 126 | files[i] = strdup(path); 127 | if (!files[i]) { 128 | ret = -ENOMEM; 129 | goto fail; 130 | } 131 | i++; 132 | 133 | if ((size_t)i > sizeof(files) / sizeof(*files)) { 134 | log_warning("Too many files on %s. Not all of them will be considered", opt->conf_dir); 135 | break; 136 | } 137 | } 138 | 139 | qsort(files, (size_t)i, sizeof(char *), cmpstr); 140 | 141 | for (j = 0; j < i; j++) { 142 | ret = conf.parse(files[j]); 143 | if (ret < 0) 144 | goto fail; 145 | free(files[j]); 146 | } 147 | 148 | closedir(dir); 149 | 150 | return 0; 151 | fail: 152 | while (j < i) { 153 | free(files[j++]); 154 | } 155 | 156 | closedir(dir); 157 | 158 | return ret; 159 | } 160 | 161 | static int log_level_from_str(const char *str) 162 | { 163 | if (strcaseeq(str, "error")) 164 | return (int)Log::Level::ERROR; 165 | if (strcaseeq(str, "warning")) 166 | return (int)Log::Level::WARNING; 167 | if (strcaseeq(str, "info")) 168 | return (int)Log::Level::INFO; 169 | if (strcaseeq(str, "debug")) 170 | return (int)Log::Level::DEBUG; 171 | 172 | return -EINVAL; 173 | } 174 | 175 | static int parse_argv(int argc, char *argv[], struct options *opt) 176 | { 177 | static const struct option options[] = {{"conf-file", required_argument, NULL, 'c'}, 178 | {"conf-dir", required_argument, NULL, 'd'}, 179 | {"debug-log-level", required_argument, NULL, 'g'}, 180 | {"verbose", no_argument, NULL, 'v'}}; 181 | int c; 182 | 183 | assert(argc >= 0); 184 | assert(argv); 185 | 186 | while ((c = getopt_long(argc, argv, "hd:c:d:g:v", options, NULL)) >= 0) { 187 | switch (c) { 188 | case 'h': 189 | help(stdout); 190 | return 0; 191 | case 'c': { 192 | opt->filename = optarg; 193 | break; 194 | } 195 | case 'd': { 196 | opt->conf_dir = optarg; 197 | break; 198 | } 199 | case 'g': { 200 | int lvl = log_level_from_str(optarg); 201 | if (lvl == -EINVAL) { 202 | log_error("Invalid argument for debug-log-level = %s", optarg); 203 | help(stderr); 204 | return -EINVAL; 205 | } 206 | Log::set_max_level((Log::Level)lvl); 207 | break; 208 | } 209 | case 'v': { 210 | Log::set_max_level(Log::Level::DEBUG); 211 | break; 212 | } 213 | case '?': 214 | default: 215 | help(stderr); 216 | return -EINVAL; 217 | } 218 | } 219 | 220 | /* positional arguments */ 221 | if (optind != argc) { 222 | log_error("Invalid argument"); 223 | help(stderr); 224 | return -EINVAL; 225 | } 226 | 227 | if (!opt->conf_dir) 228 | opt->conf_dir = get_default_conf_dir(); 229 | 230 | return 2; 231 | } 232 | 233 | int main(int argc, char *argv[]) 234 | { 235 | struct options opt = {0}; 236 | Log::open(); 237 | 238 | ConfFile *conf; 239 | GlibMainloop mainloop; 240 | 241 | if (parse_argv(argc, argv, &opt) != 2) { 242 | Log::close(); 243 | return EXIT_FAILURE; 244 | } 245 | 246 | conf = new ConfFile(); 247 | if (parse_conf_files(*conf, &opt) < 0) { 248 | delete conf; 249 | Log::close(); 250 | return EXIT_FAILURE; 251 | } 252 | 253 | CameraServer camServer(*conf); 254 | camServer.start(); 255 | 256 | delete conf; 257 | log_debug("Starting Dronecode Camera Manager"); 258 | 259 | mainloop.loop(); 260 | 261 | Log::close(); 262 | 263 | return 0; 264 | } 265 | -------------------------------------------------------------------------------- /src/mainloop.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2017 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #include "mainloop.h" 19 | 20 | Mainloop *Mainloop::mainloop = 0; 21 | -------------------------------------------------------------------------------- /src/mainloop.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2017 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #pragma once 19 | 20 | #ifdef ENABLE_AVAHI 21 | #include 22 | #endif 23 | #include 24 | 25 | class Mainloop { 26 | public: 27 | virtual void loop() = 0; 28 | #ifdef ENABLE_AVAHI 29 | virtual const AvahiPoll *get_avahi_poll_api() = 0; 30 | #endif 31 | static Mainloop *get_mainloop() { return mainloop; }; 32 | virtual unsigned int add_timeout(unsigned int timeout_msec, bool (*cb)(void *), 33 | const void *data) = 0; 34 | virtual void del_timeout(unsigned int timeout_handler) = 0; 35 | virtual int add_fd(int fd, int flags, bool (*cb)(const void *data, int flags), const void *data) = 0; 36 | virtual void remove_fd(int handler) = 0; 37 | virtual void quit() = 0; 38 | 39 | enum io_flags { IO_IN = 1, IO_OUT = 2 }; 40 | 41 | protected: 42 | static Mainloop *mainloop; 43 | }; 44 | -------------------------------------------------------------------------------- /src/mavlink_server.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2017 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #pragma once 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include "CameraComponent.h" 26 | #include "conf_file.h" 27 | #include "socket.h" 28 | 29 | typedef struct image_callback { 30 | int comp_id; /* Component ID */ 31 | struct sockaddr_in addr; /* Requester address */ 32 | } image_callback_t; 33 | 34 | class MavlinkServer { 35 | public: 36 | MavlinkServer(const ConfFile &conf); 37 | ~MavlinkServer(); 38 | void start(); 39 | void stop(); 40 | int addCameraComponent(CameraComponent *camComp); 41 | void removeCameraComponent(CameraComponent *camComp); 42 | CameraComponent *getCameraComponent(int compID); 43 | 44 | private: 45 | bool _is_running; 46 | unsigned int _timeout_handler; 47 | UDPSocket _udp; 48 | struct sockaddr_in _broadcast_addr = {}; 49 | bool _is_sys_id_found; 50 | int _system_id; 51 | int _comp_id; 52 | std::map compIdToObj; 53 | 54 | void _message_received(const struct sockaddr_in &sockaddr, const struct buffer &buf); 55 | void _handle_mavlink_message(const struct sockaddr_in &addr, mavlink_message_t *msg); 56 | void _handle_request_camera_information(const struct sockaddr_in &addr, 57 | mavlink_command_long_t &cmd); 58 | void _handle_request_camera_settings(const struct sockaddr_in &addr, 59 | mavlink_command_long_t &cmd); 60 | void _handle_request_storage_information(const struct sockaddr_in &addr, 61 | mavlink_command_long_t &cmd); 62 | void _handle_set_camera_mode(const struct sockaddr_in &addr, 63 | mavlink_command_long_t &cmd); 64 | void _handle_image_start_capture(const struct sockaddr_in &addr, mavlink_command_long_t &cmd); 65 | void _handle_image_stop_capture(const struct sockaddr_in &addr, mavlink_command_long_t &cmd); 66 | void _handle_video_start_capture(const struct sockaddr_in &addr, mavlink_command_long_t &cmd); 67 | void _handle_video_stop_capture(const struct sockaddr_in &addr, mavlink_command_long_t &cmd); 68 | void _image_captured_cb(image_callback_t cb_data, int result, int seq_num); 69 | void _handle_request_camera_capture_status(const struct sockaddr_in &addr, 70 | mavlink_command_long_t &cmd); 71 | void _handle_param_ext_request_read(const struct sockaddr_in &addr, mavlink_message_t *msg); 72 | void _handle_param_ext_request_list(const struct sockaddr_in &addr, mavlink_message_t *msg); 73 | void _handle_param_ext_set(const struct sockaddr_in &addr, mavlink_message_t *msg); 74 | void _handle_reset_camera_settings(const struct sockaddr_in &addr, mavlink_command_long_t &cmd); 75 | void _handle_heartbeat(const struct sockaddr_in &addr, mavlink_message_t *msg); 76 | bool _send_camera_capture_status(int compid, const struct sockaddr_in &addr); 77 | bool _send_mavlink_message(const struct sockaddr_in *addr, mavlink_message_t &msg); 78 | void _send_ack(const struct sockaddr_in &addr, int cmd, int comp_id, bool success); 79 | #if 0 80 | const Stream::FrameSize *_find_best_frame_size(Stream &s, uint32_t w, uint32_t v); 81 | #endif 82 | friend bool _heartbeat_cb(void *data); 83 | 84 | CameraParameters::Mode mav2dcmCameraMode(uint32_t mode); 85 | uint32_t dcm2mavCameraMode(CameraParameters::Mode mode); 86 | }; 87 | -------------------------------------------------------------------------------- /src/pollable.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2017 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | #include 20 | 21 | #include "log.h" 22 | #include "mainloop.h" 23 | #include "pollable.h" 24 | 25 | Pollable::Pollable() 26 | : _fd(-1) 27 | , _read_handler(0) 28 | , _write_handler(0) 29 | { 30 | } 31 | 32 | bool Pollable::_can_read_cb(const void *data, int flags) 33 | { 34 | assert(data); 35 | 36 | Pollable *p = (Pollable *)data; 37 | 38 | if (p->_can_read()) 39 | return true; 40 | 41 | p->monitor_read(false); 42 | return false; 43 | } 44 | 45 | bool Pollable::_can_write_cb(const void *data, int flags) 46 | { 47 | assert(data); 48 | 49 | Pollable *p = (Pollable *)data; 50 | 51 | if (p->_can_write()) 52 | return true; 53 | 54 | p->monitor_write(false); 55 | return false; 56 | } 57 | 58 | void Pollable::monitor_read(bool monitor) 59 | { 60 | Mainloop *m = Mainloop::get_mainloop(); 61 | 62 | if (_read_handler) 63 | m->remove_fd(_read_handler); 64 | 65 | if (monitor) 66 | _read_handler = m->add_fd(_fd, Mainloop::IO_IN, _can_read_cb, this); 67 | else 68 | _read_handler = 0; 69 | } 70 | 71 | void Pollable::monitor_write(bool monitor) 72 | { 73 | Mainloop *m = Mainloop::get_mainloop(); 74 | 75 | if (_write_handler) 76 | m->remove_fd(_write_handler); 77 | 78 | if (monitor) 79 | _write_handler = m->add_fd(_fd, Mainloop::IO_OUT, _can_write_cb, this); 80 | else 81 | _write_handler = 0; 82 | } 83 | -------------------------------------------------------------------------------- /src/pollable.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2017 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #pragma once 19 | 20 | #ifdef ENABLE_AVAHI 21 | #include 22 | #endif 23 | #include 24 | 25 | class Pollable { 26 | public: 27 | Pollable(); 28 | virtual ~Pollable() {} 29 | 30 | void monitor_read(bool monitor); 31 | void monitor_write(bool monitor); 32 | 33 | protected: 34 | int _fd; 35 | virtual bool _can_read() = 0; 36 | virtual bool _can_write() = 0; 37 | 38 | private: 39 | int _read_handler, _write_handler; 40 | static bool _can_read_cb(const void *data, int flags); 41 | static bool _can_write_cb(const void *data, int flags); 42 | }; 43 | -------------------------------------------------------------------------------- /src/settings.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MAVLink Router project 3 | * 4 | * Copyright (C) 2017 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #include "settings.h" 19 | #include "conf.h" 20 | #include "log.h" 21 | 22 | Settings Settings::settings; 23 | int Settings::import_conf_file(const char *filename) 24 | { 25 | const char *section, *key, *value; 26 | size_t section_len, key_len, value_len; 27 | ConfFile conf(filename); 28 | 29 | while (conf.next(§ion, §ion_len, &key, &key_len, &value, &value_len) == 0) { 30 | sections[std::string(section, section_len)][std::string(key, key_len)] 31 | = std::string(value, value_len); 32 | } 33 | 34 | return 0; 35 | } 36 | 37 | std::set Settings::get_value_as_set(std::string section, std::string key) 38 | { 39 | std::set value_set; 40 | std::string &value = sections[section][key]; 41 | size_t i = 0, j; 42 | 43 | j = value.find(' '); 44 | while (j != std::string::npos) { 45 | value_set.insert(value.substr(i, j - i)); 46 | i = j + 1; 47 | j = value.find(' ', j + 1); 48 | } 49 | value_set.insert(value.substr(i, j - i)); 50 | 51 | return value_set; 52 | } 53 | -------------------------------------------------------------------------------- /src/settings.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MAVLink Router project 3 | * 4 | * Copyright (C) 2017 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #pragma once 19 | #include 20 | #include 21 | 22 | class Settings { 23 | public: 24 | ~Settings(){}; 25 | static Settings &get_instance() { return settings; }; 26 | 27 | int import_conf_file(const char *filename); 28 | std::map> &get_sections() { return sections; }; 29 | std::set get_value_as_set(std::string section, std::string key); 30 | 31 | private: 32 | std::map> sections; 33 | Settings() {} 34 | static Settings settings; 35 | }; 36 | -------------------------------------------------------------------------------- /src/socket.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2017 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #include "log.h" 27 | #include "socket.h" 28 | 29 | #define DEFAULT_BUF_SIZE 1024 30 | 31 | Socket::Socket() 32 | : _read_cb([](const struct buffer &buf, const struct sockaddr_in &sockaddr) {}) 33 | , _write_buf{0, nullptr} 34 | { 35 | } 36 | 37 | Socket::~Socket() 38 | { 39 | free(_write_buf.data); 40 | } 41 | 42 | int Socket::write(const struct buffer &buf, const struct sockaddr_in &sockaddr) 43 | { 44 | int r; 45 | r = _do_write(buf, sockaddr); 46 | if (r >= 0) 47 | return r; 48 | 49 | if (_write_buf.data) { 50 | delete[] _write_buf.data; 51 | log_warning("There was another packet to be sent. Dropping that packet."); 52 | } 53 | 54 | _write_buf = {buf.len, new uint8_t[buf.len]}; 55 | memcpy(_write_buf.data, buf.data, buf.len); 56 | sockaddr_buf = sockaddr; 57 | monitor_write(true); 58 | 59 | return 0; 60 | } 61 | 62 | void Socket::set_read_callback( 63 | std::function cb) 64 | { 65 | _read_cb = cb; 66 | } 67 | 68 | bool Socket::_can_read() 69 | { 70 | struct buffer read_buf { 71 | DEFAULT_BUF_SIZE, new uint8_t[DEFAULT_BUF_SIZE] 72 | }; 73 | struct sockaddr_in sockaddr = {}; 74 | int r = _do_read(read_buf, sockaddr); 75 | 76 | if (r == -EAGAIN) { 77 | log_debug("Read packet failed. Trying again."); 78 | goto end; 79 | } 80 | if (r < 0) { 81 | log_debug("Read failed. Droping packet."); 82 | delete[] read_buf.data; 83 | return false; 84 | } 85 | if (r > 0) { 86 | read_buf.len = (unsigned int)r; 87 | _read_cb(read_buf, sockaddr); 88 | } 89 | 90 | end: 91 | delete[] read_buf.data; 92 | return true; 93 | } 94 | 95 | bool Socket::_can_write() 96 | { 97 | int r; 98 | 99 | if (!_write_buf.data) 100 | return false; 101 | 102 | r = _do_write(_write_buf, sockaddr_buf); 103 | if (r == -EAGAIN) { 104 | log_debug("Write package failed. Trying again."); 105 | return true; 106 | } else if (r < 0) { 107 | log_error("Write package failed. Droping packet."); 108 | } 109 | 110 | delete[] _write_buf.data; 111 | _write_buf = {0, nullptr}; 112 | return false; 113 | } 114 | 115 | UDPSocket::UDPSocket() 116 | { 117 | } 118 | 119 | UDPSocket::~UDPSocket() 120 | { 121 | close(); 122 | } 123 | 124 | void UDPSocket::close() 125 | { 126 | if (_fd >= 0) 127 | ::close(_fd); 128 | _fd = -1; 129 | } 130 | 131 | int UDPSocket::open(bool broadcast) 132 | { 133 | const int broadcast_val = 1; 134 | 135 | _fd = socket(AF_INET, SOCK_DGRAM, 0); 136 | if (_fd == -1) { 137 | log_error("Could not create socket (%m)"); 138 | return -1; 139 | } 140 | 141 | #ifdef ENABLE_GAZEBO 142 | // bind socket to port 143 | if (bind("0.0.0.0", 34550) == -1) { 144 | log_error("bind"); 145 | } 146 | #endif 147 | 148 | if (broadcast) { 149 | if (setsockopt(_fd, SOL_SOCKET, SO_BROADCAST, &broadcast_val, sizeof(broadcast_val))) { 150 | log_error("Error enabling broadcast in socket (%m)"); 151 | goto fail; 152 | } 153 | } 154 | 155 | if (fcntl(_fd, F_SETFL, O_NONBLOCK | FASYNC) < 0) { 156 | log_error("Error setting socket _fd as non-blocking (%m)"); 157 | goto fail; 158 | } 159 | 160 | log_info("Open UDP [%d]", _fd); 161 | 162 | monitor_read(true); 163 | return _fd; 164 | 165 | fail: 166 | if (_fd >= 0) { 167 | ::close(_fd); 168 | _fd = -1; 169 | } 170 | return -1; 171 | } 172 | 173 | int UDPSocket::bind(const char *ip, unsigned long port) 174 | { 175 | assert(ip); 176 | 177 | struct sockaddr_in sockaddr = {}; 178 | 179 | sockaddr.sin_family = AF_INET; 180 | sockaddr.sin_addr.s_addr = inet_addr(ip); 181 | sockaddr.sin_port = htons(port); 182 | 183 | if (_fd < 0) { 184 | log_error("Trying to bind an invalid _fd"); 185 | return -EINVAL; 186 | } 187 | 188 | if (::bind(_fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) < 0) { 189 | log_error("Error binding socket (%m)"); 190 | return -1; 191 | } 192 | 193 | log_info("Bind UDP [%d] %s:%lu", _fd, ip, port); 194 | return 0; 195 | } 196 | 197 | int UDPSocket::_do_write(const struct buffer &buf, const struct sockaddr_in &sockaddr) 198 | { 199 | if (_fd < 0) { 200 | log_error("Trying to write to an invalid _fd"); 201 | return -EINVAL; 202 | } 203 | 204 | if (!sockaddr.sin_port) { 205 | log_debug("No one ever connected to %d. No one to write for", _fd); 206 | return 0; 207 | } 208 | 209 | ssize_t r = ::sendto(_fd, buf.data, buf.len, 0, (struct sockaddr *)&sockaddr, sizeof(sockaddr)); 210 | if (r == -1) { 211 | if (errno != EAGAIN && errno != ECONNREFUSED && errno != ENETUNREACH) 212 | log_error("Error sending udp packet (%m)"); 213 | return -errno; 214 | }; 215 | 216 | /* Incomplete packet, we warn and discard the rest */ 217 | if (r != (ssize_t)buf.len) { 218 | log_debug("Discarding packet, incomplete write %zd but len=%u", r, buf.len); 219 | } 220 | 221 | log_debug("UDP: [%d] wrote %zd bytes", _fd, r); 222 | 223 | return r; 224 | } 225 | 226 | int UDPSocket::_do_read(const struct buffer &buf, struct sockaddr_in &sockaddr) 227 | { 228 | socklen_t addrlen = sizeof(sockaddr); 229 | ssize_t r = ::recvfrom(_fd, buf.data, buf.len, 0, (struct sockaddr *)&sockaddr, &addrlen); 230 | if (r == -1) { 231 | if (errno != EAGAIN) 232 | log_error("Error reading udp packet (%m)"); 233 | return -errno; 234 | } 235 | 236 | return r; 237 | } 238 | -------------------------------------------------------------------------------- /src/socket.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2017 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #pragma once 19 | 20 | #include 21 | 22 | #include "pollable.h" 23 | 24 | struct buffer { 25 | unsigned int len; 26 | uint8_t *data; 27 | }; 28 | 29 | class Socket : public Pollable { 30 | public: 31 | Socket(); 32 | virtual ~Socket(); 33 | 34 | int write(const struct buffer &buf, const struct sockaddr_in &sockaddr); 35 | void set_read_callback( 36 | std::function cb); 37 | 38 | protected: 39 | bool _can_read() override; 40 | bool _can_write() override; 41 | virtual int _do_write(const struct buffer &buf, const struct sockaddr_in &sockaddr) = 0; 42 | virtual int _do_read(const struct buffer &buf, struct sockaddr_in &sockaddr) = 0; 43 | 44 | private: 45 | std::function _read_cb; 46 | struct buffer _write_buf; 47 | struct sockaddr_in sockaddr_buf {}; 48 | }; 49 | 50 | class UDPSocket : public Socket { 51 | public: 52 | UDPSocket(); 53 | ~UDPSocket(); 54 | 55 | int open(bool broadcast); 56 | void close(); 57 | int bind(const char *addr, unsigned long port); 58 | 59 | protected: 60 | int _do_write(const struct buffer &buf, const struct sockaddr_in &sockaddr) override; 61 | int _do_read(const struct buffer &buf, struct sockaddr_in &sockaddr) override; 62 | }; 63 | -------------------------------------------------------------------------------- /src/util.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MAVLink Router project 3 | * 4 | * Copyright (C) 2016 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #include "util.h" 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | usec_t ts_usec(const struct timespec *ts) 28 | { 29 | if (ts->tv_sec == (time_t)-1 && ts->tv_nsec == (long)-1) 30 | return USEC_INFINITY; 31 | 32 | if ((usec_t)ts->tv_sec > (UINT64_MAX - (ts->tv_nsec / NSEC_PER_USEC)) / USEC_PER_SEC) 33 | return USEC_INFINITY; 34 | 35 | return (usec_t)ts->tv_sec * USEC_PER_SEC + (usec_t)ts->tv_nsec / NSEC_PER_USEC; 36 | } 37 | 38 | usec_t now_usec(void) 39 | { 40 | struct timespec ts; 41 | 42 | clock_gettime(CLOCK_MONOTONIC, &ts); 43 | 44 | return ts_usec(&ts); 45 | } 46 | 47 | int safe_atoul(const char *s, unsigned long *ret) 48 | { 49 | char *x = NULL; 50 | unsigned long l; 51 | 52 | assert(s); 53 | assert(ret); 54 | 55 | errno = 0; 56 | l = strtoul(s, &x, 0); 57 | 58 | if (!x || x == s || *x || errno) 59 | return errno ? -errno : -EINVAL; 60 | 61 | *ret = l; 62 | 63 | return 0; 64 | } 65 | 66 | int safe_atoull(const char *s, unsigned long long *ret) 67 | { 68 | char *x = NULL; 69 | unsigned long long l; 70 | 71 | assert(s); 72 | assert(ret); 73 | 74 | errno = 0; 75 | l = strtoull(s, &x, 0); 76 | 77 | if (!x || x == s || *x || errno) 78 | return errno ? -errno : -EINVAL; 79 | 80 | *ret = l; 81 | 82 | return 0; 83 | } 84 | 85 | int safe_atoi(const char *s, int *ret) 86 | { 87 | char *x = NULL; 88 | long l; 89 | 90 | assert(s); 91 | assert(ret); 92 | 93 | errno = 0; 94 | l = strtol(s, &x, 0); 95 | 96 | if (!x || x == s || *x || errno) 97 | return errno > 0 ? -errno : -EINVAL; 98 | 99 | if ((long)(int)l != l) 100 | return -ERANGE; 101 | 102 | *ret = (int)l; 103 | return 0; 104 | } 105 | 106 | static inline int is_dir(const char *path) 107 | { 108 | struct stat st; 109 | 110 | if (stat(path, &st) >= 0) 111 | return S_ISDIR(st.st_mode); 112 | 113 | return -errno; 114 | } 115 | 116 | int mkdir_p(const char *path, int len, mode_t mode) 117 | { 118 | char *start, *end; 119 | 120 | start = strndupa(path, len); 121 | end = start + len; 122 | 123 | /* 124 | * scan backwards, replacing '/' with '\0' while the component doesn't 125 | * exist 126 | */ 127 | for (;;) { 128 | int r = is_dir(start); 129 | if (r > 0) { 130 | end += strlen(end); 131 | 132 | if (end == start + len) 133 | return 0; 134 | 135 | /* end != start, since it would be caught on the first 136 | * iteration */ 137 | *end = '/'; 138 | break; 139 | } else if (r == 0) 140 | return -ENOTDIR; 141 | 142 | if (end == start) 143 | break; 144 | 145 | *end = '\0'; 146 | 147 | /* Find the next component, backwards, discarding extra '/'*/ 148 | while (end > start && *end != '/') 149 | end--; 150 | 151 | while (end > start && *(end - 1) == '/') 152 | end--; 153 | } 154 | 155 | for (; end < start + len;) { 156 | if (mkdir(start, mode) < 0 && errno != EEXIST) 157 | return -errno; 158 | 159 | end += strlen(end); 160 | *end = '/'; 161 | } 162 | 163 | return 0; 164 | } 165 | 166 | size_t mem_cpy(void *dest, size_t dsize, const void *src, size_t ssize, size_t cnt) 167 | { 168 | size_t ncopy = 0; 169 | 170 | if (dest == NULL || src == NULL) 171 | return 0; 172 | 173 | if (dsize == 0 || ssize == 0 || cnt == 0) 174 | return 0; 175 | 176 | ncopy = MIN(MIN(dsize, ssize), cnt); 177 | memmove(dest, src, ncopy); 178 | 179 | return ncopy; 180 | } 181 | -------------------------------------------------------------------------------- /src/util.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MAVLink Router project 3 | * 4 | * Copyright (C) 2016 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | #pragma once 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #include "macro.h" 27 | 28 | #ifdef __cplusplus 29 | extern "C" { 30 | #endif 31 | 32 | typedef uint64_t usec_t; 33 | typedef uint64_t nsec_t; 34 | #define USEC_INFINITY ((usec_t)-1) 35 | 36 | #define MSEC_PER_SEC 1000ULL 37 | #define USEC_PER_SEC ((usec_t)1000000ULL) 38 | #define USEC_PER_MSEC ((usec_t)1000ULL) 39 | #define NSEC_PER_SEC ((nsec_t)1000000000ULL) 40 | #define NSEC_PER_MSEC ((nsec_t)1000000ULL) 41 | #define NSEC_PER_USEC ((nsec_t)1000ULL) 42 | 43 | #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) 44 | #define streq(a, b) (strcmp((a), (b)) == 0) 45 | #define strcaseeq(a, b) (strcasecmp((a), (b)) == 0) 46 | #define strncaseeq(a, b, len) (strncasecmp((a), (b), (len)) == 0) 47 | #define memcaseeq(a, len_a, b, len_b) ((len_a) == (len_b) && strncaseeq(a, b, len_a)) 48 | 49 | #ifndef MAX 50 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) 51 | #endif 52 | 53 | #ifndef MIN 54 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) 55 | #endif 56 | 57 | int safe_atoull(const char *s, unsigned long long *ret); 58 | int safe_atoul(const char *s, unsigned long *ret); 59 | int safe_atoi(const char *s, int *ret); 60 | usec_t now_usec(void); 61 | usec_t ts_usec(const struct timespec *ts); 62 | 63 | int mkdir_p(const char *path, int len, mode_t mode); 64 | 65 | size_t mem_cpy(void *dest, size_t dsize, const void *src, size_t ssize, size_t cnt); 66 | 67 | #ifdef __cplusplus 68 | } 69 | #endif 70 | -------------------------------------------------------------------------------- /src/v4l2_interface.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Dronecode Camera Manager 3 | * 4 | * Copyright (C) 2017 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #pragma once 19 | #include 20 | #include 21 | 22 | #define V4L2_DEVICE_PATH "/dev/" 23 | #define V4L2_VIDEO_PREFIX "video" 24 | 25 | int v4l2_ioctl(int fd, int request, void *arg); 26 | int v4l2_list_devices(std::vector &devList); 27 | int v4l2_open(std::string deviceID); 28 | // int v4l2_open(const char *devicepath); 29 | int v4l2_close(int fd); 30 | int v4l2_query_cap(int fd, struct v4l2_capability &vcap); 31 | int v4l2_query_control(int fd); 32 | int v4l2_query_framesizes(int fd); 33 | int v4l2_set_input(int fd, int id); 34 | int v4l2_get_input(int fd); 35 | int v4l2_set_capturemode(int fd, uint32_t mode); 36 | int v4l2_set_pixformat(int fd, uint32_t w, uint32_t h, uint32_t pf); 37 | int v4l2_streamon(int fd); 38 | int v4l2_streamoff(int fd); 39 | int v4l2_buf_req(int fd, uint32_t count); 40 | int v4l2_buf_q(int fd, struct v4l2_buffer *pbuf); 41 | int v4l2_buf_q(int fd, uint32_t i, unsigned long bufptr, uint32_t buflen); 42 | int v4l2_buf_dq(int fd, struct v4l2_buffer *pbuf); 43 | int v4l2_buf_dq(int fd); 44 | int v4l2_get_control(int fd, int ctrl_id); 45 | int v4l2_set_control(int fd, int ctrl_id, int value); 46 | -------------------------------------------------------------------------------- /test/test_camera_parameters.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Camera Streaming Daemon project 3 | * 4 | * Copyright (C) 2017 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include "CameraParameters.h" 26 | #include "log.h" 27 | #include "util.h" 28 | 29 | CameraParameters camParam; 30 | void print_param_type(void); 31 | void print_usage(void); 32 | void test_convert_logic(uint32_t value); 33 | void input_param_id(std::string ¶mID); 34 | void input_param_value(CameraParameters::cam_param_union_t &u); 35 | int set(std::string paramID); 36 | void get(std::string paramID, int type); 37 | 38 | void print_param_type() 39 | { 40 | // std::cout << "PARAM_TYPE_UINT8 : 1" << std::endl; 41 | // std::cout << "PARAM_TYPE_INT8 : 2" << std::endl; 42 | // std::cout << "PARAM_TYPE_UINT16 : 3" << std::endl; 43 | // std::cout << "PARAM_TYPE_INT16 : 4" << std::endl; 44 | std::cout << "PARAM_TYPE_UINT32 : 5" << std::endl; 45 | // std::cout << "PARAM_TYPE_INT32 : 6" << std::endl; 46 | // std::cout << "PARAM_TYPE_UINT64 : 7" << std::endl; 47 | // std::cout << "PARAM_TYPE_UINT64 : 8" << std::endl; 48 | std::cout << "PARAM_TYPE_REAL32 : 9" << std::endl; 49 | // std::cout << "PARAM_TYPE_REAL64 : 10" << std::endl; 50 | 51 | return; 52 | } 53 | 54 | void print_usage() 55 | { 56 | std::cout << "0: EXIT" << std::endl; 57 | std::cout << "1: Test Set-Get" << std::endl; 58 | std::cout << "2: Set Param" << std::endl; 59 | std::cout << "3: Get Param" << std::endl; 60 | std::cout << "99: Test uint32<->string conversion" << std::endl; 61 | return; 62 | } 63 | 64 | // uint32(data type) -> byte array(to transmit) -> string(to store) -> byte array -> uint32 65 | void test_convert_logic(uint32_t value) 66 | { 67 | log_debug("Set DataType value: %d", value); 68 | 69 | // 1. Convert type uint32 to byte array 70 | CameraParameters::cam_param_union_t u; 71 | u.param_uint32 = value; 72 | log_debug("Set ByteArray value:"); 73 | for (int i = 0; i < CAM_PARAM_VALUE_LEN; i++) 74 | printf("0x%.2x ", u.bytes[i]); 75 | printf("\n"); 76 | 77 | // 2. Convert byte array to string 78 | std::string str(reinterpret_cast(u.bytes), CAM_PARAM_VALUE_LEN); 79 | log_debug("Stored as String Object"); 80 | 81 | // 3. Convert string to byte array 82 | // const char *arr2 = str.c_str(); 83 | uint8_t arr[CAM_PARAM_VALUE_LEN]; 84 | mem_cpy(arr, sizeof(arr), str.data(), str.size(), sizeof(arr)); 85 | log_debug("Get ByteArray value:"); 86 | for (int i = 0; i < CAM_PARAM_VALUE_LEN; i++) 87 | printf("0x%.2x ", arr[i]); 88 | printf("\n"); 89 | 90 | // 4. Convert byte array to type uint32 91 | CameraParameters::cam_param_union_t u2; 92 | mem_cpy(u2.bytes, sizeof(u2.bytes), arr, sizeof(arr), sizeof(u2.bytes)); 93 | log_debug("Get DataType value: %d", u2.param_uint32); 94 | if (u2.param_uint32 != u.param_uint32) 95 | log_error("Error in logic:%d", value); 96 | } 97 | 98 | void input_param_id(std::string ¶mID) 99 | { 100 | std::cout << "Enter Parameter ID String" << std::endl; 101 | std::cin >> paramID; 102 | } 103 | 104 | void input_param_value(CameraParameters::cam_param_union_t &u) 105 | { 106 | std::cout << "Enter Parameter Type" << std::endl; 107 | print_param_type(); 108 | scanf("%hhu", &u.type); 109 | std::cout << "Enter Parameter Value "; 110 | switch (u.type) { 111 | case CameraParameters::PARAM_TYPE_REAL32: 112 | std::cout << "Float :" << std::endl; 113 | std::cin >> u.param_float; 114 | break; 115 | case CameraParameters::PARAM_TYPE_UINT32: 116 | std::cout << "Uint32 :" << std::endl; 117 | std::cin >> u.param_uint32; 118 | break; 119 | default: 120 | std::cout << "Unsup Type:" << u.type << std::endl; 121 | break; 122 | } 123 | 124 | return; 125 | } 126 | 127 | int set(std::string paramID) 128 | { 129 | CameraParameters::cam_param_union_t u; 130 | input_param_value(u); 131 | switch (u.type) { 132 | case CameraParameters::PARAM_TYPE_REAL32: 133 | log_debug("SET Param: %s Value: %f", paramID.c_str(), u.param_float); 134 | camParam.setParameter(paramID, u.param_float); 135 | break; 136 | case CameraParameters::PARAM_TYPE_UINT32: 137 | log_debug("SET Param: %s Value: %d", paramID.c_str(), u.param_uint32); 138 | camParam.setParameter(paramID, u.param_uint32); 139 | break; 140 | default: 141 | std::cout << "Unsupported Type" << std::endl; 142 | break; 143 | } 144 | 145 | return u.type; 146 | } 147 | 148 | void get(std::string paramID, int type) 149 | { 150 | std::string paramValue; 151 | CameraParameters::cam_param_union_t u; 152 | paramValue = camParam.getParameter(paramID); 153 | if (paramValue.empty()) { 154 | log_error("No Parameter found"); 155 | return; 156 | } 157 | mem_cpy(u.bytes, sizeof(u.bytes), paramValue.data(), paramValue.size(), sizeof(u.bytes)); 158 | switch (type) { 159 | case CameraParameters::PARAM_TYPE_REAL32: 160 | log_debug("GET Param: %s Value: %f", paramID.c_str(), u.param_float); 161 | break; 162 | case CameraParameters::PARAM_TYPE_UINT32: 163 | log_debug("GET Param: %s Value: %d", paramID.c_str(), u.param_uint32); 164 | break; 165 | default: 166 | break; 167 | } 168 | } 169 | 170 | int main(int argc, char *argv[]) 171 | { 172 | Log::open(); 173 | Log::set_max_level(Log::Level::DEBUG); 174 | log_debug("Camera Parameters Client"); 175 | 176 | // print usage 177 | print_usage(); 178 | // provide options to user 179 | int input = 0; 180 | while (1) { 181 | std::cout << "Please enter selection or enter 0 to exit :" << std::endl; 182 | std::cin >> input; 183 | switch (input) { 184 | case 0: 185 | std::cout << "Exiting the program..." << std::endl; 186 | exit(0); 187 | break; 188 | case 1: { 189 | std::cout << "Test Set/Get Operation" << std::endl; 190 | std::string paramID; 191 | std::string paramValue; 192 | int type; 193 | input_param_id(paramID); 194 | type = set(paramID); 195 | get(paramID, type); 196 | break; 197 | } 198 | case 2: { 199 | std::cout << "Set Parameter" << std::endl; 200 | std::string paramID; 201 | input_param_id(paramID); 202 | set(paramID); 203 | break; 204 | } 205 | case 3: { 206 | std::cout << "Get Parameter" << std::endl; 207 | std::string paramID; 208 | input_param_id(paramID); 209 | std::string value = camParam.getParameter(paramID); 210 | if (value.empty()) 211 | std::cout << "Parameter undefined" << std::endl; 212 | std::cout << paramID << " Value:" << value << std::endl; 213 | break; 214 | } 215 | case 99: { 216 | uint32_t value; 217 | std::cout << "Test DataType<->String Conversion Logic" << std::endl; 218 | std::cout << "Please enter number to use for conversion:" << std::endl; 219 | std::cin >> value; 220 | test_convert_logic(value); 221 | } 222 | default: 223 | break; 224 | } 225 | } 226 | 227 | // for(uint32_t i =0; i< 4294967295; i++) 228 | // test_convert_logic(i); 229 | // log_debug("Test Done\n"); 230 | 231 | Log::close(); 232 | return 0; 233 | } 234 | -------------------------------------------------------------------------------- /test/test_rtsp_udp_stream_discovery.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Camera Streaming Daemon project 3 | * 4 | * Copyright (C) 2017 Intel Corporation. All rights reserved. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #include "glib_mainloop.h" 25 | #include "log.h" 26 | #include "mainloop.h" 27 | 28 | static void resolve_callback(AvahiServiceResolver *resolver, AvahiIfIndex interface, 29 | AvahiProtocol protocol, AvahiResolverEvent event, const char *name, 30 | const char *type, const char *domain, const char *host_name, 31 | const AvahiAddress *address, uint16_t port, AvahiStringList *txt, 32 | AvahiLookupResultFlags flags, void *userdata) 33 | { 34 | assert(resolver); 35 | assert(userdata); 36 | 37 | switch (event) { 38 | case AVAHI_RESOLVER_FAILURE: 39 | log_error("Avahi service resolve failed for service '%s' in domain '%s'" 40 | ". error: %s", 41 | name, domain, avahi_strerror(avahi_client_errno((AvahiClient *)userdata))); 42 | break; 43 | 44 | case AVAHI_RESOLVER_FOUND: { 45 | char address_str[AVAHI_ADDRESS_STR_MAX], *txt_str; 46 | avahi_address_snprint(address_str, sizeof(address_str), address); 47 | 48 | log_info("Service resolved: '%s' (rtsp://%s:%u%s)", name, address_str, port, name); 49 | 50 | txt_str = avahi_string_list_to_string(txt); 51 | log_info("TXT: [%s]", txt_str); 52 | 53 | avahi_free(txt_str); 54 | } 55 | } 56 | 57 | avahi_service_resolver_free(resolver); 58 | } 59 | static void browse_callback(AvahiServiceBrowser *sb, AvahiIfIndex interface, AvahiProtocol protocol, 60 | AvahiBrowserEvent event, const char *name, const char *type, 61 | const char *domain, AvahiLookupResultFlags flags, void *userdata) 62 | { 63 | AvahiClient *client = (AvahiClient *)userdata; 64 | assert(sb); 65 | 66 | switch (event) { 67 | case AVAHI_BROWSER_FAILURE: 68 | log_error("Avahi Browser error"); 69 | Mainloop::get_mainloop()->quit(); 70 | break; 71 | 72 | case AVAHI_BROWSER_NEW: 73 | if (!(avahi_service_resolver_new(client, interface, protocol, name, type, domain, 74 | AVAHI_PROTO_INET, (AvahiLookupFlags)0, resolve_callback, 75 | client))) 76 | log_error("Failed to start resolver for service. name: '%s' in domain: %s. " 77 | "error: %s", 78 | name, domain, avahi_strerror(avahi_client_errno(client))); 79 | break; 80 | 81 | case AVAHI_BROWSER_REMOVE: 82 | log_info("Service removed: '%s'", name); 83 | break; 84 | 85 | case AVAHI_BROWSER_ALL_FOR_NOW: 86 | case AVAHI_BROWSER_CACHE_EXHAUSTED: 87 | break; 88 | } 89 | } 90 | 91 | static void client_callback(AvahiClient *client, AvahiClientState state, void *userdata) 92 | { 93 | assert(client); 94 | 95 | if (state == AVAHI_CLIENT_FAILURE) { 96 | log_error("Avahi client error"); 97 | Mainloop::get_mainloop()->quit(); 98 | } 99 | } 100 | 101 | int main(int argc, char *argv[]) 102 | { 103 | Log::open(); 104 | 105 | AvahiClient *client = NULL; 106 | AvahiServiceBrowser *sb = NULL; 107 | int error; 108 | GlibMainloop mainloop; 109 | 110 | log_debug("Camera Streaming Client"); 111 | 112 | client = avahi_client_new(mainloop.get_avahi_poll_api(), (AvahiClientFlags)0, client_callback, 113 | NULL, &error); 114 | if (!client) { 115 | log_error("Failed to create avahi client: %s\n", avahi_strerror(error)); 116 | goto error; 117 | } 118 | 119 | if (!(sb = avahi_service_browser_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_INET, "_rtsp._udp", 120 | NULL, (AvahiLookupFlags)0, browse_callback, client))) { 121 | log_error("Failed to create avahi service browser: %s\n", avahi_strerror(error)); 122 | goto error; 123 | } 124 | mainloop.loop(); 125 | 126 | error: 127 | if (sb) 128 | avahi_service_browser_free(sb); 129 | if (client) 130 | avahi_client_free(client); 131 | Log::close(); 132 | 133 | return 0; 134 | } 135 | -------------------------------------------------------------------------------- /tools/checkpatch: -------------------------------------------------------------------------------- 1 | git-clang-format -------------------------------------------------------------------------------- /tools/export_v4l2_param_xml/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | g++ -std=c++11 export_v4l2_param_xml.cpp -ltinyxml -oexport_v4l2_param 3 | clean: 4 | rm -rf export_v4l2_param *.xml 5 | -------------------------------------------------------------------------------- /valgrind.supp: -------------------------------------------------------------------------------- 1 | { 2 | gst_init_memory_leak 3 | Memcheck:Leak 4 | match-leak-kinds: definite 5 | fun:malloc 6 | fun:g_malloc 7 | ... 8 | fun:_dl_init 9 | ... 10 | } 11 | --------------------------------------------------------------------------------