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.

433 lines
15KB

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