diff --git a/packages/react-native-audio-api/RNAudioAPI.podspec b/packages/react-native-audio-api/RNAudioAPI.podspec index 5cfe758ce..6b13109a7 100644 --- a/packages/react-native-audio-api/RNAudioAPI.podspec +++ b/packages/react-native-audio-api/RNAudioAPI.podspec @@ -12,6 +12,7 @@ version_flag = "-DAUDIOAPI_VERSION=#{package_json['version']}" worklets_preprocessor_flag = check_if_worklets_enabled() ? '-DRN_AUDIO_API_ENABLE_WORKLETS=1' : '' ffmpeg_flag = $RN_AUDIO_API_FFMPEG_DISABLED ? '-DRN_AUDIO_API_FFMPEG_DISABLED=1' : '' +skip_ffmpeg_argument = $RN_AUDIO_API_FFMPEG_DISABLED ? 'skipffmpeg' : '' Pod::Spec.new do |s| s.name = "RNAudioAPI" @@ -50,7 +51,7 @@ Pod::Spec.new do |s| s.prepare_command = <<-CMD chmod +x scripts/download-prebuilt-binaries.sh - scripts/download-prebuilt-binaries.sh ios + scripts/download-prebuilt-binaries.sh ios #{skip_ffmpeg_argument} CMD # Assumes Pods dir is nested under ios project dir diff --git a/packages/react-native-audio-api/android/build.gradle b/packages/react-native-audio-api/android/build.gradle index cb82f4b8c..49b6f519b 100644 --- a/packages/react-native-audio-api/android/build.gradle +++ b/packages/react-native-audio-api/android/build.gradle @@ -155,8 +155,7 @@ android { buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString() buildConfigField "boolean", "RN_AUDIO_API_ENABLE_WORKLETS", "${isWorkletsAvailable}" - - + buildConfigField "boolean", "RN_AUDIO_API_FFMPEG_DISABLED", isFFmpegDisabled().toString() externalNativeBuild { cmake { @@ -310,7 +309,7 @@ def assertMinimalReactNativeVersion = task assertMinimalReactNativeVersionTask { task downloadPrebuiltBinaries(type: Exec) { commandLine 'chmod', '+x', '../scripts/download-prebuilt-binaries.sh' commandLine 'bash', '../scripts/download-prebuilt-binaries.sh' - args 'android' + args 'android', isFFmpegDisabled() ? 'skipffmpeg' : '' } // Make preBuild depend on the download task diff --git a/packages/react-native-audio-api/android/src/main/cpp/audioapi/CMakeLists.txt b/packages/react-native-audio-api/android/src/main/cpp/audioapi/CMakeLists.txt index b41ddcac2..4130fa00a 100644 --- a/packages/react-native-audio-api/android/src/main/cpp/audioapi/CMakeLists.txt +++ b/packages/react-native-audio-api/android/src/main/cpp/audioapi/CMakeLists.txt @@ -3,6 +3,12 @@ cmake_minimum_required(VERSION 3.12.0) file(GLOB_RECURSE ANDROID_CPP_SOURCES CONFIGURE_DEPENDS "${ANDROID_CPP_DIR}/audioapi/*.cpp") file(GLOB_RECURSE COMMON_CPP_SOURCES CONFIGURE_DEPENDS "${COMMON_CPP_DIR}/audioapi/*.cpp" "${COMMON_CPP_DIR}/audioapi/*.c") +if (RN_AUDIO_API_FFMPEG_DISABLED) + list(REMOVE_ITEM COMMON_CPP_SOURCES + "${COMMON_CPP_DIR}/audioapi/libs/ffmpeg/FFmpegDecoding.cpp" + ) +endif() + set_source_files_properties( ${COMMON_CPP_SOURCES}/dsp/*.cpp PROPERTIES @@ -21,10 +27,12 @@ foreach(lib IN ITEMS opus opusfile ogg vorbis vorbisenc vorbisfile crypto ssl) set_target_properties(${lib} PROPERTIES IMPORTED_LOCATION ${EXTERNAL_DIR}/${ANDROID_ABI}/lib${lib}.a) endforeach() - foreach (lib IN ITEMS avcodec avformat avutil swresample) - add_library(${lib} SHARED IMPORTED) - set_target_properties(${lib} PROPERTIES IMPORTED_LOCATION ${JNI_LIBS_DIR}/${ANDROID_ABI}/lib${lib}.so) - endforeach() +if (NOT RN_AUDIO_API_FFMPEG_DISABLED) + foreach (lib IN ITEMS avcodec avformat avutil swresample) + add_library(${lib} SHARED IMPORTED) + set_target_properties(${lib} PROPERTIES IMPORTED_LOCATION ${JNI_LIBS_DIR}/${ANDROID_ABI}/lib${lib}.so) + endforeach() +endif() find_package(ReactAndroid REQUIRED CONFIG) find_package(fbjni REQUIRED CONFIG) @@ -120,6 +128,13 @@ else() ) endif() +if (RN_AUDIO_API_FFMPEG_DISABLED) + target_compile_definitions( + react-native-audio-api + PRIVATE RN_AUDIO_API_FFMPEG_DISABLED=1 + ) +endif() + target_link_libraries(react-native-audio-api ${LINK_LIBRARIES} ${RN_VERSION_LINK_LIBRARIES} @@ -129,11 +144,16 @@ target_link_libraries(react-native-audio-api vorbis vorbisenc vorbisfile - avcodec - avformat - avutil - swresample crypto ssl z ) + +if(NOT RN_AUDIO_API_FFMPEG_DISABLED) + target_link_libraries(react-native-audio-api + avcodec + avformat + avutil + swresample + ) +endif() diff --git a/packages/react-native-audio-api/common/cpp/audioapi/utils/SpscChannel.hpp b/packages/react-native-audio-api/common/cpp/audioapi/utils/SpscChannel.hpp index b51e71875..e4a0c4696 100644 --- a/packages/react-native-audio-api/common/cpp/audioapi/utils/SpscChannel.hpp +++ b/packages/react-native-audio-api/common/cpp/audioapi/utils/SpscChannel.hpp @@ -8,6 +8,14 @@ #include #include +#ifdef __cpp_lib_hardware_interference_size +using std::hardware_constructive_interference_size; +using std::hardware_destructive_interference_size; +#else +constexpr std::size_t hardware_constructive_interference_size = 64; +constexpr std::size_t hardware_destructive_interference_size = 64; +#endif + namespace audioapi::channels::spsc { /// @brief Overflow strategy for sender when the channel is full @@ -414,17 +422,17 @@ class InnerChannel { T *buffer_; /// Producer-side data (accessed by sender thread) - alignas(std::hardware_destructive_interference_size) std::atomic sendCursor_{0}; - alignas(std::hardware_destructive_interference_size) size_t rcvCursorCache_{ + alignas(hardware_constructive_interference_size) std::atomic sendCursor_{0}; + alignas(hardware_constructive_interference_size) size_t rcvCursorCache_{ 0}; // reduces cache coherency /// Consumer-side data (accessed by receiver thread) - alignas(std::hardware_destructive_interference_size) std::atomic rcvCursor_{0}; - alignas(std::hardware_destructive_interference_size) size_t sendCursorCache_{ + alignas(hardware_constructive_interference_size) std::atomic rcvCursor_{0}; + alignas(hardware_constructive_interference_size) size_t sendCursorCache_{ 0}; // reduces cache coherency /// Flag indicating if the oldest element is occupied - alignas(std::hardware_destructive_interference_size) std::atomic oldestOccupied_{false}; + alignas(hardware_constructive_interference_size) std::atomic oldestOccupied_{false}; friend class Sender; friend class Receiver; diff --git a/packages/react-native-audio-api/scripts/download-prebuilt-binaries.sh b/packages/react-native-audio-api/scripts/download-prebuilt-binaries.sh index c6d56b151..8c5bc6a24 100755 --- a/packages/react-native-audio-api/scripts/download-prebuilt-binaries.sh +++ b/packages/react-native-audio-api/scripts/download-prebuilt-binaries.sh @@ -18,6 +18,12 @@ else PROJECT_ROOT="$(pwd)" fi +if [ "$2" == "skipffmpeg" ]; then + SKIP_FFMPEG=true +else + SKIP_FFMPEG=false +fi + JNILIBS_DESTINATION="${PROJECT_ROOT}/android/src/main" NORMAL_DESTINATION="${PROJECT_ROOT}/common/cpp/audioapi/external" @@ -28,6 +34,10 @@ for name in "${DOWNLOAD_NAMES[@]}"; do # Get the directory name from the zip name (e.g., "armeabi-v7a.zip" -> "armeabi-v7a") EXTRACTED_DIR_NAME="${name%.zip}" + if [[ ("$EXTRACTED_DIR_NAME" == "ffmpeg_ios" || "$EXTRACTED_DIR_NAME" == "jniLibs") && "$SKIP_FFMPEG" == true ]]; then + continue + fi + # Determine the final output path if [[ "$name" == "jniLibs.zip" ]]; then OUTPUT_DIR="${JNILIBS_DESTINATION}"