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.

427 lines
15KB

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