Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Patches for new libfreenect API and a Linux build script. #17

Merged
merged 5 commits into from Jun 20, 2011
@@ -0,0 +1,15 @@
+#!/bin/sh
+echo "Assuming homebrew install of libusb and libfreenect"
+g++ -fPIC -shared -Wno-write-strings -lresolv \
+ -I/usr/local/include \
+ -I/usr/local/include/libusb-1.0 -I/usr/local/include/libfreenect \
+ `pkg-config --cflags libusb-1.0` \
+ `pkg-config --cflags opencv` \
+ `pkg-config --libs --static opencv` \
+ -L/usr/local/lib \
+ -lfreenect \
+ -lz \
+ -Wall \
+ -o depthjs \
+ np_entry.cc npp_entry.cc plugin.cc depthjs.cc \
+ ocv_freenect.cpp bg_fg_blobs.cpp
View
@@ -72,7 +72,7 @@ class Mutex {
class DepthJSDevice : public Freenect::FreenectDevice {
public:
DepthJSDevice(freenect_context *_ctx, int _index)
- : Freenect::FreenectDevice(_ctx, _index), m_buffer_depth(FREENECT_DEPTH_11BIT),m_buffer_video(FREENECT_VIDEO_RGB_SIZE), m_gamma(2048), m_new_rgb_frame(false), m_new_depth_frame(false)
+ : Freenect::FreenectDevice(_ctx, _index), m_buffer_depth(freenect_find_depth_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_11BIT).bytes),m_buffer_video(freenect_find_video_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_RGB).bytes), m_gamma(2048), m_new_rgb_frame(false), m_new_depth_frame(false)
{
for( unsigned int i = 0 ; i < 2048 ; i++) {
float v = i/2048.0;
@@ -1,215 +0,0 @@
-/*
- * This file is part of the OpenKinect Project. http://www.openkinect.org
- *
- * Copyright (c) 2010 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.
- */
-
-#pragma once
-
-#include <libfreenect.h>
-#include <stdexcept>
-#include <map>
-#include <pthread.h>
-
-namespace Freenect {
- class Noncopyable {
- public:
- Noncopyable() {}
- ~Noncopyable() {}
- private:
- Noncopyable( const Noncopyable& );
- const Noncopyable& operator=( const Noncopyable& );
- };
-
- class FreenectTiltState {
- friend class FreenectDevice;
- FreenectTiltState(freenect_raw_tilt_state *_state):
- m_code(_state->tilt_status), m_state(_state)
- {}
- public:
- void getAccelerometers(double* x, double* y, double* z) {
- freenect_get_mks_accel(m_state, x, y, z);
- }
- double getTiltDegs() {
- return freenect_get_tilt_degs(m_state);
- }
- public:
- freenect_tilt_status_code m_code;
- private:
- freenect_raw_tilt_state *m_state;
- };
-
- class FreenectDevice : Noncopyable {
- public:
- FreenectDevice(freenect_context *_ctx, int _index) {
- if(freenect_open_device(_ctx, &m_dev, _index) < 0) throw std::runtime_error("Cannot open Kinect");
- freenect_set_user(m_dev, this);
- freenect_set_video_format(m_dev, FREENECT_VIDEO_RGB);
- freenect_set_depth_format(m_dev, FREENECT_DEPTH_11BIT);
- freenect_set_depth_callback(m_dev, freenect_depth_callback);
- freenect_set_video_callback(m_dev, freenect_video_callback);
- }
- virtual ~FreenectDevice() {
- if(freenect_close_device(m_dev) < 0) {
- std::cout << "Device did not shutdown in a clean fashion";
- }
- }
- void startVideo() {
- if(freenect_start_video(m_dev) < 0) throw std::runtime_error("Cannot start RGB callback");
- }
- void stopVideo() {
- if(freenect_stop_video(m_dev) < 0) throw std::runtime_error("Cannot stop RGB callback");
- }
- void startDepth() {
- if(freenect_start_depth(m_dev) < 0) throw std::runtime_error("Cannot start depth callback");
- }
- void stopDepth() {
- if(freenect_stop_depth(m_dev) < 0) throw std::runtime_error("Cannot stop depth callback");
- }
- void setTiltDegrees(double _angle) {
- if(freenect_set_tilt_degs(m_dev, _angle) < 0) throw std::runtime_error("Cannot set angle in degrees");
- }
- void setLed(freenect_led_options _option) {
- if(freenect_set_led(m_dev, _option) < 0) throw std::runtime_error("Cannot set led");
- }
- void updateState() {
- if (freenect_update_tilt_state(m_dev) < 0) throw std::runtime_error("Cannot update device state");
- }
- FreenectTiltState getState() const {
- return FreenectTiltState(freenect_get_tilt_state(m_dev));
- }
- void setVideoFormat(freenect_video_format requested_format) {
- if (requested_format != m_video_format) {
- freenect_stop_video(m_dev);
- if (freenect_set_video_format(m_dev, requested_format) < 0) throw std::runtime_error("Cannot set video format");
- freenect_start_video(m_dev);
- m_video_format = requested_format;
- }
- }
- freenect_video_format getVideoFormat() {
- return m_video_format;
- }
- void setDepthFormat(freenect_depth_format requested_format) {
- if (requested_format != m_depth_format) {
- freenect_stop_depth(m_dev);
- if (freenect_set_depth_format(m_dev, requested_format) < 0) throw std::runtime_error("Cannot set depth format");
- freenect_start_depth(m_dev);
- m_depth_format = requested_format;
- }
- }
- freenect_depth_format getDepthFormat() {
- return m_depth_format;
- }
- // Do not call directly even in child
- virtual void VideoCallback(void *video, uint32_t timestamp) = 0;
- // Do not call directly even in child
- virtual void DepthCallback(void *depth, uint32_t timestamp) = 0;
- protected:
- int getVideoBufferSize(){
- if(m_video_format == FREENECT_VIDEO_RGB) return FREENECT_VIDEO_RGB_SIZE;
- if(m_video_format == FREENECT_VIDEO_BAYER) return FREENECT_VIDEO_BAYER_SIZE;
- if(m_video_format == FREENECT_VIDEO_IR_8BIT) return FREENECT_VIDEO_IR_8BIT_SIZE;
- if(m_video_format == FREENECT_VIDEO_IR_10BIT) return FREENECT_VIDEO_IR_10BIT_SIZE;
- if(m_video_format == FREENECT_VIDEO_IR_10BIT_PACKED) return FREENECT_VIDEO_IR_10BIT_PACKED_SIZE;
- if(m_video_format == FREENECT_VIDEO_YUV_RGB) return FREENECT_VIDEO_YUV_RGB_SIZE;
- if(m_video_format == FREENECT_VIDEO_YUV_RAW) return FREENECT_VIDEO_YUV_RAW_SIZE;
- return 0;
- }
- int getDepthBufferSize(){
- if(m_depth_format == FREENECT_DEPTH_11BIT) return FREENECT_DEPTH_11BIT_SIZE;
- if(m_depth_format == FREENECT_DEPTH_10BIT) return FREENECT_DEPTH_11BIT_SIZE;
- if(m_depth_format == FREENECT_DEPTH_11BIT_PACKED) return FREENECT_DEPTH_11BIT_PACKED_SIZE;
- if(m_depth_format == FREENECT_DEPTH_10BIT_PACKED) return FREENECT_DEPTH_10BIT_PACKED_SIZE;
- return 0;
- }
- private:
- freenect_device *m_dev;
- freenect_video_format m_video_format;
- freenect_depth_format m_depth_format;
- static void freenect_depth_callback(freenect_device *dev, void *depth, uint32_t timestamp) {
- FreenectDevice* device = static_cast<FreenectDevice*>(freenect_get_user(dev));
- device->DepthCallback(depth, timestamp);
- }
- static void freenect_video_callback(freenect_device *dev, void *video, uint32_t timestamp) {
- FreenectDevice* device = static_cast<FreenectDevice*>(freenect_get_user(dev));
- device->VideoCallback(video, timestamp);
- }
- };
-
- class Freenect : Noncopyable {
- private:
- typedef std::map<int, FreenectDevice*> DeviceMap;
- public:
- Freenect() : m_stop(false) {
- if(freenect_init(&m_ctx, NULL) < 0) throw std::runtime_error("Cannot initialize freenect library");
- if(pthread_create(&m_thread, NULL, pthread_callback, (void*)this) != 0) throw std::runtime_error("Cannot initialize freenect thread");
- }
- ~Freenect() {
- for(DeviceMap::iterator it = m_devices.begin() ; it != m_devices.end() ; ++it) {
- delete it->second;
- }
- m_stop = true;
- std::cout << "Waiting for freenect thread to die" << "\n";
- pthread_join(m_thread, NULL);
- std::cout << "Freenect thread is dead" << "\n";
- if(freenect_shutdown(m_ctx) < 0){
- std::cout << "Freenect did not shutdown in a clean fashion";
- }
- }
- template <typename ConcreteDevice>
- ConcreteDevice& createDevice(int _index) {
- DeviceMap::iterator it = m_devices.find(_index);
- if (it != m_devices.end()) delete it->second;
- ConcreteDevice * device = new ConcreteDevice(m_ctx, _index);
- m_devices.insert(std::make_pair<int, FreenectDevice*>(_index, device));
- return *device;
- }
- void deleteDevice(int _index) {
- DeviceMap::iterator it = m_devices.find(_index);
- if (it == m_devices.end()) return;
- delete it->second;
- m_devices.erase(it);
- }
- int deviceCount() {
- return freenect_num_devices(m_ctx);
- }
- // Do not call directly, thread runs here
- void operator()() {
- while(!m_stop) {
- if(freenect_process_events(m_ctx) < 0) throw std::runtime_error("Cannot process freenect events");
- }
- }
- static void *pthread_callback(void *user_data) {
- Freenect* freenect = static_cast<Freenect*>(user_data);
- (*freenect)();
- return NULL;
- }
- private:
- freenect_context *m_ctx;
- volatile bool m_stop;
- pthread_t m_thread;
- DeviceMap m_devices;
- };
-
-}
-
@@ -131,6 +131,7 @@ NPError NPP_DestroyStream(NPP instance, NPStream* stream, NPReason reason) {
}
int16_t NPP_HandleEvent(NPP instance, void* event) {
+#ifdef __APPLE__
EventRecord* carbonEvent = (EventRecord*)event;
if (carbonEvent && carbonEvent->what != 0) {
std::cout << "DepthJS Plugin: NPP_HandleEvent type: " << carbonEvent->what << "\n";
@@ -139,6 +140,7 @@ int16_t NPP_HandleEvent(NPP instance, void* event) {
NPCocoaEvent* cocoaEvent = (NPCocoaEvent*)event;
std::cout << "DepthJS: NPP_HandleEvent type: " << cocoaEvent->type << "\n";
*/
+#endif
return 0;
}
@@ -54,7 +54,8 @@ void depth_cb(freenect_device *dev, void *depth, uint32_t timestamp)
//copy to ocv buf...
//memcpy(depthMat.data, depth, FREENECT_DEPTH_SIZE);
- memcpy(depthMat.datastart, depth, FREENECT_DEPTH_11BIT_SIZE);
+ //memcpy(depthMat.datastart, depth, FREENECT_DEPTH_11BIT_SIZE);
+ memcpy(depthMat.datastart, depth, freenect_find_depth_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_11BIT).bytes);
got_frames++;
pthread_cond_signal(&frame_cond);
@@ -67,7 +68,8 @@ void rgb_cb(freenect_device *dev, void *rgb, uint32_t timestamp)
got_frames++;
//copy to ocv_buf..
//memcpy(rgbMat.data, rgb, FREENECT_RGB_SIZE);
- memcpy(rgbMat.datastart, rgb, FREENECT_VIDEO_RGB_SIZE);
+ //memcpy(rgbMat.datastart, rgb, FREENECT_VIDEO_RGB_SIZE);
+ memcpy(rgbMat.datastart, rgb, freenect_find_video_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_RGB).bytes);
pthread_cond_signal(&frame_cond);
pthread_mutex_unlock(&buf_mutex);
@@ -130,8 +132,10 @@ int initFreenect() {
//freenect_set_rgb_callback(f_dev, rgb_cb);
//freenect_set_rgb_format(f_dev, FREENECT_FORMAT_RGB);
freenect_set_video_callback(f_dev, rgb_cb);
- freenect_set_video_format(f_dev, FREENECT_VIDEO_RGB);
- freenect_set_depth_format(f_dev, FREENECT_DEPTH_11BIT);
+ //freenect_set_video_format(f_dev, FREENECT_VIDEO_RGB);
+ //freenect_set_depth_format(f_dev, FREENECT_DEPTH_11BIT);
+ freenect_set_video_mode(f_dev, freenect_find_video_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_RGB));
+ freenect_set_depth_mode(f_dev, freenect_find_depth_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_11BIT));
freenect_start_depth(f_dev);
freenect_start_video(f_dev);