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.

340 lines
12KB

  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. SERVER_EXPORT jack_driver_desc_t* driver_get_descriptor ()
  178. {
  179. jack_driver_desc_t * desc;
  180. jack_driver_desc_filler_t filler;
  181. jack_driver_param_value_t value;
  182. desc = jack_driver_descriptor_construct("avb", JackDriverMaster, "IEEE 1722 AVTP slave backend component", &filler);
  183. value.ui = 1U;
  184. jack_driver_descriptor_add_parameter(desc, &filler, "audio-ins", 'i', JackDriverParamUInt, &value, NULL, "Number of capture channels (defaults to 2)", NULL);
  185. value.ui = 48000U;
  186. jack_driver_descriptor_add_parameter(desc, &filler, "rate", 'r', JackDriverParamUInt, &value, NULL, "Sample rate", NULL);
  187. value.ui = 64U;
  188. jack_driver_descriptor_add_parameter(desc, &filler, "period", 'p', JackDriverParamUInt, &value, NULL, "Frames per period", NULL);
  189. value.ui = 1U;
  190. jack_driver_descriptor_add_parameter(desc, &filler, "num-periods", 'n', JackDriverParamUInt, &value, NULL, "Network latency setting in no. of periods", NULL);
  191. sprintf( value.str, "enp4s0");
  192. jack_driver_descriptor_add_parameter(desc, &filler, "ethernet-dev", 'e', JackDriverParamString, &value, NULL, "AVB Ethernet Device", NULL);
  193. sprintf( value.str, "00:22:97:00:41:2c:00:00");
  194. jack_driver_descriptor_add_parameter(desc, &filler, "stream-id", 's', JackDriverParamString, &value, NULL, "Stream ID for listening", NULL);
  195. sprintf( value.str, "91:e0:f0:11:11:11");
  196. jack_driver_descriptor_add_parameter(desc, &filler, "destination-mac", 'm', JackDriverParamString, &value, NULL, "Multicast Destination MAC Address for listening", NULL);
  197. return desc;
  198. }
  199. SERVER_EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params)
  200. {
  201. jack_nframes_t sample_rate = 48000;
  202. jack_nframes_t period_size = 64;
  203. unsigned int capture_ports = 1;
  204. int num_periods = 2;
  205. char sid[25];
  206. char dmac[19];
  207. char eth_dev[32];
  208. int dont_htonl_floats = 0;
  209. int always_deadline = 0;
  210. int jitter_val = 0;
  211. const JSList * node;
  212. const jack_driver_param_t * param;
  213. printf("foo bar\n");fflush(stdout);
  214. for (node = params; node; node = jack_slist_next(node)) {
  215. param = (const jack_driver_param_t*) node->data;
  216. switch (param->character) {
  217. case 'i':
  218. capture_ports = param->value.ui;
  219. break;
  220. case 'r':
  221. sample_rate = param->value.ui;
  222. break;
  223. case 'p':
  224. period_size = param->value.ui;
  225. break;
  226. case 'n':
  227. num_periods = param->value.ui;
  228. break;
  229. case 'e':
  230. sprintf(eth_dev, "%s", param->value.str);
  231. printf("Eth Dev: %s %s\n", param->value.str, eth_dev);fflush(stdout);
  232. break;
  233. case 's':
  234. sprintf(sid, "%s", param->value.str);
  235. printf("Stream ID: %s %s\n", param->value.str, sid);fflush(stdout);
  236. break;
  237. case 'm':
  238. sprintf(dmac, "%s", param->value.str);
  239. printf("D MAC: %s %s\n", param->value.str, dmac);fflush(stdout);
  240. break;
  241. }
  242. }
  243. try {
  244. Jack::JackDriverClientInterface* driver = new Jack::JackWaitThreadedDriver (
  245. new Jack::JackAVBPDriver("system", "avb_mc", engine, table, sid, dmac, eth_dev,
  246. sample_rate, period_size, num_periods));
  247. if (driver->Open(period_size, sample_rate, 1, 1, capture_ports, 0,
  248. 0, "from_master", "to_master", 0, 0) == 0) {
  249. return driver;
  250. } else {
  251. delete driver;
  252. return NULL;
  253. }
  254. } catch (...) {
  255. return NULL;
  256. }
  257. }
  258. #ifdef __cplusplus
  259. }
  260. #endif
  261. }