| 1 |
Welcome to the OSC Plugin walkthrough. This is a step by step guide on how to |
| 2 |
write a plugin for OLA, using the OSC (Open Sound Control) plugin as an |
| 3 |
example. The OSC plugin uses liblo (http://liblo.sourceforge.net/) to handle |
| 4 |
the OSC messages. |
| 5 |
|
| 6 |
This assumes you have a working knowledge of C++ including references and |
| 7 |
pointers as well as a basic understanding of the string, map and vector |
| 8 |
classes. Most of the documentation is inline with the code so please keep it up |
| 9 |
to date if you change anything. |
| 10 |
|
| 11 |
Before we begin you should do the following: |
| 12 |
* Read https://wiki.openlighting.org/index.php/OLA_developer_info which |
| 13 |
provides an overview of some of the classes we'll use. |
| 14 |
* Have an understanding of the map & vector classes from the STL - |
| 15 |
http://www.sgi.com/tech/stl/ or http://www.cplusplus.com/reference/stl/ . |
| 16 |
* Know something about the OSC specification: |
| 17 |
http://opensoundcontrol.org/spec-1_0 . You don't need to know the wire format |
| 18 |
for the OSC messages. but you should at least recognize OSC addresses and |
| 19 |
know the different data types in a message. |
| 20 |
* Look at the liblo site (http://liblo.sourceforge.net/ ) and read the |
| 21 |
examples. |
| 22 |
* Get familiar with the string class |
| 23 |
http://www.cplusplus.com/reference/string/string/, since we use that a bit. |
| 24 |
* Basic knowledge of shell scripting, which is used in the ./configure script. |
| 25 |
* Make sure you can complete a full OLA build & test cycle (make, make check). |
| 26 |
|
| 27 |
The initial commit of this plugin was Rev |
| 28 |
b9d6a47e9a2586d51dac521d7532fb11d76e0ae5. It's worth having that diff |
| 29 |
open as you step through the explanations. |
| 30 |
|
| 31 |
Let's get to it. If you make it all the way through this doc and I ever meet |
| 32 |
you in person I'll buy you a beer! |
| 33 |
|
| 34 |
|
| 35 |
Chapter 1, Build Configuration |
| 36 |
============================== |
| 37 |
|
| 38 |
Before we start writing code, we need to configure the build system. Start off |
| 39 |
by creating the plugins/osc directory. |
| 40 |
|
| 41 |
1.1 Configure script |
| 42 |
-------------------- |
| 43 |
|
| 44 |
Next we need to modify the ./configure script so that we only build the OSC |
| 45 |
plugin if liblo is installed. So that we don't break existing users of OLA, a |
| 46 |
system without liblo will just skip building the plugin, rather than causing a |
| 47 |
failure in the configure script. |
| 48 |
|
| 49 |
To be polite, we should also provide a ./configure flag to disable the osc |
| 50 |
plugin, even if liblo is installed. |
| 51 |
|
| 52 |
The ./configure is generated from configure.ac so that's where we'll do our |
| 53 |
changes. configure.ac is essentially shell script |
| 54 |
|
| 55 |
AC_ARG_ENABLE( |
| 56 |
[osc], |
| 57 |
AS_HELP_STRING([--disable-osc], |
| 58 |
[Disable the OSC plugin even if liblo exists])) |
| 59 |
|
| 60 |
These lines add a ./configure argument "--disable-osc". If this |
| 61 |
argument is provided, the variable $enable_liblo will be set to "no". |
| 62 |
|
| 63 |
have_liblo="no" |
| 64 |
|
| 65 |
This defaults the $have_liblo variable to "no". |
| 66 |
|
| 67 |
if test "${enable_osc}" != "no"; then |
| 68 |
PKG_CHECK_MODULES(liblo, [liblo >= 0.26], [have_liblo="yes"], [true]) |
| 69 |
fi |
| 70 |
|
| 71 |
If the enable_liblo variable does not equal "no" (i.e. --disable-osc) wasn't |
| 72 |
provided, we go ahead and run pkg-config to check that liblo is installed. The |
| 73 |
PKG_CHECK_MODULES() macro does the equivalent of: |
| 74 |
|
| 75 |
$ pkg-config --modversion liblo |
| 76 |
|
| 77 |
To ensure the version of liblo is >= 0.26, and if it's installed, it sets the |
| 78 |
have_liblo to "yes" and the automake variables liblo_CFLAGS and liblo_LIBS from |
| 79 |
the output of: |
| 80 |
|
| 81 |
$ pkg-config --cflags --libs liblo |
| 82 |
|
| 83 |
We'll use liblo_CFLAGS and liblo_LIBS in the Makefile. |
| 84 |
|
| 85 |
AM_CONDITIONAL(HAVE_LIBLO, test "${have_liblo}" = "yes") |
| 86 |
|
| 87 |
This sets the automake variable HAVE_LIBLO according to the value of |
| 88 |
have_liblo. This is what controls if we build the osc plugin. |
| 89 |
|
| 90 |
if test "${have_liblo}" = "yes"; then |
| 91 |
PLUGINS="${PLUGINS} osc" |
| 92 |
AC_DEFINE(HAVE_LIBLO, 1, [define if liblo is installed]) |
| 93 |
fi |
| 94 |
|
| 95 |
Finally if have_liblo is "yes", we append osc to the PLUGINS variable and |
| 96 |
set a #define HAVE_LIBLO 1. Variables specified in AC_DEFINE are written to |
| 97 |
the config.h file which can be included as a normal header file. This allows |
| 98 |
the plugin loader code to know whether to attempt to load the osc plugin. |
| 99 |
|
| 100 |
Finally don't forget to add plugins/osc/Makefile to the AC_OUTPUT section at |
| 101 |
the end. The tells autoconf to write a Makefile for the plugins/osc directory. |
| 102 |
|
| 103 |
To summarize, we know have the following: |
| 104 |
|
| 105 |
* The automake variable HAVE_LIBLO, which is true if we should build the OSC |
| 106 |
plugin. |
| 107 |
* The automake variables liblo_CFLAGS & liblo_LIBS, which contains the |
| 108 |
compiler and linker options required to use liblo. |
| 109 |
* A #define'd value HAVE_LIBLO which lets us know within the code if the OSC |
| 110 |
plugin was built. |
| 111 |
|
| 112 |
1.2 Makefiles |
| 113 |
------------- |
| 114 |
|
| 115 |
On to the Makefiles. Because we use autoconf / automake the Makefile is |
| 116 |
generated from Makefile.am. First we edit plugins/Makefile.am and add the osc |
| 117 |
directory to the SUBDIRS line. |
| 118 |
|
| 119 |
Now we can create a plugins/osc/Makefile.am. We'll cover this file line by |
| 120 |
line. |
| 121 |
|
| 122 |
include $(top_srcdir)/common.mk |
| 123 |
|
| 124 |
This does what you'd expect, it includes the top level common.mk file, which |
| 125 |
defines $(COMMON_CXXFLAGS) to include things like -Wall and -Werror. |
| 126 |
|
| 127 |
libdir = $(plugindir) |
| 128 |
|
| 129 |
This sets the destination install directory for the shared object (the OSC |
| 130 |
plugin in this case) to the plugin directory. By default that's |
| 131 |
/usr/local/lib/olad but the user may have changed it. |
| 132 |
|
| 133 |
EXTRA_DIST = OSCAddressTemplate.h OSCDevice.h OSCNode.h OSCPlugin.h OSCPort.h \ |
| 134 |
OSCTarget.h |
| 135 |
|
| 136 |
This specifies the header files that need to be included in the tarball. |
| 137 |
|
| 138 |
if HAVE_LIBLO |
| 139 |
|
| 140 |
Everything from this point onwards is only run if HAVE_LIBLO was true. |
| 141 |
|
| 142 |
noinst_LTLIBRARIES = libolaoscnode.la |
| 143 |
libolaoscnode_la_SOURCES = OSCAddressTemplate.cpp OSCNode.cpp |
| 144 |
libolaoscnode_la_CXXFLAGS = $(COMMON_CXXFLAGS) $(liblo_CFLAGS) |
| 145 |
libolaoscnode_la_LIBADD = $(liblo_LIBS) |
| 146 |
|
| 147 |
This sets up our first build target, a shared library called libolaoscnode.la. |
| 148 |
This library contains the OSCNode class, along with the functions in |
| 149 |
OSCAddressTemplate.cpp. The libolaoscnode.la is a helper library, it's not |
| 150 |
installed when the user runs `make install` (the noinst_ prefix does this) but |
| 151 |
it simplifies the build rules since both the OSC plugin and the tests depend |
| 152 |
on it. Because we depend on liblo we need to add liblo_CFLAGS and liblo_LIBS |
| 153 |
to the appropriate lines. |
| 154 |
|
| 155 |
lib_LTLIBRARIES = libolaosc.la |
| 156 |
libolaosc_la_SOURCES = OSCDevice.cpp OSCPlugin.cpp |
| 157 |
libolaosc_la_CXXFLAGS = $(COMMON_CXXFLAGS) $(liblo_CFLAGS) |
| 158 |
libolaosc_la_LIBADD = libolaoscnode.la |
| 159 |
|
| 160 |
Here is our llibolaosc.la plugin. This contains code from OSCDevice.cpp and |
| 161 |
OSCPlugin.cpp. Note that because the code in OSCPort is simple, all the code |
| 162 |
lives in OSCPort.h and there is no corresponding .cpp file. If there was we'd |
| 163 |
need to list it here as well. This depends on the libolaoscnode.la helper |
| 164 |
library. |
| 165 |
|
| 166 |
if BUILD_TESTS |
| 167 |
TESTS = OSCTester |
| 168 |
endif |
| 169 |
|
| 170 |
On to the tests, These three lines add the OSCTester program to the list of |
| 171 |
tests to execute when `make check` is run. If the user ran ./configure with |
| 172 |
--disable-unittests we don't build or run any of the unittesting code. |
| 173 |
|
| 174 |
check_PROGRAMS = $(TESTS) |
| 175 |
|
| 176 |
This sets check_PROGRAMS to what's contained in the $TESTS variable. |
| 177 |
|
| 178 |
OSCTester_SOURCES = OSCAddressTemplateTest.cpp \ |
| 179 |
OSCNodeTest.cpp \ |
| 180 |
OSCTester.cpp |
| 181 |
OSCTester_CXXFLAGS = $(COMMON_CXXFLAGS) $(CPPUNIT_CFLAGS) |
| 182 |
OSCTester_LDADD = $(CPPUNIT_LIBS) \ |
| 183 |
libolaoscnode.la \ |
| 184 |
../../common/libolacommon.la \ |
| 185 |
../../common/testing/libolatesting.la |
| 186 |
|
| 187 |
Here are the rules OSCTester. This specifies the 3 source files as well as the |
| 188 |
libraries the code depends on. |
| 189 |
|
| 190 |
endif |
| 191 |
|
| 192 |
This completes the "if HAVE_LIBLO" from above. |
| 193 |
|
| 194 |
1.3 Putting it together |
| 195 |
----------------------- |
| 196 |
|
| 197 |
At this point you can move back to the top level directory and run the |
| 198 |
following: |
| 199 |
|
| 200 |
autoreconf |
| 201 |
|
| 202 |
This creates the ./configure script, which we'll now run: |
| 203 |
|
| 204 |
./configure |
| 205 |
|
| 206 |
If you watch the output you should see the line "checking for liblo... yes". |
| 207 |
You should also see that HAVE_LIBLO is #defined to 1 in the config.h file and |
| 208 |
that the plugins/osc/Makefile has been created. |
| 209 |
|
| 210 |
That's it for the build configuration. Time to write some code! |
| 211 |
|
| 212 |
Chapter 2, Plugin Boilerplate |
| 213 |
============================= |
| 214 |
|
| 215 |
2.1 Plugin ID |
| 216 |
------------- |
| 217 |
|
| 218 |
We need to reserve a plugin ID in common/protocol/Ola.proto . Please see the |
| 219 |
note in that file regarding how we coordinate assigning plugin IDs. |
| 220 |
|
| 221 |
Once we've assigned a plugin id, we can run `make` in the top directory. This |
| 222 |
will update include/ola/plugin_id.h. You'll notice plugin_id.h isn't included |
| 223 |
in the git repo since it's a generated file. |
| 224 |
|
| 225 |
2.2 Plugin Loader |
| 226 |
----------------- |
| 227 |
|
| 228 |
We need to tell the Plugin Loader to load our new OSC plugin. Remember the |
| 229 |
HAVE_LIBLO variable in config.h? We'll make use of that now. |
| 230 |
|
| 231 |
In olad/DynamicPluginLoader.cpp we add: |
| 232 |
|
| 233 |
#ifdef HAVE_LIBLO |
| 234 |
#include "plugins/osc/OSCPlugin.h" |
| 235 |
#endif // HAVE_LIBLO |
| 236 |
|
| 237 |
DynamicPluginLoader.cpp includes config.h at the top of the file, which is how |
| 238 |
HAVE_LIBLO is defined. |
| 239 |
|
| 240 |
#ifdef HAVE_LIBLO |
| 241 |
m_plugins.push_back( |
| 242 |
new ola::plugin::osc::OSCPlugin(m_plugin_adaptor)); |
| 243 |
#endif // HAVE_LIBLO |
| 244 |
|
| 245 |
If HAVE_LIBLO is defined, we go ahead and add the OSCPlugin to the vector of |
| 246 |
plugins to load. |
| 247 |
|
| 248 |
That's it for the boilerplate |
| 249 |
|
| 250 |
|
| 251 |
Chapter 3, Helper Code |
| 252 |
====================== |
| 253 |
|
| 254 |
The OSC code is divided into two parts: |
| 255 |
|
| 256 |
- OSCNode is a C++ & DMX orientated wrapper around liblo. |
| 257 |
- OSCPlugin, OSCDevice & OSCPort are the glue between OLA and the OSCNode. |
| 258 |
|
| 259 |
There is a little bit of support code that is used in both parts, so we factor |
| 260 |
that out here. |
| 261 |
|
| 262 |
3.1 OSCTarget |
| 263 |
------------- |
| 264 |
|
| 265 |
OSCTarget.h contains a struct used to represent OSC targets. Each target has an |
| 266 |
IP & UDP Port, along with a string that holds the OSC address for the target. |
| 267 |
We also provide three constructors to help us create OSCTarget structs. The |
| 268 |
first is the default constructor, the second is the copy constructor and the |
| 269 |
third takes an IPV4SocketAddress and string and initializes the member |
| 270 |
variables. |
| 271 |
|
| 272 |
Notice in the second constructor we pass both the IPV4SocketAddress and string |
| 273 |
by (const) reference. If we passed by value (the default) we'd incur a copy of |
| 274 |
the IPV4SocketAddress / string class. A good string implementation this would |
| 275 |
avoid copying the actual string data and rather just copy the internal members |
| 276 |
of the string class. However even that is likely to be more expensive than |
| 277 |
taking a reference, so we pass strings by const reference wherever possible. |
| 278 |
|
| 279 |
You'll notice that all the OSC code is contained within the ola::plugin::osc |
| 280 |
namespace. This is to avoid conflicts with the core OLA classes and code in |
| 281 |
other plugins. |
| 282 |
|
| 283 |
3.2 ExpandTemplate() |
| 284 |
-------------------- |
| 285 |
|
| 286 |
OSCAddressTemplate.h provides a single method, ExpandTemplate() which behaves a |
| 287 |
bit like printf. Why don't we just use printf you ask? Because the strings to |
| 288 |
expand are provided in the config file by the user and using printf for user |
| 289 |
specified strings is dangerous (see |
| 290 |
http://www.cis.syr.edu/~wedu/Teaching/cis643/LectureNotes_New/Format_String.pdf). |
| 291 |
|
| 292 |
At this point it's worth mentioning testing. Have a look at the |
| 293 |
OSCAddressTemplateTest.cpp file. We define a single test method testExpand that |
| 294 |
confirms ExpandTemplate() behaves as we expect. |
| 295 |
|
| 296 |
If you're in the mood for experimentation, change |
| 297 |
|
| 298 |
output.find("%d"); |
| 299 |
|
| 300 |
to |
| 301 |
|
| 302 |
output.find_first_of("%d"); |
| 303 |
|
| 304 |
Watch the tests fail and make sure you understand why. |
| 305 |
|
| 306 |
|
| 307 |
Chapter 4, OSCNode |
| 308 |
================== |
| 309 |
|
| 310 |
As mentioned, OSCNode is the C++ wrapper around liblo. It uses the OLA |
| 311 |
network & IO classes so that it integrates nicely with OLA. As one would |
| 312 |
expect, the code is split across OSCNode.h and OSCNode.cpp |
| 313 |
|
| 314 |
4.1 OSCNode.h |
| 315 |
-------------- |
| 316 |
|
| 317 |
Lets step through the interface to the OSCNode class. First we include all the |
| 318 |
headers we're going to depend on. Since all our code exists in the |
| 319 |
ola::plugin::osc namespace, we need to either add "using ..." directives or |
| 320 |
fully qualify the data types in the rest of the file. Generally if I'm going to |
| 321 |
use a data type more than once in a file I'll typically add a using directive. |
| 322 |
|
| 323 |
Options Structure |
| 324 |
----------------- |
| 325 |
|
| 326 |
The OSCNode constructor takes a const reference to a OSCNodeOptions structure. |
| 327 |
Often the number of options passed to a constructor grows over the lifetime of |
| 328 |
the code. Consider a class Foo, which starts off with: |
| 329 |
|
| 330 |
Foo(const string &option1, int option2); |
| 331 |
|
| 332 |
Then at some later date, someone added another int and a bool option: |
| 333 |
|
| 334 |
Foo(const string &option1, int option2, int option3, bool option4); |
| 335 |
|
| 336 |
At this point you need to update all the caller of Foo(...). It gets even worse |
| 337 |
we people start adding default values: |
| 338 |
|
| 339 |
Foo(const string &option1, int option2, int option3, bool option4, |
| 340 |
bool option5 = false, bool option6 = true); |
| 341 |
|
| 342 |
Now if the caller wants to set option6 to false, they need to set option5 as |
| 343 |
well. This leads to buggy code. |
| 344 |
|
| 345 |
Passing in an options struct avoids this. The constructor can set the default |
| 346 |
values for all options and then the callers can override the options they are |
| 347 |
interested in. More options can then be added at a later stage without having |
| 348 |
to update all the callers. |
| 349 |
|
| 350 |
The downside to using an option struct is that mandatory options can be |
| 351 |
omitted, We could add a IsValid() method on the options struct to check for |
| 352 |
this, but it's not worth it in this case. |
| 353 |
|
| 354 |
Ok, back to the code, the next line typedefs a Callback1<void, const |
| 355 |
DmxBuffer&> to DMXCallback. This shortens the code a bit, shorter code is |
| 356 |
usually easier to understand. |
| 357 |
|
| 358 |
The OSCNode constructor takes a SelectServer, ExportMap as well as the |
| 359 |
previously described options struct. |
| 360 |
|
| 361 |
Methods |
| 362 |
------- |
| 363 |
|
| 364 |
The Init() and Stop() methods are self explanatory. |
| 365 |
|
| 366 |
Sending Data |
| 367 |
------------ |
| 368 |
|
| 369 |
The next three methods (AddTarget, RemoveTarget & SendData) are used for |
| 370 |
sending DMX data using OSC. To ease the burden on the caller, we group |
| 371 |
OSCTargets into groups (one target may exist in more than one group but that's |
| 372 |
rare). In OLA, each group can be thought of as a universe. |
| 373 |
|
| 374 |
Groups are identified by their group_id, which is just an integer. |
| 375 |
AddTarget(id, target) associates a target with a group. RemoveTarget does the |
| 376 |
opposite. SendData(id, dmx_data) sends the DMX data to all members of the |
| 377 |
group. |
| 378 |
|
| 379 |
Receiving Data |
| 380 |
-------------- |
| 381 |
|
| 382 |
The RegisterAddress() method is used for receiving DMX data over OSC. It takes |
| 383 |
an osc_address (e.g. /dmx/universe/1), and a callback. Whenever OSC data is |
| 384 |
received for the given address, the callback is invoked and passed the DMX |
| 385 |
data. |
| 386 |
|
| 387 |
When the callback argument to RegisterAddress() is NULL the osc_address is |
| 388 |
un-registered. |
| 389 |
|
| 390 |
Finally HandleDMXData() is a method called by liblo. |
| 391 |
|
| 392 |
Private Types, Variables & Methods |
| 393 |
---------------------------------- |
| 394 |
|
| 395 |
Let's go over the internal data members of the OSCNode class. |
| 396 |
|
| 397 |
NodeOSCTarget is derived from OSCTarget. It includes a lo_address member. We |
| 398 |
do this because liblo doesn't provide a way to create lo_addresses from a |
| 399 |
socket address, rather lo_address_new() takes strings, and then calls |
| 400 |
inet_aton to convert to the network data types. We don't want to incur this |
| 401 |
overhead ever time we send a packet, so we create the lo_address object once |
| 402 |
for each target in AddTarget(). |
| 403 |
|
| 404 |
Next we have three typedefs, which are used with our internal data structures. |
| 405 |
On the sending side, we need to track a collection of OSCTargets for each |
| 406 |
group. We do this with a map of vectors, so you can think of it like this: |
| 407 |
|
| 408 |
Group 1 -> |
| 409 |
[ target1, target2, target3 ], |
| 410 |
Group 2 -> |
| 411 |
[ target4 ] |
| 412 |
|
| 413 |
We could have used a set rather than a vector to hold the NodeOSCTarget |
| 414 |
objects, but then we'd need to define a < operator since the set container |
| 415 |
stores it's elements in sorted order. Using a set would also require us to |
| 416 |
store the objects directly rather than pointers to the objects, This would |
| 417 |
incur additional copying when elements are inserted into the set but since we |
| 418 |
only do this once when the plugin starts and the number of elements (OSC |
| 419 |
Targets) is small it wouldn't matter. |
| 420 |
|
| 421 |
We typedef a vector of pointers to NodeOSCTargets to OSCTargetVector, and then |
| 422 |
typedef a map of unsigned ints to pointers to OSCTargetVectors as a |
| 423 |
OSCTargetMap. |
| 424 |
|
| 425 |
On the receiving side, we have a map of OSC addresses to callbacks: |
| 426 |
|
| 427 |
"/dmx/universe/8" -> |
| 428 |
<Callback Object> |
| 429 |
"/dmx/universe/9 -> |
| 430 |
<Callback Object> |
| 431 |
|
| 432 |
We typedef a map of strings to callback objects to AddressCallbackMap. |
| 433 |
|
| 434 |
That's it for the types, the members are documented in the OSCNode.h file |
| 435 |
itself. We store pointers to the SelectServer and ExportMap objects passed in |
| 436 |
to the constructor as well as the UDP port to listen on. |
| 437 |
|
| 438 |
We also hold a pointer to a UnmanagedFileDescriptor which is how we add the |
| 439 |
socket description used by liblo to the SelectServer. There is also the |
| 440 |
lo_server data, as well as the OSCTargetMap and AddressCallbackMap discussed |
| 441 |
above. On Windows, we need to create an UnmanagedSocketDescriptor instead. |
| 442 |
|
| 443 |
The DescriptorReady method is called when the liblo socket description has data |
| 444 |
pending. |
| 445 |
|
| 446 |
Finally there are two static variables, DEFAULT_OSC_PORT which is the default |
| 447 |
UDP port to listen on and OSC_PORT_VARIABLE, which is what we use with the |
| 448 |
ExportMap. |
| 449 |
|
| 450 |
4.2 OSCNode.cpp |
| 451 |
--------------- |
| 452 |
|
| 453 |
Onwards to the implementation! Most of this is documented in the file itself so |
| 454 |
I'll just point out the interesting bits. |
| 455 |
|
| 456 |
void OSCErrorHandler(int error_code, const char *msg, const char *stack) |
| 457 |
|
| 458 |
The call to lo_server_new_with_proto() takes a pointer to a function which is |
| 459 |
called when liblo wants to log an error message. We pass a pointer to |
| 460 |
OSCErrorHandler which uses the OLA logging mechanism. |
| 461 |
|
| 462 |
OSCDataHandler is what liblo calls when there is new OSC data. The last |
| 463 |
argument is a pointer back to the OSCNode object, so after we perform some |
| 464 |
checks we call HandleDMXData(). We return 0 so that liblo doesn't try to find |
| 465 |
other OSC handlers to process the same data (see the liblo API for more info on |
| 466 |
this). |
| 467 |
|
| 468 |
In the OSCNode constructor, if an ExportMap was provided, we export the UDP |
| 469 |
port that liblo is listening on. If olad is built with the web server then |
| 470 |
users can navigate to http://<ip>:9090/debug and see the internal state of the |
| 471 |
server. The ExportMap provides an easy method to export this internal state, |
| 472 |
and we should really make more use of it since it's incredibly valuable for |
| 473 |
debugging. |
| 474 |
|
| 475 |
The destructor just calls Stop(). Note that Stop() may have been called |
| 476 |
previously, so we need to account for that. |
| 477 |
|
| 478 |
The Init method sets up the node. It creates a new lo_server and associates the |
| 479 |
socket description with the SelectServer. |
| 480 |
|
| 481 |
Stop deletes any of the entries in the OSCTargetMap and AddressCallbackMap, |
| 482 |
cleans up the socket description and frees the lo_server. It's important to |
| 483 |
clear both maps once the entries have been deleted since Stop() may be called |
| 484 |
more than once. Similarly, once the UnmanagedFileDescriptor and lo_server have |
| 485 |
been freed we set both of them to NULL. |
| 486 |
|
| 487 |
SendData() packs the DmxBuffer into a lo_blob variable. We use OSC blobs for |
| 488 |
transporting DMX over OSC. It then calls lo_send_from for each target in the |
| 489 |
group. |
| 490 |
|
| 491 |
|
| 492 |
Chapter 5, Testing |
| 493 |
================== |
| 494 |
|
| 495 |
Congratulations, at this point we're more than half way through. Let's take |
| 496 |
a detour and discuss about testing. |
| 497 |
|
| 498 |
All good code comes with tests and OLA is no exception. Well written tests |
| 499 |
increases the chance of having your plugin accepted into OLA. The |
| 500 |
OSCNodeTest.cpp contains the unittests for the OSCNode class. |
| 501 |
|
| 502 |
To test the node, we create a second UDP socket (liblo creates the first one) |
| 503 |
which we then use to send and receive OSC messages. For each test we register a |
| 504 |
timeout which aborts the test after ABORT_TIMEOUT_IN_MS. This prevents the test |
| 505 |
from hanging if the packets are dropped (which they shouldn't be since it's all |
| 506 |
localhost communication). |
| 507 |
|
| 508 |
In this test code we just use OLA_ASSERT_TRUE. ola/testing/TestUtils.h provides |
| 509 |
many more testing macros so take a look. |
| 510 |
|
| 511 |
Finally we need the OSCTester.cpp to pull it all together. This is just boiler |
| 512 |
plate code that sets up the unittests. |
| 513 |
|
| 514 |
|
| 515 |
Chapter 6, Plugins, Devices & Ports |
| 516 |
=================================== |
| 517 |
|
| 518 |
Now it's just a matter of building the OLA classes around the OSCNode. In OLA a |
| 519 |
Plugin creates objects of the Device type (if you use Java or other Object |
| 520 |
Orientated languages think of the Plugin class as a factory). Each device |
| 521 |
then has one of more input and output ports. For the OSC plugin, we allow the |
| 522 |
user to specify the number of input and output ports to create in the config |
| 523 |
file. |
| 524 |
|
| 525 |
Each input port is assigned an OSC address (e.g. |
| 526 |
/dmx/universe or /foo/bar). If '%d' occurs in the OSC address it's replaced by |
| 527 |
the current universe number. The default OSC address for each port is |
| 528 |
"/dmx/universe/%d" so the default behavior is sane. |
| 529 |
|
| 530 |
Each output port is configured with a list of OSC Targets in the form |
| 531 |
ip:port/address. |
| 532 |
|
| 533 |
6.1 Plugin |
| 534 |
---------- |
| 535 |
|
| 536 |
The OSCPlugin is pretty simple. like all OLA plugins, it inherits from the base |
| 537 |
ola::Plugin class. It's passed a pointer to a PluginAdaptor object, which is |
| 538 |
how the plugins interface with the rest of OLA. |
| 539 |
|
| 540 |
We'll cover the methods in a different order from which they appear in the |
| 541 |
file, since I think it makes more sense. |
| 542 |
|
| 543 |
Description() just returns a string which contains a description for the |
| 544 |
Plugin. This is what appears in the output of ola_plugin_info or on the web |
| 545 |
page under the 'Plugins' menu. It should explain what the plugin does, as well |
| 546 |
as the configuration options available. |
| 547 |
|
| 548 |
SetDefaultPreferences() is used to initialize the config file. It should check |
| 549 |
if configuration options exist, and if they don't reset them to default values. |
| 550 |
We use a bool to keep track if any of the options have changed, since if they |
| 551 |
do we need to write out the changes. |
| 552 |
|
| 553 |
Once SetDefaultPreferences() is called, the next method to run is StartHook(). |
| 554 |
This reads the configuration options, extracts the OSCTargets from the string |
| 555 |
(by calling ExtractOSCTarget()) and adds them to a vector. |
| 556 |
|
| 557 |
Once the configuration options have been processed we create a new OSCDevice, |
| 558 |
call Start() and register it with OLA. |
| 559 |
|
| 560 |
The StopHook() method unregisters the device and deletes it. |
| 561 |
|
| 562 |
GetPortCount() is a helper method to save duplicating similar code more than |
| 563 |
once. |
| 564 |
|
| 565 |
ExtractOSCTarget unpacks a string in the form "ip:port/address" into a |
| 566 |
OSCTarget structure. |
| 567 |
|
| 568 |
6.2 Device |
| 569 |
---------- |
| 570 |
|
| 571 |
Like the Plugin, the device is also pretty simple. Each OSCDevice owns a |
| 572 |
OSCNode object. In the constructor, the OSCDevice takes a pointer to the plugin |
| 573 |
which created the device, as well as the UDP port to listen on, and a list of |
| 574 |
addresses (for the input ports) and targets (for the output ports). These data |
| 575 |
structures are saved as member variables since they are required in StartHook(). |
| 576 |
|
| 577 |
The OSCDevice uses the static ID of 1, which means we can only ever have one |
| 578 |
OSCDevice. This is probably fine unless people are doing fancy things with |
| 579 |
multiple network interfaces. |
| 580 |
|
| 581 |
AllowMultiPortPatching() returns true, which means that multiple ports of the |
| 582 |
same type can be patched to the same universe. This is fine because the |
| 583 |
RegisterAddress method in OSCNode detects if we try to register the same |
| 584 |
address more than once. |
| 585 |
|
| 586 |
The only interesting method is StartHook(). This calls Init on the OSCNode and |
| 587 |
then sets up the ports. We call Init() from within StartHook() rather than in |
| 588 |
the constructor so that we can propagate a failure up the call change |
| 589 |
(constructors don't have return values so there is no way to notify the caller |
| 590 |
of an error). In general it's best to avoid any code that may fail in a |
| 591 |
constructor. |
| 592 |
|
| 593 |
StartHook iterates over the vector of addresses and creates an OSCInputPort for |
| 594 |
each. Similarly it iterates over the vector of vectors contains OSCTargets and |
| 595 |
sets up an OSCOutputPort for each. |
| 596 |
|
| 597 |
For both the input and output ports we |
| 598 |
create a description string which shows up in ola_dev_info and on the web UI. |
| 599 |
For the input port this is the OSC address they are listening on, for the |
| 600 |
output ports it's the list of OSC targets it's sending to. |
| 601 |
|
| 602 |
6.3 Ports |
| 603 |
--------- |
| 604 |
|
| 605 |
Since there isn't much code for the Port classes I put it all in OSCPort.h |
| 606 |
rather than splitting over two files. |
| 607 |
|
| 608 |
The OSCInputPort is the more complex of the two. Firstly the PreSetUniverse() |
| 609 |
method is called, which is where we Register a callback with the OSCNode (if |
| 610 |
needed). If the registration fails (say another port has the same address) we |
| 611 |
return false, and OLA informs the user that patching failed. |
| 612 |
|
| 613 |
We also need to handle the unpatch case, where the new_universe argument will |
| 614 |
be NULL. In this case we call RegisterAddress and pass a NULL as the callback |
| 615 |
argument which unregisters the address. |
| 616 |
|
| 617 |
The callback passed to RegisterAddress() runs the NewDMXData() method, which |
| 618 |
copies the DmxBuffer values into the port's internal buffer and calls |
| 619 |
DmxChanged(). This notifies the universe that the port is bound to that new |
| 620 |
data has arrived. The universe then calls ReadDMX() to collect the new data. |
| 621 |
|
| 622 |
OSCOutput port is much more simple, when the WriteDMX() method is called we call |
| 623 |
SendData() on the OSCNode object. Note that our simple DMX over OSC format has |
| 624 |
no notion of priority, so we ignore the priority argument. |
| 625 |
|
| 626 |
|
| 627 |
Chapter 7, Wrapping Up |
| 628 |
====================== |
| 629 |
|
| 630 |
That's it for the code. At this point you can change back to the top level |
| 631 |
directory and run `make` to build the new plugin. Running `make check` will run |
| 632 |
all the unittests. |
| 633 |
|
| 634 |
While you're developing, you'll find it quicker to run `make` and `make check` |
| 635 |
within the directory you're changing. Just remember to run it at the top level |
| 636 |
every so often or before trying to run olad. |
| 637 |
|
| 638 |
Chapter 8, Final Words |
| 639 |
====================== |
| 640 |
|
| 641 |
8.1 Code Style |
| 642 |
-------------- |
| 643 |
|
| 644 |
Just a couple more points to wrap things up. As mentioned into |
| 645 |
README.developer, OLA uses the Google C++ style guide |
| 646 |
(http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml) |
| 647 |
There is a tool, cpplint.py which will check the code for style violations. See |
| 648 |
README.developer for more information. |
| 649 |
|
| 650 |
8.2 Memory Leaks |
| 651 |
---------------- |
| 652 |
|
| 653 |
Valgrind is an excellent tool for discovering and debugging memory leaks. You |
| 654 |
can find out more at http://valgrind.org/. |