gravix-rtc-react-native 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (378) hide show
  1. package/CHANGELOG.md +134 -0
  2. package/LICENSE +20 -0
  3. package/LICENSES/Apache-2.0.txt +175 -0
  4. package/NOTICE +68 -0
  5. package/README.md +313 -2
  6. package/android/build.gradle +137 -0
  7. package/android/consumer-rules.pro +29 -0
  8. package/android/src/main/AndroidManifest.xml +9 -0
  9. package/android/src/main/cpp/CMakeLists.txt +48 -0
  10. package/android/src/main/cpp/gx_dfn_engine.c +332 -0
  11. package/android/src/main/cpp/gx_fx_jni.cpp +34 -0
  12. package/android/src/main/cpp/gx_ns_jni.c +162 -0
  13. package/android/src/main/cpp/third_party/onnxruntime/LICENSE +21 -0
  14. package/android/src/main/cpp/third_party/onnxruntime/include/onnxruntime_c_api.h +9227 -0
  15. package/android/src/main/cpp/third_party/onnxruntime/include/onnxruntime_ep_c_api.h +3315 -0
  16. package/android/src/main/cpp/third_party/onnxruntime/include/onnxruntime_error_code.h +98 -0
  17. package/android/src/main/java/com/gravitycompile/gravixrtc/GravixAudioHooks.kt +144 -0
  18. package/android/src/main/java/com/gravitycompile/gravixrtc/GravixAudioModule.kt +105 -0
  19. package/android/src/main/java/com/gravitycompile/gravixrtc/GravixNativeAudio.kt +310 -0
  20. package/android/src/main/java/com/gravitycompile/gravixrtc/GravixRtc.kt +134 -0
  21. package/android/src/main/java/com/gravitycompile/gravixrtc/GravixRtcModule.kt +176 -0
  22. package/android/src/main/java/com/gravitycompile/gravixrtc/GravixRtcPackage.kt +39 -0
  23. package/android/src/main/java/com/gravitycompile/gravixrtc/audio/AudioDeviceKind.java +44 -0
  24. package/android/src/main/java/com/gravitycompile/gravixrtc/audio/AudioManagerUtils.kt +120 -0
  25. package/android/src/main/java/com/gravitycompile/gravixrtc/audio/AudioSwitchManager.java +270 -0
  26. package/android/src/main/java/com/gravitycompile/gravixrtc/audio/AudioType.kt +50 -0
  27. package/android/src/main/java/com/gravitycompile/gravixrtc/music/MusicCaptureRouter.kt +375 -0
  28. package/android/src/main/java/com/gravitycompile/gravixrtc/music/MusicDecoder.kt +339 -0
  29. package/android/src/main/java/com/gravitycompile/gravixrtc/music/MusicInterruptionWatcher.kt +82 -0
  30. package/android/src/main/java/com/gravitycompile/gravixrtc/music/MusicMicRelease.kt +117 -0
  31. package/android/src/main/java/com/gravitycompile/gravixrtc/music/MusicMixKernel.kt +191 -0
  32. package/android/src/main/java/com/gravitycompile/gravixrtc/music/MusicMixerEngine.kt +617 -0
  33. package/android/src/main/java/com/gravitycompile/gravixrtc/processing/AudioRoutePolicy.kt +137 -0
  34. package/android/src/main/java/com/gravitycompile/gravixrtc/processing/GravixCaptureProcessing.kt +67 -0
  35. package/android/src/main/java/com/gravitycompile/gravixrtc/processing/GxCaptureDump.kt +143 -0
  36. package/android/src/main/java/com/gravitycompile/gravixrtc/processing/GxCaptureSwitcher.kt +196 -0
  37. package/android/src/main/java/com/gravitycompile/gravixrtc/processing/GxNoiseProcessor.kt +795 -0
  38. package/android/src/main/java/com/gravitycompile/gravixrtc/processing/GxNsLogic.kt +180 -0
  39. package/android/src/main/java/com/gravitycompile/gravixrtc/processing/GxNsNative.kt +83 -0
  40. package/android/src/main/java/com/gravitycompile/gravixrtc/processing/GxNsStudio.kt +372 -0
  41. package/android/src/main/java/com/gravitycompile/gravixrtc/processing/GxRecordStartGuard.kt +76 -0
  42. package/android/src/main/java/com/gravitycompile/gravixrtc/processing/GxRecorderSeam.kt +434 -0
  43. package/android/src/main/java/com/gravitycompile/gravixrtc/processing/GxSingerSource.kt +37 -0
  44. package/android/src/main/java/com/gravitycompile/gravixrtc/processing/GxVoiceFx.kt +160 -0
  45. package/android/src/main/java/com/gravitycompile/gravixrtc/processing/OtherAppAudioPolicy.kt +143 -0
  46. package/android/src/main/java/org/webrtc/audio/WebRtcAudioTrackHelper.kt +43 -0
  47. package/docs/ANDROID_NATIVE_HOOKS.md +102 -0
  48. package/docs/FEATURE_MATRIX.md +192 -0
  49. package/docs/IOS_NATIVE_HOOKS.md +190 -0
  50. package/gravix-rtc-react-native.podspec +49 -0
  51. package/ios/GravixRtc.h +7 -0
  52. package/ios/GravixRtc.mm +273 -0
  53. package/ios/GravixRtcSetup.h +29 -0
  54. package/ios/GravixRtcSetup.m +52 -0
  55. package/ios/audio/GravixRtcIosAudio.m +157 -0
  56. package/ios/audio/GxAudioDevice.h +62 -0
  57. package/ios/audio/GxAudioDevice.m +857 -0
  58. package/ios/audio/GxAudioSetup.h +34 -0
  59. package/ios/audio/GxAudioSetup.m +70 -0
  60. package/ios/audio/GxCompat.swift +53 -0
  61. package/ios/audio/GxIosAudio.swift +99 -0
  62. package/ios/audio/GxMusicController.swift +150 -0
  63. package/ios/audio/GxMusicPost.swift +362 -0
  64. package/ios/audio/GxNoiseProcessor.swift +718 -0
  65. package/ios/audio/GxNsDfn3CoreML.swift +195 -0
  66. package/ios/audio/GxNsStudioEngine.swift +237 -0
  67. package/ios/audio/GxNsStudioPack.swift +212 -0
  68. package/ios/audio/GxVoiceFx.swift +161 -0
  69. package/ios/audio/gx_adm_state.h +153 -0
  70. package/ios/gxcore/apple/gx_music_post.c +312 -0
  71. package/ios/gxcore/apple/gx_pcm.c +30 -0
  72. package/ios/gxcore/apple/gxns_build.h +19 -0
  73. package/ios/gxcore/apple/gxns_ios.c +17 -0
  74. package/ios/gxcore/apple/include/gx_fx.h +3 -0
  75. package/ios/gxcore/apple/include/gx_music_post.h +88 -0
  76. package/ios/gxcore/apple/include/gx_ns.h +3 -0
  77. package/ios/gxcore/apple/include/gx_pcm.h +33 -0
  78. package/ios/gxcore/apple/include/gxns_ios.h +18 -0
  79. package/ios/gxcore/apple/include/module.modulemap +10 -0
  80. package/ios/gxcore/apple/shim/rnnoise.h +2 -0
  81. package/ios/gxcore/apple/shim/speex_resampler.h +2 -0
  82. package/ios/gxcore/apple/units/gx_fx_forward.cpp +3 -0
  83. package/ios/gxcore/apple/units/gx_ns.c +7 -0
  84. package/ios/gxcore/apple/units/gx_ns_timer.c +7 -0
  85. package/ios/gxcore/apple/units/rnnoise_celt_lpc.c +7 -0
  86. package/ios/gxcore/apple/units/rnnoise_denoise.c +7 -0
  87. package/ios/gxcore/apple/units/rnnoise_kiss_fft.c +7 -0
  88. package/ios/gxcore/apple/units/rnnoise_pitch.c +7 -0
  89. package/ios/gxcore/apple/units/rnnoise_rnn.c +7 -0
  90. package/ios/gxcore/apple/units/rnnoise_rnn_data.c +7 -0
  91. package/ios/gxcore/apple/units/speexdsp_resample.c +7 -0
  92. package/ios/resources/ns/DFN3-NOTICE.txt +10 -0
  93. package/ios/resources/ns/DeepFilterNet-LICENSE-APACHE +201 -0
  94. package/ios/resources/ns/DeepFilterNet-LICENSE-MIT +19 -0
  95. package/ios/resources/ns/gxns_dfn3.mlmodelc/analytics/coremldata.bin +0 -0
  96. package/ios/resources/ns/gxns_dfn3.mlmodelc/coremldata.bin +0 -0
  97. package/ios/resources/ns/gxns_dfn3.mlmodelc/metadata.json +338 -0
  98. package/ios/resources/ns/gxns_dfn3.mlmodelc/model.mil +927 -0
  99. package/ios/resources/ns/gxns_dfn3.mlmodelc/weights/weight.bin +0 -0
  100. package/lib/module/android/bindings.js +49 -0
  101. package/lib/module/android/bindings.js.map +1 -0
  102. package/lib/module/android/index.js +13 -0
  103. package/lib/module/android/index.js.map +1 -0
  104. package/lib/module/android/micMute.js +341 -0
  105. package/lib/module/android/micMute.js.map +1 -0
  106. package/lib/module/android/nativeAudio.js +47 -0
  107. package/lib/module/android/nativeAudio.js.map +1 -0
  108. package/lib/module/android/noiseSuppression.js +170 -0
  109. package/lib/module/android/noiseSuppression.js.map +1 -0
  110. package/lib/module/android/roomMusic.js +142 -0
  111. package/lib/module/android/roomMusic.js.map +1 -0
  112. package/lib/module/android/singerMode.js +31 -0
  113. package/lib/module/android/singerMode.js.map +1 -0
  114. package/lib/module/android/voiceEffects.js +65 -0
  115. package/lib/module/android/voiceEffects.js.map +1 -0
  116. package/lib/module/audio/AudioManager.js +79 -0
  117. package/lib/module/audio/AudioManager.js.map +1 -0
  118. package/lib/module/audio/AudioSession.js +90 -0
  119. package/lib/module/audio/AudioSession.js.map +1 -0
  120. package/lib/module/audio/remoteVolume.js +54 -0
  121. package/lib/module/audio/remoteVolume.js.map +1 -0
  122. package/lib/module/components/VideoView.js +109 -0
  123. package/lib/module/components/VideoView.js.map +1 -0
  124. package/lib/module/features/audioScenario.js +53 -0
  125. package/lib/module/features/audioScenario.js.map +1 -0
  126. package/lib/module/features/beauty.js +828 -0
  127. package/lib/module/features/beauty.js.map +1 -0
  128. package/lib/module/features/micRelease.js +89 -0
  129. package/lib/module/features/micRelease.js.map +1 -0
  130. package/lib/module/features/noiseSuppression.js +213 -0
  131. package/lib/module/features/noiseSuppression.js.map +1 -0
  132. package/lib/module/features/roomMusic.js +90 -0
  133. package/lib/module/features/roomMusic.js.map +1 -0
  134. package/lib/module/features/roomMusicAndroid.js +294 -0
  135. package/lib/module/features/roomMusicAndroid.js.map +1 -0
  136. package/lib/module/features/roomMusicIos.js +250 -0
  137. package/lib/module/features/roomMusicIos.js.map +1 -0
  138. package/lib/module/features/roomMusicTypes.js +53 -0
  139. package/lib/module/features/roomMusicTypes.js.map +1 -0
  140. package/lib/module/features/screenShare.js +22 -0
  141. package/lib/module/features/screenShare.js.map +1 -0
  142. package/lib/module/features/singerMode.js +73 -0
  143. package/lib/module/features/singerMode.js.map +1 -0
  144. package/lib/module/features/voiceEffects.js +148 -0
  145. package/lib/module/features/voiceEffects.js.map +1 -0
  146. package/lib/module/hooks/context.js +34 -0
  147. package/lib/module/hooks/context.js.map +1 -0
  148. package/lib/module/hooks/hooks.js +145 -0
  149. package/lib/module/hooks/hooks.js.map +1 -0
  150. package/lib/module/hooks/types.js +4 -0
  151. package/lib/module/hooks/types.js.map +1 -0
  152. package/lib/module/hooks/useRoomEvents.js +30 -0
  153. package/lib/module/hooks/useRoomEvents.js.map +1 -0
  154. package/lib/module/index.js +43 -0
  155. package/lib/module/index.js.map +1 -0
  156. package/lib/module/ios/autoAttach.js +14 -0
  157. package/lib/module/ios/autoAttach.js.map +1 -0
  158. package/lib/module/ios/iosAudio.js +149 -0
  159. package/lib/module/ios/iosAudio.js.map +1 -0
  160. package/lib/module/join/appStateLifecycle.js +54 -0
  161. package/lib/module/join/appStateLifecycle.js.map +1 -0
  162. package/lib/module/micPolicy.js +249 -0
  163. package/lib/module/micPolicy.js.map +1 -0
  164. package/lib/module/native/NativeGravixAudio.js +18 -0
  165. package/lib/module/native/NativeGravixAudio.js.map +1 -0
  166. package/lib/module/native/NativeGravixRtc.js +13 -0
  167. package/lib/module/native/NativeGravixRtc.js.map +1 -0
  168. package/lib/module/native/module.js +23 -0
  169. package/lib/module/native/module.js.map +1 -0
  170. package/lib/module/package.json +1 -0
  171. package/lib/module/polyfills/DOMException.js +22 -0
  172. package/lib/module/polyfills/DOMException.js.map +1 -0
  173. package/lib/module/polyfills/early.js +18 -0
  174. package/lib/module/polyfills/early.js.map +1 -0
  175. package/lib/module/polyfills/event.js +71 -0
  176. package/lib/module/polyfills/event.js.map +1 -0
  177. package/lib/module/polyfills/sha256.js +92 -0
  178. package/lib/module/polyfills/sha256.js.map +1 -0
  179. package/lib/module/polyfills/subtleHmac.js +91 -0
  180. package/lib/module/polyfills/subtleHmac.js.map +1 -0
  181. package/lib/module/polyfills/textCoding.js +164 -0
  182. package/lib/module/polyfills/textCoding.js.map +1 -0
  183. package/lib/module/registerGlobals.js +135 -0
  184. package/lib/module/registerGlobals.js.map +1 -0
  185. package/lib/module/unsupported.js +90 -0
  186. package/lib/module/unsupported.js.map +1 -0
  187. package/lib/module/version.js +7 -0
  188. package/lib/module/version.js.map +1 -0
  189. package/lib/typescript/package.json +1 -0
  190. package/lib/typescript/src/android/bindings.d.ts +14 -0
  191. package/lib/typescript/src/android/bindings.d.ts.map +1 -0
  192. package/lib/typescript/src/android/index.d.ts +9 -0
  193. package/lib/typescript/src/android/index.d.ts.map +1 -0
  194. package/lib/typescript/src/android/micMute.d.ts +124 -0
  195. package/lib/typescript/src/android/micMute.d.ts.map +1 -0
  196. package/lib/typescript/src/android/nativeAudio.d.ts +40 -0
  197. package/lib/typescript/src/android/nativeAudio.d.ts.map +1 -0
  198. package/lib/typescript/src/android/noiseSuppression.d.ts +48 -0
  199. package/lib/typescript/src/android/noiseSuppression.d.ts.map +1 -0
  200. package/lib/typescript/src/android/roomMusic.d.ts +80 -0
  201. package/lib/typescript/src/android/roomMusic.d.ts.map +1 -0
  202. package/lib/typescript/src/android/singerMode.d.ts +27 -0
  203. package/lib/typescript/src/android/singerMode.d.ts.map +1 -0
  204. package/lib/typescript/src/android/voiceEffects.d.ts +30 -0
  205. package/lib/typescript/src/android/voiceEffects.d.ts.map +1 -0
  206. package/lib/typescript/src/audio/AudioManager.d.ts +32 -0
  207. package/lib/typescript/src/audio/AudioManager.d.ts.map +1 -0
  208. package/lib/typescript/src/audio/AudioSession.d.ts +70 -0
  209. package/lib/typescript/src/audio/AudioSession.d.ts.map +1 -0
  210. package/lib/typescript/src/audio/remoteVolume.d.ts +8 -0
  211. package/lib/typescript/src/audio/remoteVolume.d.ts.map +1 -0
  212. package/lib/typescript/src/components/VideoView.d.ts +44 -0
  213. package/lib/typescript/src/components/VideoView.d.ts.map +1 -0
  214. package/lib/typescript/src/features/audioScenario.d.ts +24 -0
  215. package/lib/typescript/src/features/audioScenario.d.ts.map +1 -0
  216. package/lib/typescript/src/features/beauty.d.ts +377 -0
  217. package/lib/typescript/src/features/beauty.d.ts.map +1 -0
  218. package/lib/typescript/src/features/micRelease.d.ts +28 -0
  219. package/lib/typescript/src/features/micRelease.d.ts.map +1 -0
  220. package/lib/typescript/src/features/noiseSuppression.d.ts +64 -0
  221. package/lib/typescript/src/features/noiseSuppression.d.ts.map +1 -0
  222. package/lib/typescript/src/features/roomMusic.d.ts +41 -0
  223. package/lib/typescript/src/features/roomMusic.d.ts.map +1 -0
  224. package/lib/typescript/src/features/roomMusicAndroid.d.ts +60 -0
  225. package/lib/typescript/src/features/roomMusicAndroid.d.ts.map +1 -0
  226. package/lib/typescript/src/features/roomMusicIos.d.ts +56 -0
  227. package/lib/typescript/src/features/roomMusicIos.d.ts.map +1 -0
  228. package/lib/typescript/src/features/roomMusicTypes.d.ts +76 -0
  229. package/lib/typescript/src/features/roomMusicTypes.d.ts.map +1 -0
  230. package/lib/typescript/src/features/screenShare.d.ts +12 -0
  231. package/lib/typescript/src/features/screenShare.d.ts.map +1 -0
  232. package/lib/typescript/src/features/singerMode.d.ts +20 -0
  233. package/lib/typescript/src/features/singerMode.d.ts.map +1 -0
  234. package/lib/typescript/src/features/voiceEffects.d.ts +46 -0
  235. package/lib/typescript/src/features/voiceEffects.d.ts.map +1 -0
  236. package/lib/typescript/src/hooks/context.d.ts +12 -0
  237. package/lib/typescript/src/hooks/context.d.ts.map +1 -0
  238. package/lib/typescript/src/hooks/hooks.d.ts +54 -0
  239. package/lib/typescript/src/hooks/hooks.d.ts.map +1 -0
  240. package/lib/typescript/src/hooks/types.d.ts +8 -0
  241. package/lib/typescript/src/hooks/types.d.ts.map +1 -0
  242. package/lib/typescript/src/hooks/useRoomEvents.d.ts +13 -0
  243. package/lib/typescript/src/hooks/useRoomEvents.d.ts.map +1 -0
  244. package/lib/typescript/src/index.d.ts +37 -0
  245. package/lib/typescript/src/index.d.ts.map +1 -0
  246. package/lib/typescript/src/ios/autoAttach.d.ts +2 -0
  247. package/lib/typescript/src/ios/autoAttach.d.ts.map +1 -0
  248. package/lib/typescript/src/ios/iosAudio.d.ts +96 -0
  249. package/lib/typescript/src/ios/iosAudio.d.ts.map +1 -0
  250. package/lib/typescript/src/join/appStateLifecycle.d.ts +14 -0
  251. package/lib/typescript/src/join/appStateLifecycle.d.ts.map +1 -0
  252. package/lib/typescript/src/micPolicy.d.ts +104 -0
  253. package/lib/typescript/src/micPolicy.d.ts.map +1 -0
  254. package/lib/typescript/src/native/NativeGravixAudio.d.ts +21 -0
  255. package/lib/typescript/src/native/NativeGravixAudio.d.ts.map +1 -0
  256. package/lib/typescript/src/native/NativeGravixRtc.d.ts +31 -0
  257. package/lib/typescript/src/native/NativeGravixRtc.d.ts.map +1 -0
  258. package/lib/typescript/src/native/module.d.ts +7 -0
  259. package/lib/typescript/src/native/module.d.ts.map +1 -0
  260. package/lib/typescript/src/polyfills/DOMException.d.ts +3 -0
  261. package/lib/typescript/src/polyfills/DOMException.d.ts.map +1 -0
  262. package/lib/typescript/src/polyfills/early.d.ts +2 -0
  263. package/lib/typescript/src/polyfills/early.d.ts.map +1 -0
  264. package/lib/typescript/src/polyfills/event.d.ts +2 -0
  265. package/lib/typescript/src/polyfills/event.d.ts.map +1 -0
  266. package/lib/typescript/src/polyfills/sha256.d.ts +3 -0
  267. package/lib/typescript/src/polyfills/sha256.d.ts.map +1 -0
  268. package/lib/typescript/src/polyfills/subtleHmac.d.ts +37 -0
  269. package/lib/typescript/src/polyfills/subtleHmac.d.ts.map +1 -0
  270. package/lib/typescript/src/polyfills/textCoding.d.ts +36 -0
  271. package/lib/typescript/src/polyfills/textCoding.d.ts.map +1 -0
  272. package/lib/typescript/src/registerGlobals.d.ts +42 -0
  273. package/lib/typescript/src/registerGlobals.d.ts.map +1 -0
  274. package/lib/typescript/src/unsupported.d.ts +42 -0
  275. package/lib/typescript/src/unsupported.d.ts.map +1 -0
  276. package/lib/typescript/src/version.d.ts +3 -0
  277. package/lib/typescript/src/version.d.ts.map +1 -0
  278. package/metro/index.js +39 -0
  279. package/metro/noise-stub.js +10 -0
  280. package/native/gxfx/include/gx_fx.h +82 -0
  281. package/native/gxfx/src/gx_fx.cpp +527 -0
  282. package/native/gxfx/third_party/README.md +4 -0
  283. package/native/gxfx/third_party/signalsmith-linear/LICENSE.txt +21 -0
  284. package/native/gxfx/third_party/signalsmith-linear/approx.h +191 -0
  285. package/native/gxfx/third_party/signalsmith-linear/fft.h +1386 -0
  286. package/native/gxfx/third_party/signalsmith-linear/linear.h +1129 -0
  287. package/native/gxfx/third_party/signalsmith-linear/platform/approx-cmsisdsp.h +40 -0
  288. package/native/gxfx/third_party/signalsmith-linear/platform/basic-fill-warnings.h +61 -0
  289. package/native/gxfx/third_party/signalsmith-linear/platform/fft-accelerate.h +340 -0
  290. package/native/gxfx/third_party/signalsmith-linear/platform/fft-cmsisdsp.h +307 -0
  291. package/native/gxfx/third_party/signalsmith-linear/platform/fft-ipp.h +532 -0
  292. package/native/gxfx/third_party/signalsmith-linear/platform/fft-linear.h +213 -0
  293. package/native/gxfx/third_party/signalsmith-linear/platform/fft-pffft-double.h +221 -0
  294. package/native/gxfx/third_party/signalsmith-linear/platform/fft-pffft.h +221 -0
  295. package/native/gxfx/third_party/signalsmith-linear/platform/linear-accelerate.h +593 -0
  296. package/native/gxfx/third_party/signalsmith-linear/platform/linear-cmsisdsp.h +713 -0
  297. package/native/gxfx/third_party/signalsmith-linear/platform/linear-xsimd-dispatch.cpp +6 -0
  298. package/native/gxfx/third_party/signalsmith-linear/platform/linear-xsimd.h +818 -0
  299. package/native/gxfx/third_party/signalsmith-linear/stft.h +700 -0
  300. package/native/gxfx/third_party/signalsmith-stretch-LICENSE.txt +21 -0
  301. package/native/gxfx/third_party/signalsmith-stretch.h +1060 -0
  302. package/native/gxns/CMakeLists.txt +67 -0
  303. package/native/gxns/include/gx_ns.h +285 -0
  304. package/native/gxns/src/gx_ns.c +1518 -0
  305. package/native/gxns/src/gx_ns_timer.c +84 -0
  306. package/native/gxns/src/gx_ns_timer.h +29 -0
  307. package/native/gxns/third_party/VENDOR.md +43 -0
  308. package/native/gxns/third_party/rnnoise/AUTHORS +1 -0
  309. package/native/gxns/third_party/rnnoise/COPYING +31 -0
  310. package/native/gxns/third_party/rnnoise/include/rnnoise.h +117 -0
  311. package/native/gxns/third_party/rnnoise/src/_kiss_fft_guts.h +182 -0
  312. package/native/gxns/third_party/rnnoise/src/arch.h +261 -0
  313. package/native/gxns/third_party/rnnoise/src/celt_lpc.c +172 -0
  314. package/native/gxns/third_party/rnnoise/src/celt_lpc.h +45 -0
  315. package/native/gxns/third_party/rnnoise/src/common.h +48 -0
  316. package/native/gxns/third_party/rnnoise/src/denoise.c +664 -0
  317. package/native/gxns/third_party/rnnoise/src/kiss_fft.c +601 -0
  318. package/native/gxns/third_party/rnnoise/src/kiss_fft.h +203 -0
  319. package/native/gxns/third_party/rnnoise/src/opus_types.h +159 -0
  320. package/native/gxns/third_party/rnnoise/src/pitch.c +523 -0
  321. package/native/gxns/third_party/rnnoise/src/pitch.h +149 -0
  322. package/native/gxns/third_party/rnnoise/src/rnn.c +178 -0
  323. package/native/gxns/third_party/rnnoise/src/rnn.h +65 -0
  324. package/native/gxns/third_party/rnnoise/src/rnn_data.c +11051 -0
  325. package/native/gxns/third_party/rnnoise/src/rnn_data.h +34 -0
  326. package/native/gxns/third_party/rnnoise/src/tansig_table.h +45 -0
  327. package/native/gxns/third_party/speexdsp/AUTHORS +18 -0
  328. package/native/gxns/third_party/speexdsp/COPYING +35 -0
  329. package/native/gxns/third_party/speexdsp/arch.h +232 -0
  330. package/native/gxns/third_party/speexdsp/resample.c +1239 -0
  331. package/native/gxns/third_party/speexdsp/resample_neon.h +339 -0
  332. package/native/gxns/third_party/speexdsp/resample_sse.h +128 -0
  333. package/native/gxns/third_party/speexdsp/speex_resampler.h +343 -0
  334. package/package.json +186 -4
  335. package/src/android/bindings.ts +61 -0
  336. package/src/android/index.ts +10 -0
  337. package/src/android/micMute.ts +422 -0
  338. package/src/android/nativeAudio.ts +94 -0
  339. package/src/android/noiseSuppression.ts +209 -0
  340. package/src/android/roomMusic.ts +193 -0
  341. package/src/android/singerMode.ts +53 -0
  342. package/src/android/voiceEffects.ts +126 -0
  343. package/src/audio/AudioManager.ts +111 -0
  344. package/src/audio/AudioSession.ts +190 -0
  345. package/src/audio/remoteVolume.ts +58 -0
  346. package/src/components/VideoView.tsx +152 -0
  347. package/src/features/audioScenario.ts +58 -0
  348. package/src/features/beauty.ts +1057 -0
  349. package/src/features/micRelease.ts +108 -0
  350. package/src/features/noiseSuppression.ts +251 -0
  351. package/src/features/roomMusic.ts +106 -0
  352. package/src/features/roomMusicAndroid.ts +310 -0
  353. package/src/features/roomMusicIos.ts +300 -0
  354. package/src/features/roomMusicTypes.ts +97 -0
  355. package/src/features/screenShare.ts +19 -0
  356. package/src/features/singerMode.ts +70 -0
  357. package/src/features/voiceEffects.ts +245 -0
  358. package/src/hooks/context.tsx +38 -0
  359. package/src/hooks/hooks.ts +247 -0
  360. package/src/hooks/types.ts +10 -0
  361. package/src/hooks/useRoomEvents.ts +35 -0
  362. package/src/index.tsx +134 -0
  363. package/src/ios/autoAttach.ts +11 -0
  364. package/src/ios/iosAudio.ts +202 -0
  365. package/src/join/appStateLifecycle.ts +55 -0
  366. package/src/micPolicy.ts +312 -0
  367. package/src/native/NativeGravixAudio.ts +30 -0
  368. package/src/native/NativeGravixRtc.ts +38 -0
  369. package/src/native/module.ts +22 -0
  370. package/src/polyfills/DOMException.ts +20 -0
  371. package/src/polyfills/early.ts +16 -0
  372. package/src/polyfills/event.ts +88 -0
  373. package/src/polyfills/sha256.ts +112 -0
  374. package/src/polyfills/subtleHmac.ts +124 -0
  375. package/src/polyfills/textCoding.ts +197 -0
  376. package/src/registerGlobals.ts +201 -0
  377. package/src/unsupported.ts +139 -0
  378. package/src/version.ts +4 -0
