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.

396 lines
13KB

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