The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

874 lines
31KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. namespace
  22. {
  23. //==============================================================================
  24. /** Writes OSC data to an internal memory buffer, which grows as required.
  25. The data that was written into the stream can then be accessed later as
  26. a contiguous block of memory.
  27. This class implements the Open Sound Control 1.0 Specification for
  28. the format in which the OSC data will be written into the buffer.
  29. */
  30. struct OSCOutputStream
  31. {
  32. OSCOutputStream() noexcept {}
  33. /** Returns a pointer to the data that has been written to the stream. */
  34. const void* getData() const noexcept { return output.getData(); }
  35. /** Returns the number of bytes of data that have been written to the stream. */
  36. size_t getDataSize() const noexcept { return output.getDataSize(); }
  37. //==============================================================================
  38. bool writeInt32 (int32 value)
  39. {
  40. return output.writeIntBigEndian (value);
  41. }
  42. bool writeUint64 (uint64 value)
  43. {
  44. return output.writeInt64BigEndian (int64 (value));
  45. }
  46. bool writeFloat32 (float value)
  47. {
  48. return output.writeFloatBigEndian (value);
  49. }
  50. bool writeString (const String& value)
  51. {
  52. if (! output.writeString (value))
  53. return false;
  54. const size_t numPaddingZeros = ~value.length() & 3;
  55. return output.writeRepeatedByte ('\0', numPaddingZeros);
  56. }
  57. bool writeBlob (const MemoryBlock& blob)
  58. {
  59. if (! (output.writeIntBigEndian ((int) blob.getSize())
  60. && output.write (blob.getData(), blob.getSize())))
  61. return false;
  62. const size_t numPaddingZeros = ~(blob.getSize() - 1) & 3;
  63. return output.writeRepeatedByte (0, numPaddingZeros);
  64. }
  65. bool writeColour (OSCColour colour)
  66. {
  67. return output.writeIntBigEndian ((int32) colour.toInt32());
  68. }
  69. bool writeTimeTag (OSCTimeTag timeTag)
  70. {
  71. return output.writeInt64BigEndian (int64 (timeTag.getRawTimeTag()));
  72. }
  73. bool writeAddress (const OSCAddress& address)
  74. {
  75. return writeString (address.toString());
  76. }
  77. bool writeAddressPattern (const OSCAddressPattern& ap)
  78. {
  79. return writeString (ap.toString());
  80. }
  81. bool writeTypeTagString (const OSCTypeList& typeList)
  82. {
  83. output.writeByte (',');
  84. if (typeList.size() > 0)
  85. output.write (typeList.begin(), (size_t) typeList.size());
  86. output.writeByte ('\0');
  87. size_t bytesWritten = (size_t) typeList.size() + 1;
  88. size_t numPaddingZeros = ~bytesWritten & 0x03;
  89. return output.writeRepeatedByte ('\0', numPaddingZeros);
  90. }
  91. bool writeArgument (const OSCArgument& arg)
  92. {
  93. switch (arg.getType())
  94. {
  95. case OSCTypes::int32: return writeInt32 (arg.getInt32());
  96. case OSCTypes::float32: return writeFloat32 (arg.getFloat32());
  97. case OSCTypes::string: return writeString (arg.getString());
  98. case OSCTypes::blob: return writeBlob (arg.getBlob());
  99. case OSCTypes::colour: return writeColour (arg.getColour());
  100. default:
  101. // In this very unlikely case you supplied an invalid OSCType!
  102. jassertfalse;
  103. return false;
  104. }
  105. }
  106. //==============================================================================
  107. bool writeMessage (const OSCMessage& msg)
  108. {
  109. if (! writeAddressPattern (msg.getAddressPattern()))
  110. return false;
  111. OSCTypeList typeList;
  112. for (auto& arg : msg)
  113. typeList.add (arg.getType());
  114. if (! writeTypeTagString (typeList))
  115. return false;
  116. for (auto& arg : msg)
  117. if (! writeArgument (arg))
  118. return false;
  119. return true;
  120. }
  121. bool writeBundle (const OSCBundle& bundle)
  122. {
  123. if (! writeString ("#bundle"))
  124. return false;
  125. if (! writeTimeTag (bundle.getTimeTag()))
  126. return false;
  127. for (auto& element : bundle)
  128. if (! writeBundleElement (element))
  129. return false;
  130. return true;
  131. }
  132. //==============================================================================
  133. bool writeBundleElement (const OSCBundle::Element& element)
  134. {
  135. const int64 startPos = output.getPosition();
  136. if (! writeInt32 (0)) // writing dummy value for element size
  137. return false;
  138. if (element.isBundle())
  139. {
  140. if (! writeBundle (element.getBundle()))
  141. return false;
  142. }
  143. else
  144. {
  145. if (! writeMessage (element.getMessage()))
  146. return false;
  147. }
  148. const int64 endPos = output.getPosition();
  149. const int64 elementSize = endPos - (startPos + 4);
  150. return output.setPosition (startPos)
  151. && writeInt32 ((int32) elementSize)
  152. && output.setPosition (endPos);
  153. }
  154. private:
  155. MemoryOutputStream output;
  156. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OSCOutputStream)
  157. };
  158. } // namespace
  159. //==============================================================================
  160. struct OSCSender::Pimpl
  161. {
  162. Pimpl() noexcept {}
  163. ~Pimpl() noexcept { disconnect(); }
  164. //==============================================================================
  165. bool connect (const String& newTargetHost, int newTargetPort)
  166. {
  167. if (! disconnect())
  168. return false;
  169. socket.setOwned (new DatagramSocket (true));
  170. targetHostName = newTargetHost;
  171. targetPortNumber = newTargetPort;
  172. if (socket->bindToPort (0)) // 0 = use any local port assigned by the OS.
  173. return true;
  174. socket.reset();
  175. return false;
  176. }
  177. bool connectToSocket (DatagramSocket& newSocket, const String& newTargetHost, int newTargetPort)
  178. {
  179. if (! disconnect())
  180. return false;
  181. socket.setNonOwned (&newSocket);
  182. targetHostName = newTargetHost;
  183. targetPortNumber = newTargetPort;
  184. return true;
  185. }
  186. bool disconnect()
  187. {
  188. socket.reset();
  189. return true;
  190. }
  191. //==============================================================================
  192. bool send (const OSCMessage& message, const String& hostName, int portNumber)
  193. {
  194. OSCOutputStream outStream;
  195. return outStream.writeMessage (message)
  196. && sendOutputStream (outStream, hostName, portNumber);
  197. }
  198. bool send (const OSCBundle& bundle, const String& hostName, int portNumber)
  199. {
  200. OSCOutputStream outStream;
  201. return outStream.writeBundle (bundle)
  202. && sendOutputStream (outStream, hostName, portNumber);
  203. }
  204. bool send (const OSCMessage& message) { return send (message, targetHostName, targetPortNumber); }
  205. bool send (const OSCBundle& bundle) { return send (bundle, targetHostName, targetPortNumber); }
  206. private:
  207. //==============================================================================
  208. bool sendOutputStream (OSCOutputStream& outStream, const String& hostName, int portNumber)
  209. {
  210. if (socket != nullptr)
  211. {
  212. const int streamSize = (int) outStream.getDataSize();
  213. const int bytesWritten = socket->write (hostName, portNumber,
  214. outStream.getData(), streamSize);
  215. return bytesWritten == streamSize;
  216. }
  217. // if you hit this, you tried to send some OSC data without being
  218. // connected to a port! You should call OSCSender::connect() first.
  219. jassertfalse;
  220. return false;
  221. }
  222. //==============================================================================
  223. OptionalScopedPointer<DatagramSocket> socket;
  224. String targetHostName;
  225. int targetPortNumber = 0;
  226. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Pimpl)
  227. };
  228. //==============================================================================
  229. OSCSender::OSCSender() : pimpl (new Pimpl())
  230. {
  231. }
  232. OSCSender::~OSCSender()
  233. {
  234. pimpl->disconnect();
  235. pimpl.reset();
  236. }
  237. //==============================================================================
  238. bool OSCSender::connect (const String& targetHostName, int targetPortNumber)
  239. {
  240. return pimpl->connect (targetHostName, targetPortNumber);
  241. }
  242. bool OSCSender::connectToSocket (DatagramSocket& socket, const String& targetHostName, int targetPortNumber)
  243. {
  244. return pimpl->connectToSocket (socket, targetHostName, targetPortNumber);
  245. }
  246. bool OSCSender::disconnect()
  247. {
  248. return pimpl->disconnect();
  249. }
  250. //==============================================================================
  251. bool OSCSender::send (const OSCMessage& message) { return pimpl->send (message); }
  252. bool OSCSender::send (const OSCBundle& bundle) { return pimpl->send (bundle); }
  253. bool OSCSender::sendToIPAddress (const String& host, int port, const OSCMessage& message) { return pimpl->send (message, host, port); }
  254. bool OSCSender::sendToIPAddress (const String& host, int port, const OSCBundle& bundle) { return pimpl->send (bundle, host, port); }
  255. //==============================================================================
  256. //==============================================================================
  257. #if JUCE_UNIT_TESTS
  258. class OSCBinaryWriterTests : public UnitTest
  259. {
  260. public:
  261. OSCBinaryWriterTests() : UnitTest ("OSCBinaryWriter class", "OSC") {}
  262. void runTest()
  263. {
  264. beginTest ("writing OSC addresses");
  265. {
  266. OSCOutputStream outStream;
  267. const char check[16] = { '/', 't', 'e', 's', 't', '/', 'f', 'a', 'd', 'e', 'r', '7', '\0', '\0', '\0', '\0' };
  268. OSCAddress address ("/test/fader7");
  269. expect (outStream.writeAddress (address));
  270. expect (outStream.getDataSize() == sizeof (check));
  271. expect (std::memcmp (outStream.getData(), check, sizeof (check)) == 0);
  272. }
  273. beginTest ("writing OSC address patterns");
  274. {
  275. OSCOutputStream outStream;
  276. const char check[20] = { '/', '*', '/', '*', 'p', 'u', 't', '/', 'f', 'a', 'd', 'e', 'r', '[', '0', '-', '9', ']', '\0', '\0' };
  277. OSCAddressPattern ap ("/*/*put/fader[0-9]");
  278. expect (outStream.writeAddressPattern (ap));
  279. expect (outStream.getDataSize() == sizeof (check));
  280. expect (std::memcmp (outStream.getData(), check, sizeof (check)) == 0);
  281. }
  282. beginTest ("writing OSC time tags");
  283. {
  284. OSCOutputStream outStream;
  285. const char check[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };
  286. OSCTimeTag tag;
  287. expect (outStream.writeTimeTag (tag));
  288. expect (outStream.getDataSize() == 8);
  289. expect (std::memcmp (outStream.getData(), check, sizeof (check)) == 0);
  290. }
  291. beginTest ("writing OSC type tag strings");
  292. {
  293. {
  294. OSCOutputStream outStream;
  295. OSCTypeList list;
  296. const char check[4] = { ',', '\0', '\0', '\0' };
  297. expect (outStream.writeTypeTagString (list));
  298. expect (outStream.getDataSize() == 4);
  299. expect (std::memcmp (outStream.getData(), check, sizeof (check)) == 0);
  300. }
  301. {
  302. OSCOutputStream outStream;
  303. OSCTypeList list;
  304. list.add (OSCTypes::int32);
  305. list.add (OSCTypes::float32);
  306. const char check[4] = { ',', 'i', 'f', '\0' };
  307. expect (outStream.writeTypeTagString (list));
  308. expect (outStream.getDataSize() == sizeof (check));
  309. expect (std::memcmp (outStream.getData(), check, sizeof (check)) == 0);
  310. }
  311. {
  312. OSCOutputStream outStream;
  313. OSCTypeList list;
  314. list.add (OSCTypes::blob);
  315. list.add (OSCTypes::blob);
  316. list.add (OSCTypes::string);
  317. const char check[8] = { ',', 'b', 'b', 's', '\0', '\0', '\0', '\0' };
  318. expect (outStream.writeTypeTagString (list));
  319. expect (outStream.getDataSize() == sizeof (check));
  320. expect (std::memcmp (outStream.getData(), check, sizeof (check)) == 0);
  321. }
  322. }
  323. beginTest ("writing OSC arguments");
  324. {
  325. // test data:
  326. int testInt = -2015;
  327. const uint8 testIntRepresentation[] = { 0xFF, 0xFF, 0xF8, 0x21 }; // big endian two's complement
  328. float testFloat = 345.6125f;
  329. const uint8 testFloatRepresentation[] = { 0x43, 0xAC, 0xCE, 0x66 }; // big endian IEEE 754
  330. String testString = "Hello, World!";
  331. const char testStringRepresentation[] = { 'H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!', '\0', '\0', '\0' }; // padded to size % 4 == 0
  332. const uint8 testBlobData[] = { 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };
  333. const MemoryBlock testBlob (testBlobData, sizeof (testBlobData));
  334. const uint8 testBlobRepresentation[] = { 0x00, 0x00, 0x00, 0x05, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00, 0x00, 0x00 }; // padded to size % 4 == 0
  335. // write:
  336. {
  337. // int32:
  338. OSCArgument arg (testInt);
  339. OSCOutputStream outStream;
  340. expect (outStream.writeArgument (arg));
  341. expect (outStream.getDataSize() == 4);
  342. expect (std::memcmp (outStream.getData(), testIntRepresentation, sizeof (testIntRepresentation)) == 0);
  343. }
  344. {
  345. // float32:
  346. OSCArgument arg (testFloat);
  347. OSCOutputStream outStream;
  348. expect (outStream.writeArgument (arg));
  349. expect (outStream.getDataSize() == 4);
  350. expect (std::memcmp (outStream.getData(), testFloatRepresentation, sizeof (testFloatRepresentation)) == 0);
  351. }
  352. {
  353. // string:
  354. expect (testString.length() % 4 != 0); // check whether we actually cover padding
  355. expect (sizeof (testStringRepresentation) % 4 == 0);
  356. OSCArgument arg (testString);
  357. OSCOutputStream outStream;
  358. expect (outStream.writeArgument (arg));
  359. expect (outStream.getDataSize() == sizeof (testStringRepresentation));
  360. expect (std::memcmp (outStream.getData(), testStringRepresentation, sizeof (testStringRepresentation)) == 0);
  361. }
  362. {
  363. // blob:
  364. expect (testBlob.getSize() % 4 != 0); // check whether we actually cover padding
  365. expect (sizeof (testBlobRepresentation) % 4 == 0);
  366. OSCArgument arg (testBlob);
  367. OSCOutputStream outStream;
  368. expect (outStream.writeArgument (arg));
  369. expect (outStream.getDataSize() == sizeof (testBlobRepresentation));
  370. expect (std::memcmp (outStream.getData(), testBlobRepresentation, sizeof (testBlobRepresentation)) == 0);
  371. }
  372. }
  373. beginTest ("Writing strings with correct padding");
  374. {
  375. // the only OSC-specific thing to check is the correct number of padding zeros
  376. {
  377. OSCArgument with15Chars ("123456789012345");
  378. OSCOutputStream outStream;
  379. expect (outStream.writeArgument (with15Chars));
  380. expect (outStream.getDataSize() == 16);
  381. }
  382. {
  383. OSCArgument with16Chars ("1234567890123456");
  384. OSCOutputStream outStream;
  385. expect (outStream.writeArgument (with16Chars));
  386. expect (outStream.getDataSize() == 20);
  387. }
  388. {
  389. OSCArgument with17Chars ("12345678901234567");
  390. OSCOutputStream outStream;
  391. expect (outStream.writeArgument (with17Chars));
  392. expect (outStream.getDataSize() == 20);
  393. }
  394. {
  395. OSCArgument with18Chars ("123456789012345678");
  396. OSCOutputStream outStream;
  397. expect (outStream.writeArgument (with18Chars));
  398. expect (outStream.getDataSize() == 20);
  399. }
  400. {
  401. OSCArgument with19Chars ("1234567890123456789");
  402. OSCOutputStream outStream;
  403. expect (outStream.writeArgument (with19Chars));
  404. expect (outStream.getDataSize() == 20);
  405. }
  406. {
  407. OSCArgument with20Chars ("12345678901234567890");
  408. OSCOutputStream outStream;
  409. expect (outStream.writeArgument (with20Chars));
  410. expect (outStream.getDataSize() == 24);
  411. }
  412. }
  413. beginTest ("Writing blobs with correct padding");
  414. {
  415. const char buffer[20] = {};
  416. {
  417. OSCArgument with15Bytes (MemoryBlock (buffer, 15));
  418. OSCOutputStream outStream;
  419. expect (outStream.writeArgument (with15Bytes));
  420. expect (outStream.getDataSize() == 20);
  421. }
  422. {
  423. OSCArgument with16Bytes (MemoryBlock (buffer, 16));
  424. OSCOutputStream outStream;
  425. expect (outStream.writeArgument (with16Bytes));
  426. expect (outStream.getDataSize() == 20);
  427. }
  428. {
  429. OSCArgument with17Bytes (MemoryBlock (buffer, 17));
  430. OSCOutputStream outStream;
  431. expect (outStream.writeArgument (with17Bytes));
  432. expect (outStream.getDataSize() == 24);
  433. }
  434. {
  435. OSCArgument with18Bytes (MemoryBlock (buffer, 18));
  436. OSCOutputStream outStream;
  437. expect (outStream.writeArgument (with18Bytes));
  438. expect (outStream.getDataSize() == 24);
  439. }
  440. {
  441. OSCArgument with19Bytes (MemoryBlock (buffer, 19));
  442. OSCOutputStream outStream;
  443. expect (outStream.writeArgument (with19Bytes));
  444. expect (outStream.getDataSize() == 24);
  445. }
  446. {
  447. OSCArgument with20Bytes (MemoryBlock (buffer, 20));
  448. OSCOutputStream outStream;
  449. expect (outStream.writeArgument (with20Bytes));
  450. expect (outStream.getDataSize() == 24);
  451. }
  452. }
  453. beginTest ("Writing OSC messages.");
  454. {
  455. {
  456. int32 testInt = -2015;
  457. float testFloat = 345.6125f;
  458. String testString = "Hello, World!";
  459. const uint8 testBlobData[] = { 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };
  460. const MemoryBlock testBlob (testBlobData, sizeof (testBlobData));
  461. uint8 check[52] = { '/', 't', 'e', 's', 't', '\0', '\0', '\0',
  462. ',', 'i', 'f', 's', 'b', '\0', '\0', '\0',
  463. 0xFF, 0xFF, 0xF8, 0x21,
  464. 0x43, 0xAC, 0xCE, 0x66,
  465. 'H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!', '\0', '\0', '\0',
  466. 0x00, 0x00, 0x00, 0x05, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00, 0x00, 0x00
  467. };
  468. OSCOutputStream outStream;
  469. OSCMessage msg ("/test");
  470. msg.addInt32 (testInt);
  471. msg.addFloat32 (testFloat);
  472. msg.addString (testString);
  473. msg.addBlob (testBlob);
  474. expect (outStream.writeMessage (msg));
  475. expect (outStream.getDataSize() == sizeof (check));
  476. expect (std::memcmp (outStream.getData(), check, sizeof (check)) == 0);
  477. }
  478. }
  479. beginTest ("Writing OSC bundle.");
  480. {
  481. {
  482. int32 testInt = -2015;
  483. float testFloat = 345.6125f;
  484. String testString = "Hello, World!";
  485. const uint8 testBlobData[] = { 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };
  486. const MemoryBlock testBlob (testBlobData, sizeof (testBlobData));
  487. uint8 check[] = {
  488. '#', 'b', 'u', 'n', 'd', 'l', 'e', '\0',
  489. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  490. 0x00, 0x00, 0x00, 0x34,
  491. '/', 't', 'e', 's', 't', '/', '1', '\0',
  492. ',', 'i', 'f', 's', 'b', '\0', '\0', '\0',
  493. 0xFF, 0xFF, 0xF8, 0x21,
  494. 0x43, 0xAC, 0xCE, 0x66,
  495. 'H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!', '\0', '\0', '\0',
  496. 0x00, 0x00, 0x00, 0x05, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00, 0x00, 0x00,
  497. 0x00, 0x00, 0x00, 0x0C,
  498. '/', 't', 'e', 's', 't', '/', '2', '\0',
  499. ',', '\0', '\0', '\0',
  500. 0x00, 0x00, 0x00, 0x10,
  501. '#', 'b', 'u', 'n', 'd', 'l', 'e', '\0',
  502. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  503. };
  504. OSCOutputStream outStream;
  505. OSCBundle bundle;
  506. OSCMessage msg1 ("/test/1");
  507. msg1.addInt32 (testInt);
  508. msg1.addFloat32 (testFloat);
  509. msg1.addString (testString);
  510. msg1.addBlob (testBlob);
  511. bundle.addElement (msg1);
  512. OSCMessage msg2 ("/test/2");
  513. bundle.addElement (msg2);
  514. OSCBundle subBundle;
  515. bundle.addElement (subBundle);
  516. expect (outStream.writeBundle (bundle));
  517. expect (outStream.getDataSize() == sizeof (check));
  518. expect (std::memcmp (outStream.getData(), check, sizeof (check)) == 0);
  519. }
  520. }
  521. }
  522. };
  523. static OSCBinaryWriterTests OSCBinaryWriterUnitTests;
  524. //==============================================================================
  525. class OSCRoundTripTests : public UnitTest
  526. {
  527. public:
  528. OSCRoundTripTests() : UnitTest ("OSCRoundTripTests class", "OSC") {}
  529. void runTest()
  530. {
  531. beginTest ("Empty OSC message");
  532. {
  533. OSCMessage outMessage ("/test/empty");
  534. OSCOutputStream output;
  535. output.writeMessage (outMessage);
  536. OSCInputStream input (output.getData(), output.getDataSize());
  537. OSCMessage inMessage = input.readMessage();
  538. expectEquals (inMessage.size(), 0);
  539. }
  540. beginTest ("OSC message with single argument");
  541. {
  542. OSCMessage outMessage ("/test/one_arg", 42);
  543. OSCOutputStream output;
  544. output.writeMessage (outMessage);
  545. OSCInputStream input (output.getData(), output.getDataSize());
  546. OSCMessage inMessage = input.readMessage();
  547. expectEquals (inMessage.size(), 1);
  548. expectEquals (inMessage[0].getInt32(), 42);
  549. }
  550. beginTest ("OSC message with multiple arguments");
  551. {
  552. OSCMessage outMessage ("/test/four_args", 42, 0.5f, String ("foo"), String ("bar"));
  553. OSCOutputStream output;
  554. output.writeMessage (outMessage);
  555. OSCInputStream input (output.getData(), output.getDataSize());
  556. OSCMessage inMessage = input.readMessage();
  557. expectEquals (inMessage.size(), 4);
  558. expectEquals (inMessage[0].getInt32(), 42);
  559. expectEquals (inMessage[1].getFloat32(), 0.5f);
  560. expectEquals (inMessage[2].getString(), String ("foo"));
  561. expectEquals (inMessage[3].getString(), String ("bar"));
  562. }
  563. beginTest ("Empty OSC bundle");
  564. {
  565. OSCBundle outBundle;
  566. OSCOutputStream output;
  567. output.writeBundle (outBundle);
  568. OSCInputStream input (output.getData(), output.getDataSize());
  569. OSCBundle inBundle = input.readBundle();
  570. expectEquals (inBundle.size(), 0);
  571. }
  572. beginTest ("OSC bundle with single message");
  573. {
  574. OSCMessage outMessage ("/test/one_arg", 42);
  575. OSCBundle outBundle;
  576. outBundle.addElement (outMessage);
  577. OSCOutputStream output;
  578. output.writeBundle (outBundle);
  579. OSCInputStream input (output.getData(), output.getDataSize());
  580. OSCBundle inBundle = input.readBundle();
  581. expectEquals (inBundle.size(), 1);
  582. OSCMessage inMessage = inBundle[0].getMessage();
  583. expectEquals (inMessage.getAddressPattern().toString(), String ("/test/one_arg"));
  584. expectEquals (inMessage.size(), 1);
  585. expectEquals (inMessage[0].getInt32(), 42);
  586. }
  587. beginTest ("OSC bundle with multiple messages");
  588. {
  589. OSCMessage outMessage1 ("/test/empty");
  590. OSCMessage outMessage2 ("/test/one_arg", 42);
  591. OSCMessage outMessage3 ("/test/four_args", 42, 0.5f, String ("foo"), String ("bar"));
  592. OSCBundle outBundle;
  593. outBundle.addElement (outMessage1);
  594. outBundle.addElement (outMessage2);
  595. outBundle.addElement (outMessage3);
  596. OSCOutputStream output;
  597. output.writeBundle (outBundle);
  598. OSCInputStream input (output.getData(), output.getDataSize());
  599. OSCBundle inBundle = input.readBundle();
  600. expectEquals (inBundle.size(), 3);
  601. {
  602. OSCMessage inMessage = inBundle[0].getMessage();
  603. expectEquals (inMessage.getAddressPattern().toString(), String ("/test/empty"));
  604. expectEquals (inMessage.size(), 0);
  605. }
  606. {
  607. OSCMessage inMessage = inBundle[1].getMessage();
  608. expectEquals (inMessage.getAddressPattern().toString(), String ("/test/one_arg"));
  609. expectEquals (inMessage.size(), 1);
  610. expectEquals (inMessage[0].getInt32(), 42);
  611. }
  612. {
  613. OSCMessage inMessage = inBundle[2].getMessage();
  614. expectEquals (inMessage.getAddressPattern().toString(), String ("/test/four_args"));
  615. expectEquals (inMessage.size(), 4);
  616. expectEquals (inMessage[0].getInt32(), 42);
  617. expectEquals (inMessage[1].getFloat32(), 0.5f);
  618. expectEquals (inMessage[2].getString(), String ("foo"));
  619. expectEquals (inMessage[3].getString(), String ("bar"));
  620. }
  621. }
  622. beginTest ("OSC bundle containing another bundle");
  623. {
  624. OSCBundle outBundleNested;
  625. outBundleNested.addElement (OSCMessage ("/test/one_arg", 42));
  626. OSCBundle outBundle;
  627. outBundle.addElement (outBundleNested);
  628. OSCOutputStream output;
  629. output.writeBundle (outBundle);
  630. OSCInputStream input (output.getData(), output.getDataSize());
  631. OSCBundle inBundle = input.readBundle();
  632. expectEquals (inBundle.size(), 1);
  633. expect (inBundle[0].isBundle());
  634. OSCBundle inBundleNested = inBundle[0].getBundle();
  635. expectEquals (inBundleNested.size(), 1);
  636. expect (inBundleNested[0].isMessage());
  637. OSCMessage msg = inBundleNested[0].getMessage();
  638. expectEquals (msg.getAddressPattern().toString(), String ("/test/one_arg"));
  639. expectEquals (msg.size(), 1);
  640. expectEquals (msg[0].getInt32(), 42);
  641. }
  642. beginTest ("OSC bundle containing multiple other bundles");
  643. {
  644. OSCBundle outBundleNested1;
  645. outBundleNested1.addElement (OSCMessage ("/test/empty"));
  646. OSCBundle outBundleNested2;
  647. outBundleNested2.addElement (OSCMessage ("/test/one_arg", 42));
  648. OSCBundle outBundle;
  649. outBundle.addElement (outBundleNested1);
  650. outBundle.addElement (outBundleNested2);
  651. OSCOutputStream output;
  652. output.writeBundle (outBundle);
  653. OSCInputStream input (output.getData(), output.getDataSize());
  654. OSCBundle inBundle = input.readBundle();
  655. expectEquals (inBundle.size(), 2);
  656. {
  657. expect (inBundle[0].isBundle());
  658. OSCBundle inBundleNested = inBundle[0].getBundle();
  659. expectEquals (inBundleNested.size(), 1);
  660. expect (inBundleNested[0].isMessage());
  661. OSCMessage msg = inBundleNested[0].getMessage();
  662. expectEquals (msg.getAddressPattern().toString(), String ("/test/empty"));
  663. expectEquals (msg.size(), 0);
  664. }
  665. {
  666. expect (inBundle[1].isBundle());
  667. OSCBundle inBundleNested = inBundle[1].getBundle();
  668. expectEquals (inBundleNested.size(), 1);
  669. expect (inBundleNested[0].isMessage());
  670. OSCMessage msg = inBundleNested[0].getMessage();
  671. expectEquals (msg.getAddressPattern().toString(), String ("/test/one_arg"));
  672. expectEquals (msg.size(), 1);
  673. expectEquals (msg[0].getInt32(), 42);
  674. }
  675. }
  676. }
  677. };
  678. static OSCRoundTripTests OSCRoundTripUnitTests;
  679. //==============================================================================
  680. #endif // JUCE_UNIT_TESTS
  681. } // namespace juce