Skip to content
This repository
Browse code

Merge pull request #17 from kench/master

Patches for new libfreenect API and a Linux build script.
  • Loading branch information...
commit 76ff23346ceaa68364bc2060cf5138ff3a02616c 2 parents 8a0df9b + 2c137a5
Aaron Zinman authored June 19, 2011
15  npapi_plugin/build-linux.sh
... ...
@@ -0,0 +1,15 @@
  1
+#!/bin/sh
  2
+echo "Assuming homebrew install of libusb and libfreenect"
  3
+g++ -fPIC -shared -Wno-write-strings -lresolv \
  4
+  -I/usr/local/include \
  5
+  -I/usr/local/include/libusb-1.0 -I/usr/local/include/libfreenect \
  6
+  `pkg-config --cflags libusb-1.0` \
  7
+  `pkg-config --cflags opencv` \
  8
+  `pkg-config --libs --static opencv` \
  9
+  -L/usr/local/lib \
  10
+  -lfreenect \
  11
+  -lz \
  12
+  -Wall \
  13
+  -o depthjs \
  14
+   np_entry.cc npp_entry.cc plugin.cc depthjs.cc \
  15
+   ocv_freenect.cpp bg_fg_blobs.cpp
2  npapi_plugin/depthjs.cc
@@ -72,7 +72,7 @@ class Mutex {
72 72
 class DepthJSDevice : public Freenect::FreenectDevice {
73 73
 public:
74 74
   DepthJSDevice(freenect_context *_ctx, int _index)
75  
-    : 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)
  75
+    : 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)
76 76
   {
77 77
     for( unsigned int i = 0 ; i < 2048 ; i++) {
78 78
       float v = i/2048.0;
215  npapi_plugin/libfreenect.hpp
... ...
@@ -1,215 +0,0 @@
1  
-/*
2  
- * This file is part of the OpenKinect Project. http://www.openkinect.org
3  
- *
4  
- * Copyright (c) 2010 individual OpenKinect contributors. See the CONTRIB file
5  
- * for details.
6  
- *
7  
- * This code is licensed to you under the terms of the Apache License, version
8  
- * 2.0, or, at your option, the terms of the GNU General Public License,
9  
- * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses,
10  
- * or the following URLs:
11  
- * http://www.apache.org/licenses/LICENSE-2.0
12  
- * http://www.gnu.org/licenses/gpl-2.0.txt
13  
- *
14  
- * If you redistribute this file in source form, modified or unmodified, you
15  
- * may:
16  
- *   1) Leave this header intact and distribute it under the same terms,
17  
- *      accompanying it with the APACHE20 and GPL20 files, or
18  
- *   2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or
19  
- *   3) Delete the GPL v2 clause and accompany it with the APACHE20 file
20  
- * In all cases you must keep the copyright notice intact and include a copy
21  
- * of the CONTRIB file.
22  
- *
23  
- * Binary distributions must follow the binary distribution requirements of
24  
- * either License.
25  
- */
26  
-
27  
-#pragma once
28  
-
29  
-#include <libfreenect.h>
30  
-#include <stdexcept>
31  
-#include <map>
32  
-#include <pthread.h>
33  
-
34  
-namespace Freenect {
35  
-  class Noncopyable {
36  
-    public:
37  
-    Noncopyable() {}
38  
-    ~Noncopyable() {}
39  
-    private:
40  
-    Noncopyable( const Noncopyable& );
41  
-    const Noncopyable& operator=( const Noncopyable& );
42  
-  };
43  
-
44  
-  class FreenectTiltState {
45  
-    friend class FreenectDevice;
46  
-    FreenectTiltState(freenect_raw_tilt_state *_state):
47  
-      m_code(_state->tilt_status), m_state(_state)
48  
-    {}
49  
-    public:
50  
-    void getAccelerometers(double* x, double* y, double* z) {
51  
-      freenect_get_mks_accel(m_state, x, y, z);
52  
-    }
53  
-    double getTiltDegs() {
54  
-      return freenect_get_tilt_degs(m_state);
55  
-    }
56  
-    public:
57  
-    freenect_tilt_status_code m_code;
58  
-    private:
59  
-    freenect_raw_tilt_state *m_state;
60  
-  };
61  
-
62  
-  class FreenectDevice : Noncopyable {
63  
-    public:
64  
-    FreenectDevice(freenect_context *_ctx, int _index) {
65  
-      if(freenect_open_device(_ctx, &m_dev, _index) < 0) throw std::runtime_error("Cannot open Kinect");
66  
-      freenect_set_user(m_dev, this);
67  
-      freenect_set_video_format(m_dev, FREENECT_VIDEO_RGB);
68  
-      freenect_set_depth_format(m_dev, FREENECT_DEPTH_11BIT);
69  
-      freenect_set_depth_callback(m_dev, freenect_depth_callback);
70  
-      freenect_set_video_callback(m_dev, freenect_video_callback);
71  
-    }
72  
-    virtual ~FreenectDevice() {
73  
-      if(freenect_close_device(m_dev) < 0) {
74  
-        std::cout << "Device did not shutdown in a clean fashion";
75  
-      }
76  
-    }
77  
-    void startVideo() {
78  
-      if(freenect_start_video(m_dev) < 0) throw std::runtime_error("Cannot start RGB callback");
79  
-    }
80  
-    void stopVideo() {
81  
-      if(freenect_stop_video(m_dev) < 0) throw std::runtime_error("Cannot stop RGB callback");
82  
-    }
83  
-    void startDepth() {
84  
-      if(freenect_start_depth(m_dev) < 0) throw std::runtime_error("Cannot start depth callback");
85  
-    }
86  
-    void stopDepth() {
87  
-      if(freenect_stop_depth(m_dev) < 0) throw std::runtime_error("Cannot stop depth callback");
88  
-    }
89  
-    void setTiltDegrees(double _angle) {
90  
-      if(freenect_set_tilt_degs(m_dev, _angle) < 0) throw std::runtime_error("Cannot set angle in degrees");
91  
-    }
92  
-    void setLed(freenect_led_options _option) {
93  
-      if(freenect_set_led(m_dev, _option) < 0) throw std::runtime_error("Cannot set led");
94  
-    }
95  
-    void updateState() {
96  
-      if (freenect_update_tilt_state(m_dev) < 0) throw std::runtime_error("Cannot update device state");
97  
-    }
98  
-    FreenectTiltState getState() const {
99  
-      return FreenectTiltState(freenect_get_tilt_state(m_dev));
100  
-    }
101  
-    void setVideoFormat(freenect_video_format requested_format) {
102  
-      if (requested_format != m_video_format) {
103  
-        freenect_stop_video(m_dev);
104  
-        if (freenect_set_video_format(m_dev, requested_format) < 0) throw std::runtime_error("Cannot set video format");
105  
-        freenect_start_video(m_dev);
106  
-        m_video_format = requested_format;
107  
-      }
108  
-    }
109  
-    freenect_video_format getVideoFormat() {
110  
-      return m_video_format;
111  
-    }
112  
-    void setDepthFormat(freenect_depth_format requested_format) {
113  
-      if (requested_format != m_depth_format) {
114  
-        freenect_stop_depth(m_dev);
115  
-        if (freenect_set_depth_format(m_dev, requested_format) < 0) throw std::runtime_error("Cannot set depth format");
116  
-        freenect_start_depth(m_dev);
117  
-        m_depth_format = requested_format;
118  
-      }
119  
-    }
120  
-    freenect_depth_format getDepthFormat() {
121  
-      return m_depth_format;
122  
-    }
123  
-    // Do not call directly even in child
124  
-    virtual void VideoCallback(void *video, uint32_t timestamp) = 0;
125  
-    // Do not call directly even in child
126  
-    virtual void DepthCallback(void *depth, uint32_t timestamp) = 0;
127  
-    protected:
128  
-    int getVideoBufferSize(){
129  
-      if(m_video_format == FREENECT_VIDEO_RGB) return FREENECT_VIDEO_RGB_SIZE;
130  
-      if(m_video_format == FREENECT_VIDEO_BAYER) return FREENECT_VIDEO_BAYER_SIZE;
131  
-      if(m_video_format == FREENECT_VIDEO_IR_8BIT) return FREENECT_VIDEO_IR_8BIT_SIZE;
132  
-      if(m_video_format == FREENECT_VIDEO_IR_10BIT) return FREENECT_VIDEO_IR_10BIT_SIZE;
133  
-      if(m_video_format == FREENECT_VIDEO_IR_10BIT_PACKED) return FREENECT_VIDEO_IR_10BIT_PACKED_SIZE;
134  
-      if(m_video_format == FREENECT_VIDEO_YUV_RGB) return FREENECT_VIDEO_YUV_RGB_SIZE;
135  
-      if(m_video_format == FREENECT_VIDEO_YUV_RAW) return FREENECT_VIDEO_YUV_RAW_SIZE;
136  
-      return 0;
137  
-    }
138  
-    int getDepthBufferSize(){
139  
-      if(m_depth_format == FREENECT_DEPTH_11BIT) return FREENECT_DEPTH_11BIT_SIZE;
140  
-      if(m_depth_format == FREENECT_DEPTH_10BIT) return FREENECT_DEPTH_11BIT_SIZE;
141  
-      if(m_depth_format == FREENECT_DEPTH_11BIT_PACKED) return FREENECT_DEPTH_11BIT_PACKED_SIZE;
142  
-      if(m_depth_format == FREENECT_DEPTH_10BIT_PACKED) return FREENECT_DEPTH_10BIT_PACKED_SIZE;
143  
-      return 0;
144  
-    }
145  
-    private:
146  
-    freenect_device *m_dev;
147  
-    freenect_video_format m_video_format;
148  
-    freenect_depth_format m_depth_format;
149  
-    static void freenect_depth_callback(freenect_device *dev, void *depth, uint32_t timestamp) {
150  
-      FreenectDevice* device = static_cast<FreenectDevice*>(freenect_get_user(dev));
151  
-      device->DepthCallback(depth, timestamp);
152  
-    }
153  
-    static void freenect_video_callback(freenect_device *dev, void *video, uint32_t timestamp) {
154  
-      FreenectDevice* device = static_cast<FreenectDevice*>(freenect_get_user(dev));
155  
-      device->VideoCallback(video, timestamp);
156  
-    }
157  
-  };
158  
-
159  
-  class Freenect : Noncopyable {
160  
-    private:
161  
-    typedef std::map<int, FreenectDevice*> DeviceMap;
162  
-    public:
163  
-    Freenect() : m_stop(false) {
164  
-      if(freenect_init(&m_ctx, NULL) < 0) throw std::runtime_error("Cannot initialize freenect library");
165  
-      if(pthread_create(&m_thread, NULL, pthread_callback, (void*)this) != 0) throw std::runtime_error("Cannot initialize freenect thread");
166  
-    }
167  
-    ~Freenect() {
168  
-      for(DeviceMap::iterator it = m_devices.begin() ; it != m_devices.end() ; ++it) {
169  
-        delete it->second;
170  
-      }
171  
-      m_stop = true;
172  
-      std::cout << "Waiting for freenect thread to die" << "\n";
173  
-      pthread_join(m_thread, NULL);
174  
-      std::cout << "Freenect thread is dead" << "\n";
175  
-      if(freenect_shutdown(m_ctx) < 0){
176  
-        std::cout << "Freenect did not shutdown in a clean fashion";
177  
-      }
178  
-    }
179  
-    template <typename ConcreteDevice>
180  
-    ConcreteDevice& createDevice(int _index) {
181  
-      DeviceMap::iterator it = m_devices.find(_index);
182  
-      if (it != m_devices.end()) delete it->second;
183  
-      ConcreteDevice * device = new ConcreteDevice(m_ctx, _index);
184  
-      m_devices.insert(std::make_pair<int, FreenectDevice*>(_index, device));
185  
-      return *device;
186  
-    }
187  
-    void deleteDevice(int _index) {
188  
-      DeviceMap::iterator it = m_devices.find(_index);
189  
-      if (it == m_devices.end()) return;
190  
-      delete it->second;
191  
-      m_devices.erase(it);
192  
-    }
193  
-    int deviceCount() {
194  
-      return freenect_num_devices(m_ctx);
195  
-    }
196  
-    // Do not call directly, thread runs here
197  
-    void operator()() {
198  
-      while(!m_stop) {
199  
-        if(freenect_process_events(m_ctx) < 0) throw std::runtime_error("Cannot process freenect events");
200  
-      }
201  
-    }
202  
-    static void *pthread_callback(void *user_data) {
203  
-      Freenect* freenect = static_cast<Freenect*>(user_data);
204  
-      (*freenect)();
205  
-      return NULL;
206  
-    }
207  
-    private:
208  
-    freenect_context *m_ctx;
209  
-    volatile bool m_stop;
210  
-    pthread_t m_thread;
211  
-    DeviceMap m_devices;
212  
-  };
213  
-
214  
-}
215  
-
2  npapi_plugin/npp_entry.cc
@@ -131,6 +131,7 @@ NPError NPP_DestroyStream(NPP instance, NPStream* stream, NPReason reason) {
131 131
 }
132 132
 
133 133
 int16_t NPP_HandleEvent(NPP instance, void* event) {
  134
+#ifdef __APPLE__
134 135
   EventRecord* carbonEvent = (EventRecord*)event;
135 136
   if (carbonEvent && carbonEvent->what != 0) {
136 137
     std::cout << "DepthJS Plugin: NPP_HandleEvent type: " << carbonEvent->what << "\n";
@@ -139,6 +140,7 @@ int16_t NPP_HandleEvent(NPP instance, void* event) {
139 140
   NPCocoaEvent* cocoaEvent = (NPCocoaEvent*)event;
140 141
   std::cout << "DepthJS: NPP_HandleEvent type: " << cocoaEvent->type << "\n";
141 142
   */
  143
+#endif
142 144
   return 0;
143 145
 }
144 146
 
12  npapi_plugin/ocv_freenect.cpp
@@ -54,7 +54,8 @@ void depth_cb(freenect_device *dev, void *depth, uint32_t timestamp)
54 54
 
55 55
   //copy to ocv buf...
56 56
   //memcpy(depthMat.data, depth, FREENECT_DEPTH_SIZE);
57  
-  memcpy(depthMat.datastart, depth, FREENECT_DEPTH_11BIT_SIZE);
  57
+  //memcpy(depthMat.datastart, depth, FREENECT_DEPTH_11BIT_SIZE);
  58
+  memcpy(depthMat.datastart, depth, freenect_find_depth_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_11BIT).bytes);
58 59
 
59 60
   got_frames++;
60 61
   pthread_cond_signal(&frame_cond);
@@ -67,7 +68,8 @@ void rgb_cb(freenect_device *dev, void *rgb, uint32_t timestamp)
67 68
   got_frames++;
68 69
   //copy to ocv_buf..
69 70
   //memcpy(rgbMat.data, rgb, FREENECT_RGB_SIZE);
70  
-  memcpy(rgbMat.datastart, rgb, FREENECT_VIDEO_RGB_SIZE);
  71
+  //memcpy(rgbMat.datastart, rgb, FREENECT_VIDEO_RGB_SIZE);
  72
+  memcpy(rgbMat.datastart, rgb, freenect_find_video_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_RGB).bytes);
71 73
 
72 74
   pthread_cond_signal(&frame_cond);
73 75
   pthread_mutex_unlock(&buf_mutex);
@@ -130,8 +132,10 @@ int initFreenect() {
130 132
   //freenect_set_rgb_callback(f_dev, rgb_cb);
131 133
   //freenect_set_rgb_format(f_dev, FREENECT_FORMAT_RGB);
132 134
   freenect_set_video_callback(f_dev, rgb_cb);
133  
-  freenect_set_video_format(f_dev, FREENECT_VIDEO_RGB);
134  
-  freenect_set_depth_format(f_dev, FREENECT_DEPTH_11BIT);
  135
+  //freenect_set_video_format(f_dev, FREENECT_VIDEO_RGB);
  136
+  //freenect_set_depth_format(f_dev, FREENECT_DEPTH_11BIT);
  137
+  freenect_set_video_mode(f_dev, freenect_find_video_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_RGB));
  138
+  freenect_set_depth_mode(f_dev, freenect_find_depth_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_11BIT));
135 139
 
136 140
   freenect_start_depth(f_dev);
137 141
   freenect_start_video(f_dev);

0 notes on commit 76ff233

Please sign in to comment.
Something went wrong with that request. Please try again.