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.

452 lines
15KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2004-2008 Grame.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  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. (at your option) any later version.
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include "JackSystemDeps.h"
  17. #include "JackAudioDriver.h"
  18. #include "JackTime.h"
  19. #include "JackError.h"
  20. #include "JackEngineControl.h"
  21. #include "JackPort.h"
  22. #include "JackGraphManager.h"
  23. #include "JackLockedEngine.h"
  24. #include "JackException.h"
  25. #include <assert.h>
  26. namespace Jack
  27. {
  28. JackAudioDriver::JackAudioDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table)
  29. : JackDriver(name, alias, engine, table),
  30. fCaptureChannels(0),
  31. fPlaybackChannels(0),
  32. fWithMonitorPorts(false)
  33. {}
  34. JackAudioDriver::~JackAudioDriver()
  35. {}
  36. int JackAudioDriver::SetBufferSize(jack_nframes_t buffer_size)
  37. {
  38. // Update engine and graph manager state
  39. fEngineControl->fBufferSize = buffer_size;
  40. fGraphManager->SetBufferSize(buffer_size);
  41. fEngineControl->fPeriodUsecs = jack_time_t(1000000.f / fEngineControl->fSampleRate * fEngineControl->fBufferSize); // in microsec
  42. if (!fEngineControl->fTimeOut)
  43. fEngineControl->fTimeOutUsecs = jack_time_t(2.f * fEngineControl->fPeriodUsecs);
  44. UpdateLatencies();
  45. // Redirect on slaves drivers...
  46. return JackDriver::SetBufferSize(buffer_size);
  47. }
  48. int JackAudioDriver::SetSampleRate(jack_nframes_t sample_rate)
  49. {
  50. fEngineControl->fSampleRate = sample_rate;
  51. fEngineControl->fPeriodUsecs = jack_time_t(1000000.f / fEngineControl->fSampleRate * fEngineControl->fBufferSize); // in microsec
  52. if (!fEngineControl->fTimeOut)
  53. fEngineControl->fTimeOutUsecs = jack_time_t(2.f * fEngineControl->fPeriodUsecs);
  54. return JackDriver::SetSampleRate(sample_rate);
  55. }
  56. int JackAudioDriver::Open(jack_nframes_t buffer_size,
  57. jack_nframes_t samplerate,
  58. bool capturing,
  59. bool playing,
  60. int inchannels,
  61. int outchannels,
  62. bool monitor,
  63. const char* capture_driver_name,
  64. const char* playback_driver_name,
  65. jack_nframes_t capture_latency,
  66. jack_nframes_t playback_latency)
  67. {
  68. fCaptureChannels = inchannels;
  69. fPlaybackChannels = outchannels;
  70. fWithMonitorPorts = monitor;
  71. memset(fCapturePortList, 0, sizeof(jack_port_id_t) * DRIVER_PORT_NUM);
  72. memset(fPlaybackPortList, 0, sizeof(jack_port_id_t) * DRIVER_PORT_NUM);
  73. memset(fMonitorPortList, 0, sizeof(jack_port_id_t) * DRIVER_PORT_NUM);
  74. return JackDriver::Open(buffer_size, samplerate, capturing, playing, inchannels, outchannels, monitor, capture_driver_name, playback_driver_name, capture_latency, playback_latency);
  75. }
  76. int JackAudioDriver::Open(bool capturing,
  77. bool playing,
  78. int inchannels,
  79. int outchannels,
  80. bool monitor,
  81. const char* capture_driver_name,
  82. const char* playback_driver_name,
  83. jack_nframes_t capture_latency,
  84. jack_nframes_t playback_latency)
  85. {
  86. fCaptureChannels = inchannels;
  87. fPlaybackChannels = outchannels;
  88. fWithMonitorPorts = monitor;
  89. memset(fCapturePortList, 0, sizeof(jack_port_id_t) * DRIVER_PORT_NUM);
  90. memset(fPlaybackPortList, 0, sizeof(jack_port_id_t) * DRIVER_PORT_NUM);
  91. memset(fMonitorPortList, 0, sizeof(jack_port_id_t) * DRIVER_PORT_NUM);
  92. return JackDriver::Open(capturing, playing, inchannels, outchannels, monitor, capture_driver_name, playback_driver_name, capture_latency, playback_latency);
  93. }
  94. void JackAudioDriver::UpdateLatencies()
  95. {
  96. jack_latency_range_t range;
  97. for (int i = 0; i < fCaptureChannels; i++) {
  98. range.max = range.min = fEngineControl->fBufferSize;
  99. fGraphManager->GetPort(fCapturePortList[i])->SetLatencyRange(JackCaptureLatency, &range);
  100. }
  101. for (int i = 0; i < fPlaybackChannels; i++) {
  102. if (! fEngineControl->fSyncMode) {
  103. range.max = range.min = fEngineControl->fBufferSize * 2;
  104. }
  105. fGraphManager->GetPort(fPlaybackPortList[i])->SetLatencyRange(JackPlaybackLatency, &range);
  106. if (fWithMonitorPorts) {
  107. range.min = range.max = fEngineControl->fBufferSize;
  108. fGraphManager->GetPort(fMonitorPortList[i])->SetLatencyRange(JackCaptureLatency, &range);
  109. }
  110. }
  111. }
  112. int JackAudioDriver::Attach()
  113. {
  114. JackPort* port;
  115. jack_port_id_t port_index;
  116. char name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  117. char alias[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  118. int i;
  119. jack_log("JackAudioDriver::Attach fBufferSize = %ld fSampleRate = %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
  120. for (i = 0; i < fCaptureChannels; i++) {
  121. snprintf(alias, sizeof(alias) - 1, "%s:%s:out%d", fAliasName, fCaptureDriverName, i + 1);
  122. snprintf(name, sizeof(name) - 1, "%s:capture_%d", fClientControl.fName, i + 1);
  123. if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, CaptureDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
  124. jack_error("driver: cannot register port for %s", name);
  125. return -1;
  126. }
  127. port = fGraphManager->GetPort(port_index);
  128. port->SetAlias(alias);
  129. fCapturePortList[i] = port_index;
  130. jack_log("JackAudioDriver::Attach fCapturePortList[i] port_index = %ld", port_index);
  131. }
  132. for (i = 0; i < fPlaybackChannels; i++) {
  133. snprintf(alias, sizeof(alias) - 1, "%s:%s:in%d", fAliasName, fPlaybackDriverName, i + 1);
  134. snprintf(name, sizeof(name) - 1, "%s:playback_%d", fClientControl.fName, i + 1);
  135. if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, PlaybackDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
  136. jack_error("driver: cannot register port for %s", name);
  137. return -1;
  138. }
  139. port = fGraphManager->GetPort(port_index);
  140. port->SetAlias(alias);
  141. fPlaybackPortList[i] = port_index;
  142. jack_log("JackAudioDriver::Attach fPlaybackPortList[i] port_index = %ld", port_index);
  143. // Monitor ports
  144. if (fWithMonitorPorts) {
  145. jack_log("Create monitor port");
  146. snprintf(name, sizeof(name) - 1, "%s:monitor_%u", fClientControl.fName, i + 1);
  147. if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, fEngineControl->fBufferSize, &port_index) < 0) {
  148. jack_error("Cannot register monitor port for %s", name);
  149. return -1;
  150. } else {
  151. fMonitorPortList[i] = port_index;
  152. }
  153. }
  154. }
  155. UpdateLatencies();
  156. return 0;
  157. }
  158. int JackAudioDriver::Detach()
  159. {
  160. int i;
  161. jack_log("JackAudioDriver::Detach");
  162. for (i = 0; i < fCaptureChannels; i++) {
  163. fEngine->PortUnRegister(fClientControl.fRefNum, fCapturePortList[i]);
  164. }
  165. for (i = 0; i < fPlaybackChannels; i++) {
  166. fEngine->PortUnRegister(fClientControl.fRefNum, fPlaybackPortList[i]);
  167. if (fWithMonitorPorts) {
  168. fEngine->PortUnRegister(fClientControl.fRefNum, fMonitorPortList[i]);
  169. }
  170. }
  171. return 0;
  172. }
  173. int JackAudioDriver::Write()
  174. {
  175. for (int i = 0; i < fPlaybackChannels; i++) {
  176. if (fGraphManager->GetConnectionsNum(fPlaybackPortList[i]) > 0) {
  177. jack_default_audio_sample_t* buffer = GetOutputBuffer(i);
  178. int size = sizeof(jack_default_audio_sample_t) * fEngineControl->fBufferSize;
  179. // Monitor ports
  180. if (fWithMonitorPorts && fGraphManager->GetConnectionsNum(fMonitorPortList[i]) > 0)
  181. memcpy(GetMonitorBuffer(i), buffer, size);
  182. }
  183. }
  184. return 0;
  185. }
  186. int JackAudioDriver::ProcessNull()
  187. {
  188. // Keep begin cycle time
  189. JackDriver::CycleTakeBeginTime();
  190. if (fEngineControl->fSyncMode) {
  191. ProcessGraphSyncMaster();
  192. } else {
  193. ProcessGraphAsyncMaster();
  194. }
  195. // Keep end cycle time
  196. JackDriver::CycleTakeEndTime();
  197. WaitUntilNextCycle();
  198. return 0;
  199. }
  200. int JackAudioDriver::Process()
  201. {
  202. return (fEngineControl->fSyncMode) ? ProcessSync() : ProcessAsync();
  203. }
  204. /*
  205. The driver ASYNC mode: output buffers computed at the *previous cycle* are used, the server does not
  206. synchronize to the end of client graph execution.
  207. */
  208. int JackAudioDriver::ProcessAsync()
  209. {
  210. // Read input buffers for the current cycle
  211. if (Read() < 0) {
  212. jack_error("JackAudioDriver::ProcessAsync: read error, stopping...");
  213. return -1;
  214. }
  215. // Write output buffers from the previous cycle
  216. if (Write() < 0) {
  217. jack_error("JackAudioDriver::ProcessAsync: write error, stopping...");
  218. return -1;
  219. }
  220. // Process graph
  221. if (fIsMaster) {
  222. ProcessGraphAsyncMaster();
  223. } else {
  224. ProcessGraphAsyncSlave();
  225. }
  226. // Keep end cycle time
  227. JackDriver::CycleTakeEndTime();
  228. return 0;
  229. }
  230. /*
  231. The driver SYNC mode: the server does synchronize to the end of client graph execution,
  232. if graph process succeed, output buffers computed at the *current cycle* are used.
  233. */
  234. int JackAudioDriver::ProcessSync()
  235. {
  236. // Read input buffers for the current cycle
  237. if (Read() < 0) {
  238. jack_error("JackAudioDriver::ProcessSync: read error, stopping...");
  239. return -1;
  240. }
  241. // Process graph
  242. if (fIsMaster) {
  243. if (ProcessGraphSyncMaster() < 0) {
  244. jack_error("JackAudioDriver::ProcessSync: process error, skip cycle...");
  245. goto end;
  246. }
  247. } else {
  248. if (ProcessGraphSyncSlave() < 0) {
  249. jack_error("JackAudioDriver::ProcessSync: process error, skip cycle...");
  250. goto end;
  251. }
  252. }
  253. // Write output buffers from the current cycle
  254. if (Write() < 0) {
  255. jack_error("JackAudioDriver::ProcessSync: write error, stopping...");
  256. return -1;
  257. }
  258. end:
  259. // Keep end cycle time
  260. JackDriver::CycleTakeEndTime();
  261. return 0;
  262. }
  263. void JackAudioDriver::ProcessGraphAsyncMaster()
  264. {
  265. // fBeginDateUst is set in the "low level" layer, fEndDateUst is from previous cycle
  266. if (!fEngine->Process(fBeginDateUst, fEndDateUst))
  267. jack_error("JackAudioDriver::ProcessGraphAsyncMaster: Process error");
  268. if (fGraphManager->ResumeRefNum(&fClientControl, fSynchroTable) < 0)
  269. jack_error("JackAudioDriver::ProcessGraphAsyncMaster: ResumeRefNum error");
  270. if (ProcessReadSlaves() < 0)
  271. jack_error("JackAudioDriver::ProcessGraphAsyncMaster: ProcessReadSlaves error");
  272. if (ProcessWriteSlaves() < 0)
  273. jack_error("JackAudioDriver::ProcessGraphAsyncMaster: ProcessWriteSlaves error");
  274. }
  275. void JackAudioDriver::ProcessGraphAsyncSlave()
  276. {
  277. if (fGraphManager->ResumeRefNum(&fClientControl, fSynchroTable) < 0)
  278. jack_error("JackAudioDriver::ProcessGraphAsyncSlave: ResumeRefNum error");
  279. }
  280. int JackAudioDriver::ProcessGraphSyncMaster()
  281. {
  282. int res = 0;
  283. // fBeginDateUst is set in the "low level" layer, fEndDateUst is from previous cycle
  284. if (fEngine->Process(fBeginDateUst, fEndDateUst)) {
  285. if (fGraphManager->ResumeRefNum(&fClientControl, fSynchroTable) < 0) {
  286. jack_error("JackAudioDriver::ProcessGraphSyncMaster: ResumeRefNum error");
  287. res = -1;
  288. }
  289. if (ProcessReadSlaves() < 0) {
  290. jack_error("JackAudioDriver::ProcessGraphSync: ProcessReadSlaves error, engine may now behave abnormally!!");
  291. res = -1;
  292. }
  293. if (ProcessWriteSlaves() < 0) {
  294. jack_error("JackAudioDriver::ProcessGraphSync: ProcessWriteSlaves error, engine may now behave abnormally!!");
  295. res = -1;
  296. }
  297. if (fGraphManager->SuspendRefNum(&fClientControl, fSynchroTable, DRIVER_TIMEOUT_FACTOR * fEngineControl->fTimeOutUsecs) < 0) {
  298. jack_error("JackAudioDriver::ProcessGraphSync: SuspendRefNum error, engine may now behave abnormally!!");
  299. res = -1;
  300. }
  301. } else { // Graph not finished: do not activate it
  302. jack_error("JackAudioDriver::ProcessGraphSync: Process error");
  303. res = -1;
  304. }
  305. return res;
  306. }
  307. int JackAudioDriver::ProcessGraphSyncSlave()
  308. {
  309. return fGraphManager->ResumeRefNum(&fClientControl, fSynchroTable);
  310. }
  311. int JackAudioDriver::Start()
  312. {
  313. int res = JackDriver::Start();
  314. if ((res >= 0) && fIsMaster) {
  315. res = StartSlaves();
  316. }
  317. return res;
  318. }
  319. int JackAudioDriver::Stop()
  320. {
  321. int res = JackDriver::Stop();
  322. if (fIsMaster) {
  323. if (StopSlaves() < 0) {
  324. res = -1;
  325. }
  326. }
  327. return res;
  328. }
  329. void JackAudioDriver::WaitUntilNextCycle()
  330. {
  331. int wait_time_usec = (int((float(fEngineControl->fBufferSize) / (float(fEngineControl->fSampleRate))) * 1000000.0f));
  332. wait_time_usec = int(wait_time_usec - (GetMicroSeconds() - fBeginDateUst));
  333. if (wait_time_usec > 0)
  334. JackSleep(wait_time_usec);
  335. }
  336. jack_default_audio_sample_t* JackAudioDriver::GetInputBuffer(int port_index)
  337. {
  338. return fCapturePortList[port_index]
  339. ? (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fCapturePortList[port_index], fEngineControl->fBufferSize)
  340. : NULL;
  341. }
  342. jack_default_audio_sample_t* JackAudioDriver::GetOutputBuffer(int port_index)
  343. {
  344. return fPlaybackPortList[port_index]
  345. ? (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fPlaybackPortList[port_index], fEngineControl->fBufferSize)
  346. : NULL;
  347. }
  348. jack_default_audio_sample_t* JackAudioDriver::GetMonitorBuffer(int port_index)
  349. {
  350. return fPlaybackPortList[port_index]
  351. ? (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fMonitorPortList[port_index], fEngineControl->fBufferSize)
  352. : NULL;
  353. }
  354. int JackAudioDriver::ClientNotify(int refnum, const char* name, int notify, int sync, const char* message, int value1, int value2)
  355. {
  356. switch (notify) {
  357. case kLatencyCallback:
  358. HandleLatencyCallback(value1);
  359. break;
  360. default:
  361. JackDriver::ClientNotify(refnum, name, notify, sync, message, value1, value2);
  362. break;
  363. }
  364. return 0;
  365. }
  366. void JackAudioDriver::HandleLatencyCallback(int status)
  367. {
  368. jack_latency_callback_mode_t mode = (status == 0) ? JackCaptureLatency : JackPlaybackLatency;
  369. for (int i = 0; i < fCaptureChannels; i++) {
  370. if (mode == JackPlaybackLatency) {
  371. fGraphManager->RecalculateLatency(fCapturePortList[i], mode);
  372. }
  373. }
  374. for (int i = 0; i < fPlaybackChannels; i++) {
  375. if (mode == JackCaptureLatency) {
  376. fGraphManager->RecalculateLatency(fPlaybackPortList[i], mode);
  377. }
  378. }
  379. }
  380. } // end of namespace