Skip to content

Commit 3f58e13

Browse files
committed
Merge pull request #38 from christiankerl/configurable_pipeline
make packet processing pipeline configurable
2 parents c22ded6 + e1a6dc2 commit 3f58e13

16 files changed

+313
-61
lines changed

examples/protonect/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ ADD_LIBRARY(freenect2 SHARED
7777

7878
src/double_buffer.cpp
7979
src/frame_listener_impl.cpp
80+
src/packet_pipeline.cpp
8081

8182
src/rgb_packet_stream_parser.cpp
8283
src/rgb_packet_processor.cpp

examples/protonect/include/libfreenect2/async_packet_processor.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,22 @@
2828
#define ASYNC_PACKET_PROCESSOR_H_
2929

3030
#include <libfreenect2/threading.h>
31+
#include <libfreenect2/packet_processor.h>
3132

3233
namespace libfreenect2
3334
{
3435

35-
template<typename PacketT, typename PacketProcessorT>
36-
class AsyncPacketProcessor
36+
template<typename PacketT>
37+
class AsyncPacketProcessor : public PacketProcessor<PacketT>
3738
{
3839
public:
39-
typedef PacketProcessorT* PacketProcessorPtr;
40+
typedef PacketProcessor<PacketT>* PacketProcessorPtr;
4041

4142
AsyncPacketProcessor(PacketProcessorPtr processor) :
4243
processor_(processor),
4344
current_packet_available_(false),
4445
shutdown_(false),
45-
thread_(&AsyncPacketProcessor<PacketT, PacketProcessorT>::static_execute, this)
46+
thread_(&AsyncPacketProcessor<PacketT>::static_execute, this)
4647
{
4748
}
4849
virtual ~AsyncPacketProcessor()
@@ -53,7 +54,7 @@ class AsyncPacketProcessor
5354
thread_.join();
5455
}
5556

56-
bool ready()
57+
virtual bool ready()
5758
{
5859
// try to aquire lock, if we succeed the background thread is waiting on packet_condition_
5960
bool locked = packet_mutex_.try_lock();
@@ -66,7 +67,7 @@ class AsyncPacketProcessor
6667
return locked;
6768
}
6869

69-
void process(const PacketT &packet)
70+
virtual void process(const PacketT &packet)
7071
{
7172
{
7273
libfreenect2::lock_guard l(packet_mutex_);
@@ -87,7 +88,7 @@ class AsyncPacketProcessor
8788

8889
static void static_execute(void *data)
8990
{
90-
static_cast<AsyncPacketProcessor<PacketT, PacketProcessorT> *>(data)->execute();
91+
static_cast<AsyncPacketProcessor<PacketT> *>(data)->execute();
9192
}
9293

9394
void execute()

examples/protonect/include/libfreenect2/depth_packet_processor.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <stdint.h>
3232

3333
#include <libfreenect2/frame_listener.hpp>
34+
#include <libfreenect2/packet_processor.h>
3435

3536
namespace libfreenect2
3637
{
@@ -42,7 +43,9 @@ struct DepthPacket
4243
size_t buffer_length;
4344
};
4445

45-
class DepthPacketProcessor
46+
typedef PacketProcessor<DepthPacket> BaseDepthPacketProcessor;
47+
48+
class DepthPacketProcessor : public BaseDepthPacketProcessor
4649
{
4750
public:
4851
struct Config
@@ -97,10 +100,8 @@ class DepthPacketProcessor
97100

98101
virtual void setFrameListener(libfreenect2::FrameListener *listener);
99102
virtual void setConfiguration(const libfreenect2::DepthPacketProcessor::Config &config);
100-
virtual void process(const DepthPacket &packet) = 0;
101103

102104
virtual void loadP0TablesFromCommandResponse(unsigned char* buffer, size_t buffer_length) = 0;
103-
104105
protected:
105106
libfreenect2::DepthPacketProcessor::Config config_;
106107
libfreenect2::FrameListener *listener_;

examples/protonect/include/libfreenect2/depth_packet_stream_parser.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232

3333
#include <libfreenect2/double_buffer.h>
3434
#include <libfreenect2/depth_packet_processor.h>
35-
#include <libfreenect2/async_packet_processor.h>
3635

3736
#include <libfreenect2/usb/transfer_pool.h>
3837

@@ -53,12 +52,14 @@ struct DepthSubPacketFooter
5352
class DepthPacketStreamParser : public libfreenect2::usb::TransferPool::DataReceivedCallback
5453
{
5554
public:
56-
DepthPacketStreamParser(libfreenect2::DepthPacketProcessor *processor);
55+
DepthPacketStreamParser();
5756
virtual ~DepthPacketStreamParser();
5857

58+
void setPacketProcessor(libfreenect2::BaseDepthPacketProcessor *processor);
59+
5960
virtual void onDataReceived(unsigned char* buffer, size_t length);
6061
private:
61-
libfreenect2::AsyncPacketProcessor<libfreenect2::DepthPacket, libfreenect2::DepthPacketProcessor> processor_;
62+
libfreenect2::BaseDepthPacketProcessor *processor_;
6263

6364
libfreenect2::DoubleBuffer buffer_;
6465
libfreenect2::Buffer work_buffer_;

examples/protonect/include/libfreenect2/libfreenect2.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
namespace libfreenect2
3333
{
3434

35+
class PacketPipeline;
36+
3537
class Freenect2Device
3638
{
3739
public:
@@ -80,11 +82,14 @@ class Freenect2
8082
std::string getDefaultDeviceSerialNumber();
8183

8284
Freenect2Device *openDevice(int idx);
85+
Freenect2Device *openDevice(int idx, const PacketPipeline *factory);
8386
Freenect2Device *openDevice(const std::string &serial);
87+
Freenect2Device *openDevice(const std::string &serial, const PacketPipeline *factory);
8488

8589
Freenect2Device *openDefaultDevice();
90+
Freenect2Device *openDefaultDevice(const PacketPipeline *factory);
8691
protected:
87-
Freenect2Device *openDevice(int idx, bool attempting_reset);
92+
Freenect2Device *openDevice(int idx, const PacketPipeline *factory, bool attempting_reset);
8893
private:
8994
Freenect2Impl *impl_;
9095
};
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* This file is part of the OpenKinect Project. http://www.openkinect.org
3+
*
4+
* Copyright (c) 2014 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+
#ifndef PACKET_PIPELINE_H_
28+
#define PACKET_PIPELINE_H_
29+
30+
#include <libfreenect2/usb/transfer_pool.h>
31+
#include <libfreenect2/rgb_packet_stream_parser.h>
32+
#include <libfreenect2/depth_packet_stream_parser.h>
33+
#include <libfreenect2/depth_packet_processor.h>
34+
#include <libfreenect2/rgb_packet_processor.h>
35+
36+
namespace libfreenect2
37+
{
38+
39+
class PacketPipeline
40+
{
41+
public:
42+
typedef libfreenect2::usb::TransferPool::DataReceivedCallback PacketParser;
43+
virtual ~PacketPipeline();
44+
45+
virtual PacketParser *getRgbPacketParser() const = 0;
46+
virtual PacketParser *getIrPacketParser() const = 0;
47+
48+
virtual RgbPacketProcessor *getRgbPacketProcessor() const = 0;
49+
virtual DepthPacketProcessor *getDepthPacketProcessor() const = 0;
50+
};
51+
52+
class DefaultPacketPipeline : public PacketPipeline
53+
{
54+
private:
55+
RgbPacketStreamParser *rgb_parser_;
56+
DepthPacketStreamParser *depth_parser_;
57+
58+
RgbPacketProcessor *rgb_processor_;
59+
BaseRgbPacketProcessor *async_rgb_processor_;
60+
DepthPacketProcessor *depth_processor_;
61+
BaseDepthPacketProcessor *async_depth_processor_;
62+
public:
63+
DefaultPacketPipeline();
64+
virtual ~DefaultPacketPipeline();
65+
66+
virtual PacketParser *getRgbPacketParser() const;
67+
virtual PacketParser *getIrPacketParser() const;
68+
69+
virtual RgbPacketProcessor *getRgbPacketProcessor() const;
70+
virtual DepthPacketProcessor *getDepthPacketProcessor() const;
71+
};
72+
73+
} /* namespace libfreenect2 */
74+
#endif /* PACKET_PIPELINE_H_ */
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* This file is part of the OpenKinect Project. http://www.openkinect.org
3+
*
4+
* Copyright (c) 2014 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+
#ifndef PACKET_PROCESSOR_H_
28+
#define PACKET_PROCESSOR_H_
29+
30+
namespace libfreenect2
31+
{
32+
33+
template<typename PacketT>
34+
class PacketProcessor
35+
{
36+
public:
37+
virtual ~PacketProcessor() {}
38+
39+
virtual bool ready() { return true; }
40+
virtual void process(const PacketT &packet) = 0;
41+
};
42+
43+
template<typename PacketT>
44+
class NoopPacketProcessor : public PacketProcessor<PacketT>
45+
{
46+
public:
47+
NoopPacketProcessor() {}
48+
virtual ~NoopPacketProcessor() {}
49+
50+
virtual void process(const PacketT &packet) {}
51+
};
52+
53+
template<typename PacketT>
54+
PacketProcessor<PacketT> *noopProcessor()
55+
{
56+
static NoopPacketProcessor<PacketT> noop_processor_;
57+
return &noop_processor_;
58+
}
59+
60+
} /* namespace libfreenect2 */
61+
#endif /* PACKET_PROCESSOR_H_ */

examples/protonect/include/libfreenect2/rgb_packet_processor.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <stdint.h>
3232

3333
#include <libfreenect2/frame_listener.hpp>
34+
#include <libfreenect2/packet_processor.h>
3435

3536
namespace libfreenect2
3637
{
@@ -43,15 +44,15 @@ struct RgbPacket
4344
size_t jpeg_buffer_length;
4445
};
4546

46-
class RgbPacketProcessor
47+
typedef PacketProcessor<RgbPacket> BaseRgbPacketProcessor;
48+
49+
class RgbPacketProcessor : public BaseRgbPacketProcessor
4750
{
4851
public:
4952
RgbPacketProcessor();
5053
virtual ~RgbPacketProcessor();
5154

5255
virtual void setFrameListener(libfreenect2::FrameListener *listener);
53-
virtual void process(const libfreenect2::RgbPacket &packet) = 0;
54-
5556
protected:
5657
libfreenect2::FrameListener *listener_;
5758
};

examples/protonect/include/libfreenect2/rgb_packet_stream_parser.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
#include <libfreenect2/double_buffer.h>
3333
#include <libfreenect2/rgb_packet_processor.h>
34-
#include <libfreenect2/async_packet_processor.h>
3534

3635
#include <libfreenect2/usb/transfer_pool.h>
3736

@@ -41,13 +40,15 @@ namespace libfreenect2
4140
class RgbPacketStreamParser : public libfreenect2::usb::TransferPool::DataReceivedCallback
4241
{
4342
public:
44-
RgbPacketStreamParser(libfreenect2::RgbPacketProcessor* processor);
43+
RgbPacketStreamParser();
4544
virtual ~RgbPacketStreamParser();
4645

46+
void setPacketProcessor(BaseRgbPacketProcessor *processor);
47+
4748
virtual void onDataReceived(unsigned char* buffer, size_t length);
4849
private:
4950
libfreenect2::DoubleBuffer buffer_;
50-
libfreenect2::AsyncPacketProcessor<libfreenect2::RgbPacket, libfreenect2::RgbPacketProcessor> processor_;
51+
BaseRgbPacketProcessor *processor_;
5152
};
5253

5354
} /* namespace libfreenect2 */

examples/protonect/src/depth_packet_processor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626

2727
#include <libfreenect2/depth_packet_processor.h>
28+
#include <libfreenect2/async_packet_processor.h>
2829

2930
namespace libfreenect2
3031
{
@@ -104,5 +105,4 @@ void DepthPacketProcessor::setFrameListener(libfreenect2::FrameListener *listene
104105
listener_ = listener;
105106
}
106107

107-
108108
} /* namespace libfreenect2 */

0 commit comments

Comments
 (0)