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.

372 lines
14KB

  1. /*
  2. Copyright (C) 2016-2019 Christoph Kuhr
  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 "JackAVBDriver.h"
  16. #include "JackEngineControl.h"
  17. #include "JackLockedEngine.h"
  18. #include "JackGraphManager.h"
  19. #include "JackWaitThreadedDriver.h"
  20. #include "JackTools.h"
  21. #include "driver_interface.h"
  22. #define MIN(x,y) ((x)<(y) ? (x) : (y))
  23. using namespace std;
  24. namespace Jack
  25. {
  26. JackAVBDriver::JackAVBDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table,
  27. char* stream_id, char* destination_mac, char* eth_dev,
  28. int sample_rate, int period_size, int adjust,
  29. int num_periods, int capture_ports, int playback_ports)
  30. : JackWaiterDriver(name, alias, engine, table)
  31. {
  32. jack_log("JackAVBDriver::JackAVBPDriver Ethernet Device %s", eth_dev);
  33. jack_log("Stream ID: %02x %02x %02x %02x %02x %02x %02x %02x",
  34. (uint8_t) stream_id[0],
  35. (uint8_t) stream_id[1],
  36. (uint8_t) stream_id[2],
  37. (uint8_t) stream_id[3],
  38. (uint8_t) stream_id[4],
  39. (uint8_t) stream_id[5],
  40. (uint8_t) stream_id[6],
  41. (uint8_t) stream_id[7]);
  42. jack_log("Destination MAC Address: %02x:%02x:%02x:%02x:%02x:%02x",
  43. (uint8_t) destination_mac[0],
  44. (uint8_t) destination_mac[1],
  45. (uint8_t) destination_mac[2],
  46. (uint8_t) destination_mac[3],
  47. (uint8_t) destination_mac[4],
  48. (uint8_t) destination_mac[5]);
  49. printf("JackAVBDriver::JackAVBPDriver Ethernet Device %s\n", eth_dev);
  50. printf("Stream ID: %02x %02x %02x %02x %02x %02x %02x %02x\n",
  51. (uint8_t) stream_id[0],
  52. (uint8_t) stream_id[1],
  53. (uint8_t) stream_id[2],
  54. (uint8_t) stream_id[3],
  55. (uint8_t) stream_id[4],
  56. (uint8_t) stream_id[5],
  57. (uint8_t) stream_id[6],
  58. (uint8_t) stream_id[7]);
  59. printf("Destination MAC Address: %02x:%02x:%02x:%02x:%02x:%02x\n",
  60. (uint8_t) destination_mac[0],
  61. (uint8_t) destination_mac[1],
  62. (uint8_t) destination_mac[2],
  63. (uint8_t) destination_mac[3],
  64. (uint8_t) destination_mac[4],
  65. (uint8_t) destination_mac[5]);
  66. num_packets_even_odd = 0; // even = 0, odd = 1
  67. init_avb_driver( &(this->avb_ctx),
  68. eth_dev,
  69. stream_id,
  70. destination_mac,
  71. sample_rate,
  72. period_size,
  73. num_periods,
  74. adjust,
  75. capture_ports,
  76. playback_ports
  77. );
  78. }
  79. JackAVBDriver::~JackAVBDriver()
  80. {
  81. // No destructor yet.
  82. }
  83. int JackAVBDriver::Close()
  84. {
  85. // Generic audio driver close
  86. int res = JackWaiterDriver::Close();
  87. FreePorts();
  88. shutdown_avb_driver(&avb_ctx);
  89. return res;
  90. }
  91. int JackAVBDriver::AllocPorts()
  92. {
  93. jack_port_id_t port_index;
  94. char buf[64];
  95. int chn = 0;
  96. for (chn = 0; chn < (int)avb_ctx.capture_channels; chn++) {
  97. memset(buf, 0, sizeof(buf));
  98. snprintf (buf, sizeof(buf) - 1, "system:capture_%u", chn + 1);
  99. if (fEngine->PortRegister(fClientControl.fRefNum, buf, JACK_DEFAULT_AUDIO_TYPE,
  100. CaptureDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
  101. jack_error("driver: cannot register port for %s", buf);
  102. return -1;
  103. }
  104. avb_ctx.capture_ports = jack_slist_append (avb_ctx.capture_ports, (void *)(intptr_t)port_index);
  105. }
  106. for (chn = 0; chn < (int)avb_ctx.playback_channels; chn++) {
  107. memset(buf, 0, sizeof(buf));
  108. snprintf (buf, sizeof(buf) - 1, "system:playback_%u", chn + 1);
  109. if (fEngine->PortRegister(fClientControl.fRefNum, buf, JACK_DEFAULT_AUDIO_TYPE,
  110. PlaybackDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
  111. jack_error("driver: cannot register port for %s", buf);
  112. return -1;
  113. }
  114. avb_ctx.playback_ports = jack_slist_append (avb_ctx.playback_ports, (void *)(intptr_t)port_index);
  115. }
  116. //port = fGraphManager->GetPort(port_index);
  117. return 0;
  118. }
  119. bool JackAVBDriver::Initialize()
  120. {
  121. jack_log("JackAVBDriver::Init");
  122. FreePorts();
  123. //display some additional infos
  124. printf("AVB driver started\n");
  125. if (startup_avb_driver(&avb_ctx)) {
  126. return false;
  127. }
  128. //register jack ports
  129. if (AllocPorts() != 0) {
  130. jack_error("Can't allocate ports.");
  131. return false;
  132. }
  133. //driver parametering
  134. JackTimedDriver::SetBufferSize(avb_ctx.period_size);
  135. JackTimedDriver::SetSampleRate(avb_ctx.sample_rate);
  136. JackDriver::NotifyBufferSize(avb_ctx.period_size);
  137. JackDriver::NotifySampleRate(avb_ctx.sample_rate);
  138. return true;
  139. }
  140. int JackAVBDriver::Read()
  141. {
  142. int ret = 0;
  143. JSList *node = avb_ctx.capture_ports;
  144. uint64_t cumulative_rx_int_ns = 0;
  145. int n = 0;
  146. for(n=0; n<avb_ctx.num_packets; n++){
  147. cumulative_rx_int_ns += await_avtp_rx_ts( &avb_ctx, n );
  148. // jack_log("duration: %lld", cumulative_rx_int_ns);
  149. }
  150. float cumulative_rx_int_us = cumulative_rx_int_ns / 1000;
  151. if ( cumulative_rx_int_us > avb_ctx.period_usecs) {
  152. ret = 1;
  153. NotifyXRun(fBeginDateUst, cumulative_rx_int_us);
  154. jack_error("netxruns... duration: %fms", cumulative_rx_int_us / 1000);
  155. }
  156. JackDriver::CycleTakeBeginTime();
  157. if ( ret ) return -1;
  158. while (node != NULL) {
  159. jack_port_id_t port_index = (jack_port_id_t)(intptr_t) node->data;
  160. JackPort *port = fGraphManager->GetPort(port_index);
  161. jack_default_audio_sample_t* buf = (jack_default_audio_sample_t*)fGraphManager->GetBuffer(port_index, fEngineControl->fBufferSize);
  162. //memcpy(buf, 0, avb_ctx.period_size * sizeof(jack_default_audio_sample_t));
  163. node = jack_slist_next (node);
  164. }
  165. return 0;
  166. }
  167. int JackAVBDriver::Write()
  168. {
  169. JSList *node = avb_ctx.playback_ports;
  170. while (node != NULL) {
  171. jack_port_id_t port_index = (jack_port_id_t)(intptr_t) node->data;
  172. JackPort *port = fGraphManager->GetPort(port_index);
  173. jack_default_audio_sample_t* buf = (jack_default_audio_sample_t*)fGraphManager->GetBuffer(port_index, fEngineControl->fBufferSize);
  174. //memcpy(buf, 0, avb_ctx.period_size * sizeof(jack_default_audio_sample_t));
  175. node = jack_slist_next (node);
  176. }
  177. return 0;
  178. }
  179. void JackAVBDriver::FreePorts ()
  180. {
  181. JSList *node = avb_ctx.capture_ports;
  182. while (node != NULL) {
  183. JSList *this_node = node;
  184. jack_port_id_t port_index = (jack_port_id_t)(intptr_t) node->data;
  185. node = jack_slist_remove_link(node, this_node);
  186. jack_slist_free_1(this_node);
  187. fEngine->PortUnRegister(fClientControl.fRefNum, port_index);
  188. }
  189. avb_ctx.capture_ports = NULL;
  190. node = avb_ctx.playback_ports;
  191. while (node != NULL) {
  192. JSList *this_node = node;
  193. jack_port_id_t port_index = (jack_port_id_t)(intptr_t) node->data;
  194. node = jack_slist_remove_link(node, this_node);
  195. jack_slist_free_1(this_node);
  196. fEngine->PortUnRegister(fClientControl.fRefNum, port_index);
  197. }
  198. avb_ctx.playback_ports = NULL;
  199. }
  200. #ifdef __cplusplus
  201. extern "C"
  202. {
  203. #endif
  204. inline int argumentsSplitDelimiters(char* inputString, char* outputArray, int array_len)
  205. {
  206. int tokenCnt=0;
  207. char *token;
  208. char *der_string = strdup(inputString);
  209. int m = 0;
  210. for( m=0;m<array_len;m++){
  211. if(( token = strsep(&der_string, ":")) != NULL ){
  212. outputArray[m] = (char)strtol(strdup(token), NULL, 16); // number base 16
  213. } else {
  214. tokenCnt = m;
  215. break;
  216. }
  217. }
  218. free(der_string);
  219. return tokenCnt;
  220. }
  221. SERVER_EXPORT jack_driver_desc_t* driver_get_descriptor ()
  222. {
  223. jack_driver_desc_t * desc;
  224. jack_driver_desc_filler_t filler;
  225. jack_driver_param_value_t value;
  226. desc = jack_driver_descriptor_construct("avb", JackDriverMaster, "IEEE 1722 AVTP slave backend component", &filler);
  227. value.ui = 2U;
  228. jack_driver_descriptor_add_parameter(desc, &filler, "audio-ins", 'i', JackDriverParamUInt, &value, NULL, "Number of capture channels (defaults to 1)", NULL);
  229. jack_driver_descriptor_add_parameter(desc, &filler, "audio-outs", 'o', JackDriverParamUInt, &value, NULL, "Number of playback channels (defaults to 1)", NULL);
  230. value.ui = 48000U;
  231. jack_driver_descriptor_add_parameter(desc, &filler, "rate", 'r', JackDriverParamUInt, &value, NULL, "Sample rate", NULL);
  232. value.ui = 64U;
  233. jack_driver_descriptor_add_parameter(desc, &filler, "period", 'p', JackDriverParamUInt, &value, NULL, "Frames per period", NULL);
  234. value.ui = 1U;
  235. jack_driver_descriptor_add_parameter(desc, &filler, "num-periods", 'n', JackDriverParamUInt, &value, NULL, "Network latency setting in no. of periods", NULL);
  236. value.ui = 0U;
  237. jack_driver_descriptor_add_parameter(desc, &filler, "adjust", 'a', JackDriverParamUInt, &value, NULL, "Adjust Timestamps", NULL);
  238. sprintf( value.str, "enp4s0");
  239. jack_driver_descriptor_add_parameter(desc, &filler, "eth-dev", 'e', JackDriverParamString, &value, NULL, "AVB Ethernet Device", NULL);
  240. sprintf( value.str, "00:22:97:00:41:2c:00:00");
  241. jack_driver_descriptor_add_parameter(desc, &filler, "stream-id", 's', JackDriverParamString, &value, NULL, "Stream ID for listening", NULL);
  242. sprintf( value.str, "91:e0:f0:11:11:11");
  243. jack_driver_descriptor_add_parameter(desc, &filler, "dst-mac", 'm', JackDriverParamString, &value, NULL, "Multicast Destination MAC Address for listening", NULL);
  244. return desc;
  245. }
  246. SERVER_EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params)
  247. {
  248. unsigned int sample_rate = 48000;
  249. jack_nframes_t period_size = 64;
  250. unsigned int capture_ports = 2;
  251. unsigned int playback_ports = 2;
  252. int num_periods = 2;
  253. int adjust = 0;
  254. char sid[8];
  255. char dmac[6];
  256. char eth_dev[32];
  257. const JSList * node;
  258. const jack_driver_param_t * param;
  259. printf("foo bar\n");fflush(stdout);
  260. for (node = params; node; node = jack_slist_next(node)) {
  261. param = (const jack_driver_param_t*) node->data;
  262. switch (param->character) {
  263. case 'i':
  264. capture_ports = param->value.ui;
  265. break;
  266. case 'o':
  267. playback_ports = param->value.ui;
  268. break;
  269. case 'r':
  270. sample_rate = param->value.ui;
  271. break;
  272. case 'p':
  273. period_size = param->value.ui;
  274. break;
  275. case 'n':
  276. num_periods = param->value.ui;
  277. break;
  278. case 'a':
  279. adjust = param->value.ui;
  280. break;
  281. case 'e':
  282. sprintf(eth_dev, "%s", param->value.str);
  283. printf("Eth Dev: %s %s\n", param->value.str, eth_dev);fflush(stdout);
  284. break;
  285. case 's':
  286. // split stream ID
  287. argumentsSplitDelimiters((char *)param->value.str, sid, 8);
  288. printf("Stream ID: %s %02x %02x %02x %02x %02x %02x %02x %02x \n", param->value.str,
  289. sid[0], sid[1], sid[2], sid[3], sid[4], sid[5], sid[6], sid[7]);fflush(stdout);
  290. break;
  291. case 'm':
  292. // split destination mac address
  293. argumentsSplitDelimiters((char *)param->value.str, dmac, 6);
  294. printf("Destination MAC Address: %s %02x %02x %02x %02x %02x %02x \n", param->value.str,
  295. dmac[0], dmac[1], dmac[2], dmac[3], dmac[4], dmac[5]);fflush(stdout);
  296. break;
  297. }
  298. }
  299. try {
  300. Jack::JackDriverClientInterface* driver = new Jack::JackWaitThreadedDriver (
  301. new Jack::JackAVBDriver("system", "avb_mc", engine, table, sid, dmac, eth_dev,
  302. sample_rate, period_size, num_periods,
  303. adjust, capture_ports, playback_ports));
  304. if (driver->Open(period_size, sample_rate, 1, 1, capture_ports, playback_ports,
  305. 0, "from_master", "to_master", 0, 0) == 0) {
  306. return driver;
  307. } else {
  308. delete driver;
  309. return NULL;
  310. }
  311. } catch (...) {
  312. return NULL;
  313. }
  314. }
  315. #ifdef __cplusplus
  316. }
  317. #endif
  318. }