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.

604 lines
20KB

  1. /*
  2. Copyright (C) 2011 Devin Anderson
  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 <memory>
  16. #include <new>
  17. #include <stdexcept>
  18. #include <alsa/asoundlib.h>
  19. #include "JackALSARawMidiDriver.h"
  20. #include "JackEngineControl.h"
  21. #include "JackError.h"
  22. #include "JackMidiUtil.h"
  23. using Jack::JackALSARawMidiDriver;
  24. JackALSARawMidiDriver::JackALSARawMidiDriver(const char *name,
  25. const char *alias,
  26. JackLockedEngine *engine,
  27. JackSynchro *table):
  28. JackMidiDriver(name, alias, engine, table)
  29. {
  30. thread = new JackThread(this);
  31. fCaptureChannels = 0;
  32. fds[0] = -1;
  33. fds[1] = -1;
  34. fPlaybackChannels = 0;
  35. input_ports = 0;
  36. output_ports = 0;
  37. poll_fds = 0;
  38. }
  39. JackALSARawMidiDriver::~JackALSARawMidiDriver()
  40. {
  41. delete thread;
  42. }
  43. int
  44. JackALSARawMidiDriver::Attach()
  45. {
  46. jack_nframes_t buffer_size = fEngineControl->fBufferSize;
  47. jack_port_id_t index;
  48. jack_nframes_t latency = buffer_size;
  49. jack_latency_range_t latency_range;
  50. const char *name;
  51. JackPort *port;
  52. latency_range.max = latency;
  53. latency_range.min = latency;
  54. for (int i = 0; i < fCaptureChannels; i++) {
  55. JackALSARawMidiInputPort *input_port = input_ports[i];
  56. name = input_port->GetName();
  57. index = fGraphManager->AllocatePort(fClientControl.fRefNum, name,
  58. JACK_DEFAULT_MIDI_TYPE,
  59. CaptureDriverFlags, buffer_size);
  60. if (index == NO_PORT) {
  61. jack_error("JackALSARawMidiDriver::Attach - cannot register input "
  62. "port with name '%s'.", name);
  63. // X: Do we need to deallocate ports?
  64. return -1;
  65. }
  66. port = fGraphManager->GetPort(index);
  67. port->SetAlias(input_port->GetAlias());
  68. port->SetLatencyRange(JackCaptureLatency, &latency_range);
  69. fCapturePortList[i] = index;
  70. }
  71. if (! fEngineControl->fSyncMode) {
  72. latency += buffer_size;
  73. latency_range.max = latency;
  74. latency_range.min = latency;
  75. }
  76. for (int i = 0; i < fPlaybackChannels; i++) {
  77. JackALSARawMidiOutputPort *output_port = output_ports[i];
  78. name = output_port->GetName();
  79. index = fGraphManager->AllocatePort(fClientControl.fRefNum, name,
  80. JACK_DEFAULT_MIDI_TYPE,
  81. PlaybackDriverFlags, buffer_size);
  82. if (index == NO_PORT) {
  83. jack_error("JackALSARawMidiDriver::Attach - cannot register "
  84. "output port with name '%s'.", name);
  85. // X: Do we need to deallocate ports?
  86. return -1;
  87. }
  88. port = fGraphManager->GetPort(index);
  89. port->SetAlias(output_port->GetAlias());
  90. port->SetLatencyRange(JackPlaybackLatency, &latency_range);
  91. fPlaybackPortList[i] = index;
  92. }
  93. return 0;
  94. }
  95. int
  96. JackALSARawMidiDriver::Close()
  97. {
  98. // Generic MIDI driver close
  99. int result = JackMidiDriver::Close();
  100. if (input_ports) {
  101. for (int i = 0; i < fCaptureChannels; i++) {
  102. delete input_ports[i];
  103. }
  104. delete[] input_ports;
  105. input_ports = 0;
  106. }
  107. if (output_ports) {
  108. for (int i = 0; i < fPlaybackChannels; i++) {
  109. delete output_ports[i];
  110. }
  111. delete[] output_ports;
  112. output_ports = 0;
  113. }
  114. return result;
  115. }
  116. bool
  117. JackALSARawMidiDriver::Execute()
  118. {
  119. jack_nframes_t timeout_frame = 0;
  120. for (;;) {
  121. jack_nframes_t process_frame;
  122. unsigned short revents;
  123. jack_nframes_t *timeout_frame_ptr;
  124. if (! timeout_frame) {
  125. timeout_frame_ptr = 0;
  126. } else {
  127. timeout_frame_ptr = &timeout_frame;
  128. }
  129. if (Poll(timeout_frame_ptr) == -1) {
  130. if (errno == EINTR) {
  131. continue;
  132. }
  133. jack_error("JackALSARawMidiDriver::Execute - poll error: %s",
  134. strerror(errno));
  135. break;
  136. }
  137. revents = poll_fds[0].revents;
  138. if (revents & POLLHUP) {
  139. // Driver is being stopped.
  140. break;
  141. }
  142. if (revents & (~ POLLIN)) {
  143. jack_error("JackALSARawMidiDriver::Execute - unexpected poll "
  144. "event on pipe file descriptor.");
  145. break;
  146. }
  147. timeout_frame = 0;
  148. for (int i = 0; i < fCaptureChannels; i++) {
  149. if (! input_ports[i]->ProcessALSA(&process_frame)) {
  150. jack_error("JackALSARawMidiDriver::Execute - a fatal error "
  151. "occurred while processing ALSA input events.");
  152. goto cleanup;
  153. }
  154. if (process_frame && ((! timeout_frame) ||
  155. (process_frame < timeout_frame))) {
  156. timeout_frame = process_frame;
  157. }
  158. }
  159. for (int i = 0; i < fPlaybackChannels; i++) {
  160. if (! output_ports[i]->ProcessALSA(fds[0], &process_frame)) {
  161. jack_error("JackALSARawMidiDriver::Execute - a fatal error "
  162. "occurred while processing ALSA output events.");
  163. goto cleanup;
  164. }
  165. if (process_frame && ((! timeout_frame) ||
  166. (process_frame < timeout_frame))) {
  167. timeout_frame = process_frame;
  168. }
  169. }
  170. }
  171. cleanup:
  172. close(fds[0]);
  173. fds[0] = -1;
  174. jack_info("JackALSARawMidiDriver::Execute - ALSA thread exiting.");
  175. return false;
  176. }
  177. void
  178. JackALSARawMidiDriver::
  179. GetDeviceInfo(snd_ctl_t *control, snd_rawmidi_info_t *info,
  180. std::vector<snd_rawmidi_info_t *> *info_list)
  181. {
  182. snd_rawmidi_info_set_subdevice(info, 0);
  183. int code = snd_ctl_rawmidi_info(control, info);
  184. if (code) {
  185. if (code != -ENOENT) {
  186. HandleALSAError("GetDeviceInfo", "snd_ctl_rawmidi_info", code);
  187. }
  188. return;
  189. }
  190. unsigned int count = snd_rawmidi_info_get_subdevices_count(info);
  191. for (unsigned int i = 0; i < count; i++) {
  192. snd_rawmidi_info_set_subdevice(info, i);
  193. int code = snd_ctl_rawmidi_info(control, info);
  194. if (code) {
  195. HandleALSAError("GetDeviceInfo", "snd_ctl_rawmidi_info", code);
  196. continue;
  197. }
  198. snd_rawmidi_info_t *info_copy;
  199. code = snd_rawmidi_info_malloc(&info_copy);
  200. if (code) {
  201. HandleALSAError("GetDeviceInfo", "snd_rawmidi_info_malloc", code);
  202. continue;
  203. }
  204. snd_rawmidi_info_copy(info_copy, info);
  205. try {
  206. info_list->push_back(info_copy);
  207. } catch (std::bad_alloc &e) {
  208. snd_rawmidi_info_free(info_copy);
  209. jack_error("JackALSARawMidiDriver::GetDeviceInfo - "
  210. "std::vector::push_back: %s", e.what());
  211. }
  212. }
  213. }
  214. void
  215. JackALSARawMidiDriver::HandleALSAError(const char *driver_func,
  216. const char *alsa_func, int code)
  217. {
  218. jack_error("JackALSARawMidiDriver::%s - %s: %s", driver_func, alsa_func,
  219. snd_strerror(code));
  220. }
  221. bool
  222. JackALSARawMidiDriver::Init()
  223. {
  224. set_threaded_log_function();
  225. if (thread->AcquireSelfRealTime(fEngineControl->fServerPriority + 1)) {
  226. jack_error("JackALSARawMidiDriver::Init - could not acquire realtime "
  227. "scheduling. Continuing anyway.");
  228. }
  229. return true;
  230. }
  231. int
  232. JackALSARawMidiDriver::Open(bool capturing, bool playing, int in_channels,
  233. int out_channels, bool monitor,
  234. const char *capture_driver_name,
  235. const char *playback_driver_name,
  236. jack_nframes_t capture_latency,
  237. jack_nframes_t playback_latency)
  238. {
  239. snd_rawmidi_info_t *info;
  240. int code = snd_rawmidi_info_malloc(&info);
  241. if (code) {
  242. HandleALSAError("Open", "snd_rawmidi_info_malloc", code);
  243. return -1;
  244. }
  245. std::vector<snd_rawmidi_info_t *> in_info_list;
  246. std::vector<snd_rawmidi_info_t *> out_info_list;
  247. for (int card = -1;;) {
  248. int code = snd_card_next(&card);
  249. if (code) {
  250. HandleALSAError("Open", "snd_card_next", code);
  251. continue;
  252. }
  253. if (card == -1) {
  254. break;
  255. }
  256. char name[32];
  257. snprintf(name, sizeof(name), "hw:%d", card);
  258. snd_ctl_t *control;
  259. code = snd_ctl_open(&control, name, SND_CTL_NONBLOCK);
  260. if (code) {
  261. HandleALSAError("Open", "snd_ctl_open", code);
  262. continue;
  263. }
  264. for (int device = -1;;) {
  265. code = snd_ctl_rawmidi_next_device(control, &device);
  266. if (code) {
  267. HandleALSAError("Open", "snd_ctl_rawmidi_next_device", code);
  268. continue;
  269. }
  270. if (device == -1) {
  271. break;
  272. }
  273. snd_rawmidi_info_set_device(info, device);
  274. snd_rawmidi_info_set_stream(info, SND_RAWMIDI_STREAM_INPUT);
  275. GetDeviceInfo(control, info, &in_info_list);
  276. snd_rawmidi_info_set_stream(info, SND_RAWMIDI_STREAM_OUTPUT);
  277. GetDeviceInfo(control, info, &out_info_list);
  278. }
  279. snd_ctl_close(control);
  280. }
  281. snd_rawmidi_info_free(info);
  282. size_t potential_inputs = in_info_list.size();
  283. size_t potential_outputs = out_info_list.size();
  284. if (! (potential_inputs || potential_outputs)) {
  285. jack_error("JackALSARawMidiDriver::Open - no ALSA raw MIDI input or "
  286. "output ports found.");
  287. return -1;
  288. }
  289. // XXX: Can't use auto_ptr here. These are arrays, and require the
  290. // delete[] operator.
  291. std::auto_ptr<JackALSARawMidiInputPort *> input_ptr;
  292. if (potential_inputs) {
  293. input_ports = new JackALSARawMidiInputPort *[potential_inputs];
  294. input_ptr.reset(input_ports);
  295. }
  296. std::auto_ptr<JackALSARawMidiOutputPort *> output_ptr;
  297. if (potential_outputs) {
  298. output_ports = new JackALSARawMidiOutputPort *[potential_outputs];
  299. output_ptr.reset(output_ports);
  300. }
  301. size_t num_inputs = 0;
  302. size_t num_outputs = 0;
  303. for (size_t i = 0; i < potential_inputs; i++) {
  304. snd_rawmidi_info_t *info = in_info_list.at(i);
  305. try {
  306. input_ports[num_inputs] = new JackALSARawMidiInputPort(info, i);
  307. jack_info("JackALSARawMidiDriver::Open - Input port: card=%d, "
  308. "device=%d, subdevice=%d, id=%s, name=%s, subdevice "
  309. "name=%s",
  310. snd_rawmidi_info_get_card(info),
  311. snd_rawmidi_info_get_device(info),
  312. snd_rawmidi_info_get_subdevice(info),
  313. snd_rawmidi_info_get_id(info),
  314. snd_rawmidi_info_get_name(info),
  315. snd_rawmidi_info_get_subdevice_name(info));
  316. num_inputs++;
  317. } catch (std::exception e) {
  318. jack_error("JackALSARawMidiDriver::Open - while creating new "
  319. "JackALSARawMidiInputPort: %s", e.what());
  320. }
  321. snd_rawmidi_info_free(info);
  322. }
  323. for (size_t i = 0; i < potential_outputs; i++) {
  324. snd_rawmidi_info_t *info = out_info_list.at(i);
  325. try {
  326. output_ports[num_outputs] = new JackALSARawMidiOutputPort(info, i);
  327. jack_info("JackALSARawMidiDriver::Open - Output port: card=%d, "
  328. "device=%d, subdevice=%d, id=%s, name=%s, subdevice "
  329. "name=%s",
  330. snd_rawmidi_info_get_card(info),
  331. snd_rawmidi_info_get_device(info),
  332. snd_rawmidi_info_get_subdevice(info),
  333. snd_rawmidi_info_get_id(info),
  334. snd_rawmidi_info_get_name(info),
  335. snd_rawmidi_info_get_subdevice_name(info));
  336. num_outputs++;
  337. } catch (std::exception e) {
  338. jack_error("JackALSARawMidiDriver::Open - while creating new "
  339. "JackALSARawMidiOutputPort: %s", e.what());
  340. }
  341. snd_rawmidi_info_free(info);
  342. }
  343. if (num_inputs || num_outputs) {
  344. if (! JackMidiDriver::Open(capturing, playing, num_inputs, num_outputs,
  345. monitor, capture_driver_name,
  346. playback_driver_name, capture_latency,
  347. playback_latency)) {
  348. if (potential_inputs) {
  349. input_ptr.release();
  350. }
  351. if (potential_outputs) {
  352. output_ptr.release();
  353. }
  354. return 0;
  355. }
  356. jack_error("JackALSARawMidiDriver::Open - JackMidiDriver::Open error");
  357. } else {
  358. jack_error("JackALSARawMidiDriver::Open - none of the potential "
  359. "inputs or outputs were successfully opened.");
  360. }
  361. Close();
  362. return -1;
  363. }
  364. int
  365. JackALSARawMidiDriver::Poll(const jack_nframes_t *wakeup_frame)
  366. {
  367. struct timespec timeout;
  368. struct timespec *timeout_ptr;
  369. if (! wakeup_frame) {
  370. timeout_ptr = 0;
  371. } else {
  372. timeout_ptr = &timeout;
  373. jack_time_t next_time = GetTimeFromFrames(*wakeup_frame);
  374. jack_time_t now = GetMicroSeconds();
  375. if (next_time <= now) {
  376. timeout.tv_sec = 0;
  377. timeout.tv_nsec = 0;
  378. } else {
  379. jack_time_t wait_time = next_time - now;
  380. timeout.tv_sec = wait_time / 1000000;
  381. timeout.tv_nsec = (wait_time % 1000000) * 1000;
  382. }
  383. }
  384. return ppoll(poll_fds, poll_fd_count, timeout_ptr, 0);
  385. }
  386. int
  387. JackALSARawMidiDriver::Read()
  388. {
  389. jack_nframes_t buffer_size = fEngineControl->fBufferSize;
  390. for (int i = 0; i < fCaptureChannels; i++) {
  391. if (! input_ports[i]->ProcessJack(GetInputBuffer(i), buffer_size)) {
  392. return -1;
  393. }
  394. }
  395. return 0;
  396. }
  397. int
  398. JackALSARawMidiDriver::Start()
  399. {
  400. jack_info("JackALSARawMidiDriver::Start - Starting 'alsarawmidi' driver.");
  401. JackMidiDriver::Start();
  402. poll_fd_count = 1;
  403. for (int i = 0; i < fCaptureChannels; i++) {
  404. poll_fd_count += input_ports[i]->GetPollDescriptorCount();
  405. }
  406. for (int i = 0; i < fPlaybackChannels; i++) {
  407. poll_fd_count += output_ports[i]->GetPollDescriptorCount();
  408. }
  409. try {
  410. poll_fds = new pollfd[poll_fd_count];
  411. } catch (std::bad_alloc e) {
  412. jack_error("JackALSARawMidiDriver::Start - creating poll descriptor "
  413. "structures failed: %s", e.what());
  414. return -1;
  415. }
  416. int flags;
  417. struct pollfd *poll_fd_iter;
  418. if (pipe(fds) == -1) {
  419. jack_error("JackALSARawMidiDriver::Start - while creating wake pipe: "
  420. "%s", strerror(errno));
  421. goto free_poll_descriptors;
  422. }
  423. flags = fcntl(fds[0], F_GETFL);
  424. if (flags == -1) {
  425. jack_error("JackALSARawMidiDriver::Start = while getting flags for "
  426. "read file descriptor: %s", strerror(errno));
  427. goto close_fds;
  428. }
  429. if (fcntl(fds[0], F_SETFL, flags | O_NONBLOCK) == -1) {
  430. jack_error("JackALSARawMidiDriver::Start - while setting non-blocking "
  431. "mode for read file descriptor: %s", strerror(errno));
  432. goto close_fds;
  433. }
  434. flags = fcntl(fds[1], F_GETFL);
  435. if (flags == -1) {
  436. jack_error("JackALSARawMidiDriver::Start = while getting flags for "
  437. "write file descriptor: %s", strerror(errno));
  438. goto close_fds;
  439. }
  440. if (fcntl(fds[1], F_SETFL, flags | O_NONBLOCK) == -1) {
  441. jack_error("JackALSARawMidiDriver::Start - while setting non-blocking "
  442. "mode for write file descriptor: %s", strerror(errno));
  443. goto close_fds;
  444. }
  445. poll_fds[0].events = POLLERR | POLLIN | POLLNVAL;
  446. poll_fds[0].fd = fds[0];
  447. poll_fd_iter = poll_fds + 1;
  448. for (int i = 0; i < fCaptureChannels; i++) {
  449. JackALSARawMidiInputPort *input_port = input_ports[i];
  450. input_port->PopulatePollDescriptors(poll_fd_iter);
  451. poll_fd_iter += input_port->GetPollDescriptorCount();
  452. }
  453. for (int i = 0; i < fPlaybackChannels; i++) {
  454. JackALSARawMidiOutputPort *output_port = output_ports[i];
  455. output_port->PopulatePollDescriptors(poll_fd_iter);
  456. poll_fd_iter += output_port->GetPollDescriptorCount();
  457. }
  458. jack_info("JackALSARawMidiDriver::Start - starting ALSA thread ...");
  459. if (! thread->StartSync()) {
  460. jack_info("JackALSARawMidiDriver::Start - started ALSA thread.");
  461. return 0;
  462. }
  463. jack_error("JackALSARawMidiDriver::Start - failed to start MIDI "
  464. "processing thread.");
  465. close_fds:
  466. close(fds[1]);
  467. fds[1] = -1;
  468. close(fds[0]);
  469. fds[0] = -1;
  470. free_poll_descriptors:
  471. delete[] poll_fds;
  472. poll_fds = 0;
  473. return -1;
  474. }
  475. int
  476. JackALSARawMidiDriver::Stop()
  477. {
  478. jack_info("JackALSARawMidiDriver::Stop - stopping 'alsarawmidi' driver.");
  479. if (fds[1] != -1) {
  480. close(fds[1]);
  481. fds[1] = -1;
  482. }
  483. int result;
  484. const char *verb;
  485. switch (thread->GetStatus()) {
  486. case JackThread::kIniting:
  487. case JackThread::kStarting:
  488. result = thread->Kill();
  489. verb = "kill";
  490. break;
  491. case JackThread::kRunning:
  492. result = thread->Stop();
  493. verb = "stop";
  494. break;
  495. default:
  496. result = 0;
  497. verb = 0;
  498. }
  499. if (fds[0] != -1) {
  500. close(fds[0]);
  501. fds[0] = -1;
  502. }
  503. if (poll_fds) {
  504. delete[] poll_fds;
  505. poll_fds = 0;
  506. }
  507. if (result) {
  508. jack_error("JackALSARawMidiDriver::Stop - could not %s MIDI "
  509. "processing thread.", verb);
  510. }
  511. return result;
  512. }
  513. int
  514. JackALSARawMidiDriver::Write()
  515. {
  516. jack_nframes_t buffer_size = fEngineControl->fBufferSize;
  517. int write_fd = fds[1];
  518. for (int i = 0; i < fPlaybackChannels; i++) {
  519. if (! output_ports[i]->ProcessJack(GetOutputBuffer(i), buffer_size,
  520. write_fd)) {
  521. return -1;
  522. }
  523. }
  524. return 0;
  525. }
  526. #ifdef __cplusplus
  527. extern "C" {
  528. #endif
  529. SERVER_EXPORT jack_driver_desc_t *
  530. driver_get_descriptor()
  531. {
  532. jack_driver_desc_t *desc =
  533. (jack_driver_desc_t *) malloc(sizeof(jack_driver_desc_t));
  534. if (desc) {
  535. strcpy(desc->desc, "Alternative ALSA raw MIDI backend.");
  536. strcpy(desc->name, "alsarawmidi");
  537. // X: There could be parameters here regarding setting I/O buffer
  538. // sizes. I don't think MIDI drivers can accept parameters right
  539. // now without being set as the main driver.
  540. desc->nparams = 0;
  541. desc->params = 0;
  542. }
  543. return desc;
  544. }
  545. SERVER_EXPORT Jack::JackDriverClientInterface *
  546. driver_initialize(Jack::JackLockedEngine *engine, Jack::JackSynchro *table,
  547. const JSList *params)
  548. {
  549. Jack::JackDriverClientInterface *driver =
  550. new Jack::JackALSARawMidiDriver("system_midi", "alsarawmidi",
  551. engine, table);
  552. if (driver->Open(1, 1, 0, 0, false, "midi in", "midi out", 0, 0)) {
  553. delete driver;
  554. driver = 0;
  555. }
  556. return driver;
  557. }
  558. #ifdef __cplusplus
  559. }
  560. #endif