lynx_test_restart.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* A minimal CLAP effect that asks the host for a restart (clap_host_t::request_restart) every 8 blocks, the way a
  2. * plugin does when its latency changes (lookahead switched on, a new preset), to test LYNX's restart handshake
  3. * (PR #29): the host must never deactivate the plugin while its process() call is running.
  4. * zig cc -shared -O2 -target x86_64-windows-gnu -Ithird_party/clap/include tools/testplugin/lynx_test_restart.c \
  5. * -o "LYNX Test Restart.clap"
  6. * LYNX.exe --claprestarttest "LYNX Test Restart.clap" report.txt
  7. * Each activation reports another latency (64 to 448 samples, cycling), so the host's delay
  8. * compensation must follow every restart. After 48 requests the plugin stops asking (the latency then stays).
  9. * With LYNX_TEST_RESTART_REPORT set, each instance appends a line with its counters to that file when destroyed.
  10. * Threads (issue #46): like u-he's plugins (Presswerk), it asks the host's thread_check whether start_processing /
  11. * stop_processing run on the audio thread and activate / deactivate on the main thread, and aborts the process
  12. * when they don't ("Host called the method clap_plugin.stop_processing() on wrong thread!"). Deactivate while
  13. * processing and start / stop out of order are counted (misordered). The last activation's rate and block size
  14. * are reported (last_rate, last_block). destroy must come on the main thread too (round 190az,
  15. * LYNX.exe --clapteardowntest "LYNX Test Restart.clap" report.txt): the checks ask the host it was created with, so
  16. * that host must still answer when the plugin is torn down after the host's own instance object.
  17. */
  18. #include <clap/clap.h>
  19. #include <stdatomic.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. static const char* const features[] = { CLAP_PLUGIN_FEATURE_AUDIO_EFFECT, CLAP_PLUGIN_FEATURE_UTILITY, NULL };
  24. static const clap_plugin_descriptor_t descriptor = {
  25. CLAP_VERSION_INIT, "org.lynx.test-restart", "LYNX Test Restart", "LYNX", "https://github.com/Ka1-S/LYNX-SHARE", "", "",
  26. "1.0.0", "Requests a restart every 8 blocks (host restart handshake test)", features
  27. };
  28. enum { maxRequests = 48 };
  29. typedef struct
  30. {
  31. clap_plugin_t plugin;
  32. const clap_host_t* host;
  33. atomic_int inProcess;
  34. atomic_int active;
  35. atomic_int activations, deactivations, overlapped, inactiveBlocks, requests, blocks;
  36. atomic_int processing, starts, stops, wrongThread, misordered, lastRate, lastBlock;
  37. const clap_host_thread_check_t* threads;
  38. float* dsp; /* allocated in activate, freed in deactivate (what makes a mid-process deactivate crash) */
  39. } Restart;
  40. static Restart* self (const clap_plugin_t* p) { return (Restart*) p->plugin_data; }
  41. static uint32_t ports_count (const clap_plugin_t* p, bool in) { (void) p; (void) in; return 1; }
  42. static bool ports_get (const clap_plugin_t* p, uint32_t index, bool in, clap_audio_port_info_t* info)
  43. {
  44. (void) p;
  45. if (index != 0) return false;
  46. memset (info, 0, sizeof (*info));
  47. info->id = in ? 0 : 1;
  48. strcpy (info->name, in ? "In" : "Out");
  49. info->flags = CLAP_AUDIO_PORT_IS_MAIN;
  50. info->channel_count = 2;
  51. info->port_type = CLAP_PORT_STEREO;
  52. info->in_place_pair = CLAP_INVALID_ID;
  53. return true;
  54. }
  55. static const clap_plugin_audio_ports_t audio_ports = { ports_count, ports_get };
  56. static uint32_t latency_get (const clap_plugin_t* p) { return (uint32_t) (64 * (1 + atomic_load (&self (p)->activations) % 7)); }
  57. static const clap_plugin_latency_t latency = { latency_get };
  58. /* main = 1: must be the main thread; main = 0: must be an audio thread (what the host's thread_check says) */
  59. static bool on_thread (Restart* r, int main)
  60. {
  61. if (r->threads == NULL)
  62. return true;
  63. return main ? r->threads->is_main_thread (r->host) : r->threads->is_audio_thread (r->host);
  64. }
  65. static void must_be_on (Restart* r, int main, const char* method)
  66. {
  67. if (on_thread (r, main))
  68. return;
  69. fprintf (stderr, "[CLAP] Host called the method clap_plugin.%s() on wrong thread! It must be called on %s thread!\n", method,
  70. main ? "main" : "audio");
  71. fflush (stderr);
  72. abort(); /* what u-he's plugins do (issue #46) */
  73. }
  74. static void must_be_audio_thread (Restart* r, const char* method) { must_be_on (r, 0, method); }
  75. static bool p_init (const clap_plugin_t* p)
  76. {
  77. Restart* r = self (p);
  78. r->threads = (const clap_host_thread_check_t*) r->host->get_extension (r->host, CLAP_EXT_THREAD_CHECK);
  79. return true;
  80. }
  81. static void p_destroy (const clap_plugin_t* p)
  82. {
  83. Restart* r = self (p);
  84. if (! on_thread (r, 1))
  85. atomic_fetch_add (&r->wrongThread, 1);
  86. must_be_on (r, 1, "destroy"); /* (round 190az: [main-thread & !active], also when the host's instance is long gone) */
  87. if (atomic_load (&r->processing) != 0 || atomic_load (&r->active) != 0)
  88. atomic_fetch_add (&r->misordered, 1); /* destroyed while active or processing */
  89. const char* path = getenv ("LYNX_TEST_RESTART_REPORT");
  90. if (path != NULL && *path != 0)
  91. {
  92. FILE* f = fopen (path, "a"); /* one line per instance */
  93. if (f != NULL)
  94. {
  95. fprintf (f, "requests=%d activations=%d deactivations=%d overlapped=%d inactive_blocks=%d blocks=%d latency=%u "
  96. "starts=%d stops=%d wrong_thread=%d misordered=%d last_rate=%d last_block=%d\n",
  97. atomic_load (&r->requests), atomic_load (&r->activations), atomic_load (&r->deactivations),
  98. atomic_load (&r->overlapped), atomic_load (&r->inactiveBlocks), atomic_load (&r->blocks), latency_get (p),
  99. atomic_load (&r->starts), atomic_load (&r->stops), atomic_load (&r->wrongThread), atomic_load (&r->misordered),
  100. atomic_load (&r->lastRate), atomic_load (&r->lastBlock));
  101. fclose (f);
  102. }
  103. }
  104. free (r->dsp);
  105. free (r);
  106. }
  107. static bool p_activate (const clap_plugin_t* p, double rate, uint32_t minFrames, uint32_t maxFrames)
  108. {
  109. Restart* r = self (p);
  110. (void) minFrames;
  111. if (! on_thread (r, 1))
  112. atomic_fetch_add (&r->wrongThread, 1);
  113. must_be_on (r, 1, "activate");
  114. if (atomic_load (&r->active) != 0)
  115. atomic_fetch_add (&r->misordered, 1);
  116. atomic_store (&r->lastRate, (int) rate);
  117. atomic_store (&r->lastBlock, (int) maxFrames);
  118. free (r->dsp);
  119. r->dsp = (float*) calloc (maxFrames > 0 ? maxFrames : 1, sizeof (float));
  120. atomic_fetch_add (&r->activations, 1);
  121. atomic_store (&r->active, 1);
  122. return r->dsp != NULL;
  123. }
  124. static void p_deactivate (const clap_plugin_t* p)
  125. {
  126. Restart* r = self (p);
  127. if (atomic_load (&r->inProcess) != 0)
  128. atomic_fetch_add (&r->overlapped, 1);
  129. if (! on_thread (r, 1))
  130. atomic_fetch_add (&r->wrongThread, 1);
  131. must_be_on (r, 1, "deactivate");
  132. if (atomic_load (&r->processing) != 0 || atomic_load (&r->active) == 0)
  133. atomic_fetch_add (&r->misordered, 1); /* deactivated while processing (or twice) */
  134. atomic_store (&r->active, 0);
  135. atomic_fetch_add (&r->deactivations, 1);
  136. /* (not freed here: a host that breaks the rule is counted, not crashed; the next activate replaces it) */
  137. }
  138. static bool p_start (const clap_plugin_t* p)
  139. {
  140. Restart* r = self (p);
  141. must_be_audio_thread (r, "start_processing");
  142. if (atomic_load (&r->active) == 0 || atomic_exchange (&r->processing, 1) != 0)
  143. atomic_fetch_add (&r->misordered, 1);
  144. atomic_fetch_add (&r->starts, 1);
  145. return true;
  146. }
  147. static void p_stop (const clap_plugin_t* p)
  148. {
  149. Restart* r = self (p);
  150. must_be_audio_thread (r, "stop_processing");
  151. if (atomic_exchange (&r->processing, 0) == 0)
  152. atomic_fetch_add (&r->misordered, 1);
  153. atomic_fetch_add (&r->stops, 1);
  154. }
  155. static void p_reset (const clap_plugin_t* p) { (void) p; }
  156. static clap_process_status p_process (const clap_plugin_t* p, const clap_process_t* proc)
  157. {
  158. Restart* r = self (p);
  159. atomic_store (&r->inProcess, 1);
  160. if (atomic_load (&r->active) == 0 || atomic_load (&r->processing) == 0)
  161. atomic_fetch_add (&r->inactiveBlocks, 1); /* (process outside start / stop_processing counts too) */
  162. const int block = atomic_fetch_add (&r->blocks, 1) + 1;
  163. if (block % 8 == 0 && atomic_load (&r->requests) < maxRequests)
  164. {
  165. atomic_fetch_add (&r->requests, 1);
  166. r->host->request_restart (r->host);
  167. }
  168. /* some work, long enough (~0.2 ms) that a restart from the main thread can land inside it */
  169. volatile unsigned spin = 0;
  170. for (unsigned i = 0; i < 200000u; ++i)
  171. spin += i;
  172. if (proc->audio_inputs_count >= 1 && proc->audio_outputs_count >= 1)
  173. for (uint32_t c = 0; c < 2 && c < proc->audio_inputs[0].channel_count && c < proc->audio_outputs[0].channel_count; ++c)
  174. for (uint32_t i = 0; i < proc->frames_count; ++i)
  175. proc->audio_outputs[0].data32[c][i] = proc->audio_inputs[0].data32[c][i];
  176. if (atomic_load (&r->active) == 0)
  177. atomic_fetch_add (&r->inactiveBlocks, 1); /* (deactivated while this call ran) */
  178. atomic_store (&r->inProcess, 0);
  179. return CLAP_PROCESS_CONTINUE;
  180. }
  181. static const void* p_extension (const clap_plugin_t* p, const char* id)
  182. {
  183. (void) p;
  184. if (strcmp (id, CLAP_EXT_AUDIO_PORTS) == 0) return &audio_ports;
  185. if (strcmp (id, CLAP_EXT_LATENCY) == 0) return &latency;
  186. return NULL;
  187. }
  188. static void p_main_thread (const clap_plugin_t* p) { (void) p; }
  189. static uint32_t f_count (const clap_plugin_factory_t* f) { (void) f; return 1; }
  190. static const clap_plugin_descriptor_t* f_descriptor (const clap_plugin_factory_t* f, uint32_t i) { (void) f; return i == 0 ? &descriptor : NULL; }
  191. static const clap_plugin_t* f_create (const clap_plugin_factory_t* f, const clap_host_t* host, const char* id)
  192. {
  193. (void) f;
  194. if (strcmp (id, descriptor.id) != 0) return NULL;
  195. Restart* r = (Restart*) calloc (1, sizeof (Restart));
  196. r->host = host;
  197. clap_plugin_t* p = &r->plugin;
  198. p->desc = &descriptor;
  199. p->plugin_data = r;
  200. p->init = p_init; p->destroy = p_destroy; p->activate = p_activate; p->deactivate = p_deactivate;
  201. p->start_processing = p_start; p->stop_processing = p_stop; p->reset = p_reset; p->process = p_process;
  202. p->get_extension = p_extension; p->on_main_thread = p_main_thread;
  203. return p;
  204. }
  205. static const clap_plugin_factory_t factory = { f_count, f_descriptor, f_create };
  206. static bool e_init (const char* path) { (void) path; return true; }
  207. static void e_deinit (void) {}
  208. static const void* e_factory (const char* id) { return strcmp (id, CLAP_PLUGIN_FACTORY_ID) == 0 ? &factory : NULL; }
  209. CLAP_EXPORT const clap_plugin_entry_t clap_entry = { CLAP_VERSION_INIT, e_init, e_deinit, e_factory };