Audio plugin host https://kx.studio/carla
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.

196 lines
4.6KB

  1. /*
  2. * Carla JACK API for external applications
  3. * Copyright (C) 2016-2017 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. // need to include this first
  18. #include "CarlaDefines.h"
  19. // now include a bunch of stuff
  20. #include "CarlaBackendUtils.hpp"
  21. #include "CarlaBridgeUtils.hpp"
  22. #include "CarlaMIDI.h"
  23. #include "CarlaMutex.hpp"
  24. #include "LinkedList.hpp"
  25. #include "AppConfig.h"
  26. #include "juce_core/juce_core.h"
  27. #if 0
  28. #include <jack/jack.h>
  29. #include <jack/midiport.h>
  30. #include <jack/transport.h>
  31. #include <jack/session.h>
  32. #include <jack/metadata.h>
  33. #endif
  34. #ifdef __SSE2_MATH__
  35. # include <xmmintrin.h>
  36. #endif
  37. // must be last include
  38. #include "jackbridge/JackBridge.hpp"
  39. // small check to not hurt myself
  40. #ifdef JACKBRIDGE_DIRECT
  41. # error "Cannot create custom jack server while linking to libjack directly"
  42. #endif
  43. #ifndef BUILD_BRIDGE
  44. # error "BUILD_BRIDGE not enabled, why?"
  45. #endif
  46. CARLA_BACKEND_START_NAMESPACE
  47. // --------------------------------------------------------------------------------------------------------------------
  48. struct JackPortState {
  49. char* name;
  50. char* fullname;
  51. void* buffer;
  52. uint index;
  53. uint flags;
  54. bool isSystem;
  55. JackPortState()
  56. : name(nullptr),
  57. fullname(nullptr),
  58. buffer(nullptr),
  59. index(0),
  60. flags(0),
  61. isSystem(false) {}
  62. JackPortState(const char* const cn, const char* const pn, const uint i, const uint f, const bool sys)
  63. : name(strdup(pn)),
  64. fullname(nullptr),
  65. buffer(nullptr),
  66. index(i),
  67. flags(f),
  68. isSystem(sys)
  69. {
  70. char strBuf[STR_MAX+1];
  71. snprintf(strBuf, STR_MAX, "%s:%s", cn, pn);
  72. strBuf[STR_MAX] = '\0';
  73. fullname = strdup(strBuf);
  74. }
  75. ~JackPortState()
  76. {
  77. free(name);
  78. free(fullname);
  79. }
  80. };
  81. struct JackClientState {
  82. bool activated;
  83. bool prematurelyActivated;
  84. char* name;
  85. uint32_t bufferSize;
  86. double sampleRate;
  87. bool playing;
  88. jack_position_t position;
  89. LinkedList<JackPortState*> audioIns;
  90. LinkedList<JackPortState*> audioOuts;
  91. uint32_t fakeIns, fakeOuts;
  92. LinkedList<JackPortState> midiIns;
  93. LinkedList<JackPortState> midiOuts;
  94. JackProcessCallback process;
  95. void* processPtr;
  96. JackShutdownCallback shutdown;
  97. void* shutdownPtr;
  98. JackClientState()
  99. : activated(false),
  100. prematurelyActivated(false),
  101. name(nullptr),
  102. bufferSize(0),
  103. sampleRate(0.0),
  104. playing(false),
  105. audioIns(),
  106. audioOuts(),
  107. fakeIns(0),
  108. fakeOuts(0),
  109. midiIns(),
  110. midiOuts(),
  111. process(nullptr),
  112. processPtr(nullptr),
  113. shutdown(nullptr),
  114. shutdownPtr(nullptr)
  115. {
  116. carla_zeroStruct(position);
  117. }
  118. ~JackClientState()
  119. {
  120. free(name);
  121. }
  122. };
  123. class CarlaJackClient : public juce::Thread
  124. {
  125. public:
  126. JackClientState fState;
  127. CarlaJackClient();
  128. ~CarlaJackClient() noexcept override;
  129. bool initIfNeeded(const char* const clientName);
  130. void clear() noexcept;
  131. bool isValid() const noexcept;
  132. void activate();
  133. void deactivate();
  134. void handleNonRtData();
  135. // -------------------------------------------------------------------
  136. protected:
  137. void run() override;
  138. private:
  139. BridgeAudioPool fShmAudioPool;
  140. BridgeRtClientControl fShmRtClientControl;
  141. BridgeNonRtClientControl fShmNonRtClientControl;
  142. BridgeNonRtServerControl fShmNonRtServerControl;
  143. char fBaseNameAudioPool[6+1];
  144. char fBaseNameRtClientControl[6+1];
  145. char fBaseNameNonRtClientControl[6+1];
  146. char fBaseNameNonRtServerControl[6+1];
  147. bool fIsValid;
  148. bool fIsOffline;
  149. bool fFirstIdle;
  150. int64_t fLastPingTime;
  151. uint32_t fAudioIns;
  152. uint32_t fAudioOuts;
  153. CarlaMutex fRealtimeThreadMutex;
  154. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaJackClient)
  155. };
  156. CARLA_BACKEND_END_NAMESPACE
  157. // --------------------------------------------------------------------------------------------------------------------