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.

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