jack2 codebase
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.

898 lines
31KB

  1. /*
  2. Copyright (C) 2008-2011 Romain Moret at Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #include "JackMidiPort.h"
  16. #include "JackTools.h"
  17. #include "JackPlatformPlug.h"
  18. #include "types.h"
  19. #include "transport.h"
  20. #ifndef WIN32
  21. #include <netinet/in.h>
  22. #endif
  23. #include <cmath>
  24. using namespace std;
  25. #ifndef htonll
  26. #ifdef __BIG_ENDIAN__
  27. #define htonll(x) (x)
  28. #define ntohll(x) (x)
  29. #else
  30. #define htonll(x) ((((uint64_t)htonl(x)) << 32) + htonl(x >> 32))
  31. #define ntohll(x) ((((uint64_t)ntohl(x)) << 32) + ntohl(x >> 32))
  32. #endif
  33. #endif
  34. #define MASTER_PROTOCOL 4
  35. #define SLAVE_PROTOCOL 4
  36. #define NET_PACKET_ERROR -2
  37. #define OPTIMIZED_PROTOCOL
  38. #define HEADER_SIZE (sizeof(packet_header_t))
  39. #define PACKET_AVAILABLE_SIZE(params) ((params)->fMtu - sizeof(packet_header_t))
  40. namespace Jack
  41. {
  42. typedef struct _session_params session_params_t;
  43. typedef struct _packet_header packet_header_t;
  44. typedef struct _net_transport_data net_transport_data_t;
  45. typedef struct sockaddr socket_address_t;
  46. typedef struct in_addr address_t;
  47. typedef jack_default_audio_sample_t sample_t;
  48. enum JackNetEncoder {
  49. JackFloatEncoder = 0,
  50. JackIntEncoder = 1,
  51. JackCeltEncoder = 2,
  52. };
  53. //session params ******************************************************************************
  54. /**
  55. \brief This structure containes master/slave connection parameters, it's used to setup the whole system
  56. We have :
  57. - some info like version, type and packet id
  58. - names
  59. - network parameters (hostnames and mtu)
  60. - nunber of audio and midi channels
  61. - sample rate and buffersize
  62. - number of audio frames in one network packet (depends on the channel number)
  63. - is the NetDriver in Sync or ASync mode ?
  64. - is the NetDriver linked with the master's transport
  65. Data encoding : headers (session_params and packet_header) are encoded using HTN kind of functions but float data
  66. are kept in LITTLE_ENDIAN format (to avoid 2 conversions in the more common LITTLE_ENDIAN <==> LITTLE_ENDIAN connection case).
  67. */
  68. struct _session_params
  69. {
  70. char fPacketType[7]; //packet type ('param')
  71. char fProtocolVersion; //version
  72. uint32_t fPacketID; //indicates the packet type
  73. char fName[JACK_CLIENT_NAME_SIZE]; //slave's name
  74. char fMasterNetName[256]; //master hostname (network)
  75. char fSlaveNetName[256]; //slave hostname (network)
  76. uint32_t fMtu; //connection mtu
  77. uint32_t fID; //slave's ID
  78. uint32_t fTransportSync; //is the transport synced ?
  79. int32_t fSendAudioChannels; //number of master->slave channels
  80. int32_t fReturnAudioChannels; //number of slave->master channels
  81. int32_t fSendMidiChannels; //number of master->slave midi channels
  82. int32_t fReturnMidiChannels; //number of slave->master midi channels
  83. uint32_t fSampleRate; //session sample rate
  84. uint32_t fPeriodSize; //period size
  85. uint32_t fSampleEncoder; //samples encoder
  86. uint32_t fKBps; //KB per second for CELT encoder
  87. uint32_t fSlaveSyncMode; //is the slave in sync mode ?
  88. uint32_t fNetworkLatency; //network latency
  89. };
  90. //net status **********************************************************************************
  91. /**
  92. \Brief This enum groups network error by type
  93. */
  94. enum _net_status
  95. {
  96. NET_SOCKET_ERROR = 0,
  97. NET_CONNECT_ERROR,
  98. NET_ERROR,
  99. NET_SEND_ERROR,
  100. NET_RECV_ERROR,
  101. NET_CONNECTED,
  102. NET_ROLLING
  103. };
  104. typedef enum _net_status net_status_t;
  105. //sync packet type ****************************************************************************
  106. /**
  107. \Brief This enum indicates the type of a sync packet (used in the initialization phase)
  108. */
  109. enum _sync_packet_type
  110. {
  111. INVALID = 0, //...
  112. SLAVE_AVAILABLE, //a slave is available
  113. SLAVE_SETUP, //slave configuration
  114. START_MASTER, //slave is ready, start master
  115. START_SLAVE, //master is ready, activate slave
  116. KILL_MASTER //master must stop
  117. };
  118. typedef enum _sync_packet_type sync_packet_type_t;
  119. //packet header *******************************************************************************
  120. /**
  121. \Brief This structure is a complete header
  122. A header indicates :
  123. - it is a header
  124. - the type of data the packet contains (sync, midi or audio)
  125. - the path of the packet (send -master->slave- or return -slave->master-)
  126. - the unique ID of the slave
  127. - the sample's bitdepth (unused for now)
  128. - the size of the midi data contains in the packet (indicates how much midi data will be sent)
  129. - the number of midi packet(s) : more than one is very unusual, it depends on the midi load
  130. - the ID of the current cycle (used to check missing packets)
  131. - the ID of the packet subcycle (for audio data)
  132. - a flag indicating this packet is the last of the cycle (for sync robustness, it's better to process this way)
  133. - a flag indicating if, in async mode, the previous graph was not finished or not
  134. - padding to fill 64 bytes
  135. */
  136. struct _packet_header
  137. {
  138. char fPacketType[7]; //packet type ('headr')
  139. char fDataType; //a for audio, m for midi and s for sync
  140. char fDataStream; //s for send, r for return
  141. uint32_t fID; //unique ID of the slave
  142. uint32_t fNumPacket; //number of data packets of the cycle
  143. uint32_t fPacketSize; //packet size in bytes
  144. uint32_t fActivePorts; //number of active ports
  145. uint32_t fCycle; //process cycle counter
  146. uint32_t fSubCycle; //midi/audio subcycle counter
  147. uint32_t fIsLastPckt; //is it the last packet of a given cycle ('y' or 'n')
  148. };
  149. //net timebase master
  150. /**
  151. \Brief This enum describes timebase master's type
  152. */
  153. enum _net_timebase_master
  154. {
  155. NO_CHANGE = 0,
  156. RELEASE_TIMEBASEMASTER = 1,
  157. TIMEBASEMASTER = 2,
  158. CONDITIONAL_TIMEBASEMASTER = 3
  159. };
  160. typedef enum _net_timebase_master net_timebase_master_t;
  161. //transport data ******************************************************************************
  162. /**
  163. \Brief This structure contains transport data to be sent over the network
  164. */
  165. struct _net_transport_data
  166. {
  167. uint32_t fNewState; //is it a state change
  168. uint32_t fTimebaseMaster; //is there a new timebase master
  169. int32_t fState; //current cycle state
  170. jack_position_t fPosition; //current cycle position
  171. };
  172. //midi data ***********************************************************************************
  173. /**
  174. \Brief Midi buffer and operations class
  175. This class is a toolset to manipulate Midi buffers.
  176. A JackMidiBuffer has a fixed size, which is the same than an audio buffer size.
  177. An intermediate fixed size buffer allows to uninterleave midi data (from jack ports).
  178. But for a big majority of the process cycles, this buffer is filled less than 1%,
  179. Sending over a network 99% of useless data seems completely unappropriate.
  180. The idea is to count effective midi data, and then send the smallest packet we can.
  181. To do it, we use an intermediate buffer.
  182. We have two methods to convert data from jack ports to intermediate buffer,
  183. And two others to convert this intermediate buffer to a network buffer (header + payload data)
  184. */
  185. class SERVER_EXPORT NetMidiBuffer
  186. {
  187. private:
  188. int fNPorts;
  189. size_t fMaxBufsize;
  190. int fMaxPcktSize;
  191. char* fBuffer;
  192. char* fNetBuffer;
  193. JackMidiBuffer** fPortBuffer;
  194. size_t fCycleSize; // needed size in bytes ofr an entire cycle
  195. public:
  196. NetMidiBuffer(session_params_t* params, uint32_t nports, char* net_buffer);
  197. ~NetMidiBuffer();
  198. void Reset();
  199. // needed size in bytes for an entire cycle
  200. size_t GetCycleSize();
  201. int GetNumPackets(int data_sizen, int max_size);
  202. //utility
  203. void DisplayEvents();
  204. //jack<->buffer
  205. int RenderFromJackPorts();
  206. void RenderToJackPorts();
  207. //network<->buffer
  208. void RenderFromNetwork(int sub_cycle, size_t copy_size);
  209. int RenderToNetwork(int sub_cycle, size_t total_size);
  210. void SetBuffer(int index, JackMidiBuffer* buffer);
  211. JackMidiBuffer* GetBuffer(int index);
  212. };
  213. // audio data *********************************************************************************
  214. class SERVER_EXPORT NetAudioBuffer
  215. {
  216. protected:
  217. int fNPorts;
  218. int fLastSubCycle;
  219. char* fNetBuffer;
  220. sample_t** fPortBuffer;
  221. public:
  222. NetAudioBuffer()
  223. {}
  224. virtual ~NetAudioBuffer()
  225. {}
  226. // needed syze in bytes ofr an entire cycle
  227. virtual size_t GetCycleSize() = 0;
  228. // cycle duration in sec
  229. virtual float GetCycleDuration() = 0;
  230. virtual int GetNumPackets() = 0;
  231. //jack<->buffer
  232. virtual void RenderFromJackPorts() = 0;
  233. virtual void RenderToJackPorts() = 0;
  234. //network<->buffer
  235. virtual int RenderFromNetwork(int cycle, int sub_cycle, size_t copy_size, uint32_t port_num) = 0;
  236. virtual void ActivePortsFromNetwork(char* net_buffer, uint32_t port_num) {}
  237. virtual int RenderToNetwork(int sub_cycle, uint32_t& port_num) = 0;
  238. virtual void ActivePortsToNetwork(char* net_buffer, uint32_t& port_num) {}
  239. virtual void SetBuffer(int index, sample_t* buffer) = 0;
  240. virtual sample_t* GetBuffer(int index) = 0;
  241. virtual void ActivePortsToNetwork(uint32_t& port_num);
  242. virtual void ActivePortsFromNetwork(uint32_t port_num);
  243. };
  244. /**
  245. \Brief Audio buffer and operations class
  246. This class is a toolset to manipulate audio buffers.
  247. The manipulation of audio buffers is similar to midi buffer, except those buffers have fixed size.
  248. The interleaving/uninterleaving operations are simplier here because audio buffers have fixed size,
  249. So there is no need of an intermediate buffer as in NetMidiBuffer.
  250. */
  251. struct JackPortList {
  252. // "[---Header---|--audio data--|--audio data--]..."
  253. jack_nframes_t fPeriodSize;
  254. jack_nframes_t fSubPeriodSize;
  255. size_t fSubPeriodBytesSize;
  256. sample_t** fPortBuffer;
  257. int fPacketSize;
  258. int fNPorts;
  259. size_t fCycleSize; // needed size in bytes for an entire cycle
  260. float fCycleDuration; // in sec
  261. int fLastSubCycle;
  262. JackPortList(session_params_t* params, uint32_t nports)
  263. {
  264. fNPorts = nports;
  265. fPeriodSize = params->fPeriodSize;
  266. fPacketSize = params->fMtu - sizeof(packet_header_t);
  267. if (params->fSendAudioChannels == 0 && params->fReturnAudioChannels == 0) {
  268. fSubPeriodSize = params->fPeriodSize;
  269. } else {
  270. jack_nframes_t period = (int) powf(2.f,(int)(log(float(fPacketSize)
  271. / (max(params->fReturnAudioChannels, params->fSendAudioChannels) * sizeof(sample_t))) / log(2.)));
  272. fSubPeriodSize = (period > fPeriodSize) ? fPeriodSize : period;
  273. }
  274. fSubPeriodBytesSize = fSubPeriodSize * sizeof(sample_t);
  275. fPortBuffer = new sample_t* [fNPorts];
  276. for (int port_index = 0; port_index < fNPorts; port_index++) {
  277. fPortBuffer[port_index] = NULL;
  278. }
  279. fCycleDuration = float(fSubPeriodSize) / float(params->fSampleRate);
  280. fCycleSize = params->fMtu * (fPeriodSize / fSubPeriodSize);
  281. fLastSubCycle = -1;
  282. }
  283. virtual int GetNumPackets()
  284. {
  285. /*
  286. jack_log("GetNumPackets packet = %d fPeriodSize = %d fSubPeriodSize = %d fSubPeriodBytesSize = %d",
  287. fPeriodSize / fSubPeriodSize, fPeriodSize, fSubPeriodSize, fSubPeriodBytesSize);
  288. */
  289. return fPeriodSize / fSubPeriodSize;
  290. }
  291. JackPortList()
  292. {
  293. fNPorts = 0;
  294. fPeriodSize = 0;
  295. fSubPeriodSize = 0;
  296. fSubPeriodBytesSize = 0;
  297. fPortBuffer = 0;
  298. }
  299. virtual ~JackPortList()
  300. {
  301. delete [] fPortBuffer;
  302. }
  303. void SetBuffer(int index, sample_t* buffer)
  304. {
  305. fPortBuffer[index] = buffer;
  306. }
  307. sample_t* GetBuffer(int index)
  308. {
  309. return fPortBuffer[index];
  310. }
  311. void Copy(sample_t** buffers)
  312. {
  313. for (int port_index = 0; port_index < fNPorts; port_index++) {
  314. memcpy(buffers[port_index], fPortBuffer[port_index], fPeriodSize * sizeof(float));
  315. }
  316. }
  317. // needed syze in bytes for an entire cycle
  318. size_t GetCycleSize()
  319. {
  320. return fCycleSize;
  321. }
  322. // cycle duration in sec
  323. float GetCycleDuration()
  324. {
  325. return fCycleDuration;
  326. }
  327. #ifdef __BIG_ENDIAN__
  328. static inline float SwapFloat(float f)
  329. {
  330. union
  331. {
  332. float f;
  333. unsigned char b[4];
  334. } dat1, dat2;
  335. dat1.f = f;
  336. dat2.b[0] = dat1.b[3];
  337. dat2.b[1] = dat1.b[2];
  338. dat2.b[2] = dat1.b[1];
  339. dat2.b[3] = dat1.b[0];
  340. return dat2.f;
  341. }
  342. virtual void RenderFromJackPorts()
  343. {}
  344. virtual void RenderToJackPorts()
  345. {}
  346. //network<->buffer
  347. virtual int RenderFromNetwork(char* net_buffer, int cycle, int sub_cycle, size_t copy_size, uint32_t port_num)
  348. {
  349. int res = 0;
  350. for (int port_index = 0; port_index < fNPorts; port_index++) {
  351. float* src = (float*)(net_buffer + port_index * fSubPeriodBytesSize);
  352. float* dst = (float*)(fPortBuffer[port_index] + sub_cycle * fSubPeriodSize);
  353. for (unsigned int sample = 0; sample < fSubPeriodBytesSize / sizeof(float); sample++) {
  354. dst[sample] = SwapFloat(src[sample]);
  355. }
  356. }
  357. if (sub_cycle != fLastSubCycle + 1) {
  358. jack_error("Packet(s) missing from... %d %d", fLastSubCycle, sub_cycle);
  359. res = NET_PACKET_ERROR;
  360. }
  361. fLastSubCycle = sub_cycle;
  362. return res;
  363. }
  364. virtual int RenderToNetwork(char* net_buffer, int sub_cycle, uint32_t& port_num)
  365. {
  366. for (int port_index = 0; port_index < fNPorts; port_index++) {
  367. float* src = (float*)(fPortBuffer[port_index] + sub_cycle * fSubPeriodSize);
  368. float* dst = (float*)(net_buffer + port_index * fSubPeriodBytesSize);
  369. for (unsigned int sample = 0; sample < fSubPeriodBytesSize / sizeof(float); sample++) {
  370. dst[sample] = SwapFloat(src[sample]);
  371. }
  372. }
  373. port_num = fNPorts;
  374. return fNPorts * fSubPeriodBytesSize;
  375. }
  376. #else
  377. virtual void RenderFromJackPorts()
  378. {}
  379. virtual void RenderToJackPorts()
  380. {
  381. // reset for next cycle
  382. fLastSubCycle = -1;
  383. }
  384. //network<->buffer
  385. virtual int RenderFromNetwork(char* net_buffer, int cycle, int sub_cycle, size_t copy_size, uint32_t port_num)
  386. {
  387. int res = 0;
  388. for (int port_index = 0; port_index < fNPorts; port_index++) {
  389. memcpy(fPortBuffer[port_index] + sub_cycle * fSubPeriodSize, net_buffer + port_index * fSubPeriodBytesSize, fSubPeriodBytesSize);
  390. }
  391. if (sub_cycle != fLastSubCycle + 1) {
  392. jack_error("Packet(s) missing from... %d %d", fLastSubCycle, sub_cycle);
  393. res = NET_PACKET_ERROR;
  394. }
  395. fLastSubCycle = sub_cycle;
  396. return res;
  397. }
  398. virtual int RenderToNetwork(char* net_buffer, int sub_cycle, uint32_t& port_num)
  399. {
  400. for (int port_index = 0; port_index < fNPorts; port_index++) {
  401. memcpy(net_buffer + port_index * fSubPeriodBytesSize, fPortBuffer[port_index] + sub_cycle * fSubPeriodSize, fSubPeriodBytesSize);
  402. }
  403. port_num = fNPorts;
  404. return fNPorts * fSubPeriodBytesSize;
  405. }
  406. #endif
  407. virtual void ActivePortsFromNetwork(char* net_buffer, uint32_t port_num)
  408. {}
  409. virtual void ActivePortsToNetwork(char* net_buffer, uint32_t& port_num)
  410. {
  411. // Default behaviour: all ports are active
  412. port_num = fNPorts;
  413. }
  414. };
  415. struct JackOptimizedPortList : JackPortList {
  416. // Consuming port list is transmitted in the Sync packed
  417. // "[---Header---|--active_port_num (uint32_t) ---audio data--|--active_port_num (uint32_t) ---audio data--]..."
  418. JackOptimizedPortList(session_params_t* params, uint32_t nports)
  419. :JackPortList(params, nports)
  420. {}
  421. virtual ~JackOptimizedPortList()
  422. {}
  423. int GetNumPackets()
  424. {
  425. // Count active ports
  426. int active_ports = 0;
  427. for (int port_index = 0; port_index < fNPorts; port_index++) {
  428. if (fPortBuffer[port_index]) active_ports++;
  429. }
  430. if (active_ports == 0) {
  431. fSubPeriodSize = fPeriodSize;
  432. } else {
  433. jack_nframes_t period = (int) powf(2.f, (int)(log(float(fPacketSize) / (active_ports * sizeof(sample_t))) / log(2.)));
  434. fSubPeriodSize = (period > fPeriodSize) ? fPeriodSize : period;
  435. }
  436. fSubPeriodBytesSize = fSubPeriodSize * sizeof(sample_t) + sizeof(uint32_t); // The port number in coded on 4 bytes
  437. /*
  438. jack_log("GetNumPackets packet = %d fPeriodSize = %d fSubPeriodSize = %d fSubPeriodBytesSize = %d",
  439. fPeriodSize / fSubPeriodSize, fPeriodSize, fSubPeriodSize, fSubPeriodBytesSize);
  440. */
  441. return fPeriodSize / fSubPeriodSize; // At least one packet
  442. }
  443. #ifdef __BIG_ENDIAN__
  444. // TODO
  445. #else
  446. //network<->buffer
  447. virtual int RenderFromNetwork(char* net_buffer, int cycle, int sub_cycle, size_t copy_size, uint32_t port_num)
  448. {
  449. int res = 0;
  450. // Cleanup all JACK ports at the beginning of the cycle
  451. if (sub_cycle == 0) {
  452. for (int port_index = 0; port_index < fNPorts; port_index++) {
  453. if (fPortBuffer[port_index]) {
  454. memset(fPortBuffer[port_index], 0, fPeriodSize * sizeof(sample_t));
  455. }
  456. }
  457. }
  458. if (port_num > 0) {
  459. /// Setup rendering parameters
  460. int sub_period_size, sub_period_bytes_size;
  461. if (port_num == 0) {
  462. sub_period_size = fPeriodSize;
  463. } else {
  464. jack_nframes_t period = (int) powf(2.f, (int)(log(float(fPacketSize) / (port_num * sizeof(sample_t))) / log(2.)));
  465. sub_period_size = (period > fPeriodSize) ? fPeriodSize : period;
  466. }
  467. sub_period_bytes_size = sub_period_size * sizeof(sample_t) + sizeof(uint32_t); // The port number in coded on 4 bytes
  468. for (uint32_t port_index = 0; port_index < port_num; port_index++) {
  469. // Only copy to active ports : read the active port number then audio data
  470. uint32_t* active_port_address = (uint32_t*)(net_buffer + port_index * sub_period_bytes_size);
  471. uint32_t active_port = (uint32_t)(*active_port_address);
  472. if (fPortBuffer[port_index]) {
  473. memcpy(fPortBuffer[active_port] + sub_cycle * sub_period_size, (char*)(active_port_address + 1), sub_period_bytes_size - sizeof(uint32_t));
  474. }
  475. }
  476. if (sub_cycle != fLastSubCycle + 1) {
  477. jack_error("Packet(s) missing from... %d %d", fLastSubCycle, sub_cycle);
  478. res = NET_PACKET_ERROR;
  479. }
  480. fLastSubCycle = sub_cycle;
  481. }
  482. return res;
  483. }
  484. virtual int RenderToNetwork(char* net_buffer,int sub_cycle, uint32_t& port_num)
  485. {
  486. // Init active port count
  487. port_num = 0;
  488. for (int port_index = 0; port_index < fNPorts; port_index++) {
  489. // Only copy from active ports : write the active port number then audio data
  490. if (fPortBuffer[port_index]) {
  491. uint32_t* active_port_address = (uint32_t*)(net_buffer + port_num * fSubPeriodBytesSize);
  492. *active_port_address = port_index;
  493. memcpy((char*)(active_port_address + 1), fPortBuffer[port_index] + sub_cycle * fSubPeriodSize, fSubPeriodBytesSize - sizeof(uint32_t));
  494. port_num++;
  495. }
  496. }
  497. return port_num * fSubPeriodBytesSize;
  498. }
  499. #endif
  500. virtual void ActivePortsToNetwork(char* net_buffer, uint32_t& port_num)
  501. {
  502. // Init active port count
  503. port_num = 0;
  504. short* active_port_address = (short*)net_buffer;
  505. for (int port_index = 0; port_index < fNPorts; port_index++) {
  506. // Write the active port number
  507. if (fPortBuffer[port_index]) {
  508. *active_port_address = port_index;
  509. active_port_address++;
  510. port_num++;
  511. assert(port_num < 512);
  512. }
  513. }
  514. }
  515. virtual void ActivePortsFromNetwork(char* net_buffer, uint32_t port_num)
  516. {
  517. short* active_port_address = (short*)net_buffer;
  518. for (int port_index = 0; port_index < fNPorts; port_index++) {
  519. fPortBuffer[port_index] = NULL;
  520. }
  521. for (uint port_index = 0; port_index < port_num; port_index++) {
  522. // Use -1 when port is actually connected on other side
  523. if (*active_port_address >= 0 && *active_port_address < fNPorts) {
  524. fPortBuffer[*active_port_address] = (sample_t*)-1;
  525. } else {
  526. jack_error("ActivePortsFromNetwork: incorrect port = %d", *active_port_address);
  527. }
  528. active_port_address++;
  529. }
  530. }
  531. };
  532. class SERVER_EXPORT NetFloatAudioBuffer : public NetAudioBuffer
  533. {
  534. private:
  535. #ifdef OPTIMIZED_PROTOCOL
  536. JackOptimizedPortList fPortBuffer1;
  537. #else
  538. JackPortList fPortBuffer1;
  539. #endif
  540. public:
  541. NetFloatAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer);
  542. virtual ~NetFloatAudioBuffer();
  543. // needed size in bytes for an entire cycle
  544. size_t GetCycleSize();
  545. // cycle duration in sec
  546. float GetCycleDuration()
  547. {
  548. return fPortBuffer1.GetCycleDuration();
  549. }
  550. int GetNumPackets()
  551. {
  552. return fPortBuffer1.GetNumPackets();
  553. }
  554. //jack<->buffer
  555. void RenderFromJackPorts();
  556. void RenderToJackPorts();
  557. void SetBuffer(int index, sample_t* buffer);
  558. sample_t* GetBuffer(int index);
  559. //network<->buffer
  560. int RenderFromNetwork(int cycle, int sub_cycle, size_t copy_size, uint32_t port_num);
  561. void ActivePortsFromNetwork(char* net_buffer, uint32_t port_num);
  562. int RenderToNetwork(int sub_cycle, uint32_t& ort_num);
  563. void ActivePortsToNetwork(char* net_buffer, uint32_t& port_num);
  564. };
  565. class SERVER_EXPORT NetFloatAudioBuffer1 : public NetAudioBuffer
  566. {
  567. private:
  568. jack_nframes_t fPeriodSize;
  569. jack_nframes_t fSubPeriodSize;
  570. size_t fSubPeriodBytesSize;
  571. //sample_t** fPortBuffer;
  572. int fPacketSize;
  573. //int fNPorts;
  574. size_t fCycleSize; // needed size in bytes for an entire cycle
  575. float fCycleDuration; // in sec
  576. //int fLastSubCycle;
  577. public:
  578. NetFloatAudioBuffer1(session_params_t* params, uint32_t nports, char* net_buffer);
  579. virtual ~NetFloatAudioBuffer1();
  580. // needed size in bytes for an entire cycle
  581. size_t GetCycleSize();
  582. // cycle duration in sec
  583. float GetCycleDuration();
  584. int GetNumPackets();
  585. void SetBuffer(int index, sample_t* buffer);
  586. sample_t* GetBuffer(int index);
  587. virtual void RenderFromJackPorts()
  588. {}
  589. virtual void RenderToJackPorts()
  590. {
  591. // reset for next cycle
  592. fLastSubCycle = -1;
  593. }
  594. //jack<->buffer
  595. int RenderFromNetwork(int cycle, int sub_cycle, size_t copy_size, uint32_t port_num);
  596. int RenderToNetwork(int sub_cycle, uint32_t& port_num);
  597. /*
  598. void ActivePortsToNetwork(uint32_t& port_num);
  599. void ActivePortsFromNetwork(uint32_t port_num);
  600. */
  601. };
  602. #if HAVE_CELT
  603. #include <celt/celt.h>
  604. class SERVER_EXPORT NetCeltAudioBuffer : public NetAudioBuffer
  605. {
  606. private:
  607. CELTMode** fCeltMode;
  608. CELTEncoder** fCeltEncoder;
  609. CELTDecoder** fCeltDecoder;
  610. int fCompressedSizeByte;
  611. jack_nframes_t fPeriodSize;
  612. int fNumPackets;
  613. float fCycleDuration; // in sec
  614. size_t fCycleSize; // needed size in bytes for an entire cycle
  615. size_t fSubPeriodBytesSize;
  616. size_t fLastSubPeriodBytesSize;
  617. //sample_t** fPortBuffer;
  618. unsigned char** fCompressedBuffer;
  619. void FreeCelt();
  620. public:
  621. NetCeltAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer, int kbps);
  622. virtual ~NetCeltAudioBuffer();
  623. // needed size in bytes for an entire cycle
  624. size_t GetCycleSize();
  625. // cycle duration in sec
  626. float GetCycleDuration();
  627. int GetNumPackets();
  628. void SetBuffer(int index, sample_t* buffer);
  629. sample_t* GetBuffer(int index);
  630. //jack<->buffer
  631. void RenderFromJackPorts();
  632. void RenderToJackPorts();
  633. //network<->buffer
  634. int RenderFromNetwork(int cycle, int sub_cycle, size_t copy_size, uint32_t port_num);
  635. int RenderToNetwork(int sub_cycle, uint32_t& port_num);
  636. };
  637. #endif
  638. class SERVER_EXPORT NetIntAudioBuffer : public NetAudioBuffer
  639. {
  640. private:
  641. int fCompressedSizeByte;
  642. jack_nframes_t fPeriodSize;
  643. int fNumPackets;
  644. float fCycleDuration; // in sec
  645. size_t fCycleSize; // needed size in bytes for an entire cycle
  646. size_t fSubPeriodSize;
  647. size_t fSubPeriodBytesSize;
  648. size_t fLastSubPeriodSize;
  649. size_t fLastSubPeriodBytesSize;
  650. //sample_t** fPortBuffer;
  651. short** fIntBuffer;
  652. public:
  653. NetIntAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer);
  654. virtual ~NetIntAudioBuffer();
  655. // needed size in bytes for an entire cycle
  656. size_t GetCycleSize();
  657. // cycle duration in sec
  658. float GetCycleDuration();
  659. int GetNumPackets();
  660. void SetBuffer(int index, sample_t* buffer);
  661. sample_t* GetBuffer(int index);
  662. //jack<->buffer
  663. void RenderFromJackPorts();
  664. void RenderToJackPorts();
  665. //network<->buffer
  666. int RenderFromNetwork(int cycle, int sub_cycle, size_t copy_size, uint32_t port_num);
  667. int RenderToNetwork(int sub_cycle, uint32_t& port_num);
  668. };
  669. //utility *************************************************************************************
  670. //socket API management
  671. SERVER_EXPORT int SocketAPIInit();
  672. SERVER_EXPORT int SocketAPIEnd();
  673. //n<-->h functions
  674. SERVER_EXPORT void SessionParamsHToN(session_params_t* src_params, session_params_t* dst_params);
  675. SERVER_EXPORT void SessionParamsNToH(session_params_t* src_params, session_params_t* dst_params);
  676. SERVER_EXPORT void PacketHeaderHToN(packet_header_t* src_header, packet_header_t* dst_header);
  677. SERVER_EXPORT void PacketHeaderNToH(packet_header_t* src_header, packet_header_t* dst_header);
  678. SERVER_EXPORT void MidiBufferHToN(JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer);
  679. SERVER_EXPORT void MidiBufferNToH(JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer);
  680. SERVER_EXPORT void TransportDataHToN(net_transport_data_t* src_params, net_transport_data_t* dst_params);
  681. SERVER_EXPORT void TransportDataNToH(net_transport_data_t* src_params, net_transport_data_t* dst_params);
  682. //display session parameters
  683. SERVER_EXPORT void SessionParamsDisplay(session_params_t* params);
  684. //display packet header
  685. SERVER_EXPORT void PacketHeaderDisplay(packet_header_t* header);
  686. //get the packet type from a sesion parameters
  687. SERVER_EXPORT sync_packet_type_t GetPacketType(session_params_t* params);
  688. //set the packet type in a session parameters
  689. SERVER_EXPORT int SetPacketType(session_params_t* params, sync_packet_type_t packet_type);
  690. //transport utility
  691. SERVER_EXPORT const char* GetTransportState(int transport_state);
  692. SERVER_EXPORT void NetTransportDataDisplay(net_transport_data_t* data);
  693. }