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.

924 lines
32KB

  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. public:
  217. NetAudioBuffer()
  218. {}
  219. virtual ~NetAudioBuffer()
  220. {}
  221. // needed syze in bytes ofr an entire cycle
  222. virtual size_t GetCycleSize() = 0;
  223. // cycle duration in sec
  224. virtual float GetCycleDuration() = 0;
  225. virtual int GetNumPackets() = 0;
  226. //jack<->buffer
  227. virtual void RenderFromJackPorts() = 0;
  228. virtual void RenderToJackPorts() = 0;
  229. //network<->buffer
  230. virtual int RenderFromNetwork(int cycle, int sub_cycle, size_t copy_size, uint32_t port_num) = 0;
  231. virtual void ActivePortsFromNetwork(char* net_buffer, uint32_t port_num) {}
  232. virtual int RenderToNetwork(int sub_cycle, uint32_t& port_num) = 0;
  233. virtual void ActivePortsToNetwork(char* net_buffer, uint32_t& port_num) {}
  234. virtual void SetBuffer(int index, sample_t* buffer) = 0;
  235. virtual sample_t* GetBuffer(int index) = 0;
  236. };
  237. /**
  238. \Brief Audio buffer and operations class
  239. This class is a toolset to manipulate audio buffers.
  240. The manipulation of audio buffers is similar to midi buffer, except those buffers have fixed size.
  241. The interleaving/uninterleaving operations are simplier here because audio buffers have fixed size,
  242. So there is no need of an intermediate buffer as in NetMidiBuffer.
  243. */
  244. struct JackPortList {
  245. // "[---Header---|--audio data--|--audio data--]..."
  246. jack_nframes_t fPeriodSize;
  247. jack_nframes_t fSubPeriodSize;
  248. size_t fSubPeriodBytesSize;
  249. sample_t** fPortBuffer;
  250. int fPacketSize;
  251. int fNPorts;
  252. size_t fCycleSize; // needed size in bytes for an entire cycle
  253. float fCycleDuration; // in sec
  254. int fLastSubCycle;
  255. JackPortList(session_params_t* params, uint32_t nports)
  256. {
  257. fNPorts = nports;
  258. fPeriodSize = params->fPeriodSize;
  259. fPacketSize = params->fMtu - sizeof(packet_header_t);
  260. if (params->fSendAudioChannels == 0 && params->fReturnAudioChannels == 0) {
  261. fSubPeriodSize = params->fPeriodSize;
  262. } else {
  263. jack_nframes_t period = (int) powf(2.f,(int)(log(float(fPacketSize)
  264. / (max(params->fReturnAudioChannels, params->fSendAudioChannels) * sizeof(sample_t))) / log(2.)));
  265. fSubPeriodSize = (period > fPeriodSize) ? fPeriodSize : period;
  266. }
  267. fSubPeriodBytesSize = fSubPeriodSize * sizeof(sample_t);
  268. fPortBuffer = new sample_t* [fNPorts];
  269. for (int port_index = 0; port_index < fNPorts; port_index++) {
  270. fPortBuffer[port_index] = NULL;
  271. }
  272. fCycleDuration = float(fSubPeriodSize) / float(params->fSampleRate);
  273. fCycleSize = params->fMtu * (fPeriodSize / fSubPeriodSize);
  274. fLastSubCycle = -1;
  275. }
  276. virtual int GetNumPackets()
  277. {
  278. /*
  279. jack_log("GetNumPackets packet = %d fPeriodSize = %d fSubPeriodSize = %d fSubPeriodBytesSize = %d",
  280. fPeriodSize / fSubPeriodSize, fPeriodSize, fSubPeriodSize, fSubPeriodBytesSize);
  281. */
  282. return fPeriodSize / fSubPeriodSize;
  283. }
  284. JackPortList()
  285. {
  286. fNPorts = 0;
  287. fPeriodSize = 0;
  288. fSubPeriodSize = 0;
  289. fSubPeriodBytesSize = 0;
  290. fPortBuffer = 0;
  291. }
  292. virtual ~JackPortList()
  293. {
  294. delete [] fPortBuffer;
  295. }
  296. void SetBuffer(int index, sample_t* buffer)
  297. {
  298. fPortBuffer[index] = buffer;
  299. }
  300. sample_t* GetBuffer(int index)
  301. {
  302. return fPortBuffer[index];
  303. }
  304. void Copy(sample_t** buffers)
  305. {
  306. for (int port_index = 0; port_index < fNPorts; port_index++)
  307. memcpy(buffers[port_index], fPortBuffer[port_index], fPeriodSize * sizeof(float));
  308. }
  309. // needed syze in bytes for an entire cycle
  310. size_t GetCycleSize()
  311. {
  312. return fCycleSize;
  313. }
  314. // cycle duration in sec
  315. float GetCycleDuration()
  316. {
  317. return fCycleDuration;
  318. }
  319. #ifdef __BIG_ENDIAN__
  320. static inline float SwapFloat(float f)
  321. {
  322. union
  323. {
  324. float f;
  325. unsigned char b[4];
  326. } dat1, dat2;
  327. dat1.f = f;
  328. dat2.b[0] = dat1.b[3];
  329. dat2.b[1] = dat1.b[2];
  330. dat2.b[2] = dat1.b[1];
  331. dat2.b[3] = dat1.b[0];
  332. return dat2.f;
  333. }
  334. virtual void RenderFromJackPorts()
  335. {}
  336. virtual void RenderToJackPorts()
  337. {}
  338. //network<->buffer
  339. virtual int RenderFromNetwork(char* net_buffer, int cycle, int sub_cycle, size_t copy_size, uint32_t port_num)
  340. {
  341. int res = 0;
  342. for (int port_index = 0; port_index < fNPorts; port_index++) {
  343. float* src = (float*)(net_buffer + port_index * fSubPeriodBytesSize);
  344. float* dst = (float*)(fPortBuffer[port_index] + sub_cycle * fSubPeriodSize);
  345. for (unsigned int sample = 0; sample < fSubPeriodBytesSize / sizeof(float); sample++) {
  346. dst[sample] = SwapFloat(src[sample]);
  347. }
  348. }
  349. if (sub_cycle != fLastSubCycle + 1) {
  350. jack_error("Packet(s) missing from... %d %d", fLastSubCycle, sub_cycle);
  351. res = NET_PACKET_ERROR;
  352. }
  353. fLastSubCycle = sub_cycle;
  354. return res;
  355. }
  356. virtual int RenderToNetwork(char* net_buffer, int sub_cycle, uint32_t& port_num)
  357. {
  358. for (int port_index = 0; port_index < fNPorts; port_index++) {
  359. float* src = (float*)(fPortBuffer[port_index] + sub_cycle * fSubPeriodSize);
  360. float* dst = (float*)(net_buffer + port_index * fSubPeriodBytesSize);
  361. for (unsigned int sample = 0; sample < fSubPeriodBytesSize / sizeof(float); sample++) {
  362. dst[sample] = SwapFloat(src[sample]);
  363. }
  364. }
  365. port_num = fNPorts;
  366. return fNPorts * fSubPeriodBytesSize;
  367. }
  368. #else
  369. virtual void RenderFromJackPorts()
  370. {}
  371. virtual void RenderToJackPorts()
  372. {
  373. // reset for next cycle
  374. fLastSubCycle = -1;
  375. }
  376. //network<->buffer
  377. virtual int RenderFromNetwork(char* net_buffer, int cycle, int sub_cycle, size_t copy_size, uint32_t port_num)
  378. {
  379. int res = 0;
  380. for (int port_index = 0; port_index < fNPorts; port_index++) {
  381. memcpy(fPortBuffer[port_index] + sub_cycle * fSubPeriodSize, net_buffer + port_index * fSubPeriodBytesSize, fSubPeriodBytesSize);
  382. }
  383. if (sub_cycle != fLastSubCycle + 1) {
  384. jack_error("Packet(s) missing from... %d %d", fLastSubCycle, sub_cycle);
  385. res = NET_PACKET_ERROR;
  386. }
  387. fLastSubCycle = sub_cycle;
  388. return res;
  389. }
  390. virtual int RenderToNetwork(char* net_buffer, int sub_cycle, uint32_t& port_num)
  391. {
  392. for (int port_index = 0; port_index < fNPorts; port_index++) {
  393. memcpy(net_buffer + port_index * fSubPeriodBytesSize, fPortBuffer[port_index] + sub_cycle * fSubPeriodSize, fSubPeriodBytesSize);
  394. }
  395. port_num = fNPorts;
  396. return fNPorts * fSubPeriodBytesSize;
  397. }
  398. #endif
  399. virtual void ActivePortsFromNetwork(char* net_buffer, uint32_t port_num)
  400. {}
  401. virtual void ActivePortsToNetwork(char* net_buffer, uint32_t& port_num)
  402. {
  403. // Default behaviour: all ports are active
  404. port_num = fNPorts;
  405. }
  406. };
  407. struct JackOptimizedPortList : JackPortList {
  408. // Consuming port list is transmitted in the Sync packed
  409. // "[---Header---|--active_port_num---audio data--|--active_port_num---audio data--]..."
  410. JackOptimizedPortList(session_params_t* params, uint32_t nports)
  411. :JackPortList(params, nports)
  412. {}
  413. virtual ~JackOptimizedPortList()
  414. {}
  415. int GetNumPackets()
  416. {
  417. // Count active ports
  418. int active_ports = 0;
  419. for (int port_index = 0; port_index < fNPorts; port_index++) {
  420. if (fPortBuffer[port_index]) active_ports++;
  421. }
  422. if (active_ports == 0) {
  423. fSubPeriodSize = fPeriodSize;
  424. } else {
  425. jack_nframes_t period = (int) powf(2.f, (int)(log(float(fPacketSize) / (active_ports * sizeof(sample_t))) / log(2.)));
  426. fSubPeriodSize = (period > fPeriodSize) ? fPeriodSize : period;
  427. }
  428. fSubPeriodBytesSize = fSubPeriodSize * sizeof(sample_t) + sizeof(uint32_t); // The port number in coded on 4 bytes
  429. /*
  430. jack_log("GetNumPackets packet = %d fPeriodSize = %d fSubPeriodSize = %d fSubPeriodBytesSize = %d",
  431. fPeriodSize / fSubPeriodSize, fPeriodSize, fSubPeriodSize, fSubPeriodBytesSize);
  432. */
  433. return fPeriodSize / fSubPeriodSize; // At least one packet
  434. }
  435. #ifdef __BIG_ENDIAN__
  436. // TODO
  437. #else
  438. //network<->buffer
  439. virtual int RenderFromNetwork(char* net_buffer, int cycle, int sub_cycle, size_t copy_size, uint32_t port_num)
  440. {
  441. int res = 0;
  442. // Cleanup all JACK ports at the beginning of the cycle
  443. if (sub_cycle == 0) {
  444. for (int port_index = 0; port_index < fNPorts; port_index++) {
  445. if (fPortBuffer[port_index]) {
  446. memset(fPortBuffer[port_index], 0, fPeriodSize * sizeof(sample_t));
  447. }
  448. }
  449. }
  450. if (port_num > 0) {
  451. /// Setup rendering parameters
  452. int sub_period_size, sub_period_bytes_size;
  453. if (port_num == 0) {
  454. sub_period_size = fPeriodSize;
  455. } else {
  456. jack_nframes_t period = (int) powf(2.f, (int)(log(float(fPacketSize) / (port_num * sizeof(sample_t))) / log(2.)));
  457. sub_period_size = (period > fPeriodSize) ? fPeriodSize : period;
  458. }
  459. sub_period_bytes_size = sub_period_size * sizeof(sample_t) + sizeof(uint32_t); // The port number in coded on 4 bytes
  460. for (uint32_t port_index = 0; port_index < port_num; port_index++) {
  461. // Only copy to active ports : read the active port number then audio data
  462. int* active_port_address = (int*)(net_buffer + port_index * sub_period_bytes_size);
  463. int active_port = (int)(*active_port_address);
  464. if (fPortBuffer[port_index]) {
  465. memcpy(fPortBuffer[active_port] + sub_cycle * sub_period_size, (char*)(active_port_address + 1), sub_period_bytes_size - sizeof(int));
  466. }
  467. }
  468. if (sub_cycle != fLastSubCycle + 1) {
  469. jack_error("Packet(s) missing from... %d %d", fLastSubCycle, sub_cycle);
  470. res = NET_PACKET_ERROR;
  471. }
  472. fLastSubCycle = sub_cycle;
  473. }
  474. return res;
  475. }
  476. virtual int RenderToNetwork(char* net_buffer,int sub_cycle, uint32_t& port_num)
  477. {
  478. // Init active port count
  479. port_num = 0;
  480. for (int port_index = 0; port_index < fNPorts; port_index++) {
  481. // Only copy from active ports : write the active port number then audio data
  482. if (fPortBuffer[port_index]) {
  483. int* active_port_address = (int*)(net_buffer + port_num * fSubPeriodBytesSize);
  484. *active_port_address = port_index;
  485. memcpy((char*)(active_port_address + 1), fPortBuffer[port_index] + sub_cycle * fSubPeriodSize, fSubPeriodBytesSize - sizeof(uint32_t));
  486. port_num++;
  487. }
  488. }
  489. return port_num * fSubPeriodBytesSize;
  490. }
  491. #endif
  492. virtual void ActivePortsToNetwork(char* net_buffer, uint32_t& port_num)
  493. {
  494. // Init active port count
  495. port_num = 0;
  496. short* active_port_address = (short*)net_buffer;
  497. for (int port_index = 0; port_index < fNPorts; port_index++) {
  498. // Write the active port number
  499. if (fPortBuffer[port_index]) {
  500. *active_port_address = port_index;
  501. active_port_address++;
  502. port_num++;
  503. assert(port_num < 512);
  504. }
  505. }
  506. }
  507. virtual void ActivePortsFromNetwork(char* net_buffer, uint32_t port_num)
  508. {
  509. short* active_port_address = (short*)net_buffer;
  510. for (int port_index = 0; port_index < fNPorts; port_index++) {
  511. fPortBuffer[port_index] = NULL;
  512. }
  513. for (uint port_index = 0; port_index < port_num; port_index++) {
  514. // Use -1 when port is actually connected on other side
  515. if (*active_port_address >= 0 && *active_port_address < fNPorts) {
  516. fPortBuffer[*active_port_address] = (sample_t*)-1;
  517. } else {
  518. jack_error("ActivePortsFromNetwork: incorrect port = %d", *active_port_address);
  519. }
  520. active_port_address++;
  521. }
  522. }
  523. };
  524. class SERVER_EXPORT NetFloatAudioBuffer : public NetAudioBuffer
  525. {
  526. private:
  527. #ifdef OPTIMIZED_PROTOCOL
  528. JackOptimizedPortList fPortBuffer;
  529. #else
  530. JackPortList fPortBuffer;
  531. #endif
  532. char* fNetBuffer;
  533. public:
  534. NetFloatAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer);
  535. ~NetFloatAudioBuffer();
  536. // needed size in bytes for an entire cycle
  537. size_t GetCycleSize();
  538. // cycle duration in sec
  539. float GetCycleDuration()
  540. {
  541. return fPortBuffer.GetCycleDuration();
  542. }
  543. int GetNumPackets()
  544. {
  545. return fPortBuffer.GetNumPackets();
  546. }
  547. //jack<->buffer
  548. void RenderFromJackPorts();
  549. void RenderToJackPorts();
  550. void SetBuffer(int index, sample_t* buffer);
  551. sample_t* GetBuffer(int index);
  552. //network<->buffer
  553. int RenderFromNetwork(int cycle, int sub_cycle, size_t copy_size, uint32_t port_num);
  554. void ActivePortsFromNetwork(char* net_buffer, uint32_t port_num);
  555. int RenderToNetwork(int sub_cycle, uint32_t& ort_num);
  556. void ActivePortsToNetwork(char* net_buffer, uint32_t& port_num);
  557. };
  558. #if HAVE_CELT
  559. #include <celt/celt.h>
  560. class SERVER_EXPORT NetCeltAudioBuffer : public NetAudioBuffer
  561. {
  562. private:
  563. CELTMode** fCeltMode;
  564. CELTEncoder** fCeltEncoder;
  565. CELTDecoder** fCeltDecoder;
  566. int fCompressedSizeByte;
  567. jack_nframes_t fPeriodSize;
  568. int fNumPackets;
  569. float fCycleDuration; // in sec
  570. size_t fCycleSize; // needed size in bytes for an entire cycle
  571. size_t fSubPeriodBytesSize;
  572. size_t fLastSubPeriodBytesSize;
  573. sample_t** fPortBuffer;
  574. char* fNetBuffer;
  575. unsigned char** fCompressedBuffer;
  576. int fNPorts;
  577. int fLastSubCycle;
  578. void FreeCelt();
  579. public:
  580. NetCeltAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer, int kbps);
  581. virtual ~NetCeltAudioBuffer();
  582. // needed size in bytes for an entire cycle
  583. size_t GetCycleSize();
  584. // cycle duration in sec
  585. float GetCycleDuration();
  586. int GetNumPackets();
  587. void SetBuffer(int index, sample_t* buffer);
  588. sample_t* GetBuffer(int index);
  589. //jack<->buffer
  590. void RenderFromJackPorts();
  591. void RenderToJackPorts();
  592. //network<->buffer
  593. int RenderFromNetwork(int cycle, int sub_cycle, size_t copy_size, uint32_t port_num);
  594. int RenderToNetwork(int sub_cycle, uint32_t& port_num);
  595. };
  596. #endif
  597. class SERVER_EXPORT NetIntAudioBuffer : public NetAudioBuffer
  598. {
  599. private:
  600. int fCompressedSizeByte;
  601. jack_nframes_t fPeriodSize;
  602. int fNumPackets;
  603. float fCycleDuration; // in sec
  604. size_t fCycleSize; // needed size in bytes for an entire cycle
  605. size_t fSubPeriodSize;
  606. size_t fSubPeriodBytesSize;
  607. size_t fLastSubPeriodSize;
  608. size_t fLastSubPeriodBytesSize;
  609. sample_t** fPortBuffer;
  610. char* fNetBuffer;
  611. short** fIntBuffer;
  612. int fNPorts;
  613. int fLastSubCycle;
  614. public:
  615. NetIntAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer);
  616. virtual ~NetIntAudioBuffer();
  617. // needed size in bytes for an entire cycle
  618. size_t GetCycleSize();
  619. // cycle duration in sec
  620. float GetCycleDuration();
  621. int GetNumPackets();
  622. void SetBuffer(int index, sample_t* buffer);
  623. sample_t* GetBuffer(int index);
  624. //jack<->buffer
  625. void RenderFromJackPorts();
  626. void RenderToJackPorts();
  627. //network<->buffer
  628. int RenderFromNetwork(int cycle, int sub_cycle, size_t copy_size, uint32_t port_num);
  629. int RenderToNetwork(int sub_cycle, uint32_t& port_num);
  630. };
  631. /*
  632. #define AUDIO_BUFFER_SIZE 8
  633. struct JackPortListAllocate : public JackPortList {
  634. JackPortListAllocate()
  635. {
  636. fNPorts = 0;
  637. fPeriodSize = 0;
  638. fSubPeriodSize = 0;
  639. fSubPeriodBytesSize = 0;
  640. fPortBuffer = 0;
  641. }
  642. ~JackPortListAllocate()
  643. {
  644. for (int port_index = 0; port_index < fNPorts; port_index++)
  645. delete [] fPortBuffer[port_index];
  646. delete [] fPortBuffer;
  647. }
  648. void Init(session_params_t* params, uint32_t nports)
  649. {
  650. fNPorts = nports;
  651. fPeriodSize = params->fPeriodSize;
  652. if (params->fSendAudioChannels == 0 && params->fReturnAudioChannels == 0) {
  653. fSubPeriodSize = params->fPeriodSize;
  654. } else {
  655. jack_nframes_t period = (int) powf(2.f, (int)(log(float((params->fMtu - sizeof(packet_header_t)))
  656. / (max(params->fReturnAudioChannels, params->fSendAudioChannels) * sizeof(sample_t))) / log(2.)));
  657. fSubPeriodSize = (period > params->fPeriodSize) ? params->fPeriodSize : period;
  658. }
  659. fSubPeriodBytesSize = fSubPeriodSize * sizeof(sample_t);
  660. fPortBuffer = new sample_t* [fNPorts];
  661. for (int port_index = 0; port_index < fNPorts; port_index++)
  662. fPortBuffer[port_index] = new sample_t[fPeriodSize];
  663. }
  664. };
  665. class SERVER_EXPORT NetBufferedAudioBuffer : public NetAudioBuffer
  666. {
  667. private:
  668. char* fNetBuffer;
  669. JackPortListAllocate fPortBuffer[AUDIO_BUFFER_SIZE];
  670. sample_t** fJackPortBuffer;
  671. int fMaxCycle;
  672. public:
  673. NetBufferedAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer);
  674. ~NetBufferedAudioBuffer();
  675. // needed syze in bytes ofr an entire cycle
  676. size_t GetCycleSize();
  677. // cycle duration in sec
  678. float GetCycleDuration()
  679. {
  680. return fPortBuffer[0].GetCycleDuration();
  681. }
  682. //jack<->buffer
  683. void RenderFromJackPorts(int sub_cycle);
  684. void RenderToJackPorts(int cycle, int sub_cycle);
  685. //void FinishRenderToJackPorts(int cycle);
  686. //network<->buffer
  687. void RenderFromNetwork(int sub_cycle, size_t copy_size)
  688. {
  689. // TODO
  690. }
  691. int RenderToNetwork(int sub_cycle, size_t total_size)
  692. {
  693. // TODO
  694. return 0;
  695. }
  696. void SetBuffer(int index, sample_t* buffer);
  697. sample_t* GetBuffer(int index);
  698. };
  699. */
  700. //utility *************************************************************************************
  701. //socket API management
  702. SERVER_EXPORT int SocketAPIInit();
  703. SERVER_EXPORT int SocketAPIEnd();
  704. //n<-->h functions
  705. SERVER_EXPORT void SessionParamsHToN(session_params_t* src_params, session_params_t* dst_params);
  706. SERVER_EXPORT void SessionParamsNToH(session_params_t* src_params, session_params_t* dst_params);
  707. SERVER_EXPORT void PacketHeaderHToN(packet_header_t* src_header, packet_header_t* dst_header);
  708. SERVER_EXPORT void PacketHeaderNToH(packet_header_t* src_header, packet_header_t* dst_header);
  709. SERVER_EXPORT void MidiBufferHToN(JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer);
  710. SERVER_EXPORT void MidiBufferNToH(JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer);
  711. SERVER_EXPORT void TransportDataHToN(net_transport_data_t* src_params, net_transport_data_t* dst_params);
  712. SERVER_EXPORT void TransportDataNToH(net_transport_data_t* src_params, net_transport_data_t* dst_params);
  713. //display session parameters
  714. SERVER_EXPORT void SessionParamsDisplay(session_params_t* params);
  715. //display packet header
  716. SERVER_EXPORT void PacketHeaderDisplay(packet_header_t* header);
  717. //get the packet type from a sesion parameters
  718. SERVER_EXPORT sync_packet_type_t GetPacketType(session_params_t* params);
  719. //set the packet type in a session parameters
  720. SERVER_EXPORT int SetPacketType(session_params_t* params, sync_packet_type_t packet_type);
  721. //transport utility
  722. SERVER_EXPORT const char* GetTransportState(int transport_state);
  723. SERVER_EXPORT void NetTransportDataDisplay(net_transport_data_t* data);
  724. }