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.

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