@@ -0,0 +1,617 @@
1
+ package com.gravitycompile.gravixrtc.music
2
+
3
+ import android.content.Context
4
+ import android.media.AudioAttributes
5
+ import android.media.AudioFormat
6
+ import android.media.AudioManager
7
+ import android.media.AudioTrack
8
+ import android.os.SystemClock
9
+ import android.util.Log
10
+ import java.nio.ByteBuffer
11
+ import java.nio.ByteOrder
12
+
13
+ /** One 10 ms capture frame (the shape of webrtc-sdk's AudioBufferCallback). Returns the capture time. */
14
+ interface CaptureBufferCallback {
15
+ fun onBuffer(
16
+ buffer: ByteBuffer,
17
+ audioFormat: Int,
18
+ channelCount: Int,
19
+ sampleRate: Int,
20
+ bytesRead: Int,
21
+ captureTimeNs: Long,
22
+ ): Long
23
+ }
24
+
25
+ /**
26
+ * Mixes locally decoded music into WebRTC's microphone capture buffer: the
27
+ * music rides on the published microphone track (one stream, no second track,
28
+ * no server bot).
29
+ *
30
+ * Called by the capture seam (processing/GxRecorderSeam) from inside WebRTC's
31
+ * AudioRecord read, for every 10 ms capture frame. With no session it is a
32
+ * pass-through (after a chained foreign callback, if one was set).
33
+ *
34
+ * onBuffer runs on WebRTC's AudioRecordThread every ~10 ms: no allocation after
35
+ * warm-up, never blocks.
36
+ *
37
+ * MUTE: the engine mute sets [captureMuted]; the voice is zeroed before the
38
+ * music is added. With [holdOnMute] false (gravix_rtc
39
+ * 0.4.10 default, `continueWhileMicMuted`) the music is still mixed onto the
40
+ * zeroed voice: a voice-only mute. With [holdOnMute] true the music is held
41
+ * (not consumed) while muted.
42
+ *
43
+ * POSITION: playedSamples counts the music samples consumed by the capture
44
+ * thread (what listeners heard), rebased on seek.
45
+ *
46
+ * VOICE EFFECT: with an effect on, the music is added after it, at the end of
47
+ * the capture chain ([onPostProcess], see MusicCaptureRouter); with none, the
48
+ * mix above is unchanged.
49
+ */
50
+ object MusicMixerEngine : CaptureBufferCallback {
51
+
52
+
53
+ private const val TAG = "GxMusicMixer"
54
+
55
+ interface Listener {
56
+ fun onCompleted()
57
+ fun onError(code: String, message: String)
58
+ fun onInterruption(active: Boolean, reason: String, resumed: Boolean)
59
+ }
60
+
61
+ /** Application context (content URIs, AudioManager). Set by the plugin. */
62
+ @Volatile var appContext: Context? = null
63
+ @Volatile var listener: Listener? = null
64
+
65
+ // Observed capture format, updated on every callback.
66
+ @Volatile var captureSampleRate: Int = 48000; private set
67
+ @Volatile var captureChannels: Int = 1; private set
68
+ @Volatile var captureSeen: Boolean = false; private set
69
+ @Volatile private var lastCaptureMs: Long = 0
70
+
71
+ /** A capture callback ran within the last 500 ms (the recorder is live). */
72
+ fun captureLive(): Boolean =
73
+ lastCaptureMs != 0L && SystemClock.elapsedRealtime() - lastCaptureMs < 500
74
+
75
+ /** Module mute engaged (GravixClientPlugin.handleSetMicrophoneMute). */
76
+ @Volatile var captureMuted: Boolean = false
77
+
78
+ /** Hold the music while [captureMuted] (continueWhileMicMuted = false). */
79
+ @Volatile var holdOnMute: Boolean = false
80
+
81
+ /** Pause on a phone call / audio focus loss. */
82
+ @Volatile var pauseOnInterruption: Boolean = true
83
+
84
+ /** A callback that held the slot before us (e.g. an app's own mixer). */
85
+ @Volatile var downstream: CaptureBufferCallback? = null
86
+
87
+ val kernel = MusicMixKernel()
88
+
89
+ /** Pre-APM vs post-effect mix site (record thread). */
90
+ val router = MusicCaptureRouter(kernel).apply {
91
+ fx = object : MusicCaptureRouter.Fx {
92
+ override fun setMusicBypass(on: Boolean) = com.gravitycompile.gravixrtc.processing.GxVoiceFx.setMusicBypass(on)
93
+ }
94
+ }
95
+
96
+ private val delayTimer = java.util.concurrent.Executors.newSingleThreadScheduledExecutor { r ->
97
+ Thread(r, "GxMusicDelay").apply { isDaemon = true }
98
+ }
99
+ @Volatile private var delayTask: java.util.concurrent.ScheduledFuture<*>? = null
100
+
101
+ /**
102
+ * The voice's delay between the capture callback and the post site, at
103
+ * the capture rate: gx_ns latency + the active effect chain's. Control /
104
+ * timer thread (status readbacks allocate; never on the audio thread).
105
+ */
106
+ fun refreshDelay() {
107
+ val rate = session?.rate ?: captureSampleRate
108
+ var samples = com.gravitycompile.gravixrtc.processing.GxVoiceFx.latencyMs() * rate / 1000
109
+ val ns = try { com.gravitycompile.gravixrtc.processing.GxNoiseProcessor.stats() } catch (_: Throwable) { null }
110
+ val nsLat = (ns?.get("latencySamples") as? Number)?.toInt() ?: 0
111
+ val nsRate = (ns?.get("rate") as? Number)?.toInt() ?: 0
112
+ if (nsLat > 0 && nsRate > 0) samples += (nsLat.toLong() * rate / nsRate).toInt()
113
+ router.delayTargetSamples = samples.coerceIn(0, MusicCaptureRouter.MAX_DELAY)
114
+ }
115
+
116
+ private fun startDelayWatch() {
117
+ delayTask?.cancel(false)
118
+ delayTask = delayTimer.scheduleWithFixedDelay({
119
+ try { refreshDelay() } catch (_: Throwable) {}
120
+ }, 500, 500, java.util.concurrent.TimeUnit.MILLISECONDS)
121
+ }
122
+
123
+ private fun stopDelayWatch() {
124
+ delayTask?.cancel(false)
125
+ delayTask = null
126
+ }
127
+
128
+ @Volatile private var session: Session? = null
129
+ private var interruptions: MusicInterruptionWatcher? = null
130
+
131
+ class Session(
132
+ val ring: PcmRingBuffer,
133
+ val decoder: MusicDecoder,
134
+ val monitor: MonitorPlayer?,
135
+ val rate: Int,
136
+ val channels: Int,
137
+ ) {
138
+ @Volatile var paused = false // not consuming
139
+ @Volatile var pauseRequested = false // fading out, then paused
140
+ @Volatile var stopRequested = false // fading out, then released
141
+ @Volatile var fadedOut = false
142
+ @Volatile var interrupted: String? = null
143
+ @Volatile var completedFired = false
144
+ @Volatile var baseUs: Long = 0
145
+ @Volatile var playedSamples: Long = 0
146
+ /** System.nanoTime() of the last read that advanced [playedSamples] (0 = none yet). */
147
+ @Volatile var lastReadNs: Long = 0
148
+
149
+ /** The router's view of this session (record thread). */
150
+ val track = object : MusicCaptureRouter.Track {
151
+ override val paused: Boolean get() = this@Session.paused
152
+ override fun read(dst: ShortArray, n: Int): Int {
153
+ val got = ring.read(dst, n)
154
+ playedSamples += got
155
+ if (got > 0) lastReadNs = System.nanoTime()
156
+ return got
157
+ }
158
+ override fun onShort(got: Int) {
159
+ if (got == 0 && decoder.finished && decoder.failed == null && !completedFired) {
160
+ completedFired = true
161
+ listener?.onCompleted()
162
+ }
163
+ }
164
+ override fun monitor(musicOut: ShortArray, n: Int) { this@Session.monitor?.write(musicOut, n) }
165
+ override fun onEnvZero() {
166
+ if (pauseRequested) { this@Session.paused = true; pauseRequested = false }
167
+ if (stopRequested) fadedOut = true
168
+ }
169
+ }
170
+ }
171
+
172
+ class StartException(val code: String, message: String) : Exception(message)
173
+
174
+ // ---- control (plugin's worker thread) ----
175
+
176
+ /**
177
+ * Starts [source] (replacing a running track). Throws [StartException]
178
+ * (CAPTURE_NOT_READY, OPEN_FAILED) instead of reporting a silent success.
179
+ * Returns the duration in ms (-1 unknown).
180
+ */
181
+ @Synchronized
182
+ fun start(
183
+ source: String,
184
+ isContentUri: Boolean,
185
+ loop: Boolean,
186
+ monitorEnabled: Boolean,
187
+ ): Long {
188
+ stopInternal()
189
+ // The music is resampled to the LIVE capture format; guessing 48 kHz
190
+ // mono plays chipmunked/slowed on devices capturing at 16 kHz.
191
+ if (!captureLive()) {
192
+ throw StartException("CAPTURE_NOT_READY", "the microphone capture is not running")
193
+ }
194
+ val rate = captureSampleRate
195
+ val ch = captureChannels
196
+ val ring = PcmRingBuffer(rate * ch * 2) // ~2 s
197
+ val decoder = MusicDecoder(appContext, source, isContentUri, rate, ch, ring)
198
+ decoder.loop = loop
199
+ decoder.prepare()?.let { reason ->
200
+ decoder.shutdown()
201
+ throw StartException("OPEN_FAILED", reason)
202
+ }
203
+ val monitor = if (monitorEnabled) MonitorPlayer.create(rate, ch, monitorUsesMedia()) else null
204
+ val s = Session(ring, decoder, monitor, rate, ch)
205
+ decoder.onSeekApplied = { us ->
206
+ s.baseUs = us
207
+ s.playedSamples = 0
208
+ s.lastReadNs = 0
209
+ }
210
+ decoder.onFailed = { msg -> listener?.onError("DECODE_FAILED", msg) }
211
+ kernel.rampSamples = (rate * ch / 50).coerceAtLeast(1) // 20 ms
212
+ kernel.reset(0f)
213
+ kernel.envTarget = 1f
214
+ // noise suppression: the whole chain goes dry (crossfade) while music plays
215
+ com.gravitycompile.gravixrtc.processing.GxNoiseProcessor.onMusic(true)
216
+ // voice effect on: the music is added after it (post route), else the effect goes dry
217
+ refreshDelay()
218
+ router.begin(rate, ch)
219
+ session = s
220
+ startDelayWatch()
221
+ decoder.start()
222
+ startInterruptionWatch()
223
+ Log.i(TAG, "music started: ${rate}Hz/${ch}ch loop=$loop monitor=${monitor != null} ${router.describe()}")
224
+ return durationMs()
225
+ }
226
+
227
+ fun pause() {
228
+ val s = session ?: return
229
+ s.interrupted = null // a user pause is never auto-resumed
230
+ requestPause(s)
231
+ }
232
+
233
+ private fun requestPause(s: Session) {
234
+ if (s.paused || s.pauseRequested) return
235
+ s.pauseRequested = true
236
+ kernel.envTarget = 0f
237
+ if (!captureLive()) s.paused = true
238
+ }
239
+
240
+ fun resume() {
241
+ val s = session ?: return
242
+ s.interrupted = null
243
+ resumeInternal(s)
244
+ }
245
+
246
+ private fun resumeInternal(s: Session) {
247
+ s.pauseRequested = false
248
+ s.lastReadNs = 0 // the age restarts at the first read after the pause
249
+ s.paused = false
250
+ kernel.envTarget = 1f
251
+ }
252
+
253
+ fun setLoop(on: Boolean) { session?.decoder?.loop = on }
254
+ fun isActive(): Boolean = session != null
255
+ fun isPaused(): Boolean = session?.let { it.paused || it.pauseRequested } ?: false
256
+ fun interruptedReason(): String? = session?.interrupted
257
+
258
+ /** Current playback position in ms, or -1 when no session. */
259
+ fun positionMs(): Long {
260
+ val s = session ?: return -1
261
+ val played = s.playedSamples * 1000L / (s.rate.toLong() * s.channels)
262
+ var posMs = s.baseUs / 1000 + played
263
+ val dur = durationMs()
264
+ if (dur > 0) posMs = if (s.decoder.loop) posMs % dur else posMs.coerceAtMost(dur)
265
+ return posMs
266
+ }
267
+
268
+ /**
269
+ * Age in microseconds of [positionMs]: the time since the capture thread last
270
+ * consumed music samples (0 while paused, held or before the first read), so
271
+ * a caller can extrapolate the position to "now" without polling faster.
272
+ */
273
+ fun positionAgeUs(): Long {
274
+ val s = session ?: return 0
275
+ val t = s.lastReadNs
276
+ if (t == 0L || s.paused) return 0
277
+ return ((System.nanoTime() - t) / 1000).coerceIn(0, 1_000_000)
278
+ }
279
+
280
+ /**
281
+ * Music delay between consumption ([positionMs]) and the outgoing stream:
282
+ * the POST route's delay line (NS + effect latency), 0 on PRE.
283
+ */
284
+ fun outputDelayMs(): Long {
285
+ val rate = captureSampleRate
286
+ if (router.route != MusicCaptureRouter.Route.POST || rate <= 0) return 0
287
+ return router.delaySamples * 1000L / rate
288
+ }
289
+
290
+ /** Track duration in ms, or -1 when unknown / no session. */
291
+ fun durationMs(): Long {
292
+ val us = session?.decoder?.durationUs ?: return -1
293
+ return if (us > 0) us / 1000 else -1
294
+ }
295
+
296
+ /** Seek the current track. No-op when no session. */
297
+ fun seekTo(positionMs: Long) {
298
+ session?.decoder?.requestSeek(positionMs * 1000)
299
+ }
300
+
301
+ @Synchronized
302
+ fun stop() = stopInternal()
303
+
304
+ private fun stopInternal() {
305
+ val s = session ?: return
306
+ // fade out first (<= 60 ms) so listeners hear no click
307
+ if (captureLive() && !s.paused) {
308
+ s.stopRequested = true
309
+ kernel.envTarget = 0f
310
+ val deadline = SystemClock.elapsedRealtime() + 60
311
+ while (!s.fadedOut && SystemClock.elapsedRealtime() < deadline) {
312
+ try { Thread.sleep(5) } catch (_: InterruptedException) { break }
313
+ }
314
+ }
315
+ session = null
316
+ stopInterruptionWatch()
317
+ s.decoder.shutdown()
318
+ s.monitor?.release()
319
+ val routeInfo = router.describe()
320
+ stopDelayWatch()
321
+ router.end()
322
+ com.gravitycompile.gravixrtc.processing.GxNoiseProcessor.onMusic(false)
323
+ Log.i(TAG, "music stopped $routeInfo")
324
+ }
325
+
326
+ // ---- interruptions (phone call / audio focus) ----
327
+
328
+ private fun startInterruptionWatch() {
329
+ val ctx = appContext ?: return
330
+ if (interruptions == null) {
331
+ interruptions = MusicInterruptionWatcher(ctx) { inCall ->
332
+ if (inCall) interruptionBegan("call") else interruptionEnded("call")
333
+ }.also { it.start() }
334
+ }
335
+ }
336
+
337
+ private fun stopInterruptionWatch() {
338
+ interruptions?.stop()
339
+ interruptions = null
340
+ }
341
+
342
+ /** From GxAudioSwitchManager's focus listener. */
343
+ fun onAudioFocusChange(change: Int) {
344
+ when (change) {
345
+ AudioManager.AUDIOFOCUS_LOSS_TRANSIENT -> interruptionBegan("focusLossTransient")
346
+ AudioManager.AUDIOFOCUS_LOSS -> {
347
+ // permanent: paused like a user pause (no auto-resume)
348
+ val s = session ?: return
349
+ if (!pauseOnInterruption || s.paused || s.pauseRequested) return
350
+ s.interrupted = null
351
+ requestPause(s)
352
+ listener?.onInterruption(true, "focusLoss", false)
353
+ }
354
+ AudioManager.AUDIOFOCUS_GAIN -> interruptionEnded("focusLossTransient")
355
+ else -> Unit
356
+ }
357
+ }
358
+
359
+ fun interruptionBegan(reason: String) {
360
+ val s = session ?: return
361
+ if (!pauseOnInterruption || s.interrupted != null) return
362
+ if (s.paused || s.pauseRequested) return // already paused by the user
363
+ s.interrupted = reason
364
+ requestPause(s)
365
+ Log.i(TAG, "music interrupted: $reason")
366
+ listener?.onInterruption(true, reason, false)
367
+ }
368
+
369
+ fun interruptionEnded(reason: String) {
370
+ val s = session ?: return
371
+ if (s.interrupted != reason) return
372
+ s.interrupted = null
373
+ resumeInternal(s)
374
+ Log.i(TAG, "music resumed after $reason")
375
+ listener?.onInterruption(false, reason, true)
376
+ }
377
+
378
+ private fun monitorUsesMedia(): Boolean {
379
+ val am = appContext?.getSystemService(Context.AUDIO_SERVICE) as? AudioManager ?: return true
380
+ // A VOICE_COMMUNICATION track in MODE_NORMAL comes out of the earpiece,
381
+ // a media track in MODE_IN_COMMUNICATION may take another route: follow
382
+ // the room's mode.
383
+ return am.mode != AudioManager.MODE_IN_COMMUNICATION
384
+ }
385
+
386
+ // ---- capture-thread hot path ----
387
+
388
+ @Volatile private var mixFailureLogged = false
389
+ @Volatile private var postFailureLogged = false
390
+
391
+ /**
392
+ * APM capturePostProcessing, after the voice effect (GxNoiseProcessor.process):
393
+ * [buffer] = [numFrames] int16-scale floats, [channels] = APM channels
394
+ * (0 unknown). Adds the music handed over by the capture callback, if any.
395
+ */
396
+ fun onPostProcess(buffer: ByteBuffer, numFrames: Int, channels: Int) {
397
+ try {
398
+ router.post(buffer, numFrames, channels)
399
+ } catch (e: Throwable) {
400
+ if (!postFailureLogged) {
401
+ postFailureLogged = true
402
+ Log.e(TAG, "post mix failed, passing the frame through", e)
403
+ }
404
+ }
405
+ }
406
+
407
+ override fun onBuffer(
408
+ buffer: ByteBuffer,
409
+ audioFormat: Int,
410
+ channelCount: Int,
411
+ sampleRate: Int,
412
+ bytesRead: Int,
413
+ captureTimeNs: Long,
414
+ ): Long {
415
+ // An exception here would kill WebRTC's record thread, and the app with
416
+ // it: whatever goes wrong, the capture goes on unmixed.
417
+ return try {
418
+ mixInto(buffer, audioFormat, channelCount, sampleRate, bytesRead, captureTimeNs)
419
+ } catch (e: Throwable) {
420
+ if (!mixFailureLogged) {
421
+ mixFailureLogged = true
422
+ Log.e(TAG, "mix failed, passing the capture through", e)
423
+ }
424
+ captureTimeNs
425
+ } finally {
426
+ // bytesRead == 0 only comes from the capture seam's released path (GxRecorderSeam: the
427
+ // hardware recorder is stopped): nothing blocks there, so this callback is its clock. Keyed on bytesRead alone, never on a
428
+ // session or mute flag: the thread must not free-run whatever state we are in.
429
+ if (bytesRead == 0) pace(buffer.capacity(), channelCount, sampleRate) else nextSilentFrameNs = 0
430
+ }
431
+ }
432
+
433
+ // ---- silent-path clock (record thread only) ----
434
+
435
+ private var nextSilentFrameNs = 0L
436
+
437
+ /** Frames delivered through the no-AudioRecord path (diagnostics, getState). */
438
+ @Volatile var silentFrames: Long = 0; private set
439
+
440
+ /**
441
+ * Holds the record thread to real time on the no-AudioRecord path: absolute deadlines one
442
+ * buffer (10 ms) apart, re-synced when far behind (a GC pause) or ahead.
443
+ */
444
+ private fun pace(capacityBytes: Int, channelCount: Int, sampleRate: Int) {
445
+ val frames = capacityBytes / 2 / channelCount.coerceAtLeast(1)
446
+ val frameNs = if (sampleRate > 0 && frames > 0) frames * 1_000_000_000L / sampleRate else 10_000_000L
447
+ val now = System.nanoTime()
448
+ if (nextSilentFrameNs == 0L || now - nextSilentFrameNs > 10 * frameNs || nextSilentFrameNs - now > 2 * frameNs) {
449
+ nextSilentFrameNs = now
450
+ }
451
+ val wait = nextSilentFrameNs - now
452
+ if (wait > 0) java.util.concurrent.locks.LockSupport.parkNanos(wait)
453
+ nextSilentFrameNs += frameNs
454
+ silentFrames++
455
+ }
456
+
457
+ /** Zeroes the whole buffer with absolute puts (position untouched, no allocation). */
458
+ private fun zero(buffer: ByteBuffer) {
459
+ val n = buffer.capacity()
460
+ var i = 0
461
+ while (i + 8 <= n) { buffer.putLong(i, 0L); i += 8 }
462
+ while (i < n) { buffer.put(i, 0); i++ }
463
+ }
464
+
465
+ private fun mixInto(
466
+ buffer: ByteBuffer,
467
+ audioFormat: Int,
468
+ channelCount: Int,
469
+ sampleRate: Int,
470
+ bytesRead: Int,
471
+ captureTimeNs: Long,
472
+ ): Long {
473
+ var ts = captureTimeNs
474
+ // HARD RULE (0.12.2): while the module mute is on no voice sample reaches the mix, whatever
475
+ // the record thread did (WebRtcAudioRecord zeroes only full reads, run() 518-571)
476
+ if (captureMuted) zero(buffer)
477
+ downstream?.let { d ->
478
+ ts = try { d.onBuffer(buffer, audioFormat, channelCount, sampleRate, bytesRead, captureTimeNs) }
479
+ catch (e: Throwable) { captureTimeNs }
480
+ }
481
+ captureSampleRate = sampleRate
482
+ captureChannels = channelCount
483
+ captureSeen = true
484
+ lastCaptureMs = SystemClock.elapsedRealtime()
485
+
486
+ val s = session ?: return ts
487
+ if (audioFormat != AudioFormat.ENCODING_PCM_16BIT) return ts
488
+ if (captureMuted && holdOnMute) return ts
489
+
490
+ // WebRTC delivers buffer.capacity() bytes after this callback,
491
+ // regardless of bytesRead, so operate across the full capacity.
492
+ val samples = buffer.capacity() / 2
493
+ // A view over the WHOLE buffer: the record thread may hand it over with
494
+ // its position moved (the module mute rewrites it with a relative put:
495
+ // field 2026-10-05, BufferUnderflowException on the first muted frame).
496
+ val sb = MusicMixKernel.wholeShortView(buffer)
497
+ val frames = samples / channelCount.coerceAtLeast(1)
498
+ val bps = if (frames > 0) (sampleRate / frames).coerceAtLeast(1) else 100
499
+ router.pre(sb, samples, channelCount, sampleRate, bps, s.track)
500
+ return ts
501
+ }
502
+ }
503
+
504
+ /** Single-producer / single-consumer circular PCM buffer (16-bit samples). */
505
+ class PcmRingBuffer(capacitySamples: Int) {
506
+ private val buf = ShortArray(capacitySamples)
507
+ private val lock = Object()
508
+ private var readPos = 0
509
+ private var writePos = 0
510
+ private var count = 0
511
+ @Volatile var closed = false
512
+
513
+ val capacity: Int get() = buf.size
514
+ fun available(): Int = synchronized(lock) { count }
515
+
516
+ /** Blocking write from the decoder thread. */
517
+ fun write(src: ShortArray, offset: Int, len: Int) {
518
+ var off = offset
519
+ var remaining = len
520
+ while (remaining > 0 && !closed) {
521
+ synchronized(lock) {
522
+ while (count == buf.size && !closed) lock.wait(100)
523
+ if (closed) return
524
+ val n = minOf(remaining, buf.size - count, buf.size - writePos)
525
+ System.arraycopy(src, off, buf, writePos, n)
526
+ writePos = (writePos + n) % buf.size
527
+ count += n
528
+ off += n
529
+ remaining -= n
530
+ lock.notifyAll()
531
+ }
532
+ }
533
+ }
534
+
535
+ /** Non-blocking read from the capture thread. Returns samples read. */
536
+ fun read(dst: ShortArray, len: Int): Int {
537
+ synchronized(lock) {
538
+ val n = minOf(len, count)
539
+ var copied = 0
540
+ while (copied < n) {
541
+ val chunk = minOf(n - copied, buf.size - readPos)
542
+ System.arraycopy(buf, readPos, dst, copied, chunk)
543
+ readPos = (readPos + chunk) % buf.size
544
+ copied += chunk
545
+ }
546
+ count -= n
547
+ lock.notifyAll()
548
+ return n
549
+ }
550
+ }
551
+
552
+ /** Drops all buffered samples and wakes a blocked writer (used by seek). */
553
+ fun clear() {
554
+ synchronized(lock) {
555
+ readPos = 0
556
+ writePos = 0
557
+ count = 0
558
+ lock.notifyAll()
559
+ }
560
+ }
561
+
562
+ fun close() {
563
+ closed = true
564
+ synchronized(lock) { lock.notifyAll() }
565
+ }
566
+ }
567
+
568
+ /**
569
+ * Local playback so the HOST hears the music, fed the exact samples mixed into
570
+ * the outgoing track (no drift). Its usage follows the room's audio mode (see
571
+ * MusicMixerEngine.monitorUsesMedia): a monitor on the wrong stream lands on a
572
+ * different output at a different volume.
573
+ */
574
+ class MonitorPlayer private constructor(private val track: AudioTrack) {
575
+ fun write(music: ShortArray, samples: Int) {
576
+ track.write(music, 0, samples, AudioTrack.WRITE_NON_BLOCKING)
577
+ }
578
+
579
+ fun release() = runCatching { track.pause(); track.flush(); track.stop(); track.release() }
580
+
581
+ companion object {
582
+ fun create(sampleRate: Int, channels: Int, mediaUsage: Boolean): MonitorPlayer? = try {
583
+ val channelMask =
584
+ if (channels >= 2) AudioFormat.CHANNEL_OUT_STEREO
585
+ else AudioFormat.CHANNEL_OUT_MONO
586
+ val minBuf = AudioTrack.getMinBufferSize(
587
+ sampleRate, channelMask, AudioFormat.ENCODING_PCM_16BIT)
588
+ val attrs = if (mediaUsage) {
589
+ AudioAttributes.Builder()
590
+ .setUsage(AudioAttributes.USAGE_MEDIA)
591
+ .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
592
+ .build()
593
+ } else {
594
+ AudioAttributes.Builder()
595
+ .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)
596
+ .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
597
+ .build()
598
+ }
599
+ val track = AudioTrack.Builder()
600
+ .setAudioAttributes(attrs)
601
+ .setAudioFormat(
602
+ AudioFormat.Builder()
603
+ .setEncoding(AudioFormat.ENCODING_PCM_16BIT)
604
+ .setSampleRate(sampleRate)
605
+ .setChannelMask(channelMask)
606
+ .build())
607
+ .setBufferSizeInBytes(minBuf * 4)
608
+ .setTransferMode(AudioTrack.MODE_STREAM)
609
+ .build()
610
+ track.play()
611
+ MonitorPlayer(track)
612
+ } catch (e: Exception) {
613
+ Log.w("GxMusicMonitor", "monitor unavailable: $e")
614
+ null
615
+ }
616
+ }
617
+ }