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.

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