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.

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