diff --git a/tools/streamer_recorder/CMakeLists.txt b/tools/streamer_recorder/CMakeLists.txt new file mode 100644 index 000000000..0b78a236e --- /dev/null +++ b/tools/streamer_recorder/CMakeLists.txt @@ -0,0 +1,105 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.8.12.1) + +if(WIN32 AND NOT MINGW) + if(NOT DEFINED CMAKE_DEBUG_POSTFIX) + set(CMAKE_DEBUG_POSTFIX "d") + endif() +endif() + +IF(NOT DEFINED CMAKE_BUILD_TYPE) + # No effect for multi-configuration generators (e.g. for Visual Studio) + SET(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Choose: RelWithDebInfo Release Debug MinSizeRel None") +ENDIF() + +PROJECT(libfreenect2_tools_SR) + +SET(MY_DIR ${libfreenect2_tools_SR_SOURCE_DIR}) +SET(DEPENDS_DIR "${MY_DIR}/../../depends" CACHE STRING "Dependency directory") + +OPTION(ENABLE_OPENGL "Enable OpenGL support" ON) + +# The example build system is standalone and will work out-of-tree with these files copied +SET(freenect2_ROOT_DIR ${MY_DIR}/../..) +SET(flextGL_SOURCES ${freenect2_ROOT_DIR}/src/flextGL.cpp) +SET(flextGL_INCLUDE_DIRS ${freenect2_ROOT_DIR}/src) # for flextGL.h +SET(tools_SR_INCLUDE_DIRS ${MY_DIR}/include) # for local include +SET(example_INCLUDE_DIRS ${freenect2_ROOT_DIR}/examples) # for protonect viewer + + +FIND_PACKAGE(PkgConfig) # try find PKGConfig as it will be used if found +LIST(APPEND CMAKE_MODULE_PATH ${freenect2_ROOT_DIR}/cmake_modules) # FindGLFW3.cmake + +IF(TARGET freenect2) + MESSAGE(STATUS "Using in-tree freenect2 target") + SET(freenect2_LIBRARIES freenect2) + SET(freenect2_DLLS ${LIBFREENECT2_DLLS}) +ELSE() + FIND_PACKAGE(freenect2 REQUIRED) + # Out-of-tree build will have to have DLLs manually copied. +ENDIF() + +INCLUDE_DIRECTORIES( + ${freenect2_INCLUDE_DIR} +) + +SET(ProtonectSR_src + ProtonectSR.cpp +) + +SET(ProtonectSR_LIBRARIES + ${freenect2_LIBRARIES} +) + +SET(ProtonectSR_DLLS + ${freenect2_DLLS} +) + +IF(ENABLE_OPENGL) + FIND_PACKAGE(GLFW3) + FIND_PACKAGE(OpenGL) + IF(GLFW3_FOUND AND OPENGL_FOUND) + INCLUDE_DIRECTORIES( + ${GLFW3_INCLUDE_DIRS} + ${flextGL_INCLUDE_DIRS} + ${tools_SR_INCLUDE_DIRS} + ${example_INCLUDE_DIRS} + ) + + LIST(APPEND ProtonectSR_DLLS ${GLFW3_DLL}) + LIST(APPEND ProtonectSR_src + ${example_INCLUDE_DIRS}/viewer.cpp + ${tools_SR_INCLUDE_DIRS}/PracticalSocket.cpp + ${tools_SR_INCLUDE_DIRS}/streamer.cpp + ${tools_SR_INCLUDE_DIRS}/recorder.cpp + ${flextGL_SOURCES} + ) + LIST(APPEND ProtonectSR_LIBRARIES + ${GLFW3_LIBRARIES} + ${OPENGL_gl_LIBRARY} + ) + ADD_DEFINITIONS(-DEXAMPLES_WITH_OPENGL_SUPPORT=1) + ENDIF() +ENDIF(ENABLE_OPENGL) + +ADD_EXECUTABLE(ProtonectSR + ${ProtonectSR_src} +) + +TARGET_LINK_LIBRARIES(ProtonectSR + ${ProtonectSR_LIBRARIES} +) + +IF(WIN32) + INSTALL(TARGETS ProtonectSR DESTINATION bin) + LIST(REMOVE_DUPLICATES ProtonectSR_DLLS) + FOREACH(FILEI ${ProtonectSR_DLLS}) + ADD_CUSTOM_COMMAND(TARGET ProtonectSR POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${FILEI} $ + ) + ENDFOREACH(FILEI) + INSTALL(FILES ${ProtonectSR_DLLS} DESTINATION bin) +ENDIF() + +# Add OpenCV package dependency for udp-image-streaming +find_package( OpenCV REQUIRED ) +target_link_libraries( ProtonectSR ${OpenCV_LIBS} ) diff --git a/tools/streamer_recorder/PracticalSocket.cpp b/tools/streamer_recorder/PracticalSocket.cpp new file mode 100644 index 000000000..43ebc59f5 --- /dev/null +++ b/tools/streamer_recorder/PracticalSocket.cpp @@ -0,0 +1,381 @@ +/* + * C++ sockets on Unix and Windows + * Copyright (C) 2002 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "PracticalSocket.h" + +#ifdef WIN32 + #include // For socket(), connect(), send(), and recv() + typedef int socklen_t; + typedef char raw_type; // Type used for raw data on this platform +#else + #include // For data types + #include // For socket(), connect(), send(), and recv() + #include // For gethostbyname() + #include // For inet_addr() + #include // For close() + #include // For sockaddr_in + #include // For strerror(), memset() + typedef void raw_type; // Type used for raw data on this platform +#endif + +#include // For errno + +using namespace std; + +#ifdef WIN32 +static bool initialized = false; +#endif + +// SocketException Code + +SocketException::SocketException(const string &message, bool inclSysMsg) + throw() : userMessage(message) { + if (inclSysMsg) { + userMessage.append(": "); + userMessage.append(strerror(errno)); + } +} + +SocketException::~SocketException() throw() { +} + +const char *SocketException::what() const throw() { + return userMessage.c_str(); +} + +// Function to fill in address structure given an address and port +static void fillAddr(const string &address, unsigned short port, + sockaddr_in &addr) { + memset(&addr, 0, sizeof(addr)); // Zero out address structure + addr.sin_family = AF_INET; // Internet address + + hostent *host; // Resolve name + if ((host = gethostbyname(address.c_str())) == NULL) { + // strerror() will not work for gethostbyname() and hstrerror() + // is supposedly obsolete + throw SocketException("Failed to resolve name (gethostbyname())"); + } + addr.sin_addr.s_addr = *((unsigned long *) host->h_addr_list[0]); + + addr.sin_port = htons(port); // Assign port in network byte order +} + +// Socket Code + +Socket::Socket(int type, int protocol) throw(SocketException) { + #ifdef WIN32 + if (!initialized) { + WORD wVersionRequested; + WSADATA wsaData; + + wVersionRequested = MAKEWORD(2, 0); // Request WinSock v2.0 + if (WSAStartup(wVersionRequested, &wsaData) != 0) { // Load WinSock DLL + throw SocketException("Unable to load WinSock DLL"); + } + initialized = true; + } + #endif + + // Make a new socket + if ((sockDesc = socket(PF_INET, type, protocol)) < 0) { + throw SocketException("Socket creation failed (socket())", true); + } +} + +Socket::Socket(int sockDesc) { + this->sockDesc = sockDesc; +} + +Socket::~Socket() { + #ifdef WIN32 + ::closesocket(sockDesc); + #else + ::close(sockDesc); + #endif + sockDesc = -1; +} + +string Socket::getLocalAddress() throw(SocketException) { + sockaddr_in addr; + unsigned int addr_len = sizeof(addr); + + if (getsockname(sockDesc, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0) { + throw SocketException("Fetch of local address failed (getsockname())", true); + } + return inet_ntoa(addr.sin_addr); +} + +unsigned short Socket::getLocalPort() throw(SocketException) { + sockaddr_in addr; + unsigned int addr_len = sizeof(addr); + + if (getsockname(sockDesc, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0) { + throw SocketException("Fetch of local port failed (getsockname())", true); + } + return ntohs(addr.sin_port); +} + +void Socket::setLocalPort(unsigned short localPort) throw(SocketException) { + // Bind the socket to its port + sockaddr_in localAddr; + memset(&localAddr, 0, sizeof(localAddr)); + localAddr.sin_family = AF_INET; + localAddr.sin_addr.s_addr = htonl(INADDR_ANY); + localAddr.sin_port = htons(localPort); + + if (bind(sockDesc, (sockaddr *) &localAddr, sizeof(sockaddr_in)) < 0) { + throw SocketException("Set of local port failed (bind())", true); + } +} + +void Socket::setLocalAddressAndPort(const string &localAddress, + unsigned short localPort) throw(SocketException) { + // Get the address of the requested host + sockaddr_in localAddr; + fillAddr(localAddress, localPort, localAddr); + + if (bind(sockDesc, (sockaddr *) &localAddr, sizeof(sockaddr_in)) < 0) { + throw SocketException("Set of local address and port failed (bind())", true); + } +} + +void Socket::cleanUp() throw(SocketException) { + #ifdef WIN32 + if (WSACleanup() != 0) { + throw SocketException("WSACleanup() failed"); + } + #endif +} + +unsigned short Socket::resolveService(const string &service, + const string &protocol) { + struct servent *serv; /* Structure containing service information */ + + if ((serv = getservbyname(service.c_str(), protocol.c_str())) == NULL) + return atoi(service.c_str()); /* Service is port number */ + else + return ntohs(serv->s_port); /* Found port (network byte order) by name */ +} + +// CommunicatingSocket Code + +CommunicatingSocket::CommunicatingSocket(int type, int protocol) + throw(SocketException) : Socket(type, protocol) { +} + +CommunicatingSocket::CommunicatingSocket(int newConnSD) : Socket(newConnSD) { +} + +void CommunicatingSocket::connect(const string &foreignAddress, + unsigned short foreignPort) throw(SocketException) { + // Get the address of the requested host + sockaddr_in destAddr; + fillAddr(foreignAddress, foreignPort, destAddr); + + // Try to connect to the given port + if (::connect(sockDesc, (sockaddr *) &destAddr, sizeof(destAddr)) < 0) { + throw SocketException("Connect failed (connect())", true); + } +} + +void CommunicatingSocket::send(const void *buffer, int bufferLen) + throw(SocketException) { + if (::send(sockDesc, (raw_type *) buffer, bufferLen, 0) < 0) { + throw SocketException("Send failed (send())", true); + } +} + +int CommunicatingSocket::recv(void *buffer, int bufferLen) + throw(SocketException) { + int rtn; + if ((rtn = ::recv(sockDesc, (raw_type *) buffer, bufferLen, 0)) < 0) { + throw SocketException("Received failed (recv())", true); + } + + return rtn; +} + +string CommunicatingSocket::getForeignAddress() + throw(SocketException) { + sockaddr_in addr; + unsigned int addr_len = sizeof(addr); + + if (getpeername(sockDesc, (sockaddr *) &addr,(socklen_t *) &addr_len) < 0) { + throw SocketException("Fetch of foreign address failed (getpeername())", true); + } + return inet_ntoa(addr.sin_addr); +} + +unsigned short CommunicatingSocket::getForeignPort() throw(SocketException) { + sockaddr_in addr; + unsigned int addr_len = sizeof(addr); + + if (getpeername(sockDesc, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0) { + throw SocketException("Fetch of foreign port failed (getpeername())", true); + } + return ntohs(addr.sin_port); +} + +// TCPSocket Code + +TCPSocket::TCPSocket() + throw(SocketException) : CommunicatingSocket(SOCK_STREAM, + IPPROTO_TCP) { +} + +TCPSocket::TCPSocket(const string &foreignAddress, unsigned short foreignPort) + throw(SocketException) : CommunicatingSocket(SOCK_STREAM, IPPROTO_TCP) { + connect(foreignAddress, foreignPort); +} + +TCPSocket::TCPSocket(int newConnSD) : CommunicatingSocket(newConnSD) { +} + +// TCPServerSocket Code + +TCPServerSocket::TCPServerSocket(unsigned short localPort, int queueLen) + throw(SocketException) : Socket(SOCK_STREAM, IPPROTO_TCP) { + setLocalPort(localPort); + setListen(queueLen); +} + +TCPServerSocket::TCPServerSocket(const string &localAddress, + unsigned short localPort, int queueLen) + throw(SocketException) : Socket(SOCK_STREAM, IPPROTO_TCP) { + setLocalAddressAndPort(localAddress, localPort); + setListen(queueLen); +} + +TCPSocket *TCPServerSocket::accept() throw(SocketException) { + int newConnSD; + if ((newConnSD = ::accept(sockDesc, NULL, 0)) < 0) { + throw SocketException("Accept failed (accept())", true); + } + + return new TCPSocket(newConnSD); +} + +void TCPServerSocket::setListen(int queueLen) throw(SocketException) { + if (listen(sockDesc, queueLen) < 0) { + throw SocketException("Set listening socket failed (listen())", true); + } +} + +// UDPSocket Code + +UDPSocket::UDPSocket() throw(SocketException) : CommunicatingSocket(SOCK_DGRAM, + IPPROTO_UDP) { + setBroadcast(); +} + +UDPSocket::UDPSocket(unsigned short localPort) throw(SocketException) : + CommunicatingSocket(SOCK_DGRAM, IPPROTO_UDP) { + setLocalPort(localPort); + setBroadcast(); +} + +UDPSocket::UDPSocket(const string &localAddress, unsigned short localPort) + throw(SocketException) : CommunicatingSocket(SOCK_DGRAM, IPPROTO_UDP) { + setLocalAddressAndPort(localAddress, localPort); + setBroadcast(); +} + +void UDPSocket::setBroadcast() { + // If this fails, we'll hear about it when we try to send. This will allow + // system that cannot broadcast to continue if they don't plan to broadcast + int broadcastPermission = 1; + setsockopt(sockDesc, SOL_SOCKET, SO_BROADCAST, + (raw_type *) &broadcastPermission, sizeof(broadcastPermission)); +} + +void UDPSocket::disconnect() throw(SocketException) { + sockaddr_in nullAddr; + memset(&nullAddr, 0, sizeof(nullAddr)); + nullAddr.sin_family = AF_UNSPEC; + + // Try to disconnect + if (::connect(sockDesc, (sockaddr *) &nullAddr, sizeof(nullAddr)) < 0) { + #ifdef WIN32 + if (errno != WSAEAFNOSUPPORT) { + #else + if (errno != EAFNOSUPPORT) { + #endif + throw SocketException("Disconnect failed (connect())", true); + } + } +} + +void UDPSocket::sendTo(const void *buffer, int bufferLen, + const string &foreignAddress, unsigned short foreignPort) + throw(SocketException) { + sockaddr_in destAddr; + fillAddr(foreignAddress, foreignPort, destAddr); + + // Write out the whole buffer as a single message. + if (sendto(sockDesc, (raw_type *) buffer, bufferLen, 0, + (sockaddr *) &destAddr, sizeof(destAddr)) != bufferLen) { + throw SocketException("Send failed (sendto())", true); + } +} + +int UDPSocket::recvFrom(void *buffer, int bufferLen, string &sourceAddress, + unsigned short &sourcePort) throw(SocketException) { + sockaddr_in clntAddr; + socklen_t addrLen = sizeof(clntAddr); + int rtn; + if ((rtn = recvfrom(sockDesc, (raw_type *) buffer, bufferLen, 0, + (sockaddr *) &clntAddr, (socklen_t *) &addrLen)) < 0) { + throw SocketException("Receive failed (recvfrom())", true); + } + sourceAddress = inet_ntoa(clntAddr.sin_addr); + sourcePort = ntohs(clntAddr.sin_port); + + return rtn; +} + +void UDPSocket::setMulticastTTL(unsigned char multicastTTL) throw(SocketException) { + if (setsockopt(sockDesc, IPPROTO_IP, IP_MULTICAST_TTL, + (raw_type *) &multicastTTL, sizeof(multicastTTL)) < 0) { + throw SocketException("Multicast TTL set failed (setsockopt())", true); + } +} + +void UDPSocket::joinGroup(const string &multicastGroup) throw(SocketException) { + struct ip_mreq multicastRequest; + + multicastRequest.imr_multiaddr.s_addr = inet_addr(multicastGroup.c_str()); + multicastRequest.imr_interface.s_addr = htonl(INADDR_ANY); + if (setsockopt(sockDesc, IPPROTO_IP, IP_ADD_MEMBERSHIP, + (raw_type *) &multicastRequest, + sizeof(multicastRequest)) < 0) { + throw SocketException("Multicast group join failed (setsockopt())", true); + } +} + +void UDPSocket::leaveGroup(const string &multicastGroup) throw(SocketException) { + struct ip_mreq multicastRequest; + + multicastRequest.imr_multiaddr.s_addr = inet_addr(multicastGroup.c_str()); + multicastRequest.imr_interface.s_addr = htonl(INADDR_ANY); + if (setsockopt(sockDesc, IPPROTO_IP, IP_DROP_MEMBERSHIP, + (raw_type *) &multicastRequest, + sizeof(multicastRequest)) < 0) { + throw SocketException("Multicast group leave failed (setsockopt())", true); + } +} diff --git a/tools/streamer_recorder/ProtonectSR.cpp b/tools/streamer_recorder/ProtonectSR.cpp new file mode 100644 index 000000000..1ef0db87e --- /dev/null +++ b/tools/streamer_recorder/ProtonectSR.cpp @@ -0,0 +1,438 @@ +/* + * This file is part of the OpenKinect Project. http://www.openkinect.org + * + * Copyright (c) 2011 individual OpenKinect contributors. See the CONTRIB file + * for details. + * + * This code is licensed to you under the terms of the Apache License, version + * 2.0, or, at your option, the terms of the GNU General Public License, + * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses, + * or the following URLs: + * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.gnu.org/licenses/gpl-2.0.txt + * + * If you redistribute this file in source form, modified or unmodified, you + * may: + * 1) Leave this header intact and distribute it under the same terms, + * accompanying it with the APACHE20 and GPL20 files, or + * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or + * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file + * In all cases you must keep the copyright notice intact and include a copy + * of the CONTRIB file. + * + * Binary distributions must follow the binary distribution requirements of + * either License. + */ + +/** @file Protonect.cpp Main application file. */ + +#include +#include +#include + +/// [headers] +#include +#include +#include +#include +#include +#include "include/streamer.h" +#include "include/recorder.h" +/// [headers] +#ifdef EXAMPLES_WITH_OPENGL_SUPPORT +#include "viewer.h" +#endif + + +bool protonect_shutdown = false; ///< Whether the running application should shut down. + +void sigint_handler(int s) +{ + protonect_shutdown = true; +} + +bool protonect_paused = false; +libfreenect2::Freenect2Device *devtopause; + +//Doing non-trivial things in signal handler is bad. If you want to pause, +//do it in another thread. +//Though libusb operations are generally thread safe, I cannot guarantee +//everything above is thread safe when calling start()/stop() while +//waitForNewFrame(). +void sigusr1_handler(int s) +{ + if (devtopause == 0) + return; +/// [pause] + if (protonect_paused) + devtopause->start(); + else + devtopause->stop(); + protonect_paused = !protonect_paused; +/// [pause] +} + +//The following demostrates how to create a custom logger +/// [logger] +#include +#include +class MyFileLogger: public libfreenect2::Logger +{ +private: + std::ofstream logfile_; +public: + MyFileLogger(const char *filename) + { + if (filename) + logfile_.open(filename); + level_ = Debug; + } + bool good() + { + return logfile_.is_open() && logfile_.good(); + } + virtual void log(Level level, const std::string &message) + { + logfile_ << "[" << libfreenect2::Logger::level2str(level) << "] " << message << std::endl; + } +}; +/// [logger] + +/// [main] +/** + * Main application entry point. + * + * Accepted argumemnts: + * - cpu Perform depth processing with the CPU. + * - gl Perform depth processing with OpenGL. + * - cl Perform depth processing with OpenCL. + * - Serial number of the device to open. + * - -noviewer Disable viewer window. + * - -streamer Enable UDP Streaming of captured images. + * - -recorder Enable recording of captured images. + */ +int main(int argc, char *argv[]) +/// [main] +{ + std::string program_path(argv[0]); + std::cerr << "Version: " << LIBFREENECT2_VERSION << std::endl; + std::cerr << "Environment variables: LOGFILE=" << std::endl; + std::cerr << "Usage: " << program_path << " [-gpu=] [gl | cl | cuda | cpu] []" << std::endl; + std::cerr << " [-noviewer] [-norgb | -nodepth] [-help] [-version]" << std::endl; + std::cerr << " [-frames ]" << std::endl; + std::cerr << "To pause and unpause: pkill -USR1 Protonect" << std::endl; + size_t executable_name_idx = program_path.rfind("Protonect"); + + std::string binpath = "/"; + + if(executable_name_idx != std::string::npos) + { + binpath = program_path.substr(0, executable_name_idx); + } + +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) + // avoid flooing the very slow Windows console with debug messages + libfreenect2::setGlobalLogger(libfreenect2::createConsoleLogger(libfreenect2::Logger::Info)); +#else + // create a console logger with debug level (default is console logger with info level) +/// [logging] + libfreenect2::setGlobalLogger(libfreenect2::createConsoleLogger(libfreenect2::Logger::Debug)); +/// [logging] +#endif +/// [file logging] + MyFileLogger *filelogger = new MyFileLogger(getenv("LOGFILE")); + if (filelogger->good()) + libfreenect2::setGlobalLogger(filelogger); + else + delete filelogger; +/// [file logging] + +/// [context] + libfreenect2::Freenect2 freenect2; + libfreenect2::Freenect2Device *dev = 0; + libfreenect2::PacketPipeline *pipeline = 0; +/// [context] + + std::string serial = ""; + + bool viewer_enabled = true; + bool streamer_enabled = false; + bool recorder_enabled = false; + bool enable_rgb = true; + bool enable_depth = true; + int deviceId = -1; + size_t framemax = -1; + + for(int argI = 1; argI < argc; ++argI) + { + const std::string arg(argv[argI]); + + if(arg == "-help" || arg == "--help" || arg == "-h" || arg == "-v" || arg == "--version" || arg == "-version") + { + // Just let the initial lines display at the beginning of main + return 0; + } + else if(arg.find("-gpu=") == 0) + { + if (pipeline) + { + std::cerr << "-gpu must be specified before pipeline argument" << std::endl; + return -1; + } + deviceId = atoi(argv[argI] + 5); + } + else if(arg == "cpu") + { + if(!pipeline) +/// [pipeline] + pipeline = new libfreenect2::CpuPacketPipeline(); +/// [pipeline] + } + else if(arg == "gl") + { +#ifdef LIBFREENECT2_WITH_OPENGL_SUPPORT + if(!pipeline) + pipeline = new libfreenect2::OpenGLPacketPipeline(); +#else + std::cout << "OpenGL pipeline is not supported!" << std::endl; +#endif + } + else if(arg == "cl") + { +#ifdef LIBFREENECT2_WITH_OPENCL_SUPPORT + if(!pipeline) + pipeline = new libfreenect2::OpenCLPacketPipeline(deviceId); +#else + std::cout << "OpenCL pipeline is not supported!" << std::endl; +#endif + } + else if(arg == "cuda") + { +#ifdef LIBFREENECT2_WITH_CUDA_SUPPORT + if(!pipeline) + pipeline = new libfreenect2::CudaPacketPipeline(deviceId); +#else + std::cout << "CUDA pipeline is not supported!" << std::endl; +#endif + } + else if(arg.find_first_not_of("0123456789") == std::string::npos) //check if parameter could be a serial number + { + serial = arg; + } + else if(arg == "-noviewer" || arg == "--noviewer") + { + viewer_enabled = false; + } + else if(arg == "-norgb" || arg == "--norgb") + { + enable_rgb = false; + } + else if(arg == "-nodepth" || arg == "--nodepth") + { + enable_depth = false; + } + else if(arg == "-frames") + { + ++argI; + framemax = strtol(argv[argI], NULL, 0); + if (framemax == 0) { + std::cerr << "invalid frame count '" << argv[argI] << "'" << std::endl; + return -1; + } + } + else if(arg == "-streamer" || arg == "--streamer") + { + streamer_enabled = true; + } + else if(arg == "-recorder" || arg == "--recorder") + { + recorder_enabled = true; + } + else + { + std::cout << "Unknown argument: " << arg << std::endl; + } + } + + if (!enable_rgb && !enable_depth) + { + std::cerr << "Disabling both streams is not allowed!" << std::endl; + return -1; + } + +/// [discovery] + if(freenect2.enumerateDevices() == 0) + { + std::cout << "no device connected!" << std::endl; + return -1; + } + + if (serial == "") + { + serial = freenect2.getDefaultDeviceSerialNumber(); + } +/// [discovery] + + if(pipeline) + { +/// [open] + dev = freenect2.openDevice(serial, pipeline); +/// [open] + } + else + { + dev = freenect2.openDevice(serial); + } + + if(dev == 0) + { + std::cout << "failure opening device!" << std::endl; + return -1; + } + + devtopause = dev; + + signal(SIGINT,sigint_handler); +#ifdef SIGUSR1 + signal(SIGUSR1, sigusr1_handler); +#endif + protonect_shutdown = false; + +/// [listeners] + int types = 0; + if (enable_rgb) + types |= libfreenect2::Frame::Color; + if (enable_depth) + types |= libfreenect2::Frame::Ir | libfreenect2::Frame::Depth; + libfreenect2::SyncMultiFrameListener listener(types); + libfreenect2::FrameMap frames; + + dev->setColorFrameListener(&listener); + dev->setIrAndDepthFrameListener(&listener); +/// [listeners] + +/// [start] + if (enable_rgb && enable_depth) + { + if (!dev->start()) + return -1; + } + else + { + if (!dev->startStreams(enable_rgb, enable_depth)) + return -1; + } + + std::cout << "device serial: " << dev->getSerialNumber() << std::endl; + std::cout << "device firmware: " << dev->getFirmwareVersion() << std::endl; +/// [start] + +/// [registration setup] + libfreenect2::Registration* registration = new libfreenect2::Registration(dev->getIrCameraParams(), dev->getColorCameraParams()); + libfreenect2::Frame undistorted(512, 424, 4), registered(512, 424, 4); +/// [registration setup] + + size_t framecount = 0; +#ifdef EXAMPLES_WITH_OPENGL_SUPPORT + Viewer viewer; + if (viewer_enabled) + viewer.initialize(); +#else + viewer_enabled = false; +#endif + +Streamer streamer; // have to declare it outside statements to be accessible everywhere +Recorder recorder; + +if (streamer_enabled) +{ + streamer.initialize(); +} + +if (recorder_enabled) +{ + recorder.initialize(); +} + +/// [loop start] + while(!protonect_shutdown && (framemax == (size_t)-1 || framecount < framemax)) + { + listener.waitForNewFrame(frames); + libfreenect2::Frame *rgb = frames[libfreenect2::Frame::Color]; + libfreenect2::Frame *ir = frames[libfreenect2::Frame::Ir]; + libfreenect2::Frame *depth = frames[libfreenect2::Frame::Depth]; +/// [loop start] + + if (enable_rgb && enable_depth) + { +/// [registration] + registration->apply(rgb, depth, &undistorted, ®istered); +/// [registration] + } + + framecount++; + + if (streamer_enabled) + { + streamer.stream(depth); + } + + if (recorder_enabled) + { + // TODO: add recording timestamp if max frame number reached + // + avoid recording new ones + recorder.record(depth,"depth"); + recorder.record(®istered,"registered"); + // recorder.record(rgb,"rgb"); + + recorder.registTimeStamp(); + } + + if (!viewer_enabled) + { + if (framecount % 100 == 0) + std::cout << "The viewer is turned off. Received " << framecount << " frames. Ctrl-C to stop." << std::endl; + listener.release(frames); + continue; + } + +#ifdef EXAMPLES_WITH_OPENGL_SUPPORT + if (enable_rgb) + { + viewer.addFrame("RGB", rgb); + } + if (enable_depth) + { + viewer.addFrame("ir", ir); + viewer.addFrame("depth", depth); + } + if (enable_rgb && enable_depth) + { + viewer.addFrame("registered", ®istered); + } + + protonect_shutdown = protonect_shutdown || viewer.render(); +#endif + +/// [loop end] + listener.release(frames); + /** libfreenect2::this_thread::sleep_for(libfreenect2::chrono::milliseconds(100)); */ + } +/// [loop end] + + if (recorder_enabled) + { + recorder.saveTimeStamp(); + } + + // TODO: restarting ir stream doesn't work! + // TODO: bad things will happen, if frame listeners are freed before dev->stop() :( +/// [stop] + dev->stop(); + dev->close(); +/// [stop] + + delete registration; + + return 0; +} diff --git a/tools/streamer_recorder/README.md b/tools/streamer_recorder/README.md new file mode 100644 index 000000000..5f5282be2 --- /dev/null +++ b/tools/streamer_recorder/README.md @@ -0,0 +1,74 @@ +# libfreenect2 Streamer/Recorder toolbox + +## Table of Contents + +* [Description](README.md#description) +* [Maintainers](README.md#maintainers) +* [Installation](README.md#installation) + * [Windows / Visual Studio](README.md#windows--visual-studio) + * [MacOS X](README.md#mac-osx) + * [Linux](README.md#linux) + + +## Description + +Additional toolbox featuring: +- UDP streaming of kinect captured images (``-streamer`` option) +- Recording of kinect captured images to disk (``-recorder`` option) + +## Maintainers + +* David Poirier-Quinot + +## Installation + +### Windows / Visual Studio + +### Mac OSX + +* Install OPENCV + + ``` +brew install opencv3 +``` + +* Install Numpy for Blender viewer + + ``` +pip3 install numpy +``` + +and link numpy and cv2 to blender python3 site-package (rename / remove old numpy if needed) + +(tested with 1.10.4, previous versions happened to raise ``ImportError: numpy.core.multiarray failed to import`` when typing ``import cv2`` in python) + +* Build + + ``` +mkdir build && cd build +cmake .. +make +make install +``` +* Run the test program: `.ProtonectSR` + + +### Linux + +* Install build tools + + ``` +sudo apt-get install opencv3 +``` + +* Build + + ``` + +mkdir build && cd build +cmake .. +make +make install + +* Run the test program: `./ProtonectSR` + diff --git a/tools/streamer_recorder/blender_viewer/blender_viewer.blend b/tools/streamer_recorder/blender_viewer/blender_viewer.blend new file mode 100644 index 000000000..5b0c2c0d3 Binary files /dev/null and b/tools/streamer_recorder/blender_viewer/blender_viewer.blend differ diff --git a/tools/streamer_recorder/blender_viewer/scripts/main.py b/tools/streamer_recorder/blender_viewer/scripts/main.py new file mode 100644 index 000000000..ce769e74b --- /dev/null +++ b/tools/streamer_recorder/blender_viewer/scripts/main.py @@ -0,0 +1,118 @@ +from bge import ( + logic, + texture, + ) + +import socket +import bgl + +# # add cv2 lib (requires numpy higher version than current in blender) +import sys +sys.path.append('/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages') +import numpy +import cv2 +# from PIL import Image +import itertools + +def init(controller): + """ + Init, run until socket is connected + """ + + # run once per frame + if controller.sensors[0].positive and controller.sensors[1].positive: + + ### SOCKET DEFINITION + # run once + # if not hasattr(logic, 'socket'): + + # define socket, set socket parameters + logic.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + + logic.socket.setblocking(0) + timeOut = 0.001 + logic.socket.settimeout(timeOut) + + # bind socket (run until binded) + host = '127.0.0.1' + port_rcv = 10000 + logic.socket.bind((host,port_rcv)) + print('bind socket: IP = {} Port = {}'.format(host, port_rcv)) + + controller.owner['SocketConnected'] = True + + ### IMAGE TEXTURE + # get screen object + obj = logic.getCurrentScene().objects['VideoScreen'] + # get the reference pointer (ID) of the texture + ID = texture.materialID(obj, 'MAVideoMat') + # create a texture object + logic.texture = texture.Texture(obj, ID) + + # logic.imageIndex = 0 + +def run(controller): + """ + Run, run once every frames + """ + if controller.sensors[0].positive: + try: + buff_size = 4096 + msg_raw = logic.socket.recv(buff_size) + + # check for header msg indicating the number of packet that'll follow + # containing a unique frame (image) + if len(msg_raw) == 4: + nb_of_packet_per_frame = msg_raw[0] + + # loop over next packets to reconstitute image + frame_raw = b'' + for i in range(nb_of_packet_per_frame): + frame_raw = frame_raw + logic.socket.recv(buff_size) + + # frame = cv2.imdecode(numpy.fromstring(frame_raw, dtype=numpy.uint8), cv2.IMREAD_COLOR) + frame = cv2.imdecode(numpy.fromstring(frame_raw, dtype=numpy.uint8), cv2.IMREAD_GRAYSCALE) + + + if not frame is None: + + width = frame.shape[1] + height = frame.shape[0] + + l = frame.tolist() + lll = list(itertools.chain(*l)) + + # image_buffer = bgl.Buffer(bgl.GL_INT, [width*height*3], lll) + image_buffer = bgl.Buffer(bgl.GL_BYTE, width*height, lll) + + source = texture.ImageBuff() + + # Apply a filter, that way source.load does not except a 3(RGB) pixel image + source.filter = texture.FilterBlueScreen() + source.load(image_buffer, width, height) + + logic.texture.source = source + logic.texture.refresh(False) + + + except socket.timeout: + pass + + +def end(controller): + """ + called when ending BGE (e.g. to properly close network connections) + """ + if controller.sensors[0].positive: + + # close socket + logic.socket.close() + controller.owner['SocketConnected'] = False + controller.owner['End'] = True + print('close socket') + + # close cv2 window + cv2.destroyAllWindows() + + # end game engine + logic.endGame() diff --git a/tools/streamer_recorder/include/PracticalSocket.h b/tools/streamer_recorder/include/PracticalSocket.h new file mode 100644 index 000000000..ac2121fe8 --- /dev/null +++ b/tools/streamer_recorder/include/PracticalSocket.h @@ -0,0 +1,340 @@ +/* + * C++ sockets on Unix and Windows + * Copyright (C) 2002 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef __PRACTICALSOCKET_INCLUDED__ +#define __PRACTICALSOCKET_INCLUDED__ + +#include // For exception class +#include // For string +#include // For invocation to atoi. + +using namespace std; + +/** + * Signals a problem with the execution of a socket call. + */ +class SocketException : public exception { +public: + /** + * Construct a SocketException with a explanatory message. + * @param message explanatory message + * @param incSysMsg true if system message (from strerror(errno)) + * should be postfixed to the user provided message + */ + SocketException(const string &message, bool inclSysMsg = false) throw(); + + /** + * Provided just to guarantee that no exceptions are thrown. + */ + ~SocketException() throw(); + + /** + * Get the exception message + * @return exception message + */ + const char *what() const throw(); + +private: + string userMessage; // Exception message +}; + +/** + * Base class representing basic communication endpoint + */ +class Socket { +public: + /** + * Close and deallocate this socket + */ + ~Socket(); + + /** + * Get the local address + * @return local address of socket + * @exception SocketException thrown if fetch fails + */ + string getLocalAddress() throw(SocketException); + + /** + * Get the local port + * @return local port of socket + * @exception SocketException thrown if fetch fails + */ + unsigned short getLocalPort() throw(SocketException); + + /** + * Set the local port to the specified port and the local address + * to any interface + * @param localPort local port + * @exception SocketException thrown if setting local port fails + */ + void setLocalPort(unsigned short localPort) throw(SocketException); + + /** + * Set the local port to the specified port and the local address + * to the specified address. If you omit the port, a random port + * will be selected. + * @param localAddress local address + * @param localPort local port + * @exception SocketException thrown if setting local port or address fails + */ + void setLocalAddressAndPort(const string &localAddress, + unsigned short localPort = 0) throw(SocketException); + + /** + * If WinSock, unload the WinSock DLLs; otherwise do nothing. We ignore + * this in our sample client code but include it in the library for + * completeness. If you are running on Windows and you are concerned + * about DLL resource consumption, call this after you are done with all + * Socket instances. If you execute this on Windows while some instance of + * Socket exists, you are toast. For portability of client code, this is + * an empty function on non-Windows platforms so you can always include it. + * @param buffer buffer to receive the data + * @param bufferLen maximum number of bytes to read into buffer + * @return number of bytes read, 0 for EOF, and -1 for error + * @exception SocketException thrown WinSock clean up fails + */ + static void cleanUp() throw(SocketException); + + /** + * Resolve the specified service for the specified protocol to the + * corresponding port number in host byte order + * @param service service to resolve (e.g., "http") + * @param protocol protocol of service to resolve. Default is "tcp". + */ + static unsigned short resolveService(const string &service, + const string &protocol = "tcp"); + +private: + // Prevent the user from trying to use value semantics on this object + Socket(const Socket &sock); + void operator=(const Socket &sock); + +protected: + int sockDesc; // Socket descriptor + Socket(int type, int protocol) throw(SocketException); + Socket(int sockDesc); +}; + +/** + * Socket which is able to connect, send, and receive + */ +class CommunicatingSocket : public Socket { +public: + /** + * Establish a socket connection with the given foreign + * address and port + * @param foreignAddress foreign address (IP address or name) + * @param foreignPort foreign port + * @exception SocketException thrown if unable to establish connection + */ + void connect(const string &foreignAddress, unsigned short foreignPort) + throw(SocketException); + + /** + * Write the given buffer to this socket. Call connect() before + * calling send() + * @param buffer buffer to be written + * @param bufferLen number of bytes from buffer to be written + * @exception SocketException thrown if unable to send data + */ + void send(const void *buffer, int bufferLen) throw(SocketException); + + /** + * Read into the given buffer up to bufferLen bytes data from this + * socket. Call connect() before calling recv() + * @param buffer buffer to receive the data + * @param bufferLen maximum number of bytes to read into buffer + * @return number of bytes read, 0 for EOF, and -1 for error + * @exception SocketException thrown if unable to receive data + */ + int recv(void *buffer, int bufferLen) throw(SocketException); + + /** + * Get the foreign address. Call connect() before calling recv() + * @return foreign address + * @exception SocketException thrown if unable to fetch foreign address + */ + string getForeignAddress() throw(SocketException); + + /** + * Get the foreign port. Call connect() before calling recv() + * @return foreign port + * @exception SocketException thrown if unable to fetch foreign port + */ + unsigned short getForeignPort() throw(SocketException); + +protected: + CommunicatingSocket(int type, int protocol) throw(SocketException); + CommunicatingSocket(int newConnSD); +}; + +/** + * TCP socket for communication with other TCP sockets + */ +class TCPSocket : public CommunicatingSocket { +public: + /** + * Construct a TCP socket with no connection + * @exception SocketException thrown if unable to create TCP socket + */ + TCPSocket() throw(SocketException); + + /** + * Construct a TCP socket with a connection to the given foreign address + * and port + * @param foreignAddress foreign address (IP address or name) + * @param foreignPort foreign port + * @exception SocketException thrown if unable to create TCP socket + */ + TCPSocket(const string &foreignAddress, unsigned short foreignPort) + throw(SocketException); + +private: + // Access for TCPServerSocket::accept() connection creation + friend class TCPServerSocket; + TCPSocket(int newConnSD); +}; + +/** + * TCP socket class for servers + */ +class TCPServerSocket : public Socket { +public: + /** + * Construct a TCP socket for use with a server, accepting connections + * on the specified port on any interface + * @param localPort local port of server socket, a value of zero will + * give a system-assigned unused port + * @param queueLen maximum queue length for outstanding + * connection requests (default 5) + * @exception SocketException thrown if unable to create TCP server socket + */ + TCPServerSocket(unsigned short localPort, int queueLen = 5) + throw(SocketException); + + /** + * Construct a TCP socket for use with a server, accepting connections + * on the specified port on the interface specified by the given address + * @param localAddress local interface (address) of server socket + * @param localPort local port of server socket + * @param queueLen maximum queue length for outstanding + * connection requests (default 5) + * @exception SocketException thrown if unable to create TCP server socket + */ + TCPServerSocket(const string &localAddress, unsigned short localPort, + int queueLen = 5) throw(SocketException); + + /** + * Blocks until a new connection is established on this socket or error + * @return new connection socket + * @exception SocketException thrown if attempt to accept a new connection fails + */ + TCPSocket *accept() throw(SocketException); + +private: + void setListen(int queueLen) throw(SocketException); +}; + +/** + * UDP socket class + */ +class UDPSocket : public CommunicatingSocket { +public: + /** + * Construct a UDP socket + * @exception SocketException thrown if unable to create UDP socket + */ + UDPSocket() throw(SocketException); + + /** + * Construct a UDP socket with the given local port + * @param localPort local port + * @exception SocketException thrown if unable to create UDP socket + */ + UDPSocket(unsigned short localPort) throw(SocketException); + + /** + * Construct a UDP socket with the given local port and address + * @param localAddress local address + * @param localPort local port + * @exception SocketException thrown if unable to create UDP socket + */ + UDPSocket(const string &localAddress, unsigned short localPort) + throw(SocketException); + + /** + * Unset foreign address and port + * @return true if disassociation is successful + * @exception SocketException thrown if unable to disconnect UDP socket + */ + void disconnect() throw(SocketException); + + /** + * Send the given buffer as a UDP datagram to the + * specified address/port + * @param buffer buffer to be written + * @param bufferLen number of bytes to write + * @param foreignAddress address (IP address or name) to send to + * @param foreignPort port number to send to + * @return true if send is successful + * @exception SocketException thrown if unable to send datagram + */ + void sendTo(const void *buffer, int bufferLen, const string &foreignAddress, + unsigned short foreignPort) throw(SocketException); + + /** + * Read read up to bufferLen bytes data from this socket. The given buffer + * is where the data will be placed + * @param buffer buffer to receive data + * @param bufferLen maximum number of bytes to receive + * @param sourceAddress address of datagram source + * @param sourcePort port of data source + * @return number of bytes received and -1 for error + * @exception SocketException thrown if unable to receive datagram + */ + int recvFrom(void *buffer, int bufferLen, string &sourceAddress, + unsigned short &sourcePort) throw(SocketException); + + /** + * Set the multicast TTL + * @param multicastTTL multicast TTL + * @exception SocketException thrown if unable to set TTL + */ + void setMulticastTTL(unsigned char multicastTTL) throw(SocketException); + + /** + * Join the specified multicast group + * @param multicastGroup multicast group address to join + * @exception SocketException thrown if unable to join group + */ + void joinGroup(const string &multicastGroup) throw(SocketException); + + /** + * Leave the specified multicast group + * @param multicastGroup multicast group address to leave + * @exception SocketException thrown if unable to leave group + */ + void leaveGroup(const string &multicastGroup) throw(SocketException); + +private: + void setBroadcast(); +}; + +#endif diff --git a/tools/streamer_recorder/include/config.h b/tools/streamer_recorder/include/config.h new file mode 100644 index 000000000..89dac1660 --- /dev/null +++ b/tools/streamer_recorder/include/config.h @@ -0,0 +1,8 @@ +#define FRAME_HEIGHT 720 +#define FRAME_WIDTH 1280 +#define FRAME_INTERVAL (1000/30) +#define PACK_SIZE 4096 //udp pack size; note that OSX limits < 8100 bytes +#define ENCODE_QUALITY 80 +#define SERVER_ADDRESS "127.0.0.1" // Server IP adress +#define SERVER_PORT "10000" // Server Port +#define MAX_FRAME_ID 30000 // max number of recorded frames, 16.6min max at 30 FPS (max frame ID sort of hardcoded in image naming too, see below) diff --git a/tools/streamer_recorder/include/recorder.h b/tools/streamer_recorder/include/recorder.h new file mode 100644 index 000000000..f2b18fb1b --- /dev/null +++ b/tools/streamer_recorder/include/recorder.h @@ -0,0 +1,59 @@ +#ifndef RECORDER_H +#define RECORDER_H + +#include +#include +#include "config.h" + +#include +#include +#include +#include +#include // file input/output functions +#include // time stamp +#include // time stamp + +class Recorder +{ +public: + // methods + Recorder(); + void initialize(); + void record(libfreenect2::Frame* frame, const std::string& frame_type); + + void stream(libfreenect2::Frame* frame); + + void saveTimeStamp(); + void registTimeStamp(); + +private: + /////// RECORD VIDEO, NOT READY YET (RECORD IMAGE FOR NOW) /////// + // cv::VideoWriter out_capture; + ///////////////////////////////////////////////////////////////// + + cv::Mat cvMat_frame; + + // SAVE IMAGE + //cv::vector img_comp_param; //vector that stores the compression parameters of the image + std::vector img_comp_param; //vector that stores the compression parameters of the image + int frameID; + // int maxFrameID; // 16.6min max at 30 FPS (max frame ID sort of hardcoded in image naming too, see below) + + // static int timeStamps [MAX_FRAME_ID]; + std::vector timeStamps; + + int t_start; + int t_now; + + std::ostringstream oss_recordPath; + std::string recordPath; + // ----------------- + + // Timer + timeb tb; + int nSpan; + int getMilliSpan(int nTimeStart); + int getMilliCount(); +}; + +#endif diff --git a/tools/streamer_recorder/include/streamer.h b/tools/streamer_recorder/include/streamer.h new file mode 100644 index 000000000..e28cac740 --- /dev/null +++ b/tools/streamer_recorder/include/streamer.h @@ -0,0 +1,31 @@ +#ifndef STREAMER_H +#define STREAMER_H + +#include + +#include "PracticalSocket.h" +#include +#include "config.h" + +class Streamer +{ +public: + // methods + void initialize(); + void stream(libfreenect2::Frame* frame); + +private: + // frame related parameters + int jpegqual; // Compression Parameter + vector < int > compression_params; + vector < uchar > encoded; + int total_pack; + int ibuf[1]; + + // udp related parameters + string servAddress; // Server IP adress + unsigned short servPort; // Server port + UDPSocket sock; +}; + +#endif diff --git a/tools/streamer_recorder/recorder.cpp b/tools/streamer_recorder/recorder.cpp new file mode 100644 index 000000000..632625e12 --- /dev/null +++ b/tools/streamer_recorder/recorder.cpp @@ -0,0 +1,165 @@ +#include "recorder.h" +#include +#include + + +Recorder::Recorder() : timeStamps(MAX_FRAME_ID) +{ +} + +// get initial time in ms +int Recorder::getMilliCount() +{ + ftime(&tb); + int nCount = tb.millitm + (tb.time & 0xfffff) * 1000; + return nCount; +} + +// get time diff from nTimeStart to now +int Recorder::getMilliSpan(int nTimeStart) +{ + nSpan = Recorder::getMilliCount() - nTimeStart; + if(nSpan < 0) + nSpan += 0x100000 * 1000; + return nSpan; +} + +// get time diff from nTimeStart to now +void Recorder::registTimeStamp() +{ + // record time stamp for FPS syncing + timeStamps[frameID] = Recorder::getMilliSpan(t_start); + // printf("Elapsed time = %u ms \n", timeStamps[frameID]); + + frameID ++; +} + +void Recorder::initialize() +{ + std::cout << "Initialize Recorder." << std::endl; + + /////// RECORD VIDEO, NOT READY YET (RECORD IMAGE FOR NOW) /////// + + // // out_capture.open("TestVideo.avi", CV_FOURCC('M','J','P','G'), 30, cv::Size(depth->height, depth->width)); // JPEG + // out_capture.open("TestVideo.avi", CV_FOURCC('P','I','M','1'), 30, cv::Size(depth->height, depth->width),1); // MPEG, last argument defines image color yes o (channel 3 or 1) + // // out_capture.open("TestVideo.avi", CV_FOURCC('D','I','V','X'), 30, cv::Size(depth->height, depth->width)); + + // if( !out_capture.isOpened() ) + // { + // std::cout << "AVI file can not open." << std::endl; + // return 1; + // } + + ///////////////////////////////////////////////////////////////// + + // record image: define compression parameters and frame counter + img_comp_param.push_back(CV_IMWRITE_JPEG_QUALITY); //specify the compression technique + img_comp_param.push_back(100); //specify the compression quality + frameID = 0; + + // record timeStamp + t_start = getMilliCount(); +} + +void Recorder::record(libfreenect2::Frame* frame, const std::string& frame_type) +{ + if(frame_type == "depth") + { + // std::cout << "Run Recorder." << std::endl; + cvMat_frame = cv::Mat(frame->height, frame->width, CV_32FC1, frame->data) / 10; + // TODO: handle relative path + check Windows / UNIX compat. + oss_recordPath << "../recordings/depth/" << std::setw( 5 ) << std::setfill( '0' ) << frameID << ".depth"; + } + else if (frame_type == "registered" || frame_type == "rgb") + { + cvMat_frame = cv::Mat(frame->height, frame->width, CV_8UC4, frame->data); + // TODO: handle relative path + check Windows / UNIX compat. + oss_recordPath << "../recordings/regist/" << std::setw( 5 ) << std::setfill( '0' ) << frameID << ".jpg"; + // std::cout << frame->height << ":" << frame->width << ":" << frame->bytes_per_pixel << std::endl; + } + + recordPath = oss_recordPath.str(); + + // SAVE IMAGE + cv::imwrite(recordPath, cvMat_frame, img_comp_param); //write the image to file + // std::cout << recordPath << std::endl; + + // show image + // cv::namedWindow( "recorded frame", CV_WINDOW_AUTOSIZE); + // cv::imshow("recorded frame", cvMat_frame); + // cv::waitKey(0); + + // reset ostr + oss_recordPath.str(""); + oss_recordPath.clear(); + + // feedback on current recording state + if(frameID % 100 == 0) + std::cout << "-> " << frameID << "/" << MAX_FRAME_ID << " recorded frames/maxFrameID (" << frame_type << ")" << std::endl; + + /////// RECORD VIDEO, NOT READY YET (RECORD IMAGE FOR NOW) /////// + + // cv::Mat frame_depth = cv::Mat(depth->height, depth->width, CV_32FC1, depth->data) / 4500.0f; + // cv::convertScaleAbs(frame_depth, frame_depth); + + // DISPLAY INFOS + // std::cout << "kinect (h,w): " << depth->height << "," << depth->width << std::endl; + + // cv::Size s = frame_depth.size(); + // double rows = s.height; + // double cols = s.width; + // std::cout << "cvMat (h,w): " << rows << "," << cols << std::endl; + + // RECORD VIDEO + // std::cout << "11111" << std::endl; + // std::cout << "Img channels: " << frame_depth.channels() << std::endl; + // std::cout << "Img depth: " << frame_depth.depth() << std::endl; + // std::cout << "Img type: " << frame_depth.type() << std::endl; + + // frame_depth.convertTo(frame_depth, CV_8UC1); // IPL_DEPTH_8U + // cv::cvtColor(frame_depth, frame_depth, CV_GRAY2BGR); // convert image to RGB + + // CONVERT form CV_32FC1 to CV_8UC1 + // double minVal, maxVal; + // minMaxLoc(frame_depth, &minVal, &maxVal); //find minimum and maximum intensities + // cv::Mat draw; + // frame_depth.convertTo(frame_depth, CV_8UC1, 255.0/(maxVal - minVal), -minVal * 255.0/(maxVal - minVal)); + + // out_capture.open("TestVideo.avi", CV_FOURCC('P','I','M','1'), 30, dest_rgb.size()); + // cv::Mat imgY = cv::Mat(dest_rgb.size(), CV_32FC1); + // cv::cvtColor(frame_depth, frame_depth, CV_GRAY2BGR); // convert image to RGB + // std::cout << "111111" << std::endl; + // if( !frame_depth.empty() && frame_depth.data) + // { + // std::cout << "Img channels: " << frame_depth.channels() << std::endl; + // std::cout << "Img depth: " << frame_depth.depth() << std::endl; + // std::cout << "Img type: " << frame_depth.type() << std::endl; + + // out_capture.write(frame_depth); + // // out_capture << frame_depth; // same same + // std::cout << "22222" << std::endl; + // } + + ///////////////////////////////////////////////////////////////// +} + +// save timeStamp file for FPS syncing +void Recorder::saveTimeStamp() +{ + // TODO: handle relative path + check Windows / UNIX compat. + std::ofstream fout("../recordings/timeStamp.txt"); + if(fout.is_open()) + { + std::cout << "recording lasted " << ((timeStamps[frameID-1]-timeStamps[0])/1000.0) << " sec(s), writing timeStamp data..." << std::endl; + fout << "# Elapsed time in ms # \n"; + for(int i = 0; i + +void Streamer::initialize() +{ + std::cout << "Initialize Streamer." << std::endl; + + jpegqual = ENCODE_QUALITY; // Compression Parameter + + servAddress = SERVER_ADDRESS; + servPort = Socket::resolveService(SERVER_PORT, "udp"); // Server port + + compression_params.push_back(CV_IMWRITE_JPEG_QUALITY); + compression_params.push_back(jpegqual); + +} + +void Streamer::stream(libfreenect2::Frame* frame) +{ + +try { + + // int total_pack = 1 + (encoded.size() - 1) / PACK_SIZE; + cv::Mat frame_depth = cv::Mat(frame->height, frame->width, CV_32FC1, frame->data) / 10; + cv::imencode(".jpg", frame_depth, encoded, compression_params); + + // resize image + // resize(frame, encoded, Size(FRAME_WIDTH, FRAME_HEIGHT), 0, 0, INTER_LINEAR); + + // show encoded frame + // cv::namedWindow( "streamed frame", CV_WINDOW_AUTOSIZE); + // cv::imshow("streamed frame", encoded); + // cv::waitKey(0); + + total_pack = 1 + (encoded.size() - 1) / PACK_SIZE; + + // send pre-info + ibuf[0] = total_pack; + sock.sendTo(ibuf, sizeof(int), servAddress, servPort); + + // send image data packet + for (int i = 0; i < total_pack; i++) + sock.sendTo( & encoded[i * PACK_SIZE], PACK_SIZE, servAddress, servPort); + + + } catch (SocketException & e) { + cerr << e.what() << endl; + // exit(1); + } +}