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.

395 lines
13KB

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