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.

458 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 ((port_index = fGraphManager->AllocatePort(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, CaptureDriverFlags, fEngineControl->fBufferSize)) == NO_PORT) {
  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. fEngine->NotifyPortRegistration(port_index, true);
  132. }
  133. for (i = 0; i < fPlaybackChannels; i++) {
  134. snprintf(alias, sizeof(alias) - 1, "%s:%s:in%d", fAliasName, fPlaybackDriverName, i + 1);
  135. snprintf(name, sizeof(name) - 1, "%s:playback_%d", fClientControl.fName, i + 1);
  136. if ((port_index = fGraphManager->AllocatePort(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, PlaybackDriverFlags, fEngineControl->fBufferSize)) == NO_PORT) {
  137. jack_error("driver: cannot register port for %s", name);
  138. return -1;
  139. }
  140. port = fGraphManager->GetPort(port_index);
  141. port->SetAlias(alias);
  142. fPlaybackPortList[i] = port_index;
  143. jack_log("JackAudioDriver::Attach fPlaybackPortList[i] port_index = %ld", port_index);
  144. fEngine->NotifyPortRegistration(port_index, true);
  145. // Monitor ports
  146. if (fWithMonitorPorts) {
  147. jack_log("Create monitor port");
  148. snprintf(name, sizeof(name) - 1, "%s:monitor_%u", fClientControl.fName, i + 1);
  149. if ((port_index = fGraphManager->AllocatePort(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, fEngineControl->fBufferSize)) == NO_PORT) {
  150. jack_error("Cannot register monitor port for %s", name);
  151. return -1;
  152. } else {
  153. fMonitorPortList[i] = port_index;
  154. fEngine->NotifyPortRegistration(port_index, true);
  155. }
  156. }
  157. }
  158. UpdateLatencies();
  159. return 0;
  160. }
  161. int JackAudioDriver::Detach()
  162. {
  163. int i;
  164. jack_log("JackAudioDriver::Detach");
  165. for (i = 0; i < fCaptureChannels; i++) {
  166. fGraphManager->ReleasePort(fClientControl.fRefNum, fCapturePortList[i]);
  167. fEngine->NotifyPortRegistration(fCapturePortList[i], false);
  168. }
  169. for (i = 0; i < fPlaybackChannels; i++) {
  170. fGraphManager->ReleasePort(fClientControl.fRefNum, fPlaybackPortList[i]);
  171. fEngine->NotifyPortRegistration(fPlaybackPortList[i], false);
  172. if (fWithMonitorPorts) {
  173. fGraphManager->ReleasePort(fClientControl.fRefNum, fMonitorPortList[i]);
  174. fEngine->NotifyPortRegistration(fMonitorPortList[i], false);
  175. }
  176. }
  177. return 0;
  178. }
  179. int JackAudioDriver::Write()
  180. {
  181. for (int i = 0; i < fPlaybackChannels; i++) {
  182. if (fGraphManager->GetConnectionsNum(fPlaybackPortList[i]) > 0) {
  183. jack_default_audio_sample_t* buffer = GetOutputBuffer(i);
  184. int size = sizeof(jack_default_audio_sample_t) * fEngineControl->fBufferSize;
  185. // Monitor ports
  186. if (fWithMonitorPorts && fGraphManager->GetConnectionsNum(fMonitorPortList[i]) > 0)
  187. memcpy(GetMonitorBuffer(i), buffer, size);
  188. }
  189. }
  190. return 0;
  191. }
  192. int JackAudioDriver::ProcessNull()
  193. {
  194. // Keep begin cycle time
  195. JackDriver::CycleTakeBeginTime();
  196. if (fEngineControl->fSyncMode) {
  197. ProcessGraphSyncMaster();
  198. } else {
  199. ProcessGraphAsyncMaster();
  200. }
  201. // Keep end cycle time
  202. JackDriver::CycleTakeEndTime();
  203. WaitUntilNextCycle();
  204. return 0;
  205. }
  206. int JackAudioDriver::Process()
  207. {
  208. return (fEngineControl->fSyncMode) ? ProcessSync() : ProcessAsync();
  209. }
  210. /*
  211. The driver ASYNC mode: output buffers computed at the *previous cycle* are used, the server does not
  212. synchronize to the end of client graph execution.
  213. */
  214. int JackAudioDriver::ProcessAsync()
  215. {
  216. // Read input buffers for the current cycle
  217. if (Read() < 0) {
  218. jack_error("JackAudioDriver::ProcessAsync: read error, stopping...");
  219. return -1;
  220. }
  221. // Write output buffers from the previous cycle
  222. if (Write() < 0) {
  223. jack_error("JackAudioDriver::ProcessAsync: write error, stopping...");
  224. return -1;
  225. }
  226. // Process graph
  227. if (fIsMaster) {
  228. ProcessGraphAsyncMaster();
  229. } else {
  230. ProcessGraphAsyncSlave();
  231. }
  232. // Keep end cycle time
  233. JackDriver::CycleTakeEndTime();
  234. return 0;
  235. }
  236. /*
  237. The driver SYNC mode: the server does synchronize to the end of client graph execution,
  238. if graph process succeed, output buffers computed at the *current cycle* are used.
  239. */
  240. int JackAudioDriver::ProcessSync()
  241. {
  242. // Read input buffers for the current cycle
  243. if (Read() < 0) {
  244. jack_error("JackAudioDriver::ProcessSync: read error, stopping...");
  245. return -1;
  246. }
  247. // Process graph
  248. if (fIsMaster) {
  249. if (ProcessGraphSyncMaster() < 0) {
  250. jack_error("JackAudioDriver::ProcessSync: process error, skip cycle...");
  251. goto end;
  252. }
  253. } else {
  254. if (ProcessGraphSyncSlave() < 0) {
  255. jack_error("JackAudioDriver::ProcessSync: process error, skip cycle...");
  256. goto end;
  257. }
  258. }
  259. // Write output buffers from the current cycle
  260. if (Write() < 0) {
  261. jack_error("JackAudioDriver::ProcessSync: write error, stopping...");
  262. return -1;
  263. }
  264. end:
  265. // Keep end cycle time
  266. JackDriver::CycleTakeEndTime();
  267. return 0;
  268. }
  269. void JackAudioDriver::ProcessGraphAsyncMaster()
  270. {
  271. // fBeginDateUst is set in the "low level" layer, fEndDateUst is from previous cycle
  272. if (!fEngine->Process(fBeginDateUst, fEndDateUst))
  273. jack_error("JackAudioDriver::ProcessGraphAsyncMaster: Process error");
  274. if (fGraphManager->ResumeRefNum(&fClientControl, fSynchroTable) < 0)
  275. jack_error("JackAudioDriver::ProcessGraphAsyncMaster: ResumeRefNum error");
  276. if (ProcessReadSlaves() < 0)
  277. jack_error("JackAudioDriver::ProcessGraphAsyncMaster: ProcessReadSlaves error");
  278. if (ProcessWriteSlaves() < 0)
  279. jack_error("JackAudioDriver::ProcessGraphAsyncMaster: ProcessWriteSlaves error");
  280. }
  281. void JackAudioDriver::ProcessGraphAsyncSlave()
  282. {
  283. if (fGraphManager->ResumeRefNum(&fClientControl, fSynchroTable) < 0)
  284. jack_error("JackAudioDriver::ProcessGraphAsyncSlave: ResumeRefNum error");
  285. }
  286. int JackAudioDriver::ProcessGraphSyncMaster()
  287. {
  288. int res = 0;
  289. // fBeginDateUst is set in the "low level" layer, fEndDateUst is from previous cycle
  290. if (fEngine->Process(fBeginDateUst, fEndDateUst)) {
  291. if (fGraphManager->ResumeRefNum(&fClientControl, fSynchroTable) < 0) {
  292. jack_error("JackAudioDriver::ProcessGraphSyncMaster: ResumeRefNum error");
  293. res = -1;
  294. }
  295. if (ProcessReadSlaves() < 0) {
  296. jack_error("JackAudioDriver::ProcessGraphSync: ProcessReadSlaves error, engine may now behave abnormally!!");
  297. res = -1;
  298. }
  299. if (ProcessWriteSlaves() < 0) {
  300. jack_error("JackAudioDriver::ProcessGraphSync: ProcessWriteSlaves error, engine may now behave abnormally!!");
  301. res = -1;
  302. }
  303. if (fGraphManager->SuspendRefNum(&fClientControl, fSynchroTable, DRIVER_TIMEOUT_FACTOR * fEngineControl->fTimeOutUsecs) < 0) {
  304. jack_error("JackAudioDriver::ProcessGraphSync: SuspendRefNum error, engine may now behave abnormally!!");
  305. res = -1;
  306. }
  307. } else { // Graph not finished: do not activate it
  308. jack_error("JackAudioDriver::ProcessGraphSync: Process error");
  309. res = -1;
  310. }
  311. return res;
  312. }
  313. int JackAudioDriver::ProcessGraphSyncSlave()
  314. {
  315. return fGraphManager->ResumeRefNum(&fClientControl, fSynchroTable);
  316. }
  317. int JackAudioDriver::Start()
  318. {
  319. int res = JackDriver::Start();
  320. if ((res >= 0) && fIsMaster) {
  321. res = StartSlaves();
  322. }
  323. return res;
  324. }
  325. int JackAudioDriver::Stop()
  326. {
  327. int res = JackDriver::Stop();
  328. if (fIsMaster) {
  329. if (StopSlaves() < 0) {
  330. res = -1;
  331. }
  332. }
  333. return res;
  334. }
  335. void JackAudioDriver::WaitUntilNextCycle()
  336. {
  337. int wait_time_usec = (int((float(fEngineControl->fBufferSize) / (float(fEngineControl->fSampleRate))) * 1000000.0f));
  338. wait_time_usec = int(wait_time_usec - (GetMicroSeconds() - fBeginDateUst));
  339. if (wait_time_usec > 0)
  340. JackSleep(wait_time_usec);
  341. }
  342. jack_default_audio_sample_t* JackAudioDriver::GetInputBuffer(int port_index)
  343. {
  344. return fCapturePortList[port_index]
  345. ? (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fCapturePortList[port_index], fEngineControl->fBufferSize)
  346. : NULL;
  347. }
  348. jack_default_audio_sample_t* JackAudioDriver::GetOutputBuffer(int port_index)
  349. {
  350. return fPlaybackPortList[port_index]
  351. ? (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fPlaybackPortList[port_index], fEngineControl->fBufferSize)
  352. : NULL;
  353. }
  354. jack_default_audio_sample_t* JackAudioDriver::GetMonitorBuffer(int port_index)
  355. {
  356. return fPlaybackPortList[port_index]
  357. ? (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fMonitorPortList[port_index], fEngineControl->fBufferSize)
  358. : NULL;
  359. }
  360. int JackAudioDriver::ClientNotify(int refnum, const char* name, int notify, int sync, const char* message, int value1, int value2)
  361. {
  362. switch (notify) {
  363. case kLatencyCallback:
  364. HandleLatencyCallback(value1);
  365. break;
  366. default:
  367. JackDriver::ClientNotify(refnum, name, notify, sync, message, value1, value2);
  368. break;
  369. }
  370. return 0;
  371. }
  372. void JackAudioDriver::HandleLatencyCallback(int status)
  373. {
  374. jack_latency_callback_mode_t mode = (status == 0) ? JackCaptureLatency : JackPlaybackLatency;
  375. for (int i = 0; i < fCaptureChannels; i++) {
  376. if (mode == JackPlaybackLatency) {
  377. fGraphManager->RecalculateLatency(fCapturePortList[i], mode);
  378. }
  379. }
  380. for (int i = 0; i < fPlaybackChannels; i++) {
  381. if (mode == JackCaptureLatency) {
  382. fGraphManager->RecalculateLatency(fPlaybackPortList[i], mode);
  383. }
  384. }
  385. }
  386. } // end of namespace