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