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.

884 lines
30KB

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